aufs4.patch 887 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836
  1. diff -Naur null/Documentation/ABI/testing/debugfs-aufs linux-4.7/Documentation/ABI/testing/debugfs-aufs
  2. --- /dev/null
  3. +++ linux-4.7/Documentation/ABI/testing/debugfs-aufs 2016-08-08 12:57:44.593384875 -0400
  4. @@ -0,0 +1,50 @@
  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>/xino0, xino1 ... xinoN
  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. + When the aufs mount option 'noxino' is specified, it
  43. + will be empty. About XINO files, see the aufs manual.
  44. +
  45. +What: /debug/aufs/si_<id>/xigen
  46. +Date: March 2009
  47. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  48. +Description:
  49. + It shows the consumed blocks by xigen (External Inode
  50. + Generation Table), its block size and file size.
  51. + If CONFIG_AUFS_EXPORT is disabled, this entry will not
  52. + be created.
  53. + When the aufs mount option 'noxino' is specified, it
  54. + will be empty. About XINO files, see the aufs manual.
  55. diff -Naur null/Documentation/ABI/testing/sysfs-aufs linux-4.7/Documentation/ABI/testing/sysfs-aufs
  56. --- /dev/null
  57. +++ linux-4.7/Documentation/ABI/testing/sysfs-aufs 2016-08-08 12:57:44.594384853 -0400
  58. @@ -0,0 +1,31 @@
  59. +What: /sys/fs/aufs/si_<id>/
  60. +Date: March 2009
  61. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  62. +Description:
  63. + Under /sys/fs/aufs, a directory named si_<id> is created
  64. + per aufs mount, where <id> is a unique id generated
  65. + internally.
  66. +
  67. +What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
  68. +Date: March 2009
  69. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  70. +Description:
  71. + It shows the abolute path of a member directory (which
  72. + is called branch) in aufs, and its permission.
  73. +
  74. +What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
  75. +Date: July 2013
  76. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  77. +Description:
  78. + It shows the id of a member directory (which is called
  79. + branch) in aufs.
  80. +
  81. +What: /sys/fs/aufs/si_<id>/xi_path
  82. +Date: March 2009
  83. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  84. +Description:
  85. + It shows the abolute path of XINO (External Inode Number
  86. + Bitmap, Translation Table and Generation Table) file
  87. + even if it is the default path.
  88. + When the aufs mount option 'noxino' is specified, it
  89. + will be empty. About XINO files, see the aufs manual.
  90. diff -Naur null/Documentation/filesystems/aufs/design/01intro.txt linux-4.7/Documentation/filesystems/aufs/design/01intro.txt
  91. --- /dev/null
  92. +++ linux-4.7/Documentation/filesystems/aufs/design/01intro.txt 2016-08-08 12:57:44.595384831 -0400
  93. @@ -0,0 +1,170 @@
  94. +
  95. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  96. +#
  97. +# This program is free software; you can redistribute it and/or modify
  98. +# it under the terms of the GNU General Public License as published by
  99. +# the Free Software Foundation; either version 2 of the License, or
  100. +# (at your option) any later version.
  101. +#
  102. +# This program is distributed in the hope that it will be useful,
  103. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  104. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  105. +# GNU General Public License for more details.
  106. +#
  107. +# You should have received a copy of the GNU General Public License
  108. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  109. +
  110. +Introduction
  111. +----------------------------------------
  112. +
  113. +aufs [ei ju: ef es] | [a u f s]
  114. +1. abbrev. for "advanced multi-layered unification filesystem".
  115. +2. abbrev. for "another unionfs".
  116. +3. abbrev. for "auf das" in German which means "on the" in English.
  117. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  118. + But "Filesystem aufs Filesystem" is hard to understand.
  119. +
  120. +AUFS is a filesystem with features:
  121. +- multi layered stackable unification filesystem, the member directory
  122. + is called as a branch.
  123. +- branch permission and attribute, 'readonly', 'real-readonly',
  124. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  125. + combination.
  126. +- internal "file copy-on-write".
  127. +- logical deletion, whiteout.
  128. +- dynamic branch manipulation, adding, deleting and changing permission.
  129. +- allow bypassing aufs, user's direct branch access.
  130. +- external inode number translation table and bitmap which maintains the
  131. + persistent aufs inode number.
  132. +- seekable directory, including NFS readdir.
  133. +- file mapping, mmap and sharing pages.
  134. +- pseudo-link, hardlink over branches.
  135. +- loopback mounted filesystem as a branch.
  136. +- several policies to select one among multiple writable branches.
  137. +- revert a single systemcall when an error occurs in aufs.
  138. +- and more...
  139. +
  140. +
  141. +Multi Layered Stackable Unification Filesystem
  142. +----------------------------------------------------------------------
  143. +Most people already knows what it is.
  144. +It is a filesystem which unifies several directories and provides a
  145. +merged single directory. When users access a file, the access will be
  146. +passed/re-directed/converted (sorry, I am not sure which English word is
  147. +correct) to the real file on the member filesystem. The member
  148. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  149. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  150. +readonly branch is handled by creating 'whiteout' on the upper writable
  151. +branch.
  152. +
  153. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  154. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  155. +different approaches to implement the merged-view.
  156. +The former tries putting it into VFS, and the latter implements as a
  157. +separate filesystem.
  158. +(If I misunderstand about these implementations, please let me know and
  159. +I shall correct it. Because it is a long time ago when I read their
  160. +source files last time).
  161. +
  162. +UnionMount's approach will be able to small, but may be hard to share
  163. +branches between several UnionMount since the whiteout in it is
  164. +implemented in the inode on branch filesystem and always
  165. +shared. According to Bharata's post, readdir does not seems to be
  166. +finished yet.
  167. +There are several missing features known in this implementations such as
  168. +- for users, the inode number may change silently. eg. copy-up.
  169. +- link(2) may break by copy-up.
  170. +- read(2) may get an obsoleted filedata (fstat(2) too).
  171. +- fcntl(F_SETLK) may be broken by copy-up.
  172. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  173. + open(O_RDWR).
  174. +
  175. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  176. +merged into mainline. This is another implementation of UnionMount as a
  177. +separated filesystem. All the limitations and known problems which
  178. +UnionMount are equally inherited to "overlay" filesystem.
  179. +
  180. +Unionfs has a longer history. When I started implementing a stackable
  181. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  182. +inode, dentry and file objects and they have an array pointing lower
  183. +same kind objects. After contributing many patches for Unionfs, I
  184. +re-started my project AUFS (Jun 2006).
  185. +
  186. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  187. +implemented my own ideas, approaches and enhancements and it became
  188. +totally different one.
  189. +
  190. +Comparing DM snapshot and fs based implementation
  191. +- the number of bytes to be copied between devices is much smaller.
  192. +- the type of filesystem must be one and only.
  193. +- the fs must be writable, no readonly fs, even for the lower original
  194. + device. so the compression fs will not be usable. but if we use
  195. + loopback mount, we may address this issue.
  196. + for instance,
  197. + mount /cdrom/squashfs.img /sq
  198. + losetup /sq/ext2.img
  199. + losetup /somewhere/cow
  200. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  201. +- it will be difficult (or needs more operations) to extract the
  202. + difference between the original device and COW.
  203. +- DM snapshot-merge may help a lot when users try merging. in the
  204. + fs-layer union, users will use rsync(1).
  205. +
  206. +You may want to read my old paper "Filesystems in LiveCD"
  207. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  208. +
  209. +
  210. +Several characters/aspects/persona of aufs
  211. +----------------------------------------------------------------------
  212. +
  213. +Aufs has several characters, aspects or persona.
  214. +1. a filesystem, callee of VFS helper
  215. +2. sub-VFS, caller of VFS helper for branches
  216. +3. a virtual filesystem which maintains persistent inode number
  217. +4. reader/writer of files on branches such like an application
  218. +
  219. +1. Callee of VFS Helper
  220. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  221. +unlink(2) from an application reaches sys_unlink() kernel function and
  222. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  223. +calls filesystem specific unlink operation. Actually aufs implements the
  224. +unlink operation but it behaves like a redirector.
  225. +
  226. +2. Caller of VFS Helper for Branches
  227. +aufs_unlink() passes the unlink request to the branch filesystem as if
  228. +it were called from VFS. So the called unlink operation of the branch
  229. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  230. +every necessary pre/post operation for the branch filesystem.
  231. +- acquire the lock for the parent dir on a branch
  232. +- lookup in a branch
  233. +- revalidate dentry on a branch
  234. +- mnt_want_write() for a branch
  235. +- vfs_unlink() for a branch
  236. +- mnt_drop_write() for a branch
  237. +- release the lock on a branch
  238. +
  239. +3. Persistent Inode Number
  240. +One of the most important issue for a filesystem is to maintain inode
  241. +numbers. This is particularly important to support exporting a
  242. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  243. +backend block device for its own. But some storage is necessary to
  244. +keep and maintain the inode numbers. It may be a large space and may not
  245. +suit to keep in memory. Aufs rents some space from its first writable
  246. +branch filesystem (by default) and creates file(s) on it. These files
  247. +are created by aufs internally and removed soon (currently) keeping
  248. +opened.
  249. +Note: Because these files are removed, they are totally gone after
  250. + unmounting aufs. It means the inode numbers are not persistent
  251. + across unmount or reboot. I have a plan to make them really
  252. + persistent which will be important for aufs on NFS server.
  253. +
  254. +4. Read/Write Files Internally (copy-on-write)
  255. +Because a branch can be readonly, when you write a file on it, aufs will
  256. +"copy-up" it to the upper writable branch internally. And then write the
  257. +originally requested thing to the file. Generally kernel doesn't
  258. +open/read/write file actively. In aufs, even a single write may cause a
  259. +internal "file copy". This behaviour is very similar to cp(1) command.
  260. +
  261. +Some people may think it is better to pass such work to user space
  262. +helper, instead of doing in kernel space. Actually I am still thinking
  263. +about it. But currently I have implemented it in kernel space.
  264. diff -Naur null/Documentation/filesystems/aufs/design/02struct.txt linux-4.7/Documentation/filesystems/aufs/design/02struct.txt
  265. --- /dev/null
  266. +++ linux-4.7/Documentation/filesystems/aufs/design/02struct.txt 2016-08-08 12:57:44.595384831 -0400
  267. @@ -0,0 +1,258 @@
  268. +
  269. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  270. +#
  271. +# This program is free software; you can redistribute it and/or modify
  272. +# it under the terms of the GNU General Public License as published by
  273. +# the Free Software Foundation; either version 2 of the License, or
  274. +# (at your option) any later version.
  275. +#
  276. +# This program is distributed in the hope that it will be useful,
  277. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  278. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  279. +# GNU General Public License for more details.
  280. +#
  281. +# You should have received a copy of the GNU General Public License
  282. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  283. +
  284. +Basic Aufs Internal Structure
  285. +
  286. +Superblock/Inode/Dentry/File Objects
  287. +----------------------------------------------------------------------
  288. +As like an ordinary filesystem, aufs has its own
  289. +superblock/inode/dentry/file objects. All these objects have a
  290. +dynamically allocated array and store the same kind of pointers to the
  291. +lower filesystem, branch.
  292. +For example, when you build a union with one readwrite branch and one
  293. +readonly, mounted /au, /rw and /ro respectively.
  294. +- /au = /rw + /ro
  295. +- /ro/fileA exists but /rw/fileA
  296. +
  297. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  298. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  299. +- [0] = NULL (because /rw/fileA doesn't exist)
  300. +- [1] = /ro/fileA
  301. +
  302. +This style of an array is essentially same to the aufs
  303. +superblock/inode/dentry/file objects.
  304. +
  305. +Because aufs supports manipulating branches, ie. add/delete/change
  306. +branches dynamically, these objects has its own generation. When
  307. +branches are changed, the generation in aufs superblock is
  308. +incremented. And a generation in other object are compared when it is
  309. +accessed. When a generation in other objects are obsoleted, aufs
  310. +refreshes the internal array.
  311. +
  312. +
  313. +Superblock
  314. +----------------------------------------------------------------------
  315. +Additionally aufs superblock has some data for policies to select one
  316. +among multiple writable branches, XIB files, pseudo-links and kobject.
  317. +See below in detail.
  318. +About the policies which supports copy-down a directory, see
  319. +wbr_policy.txt too.
  320. +
  321. +
  322. +Branch and XINO(External Inode Number Translation Table)
  323. +----------------------------------------------------------------------
  324. +Every branch has its own xino (external inode number translation table)
  325. +file. The xino file is created and unlinked by aufs internally. When two
  326. +members of a union exist on the same filesystem, they share the single
  327. +xino file.
  328. +The struct of a xino file is simple, just a sequence of aufs inode
  329. +numbers which is indexed by the lower inode number.
  330. +In the above sample, assume the inode number of /ro/fileA is i111 and
  331. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  332. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  333. +
  334. +When the inode numbers are not contiguous, the xino file will be sparse
  335. +which has a hole in it and doesn't consume as much disk space as it
  336. +might appear. If your branch filesystem consumes disk space for such
  337. +holes, then you should specify 'xino=' option at mounting aufs.
  338. +
  339. +Aufs has a mount option to free the disk blocks for such holes in XINO
  340. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  341. +meet a problem of disk shortage due to XINO files, then you should try
  342. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  343. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  344. +the holes in XINO files.
  345. +
  346. +Also a writable branch has three kinds of "whiteout bases". All these
  347. +are existed when the branch is joined to aufs, and their names are
  348. +whiteout-ed doubly, so that users will never see their names in aufs
  349. +hierarchy.
  350. +1. a regular file which will be hardlinked to all whiteouts.
  351. +2. a directory to store a pseudo-link.
  352. +3. a directory to store an "orphan"-ed file temporary.
  353. +
  354. +1. Whiteout Base
  355. + When you remove a file on a readonly branch, aufs handles it as a
  356. + logical deletion and creates a whiteout on the upper writable branch
  357. + as a hardlink of this file in order not to consume inode on the
  358. + writable branch.
  359. +2. Pseudo-link Dir
  360. + See below, Pseudo-link.
  361. +3. Step-Parent Dir
  362. + When "fileC" exists on the lower readonly branch only and it is
  363. + opened and removed with its parent dir, and then user writes
  364. + something into it, then aufs copies-up fileC to this
  365. + directory. Because there is no other dir to store fileC. After
  366. + creating a file under this dir, the file is unlinked.
  367. +
  368. +Because aufs supports manipulating branches, ie. add/delete/change
  369. +dynamically, a branch has its own id. When the branch order changes,
  370. +aufs finds the new index by searching the branch id.
  371. +
  372. +
  373. +Pseudo-link
  374. +----------------------------------------------------------------------
  375. +Assume "fileA" exists on the lower readonly branch only and it is
  376. +hardlinked to "fileB" on the branch. When you write something to fileA,
  377. +aufs copies-up it to the upper writable branch. Additionally aufs
  378. +creates a hardlink under the Pseudo-link Directory of the writable
  379. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  380. +simple list. If fileB is read after unlinking fileA, aufs returns
  381. +filedata from the pseudo-link instead of the lower readonly
  382. +branch. Because the pseudo-link is based upon the inode, to keep the
  383. +inode number by xino (see above) is essentially necessary.
  384. +
  385. +All the hardlinks under the Pseudo-link Directory of the writable branch
  386. +should be restored in a proper location later. Aufs provides a utility
  387. +to do this. The userspace helpers executed at remounting and unmounting
  388. +aufs by default.
  389. +During this utility is running, it puts aufs into the pseudo-link
  390. +maintenance mode. In this mode, only the process which began the
  391. +maintenance mode (and its child processes) is allowed to operate in
  392. +aufs. Some other processes which are not related to the pseudo-link will
  393. +be allowed to run too, but the rest have to return an error or wait
  394. +until the maintenance mode ends. If a process already acquires an inode
  395. +mutex (in VFS), it has to return an error.
  396. +
  397. +
  398. +XIB(external inode number bitmap)
  399. +----------------------------------------------------------------------
  400. +Addition to the xino file per a branch, aufs has an external inode number
  401. +bitmap in a superblock object. It is also an internal file such like a
  402. +xino file.
  403. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  404. +not.
  405. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  406. +
  407. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  408. +reduce the number of consumed disk blocks for these files.
  409. +
  410. +
  411. +Virtual or Vertical Dir, and Readdir in Userspace
  412. +----------------------------------------------------------------------
  413. +In order to support multiple layers (branches), aufs readdir operation
  414. +constructs a virtual dir block on memory. For readdir, aufs calls
  415. +vfs_readdir() internally for each dir on branches, merges their entries
  416. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  417. +object. So the file object has its entry list until it is closed. The
  418. +entry list will be updated when the file position is zero and becomes
  419. +obsoleted. This decision is made in aufs automatically.
  420. +
  421. +The dynamically allocated memory block for the name of entries has a
  422. +unit of 512 bytes (by default) and stores the names contiguously (no
  423. +padding). Another block for each entry is handled by kmem_cache too.
  424. +During building dir blocks, aufs creates hash list and judging whether
  425. +the entry is whiteouted by its upper branch or already listed.
  426. +The merged result is cached in the corresponding inode object and
  427. +maintained by a customizable life-time option.
  428. +
  429. +Some people may call it can be a security hole or invite DoS attack
  430. +since the opened and once readdir-ed dir (file object) holds its entry
  431. +list and becomes a pressure for system memory. But I'd say it is similar
  432. +to files under /proc or /sys. The virtual files in them also holds a
  433. +memory page (generally) while they are opened. When an idea to reduce
  434. +memory for them is introduced, it will be applied to aufs too.
  435. +For those who really hate this situation, I've developed readdir(3)
  436. +library which operates this merging in userspace. You just need to set
  437. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  438. +kernel space for readdir(3).
  439. +
  440. +
  441. +Workqueue
  442. +----------------------------------------------------------------------
  443. +Aufs sometimes requires privilege access to a branch. For instance,
  444. +in copy-up/down operation. When a user process is going to make changes
  445. +to a file which exists in the lower readonly branch only, and the mode
  446. +of one of ancestor directories may not be writable by a user
  447. +process. Here aufs copy-up the file with its ancestors and they may
  448. +require privilege to set its owner/group/mode/etc.
  449. +This is a typical case of a application character of aufs (see
  450. +Introduction).
  451. +
  452. +Aufs uses workqueue synchronously for this case. It creates its own
  453. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  454. +passes the request to call mkdir or write (for example), and wait for
  455. +its completion. This approach solves a problem of a signal handler
  456. +simply.
  457. +If aufs didn't adopt the workqueue and changed the privilege of the
  458. +process, then the process may receive the unexpected SIGXFSZ or other
  459. +signals.
  460. +
  461. +Also aufs uses the system global workqueue ("events" kernel thread) too
  462. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  463. +whiteout base and etc. This is unrelated to a privilege.
  464. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  465. +superblock at the beginning, at the same time waits for the completion
  466. +of all queued asynchronous tasks.
  467. +
  468. +
  469. +Whiteout
  470. +----------------------------------------------------------------------
  471. +The whiteout in aufs is very similar to Unionfs's. That is represented
  472. +by its filename. UnionMount takes an approach of a file mode, but I am
  473. +afraid several utilities (find(1) or something) will have to support it.
  474. +
  475. +Basically the whiteout represents "logical deletion" which stops aufs to
  476. +lookup further, but also it represents "dir is opaque" which also stop
  477. +further lookup.
  478. +
  479. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  480. +In order to make several functions in a single systemcall to be
  481. +revertible, aufs adopts an approach to rename a directory to a temporary
  482. +unique whiteouted name.
  483. +For example, in rename(2) dir where the target dir already existed, aufs
  484. +renames the target dir to a temporary unique whiteouted name before the
  485. +actual rename on a branch, and then handles other actions (make it opaque,
  486. +update the attributes, etc). If an error happens in these actions, aufs
  487. +simply renames the whiteouted name back and returns an error. If all are
  488. +succeeded, aufs registers a function to remove the whiteouted unique
  489. +temporary name completely and asynchronously to the system global
  490. +workqueue.
  491. +
  492. +
  493. +Copy-up
  494. +----------------------------------------------------------------------
  495. +It is a well-known feature or concept.
  496. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  497. +internally and makes change to the new file on the upper writable branch.
  498. +When the trigger systemcall does not update the timestamps of the parent
  499. +dir, aufs reverts it after copy-up.
  500. +
  501. +
  502. +Move-down (aufs3.9 and later)
  503. +----------------------------------------------------------------------
  504. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  505. +the lower readonly branch to the upper writable branch when a user
  506. +changes something about the file.
  507. +"Move-down" is an opposite action of copy-up. Basically this action is
  508. +ran manually instead of automatically and internally.
  509. +For desgin and implementation, aufs has to consider these issues.
  510. +- whiteout for the file may exist on the lower branch.
  511. +- ancestor directories may not exist on the lower branch.
  512. +- diropq for the ancestor directories may exist on the upper branch.
  513. +- free space on the lower branch will reduce.
  514. +- another access to the file may happen during moving-down, including
  515. + UDBA (see "Revalidate Dentry and UDBA").
  516. +- the file should not be hard-linked nor pseudo-linked. they should be
  517. + handled by auplink utility later.
  518. +
  519. +Sometimes users want to move-down a file from the upper writable branch
  520. +to the lower readonly or writable branch. For instance,
  521. +- the free space of the upper writable branch is going to run out.
  522. +- create a new intermediate branch between the upper and lower branch.
  523. +- etc.
  524. +
  525. +For this purpose, use "aumvdown" command in aufs-util.git.
  526. diff -Naur null/Documentation/filesystems/aufs/design/03atomic_open.txt linux-4.7/Documentation/filesystems/aufs/design/03atomic_open.txt
  527. --- /dev/null
  528. +++ linux-4.7/Documentation/filesystems/aufs/design/03atomic_open.txt 2016-08-08 12:57:44.595384831 -0400
  529. @@ -0,0 +1,85 @@
  530. +
  531. +# Copyright (C) 2015-2016 Junjiro R. Okajima
  532. +#
  533. +# This program is free software; you can redistribute it and/or modify
  534. +# it under the terms of the GNU General Public License as published by
  535. +# the Free Software Foundation; either version 2 of the License, or
  536. +# (at your option) any later version.
  537. +#
  538. +# This program is distributed in the hope that it will be useful,
  539. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  540. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  541. +# GNU General Public License for more details.
  542. +#
  543. +# You should have received a copy of the GNU General Public License
  544. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  545. +
  546. +Support for a branch who has its ->atomic_open()
  547. +----------------------------------------------------------------------
  548. +The filesystems who implement its ->atomic_open() are not majority. For
  549. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  550. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  551. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  552. +sure whether all filesystems who have ->atomic_open() behave like this,
  553. +but NFSv4 surely returns the error.
  554. +
  555. +In order to support ->atomic_open() for aufs, there are a few
  556. +approaches.
  557. +
  558. +A. Introduce aufs_atomic_open()
  559. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  560. + branch fs.
  561. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  562. + an aufs user Pip Cet's approach
  563. + - calls aufs_create(), VFS finish_open() and notify_change().
  564. + - pass fake-mode to finish_open(), and then correct the mode by
  565. + notify_change().
  566. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  567. + - no aufs_atomic_open().
  568. + - aufs_lookup() registers the TID to an aufs internal object.
  569. + - aufs_create() does nothing when the matching TID is registered, but
  570. + registers the mode.
  571. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  572. + TID is registered.
  573. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  574. + credential
  575. + - no aufs_atomic_open().
  576. + - aufs_create() registers the TID to an internal object. this info
  577. + represents "this process created this file just now."
  578. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  579. + registered TID and re-try open() with superuser's credential.
  580. +
  581. +Pros and cons for each approach.
  582. +
  583. +A.
  584. + - straightforward but highly depends upon VFS internal.
  585. + - the atomic behavaiour is kept.
  586. + - some of parameters such as nameidata are hard to reproduce for
  587. + branch fs.
  588. + - large overhead.
  589. +B.
  590. + - easy to implement.
  591. + - the atomic behavaiour is lost.
  592. +C.
  593. + - the atomic behavaiour is kept.
  594. + - dirty and tricky.
  595. + - VFS checks whether the file is created correctly after calling
  596. + ->create(), which means this approach doesn't work.
  597. +D.
  598. + - easy to implement.
  599. + - the atomic behavaiour is lost.
  600. + - to open a file with superuser's credential and give it to a user
  601. + process is a bad idea, since the file object keeps the credential
  602. + in it. It may affect LSM or something. This approach doesn't work
  603. + either.
  604. +
  605. +The approach A is ideal, but it hard to implement. So here is a
  606. +variation of A, which is to be implemented.
  607. +
  608. +A-1. Introduce aufs_atomic_open()
  609. + - calls branch fs ->atomic_open() if exists. otherwise calls
  610. + vfs_create() and finish_open().
  611. + - the demerit is that the several checks after branch fs
  612. + ->atomic_open() are lost. in the ordinary case, the checks are
  613. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  614. + be implemented in aufs, but not all I am afraid.
  615. diff -Naur null/Documentation/filesystems/aufs/design/03lookup.txt linux-4.7/Documentation/filesystems/aufs/design/03lookup.txt
  616. --- /dev/null
  617. +++ linux-4.7/Documentation/filesystems/aufs/design/03lookup.txt 2016-08-08 12:57:44.595384831 -0400
  618. @@ -0,0 +1,113 @@
  619. +
  620. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  621. +#
  622. +# This program is free software; you can redistribute it and/or modify
  623. +# it under the terms of the GNU General Public License as published by
  624. +# the Free Software Foundation; either version 2 of the License, or
  625. +# (at your option) any later version.
  626. +#
  627. +# This program is distributed in the hope that it will be useful,
  628. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  629. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  630. +# GNU General Public License for more details.
  631. +#
  632. +# You should have received a copy of the GNU General Public License
  633. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  634. +
  635. +Lookup in a Branch
  636. +----------------------------------------------------------------------
  637. +Since aufs has a character of sub-VFS (see Introduction), it operates
  638. +lookup for branches as VFS does. It may be a heavy work. But almost all
  639. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  640. +directly connected to its parent. Digging down the directory hierarchy
  641. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  642. +aufs calls it.
  643. +
  644. +When a branch is a remote filesystem, aufs basically relies upon its
  645. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  646. +them.
  647. +For d_revalidate, aufs implements three levels of revalidate tests. See
  648. +"Revalidate Dentry and UDBA" in detail.
  649. +
  650. +
  651. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  652. +----------------------------------------------------------------------
  653. +Let's try case study.
  654. +- aufs has two branches, upper readwrite and lower readonly.
  655. + /au = /rw + /ro
  656. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  657. +- user invoked "chmod a+rx /au/dirA"
  658. +- the internal copy-up is activated and "/rw/dirA" is created and its
  659. + permission bits are set to world readable.
  660. +- then "/au/dirA" becomes world readable?
  661. +
  662. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  663. +or it may be a natively readonly filesystem. If aufs respects the lower
  664. +branch, it should not respond readdir request from other users. But user
  665. +allowed it by chmod. Should really aufs rejects showing the entries
  666. +under /ro/dirA?
  667. +
  668. +To be honest, I don't have a good solution for this case. So aufs
  669. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  670. +users.
  671. +When dirperm1 is specified, aufs checks only the highest one for the
  672. +directory permission, and shows the entries. Otherwise, as usual, checks
  673. +every dir existing on all branches and rejects the request.
  674. +
  675. +As a side effect, dirperm1 option improves the performance of aufs
  676. +because the number of permission check is reduced when the number of
  677. +branch is many.
  678. +
  679. +
  680. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  681. +----------------------------------------------------------------------
  682. +Generally VFS helpers re-validate a dentry as a part of lookup.
  683. +0. digging down the directory hierarchy.
  684. +1. lock the parent dir by its i_mutex.
  685. +2. lookup the final (child) entry.
  686. +3. revalidate it.
  687. +4. call the actual operation (create, unlink, etc.)
  688. +5. unlock the parent dir
  689. +
  690. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  691. +called. Actually aufs implements it and checks the dentry on a branch is
  692. +still valid.
  693. +But it is not enough. Because aufs has to release the lock for the
  694. +parent dir on a branch at the end of ->lookup() (step 2) and
  695. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  696. +held by VFS.
  697. +If the file on a branch is changed directly, eg. bypassing aufs, after
  698. +aufs released the lock, then the subsequent operation may cause
  699. +something unpleasant result.
  700. +
  701. +This situation is a result of VFS architecture, ->lookup() and
  702. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  703. +design from VFS's point of view. It is just not suitable for sub-VFS
  704. +character in aufs.
  705. +
  706. +Aufs supports such case by three level of revalidation which is
  707. +selectable by user.
  708. +1. Simple Revalidate
  709. + Addition to the native flow in VFS's, confirm the child-parent
  710. + relationship on the branch just after locking the parent dir on the
  711. + branch in the "actual operation" (step 4). When this validation
  712. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  713. + checks the validation of the dentry on branches.
  714. +2. Monitor Changes Internally by Inotify/Fsnotify
  715. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  716. + the dentry on the branch, and returns EBUSY if it finds different
  717. + dentry.
  718. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  719. + during it is in cache. When the event is notified, aufs registers a
  720. + function to kernel 'events' thread by schedule_work(). And the
  721. + function sets some special status to the cached aufs dentry and inode
  722. + private data. If they are not cached, then aufs has nothing to
  723. + do. When the same file is accessed through aufs (step 0-3) later,
  724. + aufs will detect the status and refresh all necessary data.
  725. + In this mode, aufs has to ignore the event which is fired by aufs
  726. + itself.
  727. +3. No Extra Validation
  728. + This is the simplest test and doesn't add any additional revalidation
  729. + test, and skip the revalidation in step 4. It is useful and improves
  730. + aufs performance when system surely hide the aufs branches from user,
  731. + by over-mounting something (or another method).
  732. diff -Naur null/Documentation/filesystems/aufs/design/04branch.txt linux-4.7/Documentation/filesystems/aufs/design/04branch.txt
  733. --- /dev/null
  734. +++ linux-4.7/Documentation/filesystems/aufs/design/04branch.txt 2016-08-08 12:57:44.595384831 -0400
  735. @@ -0,0 +1,74 @@
  736. +
  737. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  738. +#
  739. +# This program is free software; you can redistribute it and/or modify
  740. +# it under the terms of the GNU General Public License as published by
  741. +# the Free Software Foundation; either version 2 of the License, or
  742. +# (at your option) any later version.
  743. +#
  744. +# This program is distributed in the hope that it will be useful,
  745. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  746. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  747. +# GNU General Public License for more details.
  748. +#
  749. +# You should have received a copy of the GNU General Public License
  750. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  751. +
  752. +Branch Manipulation
  753. +
  754. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  755. +and changing its permission/attribute, there are a lot of works to do.
  756. +
  757. +
  758. +Add a Branch
  759. +----------------------------------------------------------------------
  760. +o Confirm the adding dir exists outside of aufs, including loopback
  761. + mount, and its various attributes.
  762. +o Initialize the xino file and whiteout bases if necessary.
  763. + See struct.txt.
  764. +
  765. +o Check the owner/group/mode of the directory
  766. + When the owner/group/mode of the adding directory differs from the
  767. + existing branch, aufs issues a warning because it may impose a
  768. + security risk.
  769. + For example, when a upper writable branch has a world writable empty
  770. + top directory, a malicious user can create any files on the writable
  771. + branch directly, like copy-up and modify manually. If something like
  772. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  773. + writable branch, and the writable branch is world-writable, then a
  774. + malicious guy may create /etc/passwd on the writable branch directly
  775. + and the infected file will be valid in aufs.
  776. + I am afraid it can be a security issue, but aufs can do nothing except
  777. + producing a warning.
  778. +
  779. +
  780. +Delete a Branch
  781. +----------------------------------------------------------------------
  782. +o Confirm the deleting branch is not busy
  783. + To be general, there is one merit to adopt "remount" interface to
  784. + manipulate branches. It is to discard caches. At deleting a branch,
  785. + aufs checks the still cached (and connected) dentries and inodes. If
  786. + there are any, then they are all in-use. An inode without its
  787. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  788. +
  789. + For the cached one, aufs checks whether the same named entry exists on
  790. + other branches.
  791. + If the cached one is a directory, because aufs provides a merged view
  792. + to users, as long as one dir is left on any branch aufs can show the
  793. + dir to users. In this case, the branch can be removed from aufs.
  794. + Otherwise aufs rejects deleting the branch.
  795. +
  796. + If any file on the deleting branch is opened by aufs, then aufs
  797. + rejects deleting.
  798. +
  799. +
  800. +Modify the Permission of a Branch
  801. +----------------------------------------------------------------------
  802. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  803. + See struct.txt.
  804. +
  805. +o rw --> ro: Confirm the modifying branch is not busy
  806. + Aufs rejects the request if any of these conditions are true.
  807. + - a file on the branch is mmap-ed.
  808. + - a regular file on the branch is opened for write and there is no
  809. + same named entry on the upper branch.
  810. diff -Naur null/Documentation/filesystems/aufs/design/05wbr_policy.txt linux-4.7/Documentation/filesystems/aufs/design/05wbr_policy.txt
  811. --- /dev/null
  812. +++ linux-4.7/Documentation/filesystems/aufs/design/05wbr_policy.txt 2016-08-08 12:57:44.595384831 -0400
  813. @@ -0,0 +1,64 @@
  814. +
  815. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  816. +#
  817. +# This program is free software; you can redistribute it and/or modify
  818. +# it under the terms of the GNU General Public License as published by
  819. +# the Free Software Foundation; either version 2 of the License, or
  820. +# (at your option) any later version.
  821. +#
  822. +# This program is distributed in the hope that it will be useful,
  823. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  824. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  825. +# GNU General Public License for more details.
  826. +#
  827. +# You should have received a copy of the GNU General Public License
  828. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  829. +
  830. +Policies to Select One among Multiple Writable Branches
  831. +----------------------------------------------------------------------
  832. +When the number of writable branch is more than one, aufs has to decide
  833. +the target branch for file creation or copy-up. By default, the highest
  834. +writable branch which has the parent (or ancestor) dir of the target
  835. +file is chosen (top-down-parent policy).
  836. +By user's request, aufs implements some other policies to select the
  837. +writable branch, for file creation several policies, round-robin,
  838. +most-free-space, and other policies. For copy-up, top-down-parent,
  839. +bottom-up-parent, bottom-up and others.
  840. +
  841. +As expected, the round-robin policy selects the branch in circular. When
  842. +you have two writable branches and creates 10 new files, 5 files will be
  843. +created for each branch. mkdir(2) systemcall is an exception. When you
  844. +create 10 new directories, all will be created on the same branch.
  845. +And the most-free-space policy selects the one which has most free
  846. +space among the writable branches. The amount of free space will be
  847. +checked by aufs internally, and users can specify its time interval.
  848. +
  849. +The policies for copy-up is more simple,
  850. +top-down-parent is equivalent to the same named on in create policy,
  851. +bottom-up-parent selects the writable branch where the parent dir
  852. +exists and the nearest upper one from the copyup-source,
  853. +bottom-up selects the nearest upper writable branch from the
  854. +copyup-source, regardless the existence of the parent dir.
  855. +
  856. +There are some rules or exceptions to apply these policies.
  857. +- If there is a readonly branch above the policy-selected branch and
  858. + the parent dir is marked as opaque (a variation of whiteout), or the
  859. + target (creating) file is whiteout-ed on the upper readonly branch,
  860. + then the result of the policy is ignored and the target file will be
  861. + created on the nearest upper writable branch than the readonly branch.
  862. +- If there is a writable branch above the policy-selected branch and
  863. + the parent dir is marked as opaque or the target file is whiteouted
  864. + on the branch, then the result of the policy is ignored and the target
  865. + file will be created on the highest one among the upper writable
  866. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  867. + it as usual.
  868. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  869. + They try selecting the branch where the source exists as possible
  870. + since copyup a large file will take long time. If it can't be,
  871. + ie. the branch where the source exists is readonly, then they will
  872. + follow the copyup policy.
  873. +- There is an exception for rename(2) when the target exists.
  874. + If the rename target exists, aufs compares the index of the branches
  875. + where the source and the target exists and selects the higher
  876. + one. If the selected branch is readonly, then aufs follows the
  877. + copyup policy.
  878. diff -Naur null/Documentation/filesystems/aufs/design/06fhsm.txt linux-4.7/Documentation/filesystems/aufs/design/06fhsm.txt
  879. --- /dev/null
  880. +++ linux-4.7/Documentation/filesystems/aufs/design/06fhsm.txt 2016-08-08 12:57:44.595384831 -0400
  881. @@ -0,0 +1,120 @@
  882. +
  883. +# Copyright (C) 2011-2016 Junjiro R. Okajima
  884. +#
  885. +# This program is free software; you can redistribute it and/or modify
  886. +# it under the terms of the GNU General Public License as published by
  887. +# the Free Software Foundation; either version 2 of the License, or
  888. +# (at your option) any later version.
  889. +#
  890. +# This program is distributed in the hope that it will be useful,
  891. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  892. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  893. +# GNU General Public License for more details.
  894. +#
  895. +# You should have received a copy of the GNU General Public License
  896. +# along with this program; if not, write to the Free Software
  897. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  898. +
  899. +
  900. +File-based Hierarchical Storage Management (FHSM)
  901. +----------------------------------------------------------------------
  902. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  903. +storage world. Aufs provides this feature as file-based with multiple
  904. +writable branches, based upon the principle of "Colder, the Lower".
  905. +Here the word "colder" means that the less used files, and "lower" means
  906. +that the position in the order of the stacked branches vertically.
  907. +These multiple writable branches are prioritized, ie. the topmost one
  908. +should be the fastest drive and be used heavily.
  909. +
  910. +o Characters in aufs FHSM story
  911. +- aufs itself and a new branch attribute.
  912. +- a new ioctl interface to move-down and to establish a connection with
  913. + the daemon ("move-down" is a converse of "copy-up").
  914. +- userspace tool and daemon.
  915. +
  916. +The userspace daemon establishes a connection with aufs and waits for
  917. +the notification. The notified information is very similar to struct
  918. +statfs containing the number of consumed blocks and inodes.
  919. +When the consumed blocks/inodes of a branch exceeds the user-specified
  920. +upper watermark, the daemon activates its move-down process until the
  921. +consumed blocks/inodes reaches the user-specified lower watermark.
  922. +
  923. +The actual move-down is done by aufs based upon the request from
  924. +user-space since we need to maintain the inode number and the internal
  925. +pointer arrays in aufs.
  926. +
  927. +Currently aufs FHSM handles the regular files only. Additionally they
  928. +must not be hard-linked nor pseudo-linked.
  929. +
  930. +
  931. +o Cowork of aufs and the user-space daemon
  932. + During the userspace daemon established the connection, aufs sends a
  933. + small notification to it whenever aufs writes something into the
  934. + writable branch. But it may cost high since aufs issues statfs(2)
  935. + internally. So user can specify a new option to cache the
  936. + info. Actually the notification is controlled by these factors.
  937. + + the specified cache time.
  938. + + classified as "force" by aufs internally.
  939. + Until the specified time expires, aufs doesn't send the info
  940. + except the forced cases. When aufs decide forcing, the info is always
  941. + notified to userspace.
  942. + For example, the number of free inodes is generally large enough and
  943. + the shortage of it happens rarely. So aufs doesn't force the
  944. + notification when creating a new file, directory and others. This is
  945. + the typical case which aufs doesn't force.
  946. + When aufs writes the actual filedata and the files consumes any of new
  947. + blocks, the aufs forces notifying.
  948. +
  949. +
  950. +o Interfaces in aufs
  951. +- New branch attribute.
  952. + + fhsm
  953. + Specifies that the branch is managed by FHSM feature. In other word,
  954. + participant in the FHSM.
  955. + When nofhsm is set to the branch, it will not be the source/target
  956. + branch of the move-down operation. This attribute is set
  957. + independently from coo and moo attributes, and if you want full
  958. + FHSM, you should specify them as well.
  959. +- New mount option.
  960. + + fhsm_sec
  961. + Specifies a second to suppress many less important info to be
  962. + notified.
  963. +- New ioctl.
  964. + + AUFS_CTL_FHSM_FD
  965. + create a new file descriptor which userspace can read the notification
  966. + (a subset of struct statfs) from aufs.
  967. +- Module parameter 'brs'
  968. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  969. + be set.
  970. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  971. + When there are two or more branches with fhsm attributes,
  972. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  973. + terminates it. As a result of remounting and branch-manipulation, the
  974. + number of branches with fhsm attribute can be one. In this case,
  975. + /sbin/mount.aufs will terminate the user-space daemon.
  976. +
  977. +
  978. +Finally the operation is done as these steps in kernel-space.
  979. +- make sure that,
  980. + + no one else is using the file.
  981. + + the file is not hard-linked.
  982. + + the file is not pseudo-linked.
  983. + + the file is a regular file.
  984. + + the parent dir is not opaqued.
  985. +- find the target writable branch.
  986. +- make sure the file is not whiteout-ed by the upper (than the target)
  987. + branch.
  988. +- make the parent dir on the target branch.
  989. +- mutex lock the inode on the branch.
  990. +- unlink the whiteout on the target branch (if exists).
  991. +- lookup and create the whiteout-ed temporary name on the target branch.
  992. +- copy the file as the whiteout-ed temporary name on the target branch.
  993. +- rename the whiteout-ed temporary name to the original name.
  994. +- unlink the file on the source branch.
  995. +- maintain the internal pointer array and the external inode number
  996. + table (XINO).
  997. +- maintain the timestamps and other attributes of the parent dir and the
  998. + file.
  999. +
  1000. +And of course, in every step, an error may happen. So the operation
  1001. +should restore the original file state after an error happens.
  1002. diff -Naur null/Documentation/filesystems/aufs/design/06mmap.txt linux-4.7/Documentation/filesystems/aufs/design/06mmap.txt
  1003. --- /dev/null
  1004. +++ linux-4.7/Documentation/filesystems/aufs/design/06mmap.txt 2016-08-08 12:57:44.596384808 -0400
  1005. @@ -0,0 +1,72 @@
  1006. +
  1007. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  1008. +#
  1009. +# This program is free software; you can redistribute it and/or modify
  1010. +# it under the terms of the GNU General Public License as published by
  1011. +# the Free Software Foundation; either version 2 of the License, or
  1012. +# (at your option) any later version.
  1013. +#
  1014. +# This program is distributed in the hope that it will be useful,
  1015. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1016. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1017. +# GNU General Public License for more details.
  1018. +#
  1019. +# You should have received a copy of the GNU General Public License
  1020. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1021. +
  1022. +mmap(2) -- File Memory Mapping
  1023. +----------------------------------------------------------------------
  1024. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1025. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1026. +->mmap().
  1027. +This approach is simple and good, but there is one problem.
  1028. +Under /proc, several entries show the mmapped files by its path (with
  1029. +device and inode number), and the printed path will be the path on the
  1030. +branch fs's instead of virtual aufs's.
  1031. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1032. +user) may expect the path on aufs.
  1033. +
  1034. +To address this issue, aufs adds a new member called vm_prfile in struct
  1035. +vm_area_struct (and struct vm_region). The original vm_file points to
  1036. +the file on the branch fs in order to handle everything correctly as
  1037. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1038. +show-functions in procfs refers to vm_prfile if it is set.
  1039. +Also we need to maintain several other places where touching vm_file
  1040. +such like
  1041. +- fork()/clone() copies vma and the reference count of vm_file is
  1042. + incremented.
  1043. +- merging vma maintains the ref count too.
  1044. +
  1045. +This is not a good approach. It just fakes the printed path. But it
  1046. +leaves all behaviour around f_mapping unchanged. This is surely an
  1047. +advantage.
  1048. +Actually aufs had adopted another complicated approach which calls
  1049. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1050. +approach, aufs met a hard problem and I could not solve it without
  1051. +switching the approach.
  1052. +
  1053. +There may be one more another approach which is
  1054. +- bind-mount the branch-root onto the aufs-root internally
  1055. +- grab the new vfsmount (ie. struct mount)
  1056. +- lazy-umount the branch-root internally
  1057. +- in open(2) the aufs-file, open the branch-file with the hidden
  1058. + vfsmount (instead of the original branch's vfsmount)
  1059. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1060. + but it may be possible from userspace by the mount helper.
  1061. +
  1062. +Adding the internal hidden vfsmount and using it in opening a file, the
  1063. +file path under /proc will be printed correctly. This approach looks
  1064. +smarter, but is not possible I am afraid.
  1065. +- aufs-root may be bind-mount later. when it happens, another hidden
  1066. + vfsmount will be required.
  1067. +- it is hard to get the chance to bind-mount and lazy-umount
  1068. + + in kernel-space, FS can have vfsmount in open(2) via
  1069. + file->f_path, and aufs can know its vfsmount. But several locks are
  1070. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1071. + here, then it may cause a deadlock.
  1072. + + in user-space, bind-mount doesn't invoke the mount helper.
  1073. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1074. + means a new member vm_prinode will be necessary. this is essentially
  1075. + equivalent to vm_prfile described above.
  1076. +
  1077. +I have to give up this "looks-smater" approach.
  1078. diff -Naur null/Documentation/filesystems/aufs/design/06xattr.txt linux-4.7/Documentation/filesystems/aufs/design/06xattr.txt
  1079. --- /dev/null
  1080. +++ linux-4.7/Documentation/filesystems/aufs/design/06xattr.txt 2016-08-08 12:57:44.596384808 -0400
  1081. @@ -0,0 +1,96 @@
  1082. +
  1083. +# Copyright (C) 2014-2016 Junjiro R. Okajima
  1084. +#
  1085. +# This program is free software; you can redistribute it and/or modify
  1086. +# it under the terms of the GNU General Public License as published by
  1087. +# the Free Software Foundation; either version 2 of the License, or
  1088. +# (at your option) any later version.
  1089. +#
  1090. +# This program is distributed in the hope that it will be useful,
  1091. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1092. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1093. +# GNU General Public License for more details.
  1094. +#
  1095. +# You should have received a copy of the GNU General Public License
  1096. +# along with this program; if not, write to the Free Software
  1097. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1098. +
  1099. +
  1100. +Listing XATTR/EA and getting the value
  1101. +----------------------------------------------------------------------
  1102. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1103. +shows the values from the topmost existing file. This behaviour is good
  1104. +for the non-dir entries since the bahaviour exactly matches the shown
  1105. +information. But for the directories, aufs considers all the same named
  1106. +entries on the lower branches. Which means, if one of the lower entry
  1107. +rejects readdir call, then aufs returns an error even if the topmost
  1108. +entry allows it. This behaviour is necessary to respect the branch fs's
  1109. +security, but can make users confused since the user-visible standard
  1110. +attributes don't match the behaviour.
  1111. +To address this issue, aufs has a mount option called dirperm1 which
  1112. +checks the permission for the topmost entry only, and ignores the lower
  1113. +entry's permission.
  1114. +
  1115. +A similar issue can happen around XATTR.
  1116. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1117. +always set. Otherwise these very unpleasant situation would happen.
  1118. +- listxattr(2) may return the duplicated entries.
  1119. +- users may not be able to remove or reset the XATTR forever,
  1120. +
  1121. +
  1122. +XATTR/EA support in the internal (copy,move)-(up,down)
  1123. +----------------------------------------------------------------------
  1124. +Generally the extended attributes of inode are categorized as these.
  1125. +- "security" for LSM and capability.
  1126. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1127. + fs generally.
  1128. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1129. +- "user" for userspace, 'user_xattr' mount option is required for the
  1130. + branch fs generally.
  1131. +
  1132. +Moreover there are some other categories. Aufs handles these rather
  1133. +unpopular categories as the ordinary ones, ie. there is no special
  1134. +condition nor exception.
  1135. +
  1136. +In copy-up, the support for XATTR on the dst branch may differ from the
  1137. +src branch. In this case, the copy-up operation will get an error and
  1138. +the original user operation which triggered the copy-up will fail. It
  1139. +can happen that even all copy-up will fail.
  1140. +When both of src and dst branches support XATTR and if an error occurs
  1141. +during copying XATTR, then the copy-up should fail obviously. That is a
  1142. +good reason and aufs should return an error to userspace. But when only
  1143. +the src branch support that XATTR, aufs should not return an error.
  1144. +For example, the src branch supports ACL but the dst branch doesn't
  1145. +because the dst branch may natively un-support it or temporary
  1146. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1147. +may NOT return an error even if the XATTR is not supported. It is
  1148. +totally up to the branch fs.
  1149. +
  1150. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1151. +fs, then aufs tries removing the just copied entry and returns the error
  1152. +to the userspace. The worst case of this situation will be all copy-up
  1153. +will fail.
  1154. +
  1155. +For the copy-up operation, there two basic approaches.
  1156. +- copy the specified XATTR only (by category above), and return the
  1157. + error unconditionally if it happens.
  1158. +- copy all XATTR, and ignore the error on the specified category only.
  1159. +
  1160. +In order to support XATTR and to implement the correct behaviour, aufs
  1161. +chooses the latter approach and introduces some new branch attributes,
  1162. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1163. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1164. +convenient, "icex" is also provided which means all "icex*" attributes
  1165. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1166. +
  1167. +The meaning of these attributes is to ignore the error from setting
  1168. +XATTR on that branch.
  1169. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1170. +error from the dst branch according to the specified attributes.
  1171. +
  1172. +Some XATTR may have its default value. The default value may come from
  1173. +the parent dir or the environment. If the default value is set at the
  1174. +file creating-time, it will be overwritten by copy-up.
  1175. +Some contradiction may happen I am afraid.
  1176. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1177. +now, aufs implements the branch attributes to ignore the error.
  1178. diff -Naur null/Documentation/filesystems/aufs/design/07export.txt linux-4.7/Documentation/filesystems/aufs/design/07export.txt
  1179. --- /dev/null
  1180. +++ linux-4.7/Documentation/filesystems/aufs/design/07export.txt 2016-08-08 12:57:44.596384808 -0400
  1181. @@ -0,0 +1,58 @@
  1182. +
  1183. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  1184. +#
  1185. +# This program is free software; you can redistribute it and/or modify
  1186. +# it under the terms of the GNU General Public License as published by
  1187. +# the Free Software Foundation; either version 2 of the License, or
  1188. +# (at your option) any later version.
  1189. +#
  1190. +# This program is distributed in the hope that it will be useful,
  1191. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1192. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1193. +# GNU General Public License for more details.
  1194. +#
  1195. +# You should have received a copy of the GNU General Public License
  1196. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1197. +
  1198. +Export Aufs via NFS
  1199. +----------------------------------------------------------------------
  1200. +Here is an approach.
  1201. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1202. + generation.
  1203. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1204. + store it in xigen file.
  1205. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1206. + file. it is necessary even if it is not unlinked, because any data of
  1207. + inode may be changed by UDBA.
  1208. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1209. + build file handle by
  1210. + + branch id (4 bytes)
  1211. + + superblock generation (4 bytes)
  1212. + + inode number (4 or 8 bytes)
  1213. + + parent dir inode number (4 or 8 bytes)
  1214. + + inode generation (4 bytes))
  1215. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1216. + bytes)
  1217. + + file handle for a branch (by exportfs_encode_fh())
  1218. +- fh_to_dentry():
  1219. + + find the index of a branch from its id in handle, and check it is
  1220. + still exist in aufs.
  1221. + + 1st level: get the inode number from handle and search it in cache.
  1222. + + 2nd level: if not found in cache, get the parent inode number from
  1223. + the handle and search it in cache. and then open the found parent
  1224. + dir, find the matching inode number by vfs_readdir() and get its
  1225. + name, and call lookup_one_len() for the target dentry.
  1226. + + 3rd level: if the parent dir is not cached, call
  1227. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1228. + build a pathname of it, convert it a pathname in aufs, call
  1229. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1230. + the 2nd level.
  1231. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1232. + for every branch, but not itself. to get this, (currently) aufs
  1233. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1234. + idea, but I didn't get other approach.
  1235. + + test the generation of the gotten inode.
  1236. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1237. + convert it into ESTALE for NFSD.
  1238. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1239. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1240. diff -Naur null/Documentation/filesystems/aufs/design/08shwh.txt linux-4.7/Documentation/filesystems/aufs/design/08shwh.txt
  1241. --- /dev/null
  1242. +++ linux-4.7/Documentation/filesystems/aufs/design/08shwh.txt 2016-08-08 12:57:44.596384808 -0400
  1243. @@ -0,0 +1,52 @@
  1244. +
  1245. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  1246. +#
  1247. +# This program is free software; you can redistribute it and/or modify
  1248. +# it under the terms of the GNU General Public License as published by
  1249. +# the Free Software Foundation; either version 2 of the License, or
  1250. +# (at your option) any later version.
  1251. +#
  1252. +# This program is distributed in the hope that it will be useful,
  1253. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1254. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1255. +# GNU General Public License for more details.
  1256. +#
  1257. +# You should have received a copy of the GNU General Public License
  1258. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1259. +
  1260. +Show Whiteout Mode (shwh)
  1261. +----------------------------------------------------------------------
  1262. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1263. +them is very useful for users. For instance, creating a new middle layer
  1264. +(branch) by merging existing layers.
  1265. +
  1266. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1267. +When you have three branches,
  1268. +- Bottom: 'system', squashfs (underlying base system), read-only
  1269. +- Middle: 'mods', squashfs, read-only
  1270. +- Top: 'overlay', ram (tmpfs), read-write
  1271. +
  1272. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1273. +the changes made to the system during the session.
  1274. +When larger changes have been made, or smaller changes have accumulated,
  1275. +the size of the saved top layer data grows. At this point, it would be
  1276. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1277. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1278. +restoring save and load speed.
  1279. +
  1280. +This merging is simplified by the use of another aufs mount, of just the
  1281. +two overlay branches using the 'shwh' option.
  1282. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1283. + aufs /livesys/merge_union
  1284. +
  1285. +A merged view of these two branches is then available at
  1286. +/livesys/merge_union, and the new feature is that the whiteouts are
  1287. +visible!
  1288. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1289. +writing to all branches. Also the default mode for all branches is 'ro'.
  1290. +It is now possible to save the combined contents of the two overlay
  1291. +branches to a new squashfs, e.g.:
  1292. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1293. +
  1294. +This new squashfs archive can be stored on the boot device and the
  1295. +initramfs will use it to replace the old one at the next boot.
  1296. diff -Naur null/Documentation/filesystems/aufs/design/10dynop.txt linux-4.7/Documentation/filesystems/aufs/design/10dynop.txt
  1297. --- /dev/null
  1298. +++ linux-4.7/Documentation/filesystems/aufs/design/10dynop.txt 2016-08-08 12:57:44.596384808 -0400
  1299. @@ -0,0 +1,47 @@
  1300. +
  1301. +# Copyright (C) 2010-2016 Junjiro R. Okajima
  1302. +#
  1303. +# This program is free software; you can redistribute it and/or modify
  1304. +# it under the terms of the GNU General Public License as published by
  1305. +# the Free Software Foundation; either version 2 of the License, or
  1306. +# (at your option) any later version.
  1307. +#
  1308. +# This program is distributed in the hope that it will be useful,
  1309. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1310. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1311. +# GNU General Public License for more details.
  1312. +#
  1313. +# You should have received a copy of the GNU General Public License
  1314. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1315. +
  1316. +Dynamically customizable FS operations
  1317. +----------------------------------------------------------------------
  1318. +Generally FS operations (struct inode_operations, struct
  1319. +address_space_operations, struct file_operations, etc.) are defined as
  1320. +"static const", but it never means that FS have only one set of
  1321. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1322. +three sets, one for XIP, for NOBH, and for normal.
  1323. +Since aufs overrides and redirects these operations, sometimes aufs has
  1324. +to change its behaviour according to the branch FS type. More importantly
  1325. +VFS acts differently if a function (member in the struct) is set or
  1326. +not. It means aufs should have several sets of operations and select one
  1327. +among them according to the branch FS definition.
  1328. +
  1329. +In order to solve this problem and not to affect the behaviour of VFS,
  1330. +aufs defines these operations dynamically. For instance, aufs defines
  1331. +dummy direct_IO function for struct address_space_operations, but it may
  1332. +not be set to the address_space_operations actually. When the branch FS
  1333. +doesn't have it, aufs doesn't set it to its address_space_operations
  1334. +while the function definition itself is still alive. So the behaviour
  1335. +itself will not change, and it will return an error when direct_IO is
  1336. +not set.
  1337. +
  1338. +The lifetime of these dynamically generated operation object is
  1339. +maintained by aufs branch object. When the branch is removed from aufs,
  1340. +the reference counter of the object is decremented. When it reaches
  1341. +zero, the dynamically generated operation object will be freed.
  1342. +
  1343. +This approach is designed to support AIO (io_submit), Direct I/O and
  1344. +XIP (DAX) mainly.
  1345. +Currently this approach is applied to address_space_operations for
  1346. +regular files only.
  1347. diff -Naur null/Documentation/filesystems/aufs/README linux-4.7/Documentation/filesystems/aufs/README
  1348. --- /dev/null
  1349. +++ linux-4.7/Documentation/filesystems/aufs/README 2016-08-08 12:57:44.595384831 -0400
  1350. @@ -0,0 +1,392 @@
  1351. +
  1352. +Aufs4 -- advanced multi layered unification filesystem version 4.x
  1353. +http://aufs.sf.net
  1354. +Junjiro R. Okajima
  1355. +
  1356. +
  1357. +0. Introduction
  1358. +----------------------------------------
  1359. +In the early days, aufs was entirely re-designed and re-implemented
  1360. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1361. +improvements and implementations, it becomes totally different from
  1362. +Unionfs while keeping the basic features.
  1363. +Recently, Unionfs Version 2.x series begin taking some of the same
  1364. +approaches to aufs1's.
  1365. +Unionfs is being developed by Professor Erez Zadok at Stony Brook
  1366. +University and his team.
  1367. +
  1368. +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
  1369. +If you want older kernel version support, try aufs2-2.6.git or
  1370. +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
  1371. +
  1372. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1373. + According to Christoph Hellwig, linux rejects all union-type
  1374. + filesystems but UnionMount.
  1375. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1376. +
  1377. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1378. + UnionMount, and he pointed out an issue around a directory mutex
  1379. + lock and aufs addressed it. But it is still unsure whether aufs will
  1380. + be merged (or any other union solution).
  1381. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1382. +
  1383. +
  1384. +1. Features
  1385. +----------------------------------------
  1386. +- unite several directories into a single virtual filesystem. The member
  1387. + directory is called as a branch.
  1388. +- you can specify the permission flags to the branch, which are 'readonly',
  1389. + 'readwrite' and 'whiteout-able.'
  1390. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1391. + readonly branch are modifiable logically.
  1392. +- dynamic branch manipulation, add, del.
  1393. +- etc...
  1394. +
  1395. +Also there are many enhancements in aufs, such as:
  1396. +- test only the highest one for the directory permission (dirperm1)
  1397. +- copyup on open (coo=)
  1398. +- 'move' policy for copy-up between two writable branches, after
  1399. + checking free space.
  1400. +- xattr, acl
  1401. +- readdir(3) in userspace.
  1402. +- keep inode number by external inode number table
  1403. +- keep the timestamps of file/dir in internal copyup operation
  1404. +- seekable directory, supporting NFS readdir.
  1405. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1406. + on branch
  1407. +- do not copyup, nor create a whiteout when it is unnecessary
  1408. +- revert a single systemcall when an error occurs in aufs
  1409. +- remount interface instead of ioctl
  1410. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1411. +- loopback mounted filesystem as a branch
  1412. +- kernel thread for removing the dir who has a plenty of whiteouts
  1413. +- support copyup sparse file (a file which has a 'hole' in it)
  1414. +- default permission flags for branches
  1415. +- selectable permission flags for ro branch, whether whiteout can
  1416. + exist or not
  1417. +- export via NFS.
  1418. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1419. +- support multiple writable branches, some policies to select one
  1420. + among multiple writable branches.
  1421. +- a new semantics for link(2) and rename(2) to support multiple
  1422. + writable branches.
  1423. +- no glibc changes are required.
  1424. +- pseudo hardlink (hardlink over branches)
  1425. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1426. + including NFS or remote filesystem branch.
  1427. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1428. +- and more...
  1429. +
  1430. +Currently these features are dropped temporary from aufs4.
  1431. +See design/08plan.txt in detail.
  1432. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1433. + (robr)
  1434. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1435. +
  1436. +Features or just an idea in the future (see also design/*.txt),
  1437. +- reorder the branch index without del/re-add.
  1438. +- permanent xino files for NFSD
  1439. +- an option for refreshing the opened files after add/del branches
  1440. +- light version, without branch manipulation. (unnecessary?)
  1441. +- copyup in userspace
  1442. +- inotify in userspace
  1443. +- readv/writev
  1444. +
  1445. +
  1446. +2. Download
  1447. +----------------------------------------
  1448. +There are three GIT trees for aufs4, aufs4-linux.git,
  1449. +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
  1450. +"aufs-util.git."
  1451. +While the aufs-util is always necessary, you need either of aufs4-linux
  1452. +or aufs4-standalone.
  1453. +
  1454. +The aufs4-linux tree includes the whole linux mainline GIT tree,
  1455. +git://git.kernel.org/.../torvalds/linux.git.
  1456. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1457. +build aufs4 as an external kernel module.
  1458. +Several extra patches are not included in this tree. Only
  1459. +aufs4-standalone tree contains them. They are described in the later
  1460. +section "Configuration and Compilation."
  1461. +
  1462. +On the other hand, the aufs4-standalone tree has only aufs source files
  1463. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1464. +But you need to apply all aufs patches manually.
  1465. +
  1466. +You will find GIT branches whose name is in form of "aufs4.x" where "x"
  1467. +represents the linux kernel version, "linux-4.x". For instance,
  1468. +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
  1469. +"aufs4.x-rcN" branch.
  1470. +
  1471. +o aufs4-linux tree
  1472. +$ git clone --reference /your/linux/git/tree \
  1473. + git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
  1474. +- if you don't have linux GIT tree, then remove "--reference ..."
  1475. +$ cd aufs4-linux.git
  1476. +$ git checkout origin/aufs4.0
  1477. +
  1478. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1479. +leave the patch-work to GIT.
  1480. +$ cd /your/linux/git/tree
  1481. +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
  1482. +$ git fetch aufs4
  1483. +$ git checkout -b my4.0 v4.0
  1484. +$ (add your local change...)
  1485. +$ git pull aufs4 aufs4.0
  1486. +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
  1487. +- you may need to solve some conflicts between your_changes and
  1488. + aufs4.0. in this case, git-rerere is recommended so that you can
  1489. + solve the similar conflicts automatically when you upgrade to 4.1 or
  1490. + later in the future.
  1491. +
  1492. +o aufs4-standalone tree
  1493. +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
  1494. +$ cd aufs4-standalone.git
  1495. +$ git checkout origin/aufs4.0
  1496. +
  1497. +o aufs-util tree
  1498. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1499. +- note that the public aufs-util.git is on SourceForge instead of
  1500. + GitHUB.
  1501. +$ cd aufs-util.git
  1502. +$ git checkout origin/aufs4.0
  1503. +
  1504. +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1505. +The minor version number, 'x' in '4.x', of aufs may not always
  1506. +follow the minor version number of the kernel.
  1507. +Because changes in the kernel that cause the use of a new
  1508. +minor version number do not always require changes to aufs-util.
  1509. +
  1510. +Since aufs-util has its own minor version number, you may not be
  1511. +able to find a GIT branch in aufs-util for your kernel's
  1512. +exact minor version number.
  1513. +In this case, you should git-checkout the branch for the
  1514. +nearest lower number.
  1515. +
  1516. +For (an unreleased) example:
  1517. +If you are using "linux-4.10" and the "aufs4.10" branch
  1518. +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
  1519. +or something numerically smaller is the branch for your kernel.
  1520. +
  1521. +Also you can view all branches by
  1522. + $ git branch -a
  1523. +
  1524. +
  1525. +3. Configuration and Compilation
  1526. +----------------------------------------
  1527. +Make sure you have git-checkout'ed the correct branch.
  1528. +
  1529. +For aufs4-linux tree,
  1530. +- enable CONFIG_AUFS_FS.
  1531. +- set other aufs configurations if necessary.
  1532. +
  1533. +For aufs4-standalone tree,
  1534. +There are several ways to build.
  1535. +
  1536. +1.
  1537. +- apply ./aufs4-kbuild.patch to your kernel source files.
  1538. +- apply ./aufs4-base.patch too.
  1539. +- apply ./aufs4-mmap.patch too.
  1540. +- apply ./aufs4-standalone.patch too, if you have a plan to set
  1541. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
  1542. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1543. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1544. +- enable CONFIG_AUFS_FS, you can select either
  1545. + =m or =y.
  1546. +- and build your kernel as usual.
  1547. +- install the built kernel.
  1548. + Note: Since linux-3.9, every filesystem module requires an alias
  1549. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1550. + modules.aliases file if you set CONFIG_AUFS_FS=m.
  1551. +- install the header files too by "make headers_install" to the
  1552. + directory where you specify. By default, it is $PWD/usr.
  1553. + "make help" shows a brief note for headers_install.
  1554. +- and reboot your system.
  1555. +
  1556. +2.
  1557. +- module only (CONFIG_AUFS_FS=m).
  1558. +- apply ./aufs4-base.patch to your kernel source files.
  1559. +- apply ./aufs4-mmap.patch too.
  1560. +- apply ./aufs4-standalone.patch too.
  1561. +- build your kernel, don't forget "make headers_install", and reboot.
  1562. +- edit ./config.mk and set other aufs configurations if necessary.
  1563. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1564. + every aufs configurations.
  1565. +- build the module by simple "make".
  1566. + Note: Since linux-3.9, every filesystem module requires an alias
  1567. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1568. + modules.aliases file.
  1569. +- you can specify ${KDIR} make variable which points to your kernel
  1570. + source tree.
  1571. +- install the files
  1572. + + run "make install" to install the aufs module, or copy the built
  1573. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1574. + + run "make install_headers" (instead of headers_install) to install
  1575. + the modified aufs header file (you can specify DESTDIR which is
  1576. + available in aufs standalone version's Makefile only), or copy
  1577. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1578. + you like manually. By default, the target directory is $PWD/usr.
  1579. +- no need to apply aufs4-kbuild.patch, nor copying source files to your
  1580. + kernel source tree.
  1581. +
  1582. +Note: The header file aufs_type.h is necessary to build aufs-util
  1583. + as well as "make headers_install" in the kernel source tree.
  1584. + headers_install is subject to be forgotten, but it is essentially
  1585. + necessary, not only for building aufs-util.
  1586. + You may not meet problems without headers_install in some older
  1587. + version though.
  1588. +
  1589. +And then,
  1590. +- read README in aufs-util, build and install it
  1591. +- note that your distribution may contain an obsoleted version of
  1592. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1593. + utilities, make sure that your compiler refers the correct aufs header
  1594. + file which is built by "make headers_install."
  1595. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1596. + then run "make install_ulib" too. And refer to the aufs manual in
  1597. + detail.
  1598. +
  1599. +There several other patches in aufs4-standalone.git. They are all
  1600. +optional. When you meet some problems, they will help you.
  1601. +- aufs4-loopback.patch
  1602. + Supports a nested loopback mount in a branch-fs. This patch is
  1603. + unnecessary until aufs produces a message like "you may want to try
  1604. + another patch for loopback file".
  1605. +- vfs-ino.patch
  1606. + Modifies a system global kernel internal function get_next_ino() in
  1607. + order to stop assigning 0 for an inode-number. Not directly related to
  1608. + aufs, but recommended generally.
  1609. +- tmpfs-idr.patch
  1610. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1611. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1612. + duplication of inode number, which is important for backup tools and
  1613. + other utilities. When you find aufs XINO files for tmpfs branch
  1614. + growing too much, try this patch.
  1615. +- lockdep-debug.patch
  1616. + Because aufs is not only an ordinary filesystem (callee of VFS), but
  1617. + also a caller of VFS functions for branch filesystems, subclassing of
  1618. + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
  1619. + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
  1620. + need to apply this debug patch to expand several constant values.
  1621. + If don't know what LOCKDEP, then you don't have apply this patch.
  1622. +
  1623. +
  1624. +4. Usage
  1625. +----------------------------------------
  1626. +At first, make sure aufs-util are installed, and please read the aufs
  1627. +manual, aufs.5 in aufs-util.git tree.
  1628. +$ man -l aufs.5
  1629. +
  1630. +And then,
  1631. +$ mkdir /tmp/rw /tmp/aufs
  1632. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1633. +
  1634. +Here is another example. The result is equivalent.
  1635. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1636. + Or
  1637. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1638. +# mount -o remount,append:${HOME} /tmp/aufs
  1639. +
  1640. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1641. +you modify a file under /tmp/aufs, the one on your home directory is
  1642. +not affected, instead the same named file will be newly created under
  1643. +/tmp/rw. And all of your modification to a file will be applied to
  1644. +the one under /tmp/rw. This is called the file based Copy on Write
  1645. +(COW) method.
  1646. +Aufs mount options are described in aufs.5.
  1647. +If you run chroot or something and make your aufs as a root directory,
  1648. +then you need to customize the shutdown script. See the aufs manual in
  1649. +detail.
  1650. +
  1651. +Additionally, there are some sample usages of aufs which are a
  1652. +diskless system with network booting, and LiveCD over NFS.
  1653. +See sample dir in CVS tree on SourceForge.
  1654. +
  1655. +
  1656. +5. Contact
  1657. +----------------------------------------
  1658. +When you have any problems or strange behaviour in aufs, please let me
  1659. +know with:
  1660. +- /proc/mounts (instead of the output of mount(8))
  1661. +- /sys/module/aufs/*
  1662. +- /sys/fs/aufs/* (if you have them)
  1663. +- /debug/aufs/* (if you have them)
  1664. +- linux kernel version
  1665. + if your kernel is not plain, for example modified by distributor,
  1666. + the url where i can download its source is necessary too.
  1667. +- aufs version which was printed at loading the module or booting the
  1668. + system, instead of the date you downloaded.
  1669. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1670. +- kernel configuration or /proc/config.gz (if you have it)
  1671. +- behaviour which you think to be incorrect
  1672. +- actual operation, reproducible one is better
  1673. +- mailto: aufs-users at lists.sourceforge.net
  1674. +
  1675. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1676. +and Feature Requests) on SourceForge. Please join and write to
  1677. +aufs-users ML.
  1678. +
  1679. +
  1680. +6. Acknowledgements
  1681. +----------------------------------------
  1682. +Thanks to everyone who have tried and are using aufs, whoever
  1683. +have reported a bug or any feedback.
  1684. +
  1685. +Especially donators:
  1686. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1687. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1688. + scripts) is making "doubling" donations.
  1689. + Unfortunately I cannot list all of the donators, but I really
  1690. + appreciate.
  1691. + It ends Aug 2010, but the ordinary donation URL is still available.
  1692. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1693. +Dai Itasaka made a donation (2007/8).
  1694. +Chuck Smith made a donation (2008/4, 10 and 12).
  1695. +Henk Schoneveld made a donation (2008/9).
  1696. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1697. +Francois Dupoux made a donation (2008/11).
  1698. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1699. + aufs2 GIT tree (2009/2).
  1700. +William Grant made a donation (2009/3).
  1701. +Patrick Lane made a donation (2009/4).
  1702. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1703. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1704. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1705. +Pavel Pronskiy made a donation (2011/2).
  1706. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1707. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1708. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1709. +11).
  1710. +Sam Liddicott made a donation (2011/9).
  1711. +Era Scarecrow made a donation (2013/4).
  1712. +Bor Ratajc made a donation (2013/4).
  1713. +Alessandro Gorreta made a donation (2013/4).
  1714. +POIRETTE Marc made a donation (2013/4).
  1715. +Alessandro Gorreta made a donation (2013/4).
  1716. +lauri kasvandik made a donation (2013/5).
  1717. +"pemasu from Finland" made a donation (2013/7).
  1718. +The Parted Magic Project made a donation (2013/9 and 11).
  1719. +Pavel Barta made a donation (2013/10).
  1720. +Nikolay Pertsev made a donation (2014/5).
  1721. +James B made a donation (2014/7 and 2015/7).
  1722. +Stefano Di Biase made a donation (2014/8).
  1723. +Daniel Epellei made a donation (2015/1).
  1724. +OmegaPhil made a donation (2016/1).
  1725. +Tomasz Szewczyk made a donation (2016/4).
  1726. +
  1727. +Thank you very much.
  1728. +Donations are always, including future donations, very important and
  1729. +helpful for me to keep on developing aufs.
  1730. +
  1731. +
  1732. +7.
  1733. +----------------------------------------
  1734. +If you are an experienced user, no explanation is needed. Aufs is
  1735. +just a linux filesystem.
  1736. +
  1737. +
  1738. +Enjoy!
  1739. +
  1740. +# Local variables: ;
  1741. +# mode: text;
  1742. +# End: ;
  1743. diff -Naur null/fs/aufs/aufs.h linux-4.7/fs/aufs/aufs.h
  1744. --- /dev/null
  1745. +++ linux-4.7/fs/aufs/aufs.h 2016-08-08 12:57:44.597384786 -0400
  1746. @@ -0,0 +1,59 @@
  1747. +/*
  1748. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  1749. + *
  1750. + * This program, aufs is free software; you can redistribute it and/or modify
  1751. + * it under the terms of the GNU General Public License as published by
  1752. + * the Free Software Foundation; either version 2 of the License, or
  1753. + * (at your option) any later version.
  1754. + *
  1755. + * This program is distributed in the hope that it will be useful,
  1756. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1757. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1758. + * GNU General Public License for more details.
  1759. + *
  1760. + * You should have received a copy of the GNU General Public License
  1761. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1762. + */
  1763. +
  1764. +/*
  1765. + * all header files
  1766. + */
  1767. +
  1768. +#ifndef __AUFS_H__
  1769. +#define __AUFS_H__
  1770. +
  1771. +#ifdef __KERNEL__
  1772. +
  1773. +#define AuStub(type, name, body, ...) \
  1774. + static inline type name(__VA_ARGS__) { body; }
  1775. +
  1776. +#define AuStubVoid(name, ...) \
  1777. + AuStub(void, name, , __VA_ARGS__)
  1778. +#define AuStubInt0(name, ...) \
  1779. + AuStub(int, name, return 0, __VA_ARGS__)
  1780. +
  1781. +#include "debug.h"
  1782. +
  1783. +#include "branch.h"
  1784. +#include "cpup.h"
  1785. +#include "dcsub.h"
  1786. +#include "dbgaufs.h"
  1787. +#include "dentry.h"
  1788. +#include "dir.h"
  1789. +#include "dynop.h"
  1790. +#include "file.h"
  1791. +#include "fstype.h"
  1792. +#include "inode.h"
  1793. +#include "loop.h"
  1794. +#include "module.h"
  1795. +#include "opts.h"
  1796. +#include "rwsem.h"
  1797. +#include "spl.h"
  1798. +#include "super.h"
  1799. +#include "sysaufs.h"
  1800. +#include "vfsub.h"
  1801. +#include "whout.h"
  1802. +#include "wkq.h"
  1803. +
  1804. +#endif /* __KERNEL__ */
  1805. +#endif /* __AUFS_H__ */
  1806. diff -Naur null/fs/aufs/branch.c linux-4.7/fs/aufs/branch.c
  1807. --- /dev/null
  1808. +++ linux-4.7/fs/aufs/branch.c 2016-08-08 12:57:44.597384786 -0400
  1809. @@ -0,0 +1,1409 @@
  1810. +/*
  1811. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  1812. + *
  1813. + * This program, aufs is free software; you can redistribute it and/or modify
  1814. + * it under the terms of the GNU General Public License as published by
  1815. + * the Free Software Foundation; either version 2 of the License, or
  1816. + * (at your option) any later version.
  1817. + *
  1818. + * This program is distributed in the hope that it will be useful,
  1819. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1820. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1821. + * GNU General Public License for more details.
  1822. + *
  1823. + * You should have received a copy of the GNU General Public License
  1824. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1825. + */
  1826. +
  1827. +/*
  1828. + * branch management
  1829. + */
  1830. +
  1831. +#include <linux/compat.h>
  1832. +#include <linux/statfs.h>
  1833. +#include "aufs.h"
  1834. +
  1835. +/*
  1836. + * free a single branch
  1837. + */
  1838. +static void au_br_do_free(struct au_branch *br)
  1839. +{
  1840. + int i;
  1841. + struct au_wbr *wbr;
  1842. + struct au_dykey **key;
  1843. +
  1844. + au_hnotify_fin_br(br);
  1845. +
  1846. + if (br->br_xino.xi_file)
  1847. + fput(br->br_xino.xi_file);
  1848. + mutex_destroy(&br->br_xino.xi_nondir_mtx);
  1849. +
  1850. + AuDebugOn(au_br_count(br));
  1851. + au_br_count_fin(br);
  1852. +
  1853. + wbr = br->br_wbr;
  1854. + if (wbr) {
  1855. + for (i = 0; i < AuBrWh_Last; i++)
  1856. + dput(wbr->wbr_wh[i]);
  1857. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  1858. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  1859. + }
  1860. +
  1861. + if (br->br_fhsm) {
  1862. + au_br_fhsm_fin(br->br_fhsm);
  1863. + au_delayed_kfree(br->br_fhsm);
  1864. + }
  1865. +
  1866. + key = br->br_dykey;
  1867. + for (i = 0; i < AuBrDynOp; i++, key++)
  1868. + if (*key)
  1869. + au_dy_put(*key);
  1870. + else
  1871. + break;
  1872. +
  1873. + /* recursive lock, s_umount of branch's */
  1874. + lockdep_off();
  1875. + path_put(&br->br_path);
  1876. + lockdep_on();
  1877. + if (wbr)
  1878. + au_delayed_kfree(wbr);
  1879. + au_delayed_kfree(br);
  1880. +}
  1881. +
  1882. +/*
  1883. + * frees all branches
  1884. + */
  1885. +void au_br_free(struct au_sbinfo *sbinfo)
  1886. +{
  1887. + aufs_bindex_t bmax;
  1888. + struct au_branch **br;
  1889. +
  1890. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  1891. +
  1892. + bmax = sbinfo->si_bbot + 1;
  1893. + br = sbinfo->si_branch;
  1894. + while (bmax--)
  1895. + au_br_do_free(*br++);
  1896. +}
  1897. +
  1898. +/*
  1899. + * find the index of a branch which is specified by @br_id.
  1900. + */
  1901. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  1902. +{
  1903. + aufs_bindex_t bindex, bbot;
  1904. +
  1905. + bbot = au_sbbot(sb);
  1906. + for (bindex = 0; bindex <= bbot; bindex++)
  1907. + if (au_sbr_id(sb, bindex) == br_id)
  1908. + return bindex;
  1909. + return -1;
  1910. +}
  1911. +
  1912. +/* ---------------------------------------------------------------------- */
  1913. +
  1914. +/*
  1915. + * add a branch
  1916. + */
  1917. +
  1918. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  1919. + struct dentry *h_root)
  1920. +{
  1921. + if (unlikely(h_adding == h_root
  1922. + || au_test_loopback_overlap(sb, h_adding)))
  1923. + return 1;
  1924. + if (h_adding->d_sb != h_root->d_sb)
  1925. + return 0;
  1926. + return au_test_subdir(h_adding, h_root)
  1927. + || au_test_subdir(h_root, h_adding);
  1928. +}
  1929. +
  1930. +/*
  1931. + * returns a newly allocated branch. @new_nbranch is a number of branches
  1932. + * after adding a branch.
  1933. + */
  1934. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  1935. + int perm)
  1936. +{
  1937. + struct au_branch *add_branch;
  1938. + struct dentry *root;
  1939. + struct inode *inode;
  1940. + int err;
  1941. +
  1942. + err = -ENOMEM;
  1943. + root = sb->s_root;
  1944. + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
  1945. + if (unlikely(!add_branch))
  1946. + goto out;
  1947. +
  1948. + err = au_hnotify_init_br(add_branch, perm);
  1949. + if (unlikely(err))
  1950. + goto out_br;
  1951. +
  1952. + if (au_br_writable(perm)) {
  1953. + /* may be freed separately at changing the branch permission */
  1954. + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
  1955. + GFP_NOFS);
  1956. + if (unlikely(!add_branch->br_wbr))
  1957. + goto out_hnotify;
  1958. + }
  1959. +
  1960. + if (au_br_fhsm(perm)) {
  1961. + err = au_fhsm_br_alloc(add_branch);
  1962. + if (unlikely(err))
  1963. + goto out_wbr;
  1964. + }
  1965. +
  1966. + err = au_sbr_realloc(au_sbi(sb), new_nbranch);
  1967. + if (!err)
  1968. + err = au_di_realloc(au_di(root), new_nbranch);
  1969. + if (!err) {
  1970. + inode = d_inode(root);
  1971. + err = au_hinode_realloc(au_ii(inode), new_nbranch);
  1972. + }
  1973. + if (!err)
  1974. + return add_branch; /* success */
  1975. +
  1976. +out_wbr:
  1977. + if (add_branch->br_wbr)
  1978. + au_delayed_kfree(add_branch->br_wbr);
  1979. +out_hnotify:
  1980. + au_hnotify_fin_br(add_branch);
  1981. +out_br:
  1982. + au_delayed_kfree(add_branch);
  1983. +out:
  1984. + return ERR_PTR(err);
  1985. +}
  1986. +
  1987. +/*
  1988. + * test if the branch permission is legal or not.
  1989. + */
  1990. +static int test_br(struct inode *inode, int brperm, char *path)
  1991. +{
  1992. + int err;
  1993. +
  1994. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  1995. + if (!err)
  1996. + goto out;
  1997. +
  1998. + err = -EINVAL;
  1999. + pr_err("write permission for readonly mount or inode, %s\n", path);
  2000. +
  2001. +out:
  2002. + return err;
  2003. +}
  2004. +
  2005. +/*
  2006. + * returns:
  2007. + * 0: success, the caller will add it
  2008. + * plus: success, it is already unified, the caller should ignore it
  2009. + * minus: error
  2010. + */
  2011. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2012. +{
  2013. + int err;
  2014. + aufs_bindex_t bbot, bindex;
  2015. + struct dentry *root, *h_dentry;
  2016. + struct inode *inode, *h_inode;
  2017. +
  2018. + root = sb->s_root;
  2019. + bbot = au_sbbot(sb);
  2020. + if (unlikely(bbot >= 0
  2021. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2022. + err = 1;
  2023. + if (!remount) {
  2024. + err = -EINVAL;
  2025. + pr_err("%s duplicated\n", add->pathname);
  2026. + }
  2027. + goto out;
  2028. + }
  2029. +
  2030. + err = -ENOSPC; /* -E2BIG; */
  2031. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2032. + || AUFS_BRANCH_MAX - 1 <= bbot)) {
  2033. + pr_err("number of branches exceeded %s\n", add->pathname);
  2034. + goto out;
  2035. + }
  2036. +
  2037. + err = -EDOM;
  2038. + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
  2039. + pr_err("bad index %d\n", add->bindex);
  2040. + goto out;
  2041. + }
  2042. +
  2043. + inode = d_inode(add->path.dentry);
  2044. + err = -ENOENT;
  2045. + if (unlikely(!inode->i_nlink)) {
  2046. + pr_err("no existence %s\n", add->pathname);
  2047. + goto out;
  2048. + }
  2049. +
  2050. + err = -EINVAL;
  2051. + if (unlikely(inode->i_sb == sb)) {
  2052. + pr_err("%s must be outside\n", add->pathname);
  2053. + goto out;
  2054. + }
  2055. +
  2056. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2057. + pr_err("unsupported filesystem, %s (%s)\n",
  2058. + add->pathname, au_sbtype(inode->i_sb));
  2059. + goto out;
  2060. + }
  2061. +
  2062. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2063. + pr_err("already stacked, %s (%s)\n",
  2064. + add->pathname, au_sbtype(inode->i_sb));
  2065. + goto out;
  2066. + }
  2067. +
  2068. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2069. + if (unlikely(err))
  2070. + goto out;
  2071. +
  2072. + if (bbot < 0)
  2073. + return 0; /* success */
  2074. +
  2075. + err = -EINVAL;
  2076. + for (bindex = 0; bindex <= bbot; bindex++)
  2077. + if (unlikely(test_overlap(sb, add->path.dentry,
  2078. + au_h_dptr(root, bindex)))) {
  2079. + pr_err("%s is overlapped\n", add->pathname);
  2080. + goto out;
  2081. + }
  2082. +
  2083. + err = 0;
  2084. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2085. + h_dentry = au_h_dptr(root, 0);
  2086. + h_inode = d_inode(h_dentry);
  2087. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2088. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2089. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2090. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2091. + add->pathname,
  2092. + i_uid_read(inode), i_gid_read(inode),
  2093. + (inode->i_mode & S_IALLUGO),
  2094. + i_uid_read(h_inode), i_gid_read(h_inode),
  2095. + (h_inode->i_mode & S_IALLUGO));
  2096. + }
  2097. +
  2098. +out:
  2099. + return err;
  2100. +}
  2101. +
  2102. +/*
  2103. + * initialize or clean the whiteouts for an adding branch
  2104. + */
  2105. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2106. + int new_perm)
  2107. +{
  2108. + int err, old_perm;
  2109. + aufs_bindex_t bindex;
  2110. + struct inode *h_inode;
  2111. + struct au_wbr *wbr;
  2112. + struct au_hinode *hdir;
  2113. + struct dentry *h_dentry;
  2114. +
  2115. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2116. + if (unlikely(err))
  2117. + goto out;
  2118. +
  2119. + wbr = br->br_wbr;
  2120. + old_perm = br->br_perm;
  2121. + br->br_perm = new_perm;
  2122. + hdir = NULL;
  2123. + h_inode = NULL;
  2124. + bindex = au_br_index(sb, br->br_id);
  2125. + if (0 <= bindex) {
  2126. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2127. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  2128. + } else {
  2129. + h_dentry = au_br_dentry(br);
  2130. + h_inode = d_inode(h_dentry);
  2131. + inode_lock_nested(h_inode, AuLsc_I_PARENT);
  2132. + }
  2133. + if (!wbr)
  2134. + err = au_wh_init(br, sb);
  2135. + else {
  2136. + wbr_wh_write_lock(wbr);
  2137. + err = au_wh_init(br, sb);
  2138. + wbr_wh_write_unlock(wbr);
  2139. + }
  2140. + if (hdir)
  2141. + au_hn_inode_unlock(hdir);
  2142. + else
  2143. + inode_unlock(h_inode);
  2144. + vfsub_mnt_drop_write(au_br_mnt(br));
  2145. + br->br_perm = old_perm;
  2146. +
  2147. + if (!err && wbr && !au_br_writable(new_perm)) {
  2148. + au_delayed_kfree(wbr);
  2149. + br->br_wbr = NULL;
  2150. + }
  2151. +
  2152. +out:
  2153. + return err;
  2154. +}
  2155. +
  2156. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2157. + int perm)
  2158. +{
  2159. + int err;
  2160. + struct kstatfs kst;
  2161. + struct au_wbr *wbr;
  2162. +
  2163. + wbr = br->br_wbr;
  2164. + au_rw_init(&wbr->wbr_wh_rwsem);
  2165. + atomic_set(&wbr->wbr_wh_running, 0);
  2166. +
  2167. + /*
  2168. + * a limit for rmdir/rename a dir
  2169. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2170. + */
  2171. + err = vfs_statfs(&br->br_path, &kst);
  2172. + if (unlikely(err))
  2173. + goto out;
  2174. + err = -EINVAL;
  2175. + if (kst.f_namelen >= NAME_MAX)
  2176. + err = au_br_init_wh(sb, br, perm);
  2177. + else
  2178. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2179. + au_br_dentry(br),
  2180. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2181. +
  2182. +out:
  2183. + return err;
  2184. +}
  2185. +
  2186. +/* initialize a new branch */
  2187. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2188. + struct au_opt_add *add)
  2189. +{
  2190. + int err;
  2191. + struct inode *h_inode;
  2192. +
  2193. + err = 0;
  2194. + mutex_init(&br->br_xino.xi_nondir_mtx);
  2195. + br->br_perm = add->perm;
  2196. + br->br_path = add->path; /* set first, path_get() later */
  2197. + spin_lock_init(&br->br_dykey_lock);
  2198. + au_br_count_init(br);
  2199. + atomic_set(&br->br_xino_running, 0);
  2200. + br->br_id = au_new_br_id(sb);
  2201. + AuDebugOn(br->br_id < 0);
  2202. +
  2203. + if (au_br_writable(add->perm)) {
  2204. + err = au_wbr_init(br, sb, add->perm);
  2205. + if (unlikely(err))
  2206. + goto out_err;
  2207. + }
  2208. +
  2209. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2210. + h_inode = d_inode(add->path.dentry);
  2211. + err = au_xino_br(sb, br, h_inode->i_ino,
  2212. + au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
  2213. + if (unlikely(err)) {
  2214. + AuDebugOn(br->br_xino.xi_file);
  2215. + goto out_err;
  2216. + }
  2217. + }
  2218. +
  2219. + sysaufs_br_init(br);
  2220. + path_get(&br->br_path);
  2221. + goto out; /* success */
  2222. +
  2223. +out_err:
  2224. + memset(&br->br_path, 0, sizeof(br->br_path));
  2225. +out:
  2226. + return err;
  2227. +}
  2228. +
  2229. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2230. + struct au_branch *br, aufs_bindex_t bbot,
  2231. + aufs_bindex_t amount)
  2232. +{
  2233. + struct au_branch **brp;
  2234. +
  2235. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2236. +
  2237. + brp = sbinfo->si_branch + bindex;
  2238. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2239. + *brp = br;
  2240. + sbinfo->si_bbot++;
  2241. + if (unlikely(bbot < 0))
  2242. + sbinfo->si_bbot = 0;
  2243. +}
  2244. +
  2245. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2246. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2247. +{
  2248. + struct au_hdentry *hdp;
  2249. +
  2250. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2251. +
  2252. + hdp = au_hdentry(dinfo, bindex);
  2253. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2254. + au_h_dentry_init(hdp);
  2255. + dinfo->di_bbot++;
  2256. + if (unlikely(bbot < 0))
  2257. + dinfo->di_btop = 0;
  2258. +}
  2259. +
  2260. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2261. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2262. +{
  2263. + struct au_hinode *hip;
  2264. +
  2265. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2266. +
  2267. + hip = au_hinode(iinfo, bindex);
  2268. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2269. + au_hinode_init(hip);
  2270. + iinfo->ii_bbot++;
  2271. + if (unlikely(bbot < 0))
  2272. + iinfo->ii_btop = 0;
  2273. +}
  2274. +
  2275. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2276. + aufs_bindex_t bindex)
  2277. +{
  2278. + struct dentry *root, *h_dentry;
  2279. + struct inode *root_inode, *h_inode;
  2280. + aufs_bindex_t bbot, amount;
  2281. +
  2282. + root = sb->s_root;
  2283. + root_inode = d_inode(root);
  2284. + bbot = au_sbbot(sb);
  2285. + amount = bbot + 1 - bindex;
  2286. + h_dentry = au_br_dentry(br);
  2287. + au_sbilist_lock();
  2288. + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
  2289. + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
  2290. + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
  2291. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2292. + h_inode = d_inode(h_dentry);
  2293. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2294. + au_sbilist_unlock();
  2295. +}
  2296. +
  2297. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2298. +{
  2299. + int err;
  2300. + aufs_bindex_t bbot, add_bindex;
  2301. + struct dentry *root, *h_dentry;
  2302. + struct inode *root_inode;
  2303. + struct au_branch *add_branch;
  2304. +
  2305. + root = sb->s_root;
  2306. + root_inode = d_inode(root);
  2307. + IMustLock(root_inode);
  2308. + IiMustWriteLock(root_inode);
  2309. + err = test_add(sb, add, remount);
  2310. + if (unlikely(err < 0))
  2311. + goto out;
  2312. + if (err) {
  2313. + err = 0;
  2314. + goto out; /* success */
  2315. + }
  2316. +
  2317. + bbot = au_sbbot(sb);
  2318. + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
  2319. + err = PTR_ERR(add_branch);
  2320. + if (IS_ERR(add_branch))
  2321. + goto out;
  2322. +
  2323. + err = au_br_init(add_branch, sb, add);
  2324. + if (unlikely(err)) {
  2325. + au_br_do_free(add_branch);
  2326. + goto out;
  2327. + }
  2328. +
  2329. + add_bindex = add->bindex;
  2330. + if (!remount)
  2331. + au_br_do_add(sb, add_branch, add_bindex);
  2332. + else {
  2333. + sysaufs_brs_del(sb, add_bindex);
  2334. + au_br_do_add(sb, add_branch, add_bindex);
  2335. + sysaufs_brs_add(sb, add_bindex);
  2336. + }
  2337. +
  2338. + h_dentry = add->path.dentry;
  2339. + if (!add_bindex) {
  2340. + au_cpup_attr_all(root_inode, /*force*/1);
  2341. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2342. + } else
  2343. + au_add_nlink(root_inode, d_inode(h_dentry));
  2344. +
  2345. + /*
  2346. + * this test/set prevents aufs from handling unnecesary notify events
  2347. + * of xino files, in case of re-adding a writable branch which was
  2348. + * once detached from aufs.
  2349. + */
  2350. + if (au_xino_brid(sb) < 0
  2351. + && au_br_writable(add_branch->br_perm)
  2352. + && !au_test_fs_bad_xino(h_dentry->d_sb)
  2353. + && add_branch->br_xino.xi_file
  2354. + && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
  2355. + au_xino_brid_set(sb, add_branch->br_id);
  2356. +
  2357. +out:
  2358. + return err;
  2359. +}
  2360. +
  2361. +/* ---------------------------------------------------------------------- */
  2362. +
  2363. +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
  2364. + unsigned long long max __maybe_unused,
  2365. + void *arg)
  2366. +{
  2367. + unsigned long long n;
  2368. + struct file **p, *f;
  2369. + struct au_sphlhead *files;
  2370. + struct au_finfo *finfo;
  2371. +
  2372. + n = 0;
  2373. + p = a;
  2374. + files = &au_sbi(sb)->si_files;
  2375. + spin_lock(&files->spin);
  2376. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  2377. + f = finfo->fi_file;
  2378. + if (file_count(f)
  2379. + && !special_file(file_inode(f)->i_mode)) {
  2380. + get_file(f);
  2381. + *p++ = f;
  2382. + n++;
  2383. + AuDebugOn(n > max);
  2384. + }
  2385. + }
  2386. + spin_unlock(&files->spin);
  2387. +
  2388. + return n;
  2389. +}
  2390. +
  2391. +static struct file **au_farray_alloc(struct super_block *sb,
  2392. + unsigned long long *max)
  2393. +{
  2394. + *max = au_nfiles(sb);
  2395. + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
  2396. +}
  2397. +
  2398. +static void au_farray_free(struct file **a, unsigned long long max)
  2399. +{
  2400. + unsigned long long ull;
  2401. +
  2402. + for (ull = 0; ull < max; ull++)
  2403. + if (a[ull])
  2404. + fput(a[ull]);
  2405. + kvfree(a);
  2406. +}
  2407. +
  2408. +/* ---------------------------------------------------------------------- */
  2409. +
  2410. +/*
  2411. + * delete a branch
  2412. + */
  2413. +
  2414. +/* to show the line number, do not make it inlined function */
  2415. +#define AuVerbose(do_info, fmt, ...) do { \
  2416. + if (do_info) \
  2417. + pr_info(fmt, ##__VA_ARGS__); \
  2418. +} while (0)
  2419. +
  2420. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
  2421. + aufs_bindex_t bbot)
  2422. +{
  2423. + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
  2424. +}
  2425. +
  2426. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
  2427. + aufs_bindex_t bbot)
  2428. +{
  2429. + return au_test_ibusy(d_inode(dentry), btop, bbot);
  2430. +}
  2431. +
  2432. +/*
  2433. + * test if the branch is deletable or not.
  2434. + */
  2435. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2436. + unsigned int sigen, const unsigned int verbose)
  2437. +{
  2438. + int err, i, j, ndentry;
  2439. + aufs_bindex_t btop, bbot;
  2440. + struct au_dcsub_pages dpages;
  2441. + struct au_dpage *dpage;
  2442. + struct dentry *d;
  2443. +
  2444. + err = au_dpages_init(&dpages, GFP_NOFS);
  2445. + if (unlikely(err))
  2446. + goto out;
  2447. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2448. + if (unlikely(err))
  2449. + goto out_dpages;
  2450. +
  2451. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2452. + dpage = dpages.dpages + i;
  2453. + ndentry = dpage->ndentry;
  2454. + for (j = 0; !err && j < ndentry; j++) {
  2455. + d = dpage->dentries[j];
  2456. + AuDebugOn(au_dcount(d) <= 0);
  2457. + if (!au_digen_test(d, sigen)) {
  2458. + di_read_lock_child(d, AuLock_IR);
  2459. + if (unlikely(au_dbrange_test(d))) {
  2460. + di_read_unlock(d, AuLock_IR);
  2461. + continue;
  2462. + }
  2463. + } else {
  2464. + di_write_lock_child(d);
  2465. + if (unlikely(au_dbrange_test(d))) {
  2466. + di_write_unlock(d);
  2467. + continue;
  2468. + }
  2469. + err = au_reval_dpath(d, sigen);
  2470. + if (!err)
  2471. + di_downgrade_lock(d, AuLock_IR);
  2472. + else {
  2473. + di_write_unlock(d);
  2474. + break;
  2475. + }
  2476. + }
  2477. +
  2478. + /* AuDbgDentry(d); */
  2479. + btop = au_dbtop(d);
  2480. + bbot = au_dbbot(d);
  2481. + if (btop <= bindex
  2482. + && bindex <= bbot
  2483. + && au_h_dptr(d, bindex)
  2484. + && au_test_dbusy(d, btop, bbot)) {
  2485. + err = -EBUSY;
  2486. + AuVerbose(verbose, "busy %pd\n", d);
  2487. + AuDbgDentry(d);
  2488. + }
  2489. + di_read_unlock(d, AuLock_IR);
  2490. + }
  2491. + }
  2492. +
  2493. +out_dpages:
  2494. + au_dpages_free(&dpages);
  2495. +out:
  2496. + return err;
  2497. +}
  2498. +
  2499. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2500. + unsigned int sigen, const unsigned int verbose)
  2501. +{
  2502. + int err;
  2503. + unsigned long long max, ull;
  2504. + struct inode *i, **array;
  2505. + aufs_bindex_t btop, bbot;
  2506. +
  2507. + array = au_iarray_alloc(sb, &max);
  2508. + err = PTR_ERR(array);
  2509. + if (IS_ERR(array))
  2510. + goto out;
  2511. +
  2512. + err = 0;
  2513. + AuDbg("b%d\n", bindex);
  2514. + for (ull = 0; !err && ull < max; ull++) {
  2515. + i = array[ull];
  2516. + if (unlikely(!i))
  2517. + break;
  2518. + if (i->i_ino == AUFS_ROOT_INO)
  2519. + continue;
  2520. +
  2521. + /* AuDbgInode(i); */
  2522. + if (au_iigen(i, NULL) == sigen)
  2523. + ii_read_lock_child(i);
  2524. + else {
  2525. + ii_write_lock_child(i);
  2526. + err = au_refresh_hinode_self(i);
  2527. + au_iigen_dec(i);
  2528. + if (!err)
  2529. + ii_downgrade_lock(i);
  2530. + else {
  2531. + ii_write_unlock(i);
  2532. + break;
  2533. + }
  2534. + }
  2535. +
  2536. + btop = au_ibtop(i);
  2537. + bbot = au_ibbot(i);
  2538. + if (btop <= bindex
  2539. + && bindex <= bbot
  2540. + && au_h_iptr(i, bindex)
  2541. + && au_test_ibusy(i, btop, bbot)) {
  2542. + err = -EBUSY;
  2543. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2544. + AuDbgInode(i);
  2545. + }
  2546. + ii_read_unlock(i);
  2547. + }
  2548. + au_iarray_free(array, max);
  2549. +
  2550. +out:
  2551. + return err;
  2552. +}
  2553. +
  2554. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2555. + const unsigned int verbose)
  2556. +{
  2557. + int err;
  2558. + unsigned int sigen;
  2559. +
  2560. + sigen = au_sigen(root->d_sb);
  2561. + DiMustNoWaiters(root);
  2562. + IiMustNoWaiters(d_inode(root));
  2563. + di_write_unlock(root);
  2564. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2565. + if (!err)
  2566. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2567. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2568. +
  2569. + return err;
  2570. +}
  2571. +
  2572. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2573. + struct file **to_free, int *idx)
  2574. +{
  2575. + int err;
  2576. + unsigned char matched, root;
  2577. + aufs_bindex_t bindex, bbot;
  2578. + struct au_fidir *fidir;
  2579. + struct au_hfile *hfile;
  2580. +
  2581. + err = 0;
  2582. + root = IS_ROOT(file->f_path.dentry);
  2583. + if (root) {
  2584. + get_file(file);
  2585. + to_free[*idx] = file;
  2586. + (*idx)++;
  2587. + goto out;
  2588. + }
  2589. +
  2590. + matched = 0;
  2591. + fidir = au_fi(file)->fi_hdir;
  2592. + AuDebugOn(!fidir);
  2593. + bbot = au_fbbot_dir(file);
  2594. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
  2595. + hfile = fidir->fd_hfile + bindex;
  2596. + if (!hfile->hf_file)
  2597. + continue;
  2598. +
  2599. + if (hfile->hf_br->br_id == br_id) {
  2600. + matched = 1;
  2601. + break;
  2602. + }
  2603. + }
  2604. + if (matched)
  2605. + err = -EBUSY;
  2606. +
  2607. +out:
  2608. + return err;
  2609. +}
  2610. +
  2611. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2612. + struct file **to_free, int opened)
  2613. +{
  2614. + int err, idx;
  2615. + unsigned long long ull, max;
  2616. + aufs_bindex_t btop;
  2617. + struct file *file, **array;
  2618. + struct dentry *root;
  2619. + struct au_hfile *hfile;
  2620. +
  2621. + array = au_farray_alloc(sb, &max);
  2622. + err = PTR_ERR(array);
  2623. + if (IS_ERR(array))
  2624. + goto out;
  2625. +
  2626. + err = 0;
  2627. + idx = 0;
  2628. + root = sb->s_root;
  2629. + di_write_unlock(root);
  2630. + for (ull = 0; ull < max; ull++) {
  2631. + file = array[ull];
  2632. + if (unlikely(!file))
  2633. + break;
  2634. +
  2635. + /* AuDbg("%pD\n", file); */
  2636. + fi_read_lock(file);
  2637. + btop = au_fbtop(file);
  2638. + if (!d_is_dir(file->f_path.dentry)) {
  2639. + hfile = &au_fi(file)->fi_htop;
  2640. + if (hfile->hf_br->br_id == br_id)
  2641. + err = -EBUSY;
  2642. + } else
  2643. + err = test_dir_busy(file, br_id, to_free, &idx);
  2644. + fi_read_unlock(file);
  2645. + if (unlikely(err))
  2646. + break;
  2647. + }
  2648. + di_write_lock_child(root);
  2649. + au_farray_free(array, max);
  2650. + AuDebugOn(idx > opened);
  2651. +
  2652. +out:
  2653. + return err;
  2654. +}
  2655. +
  2656. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2657. + aufs_bindex_t br_id)
  2658. +{
  2659. + unsigned long long ull;
  2660. + aufs_bindex_t bindex, btop, bbot, bfound;
  2661. + struct file *file;
  2662. + struct au_fidir *fidir;
  2663. + struct au_hfile *hfile;
  2664. +
  2665. + for (ull = 0; ull < opened; ull++) {
  2666. + file = to_free[ull];
  2667. + if (unlikely(!file))
  2668. + break;
  2669. +
  2670. + /* AuDbg("%pD\n", file); */
  2671. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2672. + bfound = -1;
  2673. + fidir = au_fi(file)->fi_hdir;
  2674. + AuDebugOn(!fidir);
  2675. + fi_write_lock(file);
  2676. + btop = au_fbtop(file);
  2677. + bbot = au_fbbot_dir(file);
  2678. + for (bindex = btop; bindex <= bbot; bindex++) {
  2679. + hfile = fidir->fd_hfile + bindex;
  2680. + if (!hfile->hf_file)
  2681. + continue;
  2682. +
  2683. + if (hfile->hf_br->br_id == br_id) {
  2684. + bfound = bindex;
  2685. + break;
  2686. + }
  2687. + }
  2688. + AuDebugOn(bfound < 0);
  2689. + au_set_h_fptr(file, bfound, NULL);
  2690. + if (bfound == btop) {
  2691. + for (btop++; btop <= bbot; btop++)
  2692. + if (au_hf_dir(file, btop)) {
  2693. + au_set_fbtop(file, btop);
  2694. + break;
  2695. + }
  2696. + }
  2697. + fi_write_unlock(file);
  2698. + }
  2699. +}
  2700. +
  2701. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2702. + const aufs_bindex_t bindex,
  2703. + const aufs_bindex_t bbot)
  2704. +{
  2705. + struct au_branch **brp, **p;
  2706. +
  2707. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2708. +
  2709. + brp = sbinfo->si_branch + bindex;
  2710. + if (bindex < bbot)
  2711. + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
  2712. + sbinfo->si_branch[0 + bbot] = NULL;
  2713. + sbinfo->si_bbot--;
  2714. +
  2715. + p = krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST);
  2716. + if (p)
  2717. + sbinfo->si_branch = p;
  2718. + /* harmless error */
  2719. +}
  2720. +
  2721. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2722. + const aufs_bindex_t bbot)
  2723. +{
  2724. + struct au_hdentry *hdp, *p;
  2725. +
  2726. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2727. +
  2728. + hdp = au_hdentry(dinfo, bindex);
  2729. + if (bindex < bbot)
  2730. + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
  2731. + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
  2732. + dinfo->di_bbot--;
  2733. +
  2734. + p = krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST);
  2735. + if (p)
  2736. + dinfo->di_hdentry = p;
  2737. + /* harmless error */
  2738. +}
  2739. +
  2740. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2741. + const aufs_bindex_t bbot)
  2742. +{
  2743. + struct au_hinode *hip, *p;
  2744. +
  2745. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2746. +
  2747. + hip = au_hinode(iinfo, bindex);
  2748. + if (bindex < bbot)
  2749. + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
  2750. + /* au_hinode_init(au_hinode(iinfo, bbot)); */
  2751. + iinfo->ii_bbot--;
  2752. +
  2753. + p = krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST);
  2754. + if (p)
  2755. + iinfo->ii_hinode = p;
  2756. + /* harmless error */
  2757. +}
  2758. +
  2759. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2760. + struct au_branch *br)
  2761. +{
  2762. + aufs_bindex_t bbot;
  2763. + struct au_sbinfo *sbinfo;
  2764. + struct dentry *root, *h_root;
  2765. + struct inode *inode, *h_inode;
  2766. + struct au_hinode *hinode;
  2767. +
  2768. + SiMustWriteLock(sb);
  2769. +
  2770. + root = sb->s_root;
  2771. + inode = d_inode(root);
  2772. + sbinfo = au_sbi(sb);
  2773. + bbot = sbinfo->si_bbot;
  2774. +
  2775. + h_root = au_h_dptr(root, bindex);
  2776. + hinode = au_hi(inode, bindex);
  2777. + h_inode = au_igrab(hinode->hi_inode);
  2778. + au_hiput(hinode);
  2779. +
  2780. + au_sbilist_lock();
  2781. + au_br_do_del_brp(sbinfo, bindex, bbot);
  2782. + au_br_do_del_hdp(au_di(root), bindex, bbot);
  2783. + au_br_do_del_hip(au_ii(inode), bindex, bbot);
  2784. + au_sbilist_unlock();
  2785. +
  2786. + dput(h_root);
  2787. + iput(h_inode);
  2788. + au_br_do_free(br);
  2789. +}
  2790. +
  2791. +static unsigned long long empty_cb(struct super_block *sb, void *array,
  2792. + unsigned long long max, void *arg)
  2793. +{
  2794. + return max;
  2795. +}
  2796. +
  2797. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2798. +{
  2799. + int err, rerr, i;
  2800. + unsigned long long opened;
  2801. + unsigned int mnt_flags;
  2802. + aufs_bindex_t bindex, bbot, br_id;
  2803. + unsigned char do_wh, verbose;
  2804. + struct au_branch *br;
  2805. + struct au_wbr *wbr;
  2806. + struct dentry *root;
  2807. + struct file **to_free;
  2808. +
  2809. + err = 0;
  2810. + opened = 0;
  2811. + to_free = NULL;
  2812. + root = sb->s_root;
  2813. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2814. + if (bindex < 0) {
  2815. + if (remount)
  2816. + goto out; /* success */
  2817. + err = -ENOENT;
  2818. + pr_err("%s no such branch\n", del->pathname);
  2819. + goto out;
  2820. + }
  2821. + AuDbg("bindex b%d\n", bindex);
  2822. +
  2823. + err = -EBUSY;
  2824. + mnt_flags = au_mntflags(sb);
  2825. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2826. + bbot = au_sbbot(sb);
  2827. + if (unlikely(!bbot)) {
  2828. + AuVerbose(verbose, "no more branches left\n");
  2829. + goto out;
  2830. + }
  2831. + br = au_sbr(sb, bindex);
  2832. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  2833. +
  2834. + br_id = br->br_id;
  2835. + opened = au_br_count(br);
  2836. + if (unlikely(opened)) {
  2837. + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
  2838. + err = PTR_ERR(to_free);
  2839. + if (IS_ERR(to_free))
  2840. + goto out;
  2841. +
  2842. + err = test_file_busy(sb, br_id, to_free, opened);
  2843. + if (unlikely(err)) {
  2844. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  2845. + goto out;
  2846. + }
  2847. + }
  2848. +
  2849. + wbr = br->br_wbr;
  2850. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  2851. + if (do_wh) {
  2852. + /* instead of WbrWhMustWriteLock(wbr) */
  2853. + SiMustWriteLock(sb);
  2854. + for (i = 0; i < AuBrWh_Last; i++) {
  2855. + dput(wbr->wbr_wh[i]);
  2856. + wbr->wbr_wh[i] = NULL;
  2857. + }
  2858. + }
  2859. +
  2860. + err = test_children_busy(root, bindex, verbose);
  2861. + if (unlikely(err)) {
  2862. + if (do_wh)
  2863. + goto out_wh;
  2864. + goto out;
  2865. + }
  2866. +
  2867. + err = 0;
  2868. + if (to_free) {
  2869. + /*
  2870. + * now we confirmed the branch is deletable.
  2871. + * let's free the remaining opened dirs on the branch.
  2872. + */
  2873. + di_write_unlock(root);
  2874. + br_del_file(to_free, opened, br_id);
  2875. + di_write_lock_child(root);
  2876. + }
  2877. +
  2878. + if (!remount)
  2879. + au_br_do_del(sb, bindex, br);
  2880. + else {
  2881. + sysaufs_brs_del(sb, bindex);
  2882. + au_br_do_del(sb, bindex, br);
  2883. + sysaufs_brs_add(sb, bindex);
  2884. + }
  2885. +
  2886. + if (!bindex) {
  2887. + au_cpup_attr_all(d_inode(root), /*force*/1);
  2888. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  2889. + } else
  2890. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  2891. + if (au_opt_test(mnt_flags, PLINK))
  2892. + au_plink_half_refresh(sb, br_id);
  2893. +
  2894. + if (au_xino_brid(sb) == br_id)
  2895. + au_xino_brid_set(sb, -1);
  2896. + goto out; /* success */
  2897. +
  2898. +out_wh:
  2899. + /* revert */
  2900. + rerr = au_br_init_wh(sb, br, br->br_perm);
  2901. + if (rerr)
  2902. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  2903. + del->pathname, rerr);
  2904. +out:
  2905. + if (to_free)
  2906. + au_farray_free(to_free, opened);
  2907. + return err;
  2908. +}
  2909. +
  2910. +/* ---------------------------------------------------------------------- */
  2911. +
  2912. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  2913. +{
  2914. + int err;
  2915. + aufs_bindex_t btop, bbot;
  2916. + struct aufs_ibusy ibusy;
  2917. + struct inode *inode, *h_inode;
  2918. +
  2919. + err = -EPERM;
  2920. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  2921. + goto out;
  2922. +
  2923. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  2924. + if (!err)
  2925. + err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
  2926. + if (unlikely(err)) {
  2927. + err = -EFAULT;
  2928. + AuTraceErr(err);
  2929. + goto out;
  2930. + }
  2931. +
  2932. + err = -EINVAL;
  2933. + si_read_lock(sb, AuLock_FLUSH);
  2934. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
  2935. + goto out_unlock;
  2936. +
  2937. + err = 0;
  2938. + ibusy.h_ino = 0; /* invalid */
  2939. + inode = ilookup(sb, ibusy.ino);
  2940. + if (!inode
  2941. + || inode->i_ino == AUFS_ROOT_INO
  2942. + || au_is_bad_inode(inode))
  2943. + goto out_unlock;
  2944. +
  2945. + ii_read_lock_child(inode);
  2946. + btop = au_ibtop(inode);
  2947. + bbot = au_ibbot(inode);
  2948. + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
  2949. + h_inode = au_h_iptr(inode, ibusy.bindex);
  2950. + if (h_inode && au_test_ibusy(inode, btop, bbot))
  2951. + ibusy.h_ino = h_inode->i_ino;
  2952. + }
  2953. + ii_read_unlock(inode);
  2954. + iput(inode);
  2955. +
  2956. +out_unlock:
  2957. + si_read_unlock(sb);
  2958. + if (!err) {
  2959. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  2960. + if (unlikely(err)) {
  2961. + err = -EFAULT;
  2962. + AuTraceErr(err);
  2963. + }
  2964. + }
  2965. +out:
  2966. + return err;
  2967. +}
  2968. +
  2969. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  2970. +{
  2971. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  2972. +}
  2973. +
  2974. +#ifdef CONFIG_COMPAT
  2975. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  2976. +{
  2977. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  2978. +}
  2979. +#endif
  2980. +
  2981. +/* ---------------------------------------------------------------------- */
  2982. +
  2983. +/*
  2984. + * change a branch permission
  2985. + */
  2986. +
  2987. +static void au_warn_ima(void)
  2988. +{
  2989. +#ifdef CONFIG_IMA
  2990. + /* since it doesn't support mark_files_ro() */
  2991. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  2992. +#endif
  2993. +}
  2994. +
  2995. +static int do_need_sigen_inc(int a, int b)
  2996. +{
  2997. + return au_br_whable(a) && !au_br_whable(b);
  2998. +}
  2999. +
  3000. +static int need_sigen_inc(int old, int new)
  3001. +{
  3002. + return do_need_sigen_inc(old, new)
  3003. + || do_need_sigen_inc(new, old);
  3004. +}
  3005. +
  3006. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  3007. +{
  3008. + int err, do_warn;
  3009. + unsigned int mnt_flags;
  3010. + unsigned long long ull, max;
  3011. + aufs_bindex_t br_id;
  3012. + unsigned char verbose, writer;
  3013. + struct file *file, *hf, **array;
  3014. + struct au_hfile *hfile;
  3015. +
  3016. + mnt_flags = au_mntflags(sb);
  3017. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3018. +
  3019. + array = au_farray_alloc(sb, &max);
  3020. + err = PTR_ERR(array);
  3021. + if (IS_ERR(array))
  3022. + goto out;
  3023. +
  3024. + do_warn = 0;
  3025. + br_id = au_sbr_id(sb, bindex);
  3026. + for (ull = 0; ull < max; ull++) {
  3027. + file = array[ull];
  3028. + if (unlikely(!file))
  3029. + break;
  3030. +
  3031. + /* AuDbg("%pD\n", file); */
  3032. + fi_read_lock(file);
  3033. + if (unlikely(au_test_mmapped(file))) {
  3034. + err = -EBUSY;
  3035. + AuVerbose(verbose, "mmapped %pD\n", file);
  3036. + AuDbgFile(file);
  3037. + FiMustNoWaiters(file);
  3038. + fi_read_unlock(file);
  3039. + goto out_array;
  3040. + }
  3041. +
  3042. + hfile = &au_fi(file)->fi_htop;
  3043. + hf = hfile->hf_file;
  3044. + if (!d_is_reg(file->f_path.dentry)
  3045. + || !(file->f_mode & FMODE_WRITE)
  3046. + || hfile->hf_br->br_id != br_id
  3047. + || !(hf->f_mode & FMODE_WRITE))
  3048. + array[ull] = NULL;
  3049. + else {
  3050. + do_warn = 1;
  3051. + get_file(file);
  3052. + }
  3053. +
  3054. + FiMustNoWaiters(file);
  3055. + fi_read_unlock(file);
  3056. + fput(file);
  3057. + }
  3058. +
  3059. + err = 0;
  3060. + if (do_warn)
  3061. + au_warn_ima();
  3062. +
  3063. + for (ull = 0; ull < max; ull++) {
  3064. + file = array[ull];
  3065. + if (!file)
  3066. + continue;
  3067. +
  3068. + /* todo: already flushed? */
  3069. + /*
  3070. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3071. + * approach which resets f_mode and calls mnt_drop_write() and
  3072. + * file_release_write() for each file, because the branch
  3073. + * attribute in aufs world is totally different from the native
  3074. + * fs rw/ro mode.
  3075. + */
  3076. + /* fi_read_lock(file); */
  3077. + hfile = &au_fi(file)->fi_htop;
  3078. + hf = hfile->hf_file;
  3079. + /* fi_read_unlock(file); */
  3080. + spin_lock(&hf->f_lock);
  3081. + writer = !!(hf->f_mode & FMODE_WRITER);
  3082. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3083. + spin_unlock(&hf->f_lock);
  3084. + if (writer) {
  3085. + put_write_access(file_inode(hf));
  3086. + __mnt_drop_write(hf->f_path.mnt);
  3087. + }
  3088. + }
  3089. +
  3090. +out_array:
  3091. + au_farray_free(array, max);
  3092. +out:
  3093. + AuTraceErr(err);
  3094. + return err;
  3095. +}
  3096. +
  3097. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3098. + int *do_refresh)
  3099. +{
  3100. + int err, rerr;
  3101. + aufs_bindex_t bindex;
  3102. + struct dentry *root;
  3103. + struct au_branch *br;
  3104. + struct au_br_fhsm *bf;
  3105. +
  3106. + root = sb->s_root;
  3107. + bindex = au_find_dbindex(root, mod->h_root);
  3108. + if (bindex < 0) {
  3109. + if (remount)
  3110. + return 0; /* success */
  3111. + err = -ENOENT;
  3112. + pr_err("%s no such branch\n", mod->path);
  3113. + goto out;
  3114. + }
  3115. + AuDbg("bindex b%d\n", bindex);
  3116. +
  3117. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3118. + if (unlikely(err))
  3119. + goto out;
  3120. +
  3121. + br = au_sbr(sb, bindex);
  3122. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3123. + if (br->br_perm == mod->perm)
  3124. + return 0; /* success */
  3125. +
  3126. + /* pre-allocate for non-fhsm --> fhsm */
  3127. + bf = NULL;
  3128. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3129. + err = au_fhsm_br_alloc(br);
  3130. + if (unlikely(err))
  3131. + goto out;
  3132. + bf = br->br_fhsm;
  3133. + br->br_fhsm = NULL;
  3134. + }
  3135. +
  3136. + if (au_br_writable(br->br_perm)) {
  3137. + /* remove whiteout base */
  3138. + err = au_br_init_wh(sb, br, mod->perm);
  3139. + if (unlikely(err))
  3140. + goto out_bf;
  3141. +
  3142. + if (!au_br_writable(mod->perm)) {
  3143. + /* rw --> ro, file might be mmapped */
  3144. + DiMustNoWaiters(root);
  3145. + IiMustNoWaiters(d_inode(root));
  3146. + di_write_unlock(root);
  3147. + err = au_br_mod_files_ro(sb, bindex);
  3148. + /* aufs_write_lock() calls ..._child() */
  3149. + di_write_lock_child(root);
  3150. +
  3151. + if (unlikely(err)) {
  3152. + rerr = -ENOMEM;
  3153. + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
  3154. + GFP_NOFS);
  3155. + if (br->br_wbr)
  3156. + rerr = au_wbr_init(br, sb, br->br_perm);
  3157. + if (unlikely(rerr)) {
  3158. + AuIOErr("nested error %d (%d)\n",
  3159. + rerr, err);
  3160. + br->br_perm = mod->perm;
  3161. + }
  3162. + }
  3163. + }
  3164. + } else if (au_br_writable(mod->perm)) {
  3165. + /* ro --> rw */
  3166. + err = -ENOMEM;
  3167. + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3168. + if (br->br_wbr) {
  3169. + err = au_wbr_init(br, sb, mod->perm);
  3170. + if (unlikely(err)) {
  3171. + au_delayed_kfree(br->br_wbr);
  3172. + br->br_wbr = NULL;
  3173. + }
  3174. + }
  3175. + }
  3176. + if (unlikely(err))
  3177. + goto out_bf;
  3178. +
  3179. + if (au_br_fhsm(br->br_perm)) {
  3180. + if (!au_br_fhsm(mod->perm)) {
  3181. + /* fhsm --> non-fhsm */
  3182. + au_br_fhsm_fin(br->br_fhsm);
  3183. + au_delayed_kfree(br->br_fhsm);
  3184. + br->br_fhsm = NULL;
  3185. + }
  3186. + } else if (au_br_fhsm(mod->perm))
  3187. + /* non-fhsm --> fhsm */
  3188. + br->br_fhsm = bf;
  3189. +
  3190. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3191. + br->br_perm = mod->perm;
  3192. + goto out; /* success */
  3193. +
  3194. +out_bf:
  3195. + if (bf)
  3196. + au_delayed_kfree(bf);
  3197. +out:
  3198. + AuTraceErr(err);
  3199. + return err;
  3200. +}
  3201. +
  3202. +/* ---------------------------------------------------------------------- */
  3203. +
  3204. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3205. +{
  3206. + int err;
  3207. + struct kstatfs kstfs;
  3208. +
  3209. + err = vfs_statfs(&br->br_path, &kstfs);
  3210. + if (!err) {
  3211. + stfs->f_blocks = kstfs.f_blocks;
  3212. + stfs->f_bavail = kstfs.f_bavail;
  3213. + stfs->f_files = kstfs.f_files;
  3214. + stfs->f_ffree = kstfs.f_ffree;
  3215. + }
  3216. +
  3217. + return err;
  3218. +}
  3219. diff -Naur null/fs/aufs/branch.h linux-4.7/fs/aufs/branch.h
  3220. --- /dev/null
  3221. +++ linux-4.7/fs/aufs/branch.h 2016-08-08 12:57:44.597384786 -0400
  3222. @@ -0,0 +1,309 @@
  3223. +/*
  3224. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  3225. + *
  3226. + * This program, aufs is free software; you can redistribute it and/or modify
  3227. + * it under the terms of the GNU General Public License as published by
  3228. + * the Free Software Foundation; either version 2 of the License, or
  3229. + * (at your option) any later version.
  3230. + *
  3231. + * This program is distributed in the hope that it will be useful,
  3232. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3233. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3234. + * GNU General Public License for more details.
  3235. + *
  3236. + * You should have received a copy of the GNU General Public License
  3237. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3238. + */
  3239. +
  3240. +/*
  3241. + * branch filesystems and xino for them
  3242. + */
  3243. +
  3244. +#ifndef __AUFS_BRANCH_H__
  3245. +#define __AUFS_BRANCH_H__
  3246. +
  3247. +#ifdef __KERNEL__
  3248. +
  3249. +#include <linux/mount.h>
  3250. +#include "dynop.h"
  3251. +#include "rwsem.h"
  3252. +#include "super.h"
  3253. +
  3254. +/* ---------------------------------------------------------------------- */
  3255. +
  3256. +/* a xino file */
  3257. +struct au_xino_file {
  3258. + struct file *xi_file;
  3259. + struct mutex xi_nondir_mtx;
  3260. +
  3261. + /* todo: make xino files an array to support huge inode number */
  3262. +
  3263. +#ifdef CONFIG_DEBUG_FS
  3264. + struct dentry *xi_dbgaufs;
  3265. +#endif
  3266. +};
  3267. +
  3268. +/* File-based Hierarchical Storage Management */
  3269. +struct au_br_fhsm {
  3270. +#ifdef CONFIG_AUFS_FHSM
  3271. + struct mutex bf_lock;
  3272. + unsigned long bf_jiffy;
  3273. + struct aufs_stfs bf_stfs;
  3274. + int bf_readable;
  3275. +#endif
  3276. +};
  3277. +
  3278. +/* members for writable branch only */
  3279. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3280. +struct au_wbr {
  3281. + struct au_rwsem wbr_wh_rwsem;
  3282. + struct dentry *wbr_wh[AuBrWh_Last];
  3283. + atomic_t wbr_wh_running;
  3284. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3285. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3286. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3287. +
  3288. + /* mfs mode */
  3289. + unsigned long long wbr_bytes;
  3290. +};
  3291. +
  3292. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3293. +#define AuBrDynOp (AuDyLast * 4)
  3294. +
  3295. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3296. +/* support for asynchronous destruction */
  3297. +struct au_br_hfsnotify {
  3298. + struct fsnotify_group *hfsn_group;
  3299. +};
  3300. +#endif
  3301. +
  3302. +/* sysfs entries */
  3303. +struct au_brsysfs {
  3304. + char name[16];
  3305. + struct attribute attr;
  3306. +};
  3307. +
  3308. +enum {
  3309. + AuBrSysfs_BR,
  3310. + AuBrSysfs_BRID,
  3311. + AuBrSysfs_Last
  3312. +};
  3313. +
  3314. +/* protected by superblock rwsem */
  3315. +struct au_branch {
  3316. + struct au_xino_file br_xino;
  3317. +
  3318. + aufs_bindex_t br_id;
  3319. +
  3320. + int br_perm;
  3321. + struct path br_path;
  3322. + spinlock_t br_dykey_lock;
  3323. + struct au_dykey *br_dykey[AuBrDynOp];
  3324. + struct percpu_counter br_count;
  3325. +
  3326. + struct au_wbr *br_wbr;
  3327. + struct au_br_fhsm *br_fhsm;
  3328. +
  3329. + /* xino truncation */
  3330. + atomic_t br_xino_running;
  3331. +
  3332. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3333. + struct au_br_hfsnotify *br_hfsn;
  3334. +#endif
  3335. +
  3336. +#ifdef CONFIG_SYSFS
  3337. + /* entries under sysfs per mount-point */
  3338. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3339. +#endif
  3340. +};
  3341. +
  3342. +/* ---------------------------------------------------------------------- */
  3343. +
  3344. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3345. +{
  3346. + return br->br_path.mnt;
  3347. +}
  3348. +
  3349. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3350. +{
  3351. + return br->br_path.dentry;
  3352. +}
  3353. +
  3354. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3355. +{
  3356. + return au_br_mnt(br)->mnt_sb;
  3357. +}
  3358. +
  3359. +static inline void au_br_get(struct au_branch *br)
  3360. +{
  3361. + percpu_counter_inc(&br->br_count);
  3362. +}
  3363. +
  3364. +static inline void au_br_put(struct au_branch *br)
  3365. +{
  3366. + percpu_counter_dec(&br->br_count);
  3367. +}
  3368. +
  3369. +static inline s64 au_br_count(struct au_branch *br)
  3370. +{
  3371. + return percpu_counter_sum(&br->br_count);
  3372. +}
  3373. +
  3374. +static inline void au_br_count_init(struct au_branch *br)
  3375. +{
  3376. + percpu_counter_init(&br->br_count, 0, GFP_NOFS);
  3377. +}
  3378. +
  3379. +static inline void au_br_count_fin(struct au_branch *br)
  3380. +{
  3381. + percpu_counter_destroy(&br->br_count);
  3382. +}
  3383. +
  3384. +static inline int au_br_rdonly(struct au_branch *br)
  3385. +{
  3386. + return ((au_br_sb(br)->s_flags & MS_RDONLY)
  3387. + || !au_br_writable(br->br_perm))
  3388. + ? -EROFS : 0;
  3389. +}
  3390. +
  3391. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3392. +{
  3393. +#ifdef CONFIG_AUFS_HNOTIFY
  3394. + return !(brperm & AuBrPerm_RR);
  3395. +#else
  3396. + return 0;
  3397. +#endif
  3398. +}
  3399. +
  3400. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3401. +{
  3402. + int err, exec_flag;
  3403. +
  3404. + err = 0;
  3405. + exec_flag = oflag & __FMODE_EXEC;
  3406. + if (unlikely(exec_flag && path_noexec(&br->br_path)))
  3407. + err = -EACCES;
  3408. +
  3409. + return err;
  3410. +}
  3411. +
  3412. +/* ---------------------------------------------------------------------- */
  3413. +
  3414. +/* branch.c */
  3415. +struct au_sbinfo;
  3416. +void au_br_free(struct au_sbinfo *sinfo);
  3417. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3418. +struct au_opt_add;
  3419. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3420. +struct au_opt_del;
  3421. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3422. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3423. +#ifdef CONFIG_COMPAT
  3424. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3425. +#endif
  3426. +struct au_opt_mod;
  3427. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3428. + int *do_refresh);
  3429. +struct aufs_stfs;
  3430. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3431. +
  3432. +/* xino.c */
  3433. +static const loff_t au_loff_max = LLONG_MAX;
  3434. +
  3435. +int au_xib_trunc(struct super_block *sb);
  3436. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
  3437. + loff_t *pos);
  3438. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  3439. + size_t size, loff_t *pos);
  3440. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
  3441. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
  3442. +ino_t au_xino_new_ino(struct super_block *sb);
  3443. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3444. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3445. + ino_t ino);
  3446. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3447. + ino_t *ino);
  3448. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3449. + struct file *base_file, int do_test);
  3450. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
  3451. +
  3452. +struct au_opt_xino;
  3453. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
  3454. +void au_xino_clr(struct super_block *sb);
  3455. +struct file *au_xino_def(struct super_block *sb);
  3456. +int au_xino_path(struct seq_file *seq, struct file *file);
  3457. +
  3458. +/* ---------------------------------------------------------------------- */
  3459. +
  3460. +/* Superblock to branch */
  3461. +static inline
  3462. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3463. +{
  3464. + return au_sbr(sb, bindex)->br_id;
  3465. +}
  3466. +
  3467. +static inline
  3468. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3469. +{
  3470. + return au_br_mnt(au_sbr(sb, bindex));
  3471. +}
  3472. +
  3473. +static inline
  3474. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3475. +{
  3476. + return au_br_sb(au_sbr(sb, bindex));
  3477. +}
  3478. +
  3479. +static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
  3480. +{
  3481. + au_br_get(au_sbr(sb, bindex));
  3482. +}
  3483. +
  3484. +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
  3485. +{
  3486. + au_br_put(au_sbr(sb, bindex));
  3487. +}
  3488. +
  3489. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3490. +{
  3491. + return au_sbr(sb, bindex)->br_perm;
  3492. +}
  3493. +
  3494. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3495. +{
  3496. + return au_br_whable(au_sbr_perm(sb, bindex));
  3497. +}
  3498. +
  3499. +/* ---------------------------------------------------------------------- */
  3500. +
  3501. +/*
  3502. + * wbr_wh_read_lock, wbr_wh_write_lock
  3503. + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
  3504. + */
  3505. +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
  3506. +
  3507. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
  3508. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
  3509. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
  3510. +
  3511. +/* ---------------------------------------------------------------------- */
  3512. +
  3513. +#ifdef CONFIG_AUFS_FHSM
  3514. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3515. +{
  3516. + mutex_init(&brfhsm->bf_lock);
  3517. + brfhsm->bf_jiffy = 0;
  3518. + brfhsm->bf_readable = 0;
  3519. +}
  3520. +
  3521. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3522. +{
  3523. + mutex_destroy(&brfhsm->bf_lock);
  3524. +}
  3525. +#else
  3526. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3527. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3528. +#endif
  3529. +
  3530. +#endif /* __KERNEL__ */
  3531. +#endif /* __AUFS_BRANCH_H__ */
  3532. diff -Naur null/fs/aufs/conf.mk linux-4.7/fs/aufs/conf.mk
  3533. --- /dev/null
  3534. +++ linux-4.7/fs/aufs/conf.mk 2016-08-08 12:57:44.597384786 -0400
  3535. @@ -0,0 +1,38 @@
  3536. +
  3537. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3538. +
  3539. +define AuConf
  3540. +ifdef ${1}
  3541. +AuConfStr += ${1}=${${1}}
  3542. +endif
  3543. +endef
  3544. +
  3545. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3546. + SBILIST \
  3547. + HNOTIFY HFSNOTIFY \
  3548. + EXPORT INO_T_64 \
  3549. + XATTR \
  3550. + FHSM \
  3551. + RDU \
  3552. + SHWH \
  3553. + BR_RAMFS \
  3554. + BR_FUSE POLL \
  3555. + BR_HFSPLUS \
  3556. + BDEV_LOOP \
  3557. + DEBUG MAGIC_SYSRQ
  3558. +$(foreach i, ${AuConfAll}, \
  3559. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3560. +
  3561. +AuConfName = ${obj}/conf.str
  3562. +${AuConfName}.tmp: FORCE
  3563. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3564. +${AuConfName}: ${AuConfName}.tmp
  3565. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3566. + echo ' GEN ' $@; \
  3567. + cp -p $< $@; \
  3568. + }
  3569. +FORCE:
  3570. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3571. +${obj}/sysfs.o: ${AuConfName}
  3572. +
  3573. +-include ${srctree}/${src}/conf_priv.mk
  3574. diff -Naur null/fs/aufs/cpup.c linux-4.7/fs/aufs/cpup.c
  3575. --- /dev/null
  3576. +++ linux-4.7/fs/aufs/cpup.c 2016-08-08 12:57:44.598384764 -0400
  3577. @@ -0,0 +1,1383 @@
  3578. +/*
  3579. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  3580. + *
  3581. + * This program, aufs is free software; you can redistribute it and/or modify
  3582. + * it under the terms of the GNU General Public License as published by
  3583. + * the Free Software Foundation; either version 2 of the License, or
  3584. + * (at your option) any later version.
  3585. + *
  3586. + * This program is distributed in the hope that it will be useful,
  3587. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3588. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3589. + * GNU General Public License for more details.
  3590. + *
  3591. + * You should have received a copy of the GNU General Public License
  3592. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3593. + */
  3594. +
  3595. +/*
  3596. + * copy-up functions, see wbr_policy.c for copy-down
  3597. + */
  3598. +
  3599. +#include <linux/fs_stack.h>
  3600. +#include <linux/mm.h>
  3601. +#include <linux/task_work.h>
  3602. +#include "aufs.h"
  3603. +
  3604. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3605. +{
  3606. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3607. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3608. +
  3609. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3610. +
  3611. + dst->i_flags |= iflags & ~mask;
  3612. + if (au_test_fs_notime(dst->i_sb))
  3613. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3614. +}
  3615. +
  3616. +void au_cpup_attr_timesizes(struct inode *inode)
  3617. +{
  3618. + struct inode *h_inode;
  3619. +
  3620. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3621. + fsstack_copy_attr_times(inode, h_inode);
  3622. + fsstack_copy_inode_size(inode, h_inode);
  3623. +}
  3624. +
  3625. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3626. +{
  3627. + struct inode *h_inode;
  3628. + struct super_block *sb;
  3629. + aufs_bindex_t bindex, bbot;
  3630. +
  3631. + sb = inode->i_sb;
  3632. + bindex = au_ibtop(inode);
  3633. + h_inode = au_h_iptr(inode, bindex);
  3634. + if (!force
  3635. + && !S_ISDIR(h_inode->i_mode)
  3636. + && au_opt_test(au_mntflags(sb), PLINK)
  3637. + && au_plink_test(inode))
  3638. + return;
  3639. +
  3640. + /*
  3641. + * 0 can happen in revalidating.
  3642. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3643. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3644. + * case.
  3645. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3646. + * the incorrect link count.
  3647. + */
  3648. + set_nlink(inode, h_inode->i_nlink);
  3649. +
  3650. + /*
  3651. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3652. + * it may includes whplink directory.
  3653. + */
  3654. + if (S_ISDIR(h_inode->i_mode)) {
  3655. + bbot = au_ibbot(inode);
  3656. + for (bindex++; bindex <= bbot; bindex++) {
  3657. + h_inode = au_h_iptr(inode, bindex);
  3658. + if (h_inode)
  3659. + au_add_nlink(inode, h_inode);
  3660. + }
  3661. + }
  3662. +}
  3663. +
  3664. +void au_cpup_attr_changeable(struct inode *inode)
  3665. +{
  3666. + struct inode *h_inode;
  3667. +
  3668. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3669. + inode->i_mode = h_inode->i_mode;
  3670. + inode->i_uid = h_inode->i_uid;
  3671. + inode->i_gid = h_inode->i_gid;
  3672. + au_cpup_attr_timesizes(inode);
  3673. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3674. +}
  3675. +
  3676. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3677. +{
  3678. + struct au_iinfo *iinfo = au_ii(inode);
  3679. +
  3680. + IiMustWriteLock(inode);
  3681. +
  3682. + iinfo->ii_higen = h_inode->i_generation;
  3683. + iinfo->ii_hsb1 = h_inode->i_sb;
  3684. +}
  3685. +
  3686. +void au_cpup_attr_all(struct inode *inode, int force)
  3687. +{
  3688. + struct inode *h_inode;
  3689. +
  3690. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3691. + au_cpup_attr_changeable(inode);
  3692. + if (inode->i_nlink > 0)
  3693. + au_cpup_attr_nlink(inode, force);
  3694. + inode->i_rdev = h_inode->i_rdev;
  3695. + inode->i_blkbits = h_inode->i_blkbits;
  3696. + au_cpup_igen(inode, h_inode);
  3697. +}
  3698. +
  3699. +/* ---------------------------------------------------------------------- */
  3700. +
  3701. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3702. +
  3703. +/* keep the timestamps of the parent dir when cpup */
  3704. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3705. + struct path *h_path)
  3706. +{
  3707. + struct inode *h_inode;
  3708. +
  3709. + dt->dt_dentry = dentry;
  3710. + dt->dt_h_path = *h_path;
  3711. + h_inode = d_inode(h_path->dentry);
  3712. + dt->dt_atime = h_inode->i_atime;
  3713. + dt->dt_mtime = h_inode->i_mtime;
  3714. + /* smp_mb(); */
  3715. +}
  3716. +
  3717. +void au_dtime_revert(struct au_dtime *dt)
  3718. +{
  3719. + struct iattr attr;
  3720. + int err;
  3721. +
  3722. + attr.ia_atime = dt->dt_atime;
  3723. + attr.ia_mtime = dt->dt_mtime;
  3724. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3725. + | ATTR_ATIME | ATTR_ATIME_SET;
  3726. +
  3727. + /* no delegation since this is a directory */
  3728. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3729. + if (unlikely(err))
  3730. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3731. +}
  3732. +
  3733. +/* ---------------------------------------------------------------------- */
  3734. +
  3735. +/* internal use only */
  3736. +struct au_cpup_reg_attr {
  3737. + int valid;
  3738. + struct kstat st;
  3739. + unsigned int iflags; /* inode->i_flags */
  3740. +};
  3741. +
  3742. +static noinline_for_stack
  3743. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  3744. + struct au_cpup_reg_attr *h_src_attr)
  3745. +{
  3746. + int err, sbits, icex;
  3747. + unsigned int mnt_flags;
  3748. + unsigned char verbose;
  3749. + struct iattr ia;
  3750. + struct path h_path;
  3751. + struct inode *h_isrc, *h_idst;
  3752. + struct kstat *h_st;
  3753. + struct au_branch *br;
  3754. +
  3755. + h_path.dentry = au_h_dptr(dst, bindex);
  3756. + h_idst = d_inode(h_path.dentry);
  3757. + br = au_sbr(dst->d_sb, bindex);
  3758. + h_path.mnt = au_br_mnt(br);
  3759. + h_isrc = d_inode(h_src);
  3760. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  3761. + | ATTR_ATIME | ATTR_MTIME
  3762. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  3763. + if (h_src_attr && h_src_attr->valid) {
  3764. + h_st = &h_src_attr->st;
  3765. + ia.ia_uid = h_st->uid;
  3766. + ia.ia_gid = h_st->gid;
  3767. + ia.ia_atime = h_st->atime;
  3768. + ia.ia_mtime = h_st->mtime;
  3769. + if (h_idst->i_mode != h_st->mode
  3770. + && !S_ISLNK(h_idst->i_mode)) {
  3771. + ia.ia_valid |= ATTR_MODE;
  3772. + ia.ia_mode = h_st->mode;
  3773. + }
  3774. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  3775. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  3776. + } else {
  3777. + ia.ia_uid = h_isrc->i_uid;
  3778. + ia.ia_gid = h_isrc->i_gid;
  3779. + ia.ia_atime = h_isrc->i_atime;
  3780. + ia.ia_mtime = h_isrc->i_mtime;
  3781. + if (h_idst->i_mode != h_isrc->i_mode
  3782. + && !S_ISLNK(h_idst->i_mode)) {
  3783. + ia.ia_valid |= ATTR_MODE;
  3784. + ia.ia_mode = h_isrc->i_mode;
  3785. + }
  3786. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  3787. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  3788. + }
  3789. + /* no delegation since it is just created */
  3790. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3791. +
  3792. + /* is this nfs only? */
  3793. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  3794. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  3795. + ia.ia_mode = h_isrc->i_mode;
  3796. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3797. + }
  3798. +
  3799. + icex = br->br_perm & AuBrAttr_ICEX;
  3800. + if (!err) {
  3801. + mnt_flags = au_mntflags(dst->d_sb);
  3802. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3803. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  3804. + }
  3805. +
  3806. + return err;
  3807. +}
  3808. +
  3809. +/* ---------------------------------------------------------------------- */
  3810. +
  3811. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  3812. + char *buf, unsigned long blksize)
  3813. +{
  3814. + int err;
  3815. + size_t sz, rbytes, wbytes;
  3816. + unsigned char all_zero;
  3817. + char *p, *zp;
  3818. + struct inode *h_inode;
  3819. + /* reduce stack usage */
  3820. + struct iattr *ia;
  3821. +
  3822. + zp = page_address(ZERO_PAGE(0));
  3823. + if (unlikely(!zp))
  3824. + return -ENOMEM; /* possible? */
  3825. +
  3826. + err = 0;
  3827. + all_zero = 0;
  3828. + while (len) {
  3829. + AuDbg("len %lld\n", len);
  3830. + sz = blksize;
  3831. + if (len < blksize)
  3832. + sz = len;
  3833. +
  3834. + rbytes = 0;
  3835. + /* todo: signal_pending? */
  3836. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  3837. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  3838. + err = rbytes;
  3839. + }
  3840. + if (unlikely(err < 0))
  3841. + break;
  3842. +
  3843. + all_zero = 0;
  3844. + if (len >= rbytes && rbytes == blksize)
  3845. + all_zero = !memcmp(buf, zp, rbytes);
  3846. + if (!all_zero) {
  3847. + wbytes = rbytes;
  3848. + p = buf;
  3849. + while (wbytes) {
  3850. + size_t b;
  3851. +
  3852. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  3853. + err = b;
  3854. + /* todo: signal_pending? */
  3855. + if (unlikely(err == -EAGAIN || err == -EINTR))
  3856. + continue;
  3857. + if (unlikely(err < 0))
  3858. + break;
  3859. + wbytes -= b;
  3860. + p += b;
  3861. + }
  3862. + if (unlikely(err < 0))
  3863. + break;
  3864. + } else {
  3865. + loff_t res;
  3866. +
  3867. + AuLabel(hole);
  3868. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  3869. + err = res;
  3870. + if (unlikely(res < 0))
  3871. + break;
  3872. + }
  3873. + len -= rbytes;
  3874. + err = 0;
  3875. + }
  3876. +
  3877. + /* the last block may be a hole */
  3878. + if (!err && all_zero) {
  3879. + AuLabel(last hole);
  3880. +
  3881. + err = 1;
  3882. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  3883. + /* nfs requires this step to make last hole */
  3884. + /* is this only nfs? */
  3885. + do {
  3886. + /* todo: signal_pending? */
  3887. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  3888. + } while (err == -EAGAIN || err == -EINTR);
  3889. + if (err == 1)
  3890. + dst->f_pos--;
  3891. + }
  3892. +
  3893. + if (err == 1) {
  3894. + ia = (void *)buf;
  3895. + ia->ia_size = dst->f_pos;
  3896. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  3897. + ia->ia_file = dst;
  3898. + h_inode = file_inode(dst);
  3899. + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
  3900. + /* no delegation since it is just created */
  3901. + err = vfsub_notify_change(&dst->f_path, ia,
  3902. + /*delegated*/NULL);
  3903. + inode_unlock(h_inode);
  3904. + }
  3905. + }
  3906. +
  3907. + return err;
  3908. +}
  3909. +
  3910. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  3911. +{
  3912. + int err;
  3913. + unsigned long blksize;
  3914. + unsigned char do_kfree;
  3915. + char *buf;
  3916. +
  3917. + err = -ENOMEM;
  3918. + blksize = dst->f_path.dentry->d_sb->s_blocksize;
  3919. + if (!blksize || PAGE_SIZE < blksize)
  3920. + blksize = PAGE_SIZE;
  3921. + AuDbg("blksize %lu\n", blksize);
  3922. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  3923. + if (do_kfree)
  3924. + buf = kmalloc(blksize, GFP_NOFS);
  3925. + else
  3926. + buf = (void *)__get_free_page(GFP_NOFS);
  3927. + if (unlikely(!buf))
  3928. + goto out;
  3929. +
  3930. + if (len > (1 << 22))
  3931. + AuDbg("copying a large file %lld\n", (long long)len);
  3932. +
  3933. + src->f_pos = 0;
  3934. + dst->f_pos = 0;
  3935. + err = au_do_copy_file(dst, src, len, buf, blksize);
  3936. + if (do_kfree)
  3937. + au_delayed_kfree(buf);
  3938. + else
  3939. + au_delayed_free_page((unsigned long)buf);
  3940. +
  3941. +out:
  3942. + return err;
  3943. +}
  3944. +
  3945. +/*
  3946. + * to support a sparse file which is opened with O_APPEND,
  3947. + * we need to close the file.
  3948. + */
  3949. +static int au_cp_regular(struct au_cp_generic *cpg)
  3950. +{
  3951. + int err, i;
  3952. + enum { SRC, DST };
  3953. + struct {
  3954. + aufs_bindex_t bindex;
  3955. + unsigned int flags;
  3956. + struct dentry *dentry;
  3957. + int force_wr;
  3958. + struct file *file;
  3959. + void *label;
  3960. + } *f, file[] = {
  3961. + {
  3962. + .bindex = cpg->bsrc,
  3963. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  3964. + .label = &&out
  3965. + },
  3966. + {
  3967. + .bindex = cpg->bdst,
  3968. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  3969. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  3970. + .label = &&out_src
  3971. + }
  3972. + };
  3973. + struct super_block *sb;
  3974. + struct task_struct *tsk = current;
  3975. +
  3976. + /* bsrc branch can be ro/rw. */
  3977. + sb = cpg->dentry->d_sb;
  3978. + f = file;
  3979. + for (i = 0; i < 2; i++, f++) {
  3980. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  3981. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  3982. + /*file*/NULL, f->force_wr);
  3983. + err = PTR_ERR(f->file);
  3984. + if (IS_ERR(f->file))
  3985. + goto *f->label;
  3986. + }
  3987. +
  3988. + /* try stopping to update while we copyup */
  3989. + IMustLock(d_inode(file[SRC].dentry));
  3990. + err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
  3991. +
  3992. + /* i wonder if we had O_NO_DELAY_FPUT flag */
  3993. + if (tsk->flags & PF_KTHREAD)
  3994. + __fput_sync(file[DST].file);
  3995. + else {
  3996. + WARN(1, "%pD\nPlease report this warning to aufs-users ML",
  3997. + file[DST].file);
  3998. + fput(file[DST].file);
  3999. + /*
  4000. + * too bad.
  4001. + * we have to call both since we don't know which place the file
  4002. + * was added to.
  4003. + */
  4004. + task_work_run();
  4005. + flush_delayed_fput();
  4006. + }
  4007. + au_sbr_put(sb, file[DST].bindex);
  4008. +
  4009. +out_src:
  4010. + fput(file[SRC].file);
  4011. + au_sbr_put(sb, file[SRC].bindex);
  4012. +out:
  4013. + return err;
  4014. +}
  4015. +
  4016. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  4017. + struct au_cpup_reg_attr *h_src_attr)
  4018. +{
  4019. + int err, rerr;
  4020. + loff_t l;
  4021. + struct path h_path;
  4022. + struct inode *h_src_inode, *h_dst_inode;
  4023. +
  4024. + err = 0;
  4025. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4026. + l = i_size_read(h_src_inode);
  4027. + if (cpg->len == -1 || l < cpg->len)
  4028. + cpg->len = l;
  4029. + if (cpg->len) {
  4030. + /* try stopping to update while we are referencing */
  4031. + inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
  4032. + au_pin_hdir_unlock(cpg->pin);
  4033. +
  4034. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4035. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4036. + h_src_attr->iflags = h_src_inode->i_flags;
  4037. + if (!au_test_nfs(h_src_inode->i_sb))
  4038. + err = vfs_getattr(&h_path, &h_src_attr->st);
  4039. + else {
  4040. + inode_unlock(h_src_inode);
  4041. + err = vfs_getattr(&h_path, &h_src_attr->st);
  4042. + inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
  4043. + }
  4044. + if (unlikely(err)) {
  4045. + inode_unlock(h_src_inode);
  4046. + goto out;
  4047. + }
  4048. + h_src_attr->valid = 1;
  4049. + err = au_cp_regular(cpg);
  4050. + inode_unlock(h_src_inode);
  4051. + rerr = au_pin_hdir_relock(cpg->pin);
  4052. + if (!err && rerr)
  4053. + err = rerr;
  4054. + }
  4055. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4056. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4057. + h_dst_inode = d_inode(h_path.dentry);
  4058. + spin_lock(&h_dst_inode->i_lock);
  4059. + h_dst_inode->i_state |= I_LINKABLE;
  4060. + spin_unlock(&h_dst_inode->i_lock);
  4061. + }
  4062. +
  4063. +out:
  4064. + return err;
  4065. +}
  4066. +
  4067. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4068. + struct inode *h_dir)
  4069. +{
  4070. + int err, symlen;
  4071. + mm_segment_t old_fs;
  4072. + union {
  4073. + char *k;
  4074. + char __user *u;
  4075. + } sym;
  4076. + struct inode *h_inode = d_inode(h_src);
  4077. + const struct inode_operations *h_iop = h_inode->i_op;
  4078. +
  4079. + err = -ENOSYS;
  4080. + if (unlikely(!h_iop->readlink))
  4081. + goto out;
  4082. +
  4083. + err = -ENOMEM;
  4084. + sym.k = (void *)__get_free_page(GFP_NOFS);
  4085. + if (unlikely(!sym.k))
  4086. + goto out;
  4087. +
  4088. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  4089. + old_fs = get_fs();
  4090. + set_fs(KERNEL_DS);
  4091. + symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
  4092. + err = symlen;
  4093. + set_fs(old_fs);
  4094. +
  4095. + if (symlen > 0) {
  4096. + sym.k[symlen] = 0;
  4097. + err = vfsub_symlink(h_dir, h_path, sym.k);
  4098. + }
  4099. + au_delayed_free_page((unsigned long)sym.k);
  4100. +
  4101. +out:
  4102. + return err;
  4103. +}
  4104. +
  4105. +/*
  4106. + * regardless 'acl' option, reset all ACL.
  4107. + * All ACL will be copied up later from the original entry on the lower branch.
  4108. + */
  4109. +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
  4110. +{
  4111. + int err;
  4112. + struct dentry *h_dentry;
  4113. + struct inode *h_inode;
  4114. +
  4115. + h_dentry = h_path->dentry;
  4116. + h_inode = d_inode(h_dentry);
  4117. + /* forget_all_cached_acls(h_inode)); */
  4118. + err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
  4119. + AuTraceErr(err);
  4120. + if (err == -EOPNOTSUPP)
  4121. + err = 0;
  4122. + if (!err)
  4123. + err = vfsub_acl_chmod(h_inode, mode);
  4124. +
  4125. + AuTraceErr(err);
  4126. + return err;
  4127. +}
  4128. +
  4129. +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4130. + struct inode *h_dir, struct path *h_path)
  4131. +{
  4132. + int err;
  4133. + struct inode *dir, *inode;
  4134. +
  4135. + err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
  4136. + AuTraceErr(err);
  4137. + if (err == -EOPNOTSUPP)
  4138. + err = 0;
  4139. + if (unlikely(err))
  4140. + goto out;
  4141. +
  4142. + /*
  4143. + * strange behaviour from the users view,
  4144. + * particularry setattr case
  4145. + */
  4146. + dir = d_inode(dst_parent);
  4147. + if (au_ibtop(dir) == cpg->bdst)
  4148. + au_cpup_attr_nlink(dir, /*force*/1);
  4149. + inode = d_inode(cpg->dentry);
  4150. + au_cpup_attr_nlink(inode, /*force*/1);
  4151. +
  4152. +out:
  4153. + return err;
  4154. +}
  4155. +
  4156. +static noinline_for_stack
  4157. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4158. + struct au_cpup_reg_attr *h_src_attr)
  4159. +{
  4160. + int err;
  4161. + umode_t mode;
  4162. + unsigned int mnt_flags;
  4163. + unsigned char isdir, isreg, force;
  4164. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4165. + struct au_dtime dt;
  4166. + struct path h_path;
  4167. + struct dentry *h_src, *h_dst, *h_parent;
  4168. + struct inode *h_inode, *h_dir;
  4169. + struct super_block *sb;
  4170. +
  4171. + /* bsrc branch can be ro/rw. */
  4172. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4173. + h_inode = d_inode(h_src);
  4174. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4175. +
  4176. + /* try stopping to be referenced while we are creating */
  4177. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4178. + if (au_ftest_cpup(cpg->flags, RENAME))
  4179. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4180. + AUFS_WH_PFX_LEN));
  4181. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4182. + h_dir = d_inode(h_parent);
  4183. + IMustLock(h_dir);
  4184. + AuDebugOn(h_parent != h_dst->d_parent);
  4185. +
  4186. + sb = cpg->dentry->d_sb;
  4187. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4188. + if (do_dt) {
  4189. + h_path.dentry = h_parent;
  4190. + au_dtime_store(&dt, dst_parent, &h_path);
  4191. + }
  4192. + h_path.dentry = h_dst;
  4193. +
  4194. + isreg = 0;
  4195. + isdir = 0;
  4196. + mode = h_inode->i_mode;
  4197. + switch (mode & S_IFMT) {
  4198. + case S_IFREG:
  4199. + isreg = 1;
  4200. + err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
  4201. + /*want_excl*/true);
  4202. + if (!err)
  4203. + err = au_do_cpup_regular(cpg, h_src_attr);
  4204. + break;
  4205. + case S_IFDIR:
  4206. + isdir = 1;
  4207. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4208. + if (!err)
  4209. + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
  4210. + break;
  4211. + case S_IFLNK:
  4212. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4213. + break;
  4214. + case S_IFCHR:
  4215. + case S_IFBLK:
  4216. + AuDebugOn(!capable(CAP_MKNOD));
  4217. + /*FALLTHROUGH*/
  4218. + case S_IFIFO:
  4219. + case S_IFSOCK:
  4220. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4221. + break;
  4222. + default:
  4223. + AuIOErr("Unknown inode type 0%o\n", mode);
  4224. + err = -EIO;
  4225. + }
  4226. + if (!err)
  4227. + err = au_reset_acl(h_dir, &h_path, mode);
  4228. +
  4229. + mnt_flags = au_mntflags(sb);
  4230. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4231. + && !isdir
  4232. + && au_opt_test(mnt_flags, XINO)
  4233. + && (h_inode->i_nlink == 1
  4234. + || (h_inode->i_state & I_LINKABLE))
  4235. + /* todo: unnecessary? */
  4236. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4237. + && cpg->bdst < cpg->bsrc
  4238. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4239. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4240. + /* ignore this error */
  4241. +
  4242. + if (!err) {
  4243. + force = 0;
  4244. + if (isreg) {
  4245. + force = !!cpg->len;
  4246. + if (cpg->len == -1)
  4247. + force = !!i_size_read(h_inode);
  4248. + }
  4249. + au_fhsm_wrote(sb, cpg->bdst, force);
  4250. + }
  4251. +
  4252. + if (do_dt)
  4253. + au_dtime_revert(&dt);
  4254. + return err;
  4255. +}
  4256. +
  4257. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4258. +{
  4259. + int err;
  4260. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4261. + struct inode *h_dir;
  4262. + aufs_bindex_t bdst;
  4263. +
  4264. + dentry = cpg->dentry;
  4265. + bdst = cpg->bdst;
  4266. + h_dentry = au_h_dptr(dentry, bdst);
  4267. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4268. + dget(h_dentry);
  4269. + au_set_h_dptr(dentry, bdst, NULL);
  4270. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4271. + if (!err)
  4272. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4273. + au_set_h_dptr(dentry, bdst, h_dentry);
  4274. + } else {
  4275. + err = 0;
  4276. + parent = dget_parent(dentry);
  4277. + h_parent = au_h_dptr(parent, bdst);
  4278. + dput(parent);
  4279. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  4280. + if (IS_ERR(h_path->dentry))
  4281. + err = PTR_ERR(h_path->dentry);
  4282. + }
  4283. + if (unlikely(err))
  4284. + goto out;
  4285. +
  4286. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4287. + h_dir = d_inode(h_parent);
  4288. + IMustLock(h_dir);
  4289. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4290. + /* no delegation since it is just created */
  4291. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
  4292. + dput(h_path->dentry);
  4293. +
  4294. +out:
  4295. + return err;
  4296. +}
  4297. +
  4298. +/*
  4299. + * copyup the @dentry from @bsrc to @bdst.
  4300. + * the caller must set the both of lower dentries.
  4301. + * @len is for truncating when it is -1 copyup the entire file.
  4302. + * in link/rename cases, @dst_parent may be different from the real one.
  4303. + * basic->bsrc can be larger than basic->bdst.
  4304. + */
  4305. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4306. +{
  4307. + int err, rerr;
  4308. + aufs_bindex_t old_ibtop;
  4309. + unsigned char isdir, plink;
  4310. + struct dentry *h_src, *h_dst, *h_parent;
  4311. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4312. + struct super_block *sb;
  4313. + struct au_branch *br;
  4314. + /* to reuduce stack size */
  4315. + struct {
  4316. + struct au_dtime dt;
  4317. + struct path h_path;
  4318. + struct au_cpup_reg_attr h_src_attr;
  4319. + } *a;
  4320. +
  4321. + err = -ENOMEM;
  4322. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4323. + if (unlikely(!a))
  4324. + goto out;
  4325. + a->h_src_attr.valid = 0;
  4326. +
  4327. + sb = cpg->dentry->d_sb;
  4328. + br = au_sbr(sb, cpg->bdst);
  4329. + a->h_path.mnt = au_br_mnt(br);
  4330. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4331. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4332. + h_dir = d_inode(h_parent);
  4333. + IMustLock(h_dir);
  4334. +
  4335. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4336. + inode = d_inode(cpg->dentry);
  4337. +
  4338. + if (!dst_parent)
  4339. + dst_parent = dget_parent(cpg->dentry);
  4340. + else
  4341. + dget(dst_parent);
  4342. +
  4343. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4344. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4345. + if (dst_inode) {
  4346. + if (unlikely(!plink)) {
  4347. + err = -EIO;
  4348. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4349. + "but plink is disabled\n",
  4350. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4351. + goto out_parent;
  4352. + }
  4353. +
  4354. + if (dst_inode->i_nlink) {
  4355. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4356. +
  4357. + h_src = au_plink_lkup(inode, cpg->bdst);
  4358. + err = PTR_ERR(h_src);
  4359. + if (IS_ERR(h_src))
  4360. + goto out_parent;
  4361. + if (unlikely(d_is_negative(h_src))) {
  4362. + err = -EIO;
  4363. + AuIOErr("i%lu exists on b%d "
  4364. + "but not pseudo-linked\n",
  4365. + inode->i_ino, cpg->bdst);
  4366. + dput(h_src);
  4367. + goto out_parent;
  4368. + }
  4369. +
  4370. + if (do_dt) {
  4371. + a->h_path.dentry = h_parent;
  4372. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4373. + }
  4374. +
  4375. + a->h_path.dentry = h_dst;
  4376. + delegated = NULL;
  4377. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4378. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4379. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4380. + if (do_dt)
  4381. + au_dtime_revert(&a->dt);
  4382. + if (unlikely(err == -EWOULDBLOCK)) {
  4383. + pr_warn("cannot retry for NFSv4 delegation"
  4384. + " for an internal link\n");
  4385. + iput(delegated);
  4386. + }
  4387. + dput(h_src);
  4388. + goto out_parent;
  4389. + } else
  4390. + /* todo: cpup_wh_file? */
  4391. + /* udba work */
  4392. + au_update_ibrange(inode, /*do_put_zero*/1);
  4393. + }
  4394. +
  4395. + isdir = S_ISDIR(inode->i_mode);
  4396. + old_ibtop = au_ibtop(inode);
  4397. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4398. + if (unlikely(err))
  4399. + goto out_rev;
  4400. + dst_inode = d_inode(h_dst);
  4401. + inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
  4402. + /* todo: necessary? */
  4403. + /* au_pin_hdir_unlock(cpg->pin); */
  4404. +
  4405. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  4406. + if (unlikely(err)) {
  4407. + /* todo: necessary? */
  4408. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4409. + inode_unlock(dst_inode);
  4410. + goto out_rev;
  4411. + }
  4412. +
  4413. + if (cpg->bdst < old_ibtop) {
  4414. + if (S_ISREG(inode->i_mode)) {
  4415. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4416. + if (unlikely(err)) {
  4417. + /* ignore an error */
  4418. + /* au_pin_hdir_relock(cpg->pin); */
  4419. + inode_unlock(dst_inode);
  4420. + goto out_rev;
  4421. + }
  4422. + }
  4423. + au_set_ibtop(inode, cpg->bdst);
  4424. + } else
  4425. + au_set_ibbot(inode, cpg->bdst);
  4426. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4427. + au_hi_flags(inode, isdir));
  4428. +
  4429. + /* todo: necessary? */
  4430. + /* err = au_pin_hdir_relock(cpg->pin); */
  4431. + inode_unlock(dst_inode);
  4432. + if (unlikely(err))
  4433. + goto out_rev;
  4434. +
  4435. + src_inode = d_inode(h_src);
  4436. + if (!isdir
  4437. + && (src_inode->i_nlink > 1
  4438. + || src_inode->i_state & I_LINKABLE)
  4439. + && plink)
  4440. + au_plink_append(inode, cpg->bdst, h_dst);
  4441. +
  4442. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4443. + a->h_path.dentry = h_dst;
  4444. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4445. + }
  4446. + if (!err)
  4447. + goto out_parent; /* success */
  4448. +
  4449. + /* revert */
  4450. +out_rev:
  4451. + a->h_path.dentry = h_parent;
  4452. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4453. + a->h_path.dentry = h_dst;
  4454. + rerr = 0;
  4455. + if (d_is_positive(h_dst)) {
  4456. + if (!isdir) {
  4457. + /* no delegation since it is just created */
  4458. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4459. + /*delegated*/NULL, /*force*/0);
  4460. + } else
  4461. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4462. + }
  4463. + au_dtime_revert(&a->dt);
  4464. + if (rerr) {
  4465. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4466. + err = -EIO;
  4467. + }
  4468. +out_parent:
  4469. + dput(dst_parent);
  4470. + au_delayed_kfree(a);
  4471. +out:
  4472. + return err;
  4473. +}
  4474. +
  4475. +#if 0 /* reserved */
  4476. +struct au_cpup_single_args {
  4477. + int *errp;
  4478. + struct au_cp_generic *cpg;
  4479. + struct dentry *dst_parent;
  4480. +};
  4481. +
  4482. +static void au_call_cpup_single(void *args)
  4483. +{
  4484. + struct au_cpup_single_args *a = args;
  4485. +
  4486. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4487. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4488. + au_pin_hdir_release(a->cpg->pin);
  4489. +}
  4490. +#endif
  4491. +
  4492. +/*
  4493. + * prevent SIGXFSZ in copy-up.
  4494. + * testing CAP_MKNOD is for generic fs,
  4495. + * but CAP_FSETID is for xfs only, currently.
  4496. + */
  4497. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4498. +{
  4499. + int do_sio;
  4500. + struct super_block *sb;
  4501. + struct inode *h_dir;
  4502. +
  4503. + do_sio = 0;
  4504. + sb = au_pinned_parent(pin)->d_sb;
  4505. + if (!au_wkq_test()
  4506. + && (!au_sbi(sb)->si_plink_maint_pid
  4507. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4508. + switch (mode & S_IFMT) {
  4509. + case S_IFREG:
  4510. + /* no condition about RLIMIT_FSIZE and the file size */
  4511. + do_sio = 1;
  4512. + break;
  4513. + case S_IFCHR:
  4514. + case S_IFBLK:
  4515. + do_sio = !capable(CAP_MKNOD);
  4516. + break;
  4517. + }
  4518. + if (!do_sio)
  4519. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4520. + && !capable(CAP_FSETID));
  4521. + /* this workaround may be removed in the future */
  4522. + if (!do_sio) {
  4523. + h_dir = au_pinned_h_dir(pin);
  4524. + do_sio = h_dir->i_mode & S_ISVTX;
  4525. + }
  4526. + }
  4527. +
  4528. + return do_sio;
  4529. +}
  4530. +
  4531. +#if 0 /* reserved */
  4532. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4533. +{
  4534. + int err, wkq_err;
  4535. + struct dentry *h_dentry;
  4536. +
  4537. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4538. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4539. + err = au_cpup_single(cpg, dst_parent);
  4540. + else {
  4541. + struct au_cpup_single_args args = {
  4542. + .errp = &err,
  4543. + .cpg = cpg,
  4544. + .dst_parent = dst_parent
  4545. + };
  4546. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4547. + if (unlikely(wkq_err))
  4548. + err = wkq_err;
  4549. + }
  4550. +
  4551. + return err;
  4552. +}
  4553. +#endif
  4554. +
  4555. +/*
  4556. + * copyup the @dentry from the first active lower branch to @bdst,
  4557. + * using au_cpup_single().
  4558. + */
  4559. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4560. +{
  4561. + int err;
  4562. + unsigned int flags_orig;
  4563. + struct dentry *dentry;
  4564. +
  4565. + AuDebugOn(cpg->bsrc < 0);
  4566. +
  4567. + dentry = cpg->dentry;
  4568. + DiMustWriteLock(dentry);
  4569. +
  4570. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4571. + if (!err) {
  4572. + flags_orig = cpg->flags;
  4573. + au_fset_cpup(cpg->flags, RENAME);
  4574. + err = au_cpup_single(cpg, NULL);
  4575. + cpg->flags = flags_orig;
  4576. + if (!err)
  4577. + return 0; /* success */
  4578. +
  4579. + /* revert */
  4580. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4581. + au_set_dbtop(dentry, cpg->bsrc);
  4582. + }
  4583. +
  4584. + return err;
  4585. +}
  4586. +
  4587. +struct au_cpup_simple_args {
  4588. + int *errp;
  4589. + struct au_cp_generic *cpg;
  4590. +};
  4591. +
  4592. +static void au_call_cpup_simple(void *args)
  4593. +{
  4594. + struct au_cpup_simple_args *a = args;
  4595. +
  4596. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4597. + *a->errp = au_cpup_simple(a->cpg);
  4598. + au_pin_hdir_release(a->cpg->pin);
  4599. +}
  4600. +
  4601. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4602. +{
  4603. + int err, wkq_err;
  4604. + struct dentry *dentry, *parent;
  4605. + struct file *h_file;
  4606. + struct inode *h_dir;
  4607. +
  4608. + dentry = cpg->dentry;
  4609. + h_file = NULL;
  4610. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4611. + AuDebugOn(cpg->bsrc < 0);
  4612. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4613. + err = PTR_ERR(h_file);
  4614. + if (IS_ERR(h_file))
  4615. + goto out;
  4616. + }
  4617. +
  4618. + parent = dget_parent(dentry);
  4619. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4620. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  4621. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4622. + err = au_cpup_simple(cpg);
  4623. + else {
  4624. + struct au_cpup_simple_args args = {
  4625. + .errp = &err,
  4626. + .cpg = cpg
  4627. + };
  4628. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4629. + if (unlikely(wkq_err))
  4630. + err = wkq_err;
  4631. + }
  4632. +
  4633. + dput(parent);
  4634. + if (h_file)
  4635. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4636. +
  4637. +out:
  4638. + return err;
  4639. +}
  4640. +
  4641. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4642. +{
  4643. + aufs_bindex_t bsrc, bbot;
  4644. + struct dentry *dentry, *h_dentry;
  4645. +
  4646. + if (cpg->bsrc < 0) {
  4647. + dentry = cpg->dentry;
  4648. + bbot = au_dbbot(dentry);
  4649. + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
  4650. + h_dentry = au_h_dptr(dentry, bsrc);
  4651. + if (h_dentry) {
  4652. + AuDebugOn(d_is_negative(h_dentry));
  4653. + break;
  4654. + }
  4655. + }
  4656. + AuDebugOn(bsrc > bbot);
  4657. + cpg->bsrc = bsrc;
  4658. + }
  4659. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4660. + return au_do_sio_cpup_simple(cpg);
  4661. +}
  4662. +
  4663. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4664. +{
  4665. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4666. + return au_do_sio_cpup_simple(cpg);
  4667. +}
  4668. +
  4669. +/* ---------------------------------------------------------------------- */
  4670. +
  4671. +/*
  4672. + * copyup the deleted file for writing.
  4673. + */
  4674. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  4675. + struct file *file)
  4676. +{
  4677. + int err;
  4678. + unsigned int flags_orig;
  4679. + aufs_bindex_t bsrc_orig;
  4680. + struct au_dinfo *dinfo;
  4681. + struct {
  4682. + struct au_hdentry *hd;
  4683. + struct dentry *h_dentry;
  4684. + } hdst, hsrc;
  4685. +
  4686. + dinfo = au_di(cpg->dentry);
  4687. + AuRwMustWriteLock(&dinfo->di_rwsem);
  4688. +
  4689. + bsrc_orig = cpg->bsrc;
  4690. + cpg->bsrc = dinfo->di_btop;
  4691. + hdst.hd = au_hdentry(dinfo, cpg->bdst);
  4692. + hdst.h_dentry = hdst.hd->hd_dentry;
  4693. + hdst.hd->hd_dentry = wh_dentry;
  4694. + dinfo->di_btop = cpg->bdst;
  4695. +
  4696. + hsrc.h_dentry = NULL;
  4697. + if (file) {
  4698. + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
  4699. + hsrc.h_dentry = hsrc.hd->hd_dentry;
  4700. + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
  4701. + }
  4702. + flags_orig = cpg->flags;
  4703. + cpg->flags = !AuCpup_DTIME;
  4704. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  4705. + cpg->flags = flags_orig;
  4706. + if (file) {
  4707. + if (!err)
  4708. + err = au_reopen_nondir(file);
  4709. + hsrc.hd->hd_dentry = hsrc.h_dentry;
  4710. + }
  4711. + hdst.hd->hd_dentry = hdst.h_dentry;
  4712. + dinfo->di_btop = cpg->bsrc;
  4713. + cpg->bsrc = bsrc_orig;
  4714. +
  4715. + return err;
  4716. +}
  4717. +
  4718. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4719. +{
  4720. + int err;
  4721. + aufs_bindex_t bdst;
  4722. + struct au_dtime dt;
  4723. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  4724. + struct au_branch *br;
  4725. + struct path h_path;
  4726. +
  4727. + dentry = cpg->dentry;
  4728. + bdst = cpg->bdst;
  4729. + br = au_sbr(dentry->d_sb, bdst);
  4730. + parent = dget_parent(dentry);
  4731. + h_parent = au_h_dptr(parent, bdst);
  4732. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  4733. + err = PTR_ERR(wh_dentry);
  4734. + if (IS_ERR(wh_dentry))
  4735. + goto out;
  4736. +
  4737. + h_path.dentry = h_parent;
  4738. + h_path.mnt = au_br_mnt(br);
  4739. + au_dtime_store(&dt, parent, &h_path);
  4740. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  4741. + if (unlikely(err))
  4742. + goto out_wh;
  4743. +
  4744. + dget(wh_dentry);
  4745. + h_path.dentry = wh_dentry;
  4746. + if (!d_is_dir(wh_dentry)) {
  4747. + /* no delegation since it is just created */
  4748. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  4749. + /*delegated*/NULL, /*force*/0);
  4750. + } else
  4751. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  4752. + if (unlikely(err)) {
  4753. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  4754. + wh_dentry, err);
  4755. + err = -EIO;
  4756. + }
  4757. + au_dtime_revert(&dt);
  4758. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  4759. +
  4760. +out_wh:
  4761. + dput(wh_dentry);
  4762. +out:
  4763. + dput(parent);
  4764. + return err;
  4765. +}
  4766. +
  4767. +struct au_cpup_wh_args {
  4768. + int *errp;
  4769. + struct au_cp_generic *cpg;
  4770. + struct file *file;
  4771. +};
  4772. +
  4773. +static void au_call_cpup_wh(void *args)
  4774. +{
  4775. + struct au_cpup_wh_args *a = args;
  4776. +
  4777. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4778. + *a->errp = au_cpup_wh(a->cpg, a->file);
  4779. + au_pin_hdir_release(a->cpg->pin);
  4780. +}
  4781. +
  4782. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4783. +{
  4784. + int err, wkq_err;
  4785. + aufs_bindex_t bdst;
  4786. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  4787. + struct inode *dir, *h_dir, *h_tmpdir;
  4788. + struct au_wbr *wbr;
  4789. + struct au_pin wh_pin, *pin_orig;
  4790. +
  4791. + dentry = cpg->dentry;
  4792. + bdst = cpg->bdst;
  4793. + parent = dget_parent(dentry);
  4794. + dir = d_inode(parent);
  4795. + h_orph = NULL;
  4796. + h_parent = NULL;
  4797. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  4798. + h_tmpdir = h_dir;
  4799. + pin_orig = NULL;
  4800. + if (!h_dir->i_nlink) {
  4801. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  4802. + h_orph = wbr->wbr_orph;
  4803. +
  4804. + h_parent = dget(au_h_dptr(parent, bdst));
  4805. + au_set_h_dptr(parent, bdst, dget(h_orph));
  4806. + h_tmpdir = d_inode(h_orph);
  4807. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  4808. +
  4809. + inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
  4810. + /* todo: au_h_open_pre()? */
  4811. +
  4812. + pin_orig = cpg->pin;
  4813. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  4814. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  4815. + cpg->pin = &wh_pin;
  4816. + }
  4817. +
  4818. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  4819. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4820. + err = au_cpup_wh(cpg, file);
  4821. + else {
  4822. + struct au_cpup_wh_args args = {
  4823. + .errp = &err,
  4824. + .cpg = cpg,
  4825. + .file = file
  4826. + };
  4827. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  4828. + if (unlikely(wkq_err))
  4829. + err = wkq_err;
  4830. + }
  4831. +
  4832. + if (h_orph) {
  4833. + inode_unlock(h_tmpdir);
  4834. + /* todo: au_h_open_post()? */
  4835. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  4836. + au_set_h_dptr(parent, bdst, h_parent);
  4837. + AuDebugOn(!pin_orig);
  4838. + cpg->pin = pin_orig;
  4839. + }
  4840. + iput(h_dir);
  4841. + dput(parent);
  4842. +
  4843. + return err;
  4844. +}
  4845. +
  4846. +/* ---------------------------------------------------------------------- */
  4847. +
  4848. +/*
  4849. + * generic routine for both of copy-up and copy-down.
  4850. + */
  4851. +/* cf. revalidate function in file.c */
  4852. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  4853. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  4854. + struct au_pin *pin,
  4855. + struct dentry *h_parent, void *arg),
  4856. + void *arg)
  4857. +{
  4858. + int err;
  4859. + struct au_pin pin;
  4860. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  4861. +
  4862. + err = 0;
  4863. + parent = dget_parent(dentry);
  4864. + if (IS_ROOT(parent))
  4865. + goto out;
  4866. +
  4867. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  4868. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  4869. +
  4870. + /* do not use au_dpage */
  4871. + real_parent = parent;
  4872. + while (1) {
  4873. + dput(parent);
  4874. + parent = dget_parent(dentry);
  4875. + h_parent = au_h_dptr(parent, bdst);
  4876. + if (h_parent)
  4877. + goto out; /* success */
  4878. +
  4879. + /* find top dir which is necessary to cpup */
  4880. + do {
  4881. + d = parent;
  4882. + dput(parent);
  4883. + parent = dget_parent(d);
  4884. + di_read_lock_parent3(parent, !AuLock_IR);
  4885. + h_parent = au_h_dptr(parent, bdst);
  4886. + di_read_unlock(parent, !AuLock_IR);
  4887. + } while (!h_parent);
  4888. +
  4889. + if (d != real_parent)
  4890. + di_write_lock_child3(d);
  4891. +
  4892. + /* somebody else might create while we were sleeping */
  4893. + h_dentry = au_h_dptr(d, bdst);
  4894. + if (!h_dentry || d_is_negative(h_dentry)) {
  4895. + if (h_dentry)
  4896. + au_update_dbtop(d);
  4897. +
  4898. + au_pin_set_dentry(&pin, d);
  4899. + err = au_do_pin(&pin);
  4900. + if (!err) {
  4901. + err = cp(d, bdst, &pin, h_parent, arg);
  4902. + au_unpin(&pin);
  4903. + }
  4904. + }
  4905. +
  4906. + if (d != real_parent)
  4907. + di_write_unlock(d);
  4908. + if (unlikely(err))
  4909. + break;
  4910. + }
  4911. +
  4912. +out:
  4913. + dput(parent);
  4914. + return err;
  4915. +}
  4916. +
  4917. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  4918. + struct au_pin *pin,
  4919. + struct dentry *h_parent __maybe_unused,
  4920. + void *arg __maybe_unused)
  4921. +{
  4922. + struct au_cp_generic cpg = {
  4923. + .dentry = dentry,
  4924. + .bdst = bdst,
  4925. + .bsrc = -1,
  4926. + .len = 0,
  4927. + .pin = pin,
  4928. + .flags = AuCpup_DTIME
  4929. + };
  4930. + return au_sio_cpup_simple(&cpg);
  4931. +}
  4932. +
  4933. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4934. +{
  4935. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  4936. +}
  4937. +
  4938. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4939. +{
  4940. + int err;
  4941. + struct dentry *parent;
  4942. + struct inode *dir;
  4943. +
  4944. + parent = dget_parent(dentry);
  4945. + dir = d_inode(parent);
  4946. + err = 0;
  4947. + if (au_h_iptr(dir, bdst))
  4948. + goto out;
  4949. +
  4950. + di_read_unlock(parent, AuLock_IR);
  4951. + di_write_lock_parent(parent);
  4952. + /* someone else might change our inode while we were sleeping */
  4953. + if (!au_h_iptr(dir, bdst))
  4954. + err = au_cpup_dirs(dentry, bdst);
  4955. + di_downgrade_lock(parent, AuLock_IR);
  4956. +
  4957. +out:
  4958. + dput(parent);
  4959. + return err;
  4960. +}
  4961. diff -Naur null/fs/aufs/cpup.h linux-4.7/fs/aufs/cpup.h
  4962. --- /dev/null
  4963. +++ linux-4.7/fs/aufs/cpup.h 2016-08-08 12:57:44.598384764 -0400
  4964. @@ -0,0 +1,94 @@
  4965. +/*
  4966. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  4967. + *
  4968. + * This program, aufs is free software; you can redistribute it and/or modify
  4969. + * it under the terms of the GNU General Public License as published by
  4970. + * the Free Software Foundation; either version 2 of the License, or
  4971. + * (at your option) any later version.
  4972. + *
  4973. + * This program is distributed in the hope that it will be useful,
  4974. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4975. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4976. + * GNU General Public License for more details.
  4977. + *
  4978. + * You should have received a copy of the GNU General Public License
  4979. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  4980. + */
  4981. +
  4982. +/*
  4983. + * copy-up/down functions
  4984. + */
  4985. +
  4986. +#ifndef __AUFS_CPUP_H__
  4987. +#define __AUFS_CPUP_H__
  4988. +
  4989. +#ifdef __KERNEL__
  4990. +
  4991. +#include <linux/path.h>
  4992. +
  4993. +struct inode;
  4994. +struct file;
  4995. +struct au_pin;
  4996. +
  4997. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  4998. +void au_cpup_attr_timesizes(struct inode *inode);
  4999. +void au_cpup_attr_nlink(struct inode *inode, int force);
  5000. +void au_cpup_attr_changeable(struct inode *inode);
  5001. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  5002. +void au_cpup_attr_all(struct inode *inode, int force);
  5003. +
  5004. +/* ---------------------------------------------------------------------- */
  5005. +
  5006. +struct au_cp_generic {
  5007. + struct dentry *dentry;
  5008. + aufs_bindex_t bdst, bsrc;
  5009. + loff_t len;
  5010. + struct au_pin *pin;
  5011. + unsigned int flags;
  5012. +};
  5013. +
  5014. +/* cpup flags */
  5015. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  5016. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  5017. + for link(2) */
  5018. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  5019. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  5020. + cpup */
  5021. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  5022. + existing entry */
  5023. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  5024. + the branch is marked as RO */
  5025. +
  5026. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  5027. +#define au_fset_cpup(flags, name) \
  5028. + do { (flags) |= AuCpup_##name; } while (0)
  5029. +#define au_fclr_cpup(flags, name) \
  5030. + do { (flags) &= ~AuCpup_##name; } while (0)
  5031. +
  5032. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  5033. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  5034. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  5035. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  5036. +
  5037. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5038. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5039. + struct au_pin *pin,
  5040. + struct dentry *h_parent, void *arg),
  5041. + void *arg);
  5042. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5043. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5044. +
  5045. +/* ---------------------------------------------------------------------- */
  5046. +
  5047. +/* keep timestamps when copyup */
  5048. +struct au_dtime {
  5049. + struct dentry *dt_dentry;
  5050. + struct path dt_h_path;
  5051. + struct timespec dt_atime, dt_mtime;
  5052. +};
  5053. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  5054. + struct path *h_path);
  5055. +void au_dtime_revert(struct au_dtime *dt);
  5056. +
  5057. +#endif /* __KERNEL__ */
  5058. +#endif /* __AUFS_CPUP_H__ */
  5059. diff -Naur null/fs/aufs/dbgaufs.c linux-4.7/fs/aufs/dbgaufs.c
  5060. --- /dev/null
  5061. +++ linux-4.7/fs/aufs/dbgaufs.c 2016-08-08 12:57:44.598384764 -0400
  5062. @@ -0,0 +1,432 @@
  5063. +/*
  5064. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5065. + *
  5066. + * This program, aufs is free software; you can redistribute it and/or modify
  5067. + * it under the terms of the GNU General Public License as published by
  5068. + * the Free Software Foundation; either version 2 of the License, or
  5069. + * (at your option) any later version.
  5070. + *
  5071. + * This program is distributed in the hope that it will be useful,
  5072. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5073. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5074. + * GNU General Public License for more details.
  5075. + *
  5076. + * You should have received a copy of the GNU General Public License
  5077. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5078. + */
  5079. +
  5080. +/*
  5081. + * debugfs interface
  5082. + */
  5083. +
  5084. +#include <linux/debugfs.h>
  5085. +#include "aufs.h"
  5086. +
  5087. +#ifndef CONFIG_SYSFS
  5088. +#error DEBUG_FS depends upon SYSFS
  5089. +#endif
  5090. +
  5091. +static struct dentry *dbgaufs;
  5092. +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
  5093. +
  5094. +/* 20 is max digits length of ulong 64 */
  5095. +struct dbgaufs_arg {
  5096. + int n;
  5097. + char a[20 * 4];
  5098. +};
  5099. +
  5100. +/*
  5101. + * common function for all XINO files
  5102. + */
  5103. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5104. + struct file *file)
  5105. +{
  5106. + au_delayed_kfree(file->private_data);
  5107. + return 0;
  5108. +}
  5109. +
  5110. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
  5111. +{
  5112. + int err;
  5113. + struct kstat st;
  5114. + struct dbgaufs_arg *p;
  5115. +
  5116. + err = -ENOMEM;
  5117. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5118. + if (unlikely(!p))
  5119. + goto out;
  5120. +
  5121. + err = 0;
  5122. + p->n = 0;
  5123. + file->private_data = p;
  5124. + if (!xf)
  5125. + goto out;
  5126. +
  5127. + err = vfs_getattr(&xf->f_path, &st);
  5128. + if (!err) {
  5129. + if (do_fcnt)
  5130. + p->n = snprintf
  5131. + (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
  5132. + (long)file_count(xf), st.blocks, st.blksize,
  5133. + (long long)st.size);
  5134. + else
  5135. + p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
  5136. + st.blocks, st.blksize,
  5137. + (long long)st.size);
  5138. + AuDebugOn(p->n >= sizeof(p->a));
  5139. + } else {
  5140. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5141. + err = 0;
  5142. + }
  5143. +
  5144. +out:
  5145. + return err;
  5146. +
  5147. +}
  5148. +
  5149. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5150. + size_t count, loff_t *ppos)
  5151. +{
  5152. + struct dbgaufs_arg *p;
  5153. +
  5154. + p = file->private_data;
  5155. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5156. +}
  5157. +
  5158. +/* ---------------------------------------------------------------------- */
  5159. +
  5160. +struct dbgaufs_plink_arg {
  5161. + int n;
  5162. + char a[];
  5163. +};
  5164. +
  5165. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5166. + struct file *file)
  5167. +{
  5168. + au_delayed_free_page((unsigned long)file->private_data);
  5169. + return 0;
  5170. +}
  5171. +
  5172. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5173. +{
  5174. + int err, i, limit;
  5175. + unsigned long n, sum;
  5176. + struct dbgaufs_plink_arg *p;
  5177. + struct au_sbinfo *sbinfo;
  5178. + struct super_block *sb;
  5179. + struct au_sphlhead *sphl;
  5180. +
  5181. + err = -ENOMEM;
  5182. + p = (void *)get_zeroed_page(GFP_NOFS);
  5183. + if (unlikely(!p))
  5184. + goto out;
  5185. +
  5186. + err = -EFBIG;
  5187. + sbinfo = inode->i_private;
  5188. + sb = sbinfo->si_sb;
  5189. + si_noflush_read_lock(sb);
  5190. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5191. + limit = PAGE_SIZE - sizeof(p->n);
  5192. +
  5193. + /* the number of buckets */
  5194. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5195. + p->n += n;
  5196. + limit -= n;
  5197. +
  5198. + sum = 0;
  5199. + for (i = 0, sphl = sbinfo->si_plink;
  5200. + i < AuPlink_NHASH;
  5201. + i++, sphl++) {
  5202. + n = au_sphl_count(sphl);
  5203. + sum += n;
  5204. +
  5205. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5206. + p->n += n;
  5207. + limit -= n;
  5208. + if (unlikely(limit <= 0))
  5209. + goto out_free;
  5210. + }
  5211. + p->a[p->n - 1] = '\n';
  5212. +
  5213. + /* the sum of plinks */
  5214. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5215. + p->n += n;
  5216. + limit -= n;
  5217. + if (unlikely(limit <= 0))
  5218. + goto out_free;
  5219. + } else {
  5220. +#define str "1\n0\n0\n"
  5221. + p->n = sizeof(str) - 1;
  5222. + strcpy(p->a, str);
  5223. +#undef str
  5224. + }
  5225. + si_read_unlock(sb);
  5226. +
  5227. + err = 0;
  5228. + file->private_data = p;
  5229. + goto out; /* success */
  5230. +
  5231. +out_free:
  5232. + au_delayed_free_page((unsigned long)p);
  5233. +out:
  5234. + return err;
  5235. +}
  5236. +
  5237. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5238. + size_t count, loff_t *ppos)
  5239. +{
  5240. + struct dbgaufs_plink_arg *p;
  5241. +
  5242. + p = file->private_data;
  5243. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5244. +}
  5245. +
  5246. +static const struct file_operations dbgaufs_plink_fop = {
  5247. + .owner = THIS_MODULE,
  5248. + .open = dbgaufs_plink_open,
  5249. + .release = dbgaufs_plink_release,
  5250. + .read = dbgaufs_plink_read
  5251. +};
  5252. +
  5253. +/* ---------------------------------------------------------------------- */
  5254. +
  5255. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5256. +{
  5257. + int err;
  5258. + struct au_sbinfo *sbinfo;
  5259. + struct super_block *sb;
  5260. +
  5261. + sbinfo = inode->i_private;
  5262. + sb = sbinfo->si_sb;
  5263. + si_noflush_read_lock(sb);
  5264. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
  5265. + si_read_unlock(sb);
  5266. + return err;
  5267. +}
  5268. +
  5269. +static const struct file_operations dbgaufs_xib_fop = {
  5270. + .owner = THIS_MODULE,
  5271. + .open = dbgaufs_xib_open,
  5272. + .release = dbgaufs_xi_release,
  5273. + .read = dbgaufs_xi_read
  5274. +};
  5275. +
  5276. +/* ---------------------------------------------------------------------- */
  5277. +
  5278. +#define DbgaufsXi_PREFIX "xi"
  5279. +
  5280. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5281. +{
  5282. + int err;
  5283. + long l;
  5284. + struct au_sbinfo *sbinfo;
  5285. + struct super_block *sb;
  5286. + struct file *xf;
  5287. + struct qstr *name;
  5288. +
  5289. + err = -ENOENT;
  5290. + xf = NULL;
  5291. + name = &file->f_path.dentry->d_name;
  5292. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5293. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5294. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5295. + goto out;
  5296. + err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5297. + if (unlikely(err))
  5298. + goto out;
  5299. +
  5300. + sbinfo = inode->i_private;
  5301. + sb = sbinfo->si_sb;
  5302. + si_noflush_read_lock(sb);
  5303. + if (l <= au_sbbot(sb)) {
  5304. + xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
  5305. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
  5306. + } else
  5307. + err = -ENOENT;
  5308. + si_read_unlock(sb);
  5309. +
  5310. +out:
  5311. + return err;
  5312. +}
  5313. +
  5314. +static const struct file_operations dbgaufs_xino_fop = {
  5315. + .owner = THIS_MODULE,
  5316. + .open = dbgaufs_xino_open,
  5317. + .release = dbgaufs_xi_release,
  5318. + .read = dbgaufs_xi_read
  5319. +};
  5320. +
  5321. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5322. +{
  5323. + aufs_bindex_t bbot;
  5324. + struct au_branch *br;
  5325. + struct au_xino_file *xi;
  5326. +
  5327. + if (!au_sbi(sb)->si_dbgaufs)
  5328. + return;
  5329. +
  5330. + bbot = au_sbbot(sb);
  5331. + for (; bindex <= bbot; bindex++) {
  5332. + br = au_sbr(sb, bindex);
  5333. + xi = &br->br_xino;
  5334. + debugfs_remove(xi->xi_dbgaufs);
  5335. + xi->xi_dbgaufs = NULL;
  5336. + }
  5337. +}
  5338. +
  5339. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  5340. +{
  5341. + struct au_sbinfo *sbinfo;
  5342. + struct dentry *parent;
  5343. + struct au_branch *br;
  5344. + struct au_xino_file *xi;
  5345. + aufs_bindex_t bbot;
  5346. + char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
  5347. +
  5348. + sbinfo = au_sbi(sb);
  5349. + parent = sbinfo->si_dbgaufs;
  5350. + if (!parent)
  5351. + return;
  5352. +
  5353. + bbot = au_sbbot(sb);
  5354. + for (; bindex <= bbot; bindex++) {
  5355. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5356. + br = au_sbr(sb, bindex);
  5357. + xi = &br->br_xino;
  5358. + AuDebugOn(xi->xi_dbgaufs);
  5359. + xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5360. + sbinfo, &dbgaufs_xino_fop);
  5361. + /* ignore an error */
  5362. + if (unlikely(!xi->xi_dbgaufs))
  5363. + AuWarn1("failed %s under debugfs\n", name);
  5364. + }
  5365. +}
  5366. +
  5367. +/* ---------------------------------------------------------------------- */
  5368. +
  5369. +#ifdef CONFIG_AUFS_EXPORT
  5370. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5371. +{
  5372. + int err;
  5373. + struct au_sbinfo *sbinfo;
  5374. + struct super_block *sb;
  5375. +
  5376. + sbinfo = inode->i_private;
  5377. + sb = sbinfo->si_sb;
  5378. + si_noflush_read_lock(sb);
  5379. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
  5380. + si_read_unlock(sb);
  5381. + return err;
  5382. +}
  5383. +
  5384. +static const struct file_operations dbgaufs_xigen_fop = {
  5385. + .owner = THIS_MODULE,
  5386. + .open = dbgaufs_xigen_open,
  5387. + .release = dbgaufs_xi_release,
  5388. + .read = dbgaufs_xi_read
  5389. +};
  5390. +
  5391. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5392. +{
  5393. + int err;
  5394. +
  5395. + /*
  5396. + * This function is a dynamic '__init' function actually,
  5397. + * so the tiny check for si_rwsem is unnecessary.
  5398. + */
  5399. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5400. +
  5401. + err = -EIO;
  5402. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5403. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5404. + &dbgaufs_xigen_fop);
  5405. + if (sbinfo->si_dbgaufs_xigen)
  5406. + err = 0;
  5407. +
  5408. + return err;
  5409. +}
  5410. +#else
  5411. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5412. +{
  5413. + return 0;
  5414. +}
  5415. +#endif /* CONFIG_AUFS_EXPORT */
  5416. +
  5417. +/* ---------------------------------------------------------------------- */
  5418. +
  5419. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5420. +{
  5421. + /*
  5422. + * This function is a dynamic '__fin' function actually,
  5423. + * so the tiny check for si_rwsem is unnecessary.
  5424. + */
  5425. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5426. +
  5427. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5428. + sbinfo->si_dbgaufs = NULL;
  5429. + kobject_put(&sbinfo->si_kobj);
  5430. +}
  5431. +
  5432. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5433. +{
  5434. + int err;
  5435. + char name[SysaufsSiNameLen];
  5436. +
  5437. + /*
  5438. + * This function is a dynamic '__init' function actually,
  5439. + * so the tiny check for si_rwsem is unnecessary.
  5440. + */
  5441. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5442. +
  5443. + err = -ENOENT;
  5444. + if (!dbgaufs) {
  5445. + AuErr1("/debug/aufs is uninitialized\n");
  5446. + goto out;
  5447. + }
  5448. +
  5449. + err = -EIO;
  5450. + sysaufs_name(sbinfo, name);
  5451. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5452. + if (unlikely(!sbinfo->si_dbgaufs))
  5453. + goto out;
  5454. + kobject_get(&sbinfo->si_kobj);
  5455. +
  5456. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5457. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5458. + &dbgaufs_xib_fop);
  5459. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5460. + goto out_dir;
  5461. +
  5462. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5463. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5464. + &dbgaufs_plink_fop);
  5465. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5466. + goto out_dir;
  5467. +
  5468. + err = dbgaufs_xigen_init(sbinfo);
  5469. + if (!err)
  5470. + goto out; /* success */
  5471. +
  5472. +out_dir:
  5473. + dbgaufs_si_fin(sbinfo);
  5474. +out:
  5475. + return err;
  5476. +}
  5477. +
  5478. +/* ---------------------------------------------------------------------- */
  5479. +
  5480. +void dbgaufs_fin(void)
  5481. +{
  5482. + debugfs_remove(dbgaufs);
  5483. +}
  5484. +
  5485. +int __init dbgaufs_init(void)
  5486. +{
  5487. + int err;
  5488. +
  5489. + err = -EIO;
  5490. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5491. + if (dbgaufs)
  5492. + err = 0;
  5493. + return err;
  5494. +}
  5495. diff -Naur null/fs/aufs/dbgaufs.h linux-4.7/fs/aufs/dbgaufs.h
  5496. --- /dev/null
  5497. +++ linux-4.7/fs/aufs/dbgaufs.h 2016-08-08 12:57:44.598384764 -0400
  5498. @@ -0,0 +1,48 @@
  5499. +/*
  5500. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5501. + *
  5502. + * This program, aufs is free software; you can redistribute it and/or modify
  5503. + * it under the terms of the GNU General Public License as published by
  5504. + * the Free Software Foundation; either version 2 of the License, or
  5505. + * (at your option) any later version.
  5506. + *
  5507. + * This program is distributed in the hope that it will be useful,
  5508. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5509. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5510. + * GNU General Public License for more details.
  5511. + *
  5512. + * You should have received a copy of the GNU General Public License
  5513. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5514. + */
  5515. +
  5516. +/*
  5517. + * debugfs interface
  5518. + */
  5519. +
  5520. +#ifndef __DBGAUFS_H__
  5521. +#define __DBGAUFS_H__
  5522. +
  5523. +#ifdef __KERNEL__
  5524. +
  5525. +struct super_block;
  5526. +struct au_sbinfo;
  5527. +
  5528. +#ifdef CONFIG_DEBUG_FS
  5529. +/* dbgaufs.c */
  5530. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5531. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  5532. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5533. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5534. +void dbgaufs_fin(void);
  5535. +int __init dbgaufs_init(void);
  5536. +#else
  5537. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5538. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  5539. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5540. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5541. +AuStubVoid(dbgaufs_fin, void)
  5542. +AuStubInt0(__init dbgaufs_init, void)
  5543. +#endif /* CONFIG_DEBUG_FS */
  5544. +
  5545. +#endif /* __KERNEL__ */
  5546. +#endif /* __DBGAUFS_H__ */
  5547. diff -Naur null/fs/aufs/dcsub.c linux-4.7/fs/aufs/dcsub.c
  5548. --- /dev/null
  5549. +++ linux-4.7/fs/aufs/dcsub.c 2016-08-08 12:57:44.598384764 -0400
  5550. @@ -0,0 +1,224 @@
  5551. +/*
  5552. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5553. + *
  5554. + * This program, aufs is free software; you can redistribute it and/or modify
  5555. + * it under the terms of the GNU General Public License as published by
  5556. + * the Free Software Foundation; either version 2 of the License, or
  5557. + * (at your option) any later version.
  5558. + *
  5559. + * This program is distributed in the hope that it will be useful,
  5560. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5561. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5562. + * GNU General Public License for more details.
  5563. + *
  5564. + * You should have received a copy of the GNU General Public License
  5565. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5566. + */
  5567. +
  5568. +/*
  5569. + * sub-routines for dentry cache
  5570. + */
  5571. +
  5572. +#include "aufs.h"
  5573. +
  5574. +static void au_dpage_free(struct au_dpage *dpage)
  5575. +{
  5576. + int i;
  5577. + struct dentry **p;
  5578. +
  5579. + p = dpage->dentries;
  5580. + for (i = 0; i < dpage->ndentry; i++)
  5581. + dput(*p++);
  5582. + au_delayed_free_page((unsigned long)dpage->dentries);
  5583. +}
  5584. +
  5585. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  5586. +{
  5587. + int err;
  5588. + void *p;
  5589. +
  5590. + err = -ENOMEM;
  5591. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  5592. + if (unlikely(!dpages->dpages))
  5593. + goto out;
  5594. +
  5595. + p = (void *)__get_free_page(gfp);
  5596. + if (unlikely(!p))
  5597. + goto out_dpages;
  5598. +
  5599. + dpages->dpages[0].ndentry = 0;
  5600. + dpages->dpages[0].dentries = p;
  5601. + dpages->ndpage = 1;
  5602. + return 0; /* success */
  5603. +
  5604. +out_dpages:
  5605. + au_delayed_kfree(dpages->dpages);
  5606. +out:
  5607. + return err;
  5608. +}
  5609. +
  5610. +void au_dpages_free(struct au_dcsub_pages *dpages)
  5611. +{
  5612. + int i;
  5613. + struct au_dpage *p;
  5614. +
  5615. + p = dpages->dpages;
  5616. + for (i = 0; i < dpages->ndpage; i++)
  5617. + au_dpage_free(p++);
  5618. + au_delayed_kfree(dpages->dpages);
  5619. +}
  5620. +
  5621. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  5622. + struct dentry *dentry, gfp_t gfp)
  5623. +{
  5624. + int err, sz;
  5625. + struct au_dpage *dpage;
  5626. + void *p;
  5627. +
  5628. + dpage = dpages->dpages + dpages->ndpage - 1;
  5629. + sz = PAGE_SIZE / sizeof(dentry);
  5630. + if (unlikely(dpage->ndentry >= sz)) {
  5631. + AuLabel(new dpage);
  5632. + err = -ENOMEM;
  5633. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  5634. + p = au_kzrealloc(dpages->dpages, sz,
  5635. + sz + sizeof(*dpages->dpages), gfp);
  5636. + if (unlikely(!p))
  5637. + goto out;
  5638. +
  5639. + dpages->dpages = p;
  5640. + dpage = dpages->dpages + dpages->ndpage;
  5641. + p = (void *)__get_free_page(gfp);
  5642. + if (unlikely(!p))
  5643. + goto out;
  5644. +
  5645. + dpage->ndentry = 0;
  5646. + dpage->dentries = p;
  5647. + dpages->ndpage++;
  5648. + }
  5649. +
  5650. + AuDebugOn(au_dcount(dentry) <= 0);
  5651. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  5652. + return 0; /* success */
  5653. +
  5654. +out:
  5655. + return err;
  5656. +}
  5657. +
  5658. +/* todo: BAD approach */
  5659. +/* copied from linux/fs/dcache.c */
  5660. +enum d_walk_ret {
  5661. + D_WALK_CONTINUE,
  5662. + D_WALK_QUIT,
  5663. + D_WALK_NORETRY,
  5664. + D_WALK_SKIP,
  5665. +};
  5666. +
  5667. +extern void d_walk(struct dentry *parent, void *data,
  5668. + enum d_walk_ret (*enter)(void *, struct dentry *),
  5669. + void (*finish)(void *));
  5670. +
  5671. +struct ac_dpages_arg {
  5672. + int err;
  5673. + struct au_dcsub_pages *dpages;
  5674. + struct super_block *sb;
  5675. + au_dpages_test test;
  5676. + void *arg;
  5677. +};
  5678. +
  5679. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  5680. +{
  5681. + enum d_walk_ret ret;
  5682. + struct ac_dpages_arg *arg = _arg;
  5683. +
  5684. + ret = D_WALK_CONTINUE;
  5685. + if (dentry->d_sb == arg->sb
  5686. + && !IS_ROOT(dentry)
  5687. + && au_dcount(dentry) > 0
  5688. + && au_di(dentry)
  5689. + && (!arg->test || arg->test(dentry, arg->arg))) {
  5690. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  5691. + if (unlikely(arg->err))
  5692. + ret = D_WALK_QUIT;
  5693. + }
  5694. +
  5695. + return ret;
  5696. +}
  5697. +
  5698. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5699. + au_dpages_test test, void *arg)
  5700. +{
  5701. + struct ac_dpages_arg args = {
  5702. + .err = 0,
  5703. + .dpages = dpages,
  5704. + .sb = root->d_sb,
  5705. + .test = test,
  5706. + .arg = arg
  5707. + };
  5708. +
  5709. + d_walk(root, &args, au_call_dpages_append, NULL);
  5710. +
  5711. + return args.err;
  5712. +}
  5713. +
  5714. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5715. + int do_include, au_dpages_test test, void *arg)
  5716. +{
  5717. + int err;
  5718. +
  5719. + err = 0;
  5720. + write_seqlock(&rename_lock);
  5721. + spin_lock(&dentry->d_lock);
  5722. + if (do_include
  5723. + && au_dcount(dentry) > 0
  5724. + && (!test || test(dentry, arg)))
  5725. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5726. + spin_unlock(&dentry->d_lock);
  5727. + if (unlikely(err))
  5728. + goto out;
  5729. +
  5730. + /*
  5731. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  5732. + * mount
  5733. + */
  5734. + while (!IS_ROOT(dentry)) {
  5735. + dentry = dentry->d_parent; /* rename_lock is locked */
  5736. + spin_lock(&dentry->d_lock);
  5737. + if (au_dcount(dentry) > 0
  5738. + && (!test || test(dentry, arg)))
  5739. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5740. + spin_unlock(&dentry->d_lock);
  5741. + if (unlikely(err))
  5742. + break;
  5743. + }
  5744. +
  5745. +out:
  5746. + write_sequnlock(&rename_lock);
  5747. + return err;
  5748. +}
  5749. +
  5750. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  5751. +{
  5752. + return au_di(dentry) && dentry->d_sb == arg;
  5753. +}
  5754. +
  5755. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5756. + struct dentry *dentry, int do_include)
  5757. +{
  5758. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  5759. + au_dcsub_dpages_aufs, dentry->d_sb);
  5760. +}
  5761. +
  5762. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  5763. +{
  5764. + struct path path[2] = {
  5765. + {
  5766. + .dentry = d1
  5767. + },
  5768. + {
  5769. + .dentry = d2
  5770. + }
  5771. + };
  5772. +
  5773. + return path_is_under(path + 0, path + 1);
  5774. +}
  5775. diff -Naur null/fs/aufs/dcsub.h linux-4.7/fs/aufs/dcsub.h
  5776. --- /dev/null
  5777. +++ linux-4.7/fs/aufs/dcsub.h 2016-08-08 12:57:44.598384764 -0400
  5778. @@ -0,0 +1,136 @@
  5779. +/*
  5780. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5781. + *
  5782. + * This program, aufs is free software; you can redistribute it and/or modify
  5783. + * it under the terms of the GNU General Public License as published by
  5784. + * the Free Software Foundation; either version 2 of the License, or
  5785. + * (at your option) any later version.
  5786. + *
  5787. + * This program is distributed in the hope that it will be useful,
  5788. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5789. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5790. + * GNU General Public License for more details.
  5791. + *
  5792. + * You should have received a copy of the GNU General Public License
  5793. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5794. + */
  5795. +
  5796. +/*
  5797. + * sub-routines for dentry cache
  5798. + */
  5799. +
  5800. +#ifndef __AUFS_DCSUB_H__
  5801. +#define __AUFS_DCSUB_H__
  5802. +
  5803. +#ifdef __KERNEL__
  5804. +
  5805. +#include <linux/dcache.h>
  5806. +#include <linux/fs.h>
  5807. +
  5808. +struct au_dpage {
  5809. + int ndentry;
  5810. + struct dentry **dentries;
  5811. +};
  5812. +
  5813. +struct au_dcsub_pages {
  5814. + int ndpage;
  5815. + struct au_dpage *dpages;
  5816. +};
  5817. +
  5818. +/* ---------------------------------------------------------------------- */
  5819. +
  5820. +/* dcsub.c */
  5821. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  5822. +void au_dpages_free(struct au_dcsub_pages *dpages);
  5823. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  5824. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5825. + au_dpages_test test, void *arg);
  5826. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5827. + int do_include, au_dpages_test test, void *arg);
  5828. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5829. + struct dentry *dentry, int do_include);
  5830. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  5831. +
  5832. +/* ---------------------------------------------------------------------- */
  5833. +
  5834. +/*
  5835. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  5836. + * include/linux/dcache.h. Try them (in the future).
  5837. + */
  5838. +
  5839. +static inline int au_d_hashed_positive(struct dentry *d)
  5840. +{
  5841. + int err;
  5842. + struct inode *inode = d_inode(d);
  5843. +
  5844. + err = 0;
  5845. + if (unlikely(d_unhashed(d)
  5846. + || d_is_negative(d)
  5847. + || !inode->i_nlink))
  5848. + err = -ENOENT;
  5849. + return err;
  5850. +}
  5851. +
  5852. +static inline int au_d_linkable(struct dentry *d)
  5853. +{
  5854. + int err;
  5855. + struct inode *inode = d_inode(d);
  5856. +
  5857. + err = au_d_hashed_positive(d);
  5858. + if (err
  5859. + && d_is_positive(d)
  5860. + && (inode->i_state & I_LINKABLE))
  5861. + err = 0;
  5862. + return err;
  5863. +}
  5864. +
  5865. +static inline int au_d_alive(struct dentry *d)
  5866. +{
  5867. + int err;
  5868. + struct inode *inode;
  5869. +
  5870. + err = 0;
  5871. + if (!IS_ROOT(d))
  5872. + err = au_d_hashed_positive(d);
  5873. + else {
  5874. + inode = d_inode(d);
  5875. + if (unlikely(d_unlinked(d)
  5876. + || d_is_negative(d)
  5877. + || !inode->i_nlink))
  5878. + err = -ENOENT;
  5879. + }
  5880. + return err;
  5881. +}
  5882. +
  5883. +static inline int au_alive_dir(struct dentry *d)
  5884. +{
  5885. + int err;
  5886. +
  5887. + err = au_d_alive(d);
  5888. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  5889. + err = -ENOENT;
  5890. + return err;
  5891. +}
  5892. +
  5893. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  5894. +{
  5895. + return a->len == b->len
  5896. + && !memcmp(a->name, b->name, a->len);
  5897. +}
  5898. +
  5899. +/*
  5900. + * by the commit
  5901. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  5902. + * taking d_lock
  5903. + * the type of d_lockref.count became int, but the inlined function d_count()
  5904. + * still returns unsigned int.
  5905. + * I don't know why. Maybe it is for every d_count() users?
  5906. + * Anyway au_dcount() lives on.
  5907. + */
  5908. +static inline int au_dcount(struct dentry *d)
  5909. +{
  5910. + return (int)d_count(d);
  5911. +}
  5912. +
  5913. +#endif /* __KERNEL__ */
  5914. +#endif /* __AUFS_DCSUB_H__ */
  5915. diff -Naur null/fs/aufs/debug.c linux-4.7/fs/aufs/debug.c
  5916. --- /dev/null
  5917. +++ linux-4.7/fs/aufs/debug.c 2016-08-08 12:57:44.598384764 -0400
  5918. @@ -0,0 +1,440 @@
  5919. +/*
  5920. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5921. + *
  5922. + * This program, aufs is free software; you can redistribute it and/or modify
  5923. + * it under the terms of the GNU General Public License as published by
  5924. + * the Free Software Foundation; either version 2 of the License, or
  5925. + * (at your option) any later version.
  5926. + *
  5927. + * This program is distributed in the hope that it will be useful,
  5928. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5929. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5930. + * GNU General Public License for more details.
  5931. + *
  5932. + * You should have received a copy of the GNU General Public License
  5933. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5934. + */
  5935. +
  5936. +/*
  5937. + * debug print functions
  5938. + */
  5939. +
  5940. +#include "aufs.h"
  5941. +
  5942. +/* Returns 0, or -errno. arg is in kp->arg. */
  5943. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  5944. +{
  5945. + int err, n;
  5946. +
  5947. + err = kstrtoint(val, 0, &n);
  5948. + if (!err) {
  5949. + if (n > 0)
  5950. + au_debug_on();
  5951. + else
  5952. + au_debug_off();
  5953. + }
  5954. + return err;
  5955. +}
  5956. +
  5957. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  5958. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  5959. +{
  5960. + atomic_t *a;
  5961. +
  5962. + a = kp->arg;
  5963. + return sprintf(buffer, "%d", atomic_read(a));
  5964. +}
  5965. +
  5966. +static struct kernel_param_ops param_ops_atomic_t = {
  5967. + .set = param_atomic_t_set,
  5968. + .get = param_atomic_t_get
  5969. + /* void (*free)(void *arg) */
  5970. +};
  5971. +
  5972. +atomic_t aufs_debug = ATOMIC_INIT(0);
  5973. +MODULE_PARM_DESC(debug, "debug print");
  5974. +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
  5975. +
  5976. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  5977. +char *au_plevel = KERN_DEBUG;
  5978. +#define dpri(fmt, ...) do { \
  5979. + if ((au_plevel \
  5980. + && strcmp(au_plevel, KERN_DEBUG)) \
  5981. + || au_debug_test()) \
  5982. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  5983. +} while (0)
  5984. +
  5985. +/* ---------------------------------------------------------------------- */
  5986. +
  5987. +void au_dpri_whlist(struct au_nhash *whlist)
  5988. +{
  5989. + unsigned long ul, n;
  5990. + struct hlist_head *head;
  5991. + struct au_vdir_wh *pos;
  5992. +
  5993. + n = whlist->nh_num;
  5994. + head = whlist->nh_head;
  5995. + for (ul = 0; ul < n; ul++) {
  5996. + hlist_for_each_entry(pos, head, wh_hash)
  5997. + dpri("b%d, %.*s, %d\n",
  5998. + pos->wh_bindex,
  5999. + pos->wh_str.len, pos->wh_str.name,
  6000. + pos->wh_str.len);
  6001. + head++;
  6002. + }
  6003. +}
  6004. +
  6005. +void au_dpri_vdir(struct au_vdir *vdir)
  6006. +{
  6007. + unsigned long ul;
  6008. + union au_vdir_deblk_p p;
  6009. + unsigned char *o;
  6010. +
  6011. + if (!vdir || IS_ERR(vdir)) {
  6012. + dpri("err %ld\n", PTR_ERR(vdir));
  6013. + return;
  6014. + }
  6015. +
  6016. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
  6017. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  6018. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  6019. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  6020. + p.deblk = vdir->vd_deblk[ul];
  6021. + o = p.deblk;
  6022. + dpri("[%lu]: %p\n", ul, o);
  6023. + }
  6024. +}
  6025. +
  6026. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  6027. + struct dentry *wh)
  6028. +{
  6029. + char *n = NULL;
  6030. + int l = 0;
  6031. +
  6032. + if (!inode || IS_ERR(inode)) {
  6033. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  6034. + return -1;
  6035. + }
  6036. +
  6037. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  6038. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  6039. + && sizeof(inode->i_blocks) != sizeof(u64));
  6040. + if (wh) {
  6041. + n = (void *)wh->d_name.name;
  6042. + l = wh->d_name.len;
  6043. + }
  6044. +
  6045. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  6046. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  6047. + bindex, inode,
  6048. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  6049. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  6050. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  6051. + hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
  6052. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  6053. + inode->i_state, inode->i_flags, inode->i_version,
  6054. + inode->i_generation,
  6055. + l ? ", wh " : "", l, n);
  6056. + return 0;
  6057. +}
  6058. +
  6059. +void au_dpri_inode(struct inode *inode)
  6060. +{
  6061. + struct au_iinfo *iinfo;
  6062. + struct au_hinode *hi;
  6063. + aufs_bindex_t bindex;
  6064. + int err, hn;
  6065. +
  6066. + err = do_pri_inode(-1, inode, -1, NULL);
  6067. + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
  6068. + return;
  6069. +
  6070. + iinfo = au_ii(inode);
  6071. + dpri("i-1: btop %d, bbot %d, gen %d\n",
  6072. + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
  6073. + if (iinfo->ii_btop < 0)
  6074. + return;
  6075. + hn = 0;
  6076. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
  6077. + hi = au_hinode(iinfo, bindex);
  6078. + hn = !!au_hn(hi);
  6079. + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
  6080. + }
  6081. +}
  6082. +
  6083. +void au_dpri_dalias(struct inode *inode)
  6084. +{
  6085. + struct dentry *d;
  6086. +
  6087. + spin_lock(&inode->i_lock);
  6088. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6089. + au_dpri_dentry(d);
  6090. + spin_unlock(&inode->i_lock);
  6091. +}
  6092. +
  6093. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6094. +{
  6095. + struct dentry *wh = NULL;
  6096. + int hn;
  6097. + struct inode *inode;
  6098. + struct au_iinfo *iinfo;
  6099. + struct au_hinode *hi;
  6100. +
  6101. + if (!dentry || IS_ERR(dentry)) {
  6102. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6103. + return -1;
  6104. + }
  6105. + /* do not call dget_parent() here */
  6106. + /* note: access d_xxx without d_lock */
  6107. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6108. + bindex, dentry, dentry,
  6109. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6110. + au_dcount(dentry), dentry->d_flags,
  6111. + d_unhashed(dentry) ? "un" : "");
  6112. + hn = -1;
  6113. + inode = NULL;
  6114. + if (d_is_positive(dentry))
  6115. + inode = d_inode(dentry);
  6116. + if (inode
  6117. + && au_test_aufs(dentry->d_sb)
  6118. + && bindex >= 0
  6119. + && !au_is_bad_inode(inode)) {
  6120. + iinfo = au_ii(inode);
  6121. + hi = au_hinode(iinfo, bindex);
  6122. + hn = !!au_hn(hi);
  6123. + wh = hi->hi_whdentry;
  6124. + }
  6125. + do_pri_inode(bindex, inode, hn, wh);
  6126. + return 0;
  6127. +}
  6128. +
  6129. +void au_dpri_dentry(struct dentry *dentry)
  6130. +{
  6131. + struct au_dinfo *dinfo;
  6132. + aufs_bindex_t bindex;
  6133. + int err;
  6134. +
  6135. + err = do_pri_dentry(-1, dentry);
  6136. + if (err || !au_test_aufs(dentry->d_sb))
  6137. + return;
  6138. +
  6139. + dinfo = au_di(dentry);
  6140. + if (!dinfo)
  6141. + return;
  6142. + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6143. + dinfo->di_btop, dinfo->di_bbot,
  6144. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6145. + dinfo->di_tmpfile);
  6146. + if (dinfo->di_btop < 0)
  6147. + return;
  6148. + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
  6149. + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
  6150. +}
  6151. +
  6152. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6153. +{
  6154. + char a[32];
  6155. +
  6156. + if (!file || IS_ERR(file)) {
  6157. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6158. + return -1;
  6159. + }
  6160. + a[0] = 0;
  6161. + if (bindex < 0
  6162. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6163. + && au_test_aufs(file->f_path.dentry->d_sb)
  6164. + && au_fi(file))
  6165. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6166. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6167. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6168. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6169. + file->f_version, file->f_pos, a);
  6170. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6171. + do_pri_dentry(bindex, file->f_path.dentry);
  6172. + return 0;
  6173. +}
  6174. +
  6175. +void au_dpri_file(struct file *file)
  6176. +{
  6177. + struct au_finfo *finfo;
  6178. + struct au_fidir *fidir;
  6179. + struct au_hfile *hfile;
  6180. + aufs_bindex_t bindex;
  6181. + int err;
  6182. +
  6183. + err = do_pri_file(-1, file);
  6184. + if (err
  6185. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6186. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6187. + return;
  6188. +
  6189. + finfo = au_fi(file);
  6190. + if (!finfo)
  6191. + return;
  6192. + if (finfo->fi_btop < 0)
  6193. + return;
  6194. + fidir = finfo->fi_hdir;
  6195. + if (!fidir)
  6196. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6197. + else
  6198. + for (bindex = finfo->fi_btop;
  6199. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6200. + bindex++) {
  6201. + hfile = fidir->fd_hfile + bindex;
  6202. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6203. + }
  6204. +}
  6205. +
  6206. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6207. +{
  6208. + struct vfsmount *mnt;
  6209. + struct super_block *sb;
  6210. +
  6211. + if (!br || IS_ERR(br))
  6212. + goto out;
  6213. + mnt = au_br_mnt(br);
  6214. + if (!mnt || IS_ERR(mnt))
  6215. + goto out;
  6216. + sb = mnt->mnt_sb;
  6217. + if (!sb || IS_ERR(sb))
  6218. + goto out;
  6219. +
  6220. + dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
  6221. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6222. + "xino %d\n",
  6223. + bindex, br->br_perm, br->br_id, au_br_count(br),
  6224. + br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6225. + sb->s_flags, sb->s_count,
  6226. + atomic_read(&sb->s_active), !!br->br_xino.xi_file);
  6227. + return 0;
  6228. +
  6229. +out:
  6230. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6231. + return -1;
  6232. +}
  6233. +
  6234. +void au_dpri_sb(struct super_block *sb)
  6235. +{
  6236. + struct au_sbinfo *sbinfo;
  6237. + aufs_bindex_t bindex;
  6238. + int err;
  6239. + /* to reuduce stack size */
  6240. + struct {
  6241. + struct vfsmount mnt;
  6242. + struct au_branch fake;
  6243. + } *a;
  6244. +
  6245. + /* this function can be called from magic sysrq */
  6246. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6247. + if (unlikely(!a)) {
  6248. + dpri("no memory\n");
  6249. + return;
  6250. + }
  6251. +
  6252. + a->mnt.mnt_sb = sb;
  6253. + a->fake.br_path.mnt = &a->mnt;
  6254. + au_br_count_init(&a->fake);
  6255. + err = do_pri_br(-1, &a->fake);
  6256. + au_br_count_fin(&a->fake);
  6257. + au_delayed_kfree(a);
  6258. + dpri("dev 0x%x\n", sb->s_dev);
  6259. + if (err || !au_test_aufs(sb))
  6260. + return;
  6261. +
  6262. + sbinfo = au_sbi(sb);
  6263. + if (!sbinfo)
  6264. + return;
  6265. + dpri("nw %d, gen %u, kobj %d\n",
  6266. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6267. + atomic_read(&sbinfo->si_kobj.kref.refcount));
  6268. + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
  6269. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6270. +}
  6271. +
  6272. +/* ---------------------------------------------------------------------- */
  6273. +
  6274. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6275. +{
  6276. + struct inode *h_inode, *inode = d_inode(dentry);
  6277. + struct dentry *h_dentry;
  6278. + aufs_bindex_t bindex, bbot, bi;
  6279. +
  6280. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6281. + return;
  6282. +
  6283. + bbot = au_dbbot(dentry);
  6284. + bi = au_ibbot(inode);
  6285. + if (bi < bbot)
  6286. + bbot = bi;
  6287. + bindex = au_dbtop(dentry);
  6288. + bi = au_ibtop(inode);
  6289. + if (bi > bindex)
  6290. + bindex = bi;
  6291. +
  6292. + for (; bindex <= bbot; bindex++) {
  6293. + h_dentry = au_h_dptr(dentry, bindex);
  6294. + if (!h_dentry)
  6295. + continue;
  6296. + h_inode = au_h_iptr(inode, bindex);
  6297. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6298. + au_debug_on();
  6299. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6300. + AuDbgDentry(dentry);
  6301. + AuDbgInode(inode);
  6302. + au_debug_off();
  6303. + BUG();
  6304. + }
  6305. + }
  6306. +}
  6307. +
  6308. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6309. +{
  6310. + int err, i, j;
  6311. + struct au_dcsub_pages dpages;
  6312. + struct au_dpage *dpage;
  6313. + struct dentry **dentries;
  6314. +
  6315. + err = au_dpages_init(&dpages, GFP_NOFS);
  6316. + AuDebugOn(err);
  6317. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6318. + AuDebugOn(err);
  6319. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6320. + dpage = dpages.dpages + i;
  6321. + dentries = dpage->dentries;
  6322. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6323. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6324. + }
  6325. + au_dpages_free(&dpages);
  6326. +}
  6327. +
  6328. +void au_dbg_verify_kthread(void)
  6329. +{
  6330. + if (au_wkq_test()) {
  6331. + au_dbg_blocked();
  6332. + /*
  6333. + * It may be recursive, but udba=notify between two aufs mounts,
  6334. + * where a single ro branch is shared, is not a problem.
  6335. + */
  6336. + /* WARN_ON(1); */
  6337. + }
  6338. +}
  6339. +
  6340. +/* ---------------------------------------------------------------------- */
  6341. +
  6342. +int __init au_debug_init(void)
  6343. +{
  6344. + aufs_bindex_t bindex;
  6345. + struct au_vdir_destr destr;
  6346. +
  6347. + bindex = -1;
  6348. + AuDebugOn(bindex >= 0);
  6349. +
  6350. + destr.len = -1;
  6351. + AuDebugOn(destr.len < NAME_MAX);
  6352. +
  6353. +#ifdef CONFIG_4KSTACKS
  6354. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6355. +#endif
  6356. +
  6357. + return 0;
  6358. +}
  6359. diff -Naur null/fs/aufs/debug.h linux-4.7/fs/aufs/debug.h
  6360. --- /dev/null
  6361. +++ linux-4.7/fs/aufs/debug.h 2016-08-08 12:57:44.598384764 -0400
  6362. @@ -0,0 +1,225 @@
  6363. +/*
  6364. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  6365. + *
  6366. + * This program, aufs is free software; you can redistribute it and/or modify
  6367. + * it under the terms of the GNU General Public License as published by
  6368. + * the Free Software Foundation; either version 2 of the License, or
  6369. + * (at your option) any later version.
  6370. + *
  6371. + * This program is distributed in the hope that it will be useful,
  6372. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6373. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6374. + * GNU General Public License for more details.
  6375. + *
  6376. + * You should have received a copy of the GNU General Public License
  6377. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6378. + */
  6379. +
  6380. +/*
  6381. + * debug print functions
  6382. + */
  6383. +
  6384. +#ifndef __AUFS_DEBUG_H__
  6385. +#define __AUFS_DEBUG_H__
  6386. +
  6387. +#ifdef __KERNEL__
  6388. +
  6389. +#include <linux/atomic.h>
  6390. +#include <linux/module.h>
  6391. +#include <linux/kallsyms.h>
  6392. +#include <linux/sysrq.h>
  6393. +
  6394. +#ifdef CONFIG_AUFS_DEBUG
  6395. +#define AuDebugOn(a) BUG_ON(a)
  6396. +
  6397. +/* module parameter */
  6398. +extern atomic_t aufs_debug;
  6399. +static inline void au_debug_on(void)
  6400. +{
  6401. + atomic_inc(&aufs_debug);
  6402. +}
  6403. +static inline void au_debug_off(void)
  6404. +{
  6405. + atomic_dec_if_positive(&aufs_debug);
  6406. +}
  6407. +
  6408. +static inline int au_debug_test(void)
  6409. +{
  6410. + return atomic_read(&aufs_debug) > 0;
  6411. +}
  6412. +#else
  6413. +#define AuDebugOn(a) do {} while (0)
  6414. +AuStubVoid(au_debug_on, void)
  6415. +AuStubVoid(au_debug_off, void)
  6416. +AuStubInt0(au_debug_test, void)
  6417. +#endif /* CONFIG_AUFS_DEBUG */
  6418. +
  6419. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6420. +
  6421. +/* ---------------------------------------------------------------------- */
  6422. +
  6423. +/* debug print */
  6424. +
  6425. +#define AuDbg(fmt, ...) do { \
  6426. + if (au_debug_test()) \
  6427. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6428. +} while (0)
  6429. +#define AuLabel(l) AuDbg(#l "\n")
  6430. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6431. +#define AuWarn1(fmt, ...) do { \
  6432. + static unsigned char _c; \
  6433. + if (!_c++) \
  6434. + pr_warn(fmt, ##__VA_ARGS__); \
  6435. +} while (0)
  6436. +
  6437. +#define AuErr1(fmt, ...) do { \
  6438. + static unsigned char _c; \
  6439. + if (!_c++) \
  6440. + pr_err(fmt, ##__VA_ARGS__); \
  6441. +} while (0)
  6442. +
  6443. +#define AuIOErr1(fmt, ...) do { \
  6444. + static unsigned char _c; \
  6445. + if (!_c++) \
  6446. + AuIOErr(fmt, ##__VA_ARGS__); \
  6447. +} while (0)
  6448. +
  6449. +#define AuUnsupportMsg "This operation is not supported." \
  6450. + " Please report this application to aufs-users ML."
  6451. +#define AuUnsupport(fmt, ...) do { \
  6452. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6453. + dump_stack(); \
  6454. +} while (0)
  6455. +
  6456. +#define AuTraceErr(e) do { \
  6457. + if (unlikely((e) < 0)) \
  6458. + AuDbg("err %d\n", (int)(e)); \
  6459. +} while (0)
  6460. +
  6461. +#define AuTraceErrPtr(p) do { \
  6462. + if (IS_ERR(p)) \
  6463. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6464. +} while (0)
  6465. +
  6466. +/* dirty macros for debug print, use with "%.*s" and caution */
  6467. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6468. +
  6469. +/* ---------------------------------------------------------------------- */
  6470. +
  6471. +struct dentry;
  6472. +#ifdef CONFIG_AUFS_DEBUG
  6473. +extern struct mutex au_dbg_mtx;
  6474. +extern char *au_plevel;
  6475. +struct au_nhash;
  6476. +void au_dpri_whlist(struct au_nhash *whlist);
  6477. +struct au_vdir;
  6478. +void au_dpri_vdir(struct au_vdir *vdir);
  6479. +struct inode;
  6480. +void au_dpri_inode(struct inode *inode);
  6481. +void au_dpri_dalias(struct inode *inode);
  6482. +void au_dpri_dentry(struct dentry *dentry);
  6483. +struct file;
  6484. +void au_dpri_file(struct file *filp);
  6485. +struct super_block;
  6486. +void au_dpri_sb(struct super_block *sb);
  6487. +
  6488. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6489. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6490. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6491. +void au_dbg_verify_kthread(void);
  6492. +
  6493. +int __init au_debug_init(void);
  6494. +
  6495. +#define AuDbgWhlist(w) do { \
  6496. + mutex_lock(&au_dbg_mtx); \
  6497. + AuDbg(#w "\n"); \
  6498. + au_dpri_whlist(w); \
  6499. + mutex_unlock(&au_dbg_mtx); \
  6500. +} while (0)
  6501. +
  6502. +#define AuDbgVdir(v) do { \
  6503. + mutex_lock(&au_dbg_mtx); \
  6504. + AuDbg(#v "\n"); \
  6505. + au_dpri_vdir(v); \
  6506. + mutex_unlock(&au_dbg_mtx); \
  6507. +} while (0)
  6508. +
  6509. +#define AuDbgInode(i) do { \
  6510. + mutex_lock(&au_dbg_mtx); \
  6511. + AuDbg(#i "\n"); \
  6512. + au_dpri_inode(i); \
  6513. + mutex_unlock(&au_dbg_mtx); \
  6514. +} while (0)
  6515. +
  6516. +#define AuDbgDAlias(i) do { \
  6517. + mutex_lock(&au_dbg_mtx); \
  6518. + AuDbg(#i "\n"); \
  6519. + au_dpri_dalias(i); \
  6520. + mutex_unlock(&au_dbg_mtx); \
  6521. +} while (0)
  6522. +
  6523. +#define AuDbgDentry(d) do { \
  6524. + mutex_lock(&au_dbg_mtx); \
  6525. + AuDbg(#d "\n"); \
  6526. + au_dpri_dentry(d); \
  6527. + mutex_unlock(&au_dbg_mtx); \
  6528. +} while (0)
  6529. +
  6530. +#define AuDbgFile(f) do { \
  6531. + mutex_lock(&au_dbg_mtx); \
  6532. + AuDbg(#f "\n"); \
  6533. + au_dpri_file(f); \
  6534. + mutex_unlock(&au_dbg_mtx); \
  6535. +} while (0)
  6536. +
  6537. +#define AuDbgSb(sb) do { \
  6538. + mutex_lock(&au_dbg_mtx); \
  6539. + AuDbg(#sb "\n"); \
  6540. + au_dpri_sb(sb); \
  6541. + mutex_unlock(&au_dbg_mtx); \
  6542. +} while (0)
  6543. +
  6544. +#define AuDbgSym(addr) do { \
  6545. + char sym[KSYM_SYMBOL_LEN]; \
  6546. + sprint_symbol(sym, (unsigned long)addr); \
  6547. + AuDbg("%s\n", sym); \
  6548. +} while (0)
  6549. +#else
  6550. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6551. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6552. +AuStubVoid(au_dbg_verify_kthread, void)
  6553. +AuStubInt0(__init au_debug_init, void)
  6554. +
  6555. +#define AuDbgWhlist(w) do {} while (0)
  6556. +#define AuDbgVdir(v) do {} while (0)
  6557. +#define AuDbgInode(i) do {} while (0)
  6558. +#define AuDbgDAlias(i) do {} while (0)
  6559. +#define AuDbgDentry(d) do {} while (0)
  6560. +#define AuDbgFile(f) do {} while (0)
  6561. +#define AuDbgSb(sb) do {} while (0)
  6562. +#define AuDbgSym(addr) do {} while (0)
  6563. +#endif /* CONFIG_AUFS_DEBUG */
  6564. +
  6565. +/* ---------------------------------------------------------------------- */
  6566. +
  6567. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  6568. +int __init au_sysrq_init(void);
  6569. +void au_sysrq_fin(void);
  6570. +
  6571. +#ifdef CONFIG_HW_CONSOLE
  6572. +#define au_dbg_blocked() do { \
  6573. + WARN_ON(1); \
  6574. + handle_sysrq('w'); \
  6575. +} while (0)
  6576. +#else
  6577. +AuStubVoid(au_dbg_blocked, void)
  6578. +#endif
  6579. +
  6580. +#else
  6581. +AuStubInt0(__init au_sysrq_init, void)
  6582. +AuStubVoid(au_sysrq_fin, void)
  6583. +AuStubVoid(au_dbg_blocked, void)
  6584. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  6585. +
  6586. +#endif /* __KERNEL__ */
  6587. +#endif /* __AUFS_DEBUG_H__ */
  6588. diff -Naur null/fs/aufs/dentry.c linux-4.7/fs/aufs/dentry.c
  6589. --- /dev/null
  6590. +++ linux-4.7/fs/aufs/dentry.c 2016-08-08 12:57:44.599384741 -0400
  6591. @@ -0,0 +1,1128 @@
  6592. +/*
  6593. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  6594. + *
  6595. + * This program, aufs is free software; you can redistribute it and/or modify
  6596. + * it under the terms of the GNU General Public License as published by
  6597. + * the Free Software Foundation; either version 2 of the License, or
  6598. + * (at your option) any later version.
  6599. + *
  6600. + * This program is distributed in the hope that it will be useful,
  6601. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6602. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6603. + * GNU General Public License for more details.
  6604. + *
  6605. + * You should have received a copy of the GNU General Public License
  6606. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6607. + */
  6608. +
  6609. +/*
  6610. + * lookup and dentry operations
  6611. + */
  6612. +
  6613. +#include <linux/namei.h>
  6614. +#include "aufs.h"
  6615. +
  6616. +struct au_do_lookup_args {
  6617. + unsigned int flags;
  6618. + mode_t type;
  6619. +};
  6620. +
  6621. +/*
  6622. + * returns positive/negative dentry, NULL or an error.
  6623. + * NULL means whiteout-ed or not-found.
  6624. + */
  6625. +static struct dentry*
  6626. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  6627. + aufs_bindex_t bindex, struct qstr *wh_name,
  6628. + struct au_do_lookup_args *args)
  6629. +{
  6630. + struct dentry *h_dentry;
  6631. + struct inode *h_inode;
  6632. + struct au_branch *br;
  6633. + int wh_found, opq;
  6634. + unsigned char wh_able;
  6635. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  6636. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  6637. + IGNORE_PERM);
  6638. +
  6639. + wh_found = 0;
  6640. + br = au_sbr(dentry->d_sb, bindex);
  6641. + wh_able = !!au_br_whable(br->br_perm);
  6642. + if (wh_able)
  6643. + wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
  6644. + h_dentry = ERR_PTR(wh_found);
  6645. + if (!wh_found)
  6646. + goto real_lookup;
  6647. + if (unlikely(wh_found < 0))
  6648. + goto out;
  6649. +
  6650. + /* We found a whiteout */
  6651. + /* au_set_dbbot(dentry, bindex); */
  6652. + au_set_dbwh(dentry, bindex);
  6653. + if (!allow_neg)
  6654. + return NULL; /* success */
  6655. +
  6656. +real_lookup:
  6657. + if (!ignore_perm)
  6658. + h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  6659. + else
  6660. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6661. + if (IS_ERR(h_dentry)) {
  6662. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  6663. + && !allow_neg)
  6664. + h_dentry = NULL;
  6665. + goto out;
  6666. + }
  6667. +
  6668. + h_inode = d_inode(h_dentry);
  6669. + if (d_is_negative(h_dentry)) {
  6670. + if (!allow_neg)
  6671. + goto out_neg;
  6672. + } else if (wh_found
  6673. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  6674. + goto out_neg;
  6675. +
  6676. + if (au_dbbot(dentry) <= bindex)
  6677. + au_set_dbbot(dentry, bindex);
  6678. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  6679. + au_set_dbtop(dentry, bindex);
  6680. + au_set_h_dptr(dentry, bindex, h_dentry);
  6681. +
  6682. + if (!d_is_dir(h_dentry)
  6683. + || !wh_able
  6684. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  6685. + goto out; /* success */
  6686. +
  6687. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  6688. + opq = au_diropq_test(h_dentry);
  6689. + inode_unlock(h_inode);
  6690. + if (opq > 0)
  6691. + au_set_dbdiropq(dentry, bindex);
  6692. + else if (unlikely(opq < 0)) {
  6693. + au_set_h_dptr(dentry, bindex, NULL);
  6694. + h_dentry = ERR_PTR(opq);
  6695. + }
  6696. + goto out;
  6697. +
  6698. +out_neg:
  6699. + dput(h_dentry);
  6700. + h_dentry = NULL;
  6701. +out:
  6702. + return h_dentry;
  6703. +}
  6704. +
  6705. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  6706. +{
  6707. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  6708. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  6709. + return -EPERM;
  6710. + return 0;
  6711. +}
  6712. +
  6713. +/*
  6714. + * returns the number of lower positive dentries,
  6715. + * otherwise an error.
  6716. + * can be called at unlinking with @type is zero.
  6717. + */
  6718. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  6719. + unsigned int flags)
  6720. +{
  6721. + int npositive, err;
  6722. + aufs_bindex_t bindex, btail, bdiropq;
  6723. + unsigned char isdir, dirperm1;
  6724. + struct qstr whname;
  6725. + struct au_do_lookup_args args = {
  6726. + .flags = flags
  6727. + };
  6728. + const struct qstr *name = &dentry->d_name;
  6729. + struct dentry *parent;
  6730. + struct super_block *sb;
  6731. +
  6732. + sb = dentry->d_sb;
  6733. + err = au_test_shwh(sb, name);
  6734. + if (unlikely(err))
  6735. + goto out;
  6736. +
  6737. + err = au_wh_name_alloc(&whname, name);
  6738. + if (unlikely(err))
  6739. + goto out;
  6740. +
  6741. + isdir = !!d_is_dir(dentry);
  6742. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  6743. +
  6744. + npositive = 0;
  6745. + parent = dget_parent(dentry);
  6746. + btail = au_dbtaildir(parent);
  6747. + for (bindex = btop; bindex <= btail; bindex++) {
  6748. + struct dentry *h_parent, *h_dentry;
  6749. + struct inode *h_inode, *h_dir;
  6750. +
  6751. + h_dentry = au_h_dptr(dentry, bindex);
  6752. + if (h_dentry) {
  6753. + if (d_is_positive(h_dentry))
  6754. + npositive++;
  6755. + break;
  6756. + }
  6757. + h_parent = au_h_dptr(parent, bindex);
  6758. + if (!h_parent || !d_is_dir(h_parent))
  6759. + continue;
  6760. +
  6761. + h_dir = d_inode(h_parent);
  6762. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  6763. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
  6764. + &args);
  6765. + inode_unlock(h_dir);
  6766. + err = PTR_ERR(h_dentry);
  6767. + if (IS_ERR(h_dentry))
  6768. + goto out_parent;
  6769. + if (h_dentry)
  6770. + au_fclr_lkup(args.flags, ALLOW_NEG);
  6771. + if (dirperm1)
  6772. + au_fset_lkup(args.flags, IGNORE_PERM);
  6773. +
  6774. + if (au_dbwh(dentry) == bindex)
  6775. + break;
  6776. + if (!h_dentry)
  6777. + continue;
  6778. + if (d_is_negative(h_dentry))
  6779. + continue;
  6780. + h_inode = d_inode(h_dentry);
  6781. + npositive++;
  6782. + if (!args.type)
  6783. + args.type = h_inode->i_mode & S_IFMT;
  6784. + if (args.type != S_IFDIR)
  6785. + break;
  6786. + else if (isdir) {
  6787. + /* the type of lower may be different */
  6788. + bdiropq = au_dbdiropq(dentry);
  6789. + if (bdiropq >= 0 && bdiropq <= bindex)
  6790. + break;
  6791. + }
  6792. + }
  6793. +
  6794. + if (npositive) {
  6795. + AuLabel(positive);
  6796. + au_update_dbtop(dentry);
  6797. + }
  6798. + err = npositive;
  6799. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  6800. + && au_dbtop(dentry) < 0)) {
  6801. + err = -EIO;
  6802. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  6803. + dentry, err);
  6804. + }
  6805. +
  6806. +out_parent:
  6807. + dput(parent);
  6808. + au_delayed_kfree(whname.name);
  6809. +out:
  6810. + return err;
  6811. +}
  6812. +
  6813. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  6814. +{
  6815. + struct dentry *dentry;
  6816. + int wkq_err;
  6817. +
  6818. + if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
  6819. + dentry = vfsub_lkup_one(name, parent);
  6820. + else {
  6821. + struct vfsub_lkup_one_args args = {
  6822. + .errp = &dentry,
  6823. + .name = name,
  6824. + .parent = parent
  6825. + };
  6826. +
  6827. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  6828. + if (unlikely(wkq_err))
  6829. + dentry = ERR_PTR(wkq_err);
  6830. + }
  6831. +
  6832. + return dentry;
  6833. +}
  6834. +
  6835. +/*
  6836. + * lookup @dentry on @bindex which should be negative.
  6837. + */
  6838. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  6839. +{
  6840. + int err;
  6841. + struct dentry *parent, *h_parent, *h_dentry;
  6842. + struct au_branch *br;
  6843. +
  6844. + parent = dget_parent(dentry);
  6845. + h_parent = au_h_dptr(parent, bindex);
  6846. + br = au_sbr(dentry->d_sb, bindex);
  6847. + if (wh)
  6848. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  6849. + else
  6850. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6851. + err = PTR_ERR(h_dentry);
  6852. + if (IS_ERR(h_dentry))
  6853. + goto out;
  6854. + if (unlikely(d_is_positive(h_dentry))) {
  6855. + err = -EIO;
  6856. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  6857. + dput(h_dentry);
  6858. + goto out;
  6859. + }
  6860. +
  6861. + err = 0;
  6862. + if (bindex < au_dbtop(dentry))
  6863. + au_set_dbtop(dentry, bindex);
  6864. + if (au_dbbot(dentry) < bindex)
  6865. + au_set_dbbot(dentry, bindex);
  6866. + au_set_h_dptr(dentry, bindex, h_dentry);
  6867. +
  6868. +out:
  6869. + dput(parent);
  6870. + return err;
  6871. +}
  6872. +
  6873. +/* ---------------------------------------------------------------------- */
  6874. +
  6875. +/* subset of struct inode */
  6876. +struct au_iattr {
  6877. + unsigned long i_ino;
  6878. + /* unsigned int i_nlink; */
  6879. + kuid_t i_uid;
  6880. + kgid_t i_gid;
  6881. + u64 i_version;
  6882. +/*
  6883. + loff_t i_size;
  6884. + blkcnt_t i_blocks;
  6885. +*/
  6886. + umode_t i_mode;
  6887. +};
  6888. +
  6889. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  6890. +{
  6891. + ia->i_ino = h_inode->i_ino;
  6892. + /* ia->i_nlink = h_inode->i_nlink; */
  6893. + ia->i_uid = h_inode->i_uid;
  6894. + ia->i_gid = h_inode->i_gid;
  6895. + ia->i_version = h_inode->i_version;
  6896. +/*
  6897. + ia->i_size = h_inode->i_size;
  6898. + ia->i_blocks = h_inode->i_blocks;
  6899. +*/
  6900. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  6901. +}
  6902. +
  6903. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  6904. +{
  6905. + return ia->i_ino != h_inode->i_ino
  6906. + /* || ia->i_nlink != h_inode->i_nlink */
  6907. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  6908. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  6909. + || ia->i_version != h_inode->i_version
  6910. +/*
  6911. + || ia->i_size != h_inode->i_size
  6912. + || ia->i_blocks != h_inode->i_blocks
  6913. +*/
  6914. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  6915. +}
  6916. +
  6917. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  6918. + struct au_branch *br)
  6919. +{
  6920. + int err;
  6921. + struct au_iattr ia;
  6922. + struct inode *h_inode;
  6923. + struct dentry *h_d;
  6924. + struct super_block *h_sb;
  6925. +
  6926. + err = 0;
  6927. + memset(&ia, -1, sizeof(ia));
  6928. + h_sb = h_dentry->d_sb;
  6929. + h_inode = NULL;
  6930. + if (d_is_positive(h_dentry)) {
  6931. + h_inode = d_inode(h_dentry);
  6932. + au_iattr_save(&ia, h_inode);
  6933. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  6934. + /* nfs d_revalidate may return 0 for negative dentry */
  6935. + /* fuse d_revalidate always return 0 for negative dentry */
  6936. + goto out;
  6937. +
  6938. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  6939. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  6940. + err = PTR_ERR(h_d);
  6941. + if (IS_ERR(h_d))
  6942. + goto out;
  6943. +
  6944. + err = 0;
  6945. + if (unlikely(h_d != h_dentry
  6946. + || d_inode(h_d) != h_inode
  6947. + || (h_inode && au_iattr_test(&ia, h_inode))))
  6948. + err = au_busy_or_stale();
  6949. + dput(h_d);
  6950. +
  6951. +out:
  6952. + AuTraceErr(err);
  6953. + return err;
  6954. +}
  6955. +
  6956. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  6957. + struct dentry *h_parent, struct au_branch *br)
  6958. +{
  6959. + int err;
  6960. +
  6961. + err = 0;
  6962. + if (udba == AuOpt_UDBA_REVAL
  6963. + && !au_test_fs_remote(h_dentry->d_sb)) {
  6964. + IMustLock(h_dir);
  6965. + err = (d_inode(h_dentry->d_parent) != h_dir);
  6966. + } else if (udba != AuOpt_UDBA_NONE)
  6967. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  6968. +
  6969. + return err;
  6970. +}
  6971. +
  6972. +/* ---------------------------------------------------------------------- */
  6973. +
  6974. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  6975. +{
  6976. + int err;
  6977. + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
  6978. + struct au_hdentry tmp, *p, *q;
  6979. + struct au_dinfo *dinfo;
  6980. + struct super_block *sb;
  6981. +
  6982. + DiMustWriteLock(dentry);
  6983. +
  6984. + sb = dentry->d_sb;
  6985. + dinfo = au_di(dentry);
  6986. + bbot = dinfo->di_bbot;
  6987. + bwh = dinfo->di_bwh;
  6988. + bdiropq = dinfo->di_bdiropq;
  6989. + bindex = dinfo->di_btop;
  6990. + p = au_hdentry(dinfo, bindex);
  6991. + for (; bindex <= bbot; bindex++, p++) {
  6992. + if (!p->hd_dentry)
  6993. + continue;
  6994. +
  6995. + new_bindex = au_br_index(sb, p->hd_id);
  6996. + if (new_bindex == bindex)
  6997. + continue;
  6998. +
  6999. + if (dinfo->di_bwh == bindex)
  7000. + bwh = new_bindex;
  7001. + if (dinfo->di_bdiropq == bindex)
  7002. + bdiropq = new_bindex;
  7003. + if (new_bindex < 0) {
  7004. + au_hdput(p);
  7005. + p->hd_dentry = NULL;
  7006. + continue;
  7007. + }
  7008. +
  7009. + /* swap two lower dentries, and loop again */
  7010. + q = au_hdentry(dinfo, new_bindex);
  7011. + tmp = *q;
  7012. + *q = *p;
  7013. + *p = tmp;
  7014. + if (tmp.hd_dentry) {
  7015. + bindex--;
  7016. + p--;
  7017. + }
  7018. + }
  7019. +
  7020. + dinfo->di_bwh = -1;
  7021. + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
  7022. + dinfo->di_bwh = bwh;
  7023. +
  7024. + dinfo->di_bdiropq = -1;
  7025. + if (bdiropq >= 0
  7026. + && bdiropq <= au_sbbot(sb)
  7027. + && au_sbr_whable(sb, bdiropq))
  7028. + dinfo->di_bdiropq = bdiropq;
  7029. +
  7030. + err = -EIO;
  7031. + dinfo->di_btop = -1;
  7032. + dinfo->di_bbot = -1;
  7033. + bbot = au_dbbot(parent);
  7034. + bindex = 0;
  7035. + p = au_hdentry(dinfo, bindex);
  7036. + for (; bindex <= bbot; bindex++, p++)
  7037. + if (p->hd_dentry) {
  7038. + dinfo->di_btop = bindex;
  7039. + break;
  7040. + }
  7041. +
  7042. + if (dinfo->di_btop >= 0) {
  7043. + bindex = bbot;
  7044. + p = au_hdentry(dinfo, bindex);
  7045. + for (; bindex >= 0; bindex--, p--)
  7046. + if (p->hd_dentry) {
  7047. + dinfo->di_bbot = bindex;
  7048. + err = 0;
  7049. + break;
  7050. + }
  7051. + }
  7052. +
  7053. + return err;
  7054. +}
  7055. +
  7056. +static void au_do_hide(struct dentry *dentry)
  7057. +{
  7058. + struct inode *inode;
  7059. +
  7060. + if (d_really_is_positive(dentry)) {
  7061. + inode = d_inode(dentry);
  7062. + if (!d_is_dir(dentry)) {
  7063. + if (inode->i_nlink && !d_unhashed(dentry))
  7064. + drop_nlink(inode);
  7065. + } else {
  7066. + clear_nlink(inode);
  7067. + /* stop next lookup */
  7068. + inode->i_flags |= S_DEAD;
  7069. + }
  7070. + smp_mb(); /* necessary? */
  7071. + }
  7072. + d_drop(dentry);
  7073. +}
  7074. +
  7075. +static int au_hide_children(struct dentry *parent)
  7076. +{
  7077. + int err, i, j, ndentry;
  7078. + struct au_dcsub_pages dpages;
  7079. + struct au_dpage *dpage;
  7080. + struct dentry *dentry;
  7081. +
  7082. + err = au_dpages_init(&dpages, GFP_NOFS);
  7083. + if (unlikely(err))
  7084. + goto out;
  7085. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7086. + if (unlikely(err))
  7087. + goto out_dpages;
  7088. +
  7089. + /* in reverse order */
  7090. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7091. + dpage = dpages.dpages + i;
  7092. + ndentry = dpage->ndentry;
  7093. + for (j = ndentry - 1; j >= 0; j--) {
  7094. + dentry = dpage->dentries[j];
  7095. + if (dentry != parent)
  7096. + au_do_hide(dentry);
  7097. + }
  7098. + }
  7099. +
  7100. +out_dpages:
  7101. + au_dpages_free(&dpages);
  7102. +out:
  7103. + return err;
  7104. +}
  7105. +
  7106. +static void au_hide(struct dentry *dentry)
  7107. +{
  7108. + int err;
  7109. +
  7110. + AuDbgDentry(dentry);
  7111. + if (d_is_dir(dentry)) {
  7112. + /* shrink_dcache_parent(dentry); */
  7113. + err = au_hide_children(dentry);
  7114. + if (unlikely(err))
  7115. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7116. + dentry, err);
  7117. + }
  7118. + au_do_hide(dentry);
  7119. +}
  7120. +
  7121. +/*
  7122. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7123. + *
  7124. + * a dirty branch is added
  7125. + * - on the top of layers
  7126. + * - in the middle of layers
  7127. + * - to the bottom of layers
  7128. + *
  7129. + * on the added branch there exists
  7130. + * - a whiteout
  7131. + * - a diropq
  7132. + * - a same named entry
  7133. + * + exist
  7134. + * * negative --> positive
  7135. + * * positive --> positive
  7136. + * - type is unchanged
  7137. + * - type is changed
  7138. + * + doesn't exist
  7139. + * * negative --> negative
  7140. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7141. + * - none
  7142. + */
  7143. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7144. + struct au_dinfo *tmp)
  7145. +{
  7146. + int err;
  7147. + aufs_bindex_t bindex, bbot;
  7148. + struct {
  7149. + struct dentry *dentry;
  7150. + struct inode *inode;
  7151. + mode_t mode;
  7152. + } orig_h, tmp_h = {
  7153. + .dentry = NULL
  7154. + };
  7155. + struct au_hdentry *hd;
  7156. + struct inode *inode, *h_inode;
  7157. + struct dentry *h_dentry;
  7158. +
  7159. + err = 0;
  7160. + AuDebugOn(dinfo->di_btop < 0);
  7161. + orig_h.mode = 0;
  7162. + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
  7163. + orig_h.inode = NULL;
  7164. + if (d_is_positive(orig_h.dentry)) {
  7165. + orig_h.inode = d_inode(orig_h.dentry);
  7166. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7167. + }
  7168. + if (tmp->di_btop >= 0) {
  7169. + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
  7170. + if (d_is_positive(tmp_h.dentry)) {
  7171. + tmp_h.inode = d_inode(tmp_h.dentry);
  7172. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7173. + }
  7174. + }
  7175. +
  7176. + inode = NULL;
  7177. + if (d_really_is_positive(dentry))
  7178. + inode = d_inode(dentry);
  7179. + if (!orig_h.inode) {
  7180. + AuDbg("nagative originally\n");
  7181. + if (inode) {
  7182. + au_hide(dentry);
  7183. + goto out;
  7184. + }
  7185. + AuDebugOn(inode);
  7186. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7187. + AuDebugOn(dinfo->di_bdiropq != -1);
  7188. +
  7189. + if (!tmp_h.inode) {
  7190. + AuDbg("negative --> negative\n");
  7191. + /* should have only one negative lower */
  7192. + if (tmp->di_btop >= 0
  7193. + && tmp->di_btop < dinfo->di_btop) {
  7194. + AuDebugOn(tmp->di_btop != tmp->di_bbot);
  7195. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7196. + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
  7197. + au_di_cp(dinfo, tmp);
  7198. + hd = au_hdentry(tmp, tmp->di_btop);
  7199. + au_set_h_dptr(dentry, tmp->di_btop,
  7200. + dget(hd->hd_dentry));
  7201. + }
  7202. + au_dbg_verify_dinode(dentry);
  7203. + } else {
  7204. + AuDbg("negative --> positive\n");
  7205. + /*
  7206. + * similar to the behaviour of creating with bypassing
  7207. + * aufs.
  7208. + * unhash it in order to force an error in the
  7209. + * succeeding create operation.
  7210. + * we should not set S_DEAD here.
  7211. + */
  7212. + d_drop(dentry);
  7213. + /* au_di_swap(tmp, dinfo); */
  7214. + au_dbg_verify_dinode(dentry);
  7215. + }
  7216. + } else {
  7217. + AuDbg("positive originally\n");
  7218. + /* inode may be NULL */
  7219. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7220. + if (!tmp_h.inode) {
  7221. + AuDbg("positive --> negative\n");
  7222. + /* or bypassing aufs */
  7223. + au_hide(dentry);
  7224. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
  7225. + dinfo->di_bwh = tmp->di_bwh;
  7226. + if (inode)
  7227. + err = au_refresh_hinode_self(inode);
  7228. + au_dbg_verify_dinode(dentry);
  7229. + } else if (orig_h.mode == tmp_h.mode) {
  7230. + AuDbg("positive --> positive, same type\n");
  7231. + if (!S_ISDIR(orig_h.mode)
  7232. + && dinfo->di_btop > tmp->di_btop) {
  7233. + /*
  7234. + * similar to the behaviour of removing and
  7235. + * creating.
  7236. + */
  7237. + au_hide(dentry);
  7238. + if (inode)
  7239. + err = au_refresh_hinode_self(inode);
  7240. + au_dbg_verify_dinode(dentry);
  7241. + } else {
  7242. + /* fill empty slots */
  7243. + if (dinfo->di_btop > tmp->di_btop)
  7244. + dinfo->di_btop = tmp->di_btop;
  7245. + if (dinfo->di_bbot < tmp->di_bbot)
  7246. + dinfo->di_bbot = tmp->di_bbot;
  7247. + dinfo->di_bwh = tmp->di_bwh;
  7248. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7249. + bbot = dinfo->di_bbot;
  7250. + bindex = tmp->di_btop;
  7251. + hd = au_hdentry(tmp, bindex);
  7252. + for (; bindex <= bbot; bindex++, hd++) {
  7253. + if (au_h_dptr(dentry, bindex))
  7254. + continue;
  7255. + h_dentry = hd->hd_dentry;
  7256. + if (!h_dentry)
  7257. + continue;
  7258. + AuDebugOn(d_is_negative(h_dentry));
  7259. + h_inode = d_inode(h_dentry);
  7260. + AuDebugOn(orig_h.mode
  7261. + != (h_inode->i_mode
  7262. + & S_IFMT));
  7263. + au_set_h_dptr(dentry, bindex,
  7264. + dget(h_dentry));
  7265. + }
  7266. + if (inode)
  7267. + err = au_refresh_hinode(inode, dentry);
  7268. + au_dbg_verify_dinode(dentry);
  7269. + }
  7270. + } else {
  7271. + AuDbg("positive --> positive, different type\n");
  7272. + /* similar to the behaviour of removing and creating */
  7273. + au_hide(dentry);
  7274. + if (inode)
  7275. + err = au_refresh_hinode_self(inode);
  7276. + au_dbg_verify_dinode(dentry);
  7277. + }
  7278. + }
  7279. +
  7280. +out:
  7281. + return err;
  7282. +}
  7283. +
  7284. +void au_refresh_dop(struct dentry *dentry, int force_reval)
  7285. +{
  7286. + const struct dentry_operations *dop
  7287. + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
  7288. + static const unsigned int mask
  7289. + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
  7290. +
  7291. + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
  7292. +
  7293. + if (dentry->d_op == dop)
  7294. + return;
  7295. +
  7296. + AuDbg("%pd\n", dentry);
  7297. + spin_lock(&dentry->d_lock);
  7298. + if (dop == &aufs_dop)
  7299. + dentry->d_flags |= mask;
  7300. + else
  7301. + dentry->d_flags &= ~mask;
  7302. + dentry->d_op = dop;
  7303. + spin_unlock(&dentry->d_lock);
  7304. +}
  7305. +
  7306. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7307. +{
  7308. + int err, ebrange;
  7309. + unsigned int sigen;
  7310. + struct au_dinfo *dinfo, *tmp;
  7311. + struct super_block *sb;
  7312. + struct inode *inode;
  7313. +
  7314. + DiMustWriteLock(dentry);
  7315. + AuDebugOn(IS_ROOT(dentry));
  7316. + AuDebugOn(d_really_is_negative(parent));
  7317. +
  7318. + sb = dentry->d_sb;
  7319. + sigen = au_sigen(sb);
  7320. + err = au_digen_test(parent, sigen);
  7321. + if (unlikely(err))
  7322. + goto out;
  7323. +
  7324. + dinfo = au_di(dentry);
  7325. + err = au_di_realloc(dinfo, au_sbbot(sb) + 1);
  7326. + if (unlikely(err))
  7327. + goto out;
  7328. + ebrange = au_dbrange_test(dentry);
  7329. + if (!ebrange)
  7330. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7331. +
  7332. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7333. + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
  7334. + if (d_really_is_positive(dentry)) {
  7335. + inode = d_inode(dentry);
  7336. + err = au_refresh_hinode_self(inode);
  7337. + }
  7338. + au_dbg_verify_dinode(dentry);
  7339. + if (!err)
  7340. + goto out_dgen; /* success */
  7341. + goto out;
  7342. + }
  7343. +
  7344. + /* temporary dinfo */
  7345. + AuDbgDentry(dentry);
  7346. + err = -ENOMEM;
  7347. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7348. + if (unlikely(!tmp))
  7349. + goto out;
  7350. + au_di_swap(tmp, dinfo);
  7351. + /* returns the number of positive dentries */
  7352. + /*
  7353. + * if current working dir is removed, it returns an error.
  7354. + * but the dentry is legal.
  7355. + */
  7356. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  7357. + AuDbgDentry(dentry);
  7358. + au_di_swap(tmp, dinfo);
  7359. + if (err == -ENOENT)
  7360. + err = 0;
  7361. + if (err >= 0) {
  7362. + /* compare/refresh by dinfo */
  7363. + AuDbgDentry(dentry);
  7364. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7365. + au_dbg_verify_dinode(dentry);
  7366. + AuTraceErr(err);
  7367. + }
  7368. + au_rw_write_unlock(&tmp->di_rwsem);
  7369. + au_di_free(tmp);
  7370. + if (unlikely(err))
  7371. + goto out;
  7372. +
  7373. +out_dgen:
  7374. + au_update_digen(dentry);
  7375. +out:
  7376. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7377. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7378. + AuDbgDentry(dentry);
  7379. + }
  7380. + AuTraceErr(err);
  7381. + return err;
  7382. +}
  7383. +
  7384. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7385. + struct dentry *dentry, aufs_bindex_t bindex)
  7386. +{
  7387. + int err, valid;
  7388. +
  7389. + err = 0;
  7390. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7391. + goto out;
  7392. +
  7393. + AuDbg("b%d\n", bindex);
  7394. + /*
  7395. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7396. + * due to whiteout and branch permission.
  7397. + */
  7398. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7399. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7400. + /* it may return tri-state */
  7401. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7402. +
  7403. + if (unlikely(valid < 0))
  7404. + err = valid;
  7405. + else if (!valid)
  7406. + err = -EINVAL;
  7407. +
  7408. +out:
  7409. + AuTraceErr(err);
  7410. + return err;
  7411. +}
  7412. +
  7413. +/* todo: remove this */
  7414. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7415. + unsigned int flags, int do_udba)
  7416. +{
  7417. + int err;
  7418. + umode_t mode, h_mode;
  7419. + aufs_bindex_t bindex, btail, btop, ibs, ibe;
  7420. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7421. + struct inode *h_inode, *h_cached_inode;
  7422. + struct dentry *h_dentry;
  7423. + struct qstr *name, *h_name;
  7424. +
  7425. + err = 0;
  7426. + plus = 0;
  7427. + mode = 0;
  7428. + ibs = -1;
  7429. + ibe = -1;
  7430. + unhashed = !!d_unhashed(dentry);
  7431. + is_root = !!IS_ROOT(dentry);
  7432. + name = &dentry->d_name;
  7433. + tmpfile = au_di(dentry)->di_tmpfile;
  7434. +
  7435. + /*
  7436. + * Theoretically, REVAL test should be unnecessary in case of
  7437. + * {FS,I}NOTIFY.
  7438. + * But {fs,i}notify doesn't fire some necessary events,
  7439. + * IN_ATTRIB for atime/nlink/pageio
  7440. + * Let's do REVAL test too.
  7441. + */
  7442. + if (do_udba && inode) {
  7443. + mode = (inode->i_mode & S_IFMT);
  7444. + plus = (inode->i_nlink > 0);
  7445. + ibs = au_ibtop(inode);
  7446. + ibe = au_ibbot(inode);
  7447. + }
  7448. +
  7449. + btop = au_dbtop(dentry);
  7450. + btail = btop;
  7451. + if (inode && S_ISDIR(inode->i_mode))
  7452. + btail = au_dbtaildir(dentry);
  7453. + for (bindex = btop; bindex <= btail; bindex++) {
  7454. + h_dentry = au_h_dptr(dentry, bindex);
  7455. + if (!h_dentry)
  7456. + continue;
  7457. +
  7458. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7459. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7460. + spin_lock(&h_dentry->d_lock);
  7461. + h_name = &h_dentry->d_name;
  7462. + if (unlikely(do_udba
  7463. + && !is_root
  7464. + && ((!h_nfs
  7465. + && (unhashed != !!d_unhashed(h_dentry)
  7466. + || (!tmpfile
  7467. + && !au_qstreq(name, h_name))
  7468. + ))
  7469. + || (h_nfs
  7470. + && !(flags & LOOKUP_OPEN)
  7471. + && (h_dentry->d_flags
  7472. + & DCACHE_NFSFS_RENAMED)))
  7473. + )) {
  7474. + int h_unhashed;
  7475. +
  7476. + h_unhashed = d_unhashed(h_dentry);
  7477. + spin_unlock(&h_dentry->d_lock);
  7478. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7479. + unhashed, h_unhashed, dentry, h_dentry);
  7480. + goto err;
  7481. + }
  7482. + spin_unlock(&h_dentry->d_lock);
  7483. +
  7484. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7485. + if (unlikely(err))
  7486. + /* do not goto err, to keep the errno */
  7487. + break;
  7488. +
  7489. + /* todo: plink too? */
  7490. + if (!do_udba)
  7491. + continue;
  7492. +
  7493. + /* UDBA tests */
  7494. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7495. + goto err;
  7496. +
  7497. + h_inode = NULL;
  7498. + if (d_is_positive(h_dentry))
  7499. + h_inode = d_inode(h_dentry);
  7500. + h_plus = plus;
  7501. + h_mode = mode;
  7502. + h_cached_inode = h_inode;
  7503. + if (h_inode) {
  7504. + h_mode = (h_inode->i_mode & S_IFMT);
  7505. + h_plus = (h_inode->i_nlink > 0);
  7506. + }
  7507. + if (inode && ibs <= bindex && bindex <= ibe)
  7508. + h_cached_inode = au_h_iptr(inode, bindex);
  7509. +
  7510. + if (!h_nfs) {
  7511. + if (unlikely(plus != h_plus && !tmpfile))
  7512. + goto err;
  7513. + } else {
  7514. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7515. + && !is_root
  7516. + && !IS_ROOT(h_dentry)
  7517. + && unhashed != d_unhashed(h_dentry)))
  7518. + goto err;
  7519. + }
  7520. + if (unlikely(mode != h_mode
  7521. + || h_cached_inode != h_inode))
  7522. + goto err;
  7523. + continue;
  7524. +
  7525. +err:
  7526. + err = -EINVAL;
  7527. + break;
  7528. + }
  7529. +
  7530. + AuTraceErr(err);
  7531. + return err;
  7532. +}
  7533. +
  7534. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  7535. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7536. +{
  7537. + int err;
  7538. + struct dentry *parent;
  7539. +
  7540. + if (!au_digen_test(dentry, sigen))
  7541. + return 0;
  7542. +
  7543. + parent = dget_parent(dentry);
  7544. + di_read_lock_parent(parent, AuLock_IR);
  7545. + AuDebugOn(au_digen_test(parent, sigen));
  7546. + au_dbg_verify_gen(parent, sigen);
  7547. + err = au_refresh_dentry(dentry, parent);
  7548. + di_read_unlock(parent, AuLock_IR);
  7549. + dput(parent);
  7550. + AuTraceErr(err);
  7551. + return err;
  7552. +}
  7553. +
  7554. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7555. +{
  7556. + int err;
  7557. + struct dentry *d, *parent;
  7558. +
  7559. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  7560. + return simple_reval_dpath(dentry, sigen);
  7561. +
  7562. + /* slow loop, keep it simple and stupid */
  7563. + /* cf: au_cpup_dirs() */
  7564. + err = 0;
  7565. + parent = NULL;
  7566. + while (au_digen_test(dentry, sigen)) {
  7567. + d = dentry;
  7568. + while (1) {
  7569. + dput(parent);
  7570. + parent = dget_parent(d);
  7571. + if (!au_digen_test(parent, sigen))
  7572. + break;
  7573. + d = parent;
  7574. + }
  7575. +
  7576. + if (d != dentry)
  7577. + di_write_lock_child2(d);
  7578. +
  7579. + /* someone might update our dentry while we were sleeping */
  7580. + if (au_digen_test(d, sigen)) {
  7581. + /*
  7582. + * todo: consolidate with simple_reval_dpath(),
  7583. + * do_refresh() and au_reval_for_attr().
  7584. + */
  7585. + di_read_lock_parent(parent, AuLock_IR);
  7586. + err = au_refresh_dentry(d, parent);
  7587. + di_read_unlock(parent, AuLock_IR);
  7588. + }
  7589. +
  7590. + if (d != dentry)
  7591. + di_write_unlock(d);
  7592. + dput(parent);
  7593. + if (unlikely(err))
  7594. + break;
  7595. + }
  7596. +
  7597. + return err;
  7598. +}
  7599. +
  7600. +/*
  7601. + * if valid returns 1, otherwise 0.
  7602. + */
  7603. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  7604. +{
  7605. + int valid, err;
  7606. + unsigned int sigen;
  7607. + unsigned char do_udba;
  7608. + struct super_block *sb;
  7609. + struct inode *inode;
  7610. +
  7611. + /* todo: support rcu-walk? */
  7612. + if (flags & LOOKUP_RCU)
  7613. + return -ECHILD;
  7614. +
  7615. + valid = 0;
  7616. + if (unlikely(!au_di(dentry)))
  7617. + goto out;
  7618. +
  7619. + valid = 1;
  7620. + sb = dentry->d_sb;
  7621. + /*
  7622. + * todo: very ugly
  7623. + * i_mutex of parent dir may be held,
  7624. + * but we should not return 'invalid' due to busy.
  7625. + */
  7626. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  7627. + if (unlikely(err)) {
  7628. + valid = err;
  7629. + AuTraceErr(err);
  7630. + goto out;
  7631. + }
  7632. + inode = NULL;
  7633. + if (d_really_is_positive(dentry))
  7634. + inode = d_inode(dentry);
  7635. + if (unlikely(inode && au_is_bad_inode(inode))) {
  7636. + err = -EINVAL;
  7637. + AuTraceErr(err);
  7638. + goto out_dgrade;
  7639. + }
  7640. + if (unlikely(au_dbrange_test(dentry))) {
  7641. + err = -EINVAL;
  7642. + AuTraceErr(err);
  7643. + goto out_dgrade;
  7644. + }
  7645. +
  7646. + sigen = au_sigen(sb);
  7647. + if (au_digen_test(dentry, sigen)) {
  7648. + AuDebugOn(IS_ROOT(dentry));
  7649. + err = au_reval_dpath(dentry, sigen);
  7650. + if (unlikely(err)) {
  7651. + AuTraceErr(err);
  7652. + goto out_dgrade;
  7653. + }
  7654. + }
  7655. + di_downgrade_lock(dentry, AuLock_IR);
  7656. +
  7657. + err = -EINVAL;
  7658. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  7659. + && inode
  7660. + && !(inode->i_state && I_LINKABLE)
  7661. + && (IS_DEADDIR(inode) || !inode->i_nlink)) {
  7662. + AuTraceErr(err);
  7663. + goto out_inval;
  7664. + }
  7665. +
  7666. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  7667. + if (do_udba && inode) {
  7668. + aufs_bindex_t btop = au_ibtop(inode);
  7669. + struct inode *h_inode;
  7670. +
  7671. + if (btop >= 0) {
  7672. + h_inode = au_h_iptr(inode, btop);
  7673. + if (h_inode && au_test_higen(inode, h_inode)) {
  7674. + AuTraceErr(err);
  7675. + goto out_inval;
  7676. + }
  7677. + }
  7678. + }
  7679. +
  7680. + err = h_d_revalidate(dentry, inode, flags, do_udba);
  7681. + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
  7682. + err = -EIO;
  7683. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  7684. + dentry, err);
  7685. + }
  7686. + goto out_inval;
  7687. +
  7688. +out_dgrade:
  7689. + di_downgrade_lock(dentry, AuLock_IR);
  7690. +out_inval:
  7691. + aufs_read_unlock(dentry, AuLock_IR);
  7692. + AuTraceErr(err);
  7693. + valid = !err;
  7694. +out:
  7695. + if (!valid) {
  7696. + AuDbg("%pd invalid, %d\n", dentry, valid);
  7697. + d_drop(dentry);
  7698. + }
  7699. + return valid;
  7700. +}
  7701. +
  7702. +static void aufs_d_release(struct dentry *dentry)
  7703. +{
  7704. + if (au_di(dentry)) {
  7705. + au_di_fin(dentry);
  7706. + au_hn_di_reinit(dentry);
  7707. + }
  7708. +}
  7709. +
  7710. +const struct dentry_operations aufs_dop = {
  7711. + .d_revalidate = aufs_d_revalidate,
  7712. + .d_weak_revalidate = aufs_d_revalidate,
  7713. + .d_release = aufs_d_release
  7714. +};
  7715. +
  7716. +/* aufs_dop without d_revalidate */
  7717. +const struct dentry_operations aufs_dop_noreval = {
  7718. + .d_release = aufs_d_release
  7719. +};
  7720. diff -Naur null/fs/aufs/dentry.h linux-4.7/fs/aufs/dentry.h
  7721. --- /dev/null
  7722. +++ linux-4.7/fs/aufs/dentry.h 2016-08-08 12:57:44.599384741 -0400
  7723. @@ -0,0 +1,255 @@
  7724. +/*
  7725. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  7726. + *
  7727. + * This program, aufs is free software; you can redistribute it and/or modify
  7728. + * it under the terms of the GNU General Public License as published by
  7729. + * the Free Software Foundation; either version 2 of the License, or
  7730. + * (at your option) any later version.
  7731. + *
  7732. + * This program is distributed in the hope that it will be useful,
  7733. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7734. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7735. + * GNU General Public License for more details.
  7736. + *
  7737. + * You should have received a copy of the GNU General Public License
  7738. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7739. + */
  7740. +
  7741. +/*
  7742. + * lookup and dentry operations
  7743. + */
  7744. +
  7745. +#ifndef __AUFS_DENTRY_H__
  7746. +#define __AUFS_DENTRY_H__
  7747. +
  7748. +#ifdef __KERNEL__
  7749. +
  7750. +#include <linux/dcache.h>
  7751. +#include "rwsem.h"
  7752. +
  7753. +struct au_hdentry {
  7754. + struct dentry *hd_dentry;
  7755. + aufs_bindex_t hd_id;
  7756. +};
  7757. +
  7758. +struct au_dinfo {
  7759. + atomic_t di_generation;
  7760. +
  7761. + struct au_rwsem di_rwsem;
  7762. + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
  7763. + unsigned char di_tmpfile; /* to allow the different name */
  7764. + union {
  7765. + struct au_hdentry *di_hdentry;
  7766. + struct llist_node di_lnode; /* delayed free */
  7767. + };
  7768. +} ____cacheline_aligned_in_smp;
  7769. +
  7770. +/* ---------------------------------------------------------------------- */
  7771. +
  7772. +/* flags for au_lkup_dentry() */
  7773. +#define AuLkup_ALLOW_NEG 1
  7774. +#define AuLkup_IGNORE_PERM (1 << 1)
  7775. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  7776. +#define au_fset_lkup(flags, name) \
  7777. + do { (flags) |= AuLkup_##name; } while (0)
  7778. +#define au_fclr_lkup(flags, name) \
  7779. + do { (flags) &= ~AuLkup_##name; } while (0)
  7780. +
  7781. +/* ---------------------------------------------------------------------- */
  7782. +
  7783. +/* dentry.c */
  7784. +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
  7785. +struct au_branch;
  7786. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  7787. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7788. + struct dentry *h_parent, struct au_branch *br);
  7789. +
  7790. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  7791. + unsigned int flags);
  7792. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  7793. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  7794. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  7795. +void au_refresh_dop(struct dentry *dentry, int force_reval);
  7796. +
  7797. +/* dinfo.c */
  7798. +void au_di_init_once(void *_di);
  7799. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  7800. +void au_di_free(struct au_dinfo *dinfo);
  7801. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  7802. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  7803. +int au_di_init(struct dentry *dentry);
  7804. +void au_di_fin(struct dentry *dentry);
  7805. +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
  7806. +
  7807. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  7808. +void di_read_unlock(struct dentry *d, int flags);
  7809. +void di_downgrade_lock(struct dentry *d, int flags);
  7810. +void di_write_lock(struct dentry *d, unsigned int lsc);
  7811. +void di_write_unlock(struct dentry *d);
  7812. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  7813. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  7814. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  7815. +
  7816. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  7817. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  7818. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  7819. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  7820. +
  7821. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  7822. + struct dentry *h_dentry);
  7823. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  7824. +int au_dbrange_test(struct dentry *dentry);
  7825. +void au_update_digen(struct dentry *dentry);
  7826. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  7827. +void au_update_dbtop(struct dentry *dentry);
  7828. +void au_update_dbbot(struct dentry *dentry);
  7829. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  7830. +
  7831. +/* ---------------------------------------------------------------------- */
  7832. +
  7833. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  7834. +{
  7835. + return dentry->d_fsdata;
  7836. +}
  7837. +
  7838. +/* ---------------------------------------------------------------------- */
  7839. +
  7840. +/* lock subclass for dinfo */
  7841. +enum {
  7842. + AuLsc_DI_CHILD, /* child first */
  7843. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  7844. + AuLsc_DI_CHILD3, /* copyup dirs */
  7845. + AuLsc_DI_PARENT,
  7846. + AuLsc_DI_PARENT2,
  7847. + AuLsc_DI_PARENT3,
  7848. + AuLsc_DI_TMP /* temp for replacing dinfo */
  7849. +};
  7850. +
  7851. +/*
  7852. + * di_read_lock_child, di_write_lock_child,
  7853. + * di_read_lock_child2, di_write_lock_child2,
  7854. + * di_read_lock_child3, di_write_lock_child3,
  7855. + * di_read_lock_parent, di_write_lock_parent,
  7856. + * di_read_lock_parent2, di_write_lock_parent2,
  7857. + * di_read_lock_parent3, di_write_lock_parent3,
  7858. + */
  7859. +#define AuReadLockFunc(name, lsc) \
  7860. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  7861. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  7862. +
  7863. +#define AuWriteLockFunc(name, lsc) \
  7864. +static inline void di_write_lock_##name(struct dentry *d) \
  7865. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  7866. +
  7867. +#define AuRWLockFuncs(name, lsc) \
  7868. + AuReadLockFunc(name, lsc) \
  7869. + AuWriteLockFunc(name, lsc)
  7870. +
  7871. +AuRWLockFuncs(child, CHILD);
  7872. +AuRWLockFuncs(child2, CHILD2);
  7873. +AuRWLockFuncs(child3, CHILD3);
  7874. +AuRWLockFuncs(parent, PARENT);
  7875. +AuRWLockFuncs(parent2, PARENT2);
  7876. +AuRWLockFuncs(parent3, PARENT3);
  7877. +
  7878. +#undef AuReadLockFunc
  7879. +#undef AuWriteLockFunc
  7880. +#undef AuRWLockFuncs
  7881. +
  7882. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  7883. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  7884. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  7885. +
  7886. +/* ---------------------------------------------------------------------- */
  7887. +
  7888. +/* todo: memory barrier? */
  7889. +static inline unsigned int au_digen(struct dentry *d)
  7890. +{
  7891. + return atomic_read(&au_di(d)->di_generation);
  7892. +}
  7893. +
  7894. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  7895. +{
  7896. + hdentry->hd_dentry = NULL;
  7897. +}
  7898. +
  7899. +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
  7900. + aufs_bindex_t bindex)
  7901. +{
  7902. + return di->di_hdentry + bindex;
  7903. +}
  7904. +
  7905. +static inline void au_hdput(struct au_hdentry *hd)
  7906. +{
  7907. + if (hd)
  7908. + dput(hd->hd_dentry);
  7909. +}
  7910. +
  7911. +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
  7912. +{
  7913. + DiMustAnyLock(dentry);
  7914. + return au_di(dentry)->di_btop;
  7915. +}
  7916. +
  7917. +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
  7918. +{
  7919. + DiMustAnyLock(dentry);
  7920. + return au_di(dentry)->di_bbot;
  7921. +}
  7922. +
  7923. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  7924. +{
  7925. + DiMustAnyLock(dentry);
  7926. + return au_di(dentry)->di_bwh;
  7927. +}
  7928. +
  7929. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  7930. +{
  7931. + DiMustAnyLock(dentry);
  7932. + return au_di(dentry)->di_bdiropq;
  7933. +}
  7934. +
  7935. +/* todo: hard/soft set? */
  7936. +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
  7937. +{
  7938. + DiMustWriteLock(dentry);
  7939. + au_di(dentry)->di_btop = bindex;
  7940. +}
  7941. +
  7942. +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
  7943. +{
  7944. + DiMustWriteLock(dentry);
  7945. + au_di(dentry)->di_bbot = bindex;
  7946. +}
  7947. +
  7948. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  7949. +{
  7950. + DiMustWriteLock(dentry);
  7951. + /* dbwh can be outside of btop - bbot range */
  7952. + au_di(dentry)->di_bwh = bindex;
  7953. +}
  7954. +
  7955. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  7956. +{
  7957. + DiMustWriteLock(dentry);
  7958. + au_di(dentry)->di_bdiropq = bindex;
  7959. +}
  7960. +
  7961. +/* ---------------------------------------------------------------------- */
  7962. +
  7963. +#ifdef CONFIG_AUFS_HNOTIFY
  7964. +static inline void au_digen_dec(struct dentry *d)
  7965. +{
  7966. + atomic_dec(&au_di(d)->di_generation);
  7967. +}
  7968. +
  7969. +static inline void au_hn_di_reinit(struct dentry *dentry)
  7970. +{
  7971. + dentry->d_fsdata = NULL;
  7972. +}
  7973. +#else
  7974. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  7975. +#endif /* CONFIG_AUFS_HNOTIFY */
  7976. +
  7977. +#endif /* __KERNEL__ */
  7978. +#endif /* __AUFS_DENTRY_H__ */
  7979. diff -Naur null/fs/aufs/dinfo.c linux-4.7/fs/aufs/dinfo.c
  7980. --- /dev/null
  7981. +++ linux-4.7/fs/aufs/dinfo.c 2016-08-08 12:57:44.599384741 -0400
  7982. @@ -0,0 +1,552 @@
  7983. +/*
  7984. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  7985. + *
  7986. + * This program, aufs is free software; you can redistribute it and/or modify
  7987. + * it under the terms of the GNU General Public License as published by
  7988. + * the Free Software Foundation; either version 2 of the License, or
  7989. + * (at your option) any later version.
  7990. + *
  7991. + * This program is distributed in the hope that it will be useful,
  7992. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7993. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7994. + * GNU General Public License for more details.
  7995. + *
  7996. + * You should have received a copy of the GNU General Public License
  7997. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7998. + */
  7999. +
  8000. +/*
  8001. + * dentry private data
  8002. + */
  8003. +
  8004. +#include "aufs.h"
  8005. +
  8006. +void au_di_init_once(void *_dinfo)
  8007. +{
  8008. + struct au_dinfo *dinfo = _dinfo;
  8009. +
  8010. + au_rw_init(&dinfo->di_rwsem);
  8011. +}
  8012. +
  8013. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  8014. +{
  8015. + struct au_dinfo *dinfo;
  8016. + int nbr, i;
  8017. +
  8018. + dinfo = au_cache_alloc_dinfo();
  8019. + if (unlikely(!dinfo))
  8020. + goto out;
  8021. +
  8022. + nbr = au_sbbot(sb) + 1;
  8023. + if (nbr <= 0)
  8024. + nbr = 1;
  8025. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  8026. + if (dinfo->di_hdentry) {
  8027. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  8028. + dinfo->di_btop = -1;
  8029. + dinfo->di_bbot = -1;
  8030. + dinfo->di_bwh = -1;
  8031. + dinfo->di_bdiropq = -1;
  8032. + dinfo->di_tmpfile = 0;
  8033. + for (i = 0; i < nbr; i++)
  8034. + dinfo->di_hdentry[i].hd_id = -1;
  8035. + goto out;
  8036. + }
  8037. +
  8038. + au_cache_dfree_dinfo(dinfo);
  8039. + dinfo = NULL;
  8040. +
  8041. +out:
  8042. + return dinfo;
  8043. +}
  8044. +
  8045. +void au_di_free(struct au_dinfo *dinfo)
  8046. +{
  8047. + struct au_hdentry *p;
  8048. + aufs_bindex_t bbot, bindex;
  8049. +
  8050. + /* dentry may not be revalidated */
  8051. + bindex = dinfo->di_btop;
  8052. + if (bindex >= 0) {
  8053. + bbot = dinfo->di_bbot;
  8054. + p = au_hdentry(dinfo, bindex);
  8055. + while (bindex++ <= bbot)
  8056. + au_hdput(p++);
  8057. + }
  8058. + au_delayed_kfree(dinfo->di_hdentry);
  8059. + au_cache_dfree_dinfo(dinfo);
  8060. +}
  8061. +
  8062. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  8063. +{
  8064. + struct au_hdentry *p;
  8065. + aufs_bindex_t bi;
  8066. +
  8067. + AuRwMustWriteLock(&a->di_rwsem);
  8068. + AuRwMustWriteLock(&b->di_rwsem);
  8069. +
  8070. +#define DiSwap(v, name) \
  8071. + do { \
  8072. + v = a->di_##name; \
  8073. + a->di_##name = b->di_##name; \
  8074. + b->di_##name = v; \
  8075. + } while (0)
  8076. +
  8077. + DiSwap(p, hdentry);
  8078. + DiSwap(bi, btop);
  8079. + DiSwap(bi, bbot);
  8080. + DiSwap(bi, bwh);
  8081. + DiSwap(bi, bdiropq);
  8082. + /* smp_mb(); */
  8083. +
  8084. +#undef DiSwap
  8085. +}
  8086. +
  8087. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  8088. +{
  8089. + AuRwMustWriteLock(&dst->di_rwsem);
  8090. + AuRwMustWriteLock(&src->di_rwsem);
  8091. +
  8092. + dst->di_btop = src->di_btop;
  8093. + dst->di_bbot = src->di_bbot;
  8094. + dst->di_bwh = src->di_bwh;
  8095. + dst->di_bdiropq = src->di_bdiropq;
  8096. + /* smp_mb(); */
  8097. +}
  8098. +
  8099. +int au_di_init(struct dentry *dentry)
  8100. +{
  8101. + int err;
  8102. + struct super_block *sb;
  8103. + struct au_dinfo *dinfo;
  8104. +
  8105. + err = 0;
  8106. + sb = dentry->d_sb;
  8107. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  8108. + if (dinfo) {
  8109. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  8110. + /* smp_mb(); */ /* atomic_set */
  8111. + dentry->d_fsdata = dinfo;
  8112. + } else
  8113. + err = -ENOMEM;
  8114. +
  8115. + return err;
  8116. +}
  8117. +
  8118. +void au_di_fin(struct dentry *dentry)
  8119. +{
  8120. + struct au_dinfo *dinfo;
  8121. +
  8122. + dinfo = au_di(dentry);
  8123. + AuRwDestroy(&dinfo->di_rwsem);
  8124. + au_di_free(dinfo);
  8125. +}
  8126. +
  8127. +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
  8128. +{
  8129. + int err, sz;
  8130. + struct au_hdentry *hdp;
  8131. +
  8132. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8133. +
  8134. + err = -ENOMEM;
  8135. + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
  8136. + if (!sz)
  8137. + sz = sizeof(*hdp);
  8138. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
  8139. + if (hdp) {
  8140. + dinfo->di_hdentry = hdp;
  8141. + err = 0;
  8142. + }
  8143. +
  8144. + return err;
  8145. +}
  8146. +
  8147. +/* ---------------------------------------------------------------------- */
  8148. +
  8149. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8150. +{
  8151. + switch (lsc) {
  8152. + case AuLsc_DI_CHILD:
  8153. + ii_write_lock_child(inode);
  8154. + break;
  8155. + case AuLsc_DI_CHILD2:
  8156. + ii_write_lock_child2(inode);
  8157. + break;
  8158. + case AuLsc_DI_CHILD3:
  8159. + ii_write_lock_child3(inode);
  8160. + break;
  8161. + case AuLsc_DI_PARENT:
  8162. + ii_write_lock_parent(inode);
  8163. + break;
  8164. + case AuLsc_DI_PARENT2:
  8165. + ii_write_lock_parent2(inode);
  8166. + break;
  8167. + case AuLsc_DI_PARENT3:
  8168. + ii_write_lock_parent3(inode);
  8169. + break;
  8170. + default:
  8171. + BUG();
  8172. + }
  8173. +}
  8174. +
  8175. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8176. +{
  8177. + switch (lsc) {
  8178. + case AuLsc_DI_CHILD:
  8179. + ii_read_lock_child(inode);
  8180. + break;
  8181. + case AuLsc_DI_CHILD2:
  8182. + ii_read_lock_child2(inode);
  8183. + break;
  8184. + case AuLsc_DI_CHILD3:
  8185. + ii_read_lock_child3(inode);
  8186. + break;
  8187. + case AuLsc_DI_PARENT:
  8188. + ii_read_lock_parent(inode);
  8189. + break;
  8190. + case AuLsc_DI_PARENT2:
  8191. + ii_read_lock_parent2(inode);
  8192. + break;
  8193. + case AuLsc_DI_PARENT3:
  8194. + ii_read_lock_parent3(inode);
  8195. + break;
  8196. + default:
  8197. + BUG();
  8198. + }
  8199. +}
  8200. +
  8201. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8202. +{
  8203. + struct inode *inode;
  8204. +
  8205. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8206. + if (d_really_is_positive(d)) {
  8207. + inode = d_inode(d);
  8208. + if (au_ftest_lock(flags, IW))
  8209. + do_ii_write_lock(inode, lsc);
  8210. + else if (au_ftest_lock(flags, IR))
  8211. + do_ii_read_lock(inode, lsc);
  8212. + }
  8213. +}
  8214. +
  8215. +void di_read_unlock(struct dentry *d, int flags)
  8216. +{
  8217. + struct inode *inode;
  8218. +
  8219. + if (d_really_is_positive(d)) {
  8220. + inode = d_inode(d);
  8221. + if (au_ftest_lock(flags, IW)) {
  8222. + au_dbg_verify_dinode(d);
  8223. + ii_write_unlock(inode);
  8224. + } else if (au_ftest_lock(flags, IR)) {
  8225. + au_dbg_verify_dinode(d);
  8226. + ii_read_unlock(inode);
  8227. + }
  8228. + }
  8229. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8230. +}
  8231. +
  8232. +void di_downgrade_lock(struct dentry *d, int flags)
  8233. +{
  8234. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8235. + ii_downgrade_lock(d_inode(d));
  8236. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8237. +}
  8238. +
  8239. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8240. +{
  8241. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8242. + if (d_really_is_positive(d))
  8243. + do_ii_write_lock(d_inode(d), lsc);
  8244. +}
  8245. +
  8246. +void di_write_unlock(struct dentry *d)
  8247. +{
  8248. + au_dbg_verify_dinode(d);
  8249. + if (d_really_is_positive(d))
  8250. + ii_write_unlock(d_inode(d));
  8251. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8252. +}
  8253. +
  8254. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8255. +{
  8256. + AuDebugOn(d1 == d2
  8257. + || d_inode(d1) == d_inode(d2)
  8258. + || d1->d_sb != d2->d_sb);
  8259. +
  8260. + if (isdir && au_test_subdir(d1, d2)) {
  8261. + di_write_lock_child(d1);
  8262. + di_write_lock_child2(d2);
  8263. + } else {
  8264. + /* there should be no races */
  8265. + di_write_lock_child(d2);
  8266. + di_write_lock_child2(d1);
  8267. + }
  8268. +}
  8269. +
  8270. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8271. +{
  8272. + AuDebugOn(d1 == d2
  8273. + || d_inode(d1) == d_inode(d2)
  8274. + || d1->d_sb != d2->d_sb);
  8275. +
  8276. + if (isdir && au_test_subdir(d1, d2)) {
  8277. + di_write_lock_parent(d1);
  8278. + di_write_lock_parent2(d2);
  8279. + } else {
  8280. + /* there should be no races */
  8281. + di_write_lock_parent(d2);
  8282. + di_write_lock_parent2(d1);
  8283. + }
  8284. +}
  8285. +
  8286. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8287. +{
  8288. + di_write_unlock(d1);
  8289. + if (d_inode(d1) == d_inode(d2))
  8290. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8291. + else
  8292. + di_write_unlock(d2);
  8293. +}
  8294. +
  8295. +/* ---------------------------------------------------------------------- */
  8296. +
  8297. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8298. +{
  8299. + struct dentry *d;
  8300. +
  8301. + DiMustAnyLock(dentry);
  8302. +
  8303. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  8304. + return NULL;
  8305. + AuDebugOn(bindex < 0);
  8306. + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
  8307. + AuDebugOn(d && au_dcount(d) <= 0);
  8308. + return d;
  8309. +}
  8310. +
  8311. +/*
  8312. + * extended version of au_h_dptr().
  8313. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8314. + * error.
  8315. + */
  8316. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8317. +{
  8318. + struct dentry *h_dentry;
  8319. + struct inode *inode, *h_inode;
  8320. +
  8321. + AuDebugOn(d_really_is_negative(dentry));
  8322. +
  8323. + h_dentry = NULL;
  8324. + if (au_dbtop(dentry) <= bindex
  8325. + && bindex <= au_dbbot(dentry))
  8326. + h_dentry = au_h_dptr(dentry, bindex);
  8327. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8328. + dget(h_dentry);
  8329. + goto out; /* success */
  8330. + }
  8331. +
  8332. + inode = d_inode(dentry);
  8333. + AuDebugOn(bindex < au_ibtop(inode));
  8334. + AuDebugOn(au_ibbot(inode) < bindex);
  8335. + h_inode = au_h_iptr(inode, bindex);
  8336. + h_dentry = d_find_alias(h_inode);
  8337. + if (h_dentry) {
  8338. + if (!IS_ERR(h_dentry)) {
  8339. + if (!au_d_linkable(h_dentry))
  8340. + goto out; /* success */
  8341. + dput(h_dentry);
  8342. + } else
  8343. + goto out;
  8344. + }
  8345. +
  8346. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8347. + h_dentry = au_plink_lkup(inode, bindex);
  8348. + AuDebugOn(!h_dentry);
  8349. + if (!IS_ERR(h_dentry)) {
  8350. + if (!au_d_hashed_positive(h_dentry))
  8351. + goto out; /* success */
  8352. + dput(h_dentry);
  8353. + h_dentry = NULL;
  8354. + }
  8355. + }
  8356. +
  8357. +out:
  8358. + AuDbgDentry(h_dentry);
  8359. + return h_dentry;
  8360. +}
  8361. +
  8362. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8363. +{
  8364. + aufs_bindex_t bbot, bwh;
  8365. +
  8366. + bbot = au_dbbot(dentry);
  8367. + if (0 <= bbot) {
  8368. + bwh = au_dbwh(dentry);
  8369. + if (!bwh)
  8370. + return bwh;
  8371. + if (0 < bwh && bwh < bbot)
  8372. + return bwh - 1;
  8373. + }
  8374. + return bbot;
  8375. +}
  8376. +
  8377. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8378. +{
  8379. + aufs_bindex_t bbot, bopq;
  8380. +
  8381. + bbot = au_dbtail(dentry);
  8382. + if (0 <= bbot) {
  8383. + bopq = au_dbdiropq(dentry);
  8384. + if (0 <= bopq && bopq < bbot)
  8385. + bbot = bopq;
  8386. + }
  8387. + return bbot;
  8388. +}
  8389. +
  8390. +/* ---------------------------------------------------------------------- */
  8391. +
  8392. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8393. + struct dentry *h_dentry)
  8394. +{
  8395. + struct au_dinfo *dinfo;
  8396. + struct au_hdentry *hd;
  8397. + struct au_branch *br;
  8398. +
  8399. + DiMustWriteLock(dentry);
  8400. +
  8401. + dinfo = au_di(dentry);
  8402. + hd = au_hdentry(dinfo, bindex);
  8403. + au_hdput(hd);
  8404. + hd->hd_dentry = h_dentry;
  8405. + if (h_dentry) {
  8406. + br = au_sbr(dentry->d_sb, bindex);
  8407. + hd->hd_id = br->br_id;
  8408. + }
  8409. +}
  8410. +
  8411. +int au_dbrange_test(struct dentry *dentry)
  8412. +{
  8413. + int err;
  8414. + aufs_bindex_t btop, bbot;
  8415. +
  8416. + err = 0;
  8417. + btop = au_dbtop(dentry);
  8418. + bbot = au_dbbot(dentry);
  8419. + if (btop >= 0)
  8420. + AuDebugOn(bbot < 0 && btop > bbot);
  8421. + else {
  8422. + err = -EIO;
  8423. + AuDebugOn(bbot >= 0);
  8424. + }
  8425. +
  8426. + return err;
  8427. +}
  8428. +
  8429. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8430. +{
  8431. + int err;
  8432. +
  8433. + err = 0;
  8434. + if (unlikely(au_digen(dentry) != sigen
  8435. + || au_iigen_test(d_inode(dentry), sigen)))
  8436. + err = -EIO;
  8437. +
  8438. + return err;
  8439. +}
  8440. +
  8441. +void au_update_digen(struct dentry *dentry)
  8442. +{
  8443. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8444. + /* smp_mb(); */ /* atomic_set */
  8445. +}
  8446. +
  8447. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8448. +{
  8449. + struct au_dinfo *dinfo;
  8450. + struct dentry *h_d;
  8451. + struct au_hdentry *hdp;
  8452. + aufs_bindex_t bindex, bbot;
  8453. +
  8454. + DiMustWriteLock(dentry);
  8455. +
  8456. + dinfo = au_di(dentry);
  8457. + if (!dinfo || dinfo->di_btop < 0)
  8458. + return;
  8459. +
  8460. + if (do_put_zero) {
  8461. + bbot = dinfo->di_bbot;
  8462. + bindex = dinfo->di_btop;
  8463. + hdp = au_hdentry(dinfo, bindex);
  8464. + for (; bindex <= bbot; bindex++, hdp++) {
  8465. + h_d = hdp->hd_dentry;
  8466. + if (h_d && d_is_negative(h_d))
  8467. + au_set_h_dptr(dentry, bindex, NULL);
  8468. + }
  8469. + }
  8470. +
  8471. + dinfo->di_btop = 0;
  8472. + hdp = au_hdentry(dinfo, dinfo->di_btop);
  8473. + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
  8474. + if (hdp->hd_dentry)
  8475. + break;
  8476. + if (dinfo->di_btop > dinfo->di_bbot) {
  8477. + dinfo->di_btop = -1;
  8478. + dinfo->di_bbot = -1;
  8479. + return;
  8480. + }
  8481. +
  8482. + hdp = au_hdentry(dinfo, dinfo->di_bbot);
  8483. + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
  8484. + if (hdp->hd_dentry)
  8485. + break;
  8486. + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
  8487. +}
  8488. +
  8489. +void au_update_dbtop(struct dentry *dentry)
  8490. +{
  8491. + aufs_bindex_t bindex, bbot;
  8492. + struct dentry *h_dentry;
  8493. +
  8494. + bbot = au_dbbot(dentry);
  8495. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  8496. + h_dentry = au_h_dptr(dentry, bindex);
  8497. + if (!h_dentry)
  8498. + continue;
  8499. + if (d_is_positive(h_dentry)) {
  8500. + au_set_dbtop(dentry, bindex);
  8501. + return;
  8502. + }
  8503. + au_set_h_dptr(dentry, bindex, NULL);
  8504. + }
  8505. +}
  8506. +
  8507. +void au_update_dbbot(struct dentry *dentry)
  8508. +{
  8509. + aufs_bindex_t bindex, btop;
  8510. + struct dentry *h_dentry;
  8511. +
  8512. + btop = au_dbtop(dentry);
  8513. + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
  8514. + h_dentry = au_h_dptr(dentry, bindex);
  8515. + if (!h_dentry)
  8516. + continue;
  8517. + if (d_is_positive(h_dentry)) {
  8518. + au_set_dbbot(dentry, bindex);
  8519. + return;
  8520. + }
  8521. + au_set_h_dptr(dentry, bindex, NULL);
  8522. + }
  8523. +}
  8524. +
  8525. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  8526. +{
  8527. + aufs_bindex_t bindex, bbot;
  8528. +
  8529. + bbot = au_dbbot(dentry);
  8530. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
  8531. + if (au_h_dptr(dentry, bindex) == h_dentry)
  8532. + return bindex;
  8533. + return -1;
  8534. +}
  8535. diff -Naur null/fs/aufs/dir.c linux-4.7/fs/aufs/dir.c
  8536. --- /dev/null
  8537. +++ linux-4.7/fs/aufs/dir.c 2016-08-08 12:57:44.599384741 -0400
  8538. @@ -0,0 +1,762 @@
  8539. +/*
  8540. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  8541. + *
  8542. + * This program, aufs is free software; you can redistribute it and/or modify
  8543. + * it under the terms of the GNU General Public License as published by
  8544. + * the Free Software Foundation; either version 2 of the License, or
  8545. + * (at your option) any later version.
  8546. + *
  8547. + * This program is distributed in the hope that it will be useful,
  8548. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8549. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8550. + * GNU General Public License for more details.
  8551. + *
  8552. + * You should have received a copy of the GNU General Public License
  8553. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8554. + */
  8555. +
  8556. +/*
  8557. + * directory operations
  8558. + */
  8559. +
  8560. +#include <linux/fs_stack.h>
  8561. +#include "aufs.h"
  8562. +
  8563. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  8564. +{
  8565. + unsigned int nlink;
  8566. +
  8567. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8568. +
  8569. + nlink = dir->i_nlink;
  8570. + nlink += h_dir->i_nlink - 2;
  8571. + if (h_dir->i_nlink < 2)
  8572. + nlink += 2;
  8573. + smp_mb(); /* for i_nlink */
  8574. + /* 0 can happen in revaliding */
  8575. + set_nlink(dir, nlink);
  8576. +}
  8577. +
  8578. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  8579. +{
  8580. + unsigned int nlink;
  8581. +
  8582. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8583. +
  8584. + nlink = dir->i_nlink;
  8585. + nlink -= h_dir->i_nlink - 2;
  8586. + if (h_dir->i_nlink < 2)
  8587. + nlink -= 2;
  8588. + smp_mb(); /* for i_nlink */
  8589. + /* nlink == 0 means the branch-fs is broken */
  8590. + set_nlink(dir, nlink);
  8591. +}
  8592. +
  8593. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  8594. +{
  8595. + loff_t sz;
  8596. + aufs_bindex_t bindex, bbot;
  8597. + struct file *h_file;
  8598. + struct dentry *h_dentry;
  8599. +
  8600. + sz = 0;
  8601. + if (file) {
  8602. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  8603. +
  8604. + bbot = au_fbbot_dir(file);
  8605. + for (bindex = au_fbtop(file);
  8606. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  8607. + bindex++) {
  8608. + h_file = au_hf_dir(file, bindex);
  8609. + if (h_file && file_inode(h_file))
  8610. + sz += vfsub_f_size_read(h_file);
  8611. + }
  8612. + } else {
  8613. + AuDebugOn(!dentry);
  8614. + AuDebugOn(!d_is_dir(dentry));
  8615. +
  8616. + bbot = au_dbtaildir(dentry);
  8617. + for (bindex = au_dbtop(dentry);
  8618. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  8619. + bindex++) {
  8620. + h_dentry = au_h_dptr(dentry, bindex);
  8621. + if (h_dentry && d_is_positive(h_dentry))
  8622. + sz += i_size_read(d_inode(h_dentry));
  8623. + }
  8624. + }
  8625. + if (sz < KMALLOC_MAX_SIZE)
  8626. + sz = roundup_pow_of_two(sz);
  8627. + if (sz > KMALLOC_MAX_SIZE)
  8628. + sz = KMALLOC_MAX_SIZE;
  8629. + else if (sz < NAME_MAX) {
  8630. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  8631. + sz = AUFS_RDBLK_DEF;
  8632. + }
  8633. + return sz;
  8634. +}
  8635. +
  8636. +struct au_dir_ts_arg {
  8637. + struct dentry *dentry;
  8638. + aufs_bindex_t brid;
  8639. +};
  8640. +
  8641. +static void au_do_dir_ts(void *arg)
  8642. +{
  8643. + struct au_dir_ts_arg *a = arg;
  8644. + struct au_dtime dt;
  8645. + struct path h_path;
  8646. + struct inode *dir, *h_dir;
  8647. + struct super_block *sb;
  8648. + struct au_branch *br;
  8649. + struct au_hinode *hdir;
  8650. + int err;
  8651. + aufs_bindex_t btop, bindex;
  8652. +
  8653. + sb = a->dentry->d_sb;
  8654. + if (d_really_is_negative(a->dentry))
  8655. + goto out;
  8656. + /* no dir->i_mutex lock */
  8657. + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
  8658. +
  8659. + dir = d_inode(a->dentry);
  8660. + btop = au_ibtop(dir);
  8661. + bindex = au_br_index(sb, a->brid);
  8662. + if (bindex < btop)
  8663. + goto out_unlock;
  8664. +
  8665. + br = au_sbr(sb, bindex);
  8666. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  8667. + if (!h_path.dentry)
  8668. + goto out_unlock;
  8669. + h_path.mnt = au_br_mnt(br);
  8670. + au_dtime_store(&dt, a->dentry, &h_path);
  8671. +
  8672. + br = au_sbr(sb, btop);
  8673. + if (!au_br_writable(br->br_perm))
  8674. + goto out_unlock;
  8675. + h_path.dentry = au_h_dptr(a->dentry, btop);
  8676. + h_path.mnt = au_br_mnt(br);
  8677. + err = vfsub_mnt_want_write(h_path.mnt);
  8678. + if (err)
  8679. + goto out_unlock;
  8680. + hdir = au_hi(dir, btop);
  8681. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  8682. + h_dir = au_h_iptr(dir, btop);
  8683. + if (h_dir->i_nlink
  8684. + && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  8685. + dt.dt_h_path = h_path;
  8686. + au_dtime_revert(&dt);
  8687. + }
  8688. + au_hn_inode_unlock(hdir);
  8689. + vfsub_mnt_drop_write(h_path.mnt);
  8690. + au_cpup_attr_timesizes(dir);
  8691. +
  8692. +out_unlock:
  8693. + aufs_read_unlock(a->dentry, AuLock_DW);
  8694. +out:
  8695. + dput(a->dentry);
  8696. + au_nwt_done(&au_sbi(sb)->si_nowait);
  8697. + au_delayed_kfree(arg);
  8698. +}
  8699. +
  8700. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  8701. +{
  8702. + int perm, wkq_err;
  8703. + aufs_bindex_t btop;
  8704. + struct au_dir_ts_arg *arg;
  8705. + struct dentry *dentry;
  8706. + struct super_block *sb;
  8707. +
  8708. + IMustLock(dir);
  8709. +
  8710. + dentry = d_find_any_alias(dir);
  8711. + AuDebugOn(!dentry);
  8712. + sb = dentry->d_sb;
  8713. + btop = au_ibtop(dir);
  8714. + if (btop == bindex) {
  8715. + au_cpup_attr_timesizes(dir);
  8716. + goto out;
  8717. + }
  8718. +
  8719. + perm = au_sbr_perm(sb, btop);
  8720. + if (!au_br_writable(perm))
  8721. + goto out;
  8722. +
  8723. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  8724. + if (!arg)
  8725. + goto out;
  8726. +
  8727. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  8728. + arg->brid = au_sbr_id(sb, bindex);
  8729. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  8730. + if (unlikely(wkq_err)) {
  8731. + pr_err("wkq %d\n", wkq_err);
  8732. + dput(dentry);
  8733. + au_delayed_kfree(arg);
  8734. + }
  8735. +
  8736. +out:
  8737. + dput(dentry);
  8738. +}
  8739. +
  8740. +/* ---------------------------------------------------------------------- */
  8741. +
  8742. +static int reopen_dir(struct file *file)
  8743. +{
  8744. + int err;
  8745. + unsigned int flags;
  8746. + aufs_bindex_t bindex, btail, btop;
  8747. + struct dentry *dentry, *h_dentry;
  8748. + struct file *h_file;
  8749. +
  8750. + /* open all lower dirs */
  8751. + dentry = file->f_path.dentry;
  8752. + btop = au_dbtop(dentry);
  8753. + for (bindex = au_fbtop(file); bindex < btop; bindex++)
  8754. + au_set_h_fptr(file, bindex, NULL);
  8755. + au_set_fbtop(file, btop);
  8756. +
  8757. + btail = au_dbtaildir(dentry);
  8758. + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
  8759. + au_set_h_fptr(file, bindex, NULL);
  8760. + au_set_fbbot_dir(file, btail);
  8761. +
  8762. + flags = vfsub_file_flags(file);
  8763. + for (bindex = btop; bindex <= btail; bindex++) {
  8764. + h_dentry = au_h_dptr(dentry, bindex);
  8765. + if (!h_dentry)
  8766. + continue;
  8767. + h_file = au_hf_dir(file, bindex);
  8768. + if (h_file)
  8769. + continue;
  8770. +
  8771. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8772. + err = PTR_ERR(h_file);
  8773. + if (IS_ERR(h_file))
  8774. + goto out; /* close all? */
  8775. + au_set_h_fptr(file, bindex, h_file);
  8776. + }
  8777. + au_update_figen(file);
  8778. + /* todo: necessary? */
  8779. + /* file->f_ra = h_file->f_ra; */
  8780. + err = 0;
  8781. +
  8782. +out:
  8783. + return err;
  8784. +}
  8785. +
  8786. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  8787. +{
  8788. + int err;
  8789. + aufs_bindex_t bindex, btail;
  8790. + struct dentry *dentry, *h_dentry;
  8791. + struct vfsmount *mnt;
  8792. +
  8793. + FiMustWriteLock(file);
  8794. + AuDebugOn(h_file);
  8795. +
  8796. + err = 0;
  8797. + mnt = file->f_path.mnt;
  8798. + dentry = file->f_path.dentry;
  8799. + file->f_version = d_inode(dentry)->i_version;
  8800. + bindex = au_dbtop(dentry);
  8801. + au_set_fbtop(file, bindex);
  8802. + btail = au_dbtaildir(dentry);
  8803. + au_set_fbbot_dir(file, btail);
  8804. + for (; !err && bindex <= btail; bindex++) {
  8805. + h_dentry = au_h_dptr(dentry, bindex);
  8806. + if (!h_dentry)
  8807. + continue;
  8808. +
  8809. + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
  8810. + if (unlikely(err))
  8811. + break;
  8812. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8813. + if (IS_ERR(h_file)) {
  8814. + err = PTR_ERR(h_file);
  8815. + break;
  8816. + }
  8817. + au_set_h_fptr(file, bindex, h_file);
  8818. + }
  8819. + au_update_figen(file);
  8820. + /* todo: necessary? */
  8821. + /* file->f_ra = h_file->f_ra; */
  8822. + if (!err)
  8823. + return 0; /* success */
  8824. +
  8825. + /* close all */
  8826. + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
  8827. + au_set_h_fptr(file, bindex, NULL);
  8828. + au_set_fbtop(file, -1);
  8829. + au_set_fbbot_dir(file, -1);
  8830. +
  8831. + return err;
  8832. +}
  8833. +
  8834. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  8835. + struct file *file)
  8836. +{
  8837. + int err;
  8838. + struct super_block *sb;
  8839. + struct au_fidir *fidir;
  8840. +
  8841. + err = -ENOMEM;
  8842. + sb = file->f_path.dentry->d_sb;
  8843. + si_read_lock(sb, AuLock_FLUSH);
  8844. + fidir = au_fidir_alloc(sb);
  8845. + if (fidir) {
  8846. + struct au_do_open_args args = {
  8847. + .open = do_open_dir,
  8848. + .fidir = fidir
  8849. + };
  8850. + err = au_do_open(file, &args);
  8851. + if (unlikely(err))
  8852. + au_delayed_kfree(fidir);
  8853. + }
  8854. + si_read_unlock(sb);
  8855. + return err;
  8856. +}
  8857. +
  8858. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  8859. + struct file *file)
  8860. +{
  8861. + struct au_vdir *vdir_cache;
  8862. + struct au_finfo *finfo;
  8863. + struct au_fidir *fidir;
  8864. + struct au_hfile *hf;
  8865. + aufs_bindex_t bindex, bbot;
  8866. + int execed, delayed;
  8867. +
  8868. + delayed = (current->flags & PF_KTHREAD) || in_interrupt();
  8869. + finfo = au_fi(file);
  8870. + fidir = finfo->fi_hdir;
  8871. + if (fidir) {
  8872. + au_sphl_del(&finfo->fi_hlist,
  8873. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  8874. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  8875. + if (vdir_cache)
  8876. + au_vdir_free(vdir_cache, delayed);
  8877. +
  8878. + bindex = finfo->fi_btop;
  8879. + if (bindex >= 0) {
  8880. + execed = vfsub_file_execed(file);
  8881. + hf = fidir->fd_hfile + bindex;
  8882. + /*
  8883. + * calls fput() instead of filp_close(),
  8884. + * since no dnotify or lock for the lower file.
  8885. + */
  8886. + bbot = fidir->fd_bbot;
  8887. + for (; bindex <= bbot; bindex++, hf++)
  8888. + if (hf->hf_file)
  8889. + au_hfput(hf, execed);
  8890. + }
  8891. + au_delayed_kfree(fidir);
  8892. + finfo->fi_hdir = NULL;
  8893. + }
  8894. + au_finfo_fin(file, delayed);
  8895. + return 0;
  8896. +}
  8897. +
  8898. +/* ---------------------------------------------------------------------- */
  8899. +
  8900. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  8901. +{
  8902. + int err;
  8903. + aufs_bindex_t bindex, bbot;
  8904. + struct file *h_file;
  8905. +
  8906. + err = 0;
  8907. + bbot = au_fbbot_dir(file);
  8908. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  8909. + h_file = au_hf_dir(file, bindex);
  8910. + if (h_file)
  8911. + err = vfsub_flush(h_file, id);
  8912. + }
  8913. + return err;
  8914. +}
  8915. +
  8916. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  8917. +{
  8918. + return au_do_flush(file, id, au_do_flush_dir);
  8919. +}
  8920. +
  8921. +/* ---------------------------------------------------------------------- */
  8922. +
  8923. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  8924. +{
  8925. + int err;
  8926. + aufs_bindex_t bbot, bindex;
  8927. + struct inode *inode;
  8928. + struct super_block *sb;
  8929. +
  8930. + err = 0;
  8931. + sb = dentry->d_sb;
  8932. + inode = d_inode(dentry);
  8933. + IMustLock(inode);
  8934. + bbot = au_dbbot(dentry);
  8935. + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
  8936. + struct path h_path;
  8937. +
  8938. + if (au_test_ro(sb, bindex, inode))
  8939. + continue;
  8940. + h_path.dentry = au_h_dptr(dentry, bindex);
  8941. + if (!h_path.dentry)
  8942. + continue;
  8943. +
  8944. + h_path.mnt = au_sbr_mnt(sb, bindex);
  8945. + err = vfsub_fsync(NULL, &h_path, datasync);
  8946. + }
  8947. +
  8948. + return err;
  8949. +}
  8950. +
  8951. +static int au_do_fsync_dir(struct file *file, int datasync)
  8952. +{
  8953. + int err;
  8954. + aufs_bindex_t bbot, bindex;
  8955. + struct file *h_file;
  8956. + struct super_block *sb;
  8957. + struct inode *inode;
  8958. +
  8959. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  8960. + if (unlikely(err))
  8961. + goto out;
  8962. +
  8963. + inode = file_inode(file);
  8964. + sb = inode->i_sb;
  8965. + bbot = au_fbbot_dir(file);
  8966. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  8967. + h_file = au_hf_dir(file, bindex);
  8968. + if (!h_file || au_test_ro(sb, bindex, inode))
  8969. + continue;
  8970. +
  8971. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  8972. + }
  8973. +
  8974. +out:
  8975. + return err;
  8976. +}
  8977. +
  8978. +/*
  8979. + * @file may be NULL
  8980. + */
  8981. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  8982. + int datasync)
  8983. +{
  8984. + int err;
  8985. + struct dentry *dentry;
  8986. + struct inode *inode;
  8987. + struct super_block *sb;
  8988. +
  8989. + err = 0;
  8990. + dentry = file->f_path.dentry;
  8991. + inode = d_inode(dentry);
  8992. + inode_lock(inode);
  8993. + sb = dentry->d_sb;
  8994. + si_noflush_read_lock(sb);
  8995. + if (file)
  8996. + err = au_do_fsync_dir(file, datasync);
  8997. + else {
  8998. + di_write_lock_child(dentry);
  8999. + err = au_do_fsync_dir_no_file(dentry, datasync);
  9000. + }
  9001. + au_cpup_attr_timesizes(inode);
  9002. + di_write_unlock(dentry);
  9003. + if (file)
  9004. + fi_write_unlock(file);
  9005. +
  9006. + si_read_unlock(sb);
  9007. + inode_unlock(inode);
  9008. + return err;
  9009. +}
  9010. +
  9011. +/* ---------------------------------------------------------------------- */
  9012. +
  9013. +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
  9014. +{
  9015. + int err;
  9016. + struct dentry *dentry;
  9017. + struct inode *inode, *h_inode;
  9018. + struct super_block *sb;
  9019. +
  9020. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  9021. +
  9022. + dentry = file->f_path.dentry;
  9023. + inode = d_inode(dentry);
  9024. + IMustLock(inode);
  9025. +
  9026. + sb = dentry->d_sb;
  9027. + si_read_lock(sb, AuLock_FLUSH);
  9028. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  9029. + if (unlikely(err))
  9030. + goto out;
  9031. + err = au_alive_dir(dentry);
  9032. + if (!err)
  9033. + err = au_vdir_init(file);
  9034. + di_downgrade_lock(dentry, AuLock_IR);
  9035. + if (unlikely(err))
  9036. + goto out_unlock;
  9037. +
  9038. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  9039. + if (!au_test_nfsd()) {
  9040. + err = au_vdir_fill_de(file, ctx);
  9041. + fsstack_copy_attr_atime(inode, h_inode);
  9042. + } else {
  9043. + /*
  9044. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  9045. + * encode_fh() and others.
  9046. + */
  9047. + atomic_inc(&h_inode->i_count);
  9048. + di_read_unlock(dentry, AuLock_IR);
  9049. + si_read_unlock(sb);
  9050. + err = au_vdir_fill_de(file, ctx);
  9051. + fsstack_copy_attr_atime(inode, h_inode);
  9052. + fi_write_unlock(file);
  9053. + iput(h_inode);
  9054. +
  9055. + AuTraceErr(err);
  9056. + return err;
  9057. + }
  9058. +
  9059. +out_unlock:
  9060. + di_read_unlock(dentry, AuLock_IR);
  9061. + fi_write_unlock(file);
  9062. +out:
  9063. + si_read_unlock(sb);
  9064. + return err;
  9065. +}
  9066. +
  9067. +/* ---------------------------------------------------------------------- */
  9068. +
  9069. +#define AuTestEmpty_WHONLY 1
  9070. +#define AuTestEmpty_CALLED (1 << 1)
  9071. +#define AuTestEmpty_SHWH (1 << 2)
  9072. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  9073. +#define au_fset_testempty(flags, name) \
  9074. + do { (flags) |= AuTestEmpty_##name; } while (0)
  9075. +#define au_fclr_testempty(flags, name) \
  9076. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  9077. +
  9078. +#ifndef CONFIG_AUFS_SHWH
  9079. +#undef AuTestEmpty_SHWH
  9080. +#define AuTestEmpty_SHWH 0
  9081. +#endif
  9082. +
  9083. +struct test_empty_arg {
  9084. + struct dir_context ctx;
  9085. + struct au_nhash *whlist;
  9086. + unsigned int flags;
  9087. + int err;
  9088. + aufs_bindex_t bindex;
  9089. +};
  9090. +
  9091. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  9092. + int namelen, loff_t offset __maybe_unused, u64 ino,
  9093. + unsigned int d_type)
  9094. +{
  9095. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  9096. + ctx);
  9097. + char *name = (void *)__name;
  9098. +
  9099. + arg->err = 0;
  9100. + au_fset_testempty(arg->flags, CALLED);
  9101. + /* smp_mb(); */
  9102. + if (name[0] == '.'
  9103. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  9104. + goto out; /* success */
  9105. +
  9106. + if (namelen <= AUFS_WH_PFX_LEN
  9107. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  9108. + if (au_ftest_testempty(arg->flags, WHONLY)
  9109. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  9110. + arg->err = -ENOTEMPTY;
  9111. + goto out;
  9112. + }
  9113. +
  9114. + name += AUFS_WH_PFX_LEN;
  9115. + namelen -= AUFS_WH_PFX_LEN;
  9116. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  9117. + arg->err = au_nhash_append_wh
  9118. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  9119. + au_ftest_testempty(arg->flags, SHWH));
  9120. +
  9121. +out:
  9122. + /* smp_mb(); */
  9123. + AuTraceErr(arg->err);
  9124. + return arg->err;
  9125. +}
  9126. +
  9127. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9128. +{
  9129. + int err;
  9130. + struct file *h_file;
  9131. +
  9132. + h_file = au_h_open(dentry, arg->bindex,
  9133. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9134. + /*file*/NULL, /*force_wr*/0);
  9135. + err = PTR_ERR(h_file);
  9136. + if (IS_ERR(h_file))
  9137. + goto out;
  9138. +
  9139. + err = 0;
  9140. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9141. + && !file_inode(h_file)->i_nlink)
  9142. + goto out_put;
  9143. +
  9144. + do {
  9145. + arg->err = 0;
  9146. + au_fclr_testempty(arg->flags, CALLED);
  9147. + /* smp_mb(); */
  9148. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9149. + if (err >= 0)
  9150. + err = arg->err;
  9151. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9152. +
  9153. +out_put:
  9154. + fput(h_file);
  9155. + au_sbr_put(dentry->d_sb, arg->bindex);
  9156. +out:
  9157. + return err;
  9158. +}
  9159. +
  9160. +struct do_test_empty_args {
  9161. + int *errp;
  9162. + struct dentry *dentry;
  9163. + struct test_empty_arg *arg;
  9164. +};
  9165. +
  9166. +static void call_do_test_empty(void *args)
  9167. +{
  9168. + struct do_test_empty_args *a = args;
  9169. + *a->errp = do_test_empty(a->dentry, a->arg);
  9170. +}
  9171. +
  9172. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9173. +{
  9174. + int err, wkq_err;
  9175. + struct dentry *h_dentry;
  9176. + struct inode *h_inode;
  9177. +
  9178. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9179. + h_inode = d_inode(h_dentry);
  9180. + /* todo: i_mode changes anytime? */
  9181. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  9182. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  9183. + inode_unlock(h_inode);
  9184. + if (!err)
  9185. + err = do_test_empty(dentry, arg);
  9186. + else {
  9187. + struct do_test_empty_args args = {
  9188. + .errp = &err,
  9189. + .dentry = dentry,
  9190. + .arg = arg
  9191. + };
  9192. + unsigned int flags = arg->flags;
  9193. +
  9194. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9195. + if (unlikely(wkq_err))
  9196. + err = wkq_err;
  9197. + arg->flags = flags;
  9198. + }
  9199. +
  9200. + return err;
  9201. +}
  9202. +
  9203. +int au_test_empty_lower(struct dentry *dentry)
  9204. +{
  9205. + int err;
  9206. + unsigned int rdhash;
  9207. + aufs_bindex_t bindex, btop, btail;
  9208. + struct au_nhash whlist;
  9209. + struct test_empty_arg arg = {
  9210. + .ctx = {
  9211. + .actor = test_empty_cb
  9212. + }
  9213. + };
  9214. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9215. +
  9216. + SiMustAnyLock(dentry->d_sb);
  9217. +
  9218. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9219. + if (!rdhash)
  9220. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9221. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9222. + if (unlikely(err))
  9223. + goto out;
  9224. +
  9225. + arg.flags = 0;
  9226. + arg.whlist = &whlist;
  9227. + btop = au_dbtop(dentry);
  9228. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9229. + au_fset_testempty(arg.flags, SHWH);
  9230. + test_empty = do_test_empty;
  9231. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9232. + test_empty = sio_test_empty;
  9233. + arg.bindex = btop;
  9234. + err = test_empty(dentry, &arg);
  9235. + if (unlikely(err))
  9236. + goto out_whlist;
  9237. +
  9238. + au_fset_testempty(arg.flags, WHONLY);
  9239. + btail = au_dbtaildir(dentry);
  9240. + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
  9241. + struct dentry *h_dentry;
  9242. +
  9243. + h_dentry = au_h_dptr(dentry, bindex);
  9244. + if (h_dentry && d_is_positive(h_dentry)) {
  9245. + arg.bindex = bindex;
  9246. + err = test_empty(dentry, &arg);
  9247. + }
  9248. + }
  9249. +
  9250. +out_whlist:
  9251. + au_nhash_wh_free(&whlist);
  9252. +out:
  9253. + return err;
  9254. +}
  9255. +
  9256. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9257. +{
  9258. + int err;
  9259. + struct test_empty_arg arg = {
  9260. + .ctx = {
  9261. + .actor = test_empty_cb
  9262. + }
  9263. + };
  9264. + aufs_bindex_t bindex, btail;
  9265. +
  9266. + err = 0;
  9267. + arg.whlist = whlist;
  9268. + arg.flags = AuTestEmpty_WHONLY;
  9269. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9270. + au_fset_testempty(arg.flags, SHWH);
  9271. + btail = au_dbtaildir(dentry);
  9272. + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
  9273. + struct dentry *h_dentry;
  9274. +
  9275. + h_dentry = au_h_dptr(dentry, bindex);
  9276. + if (h_dentry && d_is_positive(h_dentry)) {
  9277. + arg.bindex = bindex;
  9278. + err = sio_test_empty(dentry, &arg);
  9279. + }
  9280. + }
  9281. +
  9282. + return err;
  9283. +}
  9284. +
  9285. +/* ---------------------------------------------------------------------- */
  9286. +
  9287. +const struct file_operations aufs_dir_fop = {
  9288. + .owner = THIS_MODULE,
  9289. + .llseek = default_llseek,
  9290. + .read = generic_read_dir,
  9291. + .iterate_shared = aufs_iterate_shared,
  9292. + .unlocked_ioctl = aufs_ioctl_dir,
  9293. +#ifdef CONFIG_COMPAT
  9294. + .compat_ioctl = aufs_compat_ioctl_dir,
  9295. +#endif
  9296. + .open = aufs_open_dir,
  9297. + .release = aufs_release_dir,
  9298. + .flush = aufs_flush_dir,
  9299. + .fsync = aufs_fsync_dir
  9300. +};
  9301. diff -Naur null/fs/aufs/dir.h linux-4.7/fs/aufs/dir.h
  9302. --- /dev/null
  9303. +++ linux-4.7/fs/aufs/dir.h 2016-08-08 12:57:44.599384741 -0400
  9304. @@ -0,0 +1,137 @@
  9305. +/*
  9306. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  9307. + *
  9308. + * This program, aufs is free software; you can redistribute it and/or modify
  9309. + * it under the terms of the GNU General Public License as published by
  9310. + * the Free Software Foundation; either version 2 of the License, or
  9311. + * (at your option) any later version.
  9312. + *
  9313. + * This program is distributed in the hope that it will be useful,
  9314. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9315. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9316. + * GNU General Public License for more details.
  9317. + *
  9318. + * You should have received a copy of the GNU General Public License
  9319. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9320. + */
  9321. +
  9322. +/*
  9323. + * directory operations
  9324. + */
  9325. +
  9326. +#ifndef __AUFS_DIR_H__
  9327. +#define __AUFS_DIR_H__
  9328. +
  9329. +#ifdef __KERNEL__
  9330. +
  9331. +#include <linux/fs.h>
  9332. +
  9333. +/* ---------------------------------------------------------------------- */
  9334. +
  9335. +/* need to be faster and smaller */
  9336. +
  9337. +struct au_nhash {
  9338. + unsigned int nh_num;
  9339. + struct hlist_head *nh_head;
  9340. +};
  9341. +
  9342. +struct au_vdir_destr {
  9343. + unsigned char len;
  9344. + unsigned char name[0];
  9345. +} __packed;
  9346. +
  9347. +struct au_vdir_dehstr {
  9348. + struct hlist_node hash;
  9349. + union {
  9350. + struct au_vdir_destr *str;
  9351. + struct llist_node lnode; /* delayed free */
  9352. + };
  9353. +} ____cacheline_aligned_in_smp;
  9354. +
  9355. +struct au_vdir_de {
  9356. + ino_t de_ino;
  9357. + unsigned char de_type;
  9358. + /* caution: packed */
  9359. + struct au_vdir_destr de_str;
  9360. +} __packed;
  9361. +
  9362. +struct au_vdir_wh {
  9363. + struct hlist_node wh_hash;
  9364. +#ifdef CONFIG_AUFS_SHWH
  9365. + ino_t wh_ino;
  9366. + aufs_bindex_t wh_bindex;
  9367. + unsigned char wh_type;
  9368. +#else
  9369. + aufs_bindex_t wh_bindex;
  9370. +#endif
  9371. + /* caution: packed */
  9372. + struct au_vdir_destr wh_str;
  9373. +} __packed;
  9374. +
  9375. +union au_vdir_deblk_p {
  9376. + unsigned char *deblk;
  9377. + struct au_vdir_de *de;
  9378. +};
  9379. +
  9380. +struct au_vdir {
  9381. + unsigned char **vd_deblk;
  9382. + unsigned long vd_nblk;
  9383. + struct {
  9384. + unsigned long ul;
  9385. + union au_vdir_deblk_p p;
  9386. + } vd_last;
  9387. +
  9388. + unsigned long vd_version;
  9389. + unsigned int vd_deblk_sz;
  9390. + union {
  9391. + unsigned long vd_jiffy;
  9392. + struct llist_node vd_lnode; /* delayed free */
  9393. + };
  9394. +} ____cacheline_aligned_in_smp;
  9395. +
  9396. +/* ---------------------------------------------------------------------- */
  9397. +
  9398. +/* dir.c */
  9399. +extern const struct file_operations aufs_dir_fop;
  9400. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9401. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9402. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9403. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9404. +int au_test_empty_lower(struct dentry *dentry);
  9405. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9406. +
  9407. +/* vdir.c */
  9408. +unsigned int au_rdhash_est(loff_t sz);
  9409. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9410. +void au_nhash_wh_free(struct au_nhash *whlist);
  9411. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9412. + int limit);
  9413. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9414. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9415. + unsigned int d_type, aufs_bindex_t bindex,
  9416. + unsigned char shwh);
  9417. +void au_vdir_free(struct au_vdir *vdir, int atonce);
  9418. +int au_vdir_init(struct file *file);
  9419. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9420. +
  9421. +/* ioctl.c */
  9422. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9423. +
  9424. +#ifdef CONFIG_AUFS_RDU
  9425. +/* rdu.c */
  9426. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9427. +#ifdef CONFIG_COMPAT
  9428. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9429. + unsigned long arg);
  9430. +#endif
  9431. +#else
  9432. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9433. + unsigned int cmd, unsigned long arg)
  9434. +#ifdef CONFIG_COMPAT
  9435. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9436. + unsigned int cmd, unsigned long arg)
  9437. +#endif
  9438. +#endif
  9439. +
  9440. +#endif /* __KERNEL__ */
  9441. +#endif /* __AUFS_DIR_H__ */
  9442. diff -Naur null/fs/aufs/dynop.c linux-4.7/fs/aufs/dynop.c
  9443. --- /dev/null
  9444. +++ linux-4.7/fs/aufs/dynop.c 2016-08-08 12:57:44.600384719 -0400
  9445. @@ -0,0 +1,369 @@
  9446. +/*
  9447. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  9448. + *
  9449. + * This program, aufs is free software; you can redistribute it and/or modify
  9450. + * it under the terms of the GNU General Public License as published by
  9451. + * the Free Software Foundation; either version 2 of the License, or
  9452. + * (at your option) any later version.
  9453. + *
  9454. + * This program is distributed in the hope that it will be useful,
  9455. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9456. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9457. + * GNU General Public License for more details.
  9458. + *
  9459. + * You should have received a copy of the GNU General Public License
  9460. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9461. + */
  9462. +
  9463. +/*
  9464. + * dynamically customizable operations for regular files
  9465. + */
  9466. +
  9467. +#include "aufs.h"
  9468. +
  9469. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  9470. +
  9471. +/*
  9472. + * How large will these lists be?
  9473. + * Usually just a few elements, 20-30 at most for each, I guess.
  9474. + */
  9475. +static struct au_sphlhead dynop[AuDyLast];
  9476. +
  9477. +static struct au_dykey *dy_gfind_get(struct au_sphlhead *sphl, const void *h_op)
  9478. +{
  9479. + struct au_dykey *key, *tmp;
  9480. + struct hlist_head *head;
  9481. +
  9482. + key = NULL;
  9483. + head = &sphl->head;
  9484. + rcu_read_lock();
  9485. + hlist_for_each_entry_rcu(tmp, head, dk_hnode)
  9486. + if (tmp->dk_op.dy_hop == h_op) {
  9487. + key = tmp;
  9488. + kref_get(&key->dk_kref);
  9489. + break;
  9490. + }
  9491. + rcu_read_unlock();
  9492. +
  9493. + return key;
  9494. +}
  9495. +
  9496. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  9497. +{
  9498. + struct au_dykey **k, *found;
  9499. + const void *h_op = key->dk_op.dy_hop;
  9500. + int i;
  9501. +
  9502. + found = NULL;
  9503. + k = br->br_dykey;
  9504. + for (i = 0; i < AuBrDynOp; i++)
  9505. + if (k[i]) {
  9506. + if (k[i]->dk_op.dy_hop == h_op) {
  9507. + found = k[i];
  9508. + break;
  9509. + }
  9510. + } else
  9511. + break;
  9512. + if (!found) {
  9513. + spin_lock(&br->br_dykey_lock);
  9514. + for (; i < AuBrDynOp; i++)
  9515. + if (k[i]) {
  9516. + if (k[i]->dk_op.dy_hop == h_op) {
  9517. + found = k[i];
  9518. + break;
  9519. + }
  9520. + } else {
  9521. + k[i] = key;
  9522. + break;
  9523. + }
  9524. + spin_unlock(&br->br_dykey_lock);
  9525. + BUG_ON(i == AuBrDynOp); /* expand the array */
  9526. + }
  9527. +
  9528. + return found;
  9529. +}
  9530. +
  9531. +/* kref_get() if @key is already added */
  9532. +static struct au_dykey *dy_gadd(struct au_sphlhead *sphl, struct au_dykey *key)
  9533. +{
  9534. + struct au_dykey *tmp, *found;
  9535. + struct hlist_head *head;
  9536. + const void *h_op = key->dk_op.dy_hop;
  9537. +
  9538. + found = NULL;
  9539. + head = &sphl->head;
  9540. + spin_lock(&sphl->spin);
  9541. + hlist_for_each_entry(tmp, head, dk_hnode)
  9542. + if (tmp->dk_op.dy_hop == h_op) {
  9543. + kref_get(&tmp->dk_kref);
  9544. + found = tmp;
  9545. + break;
  9546. + }
  9547. + if (!found)
  9548. + hlist_add_head_rcu(&key->dk_hnode, head);
  9549. + spin_unlock(&sphl->spin);
  9550. +
  9551. + if (!found)
  9552. + DyPrSym(key);
  9553. + return found;
  9554. +}
  9555. +
  9556. +static void dy_free_rcu(struct rcu_head *rcu)
  9557. +{
  9558. + struct au_dykey *key;
  9559. +
  9560. + key = container_of(rcu, struct au_dykey, dk_rcu);
  9561. + DyPrSym(key);
  9562. + kfree(key); /* not delayed */
  9563. +}
  9564. +
  9565. +static void dy_free(struct kref *kref)
  9566. +{
  9567. + struct au_dykey *key;
  9568. + struct au_sphlhead *sphl;
  9569. +
  9570. + key = container_of(kref, struct au_dykey, dk_kref);
  9571. + sphl = dynop + key->dk_op.dy_type;
  9572. + au_sphl_del_rcu(&key->dk_hnode, sphl);
  9573. + call_rcu(&key->dk_rcu, dy_free_rcu);
  9574. +}
  9575. +
  9576. +void au_dy_put(struct au_dykey *key)
  9577. +{
  9578. + kref_put(&key->dk_kref, dy_free);
  9579. +}
  9580. +
  9581. +/* ---------------------------------------------------------------------- */
  9582. +
  9583. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  9584. +
  9585. +#ifdef CONFIG_AUFS_DEBUG
  9586. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  9587. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  9588. +#else
  9589. +#define DyDbgDeclare(cnt) do {} while (0)
  9590. +#define DyDbgInc(cnt) do {} while (0)
  9591. +#endif
  9592. +
  9593. +#define DySet(func, dst, src, h_op, h_sb) do { \
  9594. + DyDbgInc(cnt); \
  9595. + if (h_op->func) { \
  9596. + if (src.func) \
  9597. + dst.func = src.func; \
  9598. + else \
  9599. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  9600. + } \
  9601. +} while (0)
  9602. +
  9603. +#define DySetForce(func, dst, src) do { \
  9604. + AuDebugOn(!src.func); \
  9605. + DyDbgInc(cnt); \
  9606. + dst.func = src.func; \
  9607. +} while (0)
  9608. +
  9609. +#define DySetAop(func) \
  9610. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  9611. +#define DySetAopForce(func) \
  9612. + DySetForce(func, dyaop->da_op, aufs_aop)
  9613. +
  9614. +static void dy_aop(struct au_dykey *key, const void *h_op,
  9615. + struct super_block *h_sb __maybe_unused)
  9616. +{
  9617. + struct au_dyaop *dyaop = (void *)key;
  9618. + const struct address_space_operations *h_aop = h_op;
  9619. + DyDbgDeclare(cnt);
  9620. +
  9621. + AuDbg("%s\n", au_sbtype(h_sb));
  9622. +
  9623. + DySetAop(writepage);
  9624. + DySetAopForce(readpage); /* force */
  9625. + DySetAop(writepages);
  9626. + DySetAop(set_page_dirty);
  9627. + DySetAop(readpages);
  9628. + DySetAop(write_begin);
  9629. + DySetAop(write_end);
  9630. + DySetAop(bmap);
  9631. + DySetAop(invalidatepage);
  9632. + DySetAop(releasepage);
  9633. + DySetAop(freepage);
  9634. + /* this one will be changed according to an aufs mount option */
  9635. + DySetAop(direct_IO);
  9636. + DySetAop(migratepage);
  9637. + DySetAop(launder_page);
  9638. + DySetAop(is_partially_uptodate);
  9639. + DySetAop(is_dirty_writeback);
  9640. + DySetAop(error_remove_page);
  9641. + DySetAop(swap_activate);
  9642. + DySetAop(swap_deactivate);
  9643. +
  9644. + DyDbgSize(cnt, *h_aop);
  9645. +}
  9646. +
  9647. +/* ---------------------------------------------------------------------- */
  9648. +
  9649. +static void dy_bug(struct kref *kref)
  9650. +{
  9651. + BUG();
  9652. +}
  9653. +
  9654. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  9655. +{
  9656. + struct au_dykey *key, *old;
  9657. + struct au_sphlhead *sphl;
  9658. + struct op {
  9659. + unsigned int sz;
  9660. + void (*set)(struct au_dykey *key, const void *h_op,
  9661. + struct super_block *h_sb __maybe_unused);
  9662. + };
  9663. + static const struct op a[] = {
  9664. + [AuDy_AOP] = {
  9665. + .sz = sizeof(struct au_dyaop),
  9666. + .set = dy_aop
  9667. + }
  9668. + };
  9669. + const struct op *p;
  9670. +
  9671. + sphl = dynop + op->dy_type;
  9672. + key = dy_gfind_get(sphl, op->dy_hop);
  9673. + if (key)
  9674. + goto out_add; /* success */
  9675. +
  9676. + p = a + op->dy_type;
  9677. + key = kzalloc(p->sz, GFP_NOFS);
  9678. + if (unlikely(!key)) {
  9679. + key = ERR_PTR(-ENOMEM);
  9680. + goto out;
  9681. + }
  9682. +
  9683. + key->dk_op.dy_hop = op->dy_hop;
  9684. + kref_init(&key->dk_kref);
  9685. + p->set(key, op->dy_hop, au_br_sb(br));
  9686. + old = dy_gadd(sphl, key);
  9687. + if (old) {
  9688. + au_delayed_kfree(key);
  9689. + key = old;
  9690. + }
  9691. +
  9692. +out_add:
  9693. + old = dy_bradd(br, key);
  9694. + if (old)
  9695. + /* its ref-count should never be zero here */
  9696. + kref_put(&key->dk_kref, dy_bug);
  9697. +out:
  9698. + return key;
  9699. +}
  9700. +
  9701. +/* ---------------------------------------------------------------------- */
  9702. +/*
  9703. + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
  9704. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  9705. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  9706. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  9707. + * See the aufs manual in detail.
  9708. + */
  9709. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  9710. +{
  9711. + if (!do_dx)
  9712. + dyaop->da_op.direct_IO = NULL;
  9713. + else
  9714. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  9715. +}
  9716. +
  9717. +static struct au_dyaop *dy_aget(struct au_branch *br,
  9718. + const struct address_space_operations *h_aop,
  9719. + int do_dx)
  9720. +{
  9721. + struct au_dyaop *dyaop;
  9722. + struct au_dynop op;
  9723. +
  9724. + op.dy_type = AuDy_AOP;
  9725. + op.dy_haop = h_aop;
  9726. + dyaop = (void *)dy_get(&op, br);
  9727. + if (IS_ERR(dyaop))
  9728. + goto out;
  9729. + dy_adx(dyaop, do_dx);
  9730. +
  9731. +out:
  9732. + return dyaop;
  9733. +}
  9734. +
  9735. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9736. + struct inode *h_inode)
  9737. +{
  9738. + int err, do_dx;
  9739. + struct super_block *sb;
  9740. + struct au_branch *br;
  9741. + struct au_dyaop *dyaop;
  9742. +
  9743. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  9744. + IiMustWriteLock(inode);
  9745. +
  9746. + sb = inode->i_sb;
  9747. + br = au_sbr(sb, bindex);
  9748. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  9749. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  9750. + err = PTR_ERR(dyaop);
  9751. + if (IS_ERR(dyaop))
  9752. + /* unnecessary to call dy_fput() */
  9753. + goto out;
  9754. +
  9755. + err = 0;
  9756. + inode->i_mapping->a_ops = &dyaop->da_op;
  9757. +
  9758. +out:
  9759. + return err;
  9760. +}
  9761. +
  9762. +/*
  9763. + * Is it safe to replace a_ops during the inode/file is in operation?
  9764. + * Yes, I hope so.
  9765. + */
  9766. +int au_dy_irefresh(struct inode *inode)
  9767. +{
  9768. + int err;
  9769. + aufs_bindex_t btop;
  9770. + struct inode *h_inode;
  9771. +
  9772. + err = 0;
  9773. + if (S_ISREG(inode->i_mode)) {
  9774. + btop = au_ibtop(inode);
  9775. + h_inode = au_h_iptr(inode, btop);
  9776. + err = au_dy_iaop(inode, btop, h_inode);
  9777. + }
  9778. + return err;
  9779. +}
  9780. +
  9781. +void au_dy_arefresh(int do_dx)
  9782. +{
  9783. + struct au_sphlhead *sphl;
  9784. + struct hlist_head *head;
  9785. + struct au_dykey *key;
  9786. +
  9787. + sphl = dynop + AuDy_AOP;
  9788. + head = &sphl->head;
  9789. + spin_lock(&sphl->spin);
  9790. + hlist_for_each_entry(key, head, dk_hnode)
  9791. + dy_adx((void *)key, do_dx);
  9792. + spin_unlock(&sphl->spin);
  9793. +}
  9794. +
  9795. +/* ---------------------------------------------------------------------- */
  9796. +
  9797. +void __init au_dy_init(void)
  9798. +{
  9799. + int i;
  9800. +
  9801. + /* make sure that 'struct au_dykey *' can be any type */
  9802. + BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
  9803. +
  9804. + for (i = 0; i < AuDyLast; i++)
  9805. + au_sphl_init(dynop + i);
  9806. +}
  9807. +
  9808. +void au_dy_fin(void)
  9809. +{
  9810. + int i;
  9811. +
  9812. + for (i = 0; i < AuDyLast; i++)
  9813. + WARN_ON(!hlist_empty(&dynop[i].head));
  9814. +}
  9815. diff -Naur null/fs/aufs/dynop.h linux-4.7/fs/aufs/dynop.h
  9816. --- /dev/null
  9817. +++ linux-4.7/fs/aufs/dynop.h 2016-08-08 12:57:44.600384719 -0400
  9818. @@ -0,0 +1,74 @@
  9819. +/*
  9820. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  9821. + *
  9822. + * This program, aufs is free software; you can redistribute it and/or modify
  9823. + * it under the terms of the GNU General Public License as published by
  9824. + * the Free Software Foundation; either version 2 of the License, or
  9825. + * (at your option) any later version.
  9826. + *
  9827. + * This program is distributed in the hope that it will be useful,
  9828. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9829. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9830. + * GNU General Public License for more details.
  9831. + *
  9832. + * You should have received a copy of the GNU General Public License
  9833. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9834. + */
  9835. +
  9836. +/*
  9837. + * dynamically customizable operations (for regular files only)
  9838. + */
  9839. +
  9840. +#ifndef __AUFS_DYNOP_H__
  9841. +#define __AUFS_DYNOP_H__
  9842. +
  9843. +#ifdef __KERNEL__
  9844. +
  9845. +#include <linux/fs.h>
  9846. +#include <linux/kref.h>
  9847. +
  9848. +enum {AuDy_AOP, AuDyLast};
  9849. +
  9850. +struct au_dynop {
  9851. + int dy_type;
  9852. + union {
  9853. + const void *dy_hop;
  9854. + const struct address_space_operations *dy_haop;
  9855. + };
  9856. +};
  9857. +
  9858. +struct au_dykey {
  9859. + union {
  9860. + struct hlist_node dk_hnode;
  9861. + struct rcu_head dk_rcu;
  9862. + };
  9863. + struct au_dynop dk_op;
  9864. +
  9865. + /*
  9866. + * during I am in the branch local array, kref is gotten. when the
  9867. + * branch is removed, kref is put.
  9868. + */
  9869. + struct kref dk_kref;
  9870. +};
  9871. +
  9872. +/* stop unioning since their sizes are very different from each other */
  9873. +struct au_dyaop {
  9874. + struct au_dykey da_key;
  9875. + struct address_space_operations da_op; /* not const */
  9876. +};
  9877. +
  9878. +/* ---------------------------------------------------------------------- */
  9879. +
  9880. +/* dynop.c */
  9881. +struct au_branch;
  9882. +void au_dy_put(struct au_dykey *key);
  9883. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9884. + struct inode *h_inode);
  9885. +int au_dy_irefresh(struct inode *inode);
  9886. +void au_dy_arefresh(int do_dio);
  9887. +
  9888. +void __init au_dy_init(void);
  9889. +void au_dy_fin(void);
  9890. +
  9891. +#endif /* __KERNEL__ */
  9892. +#endif /* __AUFS_DYNOP_H__ */
  9893. diff -Naur null/fs/aufs/export.c linux-4.7/fs/aufs/export.c
  9894. --- /dev/null
  9895. +++ linux-4.7/fs/aufs/export.c 2016-08-08 12:57:44.600384719 -0400
  9896. @@ -0,0 +1,837 @@
  9897. +/*
  9898. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  9899. + *
  9900. + * This program, aufs is free software; you can redistribute it and/or modify
  9901. + * it under the terms of the GNU General Public License as published by
  9902. + * the Free Software Foundation; either version 2 of the License, or
  9903. + * (at your option) any later version.
  9904. + *
  9905. + * This program is distributed in the hope that it will be useful,
  9906. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9907. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9908. + * GNU General Public License for more details.
  9909. + *
  9910. + * You should have received a copy of the GNU General Public License
  9911. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9912. + */
  9913. +
  9914. +/*
  9915. + * export via nfs
  9916. + */
  9917. +
  9918. +#include <linux/exportfs.h>
  9919. +#include <linux/fs_struct.h>
  9920. +#include <linux/namei.h>
  9921. +#include <linux/nsproxy.h>
  9922. +#include <linux/random.h>
  9923. +#include <linux/writeback.h>
  9924. +#include "../fs/mount.h"
  9925. +#include "aufs.h"
  9926. +
  9927. +union conv {
  9928. +#ifdef CONFIG_AUFS_INO_T_64
  9929. + __u32 a[2];
  9930. +#else
  9931. + __u32 a[1];
  9932. +#endif
  9933. + ino_t ino;
  9934. +};
  9935. +
  9936. +static ino_t decode_ino(__u32 *a)
  9937. +{
  9938. + union conv u;
  9939. +
  9940. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  9941. + u.a[0] = a[0];
  9942. +#ifdef CONFIG_AUFS_INO_T_64
  9943. + u.a[1] = a[1];
  9944. +#endif
  9945. + return u.ino;
  9946. +}
  9947. +
  9948. +static void encode_ino(__u32 *a, ino_t ino)
  9949. +{
  9950. + union conv u;
  9951. +
  9952. + u.ino = ino;
  9953. + a[0] = u.a[0];
  9954. +#ifdef CONFIG_AUFS_INO_T_64
  9955. + a[1] = u.a[1];
  9956. +#endif
  9957. +}
  9958. +
  9959. +/* NFS file handle */
  9960. +enum {
  9961. + Fh_br_id,
  9962. + Fh_sigen,
  9963. +#ifdef CONFIG_AUFS_INO_T_64
  9964. + /* support 64bit inode number */
  9965. + Fh_ino1,
  9966. + Fh_ino2,
  9967. + Fh_dir_ino1,
  9968. + Fh_dir_ino2,
  9969. +#else
  9970. + Fh_ino1,
  9971. + Fh_dir_ino1,
  9972. +#endif
  9973. + Fh_igen,
  9974. + Fh_h_type,
  9975. + Fh_tail,
  9976. +
  9977. + Fh_ino = Fh_ino1,
  9978. + Fh_dir_ino = Fh_dir_ino1
  9979. +};
  9980. +
  9981. +static int au_test_anon(struct dentry *dentry)
  9982. +{
  9983. + /* note: read d_flags without d_lock */
  9984. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  9985. +}
  9986. +
  9987. +int au_test_nfsd(void)
  9988. +{
  9989. + int ret;
  9990. + struct task_struct *tsk = current;
  9991. + char comm[sizeof(tsk->comm)];
  9992. +
  9993. + ret = 0;
  9994. + if (tsk->flags & PF_KTHREAD) {
  9995. + get_task_comm(comm, tsk);
  9996. + ret = !strcmp(comm, "nfsd");
  9997. + }
  9998. +
  9999. + return ret;
  10000. +}
  10001. +
  10002. +/* ---------------------------------------------------------------------- */
  10003. +/* inode generation external table */
  10004. +
  10005. +void au_xigen_inc(struct inode *inode)
  10006. +{
  10007. + loff_t pos;
  10008. + ssize_t sz;
  10009. + __u32 igen;
  10010. + struct super_block *sb;
  10011. + struct au_sbinfo *sbinfo;
  10012. +
  10013. + sb = inode->i_sb;
  10014. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  10015. +
  10016. + sbinfo = au_sbi(sb);
  10017. + pos = inode->i_ino;
  10018. + pos *= sizeof(igen);
  10019. + igen = inode->i_generation + 1;
  10020. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  10021. + sizeof(igen), &pos);
  10022. + if (sz == sizeof(igen))
  10023. + return; /* success */
  10024. +
  10025. + if (unlikely(sz >= 0))
  10026. + AuIOErr("xigen error (%zd)\n", sz);
  10027. +}
  10028. +
  10029. +int au_xigen_new(struct inode *inode)
  10030. +{
  10031. + int err;
  10032. + loff_t pos;
  10033. + ssize_t sz;
  10034. + struct super_block *sb;
  10035. + struct au_sbinfo *sbinfo;
  10036. + struct file *file;
  10037. +
  10038. + err = 0;
  10039. + /* todo: dirty, at mount time */
  10040. + if (inode->i_ino == AUFS_ROOT_INO)
  10041. + goto out;
  10042. + sb = inode->i_sb;
  10043. + SiMustAnyLock(sb);
  10044. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  10045. + goto out;
  10046. +
  10047. + err = -EFBIG;
  10048. + pos = inode->i_ino;
  10049. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  10050. + AuIOErr1("too large i%lld\n", pos);
  10051. + goto out;
  10052. + }
  10053. + pos *= sizeof(inode->i_generation);
  10054. +
  10055. + err = 0;
  10056. + sbinfo = au_sbi(sb);
  10057. + file = sbinfo->si_xigen;
  10058. + BUG_ON(!file);
  10059. +
  10060. + if (vfsub_f_size_read(file)
  10061. + < pos + sizeof(inode->i_generation)) {
  10062. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  10063. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  10064. + sizeof(inode->i_generation), &pos);
  10065. + } else
  10066. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  10067. + sizeof(inode->i_generation), &pos);
  10068. + if (sz == sizeof(inode->i_generation))
  10069. + goto out; /* success */
  10070. +
  10071. + err = sz;
  10072. + if (unlikely(sz >= 0)) {
  10073. + err = -EIO;
  10074. + AuIOErr("xigen error (%zd)\n", sz);
  10075. + }
  10076. +
  10077. +out:
  10078. + return err;
  10079. +}
  10080. +
  10081. +int au_xigen_set(struct super_block *sb, struct file *base)
  10082. +{
  10083. + int err;
  10084. + struct au_sbinfo *sbinfo;
  10085. + struct file *file;
  10086. +
  10087. + SiMustWriteLock(sb);
  10088. +
  10089. + sbinfo = au_sbi(sb);
  10090. + file = au_xino_create2(base, sbinfo->si_xigen);
  10091. + err = PTR_ERR(file);
  10092. + if (IS_ERR(file))
  10093. + goto out;
  10094. + err = 0;
  10095. + if (sbinfo->si_xigen)
  10096. + fput(sbinfo->si_xigen);
  10097. + sbinfo->si_xigen = file;
  10098. +
  10099. +out:
  10100. + return err;
  10101. +}
  10102. +
  10103. +void au_xigen_clr(struct super_block *sb)
  10104. +{
  10105. + struct au_sbinfo *sbinfo;
  10106. +
  10107. + SiMustWriteLock(sb);
  10108. +
  10109. + sbinfo = au_sbi(sb);
  10110. + if (sbinfo->si_xigen) {
  10111. + fput(sbinfo->si_xigen);
  10112. + sbinfo->si_xigen = NULL;
  10113. + }
  10114. +}
  10115. +
  10116. +/* ---------------------------------------------------------------------- */
  10117. +
  10118. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  10119. + ino_t dir_ino)
  10120. +{
  10121. + struct dentry *dentry, *d;
  10122. + struct inode *inode;
  10123. + unsigned int sigen;
  10124. +
  10125. + dentry = NULL;
  10126. + inode = ilookup(sb, ino);
  10127. + if (!inode)
  10128. + goto out;
  10129. +
  10130. + dentry = ERR_PTR(-ESTALE);
  10131. + sigen = au_sigen(sb);
  10132. + if (unlikely(au_is_bad_inode(inode)
  10133. + || IS_DEADDIR(inode)
  10134. + || sigen != au_iigen(inode, NULL)))
  10135. + goto out_iput;
  10136. +
  10137. + dentry = NULL;
  10138. + if (!dir_ino || S_ISDIR(inode->i_mode))
  10139. + dentry = d_find_alias(inode);
  10140. + else {
  10141. + spin_lock(&inode->i_lock);
  10142. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  10143. + spin_lock(&d->d_lock);
  10144. + if (!au_test_anon(d)
  10145. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  10146. + dentry = dget_dlock(d);
  10147. + spin_unlock(&d->d_lock);
  10148. + break;
  10149. + }
  10150. + spin_unlock(&d->d_lock);
  10151. + }
  10152. + spin_unlock(&inode->i_lock);
  10153. + }
  10154. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  10155. + /* need to refresh */
  10156. + dput(dentry);
  10157. + dentry = NULL;
  10158. + }
  10159. +
  10160. +out_iput:
  10161. + iput(inode);
  10162. +out:
  10163. + AuTraceErrPtr(dentry);
  10164. + return dentry;
  10165. +}
  10166. +
  10167. +/* ---------------------------------------------------------------------- */
  10168. +
  10169. +/* todo: dirty? */
  10170. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  10171. +
  10172. +struct au_compare_mnt_args {
  10173. + /* input */
  10174. + struct super_block *sb;
  10175. +
  10176. + /* output */
  10177. + struct vfsmount *mnt;
  10178. +};
  10179. +
  10180. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  10181. +{
  10182. + struct au_compare_mnt_args *a = arg;
  10183. +
  10184. + if (mnt->mnt_sb != a->sb)
  10185. + return 0;
  10186. + a->mnt = mntget(mnt);
  10187. + return 1;
  10188. +}
  10189. +
  10190. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  10191. +{
  10192. + int err;
  10193. + struct path root;
  10194. + struct au_compare_mnt_args args = {
  10195. + .sb = sb
  10196. + };
  10197. +
  10198. + get_fs_root(current->fs, &root);
  10199. + rcu_read_lock();
  10200. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  10201. + rcu_read_unlock();
  10202. + path_put(&root);
  10203. + AuDebugOn(!err);
  10204. + AuDebugOn(!args.mnt);
  10205. + return args.mnt;
  10206. +}
  10207. +
  10208. +struct au_nfsd_si_lock {
  10209. + unsigned int sigen;
  10210. + aufs_bindex_t bindex, br_id;
  10211. + unsigned char force_lock;
  10212. +};
  10213. +
  10214. +static int si_nfsd_read_lock(struct super_block *sb,
  10215. + struct au_nfsd_si_lock *nsi_lock)
  10216. +{
  10217. + int err;
  10218. + aufs_bindex_t bindex;
  10219. +
  10220. + si_read_lock(sb, AuLock_FLUSH);
  10221. +
  10222. + /* branch id may be wrapped around */
  10223. + err = 0;
  10224. + bindex = au_br_index(sb, nsi_lock->br_id);
  10225. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  10226. + goto out; /* success */
  10227. +
  10228. + err = -ESTALE;
  10229. + bindex = -1;
  10230. + if (!nsi_lock->force_lock)
  10231. + si_read_unlock(sb);
  10232. +
  10233. +out:
  10234. + nsi_lock->bindex = bindex;
  10235. + return err;
  10236. +}
  10237. +
  10238. +struct find_name_by_ino {
  10239. + struct dir_context ctx;
  10240. + int called, found;
  10241. + ino_t ino;
  10242. + char *name;
  10243. + int namelen;
  10244. +};
  10245. +
  10246. +static int
  10247. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  10248. + loff_t offset, u64 ino, unsigned int d_type)
  10249. +{
  10250. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  10251. + ctx);
  10252. +
  10253. + a->called++;
  10254. + if (a->ino != ino)
  10255. + return 0;
  10256. +
  10257. + memcpy(a->name, name, namelen);
  10258. + a->namelen = namelen;
  10259. + a->found = 1;
  10260. + return 1;
  10261. +}
  10262. +
  10263. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  10264. + struct au_nfsd_si_lock *nsi_lock)
  10265. +{
  10266. + struct dentry *dentry, *parent;
  10267. + struct file *file;
  10268. + struct inode *dir;
  10269. + struct find_name_by_ino arg = {
  10270. + .ctx = {
  10271. + .actor = find_name_by_ino
  10272. + }
  10273. + };
  10274. + int err;
  10275. +
  10276. + parent = path->dentry;
  10277. + if (nsi_lock)
  10278. + si_read_unlock(parent->d_sb);
  10279. + file = vfsub_dentry_open(path, au_dir_roflags);
  10280. + dentry = (void *)file;
  10281. + if (IS_ERR(file))
  10282. + goto out;
  10283. +
  10284. + dentry = ERR_PTR(-ENOMEM);
  10285. + arg.name = (void *)__get_free_page(GFP_NOFS);
  10286. + if (unlikely(!arg.name))
  10287. + goto out_file;
  10288. + arg.ino = ino;
  10289. + arg.found = 0;
  10290. + do {
  10291. + arg.called = 0;
  10292. + /* smp_mb(); */
  10293. + err = vfsub_iterate_dir(file, &arg.ctx);
  10294. + } while (!err && !arg.found && arg.called);
  10295. + dentry = ERR_PTR(err);
  10296. + if (unlikely(err))
  10297. + goto out_name;
  10298. + /* instead of ENOENT */
  10299. + dentry = ERR_PTR(-ESTALE);
  10300. + if (!arg.found)
  10301. + goto out_name;
  10302. +
  10303. + /* do not call vfsub_lkup_one() */
  10304. + dir = d_inode(parent);
  10305. + dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
  10306. + AuTraceErrPtr(dentry);
  10307. + if (IS_ERR(dentry))
  10308. + goto out_name;
  10309. + AuDebugOn(au_test_anon(dentry));
  10310. + if (unlikely(d_really_is_negative(dentry))) {
  10311. + dput(dentry);
  10312. + dentry = ERR_PTR(-ENOENT);
  10313. + }
  10314. +
  10315. +out_name:
  10316. + au_delayed_free_page((unsigned long)arg.name);
  10317. +out_file:
  10318. + fput(file);
  10319. +out:
  10320. + if (unlikely(nsi_lock
  10321. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  10322. + if (!IS_ERR(dentry)) {
  10323. + dput(dentry);
  10324. + dentry = ERR_PTR(-ESTALE);
  10325. + }
  10326. + AuTraceErrPtr(dentry);
  10327. + return dentry;
  10328. +}
  10329. +
  10330. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  10331. + ino_t dir_ino,
  10332. + struct au_nfsd_si_lock *nsi_lock)
  10333. +{
  10334. + struct dentry *dentry;
  10335. + struct path path;
  10336. +
  10337. + if (dir_ino != AUFS_ROOT_INO) {
  10338. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  10339. + dentry = path.dentry;
  10340. + if (!path.dentry || IS_ERR(path.dentry))
  10341. + goto out;
  10342. + AuDebugOn(au_test_anon(path.dentry));
  10343. + } else
  10344. + path.dentry = dget(sb->s_root);
  10345. +
  10346. + path.mnt = au_mnt_get(sb);
  10347. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  10348. + path_put(&path);
  10349. +
  10350. +out:
  10351. + AuTraceErrPtr(dentry);
  10352. + return dentry;
  10353. +}
  10354. +
  10355. +/* ---------------------------------------------------------------------- */
  10356. +
  10357. +static int h_acceptable(void *expv, struct dentry *dentry)
  10358. +{
  10359. + return 1;
  10360. +}
  10361. +
  10362. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  10363. + char *buf, int len, struct super_block *sb)
  10364. +{
  10365. + char *p;
  10366. + int n;
  10367. + struct path path;
  10368. +
  10369. + p = d_path(h_rootpath, buf, len);
  10370. + if (IS_ERR(p))
  10371. + goto out;
  10372. + n = strlen(p);
  10373. +
  10374. + path.mnt = h_rootpath->mnt;
  10375. + path.dentry = h_parent;
  10376. + p = d_path(&path, buf, len);
  10377. + if (IS_ERR(p))
  10378. + goto out;
  10379. + if (n != 1)
  10380. + p += n;
  10381. +
  10382. + path.mnt = au_mnt_get(sb);
  10383. + path.dentry = sb->s_root;
  10384. + p = d_path(&path, buf, len - strlen(p));
  10385. + mntput(path.mnt);
  10386. + if (IS_ERR(p))
  10387. + goto out;
  10388. + if (n != 1)
  10389. + p[strlen(p)] = '/';
  10390. +
  10391. +out:
  10392. + AuTraceErrPtr(p);
  10393. + return p;
  10394. +}
  10395. +
  10396. +static
  10397. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  10398. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  10399. +{
  10400. + struct dentry *dentry, *h_parent, *root;
  10401. + struct super_block *h_sb;
  10402. + char *pathname, *p;
  10403. + struct vfsmount *h_mnt;
  10404. + struct au_branch *br;
  10405. + int err;
  10406. + struct path path;
  10407. +
  10408. + br = au_sbr(sb, nsi_lock->bindex);
  10409. + h_mnt = au_br_mnt(br);
  10410. + h_sb = h_mnt->mnt_sb;
  10411. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  10412. + lockdep_off();
  10413. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  10414. + fh_len - Fh_tail, fh[Fh_h_type],
  10415. + h_acceptable, /*context*/NULL);
  10416. + lockdep_on();
  10417. + dentry = h_parent;
  10418. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  10419. + AuWarn1("%s decode_fh failed, %ld\n",
  10420. + au_sbtype(h_sb), PTR_ERR(h_parent));
  10421. + goto out;
  10422. + }
  10423. + dentry = NULL;
  10424. + if (unlikely(au_test_anon(h_parent))) {
  10425. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  10426. + au_sbtype(h_sb));
  10427. + goto out_h_parent;
  10428. + }
  10429. +
  10430. + dentry = ERR_PTR(-ENOMEM);
  10431. + pathname = (void *)__get_free_page(GFP_NOFS);
  10432. + if (unlikely(!pathname))
  10433. + goto out_h_parent;
  10434. +
  10435. + root = sb->s_root;
  10436. + path.mnt = h_mnt;
  10437. + di_read_lock_parent(root, !AuLock_IR);
  10438. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  10439. + di_read_unlock(root, !AuLock_IR);
  10440. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  10441. + dentry = (void *)p;
  10442. + if (IS_ERR(p))
  10443. + goto out_pathname;
  10444. +
  10445. + si_read_unlock(sb);
  10446. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  10447. + dentry = ERR_PTR(err);
  10448. + if (unlikely(err))
  10449. + goto out_relock;
  10450. +
  10451. + dentry = ERR_PTR(-ENOENT);
  10452. + AuDebugOn(au_test_anon(path.dentry));
  10453. + if (unlikely(d_really_is_negative(path.dentry)))
  10454. + goto out_path;
  10455. +
  10456. + if (ino != d_inode(path.dentry)->i_ino)
  10457. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  10458. + else
  10459. + dentry = dget(path.dentry);
  10460. +
  10461. +out_path:
  10462. + path_put(&path);
  10463. +out_relock:
  10464. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  10465. + if (!IS_ERR(dentry)) {
  10466. + dput(dentry);
  10467. + dentry = ERR_PTR(-ESTALE);
  10468. + }
  10469. +out_pathname:
  10470. + au_delayed_free_page((unsigned long)pathname);
  10471. +out_h_parent:
  10472. + dput(h_parent);
  10473. +out:
  10474. + AuTraceErrPtr(dentry);
  10475. + return dentry;
  10476. +}
  10477. +
  10478. +/* ---------------------------------------------------------------------- */
  10479. +
  10480. +static struct dentry *
  10481. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  10482. + int fh_type)
  10483. +{
  10484. + struct dentry *dentry;
  10485. + __u32 *fh = fid->raw;
  10486. + struct au_branch *br;
  10487. + ino_t ino, dir_ino;
  10488. + struct au_nfsd_si_lock nsi_lock = {
  10489. + .force_lock = 0
  10490. + };
  10491. +
  10492. + dentry = ERR_PTR(-ESTALE);
  10493. + /* it should never happen, but the file handle is unreliable */
  10494. + if (unlikely(fh_len < Fh_tail))
  10495. + goto out;
  10496. + nsi_lock.sigen = fh[Fh_sigen];
  10497. + nsi_lock.br_id = fh[Fh_br_id];
  10498. +
  10499. + /* branch id may be wrapped around */
  10500. + br = NULL;
  10501. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  10502. + goto out;
  10503. + nsi_lock.force_lock = 1;
  10504. +
  10505. + /* is this inode still cached? */
  10506. + ino = decode_ino(fh + Fh_ino);
  10507. + /* it should never happen */
  10508. + if (unlikely(ino == AUFS_ROOT_INO))
  10509. + goto out_unlock;
  10510. +
  10511. + dir_ino = decode_ino(fh + Fh_dir_ino);
  10512. + dentry = decode_by_ino(sb, ino, dir_ino);
  10513. + if (IS_ERR(dentry))
  10514. + goto out_unlock;
  10515. + if (dentry)
  10516. + goto accept;
  10517. +
  10518. + /* is the parent dir cached? */
  10519. + br = au_sbr(sb, nsi_lock.bindex);
  10520. + au_br_get(br);
  10521. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  10522. + if (IS_ERR(dentry))
  10523. + goto out_unlock;
  10524. + if (dentry)
  10525. + goto accept;
  10526. +
  10527. + /* lookup path */
  10528. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  10529. + if (IS_ERR(dentry))
  10530. + goto out_unlock;
  10531. + if (unlikely(!dentry))
  10532. + /* todo?: make it ESTALE */
  10533. + goto out_unlock;
  10534. +
  10535. +accept:
  10536. + if (!au_digen_test(dentry, au_sigen(sb))
  10537. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  10538. + goto out_unlock; /* success */
  10539. +
  10540. + dput(dentry);
  10541. + dentry = ERR_PTR(-ESTALE);
  10542. +out_unlock:
  10543. + if (br)
  10544. + au_br_put(br);
  10545. + si_read_unlock(sb);
  10546. +out:
  10547. + AuTraceErrPtr(dentry);
  10548. + return dentry;
  10549. +}
  10550. +
  10551. +#if 0 /* reserved for future use */
  10552. +/* support subtreecheck option */
  10553. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  10554. + int fh_len, int fh_type)
  10555. +{
  10556. + struct dentry *parent;
  10557. + __u32 *fh = fid->raw;
  10558. + ino_t dir_ino;
  10559. +
  10560. + dir_ino = decode_ino(fh + Fh_dir_ino);
  10561. + parent = decode_by_ino(sb, dir_ino, 0);
  10562. + if (IS_ERR(parent))
  10563. + goto out;
  10564. + if (!parent)
  10565. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  10566. + dir_ino, fh, fh_len);
  10567. +
  10568. +out:
  10569. + AuTraceErrPtr(parent);
  10570. + return parent;
  10571. +}
  10572. +#endif
  10573. +
  10574. +/* ---------------------------------------------------------------------- */
  10575. +
  10576. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  10577. + struct inode *dir)
  10578. +{
  10579. + int err;
  10580. + aufs_bindex_t bindex;
  10581. + struct super_block *sb, *h_sb;
  10582. + struct dentry *dentry, *parent, *h_parent;
  10583. + struct inode *h_dir;
  10584. + struct au_branch *br;
  10585. +
  10586. + err = -ENOSPC;
  10587. + if (unlikely(*max_len <= Fh_tail)) {
  10588. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  10589. + goto out;
  10590. + }
  10591. +
  10592. + err = FILEID_ROOT;
  10593. + if (inode->i_ino == AUFS_ROOT_INO) {
  10594. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  10595. + goto out;
  10596. + }
  10597. +
  10598. + h_parent = NULL;
  10599. + sb = inode->i_sb;
  10600. + err = si_read_lock(sb, AuLock_FLUSH);
  10601. + if (unlikely(err))
  10602. + goto out;
  10603. +
  10604. +#ifdef CONFIG_AUFS_DEBUG
  10605. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  10606. + AuWarn1("NFS-exporting requires xino\n");
  10607. +#endif
  10608. + err = -EIO;
  10609. + parent = NULL;
  10610. + ii_read_lock_child(inode);
  10611. + bindex = au_ibtop(inode);
  10612. + if (!dir) {
  10613. + dentry = d_find_any_alias(inode);
  10614. + if (unlikely(!dentry))
  10615. + goto out_unlock;
  10616. + AuDebugOn(au_test_anon(dentry));
  10617. + parent = dget_parent(dentry);
  10618. + dput(dentry);
  10619. + if (unlikely(!parent))
  10620. + goto out_unlock;
  10621. + if (d_really_is_positive(parent))
  10622. + dir = d_inode(parent);
  10623. + }
  10624. +
  10625. + ii_read_lock_parent(dir);
  10626. + h_dir = au_h_iptr(dir, bindex);
  10627. + ii_read_unlock(dir);
  10628. + if (unlikely(!h_dir))
  10629. + goto out_parent;
  10630. + h_parent = d_find_any_alias(h_dir);
  10631. + if (unlikely(!h_parent))
  10632. + goto out_hparent;
  10633. +
  10634. + err = -EPERM;
  10635. + br = au_sbr(sb, bindex);
  10636. + h_sb = au_br_sb(br);
  10637. + if (unlikely(!h_sb->s_export_op)) {
  10638. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  10639. + goto out_hparent;
  10640. + }
  10641. +
  10642. + fh[Fh_br_id] = br->br_id;
  10643. + fh[Fh_sigen] = au_sigen(sb);
  10644. + encode_ino(fh + Fh_ino, inode->i_ino);
  10645. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  10646. + fh[Fh_igen] = inode->i_generation;
  10647. +
  10648. + *max_len -= Fh_tail;
  10649. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  10650. + max_len,
  10651. + /*connectable or subtreecheck*/0);
  10652. + err = fh[Fh_h_type];
  10653. + *max_len += Fh_tail;
  10654. + /* todo: macros? */
  10655. + if (err != FILEID_INVALID)
  10656. + err = 99;
  10657. + else
  10658. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  10659. +
  10660. +out_hparent:
  10661. + dput(h_parent);
  10662. +out_parent:
  10663. + dput(parent);
  10664. +out_unlock:
  10665. + ii_read_unlock(inode);
  10666. + si_read_unlock(sb);
  10667. +out:
  10668. + if (unlikely(err < 0))
  10669. + err = FILEID_INVALID;
  10670. + return err;
  10671. +}
  10672. +
  10673. +/* ---------------------------------------------------------------------- */
  10674. +
  10675. +static int aufs_commit_metadata(struct inode *inode)
  10676. +{
  10677. + int err;
  10678. + aufs_bindex_t bindex;
  10679. + struct super_block *sb;
  10680. + struct inode *h_inode;
  10681. + int (*f)(struct inode *inode);
  10682. +
  10683. + sb = inode->i_sb;
  10684. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10685. + ii_write_lock_child(inode);
  10686. + bindex = au_ibtop(inode);
  10687. + AuDebugOn(bindex < 0);
  10688. + h_inode = au_h_iptr(inode, bindex);
  10689. +
  10690. + f = h_inode->i_sb->s_export_op->commit_metadata;
  10691. + if (f)
  10692. + err = f(h_inode);
  10693. + else {
  10694. + struct writeback_control wbc = {
  10695. + .sync_mode = WB_SYNC_ALL,
  10696. + .nr_to_write = 0 /* metadata only */
  10697. + };
  10698. +
  10699. + err = sync_inode(h_inode, &wbc);
  10700. + }
  10701. +
  10702. + au_cpup_attr_timesizes(inode);
  10703. + ii_write_unlock(inode);
  10704. + si_read_unlock(sb);
  10705. + return err;
  10706. +}
  10707. +
  10708. +/* ---------------------------------------------------------------------- */
  10709. +
  10710. +static struct export_operations aufs_export_op = {
  10711. + .fh_to_dentry = aufs_fh_to_dentry,
  10712. + /* .fh_to_parent = aufs_fh_to_parent, */
  10713. + .encode_fh = aufs_encode_fh,
  10714. + .commit_metadata = aufs_commit_metadata
  10715. +};
  10716. +
  10717. +void au_export_init(struct super_block *sb)
  10718. +{
  10719. + struct au_sbinfo *sbinfo;
  10720. + __u32 u;
  10721. +
  10722. + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
  10723. + && IS_MODULE(CONFIG_EXPORTFS),
  10724. + AUFS_NAME ": unsupported configuration "
  10725. + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
  10726. +
  10727. + sb->s_export_op = &aufs_export_op;
  10728. + sbinfo = au_sbi(sb);
  10729. + sbinfo->si_xigen = NULL;
  10730. + get_random_bytes(&u, sizeof(u));
  10731. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  10732. + atomic_set(&sbinfo->si_xigen_next, u);
  10733. +}
  10734. diff -Naur null/fs/aufs/fhsm.c linux-4.7/fs/aufs/fhsm.c
  10735. --- /dev/null
  10736. +++ linux-4.7/fs/aufs/fhsm.c 2016-08-08 12:57:44.601384697 -0400
  10737. @@ -0,0 +1,426 @@
  10738. +/*
  10739. + * Copyright (C) 2011-2016 Junjiro R. Okajima
  10740. + *
  10741. + * This program, aufs is free software; you can redistribute it and/or modify
  10742. + * it under the terms of the GNU General Public License as published by
  10743. + * the Free Software Foundation; either version 2 of the License, or
  10744. + * (at your option) any later version.
  10745. + *
  10746. + * This program is distributed in the hope that it will be useful,
  10747. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10748. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10749. + * GNU General Public License for more details.
  10750. + *
  10751. + * You should have received a copy of the GNU General Public License
  10752. + * along with this program; if not, write to the Free Software
  10753. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  10754. + */
  10755. +
  10756. +/*
  10757. + * File-based Hierarchy Storage Management
  10758. + */
  10759. +
  10760. +#include <linux/anon_inodes.h>
  10761. +#include <linux/poll.h>
  10762. +#include <linux/seq_file.h>
  10763. +#include <linux/statfs.h>
  10764. +#include "aufs.h"
  10765. +
  10766. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  10767. +{
  10768. + struct au_sbinfo *sbinfo;
  10769. + struct au_fhsm *fhsm;
  10770. +
  10771. + SiMustAnyLock(sb);
  10772. +
  10773. + sbinfo = au_sbi(sb);
  10774. + fhsm = &sbinfo->si_fhsm;
  10775. + AuDebugOn(!fhsm);
  10776. + return fhsm->fhsm_bottom;
  10777. +}
  10778. +
  10779. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  10780. +{
  10781. + struct au_sbinfo *sbinfo;
  10782. + struct au_fhsm *fhsm;
  10783. +
  10784. + SiMustWriteLock(sb);
  10785. +
  10786. + sbinfo = au_sbi(sb);
  10787. + fhsm = &sbinfo->si_fhsm;
  10788. + AuDebugOn(!fhsm);
  10789. + fhsm->fhsm_bottom = bindex;
  10790. +}
  10791. +
  10792. +/* ---------------------------------------------------------------------- */
  10793. +
  10794. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  10795. +{
  10796. + struct au_br_fhsm *bf;
  10797. +
  10798. + bf = br->br_fhsm;
  10799. + MtxMustLock(&bf->bf_lock);
  10800. +
  10801. + return !bf->bf_readable
  10802. + || time_after(jiffies,
  10803. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  10804. +}
  10805. +
  10806. +/* ---------------------------------------------------------------------- */
  10807. +
  10808. +static void au_fhsm_notify(struct super_block *sb, int val)
  10809. +{
  10810. + struct au_sbinfo *sbinfo;
  10811. + struct au_fhsm *fhsm;
  10812. +
  10813. + SiMustAnyLock(sb);
  10814. +
  10815. + sbinfo = au_sbi(sb);
  10816. + fhsm = &sbinfo->si_fhsm;
  10817. + if (au_fhsm_pid(fhsm)
  10818. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  10819. + atomic_set(&fhsm->fhsm_readable, val);
  10820. + if (val)
  10821. + wake_up(&fhsm->fhsm_wqh);
  10822. + }
  10823. +}
  10824. +
  10825. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  10826. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  10827. +{
  10828. + int err;
  10829. + struct au_branch *br;
  10830. + struct au_br_fhsm *bf;
  10831. +
  10832. + br = au_sbr(sb, bindex);
  10833. + AuDebugOn(au_br_rdonly(br));
  10834. + bf = br->br_fhsm;
  10835. + AuDebugOn(!bf);
  10836. +
  10837. + if (do_lock)
  10838. + mutex_lock(&bf->bf_lock);
  10839. + else
  10840. + MtxMustLock(&bf->bf_lock);
  10841. +
  10842. + /* sb->s_root for NFS is unreliable */
  10843. + err = au_br_stfs(br, &bf->bf_stfs);
  10844. + if (unlikely(err)) {
  10845. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  10846. + goto out;
  10847. + }
  10848. +
  10849. + bf->bf_jiffy = jiffies;
  10850. + bf->bf_readable = 1;
  10851. + if (do_notify)
  10852. + au_fhsm_notify(sb, /*val*/1);
  10853. + if (rstfs)
  10854. + *rstfs = bf->bf_stfs;
  10855. +
  10856. +out:
  10857. + if (do_lock)
  10858. + mutex_unlock(&bf->bf_lock);
  10859. + au_fhsm_notify(sb, /*val*/1);
  10860. +
  10861. + return err;
  10862. +}
  10863. +
  10864. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  10865. +{
  10866. + int err;
  10867. + struct au_sbinfo *sbinfo;
  10868. + struct au_fhsm *fhsm;
  10869. + struct au_branch *br;
  10870. + struct au_br_fhsm *bf;
  10871. +
  10872. + AuDbg("b%d, force %d\n", bindex, force);
  10873. + SiMustAnyLock(sb);
  10874. +
  10875. + sbinfo = au_sbi(sb);
  10876. + fhsm = &sbinfo->si_fhsm;
  10877. + if (!au_ftest_si(sbinfo, FHSM)
  10878. + || fhsm->fhsm_bottom == bindex)
  10879. + return;
  10880. +
  10881. + br = au_sbr(sb, bindex);
  10882. + bf = br->br_fhsm;
  10883. + AuDebugOn(!bf);
  10884. + mutex_lock(&bf->bf_lock);
  10885. + if (force
  10886. + || au_fhsm_pid(fhsm)
  10887. + || au_fhsm_test_jiffy(sbinfo, br))
  10888. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  10889. + /*do_notify*/1);
  10890. + mutex_unlock(&bf->bf_lock);
  10891. +}
  10892. +
  10893. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  10894. +{
  10895. + aufs_bindex_t bindex, bbot;
  10896. + struct au_branch *br;
  10897. +
  10898. + /* exclude the bottom */
  10899. + bbot = au_fhsm_bottom(sb);
  10900. + for (bindex = 0; bindex < bbot; bindex++) {
  10901. + br = au_sbr(sb, bindex);
  10902. + if (au_br_fhsm(br->br_perm))
  10903. + au_fhsm_wrote(sb, bindex, force);
  10904. + }
  10905. +}
  10906. +
  10907. +/* ---------------------------------------------------------------------- */
  10908. +
  10909. +static unsigned int au_fhsm_poll(struct file *file,
  10910. + struct poll_table_struct *wait)
  10911. +{
  10912. + unsigned int mask;
  10913. + struct au_sbinfo *sbinfo;
  10914. + struct au_fhsm *fhsm;
  10915. +
  10916. + mask = 0;
  10917. + sbinfo = file->private_data;
  10918. + fhsm = &sbinfo->si_fhsm;
  10919. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  10920. + if (atomic_read(&fhsm->fhsm_readable))
  10921. + mask = POLLIN /* | POLLRDNORM */;
  10922. +
  10923. + AuTraceErr((int)mask);
  10924. + return mask;
  10925. +}
  10926. +
  10927. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  10928. + struct aufs_stfs *stfs, __s16 brid)
  10929. +{
  10930. + int err;
  10931. +
  10932. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  10933. + if (!err)
  10934. + err = __put_user(brid, &stbr->brid);
  10935. + if (unlikely(err))
  10936. + err = -EFAULT;
  10937. +
  10938. + return err;
  10939. +}
  10940. +
  10941. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  10942. + struct aufs_stbr __user *stbr, size_t count)
  10943. +{
  10944. + ssize_t err;
  10945. + int nstbr;
  10946. + aufs_bindex_t bindex, bbot;
  10947. + struct au_branch *br;
  10948. + struct au_br_fhsm *bf;
  10949. +
  10950. + /* except the bottom branch */
  10951. + err = 0;
  10952. + nstbr = 0;
  10953. + bbot = au_fhsm_bottom(sb);
  10954. + for (bindex = 0; !err && bindex < bbot; bindex++) {
  10955. + br = au_sbr(sb, bindex);
  10956. + if (!au_br_fhsm(br->br_perm))
  10957. + continue;
  10958. +
  10959. + bf = br->br_fhsm;
  10960. + mutex_lock(&bf->bf_lock);
  10961. + if (bf->bf_readable) {
  10962. + err = -EFAULT;
  10963. + if (count >= sizeof(*stbr))
  10964. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  10965. + br->br_id);
  10966. + if (!err) {
  10967. + bf->bf_readable = 0;
  10968. + count -= sizeof(*stbr);
  10969. + nstbr++;
  10970. + }
  10971. + }
  10972. + mutex_unlock(&bf->bf_lock);
  10973. + }
  10974. + if (!err)
  10975. + err = sizeof(*stbr) * nstbr;
  10976. +
  10977. + return err;
  10978. +}
  10979. +
  10980. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  10981. + loff_t *pos)
  10982. +{
  10983. + ssize_t err;
  10984. + int readable;
  10985. + aufs_bindex_t nfhsm, bindex, bbot;
  10986. + struct au_sbinfo *sbinfo;
  10987. + struct au_fhsm *fhsm;
  10988. + struct au_branch *br;
  10989. + struct super_block *sb;
  10990. +
  10991. + err = 0;
  10992. + sbinfo = file->private_data;
  10993. + fhsm = &sbinfo->si_fhsm;
  10994. +need_data:
  10995. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  10996. + if (!atomic_read(&fhsm->fhsm_readable)) {
  10997. + if (vfsub_file_flags(file) & O_NONBLOCK)
  10998. + err = -EAGAIN;
  10999. + else
  11000. + err = wait_event_interruptible_locked_irq
  11001. + (fhsm->fhsm_wqh,
  11002. + atomic_read(&fhsm->fhsm_readable));
  11003. + }
  11004. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  11005. + if (unlikely(err))
  11006. + goto out;
  11007. +
  11008. + /* sb may already be dead */
  11009. + au_rw_read_lock(&sbinfo->si_rwsem);
  11010. + readable = atomic_read(&fhsm->fhsm_readable);
  11011. + if (readable > 0) {
  11012. + sb = sbinfo->si_sb;
  11013. + AuDebugOn(!sb);
  11014. + /* exclude the bottom branch */
  11015. + nfhsm = 0;
  11016. + bbot = au_fhsm_bottom(sb);
  11017. + for (bindex = 0; bindex < bbot; bindex++) {
  11018. + br = au_sbr(sb, bindex);
  11019. + if (au_br_fhsm(br->br_perm))
  11020. + nfhsm++;
  11021. + }
  11022. + err = -EMSGSIZE;
  11023. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  11024. + atomic_set(&fhsm->fhsm_readable, 0);
  11025. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  11026. + count);
  11027. + }
  11028. + }
  11029. + au_rw_read_unlock(&sbinfo->si_rwsem);
  11030. + if (!readable)
  11031. + goto need_data;
  11032. +
  11033. +out:
  11034. + return err;
  11035. +}
  11036. +
  11037. +static int au_fhsm_release(struct inode *inode, struct file *file)
  11038. +{
  11039. + struct au_sbinfo *sbinfo;
  11040. + struct au_fhsm *fhsm;
  11041. +
  11042. + /* sb may already be dead */
  11043. + sbinfo = file->private_data;
  11044. + fhsm = &sbinfo->si_fhsm;
  11045. + spin_lock(&fhsm->fhsm_spin);
  11046. + fhsm->fhsm_pid = 0;
  11047. + spin_unlock(&fhsm->fhsm_spin);
  11048. + kobject_put(&sbinfo->si_kobj);
  11049. +
  11050. + return 0;
  11051. +}
  11052. +
  11053. +static const struct file_operations au_fhsm_fops = {
  11054. + .owner = THIS_MODULE,
  11055. + .llseek = noop_llseek,
  11056. + .read = au_fhsm_read,
  11057. + .poll = au_fhsm_poll,
  11058. + .release = au_fhsm_release
  11059. +};
  11060. +
  11061. +int au_fhsm_fd(struct super_block *sb, int oflags)
  11062. +{
  11063. + int err, fd;
  11064. + struct au_sbinfo *sbinfo;
  11065. + struct au_fhsm *fhsm;
  11066. +
  11067. + err = -EPERM;
  11068. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  11069. + goto out;
  11070. +
  11071. + err = -EINVAL;
  11072. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  11073. + goto out;
  11074. +
  11075. + err = 0;
  11076. + sbinfo = au_sbi(sb);
  11077. + fhsm = &sbinfo->si_fhsm;
  11078. + spin_lock(&fhsm->fhsm_spin);
  11079. + if (!fhsm->fhsm_pid)
  11080. + fhsm->fhsm_pid = current->pid;
  11081. + else
  11082. + err = -EBUSY;
  11083. + spin_unlock(&fhsm->fhsm_spin);
  11084. + if (unlikely(err))
  11085. + goto out;
  11086. +
  11087. + oflags |= O_RDONLY;
  11088. + /* oflags |= FMODE_NONOTIFY; */
  11089. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  11090. + err = fd;
  11091. + if (unlikely(fd < 0))
  11092. + goto out_pid;
  11093. +
  11094. + /* succeed reglardless 'fhsm' status */
  11095. + kobject_get(&sbinfo->si_kobj);
  11096. + si_noflush_read_lock(sb);
  11097. + if (au_ftest_si(sbinfo, FHSM))
  11098. + au_fhsm_wrote_all(sb, /*force*/0);
  11099. + si_read_unlock(sb);
  11100. + goto out; /* success */
  11101. +
  11102. +out_pid:
  11103. + spin_lock(&fhsm->fhsm_spin);
  11104. + fhsm->fhsm_pid = 0;
  11105. + spin_unlock(&fhsm->fhsm_spin);
  11106. +out:
  11107. + AuTraceErr(err);
  11108. + return err;
  11109. +}
  11110. +
  11111. +/* ---------------------------------------------------------------------- */
  11112. +
  11113. +int au_fhsm_br_alloc(struct au_branch *br)
  11114. +{
  11115. + int err;
  11116. +
  11117. + err = 0;
  11118. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  11119. + if (br->br_fhsm)
  11120. + au_br_fhsm_init(br->br_fhsm);
  11121. + else
  11122. + err = -ENOMEM;
  11123. +
  11124. + return err;
  11125. +}
  11126. +
  11127. +/* ---------------------------------------------------------------------- */
  11128. +
  11129. +void au_fhsm_fin(struct super_block *sb)
  11130. +{
  11131. + au_fhsm_notify(sb, /*val*/-1);
  11132. +}
  11133. +
  11134. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  11135. +{
  11136. + struct au_fhsm *fhsm;
  11137. +
  11138. + fhsm = &sbinfo->si_fhsm;
  11139. + spin_lock_init(&fhsm->fhsm_spin);
  11140. + init_waitqueue_head(&fhsm->fhsm_wqh);
  11141. + atomic_set(&fhsm->fhsm_readable, 0);
  11142. + fhsm->fhsm_expire
  11143. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  11144. + fhsm->fhsm_bottom = -1;
  11145. +}
  11146. +
  11147. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  11148. +{
  11149. + sbinfo->si_fhsm.fhsm_expire
  11150. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  11151. +}
  11152. +
  11153. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  11154. +{
  11155. + unsigned int u;
  11156. +
  11157. + if (!au_ftest_si(sbinfo, FHSM))
  11158. + return;
  11159. +
  11160. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  11161. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  11162. + seq_printf(seq, ",fhsm_sec=%u", u);
  11163. +}
  11164. diff -Naur null/fs/aufs/file.c linux-4.7/fs/aufs/file.c
  11165. --- /dev/null
  11166. +++ linux-4.7/fs/aufs/file.c 2016-08-08 12:57:44.601384697 -0400
  11167. @@ -0,0 +1,845 @@
  11168. +/*
  11169. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  11170. + *
  11171. + * This program, aufs is free software; you can redistribute it and/or modify
  11172. + * it under the terms of the GNU General Public License as published by
  11173. + * the Free Software Foundation; either version 2 of the License, or
  11174. + * (at your option) any later version.
  11175. + *
  11176. + * This program is distributed in the hope that it will be useful,
  11177. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11178. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11179. + * GNU General Public License for more details.
  11180. + *
  11181. + * You should have received a copy of the GNU General Public License
  11182. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11183. + */
  11184. +
  11185. +/*
  11186. + * handling file/dir, and address_space operation
  11187. + */
  11188. +
  11189. +#ifdef CONFIG_AUFS_DEBUG
  11190. +#include <linux/migrate.h>
  11191. +#endif
  11192. +#include <linux/pagemap.h>
  11193. +#include "aufs.h"
  11194. +
  11195. +/* drop flags for writing */
  11196. +unsigned int au_file_roflags(unsigned int flags)
  11197. +{
  11198. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  11199. + flags |= O_RDONLY | O_NOATIME;
  11200. + return flags;
  11201. +}
  11202. +
  11203. +/* common functions to regular file and dir */
  11204. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  11205. + struct file *file, int force_wr)
  11206. +{
  11207. + struct file *h_file;
  11208. + struct dentry *h_dentry;
  11209. + struct inode *h_inode;
  11210. + struct super_block *sb;
  11211. + struct au_branch *br;
  11212. + struct path h_path;
  11213. + int err;
  11214. +
  11215. + /* a race condition can happen between open and unlink/rmdir */
  11216. + h_file = ERR_PTR(-ENOENT);
  11217. + h_dentry = au_h_dptr(dentry, bindex);
  11218. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  11219. + goto out;
  11220. + h_inode = d_inode(h_dentry);
  11221. + spin_lock(&h_dentry->d_lock);
  11222. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  11223. + /* || !d_inode(dentry)->i_nlink */
  11224. + ;
  11225. + spin_unlock(&h_dentry->d_lock);
  11226. + if (unlikely(err))
  11227. + goto out;
  11228. +
  11229. + sb = dentry->d_sb;
  11230. + br = au_sbr(sb, bindex);
  11231. + err = au_br_test_oflag(flags, br);
  11232. + h_file = ERR_PTR(err);
  11233. + if (unlikely(err))
  11234. + goto out;
  11235. +
  11236. + /* drop flags for writing */
  11237. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  11238. + if (force_wr && !(flags & O_WRONLY))
  11239. + force_wr = 0;
  11240. + flags = au_file_roflags(flags);
  11241. + if (force_wr) {
  11242. + h_file = ERR_PTR(-EROFS);
  11243. + flags = au_file_roflags(flags);
  11244. + if (unlikely(vfsub_native_ro(h_inode)
  11245. + || IS_APPEND(h_inode)))
  11246. + goto out;
  11247. + flags &= ~O_ACCMODE;
  11248. + flags |= O_WRONLY;
  11249. + }
  11250. + }
  11251. + flags &= ~O_CREAT;
  11252. + au_br_get(br);
  11253. + h_path.dentry = h_dentry;
  11254. + h_path.mnt = au_br_mnt(br);
  11255. + h_file = vfsub_dentry_open(&h_path, flags);
  11256. + if (IS_ERR(h_file))
  11257. + goto out_br;
  11258. +
  11259. + if (flags & __FMODE_EXEC) {
  11260. + err = deny_write_access(h_file);
  11261. + if (unlikely(err)) {
  11262. + fput(h_file);
  11263. + h_file = ERR_PTR(err);
  11264. + goto out_br;
  11265. + }
  11266. + }
  11267. + fsnotify_open(h_file);
  11268. + goto out; /* success */
  11269. +
  11270. +out_br:
  11271. + au_br_put(br);
  11272. +out:
  11273. + return h_file;
  11274. +}
  11275. +
  11276. +static int au_cmoo(struct dentry *dentry)
  11277. +{
  11278. + int err, cmoo;
  11279. + unsigned int udba;
  11280. + struct path h_path;
  11281. + struct au_pin pin;
  11282. + struct au_cp_generic cpg = {
  11283. + .dentry = dentry,
  11284. + .bdst = -1,
  11285. + .bsrc = -1,
  11286. + .len = -1,
  11287. + .pin = &pin,
  11288. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  11289. + };
  11290. + struct inode *delegated;
  11291. + struct super_block *sb;
  11292. + struct au_sbinfo *sbinfo;
  11293. + struct au_fhsm *fhsm;
  11294. + pid_t pid;
  11295. + struct au_branch *br;
  11296. + struct dentry *parent;
  11297. + struct au_hinode *hdir;
  11298. +
  11299. + DiMustWriteLock(dentry);
  11300. + IiMustWriteLock(d_inode(dentry));
  11301. +
  11302. + err = 0;
  11303. + if (IS_ROOT(dentry))
  11304. + goto out;
  11305. + cpg.bsrc = au_dbtop(dentry);
  11306. + if (!cpg.bsrc)
  11307. + goto out;
  11308. +
  11309. + sb = dentry->d_sb;
  11310. + sbinfo = au_sbi(sb);
  11311. + fhsm = &sbinfo->si_fhsm;
  11312. + pid = au_fhsm_pid(fhsm);
  11313. + if (pid
  11314. + && (current->pid == pid
  11315. + || current->real_parent->pid == pid))
  11316. + goto out;
  11317. +
  11318. + br = au_sbr(sb, cpg.bsrc);
  11319. + cmoo = au_br_cmoo(br->br_perm);
  11320. + if (!cmoo)
  11321. + goto out;
  11322. + if (!d_is_reg(dentry))
  11323. + cmoo &= AuBrAttr_COO_ALL;
  11324. + if (!cmoo)
  11325. + goto out;
  11326. +
  11327. + parent = dget_parent(dentry);
  11328. + di_write_lock_parent(parent);
  11329. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  11330. + cpg.bdst = err;
  11331. + if (unlikely(err < 0)) {
  11332. + err = 0; /* there is no upper writable branch */
  11333. + goto out_dgrade;
  11334. + }
  11335. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  11336. +
  11337. + /* do not respect the coo attrib for the target branch */
  11338. + err = au_cpup_dirs(dentry, cpg.bdst);
  11339. + if (unlikely(err))
  11340. + goto out_dgrade;
  11341. +
  11342. + di_downgrade_lock(parent, AuLock_IR);
  11343. + udba = au_opt_udba(sb);
  11344. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  11345. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11346. + if (unlikely(err))
  11347. + goto out_parent;
  11348. +
  11349. + err = au_sio_cpup_simple(&cpg);
  11350. + au_unpin(&pin);
  11351. + if (unlikely(err))
  11352. + goto out_parent;
  11353. + if (!(cmoo & AuBrWAttr_MOO))
  11354. + goto out_parent; /* success */
  11355. +
  11356. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  11357. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11358. + if (unlikely(err))
  11359. + goto out_parent;
  11360. +
  11361. + h_path.mnt = au_br_mnt(br);
  11362. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  11363. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  11364. + delegated = NULL;
  11365. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  11366. + au_unpin(&pin);
  11367. + /* todo: keep h_dentry or not? */
  11368. + if (unlikely(err == -EWOULDBLOCK)) {
  11369. + pr_warn("cannot retry for NFSv4 delegation"
  11370. + " for an internal unlink\n");
  11371. + iput(delegated);
  11372. + }
  11373. + if (unlikely(err)) {
  11374. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  11375. + dentry, err);
  11376. + err = 0;
  11377. + }
  11378. + goto out_parent; /* success */
  11379. +
  11380. +out_dgrade:
  11381. + di_downgrade_lock(parent, AuLock_IR);
  11382. +out_parent:
  11383. + di_read_unlock(parent, AuLock_IR);
  11384. + dput(parent);
  11385. +out:
  11386. + AuTraceErr(err);
  11387. + return err;
  11388. +}
  11389. +
  11390. +int au_do_open(struct file *file, struct au_do_open_args *args)
  11391. +{
  11392. + int err, no_lock = args->no_lock;
  11393. + struct dentry *dentry;
  11394. + struct au_finfo *finfo;
  11395. +
  11396. + if (!no_lock)
  11397. + err = au_finfo_init(file, args->fidir);
  11398. + else {
  11399. + lockdep_off();
  11400. + err = au_finfo_init(file, args->fidir);
  11401. + lockdep_on();
  11402. + }
  11403. + if (unlikely(err))
  11404. + goto out;
  11405. +
  11406. + dentry = file->f_path.dentry;
  11407. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  11408. + if (!no_lock) {
  11409. + di_write_lock_child(dentry);
  11410. + err = au_cmoo(dentry);
  11411. + di_downgrade_lock(dentry, AuLock_IR);
  11412. + if (!err)
  11413. + err = args->open(file, vfsub_file_flags(file), NULL);
  11414. + di_read_unlock(dentry, AuLock_IR);
  11415. + } else {
  11416. + err = au_cmoo(dentry);
  11417. + if (!err)
  11418. + err = args->open(file, vfsub_file_flags(file),
  11419. + args->h_file);
  11420. + if (!err && au_fbtop(file) != au_dbtop(dentry))
  11421. + /*
  11422. + * cmoo happens after h_file was opened.
  11423. + * need to refresh file later.
  11424. + */
  11425. + atomic_dec(&au_fi(file)->fi_generation);
  11426. + }
  11427. +
  11428. + finfo = au_fi(file);
  11429. + if (!err) {
  11430. + finfo->fi_file = file;
  11431. + au_sphl_add(&finfo->fi_hlist,
  11432. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  11433. + }
  11434. + if (!no_lock)
  11435. + fi_write_unlock(file);
  11436. + else {
  11437. + lockdep_off();
  11438. + fi_write_unlock(file);
  11439. + lockdep_on();
  11440. + }
  11441. + if (unlikely(err)) {
  11442. + finfo->fi_hdir = NULL;
  11443. + au_finfo_fin(file, /*atonce*/0);
  11444. + }
  11445. +
  11446. +out:
  11447. + return err;
  11448. +}
  11449. +
  11450. +int au_reopen_nondir(struct file *file)
  11451. +{
  11452. + int err;
  11453. + aufs_bindex_t btop;
  11454. + struct dentry *dentry;
  11455. + struct file *h_file, *h_file_tmp;
  11456. +
  11457. + dentry = file->f_path.dentry;
  11458. + btop = au_dbtop(dentry);
  11459. + h_file_tmp = NULL;
  11460. + if (au_fbtop(file) == btop) {
  11461. + h_file = au_hf_top(file);
  11462. + if (file->f_mode == h_file->f_mode)
  11463. + return 0; /* success */
  11464. + h_file_tmp = h_file;
  11465. + get_file(h_file_tmp);
  11466. + au_set_h_fptr(file, btop, NULL);
  11467. + }
  11468. + AuDebugOn(au_fi(file)->fi_hdir);
  11469. + /*
  11470. + * it can happen
  11471. + * file exists on both of rw and ro
  11472. + * open --> dbtop and fbtop are both 0
  11473. + * prepend a branch as rw, "rw" become ro
  11474. + * remove rw/file
  11475. + * delete the top branch, "rw" becomes rw again
  11476. + * --> dbtop is 1, fbtop is still 0
  11477. + * write --> fbtop is 0 but dbtop is 1
  11478. + */
  11479. + /* AuDebugOn(au_fbtop(file) < btop); */
  11480. +
  11481. + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
  11482. + file, /*force_wr*/0);
  11483. + err = PTR_ERR(h_file);
  11484. + if (IS_ERR(h_file)) {
  11485. + if (h_file_tmp) {
  11486. + au_sbr_get(dentry->d_sb, btop);
  11487. + au_set_h_fptr(file, btop, h_file_tmp);
  11488. + h_file_tmp = NULL;
  11489. + }
  11490. + goto out; /* todo: close all? */
  11491. + }
  11492. +
  11493. + err = 0;
  11494. + au_set_fbtop(file, btop);
  11495. + au_set_h_fptr(file, btop, h_file);
  11496. + au_update_figen(file);
  11497. + /* todo: necessary? */
  11498. + /* file->f_ra = h_file->f_ra; */
  11499. +
  11500. +out:
  11501. + if (h_file_tmp)
  11502. + fput(h_file_tmp);
  11503. + return err;
  11504. +}
  11505. +
  11506. +/* ---------------------------------------------------------------------- */
  11507. +
  11508. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  11509. + struct dentry *hi_wh)
  11510. +{
  11511. + int err;
  11512. + aufs_bindex_t btop;
  11513. + struct au_dinfo *dinfo;
  11514. + struct dentry *h_dentry;
  11515. + struct au_hdentry *hdp;
  11516. +
  11517. + dinfo = au_di(file->f_path.dentry);
  11518. + AuRwMustWriteLock(&dinfo->di_rwsem);
  11519. +
  11520. + btop = dinfo->di_btop;
  11521. + dinfo->di_btop = btgt;
  11522. + hdp = au_hdentry(dinfo, btgt);
  11523. + h_dentry = hdp->hd_dentry;
  11524. + hdp->hd_dentry = hi_wh;
  11525. + err = au_reopen_nondir(file);
  11526. + hdp->hd_dentry = h_dentry;
  11527. + dinfo->di_btop = btop;
  11528. +
  11529. + return err;
  11530. +}
  11531. +
  11532. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  11533. + aufs_bindex_t bcpup, struct au_pin *pin)
  11534. +{
  11535. + int err;
  11536. + struct inode *inode, *h_inode;
  11537. + struct dentry *h_dentry, *hi_wh;
  11538. + struct au_cp_generic cpg = {
  11539. + .dentry = file->f_path.dentry,
  11540. + .bdst = bcpup,
  11541. + .bsrc = -1,
  11542. + .len = len,
  11543. + .pin = pin
  11544. + };
  11545. +
  11546. + au_update_dbtop(cpg.dentry);
  11547. + inode = d_inode(cpg.dentry);
  11548. + h_inode = NULL;
  11549. + if (au_dbtop(cpg.dentry) <= bcpup
  11550. + && au_dbbot(cpg.dentry) >= bcpup) {
  11551. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  11552. + if (h_dentry && d_is_positive(h_dentry))
  11553. + h_inode = d_inode(h_dentry);
  11554. + }
  11555. + hi_wh = au_hi_wh(inode, bcpup);
  11556. + if (!hi_wh && !h_inode)
  11557. + err = au_sio_cpup_wh(&cpg, file);
  11558. + else
  11559. + /* already copied-up after unlink */
  11560. + err = au_reopen_wh(file, bcpup, hi_wh);
  11561. +
  11562. + if (!err
  11563. + && (inode->i_nlink > 1
  11564. + || (inode->i_state & I_LINKABLE))
  11565. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  11566. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  11567. +
  11568. + return err;
  11569. +}
  11570. +
  11571. +/*
  11572. + * prepare the @file for writing.
  11573. + */
  11574. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  11575. +{
  11576. + int err;
  11577. + aufs_bindex_t dbtop;
  11578. + struct dentry *parent;
  11579. + struct inode *inode;
  11580. + struct super_block *sb;
  11581. + struct file *h_file;
  11582. + struct au_cp_generic cpg = {
  11583. + .dentry = file->f_path.dentry,
  11584. + .bdst = -1,
  11585. + .bsrc = -1,
  11586. + .len = len,
  11587. + .pin = pin,
  11588. + .flags = AuCpup_DTIME
  11589. + };
  11590. +
  11591. + sb = cpg.dentry->d_sb;
  11592. + inode = d_inode(cpg.dentry);
  11593. + cpg.bsrc = au_fbtop(file);
  11594. + err = au_test_ro(sb, cpg.bsrc, inode);
  11595. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  11596. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  11597. + /*flags*/0);
  11598. + goto out;
  11599. + }
  11600. +
  11601. + /* need to cpup or reopen */
  11602. + parent = dget_parent(cpg.dentry);
  11603. + di_write_lock_parent(parent);
  11604. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11605. + cpg.bdst = err;
  11606. + if (unlikely(err < 0))
  11607. + goto out_dgrade;
  11608. + err = 0;
  11609. +
  11610. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  11611. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  11612. + if (unlikely(err))
  11613. + goto out_dgrade;
  11614. + }
  11615. +
  11616. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11617. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11618. + if (unlikely(err))
  11619. + goto out_dgrade;
  11620. +
  11621. + dbtop = au_dbtop(cpg.dentry);
  11622. + if (dbtop <= cpg.bdst)
  11623. + cpg.bsrc = cpg.bdst;
  11624. +
  11625. + if (dbtop <= cpg.bdst /* just reopen */
  11626. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  11627. + ) {
  11628. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  11629. + if (IS_ERR(h_file))
  11630. + err = PTR_ERR(h_file);
  11631. + else {
  11632. + di_downgrade_lock(parent, AuLock_IR);
  11633. + if (dbtop > cpg.bdst)
  11634. + err = au_sio_cpup_simple(&cpg);
  11635. + if (!err)
  11636. + err = au_reopen_nondir(file);
  11637. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  11638. + }
  11639. + } else { /* copyup as wh and reopen */
  11640. + /*
  11641. + * since writable hfsplus branch is not supported,
  11642. + * h_open_pre/post() are unnecessary.
  11643. + */
  11644. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  11645. + di_downgrade_lock(parent, AuLock_IR);
  11646. + }
  11647. +
  11648. + if (!err) {
  11649. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  11650. + goto out_dput; /* success */
  11651. + }
  11652. + au_unpin(pin);
  11653. + goto out_unlock;
  11654. +
  11655. +out_dgrade:
  11656. + di_downgrade_lock(parent, AuLock_IR);
  11657. +out_unlock:
  11658. + di_read_unlock(parent, AuLock_IR);
  11659. +out_dput:
  11660. + dput(parent);
  11661. +out:
  11662. + return err;
  11663. +}
  11664. +
  11665. +/* ---------------------------------------------------------------------- */
  11666. +
  11667. +int au_do_flush(struct file *file, fl_owner_t id,
  11668. + int (*flush)(struct file *file, fl_owner_t id))
  11669. +{
  11670. + int err;
  11671. + struct super_block *sb;
  11672. + struct inode *inode;
  11673. +
  11674. + inode = file_inode(file);
  11675. + sb = inode->i_sb;
  11676. + si_noflush_read_lock(sb);
  11677. + fi_read_lock(file);
  11678. + ii_read_lock_child(inode);
  11679. +
  11680. + err = flush(file, id);
  11681. + au_cpup_attr_timesizes(inode);
  11682. +
  11683. + ii_read_unlock(inode);
  11684. + fi_read_unlock(file);
  11685. + si_read_unlock(sb);
  11686. + return err;
  11687. +}
  11688. +
  11689. +/* ---------------------------------------------------------------------- */
  11690. +
  11691. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  11692. +{
  11693. + int err;
  11694. + struct au_pin pin;
  11695. + struct au_finfo *finfo;
  11696. + struct dentry *parent, *hi_wh;
  11697. + struct inode *inode;
  11698. + struct super_block *sb;
  11699. + struct au_cp_generic cpg = {
  11700. + .dentry = file->f_path.dentry,
  11701. + .bdst = -1,
  11702. + .bsrc = -1,
  11703. + .len = -1,
  11704. + .pin = &pin,
  11705. + .flags = AuCpup_DTIME
  11706. + };
  11707. +
  11708. + FiMustWriteLock(file);
  11709. +
  11710. + err = 0;
  11711. + finfo = au_fi(file);
  11712. + sb = cpg.dentry->d_sb;
  11713. + inode = d_inode(cpg.dentry);
  11714. + cpg.bdst = au_ibtop(inode);
  11715. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  11716. + goto out;
  11717. +
  11718. + parent = dget_parent(cpg.dentry);
  11719. + if (au_test_ro(sb, cpg.bdst, inode)) {
  11720. + di_read_lock_parent(parent, !AuLock_IR);
  11721. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11722. + cpg.bdst = err;
  11723. + di_read_unlock(parent, !AuLock_IR);
  11724. + if (unlikely(err < 0))
  11725. + goto out_parent;
  11726. + err = 0;
  11727. + }
  11728. +
  11729. + di_read_lock_parent(parent, AuLock_IR);
  11730. + hi_wh = au_hi_wh(inode, cpg.bdst);
  11731. + if (!S_ISDIR(inode->i_mode)
  11732. + && au_opt_test(au_mntflags(sb), PLINK)
  11733. + && au_plink_test(inode)
  11734. + && !d_unhashed(cpg.dentry)
  11735. + && cpg.bdst < au_dbtop(cpg.dentry)) {
  11736. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  11737. + if (unlikely(err))
  11738. + goto out_unlock;
  11739. +
  11740. + /* always superio. */
  11741. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11742. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11743. + if (!err) {
  11744. + err = au_sio_cpup_simple(&cpg);
  11745. + au_unpin(&pin);
  11746. + }
  11747. + } else if (hi_wh) {
  11748. + /* already copied-up after unlink */
  11749. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  11750. + *need_reopen = 0;
  11751. + }
  11752. +
  11753. +out_unlock:
  11754. + di_read_unlock(parent, AuLock_IR);
  11755. +out_parent:
  11756. + dput(parent);
  11757. +out:
  11758. + return err;
  11759. +}
  11760. +
  11761. +static void au_do_refresh_dir(struct file *file)
  11762. +{
  11763. + int execed;
  11764. + aufs_bindex_t bindex, bbot, new_bindex, brid;
  11765. + struct au_hfile *p, tmp, *q;
  11766. + struct au_finfo *finfo;
  11767. + struct super_block *sb;
  11768. + struct au_fidir *fidir;
  11769. +
  11770. + FiMustWriteLock(file);
  11771. +
  11772. + sb = file->f_path.dentry->d_sb;
  11773. + finfo = au_fi(file);
  11774. + fidir = finfo->fi_hdir;
  11775. + AuDebugOn(!fidir);
  11776. + p = fidir->fd_hfile + finfo->fi_btop;
  11777. + brid = p->hf_br->br_id;
  11778. + bbot = fidir->fd_bbot;
  11779. + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
  11780. + if (!p->hf_file)
  11781. + continue;
  11782. +
  11783. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  11784. + if (new_bindex == bindex)
  11785. + continue;
  11786. + if (new_bindex < 0) {
  11787. + au_set_h_fptr(file, bindex, NULL);
  11788. + continue;
  11789. + }
  11790. +
  11791. + /* swap two lower inode, and loop again */
  11792. + q = fidir->fd_hfile + new_bindex;
  11793. + tmp = *q;
  11794. + *q = *p;
  11795. + *p = tmp;
  11796. + if (tmp.hf_file) {
  11797. + bindex--;
  11798. + p--;
  11799. + }
  11800. + }
  11801. +
  11802. + execed = vfsub_file_execed(file);
  11803. + p = fidir->fd_hfile;
  11804. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  11805. + bbot = au_sbbot(sb);
  11806. + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
  11807. + finfo->fi_btop++, p++)
  11808. + if (p->hf_file) {
  11809. + if (file_inode(p->hf_file))
  11810. + break;
  11811. + au_hfput(p, execed);
  11812. + }
  11813. + } else {
  11814. + bbot = au_br_index(sb, brid);
  11815. + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
  11816. + finfo->fi_btop++, p++)
  11817. + if (p->hf_file)
  11818. + au_hfput(p, execed);
  11819. + bbot = au_sbbot(sb);
  11820. + }
  11821. +
  11822. + p = fidir->fd_hfile + bbot;
  11823. + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
  11824. + fidir->fd_bbot--, p--)
  11825. + if (p->hf_file) {
  11826. + if (file_inode(p->hf_file))
  11827. + break;
  11828. + au_hfput(p, execed);
  11829. + }
  11830. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  11831. +}
  11832. +
  11833. +/*
  11834. + * after branch manipulating, refresh the file.
  11835. + */
  11836. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  11837. +{
  11838. + int err, need_reopen;
  11839. + aufs_bindex_t bbot, bindex;
  11840. + struct dentry *dentry;
  11841. + struct au_finfo *finfo;
  11842. + struct au_hfile *hfile;
  11843. +
  11844. + dentry = file->f_path.dentry;
  11845. + finfo = au_fi(file);
  11846. + if (!finfo->fi_hdir) {
  11847. + hfile = &finfo->fi_htop;
  11848. + AuDebugOn(!hfile->hf_file);
  11849. + bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
  11850. + AuDebugOn(bindex < 0);
  11851. + if (bindex != finfo->fi_btop)
  11852. + au_set_fbtop(file, bindex);
  11853. + } else {
  11854. + err = au_fidir_realloc(finfo, au_sbbot(dentry->d_sb) + 1);
  11855. + if (unlikely(err))
  11856. + goto out;
  11857. + au_do_refresh_dir(file);
  11858. + }
  11859. +
  11860. + err = 0;
  11861. + need_reopen = 1;
  11862. + if (!au_test_mmapped(file))
  11863. + err = au_file_refresh_by_inode(file, &need_reopen);
  11864. + if (!err && need_reopen && !d_unlinked(dentry))
  11865. + err = reopen(file);
  11866. + if (!err) {
  11867. + au_update_figen(file);
  11868. + goto out; /* success */
  11869. + }
  11870. +
  11871. + /* error, close all lower files */
  11872. + if (finfo->fi_hdir) {
  11873. + bbot = au_fbbot_dir(file);
  11874. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
  11875. + au_set_h_fptr(file, bindex, NULL);
  11876. + }
  11877. +
  11878. +out:
  11879. + return err;
  11880. +}
  11881. +
  11882. +/* common function to regular file and dir */
  11883. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  11884. + int wlock)
  11885. +{
  11886. + int err;
  11887. + unsigned int sigen, figen;
  11888. + aufs_bindex_t btop;
  11889. + unsigned char pseudo_link;
  11890. + struct dentry *dentry;
  11891. + struct inode *inode;
  11892. +
  11893. + err = 0;
  11894. + dentry = file->f_path.dentry;
  11895. + inode = d_inode(dentry);
  11896. + sigen = au_sigen(dentry->d_sb);
  11897. + fi_write_lock(file);
  11898. + figen = au_figen(file);
  11899. + di_write_lock_child(dentry);
  11900. + btop = au_dbtop(dentry);
  11901. + pseudo_link = (btop != au_ibtop(inode));
  11902. + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
  11903. + if (!wlock) {
  11904. + di_downgrade_lock(dentry, AuLock_IR);
  11905. + fi_downgrade_lock(file);
  11906. + }
  11907. + goto out; /* success */
  11908. + }
  11909. +
  11910. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  11911. + if (au_digen_test(dentry, sigen)) {
  11912. + err = au_reval_dpath(dentry, sigen);
  11913. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  11914. + }
  11915. +
  11916. + if (!err)
  11917. + err = refresh_file(file, reopen);
  11918. + if (!err) {
  11919. + if (!wlock) {
  11920. + di_downgrade_lock(dentry, AuLock_IR);
  11921. + fi_downgrade_lock(file);
  11922. + }
  11923. + } else {
  11924. + di_write_unlock(dentry);
  11925. + fi_write_unlock(file);
  11926. + }
  11927. +
  11928. +out:
  11929. + return err;
  11930. +}
  11931. +
  11932. +/* ---------------------------------------------------------------------- */
  11933. +
  11934. +/* cf. aufs_nopage() */
  11935. +/* for madvise(2) */
  11936. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  11937. +{
  11938. + unlock_page(page);
  11939. + return 0;
  11940. +}
  11941. +
  11942. +/* it will never be called, but necessary to support O_DIRECT */
  11943. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  11944. +{ BUG(); return 0; }
  11945. +
  11946. +/* they will never be called. */
  11947. +#ifdef CONFIG_AUFS_DEBUG
  11948. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  11949. + loff_t pos, unsigned len, unsigned flags,
  11950. + struct page **pagep, void **fsdata)
  11951. +{ AuUnsupport(); return 0; }
  11952. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  11953. + loff_t pos, unsigned len, unsigned copied,
  11954. + struct page *page, void *fsdata)
  11955. +{ AuUnsupport(); return 0; }
  11956. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  11957. +{ AuUnsupport(); return 0; }
  11958. +
  11959. +static int aufs_set_page_dirty(struct page *page)
  11960. +{ AuUnsupport(); return 0; }
  11961. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  11962. + unsigned int length)
  11963. +{ AuUnsupport(); }
  11964. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  11965. +{ AuUnsupport(); return 0; }
  11966. +#if 0 /* called by memory compaction regardless file */
  11967. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  11968. + struct page *page, enum migrate_mode mode)
  11969. +{ AuUnsupport(); return 0; }
  11970. +#endif
  11971. +static int aufs_launder_page(struct page *page)
  11972. +{ AuUnsupport(); return 0; }
  11973. +static int aufs_is_partially_uptodate(struct page *page,
  11974. + unsigned long from,
  11975. + unsigned long count)
  11976. +{ AuUnsupport(); return 0; }
  11977. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  11978. + bool *writeback)
  11979. +{ AuUnsupport(); }
  11980. +static int aufs_error_remove_page(struct address_space *mapping,
  11981. + struct page *page)
  11982. +{ AuUnsupport(); return 0; }
  11983. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  11984. + sector_t *span)
  11985. +{ AuUnsupport(); return 0; }
  11986. +static void aufs_swap_deactivate(struct file *file)
  11987. +{ AuUnsupport(); }
  11988. +#endif /* CONFIG_AUFS_DEBUG */
  11989. +
  11990. +const struct address_space_operations aufs_aop = {
  11991. + .readpage = aufs_readpage,
  11992. + .direct_IO = aufs_direct_IO,
  11993. +#ifdef CONFIG_AUFS_DEBUG
  11994. + .writepage = aufs_writepage,
  11995. + /* no writepages, because of writepage */
  11996. + .set_page_dirty = aufs_set_page_dirty,
  11997. + /* no readpages, because of readpage */
  11998. + .write_begin = aufs_write_begin,
  11999. + .write_end = aufs_write_end,
  12000. + /* no bmap, no block device */
  12001. + .invalidatepage = aufs_invalidatepage,
  12002. + .releasepage = aufs_releasepage,
  12003. + /* is fallback_migrate_page ok? */
  12004. + /* .migratepage = aufs_migratepage, */
  12005. + .launder_page = aufs_launder_page,
  12006. + .is_partially_uptodate = aufs_is_partially_uptodate,
  12007. + .is_dirty_writeback = aufs_is_dirty_writeback,
  12008. + .error_remove_page = aufs_error_remove_page,
  12009. + .swap_activate = aufs_swap_activate,
  12010. + .swap_deactivate = aufs_swap_deactivate
  12011. +#endif /* CONFIG_AUFS_DEBUG */
  12012. +};
  12013. diff -Naur null/fs/aufs/file.h linux-4.7/fs/aufs/file.h
  12014. --- /dev/null
  12015. +++ linux-4.7/fs/aufs/file.h 2016-08-08 12:57:44.601384697 -0400
  12016. @@ -0,0 +1,294 @@
  12017. +/*
  12018. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  12019. + *
  12020. + * This program, aufs is free software; you can redistribute it and/or modify
  12021. + * it under the terms of the GNU General Public License as published by
  12022. + * the Free Software Foundation; either version 2 of the License, or
  12023. + * (at your option) any later version.
  12024. + *
  12025. + * This program is distributed in the hope that it will be useful,
  12026. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12027. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12028. + * GNU General Public License for more details.
  12029. + *
  12030. + * You should have received a copy of the GNU General Public License
  12031. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12032. + */
  12033. +
  12034. +/*
  12035. + * file operations
  12036. + */
  12037. +
  12038. +#ifndef __AUFS_FILE_H__
  12039. +#define __AUFS_FILE_H__
  12040. +
  12041. +#ifdef __KERNEL__
  12042. +
  12043. +#include <linux/file.h>
  12044. +#include <linux/fs.h>
  12045. +#include <linux/poll.h>
  12046. +#include "rwsem.h"
  12047. +
  12048. +struct au_branch;
  12049. +struct au_hfile {
  12050. + struct file *hf_file;
  12051. + struct au_branch *hf_br;
  12052. +};
  12053. +
  12054. +struct au_vdir;
  12055. +struct au_fidir {
  12056. + aufs_bindex_t fd_bbot;
  12057. + aufs_bindex_t fd_nent;
  12058. + struct au_vdir *fd_vdir_cache;
  12059. + struct au_hfile fd_hfile[];
  12060. +};
  12061. +
  12062. +static inline int au_fidir_sz(int nent)
  12063. +{
  12064. + AuDebugOn(nent < 0);
  12065. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  12066. +}
  12067. +
  12068. +struct au_finfo {
  12069. + atomic_t fi_generation;
  12070. +
  12071. + struct au_rwsem fi_rwsem;
  12072. + aufs_bindex_t fi_btop;
  12073. +
  12074. + /* do not union them */
  12075. + struct { /* for non-dir */
  12076. + struct au_hfile fi_htop;
  12077. + atomic_t fi_mmapped;
  12078. + };
  12079. + struct au_fidir *fi_hdir; /* for dir only */
  12080. +
  12081. + struct hlist_node fi_hlist;
  12082. + union {
  12083. + struct file *fi_file; /* very ugly */
  12084. + struct llist_node fi_lnode; /* delayed free */
  12085. + };
  12086. +} ____cacheline_aligned_in_smp;
  12087. +
  12088. +/* ---------------------------------------------------------------------- */
  12089. +
  12090. +/* file.c */
  12091. +extern const struct address_space_operations aufs_aop;
  12092. +unsigned int au_file_roflags(unsigned int flags);
  12093. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  12094. + struct file *file, int force_wr);
  12095. +struct au_do_open_args {
  12096. + int no_lock;
  12097. + int (*open)(struct file *file, int flags,
  12098. + struct file *h_file);
  12099. + struct au_fidir *fidir;
  12100. + struct file *h_file;
  12101. +};
  12102. +int au_do_open(struct file *file, struct au_do_open_args *args);
  12103. +int au_reopen_nondir(struct file *file);
  12104. +struct au_pin;
  12105. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  12106. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  12107. + int wlock);
  12108. +int au_do_flush(struct file *file, fl_owner_t id,
  12109. + int (*flush)(struct file *file, fl_owner_t id));
  12110. +
  12111. +/* poll.c */
  12112. +#ifdef CONFIG_AUFS_POLL
  12113. +unsigned int aufs_poll(struct file *file, poll_table *wait);
  12114. +#endif
  12115. +
  12116. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  12117. +/* hfsplus.c */
  12118. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  12119. + int force_wr);
  12120. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  12121. + struct file *h_file);
  12122. +#else
  12123. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  12124. + aufs_bindex_t bindex, int force_wr)
  12125. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  12126. + struct file *h_file);
  12127. +#endif
  12128. +
  12129. +/* f_op.c */
  12130. +extern const struct file_operations aufs_file_fop;
  12131. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  12132. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  12133. +struct file *au_read_pre(struct file *file, int keep_fi);
  12134. +
  12135. +/* finfo.c */
  12136. +void au_hfput(struct au_hfile *hf, int execed);
  12137. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  12138. + struct file *h_file);
  12139. +
  12140. +void au_update_figen(struct file *file);
  12141. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  12142. +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
  12143. +
  12144. +void au_fi_init_once(void *_fi);
  12145. +void au_finfo_fin(struct file *file, int atonce);
  12146. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  12147. +
  12148. +/* ioctl.c */
  12149. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  12150. +#ifdef CONFIG_COMPAT
  12151. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  12152. + unsigned long arg);
  12153. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  12154. + unsigned long arg);
  12155. +#endif
  12156. +
  12157. +/* ---------------------------------------------------------------------- */
  12158. +
  12159. +static inline struct au_finfo *au_fi(struct file *file)
  12160. +{
  12161. + return file->private_data;
  12162. +}
  12163. +
  12164. +/* ---------------------------------------------------------------------- */
  12165. +
  12166. +/*
  12167. + * fi_read_lock, fi_write_lock,
  12168. + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
  12169. + */
  12170. +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
  12171. +
  12172. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  12173. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  12174. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  12175. +
  12176. +/* ---------------------------------------------------------------------- */
  12177. +
  12178. +/* todo: hard/soft set? */
  12179. +static inline aufs_bindex_t au_fbtop(struct file *file)
  12180. +{
  12181. + FiMustAnyLock(file);
  12182. + return au_fi(file)->fi_btop;
  12183. +}
  12184. +
  12185. +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
  12186. +{
  12187. + FiMustAnyLock(file);
  12188. + AuDebugOn(!au_fi(file)->fi_hdir);
  12189. + return au_fi(file)->fi_hdir->fd_bbot;
  12190. +}
  12191. +
  12192. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  12193. +{
  12194. + FiMustAnyLock(file);
  12195. + AuDebugOn(!au_fi(file)->fi_hdir);
  12196. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  12197. +}
  12198. +
  12199. +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
  12200. +{
  12201. + FiMustWriteLock(file);
  12202. + au_fi(file)->fi_btop = bindex;
  12203. +}
  12204. +
  12205. +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
  12206. +{
  12207. + FiMustWriteLock(file);
  12208. + AuDebugOn(!au_fi(file)->fi_hdir);
  12209. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  12210. +}
  12211. +
  12212. +static inline void au_set_fvdir_cache(struct file *file,
  12213. + struct au_vdir *vdir_cache)
  12214. +{
  12215. + FiMustWriteLock(file);
  12216. + AuDebugOn(!au_fi(file)->fi_hdir);
  12217. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  12218. +}
  12219. +
  12220. +static inline struct file *au_hf_top(struct file *file)
  12221. +{
  12222. + FiMustAnyLock(file);
  12223. + AuDebugOn(au_fi(file)->fi_hdir);
  12224. + return au_fi(file)->fi_htop.hf_file;
  12225. +}
  12226. +
  12227. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  12228. +{
  12229. + FiMustAnyLock(file);
  12230. + AuDebugOn(!au_fi(file)->fi_hdir);
  12231. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  12232. +}
  12233. +
  12234. +/* todo: memory barrier? */
  12235. +static inline unsigned int au_figen(struct file *f)
  12236. +{
  12237. + return atomic_read(&au_fi(f)->fi_generation);
  12238. +}
  12239. +
  12240. +static inline void au_set_mmapped(struct file *f)
  12241. +{
  12242. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  12243. + return;
  12244. + pr_warn("fi_mmapped wrapped around\n");
  12245. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  12246. + ;
  12247. +}
  12248. +
  12249. +static inline void au_unset_mmapped(struct file *f)
  12250. +{
  12251. + atomic_dec(&au_fi(f)->fi_mmapped);
  12252. +}
  12253. +
  12254. +static inline int au_test_mmapped(struct file *f)
  12255. +{
  12256. + return atomic_read(&au_fi(f)->fi_mmapped);
  12257. +}
  12258. +
  12259. +/* customize vma->vm_file */
  12260. +
  12261. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  12262. + struct file *file)
  12263. +{
  12264. + struct file *f;
  12265. +
  12266. + f = vma->vm_file;
  12267. + get_file(file);
  12268. + vma->vm_file = file;
  12269. + fput(f);
  12270. +}
  12271. +
  12272. +#ifdef CONFIG_MMU
  12273. +#define AuDbgVmRegion(file, vma) do {} while (0)
  12274. +
  12275. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  12276. + struct file *file)
  12277. +{
  12278. + au_do_vm_file_reset(vma, file);
  12279. +}
  12280. +#else
  12281. +#define AuDbgVmRegion(file, vma) \
  12282. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  12283. +
  12284. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  12285. + struct file *file)
  12286. +{
  12287. + struct file *f;
  12288. +
  12289. + au_do_vm_file_reset(vma, file);
  12290. + f = vma->vm_region->vm_file;
  12291. + get_file(file);
  12292. + vma->vm_region->vm_file = file;
  12293. + fput(f);
  12294. +}
  12295. +#endif /* CONFIG_MMU */
  12296. +
  12297. +/* handle vma->vm_prfile */
  12298. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  12299. + struct file *file)
  12300. +{
  12301. + get_file(file);
  12302. + vma->vm_prfile = file;
  12303. +#ifndef CONFIG_MMU
  12304. + get_file(file);
  12305. + vma->vm_region->vm_prfile = file;
  12306. +#endif
  12307. +}
  12308. +
  12309. +#endif /* __KERNEL__ */
  12310. +#endif /* __AUFS_FILE_H__ */
  12311. diff -Naur null/fs/aufs/finfo.c linux-4.7/fs/aufs/finfo.c
  12312. --- /dev/null
  12313. +++ linux-4.7/fs/aufs/finfo.c 2016-08-08 12:57:44.601384697 -0400
  12314. @@ -0,0 +1,151 @@
  12315. +/*
  12316. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  12317. + *
  12318. + * This program, aufs is free software; you can redistribute it and/or modify
  12319. + * it under the terms of the GNU General Public License as published by
  12320. + * the Free Software Foundation; either version 2 of the License, or
  12321. + * (at your option) any later version.
  12322. + *
  12323. + * This program is distributed in the hope that it will be useful,
  12324. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12325. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12326. + * GNU General Public License for more details.
  12327. + *
  12328. + * You should have received a copy of the GNU General Public License
  12329. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12330. + */
  12331. +
  12332. +/*
  12333. + * file private data
  12334. + */
  12335. +
  12336. +#include "aufs.h"
  12337. +
  12338. +void au_hfput(struct au_hfile *hf, int execed)
  12339. +{
  12340. + if (execed)
  12341. + allow_write_access(hf->hf_file);
  12342. + fput(hf->hf_file);
  12343. + hf->hf_file = NULL;
  12344. + au_br_put(hf->hf_br);
  12345. + hf->hf_br = NULL;
  12346. +}
  12347. +
  12348. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  12349. +{
  12350. + struct au_finfo *finfo = au_fi(file);
  12351. + struct au_hfile *hf;
  12352. + struct au_fidir *fidir;
  12353. +
  12354. + fidir = finfo->fi_hdir;
  12355. + if (!fidir) {
  12356. + AuDebugOn(finfo->fi_btop != bindex);
  12357. + hf = &finfo->fi_htop;
  12358. + } else
  12359. + hf = fidir->fd_hfile + bindex;
  12360. +
  12361. + if (hf && hf->hf_file)
  12362. + au_hfput(hf, vfsub_file_execed(file));
  12363. + if (val) {
  12364. + FiMustWriteLock(file);
  12365. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  12366. + hf->hf_file = val;
  12367. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  12368. + }
  12369. +}
  12370. +
  12371. +void au_update_figen(struct file *file)
  12372. +{
  12373. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  12374. + /* smp_mb(); */ /* atomic_set */
  12375. +}
  12376. +
  12377. +/* ---------------------------------------------------------------------- */
  12378. +
  12379. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  12380. +{
  12381. + struct au_fidir *fidir;
  12382. + int nbr;
  12383. +
  12384. + nbr = au_sbbot(sb) + 1;
  12385. + if (nbr < 2)
  12386. + nbr = 2; /* initial allocate for 2 branches */
  12387. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  12388. + if (fidir) {
  12389. + fidir->fd_bbot = -1;
  12390. + fidir->fd_nent = nbr;
  12391. + }
  12392. +
  12393. + return fidir;
  12394. +}
  12395. +
  12396. +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
  12397. +{
  12398. + int err;
  12399. + struct au_fidir *fidir, *p;
  12400. +
  12401. + AuRwMustWriteLock(&finfo->fi_rwsem);
  12402. + fidir = finfo->fi_hdir;
  12403. + AuDebugOn(!fidir);
  12404. +
  12405. + err = -ENOMEM;
  12406. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  12407. + GFP_NOFS);
  12408. + if (p) {
  12409. + p->fd_nent = nbr;
  12410. + finfo->fi_hdir = p;
  12411. + err = 0;
  12412. + }
  12413. +
  12414. + return err;
  12415. +}
  12416. +
  12417. +/* ---------------------------------------------------------------------- */
  12418. +
  12419. +void au_finfo_fin(struct file *file, int atonce)
  12420. +{
  12421. + struct au_finfo *finfo;
  12422. +
  12423. + au_nfiles_dec(file->f_path.dentry->d_sb);
  12424. +
  12425. + finfo = au_fi(file);
  12426. + AuDebugOn(finfo->fi_hdir);
  12427. + AuRwDestroy(&finfo->fi_rwsem);
  12428. + if (!atonce)
  12429. + au_cache_dfree_finfo(finfo);
  12430. + else
  12431. + au_cache_free_finfo(finfo);
  12432. +}
  12433. +
  12434. +void au_fi_init_once(void *_finfo)
  12435. +{
  12436. + struct au_finfo *finfo = _finfo;
  12437. +
  12438. + au_rw_init(&finfo->fi_rwsem);
  12439. +}
  12440. +
  12441. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  12442. +{
  12443. + int err;
  12444. + struct au_finfo *finfo;
  12445. + struct dentry *dentry;
  12446. +
  12447. + err = -ENOMEM;
  12448. + dentry = file->f_path.dentry;
  12449. + finfo = au_cache_alloc_finfo();
  12450. + if (unlikely(!finfo))
  12451. + goto out;
  12452. +
  12453. + err = 0;
  12454. + au_nfiles_inc(dentry->d_sb);
  12455. + au_rw_write_lock(&finfo->fi_rwsem);
  12456. + finfo->fi_btop = -1;
  12457. + finfo->fi_hdir = fidir;
  12458. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  12459. + /* smp_mb(); */ /* atomic_set */
  12460. +
  12461. + file->private_data = finfo;
  12462. +
  12463. +out:
  12464. + return err;
  12465. +}
  12466. diff -Naur null/fs/aufs/f_op.c linux-4.7/fs/aufs/f_op.c
  12467. --- /dev/null
  12468. +++ linux-4.7/fs/aufs/f_op.c 2016-08-08 12:57:44.600384719 -0400
  12469. @@ -0,0 +1,772 @@
  12470. +/*
  12471. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  12472. + *
  12473. + * This program, aufs is free software; you can redistribute it and/or modify
  12474. + * it under the terms of the GNU General Public License as published by
  12475. + * the Free Software Foundation; either version 2 of the License, or
  12476. + * (at your option) any later version.
  12477. + *
  12478. + * This program is distributed in the hope that it will be useful,
  12479. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12480. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12481. + * GNU General Public License for more details.
  12482. + *
  12483. + * You should have received a copy of the GNU General Public License
  12484. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12485. + */
  12486. +
  12487. +/*
  12488. + * file and vm operations
  12489. + */
  12490. +
  12491. +#include <linux/aio.h>
  12492. +#include <linux/fs_stack.h>
  12493. +#include <linux/mman.h>
  12494. +#include <linux/security.h>
  12495. +#include "aufs.h"
  12496. +
  12497. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  12498. +{
  12499. + int err;
  12500. + aufs_bindex_t bindex;
  12501. + struct dentry *dentry, *h_dentry;
  12502. + struct au_finfo *finfo;
  12503. + struct inode *h_inode;
  12504. +
  12505. + FiMustWriteLock(file);
  12506. +
  12507. + err = 0;
  12508. + dentry = file->f_path.dentry;
  12509. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  12510. + finfo = au_fi(file);
  12511. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  12512. + atomic_set(&finfo->fi_mmapped, 0);
  12513. + bindex = au_dbtop(dentry);
  12514. + if (!h_file) {
  12515. + h_dentry = au_h_dptr(dentry, bindex);
  12516. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  12517. + if (unlikely(err))
  12518. + goto out;
  12519. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  12520. + } else {
  12521. + h_dentry = h_file->f_path.dentry;
  12522. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  12523. + if (unlikely(err))
  12524. + goto out;
  12525. + get_file(h_file);
  12526. + }
  12527. + if (IS_ERR(h_file))
  12528. + err = PTR_ERR(h_file);
  12529. + else {
  12530. + if ((flags & __O_TMPFILE)
  12531. + && !(flags & O_EXCL)) {
  12532. + h_inode = file_inode(h_file);
  12533. + spin_lock(&h_inode->i_lock);
  12534. + h_inode->i_state |= I_LINKABLE;
  12535. + spin_unlock(&h_inode->i_lock);
  12536. + }
  12537. + au_set_fbtop(file, bindex);
  12538. + au_set_h_fptr(file, bindex, h_file);
  12539. + au_update_figen(file);
  12540. + /* todo: necessary? */
  12541. + /* file->f_ra = h_file->f_ra; */
  12542. + }
  12543. +
  12544. +out:
  12545. + return err;
  12546. +}
  12547. +
  12548. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  12549. + struct file *file)
  12550. +{
  12551. + int err;
  12552. + struct super_block *sb;
  12553. + struct au_do_open_args args = {
  12554. + .open = au_do_open_nondir
  12555. + };
  12556. +
  12557. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  12558. + file, vfsub_file_flags(file), file->f_mode);
  12559. +
  12560. + sb = file->f_path.dentry->d_sb;
  12561. + si_read_lock(sb, AuLock_FLUSH);
  12562. + err = au_do_open(file, &args);
  12563. + si_read_unlock(sb);
  12564. + return err;
  12565. +}
  12566. +
  12567. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  12568. +{
  12569. + struct au_finfo *finfo;
  12570. + aufs_bindex_t bindex;
  12571. + int delayed;
  12572. +
  12573. + finfo = au_fi(file);
  12574. + au_sphl_del(&finfo->fi_hlist,
  12575. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  12576. + bindex = finfo->fi_btop;
  12577. + if (bindex >= 0)
  12578. + au_set_h_fptr(file, bindex, NULL);
  12579. +
  12580. + delayed = (current->flags & PF_KTHREAD) || in_interrupt();
  12581. + au_finfo_fin(file, delayed);
  12582. + return 0;
  12583. +}
  12584. +
  12585. +/* ---------------------------------------------------------------------- */
  12586. +
  12587. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  12588. +{
  12589. + int err;
  12590. + struct file *h_file;
  12591. +
  12592. + err = 0;
  12593. + h_file = au_hf_top(file);
  12594. + if (h_file)
  12595. + err = vfsub_flush(h_file, id);
  12596. + return err;
  12597. +}
  12598. +
  12599. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  12600. +{
  12601. + return au_do_flush(file, id, au_do_flush_nondir);
  12602. +}
  12603. +
  12604. +/* ---------------------------------------------------------------------- */
  12605. +/*
  12606. + * read and write functions acquire [fdi]_rwsem once, but release before
  12607. + * mmap_sem. This is because to stop a race condition between mmap(2).
  12608. + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
  12609. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  12610. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  12611. + */
  12612. +
  12613. +/* Callers should call au_read_post() or fput() in the end */
  12614. +struct file *au_read_pre(struct file *file, int keep_fi)
  12615. +{
  12616. + struct file *h_file;
  12617. + int err;
  12618. +
  12619. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  12620. + if (!err) {
  12621. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  12622. + h_file = au_hf_top(file);
  12623. + get_file(h_file);
  12624. + if (!keep_fi)
  12625. + fi_read_unlock(file);
  12626. + } else
  12627. + h_file = ERR_PTR(err);
  12628. +
  12629. + return h_file;
  12630. +}
  12631. +
  12632. +static void au_read_post(struct inode *inode, struct file *h_file)
  12633. +{
  12634. + /* update without lock, I don't think it a problem */
  12635. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  12636. + fput(h_file);
  12637. +}
  12638. +
  12639. +struct au_write_pre {
  12640. + blkcnt_t blks;
  12641. + aufs_bindex_t btop;
  12642. +};
  12643. +
  12644. +/*
  12645. + * return with iinfo is write-locked
  12646. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  12647. + * end
  12648. + */
  12649. +static struct file *au_write_pre(struct file *file, int do_ready,
  12650. + struct au_write_pre *wpre)
  12651. +{
  12652. + struct file *h_file;
  12653. + struct dentry *dentry;
  12654. + int err;
  12655. + struct au_pin pin;
  12656. +
  12657. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12658. + h_file = ERR_PTR(err);
  12659. + if (unlikely(err))
  12660. + goto out;
  12661. +
  12662. + dentry = file->f_path.dentry;
  12663. + if (do_ready) {
  12664. + err = au_ready_to_write(file, -1, &pin);
  12665. + if (unlikely(err)) {
  12666. + h_file = ERR_PTR(err);
  12667. + di_write_unlock(dentry);
  12668. + goto out_fi;
  12669. + }
  12670. + }
  12671. +
  12672. + di_downgrade_lock(dentry, /*flags*/0);
  12673. + if (wpre)
  12674. + wpre->btop = au_fbtop(file);
  12675. + h_file = au_hf_top(file);
  12676. + get_file(h_file);
  12677. + if (wpre)
  12678. + wpre->blks = file_inode(h_file)->i_blocks;
  12679. + if (do_ready)
  12680. + au_unpin(&pin);
  12681. + di_read_unlock(dentry, /*flags*/0);
  12682. +
  12683. +out_fi:
  12684. + fi_write_unlock(file);
  12685. +out:
  12686. + return h_file;
  12687. +}
  12688. +
  12689. +static void au_write_post(struct inode *inode, struct file *h_file,
  12690. + struct au_write_pre *wpre, ssize_t written)
  12691. +{
  12692. + struct inode *h_inode;
  12693. +
  12694. + au_cpup_attr_timesizes(inode);
  12695. + AuDebugOn(au_ibtop(inode) != wpre->btop);
  12696. + h_inode = file_inode(h_file);
  12697. + inode->i_mode = h_inode->i_mode;
  12698. + ii_write_unlock(inode);
  12699. + fput(h_file);
  12700. +
  12701. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  12702. + if (written > 0)
  12703. + au_fhsm_wrote(inode->i_sb, wpre->btop,
  12704. + /*force*/h_inode->i_blocks > wpre->blks);
  12705. +}
  12706. +
  12707. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  12708. + loff_t *ppos)
  12709. +{
  12710. + ssize_t err;
  12711. + struct inode *inode;
  12712. + struct file *h_file;
  12713. + struct super_block *sb;
  12714. +
  12715. + inode = file_inode(file);
  12716. + sb = inode->i_sb;
  12717. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12718. +
  12719. + h_file = au_read_pre(file, /*keep_fi*/0);
  12720. + err = PTR_ERR(h_file);
  12721. + if (IS_ERR(h_file))
  12722. + goto out;
  12723. +
  12724. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  12725. + err = vfsub_read_u(h_file, buf, count, ppos);
  12726. + /* todo: necessary? */
  12727. + /* file->f_ra = h_file->f_ra; */
  12728. + au_read_post(inode, h_file);
  12729. +
  12730. +out:
  12731. + si_read_unlock(sb);
  12732. + return err;
  12733. +}
  12734. +
  12735. +/*
  12736. + * todo: very ugly
  12737. + * it locks both of i_mutex and si_rwsem for read in safe.
  12738. + * if the plink maintenance mode continues forever (that is the problem),
  12739. + * may loop forever.
  12740. + */
  12741. +static void au_mtx_and_read_lock(struct inode *inode)
  12742. +{
  12743. + int err;
  12744. + struct super_block *sb = inode->i_sb;
  12745. +
  12746. + while (1) {
  12747. + inode_lock(inode);
  12748. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  12749. + if (!err)
  12750. + break;
  12751. + inode_unlock(inode);
  12752. + si_read_lock(sb, AuLock_NOPLMW);
  12753. + si_read_unlock(sb);
  12754. + }
  12755. +}
  12756. +
  12757. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  12758. + size_t count, loff_t *ppos)
  12759. +{
  12760. + ssize_t err;
  12761. + struct au_write_pre wpre;
  12762. + struct inode *inode;
  12763. + struct file *h_file;
  12764. + char __user *buf = (char __user *)ubuf;
  12765. +
  12766. + inode = file_inode(file);
  12767. + au_mtx_and_read_lock(inode);
  12768. +
  12769. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12770. + err = PTR_ERR(h_file);
  12771. + if (IS_ERR(h_file))
  12772. + goto out;
  12773. +
  12774. + err = vfsub_write_u(h_file, buf, count, ppos);
  12775. + au_write_post(inode, h_file, &wpre, err);
  12776. +
  12777. +out:
  12778. + si_read_unlock(inode->i_sb);
  12779. + inode_unlock(inode);
  12780. + return err;
  12781. +}
  12782. +
  12783. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  12784. + struct iov_iter *iov_iter)
  12785. +{
  12786. + ssize_t err;
  12787. + struct file *file;
  12788. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  12789. +
  12790. + err = security_file_permission(h_file, rw);
  12791. + if (unlikely(err))
  12792. + goto out;
  12793. +
  12794. + err = -ENOSYS;
  12795. + iter = NULL;
  12796. + if (rw == MAY_READ)
  12797. + iter = h_file->f_op->read_iter;
  12798. + else if (rw == MAY_WRITE)
  12799. + iter = h_file->f_op->write_iter;
  12800. +
  12801. + file = kio->ki_filp;
  12802. + kio->ki_filp = h_file;
  12803. + if (iter) {
  12804. + lockdep_off();
  12805. + err = iter(kio, iov_iter);
  12806. + lockdep_on();
  12807. + } else
  12808. + /* currently there is no such fs */
  12809. + WARN_ON_ONCE(1);
  12810. + kio->ki_filp = file;
  12811. +
  12812. +out:
  12813. + return err;
  12814. +}
  12815. +
  12816. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12817. +{
  12818. + ssize_t err;
  12819. + struct file *file, *h_file;
  12820. + struct inode *inode;
  12821. + struct super_block *sb;
  12822. +
  12823. + file = kio->ki_filp;
  12824. + inode = file_inode(file);
  12825. + sb = inode->i_sb;
  12826. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12827. +
  12828. + h_file = au_read_pre(file, /*keep_fi*/1);
  12829. + err = PTR_ERR(h_file);
  12830. + if (IS_ERR(h_file))
  12831. + goto out;
  12832. +
  12833. + if (au_test_loopback_kthread()) {
  12834. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  12835. + if (file->f_mapping != h_file->f_mapping) {
  12836. + file->f_mapping = h_file->f_mapping;
  12837. + smp_mb(); /* unnecessary? */
  12838. + }
  12839. + }
  12840. + fi_read_unlock(file);
  12841. +
  12842. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  12843. + /* todo: necessary? */
  12844. + /* file->f_ra = h_file->f_ra; */
  12845. + au_read_post(inode, h_file);
  12846. +
  12847. +out:
  12848. + si_read_unlock(sb);
  12849. + return err;
  12850. +}
  12851. +
  12852. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12853. +{
  12854. + ssize_t err;
  12855. + struct au_write_pre wpre;
  12856. + struct inode *inode;
  12857. + struct file *file, *h_file;
  12858. +
  12859. + file = kio->ki_filp;
  12860. + inode = file_inode(file);
  12861. + au_mtx_and_read_lock(inode);
  12862. +
  12863. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12864. + err = PTR_ERR(h_file);
  12865. + if (IS_ERR(h_file))
  12866. + goto out;
  12867. +
  12868. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  12869. + au_write_post(inode, h_file, &wpre, err);
  12870. +
  12871. +out:
  12872. + si_read_unlock(inode->i_sb);
  12873. + inode_unlock(inode);
  12874. + return err;
  12875. +}
  12876. +
  12877. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  12878. + struct pipe_inode_info *pipe, size_t len,
  12879. + unsigned int flags)
  12880. +{
  12881. + ssize_t err;
  12882. + struct file *h_file;
  12883. + struct inode *inode;
  12884. + struct super_block *sb;
  12885. +
  12886. + inode = file_inode(file);
  12887. + sb = inode->i_sb;
  12888. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12889. +
  12890. + h_file = au_read_pre(file, /*keep_fi*/0);
  12891. + err = PTR_ERR(h_file);
  12892. + if (IS_ERR(h_file))
  12893. + goto out;
  12894. +
  12895. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  12896. + /* todo: necessasry? */
  12897. + /* file->f_ra = h_file->f_ra; */
  12898. + au_read_post(inode, h_file);
  12899. +
  12900. +out:
  12901. + si_read_unlock(sb);
  12902. + return err;
  12903. +}
  12904. +
  12905. +static ssize_t
  12906. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  12907. + size_t len, unsigned int flags)
  12908. +{
  12909. + ssize_t err;
  12910. + struct au_write_pre wpre;
  12911. + struct inode *inode;
  12912. + struct file *h_file;
  12913. +
  12914. + inode = file_inode(file);
  12915. + au_mtx_and_read_lock(inode);
  12916. +
  12917. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12918. + err = PTR_ERR(h_file);
  12919. + if (IS_ERR(h_file))
  12920. + goto out;
  12921. +
  12922. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  12923. + au_write_post(inode, h_file, &wpre, err);
  12924. +
  12925. +out:
  12926. + si_read_unlock(inode->i_sb);
  12927. + inode_unlock(inode);
  12928. + return err;
  12929. +}
  12930. +
  12931. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  12932. + loff_t len)
  12933. +{
  12934. + long err;
  12935. + struct au_write_pre wpre;
  12936. + struct inode *inode;
  12937. + struct file *h_file;
  12938. +
  12939. + inode = file_inode(file);
  12940. + au_mtx_and_read_lock(inode);
  12941. +
  12942. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12943. + err = PTR_ERR(h_file);
  12944. + if (IS_ERR(h_file))
  12945. + goto out;
  12946. +
  12947. + lockdep_off();
  12948. + err = vfs_fallocate(h_file, mode, offset, len);
  12949. + lockdep_on();
  12950. + au_write_post(inode, h_file, &wpre, /*written*/1);
  12951. +
  12952. +out:
  12953. + si_read_unlock(inode->i_sb);
  12954. + inode_unlock(inode);
  12955. + return err;
  12956. +}
  12957. +
  12958. +/* ---------------------------------------------------------------------- */
  12959. +
  12960. +/*
  12961. + * The locking order around current->mmap_sem.
  12962. + * - in most and regular cases
  12963. + * file I/O syscall -- aufs_read() or something
  12964. + * -- si_rwsem for read -- mmap_sem
  12965. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  12966. + * - in mmap case
  12967. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  12968. + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
  12969. + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
  12970. + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  12971. + * It means that when aufs acquires si_rwsem for write, the process should never
  12972. + * acquire mmap_sem.
  12973. + *
  12974. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  12975. + * problem either since any directory is not able to be mmap-ed.
  12976. + * The similar scenario is applied to aufs_readlink() too.
  12977. + */
  12978. +
  12979. +#if 0 /* stop calling security_file_mmap() */
  12980. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  12981. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  12982. +
  12983. +static unsigned long au_arch_prot_conv(unsigned long flags)
  12984. +{
  12985. + /* currently ppc64 only */
  12986. +#ifdef CONFIG_PPC64
  12987. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  12988. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  12989. + return AuConv_VM_PROT(flags, SAO);
  12990. +#else
  12991. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  12992. + return 0;
  12993. +#endif
  12994. +}
  12995. +
  12996. +static unsigned long au_prot_conv(unsigned long flags)
  12997. +{
  12998. + return AuConv_VM_PROT(flags, READ)
  12999. + | AuConv_VM_PROT(flags, WRITE)
  13000. + | AuConv_VM_PROT(flags, EXEC)
  13001. + | au_arch_prot_conv(flags);
  13002. +}
  13003. +
  13004. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  13005. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  13006. +
  13007. +static unsigned long au_flag_conv(unsigned long flags)
  13008. +{
  13009. + return AuConv_VM_MAP(flags, GROWSDOWN)
  13010. + | AuConv_VM_MAP(flags, DENYWRITE)
  13011. + | AuConv_VM_MAP(flags, LOCKED);
  13012. +}
  13013. +#endif
  13014. +
  13015. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  13016. +{
  13017. + int err;
  13018. + const unsigned char wlock
  13019. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  13020. + struct super_block *sb;
  13021. + struct file *h_file;
  13022. + struct inode *inode;
  13023. +
  13024. + AuDbgVmRegion(file, vma);
  13025. +
  13026. + inode = file_inode(file);
  13027. + sb = inode->i_sb;
  13028. + lockdep_off();
  13029. + si_read_lock(sb, AuLock_NOPLMW);
  13030. +
  13031. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  13032. + lockdep_on();
  13033. + err = PTR_ERR(h_file);
  13034. + if (IS_ERR(h_file))
  13035. + goto out;
  13036. +
  13037. + err = 0;
  13038. + au_set_mmapped(file);
  13039. + au_vm_file_reset(vma, h_file);
  13040. + /*
  13041. + * we cannot call security_mmap_file() here since it may acquire
  13042. + * mmap_sem or i_mutex.
  13043. + *
  13044. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  13045. + * au_flag_conv(vma->vm_flags));
  13046. + */
  13047. + if (!err)
  13048. + err = h_file->f_op->mmap(h_file, vma);
  13049. + if (!err) {
  13050. + au_vm_prfile_set(vma, file);
  13051. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  13052. + goto out_fput; /* success */
  13053. + }
  13054. + au_unset_mmapped(file);
  13055. + au_vm_file_reset(vma, file);
  13056. +
  13057. +out_fput:
  13058. + lockdep_off();
  13059. + ii_write_unlock(inode);
  13060. + lockdep_on();
  13061. + fput(h_file);
  13062. +out:
  13063. + lockdep_off();
  13064. + si_read_unlock(sb);
  13065. + lockdep_on();
  13066. + AuTraceErr(err);
  13067. + return err;
  13068. +}
  13069. +
  13070. +/* ---------------------------------------------------------------------- */
  13071. +
  13072. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  13073. + int datasync)
  13074. +{
  13075. + int err;
  13076. + struct au_write_pre wpre;
  13077. + struct inode *inode;
  13078. + struct file *h_file;
  13079. +
  13080. + err = 0; /* -EBADF; */ /* posix? */
  13081. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  13082. + goto out;
  13083. +
  13084. + inode = file_inode(file);
  13085. + au_mtx_and_read_lock(inode);
  13086. +
  13087. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  13088. + err = PTR_ERR(h_file);
  13089. + if (IS_ERR(h_file))
  13090. + goto out_unlock;
  13091. +
  13092. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  13093. + au_write_post(inode, h_file, &wpre, /*written*/0);
  13094. +
  13095. +out_unlock:
  13096. + si_read_unlock(inode->i_sb);
  13097. + inode_unlock(inode);
  13098. +out:
  13099. + return err;
  13100. +}
  13101. +
  13102. +/* no one supports this operation, currently */
  13103. +#if 0
  13104. +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
  13105. +{
  13106. + int err;
  13107. + struct au_write_pre wpre;
  13108. + struct inode *inode, *h_inode;
  13109. + struct file *file, *h_file;
  13110. +
  13111. + err = 0; /* -EBADF; */ /* posix? */
  13112. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  13113. + goto out;
  13114. +
  13115. + file = kio->ki_filp;
  13116. + inode = file_inode(file);
  13117. + au_mtx_and_read_lock(inode);
  13118. +
  13119. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  13120. + err = PTR_ERR(h_file);
  13121. + if (IS_ERR(h_file))
  13122. + goto out_unlock;
  13123. +
  13124. + err = -ENOSYS;
  13125. + h_file = au_hf_top(file);
  13126. + if (h_file->f_op->aio_fsync) {
  13127. + h_inode = file_inode(h_file);
  13128. + if (!is_sync_kiocb(kio)) {
  13129. + get_file(h_file);
  13130. + fput(file);
  13131. + }
  13132. + kio->ki_filp = h_file;
  13133. + err = h_file->f_op->aio_fsync(kio, datasync);
  13134. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  13135. + if (!err)
  13136. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
  13137. + /*ignore*/
  13138. + inode_unlock(h_inode);
  13139. + }
  13140. + au_write_post(inode, h_file, &wpre, /*written*/0);
  13141. +
  13142. +out_unlock:
  13143. + si_read_unlock(inode->sb);
  13144. + inode_unlock(inode);
  13145. +out:
  13146. + return err;
  13147. +}
  13148. +#endif
  13149. +
  13150. +static int aufs_fasync(int fd, struct file *file, int flag)
  13151. +{
  13152. + int err;
  13153. + struct file *h_file;
  13154. + struct super_block *sb;
  13155. +
  13156. + sb = file->f_path.dentry->d_sb;
  13157. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  13158. +
  13159. + h_file = au_read_pre(file, /*keep_fi*/0);
  13160. + err = PTR_ERR(h_file);
  13161. + if (IS_ERR(h_file))
  13162. + goto out;
  13163. +
  13164. + if (h_file->f_op->fasync)
  13165. + err = h_file->f_op->fasync(fd, h_file, flag);
  13166. + fput(h_file); /* instead of au_read_post() */
  13167. +
  13168. +out:
  13169. + si_read_unlock(sb);
  13170. + return err;
  13171. +}
  13172. +
  13173. +static int aufs_setfl(struct file *file, unsigned long arg)
  13174. +{
  13175. + int err;
  13176. + struct file *h_file;
  13177. + struct super_block *sb;
  13178. +
  13179. + sb = file->f_path.dentry->d_sb;
  13180. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  13181. +
  13182. + h_file = au_read_pre(file, /*keep_fi*/0);
  13183. + err = PTR_ERR(h_file);
  13184. + if (IS_ERR(h_file))
  13185. + goto out;
  13186. +
  13187. + arg |= vfsub_file_flags(file) & FASYNC; /* stop calling h_file->fasync */
  13188. + err = setfl(/*unused fd*/-1, h_file, arg);
  13189. + fput(h_file); /* instead of au_read_post() */
  13190. +
  13191. +out:
  13192. + si_read_unlock(sb);
  13193. + return err;
  13194. +}
  13195. +
  13196. +/* ---------------------------------------------------------------------- */
  13197. +
  13198. +/* no one supports this operation, currently */
  13199. +#if 0
  13200. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  13201. + size_t len, loff_t *pos, int more)
  13202. +{
  13203. +}
  13204. +#endif
  13205. +
  13206. +/* ---------------------------------------------------------------------- */
  13207. +
  13208. +const struct file_operations aufs_file_fop = {
  13209. + .owner = THIS_MODULE,
  13210. +
  13211. + .llseek = default_llseek,
  13212. +
  13213. + .read = aufs_read,
  13214. + .write = aufs_write,
  13215. + .read_iter = aufs_read_iter,
  13216. + .write_iter = aufs_write_iter,
  13217. +
  13218. +#ifdef CONFIG_AUFS_POLL
  13219. + .poll = aufs_poll,
  13220. +#endif
  13221. + .unlocked_ioctl = aufs_ioctl_nondir,
  13222. +#ifdef CONFIG_COMPAT
  13223. + .compat_ioctl = aufs_compat_ioctl_nondir,
  13224. +#endif
  13225. + .mmap = aufs_mmap,
  13226. + .open = aufs_open_nondir,
  13227. + .flush = aufs_flush_nondir,
  13228. + .release = aufs_release_nondir,
  13229. + .fsync = aufs_fsync_nondir,
  13230. + /* .aio_fsync = aufs_aio_fsync_nondir, */
  13231. + .fasync = aufs_fasync,
  13232. + /* .sendpage = aufs_sendpage, */
  13233. + .setfl = aufs_setfl,
  13234. + .splice_write = aufs_splice_write,
  13235. + .splice_read = aufs_splice_read,
  13236. +#if 0
  13237. + .aio_splice_write = aufs_aio_splice_write,
  13238. + .aio_splice_read = aufs_aio_splice_read,
  13239. +#endif
  13240. + .fallocate = aufs_fallocate
  13241. +};
  13242. diff -Naur null/fs/aufs/fstype.h linux-4.7/fs/aufs/fstype.h
  13243. --- /dev/null
  13244. +++ linux-4.7/fs/aufs/fstype.h 2016-08-08 12:57:44.601384697 -0400
  13245. @@ -0,0 +1,400 @@
  13246. +/*
  13247. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  13248. + *
  13249. + * This program, aufs is free software; you can redistribute it and/or modify
  13250. + * it under the terms of the GNU General Public License as published by
  13251. + * the Free Software Foundation; either version 2 of the License, or
  13252. + * (at your option) any later version.
  13253. + *
  13254. + * This program is distributed in the hope that it will be useful,
  13255. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13256. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13257. + * GNU General Public License for more details.
  13258. + *
  13259. + * You should have received a copy of the GNU General Public License
  13260. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13261. + */
  13262. +
  13263. +/*
  13264. + * judging filesystem type
  13265. + */
  13266. +
  13267. +#ifndef __AUFS_FSTYPE_H__
  13268. +#define __AUFS_FSTYPE_H__
  13269. +
  13270. +#ifdef __KERNEL__
  13271. +
  13272. +#include <linux/fs.h>
  13273. +#include <linux/magic.h>
  13274. +#include <linux/nfs_fs.h>
  13275. +#include <linux/romfs_fs.h>
  13276. +
  13277. +static inline int au_test_aufs(struct super_block *sb)
  13278. +{
  13279. + return sb->s_magic == AUFS_SUPER_MAGIC;
  13280. +}
  13281. +
  13282. +static inline const char *au_sbtype(struct super_block *sb)
  13283. +{
  13284. + return sb->s_type->name;
  13285. +}
  13286. +
  13287. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  13288. +{
  13289. +#if IS_ENABLED(CONFIG_ISO9660_FS)
  13290. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  13291. +#else
  13292. + return 0;
  13293. +#endif
  13294. +}
  13295. +
  13296. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  13297. +{
  13298. +#if IS_ENABLED(CONFIG_ROMFS_FS)
  13299. + return sb->s_magic == ROMFS_MAGIC;
  13300. +#else
  13301. + return 0;
  13302. +#endif
  13303. +}
  13304. +
  13305. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  13306. +{
  13307. +#if IS_ENABLED(CONFIG_CRAMFS)
  13308. + return sb->s_magic == CRAMFS_MAGIC;
  13309. +#endif
  13310. + return 0;
  13311. +}
  13312. +
  13313. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  13314. +{
  13315. +#if IS_ENABLED(CONFIG_NFS_FS)
  13316. + return sb->s_magic == NFS_SUPER_MAGIC;
  13317. +#else
  13318. + return 0;
  13319. +#endif
  13320. +}
  13321. +
  13322. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  13323. +{
  13324. +#if IS_ENABLED(CONFIG_FUSE_FS)
  13325. + return sb->s_magic == FUSE_SUPER_MAGIC;
  13326. +#else
  13327. + return 0;
  13328. +#endif
  13329. +}
  13330. +
  13331. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  13332. +{
  13333. +#if IS_ENABLED(CONFIG_XFS_FS)
  13334. + return sb->s_magic == XFS_SB_MAGIC;
  13335. +#else
  13336. + return 0;
  13337. +#endif
  13338. +}
  13339. +
  13340. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  13341. +{
  13342. +#ifdef CONFIG_TMPFS
  13343. + return sb->s_magic == TMPFS_MAGIC;
  13344. +#else
  13345. + return 0;
  13346. +#endif
  13347. +}
  13348. +
  13349. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  13350. +{
  13351. +#if IS_ENABLED(CONFIG_ECRYPT_FS)
  13352. + return !strcmp(au_sbtype(sb), "ecryptfs");
  13353. +#else
  13354. + return 0;
  13355. +#endif
  13356. +}
  13357. +
  13358. +static inline int au_test_ramfs(struct super_block *sb)
  13359. +{
  13360. + return sb->s_magic == RAMFS_MAGIC;
  13361. +}
  13362. +
  13363. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  13364. +{
  13365. +#if IS_ENABLED(CONFIG_UBIFS_FS)
  13366. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  13367. +#else
  13368. + return 0;
  13369. +#endif
  13370. +}
  13371. +
  13372. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  13373. +{
  13374. +#ifdef CONFIG_PROC_FS
  13375. + return sb->s_magic == PROC_SUPER_MAGIC;
  13376. +#else
  13377. + return 0;
  13378. +#endif
  13379. +}
  13380. +
  13381. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  13382. +{
  13383. +#ifdef CONFIG_SYSFS
  13384. + return sb->s_magic == SYSFS_MAGIC;
  13385. +#else
  13386. + return 0;
  13387. +#endif
  13388. +}
  13389. +
  13390. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  13391. +{
  13392. +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
  13393. + return sb->s_magic == CONFIGFS_MAGIC;
  13394. +#else
  13395. + return 0;
  13396. +#endif
  13397. +}
  13398. +
  13399. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  13400. +{
  13401. +#if IS_ENABLED(CONFIG_MINIX_FS)
  13402. + return sb->s_magic == MINIX3_SUPER_MAGIC
  13403. + || sb->s_magic == MINIX2_SUPER_MAGIC
  13404. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  13405. + || sb->s_magic == MINIX_SUPER_MAGIC
  13406. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  13407. +#else
  13408. + return 0;
  13409. +#endif
  13410. +}
  13411. +
  13412. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  13413. +{
  13414. +#if IS_ENABLED(CONFIG_FAT_FS)
  13415. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  13416. +#else
  13417. + return 0;
  13418. +#endif
  13419. +}
  13420. +
  13421. +static inline int au_test_msdos(struct super_block *sb)
  13422. +{
  13423. + return au_test_fat(sb);
  13424. +}
  13425. +
  13426. +static inline int au_test_vfat(struct super_block *sb)
  13427. +{
  13428. + return au_test_fat(sb);
  13429. +}
  13430. +
  13431. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  13432. +{
  13433. +#ifdef CONFIG_SECURITYFS
  13434. + return sb->s_magic == SECURITYFS_MAGIC;
  13435. +#else
  13436. + return 0;
  13437. +#endif
  13438. +}
  13439. +
  13440. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  13441. +{
  13442. +#if IS_ENABLED(CONFIG_SQUASHFS)
  13443. + return sb->s_magic == SQUASHFS_MAGIC;
  13444. +#else
  13445. + return 0;
  13446. +#endif
  13447. +}
  13448. +
  13449. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  13450. +{
  13451. +#if IS_ENABLED(CONFIG_BTRFS_FS)
  13452. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  13453. +#else
  13454. + return 0;
  13455. +#endif
  13456. +}
  13457. +
  13458. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  13459. +{
  13460. +#if IS_ENABLED(CONFIG_XENFS)
  13461. + return sb->s_magic == XENFS_SUPER_MAGIC;
  13462. +#else
  13463. + return 0;
  13464. +#endif
  13465. +}
  13466. +
  13467. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  13468. +{
  13469. +#ifdef CONFIG_DEBUG_FS
  13470. + return sb->s_magic == DEBUGFS_MAGIC;
  13471. +#else
  13472. + return 0;
  13473. +#endif
  13474. +}
  13475. +
  13476. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  13477. +{
  13478. +#if IS_ENABLED(CONFIG_NILFS)
  13479. + return sb->s_magic == NILFS_SUPER_MAGIC;
  13480. +#else
  13481. + return 0;
  13482. +#endif
  13483. +}
  13484. +
  13485. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  13486. +{
  13487. +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
  13488. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  13489. +#else
  13490. + return 0;
  13491. +#endif
  13492. +}
  13493. +
  13494. +/* ---------------------------------------------------------------------- */
  13495. +/*
  13496. + * they can't be an aufs branch.
  13497. + */
  13498. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  13499. +{
  13500. + return
  13501. +#ifndef CONFIG_AUFS_BR_RAMFS
  13502. + au_test_ramfs(sb) ||
  13503. +#endif
  13504. + au_test_procfs(sb)
  13505. + || au_test_sysfs(sb)
  13506. + || au_test_configfs(sb)
  13507. + || au_test_debugfs(sb)
  13508. + || au_test_securityfs(sb)
  13509. + || au_test_xenfs(sb)
  13510. + || au_test_ecryptfs(sb)
  13511. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  13512. + || au_test_aufs(sb); /* will be supported in next version */
  13513. +}
  13514. +
  13515. +static inline int au_test_fs_remote(struct super_block *sb)
  13516. +{
  13517. + return !au_test_tmpfs(sb)
  13518. +#ifdef CONFIG_AUFS_BR_RAMFS
  13519. + && !au_test_ramfs(sb)
  13520. +#endif
  13521. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  13522. +}
  13523. +
  13524. +/* ---------------------------------------------------------------------- */
  13525. +
  13526. +/*
  13527. + * Note: these functions (below) are created after reading ->getattr() in all
  13528. + * filesystems under linux/fs. it means we have to do so in every update...
  13529. + */
  13530. +
  13531. +/*
  13532. + * some filesystems require getattr to refresh the inode attributes before
  13533. + * referencing.
  13534. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  13535. + * and leave the work for d_revalidate()
  13536. + */
  13537. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  13538. +{
  13539. + return au_test_nfs(sb)
  13540. + || au_test_fuse(sb)
  13541. + /* || au_test_btrfs(sb) */ /* untested */
  13542. + ;
  13543. +}
  13544. +
  13545. +/*
  13546. + * filesystems which don't maintain i_size or i_blocks.
  13547. + */
  13548. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  13549. +{
  13550. + return au_test_xfs(sb)
  13551. + || au_test_btrfs(sb)
  13552. + || au_test_ubifs(sb)
  13553. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  13554. + /* || au_test_minix(sb) */ /* untested */
  13555. + ;
  13556. +}
  13557. +
  13558. +/*
  13559. + * filesystems which don't store the correct value in some of their inode
  13560. + * attributes.
  13561. + */
  13562. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  13563. +{
  13564. + return au_test_fs_bad_iattr_size(sb)
  13565. + || au_test_fat(sb)
  13566. + || au_test_msdos(sb)
  13567. + || au_test_vfat(sb);
  13568. +}
  13569. +
  13570. +/* they don't check i_nlink in link(2) */
  13571. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  13572. +{
  13573. + return au_test_tmpfs(sb)
  13574. +#ifdef CONFIG_AUFS_BR_RAMFS
  13575. + || au_test_ramfs(sb)
  13576. +#endif
  13577. + || au_test_ubifs(sb)
  13578. + || au_test_hfsplus(sb);
  13579. +}
  13580. +
  13581. +/*
  13582. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  13583. + */
  13584. +static inline int au_test_fs_notime(struct super_block *sb)
  13585. +{
  13586. + return au_test_nfs(sb)
  13587. + || au_test_fuse(sb)
  13588. + || au_test_ubifs(sb)
  13589. + ;
  13590. +}
  13591. +
  13592. +/* temporary support for i#1 in cramfs */
  13593. +static inline int au_test_fs_unique_ino(struct inode *inode)
  13594. +{
  13595. + if (au_test_cramfs(inode->i_sb))
  13596. + return inode->i_ino != 1;
  13597. + return 1;
  13598. +}
  13599. +
  13600. +/* ---------------------------------------------------------------------- */
  13601. +
  13602. +/*
  13603. + * the filesystem where the xino files placed must support i/o after unlink and
  13604. + * maintain i_size and i_blocks.
  13605. + */
  13606. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  13607. +{
  13608. + return au_test_fs_remote(sb)
  13609. + || au_test_fs_bad_iattr_size(sb)
  13610. + /* don't want unnecessary work for xino */
  13611. + || au_test_aufs(sb)
  13612. + || au_test_ecryptfs(sb)
  13613. + || au_test_nilfs(sb);
  13614. +}
  13615. +
  13616. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  13617. +{
  13618. + return au_test_tmpfs(sb)
  13619. + || au_test_ramfs(sb);
  13620. +}
  13621. +
  13622. +/*
  13623. + * test if the @sb is real-readonly.
  13624. + */
  13625. +static inline int au_test_fs_rr(struct super_block *sb)
  13626. +{
  13627. + return au_test_squashfs(sb)
  13628. + || au_test_iso9660(sb)
  13629. + || au_test_cramfs(sb)
  13630. + || au_test_romfs(sb);
  13631. +}
  13632. +
  13633. +/*
  13634. + * test if the @inode is nfs with 'noacl' option
  13635. + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
  13636. + */
  13637. +static inline int au_test_nfs_noacl(struct inode *inode)
  13638. +{
  13639. + return au_test_nfs(inode->i_sb)
  13640. + /* && IS_POSIXACL(inode) */
  13641. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  13642. +}
  13643. +
  13644. +#endif /* __KERNEL__ */
  13645. +#endif /* __AUFS_FSTYPE_H__ */
  13646. diff -Naur null/fs/aufs/hfsnotify.c linux-4.7/fs/aufs/hfsnotify.c
  13647. --- /dev/null
  13648. +++ linux-4.7/fs/aufs/hfsnotify.c 2016-08-08 12:57:44.602384674 -0400
  13649. @@ -0,0 +1,287 @@
  13650. +/*
  13651. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  13652. + *
  13653. + * This program, aufs is free software; you can redistribute it and/or modify
  13654. + * it under the terms of the GNU General Public License as published by
  13655. + * the Free Software Foundation; either version 2 of the License, or
  13656. + * (at your option) any later version.
  13657. + *
  13658. + * This program is distributed in the hope that it will be useful,
  13659. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13660. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13661. + * GNU General Public License for more details.
  13662. + *
  13663. + * You should have received a copy of the GNU General Public License
  13664. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13665. + */
  13666. +
  13667. +/*
  13668. + * fsnotify for the lower directories
  13669. + */
  13670. +
  13671. +#include "aufs.h"
  13672. +
  13673. +/* FS_IN_IGNORED is unnecessary */
  13674. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  13675. + | FS_CREATE | FS_EVENT_ON_CHILD);
  13676. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  13677. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  13678. +
  13679. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  13680. +{
  13681. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  13682. + hn_mark);
  13683. + /* AuDbg("here\n"); */
  13684. + au_cache_dfree_hnotify(hn);
  13685. + smp_mb__before_atomic();
  13686. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  13687. + wake_up(&au_hfsn_wq);
  13688. +}
  13689. +
  13690. +static int au_hfsn_alloc(struct au_hinode *hinode)
  13691. +{
  13692. + int err;
  13693. + struct au_hnotify *hn;
  13694. + struct super_block *sb;
  13695. + struct au_branch *br;
  13696. + struct fsnotify_mark *mark;
  13697. + aufs_bindex_t bindex;
  13698. +
  13699. + hn = hinode->hi_notify;
  13700. + sb = hn->hn_aufs_inode->i_sb;
  13701. + bindex = au_br_index(sb, hinode->hi_id);
  13702. + br = au_sbr(sb, bindex);
  13703. + AuDebugOn(!br->br_hfsn);
  13704. +
  13705. + mark = &hn->hn_mark;
  13706. + fsnotify_init_mark(mark, au_hfsn_free_mark);
  13707. + mark->mask = AuHfsnMask;
  13708. + /*
  13709. + * by udba rename or rmdir, aufs assign a new inode to the known
  13710. + * h_inode, so specify 1 to allow dups.
  13711. + */
  13712. + lockdep_off();
  13713. + err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
  13714. + /*mnt*/NULL, /*allow_dups*/1);
  13715. + lockdep_on();
  13716. +
  13717. + return err;
  13718. +}
  13719. +
  13720. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  13721. +{
  13722. + struct fsnotify_mark *mark;
  13723. + unsigned long long ull;
  13724. + struct fsnotify_group *group;
  13725. +
  13726. + ull = atomic64_inc_return(&au_hfsn_ifree);
  13727. + BUG_ON(!ull);
  13728. +
  13729. + mark = &hn->hn_mark;
  13730. + spin_lock(&mark->lock);
  13731. + group = mark->group;
  13732. + fsnotify_get_group(group);
  13733. + spin_unlock(&mark->lock);
  13734. + lockdep_off();
  13735. + fsnotify_destroy_mark(mark, group);
  13736. + fsnotify_put_mark(mark);
  13737. + fsnotify_put_group(group);
  13738. + lockdep_on();
  13739. +
  13740. + /* free hn by myself */
  13741. + return 0;
  13742. +}
  13743. +
  13744. +/* ---------------------------------------------------------------------- */
  13745. +
  13746. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  13747. +{
  13748. + struct fsnotify_mark *mark;
  13749. +
  13750. + mark = &hinode->hi_notify->hn_mark;
  13751. + spin_lock(&mark->lock);
  13752. + if (do_set) {
  13753. + AuDebugOn(mark->mask & AuHfsnMask);
  13754. + mark->mask |= AuHfsnMask;
  13755. + } else {
  13756. + AuDebugOn(!(mark->mask & AuHfsnMask));
  13757. + mark->mask &= ~AuHfsnMask;
  13758. + }
  13759. + spin_unlock(&mark->lock);
  13760. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  13761. +}
  13762. +
  13763. +/* ---------------------------------------------------------------------- */
  13764. +
  13765. +/* #define AuDbgHnotify */
  13766. +#ifdef AuDbgHnotify
  13767. +static char *au_hfsn_name(u32 mask)
  13768. +{
  13769. +#ifdef CONFIG_AUFS_DEBUG
  13770. +#define test_ret(flag) \
  13771. + do { \
  13772. + if (mask & flag) \
  13773. + return #flag; \
  13774. + } while (0)
  13775. + test_ret(FS_ACCESS);
  13776. + test_ret(FS_MODIFY);
  13777. + test_ret(FS_ATTRIB);
  13778. + test_ret(FS_CLOSE_WRITE);
  13779. + test_ret(FS_CLOSE_NOWRITE);
  13780. + test_ret(FS_OPEN);
  13781. + test_ret(FS_MOVED_FROM);
  13782. + test_ret(FS_MOVED_TO);
  13783. + test_ret(FS_CREATE);
  13784. + test_ret(FS_DELETE);
  13785. + test_ret(FS_DELETE_SELF);
  13786. + test_ret(FS_MOVE_SELF);
  13787. + test_ret(FS_UNMOUNT);
  13788. + test_ret(FS_Q_OVERFLOW);
  13789. + test_ret(FS_IN_IGNORED);
  13790. + test_ret(FS_ISDIR);
  13791. + test_ret(FS_IN_ONESHOT);
  13792. + test_ret(FS_EVENT_ON_CHILD);
  13793. + return "";
  13794. +#undef test_ret
  13795. +#else
  13796. + return "??";
  13797. +#endif
  13798. +}
  13799. +#endif
  13800. +
  13801. +/* ---------------------------------------------------------------------- */
  13802. +
  13803. +static void au_hfsn_free_group(struct fsnotify_group *group)
  13804. +{
  13805. + struct au_br_hfsnotify *hfsn = group->private;
  13806. +
  13807. + /* AuDbg("here\n"); */
  13808. + au_delayed_kfree(hfsn);
  13809. +}
  13810. +
  13811. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  13812. + struct inode *inode,
  13813. + struct fsnotify_mark *inode_mark,
  13814. + struct fsnotify_mark *vfsmount_mark,
  13815. + u32 mask, void *data, int data_type,
  13816. + const unsigned char *file_name, u32 cookie)
  13817. +{
  13818. + int err;
  13819. + struct au_hnotify *hnotify;
  13820. + struct inode *h_dir, *h_inode;
  13821. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  13822. +
  13823. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  13824. +
  13825. + err = 0;
  13826. + /* if FS_UNMOUNT happens, there must be another bug */
  13827. + AuDebugOn(mask & FS_UNMOUNT);
  13828. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  13829. + goto out;
  13830. +
  13831. + h_dir = inode;
  13832. + h_inode = NULL;
  13833. +#ifdef AuDbgHnotify
  13834. + au_debug_on();
  13835. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  13836. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  13837. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  13838. + h_dir->i_ino, mask, au_hfsn_name(mask),
  13839. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  13840. + /* WARN_ON(1); */
  13841. + }
  13842. + au_debug_off();
  13843. +#endif
  13844. +
  13845. + AuDebugOn(!inode_mark);
  13846. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  13847. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  13848. +
  13849. +out:
  13850. + return err;
  13851. +}
  13852. +
  13853. +static struct fsnotify_ops au_hfsn_ops = {
  13854. + .handle_event = au_hfsn_handle_event,
  13855. + .free_group_priv = au_hfsn_free_group
  13856. +};
  13857. +
  13858. +/* ---------------------------------------------------------------------- */
  13859. +
  13860. +static void au_hfsn_fin_br(struct au_branch *br)
  13861. +{
  13862. + struct au_br_hfsnotify *hfsn;
  13863. +
  13864. + hfsn = br->br_hfsn;
  13865. + if (hfsn) {
  13866. + lockdep_off();
  13867. + fsnotify_put_group(hfsn->hfsn_group);
  13868. + lockdep_on();
  13869. + }
  13870. +}
  13871. +
  13872. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  13873. +{
  13874. + int err;
  13875. + struct fsnotify_group *group;
  13876. + struct au_br_hfsnotify *hfsn;
  13877. +
  13878. + err = 0;
  13879. + br->br_hfsn = NULL;
  13880. + if (!au_br_hnotifyable(perm))
  13881. + goto out;
  13882. +
  13883. + err = -ENOMEM;
  13884. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  13885. + if (unlikely(!hfsn))
  13886. + goto out;
  13887. +
  13888. + err = 0;
  13889. + group = fsnotify_alloc_group(&au_hfsn_ops);
  13890. + if (IS_ERR(group)) {
  13891. + err = PTR_ERR(group);
  13892. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  13893. + goto out_hfsn;
  13894. + }
  13895. +
  13896. + group->private = hfsn;
  13897. + hfsn->hfsn_group = group;
  13898. + br->br_hfsn = hfsn;
  13899. + goto out; /* success */
  13900. +
  13901. +out_hfsn:
  13902. + au_delayed_kfree(hfsn);
  13903. +out:
  13904. + return err;
  13905. +}
  13906. +
  13907. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  13908. +{
  13909. + int err;
  13910. +
  13911. + err = 0;
  13912. + if (!br->br_hfsn)
  13913. + err = au_hfsn_init_br(br, perm);
  13914. +
  13915. + return err;
  13916. +}
  13917. +
  13918. +/* ---------------------------------------------------------------------- */
  13919. +
  13920. +static void au_hfsn_fin(void)
  13921. +{
  13922. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  13923. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  13924. +}
  13925. +
  13926. +const struct au_hnotify_op au_hnotify_op = {
  13927. + .ctl = au_hfsn_ctl,
  13928. + .alloc = au_hfsn_alloc,
  13929. + .free = au_hfsn_free,
  13930. +
  13931. + .fin = au_hfsn_fin,
  13932. +
  13933. + .reset_br = au_hfsn_reset_br,
  13934. + .fin_br = au_hfsn_fin_br,
  13935. + .init_br = au_hfsn_init_br
  13936. +};
  13937. diff -Naur null/fs/aufs/hfsplus.c linux-4.7/fs/aufs/hfsplus.c
  13938. --- /dev/null
  13939. +++ linux-4.7/fs/aufs/hfsplus.c 2016-08-08 12:57:44.602384674 -0400
  13940. @@ -0,0 +1,56 @@
  13941. +/*
  13942. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  13943. + *
  13944. + * This program, aufs is free software; you can redistribute it and/or modify
  13945. + * it under the terms of the GNU General Public License as published by
  13946. + * the Free Software Foundation; either version 2 of the License, or
  13947. + * (at your option) any later version.
  13948. + *
  13949. + * This program is distributed in the hope that it will be useful,
  13950. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13951. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13952. + * GNU General Public License for more details.
  13953. + *
  13954. + * You should have received a copy of the GNU General Public License
  13955. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13956. + */
  13957. +
  13958. +/*
  13959. + * special support for filesystems which aqucires an inode mutex
  13960. + * at final closing a file, eg, hfsplus.
  13961. + *
  13962. + * This trick is very simple and stupid, just to open the file before really
  13963. + * neceeary open to tell hfsplus that this is not the final closing.
  13964. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  13965. + * and au_h_open_post() after releasing it.
  13966. + */
  13967. +
  13968. +#include "aufs.h"
  13969. +
  13970. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  13971. + int force_wr)
  13972. +{
  13973. + struct file *h_file;
  13974. + struct dentry *h_dentry;
  13975. +
  13976. + h_dentry = au_h_dptr(dentry, bindex);
  13977. + AuDebugOn(!h_dentry);
  13978. + AuDebugOn(d_is_negative(h_dentry));
  13979. +
  13980. + h_file = NULL;
  13981. + if (au_test_hfsplus(h_dentry->d_sb)
  13982. + && d_is_reg(h_dentry))
  13983. + h_file = au_h_open(dentry, bindex,
  13984. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  13985. + /*file*/NULL, force_wr);
  13986. + return h_file;
  13987. +}
  13988. +
  13989. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  13990. + struct file *h_file)
  13991. +{
  13992. + if (h_file) {
  13993. + fput(h_file);
  13994. + au_sbr_put(dentry->d_sb, bindex);
  13995. + }
  13996. +}
  13997. diff -Naur null/fs/aufs/hnotify.c linux-4.7/fs/aufs/hnotify.c
  13998. --- /dev/null
  13999. +++ linux-4.7/fs/aufs/hnotify.c 2016-08-08 12:57:44.602384674 -0400
  14000. @@ -0,0 +1,723 @@
  14001. +/*
  14002. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  14003. + *
  14004. + * This program, aufs is free software; you can redistribute it and/or modify
  14005. + * it under the terms of the GNU General Public License as published by
  14006. + * the Free Software Foundation; either version 2 of the License, or
  14007. + * (at your option) any later version.
  14008. + *
  14009. + * This program is distributed in the hope that it will be useful,
  14010. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14011. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14012. + * GNU General Public License for more details.
  14013. + *
  14014. + * You should have received a copy of the GNU General Public License
  14015. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14016. + */
  14017. +
  14018. +/*
  14019. + * abstraction to notify the direct changes on lower directories
  14020. + */
  14021. +
  14022. +#include "aufs.h"
  14023. +
  14024. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  14025. +{
  14026. + int err;
  14027. + struct au_hnotify *hn;
  14028. +
  14029. + err = -ENOMEM;
  14030. + hn = au_cache_alloc_hnotify();
  14031. + if (hn) {
  14032. + hn->hn_aufs_inode = inode;
  14033. + hinode->hi_notify = hn;
  14034. + err = au_hnotify_op.alloc(hinode);
  14035. + AuTraceErr(err);
  14036. + if (unlikely(err)) {
  14037. + hinode->hi_notify = NULL;
  14038. + au_cache_dfree_hnotify(hn);
  14039. + /*
  14040. + * The upper dir was removed by udba, but the same named
  14041. + * dir left. In this case, aufs assignes a new inode
  14042. + * number and set the monitor again.
  14043. + * For the lower dir, the old monitnor is still left.
  14044. + */
  14045. + if (err == -EEXIST)
  14046. + err = 0;
  14047. + }
  14048. + }
  14049. +
  14050. + AuTraceErr(err);
  14051. + return err;
  14052. +}
  14053. +
  14054. +void au_hn_free(struct au_hinode *hinode)
  14055. +{
  14056. + struct au_hnotify *hn;
  14057. +
  14058. + hn = hinode->hi_notify;
  14059. + if (hn) {
  14060. + hinode->hi_notify = NULL;
  14061. + if (au_hnotify_op.free(hinode, hn))
  14062. + au_cache_dfree_hnotify(hn);
  14063. + }
  14064. +}
  14065. +
  14066. +/* ---------------------------------------------------------------------- */
  14067. +
  14068. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  14069. +{
  14070. + if (hinode->hi_notify)
  14071. + au_hnotify_op.ctl(hinode, do_set);
  14072. +}
  14073. +
  14074. +void au_hn_reset(struct inode *inode, unsigned int flags)
  14075. +{
  14076. + aufs_bindex_t bindex, bbot;
  14077. + struct inode *hi;
  14078. + struct dentry *iwhdentry;
  14079. +
  14080. + bbot = au_ibbot(inode);
  14081. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  14082. + hi = au_h_iptr(inode, bindex);
  14083. + if (!hi)
  14084. + continue;
  14085. +
  14086. + /* inode_lock_nested(hi, AuLsc_I_CHILD); */
  14087. + iwhdentry = au_hi_wh(inode, bindex);
  14088. + if (iwhdentry)
  14089. + dget(iwhdentry);
  14090. + au_igrab(hi);
  14091. + au_set_h_iptr(inode, bindex, NULL, 0);
  14092. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  14093. + flags & ~AuHi_XINO);
  14094. + iput(hi);
  14095. + dput(iwhdentry);
  14096. + /* inode_unlock(hi); */
  14097. + }
  14098. +}
  14099. +
  14100. +/* ---------------------------------------------------------------------- */
  14101. +
  14102. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  14103. +{
  14104. + int err;
  14105. + aufs_bindex_t bindex, bbot, bfound, btop;
  14106. + struct inode *h_i;
  14107. +
  14108. + err = 0;
  14109. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14110. + pr_warn("branch root dir was changed\n");
  14111. + goto out;
  14112. + }
  14113. +
  14114. + bfound = -1;
  14115. + bbot = au_ibbot(inode);
  14116. + btop = au_ibtop(inode);
  14117. +#if 0 /* reserved for future use */
  14118. + if (bindex == bbot) {
  14119. + /* keep this ino in rename case */
  14120. + goto out;
  14121. + }
  14122. +#endif
  14123. + for (bindex = btop; bindex <= bbot; bindex++)
  14124. + if (au_h_iptr(inode, bindex) == h_inode) {
  14125. + bfound = bindex;
  14126. + break;
  14127. + }
  14128. + if (bfound < 0)
  14129. + goto out;
  14130. +
  14131. + for (bindex = btop; bindex <= bbot; bindex++) {
  14132. + h_i = au_h_iptr(inode, bindex);
  14133. + if (!h_i)
  14134. + continue;
  14135. +
  14136. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  14137. + /* ignore this error */
  14138. + /* bad action? */
  14139. + }
  14140. +
  14141. + /* children inode number will be broken */
  14142. +
  14143. +out:
  14144. + AuTraceErr(err);
  14145. + return err;
  14146. +}
  14147. +
  14148. +static int hn_gen_tree(struct dentry *dentry)
  14149. +{
  14150. + int err, i, j, ndentry;
  14151. + struct au_dcsub_pages dpages;
  14152. + struct au_dpage *dpage;
  14153. + struct dentry **dentries;
  14154. +
  14155. + err = au_dpages_init(&dpages, GFP_NOFS);
  14156. + if (unlikely(err))
  14157. + goto out;
  14158. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  14159. + if (unlikely(err))
  14160. + goto out_dpages;
  14161. +
  14162. + for (i = 0; i < dpages.ndpage; i++) {
  14163. + dpage = dpages.dpages + i;
  14164. + dentries = dpage->dentries;
  14165. + ndentry = dpage->ndentry;
  14166. + for (j = 0; j < ndentry; j++) {
  14167. + struct dentry *d;
  14168. +
  14169. + d = dentries[j];
  14170. + if (IS_ROOT(d))
  14171. + continue;
  14172. +
  14173. + au_digen_dec(d);
  14174. + if (d_really_is_positive(d))
  14175. + /* todo: reset children xino?
  14176. + cached children only? */
  14177. + au_iigen_dec(d_inode(d));
  14178. + }
  14179. + }
  14180. +
  14181. +out_dpages:
  14182. + au_dpages_free(&dpages);
  14183. +
  14184. +#if 0
  14185. + /* discard children */
  14186. + dentry_unhash(dentry);
  14187. + dput(dentry);
  14188. +#endif
  14189. +out:
  14190. + return err;
  14191. +}
  14192. +
  14193. +/*
  14194. + * return 0 if processed.
  14195. + */
  14196. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  14197. + const unsigned int isdir)
  14198. +{
  14199. + int err;
  14200. + struct dentry *d;
  14201. + struct qstr *dname;
  14202. +
  14203. + err = 1;
  14204. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14205. + pr_warn("branch root dir was changed\n");
  14206. + err = 0;
  14207. + goto out;
  14208. + }
  14209. +
  14210. + if (!isdir) {
  14211. + AuDebugOn(!name);
  14212. + au_iigen_dec(inode);
  14213. + spin_lock(&inode->i_lock);
  14214. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  14215. + spin_lock(&d->d_lock);
  14216. + dname = &d->d_name;
  14217. + if (dname->len != nlen
  14218. + && memcmp(dname->name, name, nlen)) {
  14219. + spin_unlock(&d->d_lock);
  14220. + continue;
  14221. + }
  14222. + err = 0;
  14223. + au_digen_dec(d);
  14224. + spin_unlock(&d->d_lock);
  14225. + break;
  14226. + }
  14227. + spin_unlock(&inode->i_lock);
  14228. + } else {
  14229. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  14230. + d = d_find_any_alias(inode);
  14231. + if (!d) {
  14232. + au_iigen_dec(inode);
  14233. + goto out;
  14234. + }
  14235. +
  14236. + spin_lock(&d->d_lock);
  14237. + dname = &d->d_name;
  14238. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  14239. + spin_unlock(&d->d_lock);
  14240. + err = hn_gen_tree(d);
  14241. + spin_lock(&d->d_lock);
  14242. + }
  14243. + spin_unlock(&d->d_lock);
  14244. + dput(d);
  14245. + }
  14246. +
  14247. +out:
  14248. + AuTraceErr(err);
  14249. + return err;
  14250. +}
  14251. +
  14252. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  14253. +{
  14254. + int err;
  14255. +
  14256. + if (IS_ROOT(dentry)) {
  14257. + pr_warn("branch root dir was changed\n");
  14258. + return 0;
  14259. + }
  14260. +
  14261. + err = 0;
  14262. + if (!isdir) {
  14263. + au_digen_dec(dentry);
  14264. + if (d_really_is_positive(dentry))
  14265. + au_iigen_dec(d_inode(dentry));
  14266. + } else {
  14267. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  14268. + if (d_really_is_positive(dentry))
  14269. + err = hn_gen_tree(dentry);
  14270. + }
  14271. +
  14272. + AuTraceErr(err);
  14273. + return err;
  14274. +}
  14275. +
  14276. +/* ---------------------------------------------------------------------- */
  14277. +
  14278. +/* hnotify job flags */
  14279. +#define AuHnJob_XINO0 1
  14280. +#define AuHnJob_GEN (1 << 1)
  14281. +#define AuHnJob_DIRENT (1 << 2)
  14282. +#define AuHnJob_ISDIR (1 << 3)
  14283. +#define AuHnJob_TRYXINO0 (1 << 4)
  14284. +#define AuHnJob_MNTPNT (1 << 5)
  14285. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  14286. +#define au_fset_hnjob(flags, name) \
  14287. + do { (flags) |= AuHnJob_##name; } while (0)
  14288. +#define au_fclr_hnjob(flags, name) \
  14289. + do { (flags) &= ~AuHnJob_##name; } while (0)
  14290. +
  14291. +enum {
  14292. + AuHn_CHILD,
  14293. + AuHn_PARENT,
  14294. + AuHnLast
  14295. +};
  14296. +
  14297. +struct au_hnotify_args {
  14298. + struct inode *h_dir, *dir, *h_child_inode;
  14299. + u32 mask;
  14300. + unsigned int flags[AuHnLast];
  14301. + unsigned int h_child_nlen;
  14302. + char h_child_name[];
  14303. +};
  14304. +
  14305. +struct hn_job_args {
  14306. + unsigned int flags;
  14307. + struct inode *inode, *h_inode, *dir, *h_dir;
  14308. + struct dentry *dentry;
  14309. + char *h_name;
  14310. + int h_nlen;
  14311. +};
  14312. +
  14313. +static int hn_job(struct hn_job_args *a)
  14314. +{
  14315. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  14316. + int e;
  14317. +
  14318. + /* reset xino */
  14319. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  14320. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  14321. +
  14322. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  14323. + && a->inode
  14324. + && a->h_inode) {
  14325. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  14326. + if (!a->h_inode->i_nlink
  14327. + && !(a->h_inode->i_state & I_LINKABLE))
  14328. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  14329. + inode_unlock(a->h_inode);
  14330. + }
  14331. +
  14332. + /* make the generation obsolete */
  14333. + if (au_ftest_hnjob(a->flags, GEN)) {
  14334. + e = -1;
  14335. + if (a->inode)
  14336. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  14337. + isdir);
  14338. + if (e && a->dentry)
  14339. + hn_gen_by_name(a->dentry, isdir);
  14340. + /* ignore this error */
  14341. + }
  14342. +
  14343. + /* make dir entries obsolete */
  14344. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  14345. + struct au_vdir *vdir;
  14346. +
  14347. + vdir = au_ivdir(a->inode);
  14348. + if (vdir)
  14349. + vdir->vd_jiffy = 0;
  14350. + /* IMustLock(a->inode); */
  14351. + /* a->inode->i_version++; */
  14352. + }
  14353. +
  14354. + /* can do nothing but warn */
  14355. + if (au_ftest_hnjob(a->flags, MNTPNT)
  14356. + && a->dentry
  14357. + && d_mountpoint(a->dentry))
  14358. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  14359. +
  14360. + return 0;
  14361. +}
  14362. +
  14363. +/* ---------------------------------------------------------------------- */
  14364. +
  14365. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  14366. + struct inode *dir)
  14367. +{
  14368. + struct dentry *dentry, *d, *parent;
  14369. + struct qstr *dname;
  14370. +
  14371. + parent = d_find_any_alias(dir);
  14372. + if (!parent)
  14373. + return NULL;
  14374. +
  14375. + dentry = NULL;
  14376. + spin_lock(&parent->d_lock);
  14377. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  14378. + /* AuDbg("%pd\n", d); */
  14379. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  14380. + dname = &d->d_name;
  14381. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  14382. + goto cont_unlock;
  14383. + if (au_di(d))
  14384. + au_digen_dec(d);
  14385. + else
  14386. + goto cont_unlock;
  14387. + if (au_dcount(d) > 0) {
  14388. + dentry = dget_dlock(d);
  14389. + spin_unlock(&d->d_lock);
  14390. + break;
  14391. + }
  14392. +
  14393. +cont_unlock:
  14394. + spin_unlock(&d->d_lock);
  14395. + }
  14396. + spin_unlock(&parent->d_lock);
  14397. + dput(parent);
  14398. +
  14399. + if (dentry)
  14400. + di_write_lock_child(dentry);
  14401. +
  14402. + return dentry;
  14403. +}
  14404. +
  14405. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  14406. + aufs_bindex_t bindex, ino_t h_ino)
  14407. +{
  14408. + struct inode *inode;
  14409. + ino_t ino;
  14410. + int err;
  14411. +
  14412. + inode = NULL;
  14413. + err = au_xino_read(sb, bindex, h_ino, &ino);
  14414. + if (!err && ino)
  14415. + inode = ilookup(sb, ino);
  14416. + if (!inode)
  14417. + goto out;
  14418. +
  14419. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14420. + pr_warn("wrong root branch\n");
  14421. + iput(inode);
  14422. + inode = NULL;
  14423. + goto out;
  14424. + }
  14425. +
  14426. + ii_write_lock_child(inode);
  14427. +
  14428. +out:
  14429. + return inode;
  14430. +}
  14431. +
  14432. +static void au_hn_bh(void *_args)
  14433. +{
  14434. + struct au_hnotify_args *a = _args;
  14435. + struct super_block *sb;
  14436. + aufs_bindex_t bindex, bbot, bfound;
  14437. + unsigned char xino, try_iput;
  14438. + int err;
  14439. + struct inode *inode;
  14440. + ino_t h_ino;
  14441. + struct hn_job_args args;
  14442. + struct dentry *dentry;
  14443. + struct au_sbinfo *sbinfo;
  14444. +
  14445. + AuDebugOn(!_args);
  14446. + AuDebugOn(!a->h_dir);
  14447. + AuDebugOn(!a->dir);
  14448. + AuDebugOn(!a->mask);
  14449. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  14450. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  14451. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  14452. +
  14453. + inode = NULL;
  14454. + dentry = NULL;
  14455. + /*
  14456. + * do not lock a->dir->i_mutex here
  14457. + * because of d_revalidate() may cause a deadlock.
  14458. + */
  14459. + sb = a->dir->i_sb;
  14460. + AuDebugOn(!sb);
  14461. + sbinfo = au_sbi(sb);
  14462. + AuDebugOn(!sbinfo);
  14463. + si_write_lock(sb, AuLock_NOPLMW);
  14464. +
  14465. + ii_read_lock_parent(a->dir);
  14466. + bfound = -1;
  14467. + bbot = au_ibbot(a->dir);
  14468. + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
  14469. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  14470. + bfound = bindex;
  14471. + break;
  14472. + }
  14473. + ii_read_unlock(a->dir);
  14474. + if (unlikely(bfound < 0))
  14475. + goto out;
  14476. +
  14477. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  14478. + h_ino = 0;
  14479. + if (a->h_child_inode)
  14480. + h_ino = a->h_child_inode->i_ino;
  14481. +
  14482. + if (a->h_child_nlen
  14483. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  14484. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  14485. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  14486. + a->dir);
  14487. + try_iput = 0;
  14488. + if (dentry && d_really_is_positive(dentry))
  14489. + inode = d_inode(dentry);
  14490. + if (xino && !inode && h_ino
  14491. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  14492. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  14493. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  14494. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  14495. + try_iput = 1;
  14496. + }
  14497. +
  14498. + args.flags = a->flags[AuHn_CHILD];
  14499. + args.dentry = dentry;
  14500. + args.inode = inode;
  14501. + args.h_inode = a->h_child_inode;
  14502. + args.dir = a->dir;
  14503. + args.h_dir = a->h_dir;
  14504. + args.h_name = a->h_child_name;
  14505. + args.h_nlen = a->h_child_nlen;
  14506. + err = hn_job(&args);
  14507. + if (dentry) {
  14508. + if (au_di(dentry))
  14509. + di_write_unlock(dentry);
  14510. + dput(dentry);
  14511. + }
  14512. + if (inode && try_iput) {
  14513. + ii_write_unlock(inode);
  14514. + iput(inode);
  14515. + }
  14516. +
  14517. + ii_write_lock_parent(a->dir);
  14518. + args.flags = a->flags[AuHn_PARENT];
  14519. + args.dentry = NULL;
  14520. + args.inode = a->dir;
  14521. + args.h_inode = a->h_dir;
  14522. + args.dir = NULL;
  14523. + args.h_dir = NULL;
  14524. + args.h_name = NULL;
  14525. + args.h_nlen = 0;
  14526. + err = hn_job(&args);
  14527. + ii_write_unlock(a->dir);
  14528. +
  14529. +out:
  14530. + iput(a->h_child_inode);
  14531. + iput(a->h_dir);
  14532. + iput(a->dir);
  14533. + si_write_unlock(sb);
  14534. + au_nwt_done(&sbinfo->si_nowait);
  14535. + au_delayed_kfree(a);
  14536. +}
  14537. +
  14538. +/* ---------------------------------------------------------------------- */
  14539. +
  14540. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  14541. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  14542. +{
  14543. + int err, len;
  14544. + unsigned int flags[AuHnLast], f;
  14545. + unsigned char isdir, isroot, wh;
  14546. + struct inode *dir;
  14547. + struct au_hnotify_args *args;
  14548. + char *p, *h_child_name;
  14549. +
  14550. + err = 0;
  14551. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  14552. + dir = igrab(hnotify->hn_aufs_inode);
  14553. + if (!dir)
  14554. + goto out;
  14555. +
  14556. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  14557. + wh = 0;
  14558. + h_child_name = (void *)h_child_qstr->name;
  14559. + len = h_child_qstr->len;
  14560. + if (h_child_name) {
  14561. + if (len > AUFS_WH_PFX_LEN
  14562. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  14563. + h_child_name += AUFS_WH_PFX_LEN;
  14564. + len -= AUFS_WH_PFX_LEN;
  14565. + wh = 1;
  14566. + }
  14567. + }
  14568. +
  14569. + isdir = 0;
  14570. + if (h_child_inode)
  14571. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  14572. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  14573. + flags[AuHn_CHILD] = 0;
  14574. + if (isdir)
  14575. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  14576. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  14577. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  14578. + switch (mask & FS_EVENTS_POSS_ON_CHILD) {
  14579. + case FS_MOVED_FROM:
  14580. + case FS_MOVED_TO:
  14581. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  14582. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  14583. + /*FALLTHROUGH*/
  14584. + case FS_CREATE:
  14585. + AuDebugOn(!h_child_name);
  14586. + break;
  14587. +
  14588. + case FS_DELETE:
  14589. + /*
  14590. + * aufs never be able to get this child inode.
  14591. + * revalidation should be in d_revalidate()
  14592. + * by checking i_nlink, i_generation or d_unhashed().
  14593. + */
  14594. + AuDebugOn(!h_child_name);
  14595. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  14596. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  14597. + break;
  14598. +
  14599. + default:
  14600. + AuDebugOn(1);
  14601. + }
  14602. +
  14603. + if (wh)
  14604. + h_child_inode = NULL;
  14605. +
  14606. + err = -ENOMEM;
  14607. + /* iput() and kfree() will be called in au_hnotify() */
  14608. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  14609. + if (unlikely(!args)) {
  14610. + AuErr1("no memory\n");
  14611. + iput(dir);
  14612. + goto out;
  14613. + }
  14614. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  14615. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  14616. + args->mask = mask;
  14617. + args->dir = dir;
  14618. + args->h_dir = igrab(h_dir);
  14619. + if (h_child_inode)
  14620. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  14621. + args->h_child_inode = h_child_inode;
  14622. + args->h_child_nlen = len;
  14623. + if (len) {
  14624. + p = (void *)args;
  14625. + p += sizeof(*args);
  14626. + memcpy(p, h_child_name, len);
  14627. + p[len] = 0;
  14628. + }
  14629. +
  14630. + /* NFS fires the event for silly-renamed one from kworker */
  14631. + f = 0;
  14632. + if (!dir->i_nlink
  14633. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  14634. + f = AuWkq_NEST;
  14635. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  14636. + if (unlikely(err)) {
  14637. + pr_err("wkq %d\n", err);
  14638. + iput(args->h_child_inode);
  14639. + iput(args->h_dir);
  14640. + iput(args->dir);
  14641. + au_delayed_kfree(args);
  14642. + }
  14643. +
  14644. +out:
  14645. + return err;
  14646. +}
  14647. +
  14648. +/* ---------------------------------------------------------------------- */
  14649. +
  14650. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  14651. +{
  14652. + int err;
  14653. +
  14654. + AuDebugOn(!(udba & AuOptMask_UDBA));
  14655. +
  14656. + err = 0;
  14657. + if (au_hnotify_op.reset_br)
  14658. + err = au_hnotify_op.reset_br(udba, br, perm);
  14659. +
  14660. + return err;
  14661. +}
  14662. +
  14663. +int au_hnotify_init_br(struct au_branch *br, int perm)
  14664. +{
  14665. + int err;
  14666. +
  14667. + err = 0;
  14668. + if (au_hnotify_op.init_br)
  14669. + err = au_hnotify_op.init_br(br, perm);
  14670. +
  14671. + return err;
  14672. +}
  14673. +
  14674. +void au_hnotify_fin_br(struct au_branch *br)
  14675. +{
  14676. + if (au_hnotify_op.fin_br)
  14677. + au_hnotify_op.fin_br(br);
  14678. +}
  14679. +
  14680. +static void au_hn_destroy_cache(void)
  14681. +{
  14682. + struct au_cache *cp;
  14683. +
  14684. + flush_delayed_work(&au_dfree.dwork);
  14685. + cp = au_dfree.cache + AuCache_HNOTIFY;
  14686. + AuDebugOn(!llist_empty(&cp->llist));
  14687. + kmem_cache_destroy(cp->cache);
  14688. + cp->cache = NULL;
  14689. +}
  14690. +
  14691. +AU_CACHE_DFREE_FUNC(hnotify, HNOTIFY, hn_lnode);
  14692. +
  14693. +int __init au_hnotify_init(void)
  14694. +{
  14695. + int err;
  14696. + struct au_cache *cp;
  14697. +
  14698. + err = -ENOMEM;
  14699. + cp = au_dfree.cache + AuCache_HNOTIFY;
  14700. + cp->cache = AuCache(au_hnotify);
  14701. + if (cp->cache) {
  14702. + err = 0;
  14703. + if (au_hnotify_op.init)
  14704. + err = au_hnotify_op.init();
  14705. + if (unlikely(err))
  14706. + au_hn_destroy_cache();
  14707. + }
  14708. + AuTraceErr(err);
  14709. + return err;
  14710. +}
  14711. +
  14712. +void au_hnotify_fin(void)
  14713. +{
  14714. + struct au_cache *cp;
  14715. +
  14716. + if (au_hnotify_op.fin)
  14717. + au_hnotify_op.fin();
  14718. +
  14719. + /* cf. au_cache_fin() */
  14720. + cp = au_dfree.cache + AuCache_HNOTIFY;
  14721. + if (cp->cache)
  14722. + au_hn_destroy_cache();
  14723. +}
  14724. diff -Naur null/fs/aufs/iinfo.c linux-4.7/fs/aufs/iinfo.c
  14725. --- /dev/null
  14726. +++ linux-4.7/fs/aufs/iinfo.c 2016-08-08 12:57:44.603384652 -0400
  14727. @@ -0,0 +1,284 @@
  14728. +/*
  14729. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  14730. + *
  14731. + * This program, aufs is free software; you can redistribute it and/or modify
  14732. + * it under the terms of the GNU General Public License as published by
  14733. + * the Free Software Foundation; either version 2 of the License, or
  14734. + * (at your option) any later version.
  14735. + *
  14736. + * This program is distributed in the hope that it will be useful,
  14737. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14738. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14739. + * GNU General Public License for more details.
  14740. + *
  14741. + * You should have received a copy of the GNU General Public License
  14742. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14743. + */
  14744. +
  14745. +/*
  14746. + * inode private data
  14747. + */
  14748. +
  14749. +#include "aufs.h"
  14750. +
  14751. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  14752. +{
  14753. + struct inode *h_inode;
  14754. + struct au_hinode *hinode;
  14755. +
  14756. + IiMustAnyLock(inode);
  14757. +
  14758. + hinode = au_hinode(au_ii(inode), bindex);
  14759. + h_inode = hinode->hi_inode;
  14760. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14761. + return h_inode;
  14762. +}
  14763. +
  14764. +/* todo: hard/soft set? */
  14765. +void au_hiput(struct au_hinode *hinode)
  14766. +{
  14767. + au_hn_free(hinode);
  14768. + dput(hinode->hi_whdentry);
  14769. + iput(hinode->hi_inode);
  14770. +}
  14771. +
  14772. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  14773. +{
  14774. + unsigned int flags;
  14775. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  14776. +
  14777. + flags = 0;
  14778. + if (au_opt_test(mnt_flags, XINO))
  14779. + au_fset_hi(flags, XINO);
  14780. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  14781. + au_fset_hi(flags, HNOTIFY);
  14782. + return flags;
  14783. +}
  14784. +
  14785. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  14786. + struct inode *h_inode, unsigned int flags)
  14787. +{
  14788. + struct au_hinode *hinode;
  14789. + struct inode *hi;
  14790. + struct au_iinfo *iinfo = au_ii(inode);
  14791. +
  14792. + IiMustWriteLock(inode);
  14793. +
  14794. + hinode = au_hinode(iinfo, bindex);
  14795. + hi = hinode->hi_inode;
  14796. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14797. +
  14798. + if (hi)
  14799. + au_hiput(hinode);
  14800. + hinode->hi_inode = h_inode;
  14801. + if (h_inode) {
  14802. + int err;
  14803. + struct super_block *sb = inode->i_sb;
  14804. + struct au_branch *br;
  14805. +
  14806. + AuDebugOn(inode->i_mode
  14807. + && (h_inode->i_mode & S_IFMT)
  14808. + != (inode->i_mode & S_IFMT));
  14809. + if (bindex == iinfo->ii_btop)
  14810. + au_cpup_igen(inode, h_inode);
  14811. + br = au_sbr(sb, bindex);
  14812. + hinode->hi_id = br->br_id;
  14813. + if (au_ftest_hi(flags, XINO)) {
  14814. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  14815. + inode->i_ino);
  14816. + if (unlikely(err))
  14817. + AuIOErr1("failed au_xino_write() %d\n", err);
  14818. + }
  14819. +
  14820. + if (au_ftest_hi(flags, HNOTIFY)
  14821. + && au_br_hnotifyable(br->br_perm)) {
  14822. + err = au_hn_alloc(hinode, inode);
  14823. + if (unlikely(err))
  14824. + AuIOErr1("au_hn_alloc() %d\n", err);
  14825. + }
  14826. + }
  14827. +}
  14828. +
  14829. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  14830. + struct dentry *h_wh)
  14831. +{
  14832. + struct au_hinode *hinode;
  14833. +
  14834. + IiMustWriteLock(inode);
  14835. +
  14836. + hinode = au_hinode(au_ii(inode), bindex);
  14837. + AuDebugOn(hinode->hi_whdentry);
  14838. + hinode->hi_whdentry = h_wh;
  14839. +}
  14840. +
  14841. +void au_update_iigen(struct inode *inode, int half)
  14842. +{
  14843. + struct au_iinfo *iinfo;
  14844. + struct au_iigen *iigen;
  14845. + unsigned int sigen;
  14846. +
  14847. + sigen = au_sigen(inode->i_sb);
  14848. + iinfo = au_ii(inode);
  14849. + iigen = &iinfo->ii_generation;
  14850. + spin_lock(&iigen->ig_spin);
  14851. + iigen->ig_generation = sigen;
  14852. + if (half)
  14853. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  14854. + else
  14855. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  14856. + spin_unlock(&iigen->ig_spin);
  14857. +}
  14858. +
  14859. +/* it may be called at remount time, too */
  14860. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  14861. +{
  14862. + struct au_iinfo *iinfo;
  14863. + aufs_bindex_t bindex, bbot;
  14864. +
  14865. + AuDebugOn(au_is_bad_inode(inode));
  14866. + IiMustWriteLock(inode);
  14867. +
  14868. + iinfo = au_ii(inode);
  14869. + if (do_put_zero && iinfo->ii_btop >= 0) {
  14870. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  14871. + bindex++) {
  14872. + struct inode *h_i;
  14873. +
  14874. + h_i = au_hinode(iinfo, bindex)->hi_inode;
  14875. + if (h_i
  14876. + && !h_i->i_nlink
  14877. + && !(h_i->i_state & I_LINKABLE))
  14878. + au_set_h_iptr(inode, bindex, NULL, 0);
  14879. + }
  14880. + }
  14881. +
  14882. + iinfo->ii_btop = -1;
  14883. + iinfo->ii_bbot = -1;
  14884. + bbot = au_sbbot(inode->i_sb);
  14885. + for (bindex = 0; bindex <= bbot; bindex++)
  14886. + if (au_hinode(iinfo, bindex)->hi_inode) {
  14887. + iinfo->ii_btop = bindex;
  14888. + break;
  14889. + }
  14890. + if (iinfo->ii_btop >= 0)
  14891. + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
  14892. + if (au_hinode(iinfo, bindex)->hi_inode) {
  14893. + iinfo->ii_bbot = bindex;
  14894. + break;
  14895. + }
  14896. + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
  14897. +}
  14898. +
  14899. +/* ---------------------------------------------------------------------- */
  14900. +
  14901. +void au_icntnr_init_once(void *_c)
  14902. +{
  14903. + struct au_icntnr *c = _c;
  14904. + struct au_iinfo *iinfo = &c->iinfo;
  14905. +
  14906. + spin_lock_init(&iinfo->ii_generation.ig_spin);
  14907. + au_rw_init(&iinfo->ii_rwsem);
  14908. + inode_init_once(&c->vfs_inode);
  14909. +}
  14910. +
  14911. +void au_hinode_init(struct au_hinode *hinode)
  14912. +{
  14913. + hinode->hi_inode = NULL;
  14914. + hinode->hi_id = -1;
  14915. + au_hn_init(hinode);
  14916. + hinode->hi_whdentry = NULL;
  14917. +}
  14918. +
  14919. +int au_iinfo_init(struct inode *inode)
  14920. +{
  14921. + struct au_iinfo *iinfo;
  14922. + struct super_block *sb;
  14923. + struct au_hinode *hi;
  14924. + int nbr, i;
  14925. +
  14926. + sb = inode->i_sb;
  14927. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  14928. + nbr = au_sbbot(sb) + 1;
  14929. + if (unlikely(nbr <= 0))
  14930. + nbr = 1;
  14931. + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  14932. + if (hi) {
  14933. + au_ninodes_inc(sb);
  14934. +
  14935. + iinfo->ii_hinode = hi;
  14936. + for (i = 0; i < nbr; i++, hi++)
  14937. + au_hinode_init(hi);
  14938. +
  14939. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  14940. + iinfo->ii_btop = -1;
  14941. + iinfo->ii_bbot = -1;
  14942. + iinfo->ii_vdir = NULL;
  14943. + return 0;
  14944. + }
  14945. + return -ENOMEM;
  14946. +}
  14947. +
  14948. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr)
  14949. +{
  14950. + int err, i;
  14951. + struct au_hinode *hip;
  14952. +
  14953. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  14954. +
  14955. + err = -ENOMEM;
  14956. + hip = krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS);
  14957. + if (hip) {
  14958. + iinfo->ii_hinode = hip;
  14959. + i = iinfo->ii_bbot + 1;
  14960. + hip += i;
  14961. + for (; i < nbr; i++, hip++)
  14962. + au_hinode_init(hip);
  14963. + err = 0;
  14964. + }
  14965. +
  14966. + return err;
  14967. +}
  14968. +
  14969. +void au_iinfo_fin(struct inode *inode)
  14970. +{
  14971. + struct au_iinfo *iinfo;
  14972. + struct au_hinode *hi;
  14973. + struct super_block *sb;
  14974. + aufs_bindex_t bindex, bbot;
  14975. + const unsigned char unlinked = !inode->i_nlink;
  14976. +
  14977. + AuDebugOn(au_is_bad_inode(inode));
  14978. +
  14979. + sb = inode->i_sb;
  14980. + au_ninodes_dec(sb);
  14981. + if (si_pid_test(sb))
  14982. + au_xino_delete_inode(inode, unlinked);
  14983. + else {
  14984. + /*
  14985. + * it is safe to hide the dependency between sbinfo and
  14986. + * sb->s_umount.
  14987. + */
  14988. + lockdep_off();
  14989. + si_noflush_read_lock(sb);
  14990. + au_xino_delete_inode(inode, unlinked);
  14991. + si_read_unlock(sb);
  14992. + lockdep_on();
  14993. + }
  14994. +
  14995. + iinfo = au_ii(inode);
  14996. + if (iinfo->ii_vdir)
  14997. + au_vdir_free(iinfo->ii_vdir, /*atonce*/0);
  14998. +
  14999. + bindex = iinfo->ii_btop;
  15000. + if (bindex >= 0) {
  15001. + hi = au_hinode(iinfo, bindex);
  15002. + bbot = iinfo->ii_bbot;
  15003. + while (bindex++ <= bbot) {
  15004. + if (hi->hi_inode)
  15005. + au_hiput(hi);
  15006. + hi++;
  15007. + }
  15008. + }
  15009. + au_delayed_kfree(iinfo->ii_hinode);
  15010. + AuRwDestroy(&iinfo->ii_rwsem);
  15011. +}
  15012. diff -Naur null/fs/aufs/inode.c linux-4.7/fs/aufs/inode.c
  15013. --- /dev/null
  15014. +++ linux-4.7/fs/aufs/inode.c 2016-08-08 12:57:44.604384630 -0400
  15015. @@ -0,0 +1,517 @@
  15016. +/*
  15017. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  15018. + *
  15019. + * This program, aufs is free software; you can redistribute it and/or modify
  15020. + * it under the terms of the GNU General Public License as published by
  15021. + * the Free Software Foundation; either version 2 of the License, or
  15022. + * (at your option) any later version.
  15023. + *
  15024. + * This program is distributed in the hope that it will be useful,
  15025. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15026. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15027. + * GNU General Public License for more details.
  15028. + *
  15029. + * You should have received a copy of the GNU General Public License
  15030. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15031. + */
  15032. +
  15033. +/*
  15034. + * inode functions
  15035. + */
  15036. +
  15037. +#include "aufs.h"
  15038. +
  15039. +struct inode *au_igrab(struct inode *inode)
  15040. +{
  15041. + if (inode) {
  15042. + AuDebugOn(!atomic_read(&inode->i_count));
  15043. + ihold(inode);
  15044. + }
  15045. + return inode;
  15046. +}
  15047. +
  15048. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  15049. +{
  15050. + au_cpup_attr_all(inode, /*force*/0);
  15051. + au_update_iigen(inode, /*half*/1);
  15052. + if (do_version)
  15053. + inode->i_version++;
  15054. +}
  15055. +
  15056. +static int au_ii_refresh(struct inode *inode, int *update)
  15057. +{
  15058. + int err, e;
  15059. + umode_t type;
  15060. + aufs_bindex_t bindex, new_bindex;
  15061. + struct super_block *sb;
  15062. + struct au_iinfo *iinfo;
  15063. + struct au_hinode *p, *q, tmp;
  15064. +
  15065. + AuDebugOn(au_is_bad_inode(inode));
  15066. + IiMustWriteLock(inode);
  15067. +
  15068. + *update = 0;
  15069. + sb = inode->i_sb;
  15070. + type = inode->i_mode & S_IFMT;
  15071. + iinfo = au_ii(inode);
  15072. + err = au_hinode_realloc(iinfo, au_sbbot(sb) + 1);
  15073. + if (unlikely(err))
  15074. + goto out;
  15075. +
  15076. + AuDebugOn(iinfo->ii_btop < 0);
  15077. + p = au_hinode(iinfo, iinfo->ii_btop);
  15078. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  15079. + bindex++, p++) {
  15080. + if (!p->hi_inode)
  15081. + continue;
  15082. +
  15083. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  15084. + new_bindex = au_br_index(sb, p->hi_id);
  15085. + if (new_bindex == bindex)
  15086. + continue;
  15087. +
  15088. + if (new_bindex < 0) {
  15089. + *update = 1;
  15090. + au_hiput(p);
  15091. + p->hi_inode = NULL;
  15092. + continue;
  15093. + }
  15094. +
  15095. + if (new_bindex < iinfo->ii_btop)
  15096. + iinfo->ii_btop = new_bindex;
  15097. + if (iinfo->ii_bbot < new_bindex)
  15098. + iinfo->ii_bbot = new_bindex;
  15099. + /* swap two lower inode, and loop again */
  15100. + q = au_hinode(iinfo, new_bindex);
  15101. + tmp = *q;
  15102. + *q = *p;
  15103. + *p = tmp;
  15104. + if (tmp.hi_inode) {
  15105. + bindex--;
  15106. + p--;
  15107. + }
  15108. + }
  15109. + au_update_ibrange(inode, /*do_put_zero*/0);
  15110. + e = au_dy_irefresh(inode);
  15111. + if (unlikely(e && !err))
  15112. + err = e;
  15113. +
  15114. +out:
  15115. + AuTraceErr(err);
  15116. + return err;
  15117. +}
  15118. +
  15119. +void au_refresh_iop(struct inode *inode, int force_getattr)
  15120. +{
  15121. + int type;
  15122. + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
  15123. + const struct inode_operations *iop
  15124. + = force_getattr ? aufs_iop : sbi->si_iop_array;
  15125. +
  15126. + if (inode->i_op == iop)
  15127. + return;
  15128. +
  15129. + switch (inode->i_mode & S_IFMT) {
  15130. + case S_IFDIR:
  15131. + type = AuIop_DIR;
  15132. + break;
  15133. + case S_IFLNK:
  15134. + type = AuIop_SYMLINK;
  15135. + break;
  15136. + default:
  15137. + type = AuIop_OTHER;
  15138. + break;
  15139. + }
  15140. +
  15141. + inode->i_op = iop + type;
  15142. + /* unnecessary smp_wmb() */
  15143. +}
  15144. +
  15145. +int au_refresh_hinode_self(struct inode *inode)
  15146. +{
  15147. + int err, update;
  15148. +
  15149. + err = au_ii_refresh(inode, &update);
  15150. + if (!err)
  15151. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  15152. +
  15153. + AuTraceErr(err);
  15154. + return err;
  15155. +}
  15156. +
  15157. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  15158. +{
  15159. + int err, e, update;
  15160. + unsigned int flags;
  15161. + umode_t mode;
  15162. + aufs_bindex_t bindex, bbot;
  15163. + unsigned char isdir;
  15164. + struct au_hinode *p;
  15165. + struct au_iinfo *iinfo;
  15166. +
  15167. + err = au_ii_refresh(inode, &update);
  15168. + if (unlikely(err))
  15169. + goto out;
  15170. +
  15171. + update = 0;
  15172. + iinfo = au_ii(inode);
  15173. + p = au_hinode(iinfo, iinfo->ii_btop);
  15174. + mode = (inode->i_mode & S_IFMT);
  15175. + isdir = S_ISDIR(mode);
  15176. + flags = au_hi_flags(inode, isdir);
  15177. + bbot = au_dbbot(dentry);
  15178. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  15179. + struct inode *h_i, *h_inode;
  15180. + struct dentry *h_d;
  15181. +
  15182. + h_d = au_h_dptr(dentry, bindex);
  15183. + if (!h_d || d_is_negative(h_d))
  15184. + continue;
  15185. +
  15186. + h_inode = d_inode(h_d);
  15187. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  15188. + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
  15189. + h_i = au_h_iptr(inode, bindex);
  15190. + if (h_i) {
  15191. + if (h_i == h_inode)
  15192. + continue;
  15193. + err = -EIO;
  15194. + break;
  15195. + }
  15196. + }
  15197. + if (bindex < iinfo->ii_btop)
  15198. + iinfo->ii_btop = bindex;
  15199. + if (iinfo->ii_bbot < bindex)
  15200. + iinfo->ii_bbot = bindex;
  15201. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  15202. + update = 1;
  15203. + }
  15204. + au_update_ibrange(inode, /*do_put_zero*/0);
  15205. + e = au_dy_irefresh(inode);
  15206. + if (unlikely(e && !err))
  15207. + err = e;
  15208. + if (!err)
  15209. + au_refresh_hinode_attr(inode, update && isdir);
  15210. +
  15211. +out:
  15212. + AuTraceErr(err);
  15213. + return err;
  15214. +}
  15215. +
  15216. +static int set_inode(struct inode *inode, struct dentry *dentry)
  15217. +{
  15218. + int err;
  15219. + unsigned int flags;
  15220. + umode_t mode;
  15221. + aufs_bindex_t bindex, btop, btail;
  15222. + unsigned char isdir;
  15223. + struct dentry *h_dentry;
  15224. + struct inode *h_inode;
  15225. + struct au_iinfo *iinfo;
  15226. + struct inode_operations *iop;
  15227. +
  15228. + IiMustWriteLock(inode);
  15229. +
  15230. + err = 0;
  15231. + isdir = 0;
  15232. + iop = au_sbi(inode->i_sb)->si_iop_array;
  15233. + btop = au_dbtop(dentry);
  15234. + h_dentry = au_h_dptr(dentry, btop);
  15235. + h_inode = d_inode(h_dentry);
  15236. + mode = h_inode->i_mode;
  15237. + switch (mode & S_IFMT) {
  15238. + case S_IFREG:
  15239. + btail = au_dbtail(dentry);
  15240. + inode->i_op = iop + AuIop_OTHER;
  15241. + inode->i_fop = &aufs_file_fop;
  15242. + err = au_dy_iaop(inode, btop, h_inode);
  15243. + if (unlikely(err))
  15244. + goto out;
  15245. + break;
  15246. + case S_IFDIR:
  15247. + isdir = 1;
  15248. + btail = au_dbtaildir(dentry);
  15249. + inode->i_op = iop + AuIop_DIR;
  15250. + inode->i_fop = &aufs_dir_fop;
  15251. + break;
  15252. + case S_IFLNK:
  15253. + btail = au_dbtail(dentry);
  15254. + inode->i_op = iop + AuIop_SYMLINK;
  15255. + break;
  15256. + case S_IFBLK:
  15257. + case S_IFCHR:
  15258. + case S_IFIFO:
  15259. + case S_IFSOCK:
  15260. + btail = au_dbtail(dentry);
  15261. + inode->i_op = iop + AuIop_OTHER;
  15262. + init_special_inode(inode, mode, h_inode->i_rdev);
  15263. + break;
  15264. + default:
  15265. + AuIOErr("Unknown file type 0%o\n", mode);
  15266. + err = -EIO;
  15267. + goto out;
  15268. + }
  15269. +
  15270. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  15271. + flags = au_hi_flags(inode, isdir);
  15272. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  15273. + && au_ftest_hi(flags, HNOTIFY)
  15274. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  15275. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  15276. + au_fclr_hi(flags, HNOTIFY);
  15277. + iinfo = au_ii(inode);
  15278. + iinfo->ii_btop = btop;
  15279. + iinfo->ii_bbot = btail;
  15280. + for (bindex = btop; bindex <= btail; bindex++) {
  15281. + h_dentry = au_h_dptr(dentry, bindex);
  15282. + if (h_dentry)
  15283. + au_set_h_iptr(inode, bindex,
  15284. + au_igrab(d_inode(h_dentry)), flags);
  15285. + }
  15286. + au_cpup_attr_all(inode, /*force*/1);
  15287. + /*
  15288. + * to force calling aufs_get_acl() every time,
  15289. + * do not call cache_no_acl() for aufs inode.
  15290. + */
  15291. +
  15292. +out:
  15293. + return err;
  15294. +}
  15295. +
  15296. +/*
  15297. + * successful returns with iinfo write_locked
  15298. + * minus: errno
  15299. + * zero: success, matched
  15300. + * plus: no error, but unmatched
  15301. + */
  15302. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  15303. +{
  15304. + int err;
  15305. + unsigned int gen, igflags;
  15306. + aufs_bindex_t bindex, bbot;
  15307. + struct inode *h_inode, *h_dinode;
  15308. + struct dentry *h_dentry;
  15309. +
  15310. + /*
  15311. + * before this function, if aufs got any iinfo lock, it must be only
  15312. + * one, the parent dir.
  15313. + * it can happen by UDBA and the obsoleted inode number.
  15314. + */
  15315. + err = -EIO;
  15316. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  15317. + goto out;
  15318. +
  15319. + err = 1;
  15320. + ii_write_lock_new_child(inode);
  15321. + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
  15322. + h_dinode = d_inode(h_dentry);
  15323. + bbot = au_ibbot(inode);
  15324. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  15325. + h_inode = au_h_iptr(inode, bindex);
  15326. + if (!h_inode || h_inode != h_dinode)
  15327. + continue;
  15328. +
  15329. + err = 0;
  15330. + gen = au_iigen(inode, &igflags);
  15331. + if (gen == au_digen(dentry)
  15332. + && !au_ig_ftest(igflags, HALF_REFRESHED))
  15333. + break;
  15334. +
  15335. + /* fully refresh inode using dentry */
  15336. + err = au_refresh_hinode(inode, dentry);
  15337. + if (!err)
  15338. + au_update_iigen(inode, /*half*/0);
  15339. + break;
  15340. + }
  15341. +
  15342. + if (unlikely(err))
  15343. + ii_write_unlock(inode);
  15344. +out:
  15345. + return err;
  15346. +}
  15347. +
  15348. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  15349. + unsigned int d_type, ino_t *ino)
  15350. +{
  15351. + int err;
  15352. + struct mutex *mtx;
  15353. +
  15354. + /* prevent hardlinked inode number from race condition */
  15355. + mtx = NULL;
  15356. + if (d_type != DT_DIR) {
  15357. + mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
  15358. + mutex_lock(mtx);
  15359. + }
  15360. + err = au_xino_read(sb, bindex, h_ino, ino);
  15361. + if (unlikely(err))
  15362. + goto out;
  15363. +
  15364. + if (!*ino) {
  15365. + err = -EIO;
  15366. + *ino = au_xino_new_ino(sb);
  15367. + if (unlikely(!*ino))
  15368. + goto out;
  15369. + err = au_xino_write(sb, bindex, h_ino, *ino);
  15370. + if (unlikely(err))
  15371. + goto out;
  15372. + }
  15373. +
  15374. +out:
  15375. + if (mtx)
  15376. + mutex_unlock(mtx);
  15377. + return err;
  15378. +}
  15379. +
  15380. +/* successful returns with iinfo write_locked */
  15381. +/* todo: return with unlocked? */
  15382. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  15383. +{
  15384. + struct inode *inode, *h_inode;
  15385. + struct dentry *h_dentry;
  15386. + struct super_block *sb;
  15387. + struct mutex *mtx;
  15388. + ino_t h_ino, ino;
  15389. + int err;
  15390. + aufs_bindex_t btop;
  15391. +
  15392. + sb = dentry->d_sb;
  15393. + btop = au_dbtop(dentry);
  15394. + h_dentry = au_h_dptr(dentry, btop);
  15395. + h_inode = d_inode(h_dentry);
  15396. + h_ino = h_inode->i_ino;
  15397. +
  15398. + /*
  15399. + * stop 'race'-ing between hardlinks under different
  15400. + * parents.
  15401. + */
  15402. + mtx = NULL;
  15403. + if (!d_is_dir(h_dentry))
  15404. + mtx = &au_sbr(sb, btop)->br_xino.xi_nondir_mtx;
  15405. +
  15406. +new_ino:
  15407. + if (mtx)
  15408. + mutex_lock(mtx);
  15409. + err = au_xino_read(sb, btop, h_ino, &ino);
  15410. + inode = ERR_PTR(err);
  15411. + if (unlikely(err))
  15412. + goto out;
  15413. +
  15414. + if (!ino) {
  15415. + ino = au_xino_new_ino(sb);
  15416. + if (unlikely(!ino)) {
  15417. + inode = ERR_PTR(-EIO);
  15418. + goto out;
  15419. + }
  15420. + }
  15421. +
  15422. + AuDbg("i%lu\n", (unsigned long)ino);
  15423. + inode = au_iget_locked(sb, ino);
  15424. + err = PTR_ERR(inode);
  15425. + if (IS_ERR(inode))
  15426. + goto out;
  15427. +
  15428. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  15429. + if (inode->i_state & I_NEW) {
  15430. + ii_write_lock_new_child(inode);
  15431. + err = set_inode(inode, dentry);
  15432. + if (!err) {
  15433. + unlock_new_inode(inode);
  15434. + goto out; /* success */
  15435. + }
  15436. +
  15437. + /*
  15438. + * iget_failed() calls iput(), but we need to call
  15439. + * ii_write_unlock() after iget_failed(). so dirty hack for
  15440. + * i_count.
  15441. + */
  15442. + atomic_inc(&inode->i_count);
  15443. + iget_failed(inode);
  15444. + ii_write_unlock(inode);
  15445. + au_xino_write(sb, btop, h_ino, /*ino*/0);
  15446. + /* ignore this error */
  15447. + goto out_iput;
  15448. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  15449. + /*
  15450. + * horrible race condition between lookup, readdir and copyup
  15451. + * (or something).
  15452. + */
  15453. + if (mtx)
  15454. + mutex_unlock(mtx);
  15455. + err = reval_inode(inode, dentry);
  15456. + if (unlikely(err < 0)) {
  15457. + mtx = NULL;
  15458. + goto out_iput;
  15459. + }
  15460. +
  15461. + if (!err) {
  15462. + mtx = NULL;
  15463. + goto out; /* success */
  15464. + } else if (mtx)
  15465. + mutex_lock(mtx);
  15466. + }
  15467. +
  15468. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  15469. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  15470. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  15471. + btop, au_sbtype(h_dentry->d_sb), dentry,
  15472. + (unsigned long)h_ino, (unsigned long)ino);
  15473. + ino = 0;
  15474. + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
  15475. + if (!err) {
  15476. + iput(inode);
  15477. + if (mtx)
  15478. + mutex_unlock(mtx);
  15479. + goto new_ino;
  15480. + }
  15481. +
  15482. +out_iput:
  15483. + iput(inode);
  15484. + inode = ERR_PTR(err);
  15485. +out:
  15486. + if (mtx)
  15487. + mutex_unlock(mtx);
  15488. + return inode;
  15489. +}
  15490. +
  15491. +/* ---------------------------------------------------------------------- */
  15492. +
  15493. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  15494. + struct inode *inode)
  15495. +{
  15496. + int err;
  15497. + struct inode *hi;
  15498. +
  15499. + err = au_br_rdonly(au_sbr(sb, bindex));
  15500. +
  15501. + /* pseudo-link after flushed may happen out of bounds */
  15502. + if (!err
  15503. + && inode
  15504. + && au_ibtop(inode) <= bindex
  15505. + && bindex <= au_ibbot(inode)) {
  15506. + /*
  15507. + * permission check is unnecessary since vfsub routine
  15508. + * will be called later
  15509. + */
  15510. + hi = au_h_iptr(inode, bindex);
  15511. + if (hi)
  15512. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  15513. + }
  15514. +
  15515. + return err;
  15516. +}
  15517. +
  15518. +int au_test_h_perm(struct inode *h_inode, int mask)
  15519. +{
  15520. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  15521. + return 0;
  15522. + return inode_permission(h_inode, mask);
  15523. +}
  15524. +
  15525. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  15526. +{
  15527. + if (au_test_nfs(h_inode->i_sb)
  15528. + && (mask & MAY_WRITE)
  15529. + && S_ISDIR(h_inode->i_mode))
  15530. + mask |= MAY_READ; /* force permission check */
  15531. + return au_test_h_perm(h_inode, mask);
  15532. +}
  15533. diff -Naur null/fs/aufs/inode.h linux-4.7/fs/aufs/inode.h
  15534. --- /dev/null
  15535. +++ linux-4.7/fs/aufs/inode.h 2016-08-08 12:57:44.604384630 -0400
  15536. @@ -0,0 +1,700 @@
  15537. +/*
  15538. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  15539. + *
  15540. + * This program, aufs is free software; you can redistribute it and/or modify
  15541. + * it under the terms of the GNU General Public License as published by
  15542. + * the Free Software Foundation; either version 2 of the License, or
  15543. + * (at your option) any later version.
  15544. + *
  15545. + * This program is distributed in the hope that it will be useful,
  15546. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15547. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15548. + * GNU General Public License for more details.
  15549. + *
  15550. + * You should have received a copy of the GNU General Public License
  15551. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15552. + */
  15553. +
  15554. +/*
  15555. + * inode operations
  15556. + */
  15557. +
  15558. +#ifndef __AUFS_INODE_H__
  15559. +#define __AUFS_INODE_H__
  15560. +
  15561. +#ifdef __KERNEL__
  15562. +
  15563. +#include <linux/fsnotify.h>
  15564. +#include "rwsem.h"
  15565. +
  15566. +struct vfsmount;
  15567. +
  15568. +struct au_hnotify {
  15569. +#ifdef CONFIG_AUFS_HNOTIFY
  15570. +#ifdef CONFIG_AUFS_HFSNOTIFY
  15571. + /* never use fsnotify_add_vfsmount_mark() */
  15572. + struct fsnotify_mark hn_mark;
  15573. +#endif
  15574. + union {
  15575. + struct inode *hn_aufs_inode; /* no get/put */
  15576. + struct llist_node hn_lnode; /* delayed free */
  15577. + };
  15578. +#endif
  15579. +} ____cacheline_aligned_in_smp;
  15580. +
  15581. +struct au_hinode {
  15582. + struct inode *hi_inode;
  15583. + aufs_bindex_t hi_id;
  15584. +#ifdef CONFIG_AUFS_HNOTIFY
  15585. + struct au_hnotify *hi_notify;
  15586. +#endif
  15587. +
  15588. + /* reference to the copied-up whiteout with get/put */
  15589. + struct dentry *hi_whdentry;
  15590. +};
  15591. +
  15592. +/* ig_flags */
  15593. +#define AuIG_HALF_REFRESHED 1
  15594. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  15595. +#define au_ig_fset(flags, name) \
  15596. + do { (flags) |= AuIG_##name; } while (0)
  15597. +#define au_ig_fclr(flags, name) \
  15598. + do { (flags) &= ~AuIG_##name; } while (0)
  15599. +
  15600. +struct au_iigen {
  15601. + spinlock_t ig_spin;
  15602. + __u32 ig_generation, ig_flags;
  15603. +};
  15604. +
  15605. +struct au_vdir;
  15606. +struct au_iinfo {
  15607. + struct au_iigen ii_generation;
  15608. + struct super_block *ii_hsb1; /* no get/put */
  15609. +
  15610. + struct au_rwsem ii_rwsem;
  15611. + aufs_bindex_t ii_btop, ii_bbot;
  15612. + __u32 ii_higen;
  15613. + struct au_hinode *ii_hinode;
  15614. + struct au_vdir *ii_vdir;
  15615. +};
  15616. +
  15617. +struct au_icntnr {
  15618. + struct au_iinfo iinfo;
  15619. + struct inode vfs_inode;
  15620. + union {
  15621. + struct hlist_node plink;
  15622. + struct llist_node lnode; /* delayed free */
  15623. + };
  15624. +} ____cacheline_aligned_in_smp;
  15625. +
  15626. +/* au_pin flags */
  15627. +#define AuPin_DI_LOCKED 1
  15628. +#define AuPin_MNT_WRITE (1 << 1)
  15629. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  15630. +#define au_fset_pin(flags, name) \
  15631. + do { (flags) |= AuPin_##name; } while (0)
  15632. +#define au_fclr_pin(flags, name) \
  15633. + do { (flags) &= ~AuPin_##name; } while (0)
  15634. +
  15635. +struct au_pin {
  15636. + /* input */
  15637. + struct dentry *dentry;
  15638. + unsigned int udba;
  15639. + unsigned char lsc_di, lsc_hi, flags;
  15640. + aufs_bindex_t bindex;
  15641. +
  15642. + /* output */
  15643. + struct dentry *parent;
  15644. + struct au_hinode *hdir;
  15645. + struct vfsmount *h_mnt;
  15646. +
  15647. + /* temporary unlock/relock for copyup */
  15648. + struct dentry *h_dentry, *h_parent;
  15649. + struct au_branch *br;
  15650. + struct task_struct *task;
  15651. +};
  15652. +
  15653. +void au_pin_hdir_unlock(struct au_pin *p);
  15654. +int au_pin_hdir_lock(struct au_pin *p);
  15655. +int au_pin_hdir_relock(struct au_pin *p);
  15656. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  15657. +void au_pin_hdir_release(struct au_pin *p);
  15658. +
  15659. +/* ---------------------------------------------------------------------- */
  15660. +
  15661. +static inline struct au_iinfo *au_ii(struct inode *inode)
  15662. +{
  15663. + BUG_ON(is_bad_inode(inode));
  15664. + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  15665. +}
  15666. +
  15667. +/* ---------------------------------------------------------------------- */
  15668. +
  15669. +/* inode.c */
  15670. +struct inode *au_igrab(struct inode *inode);
  15671. +void au_refresh_iop(struct inode *inode, int force_getattr);
  15672. +int au_refresh_hinode_self(struct inode *inode);
  15673. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  15674. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  15675. + unsigned int d_type, ino_t *ino);
  15676. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  15677. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  15678. + struct inode *inode);
  15679. +int au_test_h_perm(struct inode *h_inode, int mask);
  15680. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  15681. +
  15682. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  15683. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  15684. +{
  15685. +#ifdef CONFIG_AUFS_SHWH
  15686. + return au_ino(sb, bindex, h_ino, d_type, ino);
  15687. +#else
  15688. + return 0;
  15689. +#endif
  15690. +}
  15691. +
  15692. +/* i_op.c */
  15693. +enum {
  15694. + AuIop_SYMLINK,
  15695. + AuIop_DIR,
  15696. + AuIop_OTHER,
  15697. + AuIop_Last
  15698. +};
  15699. +extern struct inode_operations aufs_iop[AuIop_Last],
  15700. + aufs_iop_nogetattr[AuIop_Last];
  15701. +
  15702. +/* au_wr_dir flags */
  15703. +#define AuWrDir_ADD_ENTRY 1
  15704. +#define AuWrDir_ISDIR (1 << 1)
  15705. +#define AuWrDir_TMPFILE (1 << 2)
  15706. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  15707. +#define au_fset_wrdir(flags, name) \
  15708. + do { (flags) |= AuWrDir_##name; } while (0)
  15709. +#define au_fclr_wrdir(flags, name) \
  15710. + do { (flags) &= ~AuWrDir_##name; } while (0)
  15711. +
  15712. +struct au_wr_dir_args {
  15713. + aufs_bindex_t force_btgt;
  15714. + unsigned char flags;
  15715. +};
  15716. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  15717. + struct au_wr_dir_args *args);
  15718. +
  15719. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  15720. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  15721. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  15722. + unsigned int udba, unsigned char flags);
  15723. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  15724. + unsigned int udba, unsigned char flags) __must_check;
  15725. +int au_do_pin(struct au_pin *pin) __must_check;
  15726. +void au_unpin(struct au_pin *pin);
  15727. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  15728. +
  15729. +#define AuIcpup_DID_CPUP 1
  15730. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  15731. +#define au_fset_icpup(flags, name) \
  15732. + do { (flags) |= AuIcpup_##name; } while (0)
  15733. +#define au_fclr_icpup(flags, name) \
  15734. + do { (flags) &= ~AuIcpup_##name; } while (0)
  15735. +
  15736. +struct au_icpup_args {
  15737. + unsigned char flags;
  15738. + unsigned char pin_flags;
  15739. + aufs_bindex_t btgt;
  15740. + unsigned int udba;
  15741. + struct au_pin pin;
  15742. + struct path h_path;
  15743. + struct inode *h_inode;
  15744. +};
  15745. +
  15746. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  15747. + struct au_icpup_args *a);
  15748. +
  15749. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
  15750. +
  15751. +/* i_op_add.c */
  15752. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  15753. + struct dentry *h_parent, int isdir);
  15754. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  15755. + dev_t dev);
  15756. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  15757. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  15758. + bool want_excl);
  15759. +struct vfsub_aopen_args;
  15760. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  15761. + struct vfsub_aopen_args *args);
  15762. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  15763. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  15764. + struct dentry *dentry);
  15765. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  15766. +
  15767. +/* i_op_del.c */
  15768. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  15769. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  15770. + struct dentry *h_parent, int isdir);
  15771. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  15772. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  15773. +
  15774. +/* i_op_ren.c */
  15775. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  15776. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  15777. + struct inode *dir, struct dentry *dentry);
  15778. +
  15779. +/* iinfo.c */
  15780. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  15781. +void au_hiput(struct au_hinode *hinode);
  15782. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  15783. + struct dentry *h_wh);
  15784. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  15785. +
  15786. +/* hinode flags */
  15787. +#define AuHi_XINO 1
  15788. +#define AuHi_HNOTIFY (1 << 1)
  15789. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  15790. +#define au_fset_hi(flags, name) \
  15791. + do { (flags) |= AuHi_##name; } while (0)
  15792. +#define au_fclr_hi(flags, name) \
  15793. + do { (flags) &= ~AuHi_##name; } while (0)
  15794. +
  15795. +#ifndef CONFIG_AUFS_HNOTIFY
  15796. +#undef AuHi_HNOTIFY
  15797. +#define AuHi_HNOTIFY 0
  15798. +#endif
  15799. +
  15800. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  15801. + struct inode *h_inode, unsigned int flags);
  15802. +
  15803. +void au_update_iigen(struct inode *inode, int half);
  15804. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  15805. +
  15806. +void au_icntnr_init_once(void *_c);
  15807. +void au_hinode_init(struct au_hinode *hinode);
  15808. +int au_iinfo_init(struct inode *inode);
  15809. +void au_iinfo_fin(struct inode *inode);
  15810. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr);
  15811. +
  15812. +#ifdef CONFIG_PROC_FS
  15813. +/* plink.c */
  15814. +int au_plink_maint(struct super_block *sb, int flags);
  15815. +struct au_sbinfo;
  15816. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  15817. +int au_plink_maint_enter(struct super_block *sb);
  15818. +#ifdef CONFIG_AUFS_DEBUG
  15819. +void au_plink_list(struct super_block *sb);
  15820. +#else
  15821. +AuStubVoid(au_plink_list, struct super_block *sb)
  15822. +#endif
  15823. +int au_plink_test(struct inode *inode);
  15824. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  15825. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  15826. + struct dentry *h_dentry);
  15827. +void au_plink_put(struct super_block *sb, int verbose);
  15828. +void au_plink_clean(struct super_block *sb, int verbose);
  15829. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  15830. +#else
  15831. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  15832. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  15833. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  15834. +AuStubVoid(au_plink_list, struct super_block *sb);
  15835. +AuStubInt0(au_plink_test, struct inode *inode);
  15836. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  15837. + struct inode *inode, aufs_bindex_t bindex);
  15838. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  15839. + struct dentry *h_dentry);
  15840. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  15841. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  15842. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  15843. +#endif /* CONFIG_PROC_FS */
  15844. +
  15845. +#ifdef CONFIG_AUFS_XATTR
  15846. +/* xattr.c */
  15847. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  15848. + unsigned int verbose);
  15849. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  15850. +ssize_t aufs_getxattr(struct dentry *dentry, struct inode *inode,
  15851. + const char *name, void *value, size_t size);
  15852. +int aufs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
  15853. + const void *value, size_t size, int flags);
  15854. +int aufs_removexattr(struct dentry *dentry, const char *name);
  15855. +
  15856. +/* void au_xattr_init(struct super_block *sb); */
  15857. +#else
  15858. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  15859. + int ignore_flags, unsigned int verbose);
  15860. +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
  15861. +#endif
  15862. +
  15863. +#ifdef CONFIG_FS_POSIX_ACL
  15864. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  15865. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  15866. +#endif
  15867. +
  15868. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  15869. +enum {
  15870. + AU_XATTR_SET,
  15871. + AU_XATTR_REMOVE,
  15872. + AU_ACL_SET
  15873. +};
  15874. +
  15875. +struct au_srxattr {
  15876. + int type;
  15877. + union {
  15878. + struct {
  15879. + const char *name;
  15880. + const void *value;
  15881. + size_t size;
  15882. + int flags;
  15883. + } set;
  15884. + struct {
  15885. + const char *name;
  15886. + } remove;
  15887. + struct {
  15888. + struct posix_acl *acl;
  15889. + int type;
  15890. + } acl_set;
  15891. + } u;
  15892. +};
  15893. +ssize_t au_srxattr(struct dentry *dentry, struct inode *inode,
  15894. + struct au_srxattr *arg);
  15895. +#endif
  15896. +
  15897. +/* ---------------------------------------------------------------------- */
  15898. +
  15899. +/* lock subclass for iinfo */
  15900. +enum {
  15901. + AuLsc_II_CHILD, /* child first */
  15902. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  15903. + AuLsc_II_CHILD3, /* copyup dirs */
  15904. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  15905. + AuLsc_II_PARENT2,
  15906. + AuLsc_II_PARENT3, /* copyup dirs */
  15907. + AuLsc_II_NEW_CHILD
  15908. +};
  15909. +
  15910. +/*
  15911. + * ii_read_lock_child, ii_write_lock_child,
  15912. + * ii_read_lock_child2, ii_write_lock_child2,
  15913. + * ii_read_lock_child3, ii_write_lock_child3,
  15914. + * ii_read_lock_parent, ii_write_lock_parent,
  15915. + * ii_read_lock_parent2, ii_write_lock_parent2,
  15916. + * ii_read_lock_parent3, ii_write_lock_parent3,
  15917. + * ii_read_lock_new_child, ii_write_lock_new_child,
  15918. + */
  15919. +#define AuReadLockFunc(name, lsc) \
  15920. +static inline void ii_read_lock_##name(struct inode *i) \
  15921. +{ \
  15922. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15923. +}
  15924. +
  15925. +#define AuWriteLockFunc(name, lsc) \
  15926. +static inline void ii_write_lock_##name(struct inode *i) \
  15927. +{ \
  15928. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15929. +}
  15930. +
  15931. +#define AuRWLockFuncs(name, lsc) \
  15932. + AuReadLockFunc(name, lsc) \
  15933. + AuWriteLockFunc(name, lsc)
  15934. +
  15935. +AuRWLockFuncs(child, CHILD);
  15936. +AuRWLockFuncs(child2, CHILD2);
  15937. +AuRWLockFuncs(child3, CHILD3);
  15938. +AuRWLockFuncs(parent, PARENT);
  15939. +AuRWLockFuncs(parent2, PARENT2);
  15940. +AuRWLockFuncs(parent3, PARENT3);
  15941. +AuRWLockFuncs(new_child, NEW_CHILD);
  15942. +
  15943. +#undef AuReadLockFunc
  15944. +#undef AuWriteLockFunc
  15945. +#undef AuRWLockFuncs
  15946. +
  15947. +/*
  15948. + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
  15949. + */
  15950. +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
  15951. +
  15952. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  15953. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  15954. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  15955. +
  15956. +/* ---------------------------------------------------------------------- */
  15957. +
  15958. +static inline void au_icntnr_init(struct au_icntnr *c)
  15959. +{
  15960. +#ifdef CONFIG_AUFS_DEBUG
  15961. + c->vfs_inode.i_mode = 0;
  15962. +#endif
  15963. +}
  15964. +
  15965. +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
  15966. +{
  15967. + unsigned int gen;
  15968. + struct au_iinfo *iinfo;
  15969. + struct au_iigen *iigen;
  15970. +
  15971. + iinfo = au_ii(inode);
  15972. + iigen = &iinfo->ii_generation;
  15973. + spin_lock(&iigen->ig_spin);
  15974. + if (igflags)
  15975. + *igflags = iigen->ig_flags;
  15976. + gen = iigen->ig_generation;
  15977. + spin_unlock(&iigen->ig_spin);
  15978. +
  15979. + return gen;
  15980. +}
  15981. +
  15982. +/* tiny test for inode number */
  15983. +/* tmpfs generation is too rough */
  15984. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  15985. +{
  15986. + struct au_iinfo *iinfo;
  15987. +
  15988. + iinfo = au_ii(inode);
  15989. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  15990. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  15991. + && iinfo->ii_higen == h_inode->i_generation);
  15992. +}
  15993. +
  15994. +static inline void au_iigen_dec(struct inode *inode)
  15995. +{
  15996. + struct au_iinfo *iinfo;
  15997. + struct au_iigen *iigen;
  15998. +
  15999. + iinfo = au_ii(inode);
  16000. + iigen = &iinfo->ii_generation;
  16001. + spin_lock(&iigen->ig_spin);
  16002. + iigen->ig_generation--;
  16003. + spin_unlock(&iigen->ig_spin);
  16004. +}
  16005. +
  16006. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  16007. +{
  16008. + int err;
  16009. +
  16010. + err = 0;
  16011. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  16012. + err = -EIO;
  16013. +
  16014. + return err;
  16015. +}
  16016. +
  16017. +/* ---------------------------------------------------------------------- */
  16018. +
  16019. +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
  16020. + aufs_bindex_t bindex)
  16021. +{
  16022. + return iinfo->ii_hinode + bindex;
  16023. +}
  16024. +
  16025. +static inline int au_is_bad_inode(struct inode *inode)
  16026. +{
  16027. + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
  16028. +}
  16029. +
  16030. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  16031. + aufs_bindex_t bindex)
  16032. +{
  16033. + IiMustAnyLock(inode);
  16034. + return au_hinode(au_ii(inode), bindex)->hi_id;
  16035. +}
  16036. +
  16037. +static inline aufs_bindex_t au_ibtop(struct inode *inode)
  16038. +{
  16039. + IiMustAnyLock(inode);
  16040. + return au_ii(inode)->ii_btop;
  16041. +}
  16042. +
  16043. +static inline aufs_bindex_t au_ibbot(struct inode *inode)
  16044. +{
  16045. + IiMustAnyLock(inode);
  16046. + return au_ii(inode)->ii_bbot;
  16047. +}
  16048. +
  16049. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  16050. +{
  16051. + IiMustAnyLock(inode);
  16052. + return au_ii(inode)->ii_vdir;
  16053. +}
  16054. +
  16055. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  16056. +{
  16057. + IiMustAnyLock(inode);
  16058. + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
  16059. +}
  16060. +
  16061. +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
  16062. +{
  16063. + IiMustWriteLock(inode);
  16064. + au_ii(inode)->ii_btop = bindex;
  16065. +}
  16066. +
  16067. +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
  16068. +{
  16069. + IiMustWriteLock(inode);
  16070. + au_ii(inode)->ii_bbot = bindex;
  16071. +}
  16072. +
  16073. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  16074. +{
  16075. + IiMustWriteLock(inode);
  16076. + au_ii(inode)->ii_vdir = vdir;
  16077. +}
  16078. +
  16079. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  16080. +{
  16081. + IiMustAnyLock(inode);
  16082. + return au_hinode(au_ii(inode), bindex);
  16083. +}
  16084. +
  16085. +/* ---------------------------------------------------------------------- */
  16086. +
  16087. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  16088. +{
  16089. + if (pin)
  16090. + return pin->parent;
  16091. + return NULL;
  16092. +}
  16093. +
  16094. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  16095. +{
  16096. + if (pin && pin->hdir)
  16097. + return pin->hdir->hi_inode;
  16098. + return NULL;
  16099. +}
  16100. +
  16101. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  16102. +{
  16103. + if (pin)
  16104. + return pin->hdir;
  16105. + return NULL;
  16106. +}
  16107. +
  16108. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  16109. +{
  16110. + if (pin)
  16111. + pin->dentry = dentry;
  16112. +}
  16113. +
  16114. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  16115. + unsigned char lflag)
  16116. +{
  16117. + if (pin) {
  16118. + if (lflag)
  16119. + au_fset_pin(pin->flags, DI_LOCKED);
  16120. + else
  16121. + au_fclr_pin(pin->flags, DI_LOCKED);
  16122. + }
  16123. +}
  16124. +
  16125. +#if 0 /* reserved */
  16126. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  16127. +{
  16128. + if (pin) {
  16129. + dput(pin->parent);
  16130. + pin->parent = dget(parent);
  16131. + }
  16132. +}
  16133. +#endif
  16134. +
  16135. +/* ---------------------------------------------------------------------- */
  16136. +
  16137. +struct au_branch;
  16138. +#ifdef CONFIG_AUFS_HNOTIFY
  16139. +struct au_hnotify_op {
  16140. + void (*ctl)(struct au_hinode *hinode, int do_set);
  16141. + int (*alloc)(struct au_hinode *hinode);
  16142. +
  16143. + /*
  16144. + * if it returns true, the the caller should free hinode->hi_notify,
  16145. + * otherwise ->free() frees it.
  16146. + */
  16147. + int (*free)(struct au_hinode *hinode,
  16148. + struct au_hnotify *hn) __must_check;
  16149. +
  16150. + void (*fin)(void);
  16151. + int (*init)(void);
  16152. +
  16153. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  16154. + void (*fin_br)(struct au_branch *br);
  16155. + int (*init_br)(struct au_branch *br, int perm);
  16156. +};
  16157. +
  16158. +/* hnotify.c */
  16159. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  16160. +void au_hn_free(struct au_hinode *hinode);
  16161. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  16162. +void au_hn_reset(struct inode *inode, unsigned int flags);
  16163. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  16164. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  16165. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  16166. +int au_hnotify_init_br(struct au_branch *br, int perm);
  16167. +void au_hnotify_fin_br(struct au_branch *br);
  16168. +int __init au_hnotify_init(void);
  16169. +void au_hnotify_fin(void);
  16170. +
  16171. +/* hfsnotify.c */
  16172. +extern const struct au_hnotify_op au_hnotify_op;
  16173. +
  16174. +static inline
  16175. +void au_hn_init(struct au_hinode *hinode)
  16176. +{
  16177. + hinode->hi_notify = NULL;
  16178. +}
  16179. +
  16180. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  16181. +{
  16182. + return hinode->hi_notify;
  16183. +}
  16184. +
  16185. +#else
  16186. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  16187. + struct au_hinode *hinode __maybe_unused,
  16188. + struct inode *inode __maybe_unused)
  16189. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  16190. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  16191. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  16192. + int do_set __maybe_unused)
  16193. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  16194. + unsigned int flags __maybe_unused)
  16195. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  16196. + struct au_branch *br __maybe_unused,
  16197. + int perm __maybe_unused)
  16198. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  16199. + int perm __maybe_unused)
  16200. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  16201. +AuStubInt0(__init au_hnotify_init, void)
  16202. +AuStubVoid(au_hnotify_fin, void)
  16203. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  16204. +#endif /* CONFIG_AUFS_HNOTIFY */
  16205. +
  16206. +static inline void au_hn_suspend(struct au_hinode *hdir)
  16207. +{
  16208. + au_hn_ctl(hdir, /*do_set*/0);
  16209. +}
  16210. +
  16211. +static inline void au_hn_resume(struct au_hinode *hdir)
  16212. +{
  16213. + au_hn_ctl(hdir, /*do_set*/1);
  16214. +}
  16215. +
  16216. +static inline void au_hn_inode_lock(struct au_hinode *hdir)
  16217. +{
  16218. + inode_lock(hdir->hi_inode);
  16219. + au_hn_suspend(hdir);
  16220. +}
  16221. +
  16222. +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
  16223. + unsigned int sc __maybe_unused)
  16224. +{
  16225. + inode_lock_nested(hdir->hi_inode, sc);
  16226. + au_hn_suspend(hdir);
  16227. +}
  16228. +
  16229. +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
  16230. +{
  16231. + au_hn_resume(hdir);
  16232. + inode_unlock(hdir->hi_inode);
  16233. +}
  16234. +
  16235. +#endif /* __KERNEL__ */
  16236. +#endif /* __AUFS_INODE_H__ */
  16237. diff -Naur null/fs/aufs/ioctl.c linux-4.7/fs/aufs/ioctl.c
  16238. --- /dev/null
  16239. +++ linux-4.7/fs/aufs/ioctl.c 2016-08-08 12:57:44.604384630 -0400
  16240. @@ -0,0 +1,219 @@
  16241. +/*
  16242. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  16243. + *
  16244. + * This program, aufs is free software; you can redistribute it and/or modify
  16245. + * it under the terms of the GNU General Public License as published by
  16246. + * the Free Software Foundation; either version 2 of the License, or
  16247. + * (at your option) any later version.
  16248. + *
  16249. + * This program is distributed in the hope that it will be useful,
  16250. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16251. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16252. + * GNU General Public License for more details.
  16253. + *
  16254. + * You should have received a copy of the GNU General Public License
  16255. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16256. + */
  16257. +
  16258. +/*
  16259. + * ioctl
  16260. + * plink-management and readdir in userspace.
  16261. + * assist the pathconf(3) wrapper library.
  16262. + * move-down
  16263. + * File-based Hierarchical Storage Management.
  16264. + */
  16265. +
  16266. +#include <linux/compat.h>
  16267. +#include <linux/file.h>
  16268. +#include "aufs.h"
  16269. +
  16270. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  16271. +{
  16272. + int err, fd;
  16273. + aufs_bindex_t wbi, bindex, bbot;
  16274. + struct file *h_file;
  16275. + struct super_block *sb;
  16276. + struct dentry *root;
  16277. + struct au_branch *br;
  16278. + struct aufs_wbr_fd wbrfd = {
  16279. + .oflags = au_dir_roflags,
  16280. + .brid = -1
  16281. + };
  16282. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  16283. + | O_NOATIME | O_CLOEXEC;
  16284. +
  16285. + AuDebugOn(wbrfd.oflags & ~valid);
  16286. +
  16287. + if (arg) {
  16288. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  16289. + if (unlikely(err)) {
  16290. + err = -EFAULT;
  16291. + goto out;
  16292. + }
  16293. +
  16294. + err = -EINVAL;
  16295. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  16296. + wbrfd.oflags |= au_dir_roflags;
  16297. + AuDbg("0%o\n", wbrfd.oflags);
  16298. + if (unlikely(wbrfd.oflags & ~valid))
  16299. + goto out;
  16300. + }
  16301. +
  16302. + fd = get_unused_fd_flags(0);
  16303. + err = fd;
  16304. + if (unlikely(fd < 0))
  16305. + goto out;
  16306. +
  16307. + h_file = ERR_PTR(-EINVAL);
  16308. + wbi = 0;
  16309. + br = NULL;
  16310. + sb = path->dentry->d_sb;
  16311. + root = sb->s_root;
  16312. + aufs_read_lock(root, AuLock_IR);
  16313. + bbot = au_sbbot(sb);
  16314. + if (wbrfd.brid >= 0) {
  16315. + wbi = au_br_index(sb, wbrfd.brid);
  16316. + if (unlikely(wbi < 0 || wbi > bbot))
  16317. + goto out_unlock;
  16318. + }
  16319. +
  16320. + h_file = ERR_PTR(-ENOENT);
  16321. + br = au_sbr(sb, wbi);
  16322. + if (!au_br_writable(br->br_perm)) {
  16323. + if (arg)
  16324. + goto out_unlock;
  16325. +
  16326. + bindex = wbi + 1;
  16327. + wbi = -1;
  16328. + for (; bindex <= bbot; bindex++) {
  16329. + br = au_sbr(sb, bindex);
  16330. + if (au_br_writable(br->br_perm)) {
  16331. + wbi = bindex;
  16332. + br = au_sbr(sb, wbi);
  16333. + break;
  16334. + }
  16335. + }
  16336. + }
  16337. + AuDbg("wbi %d\n", wbi);
  16338. + if (wbi >= 0)
  16339. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  16340. + /*force_wr*/0);
  16341. +
  16342. +out_unlock:
  16343. + aufs_read_unlock(root, AuLock_IR);
  16344. + err = PTR_ERR(h_file);
  16345. + if (IS_ERR(h_file))
  16346. + goto out_fd;
  16347. +
  16348. + au_br_put(br); /* cf. au_h_open() */
  16349. + fd_install(fd, h_file);
  16350. + err = fd;
  16351. + goto out; /* success */
  16352. +
  16353. +out_fd:
  16354. + put_unused_fd(fd);
  16355. +out:
  16356. + AuTraceErr(err);
  16357. + return err;
  16358. +}
  16359. +
  16360. +/* ---------------------------------------------------------------------- */
  16361. +
  16362. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  16363. +{
  16364. + long err;
  16365. + struct dentry *dentry;
  16366. +
  16367. + switch (cmd) {
  16368. + case AUFS_CTL_RDU:
  16369. + case AUFS_CTL_RDU_INO:
  16370. + err = au_rdu_ioctl(file, cmd, arg);
  16371. + break;
  16372. +
  16373. + case AUFS_CTL_WBR_FD:
  16374. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  16375. + break;
  16376. +
  16377. + case AUFS_CTL_IBUSY:
  16378. + err = au_ibusy_ioctl(file, arg);
  16379. + break;
  16380. +
  16381. + case AUFS_CTL_BRINFO:
  16382. + err = au_brinfo_ioctl(file, arg);
  16383. + break;
  16384. +
  16385. + case AUFS_CTL_FHSM_FD:
  16386. + dentry = file->f_path.dentry;
  16387. + if (IS_ROOT(dentry))
  16388. + err = au_fhsm_fd(dentry->d_sb, arg);
  16389. + else
  16390. + err = -ENOTTY;
  16391. + break;
  16392. +
  16393. + default:
  16394. + /* do not call the lower */
  16395. + AuDbg("0x%x\n", cmd);
  16396. + err = -ENOTTY;
  16397. + }
  16398. +
  16399. + AuTraceErr(err);
  16400. + return err;
  16401. +}
  16402. +
  16403. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  16404. +{
  16405. + long err;
  16406. +
  16407. + switch (cmd) {
  16408. + case AUFS_CTL_MVDOWN:
  16409. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  16410. + break;
  16411. +
  16412. + case AUFS_CTL_WBR_FD:
  16413. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  16414. + break;
  16415. +
  16416. + default:
  16417. + /* do not call the lower */
  16418. + AuDbg("0x%x\n", cmd);
  16419. + err = -ENOTTY;
  16420. + }
  16421. +
  16422. + AuTraceErr(err);
  16423. + return err;
  16424. +}
  16425. +
  16426. +#ifdef CONFIG_COMPAT
  16427. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  16428. + unsigned long arg)
  16429. +{
  16430. + long err;
  16431. +
  16432. + switch (cmd) {
  16433. + case AUFS_CTL_RDU:
  16434. + case AUFS_CTL_RDU_INO:
  16435. + err = au_rdu_compat_ioctl(file, cmd, arg);
  16436. + break;
  16437. +
  16438. + case AUFS_CTL_IBUSY:
  16439. + err = au_ibusy_compat_ioctl(file, arg);
  16440. + break;
  16441. +
  16442. + case AUFS_CTL_BRINFO:
  16443. + err = au_brinfo_compat_ioctl(file, arg);
  16444. + break;
  16445. +
  16446. + default:
  16447. + err = aufs_ioctl_dir(file, cmd, arg);
  16448. + }
  16449. +
  16450. + AuTraceErr(err);
  16451. + return err;
  16452. +}
  16453. +
  16454. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  16455. + unsigned long arg)
  16456. +{
  16457. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  16458. +}
  16459. +#endif
  16460. diff -Naur null/fs/aufs/i_op_add.c linux-4.7/fs/aufs/i_op_add.c
  16461. --- /dev/null
  16462. +++ linux-4.7/fs/aufs/i_op_add.c 2016-08-08 12:57:44.603384652 -0400
  16463. @@ -0,0 +1,924 @@
  16464. +/*
  16465. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  16466. + *
  16467. + * This program, aufs is free software; you can redistribute it and/or modify
  16468. + * it under the terms of the GNU General Public License as published by
  16469. + * the Free Software Foundation; either version 2 of the License, or
  16470. + * (at your option) any later version.
  16471. + *
  16472. + * This program is distributed in the hope that it will be useful,
  16473. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16474. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16475. + * GNU General Public License for more details.
  16476. + *
  16477. + * You should have received a copy of the GNU General Public License
  16478. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16479. + */
  16480. +
  16481. +/*
  16482. + * inode operations (add entry)
  16483. + */
  16484. +
  16485. +#include "aufs.h"
  16486. +
  16487. +/*
  16488. + * final procedure of adding a new entry, except link(2).
  16489. + * remove whiteout, instantiate, copyup the parent dir's times and size
  16490. + * and update version.
  16491. + * if it failed, re-create the removed whiteout.
  16492. + */
  16493. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  16494. + struct dentry *wh_dentry, struct dentry *dentry)
  16495. +{
  16496. + int err, rerr;
  16497. + aufs_bindex_t bwh;
  16498. + struct path h_path;
  16499. + struct super_block *sb;
  16500. + struct inode *inode, *h_dir;
  16501. + struct dentry *wh;
  16502. +
  16503. + bwh = -1;
  16504. + sb = dir->i_sb;
  16505. + if (wh_dentry) {
  16506. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  16507. + IMustLock(h_dir);
  16508. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  16509. + bwh = au_dbwh(dentry);
  16510. + h_path.dentry = wh_dentry;
  16511. + h_path.mnt = au_sbr_mnt(sb, bindex);
  16512. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  16513. + dentry);
  16514. + if (unlikely(err))
  16515. + goto out;
  16516. + }
  16517. +
  16518. + inode = au_new_inode(dentry, /*must_new*/1);
  16519. + if (!IS_ERR(inode)) {
  16520. + d_instantiate(dentry, inode);
  16521. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  16522. + IMustLock(dir);
  16523. + au_dir_ts(dir, bindex);
  16524. + dir->i_version++;
  16525. + au_fhsm_wrote(sb, bindex, /*force*/0);
  16526. + return 0; /* success */
  16527. + }
  16528. +
  16529. + err = PTR_ERR(inode);
  16530. + if (!wh_dentry)
  16531. + goto out;
  16532. +
  16533. + /* revert */
  16534. + /* dir inode is locked */
  16535. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  16536. + rerr = PTR_ERR(wh);
  16537. + if (IS_ERR(wh)) {
  16538. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  16539. + dentry, err, rerr);
  16540. + err = -EIO;
  16541. + } else
  16542. + dput(wh);
  16543. +
  16544. +out:
  16545. + return err;
  16546. +}
  16547. +
  16548. +static int au_d_may_add(struct dentry *dentry)
  16549. +{
  16550. + int err;
  16551. +
  16552. + err = 0;
  16553. + if (unlikely(d_unhashed(dentry)))
  16554. + err = -ENOENT;
  16555. + if (unlikely(d_really_is_positive(dentry)))
  16556. + err = -EEXIST;
  16557. + return err;
  16558. +}
  16559. +
  16560. +/*
  16561. + * simple tests for the adding inode operations.
  16562. + * following the checks in vfs, plus the parent-child relationship.
  16563. + */
  16564. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  16565. + struct dentry *h_parent, int isdir)
  16566. +{
  16567. + int err;
  16568. + umode_t h_mode;
  16569. + struct dentry *h_dentry;
  16570. + struct inode *h_inode;
  16571. +
  16572. + err = -ENAMETOOLONG;
  16573. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16574. + goto out;
  16575. +
  16576. + h_dentry = au_h_dptr(dentry, bindex);
  16577. + if (d_really_is_negative(dentry)) {
  16578. + err = -EEXIST;
  16579. + if (unlikely(d_is_positive(h_dentry)))
  16580. + goto out;
  16581. + } else {
  16582. + /* rename(2) case */
  16583. + err = -EIO;
  16584. + if (unlikely(d_is_negative(h_dentry)))
  16585. + goto out;
  16586. + h_inode = d_inode(h_dentry);
  16587. + if (unlikely(!h_inode->i_nlink))
  16588. + goto out;
  16589. +
  16590. + h_mode = h_inode->i_mode;
  16591. + if (!isdir) {
  16592. + err = -EISDIR;
  16593. + if (unlikely(S_ISDIR(h_mode)))
  16594. + goto out;
  16595. + } else if (unlikely(!S_ISDIR(h_mode))) {
  16596. + err = -ENOTDIR;
  16597. + goto out;
  16598. + }
  16599. + }
  16600. +
  16601. + err = 0;
  16602. + /* expected parent dir is locked */
  16603. + if (unlikely(h_parent != h_dentry->d_parent))
  16604. + err = -EIO;
  16605. +
  16606. +out:
  16607. + AuTraceErr(err);
  16608. + return err;
  16609. +}
  16610. +
  16611. +/*
  16612. + * initial procedure of adding a new entry.
  16613. + * prepare writable branch and the parent dir, lock it,
  16614. + * and lookup whiteout for the new entry.
  16615. + */
  16616. +static struct dentry*
  16617. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  16618. + struct dentry *src_dentry, struct au_pin *pin,
  16619. + struct au_wr_dir_args *wr_dir_args)
  16620. +{
  16621. + struct dentry *wh_dentry, *h_parent;
  16622. + struct super_block *sb;
  16623. + struct au_branch *br;
  16624. + int err;
  16625. + unsigned int udba;
  16626. + aufs_bindex_t bcpup;
  16627. +
  16628. + AuDbg("%pd\n", dentry);
  16629. +
  16630. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  16631. + bcpup = err;
  16632. + wh_dentry = ERR_PTR(err);
  16633. + if (unlikely(err < 0))
  16634. + goto out;
  16635. +
  16636. + sb = dentry->d_sb;
  16637. + udba = au_opt_udba(sb);
  16638. + err = au_pin(pin, dentry, bcpup, udba,
  16639. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16640. + wh_dentry = ERR_PTR(err);
  16641. + if (unlikely(err))
  16642. + goto out;
  16643. +
  16644. + h_parent = au_pinned_h_parent(pin);
  16645. + if (udba != AuOpt_UDBA_NONE
  16646. + && au_dbtop(dentry) == bcpup)
  16647. + err = au_may_add(dentry, bcpup, h_parent,
  16648. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  16649. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16650. + err = -ENAMETOOLONG;
  16651. + wh_dentry = ERR_PTR(err);
  16652. + if (unlikely(err))
  16653. + goto out_unpin;
  16654. +
  16655. + br = au_sbr(sb, bcpup);
  16656. + if (dt) {
  16657. + struct path tmp = {
  16658. + .dentry = h_parent,
  16659. + .mnt = au_br_mnt(br)
  16660. + };
  16661. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  16662. + }
  16663. +
  16664. + wh_dentry = NULL;
  16665. + if (bcpup != au_dbwh(dentry))
  16666. + goto out; /* success */
  16667. +
  16668. + /*
  16669. + * ENAMETOOLONG here means that if we allowed create such name, then it
  16670. + * would not be able to removed in the future. So we don't allow such
  16671. + * name here and we don't handle ENAMETOOLONG differently here.
  16672. + */
  16673. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  16674. +
  16675. +out_unpin:
  16676. + if (IS_ERR(wh_dentry))
  16677. + au_unpin(pin);
  16678. +out:
  16679. + return wh_dentry;
  16680. +}
  16681. +
  16682. +/* ---------------------------------------------------------------------- */
  16683. +
  16684. +enum { Mknod, Symlink, Creat };
  16685. +struct simple_arg {
  16686. + int type;
  16687. + union {
  16688. + struct {
  16689. + umode_t mode;
  16690. + bool want_excl;
  16691. + bool try_aopen;
  16692. + struct vfsub_aopen_args *aopen;
  16693. + } c;
  16694. + struct {
  16695. + const char *symname;
  16696. + } s;
  16697. + struct {
  16698. + umode_t mode;
  16699. + dev_t dev;
  16700. + } m;
  16701. + } u;
  16702. +};
  16703. +
  16704. +static int add_simple(struct inode *dir, struct dentry *dentry,
  16705. + struct simple_arg *arg)
  16706. +{
  16707. + int err, rerr;
  16708. + aufs_bindex_t btop;
  16709. + unsigned char created;
  16710. + const unsigned char try_aopen
  16711. + = (arg->type == Creat && arg->u.c.try_aopen);
  16712. + struct dentry *wh_dentry, *parent;
  16713. + struct inode *h_dir;
  16714. + struct super_block *sb;
  16715. + struct au_branch *br;
  16716. + /* to reuduce stack size */
  16717. + struct {
  16718. + struct au_dtime dt;
  16719. + struct au_pin pin;
  16720. + struct path h_path;
  16721. + struct au_wr_dir_args wr_dir_args;
  16722. + } *a;
  16723. +
  16724. + AuDbg("%pd\n", dentry);
  16725. + IMustLock(dir);
  16726. +
  16727. + err = -ENOMEM;
  16728. + a = kmalloc(sizeof(*a), GFP_NOFS);
  16729. + if (unlikely(!a))
  16730. + goto out;
  16731. + a->wr_dir_args.force_btgt = -1;
  16732. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  16733. +
  16734. + parent = dentry->d_parent; /* dir inode is locked */
  16735. + if (!try_aopen) {
  16736. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  16737. + if (unlikely(err))
  16738. + goto out_free;
  16739. + }
  16740. + err = au_d_may_add(dentry);
  16741. + if (unlikely(err))
  16742. + goto out_unlock;
  16743. + if (!try_aopen)
  16744. + di_write_lock_parent(parent);
  16745. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  16746. + &a->pin, &a->wr_dir_args);
  16747. + err = PTR_ERR(wh_dentry);
  16748. + if (IS_ERR(wh_dentry))
  16749. + goto out_parent;
  16750. +
  16751. + btop = au_dbtop(dentry);
  16752. + sb = dentry->d_sb;
  16753. + br = au_sbr(sb, btop);
  16754. + a->h_path.dentry = au_h_dptr(dentry, btop);
  16755. + a->h_path.mnt = au_br_mnt(br);
  16756. + h_dir = au_pinned_h_dir(&a->pin);
  16757. + switch (arg->type) {
  16758. + case Creat:
  16759. + err = 0;
  16760. + if (!try_aopen || !h_dir->i_op->atomic_open)
  16761. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  16762. + arg->u.c.want_excl);
  16763. + else
  16764. + err = vfsub_atomic_open(h_dir, a->h_path.dentry,
  16765. + arg->u.c.aopen, br);
  16766. + break;
  16767. + case Symlink:
  16768. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  16769. + break;
  16770. + case Mknod:
  16771. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  16772. + arg->u.m.dev);
  16773. + break;
  16774. + default:
  16775. + BUG();
  16776. + }
  16777. + created = !err;
  16778. + if (!err)
  16779. + err = epilog(dir, btop, wh_dentry, dentry);
  16780. +
  16781. + /* revert */
  16782. + if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
  16783. + /* no delegation since it is just created */
  16784. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  16785. + /*force*/0);
  16786. + if (rerr) {
  16787. + AuIOErr("%pd revert failure(%d, %d)\n",
  16788. + dentry, err, rerr);
  16789. + err = -EIO;
  16790. + }
  16791. + au_dtime_revert(&a->dt);
  16792. + }
  16793. +
  16794. + if (!err && try_aopen && !h_dir->i_op->atomic_open)
  16795. + *arg->u.c.aopen->opened |= FILE_CREATED;
  16796. +
  16797. + au_unpin(&a->pin);
  16798. + dput(wh_dentry);
  16799. +
  16800. +out_parent:
  16801. + if (!try_aopen)
  16802. + di_write_unlock(parent);
  16803. +out_unlock:
  16804. + if (unlikely(err)) {
  16805. + au_update_dbtop(dentry);
  16806. + d_drop(dentry);
  16807. + }
  16808. + if (!try_aopen)
  16809. + aufs_read_unlock(dentry, AuLock_DW);
  16810. +out_free:
  16811. + au_delayed_kfree(a);
  16812. +out:
  16813. + return err;
  16814. +}
  16815. +
  16816. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  16817. + dev_t dev)
  16818. +{
  16819. + struct simple_arg arg = {
  16820. + .type = Mknod,
  16821. + .u.m = {
  16822. + .mode = mode,
  16823. + .dev = dev
  16824. + }
  16825. + };
  16826. + return add_simple(dir, dentry, &arg);
  16827. +}
  16828. +
  16829. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  16830. +{
  16831. + struct simple_arg arg = {
  16832. + .type = Symlink,
  16833. + .u.s.symname = symname
  16834. + };
  16835. + return add_simple(dir, dentry, &arg);
  16836. +}
  16837. +
  16838. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  16839. + bool want_excl)
  16840. +{
  16841. + struct simple_arg arg = {
  16842. + .type = Creat,
  16843. + .u.c = {
  16844. + .mode = mode,
  16845. + .want_excl = want_excl
  16846. + }
  16847. + };
  16848. + return add_simple(dir, dentry, &arg);
  16849. +}
  16850. +
  16851. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  16852. + struct vfsub_aopen_args *aopen_args)
  16853. +{
  16854. + struct simple_arg arg = {
  16855. + .type = Creat,
  16856. + .u.c = {
  16857. + .mode = aopen_args->create_mode,
  16858. + .want_excl = aopen_args->open_flag & O_EXCL,
  16859. + .try_aopen = true,
  16860. + .aopen = aopen_args
  16861. + }
  16862. + };
  16863. + return add_simple(dir, dentry, &arg);
  16864. +}
  16865. +
  16866. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  16867. +{
  16868. + int err;
  16869. + aufs_bindex_t bindex;
  16870. + struct super_block *sb;
  16871. + struct dentry *parent, *h_parent, *h_dentry;
  16872. + struct inode *h_dir, *inode;
  16873. + struct vfsmount *h_mnt;
  16874. + struct au_wr_dir_args wr_dir_args = {
  16875. + .force_btgt = -1,
  16876. + .flags = AuWrDir_TMPFILE
  16877. + };
  16878. +
  16879. + /* copy-up may happen */
  16880. + inode_lock(dir);
  16881. +
  16882. + sb = dir->i_sb;
  16883. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16884. + if (unlikely(err))
  16885. + goto out;
  16886. +
  16887. + err = au_di_init(dentry);
  16888. + if (unlikely(err))
  16889. + goto out_si;
  16890. +
  16891. + err = -EBUSY;
  16892. + parent = d_find_any_alias(dir);
  16893. + AuDebugOn(!parent);
  16894. + di_write_lock_parent(parent);
  16895. + if (unlikely(d_inode(parent) != dir))
  16896. + goto out_parent;
  16897. +
  16898. + err = au_digen_test(parent, au_sigen(sb));
  16899. + if (unlikely(err))
  16900. + goto out_parent;
  16901. +
  16902. + bindex = au_dbtop(parent);
  16903. + au_set_dbtop(dentry, bindex);
  16904. + au_set_dbbot(dentry, bindex);
  16905. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  16906. + bindex = err;
  16907. + if (unlikely(err < 0))
  16908. + goto out_parent;
  16909. +
  16910. + err = -EOPNOTSUPP;
  16911. + h_dir = au_h_iptr(dir, bindex);
  16912. + if (unlikely(!h_dir->i_op->tmpfile))
  16913. + goto out_parent;
  16914. +
  16915. + h_mnt = au_sbr_mnt(sb, bindex);
  16916. + err = vfsub_mnt_want_write(h_mnt);
  16917. + if (unlikely(err))
  16918. + goto out_parent;
  16919. +
  16920. + h_parent = au_h_dptr(parent, bindex);
  16921. + err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
  16922. + if (unlikely(err))
  16923. + goto out_mnt;
  16924. +
  16925. + err = -ENOMEM;
  16926. + h_dentry = d_alloc(h_parent, &dentry->d_name);
  16927. + if (unlikely(!h_dentry))
  16928. + goto out_mnt;
  16929. +
  16930. + err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
  16931. + if (unlikely(err))
  16932. + goto out_dentry;
  16933. +
  16934. + au_set_dbtop(dentry, bindex);
  16935. + au_set_dbbot(dentry, bindex);
  16936. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  16937. + inode = au_new_inode(dentry, /*must_new*/1);
  16938. + if (IS_ERR(inode)) {
  16939. + err = PTR_ERR(inode);
  16940. + au_set_h_dptr(dentry, bindex, NULL);
  16941. + au_set_dbtop(dentry, -1);
  16942. + au_set_dbbot(dentry, -1);
  16943. + } else {
  16944. + if (!inode->i_nlink)
  16945. + set_nlink(inode, 1);
  16946. + d_tmpfile(dentry, inode);
  16947. + au_di(dentry)->di_tmpfile = 1;
  16948. +
  16949. + /* update without i_mutex */
  16950. + if (au_ibtop(dir) == au_dbtop(dentry))
  16951. + au_cpup_attr_timesizes(dir);
  16952. + }
  16953. +
  16954. +out_dentry:
  16955. + dput(h_dentry);
  16956. +out_mnt:
  16957. + vfsub_mnt_drop_write(h_mnt);
  16958. +out_parent:
  16959. + di_write_unlock(parent);
  16960. + dput(parent);
  16961. + di_write_unlock(dentry);
  16962. + if (unlikely(err)) {
  16963. + au_di_fin(dentry);
  16964. + dentry->d_fsdata = NULL;
  16965. + }
  16966. +out_si:
  16967. + si_read_unlock(sb);
  16968. +out:
  16969. + inode_unlock(dir);
  16970. + return err;
  16971. +}
  16972. +
  16973. +/* ---------------------------------------------------------------------- */
  16974. +
  16975. +struct au_link_args {
  16976. + aufs_bindex_t bdst, bsrc;
  16977. + struct au_pin pin;
  16978. + struct path h_path;
  16979. + struct dentry *src_parent, *parent;
  16980. +};
  16981. +
  16982. +static int au_cpup_before_link(struct dentry *src_dentry,
  16983. + struct au_link_args *a)
  16984. +{
  16985. + int err;
  16986. + struct dentry *h_src_dentry;
  16987. + struct au_cp_generic cpg = {
  16988. + .dentry = src_dentry,
  16989. + .bdst = a->bdst,
  16990. + .bsrc = a->bsrc,
  16991. + .len = -1,
  16992. + .pin = &a->pin,
  16993. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  16994. + };
  16995. +
  16996. + di_read_lock_parent(a->src_parent, AuLock_IR);
  16997. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  16998. + if (unlikely(err))
  16999. + goto out;
  17000. +
  17001. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  17002. + err = au_pin(&a->pin, src_dentry, a->bdst,
  17003. + au_opt_udba(src_dentry->d_sb),
  17004. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  17005. + if (unlikely(err))
  17006. + goto out;
  17007. +
  17008. + err = au_sio_cpup_simple(&cpg);
  17009. + au_unpin(&a->pin);
  17010. +
  17011. +out:
  17012. + di_read_unlock(a->src_parent, AuLock_IR);
  17013. + return err;
  17014. +}
  17015. +
  17016. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  17017. + struct au_link_args *a)
  17018. +{
  17019. + int err;
  17020. + unsigned char plink;
  17021. + aufs_bindex_t bbot;
  17022. + struct dentry *h_src_dentry;
  17023. + struct inode *h_inode, *inode, *delegated;
  17024. + struct super_block *sb;
  17025. + struct file *h_file;
  17026. +
  17027. + plink = 0;
  17028. + h_inode = NULL;
  17029. + sb = src_dentry->d_sb;
  17030. + inode = d_inode(src_dentry);
  17031. + if (au_ibtop(inode) <= a->bdst)
  17032. + h_inode = au_h_iptr(inode, a->bdst);
  17033. + if (!h_inode || !h_inode->i_nlink) {
  17034. + /* copyup src_dentry as the name of dentry. */
  17035. + bbot = au_dbbot(dentry);
  17036. + if (bbot < a->bsrc)
  17037. + au_set_dbbot(dentry, a->bsrc);
  17038. + au_set_h_dptr(dentry, a->bsrc,
  17039. + dget(au_h_dptr(src_dentry, a->bsrc)));
  17040. + dget(a->h_path.dentry);
  17041. + au_set_h_dptr(dentry, a->bdst, NULL);
  17042. + AuDbg("temporary d_inode...\n");
  17043. + spin_lock(&dentry->d_lock);
  17044. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  17045. + spin_unlock(&dentry->d_lock);
  17046. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  17047. + if (IS_ERR(h_file))
  17048. + err = PTR_ERR(h_file);
  17049. + else {
  17050. + struct au_cp_generic cpg = {
  17051. + .dentry = dentry,
  17052. + .bdst = a->bdst,
  17053. + .bsrc = -1,
  17054. + .len = -1,
  17055. + .pin = &a->pin,
  17056. + .flags = AuCpup_KEEPLINO
  17057. + };
  17058. + err = au_sio_cpup_simple(&cpg);
  17059. + au_h_open_post(dentry, a->bsrc, h_file);
  17060. + if (!err) {
  17061. + dput(a->h_path.dentry);
  17062. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  17063. + } else
  17064. + au_set_h_dptr(dentry, a->bdst,
  17065. + a->h_path.dentry);
  17066. + }
  17067. + spin_lock(&dentry->d_lock);
  17068. + dentry->d_inode = NULL; /* restore */
  17069. + spin_unlock(&dentry->d_lock);
  17070. + AuDbg("temporary d_inode...done\n");
  17071. + au_set_h_dptr(dentry, a->bsrc, NULL);
  17072. + au_set_dbbot(dentry, bbot);
  17073. + } else {
  17074. + /* the inode of src_dentry already exists on a.bdst branch */
  17075. + h_src_dentry = d_find_alias(h_inode);
  17076. + if (!h_src_dentry && au_plink_test(inode)) {
  17077. + plink = 1;
  17078. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  17079. + err = PTR_ERR(h_src_dentry);
  17080. + if (IS_ERR(h_src_dentry))
  17081. + goto out;
  17082. +
  17083. + if (unlikely(d_is_negative(h_src_dentry))) {
  17084. + dput(h_src_dentry);
  17085. + h_src_dentry = NULL;
  17086. + }
  17087. +
  17088. + }
  17089. + if (h_src_dentry) {
  17090. + delegated = NULL;
  17091. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  17092. + &a->h_path, &delegated);
  17093. + if (unlikely(err == -EWOULDBLOCK)) {
  17094. + pr_warn("cannot retry for NFSv4 delegation"
  17095. + " for an internal link\n");
  17096. + iput(delegated);
  17097. + }
  17098. + dput(h_src_dentry);
  17099. + } else {
  17100. + AuIOErr("no dentry found for hi%lu on b%d\n",
  17101. + h_inode->i_ino, a->bdst);
  17102. + err = -EIO;
  17103. + }
  17104. + }
  17105. +
  17106. + if (!err && !plink)
  17107. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  17108. +
  17109. +out:
  17110. + AuTraceErr(err);
  17111. + return err;
  17112. +}
  17113. +
  17114. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  17115. + struct dentry *dentry)
  17116. +{
  17117. + int err, rerr;
  17118. + struct au_dtime dt;
  17119. + struct au_link_args *a;
  17120. + struct dentry *wh_dentry, *h_src_dentry;
  17121. + struct inode *inode, *delegated;
  17122. + struct super_block *sb;
  17123. + struct au_wr_dir_args wr_dir_args = {
  17124. + /* .force_btgt = -1, */
  17125. + .flags = AuWrDir_ADD_ENTRY
  17126. + };
  17127. +
  17128. + IMustLock(dir);
  17129. + inode = d_inode(src_dentry);
  17130. + IMustLock(inode);
  17131. +
  17132. + err = -ENOMEM;
  17133. + a = kzalloc(sizeof(*a), GFP_NOFS);
  17134. + if (unlikely(!a))
  17135. + goto out;
  17136. +
  17137. + a->parent = dentry->d_parent; /* dir inode is locked */
  17138. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  17139. + AuLock_NOPLM | AuLock_GEN);
  17140. + if (unlikely(err))
  17141. + goto out_kfree;
  17142. + err = au_d_linkable(src_dentry);
  17143. + if (unlikely(err))
  17144. + goto out_unlock;
  17145. + err = au_d_may_add(dentry);
  17146. + if (unlikely(err))
  17147. + goto out_unlock;
  17148. +
  17149. + a->src_parent = dget_parent(src_dentry);
  17150. + wr_dir_args.force_btgt = au_ibtop(inode);
  17151. +
  17152. + di_write_lock_parent(a->parent);
  17153. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  17154. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  17155. + &wr_dir_args);
  17156. + err = PTR_ERR(wh_dentry);
  17157. + if (IS_ERR(wh_dentry))
  17158. + goto out_parent;
  17159. +
  17160. + err = 0;
  17161. + sb = dentry->d_sb;
  17162. + a->bdst = au_dbtop(dentry);
  17163. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  17164. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  17165. + a->bsrc = au_ibtop(inode);
  17166. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  17167. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  17168. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  17169. + if (!h_src_dentry) {
  17170. + a->bsrc = au_dbtop(src_dentry);
  17171. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  17172. + AuDebugOn(!h_src_dentry);
  17173. + } else if (IS_ERR(h_src_dentry)) {
  17174. + err = PTR_ERR(h_src_dentry);
  17175. + goto out_parent;
  17176. + }
  17177. +
  17178. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  17179. + if (a->bdst < a->bsrc
  17180. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  17181. + err = au_cpup_or_link(src_dentry, dentry, a);
  17182. + else {
  17183. + delegated = NULL;
  17184. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  17185. + &a->h_path, &delegated);
  17186. + if (unlikely(err == -EWOULDBLOCK)) {
  17187. + pr_warn("cannot retry for NFSv4 delegation"
  17188. + " for an internal link\n");
  17189. + iput(delegated);
  17190. + }
  17191. + }
  17192. + dput(h_src_dentry);
  17193. + } else {
  17194. + /*
  17195. + * copyup src_dentry to the branch we process,
  17196. + * and then link(2) to it.
  17197. + */
  17198. + dput(h_src_dentry);
  17199. + if (a->bdst < a->bsrc
  17200. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  17201. + au_unpin(&a->pin);
  17202. + di_write_unlock(a->parent);
  17203. + err = au_cpup_before_link(src_dentry, a);
  17204. + di_write_lock_parent(a->parent);
  17205. + if (!err)
  17206. + err = au_pin(&a->pin, dentry, a->bdst,
  17207. + au_opt_udba(sb),
  17208. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  17209. + if (unlikely(err))
  17210. + goto out_wh;
  17211. + }
  17212. + if (!err) {
  17213. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  17214. + err = -ENOENT;
  17215. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  17216. + delegated = NULL;
  17217. + err = vfsub_link(h_src_dentry,
  17218. + au_pinned_h_dir(&a->pin),
  17219. + &a->h_path, &delegated);
  17220. + if (unlikely(err == -EWOULDBLOCK)) {
  17221. + pr_warn("cannot retry"
  17222. + " for NFSv4 delegation"
  17223. + " for an internal link\n");
  17224. + iput(delegated);
  17225. + }
  17226. + }
  17227. + }
  17228. + }
  17229. + if (unlikely(err))
  17230. + goto out_unpin;
  17231. +
  17232. + if (wh_dentry) {
  17233. + a->h_path.dentry = wh_dentry;
  17234. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  17235. + dentry);
  17236. + if (unlikely(err))
  17237. + goto out_revert;
  17238. + }
  17239. +
  17240. + au_dir_ts(dir, a->bdst);
  17241. + dir->i_version++;
  17242. + inc_nlink(inode);
  17243. + inode->i_ctime = dir->i_ctime;
  17244. + d_instantiate(dentry, au_igrab(inode));
  17245. + if (d_unhashed(a->h_path.dentry))
  17246. + /* some filesystem calls d_drop() */
  17247. + d_drop(dentry);
  17248. + /* some filesystems consume an inode even hardlink */
  17249. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  17250. + goto out_unpin; /* success */
  17251. +
  17252. +out_revert:
  17253. + /* no delegation since it is just created */
  17254. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  17255. + /*delegated*/NULL, /*force*/0);
  17256. + if (unlikely(rerr)) {
  17257. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  17258. + err = -EIO;
  17259. + }
  17260. + au_dtime_revert(&dt);
  17261. +out_unpin:
  17262. + au_unpin(&a->pin);
  17263. +out_wh:
  17264. + dput(wh_dentry);
  17265. +out_parent:
  17266. + di_write_unlock(a->parent);
  17267. + dput(a->src_parent);
  17268. +out_unlock:
  17269. + if (unlikely(err)) {
  17270. + au_update_dbtop(dentry);
  17271. + d_drop(dentry);
  17272. + }
  17273. + aufs_read_and_write_unlock2(dentry, src_dentry);
  17274. +out_kfree:
  17275. + au_delayed_kfree(a);
  17276. +out:
  17277. + AuTraceErr(err);
  17278. + return err;
  17279. +}
  17280. +
  17281. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  17282. +{
  17283. + int err, rerr;
  17284. + aufs_bindex_t bindex;
  17285. + unsigned char diropq;
  17286. + struct path h_path;
  17287. + struct dentry *wh_dentry, *parent, *opq_dentry;
  17288. + struct inode *h_inode;
  17289. + struct super_block *sb;
  17290. + struct {
  17291. + struct au_pin pin;
  17292. + struct au_dtime dt;
  17293. + } *a; /* reduce the stack usage */
  17294. + struct au_wr_dir_args wr_dir_args = {
  17295. + .force_btgt = -1,
  17296. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  17297. + };
  17298. +
  17299. + IMustLock(dir);
  17300. +
  17301. + err = -ENOMEM;
  17302. + a = kmalloc(sizeof(*a), GFP_NOFS);
  17303. + if (unlikely(!a))
  17304. + goto out;
  17305. +
  17306. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  17307. + if (unlikely(err))
  17308. + goto out_free;
  17309. + err = au_d_may_add(dentry);
  17310. + if (unlikely(err))
  17311. + goto out_unlock;
  17312. +
  17313. + parent = dentry->d_parent; /* dir inode is locked */
  17314. + di_write_lock_parent(parent);
  17315. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  17316. + &a->pin, &wr_dir_args);
  17317. + err = PTR_ERR(wh_dentry);
  17318. + if (IS_ERR(wh_dentry))
  17319. + goto out_parent;
  17320. +
  17321. + sb = dentry->d_sb;
  17322. + bindex = au_dbtop(dentry);
  17323. + h_path.dentry = au_h_dptr(dentry, bindex);
  17324. + h_path.mnt = au_sbr_mnt(sb, bindex);
  17325. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  17326. + if (unlikely(err))
  17327. + goto out_unpin;
  17328. +
  17329. + /* make the dir opaque */
  17330. + diropq = 0;
  17331. + h_inode = d_inode(h_path.dentry);
  17332. + if (wh_dentry
  17333. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  17334. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  17335. + opq_dentry = au_diropq_create(dentry, bindex);
  17336. + inode_unlock(h_inode);
  17337. + err = PTR_ERR(opq_dentry);
  17338. + if (IS_ERR(opq_dentry))
  17339. + goto out_dir;
  17340. + dput(opq_dentry);
  17341. + diropq = 1;
  17342. + }
  17343. +
  17344. + err = epilog(dir, bindex, wh_dentry, dentry);
  17345. + if (!err) {
  17346. + inc_nlink(dir);
  17347. + goto out_unpin; /* success */
  17348. + }
  17349. +
  17350. + /* revert */
  17351. + if (diropq) {
  17352. + AuLabel(revert opq);
  17353. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  17354. + rerr = au_diropq_remove(dentry, bindex);
  17355. + inode_unlock(h_inode);
  17356. + if (rerr) {
  17357. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  17358. + dentry, err, rerr);
  17359. + err = -EIO;
  17360. + }
  17361. + }
  17362. +
  17363. +out_dir:
  17364. + AuLabel(revert dir);
  17365. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  17366. + if (rerr) {
  17367. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  17368. + dentry, err, rerr);
  17369. + err = -EIO;
  17370. + }
  17371. + au_dtime_revert(&a->dt);
  17372. +out_unpin:
  17373. + au_unpin(&a->pin);
  17374. + dput(wh_dentry);
  17375. +out_parent:
  17376. + di_write_unlock(parent);
  17377. +out_unlock:
  17378. + if (unlikely(err)) {
  17379. + au_update_dbtop(dentry);
  17380. + d_drop(dentry);
  17381. + }
  17382. + aufs_read_unlock(dentry, AuLock_DW);
  17383. +out_free:
  17384. + au_delayed_kfree(a);
  17385. +out:
  17386. + return err;
  17387. +}
  17388. diff -Naur null/fs/aufs/i_op.c linux-4.7/fs/aufs/i_op.c
  17389. --- /dev/null
  17390. +++ linux-4.7/fs/aufs/i_op.c 2016-08-08 12:57:44.602384674 -0400
  17391. @@ -0,0 +1,1413 @@
  17392. +/*
  17393. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  17394. + *
  17395. + * This program, aufs is free software; you can redistribute it and/or modify
  17396. + * it under the terms of the GNU General Public License as published by
  17397. + * the Free Software Foundation; either version 2 of the License, or
  17398. + * (at your option) any later version.
  17399. + *
  17400. + * This program is distributed in the hope that it will be useful,
  17401. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17402. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17403. + * GNU General Public License for more details.
  17404. + *
  17405. + * You should have received a copy of the GNU General Public License
  17406. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17407. + */
  17408. +
  17409. +/*
  17410. + * inode operations (except add/del/rename)
  17411. + */
  17412. +
  17413. +#include <linux/device_cgroup.h>
  17414. +#include <linux/fs_stack.h>
  17415. +#include <linux/namei.h>
  17416. +#include <linux/security.h>
  17417. +#include "aufs.h"
  17418. +
  17419. +static int h_permission(struct inode *h_inode, int mask,
  17420. + struct path *h_path, int brperm)
  17421. +{
  17422. + int err;
  17423. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  17424. +
  17425. + err = -EACCES;
  17426. + if ((write_mask && IS_IMMUTABLE(h_inode))
  17427. + || ((mask & MAY_EXEC)
  17428. + && S_ISREG(h_inode->i_mode)
  17429. + && (path_noexec(h_path)
  17430. + || !(h_inode->i_mode & S_IXUGO))))
  17431. + goto out;
  17432. +
  17433. + /*
  17434. + * - skip the lower fs test in the case of write to ro branch.
  17435. + * - nfs dir permission write check is optimized, but a policy for
  17436. + * link/rename requires a real check.
  17437. + * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
  17438. + * in this case, generic_permission() returns -EOPNOTSUPP.
  17439. + */
  17440. + if ((write_mask && !au_br_writable(brperm))
  17441. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  17442. + && write_mask && !(mask & MAY_READ))
  17443. + || !h_inode->i_op->permission) {
  17444. + /* AuLabel(generic_permission); */
  17445. + /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
  17446. + err = generic_permission(h_inode, mask);
  17447. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  17448. + err = h_inode->i_op->permission(h_inode, mask);
  17449. + AuTraceErr(err);
  17450. + } else {
  17451. + /* AuLabel(h_inode->permission); */
  17452. + err = h_inode->i_op->permission(h_inode, mask);
  17453. + AuTraceErr(err);
  17454. + }
  17455. +
  17456. + if (!err)
  17457. + err = devcgroup_inode_permission(h_inode, mask);
  17458. + if (!err)
  17459. + err = security_inode_permission(h_inode, mask);
  17460. +
  17461. +#if 0
  17462. + if (!err) {
  17463. + /* todo: do we need to call ima_path_check()? */
  17464. + struct path h_path = {
  17465. + .dentry =
  17466. + .mnt = h_mnt
  17467. + };
  17468. + err = ima_path_check(&h_path,
  17469. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  17470. + IMA_COUNT_LEAVE);
  17471. + }
  17472. +#endif
  17473. +
  17474. +out:
  17475. + return err;
  17476. +}
  17477. +
  17478. +static int aufs_permission(struct inode *inode, int mask)
  17479. +{
  17480. + int err;
  17481. + aufs_bindex_t bindex, bbot;
  17482. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  17483. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  17484. + struct inode *h_inode;
  17485. + struct super_block *sb;
  17486. + struct au_branch *br;
  17487. +
  17488. + /* todo: support rcu-walk? */
  17489. + if (mask & MAY_NOT_BLOCK)
  17490. + return -ECHILD;
  17491. +
  17492. + sb = inode->i_sb;
  17493. + si_read_lock(sb, AuLock_FLUSH);
  17494. + ii_read_lock_child(inode);
  17495. +#if 0
  17496. + err = au_iigen_test(inode, au_sigen(sb));
  17497. + if (unlikely(err))
  17498. + goto out;
  17499. +#endif
  17500. +
  17501. + if (!isdir
  17502. + || write_mask
  17503. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  17504. + err = au_busy_or_stale();
  17505. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  17506. + if (unlikely(!h_inode
  17507. + || (h_inode->i_mode & S_IFMT)
  17508. + != (inode->i_mode & S_IFMT)))
  17509. + goto out;
  17510. +
  17511. + err = 0;
  17512. + bindex = au_ibtop(inode);
  17513. + br = au_sbr(sb, bindex);
  17514. + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
  17515. + if (write_mask
  17516. + && !err
  17517. + && !special_file(h_inode->i_mode)) {
  17518. + /* test whether the upper writable branch exists */
  17519. + err = -EROFS;
  17520. + for (; bindex >= 0; bindex--)
  17521. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  17522. + err = 0;
  17523. + break;
  17524. + }
  17525. + }
  17526. + goto out;
  17527. + }
  17528. +
  17529. + /* non-write to dir */
  17530. + err = 0;
  17531. + bbot = au_ibbot(inode);
  17532. + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
  17533. + h_inode = au_h_iptr(inode, bindex);
  17534. + if (h_inode) {
  17535. + err = au_busy_or_stale();
  17536. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  17537. + break;
  17538. +
  17539. + br = au_sbr(sb, bindex);
  17540. + err = h_permission(h_inode, mask, &br->br_path,
  17541. + br->br_perm);
  17542. + }
  17543. + }
  17544. +
  17545. +out:
  17546. + ii_read_unlock(inode);
  17547. + si_read_unlock(sb);
  17548. + return err;
  17549. +}
  17550. +
  17551. +/* ---------------------------------------------------------------------- */
  17552. +
  17553. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  17554. + unsigned int flags)
  17555. +{
  17556. + struct dentry *ret, *parent;
  17557. + struct inode *inode;
  17558. + struct super_block *sb;
  17559. + int err, npositive;
  17560. +
  17561. + IMustLock(dir);
  17562. +
  17563. + /* todo: support rcu-walk? */
  17564. + ret = ERR_PTR(-ECHILD);
  17565. + if (flags & LOOKUP_RCU)
  17566. + goto out;
  17567. +
  17568. + ret = ERR_PTR(-ENAMETOOLONG);
  17569. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  17570. + goto out;
  17571. +
  17572. + sb = dir->i_sb;
  17573. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  17574. + ret = ERR_PTR(err);
  17575. + if (unlikely(err))
  17576. + goto out;
  17577. +
  17578. + err = au_di_init(dentry);
  17579. + ret = ERR_PTR(err);
  17580. + if (unlikely(err))
  17581. + goto out_si;
  17582. +
  17583. + inode = NULL;
  17584. + npositive = 0; /* suppress a warning */
  17585. + parent = dentry->d_parent; /* dir inode is locked */
  17586. + di_read_lock_parent(parent, AuLock_IR);
  17587. + err = au_alive_dir(parent);
  17588. + if (!err)
  17589. + err = au_digen_test(parent, au_sigen(sb));
  17590. + if (!err) {
  17591. + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
  17592. + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
  17593. + AuLkup_ALLOW_NEG);
  17594. + err = npositive;
  17595. + }
  17596. + di_read_unlock(parent, AuLock_IR);
  17597. + ret = ERR_PTR(err);
  17598. + if (unlikely(err < 0))
  17599. + goto out_unlock;
  17600. +
  17601. + if (npositive) {
  17602. + inode = au_new_inode(dentry, /*must_new*/0);
  17603. + if (IS_ERR(inode)) {
  17604. + ret = (void *)inode;
  17605. + inode = NULL;
  17606. + goto out_unlock;
  17607. + }
  17608. + }
  17609. +
  17610. + if (inode)
  17611. + atomic_inc(&inode->i_count);
  17612. + ret = d_splice_alias(inode, dentry);
  17613. +#if 0
  17614. + if (unlikely(d_need_lookup(dentry))) {
  17615. + spin_lock(&dentry->d_lock);
  17616. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  17617. + spin_unlock(&dentry->d_lock);
  17618. + } else
  17619. +#endif
  17620. + if (inode) {
  17621. + if (!IS_ERR(ret)) {
  17622. + iput(inode);
  17623. + if (ret && ret != dentry)
  17624. + ii_write_unlock(inode);
  17625. + } else {
  17626. + ii_write_unlock(inode);
  17627. + iput(inode);
  17628. + inode = NULL;
  17629. + }
  17630. + }
  17631. +
  17632. +out_unlock:
  17633. + di_write_unlock(dentry);
  17634. +out_si:
  17635. + si_read_unlock(sb);
  17636. +out:
  17637. + return ret;
  17638. +}
  17639. +
  17640. +/* ---------------------------------------------------------------------- */
  17641. +
  17642. +struct aopen_node {
  17643. + struct hlist_node hlist;
  17644. + struct file *file, *h_file;
  17645. +};
  17646. +
  17647. +static int au_do_aopen(struct inode *inode, struct file *file)
  17648. +{
  17649. + struct au_sphlhead *aopen;
  17650. + struct aopen_node *node;
  17651. + struct au_do_open_args args = {
  17652. + .no_lock = 1,
  17653. + .open = au_do_open_nondir
  17654. + };
  17655. +
  17656. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  17657. + spin_lock(&aopen->spin);
  17658. + hlist_for_each_entry(node, &aopen->head, hlist)
  17659. + if (node->file == file) {
  17660. + args.h_file = node->h_file;
  17661. + break;
  17662. + }
  17663. + spin_unlock(&aopen->spin);
  17664. + /* AuDebugOn(!args.h_file); */
  17665. +
  17666. + return au_do_open(file, &args);
  17667. +}
  17668. +
  17669. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  17670. + struct file *file, unsigned int open_flag,
  17671. + umode_t create_mode, int *opened)
  17672. +{
  17673. + int err, h_opened = *opened;
  17674. + unsigned int lkup_flags;
  17675. + struct dentry *parent, *d;
  17676. + struct au_sphlhead *aopen;
  17677. + struct vfsub_aopen_args args = {
  17678. + .open_flag = open_flag,
  17679. + .create_mode = create_mode,
  17680. + .opened = &h_opened
  17681. + };
  17682. + struct aopen_node aopen_node = {
  17683. + .file = file
  17684. + };
  17685. +
  17686. + IMustLock(dir);
  17687. + AuDbg("open_flag 0%o\n", open_flag);
  17688. + AuDbgDentry(dentry);
  17689. +
  17690. + err = 0;
  17691. + if (!au_di(dentry)) {
  17692. + lkup_flags = LOOKUP_OPEN;
  17693. + if (open_flag & O_CREAT)
  17694. + lkup_flags |= LOOKUP_CREATE;
  17695. + d = aufs_lookup(dir, dentry, lkup_flags);
  17696. + if (IS_ERR(d)) {
  17697. + err = PTR_ERR(d);
  17698. + AuTraceErr(err);
  17699. + goto out;
  17700. + } else if (d) {
  17701. + /*
  17702. + * obsoleted dentry found.
  17703. + * another error will be returned later.
  17704. + */
  17705. + d_drop(d);
  17706. + AuDbgDentry(d);
  17707. + dput(d);
  17708. + }
  17709. + AuDbgDentry(dentry);
  17710. + }
  17711. +
  17712. + if (d_is_positive(dentry)
  17713. + || d_unhashed(dentry)
  17714. + || d_unlinked(dentry)
  17715. + || !(open_flag & O_CREAT))
  17716. + goto out_no_open;
  17717. +
  17718. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  17719. + if (unlikely(err))
  17720. + goto out;
  17721. +
  17722. + parent = dentry->d_parent; /* dir is locked */
  17723. + di_write_lock_parent(parent);
  17724. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  17725. + if (unlikely(err))
  17726. + goto out_unlock;
  17727. +
  17728. + AuDbgDentry(dentry);
  17729. + if (d_is_positive(dentry))
  17730. + goto out_unlock;
  17731. +
  17732. + args.file = get_empty_filp();
  17733. + err = PTR_ERR(args.file);
  17734. + if (IS_ERR(args.file))
  17735. + goto out_unlock;
  17736. +
  17737. + args.file->f_flags = file->f_flags;
  17738. + err = au_aopen_or_create(dir, dentry, &args);
  17739. + AuTraceErr(err);
  17740. + AuDbgFile(args.file);
  17741. + if (unlikely(err < 0)) {
  17742. + if (h_opened & FILE_OPENED)
  17743. + fput(args.file);
  17744. + else
  17745. + put_filp(args.file);
  17746. + goto out_unlock;
  17747. + }
  17748. +
  17749. + /* some filesystems don't set FILE_CREATED while succeeded? */
  17750. + *opened |= FILE_CREATED;
  17751. + if (h_opened & FILE_OPENED)
  17752. + aopen_node.h_file = args.file;
  17753. + else {
  17754. + put_filp(args.file);
  17755. + args.file = NULL;
  17756. + }
  17757. + aopen = &au_sbi(dir->i_sb)->si_aopen;
  17758. + au_sphl_add(&aopen_node.hlist, aopen);
  17759. + err = finish_open(file, dentry, au_do_aopen, opened);
  17760. + au_sphl_del(&aopen_node.hlist, aopen);
  17761. + AuTraceErr(err);
  17762. + AuDbgFile(file);
  17763. + if (aopen_node.h_file)
  17764. + fput(aopen_node.h_file);
  17765. +
  17766. +out_unlock:
  17767. + di_write_unlock(parent);
  17768. + aufs_read_unlock(dentry, AuLock_DW);
  17769. + AuDbgDentry(dentry);
  17770. + if (unlikely(err < 0))
  17771. + goto out;
  17772. +out_no_open:
  17773. + if (err >= 0 && !(*opened & FILE_CREATED)) {
  17774. + AuLabel(out_no_open);
  17775. + dget(dentry);
  17776. + err = finish_no_open(file, dentry);
  17777. + }
  17778. +out:
  17779. + AuDbg("%pd%s%s\n", dentry,
  17780. + (*opened & FILE_CREATED) ? " created" : "",
  17781. + (*opened & FILE_OPENED) ? " opened" : "");
  17782. + AuTraceErr(err);
  17783. + return err;
  17784. +}
  17785. +
  17786. +
  17787. +/* ---------------------------------------------------------------------- */
  17788. +
  17789. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  17790. + const unsigned char add_entry, aufs_bindex_t bcpup,
  17791. + aufs_bindex_t btop)
  17792. +{
  17793. + int err;
  17794. + struct dentry *h_parent;
  17795. + struct inode *h_dir;
  17796. +
  17797. + if (add_entry)
  17798. + IMustLock(d_inode(parent));
  17799. + else
  17800. + di_write_lock_parent(parent);
  17801. +
  17802. + err = 0;
  17803. + if (!au_h_dptr(parent, bcpup)) {
  17804. + if (btop > bcpup)
  17805. + err = au_cpup_dirs(dentry, bcpup);
  17806. + else if (btop < bcpup)
  17807. + err = au_cpdown_dirs(dentry, bcpup);
  17808. + else
  17809. + BUG();
  17810. + }
  17811. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  17812. + h_parent = au_h_dptr(parent, bcpup);
  17813. + h_dir = d_inode(h_parent);
  17814. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  17815. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  17816. + /* todo: no unlock here */
  17817. + inode_unlock(h_dir);
  17818. +
  17819. + AuDbg("bcpup %d\n", bcpup);
  17820. + if (!err) {
  17821. + if (d_really_is_negative(dentry))
  17822. + au_set_h_dptr(dentry, btop, NULL);
  17823. + au_update_dbrange(dentry, /*do_put_zero*/0);
  17824. + }
  17825. + }
  17826. +
  17827. + if (!add_entry)
  17828. + di_write_unlock(parent);
  17829. + if (!err)
  17830. + err = bcpup; /* success */
  17831. +
  17832. + AuTraceErr(err);
  17833. + return err;
  17834. +}
  17835. +
  17836. +/*
  17837. + * decide the branch and the parent dir where we will create a new entry.
  17838. + * returns new bindex or an error.
  17839. + * copyup the parent dir if needed.
  17840. + */
  17841. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  17842. + struct au_wr_dir_args *args)
  17843. +{
  17844. + int err;
  17845. + unsigned int flags;
  17846. + aufs_bindex_t bcpup, btop, src_btop;
  17847. + const unsigned char add_entry
  17848. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  17849. + | au_ftest_wrdir(args->flags, TMPFILE);
  17850. + struct super_block *sb;
  17851. + struct dentry *parent;
  17852. + struct au_sbinfo *sbinfo;
  17853. +
  17854. + sb = dentry->d_sb;
  17855. + sbinfo = au_sbi(sb);
  17856. + parent = dget_parent(dentry);
  17857. + btop = au_dbtop(dentry);
  17858. + bcpup = btop;
  17859. + if (args->force_btgt < 0) {
  17860. + if (src_dentry) {
  17861. + src_btop = au_dbtop(src_dentry);
  17862. + if (src_btop < btop)
  17863. + bcpup = src_btop;
  17864. + } else if (add_entry) {
  17865. + flags = 0;
  17866. + if (au_ftest_wrdir(args->flags, ISDIR))
  17867. + au_fset_wbr(flags, DIR);
  17868. + err = AuWbrCreate(sbinfo, dentry, flags);
  17869. + bcpup = err;
  17870. + }
  17871. +
  17872. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  17873. + if (add_entry)
  17874. + err = AuWbrCopyup(sbinfo, dentry);
  17875. + else {
  17876. + if (!IS_ROOT(dentry)) {
  17877. + di_read_lock_parent(parent, !AuLock_IR);
  17878. + err = AuWbrCopyup(sbinfo, dentry);
  17879. + di_read_unlock(parent, !AuLock_IR);
  17880. + } else
  17881. + err = AuWbrCopyup(sbinfo, dentry);
  17882. + }
  17883. + bcpup = err;
  17884. + if (unlikely(err < 0))
  17885. + goto out;
  17886. + }
  17887. + } else {
  17888. + bcpup = args->force_btgt;
  17889. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  17890. + }
  17891. +
  17892. + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
  17893. + err = bcpup;
  17894. + if (bcpup == btop)
  17895. + goto out; /* success */
  17896. +
  17897. + /* copyup the new parent into the branch we process */
  17898. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
  17899. + if (err >= 0) {
  17900. + if (d_really_is_negative(dentry)) {
  17901. + au_set_h_dptr(dentry, btop, NULL);
  17902. + au_set_dbtop(dentry, bcpup);
  17903. + au_set_dbbot(dentry, bcpup);
  17904. + }
  17905. + AuDebugOn(add_entry
  17906. + && !au_ftest_wrdir(args->flags, TMPFILE)
  17907. + && !au_h_dptr(dentry, bcpup));
  17908. + }
  17909. +
  17910. +out:
  17911. + dput(parent);
  17912. + return err;
  17913. +}
  17914. +
  17915. +/* ---------------------------------------------------------------------- */
  17916. +
  17917. +void au_pin_hdir_unlock(struct au_pin *p)
  17918. +{
  17919. + if (p->hdir)
  17920. + au_hn_inode_unlock(p->hdir);
  17921. +}
  17922. +
  17923. +int au_pin_hdir_lock(struct au_pin *p)
  17924. +{
  17925. + int err;
  17926. +
  17927. + err = 0;
  17928. + if (!p->hdir)
  17929. + goto out;
  17930. +
  17931. + /* even if an error happens later, keep this lock */
  17932. + au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
  17933. +
  17934. + err = -EBUSY;
  17935. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  17936. + goto out;
  17937. +
  17938. + err = 0;
  17939. + if (p->h_dentry)
  17940. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  17941. + p->h_parent, p->br);
  17942. +
  17943. +out:
  17944. + return err;
  17945. +}
  17946. +
  17947. +int au_pin_hdir_relock(struct au_pin *p)
  17948. +{
  17949. + int err, i;
  17950. + struct inode *h_i;
  17951. + struct dentry *h_d[] = {
  17952. + p->h_dentry,
  17953. + p->h_parent
  17954. + };
  17955. +
  17956. + err = au_pin_hdir_lock(p);
  17957. + if (unlikely(err))
  17958. + goto out;
  17959. +
  17960. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  17961. + if (!h_d[i])
  17962. + continue;
  17963. + if (d_is_positive(h_d[i])) {
  17964. + h_i = d_inode(h_d[i]);
  17965. + err = !h_i->i_nlink;
  17966. + }
  17967. + }
  17968. +
  17969. +out:
  17970. + return err;
  17971. +}
  17972. +
  17973. +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  17974. +{
  17975. +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
  17976. + p->hdir->hi_inode->i_rwsem.owner = task;
  17977. +#endif
  17978. +}
  17979. +
  17980. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  17981. +{
  17982. + if (p->hdir) {
  17983. + rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
  17984. + p->lsc_hi, 0, NULL, _RET_IP_);
  17985. + au_pin_hdir_set_owner(p, current);
  17986. + }
  17987. +}
  17988. +
  17989. +void au_pin_hdir_release(struct au_pin *p)
  17990. +{
  17991. + if (p->hdir) {
  17992. + au_pin_hdir_set_owner(p, p->task);
  17993. + rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
  17994. + }
  17995. +}
  17996. +
  17997. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  17998. +{
  17999. + if (pin && pin->parent)
  18000. + return au_h_dptr(pin->parent, pin->bindex);
  18001. + return NULL;
  18002. +}
  18003. +
  18004. +void au_unpin(struct au_pin *p)
  18005. +{
  18006. + if (p->hdir)
  18007. + au_pin_hdir_unlock(p);
  18008. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  18009. + vfsub_mnt_drop_write(p->h_mnt);
  18010. + if (!p->hdir)
  18011. + return;
  18012. +
  18013. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18014. + di_read_unlock(p->parent, AuLock_IR);
  18015. + iput(p->hdir->hi_inode);
  18016. + dput(p->parent);
  18017. + p->parent = NULL;
  18018. + p->hdir = NULL;
  18019. + p->h_mnt = NULL;
  18020. + /* do not clear p->task */
  18021. +}
  18022. +
  18023. +int au_do_pin(struct au_pin *p)
  18024. +{
  18025. + int err;
  18026. + struct super_block *sb;
  18027. + struct inode *h_dir;
  18028. +
  18029. + err = 0;
  18030. + sb = p->dentry->d_sb;
  18031. + p->br = au_sbr(sb, p->bindex);
  18032. + if (IS_ROOT(p->dentry)) {
  18033. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  18034. + p->h_mnt = au_br_mnt(p->br);
  18035. + err = vfsub_mnt_want_write(p->h_mnt);
  18036. + if (unlikely(err)) {
  18037. + au_fclr_pin(p->flags, MNT_WRITE);
  18038. + goto out_err;
  18039. + }
  18040. + }
  18041. + goto out;
  18042. + }
  18043. +
  18044. + p->h_dentry = NULL;
  18045. + if (p->bindex <= au_dbbot(p->dentry))
  18046. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  18047. +
  18048. + p->parent = dget_parent(p->dentry);
  18049. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18050. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  18051. +
  18052. + h_dir = NULL;
  18053. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  18054. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  18055. + if (p->hdir)
  18056. + h_dir = p->hdir->hi_inode;
  18057. +
  18058. + /*
  18059. + * udba case, or
  18060. + * if DI_LOCKED is not set, then p->parent may be different
  18061. + * and h_parent can be NULL.
  18062. + */
  18063. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  18064. + err = -EBUSY;
  18065. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18066. + di_read_unlock(p->parent, AuLock_IR);
  18067. + dput(p->parent);
  18068. + p->parent = NULL;
  18069. + goto out_err;
  18070. + }
  18071. +
  18072. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  18073. + p->h_mnt = au_br_mnt(p->br);
  18074. + err = vfsub_mnt_want_write(p->h_mnt);
  18075. + if (unlikely(err)) {
  18076. + au_fclr_pin(p->flags, MNT_WRITE);
  18077. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18078. + di_read_unlock(p->parent, AuLock_IR);
  18079. + dput(p->parent);
  18080. + p->parent = NULL;
  18081. + goto out_err;
  18082. + }
  18083. + }
  18084. +
  18085. + au_igrab(h_dir);
  18086. + err = au_pin_hdir_lock(p);
  18087. + if (!err)
  18088. + goto out; /* success */
  18089. +
  18090. + au_unpin(p);
  18091. +
  18092. +out_err:
  18093. + pr_err("err %d\n", err);
  18094. + err = au_busy_or_stale();
  18095. +out:
  18096. + return err;
  18097. +}
  18098. +
  18099. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  18100. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  18101. + unsigned int udba, unsigned char flags)
  18102. +{
  18103. + p->dentry = dentry;
  18104. + p->udba = udba;
  18105. + p->lsc_di = lsc_di;
  18106. + p->lsc_hi = lsc_hi;
  18107. + p->flags = flags;
  18108. + p->bindex = bindex;
  18109. +
  18110. + p->parent = NULL;
  18111. + p->hdir = NULL;
  18112. + p->h_mnt = NULL;
  18113. +
  18114. + p->h_dentry = NULL;
  18115. + p->h_parent = NULL;
  18116. + p->br = NULL;
  18117. + p->task = current;
  18118. +}
  18119. +
  18120. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  18121. + unsigned int udba, unsigned char flags)
  18122. +{
  18123. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  18124. + udba, flags);
  18125. + return au_do_pin(pin);
  18126. +}
  18127. +
  18128. +/* ---------------------------------------------------------------------- */
  18129. +
  18130. +/*
  18131. + * ->setattr() and ->getattr() are called in various cases.
  18132. + * chmod, stat: dentry is revalidated.
  18133. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  18134. + * unhashed.
  18135. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  18136. + */
  18137. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  18138. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  18139. +{
  18140. + int err;
  18141. + struct dentry *parent;
  18142. +
  18143. + err = 0;
  18144. + if (au_digen_test(dentry, sigen)) {
  18145. + parent = dget_parent(dentry);
  18146. + di_read_lock_parent(parent, AuLock_IR);
  18147. + err = au_refresh_dentry(dentry, parent);
  18148. + di_read_unlock(parent, AuLock_IR);
  18149. + dput(parent);
  18150. + }
  18151. +
  18152. + AuTraceErr(err);
  18153. + return err;
  18154. +}
  18155. +
  18156. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  18157. + struct au_icpup_args *a)
  18158. +{
  18159. + int err;
  18160. + loff_t sz;
  18161. + aufs_bindex_t btop, ibtop;
  18162. + struct dentry *hi_wh, *parent;
  18163. + struct inode *inode;
  18164. + struct au_wr_dir_args wr_dir_args = {
  18165. + .force_btgt = -1,
  18166. + .flags = 0
  18167. + };
  18168. +
  18169. + if (d_is_dir(dentry))
  18170. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  18171. + /* plink or hi_wh() case */
  18172. + btop = au_dbtop(dentry);
  18173. + inode = d_inode(dentry);
  18174. + ibtop = au_ibtop(inode);
  18175. + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
  18176. + wr_dir_args.force_btgt = ibtop;
  18177. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  18178. + if (unlikely(err < 0))
  18179. + goto out;
  18180. + a->btgt = err;
  18181. + if (err != btop)
  18182. + au_fset_icpup(a->flags, DID_CPUP);
  18183. +
  18184. + err = 0;
  18185. + a->pin_flags = AuPin_MNT_WRITE;
  18186. + parent = NULL;
  18187. + if (!IS_ROOT(dentry)) {
  18188. + au_fset_pin(a->pin_flags, DI_LOCKED);
  18189. + parent = dget_parent(dentry);
  18190. + di_write_lock_parent(parent);
  18191. + }
  18192. +
  18193. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  18194. + if (unlikely(err))
  18195. + goto out_parent;
  18196. +
  18197. + sz = -1;
  18198. + a->h_path.dentry = au_h_dptr(dentry, btop);
  18199. + a->h_inode = d_inode(a->h_path.dentry);
  18200. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  18201. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  18202. + if (ia->ia_size < i_size_read(a->h_inode))
  18203. + sz = ia->ia_size;
  18204. + inode_unlock(a->h_inode);
  18205. + }
  18206. +
  18207. + hi_wh = NULL;
  18208. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  18209. + hi_wh = au_hi_wh(inode, a->btgt);
  18210. + if (!hi_wh) {
  18211. + struct au_cp_generic cpg = {
  18212. + .dentry = dentry,
  18213. + .bdst = a->btgt,
  18214. + .bsrc = -1,
  18215. + .len = sz,
  18216. + .pin = &a->pin
  18217. + };
  18218. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  18219. + if (unlikely(err))
  18220. + goto out_unlock;
  18221. + hi_wh = au_hi_wh(inode, a->btgt);
  18222. + /* todo: revalidate hi_wh? */
  18223. + }
  18224. + }
  18225. +
  18226. + if (parent) {
  18227. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  18228. + di_downgrade_lock(parent, AuLock_IR);
  18229. + dput(parent);
  18230. + parent = NULL;
  18231. + }
  18232. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  18233. + goto out; /* success */
  18234. +
  18235. + if (!d_unhashed(dentry)) {
  18236. + struct au_cp_generic cpg = {
  18237. + .dentry = dentry,
  18238. + .bdst = a->btgt,
  18239. + .bsrc = btop,
  18240. + .len = sz,
  18241. + .pin = &a->pin,
  18242. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  18243. + };
  18244. + err = au_sio_cpup_simple(&cpg);
  18245. + if (!err)
  18246. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  18247. + } else if (!hi_wh)
  18248. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  18249. + else
  18250. + a->h_path.dentry = hi_wh; /* do not dget here */
  18251. +
  18252. +out_unlock:
  18253. + a->h_inode = d_inode(a->h_path.dentry);
  18254. + if (!err)
  18255. + goto out; /* success */
  18256. + au_unpin(&a->pin);
  18257. +out_parent:
  18258. + if (parent) {
  18259. + di_write_unlock(parent);
  18260. + dput(parent);
  18261. + }
  18262. +out:
  18263. + if (!err)
  18264. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  18265. + return err;
  18266. +}
  18267. +
  18268. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  18269. +{
  18270. + int err;
  18271. + struct inode *inode, *delegated;
  18272. + struct super_block *sb;
  18273. + struct file *file;
  18274. + struct au_icpup_args *a;
  18275. +
  18276. + inode = d_inode(dentry);
  18277. + IMustLock(inode);
  18278. +
  18279. + err = -ENOMEM;
  18280. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18281. + if (unlikely(!a))
  18282. + goto out;
  18283. +
  18284. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  18285. + ia->ia_valid &= ~ATTR_MODE;
  18286. +
  18287. + file = NULL;
  18288. + sb = dentry->d_sb;
  18289. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18290. + if (unlikely(err))
  18291. + goto out_kfree;
  18292. +
  18293. + if (ia->ia_valid & ATTR_FILE) {
  18294. + /* currently ftruncate(2) only */
  18295. + AuDebugOn(!d_is_reg(dentry));
  18296. + file = ia->ia_file;
  18297. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  18298. + if (unlikely(err))
  18299. + goto out_si;
  18300. + ia->ia_file = au_hf_top(file);
  18301. + a->udba = AuOpt_UDBA_NONE;
  18302. + } else {
  18303. + /* fchmod() doesn't pass ia_file */
  18304. + a->udba = au_opt_udba(sb);
  18305. + di_write_lock_child(dentry);
  18306. + /* no d_unlinked(), to set UDBA_NONE for root */
  18307. + if (d_unhashed(dentry))
  18308. + a->udba = AuOpt_UDBA_NONE;
  18309. + if (a->udba != AuOpt_UDBA_NONE) {
  18310. + AuDebugOn(IS_ROOT(dentry));
  18311. + err = au_reval_for_attr(dentry, au_sigen(sb));
  18312. + if (unlikely(err))
  18313. + goto out_dentry;
  18314. + }
  18315. + }
  18316. +
  18317. + err = au_pin_and_icpup(dentry, ia, a);
  18318. + if (unlikely(err < 0))
  18319. + goto out_dentry;
  18320. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  18321. + ia->ia_file = NULL;
  18322. + ia->ia_valid &= ~ATTR_FILE;
  18323. + }
  18324. +
  18325. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  18326. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  18327. + == (ATTR_MODE | ATTR_CTIME)) {
  18328. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  18329. + if (unlikely(err))
  18330. + goto out_unlock;
  18331. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  18332. + && (ia->ia_valid & ATTR_CTIME)) {
  18333. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  18334. + if (unlikely(err))
  18335. + goto out_unlock;
  18336. + }
  18337. +
  18338. + if (ia->ia_valid & ATTR_SIZE) {
  18339. + struct file *f;
  18340. +
  18341. + if (ia->ia_size < i_size_read(inode))
  18342. + /* unmap only */
  18343. + truncate_setsize(inode, ia->ia_size);
  18344. +
  18345. + f = NULL;
  18346. + if (ia->ia_valid & ATTR_FILE)
  18347. + f = ia->ia_file;
  18348. + inode_unlock(a->h_inode);
  18349. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  18350. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  18351. + } else {
  18352. + delegated = NULL;
  18353. + while (1) {
  18354. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  18355. + if (delegated) {
  18356. + err = break_deleg_wait(&delegated);
  18357. + if (!err)
  18358. + continue;
  18359. + }
  18360. + break;
  18361. + }
  18362. + }
  18363. + /*
  18364. + * regardless aufs 'acl' option setting.
  18365. + * why don't all acl-aware fs call this func from their ->setattr()?
  18366. + */
  18367. + if (!err && (ia->ia_valid & ATTR_MODE))
  18368. + err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
  18369. + if (!err)
  18370. + au_cpup_attr_changeable(inode);
  18371. +
  18372. +out_unlock:
  18373. + inode_unlock(a->h_inode);
  18374. + au_unpin(&a->pin);
  18375. + if (unlikely(err))
  18376. + au_update_dbtop(dentry);
  18377. +out_dentry:
  18378. + di_write_unlock(dentry);
  18379. + if (file) {
  18380. + fi_write_unlock(file);
  18381. + ia->ia_file = file;
  18382. + ia->ia_valid |= ATTR_FILE;
  18383. + }
  18384. +out_si:
  18385. + si_read_unlock(sb);
  18386. +out_kfree:
  18387. + au_delayed_kfree(a);
  18388. +out:
  18389. + AuTraceErr(err);
  18390. + return err;
  18391. +}
  18392. +
  18393. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  18394. +static int au_h_path_to_set_attr(struct dentry *dentry,
  18395. + struct au_icpup_args *a, struct path *h_path)
  18396. +{
  18397. + int err;
  18398. + struct super_block *sb;
  18399. +
  18400. + sb = dentry->d_sb;
  18401. + a->udba = au_opt_udba(sb);
  18402. + /* no d_unlinked(), to set UDBA_NONE for root */
  18403. + if (d_unhashed(dentry))
  18404. + a->udba = AuOpt_UDBA_NONE;
  18405. + if (a->udba != AuOpt_UDBA_NONE) {
  18406. + AuDebugOn(IS_ROOT(dentry));
  18407. + err = au_reval_for_attr(dentry, au_sigen(sb));
  18408. + if (unlikely(err))
  18409. + goto out;
  18410. + }
  18411. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  18412. + if (unlikely(err < 0))
  18413. + goto out;
  18414. +
  18415. + h_path->dentry = a->h_path.dentry;
  18416. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  18417. +
  18418. +out:
  18419. + return err;
  18420. +}
  18421. +
  18422. +ssize_t au_srxattr(struct dentry *dentry, struct inode *inode,
  18423. + struct au_srxattr *arg)
  18424. +{
  18425. + int err;
  18426. + struct path h_path;
  18427. + struct super_block *sb;
  18428. + struct au_icpup_args *a;
  18429. + struct inode *h_inode;
  18430. +
  18431. + IMustLock(inode);
  18432. +
  18433. + err = -ENOMEM;
  18434. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18435. + if (unlikely(!a))
  18436. + goto out;
  18437. +
  18438. + sb = dentry->d_sb;
  18439. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18440. + if (unlikely(err))
  18441. + goto out_kfree;
  18442. +
  18443. + h_path.dentry = NULL; /* silence gcc */
  18444. + di_write_lock_child(dentry);
  18445. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  18446. + if (unlikely(err))
  18447. + goto out_di;
  18448. +
  18449. + inode_unlock(a->h_inode);
  18450. + switch (arg->type) {
  18451. + case AU_XATTR_SET:
  18452. + AuDebugOn(d_is_negative(h_path.dentry));
  18453. + err = vfsub_setxattr(h_path.dentry,
  18454. + arg->u.set.name, arg->u.set.value,
  18455. + arg->u.set.size, arg->u.set.flags);
  18456. + break;
  18457. + case AU_XATTR_REMOVE:
  18458. + err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
  18459. + break;
  18460. + case AU_ACL_SET:
  18461. + err = -EOPNOTSUPP;
  18462. + h_inode = d_inode(h_path.dentry);
  18463. + if (h_inode->i_op->set_acl)
  18464. + err = h_inode->i_op->set_acl(h_inode,
  18465. + arg->u.acl_set.acl,
  18466. + arg->u.acl_set.type);
  18467. + break;
  18468. + }
  18469. + if (!err)
  18470. + au_cpup_attr_timesizes(inode);
  18471. +
  18472. + au_unpin(&a->pin);
  18473. + if (unlikely(err))
  18474. + au_update_dbtop(dentry);
  18475. +
  18476. +out_di:
  18477. + di_write_unlock(dentry);
  18478. + si_read_unlock(sb);
  18479. +out_kfree:
  18480. + au_delayed_kfree(a);
  18481. +out:
  18482. + AuTraceErr(err);
  18483. + return err;
  18484. +}
  18485. +#endif
  18486. +
  18487. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  18488. + unsigned int nlink)
  18489. +{
  18490. + unsigned int n;
  18491. +
  18492. + inode->i_mode = st->mode;
  18493. + /* don't i_[ug]id_write() here */
  18494. + inode->i_uid = st->uid;
  18495. + inode->i_gid = st->gid;
  18496. + inode->i_atime = st->atime;
  18497. + inode->i_mtime = st->mtime;
  18498. + inode->i_ctime = st->ctime;
  18499. +
  18500. + au_cpup_attr_nlink(inode, /*force*/0);
  18501. + if (S_ISDIR(inode->i_mode)) {
  18502. + n = inode->i_nlink;
  18503. + n -= nlink;
  18504. + n += st->nlink;
  18505. + smp_mb(); /* for i_nlink */
  18506. + /* 0 can happen */
  18507. + set_nlink(inode, n);
  18508. + }
  18509. +
  18510. + spin_lock(&inode->i_lock);
  18511. + inode->i_blocks = st->blocks;
  18512. + i_size_write(inode, st->size);
  18513. + spin_unlock(&inode->i_lock);
  18514. +}
  18515. +
  18516. +/*
  18517. + * common routine for aufs_getattr() and aufs_getxattr().
  18518. + * returns zero or negative (an error).
  18519. + * @dentry will be read-locked in success.
  18520. + */
  18521. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
  18522. +{
  18523. + int err;
  18524. + unsigned int mnt_flags, sigen;
  18525. + unsigned char udba_none;
  18526. + aufs_bindex_t bindex;
  18527. + struct super_block *sb, *h_sb;
  18528. + struct inode *inode;
  18529. +
  18530. + h_path->mnt = NULL;
  18531. + h_path->dentry = NULL;
  18532. +
  18533. + err = 0;
  18534. + sb = dentry->d_sb;
  18535. + mnt_flags = au_mntflags(sb);
  18536. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  18537. +
  18538. + /* support fstat(2) */
  18539. + if (!d_unlinked(dentry) && !udba_none) {
  18540. + sigen = au_sigen(sb);
  18541. + err = au_digen_test(dentry, sigen);
  18542. + if (!err) {
  18543. + di_read_lock_child(dentry, AuLock_IR);
  18544. + err = au_dbrange_test(dentry);
  18545. + if (unlikely(err)) {
  18546. + di_read_unlock(dentry, AuLock_IR);
  18547. + goto out;
  18548. + }
  18549. + } else {
  18550. + AuDebugOn(IS_ROOT(dentry));
  18551. + di_write_lock_child(dentry);
  18552. + err = au_dbrange_test(dentry);
  18553. + if (!err)
  18554. + err = au_reval_for_attr(dentry, sigen);
  18555. + if (!err)
  18556. + di_downgrade_lock(dentry, AuLock_IR);
  18557. + else {
  18558. + di_write_unlock(dentry);
  18559. + goto out;
  18560. + }
  18561. + }
  18562. + } else
  18563. + di_read_lock_child(dentry, AuLock_IR);
  18564. +
  18565. + inode = d_inode(dentry);
  18566. + bindex = au_ibtop(inode);
  18567. + h_path->mnt = au_sbr_mnt(sb, bindex);
  18568. + h_sb = h_path->mnt->mnt_sb;
  18569. + if (!force
  18570. + && !au_test_fs_bad_iattr(h_sb)
  18571. + && udba_none)
  18572. + goto out; /* success */
  18573. +
  18574. + if (au_dbtop(dentry) == bindex)
  18575. + h_path->dentry = au_h_dptr(dentry, bindex);
  18576. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  18577. + h_path->dentry = au_plink_lkup(inode, bindex);
  18578. + if (IS_ERR(h_path->dentry))
  18579. + /* pretending success */
  18580. + h_path->dentry = NULL;
  18581. + else
  18582. + dput(h_path->dentry);
  18583. + }
  18584. +
  18585. +out:
  18586. + return err;
  18587. +}
  18588. +
  18589. +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
  18590. + struct dentry *dentry, struct kstat *st)
  18591. +{
  18592. + int err;
  18593. + unsigned char positive;
  18594. + struct path h_path;
  18595. + struct inode *inode;
  18596. + struct super_block *sb;
  18597. +
  18598. + inode = d_inode(dentry);
  18599. + sb = dentry->d_sb;
  18600. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18601. + if (unlikely(err))
  18602. + goto out;
  18603. + err = au_h_path_getattr(dentry, /*force*/0, &h_path);
  18604. + if (unlikely(err))
  18605. + goto out_si;
  18606. + if (unlikely(!h_path.dentry))
  18607. + /* illegally overlapped or something */
  18608. + goto out_fill; /* pretending success */
  18609. +
  18610. + positive = d_is_positive(h_path.dentry);
  18611. + if (positive)
  18612. + err = vfs_getattr(&h_path, st);
  18613. + if (!err) {
  18614. + if (positive)
  18615. + au_refresh_iattr(inode, st,
  18616. + d_inode(h_path.dentry)->i_nlink);
  18617. + goto out_fill; /* success */
  18618. + }
  18619. + AuTraceErr(err);
  18620. + goto out_di;
  18621. +
  18622. +out_fill:
  18623. + generic_fillattr(inode, st);
  18624. +out_di:
  18625. + di_read_unlock(dentry, AuLock_IR);
  18626. +out_si:
  18627. + si_read_unlock(sb);
  18628. +out:
  18629. + AuTraceErr(err);
  18630. + return err;
  18631. +}
  18632. +
  18633. +/* ---------------------------------------------------------------------- */
  18634. +
  18635. +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
  18636. + struct delayed_call *done)
  18637. +{
  18638. + const char *ret;
  18639. + struct dentry *h_dentry;
  18640. + struct inode *h_inode;
  18641. + int err;
  18642. + aufs_bindex_t bindex;
  18643. +
  18644. + ret = NULL; /* suppress a warning */
  18645. + err = -ECHILD;
  18646. + if (!dentry)
  18647. + goto out;
  18648. +
  18649. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  18650. + if (unlikely(err))
  18651. + goto out;
  18652. +
  18653. + err = au_d_hashed_positive(dentry);
  18654. + if (unlikely(err))
  18655. + goto out_unlock;
  18656. +
  18657. + err = -EINVAL;
  18658. + inode = d_inode(dentry);
  18659. + bindex = au_ibtop(inode);
  18660. + h_inode = au_h_iptr(inode, bindex);
  18661. + if (unlikely(!h_inode->i_op->get_link))
  18662. + goto out_unlock;
  18663. +
  18664. + err = -EBUSY;
  18665. + h_dentry = NULL;
  18666. + if (au_dbtop(dentry) <= bindex) {
  18667. + h_dentry = au_h_dptr(dentry, bindex);
  18668. + if (h_dentry)
  18669. + dget(h_dentry);
  18670. + }
  18671. + if (!h_dentry) {
  18672. + h_dentry = d_find_any_alias(h_inode);
  18673. + if (IS_ERR(h_dentry)) {
  18674. + err = PTR_ERR(h_dentry);
  18675. + goto out_unlock;
  18676. + }
  18677. + }
  18678. + if (unlikely(!h_dentry))
  18679. + goto out_unlock;
  18680. +
  18681. + err = 0;
  18682. + AuDbg("%pf\n", h_inode->i_op->get_link);
  18683. + AuDbgDentry(h_dentry);
  18684. + ret = h_inode->i_op->get_link(h_dentry, h_inode, done);
  18685. + dput(h_dentry);
  18686. + if (IS_ERR(ret))
  18687. + err = PTR_ERR(ret);
  18688. +
  18689. +out_unlock:
  18690. + aufs_read_unlock(dentry, AuLock_IR);
  18691. +out:
  18692. + if (unlikely(err))
  18693. + ret = ERR_PTR(err);
  18694. + AuTraceErrPtr(ret);
  18695. + return ret;
  18696. +}
  18697. +
  18698. +/* ---------------------------------------------------------------------- */
  18699. +
  18700. +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
  18701. +{
  18702. + int err;
  18703. + struct super_block *sb;
  18704. + struct inode *h_inode;
  18705. +
  18706. + sb = inode->i_sb;
  18707. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  18708. + lockdep_off();
  18709. + si_read_lock(sb, AuLock_FLUSH);
  18710. + ii_write_lock_child(inode);
  18711. + lockdep_on();
  18712. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  18713. + err = vfsub_update_time(h_inode, ts, flags);
  18714. + lockdep_off();
  18715. + if (!err)
  18716. + au_cpup_attr_timesizes(inode);
  18717. + ii_write_unlock(inode);
  18718. + si_read_unlock(sb);
  18719. + lockdep_on();
  18720. +
  18721. + if (!err && (flags & S_VERSION))
  18722. + inode_inc_iversion(inode);
  18723. +
  18724. + return err;
  18725. +}
  18726. +
  18727. +/* ---------------------------------------------------------------------- */
  18728. +
  18729. +/* no getattr version will be set by module.c:aufs_init() */
  18730. +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
  18731. + aufs_iop[] = {
  18732. + [AuIop_SYMLINK] = {
  18733. + .permission = aufs_permission,
  18734. +#ifdef CONFIG_FS_POSIX_ACL
  18735. + .get_acl = aufs_get_acl,
  18736. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  18737. +#endif
  18738. +
  18739. + .setattr = aufs_setattr,
  18740. + .getattr = aufs_getattr,
  18741. +
  18742. +#ifdef CONFIG_AUFS_XATTR
  18743. + .setxattr = aufs_setxattr,
  18744. + .getxattr = aufs_getxattr,
  18745. + .listxattr = aufs_listxattr,
  18746. + .removexattr = aufs_removexattr,
  18747. +#endif
  18748. +
  18749. + .readlink = generic_readlink,
  18750. + .get_link = aufs_get_link,
  18751. +
  18752. + /* .update_time = aufs_update_time */
  18753. + },
  18754. + [AuIop_DIR] = {
  18755. + .create = aufs_create,
  18756. + .lookup = aufs_lookup,
  18757. + .link = aufs_link,
  18758. + .unlink = aufs_unlink,
  18759. + .symlink = aufs_symlink,
  18760. + .mkdir = aufs_mkdir,
  18761. + .rmdir = aufs_rmdir,
  18762. + .mknod = aufs_mknod,
  18763. + .rename = aufs_rename,
  18764. +
  18765. + .permission = aufs_permission,
  18766. +#ifdef CONFIG_FS_POSIX_ACL
  18767. + .get_acl = aufs_get_acl,
  18768. + .set_acl = aufs_set_acl,
  18769. +#endif
  18770. +
  18771. + .setattr = aufs_setattr,
  18772. + .getattr = aufs_getattr,
  18773. +
  18774. +#ifdef CONFIG_AUFS_XATTR
  18775. + .setxattr = aufs_setxattr,
  18776. + .getxattr = aufs_getxattr,
  18777. + .listxattr = aufs_listxattr,
  18778. + .removexattr = aufs_removexattr,
  18779. +#endif
  18780. +
  18781. + .update_time = aufs_update_time,
  18782. + .atomic_open = aufs_atomic_open,
  18783. + .tmpfile = aufs_tmpfile
  18784. + },
  18785. + [AuIop_OTHER] = {
  18786. + .permission = aufs_permission,
  18787. +#ifdef CONFIG_FS_POSIX_ACL
  18788. + .get_acl = aufs_get_acl,
  18789. + .set_acl = aufs_set_acl,
  18790. +#endif
  18791. +
  18792. + .setattr = aufs_setattr,
  18793. + .getattr = aufs_getattr,
  18794. +
  18795. +#ifdef CONFIG_AUFS_XATTR
  18796. + .setxattr = aufs_setxattr,
  18797. + .getxattr = aufs_getxattr,
  18798. + .listxattr = aufs_listxattr,
  18799. + .removexattr = aufs_removexattr,
  18800. +#endif
  18801. +
  18802. + .update_time = aufs_update_time
  18803. + }
  18804. +};
  18805. diff -Naur null/fs/aufs/i_op_del.c linux-4.7/fs/aufs/i_op_del.c
  18806. --- /dev/null
  18807. +++ linux-4.7/fs/aufs/i_op_del.c 2016-08-08 12:57:44.603384652 -0400
  18808. @@ -0,0 +1,511 @@
  18809. +/*
  18810. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  18811. + *
  18812. + * This program, aufs is free software; you can redistribute it and/or modify
  18813. + * it under the terms of the GNU General Public License as published by
  18814. + * the Free Software Foundation; either version 2 of the License, or
  18815. + * (at your option) any later version.
  18816. + *
  18817. + * This program is distributed in the hope that it will be useful,
  18818. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18819. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18820. + * GNU General Public License for more details.
  18821. + *
  18822. + * You should have received a copy of the GNU General Public License
  18823. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18824. + */
  18825. +
  18826. +/*
  18827. + * inode operations (del entry)
  18828. + */
  18829. +
  18830. +#include "aufs.h"
  18831. +
  18832. +/*
  18833. + * decide if a new whiteout for @dentry is necessary or not.
  18834. + * when it is necessary, prepare the parent dir for the upper branch whose
  18835. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  18836. + * be done by caller.
  18837. + * return value:
  18838. + * 0: wh is unnecessary
  18839. + * plus: wh is necessary
  18840. + * minus: error
  18841. + */
  18842. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  18843. +{
  18844. + int need_wh, err;
  18845. + aufs_bindex_t btop;
  18846. + struct super_block *sb;
  18847. +
  18848. + sb = dentry->d_sb;
  18849. + btop = au_dbtop(dentry);
  18850. + if (*bcpup < 0) {
  18851. + *bcpup = btop;
  18852. + if (au_test_ro(sb, btop, d_inode(dentry))) {
  18853. + err = AuWbrCopyup(au_sbi(sb), dentry);
  18854. + *bcpup = err;
  18855. + if (unlikely(err < 0))
  18856. + goto out;
  18857. + }
  18858. + } else
  18859. + AuDebugOn(btop < *bcpup
  18860. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  18861. + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
  18862. +
  18863. + if (*bcpup != btop) {
  18864. + err = au_cpup_dirs(dentry, *bcpup);
  18865. + if (unlikely(err))
  18866. + goto out;
  18867. + need_wh = 1;
  18868. + } else {
  18869. + struct au_dinfo *dinfo, *tmp;
  18870. +
  18871. + need_wh = -ENOMEM;
  18872. + dinfo = au_di(dentry);
  18873. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  18874. + if (tmp) {
  18875. + au_di_cp(tmp, dinfo);
  18876. + au_di_swap(tmp, dinfo);
  18877. + /* returns the number of positive dentries */
  18878. + need_wh = au_lkup_dentry(dentry, btop + 1,
  18879. + /* AuLkup_IGNORE_PERM */ 0);
  18880. + au_di_swap(tmp, dinfo);
  18881. + au_rw_write_unlock(&tmp->di_rwsem);
  18882. + au_di_free(tmp);
  18883. + }
  18884. + }
  18885. + AuDbg("need_wh %d\n", need_wh);
  18886. + err = need_wh;
  18887. +
  18888. +out:
  18889. + return err;
  18890. +}
  18891. +
  18892. +/*
  18893. + * simple tests for the del-entry operations.
  18894. + * following the checks in vfs, plus the parent-child relationship.
  18895. + */
  18896. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  18897. + struct dentry *h_parent, int isdir)
  18898. +{
  18899. + int err;
  18900. + umode_t h_mode;
  18901. + struct dentry *h_dentry, *h_latest;
  18902. + struct inode *h_inode;
  18903. +
  18904. + h_dentry = au_h_dptr(dentry, bindex);
  18905. + if (d_really_is_positive(dentry)) {
  18906. + err = -ENOENT;
  18907. + if (unlikely(d_is_negative(h_dentry)))
  18908. + goto out;
  18909. + h_inode = d_inode(h_dentry);
  18910. + if (unlikely(!h_inode->i_nlink))
  18911. + goto out;
  18912. +
  18913. + h_mode = h_inode->i_mode;
  18914. + if (!isdir) {
  18915. + err = -EISDIR;
  18916. + if (unlikely(S_ISDIR(h_mode)))
  18917. + goto out;
  18918. + } else if (unlikely(!S_ISDIR(h_mode))) {
  18919. + err = -ENOTDIR;
  18920. + goto out;
  18921. + }
  18922. + } else {
  18923. + /* rename(2) case */
  18924. + err = -EIO;
  18925. + if (unlikely(d_is_positive(h_dentry)))
  18926. + goto out;
  18927. + }
  18928. +
  18929. + err = -ENOENT;
  18930. + /* expected parent dir is locked */
  18931. + if (unlikely(h_parent != h_dentry->d_parent))
  18932. + goto out;
  18933. + err = 0;
  18934. +
  18935. + /*
  18936. + * rmdir a dir may break the consistency on some filesystem.
  18937. + * let's try heavy test.
  18938. + */
  18939. + err = -EACCES;
  18940. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  18941. + && au_test_h_perm(d_inode(h_parent),
  18942. + MAY_EXEC | MAY_WRITE)))
  18943. + goto out;
  18944. +
  18945. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  18946. + err = -EIO;
  18947. + if (IS_ERR(h_latest))
  18948. + goto out;
  18949. + if (h_latest == h_dentry)
  18950. + err = 0;
  18951. + dput(h_latest);
  18952. +
  18953. +out:
  18954. + return err;
  18955. +}
  18956. +
  18957. +/*
  18958. + * decide the branch where we operate for @dentry. the branch index will be set
  18959. + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
  18960. + * dir for reverting.
  18961. + * when a new whiteout is necessary, create it.
  18962. + */
  18963. +static struct dentry*
  18964. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  18965. + struct au_dtime *dt, struct au_pin *pin)
  18966. +{
  18967. + struct dentry *wh_dentry;
  18968. + struct super_block *sb;
  18969. + struct path h_path;
  18970. + int err, need_wh;
  18971. + unsigned int udba;
  18972. + aufs_bindex_t bcpup;
  18973. +
  18974. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  18975. + wh_dentry = ERR_PTR(need_wh);
  18976. + if (unlikely(need_wh < 0))
  18977. + goto out;
  18978. +
  18979. + sb = dentry->d_sb;
  18980. + udba = au_opt_udba(sb);
  18981. + bcpup = *rbcpup;
  18982. + err = au_pin(pin, dentry, bcpup, udba,
  18983. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  18984. + wh_dentry = ERR_PTR(err);
  18985. + if (unlikely(err))
  18986. + goto out;
  18987. +
  18988. + h_path.dentry = au_pinned_h_parent(pin);
  18989. + if (udba != AuOpt_UDBA_NONE
  18990. + && au_dbtop(dentry) == bcpup) {
  18991. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  18992. + wh_dentry = ERR_PTR(err);
  18993. + if (unlikely(err))
  18994. + goto out_unpin;
  18995. + }
  18996. +
  18997. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  18998. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  18999. + wh_dentry = NULL;
  19000. + if (!need_wh)
  19001. + goto out; /* success, no need to create whiteout */
  19002. +
  19003. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  19004. + if (IS_ERR(wh_dentry))
  19005. + goto out_unpin;
  19006. +
  19007. + /* returns with the parent is locked and wh_dentry is dget-ed */
  19008. + goto out; /* success */
  19009. +
  19010. +out_unpin:
  19011. + au_unpin(pin);
  19012. +out:
  19013. + return wh_dentry;
  19014. +}
  19015. +
  19016. +/*
  19017. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  19018. + * in order to be revertible and save time for removing many child whiteouts
  19019. + * under the dir.
  19020. + * returns 1 when there are too many child whiteout and caller should remove
  19021. + * them asynchronously. returns 0 when the number of children is enough small to
  19022. + * remove now or the branch fs is a remote fs.
  19023. + * otherwise return an error.
  19024. + */
  19025. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  19026. + struct au_nhash *whlist, struct inode *dir)
  19027. +{
  19028. + int rmdir_later, err, dirwh;
  19029. + struct dentry *h_dentry;
  19030. + struct super_block *sb;
  19031. + struct inode *inode;
  19032. +
  19033. + sb = dentry->d_sb;
  19034. + SiMustAnyLock(sb);
  19035. + h_dentry = au_h_dptr(dentry, bindex);
  19036. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  19037. + if (unlikely(err))
  19038. + goto out;
  19039. +
  19040. + /* stop monitoring */
  19041. + inode = d_inode(dentry);
  19042. + au_hn_free(au_hi(inode, bindex));
  19043. +
  19044. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  19045. + dirwh = au_sbi(sb)->si_dirwh;
  19046. + rmdir_later = (dirwh <= 1);
  19047. + if (!rmdir_later)
  19048. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  19049. + dirwh);
  19050. + if (rmdir_later)
  19051. + return rmdir_later;
  19052. + }
  19053. +
  19054. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  19055. + if (unlikely(err)) {
  19056. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  19057. + h_dentry, bindex, err);
  19058. + err = 0;
  19059. + }
  19060. +
  19061. +out:
  19062. + AuTraceErr(err);
  19063. + return err;
  19064. +}
  19065. +
  19066. +/*
  19067. + * final procedure for deleting a entry.
  19068. + * maintain dentry and iattr.
  19069. + */
  19070. +static void epilog(struct inode *dir, struct dentry *dentry,
  19071. + aufs_bindex_t bindex)
  19072. +{
  19073. + struct inode *inode;
  19074. +
  19075. + inode = d_inode(dentry);
  19076. + d_drop(dentry);
  19077. + inode->i_ctime = dir->i_ctime;
  19078. +
  19079. + au_dir_ts(dir, bindex);
  19080. + dir->i_version++;
  19081. +}
  19082. +
  19083. +/*
  19084. + * when an error happened, remove the created whiteout and revert everything.
  19085. + */
  19086. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  19087. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  19088. + struct dentry *dentry, struct au_dtime *dt)
  19089. +{
  19090. + int rerr;
  19091. + struct path h_path = {
  19092. + .dentry = wh_dentry,
  19093. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  19094. + };
  19095. +
  19096. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  19097. + if (!rerr) {
  19098. + au_set_dbwh(dentry, bwh);
  19099. + au_dtime_revert(dt);
  19100. + return 0;
  19101. + }
  19102. +
  19103. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  19104. + return -EIO;
  19105. +}
  19106. +
  19107. +/* ---------------------------------------------------------------------- */
  19108. +
  19109. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  19110. +{
  19111. + int err;
  19112. + aufs_bindex_t bwh, bindex, btop;
  19113. + struct inode *inode, *h_dir, *delegated;
  19114. + struct dentry *parent, *wh_dentry;
  19115. + /* to reuduce stack size */
  19116. + struct {
  19117. + struct au_dtime dt;
  19118. + struct au_pin pin;
  19119. + struct path h_path;
  19120. + } *a;
  19121. +
  19122. + IMustLock(dir);
  19123. +
  19124. + err = -ENOMEM;
  19125. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19126. + if (unlikely(!a))
  19127. + goto out;
  19128. +
  19129. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  19130. + if (unlikely(err))
  19131. + goto out_free;
  19132. + err = au_d_hashed_positive(dentry);
  19133. + if (unlikely(err))
  19134. + goto out_unlock;
  19135. + inode = d_inode(dentry);
  19136. + IMustLock(inode);
  19137. + err = -EISDIR;
  19138. + if (unlikely(d_is_dir(dentry)))
  19139. + goto out_unlock; /* possible? */
  19140. +
  19141. + btop = au_dbtop(dentry);
  19142. + bwh = au_dbwh(dentry);
  19143. + bindex = -1;
  19144. + parent = dentry->d_parent; /* dir inode is locked */
  19145. + di_write_lock_parent(parent);
  19146. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  19147. + &a->pin);
  19148. + err = PTR_ERR(wh_dentry);
  19149. + if (IS_ERR(wh_dentry))
  19150. + goto out_parent;
  19151. +
  19152. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
  19153. + a->h_path.dentry = au_h_dptr(dentry, btop);
  19154. + dget(a->h_path.dentry);
  19155. + if (bindex == btop) {
  19156. + h_dir = au_pinned_h_dir(&a->pin);
  19157. + delegated = NULL;
  19158. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  19159. + if (unlikely(err == -EWOULDBLOCK)) {
  19160. + pr_warn("cannot retry for NFSv4 delegation"
  19161. + " for an internal unlink\n");
  19162. + iput(delegated);
  19163. + }
  19164. + } else {
  19165. + /* dir inode is locked */
  19166. + h_dir = d_inode(wh_dentry->d_parent);
  19167. + IMustLock(h_dir);
  19168. + err = 0;
  19169. + }
  19170. +
  19171. + if (!err) {
  19172. + vfsub_drop_nlink(inode);
  19173. + epilog(dir, dentry, bindex);
  19174. +
  19175. + /* update target timestamps */
  19176. + if (bindex == btop) {
  19177. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  19178. + /*ignore*/
  19179. + inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  19180. + } else
  19181. + /* todo: this timestamp may be reverted later */
  19182. + inode->i_ctime = h_dir->i_ctime;
  19183. + goto out_unpin; /* success */
  19184. + }
  19185. +
  19186. + /* revert */
  19187. + if (wh_dentry) {
  19188. + int rerr;
  19189. +
  19190. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  19191. + &a->dt);
  19192. + if (rerr)
  19193. + err = rerr;
  19194. + }
  19195. +
  19196. +out_unpin:
  19197. + au_unpin(&a->pin);
  19198. + dput(wh_dentry);
  19199. + dput(a->h_path.dentry);
  19200. +out_parent:
  19201. + di_write_unlock(parent);
  19202. +out_unlock:
  19203. + aufs_read_unlock(dentry, AuLock_DW);
  19204. +out_free:
  19205. + au_delayed_kfree(a);
  19206. +out:
  19207. + return err;
  19208. +}
  19209. +
  19210. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  19211. +{
  19212. + int err, rmdir_later;
  19213. + aufs_bindex_t bwh, bindex, btop;
  19214. + struct inode *inode;
  19215. + struct dentry *parent, *wh_dentry, *h_dentry;
  19216. + struct au_whtmp_rmdir *args;
  19217. + /* to reuduce stack size */
  19218. + struct {
  19219. + struct au_dtime dt;
  19220. + struct au_pin pin;
  19221. + } *a;
  19222. +
  19223. + IMustLock(dir);
  19224. +
  19225. + err = -ENOMEM;
  19226. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19227. + if (unlikely(!a))
  19228. + goto out;
  19229. +
  19230. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  19231. + if (unlikely(err))
  19232. + goto out_free;
  19233. + err = au_alive_dir(dentry);
  19234. + if (unlikely(err))
  19235. + goto out_unlock;
  19236. + inode = d_inode(dentry);
  19237. + IMustLock(inode);
  19238. + err = -ENOTDIR;
  19239. + if (unlikely(!d_is_dir(dentry)))
  19240. + goto out_unlock; /* possible? */
  19241. +
  19242. + err = -ENOMEM;
  19243. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  19244. + if (unlikely(!args))
  19245. + goto out_unlock;
  19246. +
  19247. + parent = dentry->d_parent; /* dir inode is locked */
  19248. + di_write_lock_parent(parent);
  19249. + err = au_test_empty(dentry, &args->whlist);
  19250. + if (unlikely(err))
  19251. + goto out_parent;
  19252. +
  19253. + btop = au_dbtop(dentry);
  19254. + bwh = au_dbwh(dentry);
  19255. + bindex = -1;
  19256. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  19257. + &a->pin);
  19258. + err = PTR_ERR(wh_dentry);
  19259. + if (IS_ERR(wh_dentry))
  19260. + goto out_parent;
  19261. +
  19262. + h_dentry = au_h_dptr(dentry, btop);
  19263. + dget(h_dentry);
  19264. + rmdir_later = 0;
  19265. + if (bindex == btop) {
  19266. + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
  19267. + if (err > 0) {
  19268. + rmdir_later = err;
  19269. + err = 0;
  19270. + }
  19271. + } else {
  19272. + /* stop monitoring */
  19273. + au_hn_free(au_hi(inode, btop));
  19274. +
  19275. + /* dir inode is locked */
  19276. + IMustLock(d_inode(wh_dentry->d_parent));
  19277. + err = 0;
  19278. + }
  19279. +
  19280. + if (!err) {
  19281. + vfsub_dead_dir(inode);
  19282. + au_set_dbdiropq(dentry, -1);
  19283. + epilog(dir, dentry, bindex);
  19284. +
  19285. + if (rmdir_later) {
  19286. + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
  19287. + args = NULL;
  19288. + }
  19289. +
  19290. + goto out_unpin; /* success */
  19291. + }
  19292. +
  19293. + /* revert */
  19294. + AuLabel(revert);
  19295. + if (wh_dentry) {
  19296. + int rerr;
  19297. +
  19298. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  19299. + &a->dt);
  19300. + if (rerr)
  19301. + err = rerr;
  19302. + }
  19303. +
  19304. +out_unpin:
  19305. + au_unpin(&a->pin);
  19306. + dput(wh_dentry);
  19307. + dput(h_dentry);
  19308. +out_parent:
  19309. + di_write_unlock(parent);
  19310. + if (args)
  19311. + au_whtmp_rmdir_free(args);
  19312. +out_unlock:
  19313. + aufs_read_unlock(dentry, AuLock_DW);
  19314. +out_free:
  19315. + au_delayed_kfree(a);
  19316. +out:
  19317. + AuTraceErr(err);
  19318. + return err;
  19319. +}
  19320. diff -Naur null/fs/aufs/i_op_ren.c linux-4.7/fs/aufs/i_op_ren.c
  19321. --- /dev/null
  19322. +++ linux-4.7/fs/aufs/i_op_ren.c 2016-08-08 12:57:44.603384652 -0400
  19323. @@ -0,0 +1,1015 @@
  19324. +/*
  19325. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  19326. + *
  19327. + * This program, aufs is free software; you can redistribute it and/or modify
  19328. + * it under the terms of the GNU General Public License as published by
  19329. + * the Free Software Foundation; either version 2 of the License, or
  19330. + * (at your option) any later version.
  19331. + *
  19332. + * This program is distributed in the hope that it will be useful,
  19333. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19334. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19335. + * GNU General Public License for more details.
  19336. + *
  19337. + * You should have received a copy of the GNU General Public License
  19338. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19339. + */
  19340. +
  19341. +/*
  19342. + * inode operation (rename entry)
  19343. + * todo: this is crazy monster
  19344. + */
  19345. +
  19346. +#include "aufs.h"
  19347. +
  19348. +enum { AuSRC, AuDST, AuSrcDst };
  19349. +enum { AuPARENT, AuCHILD, AuParentChild };
  19350. +
  19351. +#define AuRen_ISDIR 1
  19352. +#define AuRen_ISSAMEDIR (1 << 1)
  19353. +#define AuRen_WHSRC (1 << 2)
  19354. +#define AuRen_WHDST (1 << 3)
  19355. +#define AuRen_MNT_WRITE (1 << 4)
  19356. +#define AuRen_DT_DSTDIR (1 << 5)
  19357. +#define AuRen_DIROPQ (1 << 6)
  19358. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  19359. +#define au_fset_ren(flags, name) \
  19360. + do { (flags) |= AuRen_##name; } while (0)
  19361. +#define au_fclr_ren(flags, name) \
  19362. + do { (flags) &= ~AuRen_##name; } while (0)
  19363. +
  19364. +struct au_ren_args {
  19365. + struct {
  19366. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  19367. + *wh_dentry;
  19368. + struct inode *dir, *inode;
  19369. + struct au_hinode *hdir;
  19370. + struct au_dtime dt[AuParentChild];
  19371. + aufs_bindex_t btop;
  19372. + } sd[AuSrcDst];
  19373. +
  19374. +#define src_dentry sd[AuSRC].dentry
  19375. +#define src_dir sd[AuSRC].dir
  19376. +#define src_inode sd[AuSRC].inode
  19377. +#define src_h_dentry sd[AuSRC].h_dentry
  19378. +#define src_parent sd[AuSRC].parent
  19379. +#define src_h_parent sd[AuSRC].h_parent
  19380. +#define src_wh_dentry sd[AuSRC].wh_dentry
  19381. +#define src_hdir sd[AuSRC].hdir
  19382. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  19383. +#define src_dt sd[AuSRC].dt
  19384. +#define src_btop sd[AuSRC].btop
  19385. +
  19386. +#define dst_dentry sd[AuDST].dentry
  19387. +#define dst_dir sd[AuDST].dir
  19388. +#define dst_inode sd[AuDST].inode
  19389. +#define dst_h_dentry sd[AuDST].h_dentry
  19390. +#define dst_parent sd[AuDST].parent
  19391. +#define dst_h_parent sd[AuDST].h_parent
  19392. +#define dst_wh_dentry sd[AuDST].wh_dentry
  19393. +#define dst_hdir sd[AuDST].hdir
  19394. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  19395. +#define dst_dt sd[AuDST].dt
  19396. +#define dst_btop sd[AuDST].btop
  19397. +
  19398. + struct dentry *h_trap;
  19399. + struct au_branch *br;
  19400. + struct au_hinode *src_hinode;
  19401. + struct path h_path;
  19402. + struct au_nhash whlist;
  19403. + aufs_bindex_t btgt, src_bwh, src_bdiropq;
  19404. +
  19405. + unsigned int flags;
  19406. +
  19407. + struct au_whtmp_rmdir *thargs;
  19408. + struct dentry *h_dst;
  19409. +};
  19410. +
  19411. +/* ---------------------------------------------------------------------- */
  19412. +
  19413. +/*
  19414. + * functions for reverting.
  19415. + * when an error happened in a single rename systemcall, we should revert
  19416. + * everything as if nothing happened.
  19417. + * we don't need to revert the copied-up/down the parent dir since they are
  19418. + * harmless.
  19419. + */
  19420. +
  19421. +#define RevertFailure(fmt, ...) do { \
  19422. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  19423. + ##__VA_ARGS__, err, rerr); \
  19424. + err = -EIO; \
  19425. +} while (0)
  19426. +
  19427. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  19428. +{
  19429. + int rerr;
  19430. +
  19431. + au_hn_inode_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  19432. + rerr = au_diropq_remove(a->src_dentry, a->btgt);
  19433. + au_hn_inode_unlock(a->src_hinode);
  19434. + au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
  19435. + if (rerr)
  19436. + RevertFailure("remove diropq %pd", a->src_dentry);
  19437. +}
  19438. +
  19439. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  19440. +{
  19441. + int rerr;
  19442. + struct inode *delegated;
  19443. +
  19444. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  19445. + a->src_h_parent);
  19446. + rerr = PTR_ERR(a->h_path.dentry);
  19447. + if (IS_ERR(a->h_path.dentry)) {
  19448. + RevertFailure("lkup one %pd", a->src_dentry);
  19449. + return;
  19450. + }
  19451. +
  19452. + delegated = NULL;
  19453. + rerr = vfsub_rename(a->dst_h_dir,
  19454. + au_h_dptr(a->src_dentry, a->btgt),
  19455. + a->src_h_dir, &a->h_path, &delegated);
  19456. + if (unlikely(rerr == -EWOULDBLOCK)) {
  19457. + pr_warn("cannot retry for NFSv4 delegation"
  19458. + " for an internal rename\n");
  19459. + iput(delegated);
  19460. + }
  19461. + d_drop(a->h_path.dentry);
  19462. + dput(a->h_path.dentry);
  19463. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  19464. + if (rerr)
  19465. + RevertFailure("rename %pd", a->src_dentry);
  19466. +}
  19467. +
  19468. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  19469. +{
  19470. + int rerr;
  19471. + struct inode *delegated;
  19472. +
  19473. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  19474. + a->dst_h_parent);
  19475. + rerr = PTR_ERR(a->h_path.dentry);
  19476. + if (IS_ERR(a->h_path.dentry)) {
  19477. + RevertFailure("lkup one %pd", a->dst_dentry);
  19478. + return;
  19479. + }
  19480. + if (d_is_positive(a->h_path.dentry)) {
  19481. + d_drop(a->h_path.dentry);
  19482. + dput(a->h_path.dentry);
  19483. + return;
  19484. + }
  19485. +
  19486. + delegated = NULL;
  19487. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  19488. + &delegated);
  19489. + if (unlikely(rerr == -EWOULDBLOCK)) {
  19490. + pr_warn("cannot retry for NFSv4 delegation"
  19491. + " for an internal rename\n");
  19492. + iput(delegated);
  19493. + }
  19494. + d_drop(a->h_path.dentry);
  19495. + dput(a->h_path.dentry);
  19496. + if (!rerr)
  19497. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  19498. + else
  19499. + RevertFailure("rename %pd", a->h_dst);
  19500. +}
  19501. +
  19502. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  19503. +{
  19504. + int rerr;
  19505. +
  19506. + a->h_path.dentry = a->src_wh_dentry;
  19507. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  19508. + au_set_dbwh(a->src_dentry, a->src_bwh);
  19509. + if (rerr)
  19510. + RevertFailure("unlink %pd", a->src_wh_dentry);
  19511. +}
  19512. +#undef RevertFailure
  19513. +
  19514. +/* ---------------------------------------------------------------------- */
  19515. +
  19516. +/*
  19517. + * when we have to copyup the renaming entry, do it with the rename-target name
  19518. + * in order to minimize the cost (the later actual rename is unnecessary).
  19519. + * otherwise rename it on the target branch.
  19520. + */
  19521. +static int au_ren_or_cpup(struct au_ren_args *a)
  19522. +{
  19523. + int err;
  19524. + struct dentry *d;
  19525. + struct inode *delegated;
  19526. +
  19527. + d = a->src_dentry;
  19528. + if (au_dbtop(d) == a->btgt) {
  19529. + a->h_path.dentry = a->dst_h_dentry;
  19530. + if (au_ftest_ren(a->flags, DIROPQ)
  19531. + && au_dbdiropq(d) == a->btgt)
  19532. + au_fclr_ren(a->flags, DIROPQ);
  19533. + AuDebugOn(au_dbtop(d) != a->btgt);
  19534. + delegated = NULL;
  19535. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  19536. + a->dst_h_dir, &a->h_path, &delegated);
  19537. + if (unlikely(err == -EWOULDBLOCK)) {
  19538. + pr_warn("cannot retry for NFSv4 delegation"
  19539. + " for an internal rename\n");
  19540. + iput(delegated);
  19541. + }
  19542. + } else
  19543. + BUG();
  19544. +
  19545. + if (!err && a->h_dst)
  19546. + /* it will be set to dinfo later */
  19547. + dget(a->h_dst);
  19548. +
  19549. + return err;
  19550. +}
  19551. +
  19552. +/* cf. aufs_rmdir() */
  19553. +static int au_ren_del_whtmp(struct au_ren_args *a)
  19554. +{
  19555. + int err;
  19556. + struct inode *dir;
  19557. +
  19558. + dir = a->dst_dir;
  19559. + SiMustAnyLock(dir->i_sb);
  19560. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  19561. + au_sbi(dir->i_sb)->si_dirwh)
  19562. + || au_test_fs_remote(a->h_dst->d_sb)) {
  19563. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  19564. + if (unlikely(err))
  19565. + pr_warn("failed removing whtmp dir %pd (%d), "
  19566. + "ignored.\n", a->h_dst, err);
  19567. + } else {
  19568. + au_nhash_wh_free(&a->thargs->whlist);
  19569. + a->thargs->whlist = a->whlist;
  19570. + a->whlist.nh_num = 0;
  19571. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  19572. + dput(a->h_dst);
  19573. + a->thargs = NULL;
  19574. + }
  19575. +
  19576. + return 0;
  19577. +}
  19578. +
  19579. +/* make it 'opaque' dir. */
  19580. +static int au_ren_diropq(struct au_ren_args *a)
  19581. +{
  19582. + int err;
  19583. + struct dentry *diropq;
  19584. +
  19585. + err = 0;
  19586. + a->src_bdiropq = au_dbdiropq(a->src_dentry);
  19587. + a->src_hinode = au_hi(a->src_inode, a->btgt);
  19588. + au_hn_inode_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  19589. + diropq = au_diropq_create(a->src_dentry, a->btgt);
  19590. + au_hn_inode_unlock(a->src_hinode);
  19591. + if (IS_ERR(diropq))
  19592. + err = PTR_ERR(diropq);
  19593. + else
  19594. + dput(diropq);
  19595. +
  19596. + return err;
  19597. +}
  19598. +
  19599. +static int do_rename(struct au_ren_args *a)
  19600. +{
  19601. + int err;
  19602. + struct dentry *d, *h_d;
  19603. +
  19604. + /* prepare workqueue args for asynchronous rmdir */
  19605. + h_d = a->dst_h_dentry;
  19606. + if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
  19607. + err = -ENOMEM;
  19608. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
  19609. + if (unlikely(!a->thargs))
  19610. + goto out;
  19611. + a->h_dst = dget(h_d);
  19612. + }
  19613. +
  19614. + /* create whiteout for src_dentry */
  19615. + if (au_ftest_ren(a->flags, WHSRC)) {
  19616. + a->src_bwh = au_dbwh(a->src_dentry);
  19617. + AuDebugOn(a->src_bwh >= 0);
  19618. + a->src_wh_dentry
  19619. + = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
  19620. + err = PTR_ERR(a->src_wh_dentry);
  19621. + if (IS_ERR(a->src_wh_dentry))
  19622. + goto out_thargs;
  19623. + }
  19624. +
  19625. + /* lookup whiteout for dentry */
  19626. + if (au_ftest_ren(a->flags, WHDST)) {
  19627. + h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
  19628. + a->br);
  19629. + err = PTR_ERR(h_d);
  19630. + if (IS_ERR(h_d))
  19631. + goto out_whsrc;
  19632. + if (d_is_negative(h_d))
  19633. + dput(h_d);
  19634. + else
  19635. + a->dst_wh_dentry = h_d;
  19636. + }
  19637. +
  19638. + /* rename dentry to tmpwh */
  19639. + if (a->thargs) {
  19640. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  19641. + if (unlikely(err))
  19642. + goto out_whdst;
  19643. +
  19644. + d = a->dst_dentry;
  19645. + au_set_h_dptr(d, a->btgt, NULL);
  19646. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  19647. + if (unlikely(err))
  19648. + goto out_whtmp;
  19649. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  19650. + }
  19651. +
  19652. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
  19653. +
  19654. + /* rename by vfs_rename or cpup */
  19655. + d = a->dst_dentry;
  19656. + if (au_ftest_ren(a->flags, ISDIR)
  19657. + && (a->dst_wh_dentry
  19658. + || au_dbdiropq(d) == a->btgt
  19659. + /* hide the lower to keep xino */
  19660. + || a->btgt < au_dbbot(d)
  19661. + || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
  19662. + au_fset_ren(a->flags, DIROPQ);
  19663. + err = au_ren_or_cpup(a);
  19664. + if (unlikely(err))
  19665. + /* leave the copied-up one */
  19666. + goto out_whtmp;
  19667. +
  19668. + /* make dir opaque */
  19669. + if (au_ftest_ren(a->flags, DIROPQ)) {
  19670. + err = au_ren_diropq(a);
  19671. + if (unlikely(err))
  19672. + goto out_rename;
  19673. + }
  19674. +
  19675. + /* update target timestamps */
  19676. + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
  19677. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  19678. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  19679. + a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  19680. +
  19681. + /* remove whiteout for dentry */
  19682. + if (a->dst_wh_dentry) {
  19683. + a->h_path.dentry = a->dst_wh_dentry;
  19684. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  19685. + a->dst_dentry);
  19686. + if (unlikely(err))
  19687. + goto out_diropq;
  19688. + }
  19689. +
  19690. + /* remove whtmp */
  19691. + if (a->thargs)
  19692. + au_ren_del_whtmp(a); /* ignore this error */
  19693. +
  19694. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  19695. + err = 0;
  19696. + goto out_success;
  19697. +
  19698. +out_diropq:
  19699. + if (au_ftest_ren(a->flags, DIROPQ))
  19700. + au_ren_rev_diropq(err, a);
  19701. +out_rename:
  19702. + au_ren_rev_rename(err, a);
  19703. + dput(a->h_dst);
  19704. +out_whtmp:
  19705. + if (a->thargs)
  19706. + au_ren_rev_whtmp(err, a);
  19707. +out_whdst:
  19708. + dput(a->dst_wh_dentry);
  19709. + a->dst_wh_dentry = NULL;
  19710. +out_whsrc:
  19711. + if (a->src_wh_dentry)
  19712. + au_ren_rev_whsrc(err, a);
  19713. +out_success:
  19714. + dput(a->src_wh_dentry);
  19715. + dput(a->dst_wh_dentry);
  19716. +out_thargs:
  19717. + if (a->thargs) {
  19718. + dput(a->h_dst);
  19719. + au_whtmp_rmdir_free(a->thargs);
  19720. + a->thargs = NULL;
  19721. + }
  19722. +out:
  19723. + return err;
  19724. +}
  19725. +
  19726. +/* ---------------------------------------------------------------------- */
  19727. +
  19728. +/*
  19729. + * test if @dentry dir can be rename destination or not.
  19730. + * success means, it is a logically empty dir.
  19731. + */
  19732. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  19733. +{
  19734. + return au_test_empty(dentry, whlist);
  19735. +}
  19736. +
  19737. +/*
  19738. + * test if @dentry dir can be rename source or not.
  19739. + * if it can, return 0 and @children is filled.
  19740. + * success means,
  19741. + * - it is a logically empty dir.
  19742. + * - or, it exists on writable branch and has no children including whiteouts
  19743. + * on the lower branch.
  19744. + */
  19745. +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
  19746. +{
  19747. + int err;
  19748. + unsigned int rdhash;
  19749. + aufs_bindex_t btop;
  19750. +
  19751. + btop = au_dbtop(dentry);
  19752. + if (btop != btgt) {
  19753. + struct au_nhash whlist;
  19754. +
  19755. + SiMustAnyLock(dentry->d_sb);
  19756. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  19757. + if (!rdhash)
  19758. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  19759. + dentry));
  19760. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  19761. + if (unlikely(err))
  19762. + goto out;
  19763. + err = au_test_empty(dentry, &whlist);
  19764. + au_nhash_wh_free(&whlist);
  19765. + goto out;
  19766. + }
  19767. +
  19768. + if (btop == au_dbtaildir(dentry))
  19769. + return 0; /* success */
  19770. +
  19771. + err = au_test_empty_lower(dentry);
  19772. +
  19773. +out:
  19774. + if (err == -ENOTEMPTY) {
  19775. + AuWarn1("renaming dir who has child(ren) on multiple branches,"
  19776. + " is not supported\n");
  19777. + err = -EXDEV;
  19778. + }
  19779. + return err;
  19780. +}
  19781. +
  19782. +/* side effect: sets whlist and h_dentry */
  19783. +static int au_ren_may_dir(struct au_ren_args *a)
  19784. +{
  19785. + int err;
  19786. + unsigned int rdhash;
  19787. + struct dentry *d;
  19788. +
  19789. + d = a->dst_dentry;
  19790. + SiMustAnyLock(d->d_sb);
  19791. +
  19792. + err = 0;
  19793. + if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
  19794. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  19795. + if (!rdhash)
  19796. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  19797. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  19798. + if (unlikely(err))
  19799. + goto out;
  19800. +
  19801. + au_set_dbtop(d, a->dst_btop);
  19802. + err = may_rename_dstdir(d, &a->whlist);
  19803. + au_set_dbtop(d, a->btgt);
  19804. + }
  19805. + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
  19806. + if (unlikely(err))
  19807. + goto out;
  19808. +
  19809. + d = a->src_dentry;
  19810. + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
  19811. + if (au_ftest_ren(a->flags, ISDIR)) {
  19812. + err = may_rename_srcdir(d, a->btgt);
  19813. + if (unlikely(err)) {
  19814. + au_nhash_wh_free(&a->whlist);
  19815. + a->whlist.nh_num = 0;
  19816. + }
  19817. + }
  19818. +out:
  19819. + return err;
  19820. +}
  19821. +
  19822. +/* ---------------------------------------------------------------------- */
  19823. +
  19824. +/*
  19825. + * simple tests for rename.
  19826. + * following the checks in vfs, plus the parent-child relationship.
  19827. + */
  19828. +static int au_may_ren(struct au_ren_args *a)
  19829. +{
  19830. + int err, isdir;
  19831. + struct inode *h_inode;
  19832. +
  19833. + if (a->src_btop == a->btgt) {
  19834. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  19835. + au_ftest_ren(a->flags, ISDIR));
  19836. + if (unlikely(err))
  19837. + goto out;
  19838. + err = -EINVAL;
  19839. + if (unlikely(a->src_h_dentry == a->h_trap))
  19840. + goto out;
  19841. + }
  19842. +
  19843. + err = 0;
  19844. + if (a->dst_btop != a->btgt)
  19845. + goto out;
  19846. +
  19847. + err = -ENOTEMPTY;
  19848. + if (unlikely(a->dst_h_dentry == a->h_trap))
  19849. + goto out;
  19850. +
  19851. + err = -EIO;
  19852. + isdir = !!au_ftest_ren(a->flags, ISDIR);
  19853. + if (d_really_is_negative(a->dst_dentry)) {
  19854. + if (d_is_negative(a->dst_h_dentry))
  19855. + err = au_may_add(a->dst_dentry, a->btgt,
  19856. + a->dst_h_parent, isdir);
  19857. + } else {
  19858. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  19859. + goto out;
  19860. + h_inode = d_inode(a->dst_h_dentry);
  19861. + if (h_inode->i_nlink)
  19862. + err = au_may_del(a->dst_dentry, a->btgt,
  19863. + a->dst_h_parent, isdir);
  19864. + }
  19865. +
  19866. +out:
  19867. + if (unlikely(err == -ENOENT || err == -EEXIST))
  19868. + err = -EIO;
  19869. + AuTraceErr(err);
  19870. + return err;
  19871. +}
  19872. +
  19873. +/* ---------------------------------------------------------------------- */
  19874. +
  19875. +/*
  19876. + * locking order
  19877. + * (VFS)
  19878. + * - src_dir and dir by lock_rename()
  19879. + * - inode if exitsts
  19880. + * (aufs)
  19881. + * - lock all
  19882. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  19883. + * + si_read_lock
  19884. + * + di_write_lock2_child()
  19885. + * + di_write_lock_child()
  19886. + * + ii_write_lock_child()
  19887. + * + di_write_lock_child2()
  19888. + * + ii_write_lock_child2()
  19889. + * + src_parent and parent
  19890. + * + di_write_lock_parent()
  19891. + * + ii_write_lock_parent()
  19892. + * + di_write_lock_parent2()
  19893. + * + ii_write_lock_parent2()
  19894. + * + lower src_dir and dir by vfsub_lock_rename()
  19895. + * + verify the every relationships between child and parent. if any
  19896. + * of them failed, unlock all and return -EBUSY.
  19897. + */
  19898. +static void au_ren_unlock(struct au_ren_args *a)
  19899. +{
  19900. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  19901. + a->dst_h_parent, a->dst_hdir);
  19902. + if (au_ftest_ren(a->flags, MNT_WRITE))
  19903. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  19904. +}
  19905. +
  19906. +static int au_ren_lock(struct au_ren_args *a)
  19907. +{
  19908. + int err;
  19909. + unsigned int udba;
  19910. +
  19911. + err = 0;
  19912. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  19913. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  19914. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  19915. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  19916. +
  19917. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  19918. + if (unlikely(err))
  19919. + goto out;
  19920. + au_fset_ren(a->flags, MNT_WRITE);
  19921. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  19922. + a->dst_h_parent, a->dst_hdir);
  19923. + udba = au_opt_udba(a->src_dentry->d_sb);
  19924. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  19925. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  19926. + err = au_busy_or_stale();
  19927. + if (!err && au_dbtop(a->src_dentry) == a->btgt)
  19928. + err = au_h_verify(a->src_h_dentry, udba,
  19929. + d_inode(a->src_h_parent), a->src_h_parent,
  19930. + a->br);
  19931. + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
  19932. + err = au_h_verify(a->dst_h_dentry, udba,
  19933. + d_inode(a->dst_h_parent), a->dst_h_parent,
  19934. + a->br);
  19935. + if (!err)
  19936. + goto out; /* success */
  19937. +
  19938. + err = au_busy_or_stale();
  19939. + au_ren_unlock(a);
  19940. +
  19941. +out:
  19942. + return err;
  19943. +}
  19944. +
  19945. +/* ---------------------------------------------------------------------- */
  19946. +
  19947. +static void au_ren_refresh_dir(struct au_ren_args *a)
  19948. +{
  19949. + struct inode *dir;
  19950. +
  19951. + dir = a->dst_dir;
  19952. + dir->i_version++;
  19953. + if (au_ftest_ren(a->flags, ISDIR)) {
  19954. + /* is this updating defined in POSIX? */
  19955. + au_cpup_attr_timesizes(a->src_inode);
  19956. + au_cpup_attr_nlink(dir, /*force*/1);
  19957. + }
  19958. +
  19959. + au_dir_ts(dir, a->btgt);
  19960. +
  19961. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  19962. + return;
  19963. +
  19964. + dir = a->src_dir;
  19965. + dir->i_version++;
  19966. + if (au_ftest_ren(a->flags, ISDIR))
  19967. + au_cpup_attr_nlink(dir, /*force*/1);
  19968. + au_dir_ts(dir, a->btgt);
  19969. +}
  19970. +
  19971. +static void au_ren_refresh(struct au_ren_args *a)
  19972. +{
  19973. + aufs_bindex_t bbot, bindex;
  19974. + struct dentry *d, *h_d;
  19975. + struct inode *i, *h_i;
  19976. + struct super_block *sb;
  19977. +
  19978. + d = a->dst_dentry;
  19979. + d_drop(d);
  19980. + if (a->h_dst)
  19981. + /* already dget-ed by au_ren_or_cpup() */
  19982. + au_set_h_dptr(d, a->btgt, a->h_dst);
  19983. +
  19984. + i = a->dst_inode;
  19985. + if (i) {
  19986. + if (!au_ftest_ren(a->flags, ISDIR))
  19987. + vfsub_drop_nlink(i);
  19988. + else {
  19989. + vfsub_dead_dir(i);
  19990. + au_cpup_attr_timesizes(i);
  19991. + }
  19992. + au_update_dbrange(d, /*do_put_zero*/1);
  19993. + } else {
  19994. + bbot = a->btgt;
  19995. + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
  19996. + au_set_h_dptr(d, bindex, NULL);
  19997. + bbot = au_dbbot(d);
  19998. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
  19999. + au_set_h_dptr(d, bindex, NULL);
  20000. + au_update_dbrange(d, /*do_put_zero*/0);
  20001. + }
  20002. +
  20003. + d = a->src_dentry;
  20004. + au_set_dbwh(d, -1);
  20005. + bbot = au_dbbot(d);
  20006. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  20007. + h_d = au_h_dptr(d, bindex);
  20008. + if (h_d)
  20009. + au_set_h_dptr(d, bindex, NULL);
  20010. + }
  20011. + au_set_dbbot(d, a->btgt);
  20012. +
  20013. + sb = d->d_sb;
  20014. + i = a->src_inode;
  20015. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  20016. + return; /* success */
  20017. +
  20018. + bbot = au_ibbot(i);
  20019. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  20020. + h_i = au_h_iptr(i, bindex);
  20021. + if (h_i) {
  20022. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  20023. + /* ignore this error */
  20024. + au_set_h_iptr(i, bindex, NULL, 0);
  20025. + }
  20026. + }
  20027. + au_set_ibbot(i, a->btgt);
  20028. +}
  20029. +
  20030. +/* ---------------------------------------------------------------------- */
  20031. +
  20032. +/* mainly for link(2) and rename(2) */
  20033. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  20034. +{
  20035. + aufs_bindex_t bdiropq, bwh;
  20036. + struct dentry *parent;
  20037. + struct au_branch *br;
  20038. +
  20039. + parent = dentry->d_parent;
  20040. + IMustLock(d_inode(parent)); /* dir is locked */
  20041. +
  20042. + bdiropq = au_dbdiropq(parent);
  20043. + bwh = au_dbwh(dentry);
  20044. + br = au_sbr(dentry->d_sb, btgt);
  20045. + if (au_br_rdonly(br)
  20046. + || (0 <= bdiropq && bdiropq < btgt)
  20047. + || (0 <= bwh && bwh < btgt))
  20048. + btgt = -1;
  20049. +
  20050. + AuDbg("btgt %d\n", btgt);
  20051. + return btgt;
  20052. +}
  20053. +
  20054. +/* sets src_btop, dst_btop and btgt */
  20055. +static int au_ren_wbr(struct au_ren_args *a)
  20056. +{
  20057. + int err;
  20058. + struct au_wr_dir_args wr_dir_args = {
  20059. + /* .force_btgt = -1, */
  20060. + .flags = AuWrDir_ADD_ENTRY
  20061. + };
  20062. +
  20063. + a->src_btop = au_dbtop(a->src_dentry);
  20064. + a->dst_btop = au_dbtop(a->dst_dentry);
  20065. + if (au_ftest_ren(a->flags, ISDIR))
  20066. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  20067. + wr_dir_args.force_btgt = a->src_btop;
  20068. + if (a->dst_inode && a->dst_btop < a->src_btop)
  20069. + wr_dir_args.force_btgt = a->dst_btop;
  20070. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  20071. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  20072. + a->btgt = err;
  20073. +
  20074. + return err;
  20075. +}
  20076. +
  20077. +static void au_ren_dt(struct au_ren_args *a)
  20078. +{
  20079. + a->h_path.dentry = a->src_h_parent;
  20080. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  20081. + if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
  20082. + a->h_path.dentry = a->dst_h_parent;
  20083. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  20084. + }
  20085. +
  20086. + au_fclr_ren(a->flags, DT_DSTDIR);
  20087. + if (!au_ftest_ren(a->flags, ISDIR))
  20088. + return;
  20089. +
  20090. + a->h_path.dentry = a->src_h_dentry;
  20091. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  20092. + if (d_is_positive(a->dst_h_dentry)) {
  20093. + au_fset_ren(a->flags, DT_DSTDIR);
  20094. + a->h_path.dentry = a->dst_h_dentry;
  20095. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  20096. + }
  20097. +}
  20098. +
  20099. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  20100. +{
  20101. + struct dentry *h_d;
  20102. + struct inode *h_inode;
  20103. +
  20104. + au_dtime_revert(a->src_dt + AuPARENT);
  20105. + if (!au_ftest_ren(a->flags, ISSAMEDIR))
  20106. + au_dtime_revert(a->dst_dt + AuPARENT);
  20107. +
  20108. + if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
  20109. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  20110. + h_inode = d_inode(h_d);
  20111. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  20112. + au_dtime_revert(a->src_dt + AuCHILD);
  20113. + inode_unlock(h_inode);
  20114. +
  20115. + if (au_ftest_ren(a->flags, DT_DSTDIR)) {
  20116. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  20117. + h_inode = d_inode(h_d);
  20118. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  20119. + au_dtime_revert(a->dst_dt + AuCHILD);
  20120. + inode_unlock(h_inode);
  20121. + }
  20122. + }
  20123. +}
  20124. +
  20125. +/* ---------------------------------------------------------------------- */
  20126. +
  20127. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  20128. + struct inode *_dst_dir, struct dentry *_dst_dentry)
  20129. +{
  20130. + int err, flags;
  20131. + /* reduce stack space */
  20132. + struct au_ren_args *a;
  20133. +
  20134. + AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
  20135. + IMustLock(_src_dir);
  20136. + IMustLock(_dst_dir);
  20137. +
  20138. + err = -ENOMEM;
  20139. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  20140. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20141. + if (unlikely(!a))
  20142. + goto out;
  20143. +
  20144. + a->src_dir = _src_dir;
  20145. + a->src_dentry = _src_dentry;
  20146. + a->src_inode = NULL;
  20147. + if (d_really_is_positive(a->src_dentry))
  20148. + a->src_inode = d_inode(a->src_dentry);
  20149. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  20150. + a->dst_dir = _dst_dir;
  20151. + a->dst_dentry = _dst_dentry;
  20152. + a->dst_inode = NULL;
  20153. + if (d_really_is_positive(a->dst_dentry))
  20154. + a->dst_inode = d_inode(a->dst_dentry);
  20155. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  20156. + if (a->dst_inode) {
  20157. + IMustLock(a->dst_inode);
  20158. + au_igrab(a->dst_inode);
  20159. + }
  20160. +
  20161. + err = -ENOTDIR;
  20162. + flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  20163. + if (d_is_dir(a->src_dentry)) {
  20164. + au_fset_ren(a->flags, ISDIR);
  20165. + if (unlikely(d_really_is_positive(a->dst_dentry)
  20166. + && !d_is_dir(a->dst_dentry)))
  20167. + goto out_free;
  20168. + flags |= AuLock_DIRS;
  20169. + }
  20170. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, flags);
  20171. + if (unlikely(err))
  20172. + goto out_free;
  20173. +
  20174. + err = au_d_hashed_positive(a->src_dentry);
  20175. + if (unlikely(err))
  20176. + goto out_unlock;
  20177. + err = -ENOENT;
  20178. + if (a->dst_inode) {
  20179. + /*
  20180. + * If it is a dir, VFS unhash dst_dentry before this
  20181. + * function. It means we cannot rely upon d_unhashed().
  20182. + */
  20183. + if (unlikely(!a->dst_inode->i_nlink))
  20184. + goto out_unlock;
  20185. + if (!S_ISDIR(a->dst_inode->i_mode)) {
  20186. + err = au_d_hashed_positive(a->dst_dentry);
  20187. + if (unlikely(err))
  20188. + goto out_unlock;
  20189. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  20190. + goto out_unlock;
  20191. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  20192. + goto out_unlock;
  20193. +
  20194. + /*
  20195. + * is it possible?
  20196. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  20197. + * there may exist a problem somewhere else.
  20198. + */
  20199. + err = -EINVAL;
  20200. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  20201. + goto out_unlock;
  20202. +
  20203. + au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
  20204. + di_write_lock_parent(a->dst_parent);
  20205. +
  20206. + /* which branch we process */
  20207. + err = au_ren_wbr(a);
  20208. + if (unlikely(err < 0))
  20209. + goto out_parent;
  20210. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  20211. + a->h_path.mnt = au_br_mnt(a->br);
  20212. +
  20213. + /* are they available to be renamed */
  20214. + err = au_ren_may_dir(a);
  20215. + if (unlikely(err))
  20216. + goto out_children;
  20217. +
  20218. + /* prepare the writable parent dir on the same branch */
  20219. + if (a->dst_btop == a->btgt) {
  20220. + au_fset_ren(a->flags, WHDST);
  20221. + } else {
  20222. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  20223. + if (unlikely(err))
  20224. + goto out_children;
  20225. + }
  20226. +
  20227. + if (a->src_dir != a->dst_dir) {
  20228. + /*
  20229. + * this temporary unlock is safe,
  20230. + * because both dir->i_mutex are locked.
  20231. + */
  20232. + di_write_unlock(a->dst_parent);
  20233. + di_write_lock_parent(a->src_parent);
  20234. + err = au_wr_dir_need_wh(a->src_dentry,
  20235. + au_ftest_ren(a->flags, ISDIR),
  20236. + &a->btgt);
  20237. + di_write_unlock(a->src_parent);
  20238. + di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
  20239. + au_fclr_ren(a->flags, ISSAMEDIR);
  20240. + } else
  20241. + err = au_wr_dir_need_wh(a->src_dentry,
  20242. + au_ftest_ren(a->flags, ISDIR),
  20243. + &a->btgt);
  20244. + if (unlikely(err < 0))
  20245. + goto out_children;
  20246. + if (err)
  20247. + au_fset_ren(a->flags, WHSRC);
  20248. +
  20249. + /* cpup src */
  20250. + if (a->src_btop != a->btgt) {
  20251. + struct au_pin pin;
  20252. +
  20253. + err = au_pin(&pin, a->src_dentry, a->btgt,
  20254. + au_opt_udba(a->src_dentry->d_sb),
  20255. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20256. + if (!err) {
  20257. + struct au_cp_generic cpg = {
  20258. + .dentry = a->src_dentry,
  20259. + .bdst = a->btgt,
  20260. + .bsrc = a->src_btop,
  20261. + .len = -1,
  20262. + .pin = &pin,
  20263. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  20264. + };
  20265. + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
  20266. + err = au_sio_cpup_simple(&cpg);
  20267. + au_unpin(&pin);
  20268. + }
  20269. + if (unlikely(err))
  20270. + goto out_children;
  20271. + a->src_btop = a->btgt;
  20272. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  20273. + au_fset_ren(a->flags, WHSRC);
  20274. + }
  20275. +
  20276. + /* lock them all */
  20277. + err = au_ren_lock(a);
  20278. + if (unlikely(err))
  20279. + /* leave the copied-up one */
  20280. + goto out_children;
  20281. +
  20282. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  20283. + err = au_may_ren(a);
  20284. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  20285. + err = -ENAMETOOLONG;
  20286. + if (unlikely(err))
  20287. + goto out_hdir;
  20288. +
  20289. + /* store timestamps to be revertible */
  20290. + au_ren_dt(a);
  20291. +
  20292. + /* here we go */
  20293. + err = do_rename(a);
  20294. + if (unlikely(err))
  20295. + goto out_dt;
  20296. +
  20297. + /* update dir attributes */
  20298. + au_ren_refresh_dir(a);
  20299. +
  20300. + /* dput/iput all lower dentries */
  20301. + au_ren_refresh(a);
  20302. +
  20303. + goto out_hdir; /* success */
  20304. +
  20305. +out_dt:
  20306. + au_ren_rev_dt(err, a);
  20307. +out_hdir:
  20308. + au_ren_unlock(a);
  20309. +out_children:
  20310. + au_nhash_wh_free(&a->whlist);
  20311. + if (err && a->dst_inode && a->dst_btop != a->btgt) {
  20312. + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
  20313. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  20314. + au_set_dbtop(a->dst_dentry, a->dst_btop);
  20315. + }
  20316. +out_parent:
  20317. + if (!err)
  20318. + d_move(a->src_dentry, a->dst_dentry);
  20319. + else {
  20320. + au_update_dbtop(a->dst_dentry);
  20321. + if (!a->dst_inode)
  20322. + d_drop(a->dst_dentry);
  20323. + }
  20324. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  20325. + di_write_unlock(a->dst_parent);
  20326. + else
  20327. + di_write_unlock2(a->src_parent, a->dst_parent);
  20328. +out_unlock:
  20329. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  20330. +out_free:
  20331. + iput(a->dst_inode);
  20332. + if (a->thargs)
  20333. + au_whtmp_rmdir_free(a->thargs);
  20334. + au_delayed_kfree(a);
  20335. +out:
  20336. + AuTraceErr(err);
  20337. + return err;
  20338. +}
  20339. diff -Naur null/fs/aufs/Kconfig linux-4.7/fs/aufs/Kconfig
  20340. --- /dev/null
  20341. +++ linux-4.7/fs/aufs/Kconfig 2016-08-08 12:57:44.597384786 -0400
  20342. @@ -0,0 +1,185 @@
  20343. +config AUFS_FS
  20344. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  20345. + help
  20346. + Aufs is a stackable unification filesystem such as Unionfs,
  20347. + which unifies several directories and provides a merged single
  20348. + directory.
  20349. + In the early days, aufs was entirely re-designed and
  20350. + re-implemented Unionfs Version 1.x series. Introducing many
  20351. + original ideas, approaches and improvements, it becomes totally
  20352. + different from Unionfs while keeping the basic features.
  20353. +
  20354. +if AUFS_FS
  20355. +choice
  20356. + prompt "Maximum number of branches"
  20357. + default AUFS_BRANCH_MAX_127
  20358. + help
  20359. + Specifies the maximum number of branches (or member directories)
  20360. + in a single aufs. The larger value consumes more system
  20361. + resources and has a minor impact to performance.
  20362. +config AUFS_BRANCH_MAX_127
  20363. + bool "127"
  20364. + help
  20365. + Specifies the maximum number of branches (or member directories)
  20366. + in a single aufs. The larger value consumes more system
  20367. + resources and has a minor impact to performance.
  20368. +config AUFS_BRANCH_MAX_511
  20369. + bool "511"
  20370. + help
  20371. + Specifies the maximum number of branches (or member directories)
  20372. + in a single aufs. The larger value consumes more system
  20373. + resources and has a minor impact to performance.
  20374. +config AUFS_BRANCH_MAX_1023
  20375. + bool "1023"
  20376. + help
  20377. + Specifies the maximum number of branches (or member directories)
  20378. + in a single aufs. The larger value consumes more system
  20379. + resources and has a minor impact to performance.
  20380. +config AUFS_BRANCH_MAX_32767
  20381. + bool "32767"
  20382. + help
  20383. + Specifies the maximum number of branches (or member directories)
  20384. + in a single aufs. The larger value consumes more system
  20385. + resources and has a minor impact to performance.
  20386. +endchoice
  20387. +
  20388. +config AUFS_SBILIST
  20389. + bool
  20390. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  20391. + default y
  20392. + help
  20393. + Automatic configuration for internal use.
  20394. + When aufs supports Magic SysRq or /proc, enabled automatically.
  20395. +
  20396. +config AUFS_HNOTIFY
  20397. + bool "Detect direct branch access (bypassing aufs)"
  20398. + help
  20399. + If you want to modify files on branches directly, eg. bypassing aufs,
  20400. + and want aufs to detect the changes of them fully, then enable this
  20401. + option and use 'udba=notify' mount option.
  20402. + Currently there is only one available configuration, "fsnotify".
  20403. + It will have a negative impact to the performance.
  20404. + See detail in aufs.5.
  20405. +
  20406. +choice
  20407. + prompt "method" if AUFS_HNOTIFY
  20408. + default AUFS_HFSNOTIFY
  20409. +config AUFS_HFSNOTIFY
  20410. + bool "fsnotify"
  20411. + select FSNOTIFY
  20412. +endchoice
  20413. +
  20414. +config AUFS_EXPORT
  20415. + bool "NFS-exportable aufs"
  20416. + depends on EXPORTFS
  20417. + help
  20418. + If you want to export your mounted aufs via NFS, then enable this
  20419. + option. There are several requirements for this configuration.
  20420. + See detail in aufs.5.
  20421. +
  20422. +config AUFS_INO_T_64
  20423. + bool
  20424. + depends on AUFS_EXPORT
  20425. + depends on 64BIT && !(ALPHA || S390)
  20426. + default y
  20427. + help
  20428. + Automatic configuration for internal use.
  20429. + /* typedef unsigned long/int __kernel_ino_t */
  20430. + /* alpha and s390x are int */
  20431. +
  20432. +config AUFS_XATTR
  20433. + bool "support for XATTR/EA (including Security Labels)"
  20434. + help
  20435. + If your branch fs supports XATTR/EA and you want to make them
  20436. + available in aufs too, then enable this opsion and specify the
  20437. + branch attributes for EA.
  20438. + See detail in aufs.5.
  20439. +
  20440. +config AUFS_FHSM
  20441. + bool "File-based Hierarchical Storage Management"
  20442. + help
  20443. + Hierarchical Storage Management (or HSM) is a well-known feature
  20444. + in the storage world. Aufs provides this feature as file-based.
  20445. + with multiple branches.
  20446. + These multiple branches are prioritized, ie. the topmost one
  20447. + should be the fastest drive and be used heavily.
  20448. +
  20449. +config AUFS_RDU
  20450. + bool "Readdir in userspace"
  20451. + help
  20452. + Aufs has two methods to provide a merged view for a directory,
  20453. + by a user-space library and by kernel-space natively. The latter
  20454. + is always enabled but sometimes large and slow.
  20455. + If you enable this option, install the library in aufs2-util
  20456. + package, and set some environment variables for your readdir(3),
  20457. + then the work will be handled in user-space which generally
  20458. + shows better performance in most cases.
  20459. + See detail in aufs.5.
  20460. +
  20461. +config AUFS_SHWH
  20462. + bool "Show whiteouts"
  20463. + help
  20464. + If you want to make the whiteouts in aufs visible, then enable
  20465. + this option and specify 'shwh' mount option. Although it may
  20466. + sounds like philosophy or something, but in technically it
  20467. + simply shows the name of whiteout with keeping its behaviour.
  20468. +
  20469. +config AUFS_BR_RAMFS
  20470. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  20471. + help
  20472. + If you want to use ramfs as an aufs branch fs, then enable this
  20473. + option. Generally tmpfs is recommended.
  20474. + Aufs prohibited them to be a branch fs by default, because
  20475. + initramfs becomes unusable after switch_root or something
  20476. + generally. If you sets initramfs as an aufs branch and boot your
  20477. + system by switch_root, you will meet a problem easily since the
  20478. + files in initramfs may be inaccessible.
  20479. + Unless you are going to use ramfs as an aufs branch fs without
  20480. + switch_root or something, leave it N.
  20481. +
  20482. +config AUFS_BR_FUSE
  20483. + bool "Fuse fs as an aufs branch"
  20484. + depends on FUSE_FS
  20485. + select AUFS_POLL
  20486. + help
  20487. + If you want to use fuse-based userspace filesystem as an aufs
  20488. + branch fs, then enable this option.
  20489. + It implements the internal poll(2) operation which is
  20490. + implemented by fuse only (curretnly).
  20491. +
  20492. +config AUFS_POLL
  20493. + bool
  20494. + help
  20495. + Automatic configuration for internal use.
  20496. +
  20497. +config AUFS_BR_HFSPLUS
  20498. + bool "Hfsplus as an aufs branch"
  20499. + depends on HFSPLUS_FS
  20500. + default y
  20501. + help
  20502. + If you want to use hfsplus fs as an aufs branch fs, then enable
  20503. + this option. This option introduces a small overhead at
  20504. + copying-up a file on hfsplus.
  20505. +
  20506. +config AUFS_BDEV_LOOP
  20507. + bool
  20508. + depends on BLK_DEV_LOOP
  20509. + default y
  20510. + help
  20511. + Automatic configuration for internal use.
  20512. + Convert =[ym] into =y.
  20513. +
  20514. +config AUFS_DEBUG
  20515. + bool "Debug aufs"
  20516. + help
  20517. + Enable this to compile aufs internal debug code.
  20518. + It will have a negative impact to the performance.
  20519. +
  20520. +config AUFS_MAGIC_SYSRQ
  20521. + bool
  20522. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  20523. + default y
  20524. + help
  20525. + Automatic configuration for internal use.
  20526. + When aufs supports Magic SysRq, enabled automatically.
  20527. +endif
  20528. diff -Naur null/fs/aufs/loop.c linux-4.7/fs/aufs/loop.c
  20529. --- /dev/null
  20530. +++ linux-4.7/fs/aufs/loop.c 2016-08-08 12:57:44.604384630 -0400
  20531. @@ -0,0 +1,146 @@
  20532. +/*
  20533. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  20534. + *
  20535. + * This program, aufs is free software; you can redistribute it and/or modify
  20536. + * it under the terms of the GNU General Public License as published by
  20537. + * the Free Software Foundation; either version 2 of the License, or
  20538. + * (at your option) any later version.
  20539. + *
  20540. + * This program is distributed in the hope that it will be useful,
  20541. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20542. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20543. + * GNU General Public License for more details.
  20544. + *
  20545. + * You should have received a copy of the GNU General Public License
  20546. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20547. + */
  20548. +
  20549. +/*
  20550. + * support for loopback block device as a branch
  20551. + */
  20552. +
  20553. +#include "aufs.h"
  20554. +
  20555. +/* added into drivers/block/loop.c */
  20556. +static struct file *(*backing_file_func)(struct super_block *sb);
  20557. +
  20558. +/*
  20559. + * test if two lower dentries have overlapping branches.
  20560. + */
  20561. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  20562. +{
  20563. + struct super_block *h_sb;
  20564. + struct file *backing_file;
  20565. +
  20566. + if (unlikely(!backing_file_func)) {
  20567. + /* don't load "loop" module here */
  20568. + backing_file_func = symbol_get(loop_backing_file);
  20569. + if (unlikely(!backing_file_func))
  20570. + /* "loop" module is not loaded */
  20571. + return 0;
  20572. + }
  20573. +
  20574. + h_sb = h_adding->d_sb;
  20575. + backing_file = backing_file_func(h_sb);
  20576. + if (!backing_file)
  20577. + return 0;
  20578. +
  20579. + h_adding = backing_file->f_path.dentry;
  20580. + /*
  20581. + * h_adding can be local NFS.
  20582. + * in this case aufs cannot detect the loop.
  20583. + */
  20584. + if (unlikely(h_adding->d_sb == sb))
  20585. + return 1;
  20586. + return !!au_test_subdir(h_adding, sb->s_root);
  20587. +}
  20588. +
  20589. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  20590. +int au_test_loopback_kthread(void)
  20591. +{
  20592. + int ret;
  20593. + struct task_struct *tsk = current;
  20594. + char c, comm[sizeof(tsk->comm)];
  20595. +
  20596. + ret = 0;
  20597. + if (tsk->flags & PF_KTHREAD) {
  20598. + get_task_comm(comm, tsk);
  20599. + c = comm[4];
  20600. + ret = ('0' <= c && c <= '9'
  20601. + && !strncmp(comm, "loop", 4));
  20602. + }
  20603. +
  20604. + return ret;
  20605. +}
  20606. +
  20607. +/* ---------------------------------------------------------------------- */
  20608. +
  20609. +#define au_warn_loopback_step 16
  20610. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  20611. +static unsigned long *au_warn_loopback_array;
  20612. +
  20613. +void au_warn_loopback(struct super_block *h_sb)
  20614. +{
  20615. + int i, new_nelem;
  20616. + unsigned long *a, magic;
  20617. + static DEFINE_SPINLOCK(spin);
  20618. +
  20619. + magic = h_sb->s_magic;
  20620. + spin_lock(&spin);
  20621. + a = au_warn_loopback_array;
  20622. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  20623. + if (a[i] == magic) {
  20624. + spin_unlock(&spin);
  20625. + return;
  20626. + }
  20627. +
  20628. + /* h_sb is new to us, print it */
  20629. + if (i < au_warn_loopback_nelem) {
  20630. + a[i] = magic;
  20631. + goto pr;
  20632. + }
  20633. +
  20634. + /* expand the array */
  20635. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  20636. + a = au_kzrealloc(au_warn_loopback_array,
  20637. + au_warn_loopback_nelem * sizeof(unsigned long),
  20638. + new_nelem * sizeof(unsigned long), GFP_ATOMIC);
  20639. + if (a) {
  20640. + au_warn_loopback_nelem = new_nelem;
  20641. + au_warn_loopback_array = a;
  20642. + a[i] = magic;
  20643. + goto pr;
  20644. + }
  20645. +
  20646. + spin_unlock(&spin);
  20647. + AuWarn1("realloc failed, ignored\n");
  20648. + return;
  20649. +
  20650. +pr:
  20651. + spin_unlock(&spin);
  20652. + pr_warn("you may want to try another patch for loopback file "
  20653. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  20654. +}
  20655. +
  20656. +int au_loopback_init(void)
  20657. +{
  20658. + int err;
  20659. + struct super_block *sb __maybe_unused;
  20660. +
  20661. + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
  20662. +
  20663. + err = 0;
  20664. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  20665. + sizeof(unsigned long), GFP_NOFS);
  20666. + if (unlikely(!au_warn_loopback_array))
  20667. + err = -ENOMEM;
  20668. +
  20669. + return err;
  20670. +}
  20671. +
  20672. +void au_loopback_fin(void)
  20673. +{
  20674. + if (backing_file_func)
  20675. + symbol_put(loop_backing_file);
  20676. + au_delayed_kfree(au_warn_loopback_array);
  20677. +}
  20678. diff -Naur null/fs/aufs/loop.h linux-4.7/fs/aufs/loop.h
  20679. --- /dev/null
  20680. +++ linux-4.7/fs/aufs/loop.h 2016-08-08 12:57:44.604384630 -0400
  20681. @@ -0,0 +1,52 @@
  20682. +/*
  20683. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  20684. + *
  20685. + * This program, aufs is free software; you can redistribute it and/or modify
  20686. + * it under the terms of the GNU General Public License as published by
  20687. + * the Free Software Foundation; either version 2 of the License, or
  20688. + * (at your option) any later version.
  20689. + *
  20690. + * This program is distributed in the hope that it will be useful,
  20691. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20692. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20693. + * GNU General Public License for more details.
  20694. + *
  20695. + * You should have received a copy of the GNU General Public License
  20696. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20697. + */
  20698. +
  20699. +/*
  20700. + * support for loopback mount as a branch
  20701. + */
  20702. +
  20703. +#ifndef __AUFS_LOOP_H__
  20704. +#define __AUFS_LOOP_H__
  20705. +
  20706. +#ifdef __KERNEL__
  20707. +
  20708. +struct dentry;
  20709. +struct super_block;
  20710. +
  20711. +#ifdef CONFIG_AUFS_BDEV_LOOP
  20712. +/* drivers/block/loop.c */
  20713. +struct file *loop_backing_file(struct super_block *sb);
  20714. +
  20715. +/* loop.c */
  20716. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  20717. +int au_test_loopback_kthread(void);
  20718. +void au_warn_loopback(struct super_block *h_sb);
  20719. +
  20720. +int au_loopback_init(void);
  20721. +void au_loopback_fin(void);
  20722. +#else
  20723. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  20724. + struct dentry *h_adding)
  20725. +AuStubInt0(au_test_loopback_kthread, void)
  20726. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  20727. +
  20728. +AuStubInt0(au_loopback_init, void)
  20729. +AuStubVoid(au_loopback_fin, void)
  20730. +#endif /* BLK_DEV_LOOP */
  20731. +
  20732. +#endif /* __KERNEL__ */
  20733. +#endif /* __AUFS_LOOP_H__ */
  20734. diff -Naur null/fs/aufs/magic.mk linux-4.7/fs/aufs/magic.mk
  20735. --- /dev/null
  20736. +++ linux-4.7/fs/aufs/magic.mk 2016-08-08 12:57:44.605384608 -0400
  20737. @@ -0,0 +1,30 @@
  20738. +
  20739. +# defined in ${srctree}/fs/fuse/inode.c
  20740. +# tristate
  20741. +ifdef CONFIG_FUSE_FS
  20742. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  20743. +endif
  20744. +
  20745. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  20746. +# tristate
  20747. +ifdef CONFIG_XFS_FS
  20748. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  20749. +endif
  20750. +
  20751. +# defined in ${srctree}/fs/configfs/mount.c
  20752. +# tristate
  20753. +ifdef CONFIG_CONFIGFS_FS
  20754. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  20755. +endif
  20756. +
  20757. +# defined in ${srctree}/fs/ubifs/ubifs.h
  20758. +# tristate
  20759. +ifdef CONFIG_UBIFS_FS
  20760. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  20761. +endif
  20762. +
  20763. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  20764. +# tristate
  20765. +ifdef CONFIG_HFSPLUS_FS
  20766. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  20767. +endif
  20768. diff -Naur null/fs/aufs/Makefile linux-4.7/fs/aufs/Makefile
  20769. --- /dev/null
  20770. +++ linux-4.7/fs/aufs/Makefile 2016-08-08 12:57:44.597384786 -0400
  20771. @@ -0,0 +1,44 @@
  20772. +
  20773. +include ${src}/magic.mk
  20774. +ifeq (${CONFIG_AUFS_FS},m)
  20775. +include ${src}/conf.mk
  20776. +endif
  20777. +-include ${src}/priv_def.mk
  20778. +
  20779. +# cf. include/linux/kernel.h
  20780. +# enable pr_debug
  20781. +ccflags-y += -DDEBUG
  20782. +# sparse requires the full pathname
  20783. +ifdef M
  20784. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  20785. +else
  20786. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  20787. +endif
  20788. +
  20789. +obj-$(CONFIG_AUFS_FS) += aufs.o
  20790. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  20791. + wkq.o vfsub.o dcsub.o \
  20792. + cpup.o whout.o wbr_policy.o \
  20793. + dinfo.o dentry.o \
  20794. + dynop.o \
  20795. + finfo.o file.o f_op.o \
  20796. + dir.o vdir.o \
  20797. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  20798. + mvdown.o ioctl.o
  20799. +
  20800. +# all are boolean
  20801. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  20802. +aufs-$(CONFIG_SYSFS) += sysfs.o
  20803. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  20804. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  20805. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  20806. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  20807. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  20808. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  20809. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  20810. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  20811. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  20812. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  20813. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  20814. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  20815. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  20816. diff -Naur null/fs/aufs/module.c linux-4.7/fs/aufs/module.c
  20817. --- /dev/null
  20818. +++ linux-4.7/fs/aufs/module.c 2016-08-08 12:57:44.605384608 -0400
  20819. @@ -0,0 +1,289 @@
  20820. +/*
  20821. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  20822. + *
  20823. + * This program, aufs is free software; you can redistribute it and/or modify
  20824. + * it under the terms of the GNU General Public License as published by
  20825. + * the Free Software Foundation; either version 2 of the License, or
  20826. + * (at your option) any later version.
  20827. + *
  20828. + * This program is distributed in the hope that it will be useful,
  20829. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20830. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20831. + * GNU General Public License for more details.
  20832. + *
  20833. + * You should have received a copy of the GNU General Public License
  20834. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20835. + */
  20836. +
  20837. +/*
  20838. + * module global variables and operations
  20839. + */
  20840. +
  20841. +#include <linux/module.h>
  20842. +#include <linux/seq_file.h>
  20843. +#include "aufs.h"
  20844. +
  20845. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
  20846. +{
  20847. + if (new_sz <= nused)
  20848. + return p;
  20849. +
  20850. + p = krealloc(p, new_sz, gfp);
  20851. + if (p)
  20852. + memset(p + nused, 0, new_sz - nused);
  20853. + return p;
  20854. +}
  20855. +
  20856. +/* ---------------------------------------------------------------------- */
  20857. +/*
  20858. + * aufs caches
  20859. + */
  20860. +
  20861. +struct au_dfree au_dfree;
  20862. +
  20863. +/* delayed free */
  20864. +static void au_do_dfree(struct work_struct *work __maybe_unused)
  20865. +{
  20866. + struct llist_head *head;
  20867. + struct llist_node *node, *next;
  20868. +
  20869. +#define AU_CACHE_DFREE_DO_BODY(name, idx, lnode) do { \
  20870. + head = &au_dfree.cache[AuCache_##idx].llist; \
  20871. + node = llist_del_all(head); \
  20872. + for (; node; node = next) { \
  20873. + struct au_##name *p = \
  20874. + p = llist_entry(node, struct au_##name, \
  20875. + lnode); \
  20876. + next = llist_next(node); \
  20877. + au_cache_free_##name(p); \
  20878. + } \
  20879. + } while (0)
  20880. +
  20881. + AU_CACHE_DFREE_DO_BODY(dinfo, DINFO, di_lnode);
  20882. + AU_CACHE_DFREE_DO_BODY(icntnr, ICNTNR, lnode);
  20883. + AU_CACHE_DFREE_DO_BODY(finfo, FINFO, fi_lnode);
  20884. + AU_CACHE_DFREE_DO_BODY(vdir, VDIR, vd_lnode);
  20885. + AU_CACHE_DFREE_DO_BODY(vdir_dehstr, DEHSTR, lnode);
  20886. +#ifdef CONFIG_AUFS_HNOTIFY
  20887. + AU_CACHE_DFREE_DO_BODY(hnotify, HNOTIFY, hn_lnode);
  20888. +#endif
  20889. +
  20890. +#define AU_DFREE_DO_BODY(llist, func) do { \
  20891. + node = llist_del_all(llist); \
  20892. + for (; node; node = next) { \
  20893. + next = llist_next(node); \
  20894. + func(node); \
  20895. + } \
  20896. + } while (0)
  20897. +
  20898. + AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_KFREE, kfree);
  20899. + AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_FREE_PAGE, au_free_page);
  20900. +
  20901. +#undef AU_CACHE_DFREE_DO_BODY
  20902. +#undef AU_DFREE_DO_BODY
  20903. +}
  20904. +
  20905. +AU_CACHE_DFREE_FUNC(dinfo, DINFO, di_lnode);
  20906. +AU_CACHE_DFREE_FUNC(icntnr, ICNTNR, lnode);
  20907. +AU_CACHE_DFREE_FUNC(finfo, FINFO, fi_lnode);
  20908. +AU_CACHE_DFREE_FUNC(vdir, VDIR, vd_lnode);
  20909. +AU_CACHE_DFREE_FUNC(vdir_dehstr, DEHSTR, lnode);
  20910. +
  20911. +static void au_cache_fin(void)
  20912. +{
  20913. + int i;
  20914. + struct au_cache *cp;
  20915. +
  20916. + /*
  20917. + * Make sure all delayed rcu free inodes are flushed before we
  20918. + * destroy cache.
  20919. + */
  20920. + rcu_barrier();
  20921. +
  20922. + /* excluding AuCache_HNOTIFY */
  20923. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  20924. + flush_delayed_work(&au_dfree.dwork);
  20925. + for (i = 0; i < AuCache_HNOTIFY; i++) {
  20926. + cp = au_dfree.cache + i;
  20927. + AuDebugOn(!llist_empty(&cp->llist));
  20928. + kmem_cache_destroy(cp->cache);
  20929. + cp->cache = NULL;
  20930. + }
  20931. +}
  20932. +
  20933. +static int __init au_cache_init(void)
  20934. +{
  20935. + struct au_cache *cp;
  20936. +
  20937. + cp = au_dfree.cache;
  20938. + cp[AuCache_DINFO].cache = AuCacheCtor(au_dinfo, au_di_init_once);
  20939. + if (cp[AuCache_DINFO].cache)
  20940. + /* SLAB_DESTROY_BY_RCU */
  20941. + cp[AuCache_ICNTNR].cache = AuCacheCtor(au_icntnr,
  20942. + au_icntnr_init_once);
  20943. + if (cp[AuCache_ICNTNR].cache)
  20944. + cp[AuCache_FINFO].cache = AuCacheCtor(au_finfo,
  20945. + au_fi_init_once);
  20946. + if (cp[AuCache_FINFO].cache)
  20947. + cp[AuCache_VDIR].cache = AuCache(au_vdir);
  20948. + if (cp[AuCache_VDIR].cache)
  20949. + cp[AuCache_DEHSTR].cache = AuCache(au_vdir_dehstr);
  20950. + if (cp[AuCache_DEHSTR].cache)
  20951. + return 0;
  20952. +
  20953. + au_cache_fin();
  20954. + return -ENOMEM;
  20955. +}
  20956. +
  20957. +/* ---------------------------------------------------------------------- */
  20958. +
  20959. +int au_dir_roflags;
  20960. +
  20961. +#ifdef CONFIG_AUFS_SBILIST
  20962. +/*
  20963. + * iterate_supers_type() doesn't protect us from
  20964. + * remounting (branch management)
  20965. + */
  20966. +struct au_sphlhead au_sbilist;
  20967. +#endif
  20968. +
  20969. +/*
  20970. + * functions for module interface.
  20971. + */
  20972. +MODULE_LICENSE("GPL");
  20973. +/* MODULE_LICENSE("GPL v2"); */
  20974. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  20975. +MODULE_DESCRIPTION(AUFS_NAME
  20976. + " -- Advanced multi layered unification filesystem");
  20977. +MODULE_VERSION(AUFS_VERSION);
  20978. +MODULE_ALIAS_FS(AUFS_NAME);
  20979. +
  20980. +/* this module parameter has no meaning when SYSFS is disabled */
  20981. +int sysaufs_brs = 1;
  20982. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  20983. +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
  20984. +
  20985. +/* this module parameter has no meaning when USER_NS is disabled */
  20986. +bool au_userns;
  20987. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  20988. +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
  20989. +
  20990. +/* ---------------------------------------------------------------------- */
  20991. +
  20992. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  20993. +
  20994. +int au_seq_path(struct seq_file *seq, struct path *path)
  20995. +{
  20996. + int err;
  20997. +
  20998. + err = seq_path(seq, path, au_esc_chars);
  20999. + if (err > 0)
  21000. + err = 0;
  21001. + else if (err < 0)
  21002. + err = -ENOMEM;
  21003. +
  21004. + return err;
  21005. +}
  21006. +
  21007. +/* ---------------------------------------------------------------------- */
  21008. +
  21009. +static int __init aufs_init(void)
  21010. +{
  21011. + int err, i;
  21012. + char *p;
  21013. + struct au_cache *cp;
  21014. +
  21015. + p = au_esc_chars;
  21016. + for (i = 1; i <= ' '; i++)
  21017. + *p++ = i;
  21018. + *p++ = '\\';
  21019. + *p++ = '\x7f';
  21020. + *p = 0;
  21021. +
  21022. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  21023. +
  21024. + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
  21025. + for (i = 0; i < AuIop_Last; i++)
  21026. + aufs_iop_nogetattr[i].getattr = NULL;
  21027. +
  21028. + /* First, initialize au_dfree */
  21029. + for (i = 0; i < AuCache_Last; i++) { /* including hnotify */
  21030. + cp = au_dfree.cache + i;
  21031. + cp->cache = NULL;
  21032. + init_llist_head(&cp->llist);
  21033. + }
  21034. + for (i = 0; i < AU_DFREE_Last; i++)
  21035. + init_llist_head(au_dfree.llist + i);
  21036. + INIT_DELAYED_WORK(&au_dfree.dwork, au_do_dfree);
  21037. +
  21038. + au_sbilist_init();
  21039. + sysaufs_brs_init();
  21040. + au_debug_init();
  21041. + au_dy_init();
  21042. + err = sysaufs_init();
  21043. + if (unlikely(err))
  21044. + goto out;
  21045. + err = au_procfs_init();
  21046. + if (unlikely(err))
  21047. + goto out_sysaufs;
  21048. + err = au_wkq_init();
  21049. + if (unlikely(err))
  21050. + goto out_procfs;
  21051. + err = au_loopback_init();
  21052. + if (unlikely(err))
  21053. + goto out_wkq;
  21054. + err = au_hnotify_init();
  21055. + if (unlikely(err))
  21056. + goto out_loopback;
  21057. + err = au_sysrq_init();
  21058. + if (unlikely(err))
  21059. + goto out_hin;
  21060. + err = au_cache_init();
  21061. + if (unlikely(err))
  21062. + goto out_sysrq;
  21063. +
  21064. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  21065. + err = register_filesystem(&aufs_fs_type);
  21066. + if (unlikely(err))
  21067. + goto out_cache;
  21068. +
  21069. + /* since we define pr_fmt, call printk directly */
  21070. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  21071. + goto out; /* success */
  21072. +
  21073. +out_cache:
  21074. + au_cache_fin();
  21075. +out_sysrq:
  21076. + au_sysrq_fin();
  21077. +out_hin:
  21078. + au_hnotify_fin();
  21079. +out_loopback:
  21080. + au_loopback_fin();
  21081. +out_wkq:
  21082. + au_wkq_fin();
  21083. +out_procfs:
  21084. + au_procfs_fin();
  21085. +out_sysaufs:
  21086. + sysaufs_fin();
  21087. + au_dy_fin();
  21088. + flush_delayed_work(&au_dfree.dwork);
  21089. +out:
  21090. + return err;
  21091. +}
  21092. +
  21093. +static void __exit aufs_exit(void)
  21094. +{
  21095. + unregister_filesystem(&aufs_fs_type);
  21096. + au_cache_fin();
  21097. + au_sysrq_fin();
  21098. + au_hnotify_fin();
  21099. + au_loopback_fin();
  21100. + au_wkq_fin();
  21101. + au_procfs_fin();
  21102. + sysaufs_fin();
  21103. + au_dy_fin();
  21104. + flush_delayed_work(&au_dfree.dwork);
  21105. +}
  21106. +
  21107. +module_init(aufs_init);
  21108. +module_exit(aufs_exit);
  21109. diff -Naur null/fs/aufs/module.h linux-4.7/fs/aufs/module.h
  21110. --- /dev/null
  21111. +++ linux-4.7/fs/aufs/module.h 2016-08-08 12:57:44.605384608 -0400
  21112. @@ -0,0 +1,144 @@
  21113. +/*
  21114. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  21115. + *
  21116. + * This program, aufs is free software; you can redistribute it and/or modify
  21117. + * it under the terms of the GNU General Public License as published by
  21118. + * the Free Software Foundation; either version 2 of the License, or
  21119. + * (at your option) any later version.
  21120. + *
  21121. + * This program is distributed in the hope that it will be useful,
  21122. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21123. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21124. + * GNU General Public License for more details.
  21125. + *
  21126. + * You should have received a copy of the GNU General Public License
  21127. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21128. + */
  21129. +
  21130. +/*
  21131. + * module initialization and module-global
  21132. + */
  21133. +
  21134. +#ifndef __AUFS_MODULE_H__
  21135. +#define __AUFS_MODULE_H__
  21136. +
  21137. +#ifdef __KERNEL__
  21138. +
  21139. +#include <linux/slab.h>
  21140. +#include "debug.h"
  21141. +
  21142. +struct path;
  21143. +struct seq_file;
  21144. +
  21145. +/* module parameters */
  21146. +extern int sysaufs_brs;
  21147. +extern bool au_userns;
  21148. +
  21149. +/* ---------------------------------------------------------------------- */
  21150. +
  21151. +extern int au_dir_roflags;
  21152. +
  21153. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
  21154. +int au_seq_path(struct seq_file *seq, struct path *path);
  21155. +
  21156. +#ifdef CONFIG_PROC_FS
  21157. +/* procfs.c */
  21158. +int __init au_procfs_init(void);
  21159. +void au_procfs_fin(void);
  21160. +#else
  21161. +AuStubInt0(au_procfs_init, void);
  21162. +AuStubVoid(au_procfs_fin, void);
  21163. +#endif
  21164. +
  21165. +/* ---------------------------------------------------------------------- */
  21166. +
  21167. +/* kmem cache and delayed free */
  21168. +enum {
  21169. + AuCache_DINFO,
  21170. + AuCache_ICNTNR,
  21171. + AuCache_FINFO,
  21172. + AuCache_VDIR,
  21173. + AuCache_DEHSTR,
  21174. + AuCache_HNOTIFY, /* must be last */
  21175. + AuCache_Last
  21176. +};
  21177. +
  21178. +enum {
  21179. + AU_DFREE_KFREE,
  21180. + AU_DFREE_FREE_PAGE,
  21181. + AU_DFREE_Last
  21182. +};
  21183. +
  21184. +struct au_cache {
  21185. + struct kmem_cache *cache;
  21186. + struct llist_head llist; /* delayed free */
  21187. +};
  21188. +
  21189. +/*
  21190. + * in order to reduce the cost of the internal timer, consolidate all the
  21191. + * delayed free works into a single delayed_work.
  21192. + */
  21193. +struct au_dfree {
  21194. + struct au_cache cache[AuCache_Last];
  21195. + struct llist_head llist[AU_DFREE_Last];
  21196. + struct delayed_work dwork;
  21197. +};
  21198. +
  21199. +extern struct au_dfree au_dfree;
  21200. +
  21201. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  21202. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  21203. +#define AuCacheCtor(type, ctor) \
  21204. + kmem_cache_create(#type, sizeof(struct type), \
  21205. + __alignof__(struct type), AuCacheFlags, ctor)
  21206. +
  21207. +#define AU_DFREE_DELAY msecs_to_jiffies(10)
  21208. +#define AU_DFREE_BODY(lnode, llist) do { \
  21209. + if (llist_add(lnode, llist)) \
  21210. + schedule_delayed_work(&au_dfree.dwork, \
  21211. + AU_DFREE_DELAY); \
  21212. + } while (0)
  21213. +#define AU_CACHE_DFREE_FUNC(name, idx, lnode) \
  21214. + void au_cache_dfree_##name(struct au_##name *p) \
  21215. + { \
  21216. + struct au_cache *cp = au_dfree.cache + AuCache_##idx; \
  21217. + AU_DFREE_BODY(&p->lnode, &cp->llist); \
  21218. + }
  21219. +
  21220. +#define AuCacheFuncs(name, index) \
  21221. +static inline struct au_##name *au_cache_alloc_##name(void) \
  21222. +{ return kmem_cache_alloc(au_dfree.cache[AuCache_##index].cache, GFP_NOFS); } \
  21223. +static inline void au_cache_free_##name(struct au_##name *p) \
  21224. +{ kmem_cache_free(au_dfree.cache[AuCache_##index].cache, p); } \
  21225. +void au_cache_dfree_##name(struct au_##name *p)
  21226. +
  21227. +AuCacheFuncs(dinfo, DINFO);
  21228. +AuCacheFuncs(icntnr, ICNTNR);
  21229. +AuCacheFuncs(finfo, FINFO);
  21230. +AuCacheFuncs(vdir, VDIR);
  21231. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  21232. +#ifdef CONFIG_AUFS_HNOTIFY
  21233. +AuCacheFuncs(hnotify, HNOTIFY);
  21234. +#endif
  21235. +
  21236. +static inline void au_delayed_kfree(const void *p)
  21237. +{
  21238. + AuDebugOn(!p);
  21239. + AuDebugOn(ksize(p) < sizeof(struct llist_node));
  21240. +
  21241. + AU_DFREE_BODY((void *)p, au_dfree.llist + AU_DFREE_KFREE);
  21242. +}
  21243. +
  21244. +/* cast only */
  21245. +static inline void au_free_page(void *p)
  21246. +{
  21247. + free_page((unsigned long)p);
  21248. +}
  21249. +
  21250. +static inline void au_delayed_free_page(unsigned long addr)
  21251. +{
  21252. + AU_DFREE_BODY((void *)addr, au_dfree.llist + AU_DFREE_FREE_PAGE);
  21253. +}
  21254. +
  21255. +#endif /* __KERNEL__ */
  21256. +#endif /* __AUFS_MODULE_H__ */
  21257. diff -Naur null/fs/aufs/mvdown.c linux-4.7/fs/aufs/mvdown.c
  21258. --- /dev/null
  21259. +++ linux-4.7/fs/aufs/mvdown.c 2016-08-08 12:57:44.605384608 -0400
  21260. @@ -0,0 +1,704 @@
  21261. +/*
  21262. + * Copyright (C) 2011-2016 Junjiro R. Okajima
  21263. + *
  21264. + * This program, aufs is free software; you can redistribute it and/or modify
  21265. + * it under the terms of the GNU General Public License as published by
  21266. + * the Free Software Foundation; either version 2 of the License, or
  21267. + * (at your option) any later version.
  21268. + *
  21269. + * This program is distributed in the hope that it will be useful,
  21270. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21271. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21272. + * GNU General Public License for more details.
  21273. + *
  21274. + * You should have received a copy of the GNU General Public License
  21275. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21276. + */
  21277. +
  21278. +/*
  21279. + * move-down, opposite of copy-up
  21280. + */
  21281. +
  21282. +#include "aufs.h"
  21283. +
  21284. +struct au_mvd_args {
  21285. + struct {
  21286. + struct super_block *h_sb;
  21287. + struct dentry *h_parent;
  21288. + struct au_hinode *hdir;
  21289. + struct inode *h_dir, *h_inode;
  21290. + struct au_pin pin;
  21291. + } info[AUFS_MVDOWN_NARRAY];
  21292. +
  21293. + struct aufs_mvdown mvdown;
  21294. + struct dentry *dentry, *parent;
  21295. + struct inode *inode, *dir;
  21296. + struct super_block *sb;
  21297. + aufs_bindex_t bopq, bwh, bfound;
  21298. + unsigned char rename_lock;
  21299. +};
  21300. +
  21301. +#define mvd_errno mvdown.au_errno
  21302. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  21303. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  21304. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  21305. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  21306. +
  21307. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  21308. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  21309. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  21310. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  21311. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  21312. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  21313. +
  21314. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  21315. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  21316. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  21317. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  21318. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  21319. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  21320. +
  21321. +#define AU_MVD_PR(flag, ...) do { \
  21322. + if (flag) \
  21323. + pr_err(__VA_ARGS__); \
  21324. + } while (0)
  21325. +
  21326. +static int find_lower_writable(struct au_mvd_args *a)
  21327. +{
  21328. + struct super_block *sb;
  21329. + aufs_bindex_t bindex, bbot;
  21330. + struct au_branch *br;
  21331. +
  21332. + sb = a->sb;
  21333. + bindex = a->mvd_bsrc;
  21334. + bbot = au_sbbot(sb);
  21335. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  21336. + for (bindex++; bindex <= bbot; bindex++) {
  21337. + br = au_sbr(sb, bindex);
  21338. + if (au_br_fhsm(br->br_perm)
  21339. + && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
  21340. + return bindex;
  21341. + }
  21342. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  21343. + for (bindex++; bindex <= bbot; bindex++) {
  21344. + br = au_sbr(sb, bindex);
  21345. + if (!au_br_rdonly(br))
  21346. + return bindex;
  21347. + }
  21348. + else
  21349. + for (bindex++; bindex <= bbot; bindex++) {
  21350. + br = au_sbr(sb, bindex);
  21351. + if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
  21352. + if (au_br_rdonly(br))
  21353. + a->mvdown.flags
  21354. + |= AUFS_MVDOWN_ROLOWER_R;
  21355. + return bindex;
  21356. + }
  21357. + }
  21358. +
  21359. + return -1;
  21360. +}
  21361. +
  21362. +/* make the parent dir on bdst */
  21363. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  21364. +{
  21365. + int err;
  21366. +
  21367. + err = 0;
  21368. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  21369. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  21370. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  21371. + a->mvd_h_dst_parent = NULL;
  21372. + if (au_dbbot(a->parent) >= a->mvd_bdst)
  21373. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  21374. + if (!a->mvd_h_dst_parent) {
  21375. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  21376. + if (unlikely(err)) {
  21377. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  21378. + goto out;
  21379. + }
  21380. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  21381. + }
  21382. +
  21383. +out:
  21384. + AuTraceErr(err);
  21385. + return err;
  21386. +}
  21387. +
  21388. +/* lock them all */
  21389. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  21390. +{
  21391. + int err;
  21392. + struct dentry *h_trap;
  21393. +
  21394. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  21395. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  21396. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  21397. + au_opt_udba(a->sb),
  21398. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21399. + AuTraceErr(err);
  21400. + if (unlikely(err)) {
  21401. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  21402. + goto out;
  21403. + }
  21404. +
  21405. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  21406. + a->rename_lock = 0;
  21407. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  21408. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  21409. + au_opt_udba(a->sb),
  21410. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21411. + err = au_do_pin(&a->mvd_pin_src);
  21412. + AuTraceErr(err);
  21413. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  21414. + if (unlikely(err)) {
  21415. + AU_MVD_PR(dmsg, "pin_src failed\n");
  21416. + goto out_dst;
  21417. + }
  21418. + goto out; /* success */
  21419. + }
  21420. +
  21421. + a->rename_lock = 1;
  21422. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  21423. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  21424. + au_opt_udba(a->sb),
  21425. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21426. + AuTraceErr(err);
  21427. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  21428. + if (unlikely(err)) {
  21429. + AU_MVD_PR(dmsg, "pin_src failed\n");
  21430. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21431. + goto out_dst;
  21432. + }
  21433. + au_pin_hdir_unlock(&a->mvd_pin_src);
  21434. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21435. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21436. + if (h_trap) {
  21437. + err = (h_trap != a->mvd_h_src_parent);
  21438. + if (err)
  21439. + err = (h_trap != a->mvd_h_dst_parent);
  21440. + }
  21441. + BUG_ON(err); /* it should never happen */
  21442. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  21443. + err = -EBUSY;
  21444. + AuTraceErr(err);
  21445. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21446. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21447. + au_pin_hdir_lock(&a->mvd_pin_src);
  21448. + au_unpin(&a->mvd_pin_src);
  21449. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21450. + goto out_dst;
  21451. + }
  21452. + goto out; /* success */
  21453. +
  21454. +out_dst:
  21455. + au_unpin(&a->mvd_pin_dst);
  21456. +out:
  21457. + AuTraceErr(err);
  21458. + return err;
  21459. +}
  21460. +
  21461. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  21462. +{
  21463. + if (!a->rename_lock)
  21464. + au_unpin(&a->mvd_pin_src);
  21465. + else {
  21466. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21467. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21468. + au_pin_hdir_lock(&a->mvd_pin_src);
  21469. + au_unpin(&a->mvd_pin_src);
  21470. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21471. + }
  21472. + au_unpin(&a->mvd_pin_dst);
  21473. +}
  21474. +
  21475. +/* copy-down the file */
  21476. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  21477. +{
  21478. + int err;
  21479. + struct au_cp_generic cpg = {
  21480. + .dentry = a->dentry,
  21481. + .bdst = a->mvd_bdst,
  21482. + .bsrc = a->mvd_bsrc,
  21483. + .len = -1,
  21484. + .pin = &a->mvd_pin_dst,
  21485. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  21486. + };
  21487. +
  21488. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  21489. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  21490. + au_fset_cpup(cpg.flags, OVERWRITE);
  21491. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  21492. + au_fset_cpup(cpg.flags, RWDST);
  21493. + err = au_sio_cpdown_simple(&cpg);
  21494. + if (unlikely(err))
  21495. + AU_MVD_PR(dmsg, "cpdown failed\n");
  21496. +
  21497. + AuTraceErr(err);
  21498. + return err;
  21499. +}
  21500. +
  21501. +/*
  21502. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  21503. + * were sleeping
  21504. + */
  21505. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  21506. +{
  21507. + int err;
  21508. + struct path h_path;
  21509. + struct au_branch *br;
  21510. + struct inode *delegated;
  21511. +
  21512. + br = au_sbr(a->sb, a->mvd_bdst);
  21513. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  21514. + err = PTR_ERR(h_path.dentry);
  21515. + if (IS_ERR(h_path.dentry)) {
  21516. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  21517. + goto out;
  21518. + }
  21519. +
  21520. + err = 0;
  21521. + if (d_is_positive(h_path.dentry)) {
  21522. + h_path.mnt = au_br_mnt(br);
  21523. + delegated = NULL;
  21524. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  21525. + &delegated, /*force*/0);
  21526. + if (unlikely(err == -EWOULDBLOCK)) {
  21527. + pr_warn("cannot retry for NFSv4 delegation"
  21528. + " for an internal unlink\n");
  21529. + iput(delegated);
  21530. + }
  21531. + if (unlikely(err))
  21532. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  21533. + }
  21534. + dput(h_path.dentry);
  21535. +
  21536. +out:
  21537. + AuTraceErr(err);
  21538. + return err;
  21539. +}
  21540. +
  21541. +/*
  21542. + * unlink the topmost h_dentry
  21543. + */
  21544. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  21545. +{
  21546. + int err;
  21547. + struct path h_path;
  21548. + struct inode *delegated;
  21549. +
  21550. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  21551. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  21552. + delegated = NULL;
  21553. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  21554. + if (unlikely(err == -EWOULDBLOCK)) {
  21555. + pr_warn("cannot retry for NFSv4 delegation"
  21556. + " for an internal unlink\n");
  21557. + iput(delegated);
  21558. + }
  21559. + if (unlikely(err))
  21560. + AU_MVD_PR(dmsg, "unlink failed\n");
  21561. +
  21562. + AuTraceErr(err);
  21563. + return err;
  21564. +}
  21565. +
  21566. +/* Since mvdown succeeded, we ignore an error of this function */
  21567. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  21568. +{
  21569. + int err;
  21570. + struct au_branch *br;
  21571. +
  21572. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  21573. + br = au_sbr(a->sb, a->mvd_bsrc);
  21574. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  21575. + if (!err) {
  21576. + br = au_sbr(a->sb, a->mvd_bdst);
  21577. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  21578. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  21579. + }
  21580. + if (!err)
  21581. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  21582. + else
  21583. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  21584. +}
  21585. +
  21586. +/*
  21587. + * copy-down the file and unlink the bsrc file.
  21588. + * - unlink the bdst whout if exist
  21589. + * - copy-down the file (with whtmp name and rename)
  21590. + * - unlink the bsrc file
  21591. + */
  21592. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  21593. +{
  21594. + int err;
  21595. +
  21596. + err = au_do_mkdir(dmsg, a);
  21597. + if (!err)
  21598. + err = au_do_lock(dmsg, a);
  21599. + if (unlikely(err))
  21600. + goto out;
  21601. +
  21602. + /*
  21603. + * do not revert the activities we made on bdst since they should be
  21604. + * harmless in aufs.
  21605. + */
  21606. +
  21607. + err = au_do_cpdown(dmsg, a);
  21608. + if (!err)
  21609. + err = au_do_unlink_wh(dmsg, a);
  21610. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  21611. + err = au_do_unlink(dmsg, a);
  21612. + if (unlikely(err))
  21613. + goto out_unlock;
  21614. +
  21615. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  21616. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  21617. + if (find_lower_writable(a) < 0)
  21618. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  21619. +
  21620. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  21621. + au_do_stfs(dmsg, a);
  21622. +
  21623. + /* maintain internal array */
  21624. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  21625. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  21626. + au_set_dbtop(a->dentry, a->mvd_bdst);
  21627. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  21628. + au_set_ibtop(a->inode, a->mvd_bdst);
  21629. + } else {
  21630. + /* hide the lower */
  21631. + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
  21632. + au_set_dbbot(a->dentry, a->mvd_bsrc);
  21633. + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
  21634. + au_set_ibbot(a->inode, a->mvd_bsrc);
  21635. + }
  21636. + if (au_dbbot(a->dentry) < a->mvd_bdst)
  21637. + au_set_dbbot(a->dentry, a->mvd_bdst);
  21638. + if (au_ibbot(a->inode) < a->mvd_bdst)
  21639. + au_set_ibbot(a->inode, a->mvd_bdst);
  21640. +
  21641. +out_unlock:
  21642. + au_do_unlock(dmsg, a);
  21643. +out:
  21644. + AuTraceErr(err);
  21645. + return err;
  21646. +}
  21647. +
  21648. +/* ---------------------------------------------------------------------- */
  21649. +
  21650. +/* make sure the file is idle */
  21651. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  21652. +{
  21653. + int err, plinked;
  21654. +
  21655. + err = 0;
  21656. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  21657. + if (au_dbtop(a->dentry) == a->mvd_bsrc
  21658. + && au_dcount(a->dentry) == 1
  21659. + && atomic_read(&a->inode->i_count) == 1
  21660. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  21661. + && (!plinked || !au_plink_test(a->inode))
  21662. + && a->inode->i_nlink == 1)
  21663. + goto out;
  21664. +
  21665. + err = -EBUSY;
  21666. + AU_MVD_PR(dmsg,
  21667. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  21668. + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
  21669. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  21670. + a->mvd_h_src_inode->i_nlink,
  21671. + plinked, plinked ? au_plink_test(a->inode) : 0);
  21672. +
  21673. +out:
  21674. + AuTraceErr(err);
  21675. + return err;
  21676. +}
  21677. +
  21678. +/* make sure the parent dir is fine */
  21679. +static int au_mvd_args_parent(const unsigned char dmsg,
  21680. + struct au_mvd_args *a)
  21681. +{
  21682. + int err;
  21683. + aufs_bindex_t bindex;
  21684. +
  21685. + err = 0;
  21686. + if (unlikely(au_alive_dir(a->parent))) {
  21687. + err = -ENOENT;
  21688. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  21689. + goto out;
  21690. + }
  21691. +
  21692. + a->bopq = au_dbdiropq(a->parent);
  21693. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  21694. + AuDbg("b%d\n", bindex);
  21695. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  21696. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  21697. + err = -EINVAL;
  21698. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  21699. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  21700. + a->bopq, a->mvd_bdst);
  21701. + }
  21702. +
  21703. +out:
  21704. + AuTraceErr(err);
  21705. + return err;
  21706. +}
  21707. +
  21708. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  21709. + struct au_mvd_args *a)
  21710. +{
  21711. + int err;
  21712. + struct au_dinfo *dinfo, *tmp;
  21713. +
  21714. + /* lookup the next lower positive entry */
  21715. + err = -ENOMEM;
  21716. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  21717. + if (unlikely(!tmp))
  21718. + goto out;
  21719. +
  21720. + a->bfound = -1;
  21721. + a->bwh = -1;
  21722. + dinfo = au_di(a->dentry);
  21723. + au_di_cp(tmp, dinfo);
  21724. + au_di_swap(tmp, dinfo);
  21725. +
  21726. + /* returns the number of positive dentries */
  21727. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
  21728. + /* AuLkup_IGNORE_PERM */ 0);
  21729. + if (!err)
  21730. + a->bwh = au_dbwh(a->dentry);
  21731. + else if (err > 0)
  21732. + a->bfound = au_dbtop(a->dentry);
  21733. +
  21734. + au_di_swap(tmp, dinfo);
  21735. + au_rw_write_unlock(&tmp->di_rwsem);
  21736. + au_di_free(tmp);
  21737. + if (unlikely(err < 0))
  21738. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  21739. +
  21740. + /*
  21741. + * here, we have these cases.
  21742. + * bfound == -1
  21743. + * no positive dentry under bsrc. there are more sub-cases.
  21744. + * bwh < 0
  21745. + * there no whiteout, we can safely move-down.
  21746. + * bwh <= bsrc
  21747. + * impossible
  21748. + * bsrc < bwh && bwh < bdst
  21749. + * there is a whiteout on RO branch. cannot proceed.
  21750. + * bwh == bdst
  21751. + * there is a whiteout on the RW target branch. it should
  21752. + * be removed.
  21753. + * bdst < bwh
  21754. + * there is a whiteout somewhere unrelated branch.
  21755. + * -1 < bfound && bfound <= bsrc
  21756. + * impossible.
  21757. + * bfound < bdst
  21758. + * found, but it is on RO branch between bsrc and bdst. cannot
  21759. + * proceed.
  21760. + * bfound == bdst
  21761. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  21762. + * error.
  21763. + * bdst < bfound
  21764. + * found, after we create the file on bdst, it will be hidden.
  21765. + */
  21766. +
  21767. + AuDebugOn(a->bfound == -1
  21768. + && a->bwh != -1
  21769. + && a->bwh <= a->mvd_bsrc);
  21770. + AuDebugOn(-1 < a->bfound
  21771. + && a->bfound <= a->mvd_bsrc);
  21772. +
  21773. + err = -EINVAL;
  21774. + if (a->bfound == -1
  21775. + && a->mvd_bsrc < a->bwh
  21776. + && a->bwh != -1
  21777. + && a->bwh < a->mvd_bdst) {
  21778. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  21779. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  21780. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  21781. + goto out;
  21782. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  21783. + a->mvd_errno = EAU_MVDOWN_UPPER;
  21784. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  21785. + a->mvd_bdst, a->bfound);
  21786. + goto out;
  21787. + }
  21788. +
  21789. + err = 0; /* success */
  21790. +
  21791. +out:
  21792. + AuTraceErr(err);
  21793. + return err;
  21794. +}
  21795. +
  21796. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  21797. +{
  21798. + int err;
  21799. +
  21800. + err = 0;
  21801. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  21802. + && a->bfound == a->mvd_bdst)
  21803. + err = -EEXIST;
  21804. + AuTraceErr(err);
  21805. + return err;
  21806. +}
  21807. +
  21808. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  21809. +{
  21810. + int err;
  21811. + struct au_branch *br;
  21812. +
  21813. + err = -EISDIR;
  21814. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  21815. + goto out;
  21816. +
  21817. + err = -EINVAL;
  21818. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  21819. + a->mvd_bsrc = au_ibtop(a->inode);
  21820. + else {
  21821. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  21822. + if (unlikely(a->mvd_bsrc < 0
  21823. + || (a->mvd_bsrc < au_dbtop(a->dentry)
  21824. + || au_dbbot(a->dentry) < a->mvd_bsrc
  21825. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  21826. + || (a->mvd_bsrc < au_ibtop(a->inode)
  21827. + || au_ibbot(a->inode) < a->mvd_bsrc
  21828. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  21829. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  21830. + AU_MVD_PR(dmsg, "no upper\n");
  21831. + goto out;
  21832. + }
  21833. + }
  21834. + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
  21835. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  21836. + AU_MVD_PR(dmsg, "on the bottom\n");
  21837. + goto out;
  21838. + }
  21839. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  21840. + br = au_sbr(a->sb, a->mvd_bsrc);
  21841. + err = au_br_rdonly(br);
  21842. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  21843. + if (unlikely(err))
  21844. + goto out;
  21845. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  21846. + || IS_APPEND(a->mvd_h_src_inode))) {
  21847. + if (err)
  21848. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  21849. + /* go on */
  21850. + } else
  21851. + goto out;
  21852. +
  21853. + err = -EINVAL;
  21854. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  21855. + a->mvd_bdst = find_lower_writable(a);
  21856. + if (unlikely(a->mvd_bdst < 0)) {
  21857. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  21858. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  21859. + goto out;
  21860. + }
  21861. + } else {
  21862. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  21863. + if (unlikely(a->mvd_bdst < 0
  21864. + || au_sbbot(a->sb) < a->mvd_bdst)) {
  21865. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  21866. + AU_MVD_PR(dmsg, "no lower brid\n");
  21867. + goto out;
  21868. + }
  21869. + }
  21870. +
  21871. + err = au_mvd_args_busy(dmsg, a);
  21872. + if (!err)
  21873. + err = au_mvd_args_parent(dmsg, a);
  21874. + if (!err)
  21875. + err = au_mvd_args_intermediate(dmsg, a);
  21876. + if (!err)
  21877. + err = au_mvd_args_exist(dmsg, a);
  21878. + if (!err)
  21879. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  21880. +
  21881. +out:
  21882. + AuTraceErr(err);
  21883. + return err;
  21884. +}
  21885. +
  21886. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  21887. +{
  21888. + int err, e;
  21889. + unsigned char dmsg;
  21890. + struct au_mvd_args *args;
  21891. + struct inode *inode;
  21892. +
  21893. + inode = d_inode(dentry);
  21894. + err = -EPERM;
  21895. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  21896. + goto out;
  21897. +
  21898. + err = -ENOMEM;
  21899. + args = kmalloc(sizeof(*args), GFP_NOFS);
  21900. + if (unlikely(!args))
  21901. + goto out;
  21902. +
  21903. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  21904. + if (!err)
  21905. + err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
  21906. + if (unlikely(err)) {
  21907. + err = -EFAULT;
  21908. + AuTraceErr(err);
  21909. + goto out_free;
  21910. + }
  21911. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  21912. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  21913. + args->mvdown.au_errno = 0;
  21914. + args->dentry = dentry;
  21915. + args->inode = inode;
  21916. + args->sb = dentry->d_sb;
  21917. +
  21918. + err = -ENOENT;
  21919. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  21920. + args->parent = dget_parent(dentry);
  21921. + args->dir = d_inode(args->parent);
  21922. + inode_lock_nested(args->dir, I_MUTEX_PARENT);
  21923. + dput(args->parent);
  21924. + if (unlikely(args->parent != dentry->d_parent)) {
  21925. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  21926. + goto out_dir;
  21927. + }
  21928. +
  21929. + inode_lock_nested(inode, I_MUTEX_CHILD);
  21930. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
  21931. + if (unlikely(err))
  21932. + goto out_inode;
  21933. +
  21934. + di_write_lock_parent(args->parent);
  21935. + err = au_mvd_args(dmsg, args);
  21936. + if (unlikely(err))
  21937. + goto out_parent;
  21938. +
  21939. + err = au_do_mvdown(dmsg, args);
  21940. + if (unlikely(err))
  21941. + goto out_parent;
  21942. +
  21943. + au_cpup_attr_timesizes(args->dir);
  21944. + au_cpup_attr_timesizes(inode);
  21945. + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
  21946. + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
  21947. + /* au_digen_dec(dentry); */
  21948. +
  21949. +out_parent:
  21950. + di_write_unlock(args->parent);
  21951. + aufs_read_unlock(dentry, AuLock_DW);
  21952. +out_inode:
  21953. + inode_unlock(inode);
  21954. +out_dir:
  21955. + inode_unlock(args->dir);
  21956. +out_free:
  21957. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  21958. + if (unlikely(e))
  21959. + err = -EFAULT;
  21960. + au_delayed_kfree(args);
  21961. +out:
  21962. + AuTraceErr(err);
  21963. + return err;
  21964. +}
  21965. diff -Naur null/fs/aufs/opts.c linux-4.7/fs/aufs/opts.c
  21966. --- /dev/null
  21967. +++ linux-4.7/fs/aufs/opts.c 2016-08-08 12:57:44.606384585 -0400
  21968. @@ -0,0 +1,1860 @@
  21969. +/*
  21970. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  21971. + *
  21972. + * This program, aufs is free software; you can redistribute it and/or modify
  21973. + * it under the terms of the GNU General Public License as published by
  21974. + * the Free Software Foundation; either version 2 of the License, or
  21975. + * (at your option) any later version.
  21976. + *
  21977. + * This program is distributed in the hope that it will be useful,
  21978. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21979. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21980. + * GNU General Public License for more details.
  21981. + *
  21982. + * You should have received a copy of the GNU General Public License
  21983. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21984. + */
  21985. +
  21986. +/*
  21987. + * mount options/flags
  21988. + */
  21989. +
  21990. +#include <linux/namei.h>
  21991. +#include <linux/types.h> /* a distribution requires */
  21992. +#include <linux/parser.h>
  21993. +#include "aufs.h"
  21994. +
  21995. +/* ---------------------------------------------------------------------- */
  21996. +
  21997. +enum {
  21998. + Opt_br,
  21999. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  22000. + Opt_idel, Opt_imod,
  22001. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  22002. + Opt_rdblk_def, Opt_rdhash_def,
  22003. + Opt_xino, Opt_noxino,
  22004. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  22005. + Opt_trunc_xino_path, Opt_itrunc_xino,
  22006. + Opt_trunc_xib, Opt_notrunc_xib,
  22007. + Opt_shwh, Opt_noshwh,
  22008. + Opt_plink, Opt_noplink, Opt_list_plink,
  22009. + Opt_udba,
  22010. + Opt_dio, Opt_nodio,
  22011. + Opt_diropq_a, Opt_diropq_w,
  22012. + Opt_warn_perm, Opt_nowarn_perm,
  22013. + Opt_wbr_copyup, Opt_wbr_create,
  22014. + Opt_fhsm_sec,
  22015. + Opt_verbose, Opt_noverbose,
  22016. + Opt_sum, Opt_nosum, Opt_wsum,
  22017. + Opt_dirperm1, Opt_nodirperm1,
  22018. + Opt_acl, Opt_noacl,
  22019. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  22020. +};
  22021. +
  22022. +static match_table_t options = {
  22023. + {Opt_br, "br=%s"},
  22024. + {Opt_br, "br:%s"},
  22025. +
  22026. + {Opt_add, "add=%d:%s"},
  22027. + {Opt_add, "add:%d:%s"},
  22028. + {Opt_add, "ins=%d:%s"},
  22029. + {Opt_add, "ins:%d:%s"},
  22030. + {Opt_append, "append=%s"},
  22031. + {Opt_append, "append:%s"},
  22032. + {Opt_prepend, "prepend=%s"},
  22033. + {Opt_prepend, "prepend:%s"},
  22034. +
  22035. + {Opt_del, "del=%s"},
  22036. + {Opt_del, "del:%s"},
  22037. + /* {Opt_idel, "idel:%d"}, */
  22038. + {Opt_mod, "mod=%s"},
  22039. + {Opt_mod, "mod:%s"},
  22040. + /* {Opt_imod, "imod:%d:%s"}, */
  22041. +
  22042. + {Opt_dirwh, "dirwh=%d"},
  22043. +
  22044. + {Opt_xino, "xino=%s"},
  22045. + {Opt_noxino, "noxino"},
  22046. + {Opt_trunc_xino, "trunc_xino"},
  22047. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  22048. + {Opt_notrunc_xino, "notrunc_xino"},
  22049. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  22050. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  22051. + /* {Opt_zxino, "zxino=%s"}, */
  22052. + {Opt_trunc_xib, "trunc_xib"},
  22053. + {Opt_notrunc_xib, "notrunc_xib"},
  22054. +
  22055. +#ifdef CONFIG_PROC_FS
  22056. + {Opt_plink, "plink"},
  22057. +#else
  22058. + {Opt_ignore_silent, "plink"},
  22059. +#endif
  22060. +
  22061. + {Opt_noplink, "noplink"},
  22062. +
  22063. +#ifdef CONFIG_AUFS_DEBUG
  22064. + {Opt_list_plink, "list_plink"},
  22065. +#endif
  22066. +
  22067. + {Opt_udba, "udba=%s"},
  22068. +
  22069. + {Opt_dio, "dio"},
  22070. + {Opt_nodio, "nodio"},
  22071. +
  22072. +#ifdef CONFIG_AUFS_FHSM
  22073. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  22074. +#else
  22075. + {Opt_ignore_silent, "fhsm_sec=%d"},
  22076. +#endif
  22077. +
  22078. + {Opt_diropq_a, "diropq=always"},
  22079. + {Opt_diropq_a, "diropq=a"},
  22080. + {Opt_diropq_w, "diropq=whiteouted"},
  22081. + {Opt_diropq_w, "diropq=w"},
  22082. +
  22083. + {Opt_warn_perm, "warn_perm"},
  22084. + {Opt_nowarn_perm, "nowarn_perm"},
  22085. +
  22086. + /* keep them temporary */
  22087. + {Opt_ignore_silent, "nodlgt"},
  22088. + {Opt_ignore_silent, "clean_plink"},
  22089. +
  22090. +#ifdef CONFIG_AUFS_SHWH
  22091. + {Opt_shwh, "shwh"},
  22092. +#endif
  22093. + {Opt_noshwh, "noshwh"},
  22094. +
  22095. + {Opt_dirperm1, "dirperm1"},
  22096. + {Opt_nodirperm1, "nodirperm1"},
  22097. +
  22098. + {Opt_verbose, "verbose"},
  22099. + {Opt_verbose, "v"},
  22100. + {Opt_noverbose, "noverbose"},
  22101. + {Opt_noverbose, "quiet"},
  22102. + {Opt_noverbose, "q"},
  22103. + {Opt_noverbose, "silent"},
  22104. +
  22105. + {Opt_sum, "sum"},
  22106. + {Opt_nosum, "nosum"},
  22107. + {Opt_wsum, "wsum"},
  22108. +
  22109. + {Opt_rdcache, "rdcache=%d"},
  22110. + {Opt_rdblk, "rdblk=%d"},
  22111. + {Opt_rdblk_def, "rdblk=def"},
  22112. + {Opt_rdhash, "rdhash=%d"},
  22113. + {Opt_rdhash_def, "rdhash=def"},
  22114. +
  22115. + {Opt_wbr_create, "create=%s"},
  22116. + {Opt_wbr_create, "create_policy=%s"},
  22117. + {Opt_wbr_copyup, "cpup=%s"},
  22118. + {Opt_wbr_copyup, "copyup=%s"},
  22119. + {Opt_wbr_copyup, "copyup_policy=%s"},
  22120. +
  22121. + /* generic VFS flag */
  22122. +#ifdef CONFIG_FS_POSIX_ACL
  22123. + {Opt_acl, "acl"},
  22124. + {Opt_noacl, "noacl"},
  22125. +#else
  22126. + {Opt_ignore_silent, "acl"},
  22127. + {Opt_ignore_silent, "noacl"},
  22128. +#endif
  22129. +
  22130. + /* internal use for the scripts */
  22131. + {Opt_ignore_silent, "si=%s"},
  22132. +
  22133. + {Opt_br, "dirs=%s"},
  22134. + {Opt_ignore, "debug=%d"},
  22135. + {Opt_ignore, "delete=whiteout"},
  22136. + {Opt_ignore, "delete=all"},
  22137. + {Opt_ignore, "imap=%s"},
  22138. +
  22139. + /* temporary workaround, due to old mount(8)? */
  22140. + {Opt_ignore_silent, "relatime"},
  22141. +
  22142. + {Opt_err, NULL}
  22143. +};
  22144. +
  22145. +/* ---------------------------------------------------------------------- */
  22146. +
  22147. +static const char *au_parser_pattern(int val, match_table_t tbl)
  22148. +{
  22149. + struct match_token *p;
  22150. +
  22151. + p = tbl;
  22152. + while (p->pattern) {
  22153. + if (p->token == val)
  22154. + return p->pattern;
  22155. + p++;
  22156. + }
  22157. + BUG();
  22158. + return "??";
  22159. +}
  22160. +
  22161. +static const char *au_optstr(int *val, match_table_t tbl)
  22162. +{
  22163. + struct match_token *p;
  22164. + int v;
  22165. +
  22166. + v = *val;
  22167. + if (!v)
  22168. + goto out;
  22169. + p = tbl;
  22170. + while (p->pattern) {
  22171. + if (p->token
  22172. + && (v & p->token) == p->token) {
  22173. + *val &= ~p->token;
  22174. + return p->pattern;
  22175. + }
  22176. + p++;
  22177. + }
  22178. +
  22179. +out:
  22180. + return NULL;
  22181. +}
  22182. +
  22183. +/* ---------------------------------------------------------------------- */
  22184. +
  22185. +static match_table_t brperm = {
  22186. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  22187. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  22188. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  22189. + {0, NULL}
  22190. +};
  22191. +
  22192. +static match_table_t brattr = {
  22193. + /* general */
  22194. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  22195. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  22196. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  22197. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  22198. +#ifdef CONFIG_AUFS_FHSM
  22199. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  22200. +#endif
  22201. +#ifdef CONFIG_AUFS_XATTR
  22202. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  22203. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  22204. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  22205. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  22206. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  22207. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  22208. +#endif
  22209. +
  22210. + /* ro/rr branch */
  22211. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  22212. +
  22213. + /* rw branch */
  22214. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  22215. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  22216. +
  22217. + {0, NULL}
  22218. +};
  22219. +
  22220. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  22221. +{
  22222. + int attr, v;
  22223. + char *p;
  22224. +
  22225. + attr = 0;
  22226. + do {
  22227. + p = strchr(str, '+');
  22228. + if (p)
  22229. + *p = 0;
  22230. + v = match_token(str, table, args);
  22231. + if (v) {
  22232. + if (v & AuBrAttr_CMOO_Mask)
  22233. + attr &= ~AuBrAttr_CMOO_Mask;
  22234. + attr |= v;
  22235. + } else {
  22236. + if (p)
  22237. + *p = '+';
  22238. + pr_warn("ignored branch attribute %s\n", str);
  22239. + break;
  22240. + }
  22241. + if (p)
  22242. + str = p + 1;
  22243. + } while (p);
  22244. +
  22245. + return attr;
  22246. +}
  22247. +
  22248. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  22249. +{
  22250. + int sz;
  22251. + const char *p;
  22252. + char *q;
  22253. +
  22254. + q = str->a;
  22255. + *q = 0;
  22256. + p = au_optstr(&perm, brattr);
  22257. + if (p) {
  22258. + sz = strlen(p);
  22259. + memcpy(q, p, sz + 1);
  22260. + q += sz;
  22261. + } else
  22262. + goto out;
  22263. +
  22264. + do {
  22265. + p = au_optstr(&perm, brattr);
  22266. + if (p) {
  22267. + *q++ = '+';
  22268. + sz = strlen(p);
  22269. + memcpy(q, p, sz + 1);
  22270. + q += sz;
  22271. + }
  22272. + } while (p);
  22273. +
  22274. +out:
  22275. + return q - str->a;
  22276. +}
  22277. +
  22278. +static int noinline_for_stack br_perm_val(char *perm)
  22279. +{
  22280. + int val, bad, sz;
  22281. + char *p;
  22282. + substring_t args[MAX_OPT_ARGS];
  22283. + au_br_perm_str_t attr;
  22284. +
  22285. + p = strchr(perm, '+');
  22286. + if (p)
  22287. + *p = 0;
  22288. + val = match_token(perm, brperm, args);
  22289. + if (!val) {
  22290. + if (p)
  22291. + *p = '+';
  22292. + pr_warn("ignored branch permission %s\n", perm);
  22293. + val = AuBrPerm_RO;
  22294. + goto out;
  22295. + }
  22296. + if (!p)
  22297. + goto out;
  22298. +
  22299. + val |= br_attr_val(p + 1, brattr, args);
  22300. +
  22301. + bad = 0;
  22302. + switch (val & AuBrPerm_Mask) {
  22303. + case AuBrPerm_RO:
  22304. + case AuBrPerm_RR:
  22305. + bad = val & AuBrWAttr_Mask;
  22306. + val &= ~AuBrWAttr_Mask;
  22307. + break;
  22308. + case AuBrPerm_RW:
  22309. + bad = val & AuBrRAttr_Mask;
  22310. + val &= ~AuBrRAttr_Mask;
  22311. + break;
  22312. + }
  22313. +
  22314. + /*
  22315. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  22316. + * does not treat it as an error, just warning.
  22317. + * this is a tiny guard for the user operation.
  22318. + */
  22319. + if (val & AuBrAttr_UNPIN) {
  22320. + bad |= AuBrAttr_UNPIN;
  22321. + val &= ~AuBrAttr_UNPIN;
  22322. + }
  22323. +
  22324. + if (unlikely(bad)) {
  22325. + sz = au_do_optstr_br_attr(&attr, bad);
  22326. + AuDebugOn(!sz);
  22327. + pr_warn("ignored branch attribute %s\n", attr.a);
  22328. + }
  22329. +
  22330. +out:
  22331. + return val;
  22332. +}
  22333. +
  22334. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  22335. +{
  22336. + au_br_perm_str_t attr;
  22337. + const char *p;
  22338. + char *q;
  22339. + int sz;
  22340. +
  22341. + q = str->a;
  22342. + p = au_optstr(&perm, brperm);
  22343. + AuDebugOn(!p || !*p);
  22344. + sz = strlen(p);
  22345. + memcpy(q, p, sz + 1);
  22346. + q += sz;
  22347. +
  22348. + sz = au_do_optstr_br_attr(&attr, perm);
  22349. + if (sz) {
  22350. + *q++ = '+';
  22351. + memcpy(q, attr.a, sz + 1);
  22352. + }
  22353. +
  22354. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  22355. +}
  22356. +
  22357. +/* ---------------------------------------------------------------------- */
  22358. +
  22359. +static match_table_t udbalevel = {
  22360. + {AuOpt_UDBA_REVAL, "reval"},
  22361. + {AuOpt_UDBA_NONE, "none"},
  22362. +#ifdef CONFIG_AUFS_HNOTIFY
  22363. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  22364. +#ifdef CONFIG_AUFS_HFSNOTIFY
  22365. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  22366. +#endif
  22367. +#endif
  22368. + {-1, NULL}
  22369. +};
  22370. +
  22371. +static int noinline_for_stack udba_val(char *str)
  22372. +{
  22373. + substring_t args[MAX_OPT_ARGS];
  22374. +
  22375. + return match_token(str, udbalevel, args);
  22376. +}
  22377. +
  22378. +const char *au_optstr_udba(int udba)
  22379. +{
  22380. + return au_parser_pattern(udba, udbalevel);
  22381. +}
  22382. +
  22383. +/* ---------------------------------------------------------------------- */
  22384. +
  22385. +static match_table_t au_wbr_create_policy = {
  22386. + {AuWbrCreate_TDP, "tdp"},
  22387. + {AuWbrCreate_TDP, "top-down-parent"},
  22388. + {AuWbrCreate_RR, "rr"},
  22389. + {AuWbrCreate_RR, "round-robin"},
  22390. + {AuWbrCreate_MFS, "mfs"},
  22391. + {AuWbrCreate_MFS, "most-free-space"},
  22392. + {AuWbrCreate_MFSV, "mfs:%d"},
  22393. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  22394. +
  22395. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  22396. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  22397. + {AuWbrCreate_PMFS, "pmfs"},
  22398. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  22399. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  22400. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  22401. +
  22402. + {-1, NULL}
  22403. +};
  22404. +
  22405. +/*
  22406. + * cf. linux/lib/parser.c and cmdline.c
  22407. + * gave up calling memparse() since it uses simple_strtoull() instead of
  22408. + * kstrto...().
  22409. + */
  22410. +static int noinline_for_stack
  22411. +au_match_ull(substring_t *s, unsigned long long *result)
  22412. +{
  22413. + int err;
  22414. + unsigned int len;
  22415. + char a[32];
  22416. +
  22417. + err = -ERANGE;
  22418. + len = s->to - s->from;
  22419. + if (len + 1 <= sizeof(a)) {
  22420. + memcpy(a, s->from, len);
  22421. + a[len] = '\0';
  22422. + err = kstrtoull(a, 0, result);
  22423. + }
  22424. + return err;
  22425. +}
  22426. +
  22427. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  22428. + struct au_opt_wbr_create *create)
  22429. +{
  22430. + int err;
  22431. + unsigned long long ull;
  22432. +
  22433. + err = 0;
  22434. + if (!au_match_ull(arg, &ull))
  22435. + create->mfsrr_watermark = ull;
  22436. + else {
  22437. + pr_err("bad integer in %s\n", str);
  22438. + err = -EINVAL;
  22439. + }
  22440. +
  22441. + return err;
  22442. +}
  22443. +
  22444. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  22445. + struct au_opt_wbr_create *create)
  22446. +{
  22447. + int n, err;
  22448. +
  22449. + err = 0;
  22450. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  22451. + create->mfs_second = n;
  22452. + else {
  22453. + pr_err("bad integer in %s\n", str);
  22454. + err = -EINVAL;
  22455. + }
  22456. +
  22457. + return err;
  22458. +}
  22459. +
  22460. +static int noinline_for_stack
  22461. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  22462. +{
  22463. + int err, e;
  22464. + substring_t args[MAX_OPT_ARGS];
  22465. +
  22466. + err = match_token(str, au_wbr_create_policy, args);
  22467. + create->wbr_create = err;
  22468. + switch (err) {
  22469. + case AuWbrCreate_MFSRRV:
  22470. + case AuWbrCreate_PMFSRRV:
  22471. + e = au_wbr_mfs_wmark(&args[0], str, create);
  22472. + if (!e)
  22473. + e = au_wbr_mfs_sec(&args[1], str, create);
  22474. + if (unlikely(e))
  22475. + err = e;
  22476. + break;
  22477. + case AuWbrCreate_MFSRR:
  22478. + case AuWbrCreate_PMFSRR:
  22479. + e = au_wbr_mfs_wmark(&args[0], str, create);
  22480. + if (unlikely(e)) {
  22481. + err = e;
  22482. + break;
  22483. + }
  22484. + /*FALLTHROUGH*/
  22485. + case AuWbrCreate_MFS:
  22486. + case AuWbrCreate_PMFS:
  22487. + create->mfs_second = AUFS_MFS_DEF_SEC;
  22488. + break;
  22489. + case AuWbrCreate_MFSV:
  22490. + case AuWbrCreate_PMFSV:
  22491. + e = au_wbr_mfs_sec(&args[0], str, create);
  22492. + if (unlikely(e))
  22493. + err = e;
  22494. + break;
  22495. + }
  22496. +
  22497. + return err;
  22498. +}
  22499. +
  22500. +const char *au_optstr_wbr_create(int wbr_create)
  22501. +{
  22502. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  22503. +}
  22504. +
  22505. +static match_table_t au_wbr_copyup_policy = {
  22506. + {AuWbrCopyup_TDP, "tdp"},
  22507. + {AuWbrCopyup_TDP, "top-down-parent"},
  22508. + {AuWbrCopyup_BUP, "bup"},
  22509. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  22510. + {AuWbrCopyup_BU, "bu"},
  22511. + {AuWbrCopyup_BU, "bottom-up"},
  22512. + {-1, NULL}
  22513. +};
  22514. +
  22515. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  22516. +{
  22517. + substring_t args[MAX_OPT_ARGS];
  22518. +
  22519. + return match_token(str, au_wbr_copyup_policy, args);
  22520. +}
  22521. +
  22522. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  22523. +{
  22524. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  22525. +}
  22526. +
  22527. +/* ---------------------------------------------------------------------- */
  22528. +
  22529. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  22530. +
  22531. +static void dump_opts(struct au_opts *opts)
  22532. +{
  22533. +#ifdef CONFIG_AUFS_DEBUG
  22534. + /* reduce stack space */
  22535. + union {
  22536. + struct au_opt_add *add;
  22537. + struct au_opt_del *del;
  22538. + struct au_opt_mod *mod;
  22539. + struct au_opt_xino *xino;
  22540. + struct au_opt_xino_itrunc *xino_itrunc;
  22541. + struct au_opt_wbr_create *create;
  22542. + } u;
  22543. + struct au_opt *opt;
  22544. +
  22545. + opt = opts->opt;
  22546. + while (opt->type != Opt_tail) {
  22547. + switch (opt->type) {
  22548. + case Opt_add:
  22549. + u.add = &opt->add;
  22550. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  22551. + u.add->bindex, u.add->pathname, u.add->perm,
  22552. + u.add->path.dentry);
  22553. + break;
  22554. + case Opt_del:
  22555. + case Opt_idel:
  22556. + u.del = &opt->del;
  22557. + AuDbg("del {%s, %p}\n",
  22558. + u.del->pathname, u.del->h_path.dentry);
  22559. + break;
  22560. + case Opt_mod:
  22561. + case Opt_imod:
  22562. + u.mod = &opt->mod;
  22563. + AuDbg("mod {%s, 0x%x, %p}\n",
  22564. + u.mod->path, u.mod->perm, u.mod->h_root);
  22565. + break;
  22566. + case Opt_append:
  22567. + u.add = &opt->add;
  22568. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  22569. + u.add->bindex, u.add->pathname, u.add->perm,
  22570. + u.add->path.dentry);
  22571. + break;
  22572. + case Opt_prepend:
  22573. + u.add = &opt->add;
  22574. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  22575. + u.add->bindex, u.add->pathname, u.add->perm,
  22576. + u.add->path.dentry);
  22577. + break;
  22578. + case Opt_dirwh:
  22579. + AuDbg("dirwh %d\n", opt->dirwh);
  22580. + break;
  22581. + case Opt_rdcache:
  22582. + AuDbg("rdcache %d\n", opt->rdcache);
  22583. + break;
  22584. + case Opt_rdblk:
  22585. + AuDbg("rdblk %u\n", opt->rdblk);
  22586. + break;
  22587. + case Opt_rdblk_def:
  22588. + AuDbg("rdblk_def\n");
  22589. + break;
  22590. + case Opt_rdhash:
  22591. + AuDbg("rdhash %u\n", opt->rdhash);
  22592. + break;
  22593. + case Opt_rdhash_def:
  22594. + AuDbg("rdhash_def\n");
  22595. + break;
  22596. + case Opt_xino:
  22597. + u.xino = &opt->xino;
  22598. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  22599. + break;
  22600. + case Opt_trunc_xino:
  22601. + AuLabel(trunc_xino);
  22602. + break;
  22603. + case Opt_notrunc_xino:
  22604. + AuLabel(notrunc_xino);
  22605. + break;
  22606. + case Opt_trunc_xino_path:
  22607. + case Opt_itrunc_xino:
  22608. + u.xino_itrunc = &opt->xino_itrunc;
  22609. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  22610. + break;
  22611. + case Opt_noxino:
  22612. + AuLabel(noxino);
  22613. + break;
  22614. + case Opt_trunc_xib:
  22615. + AuLabel(trunc_xib);
  22616. + break;
  22617. + case Opt_notrunc_xib:
  22618. + AuLabel(notrunc_xib);
  22619. + break;
  22620. + case Opt_shwh:
  22621. + AuLabel(shwh);
  22622. + break;
  22623. + case Opt_noshwh:
  22624. + AuLabel(noshwh);
  22625. + break;
  22626. + case Opt_dirperm1:
  22627. + AuLabel(dirperm1);
  22628. + break;
  22629. + case Opt_nodirperm1:
  22630. + AuLabel(nodirperm1);
  22631. + break;
  22632. + case Opt_plink:
  22633. + AuLabel(plink);
  22634. + break;
  22635. + case Opt_noplink:
  22636. + AuLabel(noplink);
  22637. + break;
  22638. + case Opt_list_plink:
  22639. + AuLabel(list_plink);
  22640. + break;
  22641. + case Opt_udba:
  22642. + AuDbg("udba %d, %s\n",
  22643. + opt->udba, au_optstr_udba(opt->udba));
  22644. + break;
  22645. + case Opt_dio:
  22646. + AuLabel(dio);
  22647. + break;
  22648. + case Opt_nodio:
  22649. + AuLabel(nodio);
  22650. + break;
  22651. + case Opt_diropq_a:
  22652. + AuLabel(diropq_a);
  22653. + break;
  22654. + case Opt_diropq_w:
  22655. + AuLabel(diropq_w);
  22656. + break;
  22657. + case Opt_warn_perm:
  22658. + AuLabel(warn_perm);
  22659. + break;
  22660. + case Opt_nowarn_perm:
  22661. + AuLabel(nowarn_perm);
  22662. + break;
  22663. + case Opt_verbose:
  22664. + AuLabel(verbose);
  22665. + break;
  22666. + case Opt_noverbose:
  22667. + AuLabel(noverbose);
  22668. + break;
  22669. + case Opt_sum:
  22670. + AuLabel(sum);
  22671. + break;
  22672. + case Opt_nosum:
  22673. + AuLabel(nosum);
  22674. + break;
  22675. + case Opt_wsum:
  22676. + AuLabel(wsum);
  22677. + break;
  22678. + case Opt_wbr_create:
  22679. + u.create = &opt->wbr_create;
  22680. + AuDbg("create %d, %s\n", u.create->wbr_create,
  22681. + au_optstr_wbr_create(u.create->wbr_create));
  22682. + switch (u.create->wbr_create) {
  22683. + case AuWbrCreate_MFSV:
  22684. + case AuWbrCreate_PMFSV:
  22685. + AuDbg("%d sec\n", u.create->mfs_second);
  22686. + break;
  22687. + case AuWbrCreate_MFSRR:
  22688. + AuDbg("%llu watermark\n",
  22689. + u.create->mfsrr_watermark);
  22690. + break;
  22691. + case AuWbrCreate_MFSRRV:
  22692. + case AuWbrCreate_PMFSRRV:
  22693. + AuDbg("%llu watermark, %d sec\n",
  22694. + u.create->mfsrr_watermark,
  22695. + u.create->mfs_second);
  22696. + break;
  22697. + }
  22698. + break;
  22699. + case Opt_wbr_copyup:
  22700. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  22701. + au_optstr_wbr_copyup(opt->wbr_copyup));
  22702. + break;
  22703. + case Opt_fhsm_sec:
  22704. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  22705. + break;
  22706. + case Opt_acl:
  22707. + AuLabel(acl);
  22708. + break;
  22709. + case Opt_noacl:
  22710. + AuLabel(noacl);
  22711. + break;
  22712. + default:
  22713. + BUG();
  22714. + }
  22715. + opt++;
  22716. + }
  22717. +#endif
  22718. +}
  22719. +
  22720. +void au_opts_free(struct au_opts *opts)
  22721. +{
  22722. + struct au_opt *opt;
  22723. +
  22724. + opt = opts->opt;
  22725. + while (opt->type != Opt_tail) {
  22726. + switch (opt->type) {
  22727. + case Opt_add:
  22728. + case Opt_append:
  22729. + case Opt_prepend:
  22730. + path_put(&opt->add.path);
  22731. + break;
  22732. + case Opt_del:
  22733. + case Opt_idel:
  22734. + path_put(&opt->del.h_path);
  22735. + break;
  22736. + case Opt_mod:
  22737. + case Opt_imod:
  22738. + dput(opt->mod.h_root);
  22739. + break;
  22740. + case Opt_xino:
  22741. + fput(opt->xino.file);
  22742. + break;
  22743. + }
  22744. + opt++;
  22745. + }
  22746. +}
  22747. +
  22748. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  22749. + aufs_bindex_t bindex)
  22750. +{
  22751. + int err;
  22752. + struct au_opt_add *add = &opt->add;
  22753. + char *p;
  22754. +
  22755. + add->bindex = bindex;
  22756. + add->perm = AuBrPerm_RO;
  22757. + add->pathname = opt_str;
  22758. + p = strchr(opt_str, '=');
  22759. + if (p) {
  22760. + *p++ = 0;
  22761. + if (*p)
  22762. + add->perm = br_perm_val(p);
  22763. + }
  22764. +
  22765. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  22766. + if (!err) {
  22767. + if (!p) {
  22768. + add->perm = AuBrPerm_RO;
  22769. + if (au_test_fs_rr(add->path.dentry->d_sb))
  22770. + add->perm = AuBrPerm_RR;
  22771. + else if (!bindex && !(sb_flags & MS_RDONLY))
  22772. + add->perm = AuBrPerm_RW;
  22773. + }
  22774. + opt->type = Opt_add;
  22775. + goto out;
  22776. + }
  22777. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  22778. + err = -EINVAL;
  22779. +
  22780. +out:
  22781. + return err;
  22782. +}
  22783. +
  22784. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  22785. +{
  22786. + int err;
  22787. +
  22788. + del->pathname = args[0].from;
  22789. + AuDbg("del path %s\n", del->pathname);
  22790. +
  22791. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  22792. + if (unlikely(err))
  22793. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  22794. +
  22795. + return err;
  22796. +}
  22797. +
  22798. +#if 0 /* reserved for future use */
  22799. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  22800. + struct au_opt_del *del, substring_t args[])
  22801. +{
  22802. + int err;
  22803. + struct dentry *root;
  22804. +
  22805. + err = -EINVAL;
  22806. + root = sb->s_root;
  22807. + aufs_read_lock(root, AuLock_FLUSH);
  22808. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  22809. + pr_err("out of bounds, %d\n", bindex);
  22810. + goto out;
  22811. + }
  22812. +
  22813. + err = 0;
  22814. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  22815. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  22816. +
  22817. +out:
  22818. + aufs_read_unlock(root, !AuLock_IR);
  22819. + return err;
  22820. +}
  22821. +#endif
  22822. +
  22823. +static int noinline_for_stack
  22824. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  22825. +{
  22826. + int err;
  22827. + struct path path;
  22828. + char *p;
  22829. +
  22830. + err = -EINVAL;
  22831. + mod->path = args[0].from;
  22832. + p = strchr(mod->path, '=');
  22833. + if (unlikely(!p)) {
  22834. + pr_err("no permssion %s\n", args[0].from);
  22835. + goto out;
  22836. + }
  22837. +
  22838. + *p++ = 0;
  22839. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  22840. + if (unlikely(err)) {
  22841. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  22842. + goto out;
  22843. + }
  22844. +
  22845. + mod->perm = br_perm_val(p);
  22846. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  22847. + mod->h_root = dget(path.dentry);
  22848. + path_put(&path);
  22849. +
  22850. +out:
  22851. + return err;
  22852. +}
  22853. +
  22854. +#if 0 /* reserved for future use */
  22855. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  22856. + struct au_opt_mod *mod, substring_t args[])
  22857. +{
  22858. + int err;
  22859. + struct dentry *root;
  22860. +
  22861. + err = -EINVAL;
  22862. + root = sb->s_root;
  22863. + aufs_read_lock(root, AuLock_FLUSH);
  22864. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  22865. + pr_err("out of bounds, %d\n", bindex);
  22866. + goto out;
  22867. + }
  22868. +
  22869. + err = 0;
  22870. + mod->perm = br_perm_val(args[1].from);
  22871. + AuDbg("mod path %s, perm 0x%x, %s\n",
  22872. + mod->path, mod->perm, args[1].from);
  22873. + mod->h_root = dget(au_h_dptr(root, bindex));
  22874. +
  22875. +out:
  22876. + aufs_read_unlock(root, !AuLock_IR);
  22877. + return err;
  22878. +}
  22879. +#endif
  22880. +
  22881. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  22882. + substring_t args[])
  22883. +{
  22884. + int err;
  22885. + struct file *file;
  22886. +
  22887. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  22888. + err = PTR_ERR(file);
  22889. + if (IS_ERR(file))
  22890. + goto out;
  22891. +
  22892. + err = -EINVAL;
  22893. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  22894. + fput(file);
  22895. + pr_err("%s must be outside\n", args[0].from);
  22896. + goto out;
  22897. + }
  22898. +
  22899. + err = 0;
  22900. + xino->file = file;
  22901. + xino->path = args[0].from;
  22902. +
  22903. +out:
  22904. + return err;
  22905. +}
  22906. +
  22907. +static int noinline_for_stack
  22908. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  22909. + struct au_opt_xino_itrunc *xino_itrunc,
  22910. + substring_t args[])
  22911. +{
  22912. + int err;
  22913. + aufs_bindex_t bbot, bindex;
  22914. + struct path path;
  22915. + struct dentry *root;
  22916. +
  22917. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  22918. + if (unlikely(err)) {
  22919. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  22920. + goto out;
  22921. + }
  22922. +
  22923. + xino_itrunc->bindex = -1;
  22924. + root = sb->s_root;
  22925. + aufs_read_lock(root, AuLock_FLUSH);
  22926. + bbot = au_sbbot(sb);
  22927. + for (bindex = 0; bindex <= bbot; bindex++) {
  22928. + if (au_h_dptr(root, bindex) == path.dentry) {
  22929. + xino_itrunc->bindex = bindex;
  22930. + break;
  22931. + }
  22932. + }
  22933. + aufs_read_unlock(root, !AuLock_IR);
  22934. + path_put(&path);
  22935. +
  22936. + if (unlikely(xino_itrunc->bindex < 0)) {
  22937. + pr_err("no such branch %s\n", args[0].from);
  22938. + err = -EINVAL;
  22939. + }
  22940. +
  22941. +out:
  22942. + return err;
  22943. +}
  22944. +
  22945. +/* called without aufs lock */
  22946. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  22947. +{
  22948. + int err, n, token;
  22949. + aufs_bindex_t bindex;
  22950. + unsigned char skipped;
  22951. + struct dentry *root;
  22952. + struct au_opt *opt, *opt_tail;
  22953. + char *opt_str;
  22954. + /* reduce the stack space */
  22955. + union {
  22956. + struct au_opt_xino_itrunc *xino_itrunc;
  22957. + struct au_opt_wbr_create *create;
  22958. + } u;
  22959. + struct {
  22960. + substring_t args[MAX_OPT_ARGS];
  22961. + } *a;
  22962. +
  22963. + err = -ENOMEM;
  22964. + a = kmalloc(sizeof(*a), GFP_NOFS);
  22965. + if (unlikely(!a))
  22966. + goto out;
  22967. +
  22968. + root = sb->s_root;
  22969. + err = 0;
  22970. + bindex = 0;
  22971. + opt = opts->opt;
  22972. + opt_tail = opt + opts->max_opt - 1;
  22973. + opt->type = Opt_tail;
  22974. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  22975. + err = -EINVAL;
  22976. + skipped = 0;
  22977. + token = match_token(opt_str, options, a->args);
  22978. + switch (token) {
  22979. + case Opt_br:
  22980. + err = 0;
  22981. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  22982. + && *opt_str) {
  22983. + err = opt_add(opt, opt_str, opts->sb_flags,
  22984. + bindex++);
  22985. + if (unlikely(!err && ++opt > opt_tail)) {
  22986. + err = -E2BIG;
  22987. + break;
  22988. + }
  22989. + opt->type = Opt_tail;
  22990. + skipped = 1;
  22991. + }
  22992. + break;
  22993. + case Opt_add:
  22994. + if (unlikely(match_int(&a->args[0], &n))) {
  22995. + pr_err("bad integer in %s\n", opt_str);
  22996. + break;
  22997. + }
  22998. + bindex = n;
  22999. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  23000. + bindex);
  23001. + if (!err)
  23002. + opt->type = token;
  23003. + break;
  23004. + case Opt_append:
  23005. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  23006. + /*dummy bindex*/1);
  23007. + if (!err)
  23008. + opt->type = token;
  23009. + break;
  23010. + case Opt_prepend:
  23011. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  23012. + /*bindex*/0);
  23013. + if (!err)
  23014. + opt->type = token;
  23015. + break;
  23016. + case Opt_del:
  23017. + err = au_opts_parse_del(&opt->del, a->args);
  23018. + if (!err)
  23019. + opt->type = token;
  23020. + break;
  23021. +#if 0 /* reserved for future use */
  23022. + case Opt_idel:
  23023. + del->pathname = "(indexed)";
  23024. + if (unlikely(match_int(&args[0], &n))) {
  23025. + pr_err("bad integer in %s\n", opt_str);
  23026. + break;
  23027. + }
  23028. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  23029. + if (!err)
  23030. + opt->type = token;
  23031. + break;
  23032. +#endif
  23033. + case Opt_mod:
  23034. + err = au_opts_parse_mod(&opt->mod, a->args);
  23035. + if (!err)
  23036. + opt->type = token;
  23037. + break;
  23038. +#ifdef IMOD /* reserved for future use */
  23039. + case Opt_imod:
  23040. + u.mod->path = "(indexed)";
  23041. + if (unlikely(match_int(&a->args[0], &n))) {
  23042. + pr_err("bad integer in %s\n", opt_str);
  23043. + break;
  23044. + }
  23045. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  23046. + if (!err)
  23047. + opt->type = token;
  23048. + break;
  23049. +#endif
  23050. + case Opt_xino:
  23051. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  23052. + if (!err)
  23053. + opt->type = token;
  23054. + break;
  23055. +
  23056. + case Opt_trunc_xino_path:
  23057. + err = au_opts_parse_xino_itrunc_path
  23058. + (sb, &opt->xino_itrunc, a->args);
  23059. + if (!err)
  23060. + opt->type = token;
  23061. + break;
  23062. +
  23063. + case Opt_itrunc_xino:
  23064. + u.xino_itrunc = &opt->xino_itrunc;
  23065. + if (unlikely(match_int(&a->args[0], &n))) {
  23066. + pr_err("bad integer in %s\n", opt_str);
  23067. + break;
  23068. + }
  23069. + u.xino_itrunc->bindex = n;
  23070. + aufs_read_lock(root, AuLock_FLUSH);
  23071. + if (n < 0 || au_sbbot(sb) < n) {
  23072. + pr_err("out of bounds, %d\n", n);
  23073. + aufs_read_unlock(root, !AuLock_IR);
  23074. + break;
  23075. + }
  23076. + aufs_read_unlock(root, !AuLock_IR);
  23077. + err = 0;
  23078. + opt->type = token;
  23079. + break;
  23080. +
  23081. + case Opt_dirwh:
  23082. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  23083. + break;
  23084. + err = 0;
  23085. + opt->type = token;
  23086. + break;
  23087. +
  23088. + case Opt_rdcache:
  23089. + if (unlikely(match_int(&a->args[0], &n))) {
  23090. + pr_err("bad integer in %s\n", opt_str);
  23091. + break;
  23092. + }
  23093. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  23094. + pr_err("rdcache must be smaller than %d\n",
  23095. + AUFS_RDCACHE_MAX);
  23096. + break;
  23097. + }
  23098. + opt->rdcache = n;
  23099. + err = 0;
  23100. + opt->type = token;
  23101. + break;
  23102. + case Opt_rdblk:
  23103. + if (unlikely(match_int(&a->args[0], &n)
  23104. + || n < 0
  23105. + || n > KMALLOC_MAX_SIZE)) {
  23106. + pr_err("bad integer in %s\n", opt_str);
  23107. + break;
  23108. + }
  23109. + if (unlikely(n && n < NAME_MAX)) {
  23110. + pr_err("rdblk must be larger than %d\n",
  23111. + NAME_MAX);
  23112. + break;
  23113. + }
  23114. + opt->rdblk = n;
  23115. + err = 0;
  23116. + opt->type = token;
  23117. + break;
  23118. + case Opt_rdhash:
  23119. + if (unlikely(match_int(&a->args[0], &n)
  23120. + || n < 0
  23121. + || n * sizeof(struct hlist_head)
  23122. + > KMALLOC_MAX_SIZE)) {
  23123. + pr_err("bad integer in %s\n", opt_str);
  23124. + break;
  23125. + }
  23126. + opt->rdhash = n;
  23127. + err = 0;
  23128. + opt->type = token;
  23129. + break;
  23130. +
  23131. + case Opt_trunc_xino:
  23132. + case Opt_notrunc_xino:
  23133. + case Opt_noxino:
  23134. + case Opt_trunc_xib:
  23135. + case Opt_notrunc_xib:
  23136. + case Opt_shwh:
  23137. + case Opt_noshwh:
  23138. + case Opt_dirperm1:
  23139. + case Opt_nodirperm1:
  23140. + case Opt_plink:
  23141. + case Opt_noplink:
  23142. + case Opt_list_plink:
  23143. + case Opt_dio:
  23144. + case Opt_nodio:
  23145. + case Opt_diropq_a:
  23146. + case Opt_diropq_w:
  23147. + case Opt_warn_perm:
  23148. + case Opt_nowarn_perm:
  23149. + case Opt_verbose:
  23150. + case Opt_noverbose:
  23151. + case Opt_sum:
  23152. + case Opt_nosum:
  23153. + case Opt_wsum:
  23154. + case Opt_rdblk_def:
  23155. + case Opt_rdhash_def:
  23156. + case Opt_acl:
  23157. + case Opt_noacl:
  23158. + err = 0;
  23159. + opt->type = token;
  23160. + break;
  23161. +
  23162. + case Opt_udba:
  23163. + opt->udba = udba_val(a->args[0].from);
  23164. + if (opt->udba >= 0) {
  23165. + err = 0;
  23166. + opt->type = token;
  23167. + } else
  23168. + pr_err("wrong value, %s\n", opt_str);
  23169. + break;
  23170. +
  23171. + case Opt_wbr_create:
  23172. + u.create = &opt->wbr_create;
  23173. + u.create->wbr_create
  23174. + = au_wbr_create_val(a->args[0].from, u.create);
  23175. + if (u.create->wbr_create >= 0) {
  23176. + err = 0;
  23177. + opt->type = token;
  23178. + } else
  23179. + pr_err("wrong value, %s\n", opt_str);
  23180. + break;
  23181. + case Opt_wbr_copyup:
  23182. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  23183. + if (opt->wbr_copyup >= 0) {
  23184. + err = 0;
  23185. + opt->type = token;
  23186. + } else
  23187. + pr_err("wrong value, %s\n", opt_str);
  23188. + break;
  23189. +
  23190. + case Opt_fhsm_sec:
  23191. + if (unlikely(match_int(&a->args[0], &n)
  23192. + || n < 0)) {
  23193. + pr_err("bad integer in %s\n", opt_str);
  23194. + break;
  23195. + }
  23196. + if (sysaufs_brs) {
  23197. + opt->fhsm_second = n;
  23198. + opt->type = token;
  23199. + } else
  23200. + pr_warn("ignored %s\n", opt_str);
  23201. + err = 0;
  23202. + break;
  23203. +
  23204. + case Opt_ignore:
  23205. + pr_warn("ignored %s\n", opt_str);
  23206. + /*FALLTHROUGH*/
  23207. + case Opt_ignore_silent:
  23208. + skipped = 1;
  23209. + err = 0;
  23210. + break;
  23211. + case Opt_err:
  23212. + pr_err("unknown option %s\n", opt_str);
  23213. + break;
  23214. + }
  23215. +
  23216. + if (!err && !skipped) {
  23217. + if (unlikely(++opt > opt_tail)) {
  23218. + err = -E2BIG;
  23219. + opt--;
  23220. + opt->type = Opt_tail;
  23221. + break;
  23222. + }
  23223. + opt->type = Opt_tail;
  23224. + }
  23225. + }
  23226. +
  23227. + au_delayed_kfree(a);
  23228. + dump_opts(opts);
  23229. + if (unlikely(err))
  23230. + au_opts_free(opts);
  23231. +
  23232. +out:
  23233. + return err;
  23234. +}
  23235. +
  23236. +static int au_opt_wbr_create(struct super_block *sb,
  23237. + struct au_opt_wbr_create *create)
  23238. +{
  23239. + int err;
  23240. + struct au_sbinfo *sbinfo;
  23241. +
  23242. + SiMustWriteLock(sb);
  23243. +
  23244. + err = 1; /* handled */
  23245. + sbinfo = au_sbi(sb);
  23246. + if (sbinfo->si_wbr_create_ops->fin) {
  23247. + err = sbinfo->si_wbr_create_ops->fin(sb);
  23248. + if (!err)
  23249. + err = 1;
  23250. + }
  23251. +
  23252. + sbinfo->si_wbr_create = create->wbr_create;
  23253. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  23254. + switch (create->wbr_create) {
  23255. + case AuWbrCreate_MFSRRV:
  23256. + case AuWbrCreate_MFSRR:
  23257. + case AuWbrCreate_PMFSRR:
  23258. + case AuWbrCreate_PMFSRRV:
  23259. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  23260. + /*FALLTHROUGH*/
  23261. + case AuWbrCreate_MFS:
  23262. + case AuWbrCreate_MFSV:
  23263. + case AuWbrCreate_PMFS:
  23264. + case AuWbrCreate_PMFSV:
  23265. + sbinfo->si_wbr_mfs.mfs_expire
  23266. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  23267. + break;
  23268. + }
  23269. +
  23270. + if (sbinfo->si_wbr_create_ops->init)
  23271. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  23272. +
  23273. + return err;
  23274. +}
  23275. +
  23276. +/*
  23277. + * returns,
  23278. + * plus: processed without an error
  23279. + * zero: unprocessed
  23280. + */
  23281. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  23282. + struct au_opts *opts)
  23283. +{
  23284. + int err;
  23285. + struct au_sbinfo *sbinfo;
  23286. +
  23287. + SiMustWriteLock(sb);
  23288. +
  23289. + err = 1; /* handled */
  23290. + sbinfo = au_sbi(sb);
  23291. + switch (opt->type) {
  23292. + case Opt_udba:
  23293. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  23294. + sbinfo->si_mntflags |= opt->udba;
  23295. + opts->given_udba |= opt->udba;
  23296. + break;
  23297. +
  23298. + case Opt_plink:
  23299. + au_opt_set(sbinfo->si_mntflags, PLINK);
  23300. + break;
  23301. + case Opt_noplink:
  23302. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  23303. + au_plink_put(sb, /*verbose*/1);
  23304. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  23305. + break;
  23306. + case Opt_list_plink:
  23307. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  23308. + au_plink_list(sb);
  23309. + break;
  23310. +
  23311. + case Opt_dio:
  23312. + au_opt_set(sbinfo->si_mntflags, DIO);
  23313. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  23314. + break;
  23315. + case Opt_nodio:
  23316. + au_opt_clr(sbinfo->si_mntflags, DIO);
  23317. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  23318. + break;
  23319. +
  23320. + case Opt_fhsm_sec:
  23321. + au_fhsm_set(sbinfo, opt->fhsm_second);
  23322. + break;
  23323. +
  23324. + case Opt_diropq_a:
  23325. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  23326. + break;
  23327. + case Opt_diropq_w:
  23328. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  23329. + break;
  23330. +
  23331. + case Opt_warn_perm:
  23332. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  23333. + break;
  23334. + case Opt_nowarn_perm:
  23335. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  23336. + break;
  23337. +
  23338. + case Opt_verbose:
  23339. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  23340. + break;
  23341. + case Opt_noverbose:
  23342. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  23343. + break;
  23344. +
  23345. + case Opt_sum:
  23346. + au_opt_set(sbinfo->si_mntflags, SUM);
  23347. + break;
  23348. + case Opt_wsum:
  23349. + au_opt_clr(sbinfo->si_mntflags, SUM);
  23350. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  23351. + case Opt_nosum:
  23352. + au_opt_clr(sbinfo->si_mntflags, SUM);
  23353. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  23354. + break;
  23355. +
  23356. + case Opt_wbr_create:
  23357. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  23358. + break;
  23359. + case Opt_wbr_copyup:
  23360. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  23361. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  23362. + break;
  23363. +
  23364. + case Opt_dirwh:
  23365. + sbinfo->si_dirwh = opt->dirwh;
  23366. + break;
  23367. +
  23368. + case Opt_rdcache:
  23369. + sbinfo->si_rdcache
  23370. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  23371. + break;
  23372. + case Opt_rdblk:
  23373. + sbinfo->si_rdblk = opt->rdblk;
  23374. + break;
  23375. + case Opt_rdblk_def:
  23376. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  23377. + break;
  23378. + case Opt_rdhash:
  23379. + sbinfo->si_rdhash = opt->rdhash;
  23380. + break;
  23381. + case Opt_rdhash_def:
  23382. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  23383. + break;
  23384. +
  23385. + case Opt_shwh:
  23386. + au_opt_set(sbinfo->si_mntflags, SHWH);
  23387. + break;
  23388. + case Opt_noshwh:
  23389. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  23390. + break;
  23391. +
  23392. + case Opt_dirperm1:
  23393. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  23394. + break;
  23395. + case Opt_nodirperm1:
  23396. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  23397. + break;
  23398. +
  23399. + case Opt_trunc_xino:
  23400. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  23401. + break;
  23402. + case Opt_notrunc_xino:
  23403. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  23404. + break;
  23405. +
  23406. + case Opt_trunc_xino_path:
  23407. + case Opt_itrunc_xino:
  23408. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
  23409. + if (!err)
  23410. + err = 1;
  23411. + break;
  23412. +
  23413. + case Opt_trunc_xib:
  23414. + au_fset_opts(opts->flags, TRUNC_XIB);
  23415. + break;
  23416. + case Opt_notrunc_xib:
  23417. + au_fclr_opts(opts->flags, TRUNC_XIB);
  23418. + break;
  23419. +
  23420. + case Opt_acl:
  23421. + sb->s_flags |= MS_POSIXACL;
  23422. + break;
  23423. + case Opt_noacl:
  23424. + sb->s_flags &= ~MS_POSIXACL;
  23425. + break;
  23426. +
  23427. + default:
  23428. + err = 0;
  23429. + break;
  23430. + }
  23431. +
  23432. + return err;
  23433. +}
  23434. +
  23435. +/*
  23436. + * returns tri-state.
  23437. + * plus: processed without an error
  23438. + * zero: unprocessed
  23439. + * minus: error
  23440. + */
  23441. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  23442. + struct au_opts *opts)
  23443. +{
  23444. + int err, do_refresh;
  23445. +
  23446. + err = 0;
  23447. + switch (opt->type) {
  23448. + case Opt_append:
  23449. + opt->add.bindex = au_sbbot(sb) + 1;
  23450. + if (opt->add.bindex < 0)
  23451. + opt->add.bindex = 0;
  23452. + goto add;
  23453. + case Opt_prepend:
  23454. + opt->add.bindex = 0;
  23455. + add: /* indented label */
  23456. + case Opt_add:
  23457. + err = au_br_add(sb, &opt->add,
  23458. + au_ftest_opts(opts->flags, REMOUNT));
  23459. + if (!err) {
  23460. + err = 1;
  23461. + au_fset_opts(opts->flags, REFRESH);
  23462. + }
  23463. + break;
  23464. +
  23465. + case Opt_del:
  23466. + case Opt_idel:
  23467. + err = au_br_del(sb, &opt->del,
  23468. + au_ftest_opts(opts->flags, REMOUNT));
  23469. + if (!err) {
  23470. + err = 1;
  23471. + au_fset_opts(opts->flags, TRUNC_XIB);
  23472. + au_fset_opts(opts->flags, REFRESH);
  23473. + }
  23474. + break;
  23475. +
  23476. + case Opt_mod:
  23477. + case Opt_imod:
  23478. + err = au_br_mod(sb, &opt->mod,
  23479. + au_ftest_opts(opts->flags, REMOUNT),
  23480. + &do_refresh);
  23481. + if (!err) {
  23482. + err = 1;
  23483. + if (do_refresh)
  23484. + au_fset_opts(opts->flags, REFRESH);
  23485. + }
  23486. + break;
  23487. + }
  23488. +
  23489. + return err;
  23490. +}
  23491. +
  23492. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  23493. + struct au_opt_xino **opt_xino,
  23494. + struct au_opts *opts)
  23495. +{
  23496. + int err;
  23497. + aufs_bindex_t bbot, bindex;
  23498. + struct dentry *root, *parent, *h_root;
  23499. +
  23500. + err = 0;
  23501. + switch (opt->type) {
  23502. + case Opt_xino:
  23503. + err = au_xino_set(sb, &opt->xino,
  23504. + !!au_ftest_opts(opts->flags, REMOUNT));
  23505. + if (unlikely(err))
  23506. + break;
  23507. +
  23508. + *opt_xino = &opt->xino;
  23509. + au_xino_brid_set(sb, -1);
  23510. +
  23511. + /* safe d_parent access */
  23512. + parent = opt->xino.file->f_path.dentry->d_parent;
  23513. + root = sb->s_root;
  23514. + bbot = au_sbbot(sb);
  23515. + for (bindex = 0; bindex <= bbot; bindex++) {
  23516. + h_root = au_h_dptr(root, bindex);
  23517. + if (h_root == parent) {
  23518. + au_xino_brid_set(sb, au_sbr_id(sb, bindex));
  23519. + break;
  23520. + }
  23521. + }
  23522. + break;
  23523. +
  23524. + case Opt_noxino:
  23525. + au_xino_clr(sb);
  23526. + au_xino_brid_set(sb, -1);
  23527. + *opt_xino = (void *)-1;
  23528. + break;
  23529. + }
  23530. +
  23531. + return err;
  23532. +}
  23533. +
  23534. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  23535. + unsigned int pending)
  23536. +{
  23537. + int err, fhsm;
  23538. + aufs_bindex_t bindex, bbot;
  23539. + unsigned char do_plink, skip, do_free, can_no_dreval;
  23540. + struct au_branch *br;
  23541. + struct au_wbr *wbr;
  23542. + struct dentry *root, *dentry;
  23543. + struct inode *dir, *h_dir;
  23544. + struct au_sbinfo *sbinfo;
  23545. + struct au_hinode *hdir;
  23546. +
  23547. + SiMustAnyLock(sb);
  23548. +
  23549. + sbinfo = au_sbi(sb);
  23550. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  23551. +
  23552. + if (!(sb_flags & MS_RDONLY)) {
  23553. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  23554. + pr_warn("first branch should be rw\n");
  23555. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  23556. + pr_warn_once("shwh should be used with ro\n");
  23557. + }
  23558. +
  23559. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  23560. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  23561. + pr_warn_once("udba=*notify requires xino\n");
  23562. +
  23563. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  23564. + pr_warn_once("dirperm1 breaks the protection"
  23565. + " by the permission bits on the lower branch\n");
  23566. +
  23567. + err = 0;
  23568. + fhsm = 0;
  23569. + root = sb->s_root;
  23570. + dir = d_inode(root);
  23571. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  23572. + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
  23573. + UDBA_NONE);
  23574. + bbot = au_sbbot(sb);
  23575. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  23576. + skip = 0;
  23577. + h_dir = au_h_iptr(dir, bindex);
  23578. + br = au_sbr(sb, bindex);
  23579. +
  23580. + if ((br->br_perm & AuBrAttr_ICEX)
  23581. + && !h_dir->i_op->listxattr)
  23582. + br->br_perm &= ~AuBrAttr_ICEX;
  23583. +#if 0
  23584. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  23585. + && (au_br_sb(br)->s_flags & MS_NOSEC))
  23586. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  23587. +#endif
  23588. +
  23589. + do_free = 0;
  23590. + wbr = br->br_wbr;
  23591. + if (wbr)
  23592. + wbr_wh_read_lock(wbr);
  23593. +
  23594. + if (!au_br_writable(br->br_perm)) {
  23595. + do_free = !!wbr;
  23596. + skip = (!wbr
  23597. + || (!wbr->wbr_whbase
  23598. + && !wbr->wbr_plink
  23599. + && !wbr->wbr_orph));
  23600. + } else if (!au_br_wh_linkable(br->br_perm)) {
  23601. + /* skip = (!br->br_whbase && !br->br_orph); */
  23602. + skip = (!wbr || !wbr->wbr_whbase);
  23603. + if (skip && wbr) {
  23604. + if (do_plink)
  23605. + skip = !!wbr->wbr_plink;
  23606. + else
  23607. + skip = !wbr->wbr_plink;
  23608. + }
  23609. + } else {
  23610. + /* skip = (br->br_whbase && br->br_ohph); */
  23611. + skip = (wbr && wbr->wbr_whbase);
  23612. + if (skip) {
  23613. + if (do_plink)
  23614. + skip = !!wbr->wbr_plink;
  23615. + else
  23616. + skip = !wbr->wbr_plink;
  23617. + }
  23618. + }
  23619. + if (wbr)
  23620. + wbr_wh_read_unlock(wbr);
  23621. +
  23622. + if (can_no_dreval) {
  23623. + dentry = br->br_path.dentry;
  23624. + spin_lock(&dentry->d_lock);
  23625. + if (dentry->d_flags &
  23626. + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
  23627. + can_no_dreval = 0;
  23628. + spin_unlock(&dentry->d_lock);
  23629. + }
  23630. +
  23631. + if (au_br_fhsm(br->br_perm)) {
  23632. + fhsm++;
  23633. + AuDebugOn(!br->br_fhsm);
  23634. + }
  23635. +
  23636. + if (skip)
  23637. + continue;
  23638. +
  23639. + hdir = au_hi(dir, bindex);
  23640. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  23641. + if (wbr)
  23642. + wbr_wh_write_lock(wbr);
  23643. + err = au_wh_init(br, sb);
  23644. + if (wbr)
  23645. + wbr_wh_write_unlock(wbr);
  23646. + au_hn_inode_unlock(hdir);
  23647. +
  23648. + if (!err && do_free) {
  23649. + if (wbr)
  23650. + au_delayed_kfree(wbr);
  23651. + br->br_wbr = NULL;
  23652. + }
  23653. + }
  23654. +
  23655. + if (can_no_dreval)
  23656. + au_fset_si(sbinfo, NO_DREVAL);
  23657. + else
  23658. + au_fclr_si(sbinfo, NO_DREVAL);
  23659. +
  23660. + if (fhsm >= 2) {
  23661. + au_fset_si(sbinfo, FHSM);
  23662. + for (bindex = bbot; bindex >= 0; bindex--) {
  23663. + br = au_sbr(sb, bindex);
  23664. + if (au_br_fhsm(br->br_perm)) {
  23665. + au_fhsm_set_bottom(sb, bindex);
  23666. + break;
  23667. + }
  23668. + }
  23669. + } else {
  23670. + au_fclr_si(sbinfo, FHSM);
  23671. + au_fhsm_set_bottom(sb, -1);
  23672. + }
  23673. +
  23674. + return err;
  23675. +}
  23676. +
  23677. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  23678. +{
  23679. + int err;
  23680. + unsigned int tmp;
  23681. + aufs_bindex_t bindex, bbot;
  23682. + struct au_opt *opt;
  23683. + struct au_opt_xino *opt_xino, xino;
  23684. + struct au_sbinfo *sbinfo;
  23685. + struct au_branch *br;
  23686. + struct inode *dir;
  23687. +
  23688. + SiMustWriteLock(sb);
  23689. +
  23690. + err = 0;
  23691. + opt_xino = NULL;
  23692. + opt = opts->opt;
  23693. + while (err >= 0 && opt->type != Opt_tail)
  23694. + err = au_opt_simple(sb, opt++, opts);
  23695. + if (err > 0)
  23696. + err = 0;
  23697. + else if (unlikely(err < 0))
  23698. + goto out;
  23699. +
  23700. + /* disable xino and udba temporary */
  23701. + sbinfo = au_sbi(sb);
  23702. + tmp = sbinfo->si_mntflags;
  23703. + au_opt_clr(sbinfo->si_mntflags, XINO);
  23704. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  23705. +
  23706. + opt = opts->opt;
  23707. + while (err >= 0 && opt->type != Opt_tail)
  23708. + err = au_opt_br(sb, opt++, opts);
  23709. + if (err > 0)
  23710. + err = 0;
  23711. + else if (unlikely(err < 0))
  23712. + goto out;
  23713. +
  23714. + bbot = au_sbbot(sb);
  23715. + if (unlikely(bbot < 0)) {
  23716. + err = -EINVAL;
  23717. + pr_err("no branches\n");
  23718. + goto out;
  23719. + }
  23720. +
  23721. + if (au_opt_test(tmp, XINO))
  23722. + au_opt_set(sbinfo->si_mntflags, XINO);
  23723. + opt = opts->opt;
  23724. + while (!err && opt->type != Opt_tail)
  23725. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  23726. + if (unlikely(err))
  23727. + goto out;
  23728. +
  23729. + err = au_opts_verify(sb, sb->s_flags, tmp);
  23730. + if (unlikely(err))
  23731. + goto out;
  23732. +
  23733. + /* restore xino */
  23734. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  23735. + xino.file = au_xino_def(sb);
  23736. + err = PTR_ERR(xino.file);
  23737. + if (IS_ERR(xino.file))
  23738. + goto out;
  23739. +
  23740. + err = au_xino_set(sb, &xino, /*remount*/0);
  23741. + fput(xino.file);
  23742. + if (unlikely(err))
  23743. + goto out;
  23744. + }
  23745. +
  23746. + /* restore udba */
  23747. + tmp &= AuOptMask_UDBA;
  23748. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  23749. + sbinfo->si_mntflags |= tmp;
  23750. + bbot = au_sbbot(sb);
  23751. + for (bindex = 0; bindex <= bbot; bindex++) {
  23752. + br = au_sbr(sb, bindex);
  23753. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  23754. + if (unlikely(err))
  23755. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  23756. + bindex, err);
  23757. + /* go on even if err */
  23758. + }
  23759. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  23760. + dir = d_inode(sb->s_root);
  23761. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  23762. + }
  23763. +
  23764. +out:
  23765. + return err;
  23766. +}
  23767. +
  23768. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  23769. +{
  23770. + int err, rerr;
  23771. + unsigned char no_dreval;
  23772. + struct inode *dir;
  23773. + struct au_opt_xino *opt_xino;
  23774. + struct au_opt *opt;
  23775. + struct au_sbinfo *sbinfo;
  23776. +
  23777. + SiMustWriteLock(sb);
  23778. +
  23779. + err = 0;
  23780. + dir = d_inode(sb->s_root);
  23781. + sbinfo = au_sbi(sb);
  23782. + opt_xino = NULL;
  23783. + opt = opts->opt;
  23784. + while (err >= 0 && opt->type != Opt_tail) {
  23785. + err = au_opt_simple(sb, opt, opts);
  23786. + if (!err)
  23787. + err = au_opt_br(sb, opt, opts);
  23788. + if (!err)
  23789. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  23790. + opt++;
  23791. + }
  23792. + if (err > 0)
  23793. + err = 0;
  23794. + AuTraceErr(err);
  23795. + /* go on even err */
  23796. +
  23797. + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
  23798. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  23799. + if (unlikely(rerr && !err))
  23800. + err = rerr;
  23801. +
  23802. + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
  23803. + au_fset_opts(opts->flags, REFRESH_IDOP);
  23804. +
  23805. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  23806. + rerr = au_xib_trunc(sb);
  23807. + if (unlikely(rerr && !err))
  23808. + err = rerr;
  23809. + }
  23810. +
  23811. + /* will be handled by the caller */
  23812. + if (!au_ftest_opts(opts->flags, REFRESH)
  23813. + && (opts->given_udba
  23814. + || au_opt_test(sbinfo->si_mntflags, XINO)
  23815. + || au_ftest_opts(opts->flags, REFRESH_IDOP)
  23816. + ))
  23817. + au_fset_opts(opts->flags, REFRESH);
  23818. +
  23819. + AuDbg("status 0x%x\n", opts->flags);
  23820. + return err;
  23821. +}
  23822. +
  23823. +/* ---------------------------------------------------------------------- */
  23824. +
  23825. +unsigned int au_opt_udba(struct super_block *sb)
  23826. +{
  23827. + return au_mntflags(sb) & AuOptMask_UDBA;
  23828. +}
  23829. diff -Naur null/fs/aufs/opts.h linux-4.7/fs/aufs/opts.h
  23830. --- /dev/null
  23831. +++ linux-4.7/fs/aufs/opts.h 2016-08-08 12:57:44.606384585 -0400
  23832. @@ -0,0 +1,211 @@
  23833. +/*
  23834. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  23835. + *
  23836. + * This program, aufs is free software; you can redistribute it and/or modify
  23837. + * it under the terms of the GNU General Public License as published by
  23838. + * the Free Software Foundation; either version 2 of the License, or
  23839. + * (at your option) any later version.
  23840. + *
  23841. + * This program is distributed in the hope that it will be useful,
  23842. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23843. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23844. + * GNU General Public License for more details.
  23845. + *
  23846. + * You should have received a copy of the GNU General Public License
  23847. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23848. + */
  23849. +
  23850. +/*
  23851. + * mount options/flags
  23852. + */
  23853. +
  23854. +#ifndef __AUFS_OPTS_H__
  23855. +#define __AUFS_OPTS_H__
  23856. +
  23857. +#ifdef __KERNEL__
  23858. +
  23859. +#include <linux/path.h>
  23860. +
  23861. +struct file;
  23862. +struct super_block;
  23863. +
  23864. +/* ---------------------------------------------------------------------- */
  23865. +
  23866. +/* mount flags */
  23867. +#define AuOpt_XINO 1 /* external inode number bitmap
  23868. + and translation table */
  23869. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  23870. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  23871. +#define AuOpt_UDBA_REVAL (1 << 3)
  23872. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  23873. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  23874. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  23875. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  23876. + bits */
  23877. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  23878. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  23879. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  23880. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  23881. +#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
  23882. +#define AuOpt_DIO (1 << 14) /* direct io */
  23883. +
  23884. +#ifndef CONFIG_AUFS_HNOTIFY
  23885. +#undef AuOpt_UDBA_HNOTIFY
  23886. +#define AuOpt_UDBA_HNOTIFY 0
  23887. +#endif
  23888. +#ifndef CONFIG_AUFS_SHWH
  23889. +#undef AuOpt_SHWH
  23890. +#define AuOpt_SHWH 0
  23891. +#endif
  23892. +
  23893. +#define AuOpt_Def (AuOpt_XINO \
  23894. + | AuOpt_UDBA_REVAL \
  23895. + | AuOpt_PLINK \
  23896. + /* | AuOpt_DIRPERM1 */ \
  23897. + | AuOpt_WARN_PERM)
  23898. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  23899. + | AuOpt_UDBA_REVAL \
  23900. + | AuOpt_UDBA_HNOTIFY)
  23901. +
  23902. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  23903. +#define au_opt_set(flags, name) do { \
  23904. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  23905. + ((flags) |= AuOpt_##name); \
  23906. +} while (0)
  23907. +#define au_opt_set_udba(flags, name) do { \
  23908. + (flags) &= ~AuOptMask_UDBA; \
  23909. + ((flags) |= AuOpt_##name); \
  23910. +} while (0)
  23911. +#define au_opt_clr(flags, name) do { \
  23912. + ((flags) &= ~AuOpt_##name); \
  23913. +} while (0)
  23914. +
  23915. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  23916. +{
  23917. +#ifdef CONFIG_PROC_FS
  23918. + return mntflags;
  23919. +#else
  23920. + return mntflags & ~AuOpt_PLINK;
  23921. +#endif
  23922. +}
  23923. +
  23924. +/* ---------------------------------------------------------------------- */
  23925. +
  23926. +/* policies to select one among multiple writable branches */
  23927. +enum {
  23928. + AuWbrCreate_TDP, /* top down parent */
  23929. + AuWbrCreate_RR, /* round robin */
  23930. + AuWbrCreate_MFS, /* most free space */
  23931. + AuWbrCreate_MFSV, /* mfs with seconds */
  23932. + AuWbrCreate_MFSRR, /* mfs then rr */
  23933. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  23934. + AuWbrCreate_PMFS, /* parent and mfs */
  23935. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  23936. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  23937. + AuWbrCreate_PMFSRRV, /* plus seconds */
  23938. +
  23939. + AuWbrCreate_Def = AuWbrCreate_TDP
  23940. +};
  23941. +
  23942. +enum {
  23943. + AuWbrCopyup_TDP, /* top down parent */
  23944. + AuWbrCopyup_BUP, /* bottom up parent */
  23945. + AuWbrCopyup_BU, /* bottom up */
  23946. +
  23947. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  23948. +};
  23949. +
  23950. +/* ---------------------------------------------------------------------- */
  23951. +
  23952. +struct au_opt_add {
  23953. + aufs_bindex_t bindex;
  23954. + char *pathname;
  23955. + int perm;
  23956. + struct path path;
  23957. +};
  23958. +
  23959. +struct au_opt_del {
  23960. + char *pathname;
  23961. + struct path h_path;
  23962. +};
  23963. +
  23964. +struct au_opt_mod {
  23965. + char *path;
  23966. + int perm;
  23967. + struct dentry *h_root;
  23968. +};
  23969. +
  23970. +struct au_opt_xino {
  23971. + char *path;
  23972. + struct file *file;
  23973. +};
  23974. +
  23975. +struct au_opt_xino_itrunc {
  23976. + aufs_bindex_t bindex;
  23977. +};
  23978. +
  23979. +struct au_opt_wbr_create {
  23980. + int wbr_create;
  23981. + int mfs_second;
  23982. + unsigned long long mfsrr_watermark;
  23983. +};
  23984. +
  23985. +struct au_opt {
  23986. + int type;
  23987. + union {
  23988. + struct au_opt_xino xino;
  23989. + struct au_opt_xino_itrunc xino_itrunc;
  23990. + struct au_opt_add add;
  23991. + struct au_opt_del del;
  23992. + struct au_opt_mod mod;
  23993. + int dirwh;
  23994. + int rdcache;
  23995. + unsigned int rdblk;
  23996. + unsigned int rdhash;
  23997. + int udba;
  23998. + struct au_opt_wbr_create wbr_create;
  23999. + int wbr_copyup;
  24000. + unsigned int fhsm_second;
  24001. + };
  24002. +};
  24003. +
  24004. +/* opts flags */
  24005. +#define AuOpts_REMOUNT 1
  24006. +#define AuOpts_REFRESH (1 << 1)
  24007. +#define AuOpts_TRUNC_XIB (1 << 2)
  24008. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  24009. +#define AuOpts_REFRESH_IDOP (1 << 4)
  24010. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  24011. +#define au_fset_opts(flags, name) \
  24012. + do { (flags) |= AuOpts_##name; } while (0)
  24013. +#define au_fclr_opts(flags, name) \
  24014. + do { (flags) &= ~AuOpts_##name; } while (0)
  24015. +
  24016. +struct au_opts {
  24017. + struct au_opt *opt;
  24018. + int max_opt;
  24019. +
  24020. + unsigned int given_udba;
  24021. + unsigned int flags;
  24022. + unsigned long sb_flags;
  24023. +};
  24024. +
  24025. +/* ---------------------------------------------------------------------- */
  24026. +
  24027. +/* opts.c */
  24028. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  24029. +const char *au_optstr_udba(int udba);
  24030. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  24031. +const char *au_optstr_wbr_create(int wbr_create);
  24032. +
  24033. +void au_opts_free(struct au_opts *opts);
  24034. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  24035. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  24036. + unsigned int pending);
  24037. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  24038. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  24039. +
  24040. +unsigned int au_opt_udba(struct super_block *sb);
  24041. +
  24042. +#endif /* __KERNEL__ */
  24043. +#endif /* __AUFS_OPTS_H__ */
  24044. diff -Naur null/fs/aufs/plink.c linux-4.7/fs/aufs/plink.c
  24045. --- /dev/null
  24046. +++ linux-4.7/fs/aufs/plink.c 2016-08-08 12:57:44.606384585 -0400
  24047. @@ -0,0 +1,514 @@
  24048. +/*
  24049. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  24050. + *
  24051. + * This program, aufs is free software; you can redistribute it and/or modify
  24052. + * it under the terms of the GNU General Public License as published by
  24053. + * the Free Software Foundation; either version 2 of the License, or
  24054. + * (at your option) any later version.
  24055. + *
  24056. + * This program is distributed in the hope that it will be useful,
  24057. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24058. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24059. + * GNU General Public License for more details.
  24060. + *
  24061. + * You should have received a copy of the GNU General Public License
  24062. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24063. + */
  24064. +
  24065. +/*
  24066. + * pseudo-link
  24067. + */
  24068. +
  24069. +#include "aufs.h"
  24070. +
  24071. +/*
  24072. + * the pseudo-link maintenance mode.
  24073. + * during a user process maintains the pseudo-links,
  24074. + * prohibit adding a new plink and branch manipulation.
  24075. + *
  24076. + * Flags
  24077. + * NOPLM:
  24078. + * For entry functions which will handle plink, and i_mutex is already held
  24079. + * in VFS.
  24080. + * They cannot wait and should return an error at once.
  24081. + * Callers has to check the error.
  24082. + * NOPLMW:
  24083. + * For entry functions which will handle plink, but i_mutex is not held
  24084. + * in VFS.
  24085. + * They can wait the plink maintenance mode to finish.
  24086. + *
  24087. + * They behave like F_SETLK and F_SETLKW.
  24088. + * If the caller never handle plink, then both flags are unnecessary.
  24089. + */
  24090. +
  24091. +int au_plink_maint(struct super_block *sb, int flags)
  24092. +{
  24093. + int err;
  24094. + pid_t pid, ppid;
  24095. + struct task_struct *parent, *prev;
  24096. + struct au_sbinfo *sbi;
  24097. +
  24098. + SiMustAnyLock(sb);
  24099. +
  24100. + err = 0;
  24101. + if (!au_opt_test(au_mntflags(sb), PLINK))
  24102. + goto out;
  24103. +
  24104. + sbi = au_sbi(sb);
  24105. + pid = sbi->si_plink_maint_pid;
  24106. + if (!pid || pid == current->pid)
  24107. + goto out;
  24108. +
  24109. + /* todo: it highly depends upon /sbin/mount.aufs */
  24110. + prev = NULL;
  24111. + parent = current;
  24112. + ppid = 0;
  24113. + rcu_read_lock();
  24114. + while (1) {
  24115. + parent = rcu_dereference(parent->real_parent);
  24116. + if (parent == prev)
  24117. + break;
  24118. + ppid = task_pid_vnr(parent);
  24119. + if (pid == ppid) {
  24120. + rcu_read_unlock();
  24121. + goto out;
  24122. + }
  24123. + prev = parent;
  24124. + }
  24125. + rcu_read_unlock();
  24126. +
  24127. + if (au_ftest_lock(flags, NOPLMW)) {
  24128. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  24129. + /* AuDebugOn(!lockdep_depth(current)); */
  24130. + while (sbi->si_plink_maint_pid) {
  24131. + si_read_unlock(sb);
  24132. + /* gave up wake_up_bit() */
  24133. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  24134. +
  24135. + if (au_ftest_lock(flags, FLUSH))
  24136. + au_nwt_flush(&sbi->si_nowait);
  24137. + si_noflush_read_lock(sb);
  24138. + }
  24139. + } else if (au_ftest_lock(flags, NOPLM)) {
  24140. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  24141. + err = -EAGAIN;
  24142. + }
  24143. +
  24144. +out:
  24145. + return err;
  24146. +}
  24147. +
  24148. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  24149. +{
  24150. + spin_lock(&sbinfo->si_plink_maint_lock);
  24151. + sbinfo->si_plink_maint_pid = 0;
  24152. + spin_unlock(&sbinfo->si_plink_maint_lock);
  24153. + wake_up_all(&sbinfo->si_plink_wq);
  24154. +}
  24155. +
  24156. +int au_plink_maint_enter(struct super_block *sb)
  24157. +{
  24158. + int err;
  24159. + struct au_sbinfo *sbinfo;
  24160. +
  24161. + err = 0;
  24162. + sbinfo = au_sbi(sb);
  24163. + /* make sure i am the only one in this fs */
  24164. + si_write_lock(sb, AuLock_FLUSH);
  24165. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  24166. + spin_lock(&sbinfo->si_plink_maint_lock);
  24167. + if (!sbinfo->si_plink_maint_pid)
  24168. + sbinfo->si_plink_maint_pid = current->pid;
  24169. + else
  24170. + err = -EBUSY;
  24171. + spin_unlock(&sbinfo->si_plink_maint_lock);
  24172. + }
  24173. + si_write_unlock(sb);
  24174. +
  24175. + return err;
  24176. +}
  24177. +
  24178. +/* ---------------------------------------------------------------------- */
  24179. +
  24180. +#ifdef CONFIG_AUFS_DEBUG
  24181. +void au_plink_list(struct super_block *sb)
  24182. +{
  24183. + int i;
  24184. + struct au_sbinfo *sbinfo;
  24185. + struct hlist_head *plink_hlist;
  24186. + struct au_icntnr *icntnr;
  24187. +
  24188. + SiMustAnyLock(sb);
  24189. +
  24190. + sbinfo = au_sbi(sb);
  24191. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24192. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24193. +
  24194. + for (i = 0; i < AuPlink_NHASH; i++) {
  24195. + plink_hlist = &sbinfo->si_plink[i].head;
  24196. + rcu_read_lock();
  24197. + hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
  24198. + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
  24199. + rcu_read_unlock();
  24200. + }
  24201. +}
  24202. +#endif
  24203. +
  24204. +/* is the inode pseudo-linked? */
  24205. +int au_plink_test(struct inode *inode)
  24206. +{
  24207. + int found, i;
  24208. + struct au_sbinfo *sbinfo;
  24209. + struct hlist_head *plink_hlist;
  24210. + struct au_icntnr *icntnr;
  24211. +
  24212. + sbinfo = au_sbi(inode->i_sb);
  24213. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  24214. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  24215. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  24216. +
  24217. + found = 0;
  24218. + i = au_plink_hash(inode->i_ino);
  24219. + plink_hlist = &sbinfo->si_plink[i].head;
  24220. + rcu_read_lock();
  24221. + hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
  24222. + if (&icntnr->vfs_inode == inode) {
  24223. + found = 1;
  24224. + break;
  24225. + }
  24226. + rcu_read_unlock();
  24227. + return found;
  24228. +}
  24229. +
  24230. +/* ---------------------------------------------------------------------- */
  24231. +
  24232. +/*
  24233. + * generate a name for plink.
  24234. + * the file will be stored under AUFS_WH_PLINKDIR.
  24235. + */
  24236. +/* 20 is max digits length of ulong 64 */
  24237. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  24238. +
  24239. +static int plink_name(char *name, int len, struct inode *inode,
  24240. + aufs_bindex_t bindex)
  24241. +{
  24242. + int rlen;
  24243. + struct inode *h_inode;
  24244. +
  24245. + h_inode = au_h_iptr(inode, bindex);
  24246. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  24247. + return rlen;
  24248. +}
  24249. +
  24250. +struct au_do_plink_lkup_args {
  24251. + struct dentry **errp;
  24252. + struct qstr *tgtname;
  24253. + struct dentry *h_parent;
  24254. + struct au_branch *br;
  24255. +};
  24256. +
  24257. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  24258. + struct dentry *h_parent,
  24259. + struct au_branch *br)
  24260. +{
  24261. + struct dentry *h_dentry;
  24262. + struct inode *h_inode;
  24263. +
  24264. + h_inode = d_inode(h_parent);
  24265. + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
  24266. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  24267. + inode_unlock(h_inode);
  24268. + return h_dentry;
  24269. +}
  24270. +
  24271. +static void au_call_do_plink_lkup(void *args)
  24272. +{
  24273. + struct au_do_plink_lkup_args *a = args;
  24274. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  24275. +}
  24276. +
  24277. +/* lookup the plink-ed @inode under the branch at @bindex */
  24278. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  24279. +{
  24280. + struct dentry *h_dentry, *h_parent;
  24281. + struct au_branch *br;
  24282. + int wkq_err;
  24283. + char a[PLINK_NAME_LEN];
  24284. + struct qstr tgtname = QSTR_INIT(a, 0);
  24285. +
  24286. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  24287. +
  24288. + br = au_sbr(inode->i_sb, bindex);
  24289. + h_parent = br->br_wbr->wbr_plink;
  24290. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  24291. +
  24292. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  24293. + struct au_do_plink_lkup_args args = {
  24294. + .errp = &h_dentry,
  24295. + .tgtname = &tgtname,
  24296. + .h_parent = h_parent,
  24297. + .br = br
  24298. + };
  24299. +
  24300. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  24301. + if (unlikely(wkq_err))
  24302. + h_dentry = ERR_PTR(wkq_err);
  24303. + } else
  24304. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  24305. +
  24306. + return h_dentry;
  24307. +}
  24308. +
  24309. +/* create a pseudo-link */
  24310. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  24311. + struct dentry *h_dentry, struct au_branch *br)
  24312. +{
  24313. + int err;
  24314. + struct path h_path = {
  24315. + .mnt = au_br_mnt(br)
  24316. + };
  24317. + struct inode *h_dir, *delegated;
  24318. +
  24319. + h_dir = d_inode(h_parent);
  24320. + inode_lock_nested(h_dir, AuLsc_I_CHILD2);
  24321. +again:
  24322. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  24323. + err = PTR_ERR(h_path.dentry);
  24324. + if (IS_ERR(h_path.dentry))
  24325. + goto out;
  24326. +
  24327. + err = 0;
  24328. + /* wh.plink dir is not monitored */
  24329. + /* todo: is it really safe? */
  24330. + if (d_is_positive(h_path.dentry)
  24331. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  24332. + delegated = NULL;
  24333. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  24334. + if (unlikely(err == -EWOULDBLOCK)) {
  24335. + pr_warn("cannot retry for NFSv4 delegation"
  24336. + " for an internal unlink\n");
  24337. + iput(delegated);
  24338. + }
  24339. + dput(h_path.dentry);
  24340. + h_path.dentry = NULL;
  24341. + if (!err)
  24342. + goto again;
  24343. + }
  24344. + if (!err && d_is_negative(h_path.dentry)) {
  24345. + delegated = NULL;
  24346. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  24347. + if (unlikely(err == -EWOULDBLOCK)) {
  24348. + pr_warn("cannot retry for NFSv4 delegation"
  24349. + " for an internal link\n");
  24350. + iput(delegated);
  24351. + }
  24352. + }
  24353. + dput(h_path.dentry);
  24354. +
  24355. +out:
  24356. + inode_unlock(h_dir);
  24357. + return err;
  24358. +}
  24359. +
  24360. +struct do_whplink_args {
  24361. + int *errp;
  24362. + struct qstr *tgt;
  24363. + struct dentry *h_parent;
  24364. + struct dentry *h_dentry;
  24365. + struct au_branch *br;
  24366. +};
  24367. +
  24368. +static void call_do_whplink(void *args)
  24369. +{
  24370. + struct do_whplink_args *a = args;
  24371. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  24372. +}
  24373. +
  24374. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  24375. + aufs_bindex_t bindex, struct au_branch *br)
  24376. +{
  24377. + int err, wkq_err;
  24378. + struct au_wbr *wbr;
  24379. + struct dentry *h_parent;
  24380. + char a[PLINK_NAME_LEN];
  24381. + struct qstr tgtname = QSTR_INIT(a, 0);
  24382. +
  24383. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  24384. + h_parent = wbr->wbr_plink;
  24385. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  24386. +
  24387. + /* always superio. */
  24388. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  24389. + struct do_whplink_args args = {
  24390. + .errp = &err,
  24391. + .tgt = &tgtname,
  24392. + .h_parent = h_parent,
  24393. + .h_dentry = h_dentry,
  24394. + .br = br
  24395. + };
  24396. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  24397. + if (unlikely(wkq_err))
  24398. + err = wkq_err;
  24399. + } else
  24400. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  24401. +
  24402. + return err;
  24403. +}
  24404. +
  24405. +/*
  24406. + * create a new pseudo-link for @h_dentry on @bindex.
  24407. + * the linked inode is held in aufs @inode.
  24408. + */
  24409. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  24410. + struct dentry *h_dentry)
  24411. +{
  24412. + struct super_block *sb;
  24413. + struct au_sbinfo *sbinfo;
  24414. + struct hlist_head *plink_hlist;
  24415. + struct au_icntnr *icntnr;
  24416. + struct au_sphlhead *sphl;
  24417. + int found, err, cnt, i;
  24418. +
  24419. + sb = inode->i_sb;
  24420. + sbinfo = au_sbi(sb);
  24421. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24422. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24423. +
  24424. + found = au_plink_test(inode);
  24425. + if (found)
  24426. + return;
  24427. +
  24428. + i = au_plink_hash(inode->i_ino);
  24429. + sphl = sbinfo->si_plink + i;
  24430. + plink_hlist = &sphl->head;
  24431. + au_igrab(inode);
  24432. +
  24433. + spin_lock(&sphl->spin);
  24434. + hlist_for_each_entry(icntnr, plink_hlist, plink) {
  24435. + if (&icntnr->vfs_inode == inode) {
  24436. + found = 1;
  24437. + break;
  24438. + }
  24439. + }
  24440. + if (!found) {
  24441. + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
  24442. + hlist_add_head_rcu(&icntnr->plink, plink_hlist);
  24443. + }
  24444. + spin_unlock(&sphl->spin);
  24445. + if (!found) {
  24446. + cnt = au_sphl_count(sphl);
  24447. +#define msg "unexpectedly unblanced or too many pseudo-links"
  24448. + if (cnt > AUFS_PLINK_WARN)
  24449. + AuWarn1(msg ", %d\n", cnt);
  24450. +#undef msg
  24451. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  24452. + if (unlikely(err)) {
  24453. + pr_warn("err %d, damaged pseudo link.\n", err);
  24454. + au_sphl_del_rcu(&icntnr->plink, sphl);
  24455. + iput(&icntnr->vfs_inode);
  24456. + }
  24457. + } else
  24458. + iput(&icntnr->vfs_inode);
  24459. +}
  24460. +
  24461. +/* free all plinks */
  24462. +void au_plink_put(struct super_block *sb, int verbose)
  24463. +{
  24464. + int i, warned;
  24465. + struct au_sbinfo *sbinfo;
  24466. + struct hlist_head *plink_hlist;
  24467. + struct hlist_node *tmp;
  24468. + struct au_icntnr *icntnr;
  24469. +
  24470. + SiMustWriteLock(sb);
  24471. +
  24472. + sbinfo = au_sbi(sb);
  24473. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24474. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24475. +
  24476. + /* no spin_lock since sbinfo is write-locked */
  24477. + warned = 0;
  24478. + for (i = 0; i < AuPlink_NHASH; i++) {
  24479. + plink_hlist = &sbinfo->si_plink[i].head;
  24480. + if (!warned && verbose && !hlist_empty(plink_hlist)) {
  24481. + pr_warn("pseudo-link is not flushed");
  24482. + warned = 1;
  24483. + }
  24484. + hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink)
  24485. + iput(&icntnr->vfs_inode);
  24486. + INIT_HLIST_HEAD(plink_hlist);
  24487. + }
  24488. +}
  24489. +
  24490. +void au_plink_clean(struct super_block *sb, int verbose)
  24491. +{
  24492. + struct dentry *root;
  24493. +
  24494. + root = sb->s_root;
  24495. + aufs_write_lock(root);
  24496. + if (au_opt_test(au_mntflags(sb), PLINK))
  24497. + au_plink_put(sb, verbose);
  24498. + aufs_write_unlock(root);
  24499. +}
  24500. +
  24501. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  24502. +{
  24503. + int do_put;
  24504. + aufs_bindex_t btop, bbot, bindex;
  24505. +
  24506. + do_put = 0;
  24507. + btop = au_ibtop(inode);
  24508. + bbot = au_ibbot(inode);
  24509. + if (btop >= 0) {
  24510. + for (bindex = btop; bindex <= bbot; bindex++) {
  24511. + if (!au_h_iptr(inode, bindex)
  24512. + || au_ii_br_id(inode, bindex) != br_id)
  24513. + continue;
  24514. + au_set_h_iptr(inode, bindex, NULL, 0);
  24515. + do_put = 1;
  24516. + break;
  24517. + }
  24518. + if (do_put)
  24519. + for (bindex = btop; bindex <= bbot; bindex++)
  24520. + if (au_h_iptr(inode, bindex)) {
  24521. + do_put = 0;
  24522. + break;
  24523. + }
  24524. + } else
  24525. + do_put = 1;
  24526. +
  24527. + return do_put;
  24528. +}
  24529. +
  24530. +/* free the plinks on a branch specified by @br_id */
  24531. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  24532. +{
  24533. + struct au_sbinfo *sbinfo;
  24534. + struct hlist_head *plink_hlist;
  24535. + struct hlist_node *tmp;
  24536. + struct au_icntnr *icntnr;
  24537. + struct inode *inode;
  24538. + int i, do_put;
  24539. +
  24540. + SiMustWriteLock(sb);
  24541. +
  24542. + sbinfo = au_sbi(sb);
  24543. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24544. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24545. +
  24546. + /* no spin_lock since sbinfo is write-locked */
  24547. + for (i = 0; i < AuPlink_NHASH; i++) {
  24548. + plink_hlist = &sbinfo->si_plink[i].head;
  24549. + hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink) {
  24550. + inode = au_igrab(&icntnr->vfs_inode);
  24551. + ii_write_lock_child(inode);
  24552. + do_put = au_plink_do_half_refresh(inode, br_id);
  24553. + if (do_put) {
  24554. + hlist_del(&icntnr->plink);
  24555. + iput(inode);
  24556. + }
  24557. + ii_write_unlock(inode);
  24558. + iput(inode);
  24559. + }
  24560. + }
  24561. +}
  24562. diff -Naur null/fs/aufs/poll.c linux-4.7/fs/aufs/poll.c
  24563. --- /dev/null
  24564. +++ linux-4.7/fs/aufs/poll.c 2016-08-08 12:57:44.606384585 -0400
  24565. @@ -0,0 +1,52 @@
  24566. +/*
  24567. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  24568. + *
  24569. + * This program, aufs is free software; you can redistribute it and/or modify
  24570. + * it under the terms of the GNU General Public License as published by
  24571. + * the Free Software Foundation; either version 2 of the License, or
  24572. + * (at your option) any later version.
  24573. + *
  24574. + * This program is distributed in the hope that it will be useful,
  24575. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24576. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24577. + * GNU General Public License for more details.
  24578. + *
  24579. + * You should have received a copy of the GNU General Public License
  24580. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24581. + */
  24582. +
  24583. +/*
  24584. + * poll operation
  24585. + * There is only one filesystem which implements ->poll operation, currently.
  24586. + */
  24587. +
  24588. +#include "aufs.h"
  24589. +
  24590. +unsigned int aufs_poll(struct file *file, poll_table *wait)
  24591. +{
  24592. + unsigned int mask;
  24593. + int err;
  24594. + struct file *h_file;
  24595. + struct super_block *sb;
  24596. +
  24597. + /* We should pretend an error happened. */
  24598. + mask = POLLERR /* | POLLIN | POLLOUT */;
  24599. + sb = file->f_path.dentry->d_sb;
  24600. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  24601. +
  24602. + h_file = au_read_pre(file, /*keep_fi*/0);
  24603. + err = PTR_ERR(h_file);
  24604. + if (IS_ERR(h_file))
  24605. + goto out;
  24606. +
  24607. + /* it is not an error if h_file has no operation */
  24608. + mask = DEFAULT_POLLMASK;
  24609. + if (h_file->f_op->poll)
  24610. + mask = h_file->f_op->poll(h_file, wait);
  24611. + fput(h_file); /* instead of au_read_post() */
  24612. +
  24613. +out:
  24614. + si_read_unlock(sb);
  24615. + AuTraceErr((int)mask);
  24616. + return mask;
  24617. +}
  24618. diff -Naur null/fs/aufs/posix_acl.c linux-4.7/fs/aufs/posix_acl.c
  24619. --- /dev/null
  24620. +++ linux-4.7/fs/aufs/posix_acl.c 2016-08-08 12:57:44.606384585 -0400
  24621. @@ -0,0 +1,98 @@
  24622. +/*
  24623. + * Copyright (C) 2014-2016 Junjiro R. Okajima
  24624. + *
  24625. + * This program, aufs is free software; you can redistribute it and/or modify
  24626. + * it under the terms of the GNU General Public License as published by
  24627. + * the Free Software Foundation; either version 2 of the License, or
  24628. + * (at your option) any later version.
  24629. + *
  24630. + * This program is distributed in the hope that it will be useful,
  24631. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24632. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24633. + * GNU General Public License for more details.
  24634. + *
  24635. + * You should have received a copy of the GNU General Public License
  24636. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24637. + */
  24638. +
  24639. +/*
  24640. + * posix acl operations
  24641. + */
  24642. +
  24643. +#include <linux/fs.h>
  24644. +#include "aufs.h"
  24645. +
  24646. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  24647. +{
  24648. + struct posix_acl *acl;
  24649. + int err;
  24650. + aufs_bindex_t bindex;
  24651. + struct inode *h_inode;
  24652. + struct super_block *sb;
  24653. +
  24654. + acl = NULL;
  24655. + sb = inode->i_sb;
  24656. + si_read_lock(sb, AuLock_FLUSH);
  24657. + ii_read_lock_child(inode);
  24658. + if (!(sb->s_flags & MS_POSIXACL))
  24659. + goto out;
  24660. +
  24661. + bindex = au_ibtop(inode);
  24662. + h_inode = au_h_iptr(inode, bindex);
  24663. + if (unlikely(!h_inode
  24664. + || ((h_inode->i_mode & S_IFMT)
  24665. + != (inode->i_mode & S_IFMT)))) {
  24666. + err = au_busy_or_stale();
  24667. + acl = ERR_PTR(err);
  24668. + goto out;
  24669. + }
  24670. +
  24671. + /* always topmost only */
  24672. + acl = get_acl(h_inode, type);
  24673. +
  24674. +out:
  24675. + ii_read_unlock(inode);
  24676. + si_read_unlock(sb);
  24677. +
  24678. + AuTraceErrPtr(acl);
  24679. + return acl;
  24680. +}
  24681. +
  24682. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  24683. +{
  24684. + int err;
  24685. + ssize_t ssz;
  24686. + struct dentry *dentry;
  24687. + struct au_srxattr arg = {
  24688. + .type = AU_ACL_SET,
  24689. + .u.acl_set = {
  24690. + .acl = acl,
  24691. + .type = type
  24692. + },
  24693. + };
  24694. +
  24695. + IMustLock(inode);
  24696. +
  24697. + if (inode->i_ino == AUFS_ROOT_INO)
  24698. + dentry = dget(inode->i_sb->s_root);
  24699. + else {
  24700. + dentry = d_find_alias(inode);
  24701. + if (!dentry)
  24702. + dentry = d_find_any_alias(inode);
  24703. + if (!dentry) {
  24704. + pr_warn("cannot handle this inode, "
  24705. + "please report to aufs-users ML\n");
  24706. + err = -ENOENT;
  24707. + goto out;
  24708. + }
  24709. + }
  24710. +
  24711. + ssz = au_srxattr(dentry, inode, &arg);
  24712. + dput(dentry);
  24713. + err = ssz;
  24714. + if (ssz >= 0)
  24715. + err = 0;
  24716. +
  24717. +out:
  24718. + return err;
  24719. +}
  24720. diff -Naur null/fs/aufs/procfs.c linux-4.7/fs/aufs/procfs.c
  24721. --- /dev/null
  24722. +++ linux-4.7/fs/aufs/procfs.c 2016-08-08 12:57:44.607384563 -0400
  24723. @@ -0,0 +1,169 @@
  24724. +/*
  24725. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  24726. + *
  24727. + * This program, aufs is free software; you can redistribute it and/or modify
  24728. + * it under the terms of the GNU General Public License as published by
  24729. + * the Free Software Foundation; either version 2 of the License, or
  24730. + * (at your option) any later version.
  24731. + *
  24732. + * This program is distributed in the hope that it will be useful,
  24733. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24734. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24735. + * GNU General Public License for more details.
  24736. + *
  24737. + * You should have received a copy of the GNU General Public License
  24738. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24739. + */
  24740. +
  24741. +/*
  24742. + * procfs interfaces
  24743. + */
  24744. +
  24745. +#include <linux/proc_fs.h>
  24746. +#include "aufs.h"
  24747. +
  24748. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  24749. +{
  24750. + struct au_sbinfo *sbinfo;
  24751. +
  24752. + sbinfo = file->private_data;
  24753. + if (sbinfo) {
  24754. + au_plink_maint_leave(sbinfo);
  24755. + kobject_put(&sbinfo->si_kobj);
  24756. + }
  24757. +
  24758. + return 0;
  24759. +}
  24760. +
  24761. +static void au_procfs_plm_write_clean(struct file *file)
  24762. +{
  24763. + struct au_sbinfo *sbinfo;
  24764. +
  24765. + sbinfo = file->private_data;
  24766. + if (sbinfo)
  24767. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  24768. +}
  24769. +
  24770. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  24771. +{
  24772. + int err;
  24773. + struct super_block *sb;
  24774. + struct au_sbinfo *sbinfo;
  24775. +
  24776. + err = -EBUSY;
  24777. + if (unlikely(file->private_data))
  24778. + goto out;
  24779. +
  24780. + sb = NULL;
  24781. + /* don't use au_sbilist_lock() here */
  24782. + spin_lock(&au_sbilist.spin);
  24783. + hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  24784. + if (id == sysaufs_si_id(sbinfo)) {
  24785. + kobject_get(&sbinfo->si_kobj);
  24786. + sb = sbinfo->si_sb;
  24787. + break;
  24788. + }
  24789. + spin_unlock(&au_sbilist.spin);
  24790. +
  24791. + err = -EINVAL;
  24792. + if (unlikely(!sb))
  24793. + goto out;
  24794. +
  24795. + err = au_plink_maint_enter(sb);
  24796. + if (!err)
  24797. + /* keep kobject_get() */
  24798. + file->private_data = sbinfo;
  24799. + else
  24800. + kobject_put(&sbinfo->si_kobj);
  24801. +out:
  24802. + return err;
  24803. +}
  24804. +
  24805. +/*
  24806. + * Accept a valid "si=xxxx" only.
  24807. + * Once it is accepted successfully, accept "clean" too.
  24808. + */
  24809. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  24810. + size_t count, loff_t *ppos)
  24811. +{
  24812. + ssize_t err;
  24813. + unsigned long id;
  24814. + /* last newline is allowed */
  24815. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  24816. +
  24817. + err = -EACCES;
  24818. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  24819. + goto out;
  24820. +
  24821. + err = -EINVAL;
  24822. + if (unlikely(count > sizeof(buf)))
  24823. + goto out;
  24824. +
  24825. + err = copy_from_user(buf, ubuf, count);
  24826. + if (unlikely(err)) {
  24827. + err = -EFAULT;
  24828. + goto out;
  24829. + }
  24830. + buf[count] = 0;
  24831. +
  24832. + err = -EINVAL;
  24833. + if (!strcmp("clean", buf)) {
  24834. + au_procfs_plm_write_clean(file);
  24835. + goto out_success;
  24836. + } else if (unlikely(strncmp("si=", buf, 3)))
  24837. + goto out;
  24838. +
  24839. + err = kstrtoul(buf + 3, 16, &id);
  24840. + if (unlikely(err))
  24841. + goto out;
  24842. +
  24843. + err = au_procfs_plm_write_si(file, id);
  24844. + if (unlikely(err))
  24845. + goto out;
  24846. +
  24847. +out_success:
  24848. + err = count; /* success */
  24849. +out:
  24850. + return err;
  24851. +}
  24852. +
  24853. +static const struct file_operations au_procfs_plm_fop = {
  24854. + .write = au_procfs_plm_write,
  24855. + .release = au_procfs_plm_release,
  24856. + .owner = THIS_MODULE
  24857. +};
  24858. +
  24859. +/* ---------------------------------------------------------------------- */
  24860. +
  24861. +static struct proc_dir_entry *au_procfs_dir;
  24862. +
  24863. +void au_procfs_fin(void)
  24864. +{
  24865. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  24866. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  24867. +}
  24868. +
  24869. +int __init au_procfs_init(void)
  24870. +{
  24871. + int err;
  24872. + struct proc_dir_entry *entry;
  24873. +
  24874. + err = -ENOMEM;
  24875. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  24876. + if (unlikely(!au_procfs_dir))
  24877. + goto out;
  24878. +
  24879. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
  24880. + au_procfs_dir, &au_procfs_plm_fop);
  24881. + if (unlikely(!entry))
  24882. + goto out_dir;
  24883. +
  24884. + err = 0;
  24885. + goto out; /* success */
  24886. +
  24887. +
  24888. +out_dir:
  24889. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  24890. +out:
  24891. + return err;
  24892. +}
  24893. diff -Naur null/fs/aufs/rdu.c linux-4.7/fs/aufs/rdu.c
  24894. --- /dev/null
  24895. +++ linux-4.7/fs/aufs/rdu.c 2016-08-08 12:57:44.607384563 -0400
  24896. @@ -0,0 +1,381 @@
  24897. +/*
  24898. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  24899. + *
  24900. + * This program, aufs is free software; you can redistribute it and/or modify
  24901. + * it under the terms of the GNU General Public License as published by
  24902. + * the Free Software Foundation; either version 2 of the License, or
  24903. + * (at your option) any later version.
  24904. + *
  24905. + * This program is distributed in the hope that it will be useful,
  24906. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24907. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24908. + * GNU General Public License for more details.
  24909. + *
  24910. + * You should have received a copy of the GNU General Public License
  24911. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24912. + */
  24913. +
  24914. +/*
  24915. + * readdir in userspace.
  24916. + */
  24917. +
  24918. +#include <linux/compat.h>
  24919. +#include <linux/fs_stack.h>
  24920. +#include <linux/security.h>
  24921. +#include "aufs.h"
  24922. +
  24923. +/* bits for struct aufs_rdu.flags */
  24924. +#define AuRdu_CALLED 1
  24925. +#define AuRdu_CONT (1 << 1)
  24926. +#define AuRdu_FULL (1 << 2)
  24927. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  24928. +#define au_fset_rdu(flags, name) \
  24929. + do { (flags) |= AuRdu_##name; } while (0)
  24930. +#define au_fclr_rdu(flags, name) \
  24931. + do { (flags) &= ~AuRdu_##name; } while (0)
  24932. +
  24933. +struct au_rdu_arg {
  24934. + struct dir_context ctx;
  24935. + struct aufs_rdu *rdu;
  24936. + union au_rdu_ent_ul ent;
  24937. + unsigned long end;
  24938. +
  24939. + struct super_block *sb;
  24940. + int err;
  24941. +};
  24942. +
  24943. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  24944. + loff_t offset, u64 h_ino, unsigned int d_type)
  24945. +{
  24946. + int err, len;
  24947. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  24948. + struct aufs_rdu *rdu = arg->rdu;
  24949. + struct au_rdu_ent ent;
  24950. +
  24951. + err = 0;
  24952. + arg->err = 0;
  24953. + au_fset_rdu(rdu->cookie.flags, CALLED);
  24954. + len = au_rdu_len(nlen);
  24955. + if (arg->ent.ul + len < arg->end) {
  24956. + ent.ino = h_ino;
  24957. + ent.bindex = rdu->cookie.bindex;
  24958. + ent.type = d_type;
  24959. + ent.nlen = nlen;
  24960. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  24961. + ent.type = DT_UNKNOWN;
  24962. +
  24963. + /* unnecessary to support mmap_sem since this is a dir */
  24964. + err = -EFAULT;
  24965. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  24966. + goto out;
  24967. + if (copy_to_user(arg->ent.e->name, name, nlen))
  24968. + goto out;
  24969. + /* the terminating NULL */
  24970. + if (__put_user(0, arg->ent.e->name + nlen))
  24971. + goto out;
  24972. + err = 0;
  24973. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  24974. + arg->ent.ul += len;
  24975. + rdu->rent++;
  24976. + } else {
  24977. + err = -EFAULT;
  24978. + au_fset_rdu(rdu->cookie.flags, FULL);
  24979. + rdu->full = 1;
  24980. + rdu->tail = arg->ent;
  24981. + }
  24982. +
  24983. +out:
  24984. + /* AuTraceErr(err); */
  24985. + return err;
  24986. +}
  24987. +
  24988. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  24989. +{
  24990. + int err;
  24991. + loff_t offset;
  24992. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  24993. +
  24994. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  24995. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  24996. + err = offset;
  24997. + if (unlikely(offset != cookie->h_pos))
  24998. + goto out;
  24999. +
  25000. + err = 0;
  25001. + do {
  25002. + arg->err = 0;
  25003. + au_fclr_rdu(cookie->flags, CALLED);
  25004. + /* smp_mb(); */
  25005. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  25006. + if (err >= 0)
  25007. + err = arg->err;
  25008. + } while (!err
  25009. + && au_ftest_rdu(cookie->flags, CALLED)
  25010. + && !au_ftest_rdu(cookie->flags, FULL));
  25011. + cookie->h_pos = h_file->f_pos;
  25012. +
  25013. +out:
  25014. + AuTraceErr(err);
  25015. + return err;
  25016. +}
  25017. +
  25018. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  25019. +{
  25020. + int err;
  25021. + aufs_bindex_t bbot;
  25022. + struct au_rdu_arg arg = {
  25023. + .ctx = {
  25024. + .actor = au_rdu_fill
  25025. + }
  25026. + };
  25027. + struct dentry *dentry;
  25028. + struct inode *inode;
  25029. + struct file *h_file;
  25030. + struct au_rdu_cookie *cookie = &rdu->cookie;
  25031. +
  25032. + err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
  25033. + if (unlikely(err)) {
  25034. + err = -EFAULT;
  25035. + AuTraceErr(err);
  25036. + goto out;
  25037. + }
  25038. + rdu->rent = 0;
  25039. + rdu->tail = rdu->ent;
  25040. + rdu->full = 0;
  25041. + arg.rdu = rdu;
  25042. + arg.ent = rdu->ent;
  25043. + arg.end = arg.ent.ul;
  25044. + arg.end += rdu->sz;
  25045. +
  25046. + err = -ENOTDIR;
  25047. + if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
  25048. + goto out;
  25049. +
  25050. + err = security_file_permission(file, MAY_READ);
  25051. + AuTraceErr(err);
  25052. + if (unlikely(err))
  25053. + goto out;
  25054. +
  25055. + dentry = file->f_path.dentry;
  25056. + inode = d_inode(dentry);
  25057. + inode_lock_shared(inode);
  25058. +
  25059. + arg.sb = inode->i_sb;
  25060. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  25061. + if (unlikely(err))
  25062. + goto out_mtx;
  25063. + err = au_alive_dir(dentry);
  25064. + if (unlikely(err))
  25065. + goto out_si;
  25066. + /* todo: reval? */
  25067. + fi_read_lock(file);
  25068. +
  25069. + err = -EAGAIN;
  25070. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  25071. + && cookie->generation != au_figen(file)))
  25072. + goto out_unlock;
  25073. +
  25074. + err = 0;
  25075. + if (!rdu->blk) {
  25076. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  25077. + if (!rdu->blk)
  25078. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  25079. + }
  25080. + bbot = au_fbtop(file);
  25081. + if (cookie->bindex < bbot)
  25082. + cookie->bindex = bbot;
  25083. + bbot = au_fbbot_dir(file);
  25084. + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
  25085. + for (; !err && cookie->bindex <= bbot;
  25086. + cookie->bindex++, cookie->h_pos = 0) {
  25087. + h_file = au_hf_dir(file, cookie->bindex);
  25088. + if (!h_file)
  25089. + continue;
  25090. +
  25091. + au_fclr_rdu(cookie->flags, FULL);
  25092. + err = au_rdu_do(h_file, &arg);
  25093. + AuTraceErr(err);
  25094. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  25095. + break;
  25096. + }
  25097. + AuDbg("rent %llu\n", rdu->rent);
  25098. +
  25099. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  25100. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  25101. + au_fset_rdu(cookie->flags, CONT);
  25102. + cookie->generation = au_figen(file);
  25103. + }
  25104. +
  25105. + ii_read_lock_child(inode);
  25106. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
  25107. + ii_read_unlock(inode);
  25108. +
  25109. +out_unlock:
  25110. + fi_read_unlock(file);
  25111. +out_si:
  25112. + si_read_unlock(arg.sb);
  25113. +out_mtx:
  25114. + inode_unlock_shared(inode);
  25115. +out:
  25116. + AuTraceErr(err);
  25117. + return err;
  25118. +}
  25119. +
  25120. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  25121. +{
  25122. + int err;
  25123. + ino_t ino;
  25124. + unsigned long long nent;
  25125. + union au_rdu_ent_ul *u;
  25126. + struct au_rdu_ent ent;
  25127. + struct super_block *sb;
  25128. +
  25129. + err = 0;
  25130. + nent = rdu->nent;
  25131. + u = &rdu->ent;
  25132. + sb = file->f_path.dentry->d_sb;
  25133. + si_read_lock(sb, AuLock_FLUSH);
  25134. + while (nent-- > 0) {
  25135. + /* unnecessary to support mmap_sem since this is a dir */
  25136. + err = copy_from_user(&ent, u->e, sizeof(ent));
  25137. + if (!err)
  25138. + err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
  25139. + if (unlikely(err)) {
  25140. + err = -EFAULT;
  25141. + AuTraceErr(err);
  25142. + break;
  25143. + }
  25144. +
  25145. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  25146. + if (!ent.wh)
  25147. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  25148. + else
  25149. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  25150. + &ino);
  25151. + if (unlikely(err)) {
  25152. + AuTraceErr(err);
  25153. + break;
  25154. + }
  25155. +
  25156. + err = __put_user(ino, &u->e->ino);
  25157. + if (unlikely(err)) {
  25158. + err = -EFAULT;
  25159. + AuTraceErr(err);
  25160. + break;
  25161. + }
  25162. + u->ul += au_rdu_len(ent.nlen);
  25163. + }
  25164. + si_read_unlock(sb);
  25165. +
  25166. + return err;
  25167. +}
  25168. +
  25169. +/* ---------------------------------------------------------------------- */
  25170. +
  25171. +static int au_rdu_verify(struct aufs_rdu *rdu)
  25172. +{
  25173. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  25174. + "%llu, b%d, 0x%x, g%u}\n",
  25175. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  25176. + rdu->blk,
  25177. + rdu->rent, rdu->shwh, rdu->full,
  25178. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  25179. + rdu->cookie.generation);
  25180. +
  25181. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  25182. + return 0;
  25183. +
  25184. + AuDbg("%u:%u\n",
  25185. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  25186. + return -EINVAL;
  25187. +}
  25188. +
  25189. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  25190. +{
  25191. + long err, e;
  25192. + struct aufs_rdu rdu;
  25193. + void __user *p = (void __user *)arg;
  25194. +
  25195. + err = copy_from_user(&rdu, p, sizeof(rdu));
  25196. + if (unlikely(err)) {
  25197. + err = -EFAULT;
  25198. + AuTraceErr(err);
  25199. + goto out;
  25200. + }
  25201. + err = au_rdu_verify(&rdu);
  25202. + if (unlikely(err))
  25203. + goto out;
  25204. +
  25205. + switch (cmd) {
  25206. + case AUFS_CTL_RDU:
  25207. + err = au_rdu(file, &rdu);
  25208. + if (unlikely(err))
  25209. + break;
  25210. +
  25211. + e = copy_to_user(p, &rdu, sizeof(rdu));
  25212. + if (unlikely(e)) {
  25213. + err = -EFAULT;
  25214. + AuTraceErr(err);
  25215. + }
  25216. + break;
  25217. + case AUFS_CTL_RDU_INO:
  25218. + err = au_rdu_ino(file, &rdu);
  25219. + break;
  25220. +
  25221. + default:
  25222. + /* err = -ENOTTY; */
  25223. + err = -EINVAL;
  25224. + }
  25225. +
  25226. +out:
  25227. + AuTraceErr(err);
  25228. + return err;
  25229. +}
  25230. +
  25231. +#ifdef CONFIG_COMPAT
  25232. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  25233. +{
  25234. + long err, e;
  25235. + struct aufs_rdu rdu;
  25236. + void __user *p = compat_ptr(arg);
  25237. +
  25238. + /* todo: get_user()? */
  25239. + err = copy_from_user(&rdu, p, sizeof(rdu));
  25240. + if (unlikely(err)) {
  25241. + err = -EFAULT;
  25242. + AuTraceErr(err);
  25243. + goto out;
  25244. + }
  25245. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  25246. + err = au_rdu_verify(&rdu);
  25247. + if (unlikely(err))
  25248. + goto out;
  25249. +
  25250. + switch (cmd) {
  25251. + case AUFS_CTL_RDU:
  25252. + err = au_rdu(file, &rdu);
  25253. + if (unlikely(err))
  25254. + break;
  25255. +
  25256. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  25257. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  25258. + e = copy_to_user(p, &rdu, sizeof(rdu));
  25259. + if (unlikely(e)) {
  25260. + err = -EFAULT;
  25261. + AuTraceErr(err);
  25262. + }
  25263. + break;
  25264. + case AUFS_CTL_RDU_INO:
  25265. + err = au_rdu_ino(file, &rdu);
  25266. + break;
  25267. +
  25268. + default:
  25269. + /* err = -ENOTTY; */
  25270. + err = -EINVAL;
  25271. + }
  25272. +
  25273. +out:
  25274. + AuTraceErr(err);
  25275. + return err;
  25276. +}
  25277. +#endif
  25278. diff -Naur null/fs/aufs/rwsem.h linux-4.7/fs/aufs/rwsem.h
  25279. --- /dev/null
  25280. +++ linux-4.7/fs/aufs/rwsem.h 2016-08-08 12:57:44.607384563 -0400
  25281. @@ -0,0 +1,198 @@
  25282. +/*
  25283. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25284. + *
  25285. + * This program, aufs is free software; you can redistribute it and/or modify
  25286. + * it under the terms of the GNU General Public License as published by
  25287. + * the Free Software Foundation; either version 2 of the License, or
  25288. + * (at your option) any later version.
  25289. + *
  25290. + * This program is distributed in the hope that it will be useful,
  25291. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25292. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25293. + * GNU General Public License for more details.
  25294. + *
  25295. + * You should have received a copy of the GNU General Public License
  25296. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25297. + */
  25298. +
  25299. +/*
  25300. + * simple read-write semaphore wrappers
  25301. + */
  25302. +
  25303. +#ifndef __AUFS_RWSEM_H__
  25304. +#define __AUFS_RWSEM_H__
  25305. +
  25306. +#ifdef __KERNEL__
  25307. +
  25308. +#include "debug.h"
  25309. +
  25310. +struct au_rwsem {
  25311. + struct rw_semaphore rwsem;
  25312. +#ifdef CONFIG_AUFS_DEBUG
  25313. + /* just for debugging, not almighty counter */
  25314. + atomic_t rcnt, wcnt;
  25315. +#endif
  25316. +};
  25317. +
  25318. +#ifdef CONFIG_LOCKDEP
  25319. +#define au_lockdep_set_name(rw) \
  25320. + lockdep_set_class_and_name(&(rw)->rwsem, \
  25321. + /*original key*/(rw)->rwsem.dep_map.key, \
  25322. + /*name*/#rw)
  25323. +#else
  25324. +#define au_lockdep_set_name(rw) do {} while (0)
  25325. +#endif
  25326. +
  25327. +#ifdef CONFIG_AUFS_DEBUG
  25328. +#define AuDbgCntInit(rw) do { \
  25329. + atomic_set(&(rw)->rcnt, 0); \
  25330. + atomic_set(&(rw)->wcnt, 0); \
  25331. + smp_mb(); /* atomic set */ \
  25332. +} while (0)
  25333. +
  25334. +#define AuDbgCnt(rw, cnt) atomic_read(&(rw)->cnt)
  25335. +#define AuDbgCntInc(rw, cnt) atomic_inc(&(rw)->cnt)
  25336. +#define AuDbgCntDec(rw, cnt) WARN_ON(atomic_dec_return(&(rw)->cnt) < 0)
  25337. +#define AuDbgRcntInc(rw) AuDbgCntInc(rw, rcnt)
  25338. +#define AuDbgRcntDec(rw) AuDbgCntDec(rw, rcnt)
  25339. +#define AuDbgWcntInc(rw) AuDbgCntInc(rw, wcnt)
  25340. +#define AuDbgWcntDec(rw) AuDbgCntDec(rw, wcnt)
  25341. +#else
  25342. +#define AuDbgCnt(rw, cnt) 0
  25343. +#define AuDbgCntInit(rw) do {} while (0)
  25344. +#define AuDbgRcntInc(rw) do {} while (0)
  25345. +#define AuDbgRcntDec(rw) do {} while (0)
  25346. +#define AuDbgWcntInc(rw) do {} while (0)
  25347. +#define AuDbgWcntDec(rw) do {} while (0)
  25348. +#endif /* CONFIG_AUFS_DEBUG */
  25349. +
  25350. +/* to debug easier, do not make them inlined functions */
  25351. +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(&(rw)->rwsem))
  25352. +/* rwsem_is_locked() is unusable */
  25353. +#define AuRwMustReadLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0)
  25354. +#define AuRwMustWriteLock(rw) AuDebugOn(AuDbgCnt(rw, wcnt) <= 0)
  25355. +#define AuRwMustAnyLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0 \
  25356. + && AuDbgCnt(rw, wcnt) <= 0)
  25357. +#define AuRwDestroy(rw) AuDebugOn(AuDbgCnt(rw, rcnt) \
  25358. + || AuDbgCnt(rw, wcnt))
  25359. +
  25360. +#define au_rw_init(rw) do { \
  25361. + AuDbgCntInit(rw); \
  25362. + init_rwsem(&(rw)->rwsem); \
  25363. + au_lockdep_set_name(rw); \
  25364. + } while (0)
  25365. +
  25366. +#define au_rw_init_wlock(rw) do { \
  25367. + au_rw_init(rw); \
  25368. + down_write(&(rw)->rwsem); \
  25369. + AuDbgWcntInc(rw); \
  25370. + } while (0)
  25371. +
  25372. +#define au_rw_init_wlock_nested(rw, lsc) do { \
  25373. + au_rw_init(rw); \
  25374. + down_write_nested(&(rw)->rwsem, lsc); \
  25375. + AuDbgWcntInc(rw); \
  25376. + } while (0)
  25377. +
  25378. +static inline void au_rw_read_lock(struct au_rwsem *rw)
  25379. +{
  25380. + down_read(&rw->rwsem);
  25381. + AuDbgRcntInc(rw);
  25382. +}
  25383. +
  25384. +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
  25385. +{
  25386. + down_read_nested(&rw->rwsem, lsc);
  25387. + AuDbgRcntInc(rw);
  25388. +}
  25389. +
  25390. +static inline void au_rw_read_unlock(struct au_rwsem *rw)
  25391. +{
  25392. + AuRwMustReadLock(rw);
  25393. + AuDbgRcntDec(rw);
  25394. + up_read(&rw->rwsem);
  25395. +}
  25396. +
  25397. +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
  25398. +{
  25399. + AuRwMustWriteLock(rw);
  25400. + AuDbgRcntInc(rw);
  25401. + AuDbgWcntDec(rw);
  25402. + downgrade_write(&rw->rwsem);
  25403. +}
  25404. +
  25405. +static inline void au_rw_write_lock(struct au_rwsem *rw)
  25406. +{
  25407. + down_write(&rw->rwsem);
  25408. + AuDbgWcntInc(rw);
  25409. +}
  25410. +
  25411. +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
  25412. + unsigned int lsc)
  25413. +{
  25414. + down_write_nested(&rw->rwsem, lsc);
  25415. + AuDbgWcntInc(rw);
  25416. +}
  25417. +
  25418. +static inline void au_rw_write_unlock(struct au_rwsem *rw)
  25419. +{
  25420. + AuRwMustWriteLock(rw);
  25421. + AuDbgWcntDec(rw);
  25422. + up_write(&rw->rwsem);
  25423. +}
  25424. +
  25425. +/* why is not _nested version defined */
  25426. +static inline int au_rw_read_trylock(struct au_rwsem *rw)
  25427. +{
  25428. + int ret;
  25429. +
  25430. + ret = down_read_trylock(&rw->rwsem);
  25431. + if (ret)
  25432. + AuDbgRcntInc(rw);
  25433. + return ret;
  25434. +}
  25435. +
  25436. +static inline int au_rw_write_trylock(struct au_rwsem *rw)
  25437. +{
  25438. + int ret;
  25439. +
  25440. + ret = down_write_trylock(&rw->rwsem);
  25441. + if (ret)
  25442. + AuDbgWcntInc(rw);
  25443. + return ret;
  25444. +}
  25445. +
  25446. +#undef AuDbgCntDec
  25447. +#undef AuDbgRcntInc
  25448. +#undef AuDbgRcntDec
  25449. +#undef AuDbgWcntDec
  25450. +
  25451. +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  25452. +static inline void prefix##_read_lock(param) \
  25453. +{ au_rw_read_lock(rwsem); } \
  25454. +static inline void prefix##_write_lock(param) \
  25455. +{ au_rw_write_lock(rwsem); } \
  25456. +static inline int prefix##_read_trylock(param) \
  25457. +{ return au_rw_read_trylock(rwsem); } \
  25458. +static inline int prefix##_write_trylock(param) \
  25459. +{ return au_rw_write_trylock(rwsem); }
  25460. +/* why is not _nested version defined */
  25461. +/* static inline void prefix##_read_trylock_nested(param, lsc)
  25462. +{ au_rw_read_trylock_nested(rwsem, lsc)); }
  25463. +static inline void prefix##_write_trylock_nestd(param, lsc)
  25464. +{ au_rw_write_trylock_nested(rwsem, lsc); } */
  25465. +
  25466. +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
  25467. +static inline void prefix##_read_unlock(param) \
  25468. +{ au_rw_read_unlock(rwsem); } \
  25469. +static inline void prefix##_write_unlock(param) \
  25470. +{ au_rw_write_unlock(rwsem); } \
  25471. +static inline void prefix##_downgrade_lock(param) \
  25472. +{ au_rw_dgrade_lock(rwsem); }
  25473. +
  25474. +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
  25475. + AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  25476. + AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
  25477. +
  25478. +#endif /* __KERNEL__ */
  25479. +#endif /* __AUFS_RWSEM_H__ */
  25480. diff -Naur null/fs/aufs/sbinfo.c linux-4.7/fs/aufs/sbinfo.c
  25481. --- /dev/null
  25482. +++ linux-4.7/fs/aufs/sbinfo.c 2016-08-08 12:57:44.607384563 -0400
  25483. @@ -0,0 +1,354 @@
  25484. +/*
  25485. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25486. + *
  25487. + * This program, aufs is free software; you can redistribute it and/or modify
  25488. + * it under the terms of the GNU General Public License as published by
  25489. + * the Free Software Foundation; either version 2 of the License, or
  25490. + * (at your option) any later version.
  25491. + *
  25492. + * This program is distributed in the hope that it will be useful,
  25493. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25494. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25495. + * GNU General Public License for more details.
  25496. + *
  25497. + * You should have received a copy of the GNU General Public License
  25498. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25499. + */
  25500. +
  25501. +/*
  25502. + * superblock private data
  25503. + */
  25504. +
  25505. +#include "aufs.h"
  25506. +
  25507. +/*
  25508. + * they are necessary regardless sysfs is disabled.
  25509. + */
  25510. +void au_si_free(struct kobject *kobj)
  25511. +{
  25512. + int i;
  25513. + struct au_sbinfo *sbinfo;
  25514. + char *locked __maybe_unused; /* debug only */
  25515. +
  25516. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  25517. + for (i = 0; i < AuPlink_NHASH; i++)
  25518. + AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
  25519. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  25520. +
  25521. + AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
  25522. + percpu_counter_destroy(&sbinfo->si_ninodes);
  25523. + AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
  25524. + percpu_counter_destroy(&sbinfo->si_nfiles);
  25525. +
  25526. + au_rw_write_lock(&sbinfo->si_rwsem);
  25527. + au_br_free(sbinfo);
  25528. + au_rw_write_unlock(&sbinfo->si_rwsem);
  25529. +
  25530. + au_delayed_kfree(sbinfo->si_branch);
  25531. + for (i = 0; i < AU_NPIDMAP; i++)
  25532. + if (sbinfo->au_si_pid.pid_bitmap[i])
  25533. + au_delayed_kfree(sbinfo->au_si_pid.pid_bitmap[i]);
  25534. + mutex_destroy(&sbinfo->au_si_pid.pid_mtx);
  25535. + mutex_destroy(&sbinfo->si_xib_mtx);
  25536. + AuRwDestroy(&sbinfo->si_rwsem);
  25537. +
  25538. + au_delayed_kfree(sbinfo);
  25539. +}
  25540. +
  25541. +int au_si_alloc(struct super_block *sb)
  25542. +{
  25543. + int err, i;
  25544. + struct au_sbinfo *sbinfo;
  25545. +
  25546. + err = -ENOMEM;
  25547. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  25548. + if (unlikely(!sbinfo))
  25549. + goto out;
  25550. +
  25551. + /* will be reallocated separately */
  25552. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  25553. + if (unlikely(!sbinfo->si_branch))
  25554. + goto out_sbinfo;
  25555. +
  25556. + err = sysaufs_si_init(sbinfo);
  25557. + if (unlikely(err))
  25558. + goto out_br;
  25559. +
  25560. + au_nwt_init(&sbinfo->si_nowait);
  25561. + au_rw_init_wlock(&sbinfo->si_rwsem);
  25562. + mutex_init(&sbinfo->au_si_pid.pid_mtx);
  25563. +
  25564. + percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
  25565. + percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
  25566. +
  25567. + sbinfo->si_bbot = -1;
  25568. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  25569. +
  25570. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  25571. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  25572. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  25573. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  25574. +
  25575. + au_fhsm_init(sbinfo);
  25576. +
  25577. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  25578. +
  25579. + sbinfo->si_xino_jiffy = jiffies;
  25580. + sbinfo->si_xino_expire
  25581. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  25582. + mutex_init(&sbinfo->si_xib_mtx);
  25583. + sbinfo->si_xino_brid = -1;
  25584. + /* leave si_xib_last_pindex and si_xib_next_bit */
  25585. +
  25586. + au_sphl_init(&sbinfo->si_aopen);
  25587. +
  25588. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  25589. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  25590. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  25591. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  25592. +
  25593. + for (i = 0; i < AuPlink_NHASH; i++)
  25594. + au_sphl_init(sbinfo->si_plink + i);
  25595. + init_waitqueue_head(&sbinfo->si_plink_wq);
  25596. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  25597. +
  25598. + au_sphl_init(&sbinfo->si_files);
  25599. +
  25600. + /* with getattr by default */
  25601. + sbinfo->si_iop_array = aufs_iop;
  25602. +
  25603. + /* leave other members for sysaufs and si_mnt. */
  25604. + sbinfo->si_sb = sb;
  25605. + sb->s_fs_info = sbinfo;
  25606. + si_pid_set(sb);
  25607. + return 0; /* success */
  25608. +
  25609. +out_br:
  25610. + au_delayed_kfree(sbinfo->si_branch);
  25611. +out_sbinfo:
  25612. + au_delayed_kfree(sbinfo);
  25613. +out:
  25614. + return err;
  25615. +}
  25616. +
  25617. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
  25618. +{
  25619. + int err, sz;
  25620. + struct au_branch **brp;
  25621. +
  25622. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  25623. +
  25624. + err = -ENOMEM;
  25625. + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
  25626. + if (unlikely(!sz))
  25627. + sz = sizeof(*brp);
  25628. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
  25629. + if (brp) {
  25630. + sbinfo->si_branch = brp;
  25631. + err = 0;
  25632. + }
  25633. +
  25634. + return err;
  25635. +}
  25636. +
  25637. +/* ---------------------------------------------------------------------- */
  25638. +
  25639. +unsigned int au_sigen_inc(struct super_block *sb)
  25640. +{
  25641. + unsigned int gen;
  25642. + struct inode *inode;
  25643. +
  25644. + SiMustWriteLock(sb);
  25645. +
  25646. + gen = ++au_sbi(sb)->si_generation;
  25647. + au_update_digen(sb->s_root);
  25648. + inode = d_inode(sb->s_root);
  25649. + au_update_iigen(inode, /*half*/0);
  25650. + inode->i_version++;
  25651. + return gen;
  25652. +}
  25653. +
  25654. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  25655. +{
  25656. + aufs_bindex_t br_id;
  25657. + int i;
  25658. + struct au_sbinfo *sbinfo;
  25659. +
  25660. + SiMustWriteLock(sb);
  25661. +
  25662. + sbinfo = au_sbi(sb);
  25663. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  25664. + br_id = ++sbinfo->si_last_br_id;
  25665. + AuDebugOn(br_id < 0);
  25666. + if (br_id && au_br_index(sb, br_id) < 0)
  25667. + return br_id;
  25668. + }
  25669. +
  25670. + return -1;
  25671. +}
  25672. +
  25673. +/* ---------------------------------------------------------------------- */
  25674. +
  25675. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  25676. +int si_read_lock(struct super_block *sb, int flags)
  25677. +{
  25678. + int err;
  25679. +
  25680. + err = 0;
  25681. + if (au_ftest_lock(flags, FLUSH))
  25682. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25683. +
  25684. + si_noflush_read_lock(sb);
  25685. + err = au_plink_maint(sb, flags);
  25686. + if (unlikely(err))
  25687. + si_read_unlock(sb);
  25688. +
  25689. + return err;
  25690. +}
  25691. +
  25692. +int si_write_lock(struct super_block *sb, int flags)
  25693. +{
  25694. + int err;
  25695. +
  25696. + if (au_ftest_lock(flags, FLUSH))
  25697. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25698. +
  25699. + si_noflush_write_lock(sb);
  25700. + err = au_plink_maint(sb, flags);
  25701. + if (unlikely(err))
  25702. + si_write_unlock(sb);
  25703. +
  25704. + return err;
  25705. +}
  25706. +
  25707. +/* dentry and super_block lock. call at entry point */
  25708. +int aufs_read_lock(struct dentry *dentry, int flags)
  25709. +{
  25710. + int err;
  25711. + struct super_block *sb;
  25712. +
  25713. + sb = dentry->d_sb;
  25714. + err = si_read_lock(sb, flags);
  25715. + if (unlikely(err))
  25716. + goto out;
  25717. +
  25718. + if (au_ftest_lock(flags, DW))
  25719. + di_write_lock_child(dentry);
  25720. + else
  25721. + di_read_lock_child(dentry, flags);
  25722. +
  25723. + if (au_ftest_lock(flags, GEN)) {
  25724. + err = au_digen_test(dentry, au_sigen(sb));
  25725. + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
  25726. + AuDebugOn(!err && au_dbrange_test(dentry));
  25727. + else if (!err)
  25728. + err = au_dbrange_test(dentry);
  25729. + if (unlikely(err))
  25730. + aufs_read_unlock(dentry, flags);
  25731. + }
  25732. +
  25733. +out:
  25734. + return err;
  25735. +}
  25736. +
  25737. +void aufs_read_unlock(struct dentry *dentry, int flags)
  25738. +{
  25739. + if (au_ftest_lock(flags, DW))
  25740. + di_write_unlock(dentry);
  25741. + else
  25742. + di_read_unlock(dentry, flags);
  25743. + si_read_unlock(dentry->d_sb);
  25744. +}
  25745. +
  25746. +void aufs_write_lock(struct dentry *dentry)
  25747. +{
  25748. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  25749. + di_write_lock_child(dentry);
  25750. +}
  25751. +
  25752. +void aufs_write_unlock(struct dentry *dentry)
  25753. +{
  25754. + di_write_unlock(dentry);
  25755. + si_write_unlock(dentry->d_sb);
  25756. +}
  25757. +
  25758. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  25759. +{
  25760. + int err;
  25761. + unsigned int sigen;
  25762. + struct super_block *sb;
  25763. +
  25764. + sb = d1->d_sb;
  25765. + err = si_read_lock(sb, flags);
  25766. + if (unlikely(err))
  25767. + goto out;
  25768. +
  25769. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
  25770. +
  25771. + if (au_ftest_lock(flags, GEN)) {
  25772. + sigen = au_sigen(sb);
  25773. + err = au_digen_test(d1, sigen);
  25774. + AuDebugOn(!err && au_dbrange_test(d1));
  25775. + if (!err) {
  25776. + err = au_digen_test(d2, sigen);
  25777. + AuDebugOn(!err && au_dbrange_test(d2));
  25778. + }
  25779. + if (unlikely(err))
  25780. + aufs_read_and_write_unlock2(d1, d2);
  25781. + }
  25782. +
  25783. +out:
  25784. + return err;
  25785. +}
  25786. +
  25787. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  25788. +{
  25789. + di_write_unlock2(d1, d2);
  25790. + si_read_unlock(d1->d_sb);
  25791. +}
  25792. +
  25793. +/* ---------------------------------------------------------------------- */
  25794. +
  25795. +static void si_pid_alloc(struct au_si_pid *au_si_pid, int idx)
  25796. +{
  25797. + unsigned long *p;
  25798. +
  25799. + BUILD_BUG_ON(sizeof(unsigned long) !=
  25800. + sizeof(*au_si_pid->pid_bitmap));
  25801. +
  25802. + mutex_lock(&au_si_pid->pid_mtx);
  25803. + p = au_si_pid->pid_bitmap[idx];
  25804. + while (!p) {
  25805. + /*
  25806. + * bad approach.
  25807. + * but keeping 'si_pid_set()' void is more important.
  25808. + */
  25809. + p = kcalloc(BITS_TO_LONGS(AU_PIDSTEP),
  25810. + sizeof(*au_si_pid->pid_bitmap),
  25811. + GFP_NOFS);
  25812. + if (p)
  25813. + break;
  25814. + cond_resched();
  25815. + }
  25816. + au_si_pid->pid_bitmap[idx] = p;
  25817. + mutex_unlock(&au_si_pid->pid_mtx);
  25818. +}
  25819. +
  25820. +void si_pid_set(struct super_block *sb)
  25821. +{
  25822. + pid_t bit;
  25823. + int idx;
  25824. + unsigned long *bitmap;
  25825. + struct au_si_pid *au_si_pid;
  25826. +
  25827. + si_pid_idx_bit(&idx, &bit);
  25828. + au_si_pid = &au_sbi(sb)->au_si_pid;
  25829. + bitmap = au_si_pid->pid_bitmap[idx];
  25830. + if (!bitmap) {
  25831. + si_pid_alloc(au_si_pid, idx);
  25832. + bitmap = au_si_pid->pid_bitmap[idx];
  25833. + }
  25834. + AuDebugOn(test_bit(bit, bitmap));
  25835. + set_bit(bit, bitmap);
  25836. + /* smp_mb(); */
  25837. +}
  25838. diff -Naur null/fs/aufs/spl.h linux-4.7/fs/aufs/spl.h
  25839. --- /dev/null
  25840. +++ linux-4.7/fs/aufs/spl.h 2016-08-08 12:57:44.607384563 -0400
  25841. @@ -0,0 +1,113 @@
  25842. +/*
  25843. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25844. + *
  25845. + * This program, aufs is free software; you can redistribute it and/or modify
  25846. + * it under the terms of the GNU General Public License as published by
  25847. + * the Free Software Foundation; either version 2 of the License, or
  25848. + * (at your option) any later version.
  25849. + *
  25850. + * This program is distributed in the hope that it will be useful,
  25851. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25852. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25853. + * GNU General Public License for more details.
  25854. + *
  25855. + * You should have received a copy of the GNU General Public License
  25856. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25857. + */
  25858. +
  25859. +/*
  25860. + * simple list protected by a spinlock
  25861. + */
  25862. +
  25863. +#ifndef __AUFS_SPL_H__
  25864. +#define __AUFS_SPL_H__
  25865. +
  25866. +#ifdef __KERNEL__
  25867. +
  25868. +#if 0
  25869. +struct au_splhead {
  25870. + spinlock_t spin;
  25871. + struct list_head head;
  25872. +};
  25873. +
  25874. +static inline void au_spl_init(struct au_splhead *spl)
  25875. +{
  25876. + spin_lock_init(&spl->spin);
  25877. + INIT_LIST_HEAD(&spl->head);
  25878. +}
  25879. +
  25880. +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
  25881. +{
  25882. + spin_lock(&spl->spin);
  25883. + list_add(list, &spl->head);
  25884. + spin_unlock(&spl->spin);
  25885. +}
  25886. +
  25887. +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
  25888. +{
  25889. + spin_lock(&spl->spin);
  25890. + list_del(list);
  25891. + spin_unlock(&spl->spin);
  25892. +}
  25893. +
  25894. +static inline void au_spl_del_rcu(struct list_head *list,
  25895. + struct au_splhead *spl)
  25896. +{
  25897. + spin_lock(&spl->spin);
  25898. + list_del_rcu(list);
  25899. + spin_unlock(&spl->spin);
  25900. +}
  25901. +#endif
  25902. +
  25903. +/* ---------------------------------------------------------------------- */
  25904. +
  25905. +struct au_sphlhead {
  25906. + spinlock_t spin;
  25907. + struct hlist_head head;
  25908. +};
  25909. +
  25910. +static inline void au_sphl_init(struct au_sphlhead *sphl)
  25911. +{
  25912. + spin_lock_init(&sphl->spin);
  25913. + INIT_HLIST_HEAD(&sphl->head);
  25914. +}
  25915. +
  25916. +static inline void au_sphl_add(struct hlist_node *hlist,
  25917. + struct au_sphlhead *sphl)
  25918. +{
  25919. + spin_lock(&sphl->spin);
  25920. + hlist_add_head(hlist, &sphl->head);
  25921. + spin_unlock(&sphl->spin);
  25922. +}
  25923. +
  25924. +static inline void au_sphl_del(struct hlist_node *hlist,
  25925. + struct au_sphlhead *sphl)
  25926. +{
  25927. + spin_lock(&sphl->spin);
  25928. + hlist_del(hlist);
  25929. + spin_unlock(&sphl->spin);
  25930. +}
  25931. +
  25932. +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
  25933. + struct au_sphlhead *sphl)
  25934. +{
  25935. + spin_lock(&sphl->spin);
  25936. + hlist_del_rcu(hlist);
  25937. + spin_unlock(&sphl->spin);
  25938. +}
  25939. +
  25940. +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
  25941. +{
  25942. + unsigned long cnt;
  25943. + struct hlist_node *pos;
  25944. +
  25945. + cnt = 0;
  25946. + spin_lock(&sphl->spin);
  25947. + hlist_for_each(pos, &sphl->head)
  25948. + cnt++;
  25949. + spin_unlock(&sphl->spin);
  25950. + return cnt;
  25951. +}
  25952. +
  25953. +#endif /* __KERNEL__ */
  25954. +#endif /* __AUFS_SPL_H__ */
  25955. diff -Naur null/fs/aufs/super.c linux-4.7/fs/aufs/super.c
  25956. --- /dev/null
  25957. +++ linux-4.7/fs/aufs/super.c 2016-08-08 12:57:44.608384540 -0400
  25958. @@ -0,0 +1,1038 @@
  25959. +/*
  25960. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25961. + *
  25962. + * This program, aufs is free software; you can redistribute it and/or modify
  25963. + * it under the terms of the GNU General Public License as published by
  25964. + * the Free Software Foundation; either version 2 of the License, or
  25965. + * (at your option) any later version.
  25966. + *
  25967. + * This program is distributed in the hope that it will be useful,
  25968. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25969. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25970. + * GNU General Public License for more details.
  25971. + *
  25972. + * You should have received a copy of the GNU General Public License
  25973. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25974. + */
  25975. +
  25976. +/*
  25977. + * mount and super_block operations
  25978. + */
  25979. +
  25980. +#include <linux/mm.h>
  25981. +#include <linux/seq_file.h>
  25982. +#include <linux/statfs.h>
  25983. +#include <linux/vmalloc.h>
  25984. +#include "aufs.h"
  25985. +
  25986. +/*
  25987. + * super_operations
  25988. + */
  25989. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  25990. +{
  25991. + struct au_icntnr *c;
  25992. +
  25993. + c = au_cache_alloc_icntnr();
  25994. + if (c) {
  25995. + au_icntnr_init(c);
  25996. + c->vfs_inode.i_version = 1; /* sigen(sb); */
  25997. + c->iinfo.ii_hinode = NULL;
  25998. + return &c->vfs_inode;
  25999. + }
  26000. + return NULL;
  26001. +}
  26002. +
  26003. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  26004. +{
  26005. + struct inode *inode = container_of(head, struct inode, i_rcu);
  26006. +
  26007. + au_cache_dfree_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  26008. +}
  26009. +
  26010. +static void aufs_destroy_inode(struct inode *inode)
  26011. +{
  26012. + if (!au_is_bad_inode(inode))
  26013. + au_iinfo_fin(inode);
  26014. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  26015. +}
  26016. +
  26017. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  26018. +{
  26019. + struct inode *inode;
  26020. + int err;
  26021. +
  26022. + inode = iget_locked(sb, ino);
  26023. + if (unlikely(!inode)) {
  26024. + inode = ERR_PTR(-ENOMEM);
  26025. + goto out;
  26026. + }
  26027. + if (!(inode->i_state & I_NEW))
  26028. + goto out;
  26029. +
  26030. + err = au_xigen_new(inode);
  26031. + if (!err)
  26032. + err = au_iinfo_init(inode);
  26033. + if (!err)
  26034. + inode->i_version++;
  26035. + else {
  26036. + iget_failed(inode);
  26037. + inode = ERR_PTR(err);
  26038. + }
  26039. +
  26040. +out:
  26041. + /* never return NULL */
  26042. + AuDebugOn(!inode);
  26043. + AuTraceErrPtr(inode);
  26044. + return inode;
  26045. +}
  26046. +
  26047. +/* lock free root dinfo */
  26048. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  26049. +{
  26050. + int err;
  26051. + aufs_bindex_t bindex, bbot;
  26052. + struct path path;
  26053. + struct au_hdentry *hdp;
  26054. + struct au_branch *br;
  26055. + au_br_perm_str_t perm;
  26056. +
  26057. + err = 0;
  26058. + bbot = au_sbbot(sb);
  26059. + bindex = 0;
  26060. + hdp = au_hdentry(au_di(sb->s_root), bindex);
  26061. + for (; !err && bindex <= bbot; bindex++, hdp++) {
  26062. + br = au_sbr(sb, bindex);
  26063. + path.mnt = au_br_mnt(br);
  26064. + path.dentry = hdp->hd_dentry;
  26065. + err = au_seq_path(seq, &path);
  26066. + if (!err) {
  26067. + au_optstr_br_perm(&perm, br->br_perm);
  26068. + seq_printf(seq, "=%s", perm.a);
  26069. + if (bindex != bbot)
  26070. + seq_putc(seq, ':');
  26071. + }
  26072. + }
  26073. + if (unlikely(err || seq_has_overflowed(seq)))
  26074. + err = -E2BIG;
  26075. +
  26076. + return err;
  26077. +}
  26078. +
  26079. +static void au_show_wbr_create(struct seq_file *m, int v,
  26080. + struct au_sbinfo *sbinfo)
  26081. +{
  26082. + const char *pat;
  26083. +
  26084. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  26085. +
  26086. + seq_puts(m, ",create=");
  26087. + pat = au_optstr_wbr_create(v);
  26088. + switch (v) {
  26089. + case AuWbrCreate_TDP:
  26090. + case AuWbrCreate_RR:
  26091. + case AuWbrCreate_MFS:
  26092. + case AuWbrCreate_PMFS:
  26093. + seq_puts(m, pat);
  26094. + break;
  26095. + case AuWbrCreate_MFSV:
  26096. + seq_printf(m, /*pat*/"mfs:%lu",
  26097. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26098. + / MSEC_PER_SEC);
  26099. + break;
  26100. + case AuWbrCreate_PMFSV:
  26101. + seq_printf(m, /*pat*/"pmfs:%lu",
  26102. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26103. + / MSEC_PER_SEC);
  26104. + break;
  26105. + case AuWbrCreate_MFSRR:
  26106. + seq_printf(m, /*pat*/"mfsrr:%llu",
  26107. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  26108. + break;
  26109. + case AuWbrCreate_MFSRRV:
  26110. + seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
  26111. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  26112. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26113. + / MSEC_PER_SEC);
  26114. + break;
  26115. + case AuWbrCreate_PMFSRR:
  26116. + seq_printf(m, /*pat*/"pmfsrr:%llu",
  26117. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  26118. + break;
  26119. + case AuWbrCreate_PMFSRRV:
  26120. + seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
  26121. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  26122. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26123. + / MSEC_PER_SEC);
  26124. + break;
  26125. + }
  26126. +}
  26127. +
  26128. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  26129. +{
  26130. +#ifdef CONFIG_SYSFS
  26131. + return 0;
  26132. +#else
  26133. + int err;
  26134. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  26135. + aufs_bindex_t bindex, brid;
  26136. + struct qstr *name;
  26137. + struct file *f;
  26138. + struct dentry *d, *h_root;
  26139. +
  26140. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  26141. +
  26142. + err = 0;
  26143. + f = au_sbi(sb)->si_xib;
  26144. + if (!f)
  26145. + goto out;
  26146. +
  26147. + /* stop printing the default xino path on the first writable branch */
  26148. + h_root = NULL;
  26149. + brid = au_xino_brid(sb);
  26150. + if (brid >= 0) {
  26151. + bindex = au_br_index(sb, brid);
  26152. + h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
  26153. + }
  26154. + d = f->f_path.dentry;
  26155. + name = &d->d_name;
  26156. + /* safe ->d_parent because the file is unlinked */
  26157. + if (d->d_parent == h_root
  26158. + && name->len == len
  26159. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  26160. + goto out;
  26161. +
  26162. + seq_puts(seq, ",xino=");
  26163. + err = au_xino_path(seq, f);
  26164. +
  26165. +out:
  26166. + return err;
  26167. +#endif
  26168. +}
  26169. +
  26170. +/* seq_file will re-call me in case of too long string */
  26171. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  26172. +{
  26173. + int err;
  26174. + unsigned int mnt_flags, v;
  26175. + struct super_block *sb;
  26176. + struct au_sbinfo *sbinfo;
  26177. +
  26178. +#define AuBool(name, str) do { \
  26179. + v = au_opt_test(mnt_flags, name); \
  26180. + if (v != au_opt_test(AuOpt_Def, name)) \
  26181. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  26182. +} while (0)
  26183. +
  26184. +#define AuStr(name, str) do { \
  26185. + v = mnt_flags & AuOptMask_##name; \
  26186. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  26187. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  26188. +} while (0)
  26189. +
  26190. +#define AuUInt(name, str, val) do { \
  26191. + if (val != AUFS_##name##_DEF) \
  26192. + seq_printf(m, "," #str "=%u", val); \
  26193. +} while (0)
  26194. +
  26195. + sb = dentry->d_sb;
  26196. + if (sb->s_flags & MS_POSIXACL)
  26197. + seq_puts(m, ",acl");
  26198. +
  26199. + /* lock free root dinfo */
  26200. + si_noflush_read_lock(sb);
  26201. + sbinfo = au_sbi(sb);
  26202. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  26203. +
  26204. + mnt_flags = au_mntflags(sb);
  26205. + if (au_opt_test(mnt_flags, XINO)) {
  26206. + err = au_show_xino(m, sb);
  26207. + if (unlikely(err))
  26208. + goto out;
  26209. + } else
  26210. + seq_puts(m, ",noxino");
  26211. +
  26212. + AuBool(TRUNC_XINO, trunc_xino);
  26213. + AuStr(UDBA, udba);
  26214. + AuBool(SHWH, shwh);
  26215. + AuBool(PLINK, plink);
  26216. + AuBool(DIO, dio);
  26217. + AuBool(DIRPERM1, dirperm1);
  26218. +
  26219. + v = sbinfo->si_wbr_create;
  26220. + if (v != AuWbrCreate_Def)
  26221. + au_show_wbr_create(m, v, sbinfo);
  26222. +
  26223. + v = sbinfo->si_wbr_copyup;
  26224. + if (v != AuWbrCopyup_Def)
  26225. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  26226. +
  26227. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  26228. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  26229. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  26230. +
  26231. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  26232. +
  26233. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  26234. + AuUInt(RDCACHE, rdcache, v);
  26235. +
  26236. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  26237. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  26238. +
  26239. + au_fhsm_show(m, sbinfo);
  26240. +
  26241. + AuBool(SUM, sum);
  26242. + /* AuBool(SUM_W, wsum); */
  26243. + AuBool(WARN_PERM, warn_perm);
  26244. + AuBool(VERBOSE, verbose);
  26245. +
  26246. +out:
  26247. + /* be sure to print "br:" last */
  26248. + if (!sysaufs_brs) {
  26249. + seq_puts(m, ",br:");
  26250. + au_show_brs(m, sb);
  26251. + }
  26252. + si_read_unlock(sb);
  26253. + return 0;
  26254. +
  26255. +#undef AuBool
  26256. +#undef AuStr
  26257. +#undef AuUInt
  26258. +}
  26259. +
  26260. +/* ---------------------------------------------------------------------- */
  26261. +
  26262. +/* sum mode which returns the summation for statfs(2) */
  26263. +
  26264. +static u64 au_add_till_max(u64 a, u64 b)
  26265. +{
  26266. + u64 old;
  26267. +
  26268. + old = a;
  26269. + a += b;
  26270. + if (old <= a)
  26271. + return a;
  26272. + return ULLONG_MAX;
  26273. +}
  26274. +
  26275. +static u64 au_mul_till_max(u64 a, long mul)
  26276. +{
  26277. + u64 old;
  26278. +
  26279. + old = a;
  26280. + a *= mul;
  26281. + if (old <= a)
  26282. + return a;
  26283. + return ULLONG_MAX;
  26284. +}
  26285. +
  26286. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  26287. +{
  26288. + int err;
  26289. + long bsize, factor;
  26290. + u64 blocks, bfree, bavail, files, ffree;
  26291. + aufs_bindex_t bbot, bindex, i;
  26292. + unsigned char shared;
  26293. + struct path h_path;
  26294. + struct super_block *h_sb;
  26295. +
  26296. + err = 0;
  26297. + bsize = LONG_MAX;
  26298. + files = 0;
  26299. + ffree = 0;
  26300. + blocks = 0;
  26301. + bfree = 0;
  26302. + bavail = 0;
  26303. + bbot = au_sbbot(sb);
  26304. + for (bindex = 0; bindex <= bbot; bindex++) {
  26305. + h_path.mnt = au_sbr_mnt(sb, bindex);
  26306. + h_sb = h_path.mnt->mnt_sb;
  26307. + shared = 0;
  26308. + for (i = 0; !shared && i < bindex; i++)
  26309. + shared = (au_sbr_sb(sb, i) == h_sb);
  26310. + if (shared)
  26311. + continue;
  26312. +
  26313. + /* sb->s_root for NFS is unreliable */
  26314. + h_path.dentry = h_path.mnt->mnt_root;
  26315. + err = vfs_statfs(&h_path, buf);
  26316. + if (unlikely(err))
  26317. + goto out;
  26318. +
  26319. + if (bsize > buf->f_bsize) {
  26320. + /*
  26321. + * we will reduce bsize, so we have to expand blocks
  26322. + * etc. to match them again
  26323. + */
  26324. + factor = (bsize / buf->f_bsize);
  26325. + blocks = au_mul_till_max(blocks, factor);
  26326. + bfree = au_mul_till_max(bfree, factor);
  26327. + bavail = au_mul_till_max(bavail, factor);
  26328. + bsize = buf->f_bsize;
  26329. + }
  26330. +
  26331. + factor = (buf->f_bsize / bsize);
  26332. + blocks = au_add_till_max(blocks,
  26333. + au_mul_till_max(buf->f_blocks, factor));
  26334. + bfree = au_add_till_max(bfree,
  26335. + au_mul_till_max(buf->f_bfree, factor));
  26336. + bavail = au_add_till_max(bavail,
  26337. + au_mul_till_max(buf->f_bavail, factor));
  26338. + files = au_add_till_max(files, buf->f_files);
  26339. + ffree = au_add_till_max(ffree, buf->f_ffree);
  26340. + }
  26341. +
  26342. + buf->f_bsize = bsize;
  26343. + buf->f_blocks = blocks;
  26344. + buf->f_bfree = bfree;
  26345. + buf->f_bavail = bavail;
  26346. + buf->f_files = files;
  26347. + buf->f_ffree = ffree;
  26348. + buf->f_frsize = 0;
  26349. +
  26350. +out:
  26351. + return err;
  26352. +}
  26353. +
  26354. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  26355. +{
  26356. + int err;
  26357. + struct path h_path;
  26358. + struct super_block *sb;
  26359. +
  26360. + /* lock free root dinfo */
  26361. + sb = dentry->d_sb;
  26362. + si_noflush_read_lock(sb);
  26363. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  26364. + /* sb->s_root for NFS is unreliable */
  26365. + h_path.mnt = au_sbr_mnt(sb, 0);
  26366. + h_path.dentry = h_path.mnt->mnt_root;
  26367. + err = vfs_statfs(&h_path, buf);
  26368. + } else
  26369. + err = au_statfs_sum(sb, buf);
  26370. + si_read_unlock(sb);
  26371. +
  26372. + if (!err) {
  26373. + buf->f_type = AUFS_SUPER_MAGIC;
  26374. + buf->f_namelen = AUFS_MAX_NAMELEN;
  26375. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  26376. + }
  26377. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  26378. +
  26379. + return err;
  26380. +}
  26381. +
  26382. +/* ---------------------------------------------------------------------- */
  26383. +
  26384. +static int aufs_sync_fs(struct super_block *sb, int wait)
  26385. +{
  26386. + int err, e;
  26387. + aufs_bindex_t bbot, bindex;
  26388. + struct au_branch *br;
  26389. + struct super_block *h_sb;
  26390. +
  26391. + err = 0;
  26392. + si_noflush_read_lock(sb);
  26393. + bbot = au_sbbot(sb);
  26394. + for (bindex = 0; bindex <= bbot; bindex++) {
  26395. + br = au_sbr(sb, bindex);
  26396. + if (!au_br_writable(br->br_perm))
  26397. + continue;
  26398. +
  26399. + h_sb = au_sbr_sb(sb, bindex);
  26400. + if (h_sb->s_op->sync_fs) {
  26401. + e = h_sb->s_op->sync_fs(h_sb, wait);
  26402. + if (unlikely(e && !err))
  26403. + err = e;
  26404. + /* go on even if an error happens */
  26405. + }
  26406. + }
  26407. + si_read_unlock(sb);
  26408. +
  26409. + return err;
  26410. +}
  26411. +
  26412. +/* ---------------------------------------------------------------------- */
  26413. +
  26414. +/* final actions when unmounting a file system */
  26415. +static void aufs_put_super(struct super_block *sb)
  26416. +{
  26417. + struct au_sbinfo *sbinfo;
  26418. +
  26419. + sbinfo = au_sbi(sb);
  26420. + if (!sbinfo)
  26421. + return;
  26422. +
  26423. + dbgaufs_si_fin(sbinfo);
  26424. + kobject_put(&sbinfo->si_kobj);
  26425. +}
  26426. +
  26427. +/* ---------------------------------------------------------------------- */
  26428. +
  26429. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  26430. + struct super_block *sb, void *arg)
  26431. +{
  26432. + void *array;
  26433. + unsigned long long n, sz;
  26434. +
  26435. + array = NULL;
  26436. + n = 0;
  26437. + if (!*hint)
  26438. + goto out;
  26439. +
  26440. + if (*hint > ULLONG_MAX / sizeof(array)) {
  26441. + array = ERR_PTR(-EMFILE);
  26442. + pr_err("hint %llu\n", *hint);
  26443. + goto out;
  26444. + }
  26445. +
  26446. + sz = sizeof(array) * *hint;
  26447. + array = kzalloc(sz, GFP_NOFS);
  26448. + if (unlikely(!array))
  26449. + array = vzalloc(sz);
  26450. + if (unlikely(!array)) {
  26451. + array = ERR_PTR(-ENOMEM);
  26452. + goto out;
  26453. + }
  26454. +
  26455. + n = cb(sb, array, *hint, arg);
  26456. + AuDebugOn(n > *hint);
  26457. +
  26458. +out:
  26459. + *hint = n;
  26460. + return array;
  26461. +}
  26462. +
  26463. +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
  26464. + unsigned long long max __maybe_unused,
  26465. + void *arg)
  26466. +{
  26467. + unsigned long long n;
  26468. + struct inode **p, *inode;
  26469. + struct list_head *head;
  26470. +
  26471. + n = 0;
  26472. + p = a;
  26473. + head = arg;
  26474. + spin_lock(&sb->s_inode_list_lock);
  26475. + list_for_each_entry(inode, head, i_sb_list) {
  26476. + if (!au_is_bad_inode(inode)
  26477. + && au_ii(inode)->ii_btop >= 0) {
  26478. + spin_lock(&inode->i_lock);
  26479. + if (atomic_read(&inode->i_count)) {
  26480. + au_igrab(inode);
  26481. + *p++ = inode;
  26482. + n++;
  26483. + AuDebugOn(n > max);
  26484. + }
  26485. + spin_unlock(&inode->i_lock);
  26486. + }
  26487. + }
  26488. + spin_unlock(&sb->s_inode_list_lock);
  26489. +
  26490. + return n;
  26491. +}
  26492. +
  26493. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  26494. +{
  26495. + *max = au_ninodes(sb);
  26496. + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
  26497. +}
  26498. +
  26499. +void au_iarray_free(struct inode **a, unsigned long long max)
  26500. +{
  26501. + unsigned long long ull;
  26502. +
  26503. + for (ull = 0; ull < max; ull++)
  26504. + iput(a[ull]);
  26505. + kvfree(a);
  26506. +}
  26507. +
  26508. +/* ---------------------------------------------------------------------- */
  26509. +
  26510. +/*
  26511. + * refresh dentry and inode at remount time.
  26512. + */
  26513. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  26514. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  26515. + struct dentry *parent)
  26516. +{
  26517. + int err;
  26518. +
  26519. + di_write_lock_child(dentry);
  26520. + di_read_lock_parent(parent, AuLock_IR);
  26521. + err = au_refresh_dentry(dentry, parent);
  26522. + if (!err && dir_flags)
  26523. + au_hn_reset(d_inode(dentry), dir_flags);
  26524. + di_read_unlock(parent, AuLock_IR);
  26525. + di_write_unlock(dentry);
  26526. +
  26527. + return err;
  26528. +}
  26529. +
  26530. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  26531. + struct au_sbinfo *sbinfo,
  26532. + const unsigned int dir_flags, unsigned int do_idop)
  26533. +{
  26534. + int err;
  26535. + struct dentry *parent;
  26536. +
  26537. + err = 0;
  26538. + parent = dget_parent(dentry);
  26539. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  26540. + if (d_really_is_positive(dentry)) {
  26541. + if (!d_is_dir(dentry))
  26542. + err = au_do_refresh(dentry, /*dir_flags*/0,
  26543. + parent);
  26544. + else {
  26545. + err = au_do_refresh(dentry, dir_flags, parent);
  26546. + if (unlikely(err))
  26547. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  26548. + }
  26549. + } else
  26550. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  26551. + AuDbgDentry(dentry);
  26552. + }
  26553. + dput(parent);
  26554. +
  26555. + if (!err) {
  26556. + if (do_idop)
  26557. + au_refresh_dop(dentry, /*force_reval*/0);
  26558. + } else
  26559. + au_refresh_dop(dentry, /*force_reval*/1);
  26560. +
  26561. + AuTraceErr(err);
  26562. + return err;
  26563. +}
  26564. +
  26565. +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
  26566. +{
  26567. + int err, i, j, ndentry, e;
  26568. + unsigned int sigen;
  26569. + struct au_dcsub_pages dpages;
  26570. + struct au_dpage *dpage;
  26571. + struct dentry **dentries, *d;
  26572. + struct au_sbinfo *sbinfo;
  26573. + struct dentry *root = sb->s_root;
  26574. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  26575. +
  26576. + if (do_idop)
  26577. + au_refresh_dop(root, /*force_reval*/0);
  26578. +
  26579. + err = au_dpages_init(&dpages, GFP_NOFS);
  26580. + if (unlikely(err))
  26581. + goto out;
  26582. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  26583. + if (unlikely(err))
  26584. + goto out_dpages;
  26585. +
  26586. + sigen = au_sigen(sb);
  26587. + sbinfo = au_sbi(sb);
  26588. + for (i = 0; i < dpages.ndpage; i++) {
  26589. + dpage = dpages.dpages + i;
  26590. + dentries = dpage->dentries;
  26591. + ndentry = dpage->ndentry;
  26592. + for (j = 0; j < ndentry; j++) {
  26593. + d = dentries[j];
  26594. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
  26595. + do_idop);
  26596. + if (unlikely(e && !err))
  26597. + err = e;
  26598. + /* go on even err */
  26599. + }
  26600. + }
  26601. +
  26602. +out_dpages:
  26603. + au_dpages_free(&dpages);
  26604. +out:
  26605. + return err;
  26606. +}
  26607. +
  26608. +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
  26609. +{
  26610. + int err, e;
  26611. + unsigned int sigen;
  26612. + unsigned long long max, ull;
  26613. + struct inode *inode, **array;
  26614. +
  26615. + array = au_iarray_alloc(sb, &max);
  26616. + err = PTR_ERR(array);
  26617. + if (IS_ERR(array))
  26618. + goto out;
  26619. +
  26620. + err = 0;
  26621. + sigen = au_sigen(sb);
  26622. + for (ull = 0; ull < max; ull++) {
  26623. + inode = array[ull];
  26624. + if (unlikely(!inode))
  26625. + break;
  26626. +
  26627. + e = 0;
  26628. + ii_write_lock_child(inode);
  26629. + if (au_iigen(inode, NULL) != sigen) {
  26630. + e = au_refresh_hinode_self(inode);
  26631. + if (unlikely(e)) {
  26632. + au_refresh_iop(inode, /*force_getattr*/1);
  26633. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  26634. + if (!err)
  26635. + err = e;
  26636. + /* go on even if err */
  26637. + }
  26638. + }
  26639. + if (!e && do_idop)
  26640. + au_refresh_iop(inode, /*force_getattr*/0);
  26641. + ii_write_unlock(inode);
  26642. + }
  26643. +
  26644. + au_iarray_free(array, max);
  26645. +
  26646. +out:
  26647. + return err;
  26648. +}
  26649. +
  26650. +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
  26651. +{
  26652. + int err, e;
  26653. + unsigned int udba;
  26654. + aufs_bindex_t bindex, bbot;
  26655. + struct dentry *root;
  26656. + struct inode *inode;
  26657. + struct au_branch *br;
  26658. + struct au_sbinfo *sbi;
  26659. +
  26660. + au_sigen_inc(sb);
  26661. + sbi = au_sbi(sb);
  26662. + au_fclr_si(sbi, FAILED_REFRESH_DIR);
  26663. +
  26664. + root = sb->s_root;
  26665. + DiMustNoWaiters(root);
  26666. + inode = d_inode(root);
  26667. + IiMustNoWaiters(inode);
  26668. +
  26669. + udba = au_opt_udba(sb);
  26670. + bbot = au_sbbot(sb);
  26671. + for (bindex = 0; bindex <= bbot; bindex++) {
  26672. + br = au_sbr(sb, bindex);
  26673. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  26674. + if (unlikely(err))
  26675. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  26676. + bindex, err);
  26677. + /* go on even if err */
  26678. + }
  26679. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  26680. +
  26681. + if (do_idop) {
  26682. + if (au_ftest_si(sbi, NO_DREVAL)) {
  26683. + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
  26684. + sb->s_d_op = &aufs_dop_noreval;
  26685. + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
  26686. + sbi->si_iop_array = aufs_iop_nogetattr;
  26687. + } else {
  26688. + AuDebugOn(sb->s_d_op == &aufs_dop);
  26689. + sb->s_d_op = &aufs_dop;
  26690. + AuDebugOn(sbi->si_iop_array == aufs_iop);
  26691. + sbi->si_iop_array = aufs_iop;
  26692. + }
  26693. + pr_info("reset to %pf and %pf\n",
  26694. + sb->s_d_op, sbi->si_iop_array);
  26695. + }
  26696. +
  26697. + di_write_unlock(root);
  26698. + err = au_refresh_d(sb, do_idop);
  26699. + e = au_refresh_i(sb, do_idop);
  26700. + if (unlikely(e && !err))
  26701. + err = e;
  26702. + /* aufs_write_lock() calls ..._child() */
  26703. + di_write_lock_child(root);
  26704. +
  26705. + au_cpup_attr_all(inode, /*force*/1);
  26706. +
  26707. + if (unlikely(err))
  26708. + AuIOErr("refresh failed, ignored, %d\n", err);
  26709. +}
  26710. +
  26711. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  26712. +static int cvt_err(int err)
  26713. +{
  26714. + AuTraceErr(err);
  26715. +
  26716. + switch (err) {
  26717. + case -ENOENT:
  26718. + case -ENOTDIR:
  26719. + case -EEXIST:
  26720. + case -EIO:
  26721. + err = -EINVAL;
  26722. + }
  26723. + return err;
  26724. +}
  26725. +
  26726. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  26727. +{
  26728. + int err, do_dx;
  26729. + unsigned int mntflags;
  26730. + struct au_opts opts = {
  26731. + .opt = NULL
  26732. + };
  26733. + struct dentry *root;
  26734. + struct inode *inode;
  26735. + struct au_sbinfo *sbinfo;
  26736. +
  26737. + err = 0;
  26738. + root = sb->s_root;
  26739. + if (!data || !*data) {
  26740. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  26741. + if (!err) {
  26742. + di_write_lock_child(root);
  26743. + err = au_opts_verify(sb, *flags, /*pending*/0);
  26744. + aufs_write_unlock(root);
  26745. + }
  26746. + goto out;
  26747. + }
  26748. +
  26749. + err = -ENOMEM;
  26750. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  26751. + if (unlikely(!opts.opt))
  26752. + goto out;
  26753. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  26754. + opts.flags = AuOpts_REMOUNT;
  26755. + opts.sb_flags = *flags;
  26756. +
  26757. + /* parse it before aufs lock */
  26758. + err = au_opts_parse(sb, data, &opts);
  26759. + if (unlikely(err))
  26760. + goto out_opts;
  26761. +
  26762. + sbinfo = au_sbi(sb);
  26763. + inode = d_inode(root);
  26764. + inode_lock(inode);
  26765. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  26766. + if (unlikely(err))
  26767. + goto out_mtx;
  26768. + di_write_lock_child(root);
  26769. +
  26770. + /* au_opts_remount() may return an error */
  26771. + err = au_opts_remount(sb, &opts);
  26772. + au_opts_free(&opts);
  26773. +
  26774. + if (au_ftest_opts(opts.flags, REFRESH))
  26775. + au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
  26776. +
  26777. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  26778. + mntflags = au_mntflags(sb);
  26779. + do_dx = !!au_opt_test(mntflags, DIO);
  26780. + au_dy_arefresh(do_dx);
  26781. + }
  26782. +
  26783. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  26784. + aufs_write_unlock(root);
  26785. +
  26786. +out_mtx:
  26787. + inode_unlock(inode);
  26788. +out_opts:
  26789. + au_delayed_free_page((unsigned long)opts.opt);
  26790. +out:
  26791. + err = cvt_err(err);
  26792. + AuTraceErr(err);
  26793. + return err;
  26794. +}
  26795. +
  26796. +static const struct super_operations aufs_sop = {
  26797. + .alloc_inode = aufs_alloc_inode,
  26798. + .destroy_inode = aufs_destroy_inode,
  26799. + /* always deleting, no clearing */
  26800. + .drop_inode = generic_delete_inode,
  26801. + .show_options = aufs_show_options,
  26802. + .statfs = aufs_statfs,
  26803. + .put_super = aufs_put_super,
  26804. + .sync_fs = aufs_sync_fs,
  26805. + .remount_fs = aufs_remount_fs
  26806. +};
  26807. +
  26808. +/* ---------------------------------------------------------------------- */
  26809. +
  26810. +static int alloc_root(struct super_block *sb)
  26811. +{
  26812. + int err;
  26813. + struct inode *inode;
  26814. + struct dentry *root;
  26815. +
  26816. + err = -ENOMEM;
  26817. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  26818. + err = PTR_ERR(inode);
  26819. + if (IS_ERR(inode))
  26820. + goto out;
  26821. +
  26822. + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
  26823. + inode->i_fop = &aufs_dir_fop;
  26824. + inode->i_mode = S_IFDIR;
  26825. + set_nlink(inode, 2);
  26826. + unlock_new_inode(inode);
  26827. +
  26828. + root = d_make_root(inode);
  26829. + if (unlikely(!root))
  26830. + goto out;
  26831. + err = PTR_ERR(root);
  26832. + if (IS_ERR(root))
  26833. + goto out;
  26834. +
  26835. + err = au_di_init(root);
  26836. + if (!err) {
  26837. + sb->s_root = root;
  26838. + return 0; /* success */
  26839. + }
  26840. + dput(root);
  26841. +
  26842. +out:
  26843. + return err;
  26844. +}
  26845. +
  26846. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  26847. + int silent __maybe_unused)
  26848. +{
  26849. + int err;
  26850. + struct au_opts opts = {
  26851. + .opt = NULL
  26852. + };
  26853. + struct au_sbinfo *sbinfo;
  26854. + struct dentry *root;
  26855. + struct inode *inode;
  26856. + char *arg = raw_data;
  26857. +
  26858. + if (unlikely(!arg || !*arg)) {
  26859. + err = -EINVAL;
  26860. + pr_err("no arg\n");
  26861. + goto out;
  26862. + }
  26863. +
  26864. + err = -ENOMEM;
  26865. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  26866. + if (unlikely(!opts.opt))
  26867. + goto out;
  26868. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  26869. + opts.sb_flags = sb->s_flags;
  26870. +
  26871. + err = au_si_alloc(sb);
  26872. + if (unlikely(err))
  26873. + goto out_opts;
  26874. + sbinfo = au_sbi(sb);
  26875. +
  26876. + /* all timestamps always follow the ones on the branch */
  26877. + sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
  26878. + sb->s_op = &aufs_sop;
  26879. + sb->s_d_op = &aufs_dop;
  26880. + sb->s_magic = AUFS_SUPER_MAGIC;
  26881. + sb->s_maxbytes = 0;
  26882. + sb->s_stack_depth = 1;
  26883. + au_export_init(sb);
  26884. + /* au_xattr_init(sb); */
  26885. +
  26886. + err = alloc_root(sb);
  26887. + if (unlikely(err)) {
  26888. + si_write_unlock(sb);
  26889. + goto out_info;
  26890. + }
  26891. + root = sb->s_root;
  26892. + inode = d_inode(root);
  26893. +
  26894. + /*
  26895. + * actually we can parse options regardless aufs lock here.
  26896. + * but at remount time, parsing must be done before aufs lock.
  26897. + * so we follow the same rule.
  26898. + */
  26899. + ii_write_lock_parent(inode);
  26900. + aufs_write_unlock(root);
  26901. + err = au_opts_parse(sb, arg, &opts);
  26902. + if (unlikely(err))
  26903. + goto out_root;
  26904. +
  26905. + /* lock vfs_inode first, then aufs. */
  26906. + inode_lock(inode);
  26907. + aufs_write_lock(root);
  26908. + err = au_opts_mount(sb, &opts);
  26909. + au_opts_free(&opts);
  26910. + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
  26911. + sb->s_d_op = &aufs_dop_noreval;
  26912. + pr_info("%pf\n", sb->s_d_op);
  26913. + au_refresh_dop(root, /*force_reval*/0);
  26914. + sbinfo->si_iop_array = aufs_iop_nogetattr;
  26915. + au_refresh_iop(inode, /*force_getattr*/0);
  26916. + }
  26917. + aufs_write_unlock(root);
  26918. + inode_unlock(inode);
  26919. + if (!err)
  26920. + goto out_opts; /* success */
  26921. +
  26922. +out_root:
  26923. + dput(root);
  26924. + sb->s_root = NULL;
  26925. +out_info:
  26926. + dbgaufs_si_fin(sbinfo);
  26927. + kobject_put(&sbinfo->si_kobj);
  26928. + sb->s_fs_info = NULL;
  26929. +out_opts:
  26930. + au_delayed_free_page((unsigned long)opts.opt);
  26931. +out:
  26932. + AuTraceErr(err);
  26933. + err = cvt_err(err);
  26934. + AuTraceErr(err);
  26935. + return err;
  26936. +}
  26937. +
  26938. +/* ---------------------------------------------------------------------- */
  26939. +
  26940. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  26941. + const char *dev_name __maybe_unused,
  26942. + void *raw_data)
  26943. +{
  26944. + struct dentry *root;
  26945. + struct super_block *sb;
  26946. +
  26947. + /* all timestamps always follow the ones on the branch */
  26948. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  26949. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  26950. + if (IS_ERR(root))
  26951. + goto out;
  26952. +
  26953. + sb = root->d_sb;
  26954. + si_write_lock(sb, !AuLock_FLUSH);
  26955. + sysaufs_brs_add(sb, 0);
  26956. + si_write_unlock(sb);
  26957. + au_sbilist_add(sb);
  26958. +
  26959. +out:
  26960. + return root;
  26961. +}
  26962. +
  26963. +static void aufs_kill_sb(struct super_block *sb)
  26964. +{
  26965. + struct au_sbinfo *sbinfo;
  26966. +
  26967. + sbinfo = au_sbi(sb);
  26968. + if (sbinfo) {
  26969. + au_sbilist_del(sb);
  26970. + aufs_write_lock(sb->s_root);
  26971. + au_fhsm_fin(sb);
  26972. + if (sbinfo->si_wbr_create_ops->fin)
  26973. + sbinfo->si_wbr_create_ops->fin(sb);
  26974. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  26975. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  26976. + au_remount_refresh(sb, /*do_idop*/0);
  26977. + }
  26978. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  26979. + au_plink_put(sb, /*verbose*/1);
  26980. + au_xino_clr(sb);
  26981. + sbinfo->si_sb = NULL;
  26982. + aufs_write_unlock(sb->s_root);
  26983. + au_nwt_flush(&sbinfo->si_nowait);
  26984. + }
  26985. + kill_anon_super(sb);
  26986. +}
  26987. +
  26988. +struct file_system_type aufs_fs_type = {
  26989. + .name = AUFS_FSTYPE,
  26990. + /* a race between rename and others */
  26991. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  26992. + .mount = aufs_mount,
  26993. + .kill_sb = aufs_kill_sb,
  26994. + /* no need to __module_get() and module_put(). */
  26995. + .owner = THIS_MODULE,
  26996. +};
  26997. diff -Naur null/fs/aufs/super.h linux-4.7/fs/aufs/super.h
  26998. --- /dev/null
  26999. +++ linux-4.7/fs/aufs/super.h 2016-08-08 12:57:44.608384540 -0400
  27000. @@ -0,0 +1,638 @@
  27001. +/*
  27002. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27003. + *
  27004. + * This program, aufs is free software; you can redistribute it and/or modify
  27005. + * it under the terms of the GNU General Public License as published by
  27006. + * the Free Software Foundation; either version 2 of the License, or
  27007. + * (at your option) any later version.
  27008. + *
  27009. + * This program is distributed in the hope that it will be useful,
  27010. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27011. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27012. + * GNU General Public License for more details.
  27013. + *
  27014. + * You should have received a copy of the GNU General Public License
  27015. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27016. + */
  27017. +
  27018. +/*
  27019. + * super_block operations
  27020. + */
  27021. +
  27022. +#ifndef __AUFS_SUPER_H__
  27023. +#define __AUFS_SUPER_H__
  27024. +
  27025. +#ifdef __KERNEL__
  27026. +
  27027. +#include <linux/fs.h>
  27028. +#include <linux/kobject.h>
  27029. +#include "rwsem.h"
  27030. +#include "spl.h"
  27031. +#include "wkq.h"
  27032. +
  27033. +/* policies to select one among multiple writable branches */
  27034. +struct au_wbr_copyup_operations {
  27035. + int (*copyup)(struct dentry *dentry);
  27036. +};
  27037. +
  27038. +#define AuWbr_DIR 1 /* target is a dir */
  27039. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  27040. +
  27041. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  27042. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  27043. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  27044. +
  27045. +struct au_wbr_create_operations {
  27046. + int (*create)(struct dentry *dentry, unsigned int flags);
  27047. + int (*init)(struct super_block *sb);
  27048. + int (*fin)(struct super_block *sb);
  27049. +};
  27050. +
  27051. +struct au_wbr_mfs {
  27052. + struct mutex mfs_lock; /* protect this structure */
  27053. + unsigned long mfs_jiffy;
  27054. + unsigned long mfs_expire;
  27055. + aufs_bindex_t mfs_bindex;
  27056. +
  27057. + unsigned long long mfsrr_bytes;
  27058. + unsigned long long mfsrr_watermark;
  27059. +};
  27060. +
  27061. +#define AuPlink_NHASH 100
  27062. +static inline int au_plink_hash(ino_t ino)
  27063. +{
  27064. + return ino % AuPlink_NHASH;
  27065. +}
  27066. +
  27067. +/* File-based Hierarchical Storage Management */
  27068. +struct au_fhsm {
  27069. +#ifdef CONFIG_AUFS_FHSM
  27070. + /* allow only one process who can receive the notification */
  27071. + spinlock_t fhsm_spin;
  27072. + pid_t fhsm_pid;
  27073. + wait_queue_head_t fhsm_wqh;
  27074. + atomic_t fhsm_readable;
  27075. +
  27076. + /* these are protected by si_rwsem */
  27077. + unsigned long fhsm_expire;
  27078. + aufs_bindex_t fhsm_bottom;
  27079. +#endif
  27080. +};
  27081. +
  27082. +#define AU_PIDSTEP (int)(BITS_TO_LONGS(PID_MAX_DEFAULT) * BITS_PER_LONG)
  27083. +#define AU_NPIDMAP (int)DIV_ROUND_UP(PID_MAX_LIMIT, AU_PIDSTEP)
  27084. +struct au_si_pid {
  27085. + unsigned long *pid_bitmap[AU_NPIDMAP];
  27086. + struct mutex pid_mtx;
  27087. +};
  27088. +
  27089. +struct au_branch;
  27090. +struct au_sbinfo {
  27091. + /* nowait tasks in the system-wide workqueue */
  27092. + struct au_nowait_tasks si_nowait;
  27093. +
  27094. + /*
  27095. + * tried sb->s_umount, but failed due to the dependecy between i_mutex.
  27096. + * rwsem for au_sbinfo is necessary.
  27097. + */
  27098. + struct au_rwsem si_rwsem;
  27099. +
  27100. + /* prevent recursive locking in deleting inode */
  27101. + struct au_si_pid au_si_pid;
  27102. +
  27103. + /*
  27104. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  27105. + * remount.
  27106. + */
  27107. + struct percpu_counter si_ninodes, si_nfiles;
  27108. +
  27109. + /* branch management */
  27110. + unsigned int si_generation;
  27111. +
  27112. + /* see AuSi_ flags */
  27113. + unsigned char au_si_status;
  27114. +
  27115. + aufs_bindex_t si_bbot;
  27116. +
  27117. + /* dirty trick to keep br_id plus */
  27118. + unsigned int si_last_br_id :
  27119. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  27120. + struct au_branch **si_branch;
  27121. +
  27122. + /* policy to select a writable branch */
  27123. + unsigned char si_wbr_copyup;
  27124. + unsigned char si_wbr_create;
  27125. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  27126. + struct au_wbr_create_operations *si_wbr_create_ops;
  27127. +
  27128. + /* round robin */
  27129. + atomic_t si_wbr_rr_next;
  27130. +
  27131. + /* most free space */
  27132. + struct au_wbr_mfs si_wbr_mfs;
  27133. +
  27134. + /* File-based Hierarchical Storage Management */
  27135. + struct au_fhsm si_fhsm;
  27136. +
  27137. + /* mount flags */
  27138. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  27139. + unsigned int si_mntflags;
  27140. +
  27141. + /* external inode number (bitmap and translation table) */
  27142. + vfs_readf_t si_xread;
  27143. + vfs_writef_t si_xwrite;
  27144. + struct file *si_xib;
  27145. + struct mutex si_xib_mtx; /* protect xib members */
  27146. + unsigned long *si_xib_buf;
  27147. + unsigned long si_xib_last_pindex;
  27148. + int si_xib_next_bit;
  27149. + aufs_bindex_t si_xino_brid;
  27150. + unsigned long si_xino_jiffy;
  27151. + unsigned long si_xino_expire;
  27152. + /* reserved for future use */
  27153. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  27154. +
  27155. +#ifdef CONFIG_AUFS_EXPORT
  27156. + /* i_generation */
  27157. + struct file *si_xigen;
  27158. + atomic_t si_xigen_next;
  27159. +#endif
  27160. +
  27161. + /* dirty trick to suppoer atomic_open */
  27162. + struct au_sphlhead si_aopen;
  27163. +
  27164. + /* vdir parameters */
  27165. + unsigned long si_rdcache; /* max cache time in jiffies */
  27166. + unsigned int si_rdblk; /* deblk size */
  27167. + unsigned int si_rdhash; /* hash size */
  27168. +
  27169. + /*
  27170. + * If the number of whiteouts are larger than si_dirwh, leave all of
  27171. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  27172. + * future fsck.aufs or kernel thread will remove them later.
  27173. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  27174. + */
  27175. + unsigned int si_dirwh;
  27176. +
  27177. + /* pseudo_link list */
  27178. + struct au_sphlhead si_plink[AuPlink_NHASH];
  27179. + wait_queue_head_t si_plink_wq;
  27180. + spinlock_t si_plink_maint_lock;
  27181. + pid_t si_plink_maint_pid;
  27182. +
  27183. + /* file list */
  27184. + struct au_sphlhead si_files;
  27185. +
  27186. + /* with/without getattr, brother of sb->s_d_op */
  27187. + struct inode_operations *si_iop_array;
  27188. +
  27189. + /*
  27190. + * sysfs and lifetime management.
  27191. + * this is not a small structure and it may be a waste of memory in case
  27192. + * of sysfs is disabled, particulary when many aufs-es are mounted.
  27193. + * but using sysfs is majority.
  27194. + */
  27195. + struct kobject si_kobj;
  27196. +#ifdef CONFIG_DEBUG_FS
  27197. + struct dentry *si_dbgaufs;
  27198. + struct dentry *si_dbgaufs_plink;
  27199. + struct dentry *si_dbgaufs_xib;
  27200. +#ifdef CONFIG_AUFS_EXPORT
  27201. + struct dentry *si_dbgaufs_xigen;
  27202. +#endif
  27203. +#endif
  27204. +
  27205. +#ifdef CONFIG_AUFS_SBILIST
  27206. + struct hlist_node si_list;
  27207. +#endif
  27208. +
  27209. + /* dirty, necessary for unmounting, sysfs and sysrq */
  27210. + struct super_block *si_sb;
  27211. +};
  27212. +
  27213. +/* sbinfo status flags */
  27214. +/*
  27215. + * set true when refresh_dirs() failed at remount time.
  27216. + * then try refreshing dirs at access time again.
  27217. + * if it is false, refreshing dirs at access time is unnecesary
  27218. + */
  27219. +#define AuSi_FAILED_REFRESH_DIR 1
  27220. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  27221. +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
  27222. +
  27223. +#ifndef CONFIG_AUFS_FHSM
  27224. +#undef AuSi_FHSM
  27225. +#define AuSi_FHSM 0
  27226. +#endif
  27227. +
  27228. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  27229. + unsigned int flag)
  27230. +{
  27231. + AuRwMustAnyLock(&sbi->si_rwsem);
  27232. + return sbi->au_si_status & flag;
  27233. +}
  27234. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  27235. +#define au_fset_si(sbinfo, name) do { \
  27236. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  27237. + (sbinfo)->au_si_status |= AuSi_##name; \
  27238. +} while (0)
  27239. +#define au_fclr_si(sbinfo, name) do { \
  27240. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  27241. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  27242. +} while (0)
  27243. +
  27244. +/* ---------------------------------------------------------------------- */
  27245. +
  27246. +/* policy to select one among writable branches */
  27247. +#define AuWbrCopyup(sbinfo, ...) \
  27248. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  27249. +#define AuWbrCreate(sbinfo, ...) \
  27250. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  27251. +
  27252. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  27253. +#define AuLock_DW 1 /* write-lock dentry */
  27254. +#define AuLock_IR (1 << 1) /* read-lock inode */
  27255. +#define AuLock_IW (1 << 2) /* write-lock inode */
  27256. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  27257. +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
  27258. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  27259. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  27260. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  27261. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  27262. +#define au_fset_lock(flags, name) \
  27263. + do { (flags) |= AuLock_##name; } while (0)
  27264. +#define au_fclr_lock(flags, name) \
  27265. + do { (flags) &= ~AuLock_##name; } while (0)
  27266. +
  27267. +/* ---------------------------------------------------------------------- */
  27268. +
  27269. +/* super.c */
  27270. +extern struct file_system_type aufs_fs_type;
  27271. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  27272. +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
  27273. + unsigned long long max, void *arg);
  27274. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  27275. + struct super_block *sb, void *arg);
  27276. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  27277. +void au_iarray_free(struct inode **a, unsigned long long max);
  27278. +
  27279. +/* sbinfo.c */
  27280. +void au_si_free(struct kobject *kobj);
  27281. +int au_si_alloc(struct super_block *sb);
  27282. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
  27283. +
  27284. +unsigned int au_sigen_inc(struct super_block *sb);
  27285. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  27286. +
  27287. +int si_read_lock(struct super_block *sb, int flags);
  27288. +int si_write_lock(struct super_block *sb, int flags);
  27289. +int aufs_read_lock(struct dentry *dentry, int flags);
  27290. +void aufs_read_unlock(struct dentry *dentry, int flags);
  27291. +void aufs_write_lock(struct dentry *dentry);
  27292. +void aufs_write_unlock(struct dentry *dentry);
  27293. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  27294. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  27295. +
  27296. +/* wbr_policy.c */
  27297. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  27298. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  27299. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  27300. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  27301. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
  27302. +
  27303. +/* mvdown.c */
  27304. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  27305. +
  27306. +#ifdef CONFIG_AUFS_FHSM
  27307. +/* fhsm.c */
  27308. +
  27309. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  27310. +{
  27311. + pid_t pid;
  27312. +
  27313. + spin_lock(&fhsm->fhsm_spin);
  27314. + pid = fhsm->fhsm_pid;
  27315. + spin_unlock(&fhsm->fhsm_spin);
  27316. +
  27317. + return pid;
  27318. +}
  27319. +
  27320. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  27321. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  27322. +int au_fhsm_fd(struct super_block *sb, int oflags);
  27323. +int au_fhsm_br_alloc(struct au_branch *br);
  27324. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  27325. +void au_fhsm_fin(struct super_block *sb);
  27326. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  27327. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  27328. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  27329. +#else
  27330. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  27331. + int force)
  27332. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  27333. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  27334. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  27335. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  27336. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  27337. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  27338. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  27339. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  27340. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  27341. +#endif
  27342. +
  27343. +/* ---------------------------------------------------------------------- */
  27344. +
  27345. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  27346. +{
  27347. + return sb->s_fs_info;
  27348. +}
  27349. +
  27350. +/* ---------------------------------------------------------------------- */
  27351. +
  27352. +#ifdef CONFIG_AUFS_EXPORT
  27353. +int au_test_nfsd(void);
  27354. +void au_export_init(struct super_block *sb);
  27355. +void au_xigen_inc(struct inode *inode);
  27356. +int au_xigen_new(struct inode *inode);
  27357. +int au_xigen_set(struct super_block *sb, struct file *base);
  27358. +void au_xigen_clr(struct super_block *sb);
  27359. +
  27360. +static inline int au_busy_or_stale(void)
  27361. +{
  27362. + if (!au_test_nfsd())
  27363. + return -EBUSY;
  27364. + return -ESTALE;
  27365. +}
  27366. +#else
  27367. +AuStubInt0(au_test_nfsd, void)
  27368. +AuStubVoid(au_export_init, struct super_block *sb)
  27369. +AuStubVoid(au_xigen_inc, struct inode *inode)
  27370. +AuStubInt0(au_xigen_new, struct inode *inode)
  27371. +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
  27372. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  27373. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  27374. +#endif /* CONFIG_AUFS_EXPORT */
  27375. +
  27376. +/* ---------------------------------------------------------------------- */
  27377. +
  27378. +#ifdef CONFIG_AUFS_SBILIST
  27379. +/* module.c */
  27380. +extern struct au_sphlhead au_sbilist;
  27381. +
  27382. +static inline void au_sbilist_init(void)
  27383. +{
  27384. + au_sphl_init(&au_sbilist);
  27385. +}
  27386. +
  27387. +static inline void au_sbilist_add(struct super_block *sb)
  27388. +{
  27389. + au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
  27390. +}
  27391. +
  27392. +static inline void au_sbilist_del(struct super_block *sb)
  27393. +{
  27394. + au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
  27395. +}
  27396. +
  27397. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  27398. +static inline void au_sbilist_lock(void)
  27399. +{
  27400. + spin_lock(&au_sbilist.spin);
  27401. +}
  27402. +
  27403. +static inline void au_sbilist_unlock(void)
  27404. +{
  27405. + spin_unlock(&au_sbilist.spin);
  27406. +}
  27407. +#define AuGFP_SBILIST GFP_ATOMIC
  27408. +#else
  27409. +AuStubVoid(au_sbilist_lock, void)
  27410. +AuStubVoid(au_sbilist_unlock, void)
  27411. +#define AuGFP_SBILIST GFP_NOFS
  27412. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  27413. +#else
  27414. +AuStubVoid(au_sbilist_init, void)
  27415. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  27416. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  27417. +AuStubVoid(au_sbilist_lock, void)
  27418. +AuStubVoid(au_sbilist_unlock, void)
  27419. +#define AuGFP_SBILIST GFP_NOFS
  27420. +#endif
  27421. +
  27422. +/* ---------------------------------------------------------------------- */
  27423. +
  27424. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  27425. +{
  27426. + /*
  27427. + * This function is a dynamic '__init' function actually,
  27428. + * so the tiny check for si_rwsem is unnecessary.
  27429. + */
  27430. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  27431. +#ifdef CONFIG_DEBUG_FS
  27432. + sbinfo->si_dbgaufs = NULL;
  27433. + sbinfo->si_dbgaufs_plink = NULL;
  27434. + sbinfo->si_dbgaufs_xib = NULL;
  27435. +#ifdef CONFIG_AUFS_EXPORT
  27436. + sbinfo->si_dbgaufs_xigen = NULL;
  27437. +#endif
  27438. +#endif
  27439. +}
  27440. +
  27441. +/* ---------------------------------------------------------------------- */
  27442. +
  27443. +static inline void si_pid_idx_bit(int *idx, pid_t *bit)
  27444. +{
  27445. + /* the origin of pid is 1, but the bitmap's is 0 */
  27446. + *bit = current->pid - 1;
  27447. + *idx = *bit / AU_PIDSTEP;
  27448. + *bit %= AU_PIDSTEP;
  27449. +}
  27450. +
  27451. +static inline int si_pid_test(struct super_block *sb)
  27452. +{
  27453. + pid_t bit;
  27454. + int idx;
  27455. + unsigned long *bitmap;
  27456. +
  27457. + si_pid_idx_bit(&idx, &bit);
  27458. + bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
  27459. + if (bitmap)
  27460. + return test_bit(bit, bitmap);
  27461. + return 0;
  27462. +}
  27463. +
  27464. +static inline void si_pid_clr(struct super_block *sb)
  27465. +{
  27466. + pid_t bit;
  27467. + int idx;
  27468. + unsigned long *bitmap;
  27469. +
  27470. + si_pid_idx_bit(&idx, &bit);
  27471. + bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
  27472. + BUG_ON(!bitmap);
  27473. + AuDebugOn(!test_bit(bit, bitmap));
  27474. + clear_bit(bit, bitmap);
  27475. + /* smp_mb(); */
  27476. +}
  27477. +
  27478. +void si_pid_set(struct super_block *sb);
  27479. +
  27480. +/* ---------------------------------------------------------------------- */
  27481. +
  27482. +/* lock superblock. mainly for entry point functions */
  27483. +/*
  27484. + * __si_read_lock, __si_write_lock,
  27485. + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
  27486. + */
  27487. +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
  27488. +
  27489. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  27490. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  27491. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  27492. +
  27493. +static inline void si_noflush_read_lock(struct super_block *sb)
  27494. +{
  27495. + __si_read_lock(sb);
  27496. + si_pid_set(sb);
  27497. +}
  27498. +
  27499. +static inline int si_noflush_read_trylock(struct super_block *sb)
  27500. +{
  27501. + int locked;
  27502. +
  27503. + locked = __si_read_trylock(sb);
  27504. + if (locked)
  27505. + si_pid_set(sb);
  27506. + return locked;
  27507. +}
  27508. +
  27509. +static inline void si_noflush_write_lock(struct super_block *sb)
  27510. +{
  27511. + __si_write_lock(sb);
  27512. + si_pid_set(sb);
  27513. +}
  27514. +
  27515. +static inline int si_noflush_write_trylock(struct super_block *sb)
  27516. +{
  27517. + int locked;
  27518. +
  27519. + locked = __si_write_trylock(sb);
  27520. + if (locked)
  27521. + si_pid_set(sb);
  27522. + return locked;
  27523. +}
  27524. +
  27525. +#if 0 /* reserved */
  27526. +static inline int si_read_trylock(struct super_block *sb, int flags)
  27527. +{
  27528. + if (au_ftest_lock(flags, FLUSH))
  27529. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  27530. + return si_noflush_read_trylock(sb);
  27531. +}
  27532. +#endif
  27533. +
  27534. +static inline void si_read_unlock(struct super_block *sb)
  27535. +{
  27536. + si_pid_clr(sb);
  27537. + __si_read_unlock(sb);
  27538. +}
  27539. +
  27540. +#if 0 /* reserved */
  27541. +static inline int si_write_trylock(struct super_block *sb, int flags)
  27542. +{
  27543. + if (au_ftest_lock(flags, FLUSH))
  27544. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  27545. + return si_noflush_write_trylock(sb);
  27546. +}
  27547. +#endif
  27548. +
  27549. +static inline void si_write_unlock(struct super_block *sb)
  27550. +{
  27551. + si_pid_clr(sb);
  27552. + __si_write_unlock(sb);
  27553. +}
  27554. +
  27555. +#if 0 /* reserved */
  27556. +static inline void si_downgrade_lock(struct super_block *sb)
  27557. +{
  27558. + __si_downgrade_lock(sb);
  27559. +}
  27560. +#endif
  27561. +
  27562. +/* ---------------------------------------------------------------------- */
  27563. +
  27564. +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
  27565. +{
  27566. + SiMustAnyLock(sb);
  27567. + return au_sbi(sb)->si_bbot;
  27568. +}
  27569. +
  27570. +static inline unsigned int au_mntflags(struct super_block *sb)
  27571. +{
  27572. + SiMustAnyLock(sb);
  27573. + return au_sbi(sb)->si_mntflags;
  27574. +}
  27575. +
  27576. +static inline unsigned int au_sigen(struct super_block *sb)
  27577. +{
  27578. + SiMustAnyLock(sb);
  27579. + return au_sbi(sb)->si_generation;
  27580. +}
  27581. +
  27582. +static inline unsigned long long au_ninodes(struct super_block *sb)
  27583. +{
  27584. + s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
  27585. +
  27586. + BUG_ON(n < 0);
  27587. + return n;
  27588. +}
  27589. +
  27590. +static inline void au_ninodes_inc(struct super_block *sb)
  27591. +{
  27592. + percpu_counter_inc(&au_sbi(sb)->si_ninodes);
  27593. +}
  27594. +
  27595. +static inline void au_ninodes_dec(struct super_block *sb)
  27596. +{
  27597. + percpu_counter_dec(&au_sbi(sb)->si_ninodes);
  27598. +}
  27599. +
  27600. +static inline unsigned long long au_nfiles(struct super_block *sb)
  27601. +{
  27602. + s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
  27603. +
  27604. + BUG_ON(n < 0);
  27605. + return n;
  27606. +}
  27607. +
  27608. +static inline void au_nfiles_inc(struct super_block *sb)
  27609. +{
  27610. + percpu_counter_inc(&au_sbi(sb)->si_nfiles);
  27611. +}
  27612. +
  27613. +static inline void au_nfiles_dec(struct super_block *sb)
  27614. +{
  27615. + percpu_counter_dec(&au_sbi(sb)->si_nfiles);
  27616. +}
  27617. +
  27618. +static inline struct au_branch *au_sbr(struct super_block *sb,
  27619. + aufs_bindex_t bindex)
  27620. +{
  27621. + SiMustAnyLock(sb);
  27622. + return au_sbi(sb)->si_branch[0 + bindex];
  27623. +}
  27624. +
  27625. +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
  27626. +{
  27627. + SiMustWriteLock(sb);
  27628. + au_sbi(sb)->si_xino_brid = brid;
  27629. +}
  27630. +
  27631. +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
  27632. +{
  27633. + SiMustAnyLock(sb);
  27634. + return au_sbi(sb)->si_xino_brid;
  27635. +}
  27636. +
  27637. +#endif /* __KERNEL__ */
  27638. +#endif /* __AUFS_SUPER_H__ */
  27639. diff -Naur null/fs/aufs/sysaufs.c linux-4.7/fs/aufs/sysaufs.c
  27640. --- /dev/null
  27641. +++ linux-4.7/fs/aufs/sysaufs.c 2016-08-08 12:57:44.608384540 -0400
  27642. @@ -0,0 +1,104 @@
  27643. +/*
  27644. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27645. + *
  27646. + * This program, aufs is free software; you can redistribute it and/or modify
  27647. + * it under the terms of the GNU General Public License as published by
  27648. + * the Free Software Foundation; either version 2 of the License, or
  27649. + * (at your option) any later version.
  27650. + *
  27651. + * This program is distributed in the hope that it will be useful,
  27652. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27653. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27654. + * GNU General Public License for more details.
  27655. + *
  27656. + * You should have received a copy of the GNU General Public License
  27657. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27658. + */
  27659. +
  27660. +/*
  27661. + * sysfs interface and lifetime management
  27662. + * they are necessary regardless sysfs is disabled.
  27663. + */
  27664. +
  27665. +#include <linux/random.h>
  27666. +#include "aufs.h"
  27667. +
  27668. +unsigned long sysaufs_si_mask;
  27669. +struct kset *sysaufs_kset;
  27670. +
  27671. +#define AuSiAttr(_name) { \
  27672. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  27673. + .show = sysaufs_si_##_name, \
  27674. +}
  27675. +
  27676. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  27677. +struct attribute *sysaufs_si_attrs[] = {
  27678. + &sysaufs_si_attr_xi_path.attr,
  27679. + NULL,
  27680. +};
  27681. +
  27682. +static const struct sysfs_ops au_sbi_ops = {
  27683. + .show = sysaufs_si_show
  27684. +};
  27685. +
  27686. +static struct kobj_type au_sbi_ktype = {
  27687. + .release = au_si_free,
  27688. + .sysfs_ops = &au_sbi_ops,
  27689. + .default_attrs = sysaufs_si_attrs
  27690. +};
  27691. +
  27692. +/* ---------------------------------------------------------------------- */
  27693. +
  27694. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  27695. +{
  27696. + int err;
  27697. +
  27698. + sbinfo->si_kobj.kset = sysaufs_kset;
  27699. + /* cf. sysaufs_name() */
  27700. + err = kobject_init_and_add
  27701. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  27702. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  27703. +
  27704. + dbgaufs_si_null(sbinfo);
  27705. + if (!err) {
  27706. + err = dbgaufs_si_init(sbinfo);
  27707. + if (unlikely(err))
  27708. + kobject_put(&sbinfo->si_kobj);
  27709. + }
  27710. + return err;
  27711. +}
  27712. +
  27713. +void sysaufs_fin(void)
  27714. +{
  27715. + dbgaufs_fin();
  27716. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  27717. + kset_unregister(sysaufs_kset);
  27718. +}
  27719. +
  27720. +int __init sysaufs_init(void)
  27721. +{
  27722. + int err;
  27723. +
  27724. + do {
  27725. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  27726. + } while (!sysaufs_si_mask);
  27727. +
  27728. + err = -EINVAL;
  27729. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  27730. + if (unlikely(!sysaufs_kset))
  27731. + goto out;
  27732. + err = PTR_ERR(sysaufs_kset);
  27733. + if (IS_ERR(sysaufs_kset))
  27734. + goto out;
  27735. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  27736. + if (unlikely(err)) {
  27737. + kset_unregister(sysaufs_kset);
  27738. + goto out;
  27739. + }
  27740. +
  27741. + err = dbgaufs_init();
  27742. + if (unlikely(err))
  27743. + sysaufs_fin();
  27744. +out:
  27745. + return err;
  27746. +}
  27747. diff -Naur null/fs/aufs/sysaufs.h linux-4.7/fs/aufs/sysaufs.h
  27748. --- /dev/null
  27749. +++ linux-4.7/fs/aufs/sysaufs.h 2016-08-08 12:57:44.608384540 -0400
  27750. @@ -0,0 +1,101 @@
  27751. +/*
  27752. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27753. + *
  27754. + * This program, aufs is free software; you can redistribute it and/or modify
  27755. + * it under the terms of the GNU General Public License as published by
  27756. + * the Free Software Foundation; either version 2 of the License, or
  27757. + * (at your option) any later version.
  27758. + *
  27759. + * This program is distributed in the hope that it will be useful,
  27760. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27761. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27762. + * GNU General Public License for more details.
  27763. + *
  27764. + * You should have received a copy of the GNU General Public License
  27765. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27766. + */
  27767. +
  27768. +/*
  27769. + * sysfs interface and mount lifetime management
  27770. + */
  27771. +
  27772. +#ifndef __SYSAUFS_H__
  27773. +#define __SYSAUFS_H__
  27774. +
  27775. +#ifdef __KERNEL__
  27776. +
  27777. +#include <linux/sysfs.h>
  27778. +#include "module.h"
  27779. +
  27780. +struct super_block;
  27781. +struct au_sbinfo;
  27782. +
  27783. +struct sysaufs_si_attr {
  27784. + struct attribute attr;
  27785. + int (*show)(struct seq_file *seq, struct super_block *sb);
  27786. +};
  27787. +
  27788. +/* ---------------------------------------------------------------------- */
  27789. +
  27790. +/* sysaufs.c */
  27791. +extern unsigned long sysaufs_si_mask;
  27792. +extern struct kset *sysaufs_kset;
  27793. +extern struct attribute *sysaufs_si_attrs[];
  27794. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  27795. +int __init sysaufs_init(void);
  27796. +void sysaufs_fin(void);
  27797. +
  27798. +/* ---------------------------------------------------------------------- */
  27799. +
  27800. +/* some people doesn't like to show a pointer in kernel */
  27801. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  27802. +{
  27803. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  27804. +}
  27805. +
  27806. +#define SysaufsSiNamePrefix "si_"
  27807. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  27808. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  27809. +{
  27810. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  27811. + sysaufs_si_id(sbinfo));
  27812. +}
  27813. +
  27814. +struct au_branch;
  27815. +#ifdef CONFIG_SYSFS
  27816. +/* sysfs.c */
  27817. +extern struct attribute_group *sysaufs_attr_group;
  27818. +
  27819. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  27820. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  27821. + char *buf);
  27822. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  27823. +#ifdef CONFIG_COMPAT
  27824. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  27825. +#endif
  27826. +
  27827. +void sysaufs_br_init(struct au_branch *br);
  27828. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  27829. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  27830. +
  27831. +#define sysaufs_brs_init() do {} while (0)
  27832. +
  27833. +#else
  27834. +#define sysaufs_attr_group NULL
  27835. +
  27836. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  27837. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  27838. + struct attribute *attr, char *buf)
  27839. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  27840. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  27841. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  27842. +
  27843. +static inline void sysaufs_brs_init(void)
  27844. +{
  27845. + sysaufs_brs = 0;
  27846. +}
  27847. +
  27848. +#endif /* CONFIG_SYSFS */
  27849. +
  27850. +#endif /* __KERNEL__ */
  27851. +#endif /* __SYSAUFS_H__ */
  27852. diff -Naur null/fs/aufs/sysfs.c linux-4.7/fs/aufs/sysfs.c
  27853. --- /dev/null
  27854. +++ linux-4.7/fs/aufs/sysfs.c 2016-08-08 12:57:44.608384540 -0400
  27855. @@ -0,0 +1,376 @@
  27856. +/*
  27857. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27858. + *
  27859. + * This program, aufs is free software; you can redistribute it and/or modify
  27860. + * it under the terms of the GNU General Public License as published by
  27861. + * the Free Software Foundation; either version 2 of the License, or
  27862. + * (at your option) any later version.
  27863. + *
  27864. + * This program is distributed in the hope that it will be useful,
  27865. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27866. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27867. + * GNU General Public License for more details.
  27868. + *
  27869. + * You should have received a copy of the GNU General Public License
  27870. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27871. + */
  27872. +
  27873. +/*
  27874. + * sysfs interface
  27875. + */
  27876. +
  27877. +#include <linux/compat.h>
  27878. +#include <linux/seq_file.h>
  27879. +#include "aufs.h"
  27880. +
  27881. +#ifdef CONFIG_AUFS_FS_MODULE
  27882. +/* this entry violates the "one line per file" policy of sysfs */
  27883. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  27884. + char *buf)
  27885. +{
  27886. + ssize_t err;
  27887. + static char *conf =
  27888. +/* this file is generated at compiling */
  27889. +#include "conf.str"
  27890. + ;
  27891. +
  27892. + err = snprintf(buf, PAGE_SIZE, conf);
  27893. + if (unlikely(err >= PAGE_SIZE))
  27894. + err = -EFBIG;
  27895. + return err;
  27896. +}
  27897. +
  27898. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  27899. +#endif
  27900. +
  27901. +static struct attribute *au_attr[] = {
  27902. +#ifdef CONFIG_AUFS_FS_MODULE
  27903. + &au_config_attr.attr,
  27904. +#endif
  27905. + NULL, /* need to NULL terminate the list of attributes */
  27906. +};
  27907. +
  27908. +static struct attribute_group sysaufs_attr_group_body = {
  27909. + .attrs = au_attr
  27910. +};
  27911. +
  27912. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  27913. +
  27914. +/* ---------------------------------------------------------------------- */
  27915. +
  27916. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  27917. +{
  27918. + int err;
  27919. +
  27920. + SiMustAnyLock(sb);
  27921. +
  27922. + err = 0;
  27923. + if (au_opt_test(au_mntflags(sb), XINO)) {
  27924. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  27925. + seq_putc(seq, '\n');
  27926. + }
  27927. + return err;
  27928. +}
  27929. +
  27930. +/*
  27931. + * the lifetime of branch is independent from the entry under sysfs.
  27932. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  27933. + * unlinked.
  27934. + */
  27935. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  27936. + aufs_bindex_t bindex, int idx)
  27937. +{
  27938. + int err;
  27939. + struct path path;
  27940. + struct dentry *root;
  27941. + struct au_branch *br;
  27942. + au_br_perm_str_t perm;
  27943. +
  27944. + AuDbg("b%d\n", bindex);
  27945. +
  27946. + err = 0;
  27947. + root = sb->s_root;
  27948. + di_read_lock_parent(root, !AuLock_IR);
  27949. + br = au_sbr(sb, bindex);
  27950. +
  27951. + switch (idx) {
  27952. + case AuBrSysfs_BR:
  27953. + path.mnt = au_br_mnt(br);
  27954. + path.dentry = au_h_dptr(root, bindex);
  27955. + err = au_seq_path(seq, &path);
  27956. + if (!err) {
  27957. + au_optstr_br_perm(&perm, br->br_perm);
  27958. + seq_printf(seq, "=%s\n", perm.a);
  27959. + }
  27960. + break;
  27961. + case AuBrSysfs_BRID:
  27962. + seq_printf(seq, "%d\n", br->br_id);
  27963. + break;
  27964. + }
  27965. + di_read_unlock(root, !AuLock_IR);
  27966. + if (unlikely(err || seq_has_overflowed(seq)))
  27967. + err = -E2BIG;
  27968. +
  27969. + return err;
  27970. +}
  27971. +
  27972. +/* ---------------------------------------------------------------------- */
  27973. +
  27974. +static struct seq_file *au_seq(char *p, ssize_t len)
  27975. +{
  27976. + struct seq_file *seq;
  27977. +
  27978. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  27979. + if (seq) {
  27980. + /* mutex_init(&seq.lock); */
  27981. + seq->buf = p;
  27982. + seq->size = len;
  27983. + return seq; /* success */
  27984. + }
  27985. +
  27986. + seq = ERR_PTR(-ENOMEM);
  27987. + return seq;
  27988. +}
  27989. +
  27990. +#define SysaufsBr_PREFIX "br"
  27991. +#define SysaufsBrid_PREFIX "brid"
  27992. +
  27993. +/* todo: file size may exceed PAGE_SIZE */
  27994. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  27995. + char *buf)
  27996. +{
  27997. + ssize_t err;
  27998. + int idx;
  27999. + long l;
  28000. + aufs_bindex_t bbot;
  28001. + struct au_sbinfo *sbinfo;
  28002. + struct super_block *sb;
  28003. + struct seq_file *seq;
  28004. + char *name;
  28005. + struct attribute **cattr;
  28006. +
  28007. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  28008. + sb = sbinfo->si_sb;
  28009. +
  28010. + /*
  28011. + * prevent a race condition between sysfs and aufs.
  28012. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  28013. + * prohibits maintaining the sysfs entries.
  28014. + * hew we acquire read lock after sysfs_get_active_two().
  28015. + * on the other hand, the remount process may maintain the sysfs/aufs
  28016. + * entries after acquiring write lock.
  28017. + * it can cause a deadlock.
  28018. + * simply we gave up processing read here.
  28019. + */
  28020. + err = -EBUSY;
  28021. + if (unlikely(!si_noflush_read_trylock(sb)))
  28022. + goto out;
  28023. +
  28024. + seq = au_seq(buf, PAGE_SIZE);
  28025. + err = PTR_ERR(seq);
  28026. + if (IS_ERR(seq))
  28027. + goto out_unlock;
  28028. +
  28029. + name = (void *)attr->name;
  28030. + cattr = sysaufs_si_attrs;
  28031. + while (*cattr) {
  28032. + if (!strcmp(name, (*cattr)->name)) {
  28033. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  28034. + ->show(seq, sb);
  28035. + goto out_seq;
  28036. + }
  28037. + cattr++;
  28038. + }
  28039. +
  28040. + if (!strncmp(name, SysaufsBrid_PREFIX,
  28041. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  28042. + idx = AuBrSysfs_BRID;
  28043. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  28044. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  28045. + sizeof(SysaufsBr_PREFIX) - 1)) {
  28046. + idx = AuBrSysfs_BR;
  28047. + name += sizeof(SysaufsBr_PREFIX) - 1;
  28048. + } else
  28049. + BUG();
  28050. +
  28051. + err = kstrtol(name, 10, &l);
  28052. + if (!err) {
  28053. + bbot = au_sbbot(sb);
  28054. + if (l <= bbot)
  28055. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  28056. + else
  28057. + err = -ENOENT;
  28058. + }
  28059. +
  28060. +out_seq:
  28061. + if (!err) {
  28062. + err = seq->count;
  28063. + /* sysfs limit */
  28064. + if (unlikely(err == PAGE_SIZE))
  28065. + err = -EFBIG;
  28066. + }
  28067. + au_delayed_kfree(seq);
  28068. +out_unlock:
  28069. + si_read_unlock(sb);
  28070. +out:
  28071. + return err;
  28072. +}
  28073. +
  28074. +/* ---------------------------------------------------------------------- */
  28075. +
  28076. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  28077. +{
  28078. + int err;
  28079. + int16_t brid;
  28080. + aufs_bindex_t bindex, bbot;
  28081. + size_t sz;
  28082. + char *buf;
  28083. + struct seq_file *seq;
  28084. + struct au_branch *br;
  28085. +
  28086. + si_read_lock(sb, AuLock_FLUSH);
  28087. + bbot = au_sbbot(sb);
  28088. + err = bbot + 1;
  28089. + if (!arg)
  28090. + goto out;
  28091. +
  28092. + err = -ENOMEM;
  28093. + buf = (void *)__get_free_page(GFP_NOFS);
  28094. + if (unlikely(!buf))
  28095. + goto out;
  28096. +
  28097. + seq = au_seq(buf, PAGE_SIZE);
  28098. + err = PTR_ERR(seq);
  28099. + if (IS_ERR(seq))
  28100. + goto out_buf;
  28101. +
  28102. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  28103. + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
  28104. + err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
  28105. + if (unlikely(err))
  28106. + break;
  28107. +
  28108. + br = au_sbr(sb, bindex);
  28109. + brid = br->br_id;
  28110. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  28111. + err = __put_user(brid, &arg->id);
  28112. + if (unlikely(err))
  28113. + break;
  28114. +
  28115. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  28116. + err = __put_user(br->br_perm, &arg->perm);
  28117. + if (unlikely(err))
  28118. + break;
  28119. +
  28120. + err = au_seq_path(seq, &br->br_path);
  28121. + if (unlikely(err))
  28122. + break;
  28123. + seq_putc(seq, '\0');
  28124. + if (!seq_has_overflowed(seq)) {
  28125. + err = copy_to_user(arg->path, seq->buf, seq->count);
  28126. + seq->count = 0;
  28127. + if (unlikely(err))
  28128. + break;
  28129. + } else {
  28130. + err = -E2BIG;
  28131. + goto out_seq;
  28132. + }
  28133. + }
  28134. + if (unlikely(err))
  28135. + err = -EFAULT;
  28136. +
  28137. +out_seq:
  28138. + au_delayed_kfree(seq);
  28139. +out_buf:
  28140. + au_delayed_free_page((unsigned long)buf);
  28141. +out:
  28142. + si_read_unlock(sb);
  28143. + return err;
  28144. +}
  28145. +
  28146. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  28147. +{
  28148. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  28149. +}
  28150. +
  28151. +#ifdef CONFIG_COMPAT
  28152. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  28153. +{
  28154. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  28155. +}
  28156. +#endif
  28157. +
  28158. +/* ---------------------------------------------------------------------- */
  28159. +
  28160. +void sysaufs_br_init(struct au_branch *br)
  28161. +{
  28162. + int i;
  28163. + struct au_brsysfs *br_sysfs;
  28164. + struct attribute *attr;
  28165. +
  28166. + br_sysfs = br->br_sysfs;
  28167. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  28168. + attr = &br_sysfs->attr;
  28169. + sysfs_attr_init(attr);
  28170. + attr->name = br_sysfs->name;
  28171. + attr->mode = S_IRUGO;
  28172. + br_sysfs++;
  28173. + }
  28174. +}
  28175. +
  28176. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  28177. +{
  28178. + struct au_branch *br;
  28179. + struct kobject *kobj;
  28180. + struct au_brsysfs *br_sysfs;
  28181. + int i;
  28182. + aufs_bindex_t bbot;
  28183. +
  28184. + dbgaufs_brs_del(sb, bindex);
  28185. +
  28186. + if (!sysaufs_brs)
  28187. + return;
  28188. +
  28189. + kobj = &au_sbi(sb)->si_kobj;
  28190. + bbot = au_sbbot(sb);
  28191. + for (; bindex <= bbot; bindex++) {
  28192. + br = au_sbr(sb, bindex);
  28193. + br_sysfs = br->br_sysfs;
  28194. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  28195. + sysfs_remove_file(kobj, &br_sysfs->attr);
  28196. + br_sysfs++;
  28197. + }
  28198. + }
  28199. +}
  28200. +
  28201. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  28202. +{
  28203. + int err, i;
  28204. + aufs_bindex_t bbot;
  28205. + struct kobject *kobj;
  28206. + struct au_branch *br;
  28207. + struct au_brsysfs *br_sysfs;
  28208. +
  28209. + dbgaufs_brs_add(sb, bindex);
  28210. +
  28211. + if (!sysaufs_brs)
  28212. + return;
  28213. +
  28214. + kobj = &au_sbi(sb)->si_kobj;
  28215. + bbot = au_sbbot(sb);
  28216. + for (; bindex <= bbot; bindex++) {
  28217. + br = au_sbr(sb, bindex);
  28218. + br_sysfs = br->br_sysfs;
  28219. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  28220. + SysaufsBr_PREFIX "%d", bindex);
  28221. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  28222. + SysaufsBrid_PREFIX "%d", bindex);
  28223. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  28224. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  28225. + if (unlikely(err))
  28226. + pr_warn("failed %s under sysfs(%d)\n",
  28227. + br_sysfs->name, err);
  28228. + br_sysfs++;
  28229. + }
  28230. + }
  28231. +}
  28232. diff -Naur null/fs/aufs/sysrq.c linux-4.7/fs/aufs/sysrq.c
  28233. --- /dev/null
  28234. +++ linux-4.7/fs/aufs/sysrq.c 2016-08-08 12:57:44.609384518 -0400
  28235. @@ -0,0 +1,157 @@
  28236. +/*
  28237. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  28238. + *
  28239. + * This program, aufs is free software; you can redistribute it and/or modify
  28240. + * it under the terms of the GNU General Public License as published by
  28241. + * the Free Software Foundation; either version 2 of the License, or
  28242. + * (at your option) any later version.
  28243. + *
  28244. + * This program is distributed in the hope that it will be useful,
  28245. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28246. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28247. + * GNU General Public License for more details.
  28248. + *
  28249. + * You should have received a copy of the GNU General Public License
  28250. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28251. + */
  28252. +
  28253. +/*
  28254. + * magic sysrq hanlder
  28255. + */
  28256. +
  28257. +/* #include <linux/sysrq.h> */
  28258. +#include <linux/writeback.h>
  28259. +#include "aufs.h"
  28260. +
  28261. +/* ---------------------------------------------------------------------- */
  28262. +
  28263. +static void sysrq_sb(struct super_block *sb)
  28264. +{
  28265. + char *plevel;
  28266. + struct au_sbinfo *sbinfo;
  28267. + struct file *file;
  28268. + struct au_sphlhead *files;
  28269. + struct au_finfo *finfo;
  28270. +
  28271. + plevel = au_plevel;
  28272. + au_plevel = KERN_WARNING;
  28273. +
  28274. + /* since we define pr_fmt, call printk directly */
  28275. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  28276. +
  28277. + sbinfo = au_sbi(sb);
  28278. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  28279. + pr("superblock\n");
  28280. + au_dpri_sb(sb);
  28281. +
  28282. +#if 0
  28283. + pr("root dentry\n");
  28284. + au_dpri_dentry(sb->s_root);
  28285. + pr("root inode\n");
  28286. + au_dpri_inode(d_inode(sb->s_root));
  28287. +#endif
  28288. +
  28289. +#if 0
  28290. + do {
  28291. + int err, i, j, ndentry;
  28292. + struct au_dcsub_pages dpages;
  28293. + struct au_dpage *dpage;
  28294. +
  28295. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  28296. + if (unlikely(err))
  28297. + break;
  28298. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  28299. + if (!err)
  28300. + for (i = 0; i < dpages.ndpage; i++) {
  28301. + dpage = dpages.dpages + i;
  28302. + ndentry = dpage->ndentry;
  28303. + for (j = 0; j < ndentry; j++)
  28304. + au_dpri_dentry(dpage->dentries[j]);
  28305. + }
  28306. + au_dpages_free(&dpages);
  28307. + } while (0);
  28308. +#endif
  28309. +
  28310. +#if 1
  28311. + {
  28312. + struct inode *i;
  28313. +
  28314. + pr("isolated inode\n");
  28315. + spin_lock(&sb->s_inode_list_lock);
  28316. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  28317. + spin_lock(&i->i_lock);
  28318. + if (1 || hlist_empty(&i->i_dentry))
  28319. + au_dpri_inode(i);
  28320. + spin_unlock(&i->i_lock);
  28321. + }
  28322. + spin_unlock(&sb->s_inode_list_lock);
  28323. + }
  28324. +#endif
  28325. + pr("files\n");
  28326. + files = &au_sbi(sb)->si_files;
  28327. + spin_lock(&files->spin);
  28328. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  28329. + umode_t mode;
  28330. +
  28331. + file = finfo->fi_file;
  28332. + mode = file_inode(file)->i_mode;
  28333. + if (!special_file(mode))
  28334. + au_dpri_file(file);
  28335. + }
  28336. + spin_unlock(&files->spin);
  28337. + pr("done\n");
  28338. +
  28339. +#undef pr
  28340. + au_plevel = plevel;
  28341. +}
  28342. +
  28343. +/* ---------------------------------------------------------------------- */
  28344. +
  28345. +/* module parameter */
  28346. +static char *aufs_sysrq_key = "a";
  28347. +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
  28348. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  28349. +
  28350. +static void au_sysrq(int key __maybe_unused)
  28351. +{
  28352. + struct au_sbinfo *sbinfo;
  28353. +
  28354. + lockdep_off();
  28355. + au_sbilist_lock();
  28356. + hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  28357. + sysrq_sb(sbinfo->si_sb);
  28358. + au_sbilist_unlock();
  28359. + lockdep_on();
  28360. +}
  28361. +
  28362. +static struct sysrq_key_op au_sysrq_op = {
  28363. + .handler = au_sysrq,
  28364. + .help_msg = "Aufs",
  28365. + .action_msg = "Aufs",
  28366. + .enable_mask = SYSRQ_ENABLE_DUMP
  28367. +};
  28368. +
  28369. +/* ---------------------------------------------------------------------- */
  28370. +
  28371. +int __init au_sysrq_init(void)
  28372. +{
  28373. + int err;
  28374. + char key;
  28375. +
  28376. + err = -1;
  28377. + key = *aufs_sysrq_key;
  28378. + if ('a' <= key && key <= 'z')
  28379. + err = register_sysrq_key(key, &au_sysrq_op);
  28380. + if (unlikely(err))
  28381. + pr_err("err %d, sysrq=%c\n", err, key);
  28382. + return err;
  28383. +}
  28384. +
  28385. +void au_sysrq_fin(void)
  28386. +{
  28387. + int err;
  28388. +
  28389. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  28390. + if (unlikely(err))
  28391. + pr_err("err %d (ignored)\n", err);
  28392. +}
  28393. diff -Naur null/fs/aufs/vdir.c linux-4.7/fs/aufs/vdir.c
  28394. --- /dev/null
  28395. +++ linux-4.7/fs/aufs/vdir.c 2016-08-08 12:57:44.609384518 -0400
  28396. @@ -0,0 +1,899 @@
  28397. +/*
  28398. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  28399. + *
  28400. + * This program, aufs is free software; you can redistribute it and/or modify
  28401. + * it under the terms of the GNU General Public License as published by
  28402. + * the Free Software Foundation; either version 2 of the License, or
  28403. + * (at your option) any later version.
  28404. + *
  28405. + * This program is distributed in the hope that it will be useful,
  28406. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28407. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28408. + * GNU General Public License for more details.
  28409. + *
  28410. + * You should have received a copy of the GNU General Public License
  28411. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28412. + */
  28413. +
  28414. +/*
  28415. + * virtual or vertical directory
  28416. + */
  28417. +
  28418. +#include "aufs.h"
  28419. +
  28420. +static unsigned int calc_size(int nlen)
  28421. +{
  28422. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  28423. +}
  28424. +
  28425. +static int set_deblk_end(union au_vdir_deblk_p *p,
  28426. + union au_vdir_deblk_p *deblk_end)
  28427. +{
  28428. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  28429. + p->de->de_str.len = 0;
  28430. + /* smp_mb(); */
  28431. + return 0;
  28432. + }
  28433. + return -1; /* error */
  28434. +}
  28435. +
  28436. +/* returns true or false */
  28437. +static int is_deblk_end(union au_vdir_deblk_p *p,
  28438. + union au_vdir_deblk_p *deblk_end)
  28439. +{
  28440. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  28441. + return !p->de->de_str.len;
  28442. + return 1;
  28443. +}
  28444. +
  28445. +static unsigned char *last_deblk(struct au_vdir *vdir)
  28446. +{
  28447. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  28448. +}
  28449. +
  28450. +/* ---------------------------------------------------------------------- */
  28451. +
  28452. +/* estimate the appropriate size for name hash table */
  28453. +unsigned int au_rdhash_est(loff_t sz)
  28454. +{
  28455. + unsigned int n;
  28456. +
  28457. + n = UINT_MAX;
  28458. + sz >>= 10;
  28459. + if (sz < n)
  28460. + n = sz;
  28461. + if (sz < AUFS_RDHASH_DEF)
  28462. + n = AUFS_RDHASH_DEF;
  28463. + /* pr_info("n %u\n", n); */
  28464. + return n;
  28465. +}
  28466. +
  28467. +/*
  28468. + * the allocated memory has to be freed by
  28469. + * au_nhash_wh_free() or au_nhash_de_free().
  28470. + */
  28471. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  28472. +{
  28473. + struct hlist_head *head;
  28474. + unsigned int u;
  28475. + size_t sz;
  28476. +
  28477. + sz = sizeof(*nhash->nh_head) * num_hash;
  28478. + head = kmalloc(sz, gfp);
  28479. + if (head) {
  28480. + nhash->nh_num = num_hash;
  28481. + nhash->nh_head = head;
  28482. + for (u = 0; u < num_hash; u++)
  28483. + INIT_HLIST_HEAD(head++);
  28484. + return 0; /* success */
  28485. + }
  28486. +
  28487. + return -ENOMEM;
  28488. +}
  28489. +
  28490. +static void nhash_count(struct hlist_head *head)
  28491. +{
  28492. +#if 0
  28493. + unsigned long n;
  28494. + struct hlist_node *pos;
  28495. +
  28496. + n = 0;
  28497. + hlist_for_each(pos, head)
  28498. + n++;
  28499. + pr_info("%lu\n", n);
  28500. +#endif
  28501. +}
  28502. +
  28503. +static void au_nhash_wh_do_free(struct hlist_head *head)
  28504. +{
  28505. + struct au_vdir_wh *pos;
  28506. + struct hlist_node *node;
  28507. +
  28508. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  28509. + au_delayed_kfree(pos);
  28510. +}
  28511. +
  28512. +static void au_nhash_de_do_free(struct hlist_head *head)
  28513. +{
  28514. + struct au_vdir_dehstr *pos;
  28515. + struct hlist_node *node;
  28516. +
  28517. + hlist_for_each_entry_safe(pos, node, head, hash)
  28518. + au_cache_dfree_vdir_dehstr(pos);
  28519. +}
  28520. +
  28521. +static void au_nhash_do_free(struct au_nhash *nhash,
  28522. + void (*free)(struct hlist_head *head))
  28523. +{
  28524. + unsigned int n;
  28525. + struct hlist_head *head;
  28526. +
  28527. + n = nhash->nh_num;
  28528. + if (!n)
  28529. + return;
  28530. +
  28531. + head = nhash->nh_head;
  28532. + while (n-- > 0) {
  28533. + nhash_count(head);
  28534. + free(head++);
  28535. + }
  28536. + au_delayed_kfree(nhash->nh_head);
  28537. +}
  28538. +
  28539. +void au_nhash_wh_free(struct au_nhash *whlist)
  28540. +{
  28541. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  28542. +}
  28543. +
  28544. +static void au_nhash_de_free(struct au_nhash *delist)
  28545. +{
  28546. + au_nhash_do_free(delist, au_nhash_de_do_free);
  28547. +}
  28548. +
  28549. +/* ---------------------------------------------------------------------- */
  28550. +
  28551. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  28552. + int limit)
  28553. +{
  28554. + int num;
  28555. + unsigned int u, n;
  28556. + struct hlist_head *head;
  28557. + struct au_vdir_wh *pos;
  28558. +
  28559. + num = 0;
  28560. + n = whlist->nh_num;
  28561. + head = whlist->nh_head;
  28562. + for (u = 0; u < n; u++, head++)
  28563. + hlist_for_each_entry(pos, head, wh_hash)
  28564. + if (pos->wh_bindex == btgt && ++num > limit)
  28565. + return 1;
  28566. + return 0;
  28567. +}
  28568. +
  28569. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  28570. + unsigned char *name,
  28571. + unsigned int len)
  28572. +{
  28573. + unsigned int v;
  28574. + /* const unsigned int magic_bit = 12; */
  28575. +
  28576. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  28577. +
  28578. + v = 0;
  28579. + if (len > 8)
  28580. + len = 8;
  28581. + while (len--)
  28582. + v += *name++;
  28583. + /* v = hash_long(v, magic_bit); */
  28584. + v %= nhash->nh_num;
  28585. + return nhash->nh_head + v;
  28586. +}
  28587. +
  28588. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  28589. + int nlen)
  28590. +{
  28591. + return str->len == nlen && !memcmp(str->name, name, nlen);
  28592. +}
  28593. +
  28594. +/* returns found or not */
  28595. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  28596. +{
  28597. + struct hlist_head *head;
  28598. + struct au_vdir_wh *pos;
  28599. + struct au_vdir_destr *str;
  28600. +
  28601. + head = au_name_hash(whlist, name, nlen);
  28602. + hlist_for_each_entry(pos, head, wh_hash) {
  28603. + str = &pos->wh_str;
  28604. + AuDbg("%.*s\n", str->len, str->name);
  28605. + if (au_nhash_test_name(str, name, nlen))
  28606. + return 1;
  28607. + }
  28608. + return 0;
  28609. +}
  28610. +
  28611. +/* returns found(true) or not */
  28612. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  28613. +{
  28614. + struct hlist_head *head;
  28615. + struct au_vdir_dehstr *pos;
  28616. + struct au_vdir_destr *str;
  28617. +
  28618. + head = au_name_hash(delist, name, nlen);
  28619. + hlist_for_each_entry(pos, head, hash) {
  28620. + str = pos->str;
  28621. + AuDbg("%.*s\n", str->len, str->name);
  28622. + if (au_nhash_test_name(str, name, nlen))
  28623. + return 1;
  28624. + }
  28625. + return 0;
  28626. +}
  28627. +
  28628. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  28629. + unsigned char d_type)
  28630. +{
  28631. +#ifdef CONFIG_AUFS_SHWH
  28632. + wh->wh_ino = ino;
  28633. + wh->wh_type = d_type;
  28634. +#endif
  28635. +}
  28636. +
  28637. +/* ---------------------------------------------------------------------- */
  28638. +
  28639. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  28640. + unsigned int d_type, aufs_bindex_t bindex,
  28641. + unsigned char shwh)
  28642. +{
  28643. + int err;
  28644. + struct au_vdir_destr *str;
  28645. + struct au_vdir_wh *wh;
  28646. +
  28647. + AuDbg("%.*s\n", nlen, name);
  28648. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  28649. +
  28650. + err = -ENOMEM;
  28651. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  28652. + if (unlikely(!wh))
  28653. + goto out;
  28654. +
  28655. + err = 0;
  28656. + wh->wh_bindex = bindex;
  28657. + if (shwh)
  28658. + au_shwh_init_wh(wh, ino, d_type);
  28659. + str = &wh->wh_str;
  28660. + str->len = nlen;
  28661. + memcpy(str->name, name, nlen);
  28662. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  28663. + /* smp_mb(); */
  28664. +
  28665. +out:
  28666. + return err;
  28667. +}
  28668. +
  28669. +static int append_deblk(struct au_vdir *vdir)
  28670. +{
  28671. + int err;
  28672. + unsigned long ul;
  28673. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  28674. + union au_vdir_deblk_p p, deblk_end;
  28675. + unsigned char **o;
  28676. +
  28677. + err = -ENOMEM;
  28678. + o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  28679. + GFP_NOFS);
  28680. + if (unlikely(!o))
  28681. + goto out;
  28682. +
  28683. + vdir->vd_deblk = o;
  28684. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  28685. + if (p.deblk) {
  28686. + ul = vdir->vd_nblk++;
  28687. + vdir->vd_deblk[ul] = p.deblk;
  28688. + vdir->vd_last.ul = ul;
  28689. + vdir->vd_last.p.deblk = p.deblk;
  28690. + deblk_end.deblk = p.deblk + deblk_sz;
  28691. + err = set_deblk_end(&p, &deblk_end);
  28692. + }
  28693. +
  28694. +out:
  28695. + return err;
  28696. +}
  28697. +
  28698. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  28699. + unsigned int d_type, struct au_nhash *delist)
  28700. +{
  28701. + int err;
  28702. + unsigned int sz;
  28703. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  28704. + union au_vdir_deblk_p p, *room, deblk_end;
  28705. + struct au_vdir_dehstr *dehstr;
  28706. +
  28707. + p.deblk = last_deblk(vdir);
  28708. + deblk_end.deblk = p.deblk + deblk_sz;
  28709. + room = &vdir->vd_last.p;
  28710. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  28711. + || !is_deblk_end(room, &deblk_end));
  28712. +
  28713. + sz = calc_size(nlen);
  28714. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  28715. + err = append_deblk(vdir);
  28716. + if (unlikely(err))
  28717. + goto out;
  28718. +
  28719. + p.deblk = last_deblk(vdir);
  28720. + deblk_end.deblk = p.deblk + deblk_sz;
  28721. + /* smp_mb(); */
  28722. + AuDebugOn(room->deblk != p.deblk);
  28723. + }
  28724. +
  28725. + err = -ENOMEM;
  28726. + dehstr = au_cache_alloc_vdir_dehstr();
  28727. + if (unlikely(!dehstr))
  28728. + goto out;
  28729. +
  28730. + dehstr->str = &room->de->de_str;
  28731. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  28732. + room->de->de_ino = ino;
  28733. + room->de->de_type = d_type;
  28734. + room->de->de_str.len = nlen;
  28735. + memcpy(room->de->de_str.name, name, nlen);
  28736. +
  28737. + err = 0;
  28738. + room->deblk += sz;
  28739. + if (unlikely(set_deblk_end(room, &deblk_end)))
  28740. + err = append_deblk(vdir);
  28741. + /* smp_mb(); */
  28742. +
  28743. +out:
  28744. + return err;
  28745. +}
  28746. +
  28747. +/* ---------------------------------------------------------------------- */
  28748. +
  28749. +void au_vdir_free(struct au_vdir *vdir, int atonce)
  28750. +{
  28751. + unsigned char **deblk;
  28752. +
  28753. + deblk = vdir->vd_deblk;
  28754. + if (!atonce) {
  28755. + while (vdir->vd_nblk--)
  28756. + au_delayed_kfree(*deblk++);
  28757. + au_delayed_kfree(vdir->vd_deblk);
  28758. + au_cache_dfree_vdir(vdir);
  28759. + } else {
  28760. + /* not delayed */
  28761. + while (vdir->vd_nblk--)
  28762. + kfree(*deblk++);
  28763. + kfree(vdir->vd_deblk);
  28764. + au_cache_free_vdir(vdir);
  28765. + }
  28766. +}
  28767. +
  28768. +static struct au_vdir *alloc_vdir(struct file *file)
  28769. +{
  28770. + struct au_vdir *vdir;
  28771. + struct super_block *sb;
  28772. + int err;
  28773. +
  28774. + sb = file->f_path.dentry->d_sb;
  28775. + SiMustAnyLock(sb);
  28776. +
  28777. + err = -ENOMEM;
  28778. + vdir = au_cache_alloc_vdir();
  28779. + if (unlikely(!vdir))
  28780. + goto out;
  28781. +
  28782. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  28783. + if (unlikely(!vdir->vd_deblk))
  28784. + goto out_free;
  28785. +
  28786. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  28787. + if (!vdir->vd_deblk_sz) {
  28788. + /* estimate the appropriate size for deblk */
  28789. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  28790. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  28791. + }
  28792. + vdir->vd_nblk = 0;
  28793. + vdir->vd_version = 0;
  28794. + vdir->vd_jiffy = 0;
  28795. + err = append_deblk(vdir);
  28796. + if (!err)
  28797. + return vdir; /* success */
  28798. +
  28799. + au_delayed_kfree(vdir->vd_deblk);
  28800. +
  28801. +out_free:
  28802. + au_cache_dfree_vdir(vdir);
  28803. +out:
  28804. + vdir = ERR_PTR(err);
  28805. + return vdir;
  28806. +}
  28807. +
  28808. +static int reinit_vdir(struct au_vdir *vdir)
  28809. +{
  28810. + int err;
  28811. + union au_vdir_deblk_p p, deblk_end;
  28812. +
  28813. + while (vdir->vd_nblk > 1) {
  28814. + au_delayed_kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
  28815. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  28816. + vdir->vd_nblk--;
  28817. + }
  28818. + p.deblk = vdir->vd_deblk[0];
  28819. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  28820. + err = set_deblk_end(&p, &deblk_end);
  28821. + /* keep vd_dblk_sz */
  28822. + vdir->vd_last.ul = 0;
  28823. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  28824. + vdir->vd_version = 0;
  28825. + vdir->vd_jiffy = 0;
  28826. + /* smp_mb(); */
  28827. + return err;
  28828. +}
  28829. +
  28830. +/* ---------------------------------------------------------------------- */
  28831. +
  28832. +#define AuFillVdir_CALLED 1
  28833. +#define AuFillVdir_WHABLE (1 << 1)
  28834. +#define AuFillVdir_SHWH (1 << 2)
  28835. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  28836. +#define au_fset_fillvdir(flags, name) \
  28837. + do { (flags) |= AuFillVdir_##name; } while (0)
  28838. +#define au_fclr_fillvdir(flags, name) \
  28839. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  28840. +
  28841. +#ifndef CONFIG_AUFS_SHWH
  28842. +#undef AuFillVdir_SHWH
  28843. +#define AuFillVdir_SHWH 0
  28844. +#endif
  28845. +
  28846. +struct fillvdir_arg {
  28847. + struct dir_context ctx;
  28848. + struct file *file;
  28849. + struct au_vdir *vdir;
  28850. + struct au_nhash delist;
  28851. + struct au_nhash whlist;
  28852. + aufs_bindex_t bindex;
  28853. + unsigned int flags;
  28854. + int err;
  28855. +};
  28856. +
  28857. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  28858. + loff_t offset __maybe_unused, u64 h_ino,
  28859. + unsigned int d_type)
  28860. +{
  28861. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  28862. + char *name = (void *)__name;
  28863. + struct super_block *sb;
  28864. + ino_t ino;
  28865. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  28866. +
  28867. + arg->err = 0;
  28868. + sb = arg->file->f_path.dentry->d_sb;
  28869. + au_fset_fillvdir(arg->flags, CALLED);
  28870. + /* smp_mb(); */
  28871. + if (nlen <= AUFS_WH_PFX_LEN
  28872. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  28873. + if (test_known(&arg->delist, name, nlen)
  28874. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  28875. + goto out; /* already exists or whiteouted */
  28876. +
  28877. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  28878. + if (!arg->err) {
  28879. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  28880. + d_type = DT_UNKNOWN;
  28881. + arg->err = append_de(arg->vdir, name, nlen, ino,
  28882. + d_type, &arg->delist);
  28883. + }
  28884. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  28885. + name += AUFS_WH_PFX_LEN;
  28886. + nlen -= AUFS_WH_PFX_LEN;
  28887. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  28888. + goto out; /* already whiteouted */
  28889. +
  28890. + if (shwh)
  28891. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  28892. + &ino);
  28893. + if (!arg->err) {
  28894. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  28895. + d_type = DT_UNKNOWN;
  28896. + arg->err = au_nhash_append_wh
  28897. + (&arg->whlist, name, nlen, ino, d_type,
  28898. + arg->bindex, shwh);
  28899. + }
  28900. + }
  28901. +
  28902. +out:
  28903. + if (!arg->err)
  28904. + arg->vdir->vd_jiffy = jiffies;
  28905. + /* smp_mb(); */
  28906. + AuTraceErr(arg->err);
  28907. + return arg->err;
  28908. +}
  28909. +
  28910. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  28911. + struct au_nhash *whlist, struct au_nhash *delist)
  28912. +{
  28913. +#ifdef CONFIG_AUFS_SHWH
  28914. + int err;
  28915. + unsigned int nh, u;
  28916. + struct hlist_head *head;
  28917. + struct au_vdir_wh *pos;
  28918. + struct hlist_node *n;
  28919. + char *p, *o;
  28920. + struct au_vdir_destr *destr;
  28921. +
  28922. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  28923. +
  28924. + err = -ENOMEM;
  28925. + o = p = (void *)__get_free_page(GFP_NOFS);
  28926. + if (unlikely(!p))
  28927. + goto out;
  28928. +
  28929. + err = 0;
  28930. + nh = whlist->nh_num;
  28931. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  28932. + p += AUFS_WH_PFX_LEN;
  28933. + for (u = 0; u < nh; u++) {
  28934. + head = whlist->nh_head + u;
  28935. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  28936. + destr = &pos->wh_str;
  28937. + memcpy(p, destr->name, destr->len);
  28938. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  28939. + pos->wh_ino, pos->wh_type, delist);
  28940. + if (unlikely(err))
  28941. + break;
  28942. + }
  28943. + }
  28944. +
  28945. + au_delayed_free_page((unsigned long)o);
  28946. +
  28947. +out:
  28948. + AuTraceErr(err);
  28949. + return err;
  28950. +#else
  28951. + return 0;
  28952. +#endif
  28953. +}
  28954. +
  28955. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  28956. +{
  28957. + int err;
  28958. + unsigned int rdhash;
  28959. + loff_t offset;
  28960. + aufs_bindex_t bbot, bindex, btop;
  28961. + unsigned char shwh;
  28962. + struct file *hf, *file;
  28963. + struct super_block *sb;
  28964. +
  28965. + file = arg->file;
  28966. + sb = file->f_path.dentry->d_sb;
  28967. + SiMustAnyLock(sb);
  28968. +
  28969. + rdhash = au_sbi(sb)->si_rdhash;
  28970. + if (!rdhash)
  28971. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  28972. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  28973. + if (unlikely(err))
  28974. + goto out;
  28975. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  28976. + if (unlikely(err))
  28977. + goto out_delist;
  28978. +
  28979. + err = 0;
  28980. + arg->flags = 0;
  28981. + shwh = 0;
  28982. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  28983. + shwh = 1;
  28984. + au_fset_fillvdir(arg->flags, SHWH);
  28985. + }
  28986. + btop = au_fbtop(file);
  28987. + bbot = au_fbbot_dir(file);
  28988. + for (bindex = btop; !err && bindex <= bbot; bindex++) {
  28989. + hf = au_hf_dir(file, bindex);
  28990. + if (!hf)
  28991. + continue;
  28992. +
  28993. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  28994. + err = offset;
  28995. + if (unlikely(offset))
  28996. + break;
  28997. +
  28998. + arg->bindex = bindex;
  28999. + au_fclr_fillvdir(arg->flags, WHABLE);
  29000. + if (shwh
  29001. + || (bindex != bbot
  29002. + && au_br_whable(au_sbr_perm(sb, bindex))))
  29003. + au_fset_fillvdir(arg->flags, WHABLE);
  29004. + do {
  29005. + arg->err = 0;
  29006. + au_fclr_fillvdir(arg->flags, CALLED);
  29007. + /* smp_mb(); */
  29008. + err = vfsub_iterate_dir(hf, &arg->ctx);
  29009. + if (err >= 0)
  29010. + err = arg->err;
  29011. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  29012. +
  29013. + /*
  29014. + * dir_relax() may be good for concurrency, but aufs should not
  29015. + * use it since it will cause a lockdep problem.
  29016. + */
  29017. + }
  29018. +
  29019. + if (!err && shwh)
  29020. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  29021. +
  29022. + au_nhash_wh_free(&arg->whlist);
  29023. +
  29024. +out_delist:
  29025. + au_nhash_de_free(&arg->delist);
  29026. +out:
  29027. + return err;
  29028. +}
  29029. +
  29030. +static int read_vdir(struct file *file, int may_read)
  29031. +{
  29032. + int err;
  29033. + unsigned long expire;
  29034. + unsigned char do_read;
  29035. + struct fillvdir_arg arg = {
  29036. + .ctx = {
  29037. + .actor = fillvdir
  29038. + }
  29039. + };
  29040. + struct inode *inode;
  29041. + struct au_vdir *vdir, *allocated;
  29042. +
  29043. + err = 0;
  29044. + inode = file_inode(file);
  29045. + IMustLock(inode);
  29046. + IiMustWriteLock(inode);
  29047. + SiMustAnyLock(inode->i_sb);
  29048. +
  29049. + allocated = NULL;
  29050. + do_read = 0;
  29051. + expire = au_sbi(inode->i_sb)->si_rdcache;
  29052. + vdir = au_ivdir(inode);
  29053. + if (!vdir) {
  29054. + do_read = 1;
  29055. + vdir = alloc_vdir(file);
  29056. + err = PTR_ERR(vdir);
  29057. + if (IS_ERR(vdir))
  29058. + goto out;
  29059. + err = 0;
  29060. + allocated = vdir;
  29061. + } else if (may_read
  29062. + && (inode->i_version != vdir->vd_version
  29063. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  29064. + do_read = 1;
  29065. + err = reinit_vdir(vdir);
  29066. + if (unlikely(err))
  29067. + goto out;
  29068. + }
  29069. +
  29070. + if (!do_read)
  29071. + return 0; /* success */
  29072. +
  29073. + arg.file = file;
  29074. + arg.vdir = vdir;
  29075. + err = au_do_read_vdir(&arg);
  29076. + if (!err) {
  29077. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  29078. + vdir->vd_version = inode->i_version;
  29079. + vdir->vd_last.ul = 0;
  29080. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  29081. + if (allocated)
  29082. + au_set_ivdir(inode, allocated);
  29083. + } else if (allocated)
  29084. + au_vdir_free(allocated, /*atonce*/0);
  29085. +
  29086. +out:
  29087. + return err;
  29088. +}
  29089. +
  29090. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  29091. +{
  29092. + int err, rerr;
  29093. + unsigned long ul, n;
  29094. + const unsigned int deblk_sz = src->vd_deblk_sz;
  29095. +
  29096. + AuDebugOn(tgt->vd_nblk != 1);
  29097. +
  29098. + err = -ENOMEM;
  29099. + if (tgt->vd_nblk < src->vd_nblk) {
  29100. + unsigned char **p;
  29101. +
  29102. + p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  29103. + GFP_NOFS);
  29104. + if (unlikely(!p))
  29105. + goto out;
  29106. + tgt->vd_deblk = p;
  29107. + }
  29108. +
  29109. + if (tgt->vd_deblk_sz != deblk_sz) {
  29110. + unsigned char *p;
  29111. +
  29112. + tgt->vd_deblk_sz = deblk_sz;
  29113. + p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
  29114. + if (unlikely(!p))
  29115. + goto out;
  29116. + tgt->vd_deblk[0] = p;
  29117. + }
  29118. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  29119. + tgt->vd_version = src->vd_version;
  29120. + tgt->vd_jiffy = src->vd_jiffy;
  29121. +
  29122. + n = src->vd_nblk;
  29123. + for (ul = 1; ul < n; ul++) {
  29124. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  29125. + GFP_NOFS);
  29126. + if (unlikely(!tgt->vd_deblk[ul]))
  29127. + goto out;
  29128. + tgt->vd_nblk++;
  29129. + }
  29130. + tgt->vd_nblk = n;
  29131. + tgt->vd_last.ul = tgt->vd_last.ul;
  29132. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  29133. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  29134. + - src->vd_deblk[src->vd_last.ul];
  29135. + /* smp_mb(); */
  29136. + return 0; /* success */
  29137. +
  29138. +out:
  29139. + rerr = reinit_vdir(tgt);
  29140. + BUG_ON(rerr);
  29141. + return err;
  29142. +}
  29143. +
  29144. +int au_vdir_init(struct file *file)
  29145. +{
  29146. + int err;
  29147. + struct inode *inode;
  29148. + struct au_vdir *vdir_cache, *allocated;
  29149. +
  29150. + /* test file->f_pos here instead of ctx->pos */
  29151. + err = read_vdir(file, !file->f_pos);
  29152. + if (unlikely(err))
  29153. + goto out;
  29154. +
  29155. + allocated = NULL;
  29156. + vdir_cache = au_fvdir_cache(file);
  29157. + if (!vdir_cache) {
  29158. + vdir_cache = alloc_vdir(file);
  29159. + err = PTR_ERR(vdir_cache);
  29160. + if (IS_ERR(vdir_cache))
  29161. + goto out;
  29162. + allocated = vdir_cache;
  29163. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  29164. + /* test file->f_pos here instead of ctx->pos */
  29165. + err = reinit_vdir(vdir_cache);
  29166. + if (unlikely(err))
  29167. + goto out;
  29168. + } else
  29169. + return 0; /* success */
  29170. +
  29171. + inode = file_inode(file);
  29172. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  29173. + if (!err) {
  29174. + file->f_version = inode->i_version;
  29175. + if (allocated)
  29176. + au_set_fvdir_cache(file, allocated);
  29177. + } else if (allocated)
  29178. + au_vdir_free(allocated, /*atonce*/0);
  29179. +
  29180. +out:
  29181. + return err;
  29182. +}
  29183. +
  29184. +static loff_t calc_offset(struct au_vdir *vdir)
  29185. +{
  29186. + loff_t offset;
  29187. + union au_vdir_deblk_p p;
  29188. +
  29189. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  29190. + offset = vdir->vd_last.p.deblk - p.deblk;
  29191. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  29192. + return offset;
  29193. +}
  29194. +
  29195. +/* returns true or false */
  29196. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  29197. +{
  29198. + int valid;
  29199. + unsigned int deblk_sz;
  29200. + unsigned long ul, n;
  29201. + loff_t offset;
  29202. + union au_vdir_deblk_p p, deblk_end;
  29203. + struct au_vdir *vdir_cache;
  29204. +
  29205. + valid = 1;
  29206. + vdir_cache = au_fvdir_cache(file);
  29207. + offset = calc_offset(vdir_cache);
  29208. + AuDbg("offset %lld\n", offset);
  29209. + if (ctx->pos == offset)
  29210. + goto out;
  29211. +
  29212. + vdir_cache->vd_last.ul = 0;
  29213. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  29214. + if (!ctx->pos)
  29215. + goto out;
  29216. +
  29217. + valid = 0;
  29218. + deblk_sz = vdir_cache->vd_deblk_sz;
  29219. + ul = div64_u64(ctx->pos, deblk_sz);
  29220. + AuDbg("ul %lu\n", ul);
  29221. + if (ul >= vdir_cache->vd_nblk)
  29222. + goto out;
  29223. +
  29224. + n = vdir_cache->vd_nblk;
  29225. + for (; ul < n; ul++) {
  29226. + p.deblk = vdir_cache->vd_deblk[ul];
  29227. + deblk_end.deblk = p.deblk + deblk_sz;
  29228. + offset = ul;
  29229. + offset *= deblk_sz;
  29230. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  29231. + unsigned int l;
  29232. +
  29233. + l = calc_size(p.de->de_str.len);
  29234. + offset += l;
  29235. + p.deblk += l;
  29236. + }
  29237. + if (!is_deblk_end(&p, &deblk_end)) {
  29238. + valid = 1;
  29239. + vdir_cache->vd_last.ul = ul;
  29240. + vdir_cache->vd_last.p = p;
  29241. + break;
  29242. + }
  29243. + }
  29244. +
  29245. +out:
  29246. + /* smp_mb(); */
  29247. + AuTraceErr(!valid);
  29248. + return valid;
  29249. +}
  29250. +
  29251. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  29252. +{
  29253. + unsigned int l, deblk_sz;
  29254. + union au_vdir_deblk_p deblk_end;
  29255. + struct au_vdir *vdir_cache;
  29256. + struct au_vdir_de *de;
  29257. +
  29258. + vdir_cache = au_fvdir_cache(file);
  29259. + if (!seek_vdir(file, ctx))
  29260. + return 0;
  29261. +
  29262. + deblk_sz = vdir_cache->vd_deblk_sz;
  29263. + while (1) {
  29264. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  29265. + deblk_end.deblk += deblk_sz;
  29266. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  29267. + de = vdir_cache->vd_last.p.de;
  29268. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  29269. + de->de_str.len, de->de_str.name, ctx->pos,
  29270. + (unsigned long)de->de_ino, de->de_type);
  29271. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  29272. + de->de_str.len, de->de_ino,
  29273. + de->de_type))) {
  29274. + /* todo: ignore the error caused by udba? */
  29275. + /* return err; */
  29276. + return 0;
  29277. + }
  29278. +
  29279. + l = calc_size(de->de_str.len);
  29280. + vdir_cache->vd_last.p.deblk += l;
  29281. + ctx->pos += l;
  29282. + }
  29283. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  29284. + vdir_cache->vd_last.ul++;
  29285. + vdir_cache->vd_last.p.deblk
  29286. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  29287. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  29288. + continue;
  29289. + }
  29290. + break;
  29291. + }
  29292. +
  29293. + /* smp_mb(); */
  29294. + return 0;
  29295. +}
  29296. diff -Naur null/fs/aufs/vfsub.c linux-4.7/fs/aufs/vfsub.c
  29297. --- /dev/null
  29298. +++ linux-4.7/fs/aufs/vfsub.c 2016-08-08 12:57:44.609384518 -0400
  29299. @@ -0,0 +1,884 @@
  29300. +/*
  29301. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  29302. + *
  29303. + * This program, aufs is free software; you can redistribute it and/or modify
  29304. + * it under the terms of the GNU General Public License as published by
  29305. + * the Free Software Foundation; either version 2 of the License, or
  29306. + * (at your option) any later version.
  29307. + *
  29308. + * This program is distributed in the hope that it will be useful,
  29309. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29310. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29311. + * GNU General Public License for more details.
  29312. + *
  29313. + * You should have received a copy of the GNU General Public License
  29314. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29315. + */
  29316. +
  29317. +/*
  29318. + * sub-routines for VFS
  29319. + */
  29320. +
  29321. +#include <linux/namei.h>
  29322. +#include <linux/nsproxy.h>
  29323. +#include <linux/security.h>
  29324. +#include <linux/splice.h>
  29325. +#include "../fs/mount.h"
  29326. +#include "aufs.h"
  29327. +
  29328. +#ifdef CONFIG_AUFS_BR_FUSE
  29329. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
  29330. +{
  29331. + struct nsproxy *ns;
  29332. +
  29333. + if (!au_test_fuse(h_sb) || !au_userns)
  29334. + return 0;
  29335. +
  29336. + ns = current->nsproxy;
  29337. + /* no {get,put}_nsproxy(ns) */
  29338. + return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
  29339. +}
  29340. +#endif
  29341. +
  29342. +/* ---------------------------------------------------------------------- */
  29343. +
  29344. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  29345. +{
  29346. + int err;
  29347. + struct kstat st;
  29348. + struct super_block *h_sb;
  29349. +
  29350. + /* for remote fs, leave work for its getattr or d_revalidate */
  29351. + /* for bad i_attr fs, handle them in aufs_getattr() */
  29352. + /* still some fs may acquire i_mutex. we need to skip them */
  29353. + err = 0;
  29354. + if (!did)
  29355. + did = &err;
  29356. + h_sb = h_path->dentry->d_sb;
  29357. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  29358. + if (*did)
  29359. + err = vfs_getattr(h_path, &st);
  29360. +
  29361. + return err;
  29362. +}
  29363. +
  29364. +/* ---------------------------------------------------------------------- */
  29365. +
  29366. +struct file *vfsub_dentry_open(struct path *path, int flags)
  29367. +{
  29368. + struct file *file;
  29369. +
  29370. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  29371. + current_cred());
  29372. + if (!IS_ERR_OR_NULL(file)
  29373. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  29374. + i_readcount_inc(d_inode(path->dentry));
  29375. +
  29376. + return file;
  29377. +}
  29378. +
  29379. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  29380. +{
  29381. + struct file *file;
  29382. +
  29383. + lockdep_off();
  29384. + file = filp_open(path,
  29385. + oflags /* | __FMODE_NONOTIFY */,
  29386. + mode);
  29387. + lockdep_on();
  29388. + if (IS_ERR(file))
  29389. + goto out;
  29390. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29391. +
  29392. +out:
  29393. + return file;
  29394. +}
  29395. +
  29396. +/*
  29397. + * Ideally this function should call VFS:do_last() in order to keep all its
  29398. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  29399. + * structure such as nameidata. This is a second (or third) best approach.
  29400. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  29401. + */
  29402. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  29403. + struct vfsub_aopen_args *args, struct au_branch *br)
  29404. +{
  29405. + int err;
  29406. + struct file *file = args->file;
  29407. + /* copied from linux/fs/namei.c:atomic_open() */
  29408. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  29409. +
  29410. + IMustLock(dir);
  29411. + AuDebugOn(!dir->i_op->atomic_open);
  29412. +
  29413. + err = au_br_test_oflag(args->open_flag, br);
  29414. + if (unlikely(err))
  29415. + goto out;
  29416. +
  29417. + args->file->f_path.dentry = DENTRY_NOT_SET;
  29418. + args->file->f_path.mnt = au_br_mnt(br);
  29419. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  29420. + args->create_mode, args->opened);
  29421. + if (err >= 0) {
  29422. + /* some filesystems don't set FILE_CREATED while succeeded? */
  29423. + if (*args->opened & FILE_CREATED)
  29424. + fsnotify_create(dir, dentry);
  29425. + } else
  29426. + goto out;
  29427. +
  29428. +
  29429. + if (!err) {
  29430. + /* todo: call VFS:may_open() here */
  29431. + err = open_check_o_direct(file);
  29432. + /* todo: ima_file_check() too? */
  29433. + if (!err && (args->open_flag & __FMODE_EXEC))
  29434. + err = deny_write_access(file);
  29435. + if (unlikely(err))
  29436. + /* note that the file is created and still opened */
  29437. + goto out;
  29438. + }
  29439. +
  29440. + au_br_get(br);
  29441. + fsnotify_open(file);
  29442. +
  29443. +out:
  29444. + return err;
  29445. +}
  29446. +
  29447. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  29448. +{
  29449. + int err;
  29450. +
  29451. + err = kern_path(name, flags, path);
  29452. + if (!err && d_is_positive(path->dentry))
  29453. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  29454. + return err;
  29455. +}
  29456. +
  29457. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  29458. + struct dentry *parent, int len)
  29459. +{
  29460. + struct path path = {
  29461. + .mnt = NULL
  29462. + };
  29463. +
  29464. + path.dentry = lookup_one_len_unlocked(name, parent, len);
  29465. + if (IS_ERR(path.dentry))
  29466. + goto out;
  29467. + if (d_is_positive(path.dentry))
  29468. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  29469. +
  29470. +out:
  29471. + AuTraceErrPtr(path.dentry);
  29472. + return path.dentry;
  29473. +}
  29474. +
  29475. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  29476. + int len)
  29477. +{
  29478. + struct path path = {
  29479. + .mnt = NULL
  29480. + };
  29481. +
  29482. + /* VFS checks it too, but by WARN_ON_ONCE() */
  29483. + IMustLock(d_inode(parent));
  29484. +
  29485. + path.dentry = lookup_one_len(name, parent, len);
  29486. + if (IS_ERR(path.dentry))
  29487. + goto out;
  29488. + if (d_is_positive(path.dentry))
  29489. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  29490. +
  29491. +out:
  29492. + AuTraceErrPtr(path.dentry);
  29493. + return path.dentry;
  29494. +}
  29495. +
  29496. +void vfsub_call_lkup_one(void *args)
  29497. +{
  29498. + struct vfsub_lkup_one_args *a = args;
  29499. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  29500. +}
  29501. +
  29502. +/* ---------------------------------------------------------------------- */
  29503. +
  29504. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29505. + struct dentry *d2, struct au_hinode *hdir2)
  29506. +{
  29507. + struct dentry *d;
  29508. +
  29509. + lockdep_off();
  29510. + d = lock_rename(d1, d2);
  29511. + lockdep_on();
  29512. + au_hn_suspend(hdir1);
  29513. + if (hdir1 != hdir2)
  29514. + au_hn_suspend(hdir2);
  29515. +
  29516. + return d;
  29517. +}
  29518. +
  29519. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29520. + struct dentry *d2, struct au_hinode *hdir2)
  29521. +{
  29522. + au_hn_resume(hdir1);
  29523. + if (hdir1 != hdir2)
  29524. + au_hn_resume(hdir2);
  29525. + lockdep_off();
  29526. + unlock_rename(d1, d2);
  29527. + lockdep_on();
  29528. +}
  29529. +
  29530. +/* ---------------------------------------------------------------------- */
  29531. +
  29532. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  29533. +{
  29534. + int err;
  29535. + struct dentry *d;
  29536. +
  29537. + IMustLock(dir);
  29538. +
  29539. + d = path->dentry;
  29540. + path->dentry = d->d_parent;
  29541. + err = security_path_mknod(path, d, mode, 0);
  29542. + path->dentry = d;
  29543. + if (unlikely(err))
  29544. + goto out;
  29545. +
  29546. + lockdep_off();
  29547. + err = vfs_create(dir, path->dentry, mode, want_excl);
  29548. + lockdep_on();
  29549. + if (!err) {
  29550. + struct path tmp = *path;
  29551. + int did;
  29552. +
  29553. + vfsub_update_h_iattr(&tmp, &did);
  29554. + if (did) {
  29555. + tmp.dentry = path->dentry->d_parent;
  29556. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29557. + }
  29558. + /*ignore*/
  29559. + }
  29560. +
  29561. +out:
  29562. + return err;
  29563. +}
  29564. +
  29565. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  29566. +{
  29567. + int err;
  29568. + struct dentry *d;
  29569. +
  29570. + IMustLock(dir);
  29571. +
  29572. + d = path->dentry;
  29573. + path->dentry = d->d_parent;
  29574. + err = security_path_symlink(path, d, symname);
  29575. + path->dentry = d;
  29576. + if (unlikely(err))
  29577. + goto out;
  29578. +
  29579. + lockdep_off();
  29580. + err = vfs_symlink(dir, path->dentry, symname);
  29581. + lockdep_on();
  29582. + if (!err) {
  29583. + struct path tmp = *path;
  29584. + int did;
  29585. +
  29586. + vfsub_update_h_iattr(&tmp, &did);
  29587. + if (did) {
  29588. + tmp.dentry = path->dentry->d_parent;
  29589. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29590. + }
  29591. + /*ignore*/
  29592. + }
  29593. +
  29594. +out:
  29595. + return err;
  29596. +}
  29597. +
  29598. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  29599. +{
  29600. + int err;
  29601. + struct dentry *d;
  29602. +
  29603. + IMustLock(dir);
  29604. +
  29605. + d = path->dentry;
  29606. + path->dentry = d->d_parent;
  29607. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  29608. + path->dentry = d;
  29609. + if (unlikely(err))
  29610. + goto out;
  29611. +
  29612. + lockdep_off();
  29613. + err = vfs_mknod(dir, path->dentry, mode, dev);
  29614. + lockdep_on();
  29615. + if (!err) {
  29616. + struct path tmp = *path;
  29617. + int did;
  29618. +
  29619. + vfsub_update_h_iattr(&tmp, &did);
  29620. + if (did) {
  29621. + tmp.dentry = path->dentry->d_parent;
  29622. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29623. + }
  29624. + /*ignore*/
  29625. + }
  29626. +
  29627. +out:
  29628. + return err;
  29629. +}
  29630. +
  29631. +static int au_test_nlink(struct inode *inode)
  29632. +{
  29633. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  29634. +
  29635. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  29636. + || inode->i_nlink < link_max)
  29637. + return 0;
  29638. + return -EMLINK;
  29639. +}
  29640. +
  29641. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  29642. + struct inode **delegated_inode)
  29643. +{
  29644. + int err;
  29645. + struct dentry *d;
  29646. +
  29647. + IMustLock(dir);
  29648. +
  29649. + err = au_test_nlink(d_inode(src_dentry));
  29650. + if (unlikely(err))
  29651. + return err;
  29652. +
  29653. + /* we don't call may_linkat() */
  29654. + d = path->dentry;
  29655. + path->dentry = d->d_parent;
  29656. + err = security_path_link(src_dentry, path, d);
  29657. + path->dentry = d;
  29658. + if (unlikely(err))
  29659. + goto out;
  29660. +
  29661. + lockdep_off();
  29662. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  29663. + lockdep_on();
  29664. + if (!err) {
  29665. + struct path tmp = *path;
  29666. + int did;
  29667. +
  29668. + /* fuse has different memory inode for the same inumber */
  29669. + vfsub_update_h_iattr(&tmp, &did);
  29670. + if (did) {
  29671. + tmp.dentry = path->dentry->d_parent;
  29672. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29673. + tmp.dentry = src_dentry;
  29674. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29675. + }
  29676. + /*ignore*/
  29677. + }
  29678. +
  29679. +out:
  29680. + return err;
  29681. +}
  29682. +
  29683. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  29684. + struct inode *dir, struct path *path,
  29685. + struct inode **delegated_inode)
  29686. +{
  29687. + int err;
  29688. + struct path tmp = {
  29689. + .mnt = path->mnt
  29690. + };
  29691. + struct dentry *d;
  29692. +
  29693. + IMustLock(dir);
  29694. + IMustLock(src_dir);
  29695. +
  29696. + d = path->dentry;
  29697. + path->dentry = d->d_parent;
  29698. + tmp.dentry = src_dentry->d_parent;
  29699. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  29700. + path->dentry = d;
  29701. + if (unlikely(err))
  29702. + goto out;
  29703. +
  29704. + lockdep_off();
  29705. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  29706. + delegated_inode, /*flags*/0);
  29707. + lockdep_on();
  29708. + if (!err) {
  29709. + int did;
  29710. +
  29711. + tmp.dentry = d->d_parent;
  29712. + vfsub_update_h_iattr(&tmp, &did);
  29713. + if (did) {
  29714. + tmp.dentry = src_dentry;
  29715. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29716. + tmp.dentry = src_dentry->d_parent;
  29717. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29718. + }
  29719. + /*ignore*/
  29720. + }
  29721. +
  29722. +out:
  29723. + return err;
  29724. +}
  29725. +
  29726. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  29727. +{
  29728. + int err;
  29729. + struct dentry *d;
  29730. +
  29731. + IMustLock(dir);
  29732. +
  29733. + d = path->dentry;
  29734. + path->dentry = d->d_parent;
  29735. + err = security_path_mkdir(path, d, mode);
  29736. + path->dentry = d;
  29737. + if (unlikely(err))
  29738. + goto out;
  29739. +
  29740. + lockdep_off();
  29741. + err = vfs_mkdir(dir, path->dentry, mode);
  29742. + lockdep_on();
  29743. + if (!err) {
  29744. + struct path tmp = *path;
  29745. + int did;
  29746. +
  29747. + vfsub_update_h_iattr(&tmp, &did);
  29748. + if (did) {
  29749. + tmp.dentry = path->dentry->d_parent;
  29750. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29751. + }
  29752. + /*ignore*/
  29753. + }
  29754. +
  29755. +out:
  29756. + return err;
  29757. +}
  29758. +
  29759. +int vfsub_rmdir(struct inode *dir, struct path *path)
  29760. +{
  29761. + int err;
  29762. + struct dentry *d;
  29763. +
  29764. + IMustLock(dir);
  29765. +
  29766. + d = path->dentry;
  29767. + path->dentry = d->d_parent;
  29768. + err = security_path_rmdir(path, d);
  29769. + path->dentry = d;
  29770. + if (unlikely(err))
  29771. + goto out;
  29772. +
  29773. + lockdep_off();
  29774. + err = vfs_rmdir(dir, path->dentry);
  29775. + lockdep_on();
  29776. + if (!err) {
  29777. + struct path tmp = {
  29778. + .dentry = path->dentry->d_parent,
  29779. + .mnt = path->mnt
  29780. + };
  29781. +
  29782. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  29783. + }
  29784. +
  29785. +out:
  29786. + return err;
  29787. +}
  29788. +
  29789. +/* ---------------------------------------------------------------------- */
  29790. +
  29791. +/* todo: support mmap_sem? */
  29792. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  29793. + loff_t *ppos)
  29794. +{
  29795. + ssize_t err;
  29796. +
  29797. + lockdep_off();
  29798. + err = vfs_read(file, ubuf, count, ppos);
  29799. + lockdep_on();
  29800. + if (err >= 0)
  29801. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29802. + return err;
  29803. +}
  29804. +
  29805. +/* todo: kernel_read()? */
  29806. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  29807. + loff_t *ppos)
  29808. +{
  29809. + ssize_t err;
  29810. + mm_segment_t oldfs;
  29811. + union {
  29812. + void *k;
  29813. + char __user *u;
  29814. + } buf;
  29815. +
  29816. + buf.k = kbuf;
  29817. + oldfs = get_fs();
  29818. + set_fs(KERNEL_DS);
  29819. + err = vfsub_read_u(file, buf.u, count, ppos);
  29820. + set_fs(oldfs);
  29821. + return err;
  29822. +}
  29823. +
  29824. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  29825. + loff_t *ppos)
  29826. +{
  29827. + ssize_t err;
  29828. +
  29829. + lockdep_off();
  29830. + err = vfs_write(file, ubuf, count, ppos);
  29831. + lockdep_on();
  29832. + if (err >= 0)
  29833. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29834. + return err;
  29835. +}
  29836. +
  29837. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  29838. +{
  29839. + ssize_t err;
  29840. + mm_segment_t oldfs;
  29841. + union {
  29842. + void *k;
  29843. + const char __user *u;
  29844. + } buf;
  29845. +
  29846. + buf.k = kbuf;
  29847. + oldfs = get_fs();
  29848. + set_fs(KERNEL_DS);
  29849. + err = vfsub_write_u(file, buf.u, count, ppos);
  29850. + set_fs(oldfs);
  29851. + return err;
  29852. +}
  29853. +
  29854. +int vfsub_flush(struct file *file, fl_owner_t id)
  29855. +{
  29856. + int err;
  29857. +
  29858. + err = 0;
  29859. + if (file->f_op->flush) {
  29860. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  29861. + err = file->f_op->flush(file, id);
  29862. + else {
  29863. + lockdep_off();
  29864. + err = file->f_op->flush(file, id);
  29865. + lockdep_on();
  29866. + }
  29867. + if (!err)
  29868. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  29869. + /*ignore*/
  29870. + }
  29871. + return err;
  29872. +}
  29873. +
  29874. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  29875. +{
  29876. + int err;
  29877. +
  29878. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  29879. +
  29880. + lockdep_off();
  29881. + err = iterate_dir(file, ctx);
  29882. + lockdep_on();
  29883. + if (err >= 0)
  29884. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29885. + return err;
  29886. +}
  29887. +
  29888. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  29889. + struct pipe_inode_info *pipe, size_t len,
  29890. + unsigned int flags)
  29891. +{
  29892. + long err;
  29893. +
  29894. + lockdep_off();
  29895. + err = do_splice_to(in, ppos, pipe, len, flags);
  29896. + lockdep_on();
  29897. + file_accessed(in);
  29898. + if (err >= 0)
  29899. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  29900. + return err;
  29901. +}
  29902. +
  29903. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  29904. + loff_t *ppos, size_t len, unsigned int flags)
  29905. +{
  29906. + long err;
  29907. +
  29908. + lockdep_off();
  29909. + err = do_splice_from(pipe, out, ppos, len, flags);
  29910. + lockdep_on();
  29911. + if (err >= 0)
  29912. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  29913. + return err;
  29914. +}
  29915. +
  29916. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  29917. +{
  29918. + int err;
  29919. +
  29920. + /* file can be NULL */
  29921. + lockdep_off();
  29922. + err = vfs_fsync(file, datasync);
  29923. + lockdep_on();
  29924. + if (!err) {
  29925. + if (!path) {
  29926. + AuDebugOn(!file);
  29927. + path = &file->f_path;
  29928. + }
  29929. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  29930. + }
  29931. + return err;
  29932. +}
  29933. +
  29934. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  29935. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  29936. + struct file *h_file)
  29937. +{
  29938. + int err;
  29939. + struct inode *h_inode;
  29940. + struct super_block *h_sb;
  29941. +
  29942. + if (!h_file) {
  29943. + err = vfsub_truncate(h_path, length);
  29944. + goto out;
  29945. + }
  29946. +
  29947. + h_inode = d_inode(h_path->dentry);
  29948. + h_sb = h_inode->i_sb;
  29949. + lockdep_off();
  29950. + sb_start_write(h_sb);
  29951. + lockdep_on();
  29952. + err = locks_verify_truncate(h_inode, h_file, length);
  29953. + if (!err)
  29954. + err = security_path_truncate(h_path);
  29955. + if (!err) {
  29956. + lockdep_off();
  29957. + err = do_truncate(h_path->dentry, length, attr, h_file);
  29958. + lockdep_on();
  29959. + }
  29960. + lockdep_off();
  29961. + sb_end_write(h_sb);
  29962. + lockdep_on();
  29963. +
  29964. +out:
  29965. + return err;
  29966. +}
  29967. +
  29968. +/* ---------------------------------------------------------------------- */
  29969. +
  29970. +struct au_vfsub_mkdir_args {
  29971. + int *errp;
  29972. + struct inode *dir;
  29973. + struct path *path;
  29974. + int mode;
  29975. +};
  29976. +
  29977. +static void au_call_vfsub_mkdir(void *args)
  29978. +{
  29979. + struct au_vfsub_mkdir_args *a = args;
  29980. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  29981. +}
  29982. +
  29983. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  29984. +{
  29985. + int err, do_sio, wkq_err;
  29986. +
  29987. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  29988. + if (!do_sio) {
  29989. + lockdep_off();
  29990. + err = vfsub_mkdir(dir, path, mode);
  29991. + lockdep_on();
  29992. + } else {
  29993. + struct au_vfsub_mkdir_args args = {
  29994. + .errp = &err,
  29995. + .dir = dir,
  29996. + .path = path,
  29997. + .mode = mode
  29998. + };
  29999. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  30000. + if (unlikely(wkq_err))
  30001. + err = wkq_err;
  30002. + }
  30003. +
  30004. + return err;
  30005. +}
  30006. +
  30007. +struct au_vfsub_rmdir_args {
  30008. + int *errp;
  30009. + struct inode *dir;
  30010. + struct path *path;
  30011. +};
  30012. +
  30013. +static void au_call_vfsub_rmdir(void *args)
  30014. +{
  30015. + struct au_vfsub_rmdir_args *a = args;
  30016. + *a->errp = vfsub_rmdir(a->dir, a->path);
  30017. +}
  30018. +
  30019. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  30020. +{
  30021. + int err, do_sio, wkq_err;
  30022. +
  30023. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  30024. + if (!do_sio) {
  30025. + lockdep_off();
  30026. + err = vfsub_rmdir(dir, path);
  30027. + lockdep_on();
  30028. + } else {
  30029. + struct au_vfsub_rmdir_args args = {
  30030. + .errp = &err,
  30031. + .dir = dir,
  30032. + .path = path
  30033. + };
  30034. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  30035. + if (unlikely(wkq_err))
  30036. + err = wkq_err;
  30037. + }
  30038. +
  30039. + return err;
  30040. +}
  30041. +
  30042. +/* ---------------------------------------------------------------------- */
  30043. +
  30044. +struct notify_change_args {
  30045. + int *errp;
  30046. + struct path *path;
  30047. + struct iattr *ia;
  30048. + struct inode **delegated_inode;
  30049. +};
  30050. +
  30051. +static void call_notify_change(void *args)
  30052. +{
  30053. + struct notify_change_args *a = args;
  30054. + struct inode *h_inode;
  30055. +
  30056. + h_inode = d_inode(a->path->dentry);
  30057. + IMustLock(h_inode);
  30058. +
  30059. + *a->errp = -EPERM;
  30060. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  30061. + lockdep_off();
  30062. + *a->errp = notify_change(a->path->dentry, a->ia,
  30063. + a->delegated_inode);
  30064. + lockdep_on();
  30065. + if (!*a->errp)
  30066. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  30067. + }
  30068. + AuTraceErr(*a->errp);
  30069. +}
  30070. +
  30071. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  30072. + struct inode **delegated_inode)
  30073. +{
  30074. + int err;
  30075. + struct notify_change_args args = {
  30076. + .errp = &err,
  30077. + .path = path,
  30078. + .ia = ia,
  30079. + .delegated_inode = delegated_inode
  30080. + };
  30081. +
  30082. + call_notify_change(&args);
  30083. +
  30084. + return err;
  30085. +}
  30086. +
  30087. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  30088. + struct inode **delegated_inode)
  30089. +{
  30090. + int err, wkq_err;
  30091. + struct notify_change_args args = {
  30092. + .errp = &err,
  30093. + .path = path,
  30094. + .ia = ia,
  30095. + .delegated_inode = delegated_inode
  30096. + };
  30097. +
  30098. + wkq_err = au_wkq_wait(call_notify_change, &args);
  30099. + if (unlikely(wkq_err))
  30100. + err = wkq_err;
  30101. +
  30102. + return err;
  30103. +}
  30104. +
  30105. +/* ---------------------------------------------------------------------- */
  30106. +
  30107. +struct unlink_args {
  30108. + int *errp;
  30109. + struct inode *dir;
  30110. + struct path *path;
  30111. + struct inode **delegated_inode;
  30112. +};
  30113. +
  30114. +static void call_unlink(void *args)
  30115. +{
  30116. + struct unlink_args *a = args;
  30117. + struct dentry *d = a->path->dentry;
  30118. + struct inode *h_inode;
  30119. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  30120. + && au_dcount(d) == 1);
  30121. +
  30122. + IMustLock(a->dir);
  30123. +
  30124. + a->path->dentry = d->d_parent;
  30125. + *a->errp = security_path_unlink(a->path, d);
  30126. + a->path->dentry = d;
  30127. + if (unlikely(*a->errp))
  30128. + return;
  30129. +
  30130. + if (!stop_sillyrename)
  30131. + dget(d);
  30132. + h_inode = NULL;
  30133. + if (d_is_positive(d)) {
  30134. + h_inode = d_inode(d);
  30135. + ihold(h_inode);
  30136. + }
  30137. +
  30138. + lockdep_off();
  30139. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  30140. + lockdep_on();
  30141. + if (!*a->errp) {
  30142. + struct path tmp = {
  30143. + .dentry = d->d_parent,
  30144. + .mnt = a->path->mnt
  30145. + };
  30146. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  30147. + }
  30148. +
  30149. + if (!stop_sillyrename)
  30150. + dput(d);
  30151. + if (h_inode)
  30152. + iput(h_inode);
  30153. +
  30154. + AuTraceErr(*a->errp);
  30155. +}
  30156. +
  30157. +/*
  30158. + * @dir: must be locked.
  30159. + * @dentry: target dentry.
  30160. + */
  30161. +int vfsub_unlink(struct inode *dir, struct path *path,
  30162. + struct inode **delegated_inode, int force)
  30163. +{
  30164. + int err;
  30165. + struct unlink_args args = {
  30166. + .errp = &err,
  30167. + .dir = dir,
  30168. + .path = path,
  30169. + .delegated_inode = delegated_inode
  30170. + };
  30171. +
  30172. + if (!force)
  30173. + call_unlink(&args);
  30174. + else {
  30175. + int wkq_err;
  30176. +
  30177. + wkq_err = au_wkq_wait(call_unlink, &args);
  30178. + if (unlikely(wkq_err))
  30179. + err = wkq_err;
  30180. + }
  30181. +
  30182. + return err;
  30183. +}
  30184. diff -Naur null/fs/aufs/vfsub.h linux-4.7/fs/aufs/vfsub.h
  30185. --- /dev/null
  30186. +++ linux-4.7/fs/aufs/vfsub.h 2016-08-08 12:57:44.609384518 -0400
  30187. @@ -0,0 +1,316 @@
  30188. +/*
  30189. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  30190. + *
  30191. + * This program, aufs is free software; you can redistribute it and/or modify
  30192. + * it under the terms of the GNU General Public License as published by
  30193. + * the Free Software Foundation; either version 2 of the License, or
  30194. + * (at your option) any later version.
  30195. + *
  30196. + * This program is distributed in the hope that it will be useful,
  30197. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30198. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30199. + * GNU General Public License for more details.
  30200. + *
  30201. + * You should have received a copy of the GNU General Public License
  30202. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30203. + */
  30204. +
  30205. +/*
  30206. + * sub-routines for VFS
  30207. + */
  30208. +
  30209. +#ifndef __AUFS_VFSUB_H__
  30210. +#define __AUFS_VFSUB_H__
  30211. +
  30212. +#ifdef __KERNEL__
  30213. +
  30214. +#include <linux/fs.h>
  30215. +#include <linux/mount.h>
  30216. +#include <linux/posix_acl.h>
  30217. +#include <linux/xattr.h>
  30218. +#include "debug.h"
  30219. +
  30220. +/* copied from linux/fs/internal.h */
  30221. +/* todo: BAD approach!! */
  30222. +extern void __mnt_drop_write(struct vfsmount *);
  30223. +extern int open_check_o_direct(struct file *f);
  30224. +
  30225. +/* ---------------------------------------------------------------------- */
  30226. +
  30227. +/* lock subclass for lower inode */
  30228. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  30229. +/* reduce? gave up. */
  30230. +enum {
  30231. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  30232. + AuLsc_I_PARENT, /* lower inode, parent first */
  30233. + AuLsc_I_PARENT2, /* copyup dirs */
  30234. + AuLsc_I_PARENT3, /* copyup wh */
  30235. + AuLsc_I_CHILD,
  30236. + AuLsc_I_CHILD2,
  30237. + AuLsc_I_End
  30238. +};
  30239. +
  30240. +/* to debug easier, do not make them inlined functions */
  30241. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  30242. +#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
  30243. +
  30244. +/* ---------------------------------------------------------------------- */
  30245. +
  30246. +static inline void vfsub_drop_nlink(struct inode *inode)
  30247. +{
  30248. + AuDebugOn(!inode->i_nlink);
  30249. + drop_nlink(inode);
  30250. +}
  30251. +
  30252. +static inline void vfsub_dead_dir(struct inode *inode)
  30253. +{
  30254. + AuDebugOn(!S_ISDIR(inode->i_mode));
  30255. + inode->i_flags |= S_DEAD;
  30256. + clear_nlink(inode);
  30257. +}
  30258. +
  30259. +static inline int vfsub_native_ro(struct inode *inode)
  30260. +{
  30261. + return (inode->i_sb->s_flags & MS_RDONLY)
  30262. + || IS_RDONLY(inode)
  30263. + /* || IS_APPEND(inode) */
  30264. + || IS_IMMUTABLE(inode);
  30265. +}
  30266. +
  30267. +#ifdef CONFIG_AUFS_BR_FUSE
  30268. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
  30269. +#else
  30270. +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
  30271. +#endif
  30272. +
  30273. +/* ---------------------------------------------------------------------- */
  30274. +
  30275. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  30276. +struct file *vfsub_dentry_open(struct path *path, int flags);
  30277. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  30278. +struct vfsub_aopen_args {
  30279. + struct file *file;
  30280. + unsigned int open_flag;
  30281. + umode_t create_mode;
  30282. + int *opened;
  30283. +};
  30284. +struct au_branch;
  30285. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  30286. + struct vfsub_aopen_args *args, struct au_branch *br);
  30287. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  30288. +
  30289. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  30290. + struct dentry *parent, int len);
  30291. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  30292. + int len);
  30293. +
  30294. +struct vfsub_lkup_one_args {
  30295. + struct dentry **errp;
  30296. + struct qstr *name;
  30297. + struct dentry *parent;
  30298. +};
  30299. +
  30300. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  30301. + struct dentry *parent)
  30302. +{
  30303. + return vfsub_lookup_one_len(name->name, parent, name->len);
  30304. +}
  30305. +
  30306. +void vfsub_call_lkup_one(void *args);
  30307. +
  30308. +/* ---------------------------------------------------------------------- */
  30309. +
  30310. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  30311. +{
  30312. + int err;
  30313. +
  30314. + lockdep_off();
  30315. + err = mnt_want_write(mnt);
  30316. + lockdep_on();
  30317. + return err;
  30318. +}
  30319. +
  30320. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  30321. +{
  30322. + lockdep_off();
  30323. + mnt_drop_write(mnt);
  30324. + lockdep_on();
  30325. +}
  30326. +
  30327. +#if 0 /* reserved */
  30328. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  30329. +{
  30330. + lockdep_off();
  30331. + mnt_drop_write_file(file);
  30332. + lockdep_on();
  30333. +}
  30334. +#endif
  30335. +
  30336. +/* ---------------------------------------------------------------------- */
  30337. +
  30338. +struct au_hinode;
  30339. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  30340. + struct dentry *d2, struct au_hinode *hdir2);
  30341. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  30342. + struct dentry *d2, struct au_hinode *hdir2);
  30343. +
  30344. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  30345. + bool want_excl);
  30346. +int vfsub_symlink(struct inode *dir, struct path *path,
  30347. + const char *symname);
  30348. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  30349. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  30350. + struct path *path, struct inode **delegated_inode);
  30351. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  30352. + struct inode *hdir, struct path *path,
  30353. + struct inode **delegated_inode);
  30354. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  30355. +int vfsub_rmdir(struct inode *dir, struct path *path);
  30356. +
  30357. +/* ---------------------------------------------------------------------- */
  30358. +
  30359. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  30360. + loff_t *ppos);
  30361. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  30362. + loff_t *ppos);
  30363. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  30364. + loff_t *ppos);
  30365. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  30366. + loff_t *ppos);
  30367. +int vfsub_flush(struct file *file, fl_owner_t id);
  30368. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  30369. +
  30370. +static inline loff_t vfsub_f_size_read(struct file *file)
  30371. +{
  30372. + return i_size_read(file_inode(file));
  30373. +}
  30374. +
  30375. +static inline unsigned int vfsub_file_flags(struct file *file)
  30376. +{
  30377. + unsigned int flags;
  30378. +
  30379. + spin_lock(&file->f_lock);
  30380. + flags = file->f_flags;
  30381. + spin_unlock(&file->f_lock);
  30382. +
  30383. + return flags;
  30384. +}
  30385. +
  30386. +static inline int vfsub_file_execed(struct file *file)
  30387. +{
  30388. + /* todo: direct access f_flags */
  30389. + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
  30390. +}
  30391. +
  30392. +#if 0 /* reserved */
  30393. +static inline void vfsub_file_accessed(struct file *h_file)
  30394. +{
  30395. + file_accessed(h_file);
  30396. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  30397. +}
  30398. +#endif
  30399. +
  30400. +#if 0 /* reserved */
  30401. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  30402. + struct dentry *h_dentry)
  30403. +{
  30404. + struct path h_path = {
  30405. + .dentry = h_dentry,
  30406. + .mnt = h_mnt
  30407. + };
  30408. + touch_atime(&h_path);
  30409. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  30410. +}
  30411. +#endif
  30412. +
  30413. +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
  30414. + int flags)
  30415. +{
  30416. + return update_time(h_inode, ts, flags);
  30417. + /* no vfsub_update_h_iattr() since we don't have struct path */
  30418. +}
  30419. +
  30420. +#ifdef CONFIG_FS_POSIX_ACL
  30421. +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
  30422. +{
  30423. + int err;
  30424. +
  30425. + err = posix_acl_chmod(h_inode, h_mode);
  30426. + if (err == -EOPNOTSUPP)
  30427. + err = 0;
  30428. + return err;
  30429. +}
  30430. +#else
  30431. +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
  30432. +#endif
  30433. +
  30434. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  30435. + struct pipe_inode_info *pipe, size_t len,
  30436. + unsigned int flags);
  30437. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  30438. + loff_t *ppos, size_t len, unsigned int flags);
  30439. +
  30440. +static inline long vfsub_truncate(struct path *path, loff_t length)
  30441. +{
  30442. + long err;
  30443. +
  30444. + lockdep_off();
  30445. + err = vfs_truncate(path, length);
  30446. + lockdep_on();
  30447. + return err;
  30448. +}
  30449. +
  30450. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  30451. + struct file *h_file);
  30452. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  30453. +
  30454. +/* ---------------------------------------------------------------------- */
  30455. +
  30456. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  30457. +{
  30458. + loff_t err;
  30459. +
  30460. + lockdep_off();
  30461. + err = vfs_llseek(file, offset, origin);
  30462. + lockdep_on();
  30463. + return err;
  30464. +}
  30465. +
  30466. +/* ---------------------------------------------------------------------- */
  30467. +
  30468. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  30469. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  30470. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  30471. + struct inode **delegated_inode);
  30472. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  30473. + struct inode **delegated_inode);
  30474. +int vfsub_unlink(struct inode *dir, struct path *path,
  30475. + struct inode **delegated_inode, int force);
  30476. +
  30477. +/* ---------------------------------------------------------------------- */
  30478. +
  30479. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  30480. + const void *value, size_t size, int flags)
  30481. +{
  30482. + int err;
  30483. +
  30484. + lockdep_off();
  30485. + err = vfs_setxattr(dentry, name, value, size, flags);
  30486. + lockdep_on();
  30487. +
  30488. + return err;
  30489. +}
  30490. +
  30491. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  30492. +{
  30493. + int err;
  30494. +
  30495. + lockdep_off();
  30496. + err = vfs_removexattr(dentry, name);
  30497. + lockdep_on();
  30498. +
  30499. + return err;
  30500. +}
  30501. +
  30502. +#endif /* __KERNEL__ */
  30503. +#endif /* __AUFS_VFSUB_H__ */
  30504. diff -Naur null/fs/aufs/wbr_policy.c linux-4.7/fs/aufs/wbr_policy.c
  30505. --- /dev/null
  30506. +++ linux-4.7/fs/aufs/wbr_policy.c 2016-08-08 12:57:44.609384518 -0400
  30507. @@ -0,0 +1,765 @@
  30508. +/*
  30509. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  30510. + *
  30511. + * This program, aufs is free software; you can redistribute it and/or modify
  30512. + * it under the terms of the GNU General Public License as published by
  30513. + * the Free Software Foundation; either version 2 of the License, or
  30514. + * (at your option) any later version.
  30515. + *
  30516. + * This program is distributed in the hope that it will be useful,
  30517. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30518. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30519. + * GNU General Public License for more details.
  30520. + *
  30521. + * You should have received a copy of the GNU General Public License
  30522. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30523. + */
  30524. +
  30525. +/*
  30526. + * policies for selecting one among multiple writable branches
  30527. + */
  30528. +
  30529. +#include <linux/statfs.h>
  30530. +#include "aufs.h"
  30531. +
  30532. +/* subset of cpup_attr() */
  30533. +static noinline_for_stack
  30534. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  30535. +{
  30536. + int err, sbits;
  30537. + struct iattr ia;
  30538. + struct inode *h_isrc;
  30539. +
  30540. + h_isrc = d_inode(h_src);
  30541. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  30542. + ia.ia_mode = h_isrc->i_mode;
  30543. + ia.ia_uid = h_isrc->i_uid;
  30544. + ia.ia_gid = h_isrc->i_gid;
  30545. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  30546. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  30547. + /* no delegation since it is just created */
  30548. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  30549. +
  30550. + /* is this nfs only? */
  30551. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  30552. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  30553. + ia.ia_mode = h_isrc->i_mode;
  30554. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  30555. + }
  30556. +
  30557. + return err;
  30558. +}
  30559. +
  30560. +#define AuCpdown_PARENT_OPQ 1
  30561. +#define AuCpdown_WHED (1 << 1)
  30562. +#define AuCpdown_MADE_DIR (1 << 2)
  30563. +#define AuCpdown_DIROPQ (1 << 3)
  30564. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  30565. +#define au_fset_cpdown(flags, name) \
  30566. + do { (flags) |= AuCpdown_##name; } while (0)
  30567. +#define au_fclr_cpdown(flags, name) \
  30568. + do { (flags) &= ~AuCpdown_##name; } while (0)
  30569. +
  30570. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  30571. + unsigned int *flags)
  30572. +{
  30573. + int err;
  30574. + struct dentry *opq_dentry;
  30575. +
  30576. + opq_dentry = au_diropq_create(dentry, bdst);
  30577. + err = PTR_ERR(opq_dentry);
  30578. + if (IS_ERR(opq_dentry))
  30579. + goto out;
  30580. + dput(opq_dentry);
  30581. + au_fset_cpdown(*flags, DIROPQ);
  30582. +
  30583. +out:
  30584. + return err;
  30585. +}
  30586. +
  30587. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  30588. + struct inode *dir, aufs_bindex_t bdst)
  30589. +{
  30590. + int err;
  30591. + struct path h_path;
  30592. + struct au_branch *br;
  30593. +
  30594. + br = au_sbr(dentry->d_sb, bdst);
  30595. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  30596. + err = PTR_ERR(h_path.dentry);
  30597. + if (IS_ERR(h_path.dentry))
  30598. + goto out;
  30599. +
  30600. + err = 0;
  30601. + if (d_is_positive(h_path.dentry)) {
  30602. + h_path.mnt = au_br_mnt(br);
  30603. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  30604. + dentry);
  30605. + }
  30606. + dput(h_path.dentry);
  30607. +
  30608. +out:
  30609. + return err;
  30610. +}
  30611. +
  30612. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  30613. + struct au_pin *pin,
  30614. + struct dentry *h_parent, void *arg)
  30615. +{
  30616. + int err, rerr;
  30617. + aufs_bindex_t bopq, btop;
  30618. + struct path h_path;
  30619. + struct dentry *parent;
  30620. + struct inode *h_dir, *h_inode, *inode, *dir;
  30621. + unsigned int *flags = arg;
  30622. +
  30623. + btop = au_dbtop(dentry);
  30624. + /* dentry is di-locked */
  30625. + parent = dget_parent(dentry);
  30626. + dir = d_inode(parent);
  30627. + h_dir = d_inode(h_parent);
  30628. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  30629. + IMustLock(h_dir);
  30630. +
  30631. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  30632. + if (unlikely(err < 0))
  30633. + goto out;
  30634. + h_path.dentry = au_h_dptr(dentry, bdst);
  30635. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  30636. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
  30637. + S_IRWXU | S_IRUGO | S_IXUGO);
  30638. + if (unlikely(err))
  30639. + goto out_put;
  30640. + au_fset_cpdown(*flags, MADE_DIR);
  30641. +
  30642. + bopq = au_dbdiropq(dentry);
  30643. + au_fclr_cpdown(*flags, WHED);
  30644. + au_fclr_cpdown(*flags, DIROPQ);
  30645. + if (au_dbwh(dentry) == bdst)
  30646. + au_fset_cpdown(*flags, WHED);
  30647. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  30648. + au_fset_cpdown(*flags, PARENT_OPQ);
  30649. + h_inode = d_inode(h_path.dentry);
  30650. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  30651. + if (au_ftest_cpdown(*flags, WHED)) {
  30652. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  30653. + if (unlikely(err)) {
  30654. + inode_unlock(h_inode);
  30655. + goto out_dir;
  30656. + }
  30657. + }
  30658. +
  30659. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
  30660. + inode_unlock(h_inode);
  30661. + if (unlikely(err))
  30662. + goto out_opq;
  30663. +
  30664. + if (au_ftest_cpdown(*flags, WHED)) {
  30665. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  30666. + if (unlikely(err))
  30667. + goto out_opq;
  30668. + }
  30669. +
  30670. + inode = d_inode(dentry);
  30671. + if (au_ibbot(inode) < bdst)
  30672. + au_set_ibbot(inode, bdst);
  30673. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  30674. + au_hi_flags(inode, /*isdir*/1));
  30675. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  30676. + goto out; /* success */
  30677. +
  30678. + /* revert */
  30679. +out_opq:
  30680. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  30681. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  30682. + rerr = au_diropq_remove(dentry, bdst);
  30683. + inode_unlock(h_inode);
  30684. + if (unlikely(rerr)) {
  30685. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  30686. + dentry, bdst, rerr);
  30687. + err = -EIO;
  30688. + goto out;
  30689. + }
  30690. + }
  30691. +out_dir:
  30692. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  30693. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  30694. + if (unlikely(rerr)) {
  30695. + AuIOErr("failed removing %pd b%d (%d)\n",
  30696. + dentry, bdst, rerr);
  30697. + err = -EIO;
  30698. + }
  30699. + }
  30700. +out_put:
  30701. + au_set_h_dptr(dentry, bdst, NULL);
  30702. + if (au_dbbot(dentry) == bdst)
  30703. + au_update_dbbot(dentry);
  30704. +out:
  30705. + dput(parent);
  30706. + return err;
  30707. +}
  30708. +
  30709. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  30710. +{
  30711. + int err;
  30712. + unsigned int flags;
  30713. +
  30714. + flags = 0;
  30715. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  30716. +
  30717. + return err;
  30718. +}
  30719. +
  30720. +/* ---------------------------------------------------------------------- */
  30721. +
  30722. +/* policies for create */
  30723. +
  30724. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  30725. +{
  30726. + int err, i, j, ndentry;
  30727. + aufs_bindex_t bopq;
  30728. + struct au_dcsub_pages dpages;
  30729. + struct au_dpage *dpage;
  30730. + struct dentry **dentries, *parent, *d;
  30731. +
  30732. + err = au_dpages_init(&dpages, GFP_NOFS);
  30733. + if (unlikely(err))
  30734. + goto out;
  30735. + parent = dget_parent(dentry);
  30736. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  30737. + if (unlikely(err))
  30738. + goto out_free;
  30739. +
  30740. + err = bindex;
  30741. + for (i = 0; i < dpages.ndpage; i++) {
  30742. + dpage = dpages.dpages + i;
  30743. + dentries = dpage->dentries;
  30744. + ndentry = dpage->ndentry;
  30745. + for (j = 0; j < ndentry; j++) {
  30746. + d = dentries[j];
  30747. + di_read_lock_parent2(d, !AuLock_IR);
  30748. + bopq = au_dbdiropq(d);
  30749. + di_read_unlock(d, !AuLock_IR);
  30750. + if (bopq >= 0 && bopq < err)
  30751. + err = bopq;
  30752. + }
  30753. + }
  30754. +
  30755. +out_free:
  30756. + dput(parent);
  30757. + au_dpages_free(&dpages);
  30758. +out:
  30759. + return err;
  30760. +}
  30761. +
  30762. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  30763. +{
  30764. + for (; bindex >= 0; bindex--)
  30765. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  30766. + return bindex;
  30767. + return -EROFS;
  30768. +}
  30769. +
  30770. +/* top down parent */
  30771. +static int au_wbr_create_tdp(struct dentry *dentry,
  30772. + unsigned int flags __maybe_unused)
  30773. +{
  30774. + int err;
  30775. + aufs_bindex_t btop, bindex;
  30776. + struct super_block *sb;
  30777. + struct dentry *parent, *h_parent;
  30778. +
  30779. + sb = dentry->d_sb;
  30780. + btop = au_dbtop(dentry);
  30781. + err = btop;
  30782. + if (!au_br_rdonly(au_sbr(sb, btop)))
  30783. + goto out;
  30784. +
  30785. + err = -EROFS;
  30786. + parent = dget_parent(dentry);
  30787. + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
  30788. + h_parent = au_h_dptr(parent, bindex);
  30789. + if (!h_parent || d_is_negative(h_parent))
  30790. + continue;
  30791. +
  30792. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  30793. + err = bindex;
  30794. + break;
  30795. + }
  30796. + }
  30797. + dput(parent);
  30798. +
  30799. + /* bottom up here */
  30800. + if (unlikely(err < 0)) {
  30801. + err = au_wbr_bu(sb, btop - 1);
  30802. + if (err >= 0)
  30803. + err = au_wbr_nonopq(dentry, err);
  30804. + }
  30805. +
  30806. +out:
  30807. + AuDbg("b%d\n", err);
  30808. + return err;
  30809. +}
  30810. +
  30811. +/* ---------------------------------------------------------------------- */
  30812. +
  30813. +/* an exception for the policy other than tdp */
  30814. +static int au_wbr_create_exp(struct dentry *dentry)
  30815. +{
  30816. + int err;
  30817. + aufs_bindex_t bwh, bdiropq;
  30818. + struct dentry *parent;
  30819. +
  30820. + err = -1;
  30821. + bwh = au_dbwh(dentry);
  30822. + parent = dget_parent(dentry);
  30823. + bdiropq = au_dbdiropq(parent);
  30824. + if (bwh >= 0) {
  30825. + if (bdiropq >= 0)
  30826. + err = min(bdiropq, bwh);
  30827. + else
  30828. + err = bwh;
  30829. + AuDbg("%d\n", err);
  30830. + } else if (bdiropq >= 0) {
  30831. + err = bdiropq;
  30832. + AuDbg("%d\n", err);
  30833. + }
  30834. + dput(parent);
  30835. +
  30836. + if (err >= 0)
  30837. + err = au_wbr_nonopq(dentry, err);
  30838. +
  30839. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  30840. + err = -1;
  30841. +
  30842. + AuDbg("%d\n", err);
  30843. + return err;
  30844. +}
  30845. +
  30846. +/* ---------------------------------------------------------------------- */
  30847. +
  30848. +/* round robin */
  30849. +static int au_wbr_create_init_rr(struct super_block *sb)
  30850. +{
  30851. + int err;
  30852. +
  30853. + err = au_wbr_bu(sb, au_sbbot(sb));
  30854. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  30855. + /* smp_mb(); */
  30856. +
  30857. + AuDbg("b%d\n", err);
  30858. + return err;
  30859. +}
  30860. +
  30861. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  30862. +{
  30863. + int err, nbr;
  30864. + unsigned int u;
  30865. + aufs_bindex_t bindex, bbot;
  30866. + struct super_block *sb;
  30867. + atomic_t *next;
  30868. +
  30869. + err = au_wbr_create_exp(dentry);
  30870. + if (err >= 0)
  30871. + goto out;
  30872. +
  30873. + sb = dentry->d_sb;
  30874. + next = &au_sbi(sb)->si_wbr_rr_next;
  30875. + bbot = au_sbbot(sb);
  30876. + nbr = bbot + 1;
  30877. + for (bindex = 0; bindex <= bbot; bindex++) {
  30878. + if (!au_ftest_wbr(flags, DIR)) {
  30879. + err = atomic_dec_return(next) + 1;
  30880. + /* modulo for 0 is meaningless */
  30881. + if (unlikely(!err))
  30882. + err = atomic_dec_return(next) + 1;
  30883. + } else
  30884. + err = atomic_read(next);
  30885. + AuDbg("%d\n", err);
  30886. + u = err;
  30887. + err = u % nbr;
  30888. + AuDbg("%d\n", err);
  30889. + if (!au_br_rdonly(au_sbr(sb, err)))
  30890. + break;
  30891. + err = -EROFS;
  30892. + }
  30893. +
  30894. + if (err >= 0)
  30895. + err = au_wbr_nonopq(dentry, err);
  30896. +
  30897. +out:
  30898. + AuDbg("%d\n", err);
  30899. + return err;
  30900. +}
  30901. +
  30902. +/* ---------------------------------------------------------------------- */
  30903. +
  30904. +/* most free space */
  30905. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  30906. +{
  30907. + struct super_block *sb;
  30908. + struct au_branch *br;
  30909. + struct au_wbr_mfs *mfs;
  30910. + struct dentry *h_parent;
  30911. + aufs_bindex_t bindex, bbot;
  30912. + int err;
  30913. + unsigned long long b, bavail;
  30914. + struct path h_path;
  30915. + /* reduce the stack usage */
  30916. + struct kstatfs *st;
  30917. +
  30918. + st = kmalloc(sizeof(*st), GFP_NOFS);
  30919. + if (unlikely(!st)) {
  30920. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  30921. + return;
  30922. + }
  30923. +
  30924. + bavail = 0;
  30925. + sb = dentry->d_sb;
  30926. + mfs = &au_sbi(sb)->si_wbr_mfs;
  30927. + MtxMustLock(&mfs->mfs_lock);
  30928. + mfs->mfs_bindex = -EROFS;
  30929. + mfs->mfsrr_bytes = 0;
  30930. + if (!parent) {
  30931. + bindex = 0;
  30932. + bbot = au_sbbot(sb);
  30933. + } else {
  30934. + bindex = au_dbtop(parent);
  30935. + bbot = au_dbtaildir(parent);
  30936. + }
  30937. +
  30938. + for (; bindex <= bbot; bindex++) {
  30939. + if (parent) {
  30940. + h_parent = au_h_dptr(parent, bindex);
  30941. + if (!h_parent || d_is_negative(h_parent))
  30942. + continue;
  30943. + }
  30944. + br = au_sbr(sb, bindex);
  30945. + if (au_br_rdonly(br))
  30946. + continue;
  30947. +
  30948. + /* sb->s_root for NFS is unreliable */
  30949. + h_path.mnt = au_br_mnt(br);
  30950. + h_path.dentry = h_path.mnt->mnt_root;
  30951. + err = vfs_statfs(&h_path, st);
  30952. + if (unlikely(err)) {
  30953. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  30954. + continue;
  30955. + }
  30956. +
  30957. + /* when the available size is equal, select the lower one */
  30958. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  30959. + || sizeof(b) < sizeof(st->f_bsize));
  30960. + b = st->f_bavail * st->f_bsize;
  30961. + br->br_wbr->wbr_bytes = b;
  30962. + if (b >= bavail) {
  30963. + bavail = b;
  30964. + mfs->mfs_bindex = bindex;
  30965. + mfs->mfs_jiffy = jiffies;
  30966. + }
  30967. + }
  30968. +
  30969. + mfs->mfsrr_bytes = bavail;
  30970. + AuDbg("b%d\n", mfs->mfs_bindex);
  30971. + au_delayed_kfree(st);
  30972. +}
  30973. +
  30974. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  30975. +{
  30976. + int err;
  30977. + struct dentry *parent;
  30978. + struct super_block *sb;
  30979. + struct au_wbr_mfs *mfs;
  30980. +
  30981. + err = au_wbr_create_exp(dentry);
  30982. + if (err >= 0)
  30983. + goto out;
  30984. +
  30985. + sb = dentry->d_sb;
  30986. + parent = NULL;
  30987. + if (au_ftest_wbr(flags, PARENT))
  30988. + parent = dget_parent(dentry);
  30989. + mfs = &au_sbi(sb)->si_wbr_mfs;
  30990. + mutex_lock(&mfs->mfs_lock);
  30991. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  30992. + || mfs->mfs_bindex < 0
  30993. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  30994. + au_mfs(dentry, parent);
  30995. + mutex_unlock(&mfs->mfs_lock);
  30996. + err = mfs->mfs_bindex;
  30997. + dput(parent);
  30998. +
  30999. + if (err >= 0)
  31000. + err = au_wbr_nonopq(dentry, err);
  31001. +
  31002. +out:
  31003. + AuDbg("b%d\n", err);
  31004. + return err;
  31005. +}
  31006. +
  31007. +static int au_wbr_create_init_mfs(struct super_block *sb)
  31008. +{
  31009. + struct au_wbr_mfs *mfs;
  31010. +
  31011. + mfs = &au_sbi(sb)->si_wbr_mfs;
  31012. + mutex_init(&mfs->mfs_lock);
  31013. + mfs->mfs_jiffy = 0;
  31014. + mfs->mfs_bindex = -EROFS;
  31015. +
  31016. + return 0;
  31017. +}
  31018. +
  31019. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  31020. +{
  31021. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  31022. + return 0;
  31023. +}
  31024. +
  31025. +/* ---------------------------------------------------------------------- */
  31026. +
  31027. +/* most free space and then round robin */
  31028. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  31029. +{
  31030. + int err;
  31031. + struct au_wbr_mfs *mfs;
  31032. +
  31033. + err = au_wbr_create_mfs(dentry, flags);
  31034. + if (err >= 0) {
  31035. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  31036. + mutex_lock(&mfs->mfs_lock);
  31037. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  31038. + err = au_wbr_create_rr(dentry, flags);
  31039. + mutex_unlock(&mfs->mfs_lock);
  31040. + }
  31041. +
  31042. + AuDbg("b%d\n", err);
  31043. + return err;
  31044. +}
  31045. +
  31046. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  31047. +{
  31048. + int err;
  31049. +
  31050. + au_wbr_create_init_mfs(sb); /* ignore */
  31051. + err = au_wbr_create_init_rr(sb);
  31052. +
  31053. + return err;
  31054. +}
  31055. +
  31056. +/* ---------------------------------------------------------------------- */
  31057. +
  31058. +/* top down parent and most free space */
  31059. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  31060. +{
  31061. + int err, e2;
  31062. + unsigned long long b;
  31063. + aufs_bindex_t bindex, btop, bbot;
  31064. + struct super_block *sb;
  31065. + struct dentry *parent, *h_parent;
  31066. + struct au_branch *br;
  31067. +
  31068. + err = au_wbr_create_tdp(dentry, flags);
  31069. + if (unlikely(err < 0))
  31070. + goto out;
  31071. + parent = dget_parent(dentry);
  31072. + btop = au_dbtop(parent);
  31073. + bbot = au_dbtaildir(parent);
  31074. + if (btop == bbot)
  31075. + goto out_parent; /* success */
  31076. +
  31077. + e2 = au_wbr_create_mfs(dentry, flags);
  31078. + if (e2 < 0)
  31079. + goto out_parent; /* success */
  31080. +
  31081. + /* when the available size is equal, select upper one */
  31082. + sb = dentry->d_sb;
  31083. + br = au_sbr(sb, err);
  31084. + b = br->br_wbr->wbr_bytes;
  31085. + AuDbg("b%d, %llu\n", err, b);
  31086. +
  31087. + for (bindex = btop; bindex <= bbot; bindex++) {
  31088. + h_parent = au_h_dptr(parent, bindex);
  31089. + if (!h_parent || d_is_negative(h_parent))
  31090. + continue;
  31091. +
  31092. + br = au_sbr(sb, bindex);
  31093. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  31094. + b = br->br_wbr->wbr_bytes;
  31095. + err = bindex;
  31096. + AuDbg("b%d, %llu\n", err, b);
  31097. + }
  31098. + }
  31099. +
  31100. + if (err >= 0)
  31101. + err = au_wbr_nonopq(dentry, err);
  31102. +
  31103. +out_parent:
  31104. + dput(parent);
  31105. +out:
  31106. + AuDbg("b%d\n", err);
  31107. + return err;
  31108. +}
  31109. +
  31110. +/* ---------------------------------------------------------------------- */
  31111. +
  31112. +/*
  31113. + * - top down parent
  31114. + * - most free space with parent
  31115. + * - most free space round-robin regardless parent
  31116. + */
  31117. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  31118. +{
  31119. + int err;
  31120. + unsigned long long watermark;
  31121. + struct super_block *sb;
  31122. + struct au_branch *br;
  31123. + struct au_wbr_mfs *mfs;
  31124. +
  31125. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  31126. + if (unlikely(err < 0))
  31127. + goto out;
  31128. +
  31129. + sb = dentry->d_sb;
  31130. + br = au_sbr(sb, err);
  31131. + mfs = &au_sbi(sb)->si_wbr_mfs;
  31132. + mutex_lock(&mfs->mfs_lock);
  31133. + watermark = mfs->mfsrr_watermark;
  31134. + mutex_unlock(&mfs->mfs_lock);
  31135. + if (br->br_wbr->wbr_bytes < watermark)
  31136. + /* regardless the parent dir */
  31137. + err = au_wbr_create_mfsrr(dentry, flags);
  31138. +
  31139. +out:
  31140. + AuDbg("b%d\n", err);
  31141. + return err;
  31142. +}
  31143. +
  31144. +/* ---------------------------------------------------------------------- */
  31145. +
  31146. +/* policies for copyup */
  31147. +
  31148. +/* top down parent */
  31149. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  31150. +{
  31151. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  31152. +}
  31153. +
  31154. +/* bottom up parent */
  31155. +static int au_wbr_copyup_bup(struct dentry *dentry)
  31156. +{
  31157. + int err;
  31158. + aufs_bindex_t bindex, btop;
  31159. + struct dentry *parent, *h_parent;
  31160. + struct super_block *sb;
  31161. +
  31162. + err = -EROFS;
  31163. + sb = dentry->d_sb;
  31164. + parent = dget_parent(dentry);
  31165. + btop = au_dbtop(parent);
  31166. + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
  31167. + h_parent = au_h_dptr(parent, bindex);
  31168. + if (!h_parent || d_is_negative(h_parent))
  31169. + continue;
  31170. +
  31171. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  31172. + err = bindex;
  31173. + break;
  31174. + }
  31175. + }
  31176. + dput(parent);
  31177. +
  31178. + /* bottom up here */
  31179. + if (unlikely(err < 0))
  31180. + err = au_wbr_bu(sb, btop - 1);
  31181. +
  31182. + AuDbg("b%d\n", err);
  31183. + return err;
  31184. +}
  31185. +
  31186. +/* bottom up */
  31187. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
  31188. +{
  31189. + int err;
  31190. +
  31191. + err = au_wbr_bu(dentry->d_sb, btop);
  31192. + AuDbg("b%d\n", err);
  31193. + if (err > btop)
  31194. + err = au_wbr_nonopq(dentry, err);
  31195. +
  31196. + AuDbg("b%d\n", err);
  31197. + return err;
  31198. +}
  31199. +
  31200. +static int au_wbr_copyup_bu(struct dentry *dentry)
  31201. +{
  31202. + int err;
  31203. + aufs_bindex_t btop;
  31204. +
  31205. + btop = au_dbtop(dentry);
  31206. + err = au_wbr_do_copyup_bu(dentry, btop);
  31207. + return err;
  31208. +}
  31209. +
  31210. +/* ---------------------------------------------------------------------- */
  31211. +
  31212. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  31213. + [AuWbrCopyup_TDP] = {
  31214. + .copyup = au_wbr_copyup_tdp
  31215. + },
  31216. + [AuWbrCopyup_BUP] = {
  31217. + .copyup = au_wbr_copyup_bup
  31218. + },
  31219. + [AuWbrCopyup_BU] = {
  31220. + .copyup = au_wbr_copyup_bu
  31221. + }
  31222. +};
  31223. +
  31224. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  31225. + [AuWbrCreate_TDP] = {
  31226. + .create = au_wbr_create_tdp
  31227. + },
  31228. + [AuWbrCreate_RR] = {
  31229. + .create = au_wbr_create_rr,
  31230. + .init = au_wbr_create_init_rr
  31231. + },
  31232. + [AuWbrCreate_MFS] = {
  31233. + .create = au_wbr_create_mfs,
  31234. + .init = au_wbr_create_init_mfs,
  31235. + .fin = au_wbr_create_fin_mfs
  31236. + },
  31237. + [AuWbrCreate_MFSV] = {
  31238. + .create = au_wbr_create_mfs,
  31239. + .init = au_wbr_create_init_mfs,
  31240. + .fin = au_wbr_create_fin_mfs
  31241. + },
  31242. + [AuWbrCreate_MFSRR] = {
  31243. + .create = au_wbr_create_mfsrr,
  31244. + .init = au_wbr_create_init_mfsrr,
  31245. + .fin = au_wbr_create_fin_mfs
  31246. + },
  31247. + [AuWbrCreate_MFSRRV] = {
  31248. + .create = au_wbr_create_mfsrr,
  31249. + .init = au_wbr_create_init_mfsrr,
  31250. + .fin = au_wbr_create_fin_mfs
  31251. + },
  31252. + [AuWbrCreate_PMFS] = {
  31253. + .create = au_wbr_create_pmfs,
  31254. + .init = au_wbr_create_init_mfs,
  31255. + .fin = au_wbr_create_fin_mfs
  31256. + },
  31257. + [AuWbrCreate_PMFSV] = {
  31258. + .create = au_wbr_create_pmfs,
  31259. + .init = au_wbr_create_init_mfs,
  31260. + .fin = au_wbr_create_fin_mfs
  31261. + },
  31262. + [AuWbrCreate_PMFSRR] = {
  31263. + .create = au_wbr_create_pmfsrr,
  31264. + .init = au_wbr_create_init_mfsrr,
  31265. + .fin = au_wbr_create_fin_mfs
  31266. + },
  31267. + [AuWbrCreate_PMFSRRV] = {
  31268. + .create = au_wbr_create_pmfsrr,
  31269. + .init = au_wbr_create_init_mfsrr,
  31270. + .fin = au_wbr_create_fin_mfs
  31271. + }
  31272. +};
  31273. diff -Naur null/fs/aufs/whout.c linux-4.7/fs/aufs/whout.c
  31274. --- /dev/null
  31275. +++ linux-4.7/fs/aufs/whout.c 2016-08-08 12:57:44.610384496 -0400
  31276. @@ -0,0 +1,1060 @@
  31277. +/*
  31278. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  31279. + *
  31280. + * This program, aufs is free software; you can redistribute it and/or modify
  31281. + * it under the terms of the GNU General Public License as published by
  31282. + * the Free Software Foundation; either version 2 of the License, or
  31283. + * (at your option) any later version.
  31284. + *
  31285. + * This program is distributed in the hope that it will be useful,
  31286. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31287. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31288. + * GNU General Public License for more details.
  31289. + *
  31290. + * You should have received a copy of the GNU General Public License
  31291. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31292. + */
  31293. +
  31294. +/*
  31295. + * whiteout for logical deletion and opaque directory
  31296. + */
  31297. +
  31298. +#include "aufs.h"
  31299. +
  31300. +#define WH_MASK S_IRUGO
  31301. +
  31302. +/*
  31303. + * If a directory contains this file, then it is opaque. We start with the
  31304. + * .wh. flag so that it is blocked by lookup.
  31305. + */
  31306. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  31307. + sizeof(AUFS_WH_DIROPQ) - 1);
  31308. +
  31309. +/*
  31310. + * generate whiteout name, which is NOT terminated by NULL.
  31311. + * @name: original d_name.name
  31312. + * @len: original d_name.len
  31313. + * @wh: whiteout qstr
  31314. + * returns zero when succeeds, otherwise error.
  31315. + * succeeded value as wh->name should be freed by kfree().
  31316. + */
  31317. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  31318. +{
  31319. + char *p;
  31320. +
  31321. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  31322. + return -ENAMETOOLONG;
  31323. +
  31324. + wh->len = name->len + AUFS_WH_PFX_LEN;
  31325. + p = kmalloc(wh->len, GFP_NOFS);
  31326. + wh->name = p;
  31327. + if (p) {
  31328. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31329. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  31330. + /* smp_mb(); */
  31331. + return 0;
  31332. + }
  31333. + return -ENOMEM;
  31334. +}
  31335. +
  31336. +/* ---------------------------------------------------------------------- */
  31337. +
  31338. +/*
  31339. + * test if the @wh_name exists under @h_parent.
  31340. + * @try_sio specifies the necessary of super-io.
  31341. + */
  31342. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  31343. +{
  31344. + int err;
  31345. + struct dentry *wh_dentry;
  31346. +
  31347. + if (!try_sio)
  31348. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  31349. + else
  31350. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  31351. + err = PTR_ERR(wh_dentry);
  31352. + if (IS_ERR(wh_dentry)) {
  31353. + if (err == -ENAMETOOLONG)
  31354. + err = 0;
  31355. + goto out;
  31356. + }
  31357. +
  31358. + err = 0;
  31359. + if (d_is_negative(wh_dentry))
  31360. + goto out_wh; /* success */
  31361. +
  31362. + err = 1;
  31363. + if (d_is_reg(wh_dentry))
  31364. + goto out_wh; /* success */
  31365. +
  31366. + err = -EIO;
  31367. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  31368. + wh_dentry, d_inode(wh_dentry)->i_mode);
  31369. +
  31370. +out_wh:
  31371. + dput(wh_dentry);
  31372. +out:
  31373. + return err;
  31374. +}
  31375. +
  31376. +/*
  31377. + * test if the @h_dentry sets opaque or not.
  31378. + */
  31379. +int au_diropq_test(struct dentry *h_dentry)
  31380. +{
  31381. + int err;
  31382. + struct inode *h_dir;
  31383. +
  31384. + h_dir = d_inode(h_dentry);
  31385. + err = au_wh_test(h_dentry, &diropq_name,
  31386. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  31387. + return err;
  31388. +}
  31389. +
  31390. +/*
  31391. + * returns a negative dentry whose name is unique and temporary.
  31392. + */
  31393. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  31394. + struct qstr *prefix)
  31395. +{
  31396. + struct dentry *dentry;
  31397. + int i;
  31398. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  31399. + *name, *p;
  31400. + /* strict atomic_t is unnecessary here */
  31401. + static unsigned short cnt;
  31402. + struct qstr qs;
  31403. +
  31404. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  31405. +
  31406. + name = defname;
  31407. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  31408. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  31409. + dentry = ERR_PTR(-ENAMETOOLONG);
  31410. + if (unlikely(qs.len > NAME_MAX))
  31411. + goto out;
  31412. + dentry = ERR_PTR(-ENOMEM);
  31413. + name = kmalloc(qs.len + 1, GFP_NOFS);
  31414. + if (unlikely(!name))
  31415. + goto out;
  31416. + }
  31417. +
  31418. + /* doubly whiteout-ed */
  31419. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  31420. + p = name + AUFS_WH_PFX_LEN * 2;
  31421. + memcpy(p, prefix->name, prefix->len);
  31422. + p += prefix->len;
  31423. + *p++ = '.';
  31424. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  31425. +
  31426. + qs.name = name;
  31427. + for (i = 0; i < 3; i++) {
  31428. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  31429. + dentry = au_sio_lkup_one(&qs, h_parent);
  31430. + if (IS_ERR(dentry) || d_is_negative(dentry))
  31431. + goto out_name;
  31432. + dput(dentry);
  31433. + }
  31434. + /* pr_warn("could not get random name\n"); */
  31435. + dentry = ERR_PTR(-EEXIST);
  31436. + AuDbg("%.*s\n", AuLNPair(&qs));
  31437. + BUG();
  31438. +
  31439. +out_name:
  31440. + if (name != defname)
  31441. + au_delayed_kfree(name);
  31442. +out:
  31443. + AuTraceErrPtr(dentry);
  31444. + return dentry;
  31445. +}
  31446. +
  31447. +/*
  31448. + * rename the @h_dentry on @br to the whiteouted temporary name.
  31449. + */
  31450. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  31451. +{
  31452. + int err;
  31453. + struct path h_path = {
  31454. + .mnt = au_br_mnt(br)
  31455. + };
  31456. + struct inode *h_dir, *delegated;
  31457. + struct dentry *h_parent;
  31458. +
  31459. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  31460. + h_dir = d_inode(h_parent);
  31461. + IMustLock(h_dir);
  31462. +
  31463. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  31464. + err = PTR_ERR(h_path.dentry);
  31465. + if (IS_ERR(h_path.dentry))
  31466. + goto out;
  31467. +
  31468. + /* under the same dir, no need to lock_rename() */
  31469. + delegated = NULL;
  31470. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
  31471. + AuTraceErr(err);
  31472. + if (unlikely(err == -EWOULDBLOCK)) {
  31473. + pr_warn("cannot retry for NFSv4 delegation"
  31474. + " for an internal rename\n");
  31475. + iput(delegated);
  31476. + }
  31477. + dput(h_path.dentry);
  31478. +
  31479. +out:
  31480. + AuTraceErr(err);
  31481. + return err;
  31482. +}
  31483. +
  31484. +/* ---------------------------------------------------------------------- */
  31485. +/*
  31486. + * functions for removing a whiteout
  31487. + */
  31488. +
  31489. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  31490. +{
  31491. + int err, force;
  31492. + struct inode *delegated;
  31493. +
  31494. + /*
  31495. + * forces superio when the dir has a sticky bit.
  31496. + * this may be a violation of unix fs semantics.
  31497. + */
  31498. + force = (h_dir->i_mode & S_ISVTX)
  31499. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  31500. + delegated = NULL;
  31501. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  31502. + if (unlikely(err == -EWOULDBLOCK)) {
  31503. + pr_warn("cannot retry for NFSv4 delegation"
  31504. + " for an internal unlink\n");
  31505. + iput(delegated);
  31506. + }
  31507. + return err;
  31508. +}
  31509. +
  31510. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  31511. + struct dentry *dentry)
  31512. +{
  31513. + int err;
  31514. +
  31515. + err = do_unlink_wh(h_dir, h_path);
  31516. + if (!err && dentry)
  31517. + au_set_dbwh(dentry, -1);
  31518. +
  31519. + return err;
  31520. +}
  31521. +
  31522. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  31523. + struct au_branch *br)
  31524. +{
  31525. + int err;
  31526. + struct path h_path = {
  31527. + .mnt = au_br_mnt(br)
  31528. + };
  31529. +
  31530. + err = 0;
  31531. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  31532. + if (IS_ERR(h_path.dentry))
  31533. + err = PTR_ERR(h_path.dentry);
  31534. + else {
  31535. + if (d_is_reg(h_path.dentry))
  31536. + err = do_unlink_wh(d_inode(h_parent), &h_path);
  31537. + dput(h_path.dentry);
  31538. + }
  31539. +
  31540. + return err;
  31541. +}
  31542. +
  31543. +/* ---------------------------------------------------------------------- */
  31544. +/*
  31545. + * initialize/clean whiteout for a branch
  31546. + */
  31547. +
  31548. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  31549. + const int isdir)
  31550. +{
  31551. + int err;
  31552. + struct inode *delegated;
  31553. +
  31554. + if (d_is_negative(whpath->dentry))
  31555. + return;
  31556. +
  31557. + if (isdir)
  31558. + err = vfsub_rmdir(h_dir, whpath);
  31559. + else {
  31560. + delegated = NULL;
  31561. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  31562. + if (unlikely(err == -EWOULDBLOCK)) {
  31563. + pr_warn("cannot retry for NFSv4 delegation"
  31564. + " for an internal unlink\n");
  31565. + iput(delegated);
  31566. + }
  31567. + }
  31568. + if (unlikely(err))
  31569. + pr_warn("failed removing %pd (%d), ignored.\n",
  31570. + whpath->dentry, err);
  31571. +}
  31572. +
  31573. +static int test_linkable(struct dentry *h_root)
  31574. +{
  31575. + struct inode *h_dir = d_inode(h_root);
  31576. +
  31577. + if (h_dir->i_op->link)
  31578. + return 0;
  31579. +
  31580. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  31581. + h_root, au_sbtype(h_root->d_sb));
  31582. + return -ENOSYS;
  31583. +}
  31584. +
  31585. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  31586. +static int au_whdir(struct inode *h_dir, struct path *path)
  31587. +{
  31588. + int err;
  31589. +
  31590. + err = -EEXIST;
  31591. + if (d_is_negative(path->dentry)) {
  31592. + int mode = S_IRWXU;
  31593. +
  31594. + if (au_test_nfs(path->dentry->d_sb))
  31595. + mode |= S_IXUGO;
  31596. + err = vfsub_mkdir(h_dir, path, mode);
  31597. + } else if (d_is_dir(path->dentry))
  31598. + err = 0;
  31599. + else
  31600. + pr_err("unknown %pd exists\n", path->dentry);
  31601. +
  31602. + return err;
  31603. +}
  31604. +
  31605. +struct au_wh_base {
  31606. + const struct qstr *name;
  31607. + struct dentry *dentry;
  31608. +};
  31609. +
  31610. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  31611. + struct path *h_path)
  31612. +{
  31613. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31614. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  31615. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31616. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31617. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31618. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31619. +}
  31620. +
  31621. +/*
  31622. + * returns tri-state,
  31623. + * minus: error, caller should print the message
  31624. + * zero: succuess
  31625. + * plus: error, caller should NOT print the message
  31626. + */
  31627. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  31628. + int do_plink, struct au_wh_base base[],
  31629. + struct path *h_path)
  31630. +{
  31631. + int err;
  31632. + struct inode *h_dir;
  31633. +
  31634. + h_dir = d_inode(h_root);
  31635. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31636. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  31637. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31638. + if (do_plink) {
  31639. + err = test_linkable(h_root);
  31640. + if (unlikely(err)) {
  31641. + err = 1;
  31642. + goto out;
  31643. + }
  31644. +
  31645. + err = au_whdir(h_dir, h_path);
  31646. + if (unlikely(err))
  31647. + goto out;
  31648. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  31649. + } else
  31650. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31651. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31652. + err = au_whdir(h_dir, h_path);
  31653. + if (unlikely(err))
  31654. + goto out;
  31655. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  31656. +
  31657. +out:
  31658. + return err;
  31659. +}
  31660. +
  31661. +/*
  31662. + * for the moment, aufs supports the branch filesystem which does not support
  31663. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  31664. + * copyup failed. finally, such filesystem will not be used as the writable
  31665. + * branch.
  31666. + *
  31667. + * returns tri-state, see above.
  31668. + */
  31669. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  31670. + int do_plink, struct au_wh_base base[],
  31671. + struct path *h_path)
  31672. +{
  31673. + int err;
  31674. + struct inode *h_dir;
  31675. +
  31676. + WbrWhMustWriteLock(wbr);
  31677. +
  31678. + err = test_linkable(h_root);
  31679. + if (unlikely(err)) {
  31680. + err = 1;
  31681. + goto out;
  31682. + }
  31683. +
  31684. + /*
  31685. + * todo: should this create be done in /sbin/mount.aufs helper?
  31686. + */
  31687. + err = -EEXIST;
  31688. + h_dir = d_inode(h_root);
  31689. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  31690. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31691. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  31692. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  31693. + err = 0;
  31694. + else
  31695. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  31696. + if (unlikely(err))
  31697. + goto out;
  31698. +
  31699. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31700. + if (do_plink) {
  31701. + err = au_whdir(h_dir, h_path);
  31702. + if (unlikely(err))
  31703. + goto out;
  31704. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  31705. + } else
  31706. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31707. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  31708. +
  31709. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31710. + err = au_whdir(h_dir, h_path);
  31711. + if (unlikely(err))
  31712. + goto out;
  31713. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  31714. +
  31715. +out:
  31716. + return err;
  31717. +}
  31718. +
  31719. +/*
  31720. + * initialize the whiteout base file/dir for @br.
  31721. + */
  31722. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  31723. +{
  31724. + int err, i;
  31725. + const unsigned char do_plink
  31726. + = !!au_opt_test(au_mntflags(sb), PLINK);
  31727. + struct inode *h_dir;
  31728. + struct path path = br->br_path;
  31729. + struct dentry *h_root = path.dentry;
  31730. + struct au_wbr *wbr = br->br_wbr;
  31731. + static const struct qstr base_name[] = {
  31732. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  31733. + sizeof(AUFS_BASE_NAME) - 1),
  31734. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  31735. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  31736. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  31737. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  31738. + };
  31739. + struct au_wh_base base[] = {
  31740. + [AuBrWh_BASE] = {
  31741. + .name = base_name + AuBrWh_BASE,
  31742. + .dentry = NULL
  31743. + },
  31744. + [AuBrWh_PLINK] = {
  31745. + .name = base_name + AuBrWh_PLINK,
  31746. + .dentry = NULL
  31747. + },
  31748. + [AuBrWh_ORPH] = {
  31749. + .name = base_name + AuBrWh_ORPH,
  31750. + .dentry = NULL
  31751. + }
  31752. + };
  31753. +
  31754. + if (wbr)
  31755. + WbrWhMustWriteLock(wbr);
  31756. +
  31757. + for (i = 0; i < AuBrWh_Last; i++) {
  31758. + /* doubly whiteouted */
  31759. + struct dentry *d;
  31760. +
  31761. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  31762. + err = PTR_ERR(d);
  31763. + if (IS_ERR(d))
  31764. + goto out;
  31765. +
  31766. + base[i].dentry = d;
  31767. + AuDebugOn(wbr
  31768. + && wbr->wbr_wh[i]
  31769. + && wbr->wbr_wh[i] != base[i].dentry);
  31770. + }
  31771. +
  31772. + if (wbr)
  31773. + for (i = 0; i < AuBrWh_Last; i++) {
  31774. + dput(wbr->wbr_wh[i]);
  31775. + wbr->wbr_wh[i] = NULL;
  31776. + }
  31777. +
  31778. + err = 0;
  31779. + if (!au_br_writable(br->br_perm)) {
  31780. + h_dir = d_inode(h_root);
  31781. + au_wh_init_ro(h_dir, base, &path);
  31782. + } else if (!au_br_wh_linkable(br->br_perm)) {
  31783. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  31784. + if (err > 0)
  31785. + goto out;
  31786. + else if (err)
  31787. + goto out_err;
  31788. + } else {
  31789. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  31790. + if (err > 0)
  31791. + goto out;
  31792. + else if (err)
  31793. + goto out_err;
  31794. + }
  31795. + goto out; /* success */
  31796. +
  31797. +out_err:
  31798. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  31799. + err, h_root, au_sbtype(h_root->d_sb));
  31800. +out:
  31801. + for (i = 0; i < AuBrWh_Last; i++)
  31802. + dput(base[i].dentry);
  31803. + return err;
  31804. +}
  31805. +
  31806. +/* ---------------------------------------------------------------------- */
  31807. +/*
  31808. + * whiteouts are all hard-linked usually.
  31809. + * when its link count reaches a ceiling, we create a new whiteout base
  31810. + * asynchronously.
  31811. + */
  31812. +
  31813. +struct reinit_br_wh {
  31814. + struct super_block *sb;
  31815. + struct au_branch *br;
  31816. +};
  31817. +
  31818. +static void reinit_br_wh(void *arg)
  31819. +{
  31820. + int err;
  31821. + aufs_bindex_t bindex;
  31822. + struct path h_path;
  31823. + struct reinit_br_wh *a = arg;
  31824. + struct au_wbr *wbr;
  31825. + struct inode *dir, *delegated;
  31826. + struct dentry *h_root;
  31827. + struct au_hinode *hdir;
  31828. +
  31829. + err = 0;
  31830. + wbr = a->br->br_wbr;
  31831. + /* big aufs lock */
  31832. + si_noflush_write_lock(a->sb);
  31833. + if (!au_br_writable(a->br->br_perm))
  31834. + goto out;
  31835. + bindex = au_br_index(a->sb, a->br->br_id);
  31836. + if (unlikely(bindex < 0))
  31837. + goto out;
  31838. +
  31839. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  31840. + dir = d_inode(a->sb->s_root);
  31841. + hdir = au_hi(dir, bindex);
  31842. + h_root = au_h_dptr(a->sb->s_root, bindex);
  31843. + AuDebugOn(h_root != au_br_dentry(a->br));
  31844. +
  31845. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  31846. + wbr_wh_write_lock(wbr);
  31847. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  31848. + h_root, a->br);
  31849. + if (!err) {
  31850. + h_path.dentry = wbr->wbr_whbase;
  31851. + h_path.mnt = au_br_mnt(a->br);
  31852. + delegated = NULL;
  31853. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  31854. + /*force*/0);
  31855. + if (unlikely(err == -EWOULDBLOCK)) {
  31856. + pr_warn("cannot retry for NFSv4 delegation"
  31857. + " for an internal unlink\n");
  31858. + iput(delegated);
  31859. + }
  31860. + } else {
  31861. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  31862. + err = 0;
  31863. + }
  31864. + dput(wbr->wbr_whbase);
  31865. + wbr->wbr_whbase = NULL;
  31866. + if (!err)
  31867. + err = au_wh_init(a->br, a->sb);
  31868. + wbr_wh_write_unlock(wbr);
  31869. + au_hn_inode_unlock(hdir);
  31870. + di_read_unlock(a->sb->s_root, AuLock_IR);
  31871. + if (!err)
  31872. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  31873. +
  31874. +out:
  31875. + if (wbr)
  31876. + atomic_dec(&wbr->wbr_wh_running);
  31877. + au_br_put(a->br);
  31878. + si_write_unlock(a->sb);
  31879. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  31880. + au_delayed_kfree(arg);
  31881. + if (unlikely(err))
  31882. + AuIOErr("err %d\n", err);
  31883. +}
  31884. +
  31885. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  31886. +{
  31887. + int do_dec, wkq_err;
  31888. + struct reinit_br_wh *arg;
  31889. +
  31890. + do_dec = 1;
  31891. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  31892. + goto out;
  31893. +
  31894. + /* ignore ENOMEM */
  31895. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  31896. + if (arg) {
  31897. + /*
  31898. + * dec(wh_running), kfree(arg) and dec(br_count)
  31899. + * in reinit function
  31900. + */
  31901. + arg->sb = sb;
  31902. + arg->br = br;
  31903. + au_br_get(br);
  31904. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  31905. + if (unlikely(wkq_err)) {
  31906. + atomic_dec(&br->br_wbr->wbr_wh_running);
  31907. + au_br_put(br);
  31908. + au_delayed_kfree(arg);
  31909. + }
  31910. + do_dec = 0;
  31911. + }
  31912. +
  31913. +out:
  31914. + if (do_dec)
  31915. + atomic_dec(&br->br_wbr->wbr_wh_running);
  31916. +}
  31917. +
  31918. +/* ---------------------------------------------------------------------- */
  31919. +
  31920. +/*
  31921. + * create the whiteout @wh.
  31922. + */
  31923. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  31924. + struct dentry *wh)
  31925. +{
  31926. + int err;
  31927. + struct path h_path = {
  31928. + .dentry = wh
  31929. + };
  31930. + struct au_branch *br;
  31931. + struct au_wbr *wbr;
  31932. + struct dentry *h_parent;
  31933. + struct inode *h_dir, *delegated;
  31934. +
  31935. + h_parent = wh->d_parent; /* dir inode is locked */
  31936. + h_dir = d_inode(h_parent);
  31937. + IMustLock(h_dir);
  31938. +
  31939. + br = au_sbr(sb, bindex);
  31940. + h_path.mnt = au_br_mnt(br);
  31941. + wbr = br->br_wbr;
  31942. + wbr_wh_read_lock(wbr);
  31943. + if (wbr->wbr_whbase) {
  31944. + delegated = NULL;
  31945. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  31946. + if (unlikely(err == -EWOULDBLOCK)) {
  31947. + pr_warn("cannot retry for NFSv4 delegation"
  31948. + " for an internal link\n");
  31949. + iput(delegated);
  31950. + }
  31951. + if (!err || err != -EMLINK)
  31952. + goto out;
  31953. +
  31954. + /* link count full. re-initialize br_whbase. */
  31955. + kick_reinit_br_wh(sb, br);
  31956. + }
  31957. +
  31958. + /* return this error in this context */
  31959. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  31960. + if (!err)
  31961. + au_fhsm_wrote(sb, bindex, /*force*/0);
  31962. +
  31963. +out:
  31964. + wbr_wh_read_unlock(wbr);
  31965. + return err;
  31966. +}
  31967. +
  31968. +/* ---------------------------------------------------------------------- */
  31969. +
  31970. +/*
  31971. + * create or remove the diropq.
  31972. + */
  31973. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  31974. + unsigned int flags)
  31975. +{
  31976. + struct dentry *opq_dentry, *h_dentry;
  31977. + struct super_block *sb;
  31978. + struct au_branch *br;
  31979. + int err;
  31980. +
  31981. + sb = dentry->d_sb;
  31982. + br = au_sbr(sb, bindex);
  31983. + h_dentry = au_h_dptr(dentry, bindex);
  31984. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  31985. + if (IS_ERR(opq_dentry))
  31986. + goto out;
  31987. +
  31988. + if (au_ftest_diropq(flags, CREATE)) {
  31989. + err = link_or_create_wh(sb, bindex, opq_dentry);
  31990. + if (!err) {
  31991. + au_set_dbdiropq(dentry, bindex);
  31992. + goto out; /* success */
  31993. + }
  31994. + } else {
  31995. + struct path tmp = {
  31996. + .dentry = opq_dentry,
  31997. + .mnt = au_br_mnt(br)
  31998. + };
  31999. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
  32000. + if (!err)
  32001. + au_set_dbdiropq(dentry, -1);
  32002. + }
  32003. + dput(opq_dentry);
  32004. + opq_dentry = ERR_PTR(err);
  32005. +
  32006. +out:
  32007. + return opq_dentry;
  32008. +}
  32009. +
  32010. +struct do_diropq_args {
  32011. + struct dentry **errp;
  32012. + struct dentry *dentry;
  32013. + aufs_bindex_t bindex;
  32014. + unsigned int flags;
  32015. +};
  32016. +
  32017. +static void call_do_diropq(void *args)
  32018. +{
  32019. + struct do_diropq_args *a = args;
  32020. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  32021. +}
  32022. +
  32023. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  32024. + unsigned int flags)
  32025. +{
  32026. + struct dentry *diropq, *h_dentry;
  32027. +
  32028. + h_dentry = au_h_dptr(dentry, bindex);
  32029. + if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
  32030. + diropq = do_diropq(dentry, bindex, flags);
  32031. + else {
  32032. + int wkq_err;
  32033. + struct do_diropq_args args = {
  32034. + .errp = &diropq,
  32035. + .dentry = dentry,
  32036. + .bindex = bindex,
  32037. + .flags = flags
  32038. + };
  32039. +
  32040. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  32041. + if (unlikely(wkq_err))
  32042. + diropq = ERR_PTR(wkq_err);
  32043. + }
  32044. +
  32045. + return diropq;
  32046. +}
  32047. +
  32048. +/* ---------------------------------------------------------------------- */
  32049. +
  32050. +/*
  32051. + * lookup whiteout dentry.
  32052. + * @h_parent: lower parent dentry which must exist and be locked
  32053. + * @base_name: name of dentry which will be whiteouted
  32054. + * returns dentry for whiteout.
  32055. + */
  32056. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  32057. + struct au_branch *br)
  32058. +{
  32059. + int err;
  32060. + struct qstr wh_name;
  32061. + struct dentry *wh_dentry;
  32062. +
  32063. + err = au_wh_name_alloc(&wh_name, base_name);
  32064. + wh_dentry = ERR_PTR(err);
  32065. + if (!err) {
  32066. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  32067. + au_delayed_kfree(wh_name.name);
  32068. + }
  32069. + return wh_dentry;
  32070. +}
  32071. +
  32072. +/*
  32073. + * link/create a whiteout for @dentry on @bindex.
  32074. + */
  32075. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  32076. + struct dentry *h_parent)
  32077. +{
  32078. + struct dentry *wh_dentry;
  32079. + struct super_block *sb;
  32080. + int err;
  32081. +
  32082. + sb = dentry->d_sb;
  32083. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  32084. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  32085. + err = link_or_create_wh(sb, bindex, wh_dentry);
  32086. + if (!err) {
  32087. + au_set_dbwh(dentry, bindex);
  32088. + au_fhsm_wrote(sb, bindex, /*force*/0);
  32089. + } else {
  32090. + dput(wh_dentry);
  32091. + wh_dentry = ERR_PTR(err);
  32092. + }
  32093. + }
  32094. +
  32095. + return wh_dentry;
  32096. +}
  32097. +
  32098. +/* ---------------------------------------------------------------------- */
  32099. +
  32100. +/* Delete all whiteouts in this directory on branch bindex. */
  32101. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  32102. + aufs_bindex_t bindex, struct au_branch *br)
  32103. +{
  32104. + int err;
  32105. + unsigned long ul, n;
  32106. + struct qstr wh_name;
  32107. + char *p;
  32108. + struct hlist_head *head;
  32109. + struct au_vdir_wh *pos;
  32110. + struct au_vdir_destr *str;
  32111. +
  32112. + err = -ENOMEM;
  32113. + p = (void *)__get_free_page(GFP_NOFS);
  32114. + wh_name.name = p;
  32115. + if (unlikely(!wh_name.name))
  32116. + goto out;
  32117. +
  32118. + err = 0;
  32119. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  32120. + p += AUFS_WH_PFX_LEN;
  32121. + n = whlist->nh_num;
  32122. + head = whlist->nh_head;
  32123. + for (ul = 0; !err && ul < n; ul++, head++) {
  32124. + hlist_for_each_entry(pos, head, wh_hash) {
  32125. + if (pos->wh_bindex != bindex)
  32126. + continue;
  32127. +
  32128. + str = &pos->wh_str;
  32129. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  32130. + memcpy(p, str->name, str->len);
  32131. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  32132. + err = unlink_wh_name(h_dentry, &wh_name, br);
  32133. + if (!err)
  32134. + continue;
  32135. + break;
  32136. + }
  32137. + AuIOErr("whiteout name too long %.*s\n",
  32138. + str->len, str->name);
  32139. + err = -EIO;
  32140. + break;
  32141. + }
  32142. + }
  32143. + au_delayed_free_page((unsigned long)wh_name.name);
  32144. +
  32145. +out:
  32146. + return err;
  32147. +}
  32148. +
  32149. +struct del_wh_children_args {
  32150. + int *errp;
  32151. + struct dentry *h_dentry;
  32152. + struct au_nhash *whlist;
  32153. + aufs_bindex_t bindex;
  32154. + struct au_branch *br;
  32155. +};
  32156. +
  32157. +static void call_del_wh_children(void *args)
  32158. +{
  32159. + struct del_wh_children_args *a = args;
  32160. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  32161. +}
  32162. +
  32163. +/* ---------------------------------------------------------------------- */
  32164. +
  32165. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  32166. +{
  32167. + struct au_whtmp_rmdir *whtmp;
  32168. + int err;
  32169. + unsigned int rdhash;
  32170. +
  32171. + SiMustAnyLock(sb);
  32172. +
  32173. + whtmp = kzalloc(sizeof(*whtmp), gfp);
  32174. + if (unlikely(!whtmp)) {
  32175. + whtmp = ERR_PTR(-ENOMEM);
  32176. + goto out;
  32177. + }
  32178. +
  32179. + /* no estimation for dir size */
  32180. + rdhash = au_sbi(sb)->si_rdhash;
  32181. + if (!rdhash)
  32182. + rdhash = AUFS_RDHASH_DEF;
  32183. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  32184. + if (unlikely(err)) {
  32185. + au_delayed_kfree(whtmp);
  32186. + whtmp = ERR_PTR(err);
  32187. + }
  32188. +
  32189. +out:
  32190. + return whtmp;
  32191. +}
  32192. +
  32193. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  32194. +{
  32195. + if (whtmp->br)
  32196. + au_br_put(whtmp->br);
  32197. + dput(whtmp->wh_dentry);
  32198. + iput(whtmp->dir);
  32199. + au_nhash_wh_free(&whtmp->whlist);
  32200. + au_delayed_kfree(whtmp);
  32201. +}
  32202. +
  32203. +/*
  32204. + * rmdir the whiteouted temporary named dir @h_dentry.
  32205. + * @whlist: whiteouted children.
  32206. + */
  32207. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32208. + struct dentry *wh_dentry, struct au_nhash *whlist)
  32209. +{
  32210. + int err;
  32211. + unsigned int h_nlink;
  32212. + struct path h_tmp;
  32213. + struct inode *wh_inode, *h_dir;
  32214. + struct au_branch *br;
  32215. +
  32216. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  32217. + IMustLock(h_dir);
  32218. +
  32219. + br = au_sbr(dir->i_sb, bindex);
  32220. + wh_inode = d_inode(wh_dentry);
  32221. + inode_lock_nested(wh_inode, AuLsc_I_CHILD);
  32222. +
  32223. + /*
  32224. + * someone else might change some whiteouts while we were sleeping.
  32225. + * it means this whlist may have an obsoleted entry.
  32226. + */
  32227. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  32228. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  32229. + else {
  32230. + int wkq_err;
  32231. + struct del_wh_children_args args = {
  32232. + .errp = &err,
  32233. + .h_dentry = wh_dentry,
  32234. + .whlist = whlist,
  32235. + .bindex = bindex,
  32236. + .br = br
  32237. + };
  32238. +
  32239. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  32240. + if (unlikely(wkq_err))
  32241. + err = wkq_err;
  32242. + }
  32243. + inode_unlock(wh_inode);
  32244. +
  32245. + if (!err) {
  32246. + h_tmp.dentry = wh_dentry;
  32247. + h_tmp.mnt = au_br_mnt(br);
  32248. + h_nlink = h_dir->i_nlink;
  32249. + err = vfsub_rmdir(h_dir, &h_tmp);
  32250. + /* some fs doesn't change the parent nlink in some cases */
  32251. + h_nlink -= h_dir->i_nlink;
  32252. + }
  32253. +
  32254. + if (!err) {
  32255. + if (au_ibtop(dir) == bindex) {
  32256. + /* todo: dir->i_mutex is necessary */
  32257. + au_cpup_attr_timesizes(dir);
  32258. + if (h_nlink)
  32259. + vfsub_drop_nlink(dir);
  32260. + }
  32261. + return 0; /* success */
  32262. + }
  32263. +
  32264. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  32265. + return err;
  32266. +}
  32267. +
  32268. +static void call_rmdir_whtmp(void *args)
  32269. +{
  32270. + int err;
  32271. + aufs_bindex_t bindex;
  32272. + struct au_whtmp_rmdir *a = args;
  32273. + struct super_block *sb;
  32274. + struct dentry *h_parent;
  32275. + struct inode *h_dir;
  32276. + struct au_hinode *hdir;
  32277. +
  32278. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  32279. + /* inode_lock(a->dir); */
  32280. + err = -EROFS;
  32281. + sb = a->dir->i_sb;
  32282. + si_read_lock(sb, !AuLock_FLUSH);
  32283. + if (!au_br_writable(a->br->br_perm))
  32284. + goto out;
  32285. + bindex = au_br_index(sb, a->br->br_id);
  32286. + if (unlikely(bindex < 0))
  32287. + goto out;
  32288. +
  32289. + err = -EIO;
  32290. + ii_write_lock_parent(a->dir);
  32291. + h_parent = dget_parent(a->wh_dentry);
  32292. + h_dir = d_inode(h_parent);
  32293. + hdir = au_hi(a->dir, bindex);
  32294. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  32295. + if (unlikely(err))
  32296. + goto out_mnt;
  32297. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  32298. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  32299. + a->br);
  32300. + if (!err)
  32301. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  32302. + au_hn_inode_unlock(hdir);
  32303. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  32304. +
  32305. +out_mnt:
  32306. + dput(h_parent);
  32307. + ii_write_unlock(a->dir);
  32308. +out:
  32309. + /* inode_unlock(a->dir); */
  32310. + au_whtmp_rmdir_free(a);
  32311. + si_read_unlock(sb);
  32312. + au_nwt_done(&au_sbi(sb)->si_nowait);
  32313. + if (unlikely(err))
  32314. + AuIOErr("err %d\n", err);
  32315. +}
  32316. +
  32317. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32318. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  32319. +{
  32320. + int wkq_err;
  32321. + struct super_block *sb;
  32322. +
  32323. + IMustLock(dir);
  32324. +
  32325. + /* all post-process will be done in do_rmdir_whtmp(). */
  32326. + sb = dir->i_sb;
  32327. + args->dir = au_igrab(dir);
  32328. + args->br = au_sbr(sb, bindex);
  32329. + au_br_get(args->br);
  32330. + args->wh_dentry = dget(wh_dentry);
  32331. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  32332. + if (unlikely(wkq_err)) {
  32333. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  32334. + au_whtmp_rmdir_free(args);
  32335. + }
  32336. +}
  32337. diff -Naur null/fs/aufs/whout.h linux-4.7/fs/aufs/whout.h
  32338. --- /dev/null
  32339. +++ linux-4.7/fs/aufs/whout.h 2016-08-08 12:57:44.610384496 -0400
  32340. @@ -0,0 +1,85 @@
  32341. +/*
  32342. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  32343. + *
  32344. + * This program, aufs is free software; you can redistribute it and/or modify
  32345. + * it under the terms of the GNU General Public License as published by
  32346. + * the Free Software Foundation; either version 2 of the License, or
  32347. + * (at your option) any later version.
  32348. + *
  32349. + * This program is distributed in the hope that it will be useful,
  32350. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32351. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32352. + * GNU General Public License for more details.
  32353. + *
  32354. + * You should have received a copy of the GNU General Public License
  32355. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32356. + */
  32357. +
  32358. +/*
  32359. + * whiteout for logical deletion and opaque directory
  32360. + */
  32361. +
  32362. +#ifndef __AUFS_WHOUT_H__
  32363. +#define __AUFS_WHOUT_H__
  32364. +
  32365. +#ifdef __KERNEL__
  32366. +
  32367. +#include "dir.h"
  32368. +
  32369. +/* whout.c */
  32370. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  32371. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  32372. +int au_diropq_test(struct dentry *h_dentry);
  32373. +struct au_branch;
  32374. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  32375. + struct qstr *prefix);
  32376. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  32377. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  32378. + struct dentry *dentry);
  32379. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  32380. +
  32381. +/* diropq flags */
  32382. +#define AuDiropq_CREATE 1
  32383. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  32384. +#define au_fset_diropq(flags, name) \
  32385. + do { (flags) |= AuDiropq_##name; } while (0)
  32386. +#define au_fclr_diropq(flags, name) \
  32387. + do { (flags) &= ~AuDiropq_##name; } while (0)
  32388. +
  32389. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  32390. + unsigned int flags);
  32391. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  32392. + struct au_branch *br);
  32393. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  32394. + struct dentry *h_parent);
  32395. +
  32396. +/* real rmdir for the whiteout-ed dir */
  32397. +struct au_whtmp_rmdir {
  32398. + struct inode *dir;
  32399. + struct au_branch *br;
  32400. + struct dentry *wh_dentry;
  32401. + struct au_nhash whlist;
  32402. +};
  32403. +
  32404. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  32405. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  32406. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32407. + struct dentry *wh_dentry, struct au_nhash *whlist);
  32408. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32409. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  32410. +
  32411. +/* ---------------------------------------------------------------------- */
  32412. +
  32413. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  32414. + aufs_bindex_t bindex)
  32415. +{
  32416. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  32417. +}
  32418. +
  32419. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  32420. +{
  32421. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  32422. +}
  32423. +
  32424. +#endif /* __KERNEL__ */
  32425. +#endif /* __AUFS_WHOUT_H__ */
  32426. diff -Naur null/fs/aufs/wkq.c linux-4.7/fs/aufs/wkq.c
  32427. --- /dev/null
  32428. +++ linux-4.7/fs/aufs/wkq.c 2016-08-08 12:57:44.610384496 -0400
  32429. @@ -0,0 +1,213 @@
  32430. +/*
  32431. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  32432. + *
  32433. + * This program, aufs is free software; you can redistribute it and/or modify
  32434. + * it under the terms of the GNU General Public License as published by
  32435. + * the Free Software Foundation; either version 2 of the License, or
  32436. + * (at your option) any later version.
  32437. + *
  32438. + * This program is distributed in the hope that it will be useful,
  32439. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32440. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32441. + * GNU General Public License for more details.
  32442. + *
  32443. + * You should have received a copy of the GNU General Public License
  32444. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32445. + */
  32446. +
  32447. +/*
  32448. + * workqueue for asynchronous/super-io operations
  32449. + * todo: try new dredential scheme
  32450. + */
  32451. +
  32452. +#include <linux/module.h>
  32453. +#include "aufs.h"
  32454. +
  32455. +/* internal workqueue named AUFS_WKQ_NAME */
  32456. +
  32457. +static struct workqueue_struct *au_wkq;
  32458. +
  32459. +struct au_wkinfo {
  32460. + struct work_struct wk;
  32461. + struct kobject *kobj;
  32462. +
  32463. + unsigned int flags; /* see wkq.h */
  32464. +
  32465. + au_wkq_func_t func;
  32466. + void *args;
  32467. +
  32468. + struct completion *comp;
  32469. +};
  32470. +
  32471. +/* ---------------------------------------------------------------------- */
  32472. +
  32473. +static void wkq_func(struct work_struct *wk)
  32474. +{
  32475. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  32476. +
  32477. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  32478. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  32479. +
  32480. + wkinfo->func(wkinfo->args);
  32481. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  32482. + complete(wkinfo->comp);
  32483. + else {
  32484. + kobject_put(wkinfo->kobj);
  32485. + module_put(THIS_MODULE); /* todo: ?? */
  32486. + au_delayed_kfree(wkinfo);
  32487. + }
  32488. +}
  32489. +
  32490. +/*
  32491. + * Since struct completion is large, try allocating it dynamically.
  32492. + */
  32493. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  32494. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  32495. +
  32496. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  32497. +{
  32498. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  32499. + if (*comp) {
  32500. + init_completion(*comp);
  32501. + wkinfo->comp = *comp;
  32502. + return 0;
  32503. + }
  32504. + return -ENOMEM;
  32505. +}
  32506. +
  32507. +static void au_wkq_comp_free(struct completion *comp)
  32508. +{
  32509. + au_delayed_kfree(comp);
  32510. +}
  32511. +
  32512. +#else
  32513. +
  32514. +/* no braces */
  32515. +#define AuWkqCompDeclare(name) \
  32516. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  32517. + struct completion *comp = &_ ## name
  32518. +
  32519. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  32520. +{
  32521. + wkinfo->comp = *comp;
  32522. + return 0;
  32523. +}
  32524. +
  32525. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  32526. +{
  32527. + /* empty */
  32528. +}
  32529. +#endif /* 4KSTACKS */
  32530. +
  32531. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  32532. +{
  32533. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  32534. + if (au_wkq_test()) {
  32535. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  32536. + " due to a dead dir by UDBA?\n");
  32537. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  32538. + }
  32539. + } else
  32540. + au_dbg_verify_kthread();
  32541. +
  32542. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  32543. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  32544. + queue_work(au_wkq, &wkinfo->wk);
  32545. + } else {
  32546. + INIT_WORK(&wkinfo->wk, wkq_func);
  32547. + schedule_work(&wkinfo->wk);
  32548. + }
  32549. +}
  32550. +
  32551. +/*
  32552. + * Be careful. It is easy to make deadlock happen.
  32553. + * processA: lock, wkq and wait
  32554. + * processB: wkq and wait, lock in wkq
  32555. + * --> deadlock
  32556. + */
  32557. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  32558. +{
  32559. + int err;
  32560. + AuWkqCompDeclare(comp);
  32561. + struct au_wkinfo wkinfo = {
  32562. + .flags = flags,
  32563. + .func = func,
  32564. + .args = args
  32565. + };
  32566. +
  32567. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  32568. + if (!err) {
  32569. + au_wkq_run(&wkinfo);
  32570. + /* no timeout, no interrupt */
  32571. + wait_for_completion(wkinfo.comp);
  32572. + au_wkq_comp_free(comp);
  32573. + destroy_work_on_stack(&wkinfo.wk);
  32574. + }
  32575. +
  32576. + return err;
  32577. +
  32578. +}
  32579. +
  32580. +/*
  32581. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  32582. + * problem in a concurrent umounting.
  32583. + */
  32584. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  32585. + unsigned int flags)
  32586. +{
  32587. + int err;
  32588. + struct au_wkinfo *wkinfo;
  32589. +
  32590. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  32591. +
  32592. + /*
  32593. + * wkq_func() must free this wkinfo.
  32594. + * it highly depends upon the implementation of workqueue.
  32595. + */
  32596. + err = 0;
  32597. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  32598. + if (wkinfo) {
  32599. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  32600. + wkinfo->flags = flags & ~AuWkq_WAIT;
  32601. + wkinfo->func = func;
  32602. + wkinfo->args = args;
  32603. + wkinfo->comp = NULL;
  32604. + kobject_get(wkinfo->kobj);
  32605. + __module_get(THIS_MODULE); /* todo: ?? */
  32606. +
  32607. + au_wkq_run(wkinfo);
  32608. + } else {
  32609. + err = -ENOMEM;
  32610. + au_nwt_done(&au_sbi(sb)->si_nowait);
  32611. + }
  32612. +
  32613. + return err;
  32614. +}
  32615. +
  32616. +/* ---------------------------------------------------------------------- */
  32617. +
  32618. +void au_nwt_init(struct au_nowait_tasks *nwt)
  32619. +{
  32620. + atomic_set(&nwt->nw_len, 0);
  32621. + /* smp_mb(); */ /* atomic_set */
  32622. + init_waitqueue_head(&nwt->nw_wq);
  32623. +}
  32624. +
  32625. +void au_wkq_fin(void)
  32626. +{
  32627. + destroy_workqueue(au_wkq);
  32628. +}
  32629. +
  32630. +int __init au_wkq_init(void)
  32631. +{
  32632. + int err;
  32633. +
  32634. + err = 0;
  32635. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  32636. + if (IS_ERR(au_wkq))
  32637. + err = PTR_ERR(au_wkq);
  32638. + else if (!au_wkq)
  32639. + err = -ENOMEM;
  32640. +
  32641. + return err;
  32642. +}
  32643. diff -Naur null/fs/aufs/wkq.h linux-4.7/fs/aufs/wkq.h
  32644. --- /dev/null
  32645. +++ linux-4.7/fs/aufs/wkq.h 2016-08-08 12:57:44.610384496 -0400
  32646. @@ -0,0 +1,93 @@
  32647. +/*
  32648. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  32649. + *
  32650. + * This program, aufs is free software; you can redistribute it and/or modify
  32651. + * it under the terms of the GNU General Public License as published by
  32652. + * the Free Software Foundation; either version 2 of the License, or
  32653. + * (at your option) any later version.
  32654. + *
  32655. + * This program is distributed in the hope that it will be useful,
  32656. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32657. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32658. + * GNU General Public License for more details.
  32659. + *
  32660. + * You should have received a copy of the GNU General Public License
  32661. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32662. + */
  32663. +
  32664. +/*
  32665. + * workqueue for asynchronous/super-io operations
  32666. + * todo: try new credentials management scheme
  32667. + */
  32668. +
  32669. +#ifndef __AUFS_WKQ_H__
  32670. +#define __AUFS_WKQ_H__
  32671. +
  32672. +#ifdef __KERNEL__
  32673. +
  32674. +#include <linux/percpu_counter.h>
  32675. +
  32676. +struct super_block;
  32677. +
  32678. +/* ---------------------------------------------------------------------- */
  32679. +
  32680. +/*
  32681. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  32682. + */
  32683. +struct au_nowait_tasks {
  32684. + atomic_t nw_len;
  32685. + wait_queue_head_t nw_wq;
  32686. +};
  32687. +
  32688. +/* ---------------------------------------------------------------------- */
  32689. +
  32690. +typedef void (*au_wkq_func_t)(void *args);
  32691. +
  32692. +/* wkq flags */
  32693. +#define AuWkq_WAIT 1
  32694. +#define AuWkq_NEST (1 << 1)
  32695. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  32696. +#define au_fset_wkq(flags, name) \
  32697. + do { (flags) |= AuWkq_##name; } while (0)
  32698. +#define au_fclr_wkq(flags, name) \
  32699. + do { (flags) &= ~AuWkq_##name; } while (0)
  32700. +
  32701. +#ifndef CONFIG_AUFS_HNOTIFY
  32702. +#undef AuWkq_NEST
  32703. +#define AuWkq_NEST 0
  32704. +#endif
  32705. +
  32706. +/* wkq.c */
  32707. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  32708. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  32709. + unsigned int flags);
  32710. +void au_nwt_init(struct au_nowait_tasks *nwt);
  32711. +int __init au_wkq_init(void);
  32712. +void au_wkq_fin(void);
  32713. +
  32714. +/* ---------------------------------------------------------------------- */
  32715. +
  32716. +static inline int au_wkq_test(void)
  32717. +{
  32718. + return current->flags & PF_WQ_WORKER;
  32719. +}
  32720. +
  32721. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  32722. +{
  32723. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  32724. +}
  32725. +
  32726. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  32727. +{
  32728. + if (atomic_dec_and_test(&nwt->nw_len))
  32729. + wake_up_all(&nwt->nw_wq);
  32730. +}
  32731. +
  32732. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  32733. +{
  32734. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  32735. + return 0;
  32736. +}
  32737. +
  32738. +#endif /* __KERNEL__ */
  32739. +#endif /* __AUFS_WKQ_H__ */
  32740. diff -Naur null/fs/aufs/xattr.c linux-4.7/fs/aufs/xattr.c
  32741. --- /dev/null
  32742. +++ linux-4.7/fs/aufs/xattr.c 2016-08-08 12:57:44.610384496 -0400
  32743. @@ -0,0 +1,347 @@
  32744. +/*
  32745. + * Copyright (C) 2014-2016 Junjiro R. Okajima
  32746. + *
  32747. + * This program, aufs is free software; you can redistribute it and/or modify
  32748. + * it under the terms of the GNU General Public License as published by
  32749. + * the Free Software Foundation; either version 2 of the License, or
  32750. + * (at your option) any later version.
  32751. + *
  32752. + * This program is distributed in the hope that it will be useful,
  32753. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32754. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32755. + * GNU General Public License for more details.
  32756. + *
  32757. + * You should have received a copy of the GNU General Public License
  32758. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32759. + */
  32760. +
  32761. +/*
  32762. + * handling xattr functions
  32763. + */
  32764. +
  32765. +#include <linux/xattr.h>
  32766. +#include "aufs.h"
  32767. +
  32768. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  32769. +{
  32770. + if (!ignore_flags)
  32771. + goto out;
  32772. + switch (err) {
  32773. + case -ENOMEM:
  32774. + case -EDQUOT:
  32775. + goto out;
  32776. + }
  32777. +
  32778. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  32779. + err = 0;
  32780. + goto out;
  32781. + }
  32782. +
  32783. +#define cmp(brattr, prefix) do { \
  32784. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  32785. + XATTR_##prefix##_PREFIX_LEN)) { \
  32786. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  32787. + err = 0; \
  32788. + goto out; \
  32789. + } \
  32790. + } while (0)
  32791. +
  32792. + cmp(SEC, SECURITY);
  32793. + cmp(SYS, SYSTEM);
  32794. + cmp(TR, TRUSTED);
  32795. + cmp(USR, USER);
  32796. +#undef cmp
  32797. +
  32798. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  32799. + err = 0;
  32800. +
  32801. +out:
  32802. + return err;
  32803. +}
  32804. +
  32805. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  32806. +
  32807. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  32808. + char *name, char **buf, unsigned int ignore_flags,
  32809. + unsigned int verbose)
  32810. +{
  32811. + int err;
  32812. + ssize_t ssz;
  32813. + struct inode *h_idst;
  32814. +
  32815. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  32816. + err = ssz;
  32817. + if (unlikely(err <= 0)) {
  32818. + if (err == -ENODATA
  32819. + || (err == -EOPNOTSUPP
  32820. + && ((ignore_flags & au_xattr_out_of_list)
  32821. + || (au_test_nfs_noacl(d_inode(h_src))
  32822. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  32823. + || !strcmp(name,
  32824. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  32825. + ))
  32826. + err = 0;
  32827. + if (err && (verbose || au_debug_test()))
  32828. + pr_err("%s, err %d\n", name, err);
  32829. + goto out;
  32830. + }
  32831. +
  32832. + /* unlock it temporary */
  32833. + h_idst = d_inode(h_dst);
  32834. + inode_unlock(h_idst);
  32835. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  32836. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  32837. + if (unlikely(err)) {
  32838. + if (verbose || au_debug_test())
  32839. + pr_err("%s, err %d\n", name, err);
  32840. + err = au_xattr_ignore(err, name, ignore_flags);
  32841. + }
  32842. +
  32843. +out:
  32844. + return err;
  32845. +}
  32846. +
  32847. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  32848. + unsigned int verbose)
  32849. +{
  32850. + int err, unlocked, acl_access, acl_default;
  32851. + ssize_t ssz;
  32852. + struct inode *h_isrc, *h_idst;
  32853. + char *value, *p, *o, *e;
  32854. +
  32855. + /* try stopping to update the source inode while we are referencing */
  32856. + /* there should not be the parent-child relationship between them */
  32857. + h_isrc = d_inode(h_src);
  32858. + h_idst = d_inode(h_dst);
  32859. + inode_unlock(h_idst);
  32860. + inode_lock_nested(h_isrc, AuLsc_I_CHILD);
  32861. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  32862. + unlocked = 0;
  32863. +
  32864. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  32865. + ssz = vfs_listxattr(h_src, NULL, 0);
  32866. + err = ssz;
  32867. + if (unlikely(err < 0)) {
  32868. + AuTraceErr(err);
  32869. + if (err == -ENODATA
  32870. + || err == -EOPNOTSUPP)
  32871. + err = 0; /* ignore */
  32872. + goto out;
  32873. + }
  32874. +
  32875. + err = 0;
  32876. + p = NULL;
  32877. + o = NULL;
  32878. + if (ssz) {
  32879. + err = -ENOMEM;
  32880. + p = kmalloc(ssz, GFP_NOFS);
  32881. + o = p;
  32882. + if (unlikely(!p))
  32883. + goto out;
  32884. + err = vfs_listxattr(h_src, p, ssz);
  32885. + }
  32886. + inode_unlock(h_isrc);
  32887. + unlocked = 1;
  32888. + AuDbg("err %d, ssz %zd\n", err, ssz);
  32889. + if (unlikely(err < 0))
  32890. + goto out_free;
  32891. +
  32892. + err = 0;
  32893. + e = p + ssz;
  32894. + value = NULL;
  32895. + acl_access = 0;
  32896. + acl_default = 0;
  32897. + while (!err && p < e) {
  32898. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  32899. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  32900. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  32901. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  32902. + - 1);
  32903. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  32904. + verbose);
  32905. + p += strlen(p) + 1;
  32906. + }
  32907. + AuTraceErr(err);
  32908. + ignore_flags |= au_xattr_out_of_list;
  32909. + if (!err && !acl_access) {
  32910. + err = au_do_cpup_xattr(h_dst, h_src,
  32911. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  32912. + ignore_flags, verbose);
  32913. + AuTraceErr(err);
  32914. + }
  32915. + if (!err && !acl_default) {
  32916. + err = au_do_cpup_xattr(h_dst, h_src,
  32917. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  32918. + ignore_flags, verbose);
  32919. + AuTraceErr(err);
  32920. + }
  32921. +
  32922. + if (value)
  32923. + au_delayed_kfree(value);
  32924. +
  32925. +out_free:
  32926. + if (o)
  32927. + au_delayed_kfree(o);
  32928. +out:
  32929. + if (!unlocked)
  32930. + inode_unlock(h_isrc);
  32931. + AuTraceErr(err);
  32932. + return err;
  32933. +}
  32934. +
  32935. +/* ---------------------------------------------------------------------- */
  32936. +
  32937. +enum {
  32938. + AU_XATTR_LIST,
  32939. + AU_XATTR_GET
  32940. +};
  32941. +
  32942. +struct au_lgxattr {
  32943. + int type;
  32944. + union {
  32945. + struct {
  32946. + char *list;
  32947. + size_t size;
  32948. + } list;
  32949. + struct {
  32950. + const char *name;
  32951. + void *value;
  32952. + size_t size;
  32953. + } get;
  32954. + } u;
  32955. +};
  32956. +
  32957. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  32958. +{
  32959. + ssize_t err;
  32960. + struct path h_path;
  32961. + struct super_block *sb;
  32962. +
  32963. + sb = dentry->d_sb;
  32964. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  32965. + if (unlikely(err))
  32966. + goto out;
  32967. + err = au_h_path_getattr(dentry, /*force*/1, &h_path);
  32968. + if (unlikely(err))
  32969. + goto out_si;
  32970. + if (unlikely(!h_path.dentry))
  32971. + /* illegally overlapped or something */
  32972. + goto out_di; /* pretending success */
  32973. +
  32974. + /* always topmost entry only */
  32975. + switch (arg->type) {
  32976. + case AU_XATTR_LIST:
  32977. + err = vfs_listxattr(h_path.dentry,
  32978. + arg->u.list.list, arg->u.list.size);
  32979. + break;
  32980. + case AU_XATTR_GET:
  32981. + AuDebugOn(d_is_negative(h_path.dentry));
  32982. + err = vfs_getxattr(h_path.dentry,
  32983. + arg->u.get.name, arg->u.get.value,
  32984. + arg->u.get.size);
  32985. + break;
  32986. + }
  32987. +
  32988. +out_di:
  32989. + di_read_unlock(dentry, AuLock_IR);
  32990. +out_si:
  32991. + si_read_unlock(sb);
  32992. +out:
  32993. + AuTraceErr(err);
  32994. + return err;
  32995. +}
  32996. +
  32997. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  32998. +{
  32999. + struct au_lgxattr arg = {
  33000. + .type = AU_XATTR_LIST,
  33001. + .u.list = {
  33002. + .list = list,
  33003. + .size = size
  33004. + },
  33005. + };
  33006. +
  33007. + return au_lgxattr(dentry, &arg);
  33008. +}
  33009. +
  33010. +ssize_t aufs_getxattr(struct dentry *dentry, struct inode *inode __maybe_unused,
  33011. + const char *name, void *value, size_t size)
  33012. +{
  33013. + struct au_lgxattr arg = {
  33014. + .type = AU_XATTR_GET,
  33015. + .u.get = {
  33016. + .name = name,
  33017. + .value = value,
  33018. + .size = size
  33019. + },
  33020. + };
  33021. +
  33022. + return au_lgxattr(dentry, &arg);
  33023. +}
  33024. +
  33025. +int aufs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
  33026. + const void *value, size_t size, int flags)
  33027. +{
  33028. + struct au_srxattr arg = {
  33029. + .type = AU_XATTR_SET,
  33030. + .u.set = {
  33031. + .name = name,
  33032. + .value = value,
  33033. + .size = size,
  33034. + .flags = flags
  33035. + },
  33036. + };
  33037. +
  33038. + return au_srxattr(dentry, inode, &arg);
  33039. +}
  33040. +
  33041. +int aufs_removexattr(struct dentry *dentry, const char *name)
  33042. +{
  33043. + struct au_srxattr arg = {
  33044. + .type = AU_XATTR_REMOVE,
  33045. + .u.remove = {
  33046. + .name = name
  33047. + },
  33048. + };
  33049. +
  33050. + return au_srxattr(dentry, d_inode(dentry), &arg);
  33051. +}
  33052. +
  33053. +/* ---------------------------------------------------------------------- */
  33054. +
  33055. +#if 0
  33056. +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  33057. + const char *name, size_t name_len, int type)
  33058. +{
  33059. + return aufs_listxattr(dentry, list, list_size);
  33060. +}
  33061. +
  33062. +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
  33063. + size_t size, int type)
  33064. +{
  33065. + return aufs_getxattr(dentry, name, buffer, size);
  33066. +}
  33067. +
  33068. +static int au_xattr_set(struct dentry *dentry, const char *name,
  33069. + const void *value, size_t size, int flags, int type)
  33070. +{
  33071. + return aufs_setxattr(dentry, name, value, size, flags);
  33072. +}
  33073. +
  33074. +static const struct xattr_handler au_xattr_handler = {
  33075. + /* no prefix, no flags */
  33076. + .list = au_xattr_list,
  33077. + .get = au_xattr_get,
  33078. + .set = au_xattr_set
  33079. + /* why no remove? */
  33080. +};
  33081. +
  33082. +static const struct xattr_handler *au_xattr_handlers[] = {
  33083. + &au_xattr_handler
  33084. +};
  33085. +
  33086. +void au_xattr_init(struct super_block *sb)
  33087. +{
  33088. + /* sb->s_xattr = au_xattr_handlers; */
  33089. +}
  33090. +#endif
  33091. diff -Naur null/fs/aufs/xino.c linux-4.7/fs/aufs/xino.c
  33092. --- /dev/null
  33093. +++ linux-4.7/fs/aufs/xino.c 2016-08-08 12:57:44.611384474 -0400
  33094. @@ -0,0 +1,1318 @@
  33095. +/*
  33096. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  33097. + *
  33098. + * This program, aufs is free software; you can redistribute it and/or modify
  33099. + * it under the terms of the GNU General Public License as published by
  33100. + * the Free Software Foundation; either version 2 of the License, or
  33101. + * (at your option) any later version.
  33102. + *
  33103. + * This program is distributed in the hope that it will be useful,
  33104. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33105. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33106. + * GNU General Public License for more details.
  33107. + *
  33108. + * You should have received a copy of the GNU General Public License
  33109. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33110. + */
  33111. +
  33112. +/*
  33113. + * external inode number translation table and bitmap
  33114. + */
  33115. +
  33116. +#include <linux/seq_file.h>
  33117. +#include <linux/statfs.h>
  33118. +#include "aufs.h"
  33119. +
  33120. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  33121. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
  33122. + loff_t *pos)
  33123. +{
  33124. + ssize_t err;
  33125. + mm_segment_t oldfs;
  33126. + union {
  33127. + void *k;
  33128. + char __user *u;
  33129. + } buf;
  33130. +
  33131. + buf.k = kbuf;
  33132. + oldfs = get_fs();
  33133. + set_fs(KERNEL_DS);
  33134. + do {
  33135. + /* todo: signal_pending? */
  33136. + err = func(file, buf.u, size, pos);
  33137. + } while (err == -EAGAIN || err == -EINTR);
  33138. + set_fs(oldfs);
  33139. +
  33140. +#if 0 /* reserved for future use */
  33141. + if (err > 0)
  33142. + fsnotify_access(file->f_path.dentry);
  33143. +#endif
  33144. +
  33145. + return err;
  33146. +}
  33147. +
  33148. +/* ---------------------------------------------------------------------- */
  33149. +
  33150. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  33151. + size_t size, loff_t *pos);
  33152. +
  33153. +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
  33154. + size_t size, loff_t *pos)
  33155. +{
  33156. + ssize_t err;
  33157. + mm_segment_t oldfs;
  33158. + union {
  33159. + void *k;
  33160. + const char __user *u;
  33161. + } buf;
  33162. + int i;
  33163. + const int prevent_endless = 10;
  33164. +
  33165. + i = 0;
  33166. + buf.k = kbuf;
  33167. + oldfs = get_fs();
  33168. + set_fs(KERNEL_DS);
  33169. + do {
  33170. + err = func(file, buf.u, size, pos);
  33171. + if (err == -EINTR
  33172. + && !au_wkq_test()
  33173. + && fatal_signal_pending(current)) {
  33174. + set_fs(oldfs);
  33175. + err = xino_fwrite_wkq(func, file, kbuf, size, pos);
  33176. + BUG_ON(err == -EINTR);
  33177. + oldfs = get_fs();
  33178. + set_fs(KERNEL_DS);
  33179. + }
  33180. + } while (i++ < prevent_endless
  33181. + && (err == -EAGAIN || err == -EINTR));
  33182. + set_fs(oldfs);
  33183. +
  33184. +#if 0 /* reserved for future use */
  33185. + if (err > 0)
  33186. + fsnotify_modify(file->f_path.dentry);
  33187. +#endif
  33188. +
  33189. + return err;
  33190. +}
  33191. +
  33192. +struct do_xino_fwrite_args {
  33193. + ssize_t *errp;
  33194. + vfs_writef_t func;
  33195. + struct file *file;
  33196. + void *buf;
  33197. + size_t size;
  33198. + loff_t *pos;
  33199. +};
  33200. +
  33201. +static void call_do_xino_fwrite(void *args)
  33202. +{
  33203. + struct do_xino_fwrite_args *a = args;
  33204. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  33205. +}
  33206. +
  33207. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  33208. + size_t size, loff_t *pos)
  33209. +{
  33210. + ssize_t err;
  33211. + int wkq_err;
  33212. + struct do_xino_fwrite_args args = {
  33213. + .errp = &err,
  33214. + .func = func,
  33215. + .file = file,
  33216. + .buf = buf,
  33217. + .size = size,
  33218. + .pos = pos
  33219. + };
  33220. +
  33221. + /*
  33222. + * it breaks RLIMIT_FSIZE and normal user's limit,
  33223. + * users should care about quota and real 'filesystem full.'
  33224. + */
  33225. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  33226. + if (unlikely(wkq_err))
  33227. + err = wkq_err;
  33228. +
  33229. + return err;
  33230. +}
  33231. +
  33232. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  33233. + size_t size, loff_t *pos)
  33234. +{
  33235. + ssize_t err;
  33236. +
  33237. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  33238. + lockdep_off();
  33239. + err = do_xino_fwrite(func, file, buf, size, pos);
  33240. + lockdep_on();
  33241. + } else
  33242. + err = xino_fwrite_wkq(func, file, buf, size, pos);
  33243. +
  33244. + return err;
  33245. +}
  33246. +
  33247. +/* ---------------------------------------------------------------------- */
  33248. +
  33249. +/*
  33250. + * create a new xinofile at the same place/path as @base_file.
  33251. + */
  33252. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
  33253. +{
  33254. + struct file *file;
  33255. + struct dentry *base, *parent;
  33256. + struct inode *dir, *delegated;
  33257. + struct qstr *name;
  33258. + struct path path;
  33259. + int err;
  33260. +
  33261. + base = base_file->f_path.dentry;
  33262. + parent = base->d_parent; /* dir inode is locked */
  33263. + dir = d_inode(parent);
  33264. + IMustLock(dir);
  33265. +
  33266. + file = ERR_PTR(-EINVAL);
  33267. + name = &base->d_name;
  33268. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  33269. + if (IS_ERR(path.dentry)) {
  33270. + file = (void *)path.dentry;
  33271. + pr_err("%pd lookup err %ld\n",
  33272. + base, PTR_ERR(path.dentry));
  33273. + goto out;
  33274. + }
  33275. +
  33276. + /* no need to mnt_want_write() since we call dentry_open() later */
  33277. + err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
  33278. + if (unlikely(err)) {
  33279. + file = ERR_PTR(err);
  33280. + pr_err("%pd create err %d\n", base, err);
  33281. + goto out_dput;
  33282. + }
  33283. +
  33284. + path.mnt = base_file->f_path.mnt;
  33285. + file = vfsub_dentry_open(&path,
  33286. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  33287. + /* | __FMODE_NONOTIFY */);
  33288. + if (IS_ERR(file)) {
  33289. + pr_err("%pd open err %ld\n", base, PTR_ERR(file));
  33290. + goto out_dput;
  33291. + }
  33292. +
  33293. + delegated = NULL;
  33294. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  33295. + if (unlikely(err == -EWOULDBLOCK)) {
  33296. + pr_warn("cannot retry for NFSv4 delegation"
  33297. + " for an internal unlink\n");
  33298. + iput(delegated);
  33299. + }
  33300. + if (unlikely(err)) {
  33301. + pr_err("%pd unlink err %d\n", base, err);
  33302. + goto out_fput;
  33303. + }
  33304. +
  33305. + if (copy_src) {
  33306. + /* no one can touch copy_src xino */
  33307. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  33308. + if (unlikely(err)) {
  33309. + pr_err("%pd copy err %d\n", base, err);
  33310. + goto out_fput;
  33311. + }
  33312. + }
  33313. + goto out_dput; /* success */
  33314. +
  33315. +out_fput:
  33316. + fput(file);
  33317. + file = ERR_PTR(err);
  33318. +out_dput:
  33319. + dput(path.dentry);
  33320. +out:
  33321. + return file;
  33322. +}
  33323. +
  33324. +struct au_xino_lock_dir {
  33325. + struct au_hinode *hdir;
  33326. + struct dentry *parent;
  33327. + struct inode *dir;
  33328. +};
  33329. +
  33330. +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
  33331. + struct au_xino_lock_dir *ldir)
  33332. +{
  33333. + aufs_bindex_t brid, bindex;
  33334. +
  33335. + ldir->hdir = NULL;
  33336. + bindex = -1;
  33337. + brid = au_xino_brid(sb);
  33338. + if (brid >= 0)
  33339. + bindex = au_br_index(sb, brid);
  33340. + if (bindex >= 0) {
  33341. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  33342. + au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  33343. + } else {
  33344. + ldir->parent = dget_parent(xino->f_path.dentry);
  33345. + ldir->dir = d_inode(ldir->parent);
  33346. + inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
  33347. + }
  33348. +}
  33349. +
  33350. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  33351. +{
  33352. + if (ldir->hdir)
  33353. + au_hn_inode_unlock(ldir->hdir);
  33354. + else {
  33355. + inode_unlock(ldir->dir);
  33356. + dput(ldir->parent);
  33357. + }
  33358. +}
  33359. +
  33360. +/* ---------------------------------------------------------------------- */
  33361. +
  33362. +/* trucate xino files asynchronously */
  33363. +
  33364. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
  33365. +{
  33366. + int err;
  33367. + unsigned long jiffy;
  33368. + blkcnt_t blocks;
  33369. + aufs_bindex_t bi, bbot;
  33370. + struct kstatfs *st;
  33371. + struct au_branch *br;
  33372. + struct file *new_xino, *file;
  33373. + struct super_block *h_sb;
  33374. + struct au_xino_lock_dir ldir;
  33375. +
  33376. + err = -ENOMEM;
  33377. + st = kmalloc(sizeof(*st), GFP_NOFS);
  33378. + if (unlikely(!st))
  33379. + goto out;
  33380. +
  33381. + err = -EINVAL;
  33382. + bbot = au_sbbot(sb);
  33383. + if (unlikely(bindex < 0 || bbot < bindex))
  33384. + goto out_st;
  33385. + br = au_sbr(sb, bindex);
  33386. + file = br->br_xino.xi_file;
  33387. + if (!file)
  33388. + goto out_st;
  33389. +
  33390. + err = vfs_statfs(&file->f_path, st);
  33391. + if (unlikely(err))
  33392. + AuErr1("statfs err %d, ignored\n", err);
  33393. + jiffy = jiffies;
  33394. + blocks = file_inode(file)->i_blocks;
  33395. + pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  33396. + bindex, (u64)blocks, st->f_bfree, st->f_blocks);
  33397. +
  33398. + au_xino_lock_dir(sb, file, &ldir);
  33399. + /* mnt_want_write() is unnecessary here */
  33400. + new_xino = au_xino_create2(file, file);
  33401. + au_xino_unlock_dir(&ldir);
  33402. + err = PTR_ERR(new_xino);
  33403. + if (IS_ERR(new_xino)) {
  33404. + pr_err("err %d, ignored\n", err);
  33405. + goto out_st;
  33406. + }
  33407. + err = 0;
  33408. + fput(file);
  33409. + br->br_xino.xi_file = new_xino;
  33410. +
  33411. + h_sb = au_br_sb(br);
  33412. + for (bi = 0; bi <= bbot; bi++) {
  33413. + if (unlikely(bi == bindex))
  33414. + continue;
  33415. + br = au_sbr(sb, bi);
  33416. + if (au_br_sb(br) != h_sb)
  33417. + continue;
  33418. +
  33419. + fput(br->br_xino.xi_file);
  33420. + br->br_xino.xi_file = new_xino;
  33421. + get_file(new_xino);
  33422. + }
  33423. +
  33424. + err = vfs_statfs(&new_xino->f_path, st);
  33425. + if (!err) {
  33426. + pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  33427. + bindex, (u64)file_inode(new_xino)->i_blocks,
  33428. + st->f_bfree, st->f_blocks);
  33429. + if (file_inode(new_xino)->i_blocks < blocks)
  33430. + au_sbi(sb)->si_xino_jiffy = jiffy;
  33431. + } else
  33432. + AuErr1("statfs err %d, ignored\n", err);
  33433. +
  33434. +out_st:
  33435. + au_delayed_kfree(st);
  33436. +out:
  33437. + return err;
  33438. +}
  33439. +
  33440. +struct xino_do_trunc_args {
  33441. + struct super_block *sb;
  33442. + struct au_branch *br;
  33443. +};
  33444. +
  33445. +static void xino_do_trunc(void *_args)
  33446. +{
  33447. + struct xino_do_trunc_args *args = _args;
  33448. + struct super_block *sb;
  33449. + struct au_branch *br;
  33450. + struct inode *dir;
  33451. + int err;
  33452. + aufs_bindex_t bindex;
  33453. +
  33454. + err = 0;
  33455. + sb = args->sb;
  33456. + dir = d_inode(sb->s_root);
  33457. + br = args->br;
  33458. +
  33459. + si_noflush_write_lock(sb);
  33460. + ii_read_lock_parent(dir);
  33461. + bindex = au_br_index(sb, br->br_id);
  33462. + err = au_xino_trunc(sb, bindex);
  33463. + ii_read_unlock(dir);
  33464. + if (unlikely(err))
  33465. + pr_warn("err b%d, (%d)\n", bindex, err);
  33466. + atomic_dec(&br->br_xino_running);
  33467. + au_br_put(br);
  33468. + si_write_unlock(sb);
  33469. + au_nwt_done(&au_sbi(sb)->si_nowait);
  33470. + au_delayed_kfree(args);
  33471. +}
  33472. +
  33473. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  33474. +{
  33475. + int err;
  33476. + struct kstatfs st;
  33477. + struct au_sbinfo *sbinfo;
  33478. +
  33479. + /* todo: si_xino_expire and the ratio should be customizable */
  33480. + sbinfo = au_sbi(sb);
  33481. + if (time_before(jiffies,
  33482. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  33483. + return 0;
  33484. +
  33485. + /* truncation border */
  33486. + err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
  33487. + if (unlikely(err)) {
  33488. + AuErr1("statfs err %d, ignored\n", err);
  33489. + return 0;
  33490. + }
  33491. + if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
  33492. + return 0;
  33493. +
  33494. + return 1;
  33495. +}
  33496. +
  33497. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  33498. +{
  33499. + struct xino_do_trunc_args *args;
  33500. + int wkq_err;
  33501. +
  33502. + if (!xino_trunc_test(sb, br))
  33503. + return;
  33504. +
  33505. + if (atomic_inc_return(&br->br_xino_running) > 1)
  33506. + goto out;
  33507. +
  33508. + /* lock and kfree() will be called in trunc_xino() */
  33509. + args = kmalloc(sizeof(*args), GFP_NOFS);
  33510. + if (unlikely(!args)) {
  33511. + AuErr1("no memory\n");
  33512. + goto out;
  33513. + }
  33514. +
  33515. + au_br_get(br);
  33516. + args->sb = sb;
  33517. + args->br = br;
  33518. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  33519. + if (!wkq_err)
  33520. + return; /* success */
  33521. +
  33522. + pr_err("wkq %d\n", wkq_err);
  33523. + au_br_put(br);
  33524. + au_delayed_kfree(args);
  33525. +
  33526. +out:
  33527. + atomic_dec(&br->br_xino_running);
  33528. +}
  33529. +
  33530. +/* ---------------------------------------------------------------------- */
  33531. +
  33532. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  33533. + ino_t h_ino, ino_t ino)
  33534. +{
  33535. + loff_t pos;
  33536. + ssize_t sz;
  33537. +
  33538. + pos = h_ino;
  33539. + if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
  33540. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  33541. + return -EFBIG;
  33542. + }
  33543. + pos *= sizeof(ino);
  33544. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
  33545. + if (sz == sizeof(ino))
  33546. + return 0; /* success */
  33547. +
  33548. + AuIOErr("write failed (%zd)\n", sz);
  33549. + return -EIO;
  33550. +}
  33551. +
  33552. +/*
  33553. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  33554. + * at the position of @h_ino.
  33555. + * even if @ino is zero, it is written to the xinofile and means no entry.
  33556. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  33557. + * try truncating it.
  33558. + */
  33559. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  33560. + ino_t ino)
  33561. +{
  33562. + int err;
  33563. + unsigned int mnt_flags;
  33564. + struct au_branch *br;
  33565. +
  33566. + BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
  33567. + || ((loff_t)-1) > 0);
  33568. + SiMustAnyLock(sb);
  33569. +
  33570. + mnt_flags = au_mntflags(sb);
  33571. + if (!au_opt_test(mnt_flags, XINO))
  33572. + return 0;
  33573. +
  33574. + br = au_sbr(sb, bindex);
  33575. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  33576. + h_ino, ino);
  33577. + if (!err) {
  33578. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  33579. + && au_test_fs_trunc_xino(au_br_sb(br)))
  33580. + xino_try_trunc(sb, br);
  33581. + return 0; /* success */
  33582. + }
  33583. +
  33584. + AuIOErr("write failed (%d)\n", err);
  33585. + return -EIO;
  33586. +}
  33587. +
  33588. +/* ---------------------------------------------------------------------- */
  33589. +
  33590. +/* aufs inode number bitmap */
  33591. +
  33592. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  33593. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  33594. +{
  33595. + ino_t ino;
  33596. +
  33597. + AuDebugOn(bit < 0 || page_bits <= bit);
  33598. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  33599. + return ino;
  33600. +}
  33601. +
  33602. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  33603. +{
  33604. + AuDebugOn(ino < AUFS_FIRST_INO);
  33605. + ino -= AUFS_FIRST_INO;
  33606. + *pindex = ino / page_bits;
  33607. + *bit = ino % page_bits;
  33608. +}
  33609. +
  33610. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  33611. +{
  33612. + int err;
  33613. + loff_t pos;
  33614. + ssize_t sz;
  33615. + struct au_sbinfo *sbinfo;
  33616. + struct file *xib;
  33617. + unsigned long *p;
  33618. +
  33619. + sbinfo = au_sbi(sb);
  33620. + MtxMustLock(&sbinfo->si_xib_mtx);
  33621. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  33622. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  33623. +
  33624. + if (pindex == sbinfo->si_xib_last_pindex)
  33625. + return 0;
  33626. +
  33627. + xib = sbinfo->si_xib;
  33628. + p = sbinfo->si_xib_buf;
  33629. + pos = sbinfo->si_xib_last_pindex;
  33630. + pos *= PAGE_SIZE;
  33631. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  33632. + if (unlikely(sz != PAGE_SIZE))
  33633. + goto out;
  33634. +
  33635. + pos = pindex;
  33636. + pos *= PAGE_SIZE;
  33637. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  33638. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  33639. + else {
  33640. + memset(p, 0, PAGE_SIZE);
  33641. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  33642. + }
  33643. + if (sz == PAGE_SIZE) {
  33644. + sbinfo->si_xib_last_pindex = pindex;
  33645. + return 0; /* success */
  33646. + }
  33647. +
  33648. +out:
  33649. + AuIOErr1("write failed (%zd)\n", sz);
  33650. + err = sz;
  33651. + if (sz >= 0)
  33652. + err = -EIO;
  33653. + return err;
  33654. +}
  33655. +
  33656. +/* ---------------------------------------------------------------------- */
  33657. +
  33658. +static void au_xib_clear_bit(struct inode *inode)
  33659. +{
  33660. + int err, bit;
  33661. + unsigned long pindex;
  33662. + struct super_block *sb;
  33663. + struct au_sbinfo *sbinfo;
  33664. +
  33665. + AuDebugOn(inode->i_nlink);
  33666. +
  33667. + sb = inode->i_sb;
  33668. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  33669. + AuDebugOn(page_bits <= bit);
  33670. + sbinfo = au_sbi(sb);
  33671. + mutex_lock(&sbinfo->si_xib_mtx);
  33672. + err = xib_pindex(sb, pindex);
  33673. + if (!err) {
  33674. + clear_bit(bit, sbinfo->si_xib_buf);
  33675. + sbinfo->si_xib_next_bit = bit;
  33676. + }
  33677. + mutex_unlock(&sbinfo->si_xib_mtx);
  33678. +}
  33679. +
  33680. +/* for s_op->delete_inode() */
  33681. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  33682. +{
  33683. + int err;
  33684. + unsigned int mnt_flags;
  33685. + aufs_bindex_t bindex, bbot, bi;
  33686. + unsigned char try_trunc;
  33687. + struct au_iinfo *iinfo;
  33688. + struct super_block *sb;
  33689. + struct au_hinode *hi;
  33690. + struct inode *h_inode;
  33691. + struct au_branch *br;
  33692. + vfs_writef_t xwrite;
  33693. +
  33694. + AuDebugOn(au_is_bad_inode(inode));
  33695. +
  33696. + sb = inode->i_sb;
  33697. + mnt_flags = au_mntflags(sb);
  33698. + if (!au_opt_test(mnt_flags, XINO)
  33699. + || inode->i_ino == AUFS_ROOT_INO)
  33700. + return;
  33701. +
  33702. + if (unlinked) {
  33703. + au_xigen_inc(inode);
  33704. + au_xib_clear_bit(inode);
  33705. + }
  33706. +
  33707. + iinfo = au_ii(inode);
  33708. + bindex = iinfo->ii_btop;
  33709. + if (bindex < 0)
  33710. + return;
  33711. +
  33712. + xwrite = au_sbi(sb)->si_xwrite;
  33713. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  33714. + hi = au_hinode(iinfo, bindex);
  33715. + bbot = iinfo->ii_bbot;
  33716. + for (; bindex <= bbot; bindex++, hi++) {
  33717. + h_inode = hi->hi_inode;
  33718. + if (!h_inode
  33719. + || (!unlinked && h_inode->i_nlink))
  33720. + continue;
  33721. +
  33722. + /* inode may not be revalidated */
  33723. + bi = au_br_index(sb, hi->hi_id);
  33724. + if (bi < 0)
  33725. + continue;
  33726. +
  33727. + br = au_sbr(sb, bi);
  33728. + err = au_xino_do_write(xwrite, br->br_xino.xi_file,
  33729. + h_inode->i_ino, /*ino*/0);
  33730. + if (!err && try_trunc
  33731. + && au_test_fs_trunc_xino(au_br_sb(br)))
  33732. + xino_try_trunc(sb, br);
  33733. + }
  33734. +}
  33735. +
  33736. +/* get an unused inode number from bitmap */
  33737. +ino_t au_xino_new_ino(struct super_block *sb)
  33738. +{
  33739. + ino_t ino;
  33740. + unsigned long *p, pindex, ul, pend;
  33741. + struct au_sbinfo *sbinfo;
  33742. + struct file *file;
  33743. + int free_bit, err;
  33744. +
  33745. + if (!au_opt_test(au_mntflags(sb), XINO))
  33746. + return iunique(sb, AUFS_FIRST_INO);
  33747. +
  33748. + sbinfo = au_sbi(sb);
  33749. + mutex_lock(&sbinfo->si_xib_mtx);
  33750. + p = sbinfo->si_xib_buf;
  33751. + free_bit = sbinfo->si_xib_next_bit;
  33752. + if (free_bit < page_bits && !test_bit(free_bit, p))
  33753. + goto out; /* success */
  33754. + free_bit = find_first_zero_bit(p, page_bits);
  33755. + if (free_bit < page_bits)
  33756. + goto out; /* success */
  33757. +
  33758. + pindex = sbinfo->si_xib_last_pindex;
  33759. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  33760. + err = xib_pindex(sb, ul);
  33761. + if (unlikely(err))
  33762. + goto out_err;
  33763. + free_bit = find_first_zero_bit(p, page_bits);
  33764. + if (free_bit < page_bits)
  33765. + goto out; /* success */
  33766. + }
  33767. +
  33768. + file = sbinfo->si_xib;
  33769. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  33770. + for (ul = pindex + 1; ul <= pend; ul++) {
  33771. + err = xib_pindex(sb, ul);
  33772. + if (unlikely(err))
  33773. + goto out_err;
  33774. + free_bit = find_first_zero_bit(p, page_bits);
  33775. + if (free_bit < page_bits)
  33776. + goto out; /* success */
  33777. + }
  33778. + BUG();
  33779. +
  33780. +out:
  33781. + set_bit(free_bit, p);
  33782. + sbinfo->si_xib_next_bit = free_bit + 1;
  33783. + pindex = sbinfo->si_xib_last_pindex;
  33784. + mutex_unlock(&sbinfo->si_xib_mtx);
  33785. + ino = xib_calc_ino(pindex, free_bit);
  33786. + AuDbg("i%lu\n", (unsigned long)ino);
  33787. + return ino;
  33788. +out_err:
  33789. + mutex_unlock(&sbinfo->si_xib_mtx);
  33790. + AuDbg("i0\n");
  33791. + return 0;
  33792. +}
  33793. +
  33794. +/*
  33795. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  33796. + * at the position of @h_ino.
  33797. + * if @ino does not exist and @do_new is true, get new one.
  33798. + */
  33799. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  33800. + ino_t *ino)
  33801. +{
  33802. + int err;
  33803. + ssize_t sz;
  33804. + loff_t pos;
  33805. + struct file *file;
  33806. + struct au_sbinfo *sbinfo;
  33807. +
  33808. + *ino = 0;
  33809. + if (!au_opt_test(au_mntflags(sb), XINO))
  33810. + return 0; /* no xino */
  33811. +
  33812. + err = 0;
  33813. + sbinfo = au_sbi(sb);
  33814. + pos = h_ino;
  33815. + if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
  33816. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  33817. + return -EFBIG;
  33818. + }
  33819. + pos *= sizeof(*ino);
  33820. +
  33821. + file = au_sbr(sb, bindex)->br_xino.xi_file;
  33822. + if (vfsub_f_size_read(file) < pos + sizeof(*ino))
  33823. + return 0; /* no ino */
  33824. +
  33825. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
  33826. + if (sz == sizeof(*ino))
  33827. + return 0; /* success */
  33828. +
  33829. + err = sz;
  33830. + if (unlikely(sz >= 0)) {
  33831. + err = -EIO;
  33832. + AuIOErr("xino read error (%zd)\n", sz);
  33833. + }
  33834. +
  33835. + return err;
  33836. +}
  33837. +
  33838. +/* ---------------------------------------------------------------------- */
  33839. +
  33840. +/* create and set a new xino file */
  33841. +
  33842. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
  33843. +{
  33844. + struct file *file;
  33845. + struct dentry *h_parent, *d;
  33846. + struct inode *h_dir, *inode;
  33847. + int err;
  33848. +
  33849. + /*
  33850. + * at mount-time, and the xino file is the default path,
  33851. + * hnotify is disabled so we have no notify events to ignore.
  33852. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  33853. + */
  33854. + file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  33855. + /* | __FMODE_NONOTIFY */,
  33856. + S_IRUGO | S_IWUGO);
  33857. + if (IS_ERR(file)) {
  33858. + if (!silent)
  33859. + pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
  33860. + return file;
  33861. + }
  33862. +
  33863. + /* keep file count */
  33864. + err = 0;
  33865. + inode = file_inode(file);
  33866. + h_parent = dget_parent(file->f_path.dentry);
  33867. + h_dir = d_inode(h_parent);
  33868. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  33869. + /* mnt_want_write() is unnecessary here */
  33870. + /* no delegation since it is just created */
  33871. + if (inode->i_nlink)
  33872. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  33873. + /*force*/0);
  33874. + inode_unlock(h_dir);
  33875. + dput(h_parent);
  33876. + if (unlikely(err)) {
  33877. + if (!silent)
  33878. + pr_err("unlink %s(%d)\n", fname, err);
  33879. + goto out;
  33880. + }
  33881. +
  33882. + err = -EINVAL;
  33883. + d = file->f_path.dentry;
  33884. + if (unlikely(sb == d->d_sb)) {
  33885. + if (!silent)
  33886. + pr_err("%s must be outside\n", fname);
  33887. + goto out;
  33888. + }
  33889. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  33890. + if (!silent)
  33891. + pr_err("xino doesn't support %s(%s)\n",
  33892. + fname, au_sbtype(d->d_sb));
  33893. + goto out;
  33894. + }
  33895. + return file; /* success */
  33896. +
  33897. +out:
  33898. + fput(file);
  33899. + file = ERR_PTR(err);
  33900. + return file;
  33901. +}
  33902. +
  33903. +/*
  33904. + * find another branch who is on the same filesystem of the specified
  33905. + * branch{@btgt}. search until @bbot.
  33906. + */
  33907. +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  33908. + aufs_bindex_t bbot)
  33909. +{
  33910. + aufs_bindex_t bindex;
  33911. + struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
  33912. +
  33913. + for (bindex = 0; bindex < btgt; bindex++)
  33914. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  33915. + return bindex;
  33916. + for (bindex++; bindex <= bbot; bindex++)
  33917. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  33918. + return bindex;
  33919. + return -1;
  33920. +}
  33921. +
  33922. +/* ---------------------------------------------------------------------- */
  33923. +
  33924. +/*
  33925. + * initialize the xinofile for the specified branch @br
  33926. + * at the place/path where @base_file indicates.
  33927. + * test whether another branch is on the same filesystem or not,
  33928. + * if @do_test is true.
  33929. + */
  33930. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  33931. + struct file *base_file, int do_test)
  33932. +{
  33933. + int err;
  33934. + ino_t ino;
  33935. + aufs_bindex_t bbot, bindex;
  33936. + struct au_branch *shared_br, *b;
  33937. + struct file *file;
  33938. + struct super_block *tgt_sb;
  33939. +
  33940. + shared_br = NULL;
  33941. + bbot = au_sbbot(sb);
  33942. + if (do_test) {
  33943. + tgt_sb = au_br_sb(br);
  33944. + for (bindex = 0; bindex <= bbot; bindex++) {
  33945. + b = au_sbr(sb, bindex);
  33946. + if (tgt_sb == au_br_sb(b)) {
  33947. + shared_br = b;
  33948. + break;
  33949. + }
  33950. + }
  33951. + }
  33952. +
  33953. + if (!shared_br || !shared_br->br_xino.xi_file) {
  33954. + struct au_xino_lock_dir ldir;
  33955. +
  33956. + au_xino_lock_dir(sb, base_file, &ldir);
  33957. + /* mnt_want_write() is unnecessary here */
  33958. + file = au_xino_create2(base_file, NULL);
  33959. + au_xino_unlock_dir(&ldir);
  33960. + err = PTR_ERR(file);
  33961. + if (IS_ERR(file))
  33962. + goto out;
  33963. + br->br_xino.xi_file = file;
  33964. + } else {
  33965. + br->br_xino.xi_file = shared_br->br_xino.xi_file;
  33966. + get_file(br->br_xino.xi_file);
  33967. + }
  33968. +
  33969. + ino = AUFS_ROOT_INO;
  33970. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  33971. + h_ino, ino);
  33972. + if (unlikely(err)) {
  33973. + fput(br->br_xino.xi_file);
  33974. + br->br_xino.xi_file = NULL;
  33975. + }
  33976. +
  33977. +out:
  33978. + return err;
  33979. +}
  33980. +
  33981. +/* ---------------------------------------------------------------------- */
  33982. +
  33983. +/* trucate a xino bitmap file */
  33984. +
  33985. +/* todo: slow */
  33986. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  33987. +{
  33988. + int err, bit;
  33989. + ssize_t sz;
  33990. + unsigned long pindex;
  33991. + loff_t pos, pend;
  33992. + struct au_sbinfo *sbinfo;
  33993. + vfs_readf_t func;
  33994. + ino_t *ino;
  33995. + unsigned long *p;
  33996. +
  33997. + err = 0;
  33998. + sbinfo = au_sbi(sb);
  33999. + MtxMustLock(&sbinfo->si_xib_mtx);
  34000. + p = sbinfo->si_xib_buf;
  34001. + func = sbinfo->si_xread;
  34002. + pend = vfsub_f_size_read(file);
  34003. + pos = 0;
  34004. + while (pos < pend) {
  34005. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  34006. + err = sz;
  34007. + if (unlikely(sz <= 0))
  34008. + goto out;
  34009. +
  34010. + err = 0;
  34011. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  34012. + if (unlikely(*ino < AUFS_FIRST_INO))
  34013. + continue;
  34014. +
  34015. + xib_calc_bit(*ino, &pindex, &bit);
  34016. + AuDebugOn(page_bits <= bit);
  34017. + err = xib_pindex(sb, pindex);
  34018. + if (!err)
  34019. + set_bit(bit, p);
  34020. + else
  34021. + goto out;
  34022. + }
  34023. + }
  34024. +
  34025. +out:
  34026. + return err;
  34027. +}
  34028. +
  34029. +static int xib_restore(struct super_block *sb)
  34030. +{
  34031. + int err;
  34032. + aufs_bindex_t bindex, bbot;
  34033. + void *page;
  34034. +
  34035. + err = -ENOMEM;
  34036. + page = (void *)__get_free_page(GFP_NOFS);
  34037. + if (unlikely(!page))
  34038. + goto out;
  34039. +
  34040. + err = 0;
  34041. + bbot = au_sbbot(sb);
  34042. + for (bindex = 0; !err && bindex <= bbot; bindex++)
  34043. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
  34044. + err = do_xib_restore
  34045. + (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
  34046. + else
  34047. + AuDbg("b%d\n", bindex);
  34048. + au_delayed_free_page((unsigned long)page);
  34049. +
  34050. +out:
  34051. + return err;
  34052. +}
  34053. +
  34054. +int au_xib_trunc(struct super_block *sb)
  34055. +{
  34056. + int err;
  34057. + ssize_t sz;
  34058. + loff_t pos;
  34059. + struct au_xino_lock_dir ldir;
  34060. + struct au_sbinfo *sbinfo;
  34061. + unsigned long *p;
  34062. + struct file *file;
  34063. +
  34064. + SiMustWriteLock(sb);
  34065. +
  34066. + err = 0;
  34067. + sbinfo = au_sbi(sb);
  34068. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  34069. + goto out;
  34070. +
  34071. + file = sbinfo->si_xib;
  34072. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  34073. + goto out;
  34074. +
  34075. + au_xino_lock_dir(sb, file, &ldir);
  34076. + /* mnt_want_write() is unnecessary here */
  34077. + file = au_xino_create2(sbinfo->si_xib, NULL);
  34078. + au_xino_unlock_dir(&ldir);
  34079. + err = PTR_ERR(file);
  34080. + if (IS_ERR(file))
  34081. + goto out;
  34082. + fput(sbinfo->si_xib);
  34083. + sbinfo->si_xib = file;
  34084. +
  34085. + p = sbinfo->si_xib_buf;
  34086. + memset(p, 0, PAGE_SIZE);
  34087. + pos = 0;
  34088. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  34089. + if (unlikely(sz != PAGE_SIZE)) {
  34090. + err = sz;
  34091. + AuIOErr("err %d\n", err);
  34092. + if (sz >= 0)
  34093. + err = -EIO;
  34094. + goto out;
  34095. + }
  34096. +
  34097. + mutex_lock(&sbinfo->si_xib_mtx);
  34098. + /* mnt_want_write() is unnecessary here */
  34099. + err = xib_restore(sb);
  34100. + mutex_unlock(&sbinfo->si_xib_mtx);
  34101. +
  34102. +out:
  34103. + return err;
  34104. +}
  34105. +
  34106. +/* ---------------------------------------------------------------------- */
  34107. +
  34108. +/*
  34109. + * xino mount option handlers
  34110. + */
  34111. +
  34112. +/* xino bitmap */
  34113. +static void xino_clear_xib(struct super_block *sb)
  34114. +{
  34115. + struct au_sbinfo *sbinfo;
  34116. +
  34117. + SiMustWriteLock(sb);
  34118. +
  34119. + sbinfo = au_sbi(sb);
  34120. + sbinfo->si_xread = NULL;
  34121. + sbinfo->si_xwrite = NULL;
  34122. + if (sbinfo->si_xib)
  34123. + fput(sbinfo->si_xib);
  34124. + sbinfo->si_xib = NULL;
  34125. + if (sbinfo->si_xib_buf)
  34126. + au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
  34127. + sbinfo->si_xib_buf = NULL;
  34128. +}
  34129. +
  34130. +static int au_xino_set_xib(struct super_block *sb, struct file *base)
  34131. +{
  34132. + int err;
  34133. + loff_t pos;
  34134. + struct au_sbinfo *sbinfo;
  34135. + struct file *file;
  34136. +
  34137. + SiMustWriteLock(sb);
  34138. +
  34139. + sbinfo = au_sbi(sb);
  34140. + file = au_xino_create2(base, sbinfo->si_xib);
  34141. + err = PTR_ERR(file);
  34142. + if (IS_ERR(file))
  34143. + goto out;
  34144. + if (sbinfo->si_xib)
  34145. + fput(sbinfo->si_xib);
  34146. + sbinfo->si_xib = file;
  34147. + sbinfo->si_xread = vfs_readf(file);
  34148. + sbinfo->si_xwrite = vfs_writef(file);
  34149. +
  34150. + err = -ENOMEM;
  34151. + if (!sbinfo->si_xib_buf)
  34152. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  34153. + if (unlikely(!sbinfo->si_xib_buf))
  34154. + goto out_unset;
  34155. +
  34156. + sbinfo->si_xib_last_pindex = 0;
  34157. + sbinfo->si_xib_next_bit = 0;
  34158. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  34159. + pos = 0;
  34160. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  34161. + PAGE_SIZE, &pos);
  34162. + if (unlikely(err != PAGE_SIZE))
  34163. + goto out_free;
  34164. + }
  34165. + err = 0;
  34166. + goto out; /* success */
  34167. +
  34168. +out_free:
  34169. + if (sbinfo->si_xib_buf)
  34170. + au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
  34171. + sbinfo->si_xib_buf = NULL;
  34172. + if (err >= 0)
  34173. + err = -EIO;
  34174. +out_unset:
  34175. + fput(sbinfo->si_xib);
  34176. + sbinfo->si_xib = NULL;
  34177. + sbinfo->si_xread = NULL;
  34178. + sbinfo->si_xwrite = NULL;
  34179. +out:
  34180. + return err;
  34181. +}
  34182. +
  34183. +/* xino for each branch */
  34184. +static void xino_clear_br(struct super_block *sb)
  34185. +{
  34186. + aufs_bindex_t bindex, bbot;
  34187. + struct au_branch *br;
  34188. +
  34189. + bbot = au_sbbot(sb);
  34190. + for (bindex = 0; bindex <= bbot; bindex++) {
  34191. + br = au_sbr(sb, bindex);
  34192. + if (!br || !br->br_xino.xi_file)
  34193. + continue;
  34194. +
  34195. + fput(br->br_xino.xi_file);
  34196. + br->br_xino.xi_file = NULL;
  34197. + }
  34198. +}
  34199. +
  34200. +static int au_xino_set_br(struct super_block *sb, struct file *base)
  34201. +{
  34202. + int err;
  34203. + ino_t ino;
  34204. + aufs_bindex_t bindex, bbot, bshared;
  34205. + struct {
  34206. + struct file *old, *new;
  34207. + } *fpair, *p;
  34208. + struct au_branch *br;
  34209. + struct inode *inode;
  34210. + vfs_writef_t writef;
  34211. +
  34212. + SiMustWriteLock(sb);
  34213. +
  34214. + err = -ENOMEM;
  34215. + bbot = au_sbbot(sb);
  34216. + fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
  34217. + if (unlikely(!fpair))
  34218. + goto out;
  34219. +
  34220. + inode = d_inode(sb->s_root);
  34221. + ino = AUFS_ROOT_INO;
  34222. + writef = au_sbi(sb)->si_xwrite;
  34223. + for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
  34224. + bshared = is_sb_shared(sb, bindex, bindex - 1);
  34225. + if (bshared >= 0) {
  34226. + /* shared xino */
  34227. + *p = fpair[bshared];
  34228. + get_file(p->new);
  34229. + }
  34230. +
  34231. + if (!p->new) {
  34232. + /* new xino */
  34233. + br = au_sbr(sb, bindex);
  34234. + p->old = br->br_xino.xi_file;
  34235. + p->new = au_xino_create2(base, br->br_xino.xi_file);
  34236. + err = PTR_ERR(p->new);
  34237. + if (IS_ERR(p->new)) {
  34238. + p->new = NULL;
  34239. + goto out_pair;
  34240. + }
  34241. + }
  34242. +
  34243. + err = au_xino_do_write(writef, p->new,
  34244. + au_h_iptr(inode, bindex)->i_ino, ino);
  34245. + if (unlikely(err))
  34246. + goto out_pair;
  34247. + }
  34248. +
  34249. + for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
  34250. + br = au_sbr(sb, bindex);
  34251. + if (br->br_xino.xi_file)
  34252. + fput(br->br_xino.xi_file);
  34253. + get_file(p->new);
  34254. + br->br_xino.xi_file = p->new;
  34255. + }
  34256. +
  34257. +out_pair:
  34258. + for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
  34259. + if (p->new)
  34260. + fput(p->new);
  34261. + else
  34262. + break;
  34263. + au_delayed_kfree(fpair);
  34264. +out:
  34265. + return err;
  34266. +}
  34267. +
  34268. +void au_xino_clr(struct super_block *sb)
  34269. +{
  34270. + struct au_sbinfo *sbinfo;
  34271. +
  34272. + au_xigen_clr(sb);
  34273. + xino_clear_xib(sb);
  34274. + xino_clear_br(sb);
  34275. + sbinfo = au_sbi(sb);
  34276. + /* lvalue, do not call au_mntflags() */
  34277. + au_opt_clr(sbinfo->si_mntflags, XINO);
  34278. +}
  34279. +
  34280. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
  34281. +{
  34282. + int err, skip;
  34283. + struct dentry *parent, *cur_parent;
  34284. + struct qstr *dname, *cur_name;
  34285. + struct file *cur_xino;
  34286. + struct inode *dir;
  34287. + struct au_sbinfo *sbinfo;
  34288. +
  34289. + SiMustWriteLock(sb);
  34290. +
  34291. + err = 0;
  34292. + sbinfo = au_sbi(sb);
  34293. + parent = dget_parent(xino->file->f_path.dentry);
  34294. + if (remount) {
  34295. + skip = 0;
  34296. + dname = &xino->file->f_path.dentry->d_name;
  34297. + cur_xino = sbinfo->si_xib;
  34298. + if (cur_xino) {
  34299. + cur_parent = dget_parent(cur_xino->f_path.dentry);
  34300. + cur_name = &cur_xino->f_path.dentry->d_name;
  34301. + skip = (cur_parent == parent
  34302. + && au_qstreq(dname, cur_name));
  34303. + dput(cur_parent);
  34304. + }
  34305. + if (skip)
  34306. + goto out;
  34307. + }
  34308. +
  34309. + au_opt_set(sbinfo->si_mntflags, XINO);
  34310. + dir = d_inode(parent);
  34311. + inode_lock_nested(dir, AuLsc_I_PARENT);
  34312. + /* mnt_want_write() is unnecessary here */
  34313. + err = au_xino_set_xib(sb, xino->file);
  34314. + if (!err)
  34315. + err = au_xigen_set(sb, xino->file);
  34316. + if (!err)
  34317. + err = au_xino_set_br(sb, xino->file);
  34318. + inode_unlock(dir);
  34319. + if (!err)
  34320. + goto out; /* success */
  34321. +
  34322. + /* reset all */
  34323. + AuIOErr("failed creating xino(%d).\n", err);
  34324. + au_xigen_clr(sb);
  34325. + xino_clear_xib(sb);
  34326. +
  34327. +out:
  34328. + dput(parent);
  34329. + return err;
  34330. +}
  34331. +
  34332. +/* ---------------------------------------------------------------------- */
  34333. +
  34334. +/*
  34335. + * create a xinofile at the default place/path.
  34336. + */
  34337. +struct file *au_xino_def(struct super_block *sb)
  34338. +{
  34339. + struct file *file;
  34340. + char *page, *p;
  34341. + struct au_branch *br;
  34342. + struct super_block *h_sb;
  34343. + struct path path;
  34344. + aufs_bindex_t bbot, bindex, bwr;
  34345. +
  34346. + br = NULL;
  34347. + bbot = au_sbbot(sb);
  34348. + bwr = -1;
  34349. + for (bindex = 0; bindex <= bbot; bindex++) {
  34350. + br = au_sbr(sb, bindex);
  34351. + if (au_br_writable(br->br_perm)
  34352. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  34353. + bwr = bindex;
  34354. + break;
  34355. + }
  34356. + }
  34357. +
  34358. + if (bwr >= 0) {
  34359. + file = ERR_PTR(-ENOMEM);
  34360. + page = (void *)__get_free_page(GFP_NOFS);
  34361. + if (unlikely(!page))
  34362. + goto out;
  34363. + path.mnt = au_br_mnt(br);
  34364. + path.dentry = au_h_dptr(sb->s_root, bwr);
  34365. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  34366. + file = (void *)p;
  34367. + if (!IS_ERR(p)) {
  34368. + strcat(p, "/" AUFS_XINO_FNAME);
  34369. + AuDbg("%s\n", p);
  34370. + file = au_xino_create(sb, p, /*silent*/0);
  34371. + if (!IS_ERR(file))
  34372. + au_xino_brid_set(sb, br->br_id);
  34373. + }
  34374. + au_delayed_free_page((unsigned long)page);
  34375. + } else {
  34376. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  34377. + if (IS_ERR(file))
  34378. + goto out;
  34379. + h_sb = file->f_path.dentry->d_sb;
  34380. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  34381. + pr_err("xino doesn't support %s(%s)\n",
  34382. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  34383. + fput(file);
  34384. + file = ERR_PTR(-EINVAL);
  34385. + }
  34386. + if (!IS_ERR(file))
  34387. + au_xino_brid_set(sb, -1);
  34388. + }
  34389. +
  34390. +out:
  34391. + return file;
  34392. +}
  34393. +
  34394. +/* ---------------------------------------------------------------------- */
  34395. +
  34396. +int au_xino_path(struct seq_file *seq, struct file *file)
  34397. +{
  34398. + int err;
  34399. +
  34400. + err = au_seq_path(seq, &file->f_path);
  34401. + if (unlikely(err))
  34402. + goto out;
  34403. +
  34404. +#define Deleted "\\040(deleted)"
  34405. + seq->count -= sizeof(Deleted) - 1;
  34406. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  34407. + sizeof(Deleted) - 1));
  34408. +#undef Deleted
  34409. +
  34410. +out:
  34411. + return err;
  34412. +}
  34413. diff -Naur null/include/uapi/linux/aufs_type.h linux-4.7/include/uapi/linux/aufs_type.h
  34414. --- /dev/null
  34415. +++ linux-4.7/include/uapi/linux/aufs_type.h 2016-08-08 12:57:44.611384474 -0400
  34416. @@ -0,0 +1,419 @@
  34417. +/*
  34418. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  34419. + *
  34420. + * This program, aufs is free software; you can redistribute it and/or modify
  34421. + * it under the terms of the GNU General Public License as published by
  34422. + * the Free Software Foundation; either version 2 of the License, or
  34423. + * (at your option) any later version.
  34424. + *
  34425. + * This program is distributed in the hope that it will be useful,
  34426. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34427. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34428. + * GNU General Public License for more details.
  34429. + *
  34430. + * You should have received a copy of the GNU General Public License
  34431. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34432. + */
  34433. +
  34434. +#ifndef __AUFS_TYPE_H__
  34435. +#define __AUFS_TYPE_H__
  34436. +
  34437. +#define AUFS_NAME "aufs"
  34438. +
  34439. +#ifdef __KERNEL__
  34440. +/*
  34441. + * define it before including all other headers.
  34442. + * sched.h may use pr_* macros before defining "current", so define the
  34443. + * no-current version first, and re-define later.
  34444. + */
  34445. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  34446. +#include <linux/sched.h>
  34447. +#undef pr_fmt
  34448. +#define pr_fmt(fmt) \
  34449. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  34450. + (int)sizeof(current->comm), current->comm, current->pid
  34451. +#else
  34452. +#include <stdint.h>
  34453. +#include <sys/types.h>
  34454. +#endif /* __KERNEL__ */
  34455. +
  34456. +#include <linux/limits.h>
  34457. +
  34458. +#define AUFS_VERSION "4.7-20160808"
  34459. +
  34460. +/* todo? move this to linux-2.6.19/include/magic.h */
  34461. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  34462. +
  34463. +/* ---------------------------------------------------------------------- */
  34464. +
  34465. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  34466. +typedef int8_t aufs_bindex_t;
  34467. +#define AUFS_BRANCH_MAX 127
  34468. +#else
  34469. +typedef int16_t aufs_bindex_t;
  34470. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  34471. +#define AUFS_BRANCH_MAX 511
  34472. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  34473. +#define AUFS_BRANCH_MAX 1023
  34474. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  34475. +#define AUFS_BRANCH_MAX 32767
  34476. +#endif
  34477. +#endif
  34478. +
  34479. +#ifdef __KERNEL__
  34480. +#ifndef AUFS_BRANCH_MAX
  34481. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  34482. +#endif
  34483. +#endif /* __KERNEL__ */
  34484. +
  34485. +/* ---------------------------------------------------------------------- */
  34486. +
  34487. +#define AUFS_FSTYPE AUFS_NAME
  34488. +
  34489. +#define AUFS_ROOT_INO 2
  34490. +#define AUFS_FIRST_INO 11
  34491. +
  34492. +#define AUFS_WH_PFX ".wh."
  34493. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  34494. +#define AUFS_WH_TMP_LEN 4
  34495. +/* a limit for rmdir/rename a dir and copyup */
  34496. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  34497. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  34498. + - 1 /* dot */\
  34499. + - AUFS_WH_TMP_LEN) /* hex */
  34500. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  34501. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  34502. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  34503. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  34504. +#define AUFS_DIRWH_DEF 3
  34505. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  34506. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  34507. +#define AUFS_RDBLK_DEF 512 /* bytes */
  34508. +#define AUFS_RDHASH_DEF 32
  34509. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  34510. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  34511. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  34512. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  34513. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  34514. +
  34515. +/* pseudo-link maintenace under /proc */
  34516. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  34517. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  34518. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  34519. +
  34520. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  34521. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  34522. +
  34523. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  34524. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  34525. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  34526. +
  34527. +/* doubly whiteouted */
  34528. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  34529. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  34530. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  34531. +
  34532. +/* branch permissions and attributes */
  34533. +#define AUFS_BRPERM_RW "rw"
  34534. +#define AUFS_BRPERM_RO "ro"
  34535. +#define AUFS_BRPERM_RR "rr"
  34536. +#define AUFS_BRATTR_COO_REG "coo_reg"
  34537. +#define AUFS_BRATTR_COO_ALL "coo_all"
  34538. +#define AUFS_BRATTR_FHSM "fhsm"
  34539. +#define AUFS_BRATTR_UNPIN "unpin"
  34540. +#define AUFS_BRATTR_ICEX "icex"
  34541. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  34542. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  34543. +#define AUFS_BRATTR_ICEX_TR "icextr"
  34544. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  34545. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  34546. +#define AUFS_BRRATTR_WH "wh"
  34547. +#define AUFS_BRWATTR_NLWH "nolwh"
  34548. +#define AUFS_BRWATTR_MOO "moo"
  34549. +
  34550. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  34551. +#define AuBrPerm_RO (1 << 1) /* readonly */
  34552. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  34553. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  34554. +
  34555. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  34556. +#define AuBrAttr_COO_ALL (1 << 4)
  34557. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  34558. +
  34559. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  34560. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  34561. + branch. meaningless since
  34562. + linux-3.18-rc1 */
  34563. +
  34564. +/* ignore error in copying XATTR */
  34565. +#define AuBrAttr_ICEX_SEC (1 << 7)
  34566. +#define AuBrAttr_ICEX_SYS (1 << 8)
  34567. +#define AuBrAttr_ICEX_TR (1 << 9)
  34568. +#define AuBrAttr_ICEX_USR (1 << 10)
  34569. +#define AuBrAttr_ICEX_OTH (1 << 11)
  34570. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  34571. + | AuBrAttr_ICEX_SYS \
  34572. + | AuBrAttr_ICEX_TR \
  34573. + | AuBrAttr_ICEX_USR \
  34574. + | AuBrAttr_ICEX_OTH)
  34575. +
  34576. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  34577. +#define AuBrRAttr_Mask AuBrRAttr_WH
  34578. +
  34579. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  34580. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  34581. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  34582. +
  34583. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  34584. +
  34585. +/* #warning test userspace */
  34586. +#ifdef __KERNEL__
  34587. +#ifndef CONFIG_AUFS_FHSM
  34588. +#undef AuBrAttr_FHSM
  34589. +#define AuBrAttr_FHSM 0
  34590. +#endif
  34591. +#ifndef CONFIG_AUFS_XATTR
  34592. +#undef AuBrAttr_ICEX
  34593. +#define AuBrAttr_ICEX 0
  34594. +#undef AuBrAttr_ICEX_SEC
  34595. +#define AuBrAttr_ICEX_SEC 0
  34596. +#undef AuBrAttr_ICEX_SYS
  34597. +#define AuBrAttr_ICEX_SYS 0
  34598. +#undef AuBrAttr_ICEX_TR
  34599. +#define AuBrAttr_ICEX_TR 0
  34600. +#undef AuBrAttr_ICEX_USR
  34601. +#define AuBrAttr_ICEX_USR 0
  34602. +#undef AuBrAttr_ICEX_OTH
  34603. +#define AuBrAttr_ICEX_OTH 0
  34604. +#endif
  34605. +#endif
  34606. +
  34607. +/* the longest combination */
  34608. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  34609. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  34610. + "+" AUFS_BRATTR_COO_REG \
  34611. + "+" AUFS_BRATTR_FHSM \
  34612. + "+" AUFS_BRATTR_UNPIN \
  34613. + "+" AUFS_BRATTR_ICEX_SEC \
  34614. + "+" AUFS_BRATTR_ICEX_SYS \
  34615. + "+" AUFS_BRATTR_ICEX_USR \
  34616. + "+" AUFS_BRATTR_ICEX_OTH \
  34617. + "+" AUFS_BRWATTR_NLWH)
  34618. +
  34619. +typedef struct {
  34620. + char a[AuBrPermStrSz];
  34621. +} au_br_perm_str_t;
  34622. +
  34623. +static inline int au_br_writable(int brperm)
  34624. +{
  34625. + return brperm & AuBrPerm_RW;
  34626. +}
  34627. +
  34628. +static inline int au_br_whable(int brperm)
  34629. +{
  34630. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  34631. +}
  34632. +
  34633. +static inline int au_br_wh_linkable(int brperm)
  34634. +{
  34635. + return !(brperm & AuBrWAttr_NoLinkWH);
  34636. +}
  34637. +
  34638. +static inline int au_br_cmoo(int brperm)
  34639. +{
  34640. + return brperm & AuBrAttr_CMOO_Mask;
  34641. +}
  34642. +
  34643. +static inline int au_br_fhsm(int brperm)
  34644. +{
  34645. + return brperm & AuBrAttr_FHSM;
  34646. +}
  34647. +
  34648. +/* ---------------------------------------------------------------------- */
  34649. +
  34650. +/* ioctl */
  34651. +enum {
  34652. + /* readdir in userspace */
  34653. + AuCtl_RDU,
  34654. + AuCtl_RDU_INO,
  34655. +
  34656. + AuCtl_WBR_FD, /* pathconf wrapper */
  34657. + AuCtl_IBUSY, /* busy inode */
  34658. + AuCtl_MVDOWN, /* move-down */
  34659. + AuCtl_BR, /* info about branches */
  34660. + AuCtl_FHSM_FD /* connection for fhsm */
  34661. +};
  34662. +
  34663. +/* borrowed from linux/include/linux/kernel.h */
  34664. +#ifndef ALIGN
  34665. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  34666. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  34667. +#endif
  34668. +
  34669. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  34670. +#ifndef __aligned
  34671. +#define __aligned(x) __attribute__((aligned(x)))
  34672. +#endif
  34673. +
  34674. +#ifdef __KERNEL__
  34675. +#ifndef __packed
  34676. +#define __packed __attribute__((packed))
  34677. +#endif
  34678. +#endif
  34679. +
  34680. +struct au_rdu_cookie {
  34681. + uint64_t h_pos;
  34682. + int16_t bindex;
  34683. + uint8_t flags;
  34684. + uint8_t pad;
  34685. + uint32_t generation;
  34686. +} __aligned(8);
  34687. +
  34688. +struct au_rdu_ent {
  34689. + uint64_t ino;
  34690. + int16_t bindex;
  34691. + uint8_t type;
  34692. + uint8_t nlen;
  34693. + uint8_t wh;
  34694. + char name[0];
  34695. +} __aligned(8);
  34696. +
  34697. +static inline int au_rdu_len(int nlen)
  34698. +{
  34699. + /* include the terminating NULL */
  34700. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  34701. + sizeof(uint64_t));
  34702. +}
  34703. +
  34704. +union au_rdu_ent_ul {
  34705. + struct au_rdu_ent __user *e;
  34706. + uint64_t ul;
  34707. +};
  34708. +
  34709. +enum {
  34710. + AufsCtlRduV_SZ,
  34711. + AufsCtlRduV_End
  34712. +};
  34713. +
  34714. +struct aufs_rdu {
  34715. + /* input */
  34716. + union {
  34717. + uint64_t sz; /* AuCtl_RDU */
  34718. + uint64_t nent; /* AuCtl_RDU_INO */
  34719. + };
  34720. + union au_rdu_ent_ul ent;
  34721. + uint16_t verify[AufsCtlRduV_End];
  34722. +
  34723. + /* input/output */
  34724. + uint32_t blk;
  34725. +
  34726. + /* output */
  34727. + union au_rdu_ent_ul tail;
  34728. + /* number of entries which were added in a single call */
  34729. + uint64_t rent;
  34730. + uint8_t full;
  34731. + uint8_t shwh;
  34732. +
  34733. + struct au_rdu_cookie cookie;
  34734. +} __aligned(8);
  34735. +
  34736. +/* ---------------------------------------------------------------------- */
  34737. +
  34738. +struct aufs_wbr_fd {
  34739. + uint32_t oflags;
  34740. + int16_t brid;
  34741. +} __aligned(8);
  34742. +
  34743. +/* ---------------------------------------------------------------------- */
  34744. +
  34745. +struct aufs_ibusy {
  34746. + uint64_t ino, h_ino;
  34747. + int16_t bindex;
  34748. +} __aligned(8);
  34749. +
  34750. +/* ---------------------------------------------------------------------- */
  34751. +
  34752. +/* error code for move-down */
  34753. +/* the actual message strings are implemented in aufs-util.git */
  34754. +enum {
  34755. + EAU_MVDOWN_OPAQUE = 1,
  34756. + EAU_MVDOWN_WHITEOUT,
  34757. + EAU_MVDOWN_UPPER,
  34758. + EAU_MVDOWN_BOTTOM,
  34759. + EAU_MVDOWN_NOUPPER,
  34760. + EAU_MVDOWN_NOLOWERBR,
  34761. + EAU_Last
  34762. +};
  34763. +
  34764. +/* flags for move-down */
  34765. +#define AUFS_MVDOWN_DMSG 1
  34766. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  34767. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  34768. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  34769. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  34770. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  34771. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  34772. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  34773. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  34774. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  34775. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  34776. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  34777. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  34778. +
  34779. +/* index for move-down */
  34780. +enum {
  34781. + AUFS_MVDOWN_UPPER,
  34782. + AUFS_MVDOWN_LOWER,
  34783. + AUFS_MVDOWN_NARRAY
  34784. +};
  34785. +
  34786. +/*
  34787. + * additional info of move-down
  34788. + * number of free blocks and inodes.
  34789. + * subset of struct kstatfs, but smaller and always 64bit.
  34790. + */
  34791. +struct aufs_stfs {
  34792. + uint64_t f_blocks;
  34793. + uint64_t f_bavail;
  34794. + uint64_t f_files;
  34795. + uint64_t f_ffree;
  34796. +};
  34797. +
  34798. +struct aufs_stbr {
  34799. + int16_t brid; /* optional input */
  34800. + int16_t bindex; /* output */
  34801. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  34802. +} __aligned(8);
  34803. +
  34804. +struct aufs_mvdown {
  34805. + uint32_t flags; /* input/output */
  34806. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  34807. + int8_t au_errno; /* output */
  34808. +} __aligned(8);
  34809. +
  34810. +/* ---------------------------------------------------------------------- */
  34811. +
  34812. +union aufs_brinfo {
  34813. + /* PATH_MAX may differ between kernel-space and user-space */
  34814. + char _spacer[4096];
  34815. + struct {
  34816. + int16_t id;
  34817. + int perm;
  34818. + char path[0];
  34819. + };
  34820. +} __aligned(8);
  34821. +
  34822. +/* ---------------------------------------------------------------------- */
  34823. +
  34824. +#define AuCtlType 'A'
  34825. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  34826. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  34827. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  34828. + struct aufs_wbr_fd)
  34829. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  34830. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  34831. + struct aufs_mvdown)
  34832. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  34833. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  34834. +
  34835. +#endif /* __AUFS_TYPE_H__ */