aufs4.patch 847 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819
  1. diff -Nur linux-4.0.4.orig/Documentation/ABI/testing/debugfs-aufs linux-4.0.4/Documentation/ABI/testing/debugfs-aufs
  2. --- linux-4.0.4.orig/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-4.0.4/Documentation/ABI/testing/debugfs-aufs 2015-05-30 22:11:30.000000000 +0200
  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 -Nur linux-4.0.4.orig/Documentation/ABI/testing/sysfs-aufs linux-4.0.4/Documentation/ABI/testing/sysfs-aufs
  56. --- linux-4.0.4.orig/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
  57. +++ linux-4.0.4/Documentation/ABI/testing/sysfs-aufs 2015-05-30 22:11:29.000000000 +0200
  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 -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/01intro.txt linux-4.0.4/Documentation/filesystems/aufs/design/01intro.txt
  91. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
  92. +++ linux-4.0.4/Documentation/filesystems/aufs/design/01intro.txt 2015-05-30 22:11:30.000000000 +0200
  93. @@ -0,0 +1,157 @@
  94. +
  95. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  96. +
  97. +Introduction
  98. +----------------------------------------
  99. +
  100. +aufs [ei ju: ef es] | [a u f s]
  101. +1. abbrev. for "advanced multi-layered unification filesystem".
  102. +2. abbrev. for "another unionfs".
  103. +3. abbrev. for "auf das" in German which means "on the" in English.
  104. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  105. + But "Filesystem aufs Filesystem" is hard to understand.
  106. +
  107. +AUFS is a filesystem with features:
  108. +- multi layered stackable unification filesystem, the member directory
  109. + is called as a branch.
  110. +- branch permission and attribute, 'readonly', 'real-readonly',
  111. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  112. + combination.
  113. +- internal "file copy-on-write".
  114. +- logical deletion, whiteout.
  115. +- dynamic branch manipulation, adding, deleting and changing permission.
  116. +- allow bypassing aufs, user's direct branch access.
  117. +- external inode number translation table and bitmap which maintains the
  118. + persistent aufs inode number.
  119. +- seekable directory, including NFS readdir.
  120. +- file mapping, mmap and sharing pages.
  121. +- pseudo-link, hardlink over branches.
  122. +- loopback mounted filesystem as a branch.
  123. +- several policies to select one among multiple writable branches.
  124. +- revert a single systemcall when an error occurs in aufs.
  125. +- and more...
  126. +
  127. +
  128. +Multi Layered Stackable Unification Filesystem
  129. +----------------------------------------------------------------------
  130. +Most people already knows what it is.
  131. +It is a filesystem which unifies several directories and provides a
  132. +merged single directory. When users access a file, the access will be
  133. +passed/re-directed/converted (sorry, I am not sure which English word is
  134. +correct) to the real file on the member filesystem. The member
  135. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  136. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  137. +readonly branch is handled by creating 'whiteout' on the upper writable
  138. +branch.
  139. +
  140. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  141. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  142. +different approaches to implement the merged-view.
  143. +The former tries putting it into VFS, and the latter implements as a
  144. +separate filesystem.
  145. +(If I misunderstand about these implementations, please let me know and
  146. +I shall correct it. Because it is a long time ago when I read their
  147. +source files last time).
  148. +
  149. +UnionMount's approach will be able to small, but may be hard to share
  150. +branches between several UnionMount since the whiteout in it is
  151. +implemented in the inode on branch filesystem and always
  152. +shared. According to Bharata's post, readdir does not seems to be
  153. +finished yet.
  154. +There are several missing features known in this implementations such as
  155. +- for users, the inode number may change silently. eg. copy-up.
  156. +- link(2) may break by copy-up.
  157. +- read(2) may get an obsoleted filedata (fstat(2) too).
  158. +- fcntl(F_SETLK) may be broken by copy-up.
  159. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  160. + open(O_RDWR).
  161. +
  162. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  163. +merged into mainline. This is another implementation of UnionMount as a
  164. +separated filesystem. All the limitations and known problems which
  165. +UnionMount are equally inherited to "overlay" filesystem.
  166. +
  167. +Unionfs has a longer history. When I started implementing a stackable
  168. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  169. +inode, dentry and file objects and they have an array pointing lower
  170. +same kind objects. After contributing many patches for Unionfs, I
  171. +re-started my project AUFS (Jun 2006).
  172. +
  173. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  174. +implemented my own ideas, approaches and enhancements and it became
  175. +totally different one.
  176. +
  177. +Comparing DM snapshot and fs based implementation
  178. +- the number of bytes to be copied between devices is much smaller.
  179. +- the type of filesystem must be one and only.
  180. +- the fs must be writable, no readonly fs, even for the lower original
  181. + device. so the compression fs will not be usable. but if we use
  182. + loopback mount, we may address this issue.
  183. + for instance,
  184. + mount /cdrom/squashfs.img /sq
  185. + losetup /sq/ext2.img
  186. + losetup /somewhere/cow
  187. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  188. +- it will be difficult (or needs more operations) to extract the
  189. + difference between the original device and COW.
  190. +- DM snapshot-merge may help a lot when users try merging. in the
  191. + fs-layer union, users will use rsync(1).
  192. +
  193. +You may want to read my old paper "Filesystems in LiveCD"
  194. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  195. +
  196. +
  197. +Several characters/aspects/persona of aufs
  198. +----------------------------------------------------------------------
  199. +
  200. +Aufs has several characters, aspects or persona.
  201. +1. a filesystem, callee of VFS helper
  202. +2. sub-VFS, caller of VFS helper for branches
  203. +3. a virtual filesystem which maintains persistent inode number
  204. +4. reader/writer of files on branches such like an application
  205. +
  206. +1. Callee of VFS Helper
  207. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  208. +unlink(2) from an application reaches sys_unlink() kernel function and
  209. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  210. +calls filesystem specific unlink operation. Actually aufs implements the
  211. +unlink operation but it behaves like a redirector.
  212. +
  213. +2. Caller of VFS Helper for Branches
  214. +aufs_unlink() passes the unlink request to the branch filesystem as if
  215. +it were called from VFS. So the called unlink operation of the branch
  216. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  217. +every necessary pre/post operation for the branch filesystem.
  218. +- acquire the lock for the parent dir on a branch
  219. +- lookup in a branch
  220. +- revalidate dentry on a branch
  221. +- mnt_want_write() for a branch
  222. +- vfs_unlink() for a branch
  223. +- mnt_drop_write() for a branch
  224. +- release the lock on a branch
  225. +
  226. +3. Persistent Inode Number
  227. +One of the most important issue for a filesystem is to maintain inode
  228. +numbers. This is particularly important to support exporting a
  229. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  230. +backend block device for its own. But some storage is necessary to
  231. +keep and maintain the inode numbers. It may be a large space and may not
  232. +suit to keep in memory. Aufs rents some space from its first writable
  233. +branch filesystem (by default) and creates file(s) on it. These files
  234. +are created by aufs internally and removed soon (currently) keeping
  235. +opened.
  236. +Note: Because these files are removed, they are totally gone after
  237. + unmounting aufs. It means the inode numbers are not persistent
  238. + across unmount or reboot. I have a plan to make them really
  239. + persistent which will be important for aufs on NFS server.
  240. +
  241. +4. Read/Write Files Internally (copy-on-write)
  242. +Because a branch can be readonly, when you write a file on it, aufs will
  243. +"copy-up" it to the upper writable branch internally. And then write the
  244. +originally requested thing to the file. Generally kernel doesn't
  245. +open/read/write file actively. In aufs, even a single write may cause a
  246. +internal "file copy". This behaviour is very similar to cp(1) command.
  247. +
  248. +Some people may think it is better to pass such work to user space
  249. +helper, instead of doing in kernel space. Actually I am still thinking
  250. +about it. But currently I have implemented it in kernel space.
  251. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/02struct.txt linux-4.0.4/Documentation/filesystems/aufs/design/02struct.txt
  252. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
  253. +++ linux-4.0.4/Documentation/filesystems/aufs/design/02struct.txt 2015-05-30 22:11:30.000000000 +0200
  254. @@ -0,0 +1,245 @@
  255. +
  256. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  257. +
  258. +Basic Aufs Internal Structure
  259. +
  260. +Superblock/Inode/Dentry/File Objects
  261. +----------------------------------------------------------------------
  262. +As like an ordinary filesystem, aufs has its own
  263. +superblock/inode/dentry/file objects. All these objects have a
  264. +dynamically allocated array and store the same kind of pointers to the
  265. +lower filesystem, branch.
  266. +For example, when you build a union with one readwrite branch and one
  267. +readonly, mounted /au, /rw and /ro respectively.
  268. +- /au = /rw + /ro
  269. +- /ro/fileA exists but /rw/fileA
  270. +
  271. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  272. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  273. +- [0] = NULL (because /rw/fileA doesn't exist)
  274. +- [1] = /ro/fileA
  275. +
  276. +This style of an array is essentially same to the aufs
  277. +superblock/inode/dentry/file objects.
  278. +
  279. +Because aufs supports manipulating branches, ie. add/delete/change
  280. +branches dynamically, these objects has its own generation. When
  281. +branches are changed, the generation in aufs superblock is
  282. +incremented. And a generation in other object are compared when it is
  283. +accessed. When a generation in other objects are obsoleted, aufs
  284. +refreshes the internal array.
  285. +
  286. +
  287. +Superblock
  288. +----------------------------------------------------------------------
  289. +Additionally aufs superblock has some data for policies to select one
  290. +among multiple writable branches, XIB files, pseudo-links and kobject.
  291. +See below in detail.
  292. +About the policies which supports copy-down a directory, see
  293. +wbr_policy.txt too.
  294. +
  295. +
  296. +Branch and XINO(External Inode Number Translation Table)
  297. +----------------------------------------------------------------------
  298. +Every branch has its own xino (external inode number translation table)
  299. +file. The xino file is created and unlinked by aufs internally. When two
  300. +members of a union exist on the same filesystem, they share the single
  301. +xino file.
  302. +The struct of a xino file is simple, just a sequence of aufs inode
  303. +numbers which is indexed by the lower inode number.
  304. +In the above sample, assume the inode number of /ro/fileA is i111 and
  305. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  306. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  307. +
  308. +When the inode numbers are not contiguous, the xino file will be sparse
  309. +which has a hole in it and doesn't consume as much disk space as it
  310. +might appear. If your branch filesystem consumes disk space for such
  311. +holes, then you should specify 'xino=' option at mounting aufs.
  312. +
  313. +Aufs has a mount option to free the disk blocks for such holes in XINO
  314. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  315. +meet a problem of disk shortage due to XINO files, then you should try
  316. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  317. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  318. +the holes in XINO files.
  319. +
  320. +Also a writable branch has three kinds of "whiteout bases". All these
  321. +are existed when the branch is joined to aufs, and their names are
  322. +whiteout-ed doubly, so that users will never see their names in aufs
  323. +hierarchy.
  324. +1. a regular file which will be hardlinked to all whiteouts.
  325. +2. a directory to store a pseudo-link.
  326. +3. a directory to store an "orphan"-ed file temporary.
  327. +
  328. +1. Whiteout Base
  329. + When you remove a file on a readonly branch, aufs handles it as a
  330. + logical deletion and creates a whiteout on the upper writable branch
  331. + as a hardlink of this file in order not to consume inode on the
  332. + writable branch.
  333. +2. Pseudo-link Dir
  334. + See below, Pseudo-link.
  335. +3. Step-Parent Dir
  336. + When "fileC" exists on the lower readonly branch only and it is
  337. + opened and removed with its parent dir, and then user writes
  338. + something into it, then aufs copies-up fileC to this
  339. + directory. Because there is no other dir to store fileC. After
  340. + creating a file under this dir, the file is unlinked.
  341. +
  342. +Because aufs supports manipulating branches, ie. add/delete/change
  343. +dynamically, a branch has its own id. When the branch order changes,
  344. +aufs finds the new index by searching the branch id.
  345. +
  346. +
  347. +Pseudo-link
  348. +----------------------------------------------------------------------
  349. +Assume "fileA" exists on the lower readonly branch only and it is
  350. +hardlinked to "fileB" on the branch. When you write something to fileA,
  351. +aufs copies-up it to the upper writable branch. Additionally aufs
  352. +creates a hardlink under the Pseudo-link Directory of the writable
  353. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  354. +simple list. If fileB is read after unlinking fileA, aufs returns
  355. +filedata from the pseudo-link instead of the lower readonly
  356. +branch. Because the pseudo-link is based upon the inode, to keep the
  357. +inode number by xino (see above) is essentially necessary.
  358. +
  359. +All the hardlinks under the Pseudo-link Directory of the writable branch
  360. +should be restored in a proper location later. Aufs provides a utility
  361. +to do this. The userspace helpers executed at remounting and unmounting
  362. +aufs by default.
  363. +During this utility is running, it puts aufs into the pseudo-link
  364. +maintenance mode. In this mode, only the process which began the
  365. +maintenance mode (and its child processes) is allowed to operate in
  366. +aufs. Some other processes which are not related to the pseudo-link will
  367. +be allowed to run too, but the rest have to return an error or wait
  368. +until the maintenance mode ends. If a process already acquires an inode
  369. +mutex (in VFS), it has to return an error.
  370. +
  371. +
  372. +XIB(external inode number bitmap)
  373. +----------------------------------------------------------------------
  374. +Addition to the xino file per a branch, aufs has an external inode number
  375. +bitmap in a superblock object. It is also an internal file such like a
  376. +xino file.
  377. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  378. +not.
  379. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  380. +
  381. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  382. +reduce the number of consumed disk blocks for these files.
  383. +
  384. +
  385. +Virtual or Vertical Dir, and Readdir in Userspace
  386. +----------------------------------------------------------------------
  387. +In order to support multiple layers (branches), aufs readdir operation
  388. +constructs a virtual dir block on memory. For readdir, aufs calls
  389. +vfs_readdir() internally for each dir on branches, merges their entries
  390. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  391. +object. So the file object has its entry list until it is closed. The
  392. +entry list will be updated when the file position is zero and becomes
  393. +obsoleted. This decision is made in aufs automatically.
  394. +
  395. +The dynamically allocated memory block for the name of entries has a
  396. +unit of 512 bytes (by default) and stores the names contiguously (no
  397. +padding). Another block for each entry is handled by kmem_cache too.
  398. +During building dir blocks, aufs creates hash list and judging whether
  399. +the entry is whiteouted by its upper branch or already listed.
  400. +The merged result is cached in the corresponding inode object and
  401. +maintained by a customizable life-time option.
  402. +
  403. +Some people may call it can be a security hole or invite DoS attack
  404. +since the opened and once readdir-ed dir (file object) holds its entry
  405. +list and becomes a pressure for system memory. But I'd say it is similar
  406. +to files under /proc or /sys. The virtual files in them also holds a
  407. +memory page (generally) while they are opened. When an idea to reduce
  408. +memory for them is introduced, it will be applied to aufs too.
  409. +For those who really hate this situation, I've developed readdir(3)
  410. +library which operates this merging in userspace. You just need to set
  411. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  412. +kernel space for readdir(3).
  413. +
  414. +
  415. +Workqueue
  416. +----------------------------------------------------------------------
  417. +Aufs sometimes requires privilege access to a branch. For instance,
  418. +in copy-up/down operation. When a user process is going to make changes
  419. +to a file which exists in the lower readonly branch only, and the mode
  420. +of one of ancestor directories may not be writable by a user
  421. +process. Here aufs copy-up the file with its ancestors and they may
  422. +require privilege to set its owner/group/mode/etc.
  423. +This is a typical case of a application character of aufs (see
  424. +Introduction).
  425. +
  426. +Aufs uses workqueue synchronously for this case. It creates its own
  427. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  428. +passes the request to call mkdir or write (for example), and wait for
  429. +its completion. This approach solves a problem of a signal handler
  430. +simply.
  431. +If aufs didn't adopt the workqueue and changed the privilege of the
  432. +process, then the process may receive the unexpected SIGXFSZ or other
  433. +signals.
  434. +
  435. +Also aufs uses the system global workqueue ("events" kernel thread) too
  436. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  437. +whiteout base and etc. This is unrelated to a privilege.
  438. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  439. +superblock at the beginning, at the same time waits for the completion
  440. +of all queued asynchronous tasks.
  441. +
  442. +
  443. +Whiteout
  444. +----------------------------------------------------------------------
  445. +The whiteout in aufs is very similar to Unionfs's. That is represented
  446. +by its filename. UnionMount takes an approach of a file mode, but I am
  447. +afraid several utilities (find(1) or something) will have to support it.
  448. +
  449. +Basically the whiteout represents "logical deletion" which stops aufs to
  450. +lookup further, but also it represents "dir is opaque" which also stop
  451. +further lookup.
  452. +
  453. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  454. +In order to make several functions in a single systemcall to be
  455. +revertible, aufs adopts an approach to rename a directory to a temporary
  456. +unique whiteouted name.
  457. +For example, in rename(2) dir where the target dir already existed, aufs
  458. +renames the target dir to a temporary unique whiteouted name before the
  459. +actual rename on a branch, and then handles other actions (make it opaque,
  460. +update the attributes, etc). If an error happens in these actions, aufs
  461. +simply renames the whiteouted name back and returns an error. If all are
  462. +succeeded, aufs registers a function to remove the whiteouted unique
  463. +temporary name completely and asynchronously to the system global
  464. +workqueue.
  465. +
  466. +
  467. +Copy-up
  468. +----------------------------------------------------------------------
  469. +It is a well-known feature or concept.
  470. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  471. +internally and makes change to the new file on the upper writable branch.
  472. +When the trigger systemcall does not update the timestamps of the parent
  473. +dir, aufs reverts it after copy-up.
  474. +
  475. +
  476. +Move-down (aufs3.9 and later)
  477. +----------------------------------------------------------------------
  478. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  479. +the lower readonly branch to the upper writable branch when a user
  480. +changes something about the file.
  481. +"Move-down" is an opposite action of copy-up. Basically this action is
  482. +ran manually instead of automatically and internally.
  483. +For desgin and implementation, aufs has to consider these issues.
  484. +- whiteout for the file may exist on the lower branch.
  485. +- ancestor directories may not exist on the lower branch.
  486. +- diropq for the ancestor directories may exist on the upper branch.
  487. +- free space on the lower branch will reduce.
  488. +- another access to the file may happen during moving-down, including
  489. + UDBA (see "Revalidate Dentry and UDBA").
  490. +- the file should not be hard-linked nor pseudo-linked. they should be
  491. + handled by auplink utility later.
  492. +
  493. +Sometimes users want to move-down a file from the upper writable branch
  494. +to the lower readonly or writable branch. For instance,
  495. +- the free space of the upper writable branch is going to run out.
  496. +- create a new intermediate branch between the upper and lower branch.
  497. +- etc.
  498. +
  499. +For this purpose, use "aumvdown" command in aufs-util.git.
  500. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/03atomic_open.txt linux-4.0.4/Documentation/filesystems/aufs/design/03atomic_open.txt
  501. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
  502. +++ linux-4.0.4/Documentation/filesystems/aufs/design/03atomic_open.txt 2015-05-30 22:11:31.000000000 +0200
  503. @@ -0,0 +1,72 @@
  504. +
  505. +# Copyright (C) 2015 Junjiro R. Okajima
  506. +
  507. +Support for a branch who has its ->atomic_open()
  508. +----------------------------------------------------------------------
  509. +The filesystems who implement its ->atomic_open() are not majority. For
  510. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  511. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  512. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  513. +sure whether all filesystems who have ->atomic_open() behave like this,
  514. +but NFSv4 surely returns the error.
  515. +
  516. +In order to support ->atomic_open() for aufs, there are a few
  517. +approaches.
  518. +
  519. +A. Introduce aufs_atomic_open()
  520. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  521. + branch fs.
  522. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  523. + an aufs user Pip Cet's approach
  524. + - calls aufs_create(), VFS finish_open() and notify_change().
  525. + - pass fake-mode to finish_open(), and then correct the mode by
  526. + notify_change().
  527. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  528. + - no aufs_atomic_open().
  529. + - aufs_lookup() registers the TID to an aufs internal object.
  530. + - aufs_create() does nothing when the matching TID is registered, but
  531. + registers the mode.
  532. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  533. + TID is registered.
  534. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  535. + credential
  536. + - no aufs_atomic_open().
  537. + - aufs_create() registers the TID to an internal object. this info
  538. + represents "this process created this file just now."
  539. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  540. + registered TID and re-try open() with superuser's credential.
  541. +
  542. +Pros and cons for each approach.
  543. +
  544. +A.
  545. + - straightforward but highly depends upon VFS internal.
  546. + - the atomic behavaiour is kept.
  547. + - some of parameters such as nameidata are hard to reproduce for
  548. + branch fs.
  549. + - large overhead.
  550. +B.
  551. + - easy to implement.
  552. + - the atomic behavaiour is lost.
  553. +C.
  554. + - the atomic behavaiour is kept.
  555. + - dirty and tricky.
  556. + - VFS checks whether the file is created correctly after calling
  557. + ->create(), which means this approach doesn't work.
  558. +D.
  559. + - easy to implement.
  560. + - the atomic behavaiour is lost.
  561. + - to open a file with superuser's credential and give it to a user
  562. + process is a bad idea, since the file object keeps the credential
  563. + in it. It may affect LSM or something. This approach doesn't work
  564. + either.
  565. +
  566. +The approach A is ideal, but it hard to implement. So here is a
  567. +variation of A, which is to be implemented.
  568. +
  569. +A-1. Introduce aufs_atomic_open()
  570. + - calls branch fs ->atomic_open() if exists. otherwise calls
  571. + vfs_create() and finish_open().
  572. + - the demerit is that the several checks after branch fs
  573. + ->atomic_open() are lost. in the ordinary case, the checks are
  574. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  575. + be implemented in aufs, but not all I am afraid.
  576. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/03lookup.txt linux-4.0.4/Documentation/filesystems/aufs/design/03lookup.txt
  577. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
  578. +++ linux-4.0.4/Documentation/filesystems/aufs/design/03lookup.txt 2015-05-30 22:11:30.000000000 +0200
  579. @@ -0,0 +1,100 @@
  580. +
  581. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  582. +
  583. +Lookup in a Branch
  584. +----------------------------------------------------------------------
  585. +Since aufs has a character of sub-VFS (see Introduction), it operates
  586. +lookup for branches as VFS does. It may be a heavy work. But almost all
  587. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  588. +directly connected to its parent. Digging down the directory hierarchy
  589. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  590. +aufs calls it.
  591. +
  592. +When a branch is a remote filesystem, aufs basically relies upon its
  593. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  594. +them.
  595. +For d_revalidate, aufs implements three levels of revalidate tests. See
  596. +"Revalidate Dentry and UDBA" in detail.
  597. +
  598. +
  599. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  600. +----------------------------------------------------------------------
  601. +Let's try case study.
  602. +- aufs has two branches, upper readwrite and lower readonly.
  603. + /au = /rw + /ro
  604. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  605. +- user invoked "chmod a+rx /au/dirA"
  606. +- the internal copy-up is activated and "/rw/dirA" is created and its
  607. + permission bits are set to world readable.
  608. +- then "/au/dirA" becomes world readable?
  609. +
  610. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  611. +or it may be a natively readonly filesystem. If aufs respects the lower
  612. +branch, it should not respond readdir request from other users. But user
  613. +allowed it by chmod. Should really aufs rejects showing the entries
  614. +under /ro/dirA?
  615. +
  616. +To be honest, I don't have a good solution for this case. So aufs
  617. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  618. +users.
  619. +When dirperm1 is specified, aufs checks only the highest one for the
  620. +directory permission, and shows the entries. Otherwise, as usual, checks
  621. +every dir existing on all branches and rejects the request.
  622. +
  623. +As a side effect, dirperm1 option improves the performance of aufs
  624. +because the number of permission check is reduced when the number of
  625. +branch is many.
  626. +
  627. +
  628. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  629. +----------------------------------------------------------------------
  630. +Generally VFS helpers re-validate a dentry as a part of lookup.
  631. +0. digging down the directory hierarchy.
  632. +1. lock the parent dir by its i_mutex.
  633. +2. lookup the final (child) entry.
  634. +3. revalidate it.
  635. +4. call the actual operation (create, unlink, etc.)
  636. +5. unlock the parent dir
  637. +
  638. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  639. +called. Actually aufs implements it and checks the dentry on a branch is
  640. +still valid.
  641. +But it is not enough. Because aufs has to release the lock for the
  642. +parent dir on a branch at the end of ->lookup() (step 2) and
  643. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  644. +held by VFS.
  645. +If the file on a branch is changed directly, eg. bypassing aufs, after
  646. +aufs released the lock, then the subsequent operation may cause
  647. +something unpleasant result.
  648. +
  649. +This situation is a result of VFS architecture, ->lookup() and
  650. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  651. +design from VFS's point of view. It is just not suitable for sub-VFS
  652. +character in aufs.
  653. +
  654. +Aufs supports such case by three level of revalidation which is
  655. +selectable by user.
  656. +1. Simple Revalidate
  657. + Addition to the native flow in VFS's, confirm the child-parent
  658. + relationship on the branch just after locking the parent dir on the
  659. + branch in the "actual operation" (step 4). When this validation
  660. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  661. + checks the validation of the dentry on branches.
  662. +2. Monitor Changes Internally by Inotify/Fsnotify
  663. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  664. + the dentry on the branch, and returns EBUSY if it finds different
  665. + dentry.
  666. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  667. + during it is in cache. When the event is notified, aufs registers a
  668. + function to kernel 'events' thread by schedule_work(). And the
  669. + function sets some special status to the cached aufs dentry and inode
  670. + private data. If they are not cached, then aufs has nothing to
  671. + do. When the same file is accessed through aufs (step 0-3) later,
  672. + aufs will detect the status and refresh all necessary data.
  673. + In this mode, aufs has to ignore the event which is fired by aufs
  674. + itself.
  675. +3. No Extra Validation
  676. + This is the simplest test and doesn't add any additional revalidation
  677. + test, and skip the revalidation in step 4. It is useful and improves
  678. + aufs performance when system surely hide the aufs branches from user,
  679. + by over-mounting something (or another method).
  680. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/04branch.txt linux-4.0.4/Documentation/filesystems/aufs/design/04branch.txt
  681. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
  682. +++ linux-4.0.4/Documentation/filesystems/aufs/design/04branch.txt 2015-05-30 22:11:30.000000000 +0200
  683. @@ -0,0 +1,61 @@
  684. +
  685. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  686. +
  687. +Branch Manipulation
  688. +
  689. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  690. +and changing its permission/attribute, there are a lot of works to do.
  691. +
  692. +
  693. +Add a Branch
  694. +----------------------------------------------------------------------
  695. +o Confirm the adding dir exists outside of aufs, including loopback
  696. + mount, and its various attributes.
  697. +o Initialize the xino file and whiteout bases if necessary.
  698. + See struct.txt.
  699. +
  700. +o Check the owner/group/mode of the directory
  701. + When the owner/group/mode of the adding directory differs from the
  702. + existing branch, aufs issues a warning because it may impose a
  703. + security risk.
  704. + For example, when a upper writable branch has a world writable empty
  705. + top directory, a malicious user can create any files on the writable
  706. + branch directly, like copy-up and modify manually. If something like
  707. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  708. + writable branch, and the writable branch is world-writable, then a
  709. + malicious guy may create /etc/passwd on the writable branch directly
  710. + and the infected file will be valid in aufs.
  711. + I am afraid it can be a security issue, but aufs can do nothing except
  712. + producing a warning.
  713. +
  714. +
  715. +Delete a Branch
  716. +----------------------------------------------------------------------
  717. +o Confirm the deleting branch is not busy
  718. + To be general, there is one merit to adopt "remount" interface to
  719. + manipulate branches. It is to discard caches. At deleting a branch,
  720. + aufs checks the still cached (and connected) dentries and inodes. If
  721. + there are any, then they are all in-use. An inode without its
  722. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  723. +
  724. + For the cached one, aufs checks whether the same named entry exists on
  725. + other branches.
  726. + If the cached one is a directory, because aufs provides a merged view
  727. + to users, as long as one dir is left on any branch aufs can show the
  728. + dir to users. In this case, the branch can be removed from aufs.
  729. + Otherwise aufs rejects deleting the branch.
  730. +
  731. + If any file on the deleting branch is opened by aufs, then aufs
  732. + rejects deleting.
  733. +
  734. +
  735. +Modify the Permission of a Branch
  736. +----------------------------------------------------------------------
  737. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  738. + See struct.txt.
  739. +
  740. +o rw --> ro: Confirm the modifying branch is not busy
  741. + Aufs rejects the request if any of these conditions are true.
  742. + - a file on the branch is mmap-ed.
  743. + - a regular file on the branch is opened for write and there is no
  744. + same named entry on the upper branch.
  745. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/05wbr_policy.txt linux-4.0.4/Documentation/filesystems/aufs/design/05wbr_policy.txt
  746. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
  747. +++ linux-4.0.4/Documentation/filesystems/aufs/design/05wbr_policy.txt 2015-05-30 22:11:30.000000000 +0200
  748. @@ -0,0 +1,51 @@
  749. +
  750. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  751. +
  752. +Policies to Select One among Multiple Writable Branches
  753. +----------------------------------------------------------------------
  754. +When the number of writable branch is more than one, aufs has to decide
  755. +the target branch for file creation or copy-up. By default, the highest
  756. +writable branch which has the parent (or ancestor) dir of the target
  757. +file is chosen (top-down-parent policy).
  758. +By user's request, aufs implements some other policies to select the
  759. +writable branch, for file creation several policies, round-robin,
  760. +most-free-space, and other policies. For copy-up, top-down-parent,
  761. +bottom-up-parent, bottom-up and others.
  762. +
  763. +As expected, the round-robin policy selects the branch in circular. When
  764. +you have two writable branches and creates 10 new files, 5 files will be
  765. +created for each branch. mkdir(2) systemcall is an exception. When you
  766. +create 10 new directories, all will be created on the same branch.
  767. +And the most-free-space policy selects the one which has most free
  768. +space among the writable branches. The amount of free space will be
  769. +checked by aufs internally, and users can specify its time interval.
  770. +
  771. +The policies for copy-up is more simple,
  772. +top-down-parent is equivalent to the same named on in create policy,
  773. +bottom-up-parent selects the writable branch where the parent dir
  774. +exists and the nearest upper one from the copyup-source,
  775. +bottom-up selects the nearest upper writable branch from the
  776. +copyup-source, regardless the existence of the parent dir.
  777. +
  778. +There are some rules or exceptions to apply these policies.
  779. +- If there is a readonly branch above the policy-selected branch and
  780. + the parent dir is marked as opaque (a variation of whiteout), or the
  781. + target (creating) file is whiteout-ed on the upper readonly branch,
  782. + then the result of the policy is ignored and the target file will be
  783. + created on the nearest upper writable branch than the readonly branch.
  784. +- If there is a writable branch above the policy-selected branch and
  785. + the parent dir is marked as opaque or the target file is whiteouted
  786. + on the branch, then the result of the policy is ignored and the target
  787. + file will be created on the highest one among the upper writable
  788. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  789. + it as usual.
  790. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  791. + They try selecting the branch where the source exists as possible
  792. + since copyup a large file will take long time. If it can't be,
  793. + ie. the branch where the source exists is readonly, then they will
  794. + follow the copyup policy.
  795. +- There is an exception for rename(2) when the target exists.
  796. + If the rename target exists, aufs compares the index of the branches
  797. + where the source and the target exists and selects the higher
  798. + one. If the selected branch is readonly, then aufs follows the
  799. + copyup policy.
  800. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/06fhsm.txt linux-4.0.4/Documentation/filesystems/aufs/design/06fhsm.txt
  801. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
  802. +++ linux-4.0.4/Documentation/filesystems/aufs/design/06fhsm.txt 2015-05-30 22:11:30.000000000 +0200
  803. @@ -0,0 +1,105 @@
  804. +
  805. +# Copyright (C) 2011-2015 Junjiro R. Okajima
  806. +
  807. +File-based Hierarchical Storage Management (FHSM)
  808. +----------------------------------------------------------------------
  809. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  810. +storage world. Aufs provides this feature as file-based with multiple
  811. +writable branches, based upon the principle of "Colder, the Lower".
  812. +Here the word "colder" means that the less used files, and "lower" means
  813. +that the position in the order of the stacked branches vertically.
  814. +These multiple writable branches are prioritized, ie. the topmost one
  815. +should be the fastest drive and be used heavily.
  816. +
  817. +o Characters in aufs FHSM story
  818. +- aufs itself and a new branch attribute.
  819. +- a new ioctl interface to move-down and to establish a connection with
  820. + the daemon ("move-down" is a converse of "copy-up").
  821. +- userspace tool and daemon.
  822. +
  823. +The userspace daemon establishes a connection with aufs and waits for
  824. +the notification. The notified information is very similar to struct
  825. +statfs containing the number of consumed blocks and inodes.
  826. +When the consumed blocks/inodes of a branch exceeds the user-specified
  827. +upper watermark, the daemon activates its move-down process until the
  828. +consumed blocks/inodes reaches the user-specified lower watermark.
  829. +
  830. +The actual move-down is done by aufs based upon the request from
  831. +user-space since we need to maintain the inode number and the internal
  832. +pointer arrays in aufs.
  833. +
  834. +Currently aufs FHSM handles the regular files only. Additionally they
  835. +must not be hard-linked nor pseudo-linked.
  836. +
  837. +
  838. +o Cowork of aufs and the user-space daemon
  839. + During the userspace daemon established the connection, aufs sends a
  840. + small notification to it whenever aufs writes something into the
  841. + writable branch. But it may cost high since aufs issues statfs(2)
  842. + internally. So user can specify a new option to cache the
  843. + info. Actually the notification is controlled by these factors.
  844. + + the specified cache time.
  845. + + classified as "force" by aufs internally.
  846. + Until the specified time expires, aufs doesn't send the info
  847. + except the forced cases. When aufs decide forcing, the info is always
  848. + notified to userspace.
  849. + For example, the number of free inodes is generally large enough and
  850. + the shortage of it happens rarely. So aufs doesn't force the
  851. + notification when creating a new file, directory and others. This is
  852. + the typical case which aufs doesn't force.
  853. + When aufs writes the actual filedata and the files consumes any of new
  854. + blocks, the aufs forces notifying.
  855. +
  856. +
  857. +o Interfaces in aufs
  858. +- New branch attribute.
  859. + + fhsm
  860. + Specifies that the branch is managed by FHSM feature. In other word,
  861. + participant in the FHSM.
  862. + When nofhsm is set to the branch, it will not be the source/target
  863. + branch of the move-down operation. This attribute is set
  864. + independently from coo and moo attributes, and if you want full
  865. + FHSM, you should specify them as well.
  866. +- New mount option.
  867. + + fhsm_sec
  868. + Specifies a second to suppress many less important info to be
  869. + notified.
  870. +- New ioctl.
  871. + + AUFS_CTL_FHSM_FD
  872. + create a new file descriptor which userspace can read the notification
  873. + (a subset of struct statfs) from aufs.
  874. +- Module parameter 'brs'
  875. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  876. + be set.
  877. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  878. + When there are two or more branches with fhsm attributes,
  879. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  880. + terminates it. As a result of remounting and branch-manipulation, the
  881. + number of branches with fhsm attribute can be one. In this case,
  882. + /sbin/mount.aufs will terminate the user-space daemon.
  883. +
  884. +
  885. +Finally the operation is done as these steps in kernel-space.
  886. +- make sure that,
  887. + + no one else is using the file.
  888. + + the file is not hard-linked.
  889. + + the file is not pseudo-linked.
  890. + + the file is a regular file.
  891. + + the parent dir is not opaqued.
  892. +- find the target writable branch.
  893. +- make sure the file is not whiteout-ed by the upper (than the target)
  894. + branch.
  895. +- make the parent dir on the target branch.
  896. +- mutex lock the inode on the branch.
  897. +- unlink the whiteout on the target branch (if exists).
  898. +- lookup and create the whiteout-ed temporary name on the target branch.
  899. +- copy the file as the whiteout-ed temporary name on the target branch.
  900. +- rename the whiteout-ed temporary name to the original name.
  901. +- unlink the file on the source branch.
  902. +- maintain the internal pointer array and the external inode number
  903. + table (XINO).
  904. +- maintain the timestamps and other attributes of the parent dir and the
  905. + file.
  906. +
  907. +And of course, in every step, an error may happen. So the operation
  908. +should restore the original file state after an error happens.
  909. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/06mmap.txt linux-4.0.4/Documentation/filesystems/aufs/design/06mmap.txt
  910. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
  911. +++ linux-4.0.4/Documentation/filesystems/aufs/design/06mmap.txt 2015-05-30 22:11:30.000000000 +0200
  912. @@ -0,0 +1,33 @@
  913. +
  914. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  915. +
  916. +mmap(2) -- File Memory Mapping
  917. +----------------------------------------------------------------------
  918. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  919. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  920. +->mmap().
  921. +This approach is simple and good, but there is one problem.
  922. +Under /proc, several entries show the mmapped files by its path (with
  923. +device and inode number), and the printed path will be the path on the
  924. +branch fs's instead of virtual aufs's.
  925. +This is not a problem in most cases, but some utilities lsof(1) (and its
  926. +user) may expect the path on aufs.
  927. +
  928. +To address this issue, aufs adds a new member called vm_prfile in struct
  929. +vm_area_struct (and struct vm_region). The original vm_file points to
  930. +the file on the branch fs in order to handle everything correctly as
  931. +usual. The new vm_prfile points to a virtual file in aufs, and the
  932. +show-functions in procfs refers to vm_prfile if it is set.
  933. +Also we need to maintain several other places where touching vm_file
  934. +such like
  935. +- fork()/clone() copies vma and the reference count of vm_file is
  936. + incremented.
  937. +- merging vma maintains the ref count too.
  938. +
  939. +This is not a good approach. It just fakes the printed path. But it
  940. +leaves all behaviour around f_mapping unchanged. This is surely an
  941. +advantage.
  942. +Actually aufs had adopted another complicated approach which calls
  943. +generic_file_mmap() and handles struct vm_operations_struct. In this
  944. +approach, aufs met a hard problem and I could not solve it without
  945. +switching the approach.
  946. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/06xattr.txt linux-4.0.4/Documentation/filesystems/aufs/design/06xattr.txt
  947. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
  948. +++ linux-4.0.4/Documentation/filesystems/aufs/design/06xattr.txt 2015-05-30 22:11:30.000000000 +0200
  949. @@ -0,0 +1,81 @@
  950. +
  951. +# Copyright (C) 2014-2015 Junjiro R. Okajima
  952. +
  953. +Listing XATTR/EA and getting the value
  954. +----------------------------------------------------------------------
  955. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  956. +shows the values from the topmost existing file. This behaviour is good
  957. +for the non-dir entries since the bahaviour exactly matches the shown
  958. +information. But for the directories, aufs considers all the same named
  959. +entries on the lower branches. Which means, if one of the lower entry
  960. +rejects readdir call, then aufs returns an error even if the topmost
  961. +entry allows it. This behaviour is necessary to respect the branch fs's
  962. +security, but can make users confused since the user-visible standard
  963. +attributes don't match the behaviour.
  964. +To address this issue, aufs has a mount option called dirperm1 which
  965. +checks the permission for the topmost entry only, and ignores the lower
  966. +entry's permission.
  967. +
  968. +A similar issue can happen around XATTR.
  969. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  970. +always set. Otherwise these very unpleasant situation would happen.
  971. +- listxattr(2) may return the duplicated entries.
  972. +- users may not be able to remove or reset the XATTR forever,
  973. +
  974. +
  975. +XATTR/EA support in the internal (copy,move)-(up,down)
  976. +----------------------------------------------------------------------
  977. +Generally the extended attributes of inode are categorized as these.
  978. +- "security" for LSM and capability.
  979. +- "system" for posix ACL, 'acl' mount option is required for the branch
  980. + fs generally.
  981. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  982. +- "user" for userspace, 'user_xattr' mount option is required for the
  983. + branch fs generally.
  984. +
  985. +Moreover there are some other categories. Aufs handles these rather
  986. +unpopular categories as the ordinary ones, ie. there is no special
  987. +condition nor exception.
  988. +
  989. +In copy-up, the support for XATTR on the dst branch may differ from the
  990. +src branch. In this case, the copy-up operation will get an error and
  991. +the original user operation which triggered the copy-up will fail. It
  992. +can happen that even all copy-up will fail.
  993. +When both of src and dst branches support XATTR and if an error occurs
  994. +during copying XATTR, then the copy-up should fail obviously. That is a
  995. +good reason and aufs should return an error to userspace. But when only
  996. +the src branch support that XATTR, aufs should not return an error.
  997. +For example, the src branch supports ACL but the dst branch doesn't
  998. +because the dst branch may natively un-support it or temporary
  999. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1000. +may NOT return an error even if the XATTR is not supported. It is
  1001. +totally up to the branch fs.
  1002. +
  1003. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1004. +fs, then aufs tries removing the just copied entry and returns the error
  1005. +to the userspace. The worst case of this situation will be all copy-up
  1006. +will fail.
  1007. +
  1008. +For the copy-up operation, there two basic approaches.
  1009. +- copy the specified XATTR only (by category above), and return the
  1010. + error unconditionally if it happens.
  1011. +- copy all XATTR, and ignore the error on the specified category only.
  1012. +
  1013. +In order to support XATTR and to implement the correct behaviour, aufs
  1014. +chooses the latter approach and introduces some new branch attributes,
  1015. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1016. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1017. +convenient, "icex" is also provided which means all "icex*" attributes
  1018. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1019. +
  1020. +The meaning of these attributes is to ignore the error from setting
  1021. +XATTR on that branch.
  1022. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1023. +error from the dst branch according to the specified attributes.
  1024. +
  1025. +Some XATTR may have its default value. The default value may come from
  1026. +the parent dir or the environment. If the default value is set at the
  1027. +file creating-time, it will be overwritten by copy-up.
  1028. +Some contradiction may happen I am afraid.
  1029. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1030. +now, aufs implements the branch attributes to ignore the error.
  1031. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/07export.txt linux-4.0.4/Documentation/filesystems/aufs/design/07export.txt
  1032. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
  1033. +++ linux-4.0.4/Documentation/filesystems/aufs/design/07export.txt 2015-05-30 22:11:30.000000000 +0200
  1034. @@ -0,0 +1,45 @@
  1035. +
  1036. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  1037. +
  1038. +Export Aufs via NFS
  1039. +----------------------------------------------------------------------
  1040. +Here is an approach.
  1041. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1042. + generation.
  1043. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1044. + store it in xigen file.
  1045. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1046. + file. it is necessary even if it is not unlinked, because any data of
  1047. + inode may be changed by UDBA.
  1048. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1049. + build file handle by
  1050. + + branch id (4 bytes)
  1051. + + superblock generation (4 bytes)
  1052. + + inode number (4 or 8 bytes)
  1053. + + parent dir inode number (4 or 8 bytes)
  1054. + + inode generation (4 bytes))
  1055. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1056. + bytes)
  1057. + + file handle for a branch (by exportfs_encode_fh())
  1058. +- fh_to_dentry():
  1059. + + find the index of a branch from its id in handle, and check it is
  1060. + still exist in aufs.
  1061. + + 1st level: get the inode number from handle and search it in cache.
  1062. + + 2nd level: if not found in cache, get the parent inode number from
  1063. + the handle and search it in cache. and then open the found parent
  1064. + dir, find the matching inode number by vfs_readdir() and get its
  1065. + name, and call lookup_one_len() for the target dentry.
  1066. + + 3rd level: if the parent dir is not cached, call
  1067. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1068. + build a pathname of it, convert it a pathname in aufs, call
  1069. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1070. + the 2nd level.
  1071. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1072. + for every branch, but not itself. to get this, (currently) aufs
  1073. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1074. + idea, but I didn't get other approach.
  1075. + + test the generation of the gotten inode.
  1076. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1077. + convert it into ESTALE for NFSD.
  1078. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1079. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1080. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/08shwh.txt linux-4.0.4/Documentation/filesystems/aufs/design/08shwh.txt
  1081. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
  1082. +++ linux-4.0.4/Documentation/filesystems/aufs/design/08shwh.txt 2015-05-30 22:11:30.000000000 +0200
  1083. @@ -0,0 +1,39 @@
  1084. +
  1085. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  1086. +
  1087. +Show Whiteout Mode (shwh)
  1088. +----------------------------------------------------------------------
  1089. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1090. +them is very useful for users. For instance, creating a new middle layer
  1091. +(branch) by merging existing layers.
  1092. +
  1093. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1094. +When you have three branches,
  1095. +- Bottom: 'system', squashfs (underlying base system), read-only
  1096. +- Middle: 'mods', squashfs, read-only
  1097. +- Top: 'overlay', ram (tmpfs), read-write
  1098. +
  1099. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1100. +the changes made to the system during the session.
  1101. +When larger changes have been made, or smaller changes have accumulated,
  1102. +the size of the saved top layer data grows. At this point, it would be
  1103. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1104. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1105. +restoring save and load speed.
  1106. +
  1107. +This merging is simplified by the use of another aufs mount, of just the
  1108. +two overlay branches using the 'shwh' option.
  1109. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1110. + aufs /livesys/merge_union
  1111. +
  1112. +A merged view of these two branches is then available at
  1113. +/livesys/merge_union, and the new feature is that the whiteouts are
  1114. +visible!
  1115. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1116. +writing to all branches. Also the default mode for all branches is 'ro'.
  1117. +It is now possible to save the combined contents of the two overlay
  1118. +branches to a new squashfs, e.g.:
  1119. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1120. +
  1121. +This new squashfs archive can be stored on the boot device and the
  1122. +initramfs will use it to replace the old one at the next boot.
  1123. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/design/10dynop.txt linux-4.0.4/Documentation/filesystems/aufs/design/10dynop.txt
  1124. --- linux-4.0.4.orig/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
  1125. +++ linux-4.0.4/Documentation/filesystems/aufs/design/10dynop.txt 2015-05-30 22:11:30.000000000 +0200
  1126. @@ -0,0 +1,34 @@
  1127. +
  1128. +# Copyright (C) 2010-2015 Junjiro R. Okajima
  1129. +
  1130. +Dynamically customizable FS operations
  1131. +----------------------------------------------------------------------
  1132. +Generally FS operations (struct inode_operations, struct
  1133. +address_space_operations, struct file_operations, etc.) are defined as
  1134. +"static const", but it never means that FS have only one set of
  1135. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1136. +three sets, one for XIP, for NOBH, and for normal.
  1137. +Since aufs overrides and redirects these operations, sometimes aufs has
  1138. +to change its behaviour according to the branch FS type. More importantly
  1139. +VFS acts differently if a function (member in the struct) is set or
  1140. +not. It means aufs should have several sets of operations and select one
  1141. +among them according to the branch FS definition.
  1142. +
  1143. +In order to solve this problem and not to affect the behaviour of VFS,
  1144. +aufs defines these operations dynamically. For instance, aufs defines
  1145. +dummy direct_IO function for struct address_space_operations, but it may
  1146. +not be set to the address_space_operations actually. When the branch FS
  1147. +doesn't have it, aufs doesn't set it to its address_space_operations
  1148. +while the function definition itself is still alive. So the behaviour
  1149. +itself will not change, and it will return an error when direct_IO is
  1150. +not set.
  1151. +
  1152. +The lifetime of these dynamically generated operation object is
  1153. +maintained by aufs branch object. When the branch is removed from aufs,
  1154. +the reference counter of the object is decremented. When it reaches
  1155. +zero, the dynamically generated operation object will be freed.
  1156. +
  1157. +This approach is designed to support AIO (io_submit), Direct I/O and
  1158. +XIP (DAX) mainly.
  1159. +Currently this approach is applied to address_space_operations for
  1160. +regular files only.
  1161. diff -Nur linux-4.0.4.orig/Documentation/filesystems/aufs/README linux-4.0.4/Documentation/filesystems/aufs/README
  1162. --- linux-4.0.4.orig/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
  1163. +++ linux-4.0.4/Documentation/filesystems/aufs/README 2015-05-30 22:11:29.000000000 +0200
  1164. @@ -0,0 +1,383 @@
  1165. +
  1166. +Aufs4 -- advanced multi layered unification filesystem version 4.x
  1167. +http://aufs.sf.net
  1168. +Junjiro R. Okajima
  1169. +
  1170. +
  1171. +0. Introduction
  1172. +----------------------------------------
  1173. +In the early days, aufs was entirely re-designed and re-implemented
  1174. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1175. +improvements and implementations, it becomes totally different from
  1176. +Unionfs while keeping the basic features.
  1177. +Recently, Unionfs Version 2.x series begin taking some of the same
  1178. +approaches to aufs1's.
  1179. +Unionfs is being developed by Professor Erez Zadok at Stony Brook
  1180. +University and his team.
  1181. +
  1182. +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
  1183. +If you want older kernel version support, try aufs2-2.6.git or
  1184. +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
  1185. +
  1186. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1187. + According to Christoph Hellwig, linux rejects all union-type
  1188. + filesystems but UnionMount.
  1189. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1190. +
  1191. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1192. + UnionMount, and he pointed out an issue around a directory mutex
  1193. + lock and aufs addressed it. But it is still unsure whether aufs will
  1194. + be merged (or any other union solution).
  1195. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1196. +
  1197. +
  1198. +1. Features
  1199. +----------------------------------------
  1200. +- unite several directories into a single virtual filesystem. The member
  1201. + directory is called as a branch.
  1202. +- you can specify the permission flags to the branch, which are 'readonly',
  1203. + 'readwrite' and 'whiteout-able.'
  1204. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1205. + readonly branch are modifiable logically.
  1206. +- dynamic branch manipulation, add, del.
  1207. +- etc...
  1208. +
  1209. +Also there are many enhancements in aufs, such as:
  1210. +- test only the highest one for the directory permission (dirperm1)
  1211. +- copyup on open (coo=)
  1212. +- 'move' policy for copy-up between two writable branches, after
  1213. + checking free space.
  1214. +- xattr, acl
  1215. +- readdir(3) in userspace.
  1216. +- keep inode number by external inode number table
  1217. +- keep the timestamps of file/dir in internal copyup operation
  1218. +- seekable directory, supporting NFS readdir.
  1219. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1220. + on branch
  1221. +- do not copyup, nor create a whiteout when it is unnecessary
  1222. +- revert a single systemcall when an error occurs in aufs
  1223. +- remount interface instead of ioctl
  1224. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1225. +- loopback mounted filesystem as a branch
  1226. +- kernel thread for removing the dir who has a plenty of whiteouts
  1227. +- support copyup sparse file (a file which has a 'hole' in it)
  1228. +- default permission flags for branches
  1229. +- selectable permission flags for ro branch, whether whiteout can
  1230. + exist or not
  1231. +- export via NFS.
  1232. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1233. +- support multiple writable branches, some policies to select one
  1234. + among multiple writable branches.
  1235. +- a new semantics for link(2) and rename(2) to support multiple
  1236. + writable branches.
  1237. +- no glibc changes are required.
  1238. +- pseudo hardlink (hardlink over branches)
  1239. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1240. + including NFS or remote filesystem branch.
  1241. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1242. +- and more...
  1243. +
  1244. +Currently these features are dropped temporary from aufs4.
  1245. +See design/08plan.txt in detail.
  1246. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1247. + (robr)
  1248. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1249. +
  1250. +Features or just an idea in the future (see also design/*.txt),
  1251. +- reorder the branch index without del/re-add.
  1252. +- permanent xino files for NFSD
  1253. +- an option for refreshing the opened files after add/del branches
  1254. +- light version, without branch manipulation. (unnecessary?)
  1255. +- copyup in userspace
  1256. +- inotify in userspace
  1257. +- readv/writev
  1258. +
  1259. +
  1260. +2. Download
  1261. +----------------------------------------
  1262. +There are three GIT trees for aufs4, aufs4-linux.git,
  1263. +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
  1264. +"aufs-util.git."
  1265. +While the aufs-util is always necessary, you need either of aufs4-linux
  1266. +or aufs4-standalone.
  1267. +
  1268. +The aufs4-linux tree includes the whole linux mainline GIT tree,
  1269. +git://git.kernel.org/.../torvalds/linux.git.
  1270. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1271. +build aufs4 as an external kernel module.
  1272. +Several extra patches are not included in this tree. Only
  1273. +aufs4-standalone tree contains them. They are describe in the later
  1274. +section "Configuration and Compilation."
  1275. +
  1276. +On the other hand, the aufs4-standalone tree has only aufs source files
  1277. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1278. +But you need to apply all aufs patches manually.
  1279. +
  1280. +You will find GIT branches whose name is in form of "aufs4.x" where "x"
  1281. +represents the linux kernel version, "linux-4.x". For instance,
  1282. +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
  1283. +"aufs4.x-rcN" branch.
  1284. +
  1285. +o aufs4-linux tree
  1286. +$ git clone --reference /your/linux/git/tree \
  1287. + git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
  1288. +- if you don't have linux GIT tree, then remove "--reference ..."
  1289. +$ cd aufs4-linux.git
  1290. +$ git checkout origin/aufs4.0
  1291. +
  1292. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1293. +leave the patch-work to GIT.
  1294. +$ cd /your/linux/git/tree
  1295. +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
  1296. +$ git fetch aufs4
  1297. +$ git checkout -b my4.0 v4.0
  1298. +$ (add your local change...)
  1299. +$ git pull aufs4 aufs4.0
  1300. +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
  1301. +- you may need to solve some conflicts between your_changes and
  1302. + aufs4.0. in this case, git-rerere is recommended so that you can
  1303. + solve the similar conflicts automatically when you upgrade to 4.1 or
  1304. + later in the future.
  1305. +
  1306. +o aufs4-standalone tree
  1307. +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
  1308. +$ cd aufs4-standalone.git
  1309. +$ git checkout origin/aufs4.0
  1310. +
  1311. +o aufs-util tree
  1312. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1313. +- note that the public aufs-util.git is on SourceForge instead of
  1314. + GitHUB.
  1315. +$ cd aufs-util.git
  1316. +$ git checkout origin/aufs4.0
  1317. +
  1318. +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1319. +The minor version number, 'x' in '4.x', of aufs may not always
  1320. +follow the minor version number of the kernel.
  1321. +Because changes in the kernel that cause the use of a new
  1322. +minor version number do not always require changes to aufs-util.
  1323. +
  1324. +Since aufs-util has its own minor version number, you may not be
  1325. +able to find a GIT branch in aufs-util for your kernel's
  1326. +exact minor version number.
  1327. +In this case, you should git-checkout the branch for the
  1328. +nearest lower number.
  1329. +
  1330. +For (an unreleased) example:
  1331. +If you are using "linux-4.10" and the "aufs4.10" branch
  1332. +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
  1333. +or something numerically smaller is the branch for your kernel.
  1334. +
  1335. +Also you can view all branches by
  1336. + $ git branch -a
  1337. +
  1338. +
  1339. +3. Configuration and Compilation
  1340. +----------------------------------------
  1341. +Make sure you have git-checkout'ed the correct branch.
  1342. +
  1343. +For aufs4-linux tree,
  1344. +- enable CONFIG_AUFS_FS.
  1345. +- set other aufs configurations if necessary.
  1346. +
  1347. +For aufs4-standalone tree,
  1348. +There are several ways to build.
  1349. +
  1350. +1.
  1351. +- apply ./aufs4-kbuild.patch to your kernel source files.
  1352. +- apply ./aufs4-base.patch too.
  1353. +- apply ./aufs4-mmap.patch too.
  1354. +- apply ./aufs4-standalone.patch too, if you have a plan to set
  1355. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
  1356. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1357. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1358. +- enable CONFIG_AUFS_FS, you can select either
  1359. + =m or =y.
  1360. +- and build your kernel as usual.
  1361. +- install the built kernel.
  1362. + Note: Since linux-3.9, every filesystem module requires an alias
  1363. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1364. + modules.aliases file if you set CONFIG_AUFS_FS=m.
  1365. +- install the header files too by "make headers_install" to the
  1366. + directory where you specify. By default, it is $PWD/usr.
  1367. + "make help" shows a brief note for headers_install.
  1368. +- and reboot your system.
  1369. +
  1370. +2.
  1371. +- module only (CONFIG_AUFS_FS=m).
  1372. +- apply ./aufs4-base.patch to your kernel source files.
  1373. +- apply ./aufs4-mmap.patch too.
  1374. +- apply ./aufs4-standalone.patch too.
  1375. +- build your kernel, don't forget "make headers_install", and reboot.
  1376. +- edit ./config.mk and set other aufs configurations if necessary.
  1377. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1378. + every aufs configurations.
  1379. +- build the module by simple "make".
  1380. + Note: Since linux-3.9, every filesystem module requires an alias
  1381. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1382. + modules.aliases file.
  1383. +- you can specify ${KDIR} make variable which points to your kernel
  1384. + source tree.
  1385. +- install the files
  1386. + + run "make install" to install the aufs module, or copy the built
  1387. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1388. + + run "make install_headers" (instead of headers_install) to install
  1389. + the modified aufs header file (you can specify DESTDIR which is
  1390. + available in aufs standalone version's Makefile only), or copy
  1391. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1392. + you like manually. By default, the target directory is $PWD/usr.
  1393. +- no need to apply aufs4-kbuild.patch, nor copying source files to your
  1394. + kernel source tree.
  1395. +
  1396. +Note: The header file aufs_type.h is necessary to build aufs-util
  1397. + as well as "make headers_install" in the kernel source tree.
  1398. + headers_install is subject to be forgotten, but it is essentially
  1399. + necessary, not only for building aufs-util.
  1400. + You may not meet problems without headers_install in some older
  1401. + version though.
  1402. +
  1403. +And then,
  1404. +- read README in aufs-util, build and install it
  1405. +- note that your distribution may contain an obsoleted version of
  1406. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1407. + utilities, make sure that your compiler refers the correct aufs header
  1408. + file which is built by "make headers_install."
  1409. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1410. + then run "make install_ulib" too. And refer to the aufs manual in
  1411. + detail.
  1412. +
  1413. +There several other patches in aufs4-standalone.git. They are all
  1414. +optional. When you meet some problems, they will help you.
  1415. +- aufs4-loopback.patch
  1416. + Supports a nested loopback mount in a branch-fs. This patch is
  1417. + unnecessary until aufs produces a message like "you may want to try
  1418. + another patch for loopback file".
  1419. +- vfs-ino.patch
  1420. + Modifies a system global kernel internal function get_next_ino() in
  1421. + order to stop assigning 0 for an inode-number. Not directly related to
  1422. + aufs, but recommended generally.
  1423. +- tmpfs-idr.patch
  1424. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1425. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1426. + duplication of inode number, which is important for backup tools and
  1427. + other utilities. When you find aufs XINO files for tmpfs branch
  1428. + growing too much, try this patch.
  1429. +
  1430. +
  1431. +4. Usage
  1432. +----------------------------------------
  1433. +At first, make sure aufs-util are installed, and please read the aufs
  1434. +manual, aufs.5 in aufs-util.git tree.
  1435. +$ man -l aufs.5
  1436. +
  1437. +And then,
  1438. +$ mkdir /tmp/rw /tmp/aufs
  1439. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1440. +
  1441. +Here is another example. The result is equivalent.
  1442. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1443. + Or
  1444. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1445. +# mount -o remount,append:${HOME} /tmp/aufs
  1446. +
  1447. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1448. +you modify a file under /tmp/aufs, the one on your home directory is
  1449. +not affected, instead the same named file will be newly created under
  1450. +/tmp/rw. And all of your modification to a file will be applied to
  1451. +the one under /tmp/rw. This is called the file based Copy on Write
  1452. +(COW) method.
  1453. +Aufs mount options are described in aufs.5.
  1454. +If you run chroot or something and make your aufs as a root directory,
  1455. +then you need to customize the shutdown script. See the aufs manual in
  1456. +detail.
  1457. +
  1458. +Additionally, there are some sample usages of aufs which are a
  1459. +diskless system with network booting, and LiveCD over NFS.
  1460. +See sample dir in CVS tree on SourceForge.
  1461. +
  1462. +
  1463. +5. Contact
  1464. +----------------------------------------
  1465. +When you have any problems or strange behaviour in aufs, please let me
  1466. +know with:
  1467. +- /proc/mounts (instead of the output of mount(8))
  1468. +- /sys/module/aufs/*
  1469. +- /sys/fs/aufs/* (if you have them)
  1470. +- /debug/aufs/* (if you have them)
  1471. +- linux kernel version
  1472. + if your kernel is not plain, for example modified by distributor,
  1473. + the url where i can download its source is necessary too.
  1474. +- aufs version which was printed at loading the module or booting the
  1475. + system, instead of the date you downloaded.
  1476. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1477. +- kernel configuration or /proc/config.gz (if you have it)
  1478. +- behaviour which you think to be incorrect
  1479. +- actual operation, reproducible one is better
  1480. +- mailto: aufs-users at lists.sourceforge.net
  1481. +
  1482. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1483. +and Feature Requests) on SourceForge. Please join and write to
  1484. +aufs-users ML.
  1485. +
  1486. +
  1487. +6. Acknowledgements
  1488. +----------------------------------------
  1489. +Thanks to everyone who have tried and are using aufs, whoever
  1490. +have reported a bug or any feedback.
  1491. +
  1492. +Especially donators:
  1493. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1494. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1495. + scripts) is making "doubling" donations.
  1496. + Unfortunately I cannot list all of the donators, but I really
  1497. + appreciate.
  1498. + It ends Aug 2010, but the ordinary donation URL is still available.
  1499. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1500. +Dai Itasaka made a donation (2007/8).
  1501. +Chuck Smith made a donation (2008/4, 10 and 12).
  1502. +Henk Schoneveld made a donation (2008/9).
  1503. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1504. +Francois Dupoux made a donation (2008/11).
  1505. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1506. + aufs2 GIT tree (2009/2).
  1507. +William Grant made a donation (2009/3).
  1508. +Patrick Lane made a donation (2009/4).
  1509. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1510. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1511. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1512. +Pavel Pronskiy made a donation (2011/2).
  1513. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1514. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1515. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1516. +11).
  1517. +Sam Liddicott made a donation (2011/9).
  1518. +Era Scarecrow made a donation (2013/4).
  1519. +Bor Ratajc made a donation (2013/4).
  1520. +Alessandro Gorreta made a donation (2013/4).
  1521. +POIRETTE Marc made a donation (2013/4).
  1522. +Alessandro Gorreta made a donation (2013/4).
  1523. +lauri kasvandik made a donation (2013/5).
  1524. +"pemasu from Finland" made a donation (2013/7).
  1525. +The Parted Magic Project made a donation (2013/9 and 11).
  1526. +Pavel Barta made a donation (2013/10).
  1527. +Nikolay Pertsev made a donation (2014/5).
  1528. +James B made a donation (2014/7).
  1529. +Stefano Di Biase made a donation (2014/8).
  1530. +Daniel Epellei made a donation (2015/1).
  1531. +
  1532. +Thank you very much.
  1533. +Donations are always, including future donations, very important and
  1534. +helpful for me to keep on developing aufs.
  1535. +
  1536. +
  1537. +7.
  1538. +----------------------------------------
  1539. +If you are an experienced user, no explanation is needed. Aufs is
  1540. +just a linux filesystem.
  1541. +
  1542. +
  1543. +Enjoy!
  1544. +
  1545. +# Local variables: ;
  1546. +# mode: text;
  1547. +# End: ;
  1548. diff -Nur linux-4.0.4.orig/drivers/block/loop.c linux-4.0.4/drivers/block/loop.c
  1549. --- linux-4.0.4.orig/drivers/block/loop.c 2015-05-17 18:55:21.000000000 +0200
  1550. +++ linux-4.0.4/drivers/block/loop.c 2015-05-30 22:11:30.000000000 +0200
  1551. @@ -592,6 +592,24 @@
  1552. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  1553. }
  1554. +/*
  1555. + * for AUFS
  1556. + * no get/put for file.
  1557. + */
  1558. +struct file *loop_backing_file(struct super_block *sb)
  1559. +{
  1560. + struct file *ret;
  1561. + struct loop_device *l;
  1562. +
  1563. + ret = NULL;
  1564. + if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
  1565. + l = sb->s_bdev->bd_disk->private_data;
  1566. + ret = l->lo_backing_file;
  1567. + }
  1568. + return ret;
  1569. +}
  1570. +EXPORT_SYMBOL(loop_backing_file);
  1571. +
  1572. /* loop sysfs attributes */
  1573. static ssize_t loop_attr_show(struct device *dev, char *page,
  1574. diff -Nur linux-4.0.4.orig/fs/aufs/aufs.h linux-4.0.4/fs/aufs/aufs.h
  1575. --- linux-4.0.4.orig/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
  1576. +++ linux-4.0.4/fs/aufs/aufs.h 2015-05-30 22:11:30.000000000 +0200
  1577. @@ -0,0 +1,46 @@
  1578. +/*
  1579. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  1580. + */
  1581. +
  1582. +/*
  1583. + * all header files
  1584. + */
  1585. +
  1586. +#ifndef __AUFS_H__
  1587. +#define __AUFS_H__
  1588. +
  1589. +#ifdef __KERNEL__
  1590. +
  1591. +#define AuStub(type, name, body, ...) \
  1592. + static inline type name(__VA_ARGS__) { body; }
  1593. +
  1594. +#define AuStubVoid(name, ...) \
  1595. + AuStub(void, name, , __VA_ARGS__)
  1596. +#define AuStubInt0(name, ...) \
  1597. + AuStub(int, name, return 0, __VA_ARGS__)
  1598. +
  1599. +#include "debug.h"
  1600. +
  1601. +#include "branch.h"
  1602. +#include "cpup.h"
  1603. +#include "dcsub.h"
  1604. +#include "dbgaufs.h"
  1605. +#include "dentry.h"
  1606. +#include "dir.h"
  1607. +#include "dynop.h"
  1608. +#include "file.h"
  1609. +#include "fstype.h"
  1610. +#include "inode.h"
  1611. +#include "loop.h"
  1612. +#include "module.h"
  1613. +#include "opts.h"
  1614. +#include "rwsem.h"
  1615. +#include "spl.h"
  1616. +#include "super.h"
  1617. +#include "sysaufs.h"
  1618. +#include "vfsub.h"
  1619. +#include "whout.h"
  1620. +#include "wkq.h"
  1621. +
  1622. +#endif /* __KERNEL__ */
  1623. +#endif /* __AUFS_H__ */
  1624. diff -Nur linux-4.0.4.orig/fs/aufs/branch.c linux-4.0.4/fs/aufs/branch.c
  1625. --- linux-4.0.4.orig/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
  1626. +++ linux-4.0.4/fs/aufs/branch.c 2015-05-30 22:11:30.000000000 +0200
  1627. @@ -0,0 +1,1393 @@
  1628. +/*
  1629. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  1630. + */
  1631. +
  1632. +/*
  1633. + * branch management
  1634. + */
  1635. +
  1636. +#include <linux/compat.h>
  1637. +#include <linux/statfs.h>
  1638. +#include "aufs.h"
  1639. +
  1640. +/*
  1641. + * free a single branch
  1642. + */
  1643. +static void au_br_do_free(struct au_branch *br)
  1644. +{
  1645. + int i;
  1646. + struct au_wbr *wbr;
  1647. + struct au_dykey **key;
  1648. +
  1649. + au_hnotify_fin_br(br);
  1650. +
  1651. + if (br->br_xino.xi_file)
  1652. + fput(br->br_xino.xi_file);
  1653. + mutex_destroy(&br->br_xino.xi_nondir_mtx);
  1654. +
  1655. + AuDebugOn(atomic_read(&br->br_count));
  1656. +
  1657. + wbr = br->br_wbr;
  1658. + if (wbr) {
  1659. + for (i = 0; i < AuBrWh_Last; i++)
  1660. + dput(wbr->wbr_wh[i]);
  1661. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  1662. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  1663. + }
  1664. +
  1665. + if (br->br_fhsm) {
  1666. + au_br_fhsm_fin(br->br_fhsm);
  1667. + kfree(br->br_fhsm);
  1668. + }
  1669. +
  1670. + key = br->br_dykey;
  1671. + for (i = 0; i < AuBrDynOp; i++, key++)
  1672. + if (*key)
  1673. + au_dy_put(*key);
  1674. + else
  1675. + break;
  1676. +
  1677. + /* recursive lock, s_umount of branch's */
  1678. + lockdep_off();
  1679. + path_put(&br->br_path);
  1680. + lockdep_on();
  1681. + kfree(wbr);
  1682. + kfree(br);
  1683. +}
  1684. +
  1685. +/*
  1686. + * frees all branches
  1687. + */
  1688. +void au_br_free(struct au_sbinfo *sbinfo)
  1689. +{
  1690. + aufs_bindex_t bmax;
  1691. + struct au_branch **br;
  1692. +
  1693. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  1694. +
  1695. + bmax = sbinfo->si_bend + 1;
  1696. + br = sbinfo->si_branch;
  1697. + while (bmax--)
  1698. + au_br_do_free(*br++);
  1699. +}
  1700. +
  1701. +/*
  1702. + * find the index of a branch which is specified by @br_id.
  1703. + */
  1704. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  1705. +{
  1706. + aufs_bindex_t bindex, bend;
  1707. +
  1708. + bend = au_sbend(sb);
  1709. + for (bindex = 0; bindex <= bend; bindex++)
  1710. + if (au_sbr_id(sb, bindex) == br_id)
  1711. + return bindex;
  1712. + return -1;
  1713. +}
  1714. +
  1715. +/* ---------------------------------------------------------------------- */
  1716. +
  1717. +/*
  1718. + * add a branch
  1719. + */
  1720. +
  1721. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  1722. + struct dentry *h_root)
  1723. +{
  1724. + if (unlikely(h_adding == h_root
  1725. + || au_test_loopback_overlap(sb, h_adding)))
  1726. + return 1;
  1727. + if (h_adding->d_sb != h_root->d_sb)
  1728. + return 0;
  1729. + return au_test_subdir(h_adding, h_root)
  1730. + || au_test_subdir(h_root, h_adding);
  1731. +}
  1732. +
  1733. +/*
  1734. + * returns a newly allocated branch. @new_nbranch is a number of branches
  1735. + * after adding a branch.
  1736. + */
  1737. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  1738. + int perm)
  1739. +{
  1740. + struct au_branch *add_branch;
  1741. + struct dentry *root;
  1742. + int err;
  1743. +
  1744. + err = -ENOMEM;
  1745. + root = sb->s_root;
  1746. + add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
  1747. + if (unlikely(!add_branch))
  1748. + goto out;
  1749. +
  1750. + err = au_hnotify_init_br(add_branch, perm);
  1751. + if (unlikely(err))
  1752. + goto out_br;
  1753. +
  1754. + add_branch->br_wbr = NULL;
  1755. + if (au_br_writable(perm)) {
  1756. + /* may be freed separately at changing the branch permission */
  1757. + add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
  1758. + GFP_NOFS);
  1759. + if (unlikely(!add_branch->br_wbr))
  1760. + goto out_hnotify;
  1761. + }
  1762. +
  1763. + add_branch->br_fhsm = NULL;
  1764. + if (au_br_fhsm(perm)) {
  1765. + err = au_fhsm_br_alloc(add_branch);
  1766. + if (unlikely(err))
  1767. + goto out_wbr;
  1768. + }
  1769. +
  1770. + err = au_sbr_realloc(au_sbi(sb), new_nbranch);
  1771. + if (!err)
  1772. + err = au_di_realloc(au_di(root), new_nbranch);
  1773. + if (!err)
  1774. + err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
  1775. + if (!err)
  1776. + return add_branch; /* success */
  1777. +
  1778. +out_wbr:
  1779. + kfree(add_branch->br_wbr);
  1780. +out_hnotify:
  1781. + au_hnotify_fin_br(add_branch);
  1782. +out_br:
  1783. + kfree(add_branch);
  1784. +out:
  1785. + return ERR_PTR(err);
  1786. +}
  1787. +
  1788. +/*
  1789. + * test if the branch permission is legal or not.
  1790. + */
  1791. +static int test_br(struct inode *inode, int brperm, char *path)
  1792. +{
  1793. + int err;
  1794. +
  1795. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  1796. + if (!err)
  1797. + goto out;
  1798. +
  1799. + err = -EINVAL;
  1800. + pr_err("write permission for readonly mount or inode, %s\n", path);
  1801. +
  1802. +out:
  1803. + return err;
  1804. +}
  1805. +
  1806. +/*
  1807. + * returns:
  1808. + * 0: success, the caller will add it
  1809. + * plus: success, it is already unified, the caller should ignore it
  1810. + * minus: error
  1811. + */
  1812. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  1813. +{
  1814. + int err;
  1815. + aufs_bindex_t bend, bindex;
  1816. + struct dentry *root;
  1817. + struct inode *inode, *h_inode;
  1818. +
  1819. + root = sb->s_root;
  1820. + bend = au_sbend(sb);
  1821. + if (unlikely(bend >= 0
  1822. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  1823. + err = 1;
  1824. + if (!remount) {
  1825. + err = -EINVAL;
  1826. + pr_err("%s duplicated\n", add->pathname);
  1827. + }
  1828. + goto out;
  1829. + }
  1830. +
  1831. + err = -ENOSPC; /* -E2BIG; */
  1832. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  1833. + || AUFS_BRANCH_MAX - 1 <= bend)) {
  1834. + pr_err("number of branches exceeded %s\n", add->pathname);
  1835. + goto out;
  1836. + }
  1837. +
  1838. + err = -EDOM;
  1839. + if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
  1840. + pr_err("bad index %d\n", add->bindex);
  1841. + goto out;
  1842. + }
  1843. +
  1844. + inode = add->path.dentry->d_inode;
  1845. + err = -ENOENT;
  1846. + if (unlikely(!inode->i_nlink)) {
  1847. + pr_err("no existence %s\n", add->pathname);
  1848. + goto out;
  1849. + }
  1850. +
  1851. + err = -EINVAL;
  1852. + if (unlikely(inode->i_sb == sb)) {
  1853. + pr_err("%s must be outside\n", add->pathname);
  1854. + goto out;
  1855. + }
  1856. +
  1857. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  1858. + pr_err("unsupported filesystem, %s (%s)\n",
  1859. + add->pathname, au_sbtype(inode->i_sb));
  1860. + goto out;
  1861. + }
  1862. +
  1863. + if (unlikely(inode->i_sb->s_stack_depth)) {
  1864. + pr_err("already stacked, %s (%s)\n",
  1865. + add->pathname, au_sbtype(inode->i_sb));
  1866. + goto out;
  1867. + }
  1868. +
  1869. + err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
  1870. + if (unlikely(err))
  1871. + goto out;
  1872. +
  1873. + if (bend < 0)
  1874. + return 0; /* success */
  1875. +
  1876. + err = -EINVAL;
  1877. + for (bindex = 0; bindex <= bend; bindex++)
  1878. + if (unlikely(test_overlap(sb, add->path.dentry,
  1879. + au_h_dptr(root, bindex)))) {
  1880. + pr_err("%s is overlapped\n", add->pathname);
  1881. + goto out;
  1882. + }
  1883. +
  1884. + err = 0;
  1885. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  1886. + h_inode = au_h_dptr(root, 0)->d_inode;
  1887. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  1888. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  1889. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  1890. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  1891. + add->pathname,
  1892. + i_uid_read(inode), i_gid_read(inode),
  1893. + (inode->i_mode & S_IALLUGO),
  1894. + i_uid_read(h_inode), i_gid_read(h_inode),
  1895. + (h_inode->i_mode & S_IALLUGO));
  1896. + }
  1897. +
  1898. +out:
  1899. + return err;
  1900. +}
  1901. +
  1902. +/*
  1903. + * initialize or clean the whiteouts for an adding branch
  1904. + */
  1905. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  1906. + int new_perm)
  1907. +{
  1908. + int err, old_perm;
  1909. + aufs_bindex_t bindex;
  1910. + struct mutex *h_mtx;
  1911. + struct au_wbr *wbr;
  1912. + struct au_hinode *hdir;
  1913. +
  1914. + err = vfsub_mnt_want_write(au_br_mnt(br));
  1915. + if (unlikely(err))
  1916. + goto out;
  1917. +
  1918. + wbr = br->br_wbr;
  1919. + old_perm = br->br_perm;
  1920. + br->br_perm = new_perm;
  1921. + hdir = NULL;
  1922. + h_mtx = NULL;
  1923. + bindex = au_br_index(sb, br->br_id);
  1924. + if (0 <= bindex) {
  1925. + hdir = au_hi(sb->s_root->d_inode, bindex);
  1926. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  1927. + } else {
  1928. + h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
  1929. + mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
  1930. + }
  1931. + if (!wbr)
  1932. + err = au_wh_init(br, sb);
  1933. + else {
  1934. + wbr_wh_write_lock(wbr);
  1935. + err = au_wh_init(br, sb);
  1936. + wbr_wh_write_unlock(wbr);
  1937. + }
  1938. + if (hdir)
  1939. + au_hn_imtx_unlock(hdir);
  1940. + else
  1941. + mutex_unlock(h_mtx);
  1942. + vfsub_mnt_drop_write(au_br_mnt(br));
  1943. + br->br_perm = old_perm;
  1944. +
  1945. + if (!err && wbr && !au_br_writable(new_perm)) {
  1946. + kfree(wbr);
  1947. + br->br_wbr = NULL;
  1948. + }
  1949. +
  1950. +out:
  1951. + return err;
  1952. +}
  1953. +
  1954. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  1955. + int perm)
  1956. +{
  1957. + int err;
  1958. + struct kstatfs kst;
  1959. + struct au_wbr *wbr;
  1960. +
  1961. + wbr = br->br_wbr;
  1962. + au_rw_init(&wbr->wbr_wh_rwsem);
  1963. + memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
  1964. + atomic_set(&wbr->wbr_wh_running, 0);
  1965. + wbr->wbr_bytes = 0;
  1966. +
  1967. + /*
  1968. + * a limit for rmdir/rename a dir
  1969. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  1970. + */
  1971. + err = vfs_statfs(&br->br_path, &kst);
  1972. + if (unlikely(err))
  1973. + goto out;
  1974. + err = -EINVAL;
  1975. + if (kst.f_namelen >= NAME_MAX)
  1976. + err = au_br_init_wh(sb, br, perm);
  1977. + else
  1978. + pr_err("%pd(%s), unsupported namelen %ld\n",
  1979. + au_br_dentry(br),
  1980. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  1981. +
  1982. +out:
  1983. + return err;
  1984. +}
  1985. +
  1986. +/* initialize a new branch */
  1987. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  1988. + struct au_opt_add *add)
  1989. +{
  1990. + int err;
  1991. +
  1992. + err = 0;
  1993. + memset(&br->br_xino, 0, sizeof(br->br_xino));
  1994. + mutex_init(&br->br_xino.xi_nondir_mtx);
  1995. + br->br_perm = add->perm;
  1996. + br->br_path = add->path; /* set first, path_get() later */
  1997. + spin_lock_init(&br->br_dykey_lock);
  1998. + memset(br->br_dykey, 0, sizeof(br->br_dykey));
  1999. + atomic_set(&br->br_count, 0);
  2000. + atomic_set(&br->br_xino_running, 0);
  2001. + br->br_id = au_new_br_id(sb);
  2002. + AuDebugOn(br->br_id < 0);
  2003. +
  2004. + if (au_br_writable(add->perm)) {
  2005. + err = au_wbr_init(br, sb, add->perm);
  2006. + if (unlikely(err))
  2007. + goto out_err;
  2008. + }
  2009. +
  2010. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2011. + err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
  2012. + au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
  2013. + if (unlikely(err)) {
  2014. + AuDebugOn(br->br_xino.xi_file);
  2015. + goto out_err;
  2016. + }
  2017. + }
  2018. +
  2019. + sysaufs_br_init(br);
  2020. + path_get(&br->br_path);
  2021. + goto out; /* success */
  2022. +
  2023. +out_err:
  2024. + memset(&br->br_path, 0, sizeof(br->br_path));
  2025. +out:
  2026. + return err;
  2027. +}
  2028. +
  2029. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2030. + struct au_branch *br, aufs_bindex_t bend,
  2031. + aufs_bindex_t amount)
  2032. +{
  2033. + struct au_branch **brp;
  2034. +
  2035. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2036. +
  2037. + brp = sbinfo->si_branch + bindex;
  2038. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2039. + *brp = br;
  2040. + sbinfo->si_bend++;
  2041. + if (unlikely(bend < 0))
  2042. + sbinfo->si_bend = 0;
  2043. +}
  2044. +
  2045. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2046. + aufs_bindex_t bend, aufs_bindex_t amount)
  2047. +{
  2048. + struct au_hdentry *hdp;
  2049. +
  2050. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2051. +
  2052. + hdp = dinfo->di_hdentry + bindex;
  2053. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2054. + au_h_dentry_init(hdp);
  2055. + dinfo->di_bend++;
  2056. + if (unlikely(bend < 0))
  2057. + dinfo->di_bstart = 0;
  2058. +}
  2059. +
  2060. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2061. + aufs_bindex_t bend, aufs_bindex_t amount)
  2062. +{
  2063. + struct au_hinode *hip;
  2064. +
  2065. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2066. +
  2067. + hip = iinfo->ii_hinode + bindex;
  2068. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2069. + hip->hi_inode = NULL;
  2070. + au_hn_init(hip);
  2071. + iinfo->ii_bend++;
  2072. + if (unlikely(bend < 0))
  2073. + iinfo->ii_bstart = 0;
  2074. +}
  2075. +
  2076. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2077. + aufs_bindex_t bindex)
  2078. +{
  2079. + struct dentry *root, *h_dentry;
  2080. + struct inode *root_inode;
  2081. + aufs_bindex_t bend, amount;
  2082. +
  2083. + root = sb->s_root;
  2084. + root_inode = root->d_inode;
  2085. + bend = au_sbend(sb);
  2086. + amount = bend + 1 - bindex;
  2087. + h_dentry = au_br_dentry(br);
  2088. + au_sbilist_lock();
  2089. + au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
  2090. + au_br_do_add_hdp(au_di(root), bindex, bend, amount);
  2091. + au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
  2092. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2093. + au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
  2094. + /*flags*/0);
  2095. + au_sbilist_unlock();
  2096. +}
  2097. +
  2098. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2099. +{
  2100. + int err;
  2101. + aufs_bindex_t bend, add_bindex;
  2102. + struct dentry *root, *h_dentry;
  2103. + struct inode *root_inode;
  2104. + struct au_branch *add_branch;
  2105. +
  2106. + root = sb->s_root;
  2107. + root_inode = root->d_inode;
  2108. + IMustLock(root_inode);
  2109. + err = test_add(sb, add, remount);
  2110. + if (unlikely(err < 0))
  2111. + goto out;
  2112. + if (err) {
  2113. + err = 0;
  2114. + goto out; /* success */
  2115. + }
  2116. +
  2117. + bend = au_sbend(sb);
  2118. + add_branch = au_br_alloc(sb, bend + 2, add->perm);
  2119. + err = PTR_ERR(add_branch);
  2120. + if (IS_ERR(add_branch))
  2121. + goto out;
  2122. +
  2123. + err = au_br_init(add_branch, sb, add);
  2124. + if (unlikely(err)) {
  2125. + au_br_do_free(add_branch);
  2126. + goto out;
  2127. + }
  2128. +
  2129. + add_bindex = add->bindex;
  2130. + if (!remount)
  2131. + au_br_do_add(sb, add_branch, add_bindex);
  2132. + else {
  2133. + sysaufs_brs_del(sb, add_bindex);
  2134. + au_br_do_add(sb, add_branch, add_bindex);
  2135. + sysaufs_brs_add(sb, add_bindex);
  2136. + }
  2137. +
  2138. + h_dentry = add->path.dentry;
  2139. + if (!add_bindex) {
  2140. + au_cpup_attr_all(root_inode, /*force*/1);
  2141. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2142. + } else
  2143. + au_add_nlink(root_inode, h_dentry->d_inode);
  2144. +
  2145. + /*
  2146. + * this test/set prevents aufs from handling unnecesary notify events
  2147. + * of xino files, in case of re-adding a writable branch which was
  2148. + * once detached from aufs.
  2149. + */
  2150. + if (au_xino_brid(sb) < 0
  2151. + && au_br_writable(add_branch->br_perm)
  2152. + && !au_test_fs_bad_xino(h_dentry->d_sb)
  2153. + && add_branch->br_xino.xi_file
  2154. + && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
  2155. + au_xino_brid_set(sb, add_branch->br_id);
  2156. +
  2157. +out:
  2158. + return err;
  2159. +}
  2160. +
  2161. +/* ---------------------------------------------------------------------- */
  2162. +
  2163. +static unsigned long long au_farray_cb(void *a,
  2164. + unsigned long long max __maybe_unused,
  2165. + void *arg)
  2166. +{
  2167. + unsigned long long n;
  2168. + struct file **p, *f;
  2169. + struct au_sphlhead *files;
  2170. + struct au_finfo *finfo;
  2171. + struct super_block *sb = arg;
  2172. +
  2173. + n = 0;
  2174. + p = a;
  2175. + files = &au_sbi(sb)->si_files;
  2176. + spin_lock(&files->spin);
  2177. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  2178. + f = finfo->fi_file;
  2179. + if (file_count(f)
  2180. + && !special_file(file_inode(f)->i_mode)) {
  2181. + get_file(f);
  2182. + *p++ = f;
  2183. + n++;
  2184. + AuDebugOn(n > max);
  2185. + }
  2186. + }
  2187. + spin_unlock(&files->spin);
  2188. +
  2189. + return n;
  2190. +}
  2191. +
  2192. +static struct file **au_farray_alloc(struct super_block *sb,
  2193. + unsigned long long *max)
  2194. +{
  2195. + *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
  2196. + return au_array_alloc(max, au_farray_cb, sb);
  2197. +}
  2198. +
  2199. +static void au_farray_free(struct file **a, unsigned long long max)
  2200. +{
  2201. + unsigned long long ull;
  2202. +
  2203. + for (ull = 0; ull < max; ull++)
  2204. + if (a[ull])
  2205. + fput(a[ull]);
  2206. + au_array_free(a);
  2207. +}
  2208. +
  2209. +/* ---------------------------------------------------------------------- */
  2210. +
  2211. +/*
  2212. + * delete a branch
  2213. + */
  2214. +
  2215. +/* to show the line number, do not make it inlined function */
  2216. +#define AuVerbose(do_info, fmt, ...) do { \
  2217. + if (do_info) \
  2218. + pr_info(fmt, ##__VA_ARGS__); \
  2219. +} while (0)
  2220. +
  2221. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
  2222. + aufs_bindex_t bend)
  2223. +{
  2224. + return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
  2225. +}
  2226. +
  2227. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
  2228. + aufs_bindex_t bend)
  2229. +{
  2230. + return au_test_ibusy(dentry->d_inode, bstart, bend);
  2231. +}
  2232. +
  2233. +/*
  2234. + * test if the branch is deletable or not.
  2235. + */
  2236. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2237. + unsigned int sigen, const unsigned int verbose)
  2238. +{
  2239. + int err, i, j, ndentry;
  2240. + aufs_bindex_t bstart, bend;
  2241. + struct au_dcsub_pages dpages;
  2242. + struct au_dpage *dpage;
  2243. + struct dentry *d;
  2244. +
  2245. + err = au_dpages_init(&dpages, GFP_NOFS);
  2246. + if (unlikely(err))
  2247. + goto out;
  2248. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2249. + if (unlikely(err))
  2250. + goto out_dpages;
  2251. +
  2252. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2253. + dpage = dpages.dpages + i;
  2254. + ndentry = dpage->ndentry;
  2255. + for (j = 0; !err && j < ndentry; j++) {
  2256. + d = dpage->dentries[j];
  2257. + AuDebugOn(au_dcount(d) <= 0);
  2258. + if (!au_digen_test(d, sigen)) {
  2259. + di_read_lock_child(d, AuLock_IR);
  2260. + if (unlikely(au_dbrange_test(d))) {
  2261. + di_read_unlock(d, AuLock_IR);
  2262. + continue;
  2263. + }
  2264. + } else {
  2265. + di_write_lock_child(d);
  2266. + if (unlikely(au_dbrange_test(d))) {
  2267. + di_write_unlock(d);
  2268. + continue;
  2269. + }
  2270. + err = au_reval_dpath(d, sigen);
  2271. + if (!err)
  2272. + di_downgrade_lock(d, AuLock_IR);
  2273. + else {
  2274. + di_write_unlock(d);
  2275. + break;
  2276. + }
  2277. + }
  2278. +
  2279. + /* AuDbgDentry(d); */
  2280. + bstart = au_dbstart(d);
  2281. + bend = au_dbend(d);
  2282. + if (bstart <= bindex
  2283. + && bindex <= bend
  2284. + && au_h_dptr(d, bindex)
  2285. + && au_test_dbusy(d, bstart, bend)) {
  2286. + err = -EBUSY;
  2287. + AuVerbose(verbose, "busy %pd\n", d);
  2288. + AuDbgDentry(d);
  2289. + }
  2290. + di_read_unlock(d, AuLock_IR);
  2291. + }
  2292. + }
  2293. +
  2294. +out_dpages:
  2295. + au_dpages_free(&dpages);
  2296. +out:
  2297. + return err;
  2298. +}
  2299. +
  2300. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2301. + unsigned int sigen, const unsigned int verbose)
  2302. +{
  2303. + int err;
  2304. + unsigned long long max, ull;
  2305. + struct inode *i, **array;
  2306. + aufs_bindex_t bstart, bend;
  2307. +
  2308. + array = au_iarray_alloc(sb, &max);
  2309. + err = PTR_ERR(array);
  2310. + if (IS_ERR(array))
  2311. + goto out;
  2312. +
  2313. + err = 0;
  2314. + AuDbg("b%d\n", bindex);
  2315. + for (ull = 0; !err && ull < max; ull++) {
  2316. + i = array[ull];
  2317. + if (unlikely(!i))
  2318. + break;
  2319. + if (i->i_ino == AUFS_ROOT_INO)
  2320. + continue;
  2321. +
  2322. + /* AuDbgInode(i); */
  2323. + if (au_iigen(i, NULL) == sigen)
  2324. + ii_read_lock_child(i);
  2325. + else {
  2326. + ii_write_lock_child(i);
  2327. + err = au_refresh_hinode_self(i);
  2328. + au_iigen_dec(i);
  2329. + if (!err)
  2330. + ii_downgrade_lock(i);
  2331. + else {
  2332. + ii_write_unlock(i);
  2333. + break;
  2334. + }
  2335. + }
  2336. +
  2337. + bstart = au_ibstart(i);
  2338. + bend = au_ibend(i);
  2339. + if (bstart <= bindex
  2340. + && bindex <= bend
  2341. + && au_h_iptr(i, bindex)
  2342. + && au_test_ibusy(i, bstart, bend)) {
  2343. + err = -EBUSY;
  2344. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2345. + AuDbgInode(i);
  2346. + }
  2347. + ii_read_unlock(i);
  2348. + }
  2349. + au_iarray_free(array, max);
  2350. +
  2351. +out:
  2352. + return err;
  2353. +}
  2354. +
  2355. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2356. + const unsigned int verbose)
  2357. +{
  2358. + int err;
  2359. + unsigned int sigen;
  2360. +
  2361. + sigen = au_sigen(root->d_sb);
  2362. + DiMustNoWaiters(root);
  2363. + IiMustNoWaiters(root->d_inode);
  2364. + di_write_unlock(root);
  2365. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2366. + if (!err)
  2367. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2368. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2369. +
  2370. + return err;
  2371. +}
  2372. +
  2373. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2374. + struct file **to_free, int *idx)
  2375. +{
  2376. + int err;
  2377. + unsigned char matched, root;
  2378. + aufs_bindex_t bindex, bend;
  2379. + struct au_fidir *fidir;
  2380. + struct au_hfile *hfile;
  2381. +
  2382. + err = 0;
  2383. + root = IS_ROOT(file->f_path.dentry);
  2384. + if (root) {
  2385. + get_file(file);
  2386. + to_free[*idx] = file;
  2387. + (*idx)++;
  2388. + goto out;
  2389. + }
  2390. +
  2391. + matched = 0;
  2392. + fidir = au_fi(file)->fi_hdir;
  2393. + AuDebugOn(!fidir);
  2394. + bend = au_fbend_dir(file);
  2395. + for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
  2396. + hfile = fidir->fd_hfile + bindex;
  2397. + if (!hfile->hf_file)
  2398. + continue;
  2399. +
  2400. + if (hfile->hf_br->br_id == br_id) {
  2401. + matched = 1;
  2402. + break;
  2403. + }
  2404. + }
  2405. + if (matched)
  2406. + err = -EBUSY;
  2407. +
  2408. +out:
  2409. + return err;
  2410. +}
  2411. +
  2412. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2413. + struct file **to_free, int opened)
  2414. +{
  2415. + int err, idx;
  2416. + unsigned long long ull, max;
  2417. + aufs_bindex_t bstart;
  2418. + struct file *file, **array;
  2419. + struct dentry *root;
  2420. + struct au_hfile *hfile;
  2421. +
  2422. + array = au_farray_alloc(sb, &max);
  2423. + err = PTR_ERR(array);
  2424. + if (IS_ERR(array))
  2425. + goto out;
  2426. +
  2427. + err = 0;
  2428. + idx = 0;
  2429. + root = sb->s_root;
  2430. + di_write_unlock(root);
  2431. + for (ull = 0; ull < max; ull++) {
  2432. + file = array[ull];
  2433. + if (unlikely(!file))
  2434. + break;
  2435. +
  2436. + /* AuDbg("%pD\n", file); */
  2437. + fi_read_lock(file);
  2438. + bstart = au_fbstart(file);
  2439. + if (!d_is_dir(file->f_path.dentry)) {
  2440. + hfile = &au_fi(file)->fi_htop;
  2441. + if (hfile->hf_br->br_id == br_id)
  2442. + err = -EBUSY;
  2443. + } else
  2444. + err = test_dir_busy(file, br_id, to_free, &idx);
  2445. + fi_read_unlock(file);
  2446. + if (unlikely(err))
  2447. + break;
  2448. + }
  2449. + di_write_lock_child(root);
  2450. + au_farray_free(array, max);
  2451. + AuDebugOn(idx > opened);
  2452. +
  2453. +out:
  2454. + return err;
  2455. +}
  2456. +
  2457. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2458. + aufs_bindex_t br_id)
  2459. +{
  2460. + unsigned long long ull;
  2461. + aufs_bindex_t bindex, bstart, bend, bfound;
  2462. + struct file *file;
  2463. + struct au_fidir *fidir;
  2464. + struct au_hfile *hfile;
  2465. +
  2466. + for (ull = 0; ull < opened; ull++) {
  2467. + file = to_free[ull];
  2468. + if (unlikely(!file))
  2469. + break;
  2470. +
  2471. + /* AuDbg("%pD\n", file); */
  2472. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2473. + bfound = -1;
  2474. + fidir = au_fi(file)->fi_hdir;
  2475. + AuDebugOn(!fidir);
  2476. + fi_write_lock(file);
  2477. + bstart = au_fbstart(file);
  2478. + bend = au_fbend_dir(file);
  2479. + for (bindex = bstart; bindex <= bend; bindex++) {
  2480. + hfile = fidir->fd_hfile + bindex;
  2481. + if (!hfile->hf_file)
  2482. + continue;
  2483. +
  2484. + if (hfile->hf_br->br_id == br_id) {
  2485. + bfound = bindex;
  2486. + break;
  2487. + }
  2488. + }
  2489. + AuDebugOn(bfound < 0);
  2490. + au_set_h_fptr(file, bfound, NULL);
  2491. + if (bfound == bstart) {
  2492. + for (bstart++; bstart <= bend; bstart++)
  2493. + if (au_hf_dir(file, bstart)) {
  2494. + au_set_fbstart(file, bstart);
  2495. + break;
  2496. + }
  2497. + }
  2498. + fi_write_unlock(file);
  2499. + }
  2500. +}
  2501. +
  2502. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2503. + const aufs_bindex_t bindex,
  2504. + const aufs_bindex_t bend)
  2505. +{
  2506. + struct au_branch **brp, **p;
  2507. +
  2508. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2509. +
  2510. + brp = sbinfo->si_branch + bindex;
  2511. + if (bindex < bend)
  2512. + memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
  2513. + sbinfo->si_branch[0 + bend] = NULL;
  2514. + sbinfo->si_bend--;
  2515. +
  2516. + p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
  2517. + if (p)
  2518. + sbinfo->si_branch = p;
  2519. + /* harmless error */
  2520. +}
  2521. +
  2522. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2523. + const aufs_bindex_t bend)
  2524. +{
  2525. + struct au_hdentry *hdp, *p;
  2526. +
  2527. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2528. +
  2529. + hdp = dinfo->di_hdentry;
  2530. + if (bindex < bend)
  2531. + memmove(hdp + bindex, hdp + bindex + 1,
  2532. + sizeof(*hdp) * (bend - bindex));
  2533. + hdp[0 + bend].hd_dentry = NULL;
  2534. + dinfo->di_bend--;
  2535. +
  2536. + p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
  2537. + if (p)
  2538. + dinfo->di_hdentry = p;
  2539. + /* harmless error */
  2540. +}
  2541. +
  2542. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2543. + const aufs_bindex_t bend)
  2544. +{
  2545. + struct au_hinode *hip, *p;
  2546. +
  2547. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2548. +
  2549. + hip = iinfo->ii_hinode + bindex;
  2550. + if (bindex < bend)
  2551. + memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
  2552. + iinfo->ii_hinode[0 + bend].hi_inode = NULL;
  2553. + au_hn_init(iinfo->ii_hinode + bend);
  2554. + iinfo->ii_bend--;
  2555. +
  2556. + p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
  2557. + if (p)
  2558. + iinfo->ii_hinode = p;
  2559. + /* harmless error */
  2560. +}
  2561. +
  2562. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2563. + struct au_branch *br)
  2564. +{
  2565. + aufs_bindex_t bend;
  2566. + struct au_sbinfo *sbinfo;
  2567. + struct dentry *root, *h_root;
  2568. + struct inode *inode, *h_inode;
  2569. + struct au_hinode *hinode;
  2570. +
  2571. + SiMustWriteLock(sb);
  2572. +
  2573. + root = sb->s_root;
  2574. + inode = root->d_inode;
  2575. + sbinfo = au_sbi(sb);
  2576. + bend = sbinfo->si_bend;
  2577. +
  2578. + h_root = au_h_dptr(root, bindex);
  2579. + hinode = au_hi(inode, bindex);
  2580. + h_inode = au_igrab(hinode->hi_inode);
  2581. + au_hiput(hinode);
  2582. +
  2583. + au_sbilist_lock();
  2584. + au_br_do_del_brp(sbinfo, bindex, bend);
  2585. + au_br_do_del_hdp(au_di(root), bindex, bend);
  2586. + au_br_do_del_hip(au_ii(inode), bindex, bend);
  2587. + au_sbilist_unlock();
  2588. +
  2589. + dput(h_root);
  2590. + iput(h_inode);
  2591. + au_br_do_free(br);
  2592. +}
  2593. +
  2594. +static unsigned long long empty_cb(void *array, unsigned long long max,
  2595. + void *arg)
  2596. +{
  2597. + return max;
  2598. +}
  2599. +
  2600. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2601. +{
  2602. + int err, rerr, i;
  2603. + unsigned long long opened;
  2604. + unsigned int mnt_flags;
  2605. + aufs_bindex_t bindex, bend, br_id;
  2606. + unsigned char do_wh, verbose;
  2607. + struct au_branch *br;
  2608. + struct au_wbr *wbr;
  2609. + struct dentry *root;
  2610. + struct file **to_free;
  2611. +
  2612. + err = 0;
  2613. + opened = 0;
  2614. + to_free = NULL;
  2615. + root = sb->s_root;
  2616. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2617. + if (bindex < 0) {
  2618. + if (remount)
  2619. + goto out; /* success */
  2620. + err = -ENOENT;
  2621. + pr_err("%s no such branch\n", del->pathname);
  2622. + goto out;
  2623. + }
  2624. + AuDbg("bindex b%d\n", bindex);
  2625. +
  2626. + err = -EBUSY;
  2627. + mnt_flags = au_mntflags(sb);
  2628. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2629. + bend = au_sbend(sb);
  2630. + if (unlikely(!bend)) {
  2631. + AuVerbose(verbose, "no more branches left\n");
  2632. + goto out;
  2633. + }
  2634. + br = au_sbr(sb, bindex);
  2635. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  2636. +
  2637. + br_id = br->br_id;
  2638. + opened = atomic_read(&br->br_count);
  2639. + if (unlikely(opened)) {
  2640. + to_free = au_array_alloc(&opened, empty_cb, NULL);
  2641. + err = PTR_ERR(to_free);
  2642. + if (IS_ERR(to_free))
  2643. + goto out;
  2644. +
  2645. + err = test_file_busy(sb, br_id, to_free, opened);
  2646. + if (unlikely(err)) {
  2647. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  2648. + goto out;
  2649. + }
  2650. + }
  2651. +
  2652. + wbr = br->br_wbr;
  2653. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  2654. + if (do_wh) {
  2655. + /* instead of WbrWhMustWriteLock(wbr) */
  2656. + SiMustWriteLock(sb);
  2657. + for (i = 0; i < AuBrWh_Last; i++) {
  2658. + dput(wbr->wbr_wh[i]);
  2659. + wbr->wbr_wh[i] = NULL;
  2660. + }
  2661. + }
  2662. +
  2663. + err = test_children_busy(root, bindex, verbose);
  2664. + if (unlikely(err)) {
  2665. + if (do_wh)
  2666. + goto out_wh;
  2667. + goto out;
  2668. + }
  2669. +
  2670. + err = 0;
  2671. + if (to_free) {
  2672. + /*
  2673. + * now we confirmed the branch is deletable.
  2674. + * let's free the remaining opened dirs on the branch.
  2675. + */
  2676. + di_write_unlock(root);
  2677. + br_del_file(to_free, opened, br_id);
  2678. + di_write_lock_child(root);
  2679. + }
  2680. +
  2681. + if (!remount)
  2682. + au_br_do_del(sb, bindex, br);
  2683. + else {
  2684. + sysaufs_brs_del(sb, bindex);
  2685. + au_br_do_del(sb, bindex, br);
  2686. + sysaufs_brs_add(sb, bindex);
  2687. + }
  2688. +
  2689. + if (!bindex) {
  2690. + au_cpup_attr_all(root->d_inode, /*force*/1);
  2691. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  2692. + } else
  2693. + au_sub_nlink(root->d_inode, del->h_path.dentry->d_inode);
  2694. + if (au_opt_test(mnt_flags, PLINK))
  2695. + au_plink_half_refresh(sb, br_id);
  2696. +
  2697. + if (au_xino_brid(sb) == br_id)
  2698. + au_xino_brid_set(sb, -1);
  2699. + goto out; /* success */
  2700. +
  2701. +out_wh:
  2702. + /* revert */
  2703. + rerr = au_br_init_wh(sb, br, br->br_perm);
  2704. + if (rerr)
  2705. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  2706. + del->pathname, rerr);
  2707. +out:
  2708. + if (to_free)
  2709. + au_farray_free(to_free, opened);
  2710. + return err;
  2711. +}
  2712. +
  2713. +/* ---------------------------------------------------------------------- */
  2714. +
  2715. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  2716. +{
  2717. + int err;
  2718. + aufs_bindex_t bstart, bend;
  2719. + struct aufs_ibusy ibusy;
  2720. + struct inode *inode, *h_inode;
  2721. +
  2722. + err = -EPERM;
  2723. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  2724. + goto out;
  2725. +
  2726. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  2727. + if (!err)
  2728. + err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
  2729. + if (unlikely(err)) {
  2730. + err = -EFAULT;
  2731. + AuTraceErr(err);
  2732. + goto out;
  2733. + }
  2734. +
  2735. + err = -EINVAL;
  2736. + si_read_lock(sb, AuLock_FLUSH);
  2737. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
  2738. + goto out_unlock;
  2739. +
  2740. + err = 0;
  2741. + ibusy.h_ino = 0; /* invalid */
  2742. + inode = ilookup(sb, ibusy.ino);
  2743. + if (!inode
  2744. + || inode->i_ino == AUFS_ROOT_INO
  2745. + || is_bad_inode(inode))
  2746. + goto out_unlock;
  2747. +
  2748. + ii_read_lock_child(inode);
  2749. + bstart = au_ibstart(inode);
  2750. + bend = au_ibend(inode);
  2751. + if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
  2752. + h_inode = au_h_iptr(inode, ibusy.bindex);
  2753. + if (h_inode && au_test_ibusy(inode, bstart, bend))
  2754. + ibusy.h_ino = h_inode->i_ino;
  2755. + }
  2756. + ii_read_unlock(inode);
  2757. + iput(inode);
  2758. +
  2759. +out_unlock:
  2760. + si_read_unlock(sb);
  2761. + if (!err) {
  2762. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  2763. + if (unlikely(err)) {
  2764. + err = -EFAULT;
  2765. + AuTraceErr(err);
  2766. + }
  2767. + }
  2768. +out:
  2769. + return err;
  2770. +}
  2771. +
  2772. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  2773. +{
  2774. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  2775. +}
  2776. +
  2777. +#ifdef CONFIG_COMPAT
  2778. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  2779. +{
  2780. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  2781. +}
  2782. +#endif
  2783. +
  2784. +/* ---------------------------------------------------------------------- */
  2785. +
  2786. +/*
  2787. + * change a branch permission
  2788. + */
  2789. +
  2790. +static void au_warn_ima(void)
  2791. +{
  2792. +#ifdef CONFIG_IMA
  2793. + /* since it doesn't support mark_files_ro() */
  2794. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  2795. +#endif
  2796. +}
  2797. +
  2798. +static int do_need_sigen_inc(int a, int b)
  2799. +{
  2800. + return au_br_whable(a) && !au_br_whable(b);
  2801. +}
  2802. +
  2803. +static int need_sigen_inc(int old, int new)
  2804. +{
  2805. + return do_need_sigen_inc(old, new)
  2806. + || do_need_sigen_inc(new, old);
  2807. +}
  2808. +
  2809. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  2810. +{
  2811. + int err, do_warn;
  2812. + unsigned int mnt_flags;
  2813. + unsigned long long ull, max;
  2814. + aufs_bindex_t br_id;
  2815. + unsigned char verbose, writer;
  2816. + struct file *file, *hf, **array;
  2817. + struct au_hfile *hfile;
  2818. +
  2819. + mnt_flags = au_mntflags(sb);
  2820. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2821. +
  2822. + array = au_farray_alloc(sb, &max);
  2823. + err = PTR_ERR(array);
  2824. + if (IS_ERR(array))
  2825. + goto out;
  2826. +
  2827. + do_warn = 0;
  2828. + br_id = au_sbr_id(sb, bindex);
  2829. + for (ull = 0; ull < max; ull++) {
  2830. + file = array[ull];
  2831. + if (unlikely(!file))
  2832. + break;
  2833. +
  2834. + /* AuDbg("%pD\n", file); */
  2835. + fi_read_lock(file);
  2836. + if (unlikely(au_test_mmapped(file))) {
  2837. + err = -EBUSY;
  2838. + AuVerbose(verbose, "mmapped %pD\n", file);
  2839. + AuDbgFile(file);
  2840. + FiMustNoWaiters(file);
  2841. + fi_read_unlock(file);
  2842. + goto out_array;
  2843. + }
  2844. +
  2845. + hfile = &au_fi(file)->fi_htop;
  2846. + hf = hfile->hf_file;
  2847. + if (!d_is_reg(file->f_path.dentry)
  2848. + || !(file->f_mode & FMODE_WRITE)
  2849. + || hfile->hf_br->br_id != br_id
  2850. + || !(hf->f_mode & FMODE_WRITE))
  2851. + array[ull] = NULL;
  2852. + else {
  2853. + do_warn = 1;
  2854. + get_file(file);
  2855. + }
  2856. +
  2857. + FiMustNoWaiters(file);
  2858. + fi_read_unlock(file);
  2859. + fput(file);
  2860. + }
  2861. +
  2862. + err = 0;
  2863. + if (do_warn)
  2864. + au_warn_ima();
  2865. +
  2866. + for (ull = 0; ull < max; ull++) {
  2867. + file = array[ull];
  2868. + if (!file)
  2869. + continue;
  2870. +
  2871. + /* todo: already flushed? */
  2872. + /*
  2873. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  2874. + * approach which resets f_mode and calls mnt_drop_write() and
  2875. + * file_release_write() for each file, because the branch
  2876. + * attribute in aufs world is totally different from the native
  2877. + * fs rw/ro mode.
  2878. + */
  2879. + /* fi_read_lock(file); */
  2880. + hfile = &au_fi(file)->fi_htop;
  2881. + hf = hfile->hf_file;
  2882. + /* fi_read_unlock(file); */
  2883. + spin_lock(&hf->f_lock);
  2884. + writer = !!(hf->f_mode & FMODE_WRITER);
  2885. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  2886. + spin_unlock(&hf->f_lock);
  2887. + if (writer) {
  2888. + put_write_access(file_inode(hf));
  2889. + __mnt_drop_write(hf->f_path.mnt);
  2890. + }
  2891. + }
  2892. +
  2893. +out_array:
  2894. + au_farray_free(array, max);
  2895. +out:
  2896. + AuTraceErr(err);
  2897. + return err;
  2898. +}
  2899. +
  2900. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  2901. + int *do_refresh)
  2902. +{
  2903. + int err, rerr;
  2904. + aufs_bindex_t bindex;
  2905. + struct dentry *root;
  2906. + struct au_branch *br;
  2907. + struct au_br_fhsm *bf;
  2908. +
  2909. + root = sb->s_root;
  2910. + bindex = au_find_dbindex(root, mod->h_root);
  2911. + if (bindex < 0) {
  2912. + if (remount)
  2913. + return 0; /* success */
  2914. + err = -ENOENT;
  2915. + pr_err("%s no such branch\n", mod->path);
  2916. + goto out;
  2917. + }
  2918. + AuDbg("bindex b%d\n", bindex);
  2919. +
  2920. + err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
  2921. + if (unlikely(err))
  2922. + goto out;
  2923. +
  2924. + br = au_sbr(sb, bindex);
  2925. + AuDebugOn(mod->h_root != au_br_dentry(br));
  2926. + if (br->br_perm == mod->perm)
  2927. + return 0; /* success */
  2928. +
  2929. + /* pre-allocate for non-fhsm --> fhsm */
  2930. + bf = NULL;
  2931. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  2932. + err = au_fhsm_br_alloc(br);
  2933. + if (unlikely(err))
  2934. + goto out;
  2935. + bf = br->br_fhsm;
  2936. + br->br_fhsm = NULL;
  2937. + }
  2938. +
  2939. + if (au_br_writable(br->br_perm)) {
  2940. + /* remove whiteout base */
  2941. + err = au_br_init_wh(sb, br, mod->perm);
  2942. + if (unlikely(err))
  2943. + goto out_bf;
  2944. +
  2945. + if (!au_br_writable(mod->perm)) {
  2946. + /* rw --> ro, file might be mmapped */
  2947. + DiMustNoWaiters(root);
  2948. + IiMustNoWaiters(root->d_inode);
  2949. + di_write_unlock(root);
  2950. + err = au_br_mod_files_ro(sb, bindex);
  2951. + /* aufs_write_lock() calls ..._child() */
  2952. + di_write_lock_child(root);
  2953. +
  2954. + if (unlikely(err)) {
  2955. + rerr = -ENOMEM;
  2956. + br->br_wbr = kmalloc(sizeof(*br->br_wbr),
  2957. + GFP_NOFS);
  2958. + if (br->br_wbr)
  2959. + rerr = au_wbr_init(br, sb, br->br_perm);
  2960. + if (unlikely(rerr)) {
  2961. + AuIOErr("nested error %d (%d)\n",
  2962. + rerr, err);
  2963. + br->br_perm = mod->perm;
  2964. + }
  2965. + }
  2966. + }
  2967. + } else if (au_br_writable(mod->perm)) {
  2968. + /* ro --> rw */
  2969. + err = -ENOMEM;
  2970. + br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
  2971. + if (br->br_wbr) {
  2972. + err = au_wbr_init(br, sb, mod->perm);
  2973. + if (unlikely(err)) {
  2974. + kfree(br->br_wbr);
  2975. + br->br_wbr = NULL;
  2976. + }
  2977. + }
  2978. + }
  2979. + if (unlikely(err))
  2980. + goto out_bf;
  2981. +
  2982. + if (au_br_fhsm(br->br_perm)) {
  2983. + if (!au_br_fhsm(mod->perm)) {
  2984. + /* fhsm --> non-fhsm */
  2985. + au_br_fhsm_fin(br->br_fhsm);
  2986. + kfree(br->br_fhsm);
  2987. + br->br_fhsm = NULL;
  2988. + }
  2989. + } else if (au_br_fhsm(mod->perm))
  2990. + /* non-fhsm --> fhsm */
  2991. + br->br_fhsm = bf;
  2992. +
  2993. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  2994. + br->br_perm = mod->perm;
  2995. + goto out; /* success */
  2996. +
  2997. +out_bf:
  2998. + kfree(bf);
  2999. +out:
  3000. + AuTraceErr(err);
  3001. + return err;
  3002. +}
  3003. +
  3004. +/* ---------------------------------------------------------------------- */
  3005. +
  3006. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3007. +{
  3008. + int err;
  3009. + struct kstatfs kstfs;
  3010. +
  3011. + err = vfs_statfs(&br->br_path, &kstfs);
  3012. + if (!err) {
  3013. + stfs->f_blocks = kstfs.f_blocks;
  3014. + stfs->f_bavail = kstfs.f_bavail;
  3015. + stfs->f_files = kstfs.f_files;
  3016. + stfs->f_ffree = kstfs.f_ffree;
  3017. + }
  3018. +
  3019. + return err;
  3020. +}
  3021. diff -Nur linux-4.0.4.orig/fs/aufs/branch.h linux-4.0.4/fs/aufs/branch.h
  3022. --- linux-4.0.4.orig/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
  3023. +++ linux-4.0.4/fs/aufs/branch.h 2015-05-30 22:11:31.000000000 +0200
  3024. @@ -0,0 +1,266 @@
  3025. +/*
  3026. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3027. + */
  3028. +
  3029. +/*
  3030. + * branch filesystems and xino for them
  3031. + */
  3032. +
  3033. +#ifndef __AUFS_BRANCH_H__
  3034. +#define __AUFS_BRANCH_H__
  3035. +
  3036. +#ifdef __KERNEL__
  3037. +
  3038. +#include <linux/mount.h>
  3039. +#include "dynop.h"
  3040. +#include "rwsem.h"
  3041. +#include "super.h"
  3042. +
  3043. +/* ---------------------------------------------------------------------- */
  3044. +
  3045. +/* a xino file */
  3046. +struct au_xino_file {
  3047. + struct file *xi_file;
  3048. + struct mutex xi_nondir_mtx;
  3049. +
  3050. + /* todo: make xino files an array to support huge inode number */
  3051. +
  3052. +#ifdef CONFIG_DEBUG_FS
  3053. + struct dentry *xi_dbgaufs;
  3054. +#endif
  3055. +};
  3056. +
  3057. +/* File-based Hierarchical Storage Management */
  3058. +struct au_br_fhsm {
  3059. +#ifdef CONFIG_AUFS_FHSM
  3060. + struct mutex bf_lock;
  3061. + unsigned long bf_jiffy;
  3062. + struct aufs_stfs bf_stfs;
  3063. + int bf_readable;
  3064. +#endif
  3065. +};
  3066. +
  3067. +/* members for writable branch only */
  3068. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3069. +struct au_wbr {
  3070. + struct au_rwsem wbr_wh_rwsem;
  3071. + struct dentry *wbr_wh[AuBrWh_Last];
  3072. + atomic_t wbr_wh_running;
  3073. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3074. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3075. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3076. +
  3077. + /* mfs mode */
  3078. + unsigned long long wbr_bytes;
  3079. +};
  3080. +
  3081. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3082. +#define AuBrDynOp (AuDyLast * 4)
  3083. +
  3084. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3085. +/* support for asynchronous destruction */
  3086. +struct au_br_hfsnotify {
  3087. + struct fsnotify_group *hfsn_group;
  3088. +};
  3089. +#endif
  3090. +
  3091. +/* sysfs entries */
  3092. +struct au_brsysfs {
  3093. + char name[16];
  3094. + struct attribute attr;
  3095. +};
  3096. +
  3097. +enum {
  3098. + AuBrSysfs_BR,
  3099. + AuBrSysfs_BRID,
  3100. + AuBrSysfs_Last
  3101. +};
  3102. +
  3103. +/* protected by superblock rwsem */
  3104. +struct au_branch {
  3105. + struct au_xino_file br_xino;
  3106. +
  3107. + aufs_bindex_t br_id;
  3108. +
  3109. + int br_perm;
  3110. + struct path br_path;
  3111. + spinlock_t br_dykey_lock;
  3112. + struct au_dykey *br_dykey[AuBrDynOp];
  3113. + atomic_t br_count;
  3114. +
  3115. + struct au_wbr *br_wbr;
  3116. + struct au_br_fhsm *br_fhsm;
  3117. +
  3118. + /* xino truncation */
  3119. + atomic_t br_xino_running;
  3120. +
  3121. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3122. + struct au_br_hfsnotify *br_hfsn;
  3123. +#endif
  3124. +
  3125. +#ifdef CONFIG_SYSFS
  3126. + /* entries under sysfs per mount-point */
  3127. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3128. +#endif
  3129. +};
  3130. +
  3131. +/* ---------------------------------------------------------------------- */
  3132. +
  3133. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3134. +{
  3135. + return br->br_path.mnt;
  3136. +}
  3137. +
  3138. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3139. +{
  3140. + return br->br_path.dentry;
  3141. +}
  3142. +
  3143. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3144. +{
  3145. + return au_br_mnt(br)->mnt_sb;
  3146. +}
  3147. +
  3148. +static inline int au_br_rdonly(struct au_branch *br)
  3149. +{
  3150. + return ((au_br_sb(br)->s_flags & MS_RDONLY)
  3151. + || !au_br_writable(br->br_perm))
  3152. + ? -EROFS : 0;
  3153. +}
  3154. +
  3155. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3156. +{
  3157. +#ifdef CONFIG_AUFS_HNOTIFY
  3158. + return !(brperm & AuBrPerm_RR);
  3159. +#else
  3160. + return 0;
  3161. +#endif
  3162. +}
  3163. +
  3164. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3165. +{
  3166. + int err, exec_flag;
  3167. +
  3168. + err = 0;
  3169. + exec_flag = oflag & __FMODE_EXEC;
  3170. + if (unlikely(exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC)))
  3171. + err = -EACCES;
  3172. +
  3173. + return err;
  3174. +}
  3175. +
  3176. +/* ---------------------------------------------------------------------- */
  3177. +
  3178. +/* branch.c */
  3179. +struct au_sbinfo;
  3180. +void au_br_free(struct au_sbinfo *sinfo);
  3181. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3182. +struct au_opt_add;
  3183. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3184. +struct au_opt_del;
  3185. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3186. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3187. +#ifdef CONFIG_COMPAT
  3188. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3189. +#endif
  3190. +struct au_opt_mod;
  3191. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3192. + int *do_refresh);
  3193. +struct aufs_stfs;
  3194. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3195. +
  3196. +/* xino.c */
  3197. +static const loff_t au_loff_max = LLONG_MAX;
  3198. +
  3199. +int au_xib_trunc(struct super_block *sb);
  3200. +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
  3201. + loff_t *pos);
  3202. +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
  3203. + loff_t *pos);
  3204. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
  3205. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
  3206. +ino_t au_xino_new_ino(struct super_block *sb);
  3207. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3208. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3209. + ino_t ino);
  3210. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3211. + ino_t *ino);
  3212. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3213. + struct file *base_file, int do_test);
  3214. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
  3215. +
  3216. +struct au_opt_xino;
  3217. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
  3218. +void au_xino_clr(struct super_block *sb);
  3219. +struct file *au_xino_def(struct super_block *sb);
  3220. +int au_xino_path(struct seq_file *seq, struct file *file);
  3221. +
  3222. +/* ---------------------------------------------------------------------- */
  3223. +
  3224. +/* Superblock to branch */
  3225. +static inline
  3226. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3227. +{
  3228. + return au_sbr(sb, bindex)->br_id;
  3229. +}
  3230. +
  3231. +static inline
  3232. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3233. +{
  3234. + return au_br_mnt(au_sbr(sb, bindex));
  3235. +}
  3236. +
  3237. +static inline
  3238. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3239. +{
  3240. + return au_br_sb(au_sbr(sb, bindex));
  3241. +}
  3242. +
  3243. +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
  3244. +{
  3245. + atomic_dec(&au_sbr(sb, bindex)->br_count);
  3246. +}
  3247. +
  3248. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3249. +{
  3250. + return au_sbr(sb, bindex)->br_perm;
  3251. +}
  3252. +
  3253. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3254. +{
  3255. + return au_br_whable(au_sbr_perm(sb, bindex));
  3256. +}
  3257. +
  3258. +/* ---------------------------------------------------------------------- */
  3259. +
  3260. +/*
  3261. + * wbr_wh_read_lock, wbr_wh_write_lock
  3262. + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
  3263. + */
  3264. +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
  3265. +
  3266. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
  3267. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
  3268. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
  3269. +
  3270. +/* ---------------------------------------------------------------------- */
  3271. +
  3272. +#ifdef CONFIG_AUFS_FHSM
  3273. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3274. +{
  3275. + mutex_init(&brfhsm->bf_lock);
  3276. + brfhsm->bf_jiffy = 0;
  3277. + brfhsm->bf_readable = 0;
  3278. +}
  3279. +
  3280. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3281. +{
  3282. + mutex_destroy(&brfhsm->bf_lock);
  3283. +}
  3284. +#else
  3285. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3286. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3287. +#endif
  3288. +
  3289. +#endif /* __KERNEL__ */
  3290. +#endif /* __AUFS_BRANCH_H__ */
  3291. diff -Nur linux-4.0.4.orig/fs/aufs/cpup.c linux-4.0.4/fs/aufs/cpup.c
  3292. --- linux-4.0.4.orig/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
  3293. +++ linux-4.0.4/fs/aufs/cpup.c 2015-05-30 22:11:31.000000000 +0200
  3294. @@ -0,0 +1,1295 @@
  3295. +/*
  3296. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3297. + */
  3298. +
  3299. +/*
  3300. + * copy-up functions, see wbr_policy.c for copy-down
  3301. + */
  3302. +
  3303. +#include <linux/fs_stack.h>
  3304. +#include <linux/mm.h>
  3305. +#include "aufs.h"
  3306. +
  3307. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3308. +{
  3309. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3310. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3311. +
  3312. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3313. +
  3314. + dst->i_flags |= iflags & ~mask;
  3315. + if (au_test_fs_notime(dst->i_sb))
  3316. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3317. +}
  3318. +
  3319. +void au_cpup_attr_timesizes(struct inode *inode)
  3320. +{
  3321. + struct inode *h_inode;
  3322. +
  3323. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  3324. + fsstack_copy_attr_times(inode, h_inode);
  3325. + fsstack_copy_inode_size(inode, h_inode);
  3326. +}
  3327. +
  3328. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3329. +{
  3330. + struct inode *h_inode;
  3331. + struct super_block *sb;
  3332. + aufs_bindex_t bindex, bend;
  3333. +
  3334. + sb = inode->i_sb;
  3335. + bindex = au_ibstart(inode);
  3336. + h_inode = au_h_iptr(inode, bindex);
  3337. + if (!force
  3338. + && !S_ISDIR(h_inode->i_mode)
  3339. + && au_opt_test(au_mntflags(sb), PLINK)
  3340. + && au_plink_test(inode))
  3341. + return;
  3342. +
  3343. + /*
  3344. + * 0 can happen in revalidating.
  3345. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3346. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3347. + * case.
  3348. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3349. + * the incorrect link count.
  3350. + */
  3351. + set_nlink(inode, h_inode->i_nlink);
  3352. +
  3353. + /*
  3354. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3355. + * it may includes whplink directory.
  3356. + */
  3357. + if (S_ISDIR(h_inode->i_mode)) {
  3358. + bend = au_ibend(inode);
  3359. + for (bindex++; bindex <= bend; bindex++) {
  3360. + h_inode = au_h_iptr(inode, bindex);
  3361. + if (h_inode)
  3362. + au_add_nlink(inode, h_inode);
  3363. + }
  3364. + }
  3365. +}
  3366. +
  3367. +void au_cpup_attr_changeable(struct inode *inode)
  3368. +{
  3369. + struct inode *h_inode;
  3370. +
  3371. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  3372. + inode->i_mode = h_inode->i_mode;
  3373. + inode->i_uid = h_inode->i_uid;
  3374. + inode->i_gid = h_inode->i_gid;
  3375. + au_cpup_attr_timesizes(inode);
  3376. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3377. +}
  3378. +
  3379. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3380. +{
  3381. + struct au_iinfo *iinfo = au_ii(inode);
  3382. +
  3383. + IiMustWriteLock(inode);
  3384. +
  3385. + iinfo->ii_higen = h_inode->i_generation;
  3386. + iinfo->ii_hsb1 = h_inode->i_sb;
  3387. +}
  3388. +
  3389. +void au_cpup_attr_all(struct inode *inode, int force)
  3390. +{
  3391. + struct inode *h_inode;
  3392. +
  3393. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  3394. + au_cpup_attr_changeable(inode);
  3395. + if (inode->i_nlink > 0)
  3396. + au_cpup_attr_nlink(inode, force);
  3397. + inode->i_rdev = h_inode->i_rdev;
  3398. + inode->i_blkbits = h_inode->i_blkbits;
  3399. + au_cpup_igen(inode, h_inode);
  3400. +}
  3401. +
  3402. +/* ---------------------------------------------------------------------- */
  3403. +
  3404. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3405. +
  3406. +/* keep the timestamps of the parent dir when cpup */
  3407. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3408. + struct path *h_path)
  3409. +{
  3410. + struct inode *h_inode;
  3411. +
  3412. + dt->dt_dentry = dentry;
  3413. + dt->dt_h_path = *h_path;
  3414. + h_inode = h_path->dentry->d_inode;
  3415. + dt->dt_atime = h_inode->i_atime;
  3416. + dt->dt_mtime = h_inode->i_mtime;
  3417. + /* smp_mb(); */
  3418. +}
  3419. +
  3420. +void au_dtime_revert(struct au_dtime *dt)
  3421. +{
  3422. + struct iattr attr;
  3423. + int err;
  3424. +
  3425. + attr.ia_atime = dt->dt_atime;
  3426. + attr.ia_mtime = dt->dt_mtime;
  3427. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3428. + | ATTR_ATIME | ATTR_ATIME_SET;
  3429. +
  3430. + /* no delegation since this is a directory */
  3431. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3432. + if (unlikely(err))
  3433. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3434. +}
  3435. +
  3436. +/* ---------------------------------------------------------------------- */
  3437. +
  3438. +/* internal use only */
  3439. +struct au_cpup_reg_attr {
  3440. + int valid;
  3441. + struct kstat st;
  3442. + unsigned int iflags; /* inode->i_flags */
  3443. +};
  3444. +
  3445. +static noinline_for_stack
  3446. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  3447. + struct au_cpup_reg_attr *h_src_attr)
  3448. +{
  3449. + int err, sbits, icex;
  3450. + unsigned int mnt_flags;
  3451. + unsigned char verbose;
  3452. + struct iattr ia;
  3453. + struct path h_path;
  3454. + struct inode *h_isrc, *h_idst;
  3455. + struct kstat *h_st;
  3456. + struct au_branch *br;
  3457. +
  3458. + h_path.dentry = au_h_dptr(dst, bindex);
  3459. + h_idst = h_path.dentry->d_inode;
  3460. + br = au_sbr(dst->d_sb, bindex);
  3461. + h_path.mnt = au_br_mnt(br);
  3462. + h_isrc = h_src->d_inode;
  3463. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  3464. + | ATTR_ATIME | ATTR_MTIME
  3465. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  3466. + if (h_src_attr && h_src_attr->valid) {
  3467. + h_st = &h_src_attr->st;
  3468. + ia.ia_uid = h_st->uid;
  3469. + ia.ia_gid = h_st->gid;
  3470. + ia.ia_atime = h_st->atime;
  3471. + ia.ia_mtime = h_st->mtime;
  3472. + if (h_idst->i_mode != h_st->mode
  3473. + && !S_ISLNK(h_idst->i_mode)) {
  3474. + ia.ia_valid |= ATTR_MODE;
  3475. + ia.ia_mode = h_st->mode;
  3476. + }
  3477. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  3478. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  3479. + } else {
  3480. + ia.ia_uid = h_isrc->i_uid;
  3481. + ia.ia_gid = h_isrc->i_gid;
  3482. + ia.ia_atime = h_isrc->i_atime;
  3483. + ia.ia_mtime = h_isrc->i_mtime;
  3484. + if (h_idst->i_mode != h_isrc->i_mode
  3485. + && !S_ISLNK(h_idst->i_mode)) {
  3486. + ia.ia_valid |= ATTR_MODE;
  3487. + ia.ia_mode = h_isrc->i_mode;
  3488. + }
  3489. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  3490. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  3491. + }
  3492. + /* no delegation since it is just created */
  3493. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3494. +
  3495. + /* is this nfs only? */
  3496. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  3497. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  3498. + ia.ia_mode = h_isrc->i_mode;
  3499. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3500. + }
  3501. +
  3502. + icex = br->br_perm & AuBrAttr_ICEX;
  3503. + if (!err) {
  3504. + mnt_flags = au_mntflags(dst->d_sb);
  3505. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3506. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  3507. + }
  3508. +
  3509. + return err;
  3510. +}
  3511. +
  3512. +/* ---------------------------------------------------------------------- */
  3513. +
  3514. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  3515. + char *buf, unsigned long blksize)
  3516. +{
  3517. + int err;
  3518. + size_t sz, rbytes, wbytes;
  3519. + unsigned char all_zero;
  3520. + char *p, *zp;
  3521. + struct mutex *h_mtx;
  3522. + /* reduce stack usage */
  3523. + struct iattr *ia;
  3524. +
  3525. + zp = page_address(ZERO_PAGE(0));
  3526. + if (unlikely(!zp))
  3527. + return -ENOMEM; /* possible? */
  3528. +
  3529. + err = 0;
  3530. + all_zero = 0;
  3531. + while (len) {
  3532. + AuDbg("len %lld\n", len);
  3533. + sz = blksize;
  3534. + if (len < blksize)
  3535. + sz = len;
  3536. +
  3537. + rbytes = 0;
  3538. + /* todo: signal_pending? */
  3539. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  3540. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  3541. + err = rbytes;
  3542. + }
  3543. + if (unlikely(err < 0))
  3544. + break;
  3545. +
  3546. + all_zero = 0;
  3547. + if (len >= rbytes && rbytes == blksize)
  3548. + all_zero = !memcmp(buf, zp, rbytes);
  3549. + if (!all_zero) {
  3550. + wbytes = rbytes;
  3551. + p = buf;
  3552. + while (wbytes) {
  3553. + size_t b;
  3554. +
  3555. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  3556. + err = b;
  3557. + /* todo: signal_pending? */
  3558. + if (unlikely(err == -EAGAIN || err == -EINTR))
  3559. + continue;
  3560. + if (unlikely(err < 0))
  3561. + break;
  3562. + wbytes -= b;
  3563. + p += b;
  3564. + }
  3565. + if (unlikely(err < 0))
  3566. + break;
  3567. + } else {
  3568. + loff_t res;
  3569. +
  3570. + AuLabel(hole);
  3571. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  3572. + err = res;
  3573. + if (unlikely(res < 0))
  3574. + break;
  3575. + }
  3576. + len -= rbytes;
  3577. + err = 0;
  3578. + }
  3579. +
  3580. + /* the last block may be a hole */
  3581. + if (!err && all_zero) {
  3582. + AuLabel(last hole);
  3583. +
  3584. + err = 1;
  3585. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  3586. + /* nfs requires this step to make last hole */
  3587. + /* is this only nfs? */
  3588. + do {
  3589. + /* todo: signal_pending? */
  3590. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  3591. + } while (err == -EAGAIN || err == -EINTR);
  3592. + if (err == 1)
  3593. + dst->f_pos--;
  3594. + }
  3595. +
  3596. + if (err == 1) {
  3597. + ia = (void *)buf;
  3598. + ia->ia_size = dst->f_pos;
  3599. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  3600. + ia->ia_file = dst;
  3601. + h_mtx = &file_inode(dst)->i_mutex;
  3602. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  3603. + /* no delegation since it is just created */
  3604. + err = vfsub_notify_change(&dst->f_path, ia,
  3605. + /*delegated*/NULL);
  3606. + mutex_unlock(h_mtx);
  3607. + }
  3608. + }
  3609. +
  3610. + return err;
  3611. +}
  3612. +
  3613. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  3614. +{
  3615. + int err;
  3616. + unsigned long blksize;
  3617. + unsigned char do_kfree;
  3618. + char *buf;
  3619. +
  3620. + err = -ENOMEM;
  3621. + blksize = dst->f_path.dentry->d_sb->s_blocksize;
  3622. + if (!blksize || PAGE_SIZE < blksize)
  3623. + blksize = PAGE_SIZE;
  3624. + AuDbg("blksize %lu\n", blksize);
  3625. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  3626. + if (do_kfree)
  3627. + buf = kmalloc(blksize, GFP_NOFS);
  3628. + else
  3629. + buf = (void *)__get_free_page(GFP_NOFS);
  3630. + if (unlikely(!buf))
  3631. + goto out;
  3632. +
  3633. + if (len > (1 << 22))
  3634. + AuDbg("copying a large file %lld\n", (long long)len);
  3635. +
  3636. + src->f_pos = 0;
  3637. + dst->f_pos = 0;
  3638. + err = au_do_copy_file(dst, src, len, buf, blksize);
  3639. + if (do_kfree)
  3640. + kfree(buf);
  3641. + else
  3642. + free_page((unsigned long)buf);
  3643. +
  3644. +out:
  3645. + return err;
  3646. +}
  3647. +
  3648. +/*
  3649. + * to support a sparse file which is opened with O_APPEND,
  3650. + * we need to close the file.
  3651. + */
  3652. +static int au_cp_regular(struct au_cp_generic *cpg)
  3653. +{
  3654. + int err, i;
  3655. + enum { SRC, DST };
  3656. + struct {
  3657. + aufs_bindex_t bindex;
  3658. + unsigned int flags;
  3659. + struct dentry *dentry;
  3660. + int force_wr;
  3661. + struct file *file;
  3662. + void *label;
  3663. + } *f, file[] = {
  3664. + {
  3665. + .bindex = cpg->bsrc,
  3666. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  3667. + .label = &&out
  3668. + },
  3669. + {
  3670. + .bindex = cpg->bdst,
  3671. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  3672. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  3673. + .label = &&out_src
  3674. + }
  3675. + };
  3676. + struct super_block *sb;
  3677. +
  3678. + /* bsrc branch can be ro/rw. */
  3679. + sb = cpg->dentry->d_sb;
  3680. + f = file;
  3681. + for (i = 0; i < 2; i++, f++) {
  3682. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  3683. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  3684. + /*file*/NULL, f->force_wr);
  3685. + err = PTR_ERR(f->file);
  3686. + if (IS_ERR(f->file))
  3687. + goto *f->label;
  3688. + }
  3689. +
  3690. + /* try stopping to update while we copyup */
  3691. + IMustLock(file[SRC].dentry->d_inode);
  3692. + err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
  3693. +
  3694. + fput(file[DST].file);
  3695. + au_sbr_put(sb, file[DST].bindex);
  3696. +
  3697. +out_src:
  3698. + fput(file[SRC].file);
  3699. + au_sbr_put(sb, file[SRC].bindex);
  3700. +out:
  3701. + return err;
  3702. +}
  3703. +
  3704. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  3705. + struct au_cpup_reg_attr *h_src_attr)
  3706. +{
  3707. + int err, rerr;
  3708. + loff_t l;
  3709. + struct path h_path;
  3710. + struct inode *h_src_inode, *h_dst_inode;
  3711. +
  3712. + err = 0;
  3713. + h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
  3714. + l = i_size_read(h_src_inode);
  3715. + if (cpg->len == -1 || l < cpg->len)
  3716. + cpg->len = l;
  3717. + if (cpg->len) {
  3718. + /* try stopping to update while we are referencing */
  3719. + mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
  3720. + au_pin_hdir_unlock(cpg->pin);
  3721. +
  3722. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  3723. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  3724. + h_src_attr->iflags = h_src_inode->i_flags;
  3725. + err = vfs_getattr(&h_path, &h_src_attr->st);
  3726. + if (unlikely(err)) {
  3727. + mutex_unlock(&h_src_inode->i_mutex);
  3728. + goto out;
  3729. + }
  3730. + h_src_attr->valid = 1;
  3731. + err = au_cp_regular(cpg);
  3732. + mutex_unlock(&h_src_inode->i_mutex);
  3733. + rerr = au_pin_hdir_relock(cpg->pin);
  3734. + if (!err && rerr)
  3735. + err = rerr;
  3736. + }
  3737. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  3738. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  3739. + h_dst_inode = h_path.dentry->d_inode;
  3740. + spin_lock(&h_dst_inode->i_lock);
  3741. + h_dst_inode->i_state |= I_LINKABLE;
  3742. + spin_unlock(&h_dst_inode->i_lock);
  3743. + }
  3744. +
  3745. +out:
  3746. + return err;
  3747. +}
  3748. +
  3749. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  3750. + struct inode *h_dir)
  3751. +{
  3752. + int err, symlen;
  3753. + mm_segment_t old_fs;
  3754. + union {
  3755. + char *k;
  3756. + char __user *u;
  3757. + } sym;
  3758. +
  3759. + err = -ENOSYS;
  3760. + if (unlikely(!h_src->d_inode->i_op->readlink))
  3761. + goto out;
  3762. +
  3763. + err = -ENOMEM;
  3764. + sym.k = (void *)__get_free_page(GFP_NOFS);
  3765. + if (unlikely(!sym.k))
  3766. + goto out;
  3767. +
  3768. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  3769. + old_fs = get_fs();
  3770. + set_fs(KERNEL_DS);
  3771. + symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
  3772. + err = symlen;
  3773. + set_fs(old_fs);
  3774. +
  3775. + if (symlen > 0) {
  3776. + sym.k[symlen] = 0;
  3777. + err = vfsub_symlink(h_dir, h_path, sym.k);
  3778. + }
  3779. + free_page((unsigned long)sym.k);
  3780. +
  3781. +out:
  3782. + return err;
  3783. +}
  3784. +
  3785. +static noinline_for_stack
  3786. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  3787. + struct au_cpup_reg_attr *h_src_attr)
  3788. +{
  3789. + int err;
  3790. + umode_t mode;
  3791. + unsigned int mnt_flags;
  3792. + unsigned char isdir, isreg, force;
  3793. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  3794. + struct au_dtime dt;
  3795. + struct path h_path;
  3796. + struct dentry *h_src, *h_dst, *h_parent;
  3797. + struct inode *h_inode, *h_dir;
  3798. + struct super_block *sb;
  3799. +
  3800. + /* bsrc branch can be ro/rw. */
  3801. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  3802. + h_inode = h_src->d_inode;
  3803. + AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
  3804. +
  3805. + /* try stopping to be referenced while we are creating */
  3806. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  3807. + if (au_ftest_cpup(cpg->flags, RENAME))
  3808. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  3809. + AUFS_WH_PFX_LEN));
  3810. + h_parent = h_dst->d_parent; /* dir inode is locked */
  3811. + h_dir = h_parent->d_inode;
  3812. + IMustLock(h_dir);
  3813. + AuDebugOn(h_parent != h_dst->d_parent);
  3814. +
  3815. + sb = cpg->dentry->d_sb;
  3816. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  3817. + if (do_dt) {
  3818. + h_path.dentry = h_parent;
  3819. + au_dtime_store(&dt, dst_parent, &h_path);
  3820. + }
  3821. + h_path.dentry = h_dst;
  3822. +
  3823. + isreg = 0;
  3824. + isdir = 0;
  3825. + mode = h_inode->i_mode;
  3826. + switch (mode & S_IFMT) {
  3827. + case S_IFREG:
  3828. + isreg = 1;
  3829. + err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
  3830. + /*want_excl*/true);
  3831. + if (!err)
  3832. + err = au_do_cpup_regular(cpg, h_src_attr);
  3833. + break;
  3834. + case S_IFDIR:
  3835. + isdir = 1;
  3836. + err = vfsub_mkdir(h_dir, &h_path, mode);
  3837. + if (!err) {
  3838. + /*
  3839. + * strange behaviour from the users view,
  3840. + * particularry setattr case
  3841. + */
  3842. + if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
  3843. + au_cpup_attr_nlink(dst_parent->d_inode,
  3844. + /*force*/1);
  3845. + au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
  3846. + }
  3847. + break;
  3848. + case S_IFLNK:
  3849. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  3850. + break;
  3851. + case S_IFCHR:
  3852. + case S_IFBLK:
  3853. + AuDebugOn(!capable(CAP_MKNOD));
  3854. + /*FALLTHROUGH*/
  3855. + case S_IFIFO:
  3856. + case S_IFSOCK:
  3857. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  3858. + break;
  3859. + default:
  3860. + AuIOErr("Unknown inode type 0%o\n", mode);
  3861. + err = -EIO;
  3862. + }
  3863. +
  3864. + mnt_flags = au_mntflags(sb);
  3865. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  3866. + && !isdir
  3867. + && au_opt_test(mnt_flags, XINO)
  3868. + && (h_inode->i_nlink == 1
  3869. + || (h_inode->i_state & I_LINKABLE))
  3870. + /* todo: unnecessary? */
  3871. + /* && cpg->dentry->d_inode->i_nlink == 1 */
  3872. + && cpg->bdst < cpg->bsrc
  3873. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  3874. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  3875. + /* ignore this error */
  3876. +
  3877. + if (!err) {
  3878. + force = 0;
  3879. + if (isreg) {
  3880. + force = !!cpg->len;
  3881. + if (cpg->len == -1)
  3882. + force = !!i_size_read(h_inode);
  3883. + }
  3884. + au_fhsm_wrote(sb, cpg->bdst, force);
  3885. + }
  3886. +
  3887. + if (do_dt)
  3888. + au_dtime_revert(&dt);
  3889. + return err;
  3890. +}
  3891. +
  3892. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  3893. +{
  3894. + int err;
  3895. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  3896. + struct inode *h_dir;
  3897. + aufs_bindex_t bdst;
  3898. +
  3899. + dentry = cpg->dentry;
  3900. + bdst = cpg->bdst;
  3901. + h_dentry = au_h_dptr(dentry, bdst);
  3902. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  3903. + dget(h_dentry);
  3904. + au_set_h_dptr(dentry, bdst, NULL);
  3905. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  3906. + if (!err)
  3907. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  3908. + au_set_h_dptr(dentry, bdst, h_dentry);
  3909. + } else {
  3910. + err = 0;
  3911. + parent = dget_parent(dentry);
  3912. + h_parent = au_h_dptr(parent, bdst);
  3913. + dput(parent);
  3914. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  3915. + if (IS_ERR(h_path->dentry))
  3916. + err = PTR_ERR(h_path->dentry);
  3917. + }
  3918. + if (unlikely(err))
  3919. + goto out;
  3920. +
  3921. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  3922. + h_dir = h_parent->d_inode;
  3923. + IMustLock(h_dir);
  3924. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  3925. + /* no delegation since it is just created */
  3926. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
  3927. + dput(h_path->dentry);
  3928. +
  3929. +out:
  3930. + return err;
  3931. +}
  3932. +
  3933. +/*
  3934. + * copyup the @dentry from @bsrc to @bdst.
  3935. + * the caller must set the both of lower dentries.
  3936. + * @len is for truncating when it is -1 copyup the entire file.
  3937. + * in link/rename cases, @dst_parent may be different from the real one.
  3938. + * basic->bsrc can be larger than basic->bdst.
  3939. + */
  3940. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  3941. +{
  3942. + int err, rerr;
  3943. + aufs_bindex_t old_ibstart;
  3944. + unsigned char isdir, plink;
  3945. + struct dentry *h_src, *h_dst, *h_parent;
  3946. + struct inode *dst_inode, *h_dir, *inode, *delegated;
  3947. + struct super_block *sb;
  3948. + struct au_branch *br;
  3949. + /* to reuduce stack size */
  3950. + struct {
  3951. + struct au_dtime dt;
  3952. + struct path h_path;
  3953. + struct au_cpup_reg_attr h_src_attr;
  3954. + } *a;
  3955. +
  3956. + err = -ENOMEM;
  3957. + a = kmalloc(sizeof(*a), GFP_NOFS);
  3958. + if (unlikely(!a))
  3959. + goto out;
  3960. + a->h_src_attr.valid = 0;
  3961. +
  3962. + sb = cpg->dentry->d_sb;
  3963. + br = au_sbr(sb, cpg->bdst);
  3964. + a->h_path.mnt = au_br_mnt(br);
  3965. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  3966. + h_parent = h_dst->d_parent; /* dir inode is locked */
  3967. + h_dir = h_parent->d_inode;
  3968. + IMustLock(h_dir);
  3969. +
  3970. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  3971. + inode = cpg->dentry->d_inode;
  3972. +
  3973. + if (!dst_parent)
  3974. + dst_parent = dget_parent(cpg->dentry);
  3975. + else
  3976. + dget(dst_parent);
  3977. +
  3978. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  3979. + dst_inode = au_h_iptr(inode, cpg->bdst);
  3980. + if (dst_inode) {
  3981. + if (unlikely(!plink)) {
  3982. + err = -EIO;
  3983. + AuIOErr("hi%lu(i%lu) exists on b%d "
  3984. + "but plink is disabled\n",
  3985. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  3986. + goto out_parent;
  3987. + }
  3988. +
  3989. + if (dst_inode->i_nlink) {
  3990. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  3991. +
  3992. + h_src = au_plink_lkup(inode, cpg->bdst);
  3993. + err = PTR_ERR(h_src);
  3994. + if (IS_ERR(h_src))
  3995. + goto out_parent;
  3996. + if (unlikely(!h_src->d_inode)) {
  3997. + err = -EIO;
  3998. + AuIOErr("i%lu exists on a upper branch "
  3999. + "but not pseudo-linked\n",
  4000. + inode->i_ino);
  4001. + dput(h_src);
  4002. + goto out_parent;
  4003. + }
  4004. +
  4005. + if (do_dt) {
  4006. + a->h_path.dentry = h_parent;
  4007. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4008. + }
  4009. +
  4010. + a->h_path.dentry = h_dst;
  4011. + delegated = NULL;
  4012. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4013. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4014. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4015. + if (do_dt)
  4016. + au_dtime_revert(&a->dt);
  4017. + if (unlikely(err == -EWOULDBLOCK)) {
  4018. + pr_warn("cannot retry for NFSv4 delegation"
  4019. + " for an internal link\n");
  4020. + iput(delegated);
  4021. + }
  4022. + dput(h_src);
  4023. + goto out_parent;
  4024. + } else
  4025. + /* todo: cpup_wh_file? */
  4026. + /* udba work */
  4027. + au_update_ibrange(inode, /*do_put_zero*/1);
  4028. + }
  4029. +
  4030. + isdir = S_ISDIR(inode->i_mode);
  4031. + old_ibstart = au_ibstart(inode);
  4032. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4033. + if (unlikely(err))
  4034. + goto out_rev;
  4035. + dst_inode = h_dst->d_inode;
  4036. + mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
  4037. + /* todo: necessary? */
  4038. + /* au_pin_hdir_unlock(cpg->pin); */
  4039. +
  4040. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  4041. + if (unlikely(err)) {
  4042. + /* todo: necessary? */
  4043. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4044. + mutex_unlock(&dst_inode->i_mutex);
  4045. + goto out_rev;
  4046. + }
  4047. +
  4048. + if (cpg->bdst < old_ibstart) {
  4049. + if (S_ISREG(inode->i_mode)) {
  4050. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4051. + if (unlikely(err)) {
  4052. + /* ignore an error */
  4053. + /* au_pin_hdir_relock(cpg->pin); */
  4054. + mutex_unlock(&dst_inode->i_mutex);
  4055. + goto out_rev;
  4056. + }
  4057. + }
  4058. + au_set_ibstart(inode, cpg->bdst);
  4059. + } else
  4060. + au_set_ibend(inode, cpg->bdst);
  4061. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4062. + au_hi_flags(inode, isdir));
  4063. +
  4064. + /* todo: necessary? */
  4065. + /* err = au_pin_hdir_relock(cpg->pin); */
  4066. + mutex_unlock(&dst_inode->i_mutex);
  4067. + if (unlikely(err))
  4068. + goto out_rev;
  4069. +
  4070. + if (!isdir
  4071. + && (h_src->d_inode->i_nlink > 1
  4072. + || h_src->d_inode->i_state & I_LINKABLE)
  4073. + && plink)
  4074. + au_plink_append(inode, cpg->bdst, h_dst);
  4075. +
  4076. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4077. + a->h_path.dentry = h_dst;
  4078. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4079. + }
  4080. + if (!err)
  4081. + goto out_parent; /* success */
  4082. +
  4083. + /* revert */
  4084. +out_rev:
  4085. + a->h_path.dentry = h_parent;
  4086. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4087. + a->h_path.dentry = h_dst;
  4088. + rerr = 0;
  4089. + if (h_dst->d_inode) {
  4090. + if (!isdir) {
  4091. + /* no delegation since it is just created */
  4092. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4093. + /*delegated*/NULL, /*force*/0);
  4094. + } else
  4095. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4096. + }
  4097. + au_dtime_revert(&a->dt);
  4098. + if (rerr) {
  4099. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4100. + err = -EIO;
  4101. + }
  4102. +out_parent:
  4103. + dput(dst_parent);
  4104. + kfree(a);
  4105. +out:
  4106. + return err;
  4107. +}
  4108. +
  4109. +#if 0 /* reserved */
  4110. +struct au_cpup_single_args {
  4111. + int *errp;
  4112. + struct au_cp_generic *cpg;
  4113. + struct dentry *dst_parent;
  4114. +};
  4115. +
  4116. +static void au_call_cpup_single(void *args)
  4117. +{
  4118. + struct au_cpup_single_args *a = args;
  4119. +
  4120. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4121. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4122. + au_pin_hdir_release(a->cpg->pin);
  4123. +}
  4124. +#endif
  4125. +
  4126. +/*
  4127. + * prevent SIGXFSZ in copy-up.
  4128. + * testing CAP_MKNOD is for generic fs,
  4129. + * but CAP_FSETID is for xfs only, currently.
  4130. + */
  4131. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4132. +{
  4133. + int do_sio;
  4134. + struct super_block *sb;
  4135. + struct inode *h_dir;
  4136. +
  4137. + do_sio = 0;
  4138. + sb = au_pinned_parent(pin)->d_sb;
  4139. + if (!au_wkq_test()
  4140. + && (!au_sbi(sb)->si_plink_maint_pid
  4141. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4142. + switch (mode & S_IFMT) {
  4143. + case S_IFREG:
  4144. + /* no condition about RLIMIT_FSIZE and the file size */
  4145. + do_sio = 1;
  4146. + break;
  4147. + case S_IFCHR:
  4148. + case S_IFBLK:
  4149. + do_sio = !capable(CAP_MKNOD);
  4150. + break;
  4151. + }
  4152. + if (!do_sio)
  4153. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4154. + && !capable(CAP_FSETID));
  4155. + /* this workaround may be removed in the future */
  4156. + if (!do_sio) {
  4157. + h_dir = au_pinned_h_dir(pin);
  4158. + do_sio = h_dir->i_mode & S_ISVTX;
  4159. + }
  4160. + }
  4161. +
  4162. + return do_sio;
  4163. +}
  4164. +
  4165. +#if 0 /* reserved */
  4166. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4167. +{
  4168. + int err, wkq_err;
  4169. + struct dentry *h_dentry;
  4170. +
  4171. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4172. + if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
  4173. + err = au_cpup_single(cpg, dst_parent);
  4174. + else {
  4175. + struct au_cpup_single_args args = {
  4176. + .errp = &err,
  4177. + .cpg = cpg,
  4178. + .dst_parent = dst_parent
  4179. + };
  4180. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4181. + if (unlikely(wkq_err))
  4182. + err = wkq_err;
  4183. + }
  4184. +
  4185. + return err;
  4186. +}
  4187. +#endif
  4188. +
  4189. +/*
  4190. + * copyup the @dentry from the first active lower branch to @bdst,
  4191. + * using au_cpup_single().
  4192. + */
  4193. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4194. +{
  4195. + int err;
  4196. + unsigned int flags_orig;
  4197. + struct dentry *dentry;
  4198. +
  4199. + AuDebugOn(cpg->bsrc < 0);
  4200. +
  4201. + dentry = cpg->dentry;
  4202. + DiMustWriteLock(dentry);
  4203. +
  4204. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4205. + if (!err) {
  4206. + flags_orig = cpg->flags;
  4207. + au_fset_cpup(cpg->flags, RENAME);
  4208. + err = au_cpup_single(cpg, NULL);
  4209. + cpg->flags = flags_orig;
  4210. + if (!err)
  4211. + return 0; /* success */
  4212. +
  4213. + /* revert */
  4214. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4215. + au_set_dbstart(dentry, cpg->bsrc);
  4216. + }
  4217. +
  4218. + return err;
  4219. +}
  4220. +
  4221. +struct au_cpup_simple_args {
  4222. + int *errp;
  4223. + struct au_cp_generic *cpg;
  4224. +};
  4225. +
  4226. +static void au_call_cpup_simple(void *args)
  4227. +{
  4228. + struct au_cpup_simple_args *a = args;
  4229. +
  4230. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4231. + *a->errp = au_cpup_simple(a->cpg);
  4232. + au_pin_hdir_release(a->cpg->pin);
  4233. +}
  4234. +
  4235. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4236. +{
  4237. + int err, wkq_err;
  4238. + struct dentry *dentry, *parent;
  4239. + struct file *h_file;
  4240. + struct inode *h_dir;
  4241. +
  4242. + dentry = cpg->dentry;
  4243. + h_file = NULL;
  4244. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4245. + AuDebugOn(cpg->bsrc < 0);
  4246. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4247. + err = PTR_ERR(h_file);
  4248. + if (IS_ERR(h_file))
  4249. + goto out;
  4250. + }
  4251. +
  4252. + parent = dget_parent(dentry);
  4253. + h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
  4254. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  4255. + && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
  4256. + err = au_cpup_simple(cpg);
  4257. + else {
  4258. + struct au_cpup_simple_args args = {
  4259. + .errp = &err,
  4260. + .cpg = cpg
  4261. + };
  4262. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4263. + if (unlikely(wkq_err))
  4264. + err = wkq_err;
  4265. + }
  4266. +
  4267. + dput(parent);
  4268. + if (h_file)
  4269. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4270. +
  4271. +out:
  4272. + return err;
  4273. +}
  4274. +
  4275. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4276. +{
  4277. + aufs_bindex_t bsrc, bend;
  4278. + struct dentry *dentry, *h_dentry;
  4279. +
  4280. + if (cpg->bsrc < 0) {
  4281. + dentry = cpg->dentry;
  4282. + bend = au_dbend(dentry);
  4283. + for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
  4284. + h_dentry = au_h_dptr(dentry, bsrc);
  4285. + if (h_dentry) {
  4286. + AuDebugOn(!h_dentry->d_inode);
  4287. + break;
  4288. + }
  4289. + }
  4290. + AuDebugOn(bsrc > bend);
  4291. + cpg->bsrc = bsrc;
  4292. + }
  4293. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4294. + return au_do_sio_cpup_simple(cpg);
  4295. +}
  4296. +
  4297. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4298. +{
  4299. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4300. + return au_do_sio_cpup_simple(cpg);
  4301. +}
  4302. +
  4303. +/* ---------------------------------------------------------------------- */
  4304. +
  4305. +/*
  4306. + * copyup the deleted file for writing.
  4307. + */
  4308. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  4309. + struct file *file)
  4310. +{
  4311. + int err;
  4312. + unsigned int flags_orig;
  4313. + aufs_bindex_t bsrc_orig;
  4314. + struct dentry *h_d_dst, *h_d_start;
  4315. + struct au_dinfo *dinfo;
  4316. + struct au_hdentry *hdp;
  4317. +
  4318. + dinfo = au_di(cpg->dentry);
  4319. + AuRwMustWriteLock(&dinfo->di_rwsem);
  4320. +
  4321. + bsrc_orig = cpg->bsrc;
  4322. + cpg->bsrc = dinfo->di_bstart;
  4323. + hdp = dinfo->di_hdentry;
  4324. + h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
  4325. + dinfo->di_bstart = cpg->bdst;
  4326. + hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
  4327. + h_d_start = NULL;
  4328. + if (file) {
  4329. + h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
  4330. + hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
  4331. + }
  4332. + flags_orig = cpg->flags;
  4333. + cpg->flags = !AuCpup_DTIME;
  4334. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  4335. + cpg->flags = flags_orig;
  4336. + if (file) {
  4337. + if (!err)
  4338. + err = au_reopen_nondir(file);
  4339. + hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
  4340. + }
  4341. + hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
  4342. + dinfo->di_bstart = cpg->bsrc;
  4343. + cpg->bsrc = bsrc_orig;
  4344. +
  4345. + return err;
  4346. +}
  4347. +
  4348. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4349. +{
  4350. + int err;
  4351. + aufs_bindex_t bdst;
  4352. + struct au_dtime dt;
  4353. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  4354. + struct au_branch *br;
  4355. + struct path h_path;
  4356. +
  4357. + dentry = cpg->dentry;
  4358. + bdst = cpg->bdst;
  4359. + br = au_sbr(dentry->d_sb, bdst);
  4360. + parent = dget_parent(dentry);
  4361. + h_parent = au_h_dptr(parent, bdst);
  4362. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  4363. + err = PTR_ERR(wh_dentry);
  4364. + if (IS_ERR(wh_dentry))
  4365. + goto out;
  4366. +
  4367. + h_path.dentry = h_parent;
  4368. + h_path.mnt = au_br_mnt(br);
  4369. + au_dtime_store(&dt, parent, &h_path);
  4370. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  4371. + if (unlikely(err))
  4372. + goto out_wh;
  4373. +
  4374. + dget(wh_dentry);
  4375. + h_path.dentry = wh_dentry;
  4376. + if (!d_is_dir(wh_dentry)) {
  4377. + /* no delegation since it is just created */
  4378. + err = vfsub_unlink(h_parent->d_inode, &h_path,
  4379. + /*delegated*/NULL, /*force*/0);
  4380. + } else
  4381. + err = vfsub_rmdir(h_parent->d_inode, &h_path);
  4382. + if (unlikely(err)) {
  4383. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  4384. + wh_dentry, err);
  4385. + err = -EIO;
  4386. + }
  4387. + au_dtime_revert(&dt);
  4388. + au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
  4389. +
  4390. +out_wh:
  4391. + dput(wh_dentry);
  4392. +out:
  4393. + dput(parent);
  4394. + return err;
  4395. +}
  4396. +
  4397. +struct au_cpup_wh_args {
  4398. + int *errp;
  4399. + struct au_cp_generic *cpg;
  4400. + struct file *file;
  4401. +};
  4402. +
  4403. +static void au_call_cpup_wh(void *args)
  4404. +{
  4405. + struct au_cpup_wh_args *a = args;
  4406. +
  4407. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4408. + *a->errp = au_cpup_wh(a->cpg, a->file);
  4409. + au_pin_hdir_release(a->cpg->pin);
  4410. +}
  4411. +
  4412. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4413. +{
  4414. + int err, wkq_err;
  4415. + aufs_bindex_t bdst;
  4416. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  4417. + struct inode *dir, *h_dir, *h_tmpdir;
  4418. + struct au_wbr *wbr;
  4419. + struct au_pin wh_pin, *pin_orig;
  4420. +
  4421. + dentry = cpg->dentry;
  4422. + bdst = cpg->bdst;
  4423. + parent = dget_parent(dentry);
  4424. + dir = parent->d_inode;
  4425. + h_orph = NULL;
  4426. + h_parent = NULL;
  4427. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  4428. + h_tmpdir = h_dir;
  4429. + pin_orig = NULL;
  4430. + if (!h_dir->i_nlink) {
  4431. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  4432. + h_orph = wbr->wbr_orph;
  4433. +
  4434. + h_parent = dget(au_h_dptr(parent, bdst));
  4435. + au_set_h_dptr(parent, bdst, dget(h_orph));
  4436. + h_tmpdir = h_orph->d_inode;
  4437. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  4438. +
  4439. + mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
  4440. + /* todo: au_h_open_pre()? */
  4441. +
  4442. + pin_orig = cpg->pin;
  4443. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  4444. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  4445. + cpg->pin = &wh_pin;
  4446. + }
  4447. +
  4448. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  4449. + && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
  4450. + err = au_cpup_wh(cpg, file);
  4451. + else {
  4452. + struct au_cpup_wh_args args = {
  4453. + .errp = &err,
  4454. + .cpg = cpg,
  4455. + .file = file
  4456. + };
  4457. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  4458. + if (unlikely(wkq_err))
  4459. + err = wkq_err;
  4460. + }
  4461. +
  4462. + if (h_orph) {
  4463. + mutex_unlock(&h_tmpdir->i_mutex);
  4464. + /* todo: au_h_open_post()? */
  4465. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  4466. + au_set_h_dptr(parent, bdst, h_parent);
  4467. + AuDebugOn(!pin_orig);
  4468. + cpg->pin = pin_orig;
  4469. + }
  4470. + iput(h_dir);
  4471. + dput(parent);
  4472. +
  4473. + return err;
  4474. +}
  4475. +
  4476. +/* ---------------------------------------------------------------------- */
  4477. +
  4478. +/*
  4479. + * generic routine for both of copy-up and copy-down.
  4480. + */
  4481. +/* cf. revalidate function in file.c */
  4482. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  4483. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  4484. + struct au_pin *pin,
  4485. + struct dentry *h_parent, void *arg),
  4486. + void *arg)
  4487. +{
  4488. + int err;
  4489. + struct au_pin pin;
  4490. + struct dentry *d, *parent, *h_parent, *real_parent;
  4491. +
  4492. + err = 0;
  4493. + parent = dget_parent(dentry);
  4494. + if (IS_ROOT(parent))
  4495. + goto out;
  4496. +
  4497. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  4498. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  4499. +
  4500. + /* do not use au_dpage */
  4501. + real_parent = parent;
  4502. + while (1) {
  4503. + dput(parent);
  4504. + parent = dget_parent(dentry);
  4505. + h_parent = au_h_dptr(parent, bdst);
  4506. + if (h_parent)
  4507. + goto out; /* success */
  4508. +
  4509. + /* find top dir which is necessary to cpup */
  4510. + do {
  4511. + d = parent;
  4512. + dput(parent);
  4513. + parent = dget_parent(d);
  4514. + di_read_lock_parent3(parent, !AuLock_IR);
  4515. + h_parent = au_h_dptr(parent, bdst);
  4516. + di_read_unlock(parent, !AuLock_IR);
  4517. + } while (!h_parent);
  4518. +
  4519. + if (d != real_parent)
  4520. + di_write_lock_child3(d);
  4521. +
  4522. + /* somebody else might create while we were sleeping */
  4523. + if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
  4524. + if (au_h_dptr(d, bdst))
  4525. + au_update_dbstart(d);
  4526. +
  4527. + au_pin_set_dentry(&pin, d);
  4528. + err = au_do_pin(&pin);
  4529. + if (!err) {
  4530. + err = cp(d, bdst, &pin, h_parent, arg);
  4531. + au_unpin(&pin);
  4532. + }
  4533. + }
  4534. +
  4535. + if (d != real_parent)
  4536. + di_write_unlock(d);
  4537. + if (unlikely(err))
  4538. + break;
  4539. + }
  4540. +
  4541. +out:
  4542. + dput(parent);
  4543. + return err;
  4544. +}
  4545. +
  4546. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  4547. + struct au_pin *pin,
  4548. + struct dentry *h_parent __maybe_unused,
  4549. + void *arg __maybe_unused)
  4550. +{
  4551. + struct au_cp_generic cpg = {
  4552. + .dentry = dentry,
  4553. + .bdst = bdst,
  4554. + .bsrc = -1,
  4555. + .len = 0,
  4556. + .pin = pin,
  4557. + .flags = AuCpup_DTIME
  4558. + };
  4559. + return au_sio_cpup_simple(&cpg);
  4560. +}
  4561. +
  4562. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4563. +{
  4564. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  4565. +}
  4566. +
  4567. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4568. +{
  4569. + int err;
  4570. + struct dentry *parent;
  4571. + struct inode *dir;
  4572. +
  4573. + parent = dget_parent(dentry);
  4574. + dir = parent->d_inode;
  4575. + err = 0;
  4576. + if (au_h_iptr(dir, bdst))
  4577. + goto out;
  4578. +
  4579. + di_read_unlock(parent, AuLock_IR);
  4580. + di_write_lock_parent(parent);
  4581. + /* someone else might change our inode while we were sleeping */
  4582. + if (!au_h_iptr(dir, bdst))
  4583. + err = au_cpup_dirs(dentry, bdst);
  4584. + di_downgrade_lock(parent, AuLock_IR);
  4585. +
  4586. +out:
  4587. + dput(parent);
  4588. + return err;
  4589. +}
  4590. diff -Nur linux-4.0.4.orig/fs/aufs/cpup.h linux-4.0.4/fs/aufs/cpup.h
  4591. --- linux-4.0.4.orig/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
  4592. +++ linux-4.0.4/fs/aufs/cpup.h 2015-05-30 22:11:30.000000000 +0200
  4593. @@ -0,0 +1,81 @@
  4594. +/*
  4595. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4596. + */
  4597. +
  4598. +/*
  4599. + * copy-up/down functions
  4600. + */
  4601. +
  4602. +#ifndef __AUFS_CPUP_H__
  4603. +#define __AUFS_CPUP_H__
  4604. +
  4605. +#ifdef __KERNEL__
  4606. +
  4607. +#include <linux/path.h>
  4608. +
  4609. +struct inode;
  4610. +struct file;
  4611. +struct au_pin;
  4612. +
  4613. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  4614. +void au_cpup_attr_timesizes(struct inode *inode);
  4615. +void au_cpup_attr_nlink(struct inode *inode, int force);
  4616. +void au_cpup_attr_changeable(struct inode *inode);
  4617. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  4618. +void au_cpup_attr_all(struct inode *inode, int force);
  4619. +
  4620. +/* ---------------------------------------------------------------------- */
  4621. +
  4622. +struct au_cp_generic {
  4623. + struct dentry *dentry;
  4624. + aufs_bindex_t bdst, bsrc;
  4625. + loff_t len;
  4626. + struct au_pin *pin;
  4627. + unsigned int flags;
  4628. +};
  4629. +
  4630. +/* cpup flags */
  4631. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  4632. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  4633. + for link(2) */
  4634. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  4635. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  4636. + cpup */
  4637. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  4638. + existing entry */
  4639. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  4640. + the branch is marked as RO */
  4641. +
  4642. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  4643. +#define au_fset_cpup(flags, name) \
  4644. + do { (flags) |= AuCpup_##name; } while (0)
  4645. +#define au_fclr_cpup(flags, name) \
  4646. + do { (flags) &= ~AuCpup_##name; } while (0)
  4647. +
  4648. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  4649. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  4650. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  4651. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  4652. +
  4653. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  4654. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  4655. + struct au_pin *pin,
  4656. + struct dentry *h_parent, void *arg),
  4657. + void *arg);
  4658. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  4659. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  4660. +
  4661. +/* ---------------------------------------------------------------------- */
  4662. +
  4663. +/* keep timestamps when copyup */
  4664. +struct au_dtime {
  4665. + struct dentry *dt_dentry;
  4666. + struct path dt_h_path;
  4667. + struct timespec dt_atime, dt_mtime;
  4668. +};
  4669. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  4670. + struct path *h_path);
  4671. +void au_dtime_revert(struct au_dtime *dt);
  4672. +
  4673. +#endif /* __KERNEL__ */
  4674. +#endif /* __AUFS_CPUP_H__ */
  4675. diff -Nur linux-4.0.4.orig/fs/aufs/dbgaufs.c linux-4.0.4/fs/aufs/dbgaufs.c
  4676. --- linux-4.0.4.orig/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
  4677. +++ linux-4.0.4/fs/aufs/dbgaufs.c 2015-05-30 22:11:30.000000000 +0200
  4678. @@ -0,0 +1,419 @@
  4679. +/*
  4680. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4681. + */
  4682. +
  4683. +/*
  4684. + * debugfs interface
  4685. + */
  4686. +
  4687. +#include <linux/debugfs.h>
  4688. +#include "aufs.h"
  4689. +
  4690. +#ifndef CONFIG_SYSFS
  4691. +#error DEBUG_FS depends upon SYSFS
  4692. +#endif
  4693. +
  4694. +static struct dentry *dbgaufs;
  4695. +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
  4696. +
  4697. +/* 20 is max digits length of ulong 64 */
  4698. +struct dbgaufs_arg {
  4699. + int n;
  4700. + char a[20 * 4];
  4701. +};
  4702. +
  4703. +/*
  4704. + * common function for all XINO files
  4705. + */
  4706. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  4707. + struct file *file)
  4708. +{
  4709. + kfree(file->private_data);
  4710. + return 0;
  4711. +}
  4712. +
  4713. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
  4714. +{
  4715. + int err;
  4716. + struct kstat st;
  4717. + struct dbgaufs_arg *p;
  4718. +
  4719. + err = -ENOMEM;
  4720. + p = kmalloc(sizeof(*p), GFP_NOFS);
  4721. + if (unlikely(!p))
  4722. + goto out;
  4723. +
  4724. + err = 0;
  4725. + p->n = 0;
  4726. + file->private_data = p;
  4727. + if (!xf)
  4728. + goto out;
  4729. +
  4730. + err = vfs_getattr(&xf->f_path, &st);
  4731. + if (!err) {
  4732. + if (do_fcnt)
  4733. + p->n = snprintf
  4734. + (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
  4735. + (long)file_count(xf), st.blocks, st.blksize,
  4736. + (long long)st.size);
  4737. + else
  4738. + p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
  4739. + st.blocks, st.blksize,
  4740. + (long long)st.size);
  4741. + AuDebugOn(p->n >= sizeof(p->a));
  4742. + } else {
  4743. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  4744. + err = 0;
  4745. + }
  4746. +
  4747. +out:
  4748. + return err;
  4749. +
  4750. +}
  4751. +
  4752. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  4753. + size_t count, loff_t *ppos)
  4754. +{
  4755. + struct dbgaufs_arg *p;
  4756. +
  4757. + p = file->private_data;
  4758. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  4759. +}
  4760. +
  4761. +/* ---------------------------------------------------------------------- */
  4762. +
  4763. +struct dbgaufs_plink_arg {
  4764. + int n;
  4765. + char a[];
  4766. +};
  4767. +
  4768. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  4769. + struct file *file)
  4770. +{
  4771. + free_page((unsigned long)file->private_data);
  4772. + return 0;
  4773. +}
  4774. +
  4775. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  4776. +{
  4777. + int err, i, limit;
  4778. + unsigned long n, sum;
  4779. + struct dbgaufs_plink_arg *p;
  4780. + struct au_sbinfo *sbinfo;
  4781. + struct super_block *sb;
  4782. + struct au_sphlhead *sphl;
  4783. +
  4784. + err = -ENOMEM;
  4785. + p = (void *)get_zeroed_page(GFP_NOFS);
  4786. + if (unlikely(!p))
  4787. + goto out;
  4788. +
  4789. + err = -EFBIG;
  4790. + sbinfo = inode->i_private;
  4791. + sb = sbinfo->si_sb;
  4792. + si_noflush_read_lock(sb);
  4793. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  4794. + limit = PAGE_SIZE - sizeof(p->n);
  4795. +
  4796. + /* the number of buckets */
  4797. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  4798. + p->n += n;
  4799. + limit -= n;
  4800. +
  4801. + sum = 0;
  4802. + for (i = 0, sphl = sbinfo->si_plink;
  4803. + i < AuPlink_NHASH;
  4804. + i++, sphl++) {
  4805. + n = au_sphl_count(sphl);
  4806. + sum += n;
  4807. +
  4808. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  4809. + p->n += n;
  4810. + limit -= n;
  4811. + if (unlikely(limit <= 0))
  4812. + goto out_free;
  4813. + }
  4814. + p->a[p->n - 1] = '\n';
  4815. +
  4816. + /* the sum of plinks */
  4817. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  4818. + p->n += n;
  4819. + limit -= n;
  4820. + if (unlikely(limit <= 0))
  4821. + goto out_free;
  4822. + } else {
  4823. +#define str "1\n0\n0\n"
  4824. + p->n = sizeof(str) - 1;
  4825. + strcpy(p->a, str);
  4826. +#undef str
  4827. + }
  4828. + si_read_unlock(sb);
  4829. +
  4830. + err = 0;
  4831. + file->private_data = p;
  4832. + goto out; /* success */
  4833. +
  4834. +out_free:
  4835. + free_page((unsigned long)p);
  4836. +out:
  4837. + return err;
  4838. +}
  4839. +
  4840. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  4841. + size_t count, loff_t *ppos)
  4842. +{
  4843. + struct dbgaufs_plink_arg *p;
  4844. +
  4845. + p = file->private_data;
  4846. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  4847. +}
  4848. +
  4849. +static const struct file_operations dbgaufs_plink_fop = {
  4850. + .owner = THIS_MODULE,
  4851. + .open = dbgaufs_plink_open,
  4852. + .release = dbgaufs_plink_release,
  4853. + .read = dbgaufs_plink_read
  4854. +};
  4855. +
  4856. +/* ---------------------------------------------------------------------- */
  4857. +
  4858. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  4859. +{
  4860. + int err;
  4861. + struct au_sbinfo *sbinfo;
  4862. + struct super_block *sb;
  4863. +
  4864. + sbinfo = inode->i_private;
  4865. + sb = sbinfo->si_sb;
  4866. + si_noflush_read_lock(sb);
  4867. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
  4868. + si_read_unlock(sb);
  4869. + return err;
  4870. +}
  4871. +
  4872. +static const struct file_operations dbgaufs_xib_fop = {
  4873. + .owner = THIS_MODULE,
  4874. + .open = dbgaufs_xib_open,
  4875. + .release = dbgaufs_xi_release,
  4876. + .read = dbgaufs_xi_read
  4877. +};
  4878. +
  4879. +/* ---------------------------------------------------------------------- */
  4880. +
  4881. +#define DbgaufsXi_PREFIX "xi"
  4882. +
  4883. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  4884. +{
  4885. + int err;
  4886. + long l;
  4887. + struct au_sbinfo *sbinfo;
  4888. + struct super_block *sb;
  4889. + struct file *xf;
  4890. + struct qstr *name;
  4891. +
  4892. + err = -ENOENT;
  4893. + xf = NULL;
  4894. + name = &file->f_path.dentry->d_name;
  4895. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  4896. + || memcmp(name->name, DbgaufsXi_PREFIX,
  4897. + sizeof(DbgaufsXi_PREFIX) - 1)))
  4898. + goto out;
  4899. + err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  4900. + if (unlikely(err))
  4901. + goto out;
  4902. +
  4903. + sbinfo = inode->i_private;
  4904. + sb = sbinfo->si_sb;
  4905. + si_noflush_read_lock(sb);
  4906. + if (l <= au_sbend(sb)) {
  4907. + xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
  4908. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
  4909. + } else
  4910. + err = -ENOENT;
  4911. + si_read_unlock(sb);
  4912. +
  4913. +out:
  4914. + return err;
  4915. +}
  4916. +
  4917. +static const struct file_operations dbgaufs_xino_fop = {
  4918. + .owner = THIS_MODULE,
  4919. + .open = dbgaufs_xino_open,
  4920. + .release = dbgaufs_xi_release,
  4921. + .read = dbgaufs_xi_read
  4922. +};
  4923. +
  4924. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  4925. +{
  4926. + aufs_bindex_t bend;
  4927. + struct au_branch *br;
  4928. + struct au_xino_file *xi;
  4929. +
  4930. + if (!au_sbi(sb)->si_dbgaufs)
  4931. + return;
  4932. +
  4933. + bend = au_sbend(sb);
  4934. + for (; bindex <= bend; bindex++) {
  4935. + br = au_sbr(sb, bindex);
  4936. + xi = &br->br_xino;
  4937. + debugfs_remove(xi->xi_dbgaufs);
  4938. + xi->xi_dbgaufs = NULL;
  4939. + }
  4940. +}
  4941. +
  4942. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  4943. +{
  4944. + struct au_sbinfo *sbinfo;
  4945. + struct dentry *parent;
  4946. + struct au_branch *br;
  4947. + struct au_xino_file *xi;
  4948. + aufs_bindex_t bend;
  4949. + char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
  4950. +
  4951. + sbinfo = au_sbi(sb);
  4952. + parent = sbinfo->si_dbgaufs;
  4953. + if (!parent)
  4954. + return;
  4955. +
  4956. + bend = au_sbend(sb);
  4957. + for (; bindex <= bend; bindex++) {
  4958. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  4959. + br = au_sbr(sb, bindex);
  4960. + xi = &br->br_xino;
  4961. + AuDebugOn(xi->xi_dbgaufs);
  4962. + xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  4963. + sbinfo, &dbgaufs_xino_fop);
  4964. + /* ignore an error */
  4965. + if (unlikely(!xi->xi_dbgaufs))
  4966. + AuWarn1("failed %s under debugfs\n", name);
  4967. + }
  4968. +}
  4969. +
  4970. +/* ---------------------------------------------------------------------- */
  4971. +
  4972. +#ifdef CONFIG_AUFS_EXPORT
  4973. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  4974. +{
  4975. + int err;
  4976. + struct au_sbinfo *sbinfo;
  4977. + struct super_block *sb;
  4978. +
  4979. + sbinfo = inode->i_private;
  4980. + sb = sbinfo->si_sb;
  4981. + si_noflush_read_lock(sb);
  4982. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
  4983. + si_read_unlock(sb);
  4984. + return err;
  4985. +}
  4986. +
  4987. +static const struct file_operations dbgaufs_xigen_fop = {
  4988. + .owner = THIS_MODULE,
  4989. + .open = dbgaufs_xigen_open,
  4990. + .release = dbgaufs_xi_release,
  4991. + .read = dbgaufs_xi_read
  4992. +};
  4993. +
  4994. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  4995. +{
  4996. + int err;
  4997. +
  4998. + /*
  4999. + * This function is a dynamic '__init' function actually,
  5000. + * so the tiny check for si_rwsem is unnecessary.
  5001. + */
  5002. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5003. +
  5004. + err = -EIO;
  5005. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5006. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5007. + &dbgaufs_xigen_fop);
  5008. + if (sbinfo->si_dbgaufs_xigen)
  5009. + err = 0;
  5010. +
  5011. + return err;
  5012. +}
  5013. +#else
  5014. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5015. +{
  5016. + return 0;
  5017. +}
  5018. +#endif /* CONFIG_AUFS_EXPORT */
  5019. +
  5020. +/* ---------------------------------------------------------------------- */
  5021. +
  5022. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5023. +{
  5024. + /*
  5025. + * This function is a dynamic '__fin' function actually,
  5026. + * so the tiny check for si_rwsem is unnecessary.
  5027. + */
  5028. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5029. +
  5030. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5031. + sbinfo->si_dbgaufs = NULL;
  5032. + kobject_put(&sbinfo->si_kobj);
  5033. +}
  5034. +
  5035. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5036. +{
  5037. + int err;
  5038. + char name[SysaufsSiNameLen];
  5039. +
  5040. + /*
  5041. + * This function is a dynamic '__init' function actually,
  5042. + * so the tiny check for si_rwsem is unnecessary.
  5043. + */
  5044. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5045. +
  5046. + err = -ENOENT;
  5047. + if (!dbgaufs) {
  5048. + AuErr1("/debug/aufs is uninitialized\n");
  5049. + goto out;
  5050. + }
  5051. +
  5052. + err = -EIO;
  5053. + sysaufs_name(sbinfo, name);
  5054. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5055. + if (unlikely(!sbinfo->si_dbgaufs))
  5056. + goto out;
  5057. + kobject_get(&sbinfo->si_kobj);
  5058. +
  5059. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5060. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5061. + &dbgaufs_xib_fop);
  5062. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5063. + goto out_dir;
  5064. +
  5065. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5066. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5067. + &dbgaufs_plink_fop);
  5068. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5069. + goto out_dir;
  5070. +
  5071. + err = dbgaufs_xigen_init(sbinfo);
  5072. + if (!err)
  5073. + goto out; /* success */
  5074. +
  5075. +out_dir:
  5076. + dbgaufs_si_fin(sbinfo);
  5077. +out:
  5078. + return err;
  5079. +}
  5080. +
  5081. +/* ---------------------------------------------------------------------- */
  5082. +
  5083. +void dbgaufs_fin(void)
  5084. +{
  5085. + debugfs_remove(dbgaufs);
  5086. +}
  5087. +
  5088. +int __init dbgaufs_init(void)
  5089. +{
  5090. + int err;
  5091. +
  5092. + err = -EIO;
  5093. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5094. + if (dbgaufs)
  5095. + err = 0;
  5096. + return err;
  5097. +}
  5098. diff -Nur linux-4.0.4.orig/fs/aufs/dbgaufs.h linux-4.0.4/fs/aufs/dbgaufs.h
  5099. --- linux-4.0.4.orig/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
  5100. +++ linux-4.0.4/fs/aufs/dbgaufs.h 2015-05-30 22:11:30.000000000 +0200
  5101. @@ -0,0 +1,35 @@
  5102. +/*
  5103. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5104. + */
  5105. +
  5106. +/*
  5107. + * debugfs interface
  5108. + */
  5109. +
  5110. +#ifndef __DBGAUFS_H__
  5111. +#define __DBGAUFS_H__
  5112. +
  5113. +#ifdef __KERNEL__
  5114. +
  5115. +struct super_block;
  5116. +struct au_sbinfo;
  5117. +
  5118. +#ifdef CONFIG_DEBUG_FS
  5119. +/* dbgaufs.c */
  5120. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5121. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  5122. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5123. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5124. +void dbgaufs_fin(void);
  5125. +int __init dbgaufs_init(void);
  5126. +#else
  5127. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5128. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  5129. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5130. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5131. +AuStubVoid(dbgaufs_fin, void)
  5132. +AuStubInt0(__init dbgaufs_init, void)
  5133. +#endif /* CONFIG_DEBUG_FS */
  5134. +
  5135. +#endif /* __KERNEL__ */
  5136. +#endif /* __DBGAUFS_H__ */
  5137. diff -Nur linux-4.0.4.orig/fs/aufs/dcsub.c linux-4.0.4/fs/aufs/dcsub.c
  5138. --- linux-4.0.4.orig/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
  5139. +++ linux-4.0.4/fs/aufs/dcsub.c 2015-05-30 22:11:30.000000000 +0200
  5140. @@ -0,0 +1,211 @@
  5141. +/*
  5142. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5143. + */
  5144. +
  5145. +/*
  5146. + * sub-routines for dentry cache
  5147. + */
  5148. +
  5149. +#include "aufs.h"
  5150. +
  5151. +static void au_dpage_free(struct au_dpage *dpage)
  5152. +{
  5153. + int i;
  5154. + struct dentry **p;
  5155. +
  5156. + p = dpage->dentries;
  5157. + for (i = 0; i < dpage->ndentry; i++)
  5158. + dput(*p++);
  5159. + free_page((unsigned long)dpage->dentries);
  5160. +}
  5161. +
  5162. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  5163. +{
  5164. + int err;
  5165. + void *p;
  5166. +
  5167. + err = -ENOMEM;
  5168. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  5169. + if (unlikely(!dpages->dpages))
  5170. + goto out;
  5171. +
  5172. + p = (void *)__get_free_page(gfp);
  5173. + if (unlikely(!p))
  5174. + goto out_dpages;
  5175. +
  5176. + dpages->dpages[0].ndentry = 0;
  5177. + dpages->dpages[0].dentries = p;
  5178. + dpages->ndpage = 1;
  5179. + return 0; /* success */
  5180. +
  5181. +out_dpages:
  5182. + kfree(dpages->dpages);
  5183. +out:
  5184. + return err;
  5185. +}
  5186. +
  5187. +void au_dpages_free(struct au_dcsub_pages *dpages)
  5188. +{
  5189. + int i;
  5190. + struct au_dpage *p;
  5191. +
  5192. + p = dpages->dpages;
  5193. + for (i = 0; i < dpages->ndpage; i++)
  5194. + au_dpage_free(p++);
  5195. + kfree(dpages->dpages);
  5196. +}
  5197. +
  5198. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  5199. + struct dentry *dentry, gfp_t gfp)
  5200. +{
  5201. + int err, sz;
  5202. + struct au_dpage *dpage;
  5203. + void *p;
  5204. +
  5205. + dpage = dpages->dpages + dpages->ndpage - 1;
  5206. + sz = PAGE_SIZE / sizeof(dentry);
  5207. + if (unlikely(dpage->ndentry >= sz)) {
  5208. + AuLabel(new dpage);
  5209. + err = -ENOMEM;
  5210. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  5211. + p = au_kzrealloc(dpages->dpages, sz,
  5212. + sz + sizeof(*dpages->dpages), gfp);
  5213. + if (unlikely(!p))
  5214. + goto out;
  5215. +
  5216. + dpages->dpages = p;
  5217. + dpage = dpages->dpages + dpages->ndpage;
  5218. + p = (void *)__get_free_page(gfp);
  5219. + if (unlikely(!p))
  5220. + goto out;
  5221. +
  5222. + dpage->ndentry = 0;
  5223. + dpage->dentries = p;
  5224. + dpages->ndpage++;
  5225. + }
  5226. +
  5227. + AuDebugOn(au_dcount(dentry) <= 0);
  5228. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  5229. + return 0; /* success */
  5230. +
  5231. +out:
  5232. + return err;
  5233. +}
  5234. +
  5235. +/* todo: BAD approach */
  5236. +/* copied from linux/fs/dcache.c */
  5237. +enum d_walk_ret {
  5238. + D_WALK_CONTINUE,
  5239. + D_WALK_QUIT,
  5240. + D_WALK_NORETRY,
  5241. + D_WALK_SKIP,
  5242. +};
  5243. +
  5244. +extern void d_walk(struct dentry *parent, void *data,
  5245. + enum d_walk_ret (*enter)(void *, struct dentry *),
  5246. + void (*finish)(void *));
  5247. +
  5248. +struct ac_dpages_arg {
  5249. + int err;
  5250. + struct au_dcsub_pages *dpages;
  5251. + struct super_block *sb;
  5252. + au_dpages_test test;
  5253. + void *arg;
  5254. +};
  5255. +
  5256. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  5257. +{
  5258. + enum d_walk_ret ret;
  5259. + struct ac_dpages_arg *arg = _arg;
  5260. +
  5261. + ret = D_WALK_CONTINUE;
  5262. + if (dentry->d_sb == arg->sb
  5263. + && !IS_ROOT(dentry)
  5264. + && au_dcount(dentry) > 0
  5265. + && au_di(dentry)
  5266. + && (!arg->test || arg->test(dentry, arg->arg))) {
  5267. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  5268. + if (unlikely(arg->err))
  5269. + ret = D_WALK_QUIT;
  5270. + }
  5271. +
  5272. + return ret;
  5273. +}
  5274. +
  5275. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5276. + au_dpages_test test, void *arg)
  5277. +{
  5278. + struct ac_dpages_arg args = {
  5279. + .err = 0,
  5280. + .dpages = dpages,
  5281. + .sb = root->d_sb,
  5282. + .test = test,
  5283. + .arg = arg
  5284. + };
  5285. +
  5286. + d_walk(root, &args, au_call_dpages_append, NULL);
  5287. +
  5288. + return args.err;
  5289. +}
  5290. +
  5291. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5292. + int do_include, au_dpages_test test, void *arg)
  5293. +{
  5294. + int err;
  5295. +
  5296. + err = 0;
  5297. + write_seqlock(&rename_lock);
  5298. + spin_lock(&dentry->d_lock);
  5299. + if (do_include
  5300. + && au_dcount(dentry) > 0
  5301. + && (!test || test(dentry, arg)))
  5302. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5303. + spin_unlock(&dentry->d_lock);
  5304. + if (unlikely(err))
  5305. + goto out;
  5306. +
  5307. + /*
  5308. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  5309. + * mount
  5310. + */
  5311. + while (!IS_ROOT(dentry)) {
  5312. + dentry = dentry->d_parent; /* rename_lock is locked */
  5313. + spin_lock(&dentry->d_lock);
  5314. + if (au_dcount(dentry) > 0
  5315. + && (!test || test(dentry, arg)))
  5316. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5317. + spin_unlock(&dentry->d_lock);
  5318. + if (unlikely(err))
  5319. + break;
  5320. + }
  5321. +
  5322. +out:
  5323. + write_sequnlock(&rename_lock);
  5324. + return err;
  5325. +}
  5326. +
  5327. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  5328. +{
  5329. + return au_di(dentry) && dentry->d_sb == arg;
  5330. +}
  5331. +
  5332. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5333. + struct dentry *dentry, int do_include)
  5334. +{
  5335. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  5336. + au_dcsub_dpages_aufs, dentry->d_sb);
  5337. +}
  5338. +
  5339. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  5340. +{
  5341. + struct path path[2] = {
  5342. + {
  5343. + .dentry = d1
  5344. + },
  5345. + {
  5346. + .dentry = d2
  5347. + }
  5348. + };
  5349. +
  5350. + return path_is_under(path + 0, path + 1);
  5351. +}
  5352. diff -Nur linux-4.0.4.orig/fs/aufs/dcsub.h linux-4.0.4/fs/aufs/dcsub.h
  5353. --- linux-4.0.4.orig/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
  5354. +++ linux-4.0.4/fs/aufs/dcsub.h 2015-05-30 22:11:30.000000000 +0200
  5355. @@ -0,0 +1,119 @@
  5356. +/*
  5357. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5358. + */
  5359. +
  5360. +/*
  5361. + * sub-routines for dentry cache
  5362. + */
  5363. +
  5364. +#ifndef __AUFS_DCSUB_H__
  5365. +#define __AUFS_DCSUB_H__
  5366. +
  5367. +#ifdef __KERNEL__
  5368. +
  5369. +#include <linux/dcache.h>
  5370. +#include <linux/fs.h>
  5371. +
  5372. +struct au_dpage {
  5373. + int ndentry;
  5374. + struct dentry **dentries;
  5375. +};
  5376. +
  5377. +struct au_dcsub_pages {
  5378. + int ndpage;
  5379. + struct au_dpage *dpages;
  5380. +};
  5381. +
  5382. +/* ---------------------------------------------------------------------- */
  5383. +
  5384. +/* dcsub.c */
  5385. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  5386. +void au_dpages_free(struct au_dcsub_pages *dpages);
  5387. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  5388. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5389. + au_dpages_test test, void *arg);
  5390. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5391. + int do_include, au_dpages_test test, void *arg);
  5392. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5393. + struct dentry *dentry, int do_include);
  5394. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  5395. +
  5396. +/* ---------------------------------------------------------------------- */
  5397. +
  5398. +/*
  5399. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  5400. + * include/linux/dcache.h. Try them (in the future).
  5401. + */
  5402. +
  5403. +static inline int au_d_hashed_positive(struct dentry *d)
  5404. +{
  5405. + int err;
  5406. + struct inode *inode = d->d_inode;
  5407. +
  5408. + err = 0;
  5409. + if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
  5410. + err = -ENOENT;
  5411. + return err;
  5412. +}
  5413. +
  5414. +static inline int au_d_linkable(struct dentry *d)
  5415. +{
  5416. + int err;
  5417. + struct inode *inode = d->d_inode;
  5418. +
  5419. + err = au_d_hashed_positive(d);
  5420. + if (err
  5421. + && inode
  5422. + && (inode->i_state & I_LINKABLE))
  5423. + err = 0;
  5424. + return err;
  5425. +}
  5426. +
  5427. +static inline int au_d_alive(struct dentry *d)
  5428. +{
  5429. + int err;
  5430. + struct inode *inode;
  5431. +
  5432. + err = 0;
  5433. + if (!IS_ROOT(d))
  5434. + err = au_d_hashed_positive(d);
  5435. + else {
  5436. + inode = d->d_inode;
  5437. + if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
  5438. + err = -ENOENT;
  5439. + }
  5440. + return err;
  5441. +}
  5442. +
  5443. +static inline int au_alive_dir(struct dentry *d)
  5444. +{
  5445. + int err;
  5446. +
  5447. + err = au_d_alive(d);
  5448. + if (unlikely(err || IS_DEADDIR(d->d_inode)))
  5449. + err = -ENOENT;
  5450. + return err;
  5451. +}
  5452. +
  5453. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  5454. +{
  5455. + return a->len == b->len
  5456. + && !memcmp(a->name, b->name, a->len);
  5457. +}
  5458. +
  5459. +/*
  5460. + * by the commit
  5461. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  5462. + * taking d_lock
  5463. + * the type of d_lockref.count became int, but the inlined function d_count()
  5464. + * still returns unsigned int.
  5465. + * I don't know why. Maybe it is for every d_count() users?
  5466. + * Anyway au_dcount() lives on.
  5467. + */
  5468. +static inline int au_dcount(struct dentry *d)
  5469. +{
  5470. + return (int)d_count(d);
  5471. +}
  5472. +
  5473. +#endif /* __KERNEL__ */
  5474. +#endif /* __AUFS_DCSUB_H__ */
  5475. diff -Nur linux-4.0.4.orig/fs/aufs/debug.c linux-4.0.4/fs/aufs/debug.c
  5476. --- linux-4.0.4.orig/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
  5477. +++ linux-4.0.4/fs/aufs/debug.c 2015-05-30 22:11:31.000000000 +0200
  5478. @@ -0,0 +1,425 @@
  5479. +/*
  5480. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5481. + */
  5482. +
  5483. +/*
  5484. + * debug print functions
  5485. + */
  5486. +
  5487. +#include "aufs.h"
  5488. +
  5489. +/* Returns 0, or -errno. arg is in kp->arg. */
  5490. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  5491. +{
  5492. + int err, n;
  5493. +
  5494. + err = kstrtoint(val, 0, &n);
  5495. + if (!err) {
  5496. + if (n > 0)
  5497. + au_debug_on();
  5498. + else
  5499. + au_debug_off();
  5500. + }
  5501. + return err;
  5502. +}
  5503. +
  5504. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  5505. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  5506. +{
  5507. + atomic_t *a;
  5508. +
  5509. + a = kp->arg;
  5510. + return sprintf(buffer, "%d", atomic_read(a));
  5511. +}
  5512. +
  5513. +static struct kernel_param_ops param_ops_atomic_t = {
  5514. + .set = param_atomic_t_set,
  5515. + .get = param_atomic_t_get
  5516. + /* void (*free)(void *arg) */
  5517. +};
  5518. +
  5519. +atomic_t aufs_debug = ATOMIC_INIT(0);
  5520. +MODULE_PARM_DESC(debug, "debug print");
  5521. +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
  5522. +
  5523. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  5524. +char *au_plevel = KERN_DEBUG;
  5525. +#define dpri(fmt, ...) do { \
  5526. + if ((au_plevel \
  5527. + && strcmp(au_plevel, KERN_DEBUG)) \
  5528. + || au_debug_test()) \
  5529. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  5530. +} while (0)
  5531. +
  5532. +/* ---------------------------------------------------------------------- */
  5533. +
  5534. +void au_dpri_whlist(struct au_nhash *whlist)
  5535. +{
  5536. + unsigned long ul, n;
  5537. + struct hlist_head *head;
  5538. + struct au_vdir_wh *pos;
  5539. +
  5540. + n = whlist->nh_num;
  5541. + head = whlist->nh_head;
  5542. + for (ul = 0; ul < n; ul++) {
  5543. + hlist_for_each_entry(pos, head, wh_hash)
  5544. + dpri("b%d, %.*s, %d\n",
  5545. + pos->wh_bindex,
  5546. + pos->wh_str.len, pos->wh_str.name,
  5547. + pos->wh_str.len);
  5548. + head++;
  5549. + }
  5550. +}
  5551. +
  5552. +void au_dpri_vdir(struct au_vdir *vdir)
  5553. +{
  5554. + unsigned long ul;
  5555. + union au_vdir_deblk_p p;
  5556. + unsigned char *o;
  5557. +
  5558. + if (!vdir || IS_ERR(vdir)) {
  5559. + dpri("err %ld\n", PTR_ERR(vdir));
  5560. + return;
  5561. + }
  5562. +
  5563. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
  5564. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  5565. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  5566. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  5567. + p.deblk = vdir->vd_deblk[ul];
  5568. + o = p.deblk;
  5569. + dpri("[%lu]: %p\n", ul, o);
  5570. + }
  5571. +}
  5572. +
  5573. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  5574. + struct dentry *wh)
  5575. +{
  5576. + char *n = NULL;
  5577. + int l = 0;
  5578. +
  5579. + if (!inode || IS_ERR(inode)) {
  5580. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  5581. + return -1;
  5582. + }
  5583. +
  5584. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  5585. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  5586. + && sizeof(inode->i_blocks) != sizeof(u64));
  5587. + if (wh) {
  5588. + n = (void *)wh->d_name.name;
  5589. + l = wh->d_name.len;
  5590. + }
  5591. +
  5592. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  5593. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  5594. + bindex, inode,
  5595. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  5596. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  5597. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  5598. + hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
  5599. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  5600. + inode->i_state, inode->i_flags, inode->i_version,
  5601. + inode->i_generation,
  5602. + l ? ", wh " : "", l, n);
  5603. + return 0;
  5604. +}
  5605. +
  5606. +void au_dpri_inode(struct inode *inode)
  5607. +{
  5608. + struct au_iinfo *iinfo;
  5609. + aufs_bindex_t bindex;
  5610. + int err, hn;
  5611. +
  5612. + err = do_pri_inode(-1, inode, -1, NULL);
  5613. + if (err || !au_test_aufs(inode->i_sb))
  5614. + return;
  5615. +
  5616. + iinfo = au_ii(inode);
  5617. + if (!iinfo)
  5618. + return;
  5619. + dpri("i-1: bstart %d, bend %d, gen %d\n",
  5620. + iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
  5621. + if (iinfo->ii_bstart < 0)
  5622. + return;
  5623. + hn = 0;
  5624. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
  5625. + hn = !!au_hn(iinfo->ii_hinode + bindex);
  5626. + do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
  5627. + iinfo->ii_hinode[0 + bindex].hi_whdentry);
  5628. + }
  5629. +}
  5630. +
  5631. +void au_dpri_dalias(struct inode *inode)
  5632. +{
  5633. + struct dentry *d;
  5634. +
  5635. + spin_lock(&inode->i_lock);
  5636. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  5637. + au_dpri_dentry(d);
  5638. + spin_unlock(&inode->i_lock);
  5639. +}
  5640. +
  5641. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  5642. +{
  5643. + struct dentry *wh = NULL;
  5644. + int hn;
  5645. + struct au_iinfo *iinfo;
  5646. +
  5647. + if (!dentry || IS_ERR(dentry)) {
  5648. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  5649. + return -1;
  5650. + }
  5651. + /* do not call dget_parent() here */
  5652. + /* note: access d_xxx without d_lock */
  5653. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  5654. + bindex, dentry, dentry,
  5655. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  5656. + au_dcount(dentry), dentry->d_flags,
  5657. + d_unhashed(dentry) ? "un" : "");
  5658. + hn = -1;
  5659. + if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
  5660. + iinfo = au_ii(dentry->d_inode);
  5661. + if (iinfo) {
  5662. + hn = !!au_hn(iinfo->ii_hinode + bindex);
  5663. + wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
  5664. + }
  5665. + }
  5666. + do_pri_inode(bindex, dentry->d_inode, hn, wh);
  5667. + return 0;
  5668. +}
  5669. +
  5670. +void au_dpri_dentry(struct dentry *dentry)
  5671. +{
  5672. + struct au_dinfo *dinfo;
  5673. + aufs_bindex_t bindex;
  5674. + int err;
  5675. + struct au_hdentry *hdp;
  5676. +
  5677. + err = do_pri_dentry(-1, dentry);
  5678. + if (err || !au_test_aufs(dentry->d_sb))
  5679. + return;
  5680. +
  5681. + dinfo = au_di(dentry);
  5682. + if (!dinfo)
  5683. + return;
  5684. + dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  5685. + dinfo->di_bstart, dinfo->di_bend,
  5686. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  5687. + dinfo->di_tmpfile);
  5688. + if (dinfo->di_bstart < 0)
  5689. + return;
  5690. + hdp = dinfo->di_hdentry;
  5691. + for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
  5692. + do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
  5693. +}
  5694. +
  5695. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  5696. +{
  5697. + char a[32];
  5698. +
  5699. + if (!file || IS_ERR(file)) {
  5700. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  5701. + return -1;
  5702. + }
  5703. + a[0] = 0;
  5704. + if (bindex < 0
  5705. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  5706. + && au_test_aufs(file->f_path.dentry->d_sb)
  5707. + && au_fi(file))
  5708. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  5709. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  5710. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  5711. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  5712. + file->f_version, file->f_pos, a);
  5713. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  5714. + do_pri_dentry(bindex, file->f_path.dentry);
  5715. + return 0;
  5716. +}
  5717. +
  5718. +void au_dpri_file(struct file *file)
  5719. +{
  5720. + struct au_finfo *finfo;
  5721. + struct au_fidir *fidir;
  5722. + struct au_hfile *hfile;
  5723. + aufs_bindex_t bindex;
  5724. + int err;
  5725. +
  5726. + err = do_pri_file(-1, file);
  5727. + if (err
  5728. + || IS_ERR_OR_NULL(file->f_path.dentry)
  5729. + || !au_test_aufs(file->f_path.dentry->d_sb))
  5730. + return;
  5731. +
  5732. + finfo = au_fi(file);
  5733. + if (!finfo)
  5734. + return;
  5735. + if (finfo->fi_btop < 0)
  5736. + return;
  5737. + fidir = finfo->fi_hdir;
  5738. + if (!fidir)
  5739. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  5740. + else
  5741. + for (bindex = finfo->fi_btop;
  5742. + bindex >= 0 && bindex <= fidir->fd_bbot;
  5743. + bindex++) {
  5744. + hfile = fidir->fd_hfile + bindex;
  5745. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  5746. + }
  5747. +}
  5748. +
  5749. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  5750. +{
  5751. + struct vfsmount *mnt;
  5752. + struct super_block *sb;
  5753. +
  5754. + if (!br || IS_ERR(br))
  5755. + goto out;
  5756. + mnt = au_br_mnt(br);
  5757. + if (!mnt || IS_ERR(mnt))
  5758. + goto out;
  5759. + sb = mnt->mnt_sb;
  5760. + if (!sb || IS_ERR(sb))
  5761. + goto out;
  5762. +
  5763. + dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
  5764. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  5765. + "xino %d\n",
  5766. + bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
  5767. + br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  5768. + sb->s_flags, sb->s_count,
  5769. + atomic_read(&sb->s_active), !!br->br_xino.xi_file);
  5770. + return 0;
  5771. +
  5772. +out:
  5773. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  5774. + return -1;
  5775. +}
  5776. +
  5777. +void au_dpri_sb(struct super_block *sb)
  5778. +{
  5779. + struct au_sbinfo *sbinfo;
  5780. + aufs_bindex_t bindex;
  5781. + int err;
  5782. + /* to reuduce stack size */
  5783. + struct {
  5784. + struct vfsmount mnt;
  5785. + struct au_branch fake;
  5786. + } *a;
  5787. +
  5788. + /* this function can be called from magic sysrq */
  5789. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  5790. + if (unlikely(!a)) {
  5791. + dpri("no memory\n");
  5792. + return;
  5793. + }
  5794. +
  5795. + a->mnt.mnt_sb = sb;
  5796. + a->fake.br_perm = 0;
  5797. + a->fake.br_path.mnt = &a->mnt;
  5798. + a->fake.br_xino.xi_file = NULL;
  5799. + atomic_set(&a->fake.br_count, 0);
  5800. + smp_mb(); /* atomic_set */
  5801. + err = do_pri_br(-1, &a->fake);
  5802. + kfree(a);
  5803. + dpri("dev 0x%x\n", sb->s_dev);
  5804. + if (err || !au_test_aufs(sb))
  5805. + return;
  5806. +
  5807. + sbinfo = au_sbi(sb);
  5808. + if (!sbinfo)
  5809. + return;
  5810. + dpri("nw %d, gen %u, kobj %d\n",
  5811. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  5812. + atomic_read(&sbinfo->si_kobj.kref.refcount));
  5813. + for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
  5814. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  5815. +}
  5816. +
  5817. +/* ---------------------------------------------------------------------- */
  5818. +
  5819. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  5820. +{
  5821. + struct inode *h_inode, *inode = dentry->d_inode;
  5822. + struct dentry *h_dentry;
  5823. + aufs_bindex_t bindex, bend, bi;
  5824. +
  5825. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  5826. + return;
  5827. +
  5828. + bend = au_dbend(dentry);
  5829. + bi = au_ibend(inode);
  5830. + if (bi < bend)
  5831. + bend = bi;
  5832. + bindex = au_dbstart(dentry);
  5833. + bi = au_ibstart(inode);
  5834. + if (bi > bindex)
  5835. + bindex = bi;
  5836. +
  5837. + for (; bindex <= bend; bindex++) {
  5838. + h_dentry = au_h_dptr(dentry, bindex);
  5839. + if (!h_dentry)
  5840. + continue;
  5841. + h_inode = au_h_iptr(inode, bindex);
  5842. + if (unlikely(h_inode != h_dentry->d_inode)) {
  5843. + au_debug_on();
  5844. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  5845. + AuDbgDentry(dentry);
  5846. + AuDbgInode(inode);
  5847. + au_debug_off();
  5848. + BUG();
  5849. + }
  5850. + }
  5851. +}
  5852. +
  5853. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  5854. +{
  5855. + int err, i, j;
  5856. + struct au_dcsub_pages dpages;
  5857. + struct au_dpage *dpage;
  5858. + struct dentry **dentries;
  5859. +
  5860. + err = au_dpages_init(&dpages, GFP_NOFS);
  5861. + AuDebugOn(err);
  5862. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  5863. + AuDebugOn(err);
  5864. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  5865. + dpage = dpages.dpages + i;
  5866. + dentries = dpage->dentries;
  5867. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  5868. + AuDebugOn(au_digen_test(dentries[j], sigen));
  5869. + }
  5870. + au_dpages_free(&dpages);
  5871. +}
  5872. +
  5873. +void au_dbg_verify_kthread(void)
  5874. +{
  5875. + if (au_wkq_test()) {
  5876. + au_dbg_blocked();
  5877. + /*
  5878. + * It may be recursive, but udba=notify between two aufs mounts,
  5879. + * where a single ro branch is shared, is not a problem.
  5880. + */
  5881. + /* WARN_ON(1); */
  5882. + }
  5883. +}
  5884. +
  5885. +/* ---------------------------------------------------------------------- */
  5886. +
  5887. +int __init au_debug_init(void)
  5888. +{
  5889. + aufs_bindex_t bindex;
  5890. + struct au_vdir_destr destr;
  5891. +
  5892. + bindex = -1;
  5893. + AuDebugOn(bindex >= 0);
  5894. +
  5895. + destr.len = -1;
  5896. + AuDebugOn(destr.len < NAME_MAX);
  5897. +
  5898. +#ifdef CONFIG_4KSTACKS
  5899. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  5900. +#endif
  5901. +
  5902. + return 0;
  5903. +}
  5904. diff -Nur linux-4.0.4.orig/fs/aufs/debug.h linux-4.0.4/fs/aufs/debug.h
  5905. --- linux-4.0.4.orig/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
  5906. +++ linux-4.0.4/fs/aufs/debug.h 2015-05-30 22:11:30.000000000 +0200
  5907. @@ -0,0 +1,212 @@
  5908. +/*
  5909. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5910. + */
  5911. +
  5912. +/*
  5913. + * debug print functions
  5914. + */
  5915. +
  5916. +#ifndef __AUFS_DEBUG_H__
  5917. +#define __AUFS_DEBUG_H__
  5918. +
  5919. +#ifdef __KERNEL__
  5920. +
  5921. +#include <linux/atomic.h>
  5922. +#include <linux/module.h>
  5923. +#include <linux/kallsyms.h>
  5924. +#include <linux/sysrq.h>
  5925. +
  5926. +#ifdef CONFIG_AUFS_DEBUG
  5927. +#define AuDebugOn(a) BUG_ON(a)
  5928. +
  5929. +/* module parameter */
  5930. +extern atomic_t aufs_debug;
  5931. +static inline void au_debug_on(void)
  5932. +{
  5933. + atomic_inc(&aufs_debug);
  5934. +}
  5935. +static inline void au_debug_off(void)
  5936. +{
  5937. + atomic_dec_if_positive(&aufs_debug);
  5938. +}
  5939. +
  5940. +static inline int au_debug_test(void)
  5941. +{
  5942. + return atomic_read(&aufs_debug) > 0;
  5943. +}
  5944. +#else
  5945. +#define AuDebugOn(a) do {} while (0)
  5946. +AuStubVoid(au_debug_on, void)
  5947. +AuStubVoid(au_debug_off, void)
  5948. +AuStubInt0(au_debug_test, void)
  5949. +#endif /* CONFIG_AUFS_DEBUG */
  5950. +
  5951. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  5952. +
  5953. +/* ---------------------------------------------------------------------- */
  5954. +
  5955. +/* debug print */
  5956. +
  5957. +#define AuDbg(fmt, ...) do { \
  5958. + if (au_debug_test()) \
  5959. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  5960. +} while (0)
  5961. +#define AuLabel(l) AuDbg(#l "\n")
  5962. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  5963. +#define AuWarn1(fmt, ...) do { \
  5964. + static unsigned char _c; \
  5965. + if (!_c++) \
  5966. + pr_warn(fmt, ##__VA_ARGS__); \
  5967. +} while (0)
  5968. +
  5969. +#define AuErr1(fmt, ...) do { \
  5970. + static unsigned char _c; \
  5971. + if (!_c++) \
  5972. + pr_err(fmt, ##__VA_ARGS__); \
  5973. +} while (0)
  5974. +
  5975. +#define AuIOErr1(fmt, ...) do { \
  5976. + static unsigned char _c; \
  5977. + if (!_c++) \
  5978. + AuIOErr(fmt, ##__VA_ARGS__); \
  5979. +} while (0)
  5980. +
  5981. +#define AuUnsupportMsg "This operation is not supported." \
  5982. + " Please report this application to aufs-users ML."
  5983. +#define AuUnsupport(fmt, ...) do { \
  5984. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  5985. + dump_stack(); \
  5986. +} while (0)
  5987. +
  5988. +#define AuTraceErr(e) do { \
  5989. + if (unlikely((e) < 0)) \
  5990. + AuDbg("err %d\n", (int)(e)); \
  5991. +} while (0)
  5992. +
  5993. +#define AuTraceErrPtr(p) do { \
  5994. + if (IS_ERR(p)) \
  5995. + AuDbg("err %ld\n", PTR_ERR(p)); \
  5996. +} while (0)
  5997. +
  5998. +/* dirty macros for debug print, use with "%.*s" and caution */
  5999. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6000. +
  6001. +/* ---------------------------------------------------------------------- */
  6002. +
  6003. +struct dentry;
  6004. +#ifdef CONFIG_AUFS_DEBUG
  6005. +extern struct mutex au_dbg_mtx;
  6006. +extern char *au_plevel;
  6007. +struct au_nhash;
  6008. +void au_dpri_whlist(struct au_nhash *whlist);
  6009. +struct au_vdir;
  6010. +void au_dpri_vdir(struct au_vdir *vdir);
  6011. +struct inode;
  6012. +void au_dpri_inode(struct inode *inode);
  6013. +void au_dpri_dalias(struct inode *inode);
  6014. +void au_dpri_dentry(struct dentry *dentry);
  6015. +struct file;
  6016. +void au_dpri_file(struct file *filp);
  6017. +struct super_block;
  6018. +void au_dpri_sb(struct super_block *sb);
  6019. +
  6020. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6021. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6022. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6023. +void au_dbg_verify_kthread(void);
  6024. +
  6025. +int __init au_debug_init(void);
  6026. +
  6027. +#define AuDbgWhlist(w) do { \
  6028. + mutex_lock(&au_dbg_mtx); \
  6029. + AuDbg(#w "\n"); \
  6030. + au_dpri_whlist(w); \
  6031. + mutex_unlock(&au_dbg_mtx); \
  6032. +} while (0)
  6033. +
  6034. +#define AuDbgVdir(v) do { \
  6035. + mutex_lock(&au_dbg_mtx); \
  6036. + AuDbg(#v "\n"); \
  6037. + au_dpri_vdir(v); \
  6038. + mutex_unlock(&au_dbg_mtx); \
  6039. +} while (0)
  6040. +
  6041. +#define AuDbgInode(i) do { \
  6042. + mutex_lock(&au_dbg_mtx); \
  6043. + AuDbg(#i "\n"); \
  6044. + au_dpri_inode(i); \
  6045. + mutex_unlock(&au_dbg_mtx); \
  6046. +} while (0)
  6047. +
  6048. +#define AuDbgDAlias(i) do { \
  6049. + mutex_lock(&au_dbg_mtx); \
  6050. + AuDbg(#i "\n"); \
  6051. + au_dpri_dalias(i); \
  6052. + mutex_unlock(&au_dbg_mtx); \
  6053. +} while (0)
  6054. +
  6055. +#define AuDbgDentry(d) do { \
  6056. + mutex_lock(&au_dbg_mtx); \
  6057. + AuDbg(#d "\n"); \
  6058. + au_dpri_dentry(d); \
  6059. + mutex_unlock(&au_dbg_mtx); \
  6060. +} while (0)
  6061. +
  6062. +#define AuDbgFile(f) do { \
  6063. + mutex_lock(&au_dbg_mtx); \
  6064. + AuDbg(#f "\n"); \
  6065. + au_dpri_file(f); \
  6066. + mutex_unlock(&au_dbg_mtx); \
  6067. +} while (0)
  6068. +
  6069. +#define AuDbgSb(sb) do { \
  6070. + mutex_lock(&au_dbg_mtx); \
  6071. + AuDbg(#sb "\n"); \
  6072. + au_dpri_sb(sb); \
  6073. + mutex_unlock(&au_dbg_mtx); \
  6074. +} while (0)
  6075. +
  6076. +#define AuDbgSym(addr) do { \
  6077. + char sym[KSYM_SYMBOL_LEN]; \
  6078. + sprint_symbol(sym, (unsigned long)addr); \
  6079. + AuDbg("%s\n", sym); \
  6080. +} while (0)
  6081. +#else
  6082. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6083. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6084. +AuStubVoid(au_dbg_verify_kthread, void)
  6085. +AuStubInt0(__init au_debug_init, void)
  6086. +
  6087. +#define AuDbgWhlist(w) do {} while (0)
  6088. +#define AuDbgVdir(v) do {} while (0)
  6089. +#define AuDbgInode(i) do {} while (0)
  6090. +#define AuDbgDAlias(i) do {} while (0)
  6091. +#define AuDbgDentry(d) do {} while (0)
  6092. +#define AuDbgFile(f) do {} while (0)
  6093. +#define AuDbgSb(sb) do {} while (0)
  6094. +#define AuDbgSym(addr) do {} while (0)
  6095. +#endif /* CONFIG_AUFS_DEBUG */
  6096. +
  6097. +/* ---------------------------------------------------------------------- */
  6098. +
  6099. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  6100. +int __init au_sysrq_init(void);
  6101. +void au_sysrq_fin(void);
  6102. +
  6103. +#ifdef CONFIG_HW_CONSOLE
  6104. +#define au_dbg_blocked() do { \
  6105. + WARN_ON(1); \
  6106. + handle_sysrq('w'); \
  6107. +} while (0)
  6108. +#else
  6109. +AuStubVoid(au_dbg_blocked, void)
  6110. +#endif
  6111. +
  6112. +#else
  6113. +AuStubInt0(__init au_sysrq_init, void)
  6114. +AuStubVoid(au_sysrq_fin, void)
  6115. +AuStubVoid(au_dbg_blocked, void)
  6116. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  6117. +
  6118. +#endif /* __KERNEL__ */
  6119. +#endif /* __AUFS_DEBUG_H__ */
  6120. diff -Nur linux-4.0.4.orig/fs/aufs/dentry.c linux-4.0.4/fs/aufs/dentry.c
  6121. --- linux-4.0.4.orig/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
  6122. +++ linux-4.0.4/fs/aufs/dentry.c 2015-05-30 22:11:30.000000000 +0200
  6123. @@ -0,0 +1,1084 @@
  6124. +/*
  6125. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  6126. + */
  6127. +
  6128. +/*
  6129. + * lookup and dentry operations
  6130. + */
  6131. +
  6132. +#include <linux/namei.h>
  6133. +#include "aufs.h"
  6134. +
  6135. +#define AuLkup_ALLOW_NEG 1
  6136. +#define AuLkup_IGNORE_PERM (1 << 1)
  6137. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  6138. +#define au_fset_lkup(flags, name) \
  6139. + do { (flags) |= AuLkup_##name; } while (0)
  6140. +#define au_fclr_lkup(flags, name) \
  6141. + do { (flags) &= ~AuLkup_##name; } while (0)
  6142. +
  6143. +struct au_do_lookup_args {
  6144. + unsigned int flags;
  6145. + mode_t type;
  6146. +};
  6147. +
  6148. +/*
  6149. + * returns positive/negative dentry, NULL or an error.
  6150. + * NULL means whiteout-ed or not-found.
  6151. + */
  6152. +static struct dentry*
  6153. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  6154. + aufs_bindex_t bindex, struct qstr *wh_name,
  6155. + struct au_do_lookup_args *args)
  6156. +{
  6157. + struct dentry *h_dentry;
  6158. + struct inode *h_inode;
  6159. + struct au_branch *br;
  6160. + int wh_found, opq;
  6161. + unsigned char wh_able;
  6162. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  6163. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  6164. + IGNORE_PERM);
  6165. +
  6166. + wh_found = 0;
  6167. + br = au_sbr(dentry->d_sb, bindex);
  6168. + wh_able = !!au_br_whable(br->br_perm);
  6169. + if (wh_able)
  6170. + wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
  6171. + h_dentry = ERR_PTR(wh_found);
  6172. + if (!wh_found)
  6173. + goto real_lookup;
  6174. + if (unlikely(wh_found < 0))
  6175. + goto out;
  6176. +
  6177. + /* We found a whiteout */
  6178. + /* au_set_dbend(dentry, bindex); */
  6179. + au_set_dbwh(dentry, bindex);
  6180. + if (!allow_neg)
  6181. + return NULL; /* success */
  6182. +
  6183. +real_lookup:
  6184. + if (!ignore_perm)
  6185. + h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  6186. + else
  6187. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6188. + if (IS_ERR(h_dentry)) {
  6189. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  6190. + && !allow_neg)
  6191. + h_dentry = NULL;
  6192. + goto out;
  6193. + }
  6194. +
  6195. + h_inode = h_dentry->d_inode;
  6196. + if (!h_inode) {
  6197. + if (!allow_neg)
  6198. + goto out_neg;
  6199. + } else if (wh_found
  6200. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  6201. + goto out_neg;
  6202. +
  6203. + if (au_dbend(dentry) <= bindex)
  6204. + au_set_dbend(dentry, bindex);
  6205. + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
  6206. + au_set_dbstart(dentry, bindex);
  6207. + au_set_h_dptr(dentry, bindex, h_dentry);
  6208. +
  6209. + if (!d_is_dir(h_dentry)
  6210. + || !wh_able
  6211. + || (d_is_positive(dentry) && !d_is_dir(dentry)))
  6212. + goto out; /* success */
  6213. +
  6214. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  6215. + opq = au_diropq_test(h_dentry);
  6216. + mutex_unlock(&h_inode->i_mutex);
  6217. + if (opq > 0)
  6218. + au_set_dbdiropq(dentry, bindex);
  6219. + else if (unlikely(opq < 0)) {
  6220. + au_set_h_dptr(dentry, bindex, NULL);
  6221. + h_dentry = ERR_PTR(opq);
  6222. + }
  6223. + goto out;
  6224. +
  6225. +out_neg:
  6226. + dput(h_dentry);
  6227. + h_dentry = NULL;
  6228. +out:
  6229. + return h_dentry;
  6230. +}
  6231. +
  6232. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  6233. +{
  6234. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  6235. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  6236. + return -EPERM;
  6237. + return 0;
  6238. +}
  6239. +
  6240. +/*
  6241. + * returns the number of lower positive dentries,
  6242. + * otherwise an error.
  6243. + * can be called at unlinking with @type is zero.
  6244. + */
  6245. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
  6246. +{
  6247. + int npositive, err;
  6248. + aufs_bindex_t bindex, btail, bdiropq;
  6249. + unsigned char isdir, dirperm1;
  6250. + struct qstr whname;
  6251. + struct au_do_lookup_args args = {
  6252. + .flags = 0,
  6253. + .type = type
  6254. + };
  6255. + const struct qstr *name = &dentry->d_name;
  6256. + struct dentry *parent;
  6257. + struct inode *inode;
  6258. + struct super_block *sb;
  6259. +
  6260. + sb = dentry->d_sb;
  6261. + err = au_test_shwh(sb, name);
  6262. + if (unlikely(err))
  6263. + goto out;
  6264. +
  6265. + err = au_wh_name_alloc(&whname, name);
  6266. + if (unlikely(err))
  6267. + goto out;
  6268. +
  6269. + inode = dentry->d_inode;
  6270. + isdir = !!d_is_dir(dentry);
  6271. + if (!type)
  6272. + au_fset_lkup(args.flags, ALLOW_NEG);
  6273. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  6274. +
  6275. + npositive = 0;
  6276. + parent = dget_parent(dentry);
  6277. + btail = au_dbtaildir(parent);
  6278. + for (bindex = bstart; bindex <= btail; bindex++) {
  6279. + struct dentry *h_parent, *h_dentry;
  6280. + struct inode *h_inode, *h_dir;
  6281. +
  6282. + h_dentry = au_h_dptr(dentry, bindex);
  6283. + if (h_dentry) {
  6284. + if (h_dentry->d_inode)
  6285. + npositive++;
  6286. + if (type != S_IFDIR)
  6287. + break;
  6288. + continue;
  6289. + }
  6290. + h_parent = au_h_dptr(parent, bindex);
  6291. + if (!h_parent || !d_is_dir(h_parent))
  6292. + continue;
  6293. +
  6294. + h_dir = h_parent->d_inode;
  6295. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  6296. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
  6297. + &args);
  6298. + mutex_unlock(&h_dir->i_mutex);
  6299. + err = PTR_ERR(h_dentry);
  6300. + if (IS_ERR(h_dentry))
  6301. + goto out_parent;
  6302. + if (h_dentry)
  6303. + au_fclr_lkup(args.flags, ALLOW_NEG);
  6304. + if (dirperm1)
  6305. + au_fset_lkup(args.flags, IGNORE_PERM);
  6306. +
  6307. + if (au_dbwh(dentry) >= 0)
  6308. + break;
  6309. + if (!h_dentry)
  6310. + continue;
  6311. + h_inode = h_dentry->d_inode;
  6312. + if (!h_inode)
  6313. + continue;
  6314. + npositive++;
  6315. + if (!args.type)
  6316. + args.type = h_inode->i_mode & S_IFMT;
  6317. + if (args.type != S_IFDIR)
  6318. + break;
  6319. + else if (isdir) {
  6320. + /* the type of lower may be different */
  6321. + bdiropq = au_dbdiropq(dentry);
  6322. + if (bdiropq >= 0 && bdiropq <= bindex)
  6323. + break;
  6324. + }
  6325. + }
  6326. +
  6327. + if (npositive) {
  6328. + AuLabel(positive);
  6329. + au_update_dbstart(dentry);
  6330. + }
  6331. + err = npositive;
  6332. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  6333. + && au_dbstart(dentry) < 0)) {
  6334. + err = -EIO;
  6335. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  6336. + dentry, err);
  6337. + }
  6338. +
  6339. +out_parent:
  6340. + dput(parent);
  6341. + kfree(whname.name);
  6342. +out:
  6343. + return err;
  6344. +}
  6345. +
  6346. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  6347. +{
  6348. + struct dentry *dentry;
  6349. + int wkq_err;
  6350. +
  6351. + if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
  6352. + dentry = vfsub_lkup_one(name, parent);
  6353. + else {
  6354. + struct vfsub_lkup_one_args args = {
  6355. + .errp = &dentry,
  6356. + .name = name,
  6357. + .parent = parent
  6358. + };
  6359. +
  6360. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  6361. + if (unlikely(wkq_err))
  6362. + dentry = ERR_PTR(wkq_err);
  6363. + }
  6364. +
  6365. + return dentry;
  6366. +}
  6367. +
  6368. +/*
  6369. + * lookup @dentry on @bindex which should be negative.
  6370. + */
  6371. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  6372. +{
  6373. + int err;
  6374. + struct dentry *parent, *h_parent, *h_dentry;
  6375. + struct au_branch *br;
  6376. +
  6377. + parent = dget_parent(dentry);
  6378. + h_parent = au_h_dptr(parent, bindex);
  6379. + br = au_sbr(dentry->d_sb, bindex);
  6380. + if (wh)
  6381. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  6382. + else
  6383. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6384. + err = PTR_ERR(h_dentry);
  6385. + if (IS_ERR(h_dentry))
  6386. + goto out;
  6387. + if (unlikely(h_dentry->d_inode)) {
  6388. + err = -EIO;
  6389. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  6390. + dput(h_dentry);
  6391. + goto out;
  6392. + }
  6393. +
  6394. + err = 0;
  6395. + if (bindex < au_dbstart(dentry))
  6396. + au_set_dbstart(dentry, bindex);
  6397. + if (au_dbend(dentry) < bindex)
  6398. + au_set_dbend(dentry, bindex);
  6399. + au_set_h_dptr(dentry, bindex, h_dentry);
  6400. +
  6401. +out:
  6402. + dput(parent);
  6403. + return err;
  6404. +}
  6405. +
  6406. +/* ---------------------------------------------------------------------- */
  6407. +
  6408. +/* subset of struct inode */
  6409. +struct au_iattr {
  6410. + unsigned long i_ino;
  6411. + /* unsigned int i_nlink; */
  6412. + kuid_t i_uid;
  6413. + kgid_t i_gid;
  6414. + u64 i_version;
  6415. +/*
  6416. + loff_t i_size;
  6417. + blkcnt_t i_blocks;
  6418. +*/
  6419. + umode_t i_mode;
  6420. +};
  6421. +
  6422. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  6423. +{
  6424. + ia->i_ino = h_inode->i_ino;
  6425. + /* ia->i_nlink = h_inode->i_nlink; */
  6426. + ia->i_uid = h_inode->i_uid;
  6427. + ia->i_gid = h_inode->i_gid;
  6428. + ia->i_version = h_inode->i_version;
  6429. +/*
  6430. + ia->i_size = h_inode->i_size;
  6431. + ia->i_blocks = h_inode->i_blocks;
  6432. +*/
  6433. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  6434. +}
  6435. +
  6436. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  6437. +{
  6438. + return ia->i_ino != h_inode->i_ino
  6439. + /* || ia->i_nlink != h_inode->i_nlink */
  6440. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  6441. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  6442. + || ia->i_version != h_inode->i_version
  6443. +/*
  6444. + || ia->i_size != h_inode->i_size
  6445. + || ia->i_blocks != h_inode->i_blocks
  6446. +*/
  6447. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  6448. +}
  6449. +
  6450. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  6451. + struct au_branch *br)
  6452. +{
  6453. + int err;
  6454. + struct au_iattr ia;
  6455. + struct inode *h_inode;
  6456. + struct dentry *h_d;
  6457. + struct super_block *h_sb;
  6458. +
  6459. + err = 0;
  6460. + memset(&ia, -1, sizeof(ia));
  6461. + h_sb = h_dentry->d_sb;
  6462. + h_inode = h_dentry->d_inode;
  6463. + if (h_inode)
  6464. + au_iattr_save(&ia, h_inode);
  6465. + else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  6466. + /* nfs d_revalidate may return 0 for negative dentry */
  6467. + /* fuse d_revalidate always return 0 for negative dentry */
  6468. + goto out;
  6469. +
  6470. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  6471. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  6472. + err = PTR_ERR(h_d);
  6473. + if (IS_ERR(h_d))
  6474. + goto out;
  6475. +
  6476. + err = 0;
  6477. + if (unlikely(h_d != h_dentry
  6478. + || h_d->d_inode != h_inode
  6479. + || (h_inode && au_iattr_test(&ia, h_inode))))
  6480. + err = au_busy_or_stale();
  6481. + dput(h_d);
  6482. +
  6483. +out:
  6484. + AuTraceErr(err);
  6485. + return err;
  6486. +}
  6487. +
  6488. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  6489. + struct dentry *h_parent, struct au_branch *br)
  6490. +{
  6491. + int err;
  6492. +
  6493. + err = 0;
  6494. + if (udba == AuOpt_UDBA_REVAL
  6495. + && !au_test_fs_remote(h_dentry->d_sb)) {
  6496. + IMustLock(h_dir);
  6497. + err = (h_dentry->d_parent->d_inode != h_dir);
  6498. + } else if (udba != AuOpt_UDBA_NONE)
  6499. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  6500. +
  6501. + return err;
  6502. +}
  6503. +
  6504. +/* ---------------------------------------------------------------------- */
  6505. +
  6506. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  6507. +{
  6508. + int err;
  6509. + aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
  6510. + struct au_hdentry tmp, *p, *q;
  6511. + struct au_dinfo *dinfo;
  6512. + struct super_block *sb;
  6513. +
  6514. + DiMustWriteLock(dentry);
  6515. +
  6516. + sb = dentry->d_sb;
  6517. + dinfo = au_di(dentry);
  6518. + bend = dinfo->di_bend;
  6519. + bwh = dinfo->di_bwh;
  6520. + bdiropq = dinfo->di_bdiropq;
  6521. + p = dinfo->di_hdentry + dinfo->di_bstart;
  6522. + for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
  6523. + if (!p->hd_dentry)
  6524. + continue;
  6525. +
  6526. + new_bindex = au_br_index(sb, p->hd_id);
  6527. + if (new_bindex == bindex)
  6528. + continue;
  6529. +
  6530. + if (dinfo->di_bwh == bindex)
  6531. + bwh = new_bindex;
  6532. + if (dinfo->di_bdiropq == bindex)
  6533. + bdiropq = new_bindex;
  6534. + if (new_bindex < 0) {
  6535. + au_hdput(p);
  6536. + p->hd_dentry = NULL;
  6537. + continue;
  6538. + }
  6539. +
  6540. + /* swap two lower dentries, and loop again */
  6541. + q = dinfo->di_hdentry + new_bindex;
  6542. + tmp = *q;
  6543. + *q = *p;
  6544. + *p = tmp;
  6545. + if (tmp.hd_dentry) {
  6546. + bindex--;
  6547. + p--;
  6548. + }
  6549. + }
  6550. +
  6551. + dinfo->di_bwh = -1;
  6552. + if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
  6553. + dinfo->di_bwh = bwh;
  6554. +
  6555. + dinfo->di_bdiropq = -1;
  6556. + if (bdiropq >= 0
  6557. + && bdiropq <= au_sbend(sb)
  6558. + && au_sbr_whable(sb, bdiropq))
  6559. + dinfo->di_bdiropq = bdiropq;
  6560. +
  6561. + err = -EIO;
  6562. + dinfo->di_bstart = -1;
  6563. + dinfo->di_bend = -1;
  6564. + bend = au_dbend(parent);
  6565. + p = dinfo->di_hdentry;
  6566. + for (bindex = 0; bindex <= bend; bindex++, p++)
  6567. + if (p->hd_dentry) {
  6568. + dinfo->di_bstart = bindex;
  6569. + break;
  6570. + }
  6571. +
  6572. + if (dinfo->di_bstart >= 0) {
  6573. + p = dinfo->di_hdentry + bend;
  6574. + for (bindex = bend; bindex >= 0; bindex--, p--)
  6575. + if (p->hd_dentry) {
  6576. + dinfo->di_bend = bindex;
  6577. + err = 0;
  6578. + break;
  6579. + }
  6580. + }
  6581. +
  6582. + return err;
  6583. +}
  6584. +
  6585. +static void au_do_hide(struct dentry *dentry)
  6586. +{
  6587. + struct inode *inode;
  6588. +
  6589. + inode = dentry->d_inode;
  6590. + if (inode) {
  6591. + if (!S_ISDIR(inode->i_mode)) {
  6592. + if (inode->i_nlink && !d_unhashed(dentry))
  6593. + drop_nlink(inode);
  6594. + } else {
  6595. + clear_nlink(inode);
  6596. + /* stop next lookup */
  6597. + inode->i_flags |= S_DEAD;
  6598. + }
  6599. + smp_mb(); /* necessary? */
  6600. + }
  6601. + d_drop(dentry);
  6602. +}
  6603. +
  6604. +static int au_hide_children(struct dentry *parent)
  6605. +{
  6606. + int err, i, j, ndentry;
  6607. + struct au_dcsub_pages dpages;
  6608. + struct au_dpage *dpage;
  6609. + struct dentry *dentry;
  6610. +
  6611. + err = au_dpages_init(&dpages, GFP_NOFS);
  6612. + if (unlikely(err))
  6613. + goto out;
  6614. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  6615. + if (unlikely(err))
  6616. + goto out_dpages;
  6617. +
  6618. + /* in reverse order */
  6619. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  6620. + dpage = dpages.dpages + i;
  6621. + ndentry = dpage->ndentry;
  6622. + for (j = ndentry - 1; j >= 0; j--) {
  6623. + dentry = dpage->dentries[j];
  6624. + if (dentry != parent)
  6625. + au_do_hide(dentry);
  6626. + }
  6627. + }
  6628. +
  6629. +out_dpages:
  6630. + au_dpages_free(&dpages);
  6631. +out:
  6632. + return err;
  6633. +}
  6634. +
  6635. +static void au_hide(struct dentry *dentry)
  6636. +{
  6637. + int err;
  6638. +
  6639. + AuDbgDentry(dentry);
  6640. + if (d_is_dir(dentry)) {
  6641. + /* shrink_dcache_parent(dentry); */
  6642. + err = au_hide_children(dentry);
  6643. + if (unlikely(err))
  6644. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  6645. + dentry, err);
  6646. + }
  6647. + au_do_hide(dentry);
  6648. +}
  6649. +
  6650. +/*
  6651. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  6652. + *
  6653. + * a dirty branch is added
  6654. + * - on the top of layers
  6655. + * - in the middle of layers
  6656. + * - to the bottom of layers
  6657. + *
  6658. + * on the added branch there exists
  6659. + * - a whiteout
  6660. + * - a diropq
  6661. + * - a same named entry
  6662. + * + exist
  6663. + * * negative --> positive
  6664. + * * positive --> positive
  6665. + * - type is unchanged
  6666. + * - type is changed
  6667. + * + doesn't exist
  6668. + * * negative --> negative
  6669. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  6670. + * - none
  6671. + */
  6672. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  6673. + struct au_dinfo *tmp)
  6674. +{
  6675. + int err;
  6676. + aufs_bindex_t bindex, bend;
  6677. + struct {
  6678. + struct dentry *dentry;
  6679. + struct inode *inode;
  6680. + mode_t mode;
  6681. + } orig_h, tmp_h;
  6682. + struct au_hdentry *hd;
  6683. + struct inode *inode, *h_inode;
  6684. + struct dentry *h_dentry;
  6685. +
  6686. + err = 0;
  6687. + AuDebugOn(dinfo->di_bstart < 0);
  6688. + orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
  6689. + orig_h.inode = orig_h.dentry->d_inode;
  6690. + orig_h.mode = 0;
  6691. + if (orig_h.inode)
  6692. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  6693. + memset(&tmp_h, 0, sizeof(tmp_h));
  6694. + if (tmp->di_bstart >= 0) {
  6695. + tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
  6696. + tmp_h.inode = tmp_h.dentry->d_inode;
  6697. + if (tmp_h.inode)
  6698. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  6699. + }
  6700. +
  6701. + inode = dentry->d_inode;
  6702. + if (!orig_h.inode) {
  6703. + AuDbg("nagative originally\n");
  6704. + if (inode) {
  6705. + au_hide(dentry);
  6706. + goto out;
  6707. + }
  6708. + AuDebugOn(inode);
  6709. + AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
  6710. + AuDebugOn(dinfo->di_bdiropq != -1);
  6711. +
  6712. + if (!tmp_h.inode) {
  6713. + AuDbg("negative --> negative\n");
  6714. + /* should have only one negative lower */
  6715. + if (tmp->di_bstart >= 0
  6716. + && tmp->di_bstart < dinfo->di_bstart) {
  6717. + AuDebugOn(tmp->di_bstart != tmp->di_bend);
  6718. + AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
  6719. + au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
  6720. + au_di_cp(dinfo, tmp);
  6721. + hd = tmp->di_hdentry + tmp->di_bstart;
  6722. + au_set_h_dptr(dentry, tmp->di_bstart,
  6723. + dget(hd->hd_dentry));
  6724. + }
  6725. + au_dbg_verify_dinode(dentry);
  6726. + } else {
  6727. + AuDbg("negative --> positive\n");
  6728. + /*
  6729. + * similar to the behaviour of creating with bypassing
  6730. + * aufs.
  6731. + * unhash it in order to force an error in the
  6732. + * succeeding create operation.
  6733. + * we should not set S_DEAD here.
  6734. + */
  6735. + d_drop(dentry);
  6736. + /* au_di_swap(tmp, dinfo); */
  6737. + au_dbg_verify_dinode(dentry);
  6738. + }
  6739. + } else {
  6740. + AuDbg("positive originally\n");
  6741. + /* inode may be NULL */
  6742. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  6743. + if (!tmp_h.inode) {
  6744. + AuDbg("positive --> negative\n");
  6745. + /* or bypassing aufs */
  6746. + au_hide(dentry);
  6747. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
  6748. + dinfo->di_bwh = tmp->di_bwh;
  6749. + if (inode)
  6750. + err = au_refresh_hinode_self(inode);
  6751. + au_dbg_verify_dinode(dentry);
  6752. + } else if (orig_h.mode == tmp_h.mode) {
  6753. + AuDbg("positive --> positive, same type\n");
  6754. + if (!S_ISDIR(orig_h.mode)
  6755. + && dinfo->di_bstart > tmp->di_bstart) {
  6756. + /*
  6757. + * similar to the behaviour of removing and
  6758. + * creating.
  6759. + */
  6760. + au_hide(dentry);
  6761. + if (inode)
  6762. + err = au_refresh_hinode_self(inode);
  6763. + au_dbg_verify_dinode(dentry);
  6764. + } else {
  6765. + /* fill empty slots */
  6766. + if (dinfo->di_bstart > tmp->di_bstart)
  6767. + dinfo->di_bstart = tmp->di_bstart;
  6768. + if (dinfo->di_bend < tmp->di_bend)
  6769. + dinfo->di_bend = tmp->di_bend;
  6770. + dinfo->di_bwh = tmp->di_bwh;
  6771. + dinfo->di_bdiropq = tmp->di_bdiropq;
  6772. + hd = tmp->di_hdentry;
  6773. + bend = dinfo->di_bend;
  6774. + for (bindex = tmp->di_bstart; bindex <= bend;
  6775. + bindex++) {
  6776. + if (au_h_dptr(dentry, bindex))
  6777. + continue;
  6778. + h_dentry = hd[bindex].hd_dentry;
  6779. + if (!h_dentry)
  6780. + continue;
  6781. + h_inode = h_dentry->d_inode;
  6782. + AuDebugOn(!h_inode);
  6783. + AuDebugOn(orig_h.mode
  6784. + != (h_inode->i_mode
  6785. + & S_IFMT));
  6786. + au_set_h_dptr(dentry, bindex,
  6787. + dget(h_dentry));
  6788. + }
  6789. + err = au_refresh_hinode(inode, dentry);
  6790. + au_dbg_verify_dinode(dentry);
  6791. + }
  6792. + } else {
  6793. + AuDbg("positive --> positive, different type\n");
  6794. + /* similar to the behaviour of removing and creating */
  6795. + au_hide(dentry);
  6796. + if (inode)
  6797. + err = au_refresh_hinode_self(inode);
  6798. + au_dbg_verify_dinode(dentry);
  6799. + }
  6800. + }
  6801. +
  6802. +out:
  6803. + return err;
  6804. +}
  6805. +
  6806. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  6807. +{
  6808. + int err, ebrange;
  6809. + unsigned int sigen;
  6810. + struct au_dinfo *dinfo, *tmp;
  6811. + struct super_block *sb;
  6812. + struct inode *inode;
  6813. +
  6814. + DiMustWriteLock(dentry);
  6815. + AuDebugOn(IS_ROOT(dentry));
  6816. + AuDebugOn(!parent->d_inode);
  6817. +
  6818. + sb = dentry->d_sb;
  6819. + inode = dentry->d_inode;
  6820. + sigen = au_sigen(sb);
  6821. + err = au_digen_test(parent, sigen);
  6822. + if (unlikely(err))
  6823. + goto out;
  6824. +
  6825. + dinfo = au_di(dentry);
  6826. + err = au_di_realloc(dinfo, au_sbend(sb) + 1);
  6827. + if (unlikely(err))
  6828. + goto out;
  6829. + ebrange = au_dbrange_test(dentry);
  6830. + if (!ebrange)
  6831. + ebrange = au_do_refresh_hdentry(dentry, parent);
  6832. +
  6833. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  6834. + AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
  6835. + if (inode)
  6836. + err = au_refresh_hinode_self(inode);
  6837. + au_dbg_verify_dinode(dentry);
  6838. + if (!err)
  6839. + goto out_dgen; /* success */
  6840. + goto out;
  6841. + }
  6842. +
  6843. + /* temporary dinfo */
  6844. + AuDbgDentry(dentry);
  6845. + err = -ENOMEM;
  6846. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  6847. + if (unlikely(!tmp))
  6848. + goto out;
  6849. + au_di_swap(tmp, dinfo);
  6850. + /* returns the number of positive dentries */
  6851. + /*
  6852. + * if current working dir is removed, it returns an error.
  6853. + * but the dentry is legal.
  6854. + */
  6855. + err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
  6856. + AuDbgDentry(dentry);
  6857. + au_di_swap(tmp, dinfo);
  6858. + if (err == -ENOENT)
  6859. + err = 0;
  6860. + if (err >= 0) {
  6861. + /* compare/refresh by dinfo */
  6862. + AuDbgDentry(dentry);
  6863. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  6864. + au_dbg_verify_dinode(dentry);
  6865. + AuTraceErr(err);
  6866. + }
  6867. + au_rw_write_unlock(&tmp->di_rwsem);
  6868. + au_di_free(tmp);
  6869. + if (unlikely(err))
  6870. + goto out;
  6871. +
  6872. +out_dgen:
  6873. + au_update_digen(dentry);
  6874. +out:
  6875. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  6876. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  6877. + AuDbgDentry(dentry);
  6878. + }
  6879. + AuTraceErr(err);
  6880. + return err;
  6881. +}
  6882. +
  6883. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  6884. + struct dentry *dentry, aufs_bindex_t bindex)
  6885. +{
  6886. + int err, valid;
  6887. +
  6888. + err = 0;
  6889. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  6890. + goto out;
  6891. +
  6892. + AuDbg("b%d\n", bindex);
  6893. + /*
  6894. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  6895. + * due to whiteout and branch permission.
  6896. + */
  6897. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  6898. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  6899. + /* it may return tri-state */
  6900. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  6901. +
  6902. + if (unlikely(valid < 0))
  6903. + err = valid;
  6904. + else if (!valid)
  6905. + err = -EINVAL;
  6906. +
  6907. +out:
  6908. + AuTraceErr(err);
  6909. + return err;
  6910. +}
  6911. +
  6912. +/* todo: remove this */
  6913. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  6914. + unsigned int flags, int do_udba)
  6915. +{
  6916. + int err;
  6917. + umode_t mode, h_mode;
  6918. + aufs_bindex_t bindex, btail, bstart, ibs, ibe;
  6919. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  6920. + struct inode *h_inode, *h_cached_inode;
  6921. + struct dentry *h_dentry;
  6922. + struct qstr *name, *h_name;
  6923. +
  6924. + err = 0;
  6925. + plus = 0;
  6926. + mode = 0;
  6927. + ibs = -1;
  6928. + ibe = -1;
  6929. + unhashed = !!d_unhashed(dentry);
  6930. + is_root = !!IS_ROOT(dentry);
  6931. + name = &dentry->d_name;
  6932. + tmpfile = au_di(dentry)->di_tmpfile;
  6933. +
  6934. + /*
  6935. + * Theoretically, REVAL test should be unnecessary in case of
  6936. + * {FS,I}NOTIFY.
  6937. + * But {fs,i}notify doesn't fire some necessary events,
  6938. + * IN_ATTRIB for atime/nlink/pageio
  6939. + * Let's do REVAL test too.
  6940. + */
  6941. + if (do_udba && inode) {
  6942. + mode = (inode->i_mode & S_IFMT);
  6943. + plus = (inode->i_nlink > 0);
  6944. + ibs = au_ibstart(inode);
  6945. + ibe = au_ibend(inode);
  6946. + }
  6947. +
  6948. + bstart = au_dbstart(dentry);
  6949. + btail = bstart;
  6950. + if (inode && S_ISDIR(inode->i_mode))
  6951. + btail = au_dbtaildir(dentry);
  6952. + for (bindex = bstart; bindex <= btail; bindex++) {
  6953. + h_dentry = au_h_dptr(dentry, bindex);
  6954. + if (!h_dentry)
  6955. + continue;
  6956. +
  6957. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  6958. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  6959. + spin_lock(&h_dentry->d_lock);
  6960. + h_name = &h_dentry->d_name;
  6961. + if (unlikely(do_udba
  6962. + && !is_root
  6963. + && ((!h_nfs
  6964. + && (unhashed != !!d_unhashed(h_dentry)
  6965. + || (!tmpfile
  6966. + && !au_qstreq(name, h_name))
  6967. + ))
  6968. + || (h_nfs
  6969. + && !(flags & LOOKUP_OPEN)
  6970. + && (h_dentry->d_flags
  6971. + & DCACHE_NFSFS_RENAMED)))
  6972. + )) {
  6973. + int h_unhashed;
  6974. +
  6975. + h_unhashed = d_unhashed(h_dentry);
  6976. + spin_unlock(&h_dentry->d_lock);
  6977. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  6978. + unhashed, h_unhashed, dentry, h_dentry);
  6979. + goto err;
  6980. + }
  6981. + spin_unlock(&h_dentry->d_lock);
  6982. +
  6983. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  6984. + if (unlikely(err))
  6985. + /* do not goto err, to keep the errno */
  6986. + break;
  6987. +
  6988. + /* todo: plink too? */
  6989. + if (!do_udba)
  6990. + continue;
  6991. +
  6992. + /* UDBA tests */
  6993. + h_inode = h_dentry->d_inode;
  6994. + if (unlikely(!!inode != !!h_inode))
  6995. + goto err;
  6996. +
  6997. + h_plus = plus;
  6998. + h_mode = mode;
  6999. + h_cached_inode = h_inode;
  7000. + if (h_inode) {
  7001. + h_mode = (h_inode->i_mode & S_IFMT);
  7002. + h_plus = (h_inode->i_nlink > 0);
  7003. + }
  7004. + if (inode && ibs <= bindex && bindex <= ibe)
  7005. + h_cached_inode = au_h_iptr(inode, bindex);
  7006. +
  7007. + if (!h_nfs) {
  7008. + if (unlikely(plus != h_plus && !tmpfile))
  7009. + goto err;
  7010. + } else {
  7011. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7012. + && !is_root
  7013. + && !IS_ROOT(h_dentry)
  7014. + && unhashed != d_unhashed(h_dentry)))
  7015. + goto err;
  7016. + }
  7017. + if (unlikely(mode != h_mode
  7018. + || h_cached_inode != h_inode))
  7019. + goto err;
  7020. + continue;
  7021. +
  7022. +err:
  7023. + err = -EINVAL;
  7024. + break;
  7025. + }
  7026. +
  7027. + AuTraceErr(err);
  7028. + return err;
  7029. +}
  7030. +
  7031. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  7032. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7033. +{
  7034. + int err;
  7035. + struct dentry *parent;
  7036. +
  7037. + if (!au_digen_test(dentry, sigen))
  7038. + return 0;
  7039. +
  7040. + parent = dget_parent(dentry);
  7041. + di_read_lock_parent(parent, AuLock_IR);
  7042. + AuDebugOn(au_digen_test(parent, sigen));
  7043. + au_dbg_verify_gen(parent, sigen);
  7044. + err = au_refresh_dentry(dentry, parent);
  7045. + di_read_unlock(parent, AuLock_IR);
  7046. + dput(parent);
  7047. + AuTraceErr(err);
  7048. + return err;
  7049. +}
  7050. +
  7051. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7052. +{
  7053. + int err;
  7054. + struct dentry *d, *parent;
  7055. + struct inode *inode;
  7056. +
  7057. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  7058. + return simple_reval_dpath(dentry, sigen);
  7059. +
  7060. + /* slow loop, keep it simple and stupid */
  7061. + /* cf: au_cpup_dirs() */
  7062. + err = 0;
  7063. + parent = NULL;
  7064. + while (au_digen_test(dentry, sigen)) {
  7065. + d = dentry;
  7066. + while (1) {
  7067. + dput(parent);
  7068. + parent = dget_parent(d);
  7069. + if (!au_digen_test(parent, sigen))
  7070. + break;
  7071. + d = parent;
  7072. + }
  7073. +
  7074. + inode = d->d_inode;
  7075. + if (d != dentry)
  7076. + di_write_lock_child2(d);
  7077. +
  7078. + /* someone might update our dentry while we were sleeping */
  7079. + if (au_digen_test(d, sigen)) {
  7080. + /*
  7081. + * todo: consolidate with simple_reval_dpath(),
  7082. + * do_refresh() and au_reval_for_attr().
  7083. + */
  7084. + di_read_lock_parent(parent, AuLock_IR);
  7085. + err = au_refresh_dentry(d, parent);
  7086. + di_read_unlock(parent, AuLock_IR);
  7087. + }
  7088. +
  7089. + if (d != dentry)
  7090. + di_write_unlock(d);
  7091. + dput(parent);
  7092. + if (unlikely(err))
  7093. + break;
  7094. + }
  7095. +
  7096. + return err;
  7097. +}
  7098. +
  7099. +/*
  7100. + * if valid returns 1, otherwise 0.
  7101. + */
  7102. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  7103. +{
  7104. + int valid, err;
  7105. + unsigned int sigen;
  7106. + unsigned char do_udba;
  7107. + struct super_block *sb;
  7108. + struct inode *inode;
  7109. +
  7110. + /* todo: support rcu-walk? */
  7111. + if (flags & LOOKUP_RCU)
  7112. + return -ECHILD;
  7113. +
  7114. + valid = 0;
  7115. + if (unlikely(!au_di(dentry)))
  7116. + goto out;
  7117. +
  7118. + valid = 1;
  7119. + sb = dentry->d_sb;
  7120. + /*
  7121. + * todo: very ugly
  7122. + * i_mutex of parent dir may be held,
  7123. + * but we should not return 'invalid' due to busy.
  7124. + */
  7125. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  7126. + if (unlikely(err)) {
  7127. + valid = err;
  7128. + AuTraceErr(err);
  7129. + goto out;
  7130. + }
  7131. + inode = dentry->d_inode;
  7132. + if (unlikely(inode && is_bad_inode(inode))) {
  7133. + err = -EINVAL;
  7134. + AuTraceErr(err);
  7135. + goto out_dgrade;
  7136. + }
  7137. + if (unlikely(au_dbrange_test(dentry))) {
  7138. + err = -EINVAL;
  7139. + AuTraceErr(err);
  7140. + goto out_dgrade;
  7141. + }
  7142. +
  7143. + sigen = au_sigen(sb);
  7144. + if (au_digen_test(dentry, sigen)) {
  7145. + AuDebugOn(IS_ROOT(dentry));
  7146. + err = au_reval_dpath(dentry, sigen);
  7147. + if (unlikely(err)) {
  7148. + AuTraceErr(err);
  7149. + goto out_dgrade;
  7150. + }
  7151. + }
  7152. + di_downgrade_lock(dentry, AuLock_IR);
  7153. +
  7154. + err = -EINVAL;
  7155. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  7156. + && inode
  7157. + && !(inode->i_state && I_LINKABLE)
  7158. + && (IS_DEADDIR(inode) || !inode->i_nlink))
  7159. + goto out_inval;
  7160. +
  7161. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  7162. + if (do_udba && inode) {
  7163. + aufs_bindex_t bstart = au_ibstart(inode);
  7164. + struct inode *h_inode;
  7165. +
  7166. + if (bstart >= 0) {
  7167. + h_inode = au_h_iptr(inode, bstart);
  7168. + if (h_inode && au_test_higen(inode, h_inode))
  7169. + goto out_inval;
  7170. + }
  7171. + }
  7172. +
  7173. + err = h_d_revalidate(dentry, inode, flags, do_udba);
  7174. + if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
  7175. + err = -EIO;
  7176. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  7177. + dentry, err);
  7178. + }
  7179. + goto out_inval;
  7180. +
  7181. +out_dgrade:
  7182. + di_downgrade_lock(dentry, AuLock_IR);
  7183. +out_inval:
  7184. + aufs_read_unlock(dentry, AuLock_IR);
  7185. + AuTraceErr(err);
  7186. + valid = !err;
  7187. +out:
  7188. + if (!valid) {
  7189. + AuDbg("%pd invalid, %d\n", dentry, valid);
  7190. + d_drop(dentry);
  7191. + }
  7192. + return valid;
  7193. +}
  7194. +
  7195. +static void aufs_d_release(struct dentry *dentry)
  7196. +{
  7197. + if (au_di(dentry)) {
  7198. + au_di_fin(dentry);
  7199. + au_hn_di_reinit(dentry);
  7200. + }
  7201. +}
  7202. +
  7203. +const struct dentry_operations aufs_dop = {
  7204. + .d_revalidate = aufs_d_revalidate,
  7205. + .d_weak_revalidate = aufs_d_revalidate,
  7206. + .d_release = aufs_d_release
  7207. +};
  7208. diff -Nur linux-4.0.4.orig/fs/aufs/dentry.h linux-4.0.4/fs/aufs/dentry.h
  7209. --- linux-4.0.4.orig/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
  7210. +++ linux-4.0.4/fs/aufs/dentry.h 2015-05-30 22:11:30.000000000 +0200
  7211. @@ -0,0 +1,220 @@
  7212. +/*
  7213. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7214. + */
  7215. +
  7216. +/*
  7217. + * lookup and dentry operations
  7218. + */
  7219. +
  7220. +#ifndef __AUFS_DENTRY_H__
  7221. +#define __AUFS_DENTRY_H__
  7222. +
  7223. +#ifdef __KERNEL__
  7224. +
  7225. +#include <linux/dcache.h>
  7226. +#include "rwsem.h"
  7227. +
  7228. +struct au_hdentry {
  7229. + struct dentry *hd_dentry;
  7230. + aufs_bindex_t hd_id;
  7231. +};
  7232. +
  7233. +struct au_dinfo {
  7234. + atomic_t di_generation;
  7235. +
  7236. + struct au_rwsem di_rwsem;
  7237. + aufs_bindex_t di_bstart, di_bend, di_bwh, di_bdiropq;
  7238. + unsigned char di_tmpfile; /* to allow the different name */
  7239. + struct au_hdentry *di_hdentry;
  7240. +} ____cacheline_aligned_in_smp;
  7241. +
  7242. +/* ---------------------------------------------------------------------- */
  7243. +
  7244. +/* dentry.c */
  7245. +extern const struct dentry_operations aufs_dop;
  7246. +struct au_branch;
  7247. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  7248. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7249. + struct dentry *h_parent, struct au_branch *br);
  7250. +
  7251. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
  7252. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  7253. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  7254. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  7255. +
  7256. +/* dinfo.c */
  7257. +void au_di_init_once(void *_di);
  7258. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  7259. +void au_di_free(struct au_dinfo *dinfo);
  7260. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  7261. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  7262. +int au_di_init(struct dentry *dentry);
  7263. +void au_di_fin(struct dentry *dentry);
  7264. +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
  7265. +
  7266. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  7267. +void di_read_unlock(struct dentry *d, int flags);
  7268. +void di_downgrade_lock(struct dentry *d, int flags);
  7269. +void di_write_lock(struct dentry *d, unsigned int lsc);
  7270. +void di_write_unlock(struct dentry *d);
  7271. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  7272. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  7273. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  7274. +
  7275. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  7276. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  7277. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  7278. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  7279. +
  7280. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  7281. + struct dentry *h_dentry);
  7282. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  7283. +int au_dbrange_test(struct dentry *dentry);
  7284. +void au_update_digen(struct dentry *dentry);
  7285. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  7286. +void au_update_dbstart(struct dentry *dentry);
  7287. +void au_update_dbend(struct dentry *dentry);
  7288. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  7289. +
  7290. +/* ---------------------------------------------------------------------- */
  7291. +
  7292. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  7293. +{
  7294. + return dentry->d_fsdata;
  7295. +}
  7296. +
  7297. +/* ---------------------------------------------------------------------- */
  7298. +
  7299. +/* lock subclass for dinfo */
  7300. +enum {
  7301. + AuLsc_DI_CHILD, /* child first */
  7302. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  7303. + AuLsc_DI_CHILD3, /* copyup dirs */
  7304. + AuLsc_DI_PARENT,
  7305. + AuLsc_DI_PARENT2,
  7306. + AuLsc_DI_PARENT3,
  7307. + AuLsc_DI_TMP /* temp for replacing dinfo */
  7308. +};
  7309. +
  7310. +/*
  7311. + * di_read_lock_child, di_write_lock_child,
  7312. + * di_read_lock_child2, di_write_lock_child2,
  7313. + * di_read_lock_child3, di_write_lock_child3,
  7314. + * di_read_lock_parent, di_write_lock_parent,
  7315. + * di_read_lock_parent2, di_write_lock_parent2,
  7316. + * di_read_lock_parent3, di_write_lock_parent3,
  7317. + */
  7318. +#define AuReadLockFunc(name, lsc) \
  7319. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  7320. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  7321. +
  7322. +#define AuWriteLockFunc(name, lsc) \
  7323. +static inline void di_write_lock_##name(struct dentry *d) \
  7324. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  7325. +
  7326. +#define AuRWLockFuncs(name, lsc) \
  7327. + AuReadLockFunc(name, lsc) \
  7328. + AuWriteLockFunc(name, lsc)
  7329. +
  7330. +AuRWLockFuncs(child, CHILD);
  7331. +AuRWLockFuncs(child2, CHILD2);
  7332. +AuRWLockFuncs(child3, CHILD3);
  7333. +AuRWLockFuncs(parent, PARENT);
  7334. +AuRWLockFuncs(parent2, PARENT2);
  7335. +AuRWLockFuncs(parent3, PARENT3);
  7336. +
  7337. +#undef AuReadLockFunc
  7338. +#undef AuWriteLockFunc
  7339. +#undef AuRWLockFuncs
  7340. +
  7341. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  7342. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  7343. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  7344. +
  7345. +/* ---------------------------------------------------------------------- */
  7346. +
  7347. +/* todo: memory barrier? */
  7348. +static inline unsigned int au_digen(struct dentry *d)
  7349. +{
  7350. + return atomic_read(&au_di(d)->di_generation);
  7351. +}
  7352. +
  7353. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  7354. +{
  7355. + hdentry->hd_dentry = NULL;
  7356. +}
  7357. +
  7358. +static inline void au_hdput(struct au_hdentry *hd)
  7359. +{
  7360. + if (hd)
  7361. + dput(hd->hd_dentry);
  7362. +}
  7363. +
  7364. +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
  7365. +{
  7366. + DiMustAnyLock(dentry);
  7367. + return au_di(dentry)->di_bstart;
  7368. +}
  7369. +
  7370. +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
  7371. +{
  7372. + DiMustAnyLock(dentry);
  7373. + return au_di(dentry)->di_bend;
  7374. +}
  7375. +
  7376. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  7377. +{
  7378. + DiMustAnyLock(dentry);
  7379. + return au_di(dentry)->di_bwh;
  7380. +}
  7381. +
  7382. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  7383. +{
  7384. + DiMustAnyLock(dentry);
  7385. + return au_di(dentry)->di_bdiropq;
  7386. +}
  7387. +
  7388. +/* todo: hard/soft set? */
  7389. +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
  7390. +{
  7391. + DiMustWriteLock(dentry);
  7392. + au_di(dentry)->di_bstart = bindex;
  7393. +}
  7394. +
  7395. +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
  7396. +{
  7397. + DiMustWriteLock(dentry);
  7398. + au_di(dentry)->di_bend = bindex;
  7399. +}
  7400. +
  7401. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  7402. +{
  7403. + DiMustWriteLock(dentry);
  7404. + /* dbwh can be outside of bstart - bend range */
  7405. + au_di(dentry)->di_bwh = bindex;
  7406. +}
  7407. +
  7408. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  7409. +{
  7410. + DiMustWriteLock(dentry);
  7411. + au_di(dentry)->di_bdiropq = bindex;
  7412. +}
  7413. +
  7414. +/* ---------------------------------------------------------------------- */
  7415. +
  7416. +#ifdef CONFIG_AUFS_HNOTIFY
  7417. +static inline void au_digen_dec(struct dentry *d)
  7418. +{
  7419. + atomic_dec(&au_di(d)->di_generation);
  7420. +}
  7421. +
  7422. +static inline void au_hn_di_reinit(struct dentry *dentry)
  7423. +{
  7424. + dentry->d_fsdata = NULL;
  7425. +}
  7426. +#else
  7427. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  7428. +#endif /* CONFIG_AUFS_HNOTIFY */
  7429. +
  7430. +#endif /* __KERNEL__ */
  7431. +#endif /* __AUFS_DENTRY_H__ */
  7432. diff -Nur linux-4.0.4.orig/fs/aufs/dinfo.c linux-4.0.4/fs/aufs/dinfo.c
  7433. --- linux-4.0.4.orig/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
  7434. +++ linux-4.0.4/fs/aufs/dinfo.c 2015-05-30 22:11:30.000000000 +0200
  7435. @@ -0,0 +1,531 @@
  7436. +/*
  7437. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7438. + */
  7439. +
  7440. +/*
  7441. + * dentry private data
  7442. + */
  7443. +
  7444. +#include "aufs.h"
  7445. +
  7446. +void au_di_init_once(void *_dinfo)
  7447. +{
  7448. + struct au_dinfo *dinfo = _dinfo;
  7449. + static struct lock_class_key aufs_di;
  7450. +
  7451. + au_rw_init(&dinfo->di_rwsem);
  7452. + au_rw_class(&dinfo->di_rwsem, &aufs_di);
  7453. +}
  7454. +
  7455. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  7456. +{
  7457. + struct au_dinfo *dinfo;
  7458. + int nbr, i;
  7459. +
  7460. + dinfo = au_cache_alloc_dinfo();
  7461. + if (unlikely(!dinfo))
  7462. + goto out;
  7463. +
  7464. + nbr = au_sbend(sb) + 1;
  7465. + if (nbr <= 0)
  7466. + nbr = 1;
  7467. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  7468. + if (dinfo->di_hdentry) {
  7469. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  7470. + dinfo->di_bstart = -1;
  7471. + dinfo->di_bend = -1;
  7472. + dinfo->di_bwh = -1;
  7473. + dinfo->di_bdiropq = -1;
  7474. + dinfo->di_tmpfile = 0;
  7475. + for (i = 0; i < nbr; i++)
  7476. + dinfo->di_hdentry[i].hd_id = -1;
  7477. + goto out;
  7478. + }
  7479. +
  7480. + au_cache_free_dinfo(dinfo);
  7481. + dinfo = NULL;
  7482. +
  7483. +out:
  7484. + return dinfo;
  7485. +}
  7486. +
  7487. +void au_di_free(struct au_dinfo *dinfo)
  7488. +{
  7489. + struct au_hdentry *p;
  7490. + aufs_bindex_t bend, bindex;
  7491. +
  7492. + /* dentry may not be revalidated */
  7493. + bindex = dinfo->di_bstart;
  7494. + if (bindex >= 0) {
  7495. + bend = dinfo->di_bend;
  7496. + p = dinfo->di_hdentry + bindex;
  7497. + while (bindex++ <= bend)
  7498. + au_hdput(p++);
  7499. + }
  7500. + kfree(dinfo->di_hdentry);
  7501. + au_cache_free_dinfo(dinfo);
  7502. +}
  7503. +
  7504. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  7505. +{
  7506. + struct au_hdentry *p;
  7507. + aufs_bindex_t bi;
  7508. +
  7509. + AuRwMustWriteLock(&a->di_rwsem);
  7510. + AuRwMustWriteLock(&b->di_rwsem);
  7511. +
  7512. +#define DiSwap(v, name) \
  7513. + do { \
  7514. + v = a->di_##name; \
  7515. + a->di_##name = b->di_##name; \
  7516. + b->di_##name = v; \
  7517. + } while (0)
  7518. +
  7519. + DiSwap(p, hdentry);
  7520. + DiSwap(bi, bstart);
  7521. + DiSwap(bi, bend);
  7522. + DiSwap(bi, bwh);
  7523. + DiSwap(bi, bdiropq);
  7524. + /* smp_mb(); */
  7525. +
  7526. +#undef DiSwap
  7527. +}
  7528. +
  7529. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  7530. +{
  7531. + AuRwMustWriteLock(&dst->di_rwsem);
  7532. + AuRwMustWriteLock(&src->di_rwsem);
  7533. +
  7534. + dst->di_bstart = src->di_bstart;
  7535. + dst->di_bend = src->di_bend;
  7536. + dst->di_bwh = src->di_bwh;
  7537. + dst->di_bdiropq = src->di_bdiropq;
  7538. + /* smp_mb(); */
  7539. +}
  7540. +
  7541. +int au_di_init(struct dentry *dentry)
  7542. +{
  7543. + int err;
  7544. + struct super_block *sb;
  7545. + struct au_dinfo *dinfo;
  7546. +
  7547. + err = 0;
  7548. + sb = dentry->d_sb;
  7549. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  7550. + if (dinfo) {
  7551. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  7552. + /* smp_mb(); */ /* atomic_set */
  7553. + dentry->d_fsdata = dinfo;
  7554. + } else
  7555. + err = -ENOMEM;
  7556. +
  7557. + return err;
  7558. +}
  7559. +
  7560. +void au_di_fin(struct dentry *dentry)
  7561. +{
  7562. + struct au_dinfo *dinfo;
  7563. +
  7564. + dinfo = au_di(dentry);
  7565. + AuRwDestroy(&dinfo->di_rwsem);
  7566. + au_di_free(dinfo);
  7567. +}
  7568. +
  7569. +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
  7570. +{
  7571. + int err, sz;
  7572. + struct au_hdentry *hdp;
  7573. +
  7574. + AuRwMustWriteLock(&dinfo->di_rwsem);
  7575. +
  7576. + err = -ENOMEM;
  7577. + sz = sizeof(*hdp) * (dinfo->di_bend + 1);
  7578. + if (!sz)
  7579. + sz = sizeof(*hdp);
  7580. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
  7581. + if (hdp) {
  7582. + dinfo->di_hdentry = hdp;
  7583. + err = 0;
  7584. + }
  7585. +
  7586. + return err;
  7587. +}
  7588. +
  7589. +/* ---------------------------------------------------------------------- */
  7590. +
  7591. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  7592. +{
  7593. + switch (lsc) {
  7594. + case AuLsc_DI_CHILD:
  7595. + ii_write_lock_child(inode);
  7596. + break;
  7597. + case AuLsc_DI_CHILD2:
  7598. + ii_write_lock_child2(inode);
  7599. + break;
  7600. + case AuLsc_DI_CHILD3:
  7601. + ii_write_lock_child3(inode);
  7602. + break;
  7603. + case AuLsc_DI_PARENT:
  7604. + ii_write_lock_parent(inode);
  7605. + break;
  7606. + case AuLsc_DI_PARENT2:
  7607. + ii_write_lock_parent2(inode);
  7608. + break;
  7609. + case AuLsc_DI_PARENT3:
  7610. + ii_write_lock_parent3(inode);
  7611. + break;
  7612. + default:
  7613. + BUG();
  7614. + }
  7615. +}
  7616. +
  7617. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  7618. +{
  7619. + switch (lsc) {
  7620. + case AuLsc_DI_CHILD:
  7621. + ii_read_lock_child(inode);
  7622. + break;
  7623. + case AuLsc_DI_CHILD2:
  7624. + ii_read_lock_child2(inode);
  7625. + break;
  7626. + case AuLsc_DI_CHILD3:
  7627. + ii_read_lock_child3(inode);
  7628. + break;
  7629. + case AuLsc_DI_PARENT:
  7630. + ii_read_lock_parent(inode);
  7631. + break;
  7632. + case AuLsc_DI_PARENT2:
  7633. + ii_read_lock_parent2(inode);
  7634. + break;
  7635. + case AuLsc_DI_PARENT3:
  7636. + ii_read_lock_parent3(inode);
  7637. + break;
  7638. + default:
  7639. + BUG();
  7640. + }
  7641. +}
  7642. +
  7643. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  7644. +{
  7645. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  7646. + if (d->d_inode) {
  7647. + if (au_ftest_lock(flags, IW))
  7648. + do_ii_write_lock(d->d_inode, lsc);
  7649. + else if (au_ftest_lock(flags, IR))
  7650. + do_ii_read_lock(d->d_inode, lsc);
  7651. + }
  7652. +}
  7653. +
  7654. +void di_read_unlock(struct dentry *d, int flags)
  7655. +{
  7656. + if (d->d_inode) {
  7657. + if (au_ftest_lock(flags, IW)) {
  7658. + au_dbg_verify_dinode(d);
  7659. + ii_write_unlock(d->d_inode);
  7660. + } else if (au_ftest_lock(flags, IR)) {
  7661. + au_dbg_verify_dinode(d);
  7662. + ii_read_unlock(d->d_inode);
  7663. + }
  7664. + }
  7665. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  7666. +}
  7667. +
  7668. +void di_downgrade_lock(struct dentry *d, int flags)
  7669. +{
  7670. + if (d->d_inode && au_ftest_lock(flags, IR))
  7671. + ii_downgrade_lock(d->d_inode);
  7672. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  7673. +}
  7674. +
  7675. +void di_write_lock(struct dentry *d, unsigned int lsc)
  7676. +{
  7677. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  7678. + if (d->d_inode)
  7679. + do_ii_write_lock(d->d_inode, lsc);
  7680. +}
  7681. +
  7682. +void di_write_unlock(struct dentry *d)
  7683. +{
  7684. + au_dbg_verify_dinode(d);
  7685. + if (d->d_inode)
  7686. + ii_write_unlock(d->d_inode);
  7687. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  7688. +}
  7689. +
  7690. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  7691. +{
  7692. + AuDebugOn(d1 == d2
  7693. + || d1->d_inode == d2->d_inode
  7694. + || d1->d_sb != d2->d_sb);
  7695. +
  7696. + if (isdir && au_test_subdir(d1, d2)) {
  7697. + di_write_lock_child(d1);
  7698. + di_write_lock_child2(d2);
  7699. + } else {
  7700. + /* there should be no races */
  7701. + di_write_lock_child(d2);
  7702. + di_write_lock_child2(d1);
  7703. + }
  7704. +}
  7705. +
  7706. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  7707. +{
  7708. + AuDebugOn(d1 == d2
  7709. + || d1->d_inode == d2->d_inode
  7710. + || d1->d_sb != d2->d_sb);
  7711. +
  7712. + if (isdir && au_test_subdir(d1, d2)) {
  7713. + di_write_lock_parent(d1);
  7714. + di_write_lock_parent2(d2);
  7715. + } else {
  7716. + /* there should be no races */
  7717. + di_write_lock_parent(d2);
  7718. + di_write_lock_parent2(d1);
  7719. + }
  7720. +}
  7721. +
  7722. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  7723. +{
  7724. + di_write_unlock(d1);
  7725. + if (d1->d_inode == d2->d_inode)
  7726. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  7727. + else
  7728. + di_write_unlock(d2);
  7729. +}
  7730. +
  7731. +/* ---------------------------------------------------------------------- */
  7732. +
  7733. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  7734. +{
  7735. + struct dentry *d;
  7736. +
  7737. + DiMustAnyLock(dentry);
  7738. +
  7739. + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
  7740. + return NULL;
  7741. + AuDebugOn(bindex < 0);
  7742. + d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
  7743. + AuDebugOn(d && au_dcount(d) <= 0);
  7744. + return d;
  7745. +}
  7746. +
  7747. +/*
  7748. + * extended version of au_h_dptr().
  7749. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  7750. + * error.
  7751. + */
  7752. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  7753. +{
  7754. + struct dentry *h_dentry;
  7755. + struct inode *inode, *h_inode;
  7756. +
  7757. + inode = dentry->d_inode;
  7758. + AuDebugOn(!inode);
  7759. +
  7760. + h_dentry = NULL;
  7761. + if (au_dbstart(dentry) <= bindex
  7762. + && bindex <= au_dbend(dentry))
  7763. + h_dentry = au_h_dptr(dentry, bindex);
  7764. + if (h_dentry && !au_d_linkable(h_dentry)) {
  7765. + dget(h_dentry);
  7766. + goto out; /* success */
  7767. + }
  7768. +
  7769. + AuDebugOn(bindex < au_ibstart(inode));
  7770. + AuDebugOn(au_ibend(inode) < bindex);
  7771. + h_inode = au_h_iptr(inode, bindex);
  7772. + h_dentry = d_find_alias(h_inode);
  7773. + if (h_dentry) {
  7774. + if (!IS_ERR(h_dentry)) {
  7775. + if (!au_d_linkable(h_dentry))
  7776. + goto out; /* success */
  7777. + dput(h_dentry);
  7778. + } else
  7779. + goto out;
  7780. + }
  7781. +
  7782. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  7783. + h_dentry = au_plink_lkup(inode, bindex);
  7784. + AuDebugOn(!h_dentry);
  7785. + if (!IS_ERR(h_dentry)) {
  7786. + if (!au_d_hashed_positive(h_dentry))
  7787. + goto out; /* success */
  7788. + dput(h_dentry);
  7789. + h_dentry = NULL;
  7790. + }
  7791. + }
  7792. +
  7793. +out:
  7794. + AuDbgDentry(h_dentry);
  7795. + return h_dentry;
  7796. +}
  7797. +
  7798. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  7799. +{
  7800. + aufs_bindex_t bend, bwh;
  7801. +
  7802. + bend = au_dbend(dentry);
  7803. + if (0 <= bend) {
  7804. + bwh = au_dbwh(dentry);
  7805. + if (!bwh)
  7806. + return bwh;
  7807. + if (0 < bwh && bwh < bend)
  7808. + return bwh - 1;
  7809. + }
  7810. + return bend;
  7811. +}
  7812. +
  7813. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  7814. +{
  7815. + aufs_bindex_t bend, bopq;
  7816. +
  7817. + bend = au_dbtail(dentry);
  7818. + if (0 <= bend) {
  7819. + bopq = au_dbdiropq(dentry);
  7820. + if (0 <= bopq && bopq < bend)
  7821. + bend = bopq;
  7822. + }
  7823. + return bend;
  7824. +}
  7825. +
  7826. +/* ---------------------------------------------------------------------- */
  7827. +
  7828. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  7829. + struct dentry *h_dentry)
  7830. +{
  7831. + struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
  7832. + struct au_branch *br;
  7833. +
  7834. + DiMustWriteLock(dentry);
  7835. +
  7836. + au_hdput(hd);
  7837. + hd->hd_dentry = h_dentry;
  7838. + if (h_dentry) {
  7839. + br = au_sbr(dentry->d_sb, bindex);
  7840. + hd->hd_id = br->br_id;
  7841. + }
  7842. +}
  7843. +
  7844. +int au_dbrange_test(struct dentry *dentry)
  7845. +{
  7846. + int err;
  7847. + aufs_bindex_t bstart, bend;
  7848. +
  7849. + err = 0;
  7850. + bstart = au_dbstart(dentry);
  7851. + bend = au_dbend(dentry);
  7852. + if (bstart >= 0)
  7853. + AuDebugOn(bend < 0 && bstart > bend);
  7854. + else {
  7855. + err = -EIO;
  7856. + AuDebugOn(bend >= 0);
  7857. + }
  7858. +
  7859. + return err;
  7860. +}
  7861. +
  7862. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  7863. +{
  7864. + int err;
  7865. +
  7866. + err = 0;
  7867. + if (unlikely(au_digen(dentry) != sigen
  7868. + || au_iigen_test(dentry->d_inode, sigen)))
  7869. + err = -EIO;
  7870. +
  7871. + return err;
  7872. +}
  7873. +
  7874. +void au_update_digen(struct dentry *dentry)
  7875. +{
  7876. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  7877. + /* smp_mb(); */ /* atomic_set */
  7878. +}
  7879. +
  7880. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  7881. +{
  7882. + struct au_dinfo *dinfo;
  7883. + struct dentry *h_d;
  7884. + struct au_hdentry *hdp;
  7885. +
  7886. + DiMustWriteLock(dentry);
  7887. +
  7888. + dinfo = au_di(dentry);
  7889. + if (!dinfo || dinfo->di_bstart < 0)
  7890. + return;
  7891. +
  7892. + hdp = dinfo->di_hdentry;
  7893. + if (do_put_zero) {
  7894. + aufs_bindex_t bindex, bend;
  7895. +
  7896. + bend = dinfo->di_bend;
  7897. + for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
  7898. + h_d = hdp[0 + bindex].hd_dentry;
  7899. + if (h_d && !h_d->d_inode)
  7900. + au_set_h_dptr(dentry, bindex, NULL);
  7901. + }
  7902. + }
  7903. +
  7904. + dinfo->di_bstart = -1;
  7905. + while (++dinfo->di_bstart <= dinfo->di_bend)
  7906. + if (hdp[0 + dinfo->di_bstart].hd_dentry)
  7907. + break;
  7908. + if (dinfo->di_bstart > dinfo->di_bend) {
  7909. + dinfo->di_bstart = -1;
  7910. + dinfo->di_bend = -1;
  7911. + return;
  7912. + }
  7913. +
  7914. + dinfo->di_bend++;
  7915. + while (0 <= --dinfo->di_bend)
  7916. + if (hdp[0 + dinfo->di_bend].hd_dentry)
  7917. + break;
  7918. + AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
  7919. +}
  7920. +
  7921. +void au_update_dbstart(struct dentry *dentry)
  7922. +{
  7923. + aufs_bindex_t bindex, bend;
  7924. + struct dentry *h_dentry;
  7925. +
  7926. + bend = au_dbend(dentry);
  7927. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
  7928. + h_dentry = au_h_dptr(dentry, bindex);
  7929. + if (!h_dentry)
  7930. + continue;
  7931. + if (h_dentry->d_inode) {
  7932. + au_set_dbstart(dentry, bindex);
  7933. + return;
  7934. + }
  7935. + au_set_h_dptr(dentry, bindex, NULL);
  7936. + }
  7937. +}
  7938. +
  7939. +void au_update_dbend(struct dentry *dentry)
  7940. +{
  7941. + aufs_bindex_t bindex, bstart;
  7942. + struct dentry *h_dentry;
  7943. +
  7944. + bstart = au_dbstart(dentry);
  7945. + for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
  7946. + h_dentry = au_h_dptr(dentry, bindex);
  7947. + if (!h_dentry)
  7948. + continue;
  7949. + if (h_dentry->d_inode) {
  7950. + au_set_dbend(dentry, bindex);
  7951. + return;
  7952. + }
  7953. + au_set_h_dptr(dentry, bindex, NULL);
  7954. + }
  7955. +}
  7956. +
  7957. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  7958. +{
  7959. + aufs_bindex_t bindex, bend;
  7960. +
  7961. + bend = au_dbend(dentry);
  7962. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
  7963. + if (au_h_dptr(dentry, bindex) == h_dentry)
  7964. + return bindex;
  7965. + return -1;
  7966. +}
  7967. diff -Nur linux-4.0.4.orig/fs/aufs/dir.c linux-4.0.4/fs/aufs/dir.c
  7968. --- linux-4.0.4.orig/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
  7969. +++ linux-4.0.4/fs/aufs/dir.c 2015-05-30 22:11:31.000000000 +0200
  7970. @@ -0,0 +1,738 @@
  7971. +/*
  7972. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7973. + */
  7974. +
  7975. +/*
  7976. + * directory operations
  7977. + */
  7978. +
  7979. +#include <linux/fs_stack.h>
  7980. +#include "aufs.h"
  7981. +
  7982. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  7983. +{
  7984. + unsigned int nlink;
  7985. +
  7986. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  7987. +
  7988. + nlink = dir->i_nlink;
  7989. + nlink += h_dir->i_nlink - 2;
  7990. + if (h_dir->i_nlink < 2)
  7991. + nlink += 2;
  7992. + smp_mb(); /* for i_nlink */
  7993. + /* 0 can happen in revaliding */
  7994. + set_nlink(dir, nlink);
  7995. +}
  7996. +
  7997. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  7998. +{
  7999. + unsigned int nlink;
  8000. +
  8001. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8002. +
  8003. + nlink = dir->i_nlink;
  8004. + nlink -= h_dir->i_nlink - 2;
  8005. + if (h_dir->i_nlink < 2)
  8006. + nlink -= 2;
  8007. + smp_mb(); /* for i_nlink */
  8008. + /* nlink == 0 means the branch-fs is broken */
  8009. + set_nlink(dir, nlink);
  8010. +}
  8011. +
  8012. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  8013. +{
  8014. + loff_t sz;
  8015. + aufs_bindex_t bindex, bend;
  8016. + struct file *h_file;
  8017. + struct dentry *h_dentry;
  8018. +
  8019. + sz = 0;
  8020. + if (file) {
  8021. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  8022. +
  8023. + bend = au_fbend_dir(file);
  8024. + for (bindex = au_fbstart(file);
  8025. + bindex <= bend && sz < KMALLOC_MAX_SIZE;
  8026. + bindex++) {
  8027. + h_file = au_hf_dir(file, bindex);
  8028. + if (h_file && file_inode(h_file))
  8029. + sz += vfsub_f_size_read(h_file);
  8030. + }
  8031. + } else {
  8032. + AuDebugOn(!dentry);
  8033. + AuDebugOn(!d_is_dir(dentry));
  8034. +
  8035. + bend = au_dbtaildir(dentry);
  8036. + for (bindex = au_dbstart(dentry);
  8037. + bindex <= bend && sz < KMALLOC_MAX_SIZE;
  8038. + bindex++) {
  8039. + h_dentry = au_h_dptr(dentry, bindex);
  8040. + if (h_dentry && h_dentry->d_inode)
  8041. + sz += i_size_read(h_dentry->d_inode);
  8042. + }
  8043. + }
  8044. + if (sz < KMALLOC_MAX_SIZE)
  8045. + sz = roundup_pow_of_two(sz);
  8046. + if (sz > KMALLOC_MAX_SIZE)
  8047. + sz = KMALLOC_MAX_SIZE;
  8048. + else if (sz < NAME_MAX) {
  8049. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  8050. + sz = AUFS_RDBLK_DEF;
  8051. + }
  8052. + return sz;
  8053. +}
  8054. +
  8055. +struct au_dir_ts_arg {
  8056. + struct dentry *dentry;
  8057. + aufs_bindex_t brid;
  8058. +};
  8059. +
  8060. +static void au_do_dir_ts(void *arg)
  8061. +{
  8062. + struct au_dir_ts_arg *a = arg;
  8063. + struct au_dtime dt;
  8064. + struct path h_path;
  8065. + struct inode *dir, *h_dir;
  8066. + struct super_block *sb;
  8067. + struct au_branch *br;
  8068. + struct au_hinode *hdir;
  8069. + int err;
  8070. + aufs_bindex_t bstart, bindex;
  8071. +
  8072. + sb = a->dentry->d_sb;
  8073. + dir = a->dentry->d_inode;
  8074. + if (!dir)
  8075. + goto out;
  8076. + /* no dir->i_mutex lock */
  8077. + aufs_read_lock(a->dentry, AuLock_DW | AuLock_DIR); /* noflush */
  8078. +
  8079. + bstart = au_ibstart(dir);
  8080. + bindex = au_br_index(sb, a->brid);
  8081. + if (bindex < bstart)
  8082. + goto out_unlock;
  8083. +
  8084. + br = au_sbr(sb, bindex);
  8085. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  8086. + if (!h_path.dentry)
  8087. + goto out_unlock;
  8088. + h_path.mnt = au_br_mnt(br);
  8089. + au_dtime_store(&dt, a->dentry, &h_path);
  8090. +
  8091. + br = au_sbr(sb, bstart);
  8092. + if (!au_br_writable(br->br_perm))
  8093. + goto out_unlock;
  8094. + h_path.dentry = au_h_dptr(a->dentry, bstart);
  8095. + h_path.mnt = au_br_mnt(br);
  8096. + err = vfsub_mnt_want_write(h_path.mnt);
  8097. + if (err)
  8098. + goto out_unlock;
  8099. + hdir = au_hi(dir, bstart);
  8100. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  8101. + h_dir = au_h_iptr(dir, bstart);
  8102. + if (h_dir->i_nlink
  8103. + && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  8104. + dt.dt_h_path = h_path;
  8105. + au_dtime_revert(&dt);
  8106. + }
  8107. + au_hn_imtx_unlock(hdir);
  8108. + vfsub_mnt_drop_write(h_path.mnt);
  8109. + au_cpup_attr_timesizes(dir);
  8110. +
  8111. +out_unlock:
  8112. + aufs_read_unlock(a->dentry, AuLock_DW);
  8113. +out:
  8114. + dput(a->dentry);
  8115. + au_nwt_done(&au_sbi(sb)->si_nowait);
  8116. + kfree(arg);
  8117. +}
  8118. +
  8119. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  8120. +{
  8121. + int perm, wkq_err;
  8122. + aufs_bindex_t bstart;
  8123. + struct au_dir_ts_arg *arg;
  8124. + struct dentry *dentry;
  8125. + struct super_block *sb;
  8126. +
  8127. + IMustLock(dir);
  8128. +
  8129. + dentry = d_find_any_alias(dir);
  8130. + AuDebugOn(!dentry);
  8131. + sb = dentry->d_sb;
  8132. + bstart = au_ibstart(dir);
  8133. + if (bstart == bindex) {
  8134. + au_cpup_attr_timesizes(dir);
  8135. + goto out;
  8136. + }
  8137. +
  8138. + perm = au_sbr_perm(sb, bstart);
  8139. + if (!au_br_writable(perm))
  8140. + goto out;
  8141. +
  8142. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  8143. + if (!arg)
  8144. + goto out;
  8145. +
  8146. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  8147. + arg->brid = au_sbr_id(sb, bindex);
  8148. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  8149. + if (unlikely(wkq_err)) {
  8150. + pr_err("wkq %d\n", wkq_err);
  8151. + dput(dentry);
  8152. + kfree(arg);
  8153. + }
  8154. +
  8155. +out:
  8156. + dput(dentry);
  8157. +}
  8158. +
  8159. +/* ---------------------------------------------------------------------- */
  8160. +
  8161. +static int reopen_dir(struct file *file)
  8162. +{
  8163. + int err;
  8164. + unsigned int flags;
  8165. + aufs_bindex_t bindex, btail, bstart;
  8166. + struct dentry *dentry, *h_dentry;
  8167. + struct file *h_file;
  8168. +
  8169. + /* open all lower dirs */
  8170. + dentry = file->f_path.dentry;
  8171. + bstart = au_dbstart(dentry);
  8172. + for (bindex = au_fbstart(file); bindex < bstart; bindex++)
  8173. + au_set_h_fptr(file, bindex, NULL);
  8174. + au_set_fbstart(file, bstart);
  8175. +
  8176. + btail = au_dbtaildir(dentry);
  8177. + for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
  8178. + au_set_h_fptr(file, bindex, NULL);
  8179. + au_set_fbend_dir(file, btail);
  8180. +
  8181. + flags = vfsub_file_flags(file);
  8182. + for (bindex = bstart; bindex <= btail; bindex++) {
  8183. + h_dentry = au_h_dptr(dentry, bindex);
  8184. + if (!h_dentry)
  8185. + continue;
  8186. + h_file = au_hf_dir(file, bindex);
  8187. + if (h_file)
  8188. + continue;
  8189. +
  8190. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8191. + err = PTR_ERR(h_file);
  8192. + if (IS_ERR(h_file))
  8193. + goto out; /* close all? */
  8194. + au_set_h_fptr(file, bindex, h_file);
  8195. + }
  8196. + au_update_figen(file);
  8197. + /* todo: necessary? */
  8198. + /* file->f_ra = h_file->f_ra; */
  8199. + err = 0;
  8200. +
  8201. +out:
  8202. + return err;
  8203. +}
  8204. +
  8205. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  8206. +{
  8207. + int err;
  8208. + aufs_bindex_t bindex, btail;
  8209. + struct dentry *dentry, *h_dentry;
  8210. +
  8211. + FiMustWriteLock(file);
  8212. + AuDebugOn(h_file);
  8213. +
  8214. + err = 0;
  8215. + dentry = file->f_path.dentry;
  8216. + file->f_version = dentry->d_inode->i_version;
  8217. + bindex = au_dbstart(dentry);
  8218. + au_set_fbstart(file, bindex);
  8219. + btail = au_dbtaildir(dentry);
  8220. + au_set_fbend_dir(file, btail);
  8221. + for (; !err && bindex <= btail; bindex++) {
  8222. + h_dentry = au_h_dptr(dentry, bindex);
  8223. + if (!h_dentry)
  8224. + continue;
  8225. +
  8226. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8227. + if (IS_ERR(h_file)) {
  8228. + err = PTR_ERR(h_file);
  8229. + break;
  8230. + }
  8231. + au_set_h_fptr(file, bindex, h_file);
  8232. + }
  8233. + au_update_figen(file);
  8234. + /* todo: necessary? */
  8235. + /* file->f_ra = h_file->f_ra; */
  8236. + if (!err)
  8237. + return 0; /* success */
  8238. +
  8239. + /* close all */
  8240. + for (bindex = au_fbstart(file); bindex <= btail; bindex++)
  8241. + au_set_h_fptr(file, bindex, NULL);
  8242. + au_set_fbstart(file, -1);
  8243. + au_set_fbend_dir(file, -1);
  8244. +
  8245. + return err;
  8246. +}
  8247. +
  8248. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  8249. + struct file *file)
  8250. +{
  8251. + int err;
  8252. + struct super_block *sb;
  8253. + struct au_fidir *fidir;
  8254. +
  8255. + err = -ENOMEM;
  8256. + sb = file->f_path.dentry->d_sb;
  8257. + si_read_lock(sb, AuLock_FLUSH);
  8258. + fidir = au_fidir_alloc(sb);
  8259. + if (fidir) {
  8260. + struct au_do_open_args args = {
  8261. + .open = do_open_dir,
  8262. + .fidir = fidir
  8263. + };
  8264. + err = au_do_open(file, &args);
  8265. + if (unlikely(err))
  8266. + kfree(fidir);
  8267. + }
  8268. + si_read_unlock(sb);
  8269. + return err;
  8270. +}
  8271. +
  8272. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  8273. + struct file *file)
  8274. +{
  8275. + struct au_vdir *vdir_cache;
  8276. + struct au_finfo *finfo;
  8277. + struct au_fidir *fidir;
  8278. + aufs_bindex_t bindex, bend;
  8279. +
  8280. + finfo = au_fi(file);
  8281. + fidir = finfo->fi_hdir;
  8282. + if (fidir) {
  8283. + au_sphl_del(&finfo->fi_hlist,
  8284. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  8285. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  8286. + if (vdir_cache)
  8287. + au_vdir_free(vdir_cache);
  8288. +
  8289. + bindex = finfo->fi_btop;
  8290. + if (bindex >= 0) {
  8291. + /*
  8292. + * calls fput() instead of filp_close(),
  8293. + * since no dnotify or lock for the lower file.
  8294. + */
  8295. + bend = fidir->fd_bbot;
  8296. + for (; bindex <= bend; bindex++)
  8297. + au_set_h_fptr(file, bindex, NULL);
  8298. + }
  8299. + kfree(fidir);
  8300. + finfo->fi_hdir = NULL;
  8301. + }
  8302. + au_finfo_fin(file);
  8303. + return 0;
  8304. +}
  8305. +
  8306. +/* ---------------------------------------------------------------------- */
  8307. +
  8308. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  8309. +{
  8310. + int err;
  8311. + aufs_bindex_t bindex, bend;
  8312. + struct file *h_file;
  8313. +
  8314. + err = 0;
  8315. + bend = au_fbend_dir(file);
  8316. + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
  8317. + h_file = au_hf_dir(file, bindex);
  8318. + if (h_file)
  8319. + err = vfsub_flush(h_file, id);
  8320. + }
  8321. + return err;
  8322. +}
  8323. +
  8324. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  8325. +{
  8326. + return au_do_flush(file, id, au_do_flush_dir);
  8327. +}
  8328. +
  8329. +/* ---------------------------------------------------------------------- */
  8330. +
  8331. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  8332. +{
  8333. + int err;
  8334. + aufs_bindex_t bend, bindex;
  8335. + struct inode *inode;
  8336. + struct super_block *sb;
  8337. +
  8338. + err = 0;
  8339. + sb = dentry->d_sb;
  8340. + inode = dentry->d_inode;
  8341. + IMustLock(inode);
  8342. + bend = au_dbend(dentry);
  8343. + for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
  8344. + struct path h_path;
  8345. +
  8346. + if (au_test_ro(sb, bindex, inode))
  8347. + continue;
  8348. + h_path.dentry = au_h_dptr(dentry, bindex);
  8349. + if (!h_path.dentry)
  8350. + continue;
  8351. +
  8352. + h_path.mnt = au_sbr_mnt(sb, bindex);
  8353. + err = vfsub_fsync(NULL, &h_path, datasync);
  8354. + }
  8355. +
  8356. + return err;
  8357. +}
  8358. +
  8359. +static int au_do_fsync_dir(struct file *file, int datasync)
  8360. +{
  8361. + int err;
  8362. + aufs_bindex_t bend, bindex;
  8363. + struct file *h_file;
  8364. + struct super_block *sb;
  8365. + struct inode *inode;
  8366. +
  8367. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  8368. + if (unlikely(err))
  8369. + goto out;
  8370. +
  8371. + sb = file->f_path.dentry->d_sb;
  8372. + inode = file_inode(file);
  8373. + bend = au_fbend_dir(file);
  8374. + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
  8375. + h_file = au_hf_dir(file, bindex);
  8376. + if (!h_file || au_test_ro(sb, bindex, inode))
  8377. + continue;
  8378. +
  8379. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  8380. + }
  8381. +
  8382. +out:
  8383. + return err;
  8384. +}
  8385. +
  8386. +/*
  8387. + * @file may be NULL
  8388. + */
  8389. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  8390. + int datasync)
  8391. +{
  8392. + int err;
  8393. + struct dentry *dentry;
  8394. + struct super_block *sb;
  8395. + struct mutex *mtx;
  8396. +
  8397. + err = 0;
  8398. + dentry = file->f_path.dentry;
  8399. + mtx = &dentry->d_inode->i_mutex;
  8400. + mutex_lock(mtx);
  8401. + sb = dentry->d_sb;
  8402. + si_noflush_read_lock(sb);
  8403. + if (file)
  8404. + err = au_do_fsync_dir(file, datasync);
  8405. + else {
  8406. + di_write_lock_child(dentry);
  8407. + err = au_do_fsync_dir_no_file(dentry, datasync);
  8408. + }
  8409. + au_cpup_attr_timesizes(dentry->d_inode);
  8410. + di_write_unlock(dentry);
  8411. + if (file)
  8412. + fi_write_unlock(file);
  8413. +
  8414. + si_read_unlock(sb);
  8415. + mutex_unlock(mtx);
  8416. + return err;
  8417. +}
  8418. +
  8419. +/* ---------------------------------------------------------------------- */
  8420. +
  8421. +static int aufs_iterate(struct file *file, struct dir_context *ctx)
  8422. +{
  8423. + int err;
  8424. + struct dentry *dentry;
  8425. + struct inode *inode, *h_inode;
  8426. + struct super_block *sb;
  8427. +
  8428. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  8429. +
  8430. + dentry = file->f_path.dentry;
  8431. + inode = dentry->d_inode;
  8432. + IMustLock(inode);
  8433. +
  8434. + sb = dentry->d_sb;
  8435. + si_read_lock(sb, AuLock_FLUSH);
  8436. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  8437. + if (unlikely(err))
  8438. + goto out;
  8439. + err = au_alive_dir(dentry);
  8440. + if (!err)
  8441. + err = au_vdir_init(file);
  8442. + di_downgrade_lock(dentry, AuLock_IR);
  8443. + if (unlikely(err))
  8444. + goto out_unlock;
  8445. +
  8446. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  8447. + if (!au_test_nfsd()) {
  8448. + err = au_vdir_fill_de(file, ctx);
  8449. + fsstack_copy_attr_atime(inode, h_inode);
  8450. + } else {
  8451. + /*
  8452. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  8453. + * encode_fh() and others.
  8454. + */
  8455. + atomic_inc(&h_inode->i_count);
  8456. + di_read_unlock(dentry, AuLock_IR);
  8457. + si_read_unlock(sb);
  8458. + err = au_vdir_fill_de(file, ctx);
  8459. + fsstack_copy_attr_atime(inode, h_inode);
  8460. + fi_write_unlock(file);
  8461. + iput(h_inode);
  8462. +
  8463. + AuTraceErr(err);
  8464. + return err;
  8465. + }
  8466. +
  8467. +out_unlock:
  8468. + di_read_unlock(dentry, AuLock_IR);
  8469. + fi_write_unlock(file);
  8470. +out:
  8471. + si_read_unlock(sb);
  8472. + return err;
  8473. +}
  8474. +
  8475. +/* ---------------------------------------------------------------------- */
  8476. +
  8477. +#define AuTestEmpty_WHONLY 1
  8478. +#define AuTestEmpty_CALLED (1 << 1)
  8479. +#define AuTestEmpty_SHWH (1 << 2)
  8480. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  8481. +#define au_fset_testempty(flags, name) \
  8482. + do { (flags) |= AuTestEmpty_##name; } while (0)
  8483. +#define au_fclr_testempty(flags, name) \
  8484. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  8485. +
  8486. +#ifndef CONFIG_AUFS_SHWH
  8487. +#undef AuTestEmpty_SHWH
  8488. +#define AuTestEmpty_SHWH 0
  8489. +#endif
  8490. +
  8491. +struct test_empty_arg {
  8492. + struct dir_context ctx;
  8493. + struct au_nhash *whlist;
  8494. + unsigned int flags;
  8495. + int err;
  8496. + aufs_bindex_t bindex;
  8497. +};
  8498. +
  8499. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  8500. + int namelen, loff_t offset __maybe_unused, u64 ino,
  8501. + unsigned int d_type)
  8502. +{
  8503. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  8504. + ctx);
  8505. + char *name = (void *)__name;
  8506. +
  8507. + arg->err = 0;
  8508. + au_fset_testempty(arg->flags, CALLED);
  8509. + /* smp_mb(); */
  8510. + if (name[0] == '.'
  8511. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  8512. + goto out; /* success */
  8513. +
  8514. + if (namelen <= AUFS_WH_PFX_LEN
  8515. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  8516. + if (au_ftest_testempty(arg->flags, WHONLY)
  8517. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  8518. + arg->err = -ENOTEMPTY;
  8519. + goto out;
  8520. + }
  8521. +
  8522. + name += AUFS_WH_PFX_LEN;
  8523. + namelen -= AUFS_WH_PFX_LEN;
  8524. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  8525. + arg->err = au_nhash_append_wh
  8526. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  8527. + au_ftest_testempty(arg->flags, SHWH));
  8528. +
  8529. +out:
  8530. + /* smp_mb(); */
  8531. + AuTraceErr(arg->err);
  8532. + return arg->err;
  8533. +}
  8534. +
  8535. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  8536. +{
  8537. + int err;
  8538. + struct file *h_file;
  8539. +
  8540. + h_file = au_h_open(dentry, arg->bindex,
  8541. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  8542. + /*file*/NULL, /*force_wr*/0);
  8543. + err = PTR_ERR(h_file);
  8544. + if (IS_ERR(h_file))
  8545. + goto out;
  8546. +
  8547. + err = 0;
  8548. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  8549. + && !file_inode(h_file)->i_nlink)
  8550. + goto out_put;
  8551. +
  8552. + do {
  8553. + arg->err = 0;
  8554. + au_fclr_testempty(arg->flags, CALLED);
  8555. + /* smp_mb(); */
  8556. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  8557. + if (err >= 0)
  8558. + err = arg->err;
  8559. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  8560. +
  8561. +out_put:
  8562. + fput(h_file);
  8563. + au_sbr_put(dentry->d_sb, arg->bindex);
  8564. +out:
  8565. + return err;
  8566. +}
  8567. +
  8568. +struct do_test_empty_args {
  8569. + int *errp;
  8570. + struct dentry *dentry;
  8571. + struct test_empty_arg *arg;
  8572. +};
  8573. +
  8574. +static void call_do_test_empty(void *args)
  8575. +{
  8576. + struct do_test_empty_args *a = args;
  8577. + *a->errp = do_test_empty(a->dentry, a->arg);
  8578. +}
  8579. +
  8580. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  8581. +{
  8582. + int err, wkq_err;
  8583. + struct dentry *h_dentry;
  8584. + struct inode *h_inode;
  8585. +
  8586. + h_dentry = au_h_dptr(dentry, arg->bindex);
  8587. + h_inode = h_dentry->d_inode;
  8588. + /* todo: i_mode changes anytime? */
  8589. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  8590. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  8591. + mutex_unlock(&h_inode->i_mutex);
  8592. + if (!err)
  8593. + err = do_test_empty(dentry, arg);
  8594. + else {
  8595. + struct do_test_empty_args args = {
  8596. + .errp = &err,
  8597. + .dentry = dentry,
  8598. + .arg = arg
  8599. + };
  8600. + unsigned int flags = arg->flags;
  8601. +
  8602. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  8603. + if (unlikely(wkq_err))
  8604. + err = wkq_err;
  8605. + arg->flags = flags;
  8606. + }
  8607. +
  8608. + return err;
  8609. +}
  8610. +
  8611. +int au_test_empty_lower(struct dentry *dentry)
  8612. +{
  8613. + int err;
  8614. + unsigned int rdhash;
  8615. + aufs_bindex_t bindex, bstart, btail;
  8616. + struct au_nhash whlist;
  8617. + struct test_empty_arg arg = {
  8618. + .ctx = {
  8619. + .actor = test_empty_cb
  8620. + }
  8621. + };
  8622. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  8623. +
  8624. + SiMustAnyLock(dentry->d_sb);
  8625. +
  8626. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  8627. + if (!rdhash)
  8628. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  8629. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  8630. + if (unlikely(err))
  8631. + goto out;
  8632. +
  8633. + arg.flags = 0;
  8634. + arg.whlist = &whlist;
  8635. + bstart = au_dbstart(dentry);
  8636. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  8637. + au_fset_testempty(arg.flags, SHWH);
  8638. + test_empty = do_test_empty;
  8639. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  8640. + test_empty = sio_test_empty;
  8641. + arg.bindex = bstart;
  8642. + err = test_empty(dentry, &arg);
  8643. + if (unlikely(err))
  8644. + goto out_whlist;
  8645. +
  8646. + au_fset_testempty(arg.flags, WHONLY);
  8647. + btail = au_dbtaildir(dentry);
  8648. + for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
  8649. + struct dentry *h_dentry;
  8650. +
  8651. + h_dentry = au_h_dptr(dentry, bindex);
  8652. + if (h_dentry && h_dentry->d_inode) {
  8653. + arg.bindex = bindex;
  8654. + err = test_empty(dentry, &arg);
  8655. + }
  8656. + }
  8657. +
  8658. +out_whlist:
  8659. + au_nhash_wh_free(&whlist);
  8660. +out:
  8661. + return err;
  8662. +}
  8663. +
  8664. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  8665. +{
  8666. + int err;
  8667. + struct test_empty_arg arg = {
  8668. + .ctx = {
  8669. + .actor = test_empty_cb
  8670. + }
  8671. + };
  8672. + aufs_bindex_t bindex, btail;
  8673. +
  8674. + err = 0;
  8675. + arg.whlist = whlist;
  8676. + arg.flags = AuTestEmpty_WHONLY;
  8677. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  8678. + au_fset_testempty(arg.flags, SHWH);
  8679. + btail = au_dbtaildir(dentry);
  8680. + for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
  8681. + struct dentry *h_dentry;
  8682. +
  8683. + h_dentry = au_h_dptr(dentry, bindex);
  8684. + if (h_dentry && h_dentry->d_inode) {
  8685. + arg.bindex = bindex;
  8686. + err = sio_test_empty(dentry, &arg);
  8687. + }
  8688. + }
  8689. +
  8690. + return err;
  8691. +}
  8692. +
  8693. +/* ---------------------------------------------------------------------- */
  8694. +
  8695. +const struct file_operations aufs_dir_fop = {
  8696. + .owner = THIS_MODULE,
  8697. + .llseek = default_llseek,
  8698. + .read = generic_read_dir,
  8699. + .iterate = aufs_iterate,
  8700. + .unlocked_ioctl = aufs_ioctl_dir,
  8701. +#ifdef CONFIG_COMPAT
  8702. + .compat_ioctl = aufs_compat_ioctl_dir,
  8703. +#endif
  8704. + .open = aufs_open_dir,
  8705. + .release = aufs_release_dir,
  8706. + .flush = aufs_flush_dir,
  8707. + .fsync = aufs_fsync_dir
  8708. +};
  8709. diff -Nur linux-4.0.4.orig/fs/aufs/dir.h linux-4.0.4/fs/aufs/dir.h
  8710. --- linux-4.0.4.orig/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
  8711. +++ linux-4.0.4/fs/aufs/dir.h 2015-05-30 22:11:31.000000000 +0200
  8712. @@ -0,0 +1,118 @@
  8713. +/*
  8714. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  8715. + */
  8716. +
  8717. +/*
  8718. + * directory operations
  8719. + */
  8720. +
  8721. +#ifndef __AUFS_DIR_H__
  8722. +#define __AUFS_DIR_H__
  8723. +
  8724. +#ifdef __KERNEL__
  8725. +
  8726. +#include <linux/fs.h>
  8727. +
  8728. +/* ---------------------------------------------------------------------- */
  8729. +
  8730. +/* need to be faster and smaller */
  8731. +
  8732. +struct au_nhash {
  8733. + unsigned int nh_num;
  8734. + struct hlist_head *nh_head;
  8735. +};
  8736. +
  8737. +struct au_vdir_destr {
  8738. + unsigned char len;
  8739. + unsigned char name[0];
  8740. +} __packed;
  8741. +
  8742. +struct au_vdir_dehstr {
  8743. + struct hlist_node hash;
  8744. + struct au_vdir_destr *str;
  8745. +} ____cacheline_aligned_in_smp;
  8746. +
  8747. +struct au_vdir_de {
  8748. + ino_t de_ino;
  8749. + unsigned char de_type;
  8750. + /* caution: packed */
  8751. + struct au_vdir_destr de_str;
  8752. +} __packed;
  8753. +
  8754. +struct au_vdir_wh {
  8755. + struct hlist_node wh_hash;
  8756. +#ifdef CONFIG_AUFS_SHWH
  8757. + ino_t wh_ino;
  8758. + aufs_bindex_t wh_bindex;
  8759. + unsigned char wh_type;
  8760. +#else
  8761. + aufs_bindex_t wh_bindex;
  8762. +#endif
  8763. + /* caution: packed */
  8764. + struct au_vdir_destr wh_str;
  8765. +} __packed;
  8766. +
  8767. +union au_vdir_deblk_p {
  8768. + unsigned char *deblk;
  8769. + struct au_vdir_de *de;
  8770. +};
  8771. +
  8772. +struct au_vdir {
  8773. + unsigned char **vd_deblk;
  8774. + unsigned long vd_nblk;
  8775. + struct {
  8776. + unsigned long ul;
  8777. + union au_vdir_deblk_p p;
  8778. + } vd_last;
  8779. +
  8780. + unsigned long vd_version;
  8781. + unsigned int vd_deblk_sz;
  8782. + unsigned long vd_jiffy;
  8783. +} ____cacheline_aligned_in_smp;
  8784. +
  8785. +/* ---------------------------------------------------------------------- */
  8786. +
  8787. +/* dir.c */
  8788. +extern const struct file_operations aufs_dir_fop;
  8789. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  8790. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  8791. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  8792. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  8793. +int au_test_empty_lower(struct dentry *dentry);
  8794. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  8795. +
  8796. +/* vdir.c */
  8797. +unsigned int au_rdhash_est(loff_t sz);
  8798. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  8799. +void au_nhash_wh_free(struct au_nhash *whlist);
  8800. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  8801. + int limit);
  8802. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  8803. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  8804. + unsigned int d_type, aufs_bindex_t bindex,
  8805. + unsigned char shwh);
  8806. +void au_vdir_free(struct au_vdir *vdir);
  8807. +int au_vdir_init(struct file *file);
  8808. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  8809. +
  8810. +/* ioctl.c */
  8811. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  8812. +
  8813. +#ifdef CONFIG_AUFS_RDU
  8814. +/* rdu.c */
  8815. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  8816. +#ifdef CONFIG_COMPAT
  8817. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  8818. + unsigned long arg);
  8819. +#endif
  8820. +#else
  8821. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  8822. + unsigned int cmd, unsigned long arg)
  8823. +#ifdef CONFIG_COMPAT
  8824. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  8825. + unsigned int cmd, unsigned long arg)
  8826. +#endif
  8827. +#endif
  8828. +
  8829. +#endif /* __KERNEL__ */
  8830. +#endif /* __AUFS_DIR_H__ */
  8831. diff -Nur linux-4.0.4.orig/fs/aufs/dynop.c linux-4.0.4/fs/aufs/dynop.c
  8832. --- linux-4.0.4.orig/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
  8833. +++ linux-4.0.4/fs/aufs/dynop.c 2015-05-30 22:11:30.000000000 +0200
  8834. @@ -0,0 +1,356 @@
  8835. +/*
  8836. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  8837. + */
  8838. +
  8839. +/*
  8840. + * dynamically customizable operations for regular files
  8841. + */
  8842. +
  8843. +#include "aufs.h"
  8844. +
  8845. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  8846. +
  8847. +/*
  8848. + * How large will these lists be?
  8849. + * Usually just a few elements, 20-30 at most for each, I guess.
  8850. + */
  8851. +static struct au_splhead dynop[AuDyLast];
  8852. +
  8853. +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
  8854. +{
  8855. + struct au_dykey *key, *tmp;
  8856. + struct list_head *head;
  8857. +
  8858. + key = NULL;
  8859. + head = &spl->head;
  8860. + rcu_read_lock();
  8861. + list_for_each_entry_rcu(tmp, head, dk_list)
  8862. + if (tmp->dk_op.dy_hop == h_op) {
  8863. + key = tmp;
  8864. + kref_get(&key->dk_kref);
  8865. + break;
  8866. + }
  8867. + rcu_read_unlock();
  8868. +
  8869. + return key;
  8870. +}
  8871. +
  8872. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  8873. +{
  8874. + struct au_dykey **k, *found;
  8875. + const void *h_op = key->dk_op.dy_hop;
  8876. + int i;
  8877. +
  8878. + found = NULL;
  8879. + k = br->br_dykey;
  8880. + for (i = 0; i < AuBrDynOp; i++)
  8881. + if (k[i]) {
  8882. + if (k[i]->dk_op.dy_hop == h_op) {
  8883. + found = k[i];
  8884. + break;
  8885. + }
  8886. + } else
  8887. + break;
  8888. + if (!found) {
  8889. + spin_lock(&br->br_dykey_lock);
  8890. + for (; i < AuBrDynOp; i++)
  8891. + if (k[i]) {
  8892. + if (k[i]->dk_op.dy_hop == h_op) {
  8893. + found = k[i];
  8894. + break;
  8895. + }
  8896. + } else {
  8897. + k[i] = key;
  8898. + break;
  8899. + }
  8900. + spin_unlock(&br->br_dykey_lock);
  8901. + BUG_ON(i == AuBrDynOp); /* expand the array */
  8902. + }
  8903. +
  8904. + return found;
  8905. +}
  8906. +
  8907. +/* kref_get() if @key is already added */
  8908. +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
  8909. +{
  8910. + struct au_dykey *tmp, *found;
  8911. + struct list_head *head;
  8912. + const void *h_op = key->dk_op.dy_hop;
  8913. +
  8914. + found = NULL;
  8915. + head = &spl->head;
  8916. + spin_lock(&spl->spin);
  8917. + list_for_each_entry(tmp, head, dk_list)
  8918. + if (tmp->dk_op.dy_hop == h_op) {
  8919. + kref_get(&tmp->dk_kref);
  8920. + found = tmp;
  8921. + break;
  8922. + }
  8923. + if (!found)
  8924. + list_add_rcu(&key->dk_list, head);
  8925. + spin_unlock(&spl->spin);
  8926. +
  8927. + if (!found)
  8928. + DyPrSym(key);
  8929. + return found;
  8930. +}
  8931. +
  8932. +static void dy_free_rcu(struct rcu_head *rcu)
  8933. +{
  8934. + struct au_dykey *key;
  8935. +
  8936. + key = container_of(rcu, struct au_dykey, dk_rcu);
  8937. + DyPrSym(key);
  8938. + kfree(key);
  8939. +}
  8940. +
  8941. +static void dy_free(struct kref *kref)
  8942. +{
  8943. + struct au_dykey *key;
  8944. + struct au_splhead *spl;
  8945. +
  8946. + key = container_of(kref, struct au_dykey, dk_kref);
  8947. + spl = dynop + key->dk_op.dy_type;
  8948. + au_spl_del_rcu(&key->dk_list, spl);
  8949. + call_rcu(&key->dk_rcu, dy_free_rcu);
  8950. +}
  8951. +
  8952. +void au_dy_put(struct au_dykey *key)
  8953. +{
  8954. + kref_put(&key->dk_kref, dy_free);
  8955. +}
  8956. +
  8957. +/* ---------------------------------------------------------------------- */
  8958. +
  8959. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  8960. +
  8961. +#ifdef CONFIG_AUFS_DEBUG
  8962. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  8963. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  8964. +#else
  8965. +#define DyDbgDeclare(cnt) do {} while (0)
  8966. +#define DyDbgInc(cnt) do {} while (0)
  8967. +#endif
  8968. +
  8969. +#define DySet(func, dst, src, h_op, h_sb) do { \
  8970. + DyDbgInc(cnt); \
  8971. + if (h_op->func) { \
  8972. + if (src.func) \
  8973. + dst.func = src.func; \
  8974. + else \
  8975. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  8976. + } \
  8977. +} while (0)
  8978. +
  8979. +#define DySetForce(func, dst, src) do { \
  8980. + AuDebugOn(!src.func); \
  8981. + DyDbgInc(cnt); \
  8982. + dst.func = src.func; \
  8983. +} while (0)
  8984. +
  8985. +#define DySetAop(func) \
  8986. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  8987. +#define DySetAopForce(func) \
  8988. + DySetForce(func, dyaop->da_op, aufs_aop)
  8989. +
  8990. +static void dy_aop(struct au_dykey *key, const void *h_op,
  8991. + struct super_block *h_sb __maybe_unused)
  8992. +{
  8993. + struct au_dyaop *dyaop = (void *)key;
  8994. + const struct address_space_operations *h_aop = h_op;
  8995. + DyDbgDeclare(cnt);
  8996. +
  8997. + AuDbg("%s\n", au_sbtype(h_sb));
  8998. +
  8999. + DySetAop(writepage);
  9000. + DySetAopForce(readpage); /* force */
  9001. + DySetAop(writepages);
  9002. + DySetAop(set_page_dirty);
  9003. + DySetAop(readpages);
  9004. + DySetAop(write_begin);
  9005. + DySetAop(write_end);
  9006. + DySetAop(bmap);
  9007. + DySetAop(invalidatepage);
  9008. + DySetAop(releasepage);
  9009. + DySetAop(freepage);
  9010. + /* this one will be changed according to an aufs mount option */
  9011. + DySetAop(direct_IO);
  9012. + DySetAop(migratepage);
  9013. + DySetAop(launder_page);
  9014. + DySetAop(is_partially_uptodate);
  9015. + DySetAop(is_dirty_writeback);
  9016. + DySetAop(error_remove_page);
  9017. + DySetAop(swap_activate);
  9018. + DySetAop(swap_deactivate);
  9019. +
  9020. + DyDbgSize(cnt, *h_aop);
  9021. +}
  9022. +
  9023. +/* ---------------------------------------------------------------------- */
  9024. +
  9025. +static void dy_bug(struct kref *kref)
  9026. +{
  9027. + BUG();
  9028. +}
  9029. +
  9030. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  9031. +{
  9032. + struct au_dykey *key, *old;
  9033. + struct au_splhead *spl;
  9034. + struct op {
  9035. + unsigned int sz;
  9036. + void (*set)(struct au_dykey *key, const void *h_op,
  9037. + struct super_block *h_sb __maybe_unused);
  9038. + };
  9039. + static const struct op a[] = {
  9040. + [AuDy_AOP] = {
  9041. + .sz = sizeof(struct au_dyaop),
  9042. + .set = dy_aop
  9043. + }
  9044. + };
  9045. + const struct op *p;
  9046. +
  9047. + spl = dynop + op->dy_type;
  9048. + key = dy_gfind_get(spl, op->dy_hop);
  9049. + if (key)
  9050. + goto out_add; /* success */
  9051. +
  9052. + p = a + op->dy_type;
  9053. + key = kzalloc(p->sz, GFP_NOFS);
  9054. + if (unlikely(!key)) {
  9055. + key = ERR_PTR(-ENOMEM);
  9056. + goto out;
  9057. + }
  9058. +
  9059. + key->dk_op.dy_hop = op->dy_hop;
  9060. + kref_init(&key->dk_kref);
  9061. + p->set(key, op->dy_hop, au_br_sb(br));
  9062. + old = dy_gadd(spl, key);
  9063. + if (old) {
  9064. + kfree(key);
  9065. + key = old;
  9066. + }
  9067. +
  9068. +out_add:
  9069. + old = dy_bradd(br, key);
  9070. + if (old)
  9071. + /* its ref-count should never be zero here */
  9072. + kref_put(&key->dk_kref, dy_bug);
  9073. +out:
  9074. + return key;
  9075. +}
  9076. +
  9077. +/* ---------------------------------------------------------------------- */
  9078. +/*
  9079. + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
  9080. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  9081. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  9082. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  9083. + * See the aufs manual in detail.
  9084. + */
  9085. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  9086. +{
  9087. + if (!do_dx)
  9088. + dyaop->da_op.direct_IO = NULL;
  9089. + else
  9090. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  9091. +}
  9092. +
  9093. +static struct au_dyaop *dy_aget(struct au_branch *br,
  9094. + const struct address_space_operations *h_aop,
  9095. + int do_dx)
  9096. +{
  9097. + struct au_dyaop *dyaop;
  9098. + struct au_dynop op;
  9099. +
  9100. + op.dy_type = AuDy_AOP;
  9101. + op.dy_haop = h_aop;
  9102. + dyaop = (void *)dy_get(&op, br);
  9103. + if (IS_ERR(dyaop))
  9104. + goto out;
  9105. + dy_adx(dyaop, do_dx);
  9106. +
  9107. +out:
  9108. + return dyaop;
  9109. +}
  9110. +
  9111. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9112. + struct inode *h_inode)
  9113. +{
  9114. + int err, do_dx;
  9115. + struct super_block *sb;
  9116. + struct au_branch *br;
  9117. + struct au_dyaop *dyaop;
  9118. +
  9119. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  9120. + IiMustWriteLock(inode);
  9121. +
  9122. + sb = inode->i_sb;
  9123. + br = au_sbr(sb, bindex);
  9124. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  9125. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  9126. + err = PTR_ERR(dyaop);
  9127. + if (IS_ERR(dyaop))
  9128. + /* unnecessary to call dy_fput() */
  9129. + goto out;
  9130. +
  9131. + err = 0;
  9132. + inode->i_mapping->a_ops = &dyaop->da_op;
  9133. +
  9134. +out:
  9135. + return err;
  9136. +}
  9137. +
  9138. +/*
  9139. + * Is it safe to replace a_ops during the inode/file is in operation?
  9140. + * Yes, I hope so.
  9141. + */
  9142. +int au_dy_irefresh(struct inode *inode)
  9143. +{
  9144. + int err;
  9145. + aufs_bindex_t bstart;
  9146. + struct inode *h_inode;
  9147. +
  9148. + err = 0;
  9149. + if (S_ISREG(inode->i_mode)) {
  9150. + bstart = au_ibstart(inode);
  9151. + h_inode = au_h_iptr(inode, bstart);
  9152. + err = au_dy_iaop(inode, bstart, h_inode);
  9153. + }
  9154. + return err;
  9155. +}
  9156. +
  9157. +void au_dy_arefresh(int do_dx)
  9158. +{
  9159. + struct au_splhead *spl;
  9160. + struct list_head *head;
  9161. + struct au_dykey *key;
  9162. +
  9163. + spl = dynop + AuDy_AOP;
  9164. + head = &spl->head;
  9165. + spin_lock(&spl->spin);
  9166. + list_for_each_entry(key, head, dk_list)
  9167. + dy_adx((void *)key, do_dx);
  9168. + spin_unlock(&spl->spin);
  9169. +}
  9170. +
  9171. +/* ---------------------------------------------------------------------- */
  9172. +
  9173. +void __init au_dy_init(void)
  9174. +{
  9175. + int i;
  9176. +
  9177. + /* make sure that 'struct au_dykey *' can be any type */
  9178. + BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
  9179. +
  9180. + for (i = 0; i < AuDyLast; i++)
  9181. + au_spl_init(dynop + i);
  9182. +}
  9183. +
  9184. +void au_dy_fin(void)
  9185. +{
  9186. + int i;
  9187. +
  9188. + for (i = 0; i < AuDyLast; i++)
  9189. + WARN_ON(!list_empty(&dynop[i].head));
  9190. +}
  9191. diff -Nur linux-4.0.4.orig/fs/aufs/dynop.h linux-4.0.4/fs/aufs/dynop.h
  9192. --- linux-4.0.4.orig/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
  9193. +++ linux-4.0.4/fs/aufs/dynop.h 2015-05-30 22:11:30.000000000 +0200
  9194. @@ -0,0 +1,61 @@
  9195. +/*
  9196. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  9197. + */
  9198. +
  9199. +/*
  9200. + * dynamically customizable operations (for regular files only)
  9201. + */
  9202. +
  9203. +#ifndef __AUFS_DYNOP_H__
  9204. +#define __AUFS_DYNOP_H__
  9205. +
  9206. +#ifdef __KERNEL__
  9207. +
  9208. +#include <linux/fs.h>
  9209. +#include <linux/kref.h>
  9210. +
  9211. +enum {AuDy_AOP, AuDyLast};
  9212. +
  9213. +struct au_dynop {
  9214. + int dy_type;
  9215. + union {
  9216. + const void *dy_hop;
  9217. + const struct address_space_operations *dy_haop;
  9218. + };
  9219. +};
  9220. +
  9221. +struct au_dykey {
  9222. + union {
  9223. + struct list_head dk_list;
  9224. + struct rcu_head dk_rcu;
  9225. + };
  9226. + struct au_dynop dk_op;
  9227. +
  9228. + /*
  9229. + * during I am in the branch local array, kref is gotten. when the
  9230. + * branch is removed, kref is put.
  9231. + */
  9232. + struct kref dk_kref;
  9233. +};
  9234. +
  9235. +/* stop unioning since their sizes are very different from each other */
  9236. +struct au_dyaop {
  9237. + struct au_dykey da_key;
  9238. + struct address_space_operations da_op; /* not const */
  9239. +};
  9240. +
  9241. +/* ---------------------------------------------------------------------- */
  9242. +
  9243. +/* dynop.c */
  9244. +struct au_branch;
  9245. +void au_dy_put(struct au_dykey *key);
  9246. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9247. + struct inode *h_inode);
  9248. +int au_dy_irefresh(struct inode *inode);
  9249. +void au_dy_arefresh(int do_dio);
  9250. +
  9251. +void __init au_dy_init(void);
  9252. +void au_dy_fin(void);
  9253. +
  9254. +#endif /* __KERNEL__ */
  9255. +#endif /* __AUFS_DYNOP_H__ */
  9256. diff -Nur linux-4.0.4.orig/fs/aufs/export.c linux-4.0.4/fs/aufs/export.c
  9257. --- linux-4.0.4.orig/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
  9258. +++ linux-4.0.4/fs/aufs/export.c 2015-05-30 22:11:30.000000000 +0200
  9259. @@ -0,0 +1,818 @@
  9260. +/*
  9261. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  9262. + */
  9263. +
  9264. +/*
  9265. + * export via nfs
  9266. + */
  9267. +
  9268. +#include <linux/exportfs.h>
  9269. +#include <linux/fs_struct.h>
  9270. +#include <linux/namei.h>
  9271. +#include <linux/nsproxy.h>
  9272. +#include <linux/random.h>
  9273. +#include <linux/writeback.h>
  9274. +#include "../fs/mount.h"
  9275. +#include "aufs.h"
  9276. +
  9277. +union conv {
  9278. +#ifdef CONFIG_AUFS_INO_T_64
  9279. + __u32 a[2];
  9280. +#else
  9281. + __u32 a[1];
  9282. +#endif
  9283. + ino_t ino;
  9284. +};
  9285. +
  9286. +static ino_t decode_ino(__u32 *a)
  9287. +{
  9288. + union conv u;
  9289. +
  9290. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  9291. + u.a[0] = a[0];
  9292. +#ifdef CONFIG_AUFS_INO_T_64
  9293. + u.a[1] = a[1];
  9294. +#endif
  9295. + return u.ino;
  9296. +}
  9297. +
  9298. +static void encode_ino(__u32 *a, ino_t ino)
  9299. +{
  9300. + union conv u;
  9301. +
  9302. + u.ino = ino;
  9303. + a[0] = u.a[0];
  9304. +#ifdef CONFIG_AUFS_INO_T_64
  9305. + a[1] = u.a[1];
  9306. +#endif
  9307. +}
  9308. +
  9309. +/* NFS file handle */
  9310. +enum {
  9311. + Fh_br_id,
  9312. + Fh_sigen,
  9313. +#ifdef CONFIG_AUFS_INO_T_64
  9314. + /* support 64bit inode number */
  9315. + Fh_ino1,
  9316. + Fh_ino2,
  9317. + Fh_dir_ino1,
  9318. + Fh_dir_ino2,
  9319. +#else
  9320. + Fh_ino1,
  9321. + Fh_dir_ino1,
  9322. +#endif
  9323. + Fh_igen,
  9324. + Fh_h_type,
  9325. + Fh_tail,
  9326. +
  9327. + Fh_ino = Fh_ino1,
  9328. + Fh_dir_ino = Fh_dir_ino1
  9329. +};
  9330. +
  9331. +static int au_test_anon(struct dentry *dentry)
  9332. +{
  9333. + /* note: read d_flags without d_lock */
  9334. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  9335. +}
  9336. +
  9337. +int au_test_nfsd(void)
  9338. +{
  9339. + int ret;
  9340. + struct task_struct *tsk = current;
  9341. + char comm[sizeof(tsk->comm)];
  9342. +
  9343. + ret = 0;
  9344. + if (tsk->flags & PF_KTHREAD) {
  9345. + get_task_comm(comm, tsk);
  9346. + ret = !strcmp(comm, "nfsd");
  9347. + }
  9348. +
  9349. + return ret;
  9350. +}
  9351. +
  9352. +/* ---------------------------------------------------------------------- */
  9353. +/* inode generation external table */
  9354. +
  9355. +void au_xigen_inc(struct inode *inode)
  9356. +{
  9357. + loff_t pos;
  9358. + ssize_t sz;
  9359. + __u32 igen;
  9360. + struct super_block *sb;
  9361. + struct au_sbinfo *sbinfo;
  9362. +
  9363. + sb = inode->i_sb;
  9364. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  9365. +
  9366. + sbinfo = au_sbi(sb);
  9367. + pos = inode->i_ino;
  9368. + pos *= sizeof(igen);
  9369. + igen = inode->i_generation + 1;
  9370. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  9371. + sizeof(igen), &pos);
  9372. + if (sz == sizeof(igen))
  9373. + return; /* success */
  9374. +
  9375. + if (unlikely(sz >= 0))
  9376. + AuIOErr("xigen error (%zd)\n", sz);
  9377. +}
  9378. +
  9379. +int au_xigen_new(struct inode *inode)
  9380. +{
  9381. + int err;
  9382. + loff_t pos;
  9383. + ssize_t sz;
  9384. + struct super_block *sb;
  9385. + struct au_sbinfo *sbinfo;
  9386. + struct file *file;
  9387. +
  9388. + err = 0;
  9389. + /* todo: dirty, at mount time */
  9390. + if (inode->i_ino == AUFS_ROOT_INO)
  9391. + goto out;
  9392. + sb = inode->i_sb;
  9393. + SiMustAnyLock(sb);
  9394. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  9395. + goto out;
  9396. +
  9397. + err = -EFBIG;
  9398. + pos = inode->i_ino;
  9399. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  9400. + AuIOErr1("too large i%lld\n", pos);
  9401. + goto out;
  9402. + }
  9403. + pos *= sizeof(inode->i_generation);
  9404. +
  9405. + err = 0;
  9406. + sbinfo = au_sbi(sb);
  9407. + file = sbinfo->si_xigen;
  9408. + BUG_ON(!file);
  9409. +
  9410. + if (vfsub_f_size_read(file)
  9411. + < pos + sizeof(inode->i_generation)) {
  9412. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  9413. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  9414. + sizeof(inode->i_generation), &pos);
  9415. + } else
  9416. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  9417. + sizeof(inode->i_generation), &pos);
  9418. + if (sz == sizeof(inode->i_generation))
  9419. + goto out; /* success */
  9420. +
  9421. + err = sz;
  9422. + if (unlikely(sz >= 0)) {
  9423. + err = -EIO;
  9424. + AuIOErr("xigen error (%zd)\n", sz);
  9425. + }
  9426. +
  9427. +out:
  9428. + return err;
  9429. +}
  9430. +
  9431. +int au_xigen_set(struct super_block *sb, struct file *base)
  9432. +{
  9433. + int err;
  9434. + struct au_sbinfo *sbinfo;
  9435. + struct file *file;
  9436. +
  9437. + SiMustWriteLock(sb);
  9438. +
  9439. + sbinfo = au_sbi(sb);
  9440. + file = au_xino_create2(base, sbinfo->si_xigen);
  9441. + err = PTR_ERR(file);
  9442. + if (IS_ERR(file))
  9443. + goto out;
  9444. + err = 0;
  9445. + if (sbinfo->si_xigen)
  9446. + fput(sbinfo->si_xigen);
  9447. + sbinfo->si_xigen = file;
  9448. +
  9449. +out:
  9450. + return err;
  9451. +}
  9452. +
  9453. +void au_xigen_clr(struct super_block *sb)
  9454. +{
  9455. + struct au_sbinfo *sbinfo;
  9456. +
  9457. + SiMustWriteLock(sb);
  9458. +
  9459. + sbinfo = au_sbi(sb);
  9460. + if (sbinfo->si_xigen) {
  9461. + fput(sbinfo->si_xigen);
  9462. + sbinfo->si_xigen = NULL;
  9463. + }
  9464. +}
  9465. +
  9466. +/* ---------------------------------------------------------------------- */
  9467. +
  9468. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  9469. + ino_t dir_ino)
  9470. +{
  9471. + struct dentry *dentry, *d;
  9472. + struct inode *inode;
  9473. + unsigned int sigen;
  9474. +
  9475. + dentry = NULL;
  9476. + inode = ilookup(sb, ino);
  9477. + if (!inode)
  9478. + goto out;
  9479. +
  9480. + dentry = ERR_PTR(-ESTALE);
  9481. + sigen = au_sigen(sb);
  9482. + if (unlikely(is_bad_inode(inode)
  9483. + || IS_DEADDIR(inode)
  9484. + || sigen != au_iigen(inode, NULL)))
  9485. + goto out_iput;
  9486. +
  9487. + dentry = NULL;
  9488. + if (!dir_ino || S_ISDIR(inode->i_mode))
  9489. + dentry = d_find_alias(inode);
  9490. + else {
  9491. + spin_lock(&inode->i_lock);
  9492. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  9493. + spin_lock(&d->d_lock);
  9494. + if (!au_test_anon(d)
  9495. + && d->d_parent->d_inode->i_ino == dir_ino) {
  9496. + dentry = dget_dlock(d);
  9497. + spin_unlock(&d->d_lock);
  9498. + break;
  9499. + }
  9500. + spin_unlock(&d->d_lock);
  9501. + }
  9502. + spin_unlock(&inode->i_lock);
  9503. + }
  9504. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  9505. + /* need to refresh */
  9506. + dput(dentry);
  9507. + dentry = NULL;
  9508. + }
  9509. +
  9510. +out_iput:
  9511. + iput(inode);
  9512. +out:
  9513. + AuTraceErrPtr(dentry);
  9514. + return dentry;
  9515. +}
  9516. +
  9517. +/* ---------------------------------------------------------------------- */
  9518. +
  9519. +/* todo: dirty? */
  9520. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  9521. +
  9522. +struct au_compare_mnt_args {
  9523. + /* input */
  9524. + struct super_block *sb;
  9525. +
  9526. + /* output */
  9527. + struct vfsmount *mnt;
  9528. +};
  9529. +
  9530. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  9531. +{
  9532. + struct au_compare_mnt_args *a = arg;
  9533. +
  9534. + if (mnt->mnt_sb != a->sb)
  9535. + return 0;
  9536. + a->mnt = mntget(mnt);
  9537. + return 1;
  9538. +}
  9539. +
  9540. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  9541. +{
  9542. + int err;
  9543. + struct path root;
  9544. + struct au_compare_mnt_args args = {
  9545. + .sb = sb
  9546. + };
  9547. +
  9548. + get_fs_root(current->fs, &root);
  9549. + rcu_read_lock();
  9550. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  9551. + rcu_read_unlock();
  9552. + path_put(&root);
  9553. + AuDebugOn(!err);
  9554. + AuDebugOn(!args.mnt);
  9555. + return args.mnt;
  9556. +}
  9557. +
  9558. +struct au_nfsd_si_lock {
  9559. + unsigned int sigen;
  9560. + aufs_bindex_t bindex, br_id;
  9561. + unsigned char force_lock;
  9562. +};
  9563. +
  9564. +static int si_nfsd_read_lock(struct super_block *sb,
  9565. + struct au_nfsd_si_lock *nsi_lock)
  9566. +{
  9567. + int err;
  9568. + aufs_bindex_t bindex;
  9569. +
  9570. + si_read_lock(sb, AuLock_FLUSH);
  9571. +
  9572. + /* branch id may be wrapped around */
  9573. + err = 0;
  9574. + bindex = au_br_index(sb, nsi_lock->br_id);
  9575. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  9576. + goto out; /* success */
  9577. +
  9578. + err = -ESTALE;
  9579. + bindex = -1;
  9580. + if (!nsi_lock->force_lock)
  9581. + si_read_unlock(sb);
  9582. +
  9583. +out:
  9584. + nsi_lock->bindex = bindex;
  9585. + return err;
  9586. +}
  9587. +
  9588. +struct find_name_by_ino {
  9589. + struct dir_context ctx;
  9590. + int called, found;
  9591. + ino_t ino;
  9592. + char *name;
  9593. + int namelen;
  9594. +};
  9595. +
  9596. +static int
  9597. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  9598. + loff_t offset, u64 ino, unsigned int d_type)
  9599. +{
  9600. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  9601. + ctx);
  9602. +
  9603. + a->called++;
  9604. + if (a->ino != ino)
  9605. + return 0;
  9606. +
  9607. + memcpy(a->name, name, namelen);
  9608. + a->namelen = namelen;
  9609. + a->found = 1;
  9610. + return 1;
  9611. +}
  9612. +
  9613. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  9614. + struct au_nfsd_si_lock *nsi_lock)
  9615. +{
  9616. + struct dentry *dentry, *parent;
  9617. + struct file *file;
  9618. + struct inode *dir;
  9619. + struct find_name_by_ino arg = {
  9620. + .ctx = {
  9621. + .actor = find_name_by_ino
  9622. + }
  9623. + };
  9624. + int err;
  9625. +
  9626. + parent = path->dentry;
  9627. + if (nsi_lock)
  9628. + si_read_unlock(parent->d_sb);
  9629. + file = vfsub_dentry_open(path, au_dir_roflags);
  9630. + dentry = (void *)file;
  9631. + if (IS_ERR(file))
  9632. + goto out;
  9633. +
  9634. + dentry = ERR_PTR(-ENOMEM);
  9635. + arg.name = (void *)__get_free_page(GFP_NOFS);
  9636. + if (unlikely(!arg.name))
  9637. + goto out_file;
  9638. + arg.ino = ino;
  9639. + arg.found = 0;
  9640. + do {
  9641. + arg.called = 0;
  9642. + /* smp_mb(); */
  9643. + err = vfsub_iterate_dir(file, &arg.ctx);
  9644. + } while (!err && !arg.found && arg.called);
  9645. + dentry = ERR_PTR(err);
  9646. + if (unlikely(err))
  9647. + goto out_name;
  9648. + /* instead of ENOENT */
  9649. + dentry = ERR_PTR(-ESTALE);
  9650. + if (!arg.found)
  9651. + goto out_name;
  9652. +
  9653. + /* do not call vfsub_lkup_one() */
  9654. + dir = parent->d_inode;
  9655. + mutex_lock(&dir->i_mutex);
  9656. + dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
  9657. + mutex_unlock(&dir->i_mutex);
  9658. + AuTraceErrPtr(dentry);
  9659. + if (IS_ERR(dentry))
  9660. + goto out_name;
  9661. + AuDebugOn(au_test_anon(dentry));
  9662. + if (unlikely(!dentry->d_inode)) {
  9663. + dput(dentry);
  9664. + dentry = ERR_PTR(-ENOENT);
  9665. + }
  9666. +
  9667. +out_name:
  9668. + free_page((unsigned long)arg.name);
  9669. +out_file:
  9670. + fput(file);
  9671. +out:
  9672. + if (unlikely(nsi_lock
  9673. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  9674. + if (!IS_ERR(dentry)) {
  9675. + dput(dentry);
  9676. + dentry = ERR_PTR(-ESTALE);
  9677. + }
  9678. + AuTraceErrPtr(dentry);
  9679. + return dentry;
  9680. +}
  9681. +
  9682. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  9683. + ino_t dir_ino,
  9684. + struct au_nfsd_si_lock *nsi_lock)
  9685. +{
  9686. + struct dentry *dentry;
  9687. + struct path path;
  9688. +
  9689. + if (dir_ino != AUFS_ROOT_INO) {
  9690. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  9691. + dentry = path.dentry;
  9692. + if (!path.dentry || IS_ERR(path.dentry))
  9693. + goto out;
  9694. + AuDebugOn(au_test_anon(path.dentry));
  9695. + } else
  9696. + path.dentry = dget(sb->s_root);
  9697. +
  9698. + path.mnt = au_mnt_get(sb);
  9699. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  9700. + path_put(&path);
  9701. +
  9702. +out:
  9703. + AuTraceErrPtr(dentry);
  9704. + return dentry;
  9705. +}
  9706. +
  9707. +/* ---------------------------------------------------------------------- */
  9708. +
  9709. +static int h_acceptable(void *expv, struct dentry *dentry)
  9710. +{
  9711. + return 1;
  9712. +}
  9713. +
  9714. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  9715. + char *buf, int len, struct super_block *sb)
  9716. +{
  9717. + char *p;
  9718. + int n;
  9719. + struct path path;
  9720. +
  9721. + p = d_path(h_rootpath, buf, len);
  9722. + if (IS_ERR(p))
  9723. + goto out;
  9724. + n = strlen(p);
  9725. +
  9726. + path.mnt = h_rootpath->mnt;
  9727. + path.dentry = h_parent;
  9728. + p = d_path(&path, buf, len);
  9729. + if (IS_ERR(p))
  9730. + goto out;
  9731. + if (n != 1)
  9732. + p += n;
  9733. +
  9734. + path.mnt = au_mnt_get(sb);
  9735. + path.dentry = sb->s_root;
  9736. + p = d_path(&path, buf, len - strlen(p));
  9737. + mntput(path.mnt);
  9738. + if (IS_ERR(p))
  9739. + goto out;
  9740. + if (n != 1)
  9741. + p[strlen(p)] = '/';
  9742. +
  9743. +out:
  9744. + AuTraceErrPtr(p);
  9745. + return p;
  9746. +}
  9747. +
  9748. +static
  9749. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  9750. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  9751. +{
  9752. + struct dentry *dentry, *h_parent, *root;
  9753. + struct super_block *h_sb;
  9754. + char *pathname, *p;
  9755. + struct vfsmount *h_mnt;
  9756. + struct au_branch *br;
  9757. + int err;
  9758. + struct path path;
  9759. +
  9760. + br = au_sbr(sb, nsi_lock->bindex);
  9761. + h_mnt = au_br_mnt(br);
  9762. + h_sb = h_mnt->mnt_sb;
  9763. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  9764. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  9765. + fh_len - Fh_tail, fh[Fh_h_type],
  9766. + h_acceptable, /*context*/NULL);
  9767. + dentry = h_parent;
  9768. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  9769. + AuWarn1("%s decode_fh failed, %ld\n",
  9770. + au_sbtype(h_sb), PTR_ERR(h_parent));
  9771. + goto out;
  9772. + }
  9773. + dentry = NULL;
  9774. + if (unlikely(au_test_anon(h_parent))) {
  9775. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  9776. + au_sbtype(h_sb));
  9777. + goto out_h_parent;
  9778. + }
  9779. +
  9780. + dentry = ERR_PTR(-ENOMEM);
  9781. + pathname = (void *)__get_free_page(GFP_NOFS);
  9782. + if (unlikely(!pathname))
  9783. + goto out_h_parent;
  9784. +
  9785. + root = sb->s_root;
  9786. + path.mnt = h_mnt;
  9787. + di_read_lock_parent(root, !AuLock_IR);
  9788. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  9789. + di_read_unlock(root, !AuLock_IR);
  9790. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  9791. + dentry = (void *)p;
  9792. + if (IS_ERR(p))
  9793. + goto out_pathname;
  9794. +
  9795. + si_read_unlock(sb);
  9796. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  9797. + dentry = ERR_PTR(err);
  9798. + if (unlikely(err))
  9799. + goto out_relock;
  9800. +
  9801. + dentry = ERR_PTR(-ENOENT);
  9802. + AuDebugOn(au_test_anon(path.dentry));
  9803. + if (unlikely(!path.dentry->d_inode))
  9804. + goto out_path;
  9805. +
  9806. + if (ino != path.dentry->d_inode->i_ino)
  9807. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  9808. + else
  9809. + dentry = dget(path.dentry);
  9810. +
  9811. +out_path:
  9812. + path_put(&path);
  9813. +out_relock:
  9814. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  9815. + if (!IS_ERR(dentry)) {
  9816. + dput(dentry);
  9817. + dentry = ERR_PTR(-ESTALE);
  9818. + }
  9819. +out_pathname:
  9820. + free_page((unsigned long)pathname);
  9821. +out_h_parent:
  9822. + dput(h_parent);
  9823. +out:
  9824. + AuTraceErrPtr(dentry);
  9825. + return dentry;
  9826. +}
  9827. +
  9828. +/* ---------------------------------------------------------------------- */
  9829. +
  9830. +static struct dentry *
  9831. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  9832. + int fh_type)
  9833. +{
  9834. + struct dentry *dentry;
  9835. + __u32 *fh = fid->raw;
  9836. + struct au_branch *br;
  9837. + ino_t ino, dir_ino;
  9838. + struct au_nfsd_si_lock nsi_lock = {
  9839. + .force_lock = 0
  9840. + };
  9841. +
  9842. + dentry = ERR_PTR(-ESTALE);
  9843. + /* it should never happen, but the file handle is unreliable */
  9844. + if (unlikely(fh_len < Fh_tail))
  9845. + goto out;
  9846. + nsi_lock.sigen = fh[Fh_sigen];
  9847. + nsi_lock.br_id = fh[Fh_br_id];
  9848. +
  9849. + /* branch id may be wrapped around */
  9850. + br = NULL;
  9851. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  9852. + goto out;
  9853. + nsi_lock.force_lock = 1;
  9854. +
  9855. + /* is this inode still cached? */
  9856. + ino = decode_ino(fh + Fh_ino);
  9857. + /* it should never happen */
  9858. + if (unlikely(ino == AUFS_ROOT_INO))
  9859. + goto out;
  9860. +
  9861. + dir_ino = decode_ino(fh + Fh_dir_ino);
  9862. + dentry = decode_by_ino(sb, ino, dir_ino);
  9863. + if (IS_ERR(dentry))
  9864. + goto out_unlock;
  9865. + if (dentry)
  9866. + goto accept;
  9867. +
  9868. + /* is the parent dir cached? */
  9869. + br = au_sbr(sb, nsi_lock.bindex);
  9870. + atomic_inc(&br->br_count);
  9871. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  9872. + if (IS_ERR(dentry))
  9873. + goto out_unlock;
  9874. + if (dentry)
  9875. + goto accept;
  9876. +
  9877. + /* lookup path */
  9878. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  9879. + if (IS_ERR(dentry))
  9880. + goto out_unlock;
  9881. + if (unlikely(!dentry))
  9882. + /* todo?: make it ESTALE */
  9883. + goto out_unlock;
  9884. +
  9885. +accept:
  9886. + if (!au_digen_test(dentry, au_sigen(sb))
  9887. + && dentry->d_inode->i_generation == fh[Fh_igen])
  9888. + goto out_unlock; /* success */
  9889. +
  9890. + dput(dentry);
  9891. + dentry = ERR_PTR(-ESTALE);
  9892. +out_unlock:
  9893. + if (br)
  9894. + atomic_dec(&br->br_count);
  9895. + si_read_unlock(sb);
  9896. +out:
  9897. + AuTraceErrPtr(dentry);
  9898. + return dentry;
  9899. +}
  9900. +
  9901. +#if 0 /* reserved for future use */
  9902. +/* support subtreecheck option */
  9903. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  9904. + int fh_len, int fh_type)
  9905. +{
  9906. + struct dentry *parent;
  9907. + __u32 *fh = fid->raw;
  9908. + ino_t dir_ino;
  9909. +
  9910. + dir_ino = decode_ino(fh + Fh_dir_ino);
  9911. + parent = decode_by_ino(sb, dir_ino, 0);
  9912. + if (IS_ERR(parent))
  9913. + goto out;
  9914. + if (!parent)
  9915. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  9916. + dir_ino, fh, fh_len);
  9917. +
  9918. +out:
  9919. + AuTraceErrPtr(parent);
  9920. + return parent;
  9921. +}
  9922. +#endif
  9923. +
  9924. +/* ---------------------------------------------------------------------- */
  9925. +
  9926. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  9927. + struct inode *dir)
  9928. +{
  9929. + int err;
  9930. + aufs_bindex_t bindex;
  9931. + struct super_block *sb, *h_sb;
  9932. + struct dentry *dentry, *parent, *h_parent;
  9933. + struct inode *h_dir;
  9934. + struct au_branch *br;
  9935. +
  9936. + err = -ENOSPC;
  9937. + if (unlikely(*max_len <= Fh_tail)) {
  9938. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  9939. + goto out;
  9940. + }
  9941. +
  9942. + err = FILEID_ROOT;
  9943. + if (inode->i_ino == AUFS_ROOT_INO) {
  9944. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  9945. + goto out;
  9946. + }
  9947. +
  9948. + h_parent = NULL;
  9949. + sb = inode->i_sb;
  9950. + err = si_read_lock(sb, AuLock_FLUSH);
  9951. + if (unlikely(err))
  9952. + goto out;
  9953. +
  9954. +#ifdef CONFIG_AUFS_DEBUG
  9955. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  9956. + AuWarn1("NFS-exporting requires xino\n");
  9957. +#endif
  9958. + err = -EIO;
  9959. + parent = NULL;
  9960. + ii_read_lock_child(inode);
  9961. + bindex = au_ibstart(inode);
  9962. + if (!dir) {
  9963. + dentry = d_find_any_alias(inode);
  9964. + if (unlikely(!dentry))
  9965. + goto out_unlock;
  9966. + AuDebugOn(au_test_anon(dentry));
  9967. + parent = dget_parent(dentry);
  9968. + dput(dentry);
  9969. + if (unlikely(!parent))
  9970. + goto out_unlock;
  9971. + dir = parent->d_inode;
  9972. + }
  9973. +
  9974. + ii_read_lock_parent(dir);
  9975. + h_dir = au_h_iptr(dir, bindex);
  9976. + ii_read_unlock(dir);
  9977. + if (unlikely(!h_dir))
  9978. + goto out_parent;
  9979. + h_parent = d_find_any_alias(h_dir);
  9980. + if (unlikely(!h_parent))
  9981. + goto out_hparent;
  9982. +
  9983. + err = -EPERM;
  9984. + br = au_sbr(sb, bindex);
  9985. + h_sb = au_br_sb(br);
  9986. + if (unlikely(!h_sb->s_export_op)) {
  9987. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  9988. + goto out_hparent;
  9989. + }
  9990. +
  9991. + fh[Fh_br_id] = br->br_id;
  9992. + fh[Fh_sigen] = au_sigen(sb);
  9993. + encode_ino(fh + Fh_ino, inode->i_ino);
  9994. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  9995. + fh[Fh_igen] = inode->i_generation;
  9996. +
  9997. + *max_len -= Fh_tail;
  9998. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  9999. + max_len,
  10000. + /*connectable or subtreecheck*/0);
  10001. + err = fh[Fh_h_type];
  10002. + *max_len += Fh_tail;
  10003. + /* todo: macros? */
  10004. + if (err != FILEID_INVALID)
  10005. + err = 99;
  10006. + else
  10007. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  10008. +
  10009. +out_hparent:
  10010. + dput(h_parent);
  10011. +out_parent:
  10012. + dput(parent);
  10013. +out_unlock:
  10014. + ii_read_unlock(inode);
  10015. + si_read_unlock(sb);
  10016. +out:
  10017. + if (unlikely(err < 0))
  10018. + err = FILEID_INVALID;
  10019. + return err;
  10020. +}
  10021. +
  10022. +/* ---------------------------------------------------------------------- */
  10023. +
  10024. +static int aufs_commit_metadata(struct inode *inode)
  10025. +{
  10026. + int err;
  10027. + aufs_bindex_t bindex;
  10028. + struct super_block *sb;
  10029. + struct inode *h_inode;
  10030. + int (*f)(struct inode *inode);
  10031. +
  10032. + sb = inode->i_sb;
  10033. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10034. + ii_write_lock_child(inode);
  10035. + bindex = au_ibstart(inode);
  10036. + AuDebugOn(bindex < 0);
  10037. + h_inode = au_h_iptr(inode, bindex);
  10038. +
  10039. + f = h_inode->i_sb->s_export_op->commit_metadata;
  10040. + if (f)
  10041. + err = f(h_inode);
  10042. + else {
  10043. + struct writeback_control wbc = {
  10044. + .sync_mode = WB_SYNC_ALL,
  10045. + .nr_to_write = 0 /* metadata only */
  10046. + };
  10047. +
  10048. + err = sync_inode(h_inode, &wbc);
  10049. + }
  10050. +
  10051. + au_cpup_attr_timesizes(inode);
  10052. + ii_write_unlock(inode);
  10053. + si_read_unlock(sb);
  10054. + return err;
  10055. +}
  10056. +
  10057. +/* ---------------------------------------------------------------------- */
  10058. +
  10059. +static struct export_operations aufs_export_op = {
  10060. + .fh_to_dentry = aufs_fh_to_dentry,
  10061. + /* .fh_to_parent = aufs_fh_to_parent, */
  10062. + .encode_fh = aufs_encode_fh,
  10063. + .commit_metadata = aufs_commit_metadata
  10064. +};
  10065. +
  10066. +void au_export_init(struct super_block *sb)
  10067. +{
  10068. + struct au_sbinfo *sbinfo;
  10069. + __u32 u;
  10070. +
  10071. + sb->s_export_op = &aufs_export_op;
  10072. + sbinfo = au_sbi(sb);
  10073. + sbinfo->si_xigen = NULL;
  10074. + get_random_bytes(&u, sizeof(u));
  10075. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  10076. + atomic_set(&sbinfo->si_xigen_next, u);
  10077. +}
  10078. diff -Nur linux-4.0.4.orig/fs/aufs/fhsm.c linux-4.0.4/fs/aufs/fhsm.c
  10079. --- linux-4.0.4.orig/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
  10080. +++ linux-4.0.4/fs/aufs/fhsm.c 2015-05-30 22:11:30.000000000 +0200
  10081. @@ -0,0 +1,412 @@
  10082. +/*
  10083. + * Copyright (C) 2011-2015 Junjiro R. Okajima
  10084. + */
  10085. +
  10086. +/*
  10087. + * File-based Hierarchy Storage Management
  10088. + */
  10089. +
  10090. +#include <linux/anon_inodes.h>
  10091. +#include <linux/poll.h>
  10092. +#include <linux/seq_file.h>
  10093. +#include <linux/statfs.h>
  10094. +#include "aufs.h"
  10095. +
  10096. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  10097. +{
  10098. + struct au_sbinfo *sbinfo;
  10099. + struct au_fhsm *fhsm;
  10100. +
  10101. + SiMustAnyLock(sb);
  10102. +
  10103. + sbinfo = au_sbi(sb);
  10104. + fhsm = &sbinfo->si_fhsm;
  10105. + AuDebugOn(!fhsm);
  10106. + return fhsm->fhsm_bottom;
  10107. +}
  10108. +
  10109. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  10110. +{
  10111. + struct au_sbinfo *sbinfo;
  10112. + struct au_fhsm *fhsm;
  10113. +
  10114. + SiMustWriteLock(sb);
  10115. +
  10116. + sbinfo = au_sbi(sb);
  10117. + fhsm = &sbinfo->si_fhsm;
  10118. + AuDebugOn(!fhsm);
  10119. + fhsm->fhsm_bottom = bindex;
  10120. +}
  10121. +
  10122. +/* ---------------------------------------------------------------------- */
  10123. +
  10124. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  10125. +{
  10126. + struct au_br_fhsm *bf;
  10127. +
  10128. + bf = br->br_fhsm;
  10129. + MtxMustLock(&bf->bf_lock);
  10130. +
  10131. + return !bf->bf_readable
  10132. + || time_after(jiffies,
  10133. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  10134. +}
  10135. +
  10136. +/* ---------------------------------------------------------------------- */
  10137. +
  10138. +static void au_fhsm_notify(struct super_block *sb, int val)
  10139. +{
  10140. + struct au_sbinfo *sbinfo;
  10141. + struct au_fhsm *fhsm;
  10142. +
  10143. + SiMustAnyLock(sb);
  10144. +
  10145. + sbinfo = au_sbi(sb);
  10146. + fhsm = &sbinfo->si_fhsm;
  10147. + if (au_fhsm_pid(fhsm)
  10148. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  10149. + atomic_set(&fhsm->fhsm_readable, val);
  10150. + if (val)
  10151. + wake_up(&fhsm->fhsm_wqh);
  10152. + }
  10153. +}
  10154. +
  10155. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  10156. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  10157. +{
  10158. + int err;
  10159. + struct au_branch *br;
  10160. + struct au_br_fhsm *bf;
  10161. +
  10162. + br = au_sbr(sb, bindex);
  10163. + AuDebugOn(au_br_rdonly(br));
  10164. + bf = br->br_fhsm;
  10165. + AuDebugOn(!bf);
  10166. +
  10167. + if (do_lock)
  10168. + mutex_lock(&bf->bf_lock);
  10169. + else
  10170. + MtxMustLock(&bf->bf_lock);
  10171. +
  10172. + /* sb->s_root for NFS is unreliable */
  10173. + err = au_br_stfs(br, &bf->bf_stfs);
  10174. + if (unlikely(err)) {
  10175. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  10176. + goto out;
  10177. + }
  10178. +
  10179. + bf->bf_jiffy = jiffies;
  10180. + bf->bf_readable = 1;
  10181. + if (do_notify)
  10182. + au_fhsm_notify(sb, /*val*/1);
  10183. + if (rstfs)
  10184. + *rstfs = bf->bf_stfs;
  10185. +
  10186. +out:
  10187. + if (do_lock)
  10188. + mutex_unlock(&bf->bf_lock);
  10189. + au_fhsm_notify(sb, /*val*/1);
  10190. +
  10191. + return err;
  10192. +}
  10193. +
  10194. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  10195. +{
  10196. + int err;
  10197. + struct au_sbinfo *sbinfo;
  10198. + struct au_fhsm *fhsm;
  10199. + struct au_branch *br;
  10200. + struct au_br_fhsm *bf;
  10201. +
  10202. + AuDbg("b%d, force %d\n", bindex, force);
  10203. + SiMustAnyLock(sb);
  10204. +
  10205. + sbinfo = au_sbi(sb);
  10206. + fhsm = &sbinfo->si_fhsm;
  10207. + if (!au_ftest_si(sbinfo, FHSM)
  10208. + || fhsm->fhsm_bottom == bindex)
  10209. + return;
  10210. +
  10211. + br = au_sbr(sb, bindex);
  10212. + bf = br->br_fhsm;
  10213. + AuDebugOn(!bf);
  10214. + mutex_lock(&bf->bf_lock);
  10215. + if (force
  10216. + || au_fhsm_pid(fhsm)
  10217. + || au_fhsm_test_jiffy(sbinfo, br))
  10218. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  10219. + /*do_notify*/1);
  10220. + mutex_unlock(&bf->bf_lock);
  10221. +}
  10222. +
  10223. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  10224. +{
  10225. + aufs_bindex_t bindex, bend;
  10226. + struct au_branch *br;
  10227. +
  10228. + /* exclude the bottom */
  10229. + bend = au_fhsm_bottom(sb);
  10230. + for (bindex = 0; bindex < bend; bindex++) {
  10231. + br = au_sbr(sb, bindex);
  10232. + if (au_br_fhsm(br->br_perm))
  10233. + au_fhsm_wrote(sb, bindex, force);
  10234. + }
  10235. +}
  10236. +
  10237. +/* ---------------------------------------------------------------------- */
  10238. +
  10239. +static unsigned int au_fhsm_poll(struct file *file,
  10240. + struct poll_table_struct *wait)
  10241. +{
  10242. + unsigned int mask;
  10243. + struct au_sbinfo *sbinfo;
  10244. + struct au_fhsm *fhsm;
  10245. +
  10246. + mask = 0;
  10247. + sbinfo = file->private_data;
  10248. + fhsm = &sbinfo->si_fhsm;
  10249. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  10250. + if (atomic_read(&fhsm->fhsm_readable))
  10251. + mask = POLLIN /* | POLLRDNORM */;
  10252. +
  10253. + AuTraceErr((int)mask);
  10254. + return mask;
  10255. +}
  10256. +
  10257. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  10258. + struct aufs_stfs *stfs, __s16 brid)
  10259. +{
  10260. + int err;
  10261. +
  10262. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  10263. + if (!err)
  10264. + err = __put_user(brid, &stbr->brid);
  10265. + if (unlikely(err))
  10266. + err = -EFAULT;
  10267. +
  10268. + return err;
  10269. +}
  10270. +
  10271. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  10272. + struct aufs_stbr __user *stbr, size_t count)
  10273. +{
  10274. + ssize_t err;
  10275. + int nstbr;
  10276. + aufs_bindex_t bindex, bend;
  10277. + struct au_branch *br;
  10278. + struct au_br_fhsm *bf;
  10279. +
  10280. + /* except the bottom branch */
  10281. + err = 0;
  10282. + nstbr = 0;
  10283. + bend = au_fhsm_bottom(sb);
  10284. + for (bindex = 0; !err && bindex < bend; bindex++) {
  10285. + br = au_sbr(sb, bindex);
  10286. + if (!au_br_fhsm(br->br_perm))
  10287. + continue;
  10288. +
  10289. + bf = br->br_fhsm;
  10290. + mutex_lock(&bf->bf_lock);
  10291. + if (bf->bf_readable) {
  10292. + err = -EFAULT;
  10293. + if (count >= sizeof(*stbr))
  10294. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  10295. + br->br_id);
  10296. + if (!err) {
  10297. + bf->bf_readable = 0;
  10298. + count -= sizeof(*stbr);
  10299. + nstbr++;
  10300. + }
  10301. + }
  10302. + mutex_unlock(&bf->bf_lock);
  10303. + }
  10304. + if (!err)
  10305. + err = sizeof(*stbr) * nstbr;
  10306. +
  10307. + return err;
  10308. +}
  10309. +
  10310. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  10311. + loff_t *pos)
  10312. +{
  10313. + ssize_t err;
  10314. + int readable;
  10315. + aufs_bindex_t nfhsm, bindex, bend;
  10316. + struct au_sbinfo *sbinfo;
  10317. + struct au_fhsm *fhsm;
  10318. + struct au_branch *br;
  10319. + struct super_block *sb;
  10320. +
  10321. + err = 0;
  10322. + sbinfo = file->private_data;
  10323. + fhsm = &sbinfo->si_fhsm;
  10324. +need_data:
  10325. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  10326. + if (!atomic_read(&fhsm->fhsm_readable)) {
  10327. + if (vfsub_file_flags(file) & O_NONBLOCK)
  10328. + err = -EAGAIN;
  10329. + else
  10330. + err = wait_event_interruptible_locked_irq
  10331. + (fhsm->fhsm_wqh,
  10332. + atomic_read(&fhsm->fhsm_readable));
  10333. + }
  10334. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  10335. + if (unlikely(err))
  10336. + goto out;
  10337. +
  10338. + /* sb may already be dead */
  10339. + au_rw_read_lock(&sbinfo->si_rwsem);
  10340. + readable = atomic_read(&fhsm->fhsm_readable);
  10341. + if (readable > 0) {
  10342. + sb = sbinfo->si_sb;
  10343. + AuDebugOn(!sb);
  10344. + /* exclude the bottom branch */
  10345. + nfhsm = 0;
  10346. + bend = au_fhsm_bottom(sb);
  10347. + for (bindex = 0; bindex < bend; bindex++) {
  10348. + br = au_sbr(sb, bindex);
  10349. + if (au_br_fhsm(br->br_perm))
  10350. + nfhsm++;
  10351. + }
  10352. + err = -EMSGSIZE;
  10353. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  10354. + atomic_set(&fhsm->fhsm_readable, 0);
  10355. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  10356. + count);
  10357. + }
  10358. + }
  10359. + au_rw_read_unlock(&sbinfo->si_rwsem);
  10360. + if (!readable)
  10361. + goto need_data;
  10362. +
  10363. +out:
  10364. + return err;
  10365. +}
  10366. +
  10367. +static int au_fhsm_release(struct inode *inode, struct file *file)
  10368. +{
  10369. + struct au_sbinfo *sbinfo;
  10370. + struct au_fhsm *fhsm;
  10371. +
  10372. + /* sb may already be dead */
  10373. + sbinfo = file->private_data;
  10374. + fhsm = &sbinfo->si_fhsm;
  10375. + spin_lock(&fhsm->fhsm_spin);
  10376. + fhsm->fhsm_pid = 0;
  10377. + spin_unlock(&fhsm->fhsm_spin);
  10378. + kobject_put(&sbinfo->si_kobj);
  10379. +
  10380. + return 0;
  10381. +}
  10382. +
  10383. +static const struct file_operations au_fhsm_fops = {
  10384. + .owner = THIS_MODULE,
  10385. + .llseek = noop_llseek,
  10386. + .read = au_fhsm_read,
  10387. + .poll = au_fhsm_poll,
  10388. + .release = au_fhsm_release
  10389. +};
  10390. +
  10391. +int au_fhsm_fd(struct super_block *sb, int oflags)
  10392. +{
  10393. + int err, fd;
  10394. + struct au_sbinfo *sbinfo;
  10395. + struct au_fhsm *fhsm;
  10396. +
  10397. + err = -EPERM;
  10398. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  10399. + goto out;
  10400. +
  10401. + err = -EINVAL;
  10402. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  10403. + goto out;
  10404. +
  10405. + err = 0;
  10406. + sbinfo = au_sbi(sb);
  10407. + fhsm = &sbinfo->si_fhsm;
  10408. + spin_lock(&fhsm->fhsm_spin);
  10409. + if (!fhsm->fhsm_pid)
  10410. + fhsm->fhsm_pid = current->pid;
  10411. + else
  10412. + err = -EBUSY;
  10413. + spin_unlock(&fhsm->fhsm_spin);
  10414. + if (unlikely(err))
  10415. + goto out;
  10416. +
  10417. + oflags |= O_RDONLY;
  10418. + /* oflags |= FMODE_NONOTIFY; */
  10419. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  10420. + err = fd;
  10421. + if (unlikely(fd < 0))
  10422. + goto out_pid;
  10423. +
  10424. + /* succeed reglardless 'fhsm' status */
  10425. + kobject_get(&sbinfo->si_kobj);
  10426. + si_noflush_read_lock(sb);
  10427. + if (au_ftest_si(sbinfo, FHSM))
  10428. + au_fhsm_wrote_all(sb, /*force*/0);
  10429. + si_read_unlock(sb);
  10430. + goto out; /* success */
  10431. +
  10432. +out_pid:
  10433. + spin_lock(&fhsm->fhsm_spin);
  10434. + fhsm->fhsm_pid = 0;
  10435. + spin_unlock(&fhsm->fhsm_spin);
  10436. +out:
  10437. + AuTraceErr(err);
  10438. + return err;
  10439. +}
  10440. +
  10441. +/* ---------------------------------------------------------------------- */
  10442. +
  10443. +int au_fhsm_br_alloc(struct au_branch *br)
  10444. +{
  10445. + int err;
  10446. +
  10447. + err = 0;
  10448. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  10449. + if (br->br_fhsm)
  10450. + au_br_fhsm_init(br->br_fhsm);
  10451. + else
  10452. + err = -ENOMEM;
  10453. +
  10454. + return err;
  10455. +}
  10456. +
  10457. +/* ---------------------------------------------------------------------- */
  10458. +
  10459. +void au_fhsm_fin(struct super_block *sb)
  10460. +{
  10461. + au_fhsm_notify(sb, /*val*/-1);
  10462. +}
  10463. +
  10464. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  10465. +{
  10466. + struct au_fhsm *fhsm;
  10467. +
  10468. + fhsm = &sbinfo->si_fhsm;
  10469. + spin_lock_init(&fhsm->fhsm_spin);
  10470. + init_waitqueue_head(&fhsm->fhsm_wqh);
  10471. + atomic_set(&fhsm->fhsm_readable, 0);
  10472. + fhsm->fhsm_expire
  10473. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  10474. + fhsm->fhsm_bottom = -1;
  10475. +}
  10476. +
  10477. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  10478. +{
  10479. + sbinfo->si_fhsm.fhsm_expire
  10480. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  10481. +}
  10482. +
  10483. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  10484. +{
  10485. + unsigned int u;
  10486. +
  10487. + if (!au_ftest_si(sbinfo, FHSM))
  10488. + return;
  10489. +
  10490. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  10491. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  10492. + seq_printf(seq, ",fhsm_sec=%u", u);
  10493. +}
  10494. diff -Nur linux-4.0.4.orig/fs/aufs/file.c linux-4.0.4/fs/aufs/file.c
  10495. --- linux-4.0.4.orig/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
  10496. +++ linux-4.0.4/fs/aufs/file.c 2015-05-30 22:11:31.000000000 +0200
  10497. @@ -0,0 +1,831 @@
  10498. +/*
  10499. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  10500. + */
  10501. +
  10502. +/*
  10503. + * handling file/dir, and address_space operation
  10504. + */
  10505. +
  10506. +#ifdef CONFIG_AUFS_DEBUG
  10507. +#include <linux/migrate.h>
  10508. +#endif
  10509. +#include <linux/pagemap.h>
  10510. +#include "aufs.h"
  10511. +
  10512. +/* drop flags for writing */
  10513. +unsigned int au_file_roflags(unsigned int flags)
  10514. +{
  10515. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  10516. + flags |= O_RDONLY | O_NOATIME;
  10517. + return flags;
  10518. +}
  10519. +
  10520. +/* common functions to regular file and dir */
  10521. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  10522. + struct file *file, int force_wr)
  10523. +{
  10524. + struct file *h_file;
  10525. + struct dentry *h_dentry;
  10526. + struct inode *h_inode;
  10527. + struct super_block *sb;
  10528. + struct au_branch *br;
  10529. + struct path h_path;
  10530. + int err;
  10531. +
  10532. + /* a race condition can happen between open and unlink/rmdir */
  10533. + h_file = ERR_PTR(-ENOENT);
  10534. + h_dentry = au_h_dptr(dentry, bindex);
  10535. + if (au_test_nfsd() && !h_dentry)
  10536. + goto out;
  10537. + h_inode = h_dentry->d_inode;
  10538. + if (au_test_nfsd() && !h_inode)
  10539. + goto out;
  10540. + spin_lock(&h_dentry->d_lock);
  10541. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  10542. + || !h_inode
  10543. + /* || !dentry->d_inode->i_nlink */
  10544. + ;
  10545. + spin_unlock(&h_dentry->d_lock);
  10546. + if (unlikely(err))
  10547. + goto out;
  10548. +
  10549. + sb = dentry->d_sb;
  10550. + br = au_sbr(sb, bindex);
  10551. + err = au_br_test_oflag(flags, br);
  10552. + h_file = ERR_PTR(err);
  10553. + if (unlikely(err))
  10554. + goto out;
  10555. +
  10556. + /* drop flags for writing */
  10557. + if (au_test_ro(sb, bindex, dentry->d_inode)) {
  10558. + if (force_wr && !(flags & O_WRONLY))
  10559. + force_wr = 0;
  10560. + flags = au_file_roflags(flags);
  10561. + if (force_wr) {
  10562. + h_file = ERR_PTR(-EROFS);
  10563. + flags = au_file_roflags(flags);
  10564. + if (unlikely(vfsub_native_ro(h_inode)
  10565. + || IS_APPEND(h_inode)))
  10566. + goto out;
  10567. + flags &= ~O_ACCMODE;
  10568. + flags |= O_WRONLY;
  10569. + }
  10570. + }
  10571. + flags &= ~O_CREAT;
  10572. + atomic_inc(&br->br_count);
  10573. + h_path.dentry = h_dentry;
  10574. + h_path.mnt = au_br_mnt(br);
  10575. + h_file = vfsub_dentry_open(&h_path, flags);
  10576. + if (IS_ERR(h_file))
  10577. + goto out_br;
  10578. +
  10579. + if (flags & __FMODE_EXEC) {
  10580. + err = deny_write_access(h_file);
  10581. + if (unlikely(err)) {
  10582. + fput(h_file);
  10583. + h_file = ERR_PTR(err);
  10584. + goto out_br;
  10585. + }
  10586. + }
  10587. + fsnotify_open(h_file);
  10588. + goto out; /* success */
  10589. +
  10590. +out_br:
  10591. + atomic_dec(&br->br_count);
  10592. +out:
  10593. + return h_file;
  10594. +}
  10595. +
  10596. +static int au_cmoo(struct dentry *dentry)
  10597. +{
  10598. + int err, cmoo;
  10599. + unsigned int udba;
  10600. + struct path h_path;
  10601. + struct au_pin pin;
  10602. + struct au_cp_generic cpg = {
  10603. + .dentry = dentry,
  10604. + .bdst = -1,
  10605. + .bsrc = -1,
  10606. + .len = -1,
  10607. + .pin = &pin,
  10608. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  10609. + };
  10610. + struct inode *delegated;
  10611. + struct super_block *sb;
  10612. + struct au_sbinfo *sbinfo;
  10613. + struct au_fhsm *fhsm;
  10614. + pid_t pid;
  10615. + struct au_branch *br;
  10616. + struct dentry *parent;
  10617. + struct au_hinode *hdir;
  10618. +
  10619. + DiMustWriteLock(dentry);
  10620. + IiMustWriteLock(dentry->d_inode);
  10621. +
  10622. + err = 0;
  10623. + if (IS_ROOT(dentry))
  10624. + goto out;
  10625. + cpg.bsrc = au_dbstart(dentry);
  10626. + if (!cpg.bsrc)
  10627. + goto out;
  10628. +
  10629. + sb = dentry->d_sb;
  10630. + sbinfo = au_sbi(sb);
  10631. + fhsm = &sbinfo->si_fhsm;
  10632. + pid = au_fhsm_pid(fhsm);
  10633. + if (pid
  10634. + && (current->pid == pid
  10635. + || current->real_parent->pid == pid))
  10636. + goto out;
  10637. +
  10638. + br = au_sbr(sb, cpg.bsrc);
  10639. + cmoo = au_br_cmoo(br->br_perm);
  10640. + if (!cmoo)
  10641. + goto out;
  10642. + if (!d_is_reg(dentry))
  10643. + cmoo &= AuBrAttr_COO_ALL;
  10644. + if (!cmoo)
  10645. + goto out;
  10646. +
  10647. + parent = dget_parent(dentry);
  10648. + di_write_lock_parent(parent);
  10649. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  10650. + cpg.bdst = err;
  10651. + if (unlikely(err < 0)) {
  10652. + err = 0; /* there is no upper writable branch */
  10653. + goto out_dgrade;
  10654. + }
  10655. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  10656. +
  10657. + /* do not respect the coo attrib for the target branch */
  10658. + err = au_cpup_dirs(dentry, cpg.bdst);
  10659. + if (unlikely(err))
  10660. + goto out_dgrade;
  10661. +
  10662. + di_downgrade_lock(parent, AuLock_IR);
  10663. + udba = au_opt_udba(sb);
  10664. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  10665. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  10666. + if (unlikely(err))
  10667. + goto out_parent;
  10668. +
  10669. + err = au_sio_cpup_simple(&cpg);
  10670. + au_unpin(&pin);
  10671. + if (unlikely(err))
  10672. + goto out_parent;
  10673. + if (!(cmoo & AuBrWAttr_MOO))
  10674. + goto out_parent; /* success */
  10675. +
  10676. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  10677. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  10678. + if (unlikely(err))
  10679. + goto out_parent;
  10680. +
  10681. + h_path.mnt = au_br_mnt(br);
  10682. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  10683. + hdir = au_hi(parent->d_inode, cpg.bsrc);
  10684. + delegated = NULL;
  10685. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  10686. + au_unpin(&pin);
  10687. + /* todo: keep h_dentry or not? */
  10688. + if (unlikely(err == -EWOULDBLOCK)) {
  10689. + pr_warn("cannot retry for NFSv4 delegation"
  10690. + " for an internal unlink\n");
  10691. + iput(delegated);
  10692. + }
  10693. + if (unlikely(err)) {
  10694. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  10695. + dentry, err);
  10696. + err = 0;
  10697. + }
  10698. + goto out_parent; /* success */
  10699. +
  10700. +out_dgrade:
  10701. + di_downgrade_lock(parent, AuLock_IR);
  10702. +out_parent:
  10703. + di_read_unlock(parent, AuLock_IR);
  10704. + dput(parent);
  10705. +out:
  10706. + AuTraceErr(err);
  10707. + return err;
  10708. +}
  10709. +
  10710. +int au_do_open(struct file *file, struct au_do_open_args *args)
  10711. +{
  10712. + int err, no_lock = args->no_lock;
  10713. + struct dentry *dentry;
  10714. + struct au_finfo *finfo;
  10715. +
  10716. + if (!no_lock)
  10717. + err = au_finfo_init(file, args->fidir);
  10718. + else {
  10719. + lockdep_off();
  10720. + err = au_finfo_init(file, args->fidir);
  10721. + lockdep_on();
  10722. + }
  10723. + if (unlikely(err))
  10724. + goto out;
  10725. +
  10726. + dentry = file->f_path.dentry;
  10727. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  10728. + if (!no_lock) {
  10729. + di_write_lock_child(dentry);
  10730. + err = au_cmoo(dentry);
  10731. + di_downgrade_lock(dentry, AuLock_IR);
  10732. + if (!err)
  10733. + err = args->open(file, vfsub_file_flags(file), NULL);
  10734. + di_read_unlock(dentry, AuLock_IR);
  10735. + } else {
  10736. + err = au_cmoo(dentry);
  10737. + if (!err)
  10738. + err = args->open(file, vfsub_file_flags(file),
  10739. + args->h_file);
  10740. + if (!err && au_fbstart(file) != au_dbstart(dentry))
  10741. + /*
  10742. + * cmoo happens after h_file was opened.
  10743. + * need to refresh file later.
  10744. + */
  10745. + atomic_dec(&au_fi(file)->fi_generation);
  10746. + }
  10747. +
  10748. + finfo = au_fi(file);
  10749. + if (!err) {
  10750. + finfo->fi_file = file;
  10751. + au_sphl_add(&finfo->fi_hlist,
  10752. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  10753. + }
  10754. + if (!no_lock)
  10755. + fi_write_unlock(file);
  10756. + else {
  10757. + lockdep_off();
  10758. + fi_write_unlock(file);
  10759. + lockdep_on();
  10760. + }
  10761. + if (unlikely(err)) {
  10762. + finfo->fi_hdir = NULL;
  10763. + au_finfo_fin(file);
  10764. + }
  10765. +
  10766. +out:
  10767. + return err;
  10768. +}
  10769. +
  10770. +int au_reopen_nondir(struct file *file)
  10771. +{
  10772. + int err;
  10773. + aufs_bindex_t bstart;
  10774. + struct dentry *dentry;
  10775. + struct file *h_file, *h_file_tmp;
  10776. +
  10777. + dentry = file->f_path.dentry;
  10778. + bstart = au_dbstart(dentry);
  10779. + h_file_tmp = NULL;
  10780. + if (au_fbstart(file) == bstart) {
  10781. + h_file = au_hf_top(file);
  10782. + if (file->f_mode == h_file->f_mode)
  10783. + return 0; /* success */
  10784. + h_file_tmp = h_file;
  10785. + get_file(h_file_tmp);
  10786. + au_set_h_fptr(file, bstart, NULL);
  10787. + }
  10788. + AuDebugOn(au_fi(file)->fi_hdir);
  10789. + /*
  10790. + * it can happen
  10791. + * file exists on both of rw and ro
  10792. + * open --> dbstart and fbstart are both 0
  10793. + * prepend a branch as rw, "rw" become ro
  10794. + * remove rw/file
  10795. + * delete the top branch, "rw" becomes rw again
  10796. + * --> dbstart is 1, fbstart is still 0
  10797. + * write --> fbstart is 0 but dbstart is 1
  10798. + */
  10799. + /* AuDebugOn(au_fbstart(file) < bstart); */
  10800. +
  10801. + h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
  10802. + file, /*force_wr*/0);
  10803. + err = PTR_ERR(h_file);
  10804. + if (IS_ERR(h_file)) {
  10805. + if (h_file_tmp) {
  10806. + atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
  10807. + au_set_h_fptr(file, bstart, h_file_tmp);
  10808. + h_file_tmp = NULL;
  10809. + }
  10810. + goto out; /* todo: close all? */
  10811. + }
  10812. +
  10813. + err = 0;
  10814. + au_set_fbstart(file, bstart);
  10815. + au_set_h_fptr(file, bstart, h_file);
  10816. + au_update_figen(file);
  10817. + /* todo: necessary? */
  10818. + /* file->f_ra = h_file->f_ra; */
  10819. +
  10820. +out:
  10821. + if (h_file_tmp)
  10822. + fput(h_file_tmp);
  10823. + return err;
  10824. +}
  10825. +
  10826. +/* ---------------------------------------------------------------------- */
  10827. +
  10828. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  10829. + struct dentry *hi_wh)
  10830. +{
  10831. + int err;
  10832. + aufs_bindex_t bstart;
  10833. + struct au_dinfo *dinfo;
  10834. + struct dentry *h_dentry;
  10835. + struct au_hdentry *hdp;
  10836. +
  10837. + dinfo = au_di(file->f_path.dentry);
  10838. + AuRwMustWriteLock(&dinfo->di_rwsem);
  10839. +
  10840. + bstart = dinfo->di_bstart;
  10841. + dinfo->di_bstart = btgt;
  10842. + hdp = dinfo->di_hdentry;
  10843. + h_dentry = hdp[0 + btgt].hd_dentry;
  10844. + hdp[0 + btgt].hd_dentry = hi_wh;
  10845. + err = au_reopen_nondir(file);
  10846. + hdp[0 + btgt].hd_dentry = h_dentry;
  10847. + dinfo->di_bstart = bstart;
  10848. +
  10849. + return err;
  10850. +}
  10851. +
  10852. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  10853. + aufs_bindex_t bcpup, struct au_pin *pin)
  10854. +{
  10855. + int err;
  10856. + struct inode *inode, *h_inode;
  10857. + struct dentry *h_dentry, *hi_wh;
  10858. + struct au_cp_generic cpg = {
  10859. + .dentry = file->f_path.dentry,
  10860. + .bdst = bcpup,
  10861. + .bsrc = -1,
  10862. + .len = len,
  10863. + .pin = pin
  10864. + };
  10865. +
  10866. + au_update_dbstart(cpg.dentry);
  10867. + inode = cpg.dentry->d_inode;
  10868. + h_inode = NULL;
  10869. + if (au_dbstart(cpg.dentry) <= bcpup
  10870. + && au_dbend(cpg.dentry) >= bcpup) {
  10871. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  10872. + if (h_dentry)
  10873. + h_inode = h_dentry->d_inode;
  10874. + }
  10875. + hi_wh = au_hi_wh(inode, bcpup);
  10876. + if (!hi_wh && !h_inode)
  10877. + err = au_sio_cpup_wh(&cpg, file);
  10878. + else
  10879. + /* already copied-up after unlink */
  10880. + err = au_reopen_wh(file, bcpup, hi_wh);
  10881. +
  10882. + if (!err
  10883. + && (inode->i_nlink > 1
  10884. + || (inode->i_state & I_LINKABLE))
  10885. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  10886. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  10887. +
  10888. + return err;
  10889. +}
  10890. +
  10891. +/*
  10892. + * prepare the @file for writing.
  10893. + */
  10894. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  10895. +{
  10896. + int err;
  10897. + aufs_bindex_t dbstart;
  10898. + struct dentry *parent;
  10899. + struct inode *inode;
  10900. + struct super_block *sb;
  10901. + struct file *h_file;
  10902. + struct au_cp_generic cpg = {
  10903. + .dentry = file->f_path.dentry,
  10904. + .bdst = -1,
  10905. + .bsrc = -1,
  10906. + .len = len,
  10907. + .pin = pin,
  10908. + .flags = AuCpup_DTIME
  10909. + };
  10910. +
  10911. + sb = cpg.dentry->d_sb;
  10912. + inode = cpg.dentry->d_inode;
  10913. + cpg.bsrc = au_fbstart(file);
  10914. + err = au_test_ro(sb, cpg.bsrc, inode);
  10915. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  10916. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  10917. + /*flags*/0);
  10918. + goto out;
  10919. + }
  10920. +
  10921. + /* need to cpup or reopen */
  10922. + parent = dget_parent(cpg.dentry);
  10923. + di_write_lock_parent(parent);
  10924. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  10925. + cpg.bdst = err;
  10926. + if (unlikely(err < 0))
  10927. + goto out_dgrade;
  10928. + err = 0;
  10929. +
  10930. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  10931. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  10932. + if (unlikely(err))
  10933. + goto out_dgrade;
  10934. + }
  10935. +
  10936. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  10937. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  10938. + if (unlikely(err))
  10939. + goto out_dgrade;
  10940. +
  10941. + dbstart = au_dbstart(cpg.dentry);
  10942. + if (dbstart <= cpg.bdst)
  10943. + cpg.bsrc = cpg.bdst;
  10944. +
  10945. + if (dbstart <= cpg.bdst /* just reopen */
  10946. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  10947. + ) {
  10948. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  10949. + if (IS_ERR(h_file))
  10950. + err = PTR_ERR(h_file);
  10951. + else {
  10952. + di_downgrade_lock(parent, AuLock_IR);
  10953. + if (dbstart > cpg.bdst)
  10954. + err = au_sio_cpup_simple(&cpg);
  10955. + if (!err)
  10956. + err = au_reopen_nondir(file);
  10957. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  10958. + }
  10959. + } else { /* copyup as wh and reopen */
  10960. + /*
  10961. + * since writable hfsplus branch is not supported,
  10962. + * h_open_pre/post() are unnecessary.
  10963. + */
  10964. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  10965. + di_downgrade_lock(parent, AuLock_IR);
  10966. + }
  10967. +
  10968. + if (!err) {
  10969. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  10970. + goto out_dput; /* success */
  10971. + }
  10972. + au_unpin(pin);
  10973. + goto out_unlock;
  10974. +
  10975. +out_dgrade:
  10976. + di_downgrade_lock(parent, AuLock_IR);
  10977. +out_unlock:
  10978. + di_read_unlock(parent, AuLock_IR);
  10979. +out_dput:
  10980. + dput(parent);
  10981. +out:
  10982. + return err;
  10983. +}
  10984. +
  10985. +/* ---------------------------------------------------------------------- */
  10986. +
  10987. +int au_do_flush(struct file *file, fl_owner_t id,
  10988. + int (*flush)(struct file *file, fl_owner_t id))
  10989. +{
  10990. + int err;
  10991. + struct super_block *sb;
  10992. + struct inode *inode;
  10993. +
  10994. + inode = file_inode(file);
  10995. + sb = inode->i_sb;
  10996. + si_noflush_read_lock(sb);
  10997. + fi_read_lock(file);
  10998. + ii_read_lock_child(inode);
  10999. +
  11000. + err = flush(file, id);
  11001. + au_cpup_attr_timesizes(inode);
  11002. +
  11003. + ii_read_unlock(inode);
  11004. + fi_read_unlock(file);
  11005. + si_read_unlock(sb);
  11006. + return err;
  11007. +}
  11008. +
  11009. +/* ---------------------------------------------------------------------- */
  11010. +
  11011. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  11012. +{
  11013. + int err;
  11014. + struct au_pin pin;
  11015. + struct au_finfo *finfo;
  11016. + struct dentry *parent, *hi_wh;
  11017. + struct inode *inode;
  11018. + struct super_block *sb;
  11019. + struct au_cp_generic cpg = {
  11020. + .dentry = file->f_path.dentry,
  11021. + .bdst = -1,
  11022. + .bsrc = -1,
  11023. + .len = -1,
  11024. + .pin = &pin,
  11025. + .flags = AuCpup_DTIME
  11026. + };
  11027. +
  11028. + FiMustWriteLock(file);
  11029. +
  11030. + err = 0;
  11031. + finfo = au_fi(file);
  11032. + sb = cpg.dentry->d_sb;
  11033. + inode = cpg.dentry->d_inode;
  11034. + cpg.bdst = au_ibstart(inode);
  11035. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  11036. + goto out;
  11037. +
  11038. + parent = dget_parent(cpg.dentry);
  11039. + if (au_test_ro(sb, cpg.bdst, inode)) {
  11040. + di_read_lock_parent(parent, !AuLock_IR);
  11041. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11042. + cpg.bdst = err;
  11043. + di_read_unlock(parent, !AuLock_IR);
  11044. + if (unlikely(err < 0))
  11045. + goto out_parent;
  11046. + err = 0;
  11047. + }
  11048. +
  11049. + di_read_lock_parent(parent, AuLock_IR);
  11050. + hi_wh = au_hi_wh(inode, cpg.bdst);
  11051. + if (!S_ISDIR(inode->i_mode)
  11052. + && au_opt_test(au_mntflags(sb), PLINK)
  11053. + && au_plink_test(inode)
  11054. + && !d_unhashed(cpg.dentry)
  11055. + && cpg.bdst < au_dbstart(cpg.dentry)) {
  11056. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  11057. + if (unlikely(err))
  11058. + goto out_unlock;
  11059. +
  11060. + /* always superio. */
  11061. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11062. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11063. + if (!err) {
  11064. + err = au_sio_cpup_simple(&cpg);
  11065. + au_unpin(&pin);
  11066. + }
  11067. + } else if (hi_wh) {
  11068. + /* already copied-up after unlink */
  11069. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  11070. + *need_reopen = 0;
  11071. + }
  11072. +
  11073. +out_unlock:
  11074. + di_read_unlock(parent, AuLock_IR);
  11075. +out_parent:
  11076. + dput(parent);
  11077. +out:
  11078. + return err;
  11079. +}
  11080. +
  11081. +static void au_do_refresh_dir(struct file *file)
  11082. +{
  11083. + aufs_bindex_t bindex, bend, new_bindex, brid;
  11084. + struct au_hfile *p, tmp, *q;
  11085. + struct au_finfo *finfo;
  11086. + struct super_block *sb;
  11087. + struct au_fidir *fidir;
  11088. +
  11089. + FiMustWriteLock(file);
  11090. +
  11091. + sb = file->f_path.dentry->d_sb;
  11092. + finfo = au_fi(file);
  11093. + fidir = finfo->fi_hdir;
  11094. + AuDebugOn(!fidir);
  11095. + p = fidir->fd_hfile + finfo->fi_btop;
  11096. + brid = p->hf_br->br_id;
  11097. + bend = fidir->fd_bbot;
  11098. + for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
  11099. + if (!p->hf_file)
  11100. + continue;
  11101. +
  11102. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  11103. + if (new_bindex == bindex)
  11104. + continue;
  11105. + if (new_bindex < 0) {
  11106. + au_set_h_fptr(file, bindex, NULL);
  11107. + continue;
  11108. + }
  11109. +
  11110. + /* swap two lower inode, and loop again */
  11111. + q = fidir->fd_hfile + new_bindex;
  11112. + tmp = *q;
  11113. + *q = *p;
  11114. + *p = tmp;
  11115. + if (tmp.hf_file) {
  11116. + bindex--;
  11117. + p--;
  11118. + }
  11119. + }
  11120. +
  11121. + p = fidir->fd_hfile;
  11122. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  11123. + bend = au_sbend(sb);
  11124. + for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
  11125. + finfo->fi_btop++, p++)
  11126. + if (p->hf_file) {
  11127. + if (file_inode(p->hf_file))
  11128. + break;
  11129. + au_hfput(p, file);
  11130. + }
  11131. + } else {
  11132. + bend = au_br_index(sb, brid);
  11133. + for (finfo->fi_btop = 0; finfo->fi_btop < bend;
  11134. + finfo->fi_btop++, p++)
  11135. + if (p->hf_file)
  11136. + au_hfput(p, file);
  11137. + bend = au_sbend(sb);
  11138. + }
  11139. +
  11140. + p = fidir->fd_hfile + bend;
  11141. + for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
  11142. + fidir->fd_bbot--, p--)
  11143. + if (p->hf_file) {
  11144. + if (file_inode(p->hf_file))
  11145. + break;
  11146. + au_hfput(p, file);
  11147. + }
  11148. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  11149. +}
  11150. +
  11151. +/*
  11152. + * after branch manipulating, refresh the file.
  11153. + */
  11154. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  11155. +{
  11156. + int err, need_reopen;
  11157. + aufs_bindex_t bend, bindex;
  11158. + struct dentry *dentry;
  11159. + struct au_finfo *finfo;
  11160. + struct au_hfile *hfile;
  11161. +
  11162. + dentry = file->f_path.dentry;
  11163. + finfo = au_fi(file);
  11164. + if (!finfo->fi_hdir) {
  11165. + hfile = &finfo->fi_htop;
  11166. + AuDebugOn(!hfile->hf_file);
  11167. + bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
  11168. + AuDebugOn(bindex < 0);
  11169. + if (bindex != finfo->fi_btop)
  11170. + au_set_fbstart(file, bindex);
  11171. + } else {
  11172. + err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
  11173. + if (unlikely(err))
  11174. + goto out;
  11175. + au_do_refresh_dir(file);
  11176. + }
  11177. +
  11178. + err = 0;
  11179. + need_reopen = 1;
  11180. + if (!au_test_mmapped(file))
  11181. + err = au_file_refresh_by_inode(file, &need_reopen);
  11182. + if (!err && need_reopen && !d_unlinked(dentry))
  11183. + err = reopen(file);
  11184. + if (!err) {
  11185. + au_update_figen(file);
  11186. + goto out; /* success */
  11187. + }
  11188. +
  11189. + /* error, close all lower files */
  11190. + if (finfo->fi_hdir) {
  11191. + bend = au_fbend_dir(file);
  11192. + for (bindex = au_fbstart(file); bindex <= bend; bindex++)
  11193. + au_set_h_fptr(file, bindex, NULL);
  11194. + }
  11195. +
  11196. +out:
  11197. + return err;
  11198. +}
  11199. +
  11200. +/* common function to regular file and dir */
  11201. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  11202. + int wlock)
  11203. +{
  11204. + int err;
  11205. + unsigned int sigen, figen;
  11206. + aufs_bindex_t bstart;
  11207. + unsigned char pseudo_link;
  11208. + struct dentry *dentry;
  11209. + struct inode *inode;
  11210. +
  11211. + err = 0;
  11212. + dentry = file->f_path.dentry;
  11213. + inode = dentry->d_inode;
  11214. + sigen = au_sigen(dentry->d_sb);
  11215. + fi_write_lock(file);
  11216. + figen = au_figen(file);
  11217. + di_write_lock_child(dentry);
  11218. + bstart = au_dbstart(dentry);
  11219. + pseudo_link = (bstart != au_ibstart(inode));
  11220. + if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
  11221. + if (!wlock) {
  11222. + di_downgrade_lock(dentry, AuLock_IR);
  11223. + fi_downgrade_lock(file);
  11224. + }
  11225. + goto out; /* success */
  11226. + }
  11227. +
  11228. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  11229. + if (au_digen_test(dentry, sigen)) {
  11230. + err = au_reval_dpath(dentry, sigen);
  11231. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  11232. + }
  11233. +
  11234. + if (!err)
  11235. + err = refresh_file(file, reopen);
  11236. + if (!err) {
  11237. + if (!wlock) {
  11238. + di_downgrade_lock(dentry, AuLock_IR);
  11239. + fi_downgrade_lock(file);
  11240. + }
  11241. + } else {
  11242. + di_write_unlock(dentry);
  11243. + fi_write_unlock(file);
  11244. + }
  11245. +
  11246. +out:
  11247. + return err;
  11248. +}
  11249. +
  11250. +/* ---------------------------------------------------------------------- */
  11251. +
  11252. +/* cf. aufs_nopage() */
  11253. +/* for madvise(2) */
  11254. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  11255. +{
  11256. + unlock_page(page);
  11257. + return 0;
  11258. +}
  11259. +
  11260. +/* it will never be called, but necessary to support O_DIRECT */
  11261. +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
  11262. + struct iov_iter *iter, loff_t offset)
  11263. +{ BUG(); return 0; }
  11264. +
  11265. +/* they will never be called. */
  11266. +#ifdef CONFIG_AUFS_DEBUG
  11267. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  11268. + loff_t pos, unsigned len, unsigned flags,
  11269. + struct page **pagep, void **fsdata)
  11270. +{ AuUnsupport(); return 0; }
  11271. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  11272. + loff_t pos, unsigned len, unsigned copied,
  11273. + struct page *page, void *fsdata)
  11274. +{ AuUnsupport(); return 0; }
  11275. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  11276. +{ AuUnsupport(); return 0; }
  11277. +
  11278. +static int aufs_set_page_dirty(struct page *page)
  11279. +{ AuUnsupport(); return 0; }
  11280. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  11281. + unsigned int length)
  11282. +{ AuUnsupport(); }
  11283. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  11284. +{ AuUnsupport(); return 0; }
  11285. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  11286. + struct page *page, enum migrate_mode mode)
  11287. +{ AuUnsupport(); return 0; }
  11288. +static int aufs_launder_page(struct page *page)
  11289. +{ AuUnsupport(); return 0; }
  11290. +static int aufs_is_partially_uptodate(struct page *page,
  11291. + unsigned long from,
  11292. + unsigned long count)
  11293. +{ AuUnsupport(); return 0; }
  11294. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  11295. + bool *writeback)
  11296. +{ AuUnsupport(); }
  11297. +static int aufs_error_remove_page(struct address_space *mapping,
  11298. + struct page *page)
  11299. +{ AuUnsupport(); return 0; }
  11300. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  11301. + sector_t *span)
  11302. +{ AuUnsupport(); return 0; }
  11303. +static void aufs_swap_deactivate(struct file *file)
  11304. +{ AuUnsupport(); }
  11305. +#endif /* CONFIG_AUFS_DEBUG */
  11306. +
  11307. +const struct address_space_operations aufs_aop = {
  11308. + .readpage = aufs_readpage,
  11309. + .direct_IO = aufs_direct_IO,
  11310. +#ifdef CONFIG_AUFS_DEBUG
  11311. + .writepage = aufs_writepage,
  11312. + /* no writepages, because of writepage */
  11313. + .set_page_dirty = aufs_set_page_dirty,
  11314. + /* no readpages, because of readpage */
  11315. + .write_begin = aufs_write_begin,
  11316. + .write_end = aufs_write_end,
  11317. + /* no bmap, no block device */
  11318. + .invalidatepage = aufs_invalidatepage,
  11319. + .releasepage = aufs_releasepage,
  11320. + .migratepage = aufs_migratepage,
  11321. + .launder_page = aufs_launder_page,
  11322. + .is_partially_uptodate = aufs_is_partially_uptodate,
  11323. + .is_dirty_writeback = aufs_is_dirty_writeback,
  11324. + .error_remove_page = aufs_error_remove_page,
  11325. + .swap_activate = aufs_swap_activate,
  11326. + .swap_deactivate = aufs_swap_deactivate
  11327. +#endif /* CONFIG_AUFS_DEBUG */
  11328. +};
  11329. diff -Nur linux-4.0.4.orig/fs/aufs/file.h linux-4.0.4/fs/aufs/file.h
  11330. --- linux-4.0.4.orig/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
  11331. +++ linux-4.0.4/fs/aufs/file.h 2015-05-30 22:11:31.000000000 +0200
  11332. @@ -0,0 +1,277 @@
  11333. +/*
  11334. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11335. + */
  11336. +
  11337. +/*
  11338. + * file operations
  11339. + */
  11340. +
  11341. +#ifndef __AUFS_FILE_H__
  11342. +#define __AUFS_FILE_H__
  11343. +
  11344. +#ifdef __KERNEL__
  11345. +
  11346. +#include <linux/file.h>
  11347. +#include <linux/fs.h>
  11348. +#include <linux/poll.h>
  11349. +#include "rwsem.h"
  11350. +
  11351. +struct au_branch;
  11352. +struct au_hfile {
  11353. + struct file *hf_file;
  11354. + struct au_branch *hf_br;
  11355. +};
  11356. +
  11357. +struct au_vdir;
  11358. +struct au_fidir {
  11359. + aufs_bindex_t fd_bbot;
  11360. + aufs_bindex_t fd_nent;
  11361. + struct au_vdir *fd_vdir_cache;
  11362. + struct au_hfile fd_hfile[];
  11363. +};
  11364. +
  11365. +static inline int au_fidir_sz(int nent)
  11366. +{
  11367. + AuDebugOn(nent < 0);
  11368. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  11369. +}
  11370. +
  11371. +struct au_finfo {
  11372. + atomic_t fi_generation;
  11373. +
  11374. + struct au_rwsem fi_rwsem;
  11375. + aufs_bindex_t fi_btop;
  11376. +
  11377. + /* do not union them */
  11378. + struct { /* for non-dir */
  11379. + struct au_hfile fi_htop;
  11380. + atomic_t fi_mmapped;
  11381. + };
  11382. + struct au_fidir *fi_hdir; /* for dir only */
  11383. +
  11384. + struct hlist_node fi_hlist;
  11385. + struct file *fi_file; /* very ugly */
  11386. +} ____cacheline_aligned_in_smp;
  11387. +
  11388. +/* ---------------------------------------------------------------------- */
  11389. +
  11390. +/* file.c */
  11391. +extern const struct address_space_operations aufs_aop;
  11392. +unsigned int au_file_roflags(unsigned int flags);
  11393. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  11394. + struct file *file, int force_wr);
  11395. +struct au_do_open_args {
  11396. + int no_lock;
  11397. + int (*open)(struct file *file, int flags,
  11398. + struct file *h_file);
  11399. + struct au_fidir *fidir;
  11400. + struct file *h_file;
  11401. +};
  11402. +int au_do_open(struct file *file, struct au_do_open_args *args);
  11403. +int au_reopen_nondir(struct file *file);
  11404. +struct au_pin;
  11405. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  11406. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  11407. + int wlock);
  11408. +int au_do_flush(struct file *file, fl_owner_t id,
  11409. + int (*flush)(struct file *file, fl_owner_t id));
  11410. +
  11411. +/* poll.c */
  11412. +#ifdef CONFIG_AUFS_POLL
  11413. +unsigned int aufs_poll(struct file *file, poll_table *wait);
  11414. +#endif
  11415. +
  11416. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  11417. +/* hfsplus.c */
  11418. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  11419. + int force_wr);
  11420. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  11421. + struct file *h_file);
  11422. +#else
  11423. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  11424. + aufs_bindex_t bindex, int force_wr)
  11425. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  11426. + struct file *h_file);
  11427. +#endif
  11428. +
  11429. +/* f_op.c */
  11430. +extern const struct file_operations aufs_file_fop;
  11431. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  11432. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  11433. +
  11434. +/* finfo.c */
  11435. +void au_hfput(struct au_hfile *hf, struct file *file);
  11436. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  11437. + struct file *h_file);
  11438. +
  11439. +void au_update_figen(struct file *file);
  11440. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  11441. +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
  11442. +
  11443. +void au_fi_init_once(void *_fi);
  11444. +void au_finfo_fin(struct file *file);
  11445. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  11446. +
  11447. +/* ioctl.c */
  11448. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  11449. +#ifdef CONFIG_COMPAT
  11450. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  11451. + unsigned long arg);
  11452. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  11453. + unsigned long arg);
  11454. +#endif
  11455. +
  11456. +/* ---------------------------------------------------------------------- */
  11457. +
  11458. +static inline struct au_finfo *au_fi(struct file *file)
  11459. +{
  11460. + return file->private_data;
  11461. +}
  11462. +
  11463. +/* ---------------------------------------------------------------------- */
  11464. +
  11465. +/*
  11466. + * fi_read_lock, fi_write_lock,
  11467. + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
  11468. + */
  11469. +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
  11470. +
  11471. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  11472. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  11473. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  11474. +
  11475. +/* ---------------------------------------------------------------------- */
  11476. +
  11477. +/* todo: hard/soft set? */
  11478. +static inline aufs_bindex_t au_fbstart(struct file *file)
  11479. +{
  11480. + FiMustAnyLock(file);
  11481. + return au_fi(file)->fi_btop;
  11482. +}
  11483. +
  11484. +static inline aufs_bindex_t au_fbend_dir(struct file *file)
  11485. +{
  11486. + FiMustAnyLock(file);
  11487. + AuDebugOn(!au_fi(file)->fi_hdir);
  11488. + return au_fi(file)->fi_hdir->fd_bbot;
  11489. +}
  11490. +
  11491. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  11492. +{
  11493. + FiMustAnyLock(file);
  11494. + AuDebugOn(!au_fi(file)->fi_hdir);
  11495. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  11496. +}
  11497. +
  11498. +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
  11499. +{
  11500. + FiMustWriteLock(file);
  11501. + au_fi(file)->fi_btop = bindex;
  11502. +}
  11503. +
  11504. +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
  11505. +{
  11506. + FiMustWriteLock(file);
  11507. + AuDebugOn(!au_fi(file)->fi_hdir);
  11508. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  11509. +}
  11510. +
  11511. +static inline void au_set_fvdir_cache(struct file *file,
  11512. + struct au_vdir *vdir_cache)
  11513. +{
  11514. + FiMustWriteLock(file);
  11515. + AuDebugOn(!au_fi(file)->fi_hdir);
  11516. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  11517. +}
  11518. +
  11519. +static inline struct file *au_hf_top(struct file *file)
  11520. +{
  11521. + FiMustAnyLock(file);
  11522. + AuDebugOn(au_fi(file)->fi_hdir);
  11523. + return au_fi(file)->fi_htop.hf_file;
  11524. +}
  11525. +
  11526. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  11527. +{
  11528. + FiMustAnyLock(file);
  11529. + AuDebugOn(!au_fi(file)->fi_hdir);
  11530. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  11531. +}
  11532. +
  11533. +/* todo: memory barrier? */
  11534. +static inline unsigned int au_figen(struct file *f)
  11535. +{
  11536. + return atomic_read(&au_fi(f)->fi_generation);
  11537. +}
  11538. +
  11539. +static inline void au_set_mmapped(struct file *f)
  11540. +{
  11541. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  11542. + return;
  11543. + pr_warn("fi_mmapped wrapped around\n");
  11544. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  11545. + ;
  11546. +}
  11547. +
  11548. +static inline void au_unset_mmapped(struct file *f)
  11549. +{
  11550. + atomic_dec(&au_fi(f)->fi_mmapped);
  11551. +}
  11552. +
  11553. +static inline int au_test_mmapped(struct file *f)
  11554. +{
  11555. + return atomic_read(&au_fi(f)->fi_mmapped);
  11556. +}
  11557. +
  11558. +/* customize vma->vm_file */
  11559. +
  11560. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  11561. + struct file *file)
  11562. +{
  11563. + struct file *f;
  11564. +
  11565. + f = vma->vm_file;
  11566. + get_file(file);
  11567. + vma->vm_file = file;
  11568. + fput(f);
  11569. +}
  11570. +
  11571. +#ifdef CONFIG_MMU
  11572. +#define AuDbgVmRegion(file, vma) do {} while (0)
  11573. +
  11574. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  11575. + struct file *file)
  11576. +{
  11577. + au_do_vm_file_reset(vma, file);
  11578. +}
  11579. +#else
  11580. +#define AuDbgVmRegion(file, vma) \
  11581. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  11582. +
  11583. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  11584. + struct file *file)
  11585. +{
  11586. + struct file *f;
  11587. +
  11588. + au_do_vm_file_reset(vma, file);
  11589. + f = vma->vm_region->vm_file;
  11590. + get_file(file);
  11591. + vma->vm_region->vm_file = file;
  11592. + fput(f);
  11593. +}
  11594. +#endif /* CONFIG_MMU */
  11595. +
  11596. +/* handle vma->vm_prfile */
  11597. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  11598. + struct file *file)
  11599. +{
  11600. + get_file(file);
  11601. + vma->vm_prfile = file;
  11602. +#ifndef CONFIG_MMU
  11603. + get_file(file);
  11604. + vma->vm_region->vm_prfile = file;
  11605. +#endif
  11606. +}
  11607. +
  11608. +#endif /* __KERNEL__ */
  11609. +#endif /* __AUFS_FILE_H__ */
  11610. diff -Nur linux-4.0.4.orig/fs/aufs/finfo.c linux-4.0.4/fs/aufs/finfo.c
  11611. --- linux-4.0.4.orig/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
  11612. +++ linux-4.0.4/fs/aufs/finfo.c 2015-05-30 22:11:31.000000000 +0200
  11613. @@ -0,0 +1,144 @@
  11614. +/*
  11615. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11616. + */
  11617. +
  11618. +/*
  11619. + * file private data
  11620. + */
  11621. +
  11622. +#include "aufs.h"
  11623. +
  11624. +void au_hfput(struct au_hfile *hf, struct file *file)
  11625. +{
  11626. + /* todo: direct access f_flags */
  11627. + if (vfsub_file_flags(file) & __FMODE_EXEC)
  11628. + allow_write_access(hf->hf_file);
  11629. + fput(hf->hf_file);
  11630. + hf->hf_file = NULL;
  11631. + atomic_dec(&hf->hf_br->br_count);
  11632. + hf->hf_br = NULL;
  11633. +}
  11634. +
  11635. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  11636. +{
  11637. + struct au_finfo *finfo = au_fi(file);
  11638. + struct au_hfile *hf;
  11639. + struct au_fidir *fidir;
  11640. +
  11641. + fidir = finfo->fi_hdir;
  11642. + if (!fidir) {
  11643. + AuDebugOn(finfo->fi_btop != bindex);
  11644. + hf = &finfo->fi_htop;
  11645. + } else
  11646. + hf = fidir->fd_hfile + bindex;
  11647. +
  11648. + if (hf && hf->hf_file)
  11649. + au_hfput(hf, file);
  11650. + if (val) {
  11651. + FiMustWriteLock(file);
  11652. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  11653. + hf->hf_file = val;
  11654. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  11655. + }
  11656. +}
  11657. +
  11658. +void au_update_figen(struct file *file)
  11659. +{
  11660. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  11661. + /* smp_mb(); */ /* atomic_set */
  11662. +}
  11663. +
  11664. +/* ---------------------------------------------------------------------- */
  11665. +
  11666. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  11667. +{
  11668. + struct au_fidir *fidir;
  11669. + int nbr;
  11670. +
  11671. + nbr = au_sbend(sb) + 1;
  11672. + if (nbr < 2)
  11673. + nbr = 2; /* initial allocate for 2 branches */
  11674. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  11675. + if (fidir) {
  11676. + fidir->fd_bbot = -1;
  11677. + fidir->fd_nent = nbr;
  11678. + fidir->fd_vdir_cache = NULL;
  11679. + }
  11680. +
  11681. + return fidir;
  11682. +}
  11683. +
  11684. +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
  11685. +{
  11686. + int err;
  11687. + struct au_fidir *fidir, *p;
  11688. +
  11689. + AuRwMustWriteLock(&finfo->fi_rwsem);
  11690. + fidir = finfo->fi_hdir;
  11691. + AuDebugOn(!fidir);
  11692. +
  11693. + err = -ENOMEM;
  11694. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  11695. + GFP_NOFS);
  11696. + if (p) {
  11697. + p->fd_nent = nbr;
  11698. + finfo->fi_hdir = p;
  11699. + err = 0;
  11700. + }
  11701. +
  11702. + return err;
  11703. +}
  11704. +
  11705. +/* ---------------------------------------------------------------------- */
  11706. +
  11707. +void au_finfo_fin(struct file *file)
  11708. +{
  11709. + struct au_finfo *finfo;
  11710. +
  11711. + au_nfiles_dec(file->f_path.dentry->d_sb);
  11712. +
  11713. + finfo = au_fi(file);
  11714. + AuDebugOn(finfo->fi_hdir);
  11715. + AuRwDestroy(&finfo->fi_rwsem);
  11716. + au_cache_free_finfo(finfo);
  11717. +}
  11718. +
  11719. +void au_fi_init_once(void *_finfo)
  11720. +{
  11721. + struct au_finfo *finfo = _finfo;
  11722. + static struct lock_class_key aufs_fi;
  11723. +
  11724. + au_rw_init(&finfo->fi_rwsem);
  11725. + au_rw_class(&finfo->fi_rwsem, &aufs_fi);
  11726. +}
  11727. +
  11728. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  11729. +{
  11730. + int err;
  11731. + struct au_finfo *finfo;
  11732. + struct dentry *dentry;
  11733. +
  11734. + err = -ENOMEM;
  11735. + dentry = file->f_path.dentry;
  11736. + finfo = au_cache_alloc_finfo();
  11737. + if (unlikely(!finfo))
  11738. + goto out;
  11739. +
  11740. + err = 0;
  11741. + au_nfiles_inc(dentry->d_sb);
  11742. + /* verbose coding for lock class name */
  11743. + if (!fidir)
  11744. + au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
  11745. + else
  11746. + au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
  11747. + au_rw_write_lock(&finfo->fi_rwsem);
  11748. + finfo->fi_btop = -1;
  11749. + finfo->fi_hdir = fidir;
  11750. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  11751. + /* smp_mb(); */ /* atomic_set */
  11752. +
  11753. + file->private_data = finfo;
  11754. +
  11755. +out:
  11756. + return err;
  11757. +}
  11758. diff -Nur linux-4.0.4.orig/fs/aufs/f_op.c linux-4.0.4/fs/aufs/f_op.c
  11759. --- linux-4.0.4.orig/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
  11760. +++ linux-4.0.4/fs/aufs/f_op.c 2015-05-30 22:11:31.000000000 +0200
  11761. @@ -0,0 +1,807 @@
  11762. +/*
  11763. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11764. + */
  11765. +
  11766. +/*
  11767. + * file and vm operations
  11768. + */
  11769. +
  11770. +#include <linux/aio.h>
  11771. +#include <linux/fs_stack.h>
  11772. +#include <linux/mman.h>
  11773. +#include <linux/security.h>
  11774. +#include "aufs.h"
  11775. +
  11776. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  11777. +{
  11778. + int err;
  11779. + aufs_bindex_t bindex;
  11780. + struct dentry *dentry;
  11781. + struct au_finfo *finfo;
  11782. + struct inode *h_inode;
  11783. +
  11784. + FiMustWriteLock(file);
  11785. +
  11786. + err = 0;
  11787. + dentry = file->f_path.dentry;
  11788. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  11789. + finfo = au_fi(file);
  11790. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  11791. + atomic_set(&finfo->fi_mmapped, 0);
  11792. + bindex = au_dbstart(dentry);
  11793. + if (!h_file)
  11794. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  11795. + else
  11796. + get_file(h_file);
  11797. + if (IS_ERR(h_file))
  11798. + err = PTR_ERR(h_file);
  11799. + else {
  11800. + if ((flags & __O_TMPFILE)
  11801. + && !(flags & O_EXCL)) {
  11802. + h_inode = file_inode(h_file);
  11803. + spin_lock(&h_inode->i_lock);
  11804. + h_inode->i_state |= I_LINKABLE;
  11805. + spin_unlock(&h_inode->i_lock);
  11806. + }
  11807. + au_set_fbstart(file, bindex);
  11808. + au_set_h_fptr(file, bindex, h_file);
  11809. + au_update_figen(file);
  11810. + /* todo: necessary? */
  11811. + /* file->f_ra = h_file->f_ra; */
  11812. + }
  11813. +
  11814. + return err;
  11815. +}
  11816. +
  11817. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  11818. + struct file *file)
  11819. +{
  11820. + int err;
  11821. + struct super_block *sb;
  11822. + struct au_do_open_args args = {
  11823. + .open = au_do_open_nondir
  11824. + };
  11825. +
  11826. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  11827. + file, vfsub_file_flags(file), file->f_mode);
  11828. +
  11829. + sb = file->f_path.dentry->d_sb;
  11830. + si_read_lock(sb, AuLock_FLUSH);
  11831. + err = au_do_open(file, &args);
  11832. + si_read_unlock(sb);
  11833. + return err;
  11834. +}
  11835. +
  11836. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  11837. +{
  11838. + struct au_finfo *finfo;
  11839. + aufs_bindex_t bindex;
  11840. +
  11841. + finfo = au_fi(file);
  11842. + au_sphl_del(&finfo->fi_hlist,
  11843. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  11844. + bindex = finfo->fi_btop;
  11845. + if (bindex >= 0)
  11846. + au_set_h_fptr(file, bindex, NULL);
  11847. +
  11848. + au_finfo_fin(file);
  11849. + return 0;
  11850. +}
  11851. +
  11852. +/* ---------------------------------------------------------------------- */
  11853. +
  11854. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  11855. +{
  11856. + int err;
  11857. + struct file *h_file;
  11858. +
  11859. + err = 0;
  11860. + h_file = au_hf_top(file);
  11861. + if (h_file)
  11862. + err = vfsub_flush(h_file, id);
  11863. + return err;
  11864. +}
  11865. +
  11866. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  11867. +{
  11868. + return au_do_flush(file, id, au_do_flush_nondir);
  11869. +}
  11870. +
  11871. +/* ---------------------------------------------------------------------- */
  11872. +/*
  11873. + * read and write functions acquire [fdi]_rwsem once, but release before
  11874. + * mmap_sem. This is because to stop a race condition between mmap(2).
  11875. + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
  11876. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  11877. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  11878. + */
  11879. +
  11880. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  11881. + loff_t *ppos)
  11882. +{
  11883. + ssize_t err;
  11884. + struct dentry *dentry;
  11885. + struct file *h_file;
  11886. + struct super_block *sb;
  11887. +
  11888. + dentry = file->f_path.dentry;
  11889. + sb = dentry->d_sb;
  11890. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  11891. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  11892. + if (unlikely(err))
  11893. + goto out;
  11894. +
  11895. + h_file = au_hf_top(file);
  11896. + get_file(h_file);
  11897. + di_read_unlock(dentry, AuLock_IR);
  11898. + fi_read_unlock(file);
  11899. +
  11900. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  11901. + err = vfsub_read_u(h_file, buf, count, ppos);
  11902. + /* todo: necessary? */
  11903. + /* file->f_ra = h_file->f_ra; */
  11904. + /* update without lock, I don't think it a problem */
  11905. + fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
  11906. + fput(h_file);
  11907. +
  11908. +out:
  11909. + si_read_unlock(sb);
  11910. + return err;
  11911. +}
  11912. +
  11913. +/*
  11914. + * todo: very ugly
  11915. + * it locks both of i_mutex and si_rwsem for read in safe.
  11916. + * if the plink maintenance mode continues forever (that is the problem),
  11917. + * may loop forever.
  11918. + */
  11919. +static void au_mtx_and_read_lock(struct inode *inode)
  11920. +{
  11921. + int err;
  11922. + struct super_block *sb = inode->i_sb;
  11923. +
  11924. + while (1) {
  11925. + mutex_lock(&inode->i_mutex);
  11926. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  11927. + if (!err)
  11928. + break;
  11929. + mutex_unlock(&inode->i_mutex);
  11930. + si_read_lock(sb, AuLock_NOPLMW);
  11931. + si_read_unlock(sb);
  11932. + }
  11933. +}
  11934. +
  11935. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  11936. + size_t count, loff_t *ppos)
  11937. +{
  11938. + ssize_t err;
  11939. + blkcnt_t blks;
  11940. + aufs_bindex_t bstart;
  11941. + struct au_pin pin;
  11942. + struct dentry *dentry;
  11943. + struct inode *inode, *h_inode;
  11944. + struct super_block *sb;
  11945. + struct file *h_file;
  11946. + char __user *buf = (char __user *)ubuf;
  11947. +
  11948. + dentry = file->f_path.dentry;
  11949. + sb = dentry->d_sb;
  11950. + inode = dentry->d_inode;
  11951. + au_mtx_and_read_lock(inode);
  11952. +
  11953. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  11954. + if (unlikely(err))
  11955. + goto out;
  11956. +
  11957. + err = au_ready_to_write(file, -1, &pin);
  11958. + di_downgrade_lock(dentry, AuLock_IR);
  11959. + if (unlikely(err)) {
  11960. + di_read_unlock(dentry, AuLock_IR);
  11961. + fi_write_unlock(file);
  11962. + goto out;
  11963. + }
  11964. +
  11965. + bstart = au_fbstart(file);
  11966. + h_file = au_hf_top(file);
  11967. + get_file(h_file);
  11968. + h_inode = file_inode(h_file);
  11969. + blks = h_inode->i_blocks;
  11970. + au_unpin(&pin);
  11971. + di_read_unlock(dentry, AuLock_IR);
  11972. + fi_write_unlock(file);
  11973. +
  11974. + err = vfsub_write_u(h_file, buf, count, ppos);
  11975. + ii_write_lock_child(inode);
  11976. + au_cpup_attr_timesizes(inode);
  11977. + inode->i_mode = file_inode(h_file)->i_mode;
  11978. + AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks);
  11979. + if (err > 0)
  11980. + au_fhsm_wrote(sb, bstart, /*force*/h_inode->i_blocks > blks);
  11981. + ii_write_unlock(inode);
  11982. + fput(h_file);
  11983. +
  11984. +out:
  11985. + si_read_unlock(sb);
  11986. + mutex_unlock(&inode->i_mutex);
  11987. + return err;
  11988. +}
  11989. +
  11990. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  11991. + struct iov_iter *iov_iter)
  11992. +{
  11993. + ssize_t err;
  11994. + struct file *file;
  11995. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  11996. + ssize_t (*aio)(struct kiocb *, const struct iovec *, unsigned long,
  11997. + loff_t);
  11998. +
  11999. + err = security_file_permission(h_file, rw);
  12000. + if (unlikely(err))
  12001. + goto out;
  12002. +
  12003. + err = -ENOSYS;
  12004. + iter = NULL;
  12005. + aio = NULL;
  12006. + if (rw == MAY_READ) {
  12007. + iter = h_file->f_op->read_iter;
  12008. + aio = h_file->f_op->aio_read;
  12009. + } else if (rw == MAY_WRITE) {
  12010. + iter = h_file->f_op->write_iter;
  12011. + aio = h_file->f_op->aio_write;
  12012. + }
  12013. +
  12014. + file = kio->ki_filp;
  12015. + kio->ki_filp = h_file;
  12016. + if (iter) {
  12017. + lockdep_off();
  12018. + err = iter(kio, iov_iter);
  12019. + lockdep_on();
  12020. + } else if (aio) {
  12021. + lockdep_off();
  12022. + err = aio(kio, iov_iter->iov, iov_iter->nr_segs, kio->ki_pos);
  12023. + lockdep_on();
  12024. + } else
  12025. + /* currently there is no such fs */
  12026. + WARN_ON_ONCE(1);
  12027. + kio->ki_filp = file;
  12028. +
  12029. +out:
  12030. + return err;
  12031. +}
  12032. +
  12033. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12034. +{
  12035. + ssize_t err;
  12036. + struct file *file, *h_file;
  12037. + struct dentry *dentry;
  12038. + struct super_block *sb;
  12039. +
  12040. + file = kio->ki_filp;
  12041. + dentry = file->f_path.dentry;
  12042. + sb = dentry->d_sb;
  12043. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12044. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  12045. + if (unlikely(err))
  12046. + goto out;
  12047. +
  12048. + h_file = au_hf_top(file);
  12049. + get_file(h_file);
  12050. + di_read_unlock(dentry, AuLock_IR);
  12051. + fi_read_unlock(file);
  12052. +
  12053. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  12054. + /* todo: necessary? */
  12055. + /* file->f_ra = h_file->f_ra; */
  12056. + /* update without lock, I don't think it a problem */
  12057. + fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
  12058. + fput(h_file);
  12059. +
  12060. +out:
  12061. + si_read_unlock(sb);
  12062. + return err;
  12063. +}
  12064. +
  12065. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12066. +{
  12067. + ssize_t err;
  12068. + blkcnt_t blks;
  12069. + aufs_bindex_t bstart;
  12070. + struct au_pin pin;
  12071. + struct dentry *dentry;
  12072. + struct inode *inode, *h_inode;
  12073. + struct file *file, *h_file;
  12074. + struct super_block *sb;
  12075. +
  12076. + file = kio->ki_filp;
  12077. + dentry = file->f_path.dentry;
  12078. + sb = dentry->d_sb;
  12079. + inode = dentry->d_inode;
  12080. + au_mtx_and_read_lock(inode);
  12081. +
  12082. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12083. + if (unlikely(err))
  12084. + goto out;
  12085. +
  12086. + err = au_ready_to_write(file, -1, &pin);
  12087. + di_downgrade_lock(dentry, AuLock_IR);
  12088. + if (unlikely(err)) {
  12089. + di_read_unlock(dentry, AuLock_IR);
  12090. + fi_write_unlock(file);
  12091. + goto out;
  12092. + }
  12093. +
  12094. + bstart = au_fbstart(file);
  12095. + h_file = au_hf_top(file);
  12096. + get_file(h_file);
  12097. + h_inode = file_inode(h_file);
  12098. + blks = h_inode->i_blocks;
  12099. + au_unpin(&pin);
  12100. + di_read_unlock(dentry, AuLock_IR);
  12101. + fi_write_unlock(file);
  12102. +
  12103. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  12104. + ii_write_lock_child(inode);
  12105. + au_cpup_attr_timesizes(inode);
  12106. + inode->i_mode = file_inode(h_file)->i_mode;
  12107. + AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks);
  12108. + if (err > 0)
  12109. + au_fhsm_wrote(sb, bstart, /*force*/h_inode->i_blocks > blks);
  12110. + ii_write_unlock(inode);
  12111. + fput(h_file);
  12112. +
  12113. +out:
  12114. + si_read_unlock(sb);
  12115. + mutex_unlock(&inode->i_mutex);
  12116. + return err;
  12117. +}
  12118. +
  12119. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  12120. + struct pipe_inode_info *pipe, size_t len,
  12121. + unsigned int flags)
  12122. +{
  12123. + ssize_t err;
  12124. + struct file *h_file;
  12125. + struct dentry *dentry;
  12126. + struct super_block *sb;
  12127. +
  12128. + dentry = file->f_path.dentry;
  12129. + sb = dentry->d_sb;
  12130. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12131. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  12132. + if (unlikely(err))
  12133. + goto out;
  12134. +
  12135. + err = -EINVAL;
  12136. + h_file = au_hf_top(file);
  12137. + get_file(h_file);
  12138. + if (au_test_loopback_kthread()) {
  12139. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  12140. + if (file->f_mapping != h_file->f_mapping) {
  12141. + file->f_mapping = h_file->f_mapping;
  12142. + smp_mb(); /* unnecessary? */
  12143. + }
  12144. + }
  12145. + di_read_unlock(dentry, AuLock_IR);
  12146. + fi_read_unlock(file);
  12147. +
  12148. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  12149. + /* todo: necessasry? */
  12150. + /* file->f_ra = h_file->f_ra; */
  12151. + /* update without lock, I don't think it a problem */
  12152. + fsstack_copy_attr_atime(dentry->d_inode, file_inode(h_file));
  12153. + fput(h_file);
  12154. +
  12155. +out:
  12156. + si_read_unlock(sb);
  12157. + return err;
  12158. +}
  12159. +
  12160. +static ssize_t
  12161. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  12162. + size_t len, unsigned int flags)
  12163. +{
  12164. + ssize_t err;
  12165. + blkcnt_t blks;
  12166. + aufs_bindex_t bstart;
  12167. + struct au_pin pin;
  12168. + struct dentry *dentry;
  12169. + struct inode *inode, *h_inode;
  12170. + struct super_block *sb;
  12171. + struct file *h_file;
  12172. +
  12173. + dentry = file->f_path.dentry;
  12174. + sb = dentry->d_sb;
  12175. + inode = dentry->d_inode;
  12176. + au_mtx_and_read_lock(inode);
  12177. +
  12178. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12179. + if (unlikely(err))
  12180. + goto out;
  12181. +
  12182. + err = au_ready_to_write(file, -1, &pin);
  12183. + di_downgrade_lock(dentry, AuLock_IR);
  12184. + if (unlikely(err)) {
  12185. + di_read_unlock(dentry, AuLock_IR);
  12186. + fi_write_unlock(file);
  12187. + goto out;
  12188. + }
  12189. +
  12190. + bstart = au_fbstart(file);
  12191. + h_file = au_hf_top(file);
  12192. + get_file(h_file);
  12193. + h_inode = file_inode(h_file);
  12194. + blks = h_inode->i_blocks;
  12195. + au_unpin(&pin);
  12196. + di_read_unlock(dentry, AuLock_IR);
  12197. + fi_write_unlock(file);
  12198. +
  12199. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  12200. + ii_write_lock_child(inode);
  12201. + au_cpup_attr_timesizes(inode);
  12202. + inode->i_mode = file_inode(h_file)->i_mode;
  12203. + AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks);
  12204. + if (err > 0)
  12205. + au_fhsm_wrote(sb, bstart, /*force*/h_inode->i_blocks > blks);
  12206. + ii_write_unlock(inode);
  12207. + fput(h_file);
  12208. +
  12209. +out:
  12210. + si_read_unlock(sb);
  12211. + mutex_unlock(&inode->i_mutex);
  12212. + return err;
  12213. +}
  12214. +
  12215. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  12216. + loff_t len)
  12217. +{
  12218. + long err;
  12219. + struct au_pin pin;
  12220. + struct dentry *dentry;
  12221. + struct super_block *sb;
  12222. + struct inode *inode;
  12223. + struct file *h_file;
  12224. +
  12225. + dentry = file->f_path.dentry;
  12226. + sb = dentry->d_sb;
  12227. + inode = dentry->d_inode;
  12228. + au_mtx_and_read_lock(inode);
  12229. +
  12230. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12231. + if (unlikely(err))
  12232. + goto out;
  12233. +
  12234. + err = au_ready_to_write(file, -1, &pin);
  12235. + di_downgrade_lock(dentry, AuLock_IR);
  12236. + if (unlikely(err)) {
  12237. + di_read_unlock(dentry, AuLock_IR);
  12238. + fi_write_unlock(file);
  12239. + goto out;
  12240. + }
  12241. +
  12242. + h_file = au_hf_top(file);
  12243. + get_file(h_file);
  12244. + au_unpin(&pin);
  12245. + di_read_unlock(dentry, AuLock_IR);
  12246. + fi_write_unlock(file);
  12247. +
  12248. + lockdep_off();
  12249. + err = vfs_fallocate(h_file, mode, offset, len);
  12250. + lockdep_on();
  12251. + ii_write_lock_child(inode);
  12252. + au_cpup_attr_timesizes(inode);
  12253. + inode->i_mode = file_inode(h_file)->i_mode;
  12254. + ii_write_unlock(inode);
  12255. + fput(h_file);
  12256. +
  12257. +out:
  12258. + si_read_unlock(sb);
  12259. + mutex_unlock(&inode->i_mutex);
  12260. + return err;
  12261. +}
  12262. +
  12263. +/* ---------------------------------------------------------------------- */
  12264. +
  12265. +/*
  12266. + * The locking order around current->mmap_sem.
  12267. + * - in most and regular cases
  12268. + * file I/O syscall -- aufs_read() or something
  12269. + * -- si_rwsem for read -- mmap_sem
  12270. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  12271. + * - in mmap case
  12272. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  12273. + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
  12274. + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
  12275. + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  12276. + * It means that when aufs acquires si_rwsem for write, the process should never
  12277. + * acquire mmap_sem.
  12278. + *
  12279. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  12280. + * problem either since any directory is not able to be mmap-ed.
  12281. + * The similar scenario is applied to aufs_readlink() too.
  12282. + */
  12283. +
  12284. +#if 0 /* stop calling security_file_mmap() */
  12285. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  12286. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  12287. +
  12288. +static unsigned long au_arch_prot_conv(unsigned long flags)
  12289. +{
  12290. + /* currently ppc64 only */
  12291. +#ifdef CONFIG_PPC64
  12292. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  12293. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  12294. + return AuConv_VM_PROT(flags, SAO);
  12295. +#else
  12296. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  12297. + return 0;
  12298. +#endif
  12299. +}
  12300. +
  12301. +static unsigned long au_prot_conv(unsigned long flags)
  12302. +{
  12303. + return AuConv_VM_PROT(flags, READ)
  12304. + | AuConv_VM_PROT(flags, WRITE)
  12305. + | AuConv_VM_PROT(flags, EXEC)
  12306. + | au_arch_prot_conv(flags);
  12307. +}
  12308. +
  12309. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  12310. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  12311. +
  12312. +static unsigned long au_flag_conv(unsigned long flags)
  12313. +{
  12314. + return AuConv_VM_MAP(flags, GROWSDOWN)
  12315. + | AuConv_VM_MAP(flags, DENYWRITE)
  12316. + | AuConv_VM_MAP(flags, LOCKED);
  12317. +}
  12318. +#endif
  12319. +
  12320. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  12321. +{
  12322. + int err;
  12323. + aufs_bindex_t bstart;
  12324. + const unsigned char wlock
  12325. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  12326. + struct dentry *dentry;
  12327. + struct super_block *sb;
  12328. + struct file *h_file;
  12329. + struct au_branch *br;
  12330. + struct au_pin pin;
  12331. +
  12332. + AuDbgVmRegion(file, vma);
  12333. +
  12334. + dentry = file->f_path.dentry;
  12335. + sb = dentry->d_sb;
  12336. + lockdep_off();
  12337. + si_read_lock(sb, AuLock_NOPLMW);
  12338. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12339. + if (unlikely(err))
  12340. + goto out;
  12341. +
  12342. + if (wlock) {
  12343. + err = au_ready_to_write(file, -1, &pin);
  12344. + di_write_unlock(dentry);
  12345. + if (unlikely(err)) {
  12346. + fi_write_unlock(file);
  12347. + goto out;
  12348. + }
  12349. + au_unpin(&pin);
  12350. + } else
  12351. + di_write_unlock(dentry);
  12352. +
  12353. + bstart = au_fbstart(file);
  12354. + br = au_sbr(sb, bstart);
  12355. + h_file = au_hf_top(file);
  12356. + get_file(h_file);
  12357. + au_set_mmapped(file);
  12358. + fi_write_unlock(file);
  12359. + lockdep_on();
  12360. +
  12361. + au_vm_file_reset(vma, h_file);
  12362. + /*
  12363. + * we cannot call security_mmap_file() here since it may acquire
  12364. + * mmap_sem or i_mutex.
  12365. + *
  12366. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  12367. + * au_flag_conv(vma->vm_flags));
  12368. + */
  12369. + if (!err)
  12370. + err = h_file->f_op->mmap(h_file, vma);
  12371. + if (unlikely(err))
  12372. + goto out_reset;
  12373. +
  12374. + au_vm_prfile_set(vma, file);
  12375. + /* update without lock, I don't think it a problem */
  12376. + fsstack_copy_attr_atime(file_inode(file), file_inode(h_file));
  12377. + goto out_fput; /* success */
  12378. +
  12379. +out_reset:
  12380. + au_unset_mmapped(file);
  12381. + au_vm_file_reset(vma, file);
  12382. +out_fput:
  12383. + fput(h_file);
  12384. + lockdep_off();
  12385. +out:
  12386. + si_read_unlock(sb);
  12387. + lockdep_on();
  12388. + AuTraceErr(err);
  12389. + return err;
  12390. +}
  12391. +
  12392. +/* ---------------------------------------------------------------------- */
  12393. +
  12394. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  12395. + int datasync)
  12396. +{
  12397. + int err;
  12398. + struct au_pin pin;
  12399. + struct dentry *dentry;
  12400. + struct inode *inode;
  12401. + struct file *h_file;
  12402. + struct super_block *sb;
  12403. +
  12404. + dentry = file->f_path.dentry;
  12405. + inode = dentry->d_inode;
  12406. + sb = dentry->d_sb;
  12407. + mutex_lock(&inode->i_mutex);
  12408. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  12409. + if (unlikely(err))
  12410. + goto out;
  12411. +
  12412. + err = 0; /* -EBADF; */ /* posix? */
  12413. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  12414. + goto out_si;
  12415. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12416. + if (unlikely(err))
  12417. + goto out_si;
  12418. +
  12419. + err = au_ready_to_write(file, -1, &pin);
  12420. + di_downgrade_lock(dentry, AuLock_IR);
  12421. + if (unlikely(err))
  12422. + goto out_unlock;
  12423. + au_unpin(&pin);
  12424. +
  12425. + err = -EINVAL;
  12426. + h_file = au_hf_top(file);
  12427. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  12428. + au_cpup_attr_timesizes(inode);
  12429. +
  12430. +out_unlock:
  12431. + di_read_unlock(dentry, AuLock_IR);
  12432. + fi_write_unlock(file);
  12433. +out_si:
  12434. + si_read_unlock(sb);
  12435. +out:
  12436. + mutex_unlock(&inode->i_mutex);
  12437. + return err;
  12438. +}
  12439. +
  12440. +/* no one supports this operation, currently */
  12441. +#if 0
  12442. +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
  12443. +{
  12444. + int err;
  12445. + struct au_pin pin;
  12446. + struct dentry *dentry;
  12447. + struct inode *inode;
  12448. + struct file *file, *h_file;
  12449. +
  12450. + file = kio->ki_filp;
  12451. + dentry = file->f_path.dentry;
  12452. + inode = dentry->d_inode;
  12453. + au_mtx_and_read_lock(inode);
  12454. +
  12455. + err = 0; /* -EBADF; */ /* posix? */
  12456. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  12457. + goto out;
  12458. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12459. + if (unlikely(err))
  12460. + goto out;
  12461. +
  12462. + err = au_ready_to_write(file, -1, &pin);
  12463. + di_downgrade_lock(dentry, AuLock_IR);
  12464. + if (unlikely(err))
  12465. + goto out_unlock;
  12466. + au_unpin(&pin);
  12467. +
  12468. + err = -ENOSYS;
  12469. + h_file = au_hf_top(file);
  12470. + if (h_file->f_op->aio_fsync) {
  12471. + struct mutex *h_mtx;
  12472. +
  12473. + h_mtx = &file_inode(h_file)->i_mutex;
  12474. + if (!is_sync_kiocb(kio)) {
  12475. + get_file(h_file);
  12476. + fput(file);
  12477. + }
  12478. + kio->ki_filp = h_file;
  12479. + err = h_file->f_op->aio_fsync(kio, datasync);
  12480. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  12481. + if (!err)
  12482. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
  12483. + /*ignore*/
  12484. + au_cpup_attr_timesizes(inode);
  12485. + mutex_unlock(h_mtx);
  12486. + }
  12487. +
  12488. +out_unlock:
  12489. + di_read_unlock(dentry, AuLock_IR);
  12490. + fi_write_unlock(file);
  12491. +out:
  12492. + si_read_unlock(inode->sb);
  12493. + mutex_unlock(&inode->i_mutex);
  12494. + return err;
  12495. +}
  12496. +#endif
  12497. +
  12498. +static int aufs_fasync(int fd, struct file *file, int flag)
  12499. +{
  12500. + int err;
  12501. + struct file *h_file;
  12502. + struct dentry *dentry;
  12503. + struct super_block *sb;
  12504. +
  12505. + dentry = file->f_path.dentry;
  12506. + sb = dentry->d_sb;
  12507. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12508. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  12509. + if (unlikely(err))
  12510. + goto out;
  12511. +
  12512. + h_file = au_hf_top(file);
  12513. + if (h_file->f_op->fasync)
  12514. + err = h_file->f_op->fasync(fd, h_file, flag);
  12515. +
  12516. + di_read_unlock(dentry, AuLock_IR);
  12517. + fi_read_unlock(file);
  12518. +
  12519. +out:
  12520. + si_read_unlock(sb);
  12521. + return err;
  12522. +}
  12523. +
  12524. +/* ---------------------------------------------------------------------- */
  12525. +
  12526. +/* no one supports this operation, currently */
  12527. +#if 0
  12528. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  12529. + size_t len, loff_t *pos, int more)
  12530. +{
  12531. +}
  12532. +#endif
  12533. +
  12534. +/* ---------------------------------------------------------------------- */
  12535. +
  12536. +const struct file_operations aufs_file_fop = {
  12537. + .owner = THIS_MODULE,
  12538. +
  12539. + .llseek = default_llseek,
  12540. +
  12541. + .read = aufs_read,
  12542. + .write = aufs_write,
  12543. + .read_iter = aufs_read_iter,
  12544. + .write_iter = aufs_write_iter,
  12545. +
  12546. +#ifdef CONFIG_AUFS_POLL
  12547. + .poll = aufs_poll,
  12548. +#endif
  12549. + .unlocked_ioctl = aufs_ioctl_nondir,
  12550. +#ifdef CONFIG_COMPAT
  12551. + .compat_ioctl = aufs_compat_ioctl_nondir,
  12552. +#endif
  12553. + .mmap = aufs_mmap,
  12554. + .open = aufs_open_nondir,
  12555. + .flush = aufs_flush_nondir,
  12556. + .release = aufs_release_nondir,
  12557. + .fsync = aufs_fsync_nondir,
  12558. + /* .aio_fsync = aufs_aio_fsync_nondir, */
  12559. + .fasync = aufs_fasync,
  12560. + /* .sendpage = aufs_sendpage, */
  12561. + .splice_write = aufs_splice_write,
  12562. + .splice_read = aufs_splice_read,
  12563. +#if 0
  12564. + .aio_splice_write = aufs_aio_splice_write,
  12565. + .aio_splice_read = aufs_aio_splice_read,
  12566. +#endif
  12567. + .fallocate = aufs_fallocate
  12568. +};
  12569. diff -Nur linux-4.0.4.orig/fs/aufs/fstype.h linux-4.0.4/fs/aufs/fstype.h
  12570. --- linux-4.0.4.orig/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
  12571. +++ linux-4.0.4/fs/aufs/fstype.h 2015-05-30 22:11:31.000000000 +0200
  12572. @@ -0,0 +1,387 @@
  12573. +/*
  12574. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  12575. + */
  12576. +
  12577. +/*
  12578. + * judging filesystem type
  12579. + */
  12580. +
  12581. +#ifndef __AUFS_FSTYPE_H__
  12582. +#define __AUFS_FSTYPE_H__
  12583. +
  12584. +#ifdef __KERNEL__
  12585. +
  12586. +#include <linux/fs.h>
  12587. +#include <linux/magic.h>
  12588. +#include <linux/romfs_fs.h>
  12589. +#include <linux/nfs_fs.h>
  12590. +
  12591. +static inline int au_test_aufs(struct super_block *sb)
  12592. +{
  12593. + return sb->s_magic == AUFS_SUPER_MAGIC;
  12594. +}
  12595. +
  12596. +static inline const char *au_sbtype(struct super_block *sb)
  12597. +{
  12598. + return sb->s_type->name;
  12599. +}
  12600. +
  12601. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  12602. +{
  12603. +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
  12604. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  12605. +#else
  12606. + return 0;
  12607. +#endif
  12608. +}
  12609. +
  12610. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  12611. +{
  12612. +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
  12613. + return sb->s_magic == ROMFS_MAGIC;
  12614. +#else
  12615. + return 0;
  12616. +#endif
  12617. +}
  12618. +
  12619. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  12620. +{
  12621. +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
  12622. + return sb->s_magic == CRAMFS_MAGIC;
  12623. +#endif
  12624. + return 0;
  12625. +}
  12626. +
  12627. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  12628. +{
  12629. +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
  12630. + return sb->s_magic == NFS_SUPER_MAGIC;
  12631. +#else
  12632. + return 0;
  12633. +#endif
  12634. +}
  12635. +
  12636. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  12637. +{
  12638. +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
  12639. + return sb->s_magic == FUSE_SUPER_MAGIC;
  12640. +#else
  12641. + return 0;
  12642. +#endif
  12643. +}
  12644. +
  12645. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  12646. +{
  12647. +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
  12648. + return sb->s_magic == XFS_SB_MAGIC;
  12649. +#else
  12650. + return 0;
  12651. +#endif
  12652. +}
  12653. +
  12654. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  12655. +{
  12656. +#ifdef CONFIG_TMPFS
  12657. + return sb->s_magic == TMPFS_MAGIC;
  12658. +#else
  12659. + return 0;
  12660. +#endif
  12661. +}
  12662. +
  12663. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  12664. +{
  12665. +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
  12666. + return !strcmp(au_sbtype(sb), "ecryptfs");
  12667. +#else
  12668. + return 0;
  12669. +#endif
  12670. +}
  12671. +
  12672. +static inline int au_test_ramfs(struct super_block *sb)
  12673. +{
  12674. + return sb->s_magic == RAMFS_MAGIC;
  12675. +}
  12676. +
  12677. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  12678. +{
  12679. +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
  12680. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  12681. +#else
  12682. + return 0;
  12683. +#endif
  12684. +}
  12685. +
  12686. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  12687. +{
  12688. +#ifdef CONFIG_PROC_FS
  12689. + return sb->s_magic == PROC_SUPER_MAGIC;
  12690. +#else
  12691. + return 0;
  12692. +#endif
  12693. +}
  12694. +
  12695. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  12696. +{
  12697. +#ifdef CONFIG_SYSFS
  12698. + return sb->s_magic == SYSFS_MAGIC;
  12699. +#else
  12700. + return 0;
  12701. +#endif
  12702. +}
  12703. +
  12704. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  12705. +{
  12706. +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
  12707. + return sb->s_magic == CONFIGFS_MAGIC;
  12708. +#else
  12709. + return 0;
  12710. +#endif
  12711. +}
  12712. +
  12713. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  12714. +{
  12715. +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
  12716. + return sb->s_magic == MINIX3_SUPER_MAGIC
  12717. + || sb->s_magic == MINIX2_SUPER_MAGIC
  12718. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  12719. + || sb->s_magic == MINIX_SUPER_MAGIC
  12720. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  12721. +#else
  12722. + return 0;
  12723. +#endif
  12724. +}
  12725. +
  12726. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  12727. +{
  12728. +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
  12729. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  12730. +#else
  12731. + return 0;
  12732. +#endif
  12733. +}
  12734. +
  12735. +static inline int au_test_msdos(struct super_block *sb)
  12736. +{
  12737. + return au_test_fat(sb);
  12738. +}
  12739. +
  12740. +static inline int au_test_vfat(struct super_block *sb)
  12741. +{
  12742. + return au_test_fat(sb);
  12743. +}
  12744. +
  12745. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  12746. +{
  12747. +#ifdef CONFIG_SECURITYFS
  12748. + return sb->s_magic == SECURITYFS_MAGIC;
  12749. +#else
  12750. + return 0;
  12751. +#endif
  12752. +}
  12753. +
  12754. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  12755. +{
  12756. +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
  12757. + return sb->s_magic == SQUASHFS_MAGIC;
  12758. +#else
  12759. + return 0;
  12760. +#endif
  12761. +}
  12762. +
  12763. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  12764. +{
  12765. +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
  12766. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  12767. +#else
  12768. + return 0;
  12769. +#endif
  12770. +}
  12771. +
  12772. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  12773. +{
  12774. +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
  12775. + return sb->s_magic == XENFS_SUPER_MAGIC;
  12776. +#else
  12777. + return 0;
  12778. +#endif
  12779. +}
  12780. +
  12781. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  12782. +{
  12783. +#ifdef CONFIG_DEBUG_FS
  12784. + return sb->s_magic == DEBUGFS_MAGIC;
  12785. +#else
  12786. + return 0;
  12787. +#endif
  12788. +}
  12789. +
  12790. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  12791. +{
  12792. +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
  12793. + return sb->s_magic == NILFS_SUPER_MAGIC;
  12794. +#else
  12795. + return 0;
  12796. +#endif
  12797. +}
  12798. +
  12799. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  12800. +{
  12801. +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
  12802. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  12803. +#else
  12804. + return 0;
  12805. +#endif
  12806. +}
  12807. +
  12808. +/* ---------------------------------------------------------------------- */
  12809. +/*
  12810. + * they can't be an aufs branch.
  12811. + */
  12812. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  12813. +{
  12814. + return
  12815. +#ifndef CONFIG_AUFS_BR_RAMFS
  12816. + au_test_ramfs(sb) ||
  12817. +#endif
  12818. + au_test_procfs(sb)
  12819. + || au_test_sysfs(sb)
  12820. + || au_test_configfs(sb)
  12821. + || au_test_debugfs(sb)
  12822. + || au_test_securityfs(sb)
  12823. + || au_test_xenfs(sb)
  12824. + || au_test_ecryptfs(sb)
  12825. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  12826. + || au_test_aufs(sb); /* will be supported in next version */
  12827. +}
  12828. +
  12829. +static inline int au_test_fs_remote(struct super_block *sb)
  12830. +{
  12831. + return !au_test_tmpfs(sb)
  12832. +#ifdef CONFIG_AUFS_BR_RAMFS
  12833. + && !au_test_ramfs(sb)
  12834. +#endif
  12835. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  12836. +}
  12837. +
  12838. +/* ---------------------------------------------------------------------- */
  12839. +
  12840. +/*
  12841. + * Note: these functions (below) are created after reading ->getattr() in all
  12842. + * filesystems under linux/fs. it means we have to do so in every update...
  12843. + */
  12844. +
  12845. +/*
  12846. + * some filesystems require getattr to refresh the inode attributes before
  12847. + * referencing.
  12848. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  12849. + * and leave the work for d_revalidate()
  12850. + */
  12851. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  12852. +{
  12853. + return au_test_nfs(sb)
  12854. + || au_test_fuse(sb)
  12855. + /* || au_test_btrfs(sb) */ /* untested */
  12856. + ;
  12857. +}
  12858. +
  12859. +/*
  12860. + * filesystems which don't maintain i_size or i_blocks.
  12861. + */
  12862. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  12863. +{
  12864. + return au_test_xfs(sb)
  12865. + || au_test_btrfs(sb)
  12866. + || au_test_ubifs(sb)
  12867. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  12868. + /* || au_test_minix(sb) */ /* untested */
  12869. + ;
  12870. +}
  12871. +
  12872. +/*
  12873. + * filesystems which don't store the correct value in some of their inode
  12874. + * attributes.
  12875. + */
  12876. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  12877. +{
  12878. + return au_test_fs_bad_iattr_size(sb)
  12879. + || au_test_fat(sb)
  12880. + || au_test_msdos(sb)
  12881. + || au_test_vfat(sb);
  12882. +}
  12883. +
  12884. +/* they don't check i_nlink in link(2) */
  12885. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  12886. +{
  12887. + return au_test_tmpfs(sb)
  12888. +#ifdef CONFIG_AUFS_BR_RAMFS
  12889. + || au_test_ramfs(sb)
  12890. +#endif
  12891. + || au_test_ubifs(sb)
  12892. + || au_test_hfsplus(sb);
  12893. +}
  12894. +
  12895. +/*
  12896. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  12897. + */
  12898. +static inline int au_test_fs_notime(struct super_block *sb)
  12899. +{
  12900. + return au_test_nfs(sb)
  12901. + || au_test_fuse(sb)
  12902. + || au_test_ubifs(sb)
  12903. + ;
  12904. +}
  12905. +
  12906. +/* temporary support for i#1 in cramfs */
  12907. +static inline int au_test_fs_unique_ino(struct inode *inode)
  12908. +{
  12909. + if (au_test_cramfs(inode->i_sb))
  12910. + return inode->i_ino != 1;
  12911. + return 1;
  12912. +}
  12913. +
  12914. +/* ---------------------------------------------------------------------- */
  12915. +
  12916. +/*
  12917. + * the filesystem where the xino files placed must support i/o after unlink and
  12918. + * maintain i_size and i_blocks.
  12919. + */
  12920. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  12921. +{
  12922. + return au_test_fs_remote(sb)
  12923. + || au_test_fs_bad_iattr_size(sb)
  12924. + /* don't want unnecessary work for xino */
  12925. + || au_test_aufs(sb)
  12926. + || au_test_ecryptfs(sb)
  12927. + || au_test_nilfs(sb);
  12928. +}
  12929. +
  12930. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  12931. +{
  12932. + return au_test_tmpfs(sb)
  12933. + || au_test_ramfs(sb);
  12934. +}
  12935. +
  12936. +/*
  12937. + * test if the @sb is real-readonly.
  12938. + */
  12939. +static inline int au_test_fs_rr(struct super_block *sb)
  12940. +{
  12941. + return au_test_squashfs(sb)
  12942. + || au_test_iso9660(sb)
  12943. + || au_test_cramfs(sb)
  12944. + || au_test_romfs(sb);
  12945. +}
  12946. +
  12947. +/*
  12948. + * test if the @inode is nfs with 'noacl' option
  12949. + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
  12950. + */
  12951. +static inline int au_test_nfs_noacl(struct inode *inode)
  12952. +{
  12953. + return au_test_nfs(inode->i_sb)
  12954. + /* && IS_POSIXACL(inode) */
  12955. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  12956. +}
  12957. +
  12958. +#endif /* __KERNEL__ */
  12959. +#endif /* __AUFS_FSTYPE_H__ */
  12960. diff -Nur linux-4.0.4.orig/fs/aufs/hfsnotify.c linux-4.0.4/fs/aufs/hfsnotify.c
  12961. --- linux-4.0.4.orig/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
  12962. +++ linux-4.0.4/fs/aufs/hfsnotify.c 2015-05-30 22:11:31.000000000 +0200
  12963. @@ -0,0 +1,275 @@
  12964. +/*
  12965. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  12966. + */
  12967. +
  12968. +/*
  12969. + * fsnotify for the lower directories
  12970. + */
  12971. +
  12972. +#include "aufs.h"
  12973. +
  12974. +/* FS_IN_IGNORED is unnecessary */
  12975. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  12976. + | FS_CREATE | FS_EVENT_ON_CHILD);
  12977. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  12978. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  12979. +
  12980. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  12981. +{
  12982. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  12983. + hn_mark);
  12984. + AuDbg("here\n");
  12985. + au_cache_free_hnotify(hn);
  12986. + smp_mb__before_atomic();
  12987. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  12988. + wake_up(&au_hfsn_wq);
  12989. +}
  12990. +
  12991. +static int au_hfsn_alloc(struct au_hinode *hinode)
  12992. +{
  12993. + int err;
  12994. + struct au_hnotify *hn;
  12995. + struct super_block *sb;
  12996. + struct au_branch *br;
  12997. + struct fsnotify_mark *mark;
  12998. + aufs_bindex_t bindex;
  12999. +
  13000. + hn = hinode->hi_notify;
  13001. + sb = hn->hn_aufs_inode->i_sb;
  13002. + bindex = au_br_index(sb, hinode->hi_id);
  13003. + br = au_sbr(sb, bindex);
  13004. + AuDebugOn(!br->br_hfsn);
  13005. +
  13006. + mark = &hn->hn_mark;
  13007. + fsnotify_init_mark(mark, au_hfsn_free_mark);
  13008. + mark->mask = AuHfsnMask;
  13009. + /*
  13010. + * by udba rename or rmdir, aufs assign a new inode to the known
  13011. + * h_inode, so specify 1 to allow dups.
  13012. + */
  13013. + lockdep_off();
  13014. + err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
  13015. + /*mnt*/NULL, /*allow_dups*/1);
  13016. + /* even if err */
  13017. + fsnotify_put_mark(mark);
  13018. + lockdep_on();
  13019. +
  13020. + return err;
  13021. +}
  13022. +
  13023. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  13024. +{
  13025. + struct fsnotify_mark *mark;
  13026. + unsigned long long ull;
  13027. + struct fsnotify_group *group;
  13028. +
  13029. + ull = atomic64_inc_return(&au_hfsn_ifree);
  13030. + BUG_ON(!ull);
  13031. +
  13032. + mark = &hn->hn_mark;
  13033. + spin_lock(&mark->lock);
  13034. + group = mark->group;
  13035. + fsnotify_get_group(group);
  13036. + spin_unlock(&mark->lock);
  13037. + lockdep_off();
  13038. + fsnotify_destroy_mark(mark, group);
  13039. + fsnotify_put_group(group);
  13040. + lockdep_on();
  13041. +
  13042. + /* free hn by myself */
  13043. + return 0;
  13044. +}
  13045. +
  13046. +/* ---------------------------------------------------------------------- */
  13047. +
  13048. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  13049. +{
  13050. + struct fsnotify_mark *mark;
  13051. +
  13052. + mark = &hinode->hi_notify->hn_mark;
  13053. + spin_lock(&mark->lock);
  13054. + if (do_set) {
  13055. + AuDebugOn(mark->mask & AuHfsnMask);
  13056. + mark->mask |= AuHfsnMask;
  13057. + } else {
  13058. + AuDebugOn(!(mark->mask & AuHfsnMask));
  13059. + mark->mask &= ~AuHfsnMask;
  13060. + }
  13061. + spin_unlock(&mark->lock);
  13062. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  13063. +}
  13064. +
  13065. +/* ---------------------------------------------------------------------- */
  13066. +
  13067. +/* #define AuDbgHnotify */
  13068. +#ifdef AuDbgHnotify
  13069. +static char *au_hfsn_name(u32 mask)
  13070. +{
  13071. +#ifdef CONFIG_AUFS_DEBUG
  13072. +#define test_ret(flag) \
  13073. + do { \
  13074. + if (mask & flag) \
  13075. + return #flag; \
  13076. + } while (0)
  13077. + test_ret(FS_ACCESS);
  13078. + test_ret(FS_MODIFY);
  13079. + test_ret(FS_ATTRIB);
  13080. + test_ret(FS_CLOSE_WRITE);
  13081. + test_ret(FS_CLOSE_NOWRITE);
  13082. + test_ret(FS_OPEN);
  13083. + test_ret(FS_MOVED_FROM);
  13084. + test_ret(FS_MOVED_TO);
  13085. + test_ret(FS_CREATE);
  13086. + test_ret(FS_DELETE);
  13087. + test_ret(FS_DELETE_SELF);
  13088. + test_ret(FS_MOVE_SELF);
  13089. + test_ret(FS_UNMOUNT);
  13090. + test_ret(FS_Q_OVERFLOW);
  13091. + test_ret(FS_IN_IGNORED);
  13092. + test_ret(FS_ISDIR);
  13093. + test_ret(FS_IN_ONESHOT);
  13094. + test_ret(FS_EVENT_ON_CHILD);
  13095. + return "";
  13096. +#undef test_ret
  13097. +#else
  13098. + return "??";
  13099. +#endif
  13100. +}
  13101. +#endif
  13102. +
  13103. +/* ---------------------------------------------------------------------- */
  13104. +
  13105. +static void au_hfsn_free_group(struct fsnotify_group *group)
  13106. +{
  13107. + struct au_br_hfsnotify *hfsn = group->private;
  13108. +
  13109. + AuDbg("here\n");
  13110. + kfree(hfsn);
  13111. +}
  13112. +
  13113. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  13114. + struct inode *inode,
  13115. + struct fsnotify_mark *inode_mark,
  13116. + struct fsnotify_mark *vfsmount_mark,
  13117. + u32 mask, void *data, int data_type,
  13118. + const unsigned char *file_name, u32 cookie)
  13119. +{
  13120. + int err;
  13121. + struct au_hnotify *hnotify;
  13122. + struct inode *h_dir, *h_inode;
  13123. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  13124. +
  13125. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  13126. +
  13127. + err = 0;
  13128. + /* if FS_UNMOUNT happens, there must be another bug */
  13129. + AuDebugOn(mask & FS_UNMOUNT);
  13130. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  13131. + goto out;
  13132. +
  13133. + h_dir = inode;
  13134. + h_inode = NULL;
  13135. +#ifdef AuDbgHnotify
  13136. + au_debug_on();
  13137. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  13138. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  13139. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  13140. + h_dir->i_ino, mask, au_hfsn_name(mask),
  13141. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  13142. + /* WARN_ON(1); */
  13143. + }
  13144. + au_debug_off();
  13145. +#endif
  13146. +
  13147. + AuDebugOn(!inode_mark);
  13148. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  13149. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  13150. +
  13151. +out:
  13152. + return err;
  13153. +}
  13154. +
  13155. +static struct fsnotify_ops au_hfsn_ops = {
  13156. + .handle_event = au_hfsn_handle_event,
  13157. + .free_group_priv = au_hfsn_free_group
  13158. +};
  13159. +
  13160. +/* ---------------------------------------------------------------------- */
  13161. +
  13162. +static void au_hfsn_fin_br(struct au_branch *br)
  13163. +{
  13164. + struct au_br_hfsnotify *hfsn;
  13165. +
  13166. + hfsn = br->br_hfsn;
  13167. + if (hfsn) {
  13168. + lockdep_off();
  13169. + fsnotify_put_group(hfsn->hfsn_group);
  13170. + lockdep_on();
  13171. + }
  13172. +}
  13173. +
  13174. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  13175. +{
  13176. + int err;
  13177. + struct fsnotify_group *group;
  13178. + struct au_br_hfsnotify *hfsn;
  13179. +
  13180. + err = 0;
  13181. + br->br_hfsn = NULL;
  13182. + if (!au_br_hnotifyable(perm))
  13183. + goto out;
  13184. +
  13185. + err = -ENOMEM;
  13186. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  13187. + if (unlikely(!hfsn))
  13188. + goto out;
  13189. +
  13190. + err = 0;
  13191. + group = fsnotify_alloc_group(&au_hfsn_ops);
  13192. + if (IS_ERR(group)) {
  13193. + err = PTR_ERR(group);
  13194. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  13195. + goto out_hfsn;
  13196. + }
  13197. +
  13198. + group->private = hfsn;
  13199. + hfsn->hfsn_group = group;
  13200. + br->br_hfsn = hfsn;
  13201. + goto out; /* success */
  13202. +
  13203. +out_hfsn:
  13204. + kfree(hfsn);
  13205. +out:
  13206. + return err;
  13207. +}
  13208. +
  13209. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  13210. +{
  13211. + int err;
  13212. +
  13213. + err = 0;
  13214. + if (!br->br_hfsn)
  13215. + err = au_hfsn_init_br(br, perm);
  13216. +
  13217. + return err;
  13218. +}
  13219. +
  13220. +/* ---------------------------------------------------------------------- */
  13221. +
  13222. +static void au_hfsn_fin(void)
  13223. +{
  13224. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  13225. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  13226. +}
  13227. +
  13228. +const struct au_hnotify_op au_hnotify_op = {
  13229. + .ctl = au_hfsn_ctl,
  13230. + .alloc = au_hfsn_alloc,
  13231. + .free = au_hfsn_free,
  13232. +
  13233. + .fin = au_hfsn_fin,
  13234. +
  13235. + .reset_br = au_hfsn_reset_br,
  13236. + .fin_br = au_hfsn_fin_br,
  13237. + .init_br = au_hfsn_init_br
  13238. +};
  13239. diff -Nur linux-4.0.4.orig/fs/aufs/hfsplus.c linux-4.0.4/fs/aufs/hfsplus.c
  13240. --- linux-4.0.4.orig/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
  13241. +++ linux-4.0.4/fs/aufs/hfsplus.c 2015-05-30 22:11:30.000000000 +0200
  13242. @@ -0,0 +1,43 @@
  13243. +/*
  13244. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  13245. + */
  13246. +
  13247. +/*
  13248. + * special support for filesystems which aqucires an inode mutex
  13249. + * at final closing a file, eg, hfsplus.
  13250. + *
  13251. + * This trick is very simple and stupid, just to open the file before really
  13252. + * neceeary open to tell hfsplus that this is not the final closing.
  13253. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  13254. + * and au_h_open_post() after releasing it.
  13255. + */
  13256. +
  13257. +#include "aufs.h"
  13258. +
  13259. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  13260. + int force_wr)
  13261. +{
  13262. + struct file *h_file;
  13263. + struct dentry *h_dentry;
  13264. +
  13265. + h_dentry = au_h_dptr(dentry, bindex);
  13266. + AuDebugOn(!h_dentry);
  13267. + AuDebugOn(!h_dentry->d_inode);
  13268. +
  13269. + h_file = NULL;
  13270. + if (au_test_hfsplus(h_dentry->d_sb)
  13271. + && d_is_reg(h_dentry))
  13272. + h_file = au_h_open(dentry, bindex,
  13273. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  13274. + /*file*/NULL, force_wr);
  13275. + return h_file;
  13276. +}
  13277. +
  13278. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  13279. + struct file *h_file)
  13280. +{
  13281. + if (h_file) {
  13282. + fput(h_file);
  13283. + au_sbr_put(dentry->d_sb, bindex);
  13284. + }
  13285. +}
  13286. diff -Nur linux-4.0.4.orig/fs/aufs/hnotify.c linux-4.0.4/fs/aufs/hnotify.c
  13287. --- linux-4.0.4.orig/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
  13288. +++ linux-4.0.4/fs/aufs/hnotify.c 2015-05-30 22:11:30.000000000 +0200
  13289. @@ -0,0 +1,701 @@
  13290. +/*
  13291. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13292. + */
  13293. +
  13294. +/*
  13295. + * abstraction to notify the direct changes on lower directories
  13296. + */
  13297. +
  13298. +#include "aufs.h"
  13299. +
  13300. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  13301. +{
  13302. + int err;
  13303. + struct au_hnotify *hn;
  13304. +
  13305. + err = -ENOMEM;
  13306. + hn = au_cache_alloc_hnotify();
  13307. + if (hn) {
  13308. + hn->hn_aufs_inode = inode;
  13309. + hinode->hi_notify = hn;
  13310. + err = au_hnotify_op.alloc(hinode);
  13311. + AuTraceErr(err);
  13312. + if (unlikely(err)) {
  13313. + hinode->hi_notify = NULL;
  13314. + au_cache_free_hnotify(hn);
  13315. + /*
  13316. + * The upper dir was removed by udba, but the same named
  13317. + * dir left. In this case, aufs assignes a new inode
  13318. + * number and set the monitor again.
  13319. + * For the lower dir, the old monitnor is still left.
  13320. + */
  13321. + if (err == -EEXIST)
  13322. + err = 0;
  13323. + }
  13324. + }
  13325. +
  13326. + AuTraceErr(err);
  13327. + return err;
  13328. +}
  13329. +
  13330. +void au_hn_free(struct au_hinode *hinode)
  13331. +{
  13332. + struct au_hnotify *hn;
  13333. +
  13334. + hn = hinode->hi_notify;
  13335. + if (hn) {
  13336. + hinode->hi_notify = NULL;
  13337. + if (au_hnotify_op.free(hinode, hn))
  13338. + au_cache_free_hnotify(hn);
  13339. + }
  13340. +}
  13341. +
  13342. +/* ---------------------------------------------------------------------- */
  13343. +
  13344. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  13345. +{
  13346. + if (hinode->hi_notify)
  13347. + au_hnotify_op.ctl(hinode, do_set);
  13348. +}
  13349. +
  13350. +void au_hn_reset(struct inode *inode, unsigned int flags)
  13351. +{
  13352. + aufs_bindex_t bindex, bend;
  13353. + struct inode *hi;
  13354. + struct dentry *iwhdentry;
  13355. +
  13356. + bend = au_ibend(inode);
  13357. + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
  13358. + hi = au_h_iptr(inode, bindex);
  13359. + if (!hi)
  13360. + continue;
  13361. +
  13362. + /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
  13363. + iwhdentry = au_hi_wh(inode, bindex);
  13364. + if (iwhdentry)
  13365. + dget(iwhdentry);
  13366. + au_igrab(hi);
  13367. + au_set_h_iptr(inode, bindex, NULL, 0);
  13368. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  13369. + flags & ~AuHi_XINO);
  13370. + iput(hi);
  13371. + dput(iwhdentry);
  13372. + /* mutex_unlock(&hi->i_mutex); */
  13373. + }
  13374. +}
  13375. +
  13376. +/* ---------------------------------------------------------------------- */
  13377. +
  13378. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  13379. +{
  13380. + int err;
  13381. + aufs_bindex_t bindex, bend, bfound, bstart;
  13382. + struct inode *h_i;
  13383. +
  13384. + err = 0;
  13385. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  13386. + pr_warn("branch root dir was changed\n");
  13387. + goto out;
  13388. + }
  13389. +
  13390. + bfound = -1;
  13391. + bend = au_ibend(inode);
  13392. + bstart = au_ibstart(inode);
  13393. +#if 0 /* reserved for future use */
  13394. + if (bindex == bend) {
  13395. + /* keep this ino in rename case */
  13396. + goto out;
  13397. + }
  13398. +#endif
  13399. + for (bindex = bstart; bindex <= bend; bindex++)
  13400. + if (au_h_iptr(inode, bindex) == h_inode) {
  13401. + bfound = bindex;
  13402. + break;
  13403. + }
  13404. + if (bfound < 0)
  13405. + goto out;
  13406. +
  13407. + for (bindex = bstart; bindex <= bend; bindex++) {
  13408. + h_i = au_h_iptr(inode, bindex);
  13409. + if (!h_i)
  13410. + continue;
  13411. +
  13412. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  13413. + /* ignore this error */
  13414. + /* bad action? */
  13415. + }
  13416. +
  13417. + /* children inode number will be broken */
  13418. +
  13419. +out:
  13420. + AuTraceErr(err);
  13421. + return err;
  13422. +}
  13423. +
  13424. +static int hn_gen_tree(struct dentry *dentry)
  13425. +{
  13426. + int err, i, j, ndentry;
  13427. + struct au_dcsub_pages dpages;
  13428. + struct au_dpage *dpage;
  13429. + struct dentry **dentries;
  13430. +
  13431. + err = au_dpages_init(&dpages, GFP_NOFS);
  13432. + if (unlikely(err))
  13433. + goto out;
  13434. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  13435. + if (unlikely(err))
  13436. + goto out_dpages;
  13437. +
  13438. + for (i = 0; i < dpages.ndpage; i++) {
  13439. + dpage = dpages.dpages + i;
  13440. + dentries = dpage->dentries;
  13441. + ndentry = dpage->ndentry;
  13442. + for (j = 0; j < ndentry; j++) {
  13443. + struct dentry *d;
  13444. +
  13445. + d = dentries[j];
  13446. + if (IS_ROOT(d))
  13447. + continue;
  13448. +
  13449. + au_digen_dec(d);
  13450. + if (d->d_inode)
  13451. + /* todo: reset children xino?
  13452. + cached children only? */
  13453. + au_iigen_dec(d->d_inode);
  13454. + }
  13455. + }
  13456. +
  13457. +out_dpages:
  13458. + au_dpages_free(&dpages);
  13459. +
  13460. +#if 0
  13461. + /* discard children */
  13462. + dentry_unhash(dentry);
  13463. + dput(dentry);
  13464. +#endif
  13465. +out:
  13466. + return err;
  13467. +}
  13468. +
  13469. +/*
  13470. + * return 0 if processed.
  13471. + */
  13472. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  13473. + const unsigned int isdir)
  13474. +{
  13475. + int err;
  13476. + struct dentry *d;
  13477. + struct qstr *dname;
  13478. +
  13479. + err = 1;
  13480. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  13481. + pr_warn("branch root dir was changed\n");
  13482. + err = 0;
  13483. + goto out;
  13484. + }
  13485. +
  13486. + if (!isdir) {
  13487. + AuDebugOn(!name);
  13488. + au_iigen_dec(inode);
  13489. + spin_lock(&inode->i_lock);
  13490. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  13491. + spin_lock(&d->d_lock);
  13492. + dname = &d->d_name;
  13493. + if (dname->len != nlen
  13494. + && memcmp(dname->name, name, nlen)) {
  13495. + spin_unlock(&d->d_lock);
  13496. + continue;
  13497. + }
  13498. + err = 0;
  13499. + au_digen_dec(d);
  13500. + spin_unlock(&d->d_lock);
  13501. + break;
  13502. + }
  13503. + spin_unlock(&inode->i_lock);
  13504. + } else {
  13505. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  13506. + d = d_find_any_alias(inode);
  13507. + if (!d) {
  13508. + au_iigen_dec(inode);
  13509. + goto out;
  13510. + }
  13511. +
  13512. + spin_lock(&d->d_lock);
  13513. + dname = &d->d_name;
  13514. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  13515. + spin_unlock(&d->d_lock);
  13516. + err = hn_gen_tree(d);
  13517. + spin_lock(&d->d_lock);
  13518. + }
  13519. + spin_unlock(&d->d_lock);
  13520. + dput(d);
  13521. + }
  13522. +
  13523. +out:
  13524. + AuTraceErr(err);
  13525. + return err;
  13526. +}
  13527. +
  13528. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  13529. +{
  13530. + int err;
  13531. + struct inode *inode;
  13532. +
  13533. + inode = dentry->d_inode;
  13534. + if (IS_ROOT(dentry)
  13535. + /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
  13536. + ) {
  13537. + pr_warn("branch root dir was changed\n");
  13538. + return 0;
  13539. + }
  13540. +
  13541. + err = 0;
  13542. + if (!isdir) {
  13543. + au_digen_dec(dentry);
  13544. + if (inode)
  13545. + au_iigen_dec(inode);
  13546. + } else {
  13547. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  13548. + if (inode)
  13549. + err = hn_gen_tree(dentry);
  13550. + }
  13551. +
  13552. + AuTraceErr(err);
  13553. + return err;
  13554. +}
  13555. +
  13556. +/* ---------------------------------------------------------------------- */
  13557. +
  13558. +/* hnotify job flags */
  13559. +#define AuHnJob_XINO0 1
  13560. +#define AuHnJob_GEN (1 << 1)
  13561. +#define AuHnJob_DIRENT (1 << 2)
  13562. +#define AuHnJob_ISDIR (1 << 3)
  13563. +#define AuHnJob_TRYXINO0 (1 << 4)
  13564. +#define AuHnJob_MNTPNT (1 << 5)
  13565. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  13566. +#define au_fset_hnjob(flags, name) \
  13567. + do { (flags) |= AuHnJob_##name; } while (0)
  13568. +#define au_fclr_hnjob(flags, name) \
  13569. + do { (flags) &= ~AuHnJob_##name; } while (0)
  13570. +
  13571. +enum {
  13572. + AuHn_CHILD,
  13573. + AuHn_PARENT,
  13574. + AuHnLast
  13575. +};
  13576. +
  13577. +struct au_hnotify_args {
  13578. + struct inode *h_dir, *dir, *h_child_inode;
  13579. + u32 mask;
  13580. + unsigned int flags[AuHnLast];
  13581. + unsigned int h_child_nlen;
  13582. + char h_child_name[];
  13583. +};
  13584. +
  13585. +struct hn_job_args {
  13586. + unsigned int flags;
  13587. + struct inode *inode, *h_inode, *dir, *h_dir;
  13588. + struct dentry *dentry;
  13589. + char *h_name;
  13590. + int h_nlen;
  13591. +};
  13592. +
  13593. +static int hn_job(struct hn_job_args *a)
  13594. +{
  13595. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  13596. + int e;
  13597. +
  13598. + /* reset xino */
  13599. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  13600. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  13601. +
  13602. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  13603. + && a->inode
  13604. + && a->h_inode) {
  13605. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  13606. + if (!a->h_inode->i_nlink
  13607. + && !(a->h_inode->i_state & I_LINKABLE))
  13608. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  13609. + mutex_unlock(&a->h_inode->i_mutex);
  13610. + }
  13611. +
  13612. + /* make the generation obsolete */
  13613. + if (au_ftest_hnjob(a->flags, GEN)) {
  13614. + e = -1;
  13615. + if (a->inode)
  13616. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  13617. + isdir);
  13618. + if (e && a->dentry)
  13619. + hn_gen_by_name(a->dentry, isdir);
  13620. + /* ignore this error */
  13621. + }
  13622. +
  13623. + /* make dir entries obsolete */
  13624. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  13625. + struct au_vdir *vdir;
  13626. +
  13627. + vdir = au_ivdir(a->inode);
  13628. + if (vdir)
  13629. + vdir->vd_jiffy = 0;
  13630. + /* IMustLock(a->inode); */
  13631. + /* a->inode->i_version++; */
  13632. + }
  13633. +
  13634. + /* can do nothing but warn */
  13635. + if (au_ftest_hnjob(a->flags, MNTPNT)
  13636. + && a->dentry
  13637. + && d_mountpoint(a->dentry))
  13638. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  13639. +
  13640. + return 0;
  13641. +}
  13642. +
  13643. +/* ---------------------------------------------------------------------- */
  13644. +
  13645. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  13646. + struct inode *dir)
  13647. +{
  13648. + struct dentry *dentry, *d, *parent;
  13649. + struct qstr *dname;
  13650. +
  13651. + parent = d_find_any_alias(dir);
  13652. + if (!parent)
  13653. + return NULL;
  13654. +
  13655. + dentry = NULL;
  13656. + spin_lock(&parent->d_lock);
  13657. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  13658. + /* AuDbg("%pd\n", d); */
  13659. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  13660. + dname = &d->d_name;
  13661. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  13662. + goto cont_unlock;
  13663. + if (au_di(d))
  13664. + au_digen_dec(d);
  13665. + else
  13666. + goto cont_unlock;
  13667. + if (au_dcount(d) > 0) {
  13668. + dentry = dget_dlock(d);
  13669. + spin_unlock(&d->d_lock);
  13670. + break;
  13671. + }
  13672. +
  13673. +cont_unlock:
  13674. + spin_unlock(&d->d_lock);
  13675. + }
  13676. + spin_unlock(&parent->d_lock);
  13677. + dput(parent);
  13678. +
  13679. + if (dentry)
  13680. + di_write_lock_child(dentry);
  13681. +
  13682. + return dentry;
  13683. +}
  13684. +
  13685. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  13686. + aufs_bindex_t bindex, ino_t h_ino)
  13687. +{
  13688. + struct inode *inode;
  13689. + ino_t ino;
  13690. + int err;
  13691. +
  13692. + inode = NULL;
  13693. + err = au_xino_read(sb, bindex, h_ino, &ino);
  13694. + if (!err && ino)
  13695. + inode = ilookup(sb, ino);
  13696. + if (!inode)
  13697. + goto out;
  13698. +
  13699. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  13700. + pr_warn("wrong root branch\n");
  13701. + iput(inode);
  13702. + inode = NULL;
  13703. + goto out;
  13704. + }
  13705. +
  13706. + ii_write_lock_child(inode);
  13707. +
  13708. +out:
  13709. + return inode;
  13710. +}
  13711. +
  13712. +static void au_hn_bh(void *_args)
  13713. +{
  13714. + struct au_hnotify_args *a = _args;
  13715. + struct super_block *sb;
  13716. + aufs_bindex_t bindex, bend, bfound;
  13717. + unsigned char xino, try_iput;
  13718. + int err;
  13719. + struct inode *inode;
  13720. + ino_t h_ino;
  13721. + struct hn_job_args args;
  13722. + struct dentry *dentry;
  13723. + struct au_sbinfo *sbinfo;
  13724. +
  13725. + AuDebugOn(!_args);
  13726. + AuDebugOn(!a->h_dir);
  13727. + AuDebugOn(!a->dir);
  13728. + AuDebugOn(!a->mask);
  13729. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  13730. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  13731. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  13732. +
  13733. + inode = NULL;
  13734. + dentry = NULL;
  13735. + /*
  13736. + * do not lock a->dir->i_mutex here
  13737. + * because of d_revalidate() may cause a deadlock.
  13738. + */
  13739. + sb = a->dir->i_sb;
  13740. + AuDebugOn(!sb);
  13741. + sbinfo = au_sbi(sb);
  13742. + AuDebugOn(!sbinfo);
  13743. + si_write_lock(sb, AuLock_NOPLMW);
  13744. +
  13745. + ii_read_lock_parent(a->dir);
  13746. + bfound = -1;
  13747. + bend = au_ibend(a->dir);
  13748. + for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
  13749. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  13750. + bfound = bindex;
  13751. + break;
  13752. + }
  13753. + ii_read_unlock(a->dir);
  13754. + if (unlikely(bfound < 0))
  13755. + goto out;
  13756. +
  13757. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  13758. + h_ino = 0;
  13759. + if (a->h_child_inode)
  13760. + h_ino = a->h_child_inode->i_ino;
  13761. +
  13762. + if (a->h_child_nlen
  13763. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  13764. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  13765. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  13766. + a->dir);
  13767. + try_iput = 0;
  13768. + if (dentry)
  13769. + inode = dentry->d_inode;
  13770. + if (xino && !inode && h_ino
  13771. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  13772. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  13773. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  13774. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  13775. + try_iput = 1;
  13776. + }
  13777. +
  13778. + args.flags = a->flags[AuHn_CHILD];
  13779. + args.dentry = dentry;
  13780. + args.inode = inode;
  13781. + args.h_inode = a->h_child_inode;
  13782. + args.dir = a->dir;
  13783. + args.h_dir = a->h_dir;
  13784. + args.h_name = a->h_child_name;
  13785. + args.h_nlen = a->h_child_nlen;
  13786. + err = hn_job(&args);
  13787. + if (dentry) {
  13788. + if (au_di(dentry))
  13789. + di_write_unlock(dentry);
  13790. + dput(dentry);
  13791. + }
  13792. + if (inode && try_iput) {
  13793. + ii_write_unlock(inode);
  13794. + iput(inode);
  13795. + }
  13796. +
  13797. + ii_write_lock_parent(a->dir);
  13798. + args.flags = a->flags[AuHn_PARENT];
  13799. + args.dentry = NULL;
  13800. + args.inode = a->dir;
  13801. + args.h_inode = a->h_dir;
  13802. + args.dir = NULL;
  13803. + args.h_dir = NULL;
  13804. + args.h_name = NULL;
  13805. + args.h_nlen = 0;
  13806. + err = hn_job(&args);
  13807. + ii_write_unlock(a->dir);
  13808. +
  13809. +out:
  13810. + iput(a->h_child_inode);
  13811. + iput(a->h_dir);
  13812. + iput(a->dir);
  13813. + si_write_unlock(sb);
  13814. + au_nwt_done(&sbinfo->si_nowait);
  13815. + kfree(a);
  13816. +}
  13817. +
  13818. +/* ---------------------------------------------------------------------- */
  13819. +
  13820. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  13821. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  13822. +{
  13823. + int err, len;
  13824. + unsigned int flags[AuHnLast], f;
  13825. + unsigned char isdir, isroot, wh;
  13826. + struct inode *dir;
  13827. + struct au_hnotify_args *args;
  13828. + char *p, *h_child_name;
  13829. +
  13830. + err = 0;
  13831. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  13832. + dir = igrab(hnotify->hn_aufs_inode);
  13833. + if (!dir)
  13834. + goto out;
  13835. +
  13836. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  13837. + wh = 0;
  13838. + h_child_name = (void *)h_child_qstr->name;
  13839. + len = h_child_qstr->len;
  13840. + if (h_child_name) {
  13841. + if (len > AUFS_WH_PFX_LEN
  13842. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  13843. + h_child_name += AUFS_WH_PFX_LEN;
  13844. + len -= AUFS_WH_PFX_LEN;
  13845. + wh = 1;
  13846. + }
  13847. + }
  13848. +
  13849. + isdir = 0;
  13850. + if (h_child_inode)
  13851. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  13852. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  13853. + flags[AuHn_CHILD] = 0;
  13854. + if (isdir)
  13855. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  13856. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  13857. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  13858. + switch (mask & FS_EVENTS_POSS_ON_CHILD) {
  13859. + case FS_MOVED_FROM:
  13860. + case FS_MOVED_TO:
  13861. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  13862. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  13863. + /*FALLTHROUGH*/
  13864. + case FS_CREATE:
  13865. + AuDebugOn(!h_child_name);
  13866. + break;
  13867. +
  13868. + case FS_DELETE:
  13869. + /*
  13870. + * aufs never be able to get this child inode.
  13871. + * revalidation should be in d_revalidate()
  13872. + * by checking i_nlink, i_generation or d_unhashed().
  13873. + */
  13874. + AuDebugOn(!h_child_name);
  13875. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  13876. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  13877. + break;
  13878. +
  13879. + default:
  13880. + AuDebugOn(1);
  13881. + }
  13882. +
  13883. + if (wh)
  13884. + h_child_inode = NULL;
  13885. +
  13886. + err = -ENOMEM;
  13887. + /* iput() and kfree() will be called in au_hnotify() */
  13888. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  13889. + if (unlikely(!args)) {
  13890. + AuErr1("no memory\n");
  13891. + iput(dir);
  13892. + goto out;
  13893. + }
  13894. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  13895. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  13896. + args->mask = mask;
  13897. + args->dir = dir;
  13898. + args->h_dir = igrab(h_dir);
  13899. + if (h_child_inode)
  13900. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  13901. + args->h_child_inode = h_child_inode;
  13902. + args->h_child_nlen = len;
  13903. + if (len) {
  13904. + p = (void *)args;
  13905. + p += sizeof(*args);
  13906. + memcpy(p, h_child_name, len);
  13907. + p[len] = 0;
  13908. + }
  13909. +
  13910. + /* NFS fires the event for silly-renamed one from kworker */
  13911. + f = 0;
  13912. + if (!dir->i_nlink
  13913. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  13914. + f = AuWkq_NEST;
  13915. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  13916. + if (unlikely(err)) {
  13917. + pr_err("wkq %d\n", err);
  13918. + iput(args->h_child_inode);
  13919. + iput(args->h_dir);
  13920. + iput(args->dir);
  13921. + kfree(args);
  13922. + }
  13923. +
  13924. +out:
  13925. + return err;
  13926. +}
  13927. +
  13928. +/* ---------------------------------------------------------------------- */
  13929. +
  13930. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  13931. +{
  13932. + int err;
  13933. +
  13934. + AuDebugOn(!(udba & AuOptMask_UDBA));
  13935. +
  13936. + err = 0;
  13937. + if (au_hnotify_op.reset_br)
  13938. + err = au_hnotify_op.reset_br(udba, br, perm);
  13939. +
  13940. + return err;
  13941. +}
  13942. +
  13943. +int au_hnotify_init_br(struct au_branch *br, int perm)
  13944. +{
  13945. + int err;
  13946. +
  13947. + err = 0;
  13948. + if (au_hnotify_op.init_br)
  13949. + err = au_hnotify_op.init_br(br, perm);
  13950. +
  13951. + return err;
  13952. +}
  13953. +
  13954. +void au_hnotify_fin_br(struct au_branch *br)
  13955. +{
  13956. + if (au_hnotify_op.fin_br)
  13957. + au_hnotify_op.fin_br(br);
  13958. +}
  13959. +
  13960. +static void au_hn_destroy_cache(void)
  13961. +{
  13962. + kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
  13963. + au_cachep[AuCache_HNOTIFY] = NULL;
  13964. +}
  13965. +
  13966. +int __init au_hnotify_init(void)
  13967. +{
  13968. + int err;
  13969. +
  13970. + err = -ENOMEM;
  13971. + au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
  13972. + if (au_cachep[AuCache_HNOTIFY]) {
  13973. + err = 0;
  13974. + if (au_hnotify_op.init)
  13975. + err = au_hnotify_op.init();
  13976. + if (unlikely(err))
  13977. + au_hn_destroy_cache();
  13978. + }
  13979. + AuTraceErr(err);
  13980. + return err;
  13981. +}
  13982. +
  13983. +void au_hnotify_fin(void)
  13984. +{
  13985. + if (au_hnotify_op.fin)
  13986. + au_hnotify_op.fin();
  13987. + /* cf. au_cache_fin() */
  13988. + if (au_cachep[AuCache_HNOTIFY])
  13989. + au_hn_destroy_cache();
  13990. +}
  13991. diff -Nur linux-4.0.4.orig/fs/aufs/iinfo.c linux-4.0.4/fs/aufs/iinfo.c
  13992. --- linux-4.0.4.orig/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
  13993. +++ linux-4.0.4/fs/aufs/iinfo.c 2015-05-30 22:11:30.000000000 +0200
  13994. @@ -0,0 +1,264 @@
  13995. +/*
  13996. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13997. + */
  13998. +
  13999. +/*
  14000. + * inode private data
  14001. + */
  14002. +
  14003. +#include "aufs.h"
  14004. +
  14005. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  14006. +{
  14007. + struct inode *h_inode;
  14008. +
  14009. + IiMustAnyLock(inode);
  14010. +
  14011. + h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
  14012. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14013. + return h_inode;
  14014. +}
  14015. +
  14016. +/* todo: hard/soft set? */
  14017. +void au_hiput(struct au_hinode *hinode)
  14018. +{
  14019. + au_hn_free(hinode);
  14020. + dput(hinode->hi_whdentry);
  14021. + iput(hinode->hi_inode);
  14022. +}
  14023. +
  14024. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  14025. +{
  14026. + unsigned int flags;
  14027. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  14028. +
  14029. + flags = 0;
  14030. + if (au_opt_test(mnt_flags, XINO))
  14031. + au_fset_hi(flags, XINO);
  14032. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  14033. + au_fset_hi(flags, HNOTIFY);
  14034. + return flags;
  14035. +}
  14036. +
  14037. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  14038. + struct inode *h_inode, unsigned int flags)
  14039. +{
  14040. + struct au_hinode *hinode;
  14041. + struct inode *hi;
  14042. + struct au_iinfo *iinfo = au_ii(inode);
  14043. +
  14044. + IiMustWriteLock(inode);
  14045. +
  14046. + hinode = iinfo->ii_hinode + bindex;
  14047. + hi = hinode->hi_inode;
  14048. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14049. +
  14050. + if (hi)
  14051. + au_hiput(hinode);
  14052. + hinode->hi_inode = h_inode;
  14053. + if (h_inode) {
  14054. + int err;
  14055. + struct super_block *sb = inode->i_sb;
  14056. + struct au_branch *br;
  14057. +
  14058. + AuDebugOn(inode->i_mode
  14059. + && (h_inode->i_mode & S_IFMT)
  14060. + != (inode->i_mode & S_IFMT));
  14061. + if (bindex == iinfo->ii_bstart)
  14062. + au_cpup_igen(inode, h_inode);
  14063. + br = au_sbr(sb, bindex);
  14064. + hinode->hi_id = br->br_id;
  14065. + if (au_ftest_hi(flags, XINO)) {
  14066. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  14067. + inode->i_ino);
  14068. + if (unlikely(err))
  14069. + AuIOErr1("failed au_xino_write() %d\n", err);
  14070. + }
  14071. +
  14072. + if (au_ftest_hi(flags, HNOTIFY)
  14073. + && au_br_hnotifyable(br->br_perm)) {
  14074. + err = au_hn_alloc(hinode, inode);
  14075. + if (unlikely(err))
  14076. + AuIOErr1("au_hn_alloc() %d\n", err);
  14077. + }
  14078. + }
  14079. +}
  14080. +
  14081. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  14082. + struct dentry *h_wh)
  14083. +{
  14084. + struct au_hinode *hinode;
  14085. +
  14086. + IiMustWriteLock(inode);
  14087. +
  14088. + hinode = au_ii(inode)->ii_hinode + bindex;
  14089. + AuDebugOn(hinode->hi_whdentry);
  14090. + hinode->hi_whdentry = h_wh;
  14091. +}
  14092. +
  14093. +void au_update_iigen(struct inode *inode, int half)
  14094. +{
  14095. + struct au_iinfo *iinfo;
  14096. + struct au_iigen *iigen;
  14097. + unsigned int sigen;
  14098. +
  14099. + sigen = au_sigen(inode->i_sb);
  14100. + iinfo = au_ii(inode);
  14101. + iigen = &iinfo->ii_generation;
  14102. + spin_lock(&iinfo->ii_genspin);
  14103. + iigen->ig_generation = sigen;
  14104. + if (half)
  14105. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  14106. + else
  14107. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  14108. + spin_unlock(&iinfo->ii_genspin);
  14109. +}
  14110. +
  14111. +/* it may be called at remount time, too */
  14112. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  14113. +{
  14114. + struct au_iinfo *iinfo;
  14115. + aufs_bindex_t bindex, bend;
  14116. +
  14117. + iinfo = au_ii(inode);
  14118. + if (!iinfo)
  14119. + return;
  14120. +
  14121. + IiMustWriteLock(inode);
  14122. +
  14123. + if (do_put_zero && iinfo->ii_bstart >= 0) {
  14124. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
  14125. + bindex++) {
  14126. + struct inode *h_i;
  14127. +
  14128. + h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
  14129. + if (h_i
  14130. + && !h_i->i_nlink
  14131. + && !(h_i->i_state & I_LINKABLE))
  14132. + au_set_h_iptr(inode, bindex, NULL, 0);
  14133. + }
  14134. + }
  14135. +
  14136. + iinfo->ii_bstart = -1;
  14137. + iinfo->ii_bend = -1;
  14138. + bend = au_sbend(inode->i_sb);
  14139. + for (bindex = 0; bindex <= bend; bindex++)
  14140. + if (iinfo->ii_hinode[0 + bindex].hi_inode) {
  14141. + iinfo->ii_bstart = bindex;
  14142. + break;
  14143. + }
  14144. + if (iinfo->ii_bstart >= 0)
  14145. + for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
  14146. + if (iinfo->ii_hinode[0 + bindex].hi_inode) {
  14147. + iinfo->ii_bend = bindex;
  14148. + break;
  14149. + }
  14150. + AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
  14151. +}
  14152. +
  14153. +/* ---------------------------------------------------------------------- */
  14154. +
  14155. +void au_icntnr_init_once(void *_c)
  14156. +{
  14157. + struct au_icntnr *c = _c;
  14158. + struct au_iinfo *iinfo = &c->iinfo;
  14159. + static struct lock_class_key aufs_ii;
  14160. +
  14161. + spin_lock_init(&iinfo->ii_genspin);
  14162. + au_rw_init(&iinfo->ii_rwsem);
  14163. + au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
  14164. + inode_init_once(&c->vfs_inode);
  14165. +}
  14166. +
  14167. +int au_iinfo_init(struct inode *inode)
  14168. +{
  14169. + struct au_iinfo *iinfo;
  14170. + struct super_block *sb;
  14171. + int nbr, i;
  14172. +
  14173. + sb = inode->i_sb;
  14174. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  14175. + nbr = au_sbend(sb) + 1;
  14176. + if (unlikely(nbr <= 0))
  14177. + nbr = 1;
  14178. + iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  14179. + if (iinfo->ii_hinode) {
  14180. + au_ninodes_inc(sb);
  14181. + for (i = 0; i < nbr; i++)
  14182. + iinfo->ii_hinode[i].hi_id = -1;
  14183. +
  14184. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  14185. + iinfo->ii_bstart = -1;
  14186. + iinfo->ii_bend = -1;
  14187. + iinfo->ii_vdir = NULL;
  14188. + return 0;
  14189. + }
  14190. + return -ENOMEM;
  14191. +}
  14192. +
  14193. +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
  14194. +{
  14195. + int err, sz;
  14196. + struct au_hinode *hip;
  14197. +
  14198. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  14199. +
  14200. + err = -ENOMEM;
  14201. + sz = sizeof(*hip) * (iinfo->ii_bend + 1);
  14202. + if (!sz)
  14203. + sz = sizeof(*hip);
  14204. + hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
  14205. + if (hip) {
  14206. + iinfo->ii_hinode = hip;
  14207. + err = 0;
  14208. + }
  14209. +
  14210. + return err;
  14211. +}
  14212. +
  14213. +void au_iinfo_fin(struct inode *inode)
  14214. +{
  14215. + struct au_iinfo *iinfo;
  14216. + struct au_hinode *hi;
  14217. + struct super_block *sb;
  14218. + aufs_bindex_t bindex, bend;
  14219. + const unsigned char unlinked = !inode->i_nlink;
  14220. +
  14221. + iinfo = au_ii(inode);
  14222. + /* bad_inode case */
  14223. + if (!iinfo)
  14224. + return;
  14225. +
  14226. + sb = inode->i_sb;
  14227. + au_ninodes_dec(sb);
  14228. + if (si_pid_test(sb))
  14229. + au_xino_delete_inode(inode, unlinked);
  14230. + else {
  14231. + /*
  14232. + * it is safe to hide the dependency between sbinfo and
  14233. + * sb->s_umount.
  14234. + */
  14235. + lockdep_off();
  14236. + si_noflush_read_lock(sb);
  14237. + au_xino_delete_inode(inode, unlinked);
  14238. + si_read_unlock(sb);
  14239. + lockdep_on();
  14240. + }
  14241. +
  14242. + if (iinfo->ii_vdir)
  14243. + au_vdir_free(iinfo->ii_vdir);
  14244. +
  14245. + bindex = iinfo->ii_bstart;
  14246. + if (bindex >= 0) {
  14247. + hi = iinfo->ii_hinode + bindex;
  14248. + bend = iinfo->ii_bend;
  14249. + while (bindex++ <= bend) {
  14250. + if (hi->hi_inode)
  14251. + au_hiput(hi);
  14252. + hi++;
  14253. + }
  14254. + }
  14255. + kfree(iinfo->ii_hinode);
  14256. + iinfo->ii_hinode = NULL;
  14257. + AuRwDestroy(&iinfo->ii_rwsem);
  14258. +}
  14259. diff -Nur linux-4.0.4.orig/fs/aufs/inode.c linux-4.0.4/fs/aufs/inode.c
  14260. --- linux-4.0.4.orig/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
  14261. +++ linux-4.0.4/fs/aufs/inode.c 2015-05-30 22:11:30.000000000 +0200
  14262. @@ -0,0 +1,482 @@
  14263. +/*
  14264. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  14265. + */
  14266. +
  14267. +/*
  14268. + * inode functions
  14269. + */
  14270. +
  14271. +#include "aufs.h"
  14272. +
  14273. +struct inode *au_igrab(struct inode *inode)
  14274. +{
  14275. + if (inode) {
  14276. + AuDebugOn(!atomic_read(&inode->i_count));
  14277. + ihold(inode);
  14278. + }
  14279. + return inode;
  14280. +}
  14281. +
  14282. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  14283. +{
  14284. + au_cpup_attr_all(inode, /*force*/0);
  14285. + au_update_iigen(inode, /*half*/1);
  14286. + if (do_version)
  14287. + inode->i_version++;
  14288. +}
  14289. +
  14290. +static int au_ii_refresh(struct inode *inode, int *update)
  14291. +{
  14292. + int err, e;
  14293. + umode_t type;
  14294. + aufs_bindex_t bindex, new_bindex;
  14295. + struct super_block *sb;
  14296. + struct au_iinfo *iinfo;
  14297. + struct au_hinode *p, *q, tmp;
  14298. +
  14299. + IiMustWriteLock(inode);
  14300. +
  14301. + *update = 0;
  14302. + sb = inode->i_sb;
  14303. + type = inode->i_mode & S_IFMT;
  14304. + iinfo = au_ii(inode);
  14305. + err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
  14306. + if (unlikely(err))
  14307. + goto out;
  14308. +
  14309. + AuDebugOn(iinfo->ii_bstart < 0);
  14310. + p = iinfo->ii_hinode + iinfo->ii_bstart;
  14311. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
  14312. + bindex++, p++) {
  14313. + if (!p->hi_inode)
  14314. + continue;
  14315. +
  14316. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  14317. + new_bindex = au_br_index(sb, p->hi_id);
  14318. + if (new_bindex == bindex)
  14319. + continue;
  14320. +
  14321. + if (new_bindex < 0) {
  14322. + *update = 1;
  14323. + au_hiput(p);
  14324. + p->hi_inode = NULL;
  14325. + continue;
  14326. + }
  14327. +
  14328. + if (new_bindex < iinfo->ii_bstart)
  14329. + iinfo->ii_bstart = new_bindex;
  14330. + if (iinfo->ii_bend < new_bindex)
  14331. + iinfo->ii_bend = new_bindex;
  14332. + /* swap two lower inode, and loop again */
  14333. + q = iinfo->ii_hinode + new_bindex;
  14334. + tmp = *q;
  14335. + *q = *p;
  14336. + *p = tmp;
  14337. + if (tmp.hi_inode) {
  14338. + bindex--;
  14339. + p--;
  14340. + }
  14341. + }
  14342. + au_update_ibrange(inode, /*do_put_zero*/0);
  14343. + e = au_dy_irefresh(inode);
  14344. + if (unlikely(e && !err))
  14345. + err = e;
  14346. +
  14347. +out:
  14348. + AuTraceErr(err);
  14349. + return err;
  14350. +}
  14351. +
  14352. +int au_refresh_hinode_self(struct inode *inode)
  14353. +{
  14354. + int err, update;
  14355. +
  14356. + err = au_ii_refresh(inode, &update);
  14357. + if (!err)
  14358. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  14359. +
  14360. + AuTraceErr(err);
  14361. + return err;
  14362. +}
  14363. +
  14364. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  14365. +{
  14366. + int err, e, update;
  14367. + unsigned int flags;
  14368. + umode_t mode;
  14369. + aufs_bindex_t bindex, bend;
  14370. + unsigned char isdir;
  14371. + struct au_hinode *p;
  14372. + struct au_iinfo *iinfo;
  14373. +
  14374. + err = au_ii_refresh(inode, &update);
  14375. + if (unlikely(err))
  14376. + goto out;
  14377. +
  14378. + update = 0;
  14379. + iinfo = au_ii(inode);
  14380. + p = iinfo->ii_hinode + iinfo->ii_bstart;
  14381. + mode = (inode->i_mode & S_IFMT);
  14382. + isdir = S_ISDIR(mode);
  14383. + flags = au_hi_flags(inode, isdir);
  14384. + bend = au_dbend(dentry);
  14385. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
  14386. + struct inode *h_i;
  14387. + struct dentry *h_d;
  14388. +
  14389. + h_d = au_h_dptr(dentry, bindex);
  14390. + if (!h_d || !h_d->d_inode)
  14391. + continue;
  14392. +
  14393. + AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
  14394. + if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
  14395. + h_i = au_h_iptr(inode, bindex);
  14396. + if (h_i) {
  14397. + if (h_i == h_d->d_inode)
  14398. + continue;
  14399. + err = -EIO;
  14400. + break;
  14401. + }
  14402. + }
  14403. + if (bindex < iinfo->ii_bstart)
  14404. + iinfo->ii_bstart = bindex;
  14405. + if (iinfo->ii_bend < bindex)
  14406. + iinfo->ii_bend = bindex;
  14407. + au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
  14408. + update = 1;
  14409. + }
  14410. + au_update_ibrange(inode, /*do_put_zero*/0);
  14411. + e = au_dy_irefresh(inode);
  14412. + if (unlikely(e && !err))
  14413. + err = e;
  14414. + if (!err)
  14415. + au_refresh_hinode_attr(inode, update && isdir);
  14416. +
  14417. +out:
  14418. + AuTraceErr(err);
  14419. + return err;
  14420. +}
  14421. +
  14422. +static int set_inode(struct inode *inode, struct dentry *dentry)
  14423. +{
  14424. + int err;
  14425. + unsigned int flags;
  14426. + umode_t mode;
  14427. + aufs_bindex_t bindex, bstart, btail;
  14428. + unsigned char isdir;
  14429. + struct dentry *h_dentry;
  14430. + struct inode *h_inode;
  14431. + struct au_iinfo *iinfo;
  14432. +
  14433. + IiMustWriteLock(inode);
  14434. +
  14435. + err = 0;
  14436. + isdir = 0;
  14437. + bstart = au_dbstart(dentry);
  14438. + h_inode = au_h_dptr(dentry, bstart)->d_inode;
  14439. + mode = h_inode->i_mode;
  14440. + switch (mode & S_IFMT) {
  14441. + case S_IFREG:
  14442. + btail = au_dbtail(dentry);
  14443. + inode->i_op = &aufs_iop;
  14444. + inode->i_fop = &aufs_file_fop;
  14445. + err = au_dy_iaop(inode, bstart, h_inode);
  14446. + if (unlikely(err))
  14447. + goto out;
  14448. + break;
  14449. + case S_IFDIR:
  14450. + isdir = 1;
  14451. + btail = au_dbtaildir(dentry);
  14452. + inode->i_op = &aufs_dir_iop;
  14453. + inode->i_fop = &aufs_dir_fop;
  14454. + break;
  14455. + case S_IFLNK:
  14456. + btail = au_dbtail(dentry);
  14457. + inode->i_op = &aufs_symlink_iop;
  14458. + break;
  14459. + case S_IFBLK:
  14460. + case S_IFCHR:
  14461. + case S_IFIFO:
  14462. + case S_IFSOCK:
  14463. + btail = au_dbtail(dentry);
  14464. + inode->i_op = &aufs_iop;
  14465. + init_special_inode(inode, mode, h_inode->i_rdev);
  14466. + break;
  14467. + default:
  14468. + AuIOErr("Unknown file type 0%o\n", mode);
  14469. + err = -EIO;
  14470. + goto out;
  14471. + }
  14472. +
  14473. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  14474. + flags = au_hi_flags(inode, isdir);
  14475. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  14476. + && au_ftest_hi(flags, HNOTIFY)
  14477. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  14478. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  14479. + au_fclr_hi(flags, HNOTIFY);
  14480. + iinfo = au_ii(inode);
  14481. + iinfo->ii_bstart = bstart;
  14482. + iinfo->ii_bend = btail;
  14483. + for (bindex = bstart; bindex <= btail; bindex++) {
  14484. + h_dentry = au_h_dptr(dentry, bindex);
  14485. + if (h_dentry)
  14486. + au_set_h_iptr(inode, bindex,
  14487. + au_igrab(h_dentry->d_inode), flags);
  14488. + }
  14489. + au_cpup_attr_all(inode, /*force*/1);
  14490. + /*
  14491. + * to force calling aufs_get_acl() every time,
  14492. + * do not call cache_no_acl() for aufs inode.
  14493. + */
  14494. +
  14495. +out:
  14496. + return err;
  14497. +}
  14498. +
  14499. +/*
  14500. + * successful returns with iinfo write_locked
  14501. + * minus: errno
  14502. + * zero: success, matched
  14503. + * plus: no error, but unmatched
  14504. + */
  14505. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  14506. +{
  14507. + int err;
  14508. + unsigned int gen;
  14509. + struct au_iigen iigen;
  14510. + aufs_bindex_t bindex, bend;
  14511. + struct inode *h_inode, *h_dinode;
  14512. +
  14513. + /*
  14514. + * before this function, if aufs got any iinfo lock, it must be only
  14515. + * one, the parent dir.
  14516. + * it can happen by UDBA and the obsoleted inode number.
  14517. + */
  14518. + err = -EIO;
  14519. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  14520. + goto out;
  14521. +
  14522. + err = 1;
  14523. + ii_write_lock_new_child(inode);
  14524. + h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
  14525. + bend = au_ibend(inode);
  14526. + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
  14527. + h_inode = au_h_iptr(inode, bindex);
  14528. + if (!h_inode || h_inode != h_dinode)
  14529. + continue;
  14530. +
  14531. + err = 0;
  14532. + gen = au_iigen(inode, &iigen);
  14533. + if (gen == au_digen(dentry)
  14534. + && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
  14535. + break;
  14536. +
  14537. + /* fully refresh inode using dentry */
  14538. + err = au_refresh_hinode(inode, dentry);
  14539. + if (!err)
  14540. + au_update_iigen(inode, /*half*/0);
  14541. + break;
  14542. + }
  14543. +
  14544. + if (unlikely(err))
  14545. + ii_write_unlock(inode);
  14546. +out:
  14547. + return err;
  14548. +}
  14549. +
  14550. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  14551. + unsigned int d_type, ino_t *ino)
  14552. +{
  14553. + int err;
  14554. + struct mutex *mtx;
  14555. +
  14556. + /* prevent hardlinked inode number from race condition */
  14557. + mtx = NULL;
  14558. + if (d_type != DT_DIR) {
  14559. + mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
  14560. + mutex_lock(mtx);
  14561. + }
  14562. + err = au_xino_read(sb, bindex, h_ino, ino);
  14563. + if (unlikely(err))
  14564. + goto out;
  14565. +
  14566. + if (!*ino) {
  14567. + err = -EIO;
  14568. + *ino = au_xino_new_ino(sb);
  14569. + if (unlikely(!*ino))
  14570. + goto out;
  14571. + err = au_xino_write(sb, bindex, h_ino, *ino);
  14572. + if (unlikely(err))
  14573. + goto out;
  14574. + }
  14575. +
  14576. +out:
  14577. + if (mtx)
  14578. + mutex_unlock(mtx);
  14579. + return err;
  14580. +}
  14581. +
  14582. +/* successful returns with iinfo write_locked */
  14583. +/* todo: return with unlocked? */
  14584. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  14585. +{
  14586. + struct inode *inode;
  14587. + struct dentry *h_dentry;
  14588. + struct super_block *sb;
  14589. + struct mutex *mtx;
  14590. + ino_t h_ino, ino;
  14591. + int err;
  14592. + aufs_bindex_t bstart;
  14593. +
  14594. + sb = dentry->d_sb;
  14595. + bstart = au_dbstart(dentry);
  14596. + h_dentry = au_h_dptr(dentry, bstart);
  14597. + h_ino = h_dentry->d_inode->i_ino;
  14598. +
  14599. + /*
  14600. + * stop 'race'-ing between hardlinks under different
  14601. + * parents.
  14602. + */
  14603. + mtx = NULL;
  14604. + if (!d_is_dir(h_dentry))
  14605. + mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
  14606. +
  14607. +new_ino:
  14608. + if (mtx)
  14609. + mutex_lock(mtx);
  14610. + err = au_xino_read(sb, bstart, h_ino, &ino);
  14611. + inode = ERR_PTR(err);
  14612. + if (unlikely(err))
  14613. + goto out;
  14614. +
  14615. + if (!ino) {
  14616. + ino = au_xino_new_ino(sb);
  14617. + if (unlikely(!ino)) {
  14618. + inode = ERR_PTR(-EIO);
  14619. + goto out;
  14620. + }
  14621. + }
  14622. +
  14623. + AuDbg("i%lu\n", (unsigned long)ino);
  14624. + inode = au_iget_locked(sb, ino);
  14625. + err = PTR_ERR(inode);
  14626. + if (IS_ERR(inode))
  14627. + goto out;
  14628. +
  14629. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  14630. + if (inode->i_state & I_NEW) {
  14631. + /* verbose coding for lock class name */
  14632. + if (unlikely(d_is_symlink(h_dentry)))
  14633. + au_rw_class(&au_ii(inode)->ii_rwsem,
  14634. + au_lc_key + AuLcSymlink_IIINFO);
  14635. + else if (unlikely(d_is_dir(h_dentry)))
  14636. + au_rw_class(&au_ii(inode)->ii_rwsem,
  14637. + au_lc_key + AuLcDir_IIINFO);
  14638. + else /* likely */
  14639. + au_rw_class(&au_ii(inode)->ii_rwsem,
  14640. + au_lc_key + AuLcNonDir_IIINFO);
  14641. +
  14642. + ii_write_lock_new_child(inode);
  14643. + err = set_inode(inode, dentry);
  14644. + if (!err) {
  14645. + unlock_new_inode(inode);
  14646. + goto out; /* success */
  14647. + }
  14648. +
  14649. + /*
  14650. + * iget_failed() calls iput(), but we need to call
  14651. + * ii_write_unlock() after iget_failed(). so dirty hack for
  14652. + * i_count.
  14653. + */
  14654. + atomic_inc(&inode->i_count);
  14655. + iget_failed(inode);
  14656. + ii_write_unlock(inode);
  14657. + au_xino_write(sb, bstart, h_ino, /*ino*/0);
  14658. + /* ignore this error */
  14659. + goto out_iput;
  14660. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  14661. + /*
  14662. + * horrible race condition between lookup, readdir and copyup
  14663. + * (or something).
  14664. + */
  14665. + if (mtx)
  14666. + mutex_unlock(mtx);
  14667. + err = reval_inode(inode, dentry);
  14668. + if (unlikely(err < 0)) {
  14669. + mtx = NULL;
  14670. + goto out_iput;
  14671. + }
  14672. +
  14673. + if (!err) {
  14674. + mtx = NULL;
  14675. + goto out; /* success */
  14676. + } else if (mtx)
  14677. + mutex_lock(mtx);
  14678. + }
  14679. +
  14680. + if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
  14681. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  14682. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  14683. + bstart, au_sbtype(h_dentry->d_sb), dentry,
  14684. + (unsigned long)h_ino, (unsigned long)ino);
  14685. + ino = 0;
  14686. + err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
  14687. + if (!err) {
  14688. + iput(inode);
  14689. + if (mtx)
  14690. + mutex_unlock(mtx);
  14691. + goto new_ino;
  14692. + }
  14693. +
  14694. +out_iput:
  14695. + iput(inode);
  14696. + inode = ERR_PTR(err);
  14697. +out:
  14698. + if (mtx)
  14699. + mutex_unlock(mtx);
  14700. + return inode;
  14701. +}
  14702. +
  14703. +/* ---------------------------------------------------------------------- */
  14704. +
  14705. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  14706. + struct inode *inode)
  14707. +{
  14708. + int err;
  14709. + struct inode *hi;
  14710. +
  14711. + err = au_br_rdonly(au_sbr(sb, bindex));
  14712. +
  14713. + /* pseudo-link after flushed may happen out of bounds */
  14714. + if (!err
  14715. + && inode
  14716. + && au_ibstart(inode) <= bindex
  14717. + && bindex <= au_ibend(inode)) {
  14718. + /*
  14719. + * permission check is unnecessary since vfsub routine
  14720. + * will be called later
  14721. + */
  14722. + hi = au_h_iptr(inode, bindex);
  14723. + if (hi)
  14724. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  14725. + }
  14726. +
  14727. + return err;
  14728. +}
  14729. +
  14730. +int au_test_h_perm(struct inode *h_inode, int mask)
  14731. +{
  14732. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  14733. + return 0;
  14734. + return inode_permission(h_inode, mask);
  14735. +}
  14736. +
  14737. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  14738. +{
  14739. + if (au_test_nfs(h_inode->i_sb)
  14740. + && (mask & MAY_WRITE)
  14741. + && S_ISDIR(h_inode->i_mode))
  14742. + mask |= MAY_READ; /* force permission check */
  14743. + return au_test_h_perm(h_inode, mask);
  14744. +}
  14745. diff -Nur linux-4.0.4.orig/fs/aufs/inode.h linux-4.0.4/fs/aufs/inode.h
  14746. --- linux-4.0.4.orig/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
  14747. +++ linux-4.0.4/fs/aufs/inode.h 2015-05-30 22:11:31.000000000 +0200
  14748. @@ -0,0 +1,660 @@
  14749. +/*
  14750. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  14751. + */
  14752. +
  14753. +/*
  14754. + * inode operations
  14755. + */
  14756. +
  14757. +#ifndef __AUFS_INODE_H__
  14758. +#define __AUFS_INODE_H__
  14759. +
  14760. +#ifdef __KERNEL__
  14761. +
  14762. +#include <linux/fsnotify.h>
  14763. +#include "rwsem.h"
  14764. +
  14765. +struct vfsmount;
  14766. +
  14767. +struct au_hnotify {
  14768. +#ifdef CONFIG_AUFS_HNOTIFY
  14769. +#ifdef CONFIG_AUFS_HFSNOTIFY
  14770. + /* never use fsnotify_add_vfsmount_mark() */
  14771. + struct fsnotify_mark hn_mark;
  14772. +#endif
  14773. + struct inode *hn_aufs_inode; /* no get/put */
  14774. +#endif
  14775. +} ____cacheline_aligned_in_smp;
  14776. +
  14777. +struct au_hinode {
  14778. + struct inode *hi_inode;
  14779. + aufs_bindex_t hi_id;
  14780. +#ifdef CONFIG_AUFS_HNOTIFY
  14781. + struct au_hnotify *hi_notify;
  14782. +#endif
  14783. +
  14784. + /* reference to the copied-up whiteout with get/put */
  14785. + struct dentry *hi_whdentry;
  14786. +};
  14787. +
  14788. +/* ig_flags */
  14789. +#define AuIG_HALF_REFRESHED 1
  14790. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  14791. +#define au_ig_fset(flags, name) \
  14792. + do { (flags) |= AuIG_##name; } while (0)
  14793. +#define au_ig_fclr(flags, name) \
  14794. + do { (flags) &= ~AuIG_##name; } while (0)
  14795. +
  14796. +struct au_iigen {
  14797. + __u32 ig_generation, ig_flags;
  14798. +};
  14799. +
  14800. +struct au_vdir;
  14801. +struct au_iinfo {
  14802. + spinlock_t ii_genspin;
  14803. + struct au_iigen ii_generation;
  14804. + struct super_block *ii_hsb1; /* no get/put */
  14805. +
  14806. + struct au_rwsem ii_rwsem;
  14807. + aufs_bindex_t ii_bstart, ii_bend;
  14808. + __u32 ii_higen;
  14809. + struct au_hinode *ii_hinode;
  14810. + struct au_vdir *ii_vdir;
  14811. +};
  14812. +
  14813. +struct au_icntnr {
  14814. + struct au_iinfo iinfo;
  14815. + struct inode vfs_inode;
  14816. +} ____cacheline_aligned_in_smp;
  14817. +
  14818. +/* au_pin flags */
  14819. +#define AuPin_DI_LOCKED 1
  14820. +#define AuPin_MNT_WRITE (1 << 1)
  14821. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  14822. +#define au_fset_pin(flags, name) \
  14823. + do { (flags) |= AuPin_##name; } while (0)
  14824. +#define au_fclr_pin(flags, name) \
  14825. + do { (flags) &= ~AuPin_##name; } while (0)
  14826. +
  14827. +struct au_pin {
  14828. + /* input */
  14829. + struct dentry *dentry;
  14830. + unsigned int udba;
  14831. + unsigned char lsc_di, lsc_hi, flags;
  14832. + aufs_bindex_t bindex;
  14833. +
  14834. + /* output */
  14835. + struct dentry *parent;
  14836. + struct au_hinode *hdir;
  14837. + struct vfsmount *h_mnt;
  14838. +
  14839. + /* temporary unlock/relock for copyup */
  14840. + struct dentry *h_dentry, *h_parent;
  14841. + struct au_branch *br;
  14842. + struct task_struct *task;
  14843. +};
  14844. +
  14845. +void au_pin_hdir_unlock(struct au_pin *p);
  14846. +int au_pin_hdir_lock(struct au_pin *p);
  14847. +int au_pin_hdir_relock(struct au_pin *p);
  14848. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
  14849. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  14850. +void au_pin_hdir_release(struct au_pin *p);
  14851. +
  14852. +/* ---------------------------------------------------------------------- */
  14853. +
  14854. +static inline struct au_iinfo *au_ii(struct inode *inode)
  14855. +{
  14856. + struct au_iinfo *iinfo;
  14857. +
  14858. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  14859. + if (iinfo->ii_hinode)
  14860. + return iinfo;
  14861. + return NULL; /* debugging bad_inode case */
  14862. +}
  14863. +
  14864. +/* ---------------------------------------------------------------------- */
  14865. +
  14866. +/* inode.c */
  14867. +struct inode *au_igrab(struct inode *inode);
  14868. +int au_refresh_hinode_self(struct inode *inode);
  14869. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  14870. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  14871. + unsigned int d_type, ino_t *ino);
  14872. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  14873. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  14874. + struct inode *inode);
  14875. +int au_test_h_perm(struct inode *h_inode, int mask);
  14876. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  14877. +
  14878. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  14879. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  14880. +{
  14881. +#ifdef CONFIG_AUFS_SHWH
  14882. + return au_ino(sb, bindex, h_ino, d_type, ino);
  14883. +#else
  14884. + return 0;
  14885. +#endif
  14886. +}
  14887. +
  14888. +/* i_op.c */
  14889. +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
  14890. +
  14891. +/* au_wr_dir flags */
  14892. +#define AuWrDir_ADD_ENTRY 1
  14893. +#define AuWrDir_ISDIR (1 << 1)
  14894. +#define AuWrDir_TMPFILE (1 << 2)
  14895. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  14896. +#define au_fset_wrdir(flags, name) \
  14897. + do { (flags) |= AuWrDir_##name; } while (0)
  14898. +#define au_fclr_wrdir(flags, name) \
  14899. + do { (flags) &= ~AuWrDir_##name; } while (0)
  14900. +
  14901. +struct au_wr_dir_args {
  14902. + aufs_bindex_t force_btgt;
  14903. + unsigned char flags;
  14904. +};
  14905. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  14906. + struct au_wr_dir_args *args);
  14907. +
  14908. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  14909. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  14910. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  14911. + unsigned int udba, unsigned char flags);
  14912. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  14913. + unsigned int udba, unsigned char flags) __must_check;
  14914. +int au_do_pin(struct au_pin *pin) __must_check;
  14915. +void au_unpin(struct au_pin *pin);
  14916. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  14917. +
  14918. +#define AuIcpup_DID_CPUP 1
  14919. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  14920. +#define au_fset_icpup(flags, name) \
  14921. + do { (flags) |= AuIcpup_##name; } while (0)
  14922. +#define au_fclr_icpup(flags, name) \
  14923. + do { (flags) &= ~AuIcpup_##name; } while (0)
  14924. +
  14925. +struct au_icpup_args {
  14926. + unsigned char flags;
  14927. + unsigned char pin_flags;
  14928. + aufs_bindex_t btgt;
  14929. + unsigned int udba;
  14930. + struct au_pin pin;
  14931. + struct path h_path;
  14932. + struct inode *h_inode;
  14933. +};
  14934. +
  14935. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  14936. + struct au_icpup_args *a);
  14937. +
  14938. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
  14939. +
  14940. +/* i_op_add.c */
  14941. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  14942. + struct dentry *h_parent, int isdir);
  14943. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  14944. + dev_t dev);
  14945. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  14946. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  14947. + bool want_excl);
  14948. +struct vfsub_aopen_args;
  14949. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  14950. + struct vfsub_aopen_args *args);
  14951. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  14952. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  14953. + struct dentry *dentry);
  14954. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  14955. +
  14956. +/* i_op_del.c */
  14957. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  14958. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  14959. + struct dentry *h_parent, int isdir);
  14960. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  14961. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  14962. +
  14963. +/* i_op_ren.c */
  14964. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  14965. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  14966. + struct inode *dir, struct dentry *dentry);
  14967. +
  14968. +/* iinfo.c */
  14969. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  14970. +void au_hiput(struct au_hinode *hinode);
  14971. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  14972. + struct dentry *h_wh);
  14973. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  14974. +
  14975. +/* hinode flags */
  14976. +#define AuHi_XINO 1
  14977. +#define AuHi_HNOTIFY (1 << 1)
  14978. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  14979. +#define au_fset_hi(flags, name) \
  14980. + do { (flags) |= AuHi_##name; } while (0)
  14981. +#define au_fclr_hi(flags, name) \
  14982. + do { (flags) &= ~AuHi_##name; } while (0)
  14983. +
  14984. +#ifndef CONFIG_AUFS_HNOTIFY
  14985. +#undef AuHi_HNOTIFY
  14986. +#define AuHi_HNOTIFY 0
  14987. +#endif
  14988. +
  14989. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  14990. + struct inode *h_inode, unsigned int flags);
  14991. +
  14992. +void au_update_iigen(struct inode *inode, int half);
  14993. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  14994. +
  14995. +void au_icntnr_init_once(void *_c);
  14996. +int au_iinfo_init(struct inode *inode);
  14997. +void au_iinfo_fin(struct inode *inode);
  14998. +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
  14999. +
  15000. +#ifdef CONFIG_PROC_FS
  15001. +/* plink.c */
  15002. +int au_plink_maint(struct super_block *sb, int flags);
  15003. +struct au_sbinfo;
  15004. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  15005. +int au_plink_maint_enter(struct super_block *sb);
  15006. +#ifdef CONFIG_AUFS_DEBUG
  15007. +void au_plink_list(struct super_block *sb);
  15008. +#else
  15009. +AuStubVoid(au_plink_list, struct super_block *sb)
  15010. +#endif
  15011. +int au_plink_test(struct inode *inode);
  15012. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  15013. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  15014. + struct dentry *h_dentry);
  15015. +void au_plink_put(struct super_block *sb, int verbose);
  15016. +void au_plink_clean(struct super_block *sb, int verbose);
  15017. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  15018. +#else
  15019. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  15020. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  15021. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  15022. +AuStubVoid(au_plink_list, struct super_block *sb);
  15023. +AuStubInt0(au_plink_test, struct inode *inode);
  15024. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  15025. + struct inode *inode, aufs_bindex_t bindex);
  15026. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  15027. + struct dentry *h_dentry);
  15028. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  15029. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  15030. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  15031. +#endif /* CONFIG_PROC_FS */
  15032. +
  15033. +#ifdef CONFIG_AUFS_XATTR
  15034. +/* xattr.c */
  15035. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  15036. + unsigned int verbose);
  15037. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  15038. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  15039. + size_t size);
  15040. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  15041. + size_t size, int flags);
  15042. +int aufs_removexattr(struct dentry *dentry, const char *name);
  15043. +
  15044. +/* void au_xattr_init(struct super_block *sb); */
  15045. +#else
  15046. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  15047. + int ignore_flags, unsigned int verbose);
  15048. +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
  15049. +#endif
  15050. +
  15051. +#ifdef CONFIG_FS_POSIX_ACL
  15052. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  15053. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  15054. +#endif
  15055. +
  15056. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  15057. +enum {
  15058. + AU_XATTR_SET,
  15059. + AU_XATTR_REMOVE,
  15060. + AU_ACL_SET
  15061. +};
  15062. +
  15063. +struct au_srxattr {
  15064. + int type;
  15065. + union {
  15066. + struct {
  15067. + const char *name;
  15068. + const void *value;
  15069. + size_t size;
  15070. + int flags;
  15071. + } set;
  15072. + struct {
  15073. + const char *name;
  15074. + } remove;
  15075. + struct {
  15076. + struct posix_acl *acl;
  15077. + int type;
  15078. + } acl_set;
  15079. + } u;
  15080. +};
  15081. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
  15082. +#endif
  15083. +
  15084. +/* ---------------------------------------------------------------------- */
  15085. +
  15086. +/* lock subclass for iinfo */
  15087. +enum {
  15088. + AuLsc_II_CHILD, /* child first */
  15089. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  15090. + AuLsc_II_CHILD3, /* copyup dirs */
  15091. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  15092. + AuLsc_II_PARENT2,
  15093. + AuLsc_II_PARENT3, /* copyup dirs */
  15094. + AuLsc_II_NEW_CHILD
  15095. +};
  15096. +
  15097. +/*
  15098. + * ii_read_lock_child, ii_write_lock_child,
  15099. + * ii_read_lock_child2, ii_write_lock_child2,
  15100. + * ii_read_lock_child3, ii_write_lock_child3,
  15101. + * ii_read_lock_parent, ii_write_lock_parent,
  15102. + * ii_read_lock_parent2, ii_write_lock_parent2,
  15103. + * ii_read_lock_parent3, ii_write_lock_parent3,
  15104. + * ii_read_lock_new_child, ii_write_lock_new_child,
  15105. + */
  15106. +#define AuReadLockFunc(name, lsc) \
  15107. +static inline void ii_read_lock_##name(struct inode *i) \
  15108. +{ \
  15109. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15110. +}
  15111. +
  15112. +#define AuWriteLockFunc(name, lsc) \
  15113. +static inline void ii_write_lock_##name(struct inode *i) \
  15114. +{ \
  15115. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15116. +}
  15117. +
  15118. +#define AuRWLockFuncs(name, lsc) \
  15119. + AuReadLockFunc(name, lsc) \
  15120. + AuWriteLockFunc(name, lsc)
  15121. +
  15122. +AuRWLockFuncs(child, CHILD);
  15123. +AuRWLockFuncs(child2, CHILD2);
  15124. +AuRWLockFuncs(child3, CHILD3);
  15125. +AuRWLockFuncs(parent, PARENT);
  15126. +AuRWLockFuncs(parent2, PARENT2);
  15127. +AuRWLockFuncs(parent3, PARENT3);
  15128. +AuRWLockFuncs(new_child, NEW_CHILD);
  15129. +
  15130. +#undef AuReadLockFunc
  15131. +#undef AuWriteLockFunc
  15132. +#undef AuRWLockFuncs
  15133. +
  15134. +/*
  15135. + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
  15136. + */
  15137. +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
  15138. +
  15139. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  15140. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  15141. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  15142. +
  15143. +/* ---------------------------------------------------------------------- */
  15144. +
  15145. +static inline void au_icntnr_init(struct au_icntnr *c)
  15146. +{
  15147. +#ifdef CONFIG_AUFS_DEBUG
  15148. + c->vfs_inode.i_mode = 0;
  15149. +#endif
  15150. +}
  15151. +
  15152. +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
  15153. +{
  15154. + unsigned int gen;
  15155. + struct au_iinfo *iinfo;
  15156. +
  15157. + iinfo = au_ii(inode);
  15158. + spin_lock(&iinfo->ii_genspin);
  15159. + if (iigen)
  15160. + *iigen = iinfo->ii_generation;
  15161. + gen = iinfo->ii_generation.ig_generation;
  15162. + spin_unlock(&iinfo->ii_genspin);
  15163. +
  15164. + return gen;
  15165. +}
  15166. +
  15167. +/* tiny test for inode number */
  15168. +/* tmpfs generation is too rough */
  15169. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  15170. +{
  15171. + struct au_iinfo *iinfo;
  15172. +
  15173. + iinfo = au_ii(inode);
  15174. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  15175. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  15176. + && iinfo->ii_higen == h_inode->i_generation);
  15177. +}
  15178. +
  15179. +static inline void au_iigen_dec(struct inode *inode)
  15180. +{
  15181. + struct au_iinfo *iinfo;
  15182. +
  15183. + iinfo = au_ii(inode);
  15184. + spin_lock(&iinfo->ii_genspin);
  15185. + iinfo->ii_generation.ig_generation--;
  15186. + spin_unlock(&iinfo->ii_genspin);
  15187. +}
  15188. +
  15189. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  15190. +{
  15191. + int err;
  15192. +
  15193. + err = 0;
  15194. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  15195. + err = -EIO;
  15196. +
  15197. + return err;
  15198. +}
  15199. +
  15200. +/* ---------------------------------------------------------------------- */
  15201. +
  15202. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  15203. + aufs_bindex_t bindex)
  15204. +{
  15205. + IiMustAnyLock(inode);
  15206. + return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
  15207. +}
  15208. +
  15209. +static inline aufs_bindex_t au_ibstart(struct inode *inode)
  15210. +{
  15211. + IiMustAnyLock(inode);
  15212. + return au_ii(inode)->ii_bstart;
  15213. +}
  15214. +
  15215. +static inline aufs_bindex_t au_ibend(struct inode *inode)
  15216. +{
  15217. + IiMustAnyLock(inode);
  15218. + return au_ii(inode)->ii_bend;
  15219. +}
  15220. +
  15221. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  15222. +{
  15223. + IiMustAnyLock(inode);
  15224. + return au_ii(inode)->ii_vdir;
  15225. +}
  15226. +
  15227. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  15228. +{
  15229. + IiMustAnyLock(inode);
  15230. + return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
  15231. +}
  15232. +
  15233. +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
  15234. +{
  15235. + IiMustWriteLock(inode);
  15236. + au_ii(inode)->ii_bstart = bindex;
  15237. +}
  15238. +
  15239. +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
  15240. +{
  15241. + IiMustWriteLock(inode);
  15242. + au_ii(inode)->ii_bend = bindex;
  15243. +}
  15244. +
  15245. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  15246. +{
  15247. + IiMustWriteLock(inode);
  15248. + au_ii(inode)->ii_vdir = vdir;
  15249. +}
  15250. +
  15251. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  15252. +{
  15253. + IiMustAnyLock(inode);
  15254. + return au_ii(inode)->ii_hinode + bindex;
  15255. +}
  15256. +
  15257. +/* ---------------------------------------------------------------------- */
  15258. +
  15259. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  15260. +{
  15261. + if (pin)
  15262. + return pin->parent;
  15263. + return NULL;
  15264. +}
  15265. +
  15266. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  15267. +{
  15268. + if (pin && pin->hdir)
  15269. + return pin->hdir->hi_inode;
  15270. + return NULL;
  15271. +}
  15272. +
  15273. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  15274. +{
  15275. + if (pin)
  15276. + return pin->hdir;
  15277. + return NULL;
  15278. +}
  15279. +
  15280. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  15281. +{
  15282. + if (pin)
  15283. + pin->dentry = dentry;
  15284. +}
  15285. +
  15286. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  15287. + unsigned char lflag)
  15288. +{
  15289. + if (pin) {
  15290. + if (lflag)
  15291. + au_fset_pin(pin->flags, DI_LOCKED);
  15292. + else
  15293. + au_fclr_pin(pin->flags, DI_LOCKED);
  15294. + }
  15295. +}
  15296. +
  15297. +#if 0 /* reserved */
  15298. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  15299. +{
  15300. + if (pin) {
  15301. + dput(pin->parent);
  15302. + pin->parent = dget(parent);
  15303. + }
  15304. +}
  15305. +#endif
  15306. +
  15307. +/* ---------------------------------------------------------------------- */
  15308. +
  15309. +struct au_branch;
  15310. +#ifdef CONFIG_AUFS_HNOTIFY
  15311. +struct au_hnotify_op {
  15312. + void (*ctl)(struct au_hinode *hinode, int do_set);
  15313. + int (*alloc)(struct au_hinode *hinode);
  15314. +
  15315. + /*
  15316. + * if it returns true, the the caller should free hinode->hi_notify,
  15317. + * otherwise ->free() frees it.
  15318. + */
  15319. + int (*free)(struct au_hinode *hinode,
  15320. + struct au_hnotify *hn) __must_check;
  15321. +
  15322. + void (*fin)(void);
  15323. + int (*init)(void);
  15324. +
  15325. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  15326. + void (*fin_br)(struct au_branch *br);
  15327. + int (*init_br)(struct au_branch *br, int perm);
  15328. +};
  15329. +
  15330. +/* hnotify.c */
  15331. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  15332. +void au_hn_free(struct au_hinode *hinode);
  15333. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  15334. +void au_hn_reset(struct inode *inode, unsigned int flags);
  15335. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  15336. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  15337. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  15338. +int au_hnotify_init_br(struct au_branch *br, int perm);
  15339. +void au_hnotify_fin_br(struct au_branch *br);
  15340. +int __init au_hnotify_init(void);
  15341. +void au_hnotify_fin(void);
  15342. +
  15343. +/* hfsnotify.c */
  15344. +extern const struct au_hnotify_op au_hnotify_op;
  15345. +
  15346. +static inline
  15347. +void au_hn_init(struct au_hinode *hinode)
  15348. +{
  15349. + hinode->hi_notify = NULL;
  15350. +}
  15351. +
  15352. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  15353. +{
  15354. + return hinode->hi_notify;
  15355. +}
  15356. +
  15357. +#else
  15358. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  15359. + struct au_hinode *hinode __maybe_unused,
  15360. + struct inode *inode __maybe_unused)
  15361. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  15362. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  15363. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  15364. + int do_set __maybe_unused)
  15365. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  15366. + unsigned int flags __maybe_unused)
  15367. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  15368. + struct au_branch *br __maybe_unused,
  15369. + int perm __maybe_unused)
  15370. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  15371. + int perm __maybe_unused)
  15372. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  15373. +AuStubInt0(__init au_hnotify_init, void)
  15374. +AuStubVoid(au_hnotify_fin, void)
  15375. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  15376. +#endif /* CONFIG_AUFS_HNOTIFY */
  15377. +
  15378. +static inline void au_hn_suspend(struct au_hinode *hdir)
  15379. +{
  15380. + au_hn_ctl(hdir, /*do_set*/0);
  15381. +}
  15382. +
  15383. +static inline void au_hn_resume(struct au_hinode *hdir)
  15384. +{
  15385. + au_hn_ctl(hdir, /*do_set*/1);
  15386. +}
  15387. +
  15388. +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
  15389. +{
  15390. + mutex_lock(&hdir->hi_inode->i_mutex);
  15391. + au_hn_suspend(hdir);
  15392. +}
  15393. +
  15394. +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
  15395. + unsigned int sc __maybe_unused)
  15396. +{
  15397. + mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
  15398. + au_hn_suspend(hdir);
  15399. +}
  15400. +
  15401. +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
  15402. +{
  15403. + au_hn_resume(hdir);
  15404. + mutex_unlock(&hdir->hi_inode->i_mutex);
  15405. +}
  15406. +
  15407. +#endif /* __KERNEL__ */
  15408. +#endif /* __AUFS_INODE_H__ */
  15409. diff -Nur linux-4.0.4.orig/fs/aufs/ioctl.c linux-4.0.4/fs/aufs/ioctl.c
  15410. --- linux-4.0.4.orig/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
  15411. +++ linux-4.0.4/fs/aufs/ioctl.c 2015-05-30 22:11:30.000000000 +0200
  15412. @@ -0,0 +1,206 @@
  15413. +/*
  15414. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  15415. + */
  15416. +
  15417. +/*
  15418. + * ioctl
  15419. + * plink-management and readdir in userspace.
  15420. + * assist the pathconf(3) wrapper library.
  15421. + * move-down
  15422. + * File-based Hierarchical Storage Management.
  15423. + */
  15424. +
  15425. +#include <linux/compat.h>
  15426. +#include <linux/file.h>
  15427. +#include "aufs.h"
  15428. +
  15429. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  15430. +{
  15431. + int err, fd;
  15432. + aufs_bindex_t wbi, bindex, bend;
  15433. + struct file *h_file;
  15434. + struct super_block *sb;
  15435. + struct dentry *root;
  15436. + struct au_branch *br;
  15437. + struct aufs_wbr_fd wbrfd = {
  15438. + .oflags = au_dir_roflags,
  15439. + .brid = -1
  15440. + };
  15441. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  15442. + | O_NOATIME | O_CLOEXEC;
  15443. +
  15444. + AuDebugOn(wbrfd.oflags & ~valid);
  15445. +
  15446. + if (arg) {
  15447. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  15448. + if (unlikely(err)) {
  15449. + err = -EFAULT;
  15450. + goto out;
  15451. + }
  15452. +
  15453. + err = -EINVAL;
  15454. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  15455. + wbrfd.oflags |= au_dir_roflags;
  15456. + AuDbg("0%o\n", wbrfd.oflags);
  15457. + if (unlikely(wbrfd.oflags & ~valid))
  15458. + goto out;
  15459. + }
  15460. +
  15461. + fd = get_unused_fd_flags(0);
  15462. + err = fd;
  15463. + if (unlikely(fd < 0))
  15464. + goto out;
  15465. +
  15466. + h_file = ERR_PTR(-EINVAL);
  15467. + wbi = 0;
  15468. + br = NULL;
  15469. + sb = path->dentry->d_sb;
  15470. + root = sb->s_root;
  15471. + aufs_read_lock(root, AuLock_IR);
  15472. + bend = au_sbend(sb);
  15473. + if (wbrfd.brid >= 0) {
  15474. + wbi = au_br_index(sb, wbrfd.brid);
  15475. + if (unlikely(wbi < 0 || wbi > bend))
  15476. + goto out_unlock;
  15477. + }
  15478. +
  15479. + h_file = ERR_PTR(-ENOENT);
  15480. + br = au_sbr(sb, wbi);
  15481. + if (!au_br_writable(br->br_perm)) {
  15482. + if (arg)
  15483. + goto out_unlock;
  15484. +
  15485. + bindex = wbi + 1;
  15486. + wbi = -1;
  15487. + for (; bindex <= bend; bindex++) {
  15488. + br = au_sbr(sb, bindex);
  15489. + if (au_br_writable(br->br_perm)) {
  15490. + wbi = bindex;
  15491. + br = au_sbr(sb, wbi);
  15492. + break;
  15493. + }
  15494. + }
  15495. + }
  15496. + AuDbg("wbi %d\n", wbi);
  15497. + if (wbi >= 0)
  15498. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  15499. + /*force_wr*/0);
  15500. +
  15501. +out_unlock:
  15502. + aufs_read_unlock(root, AuLock_IR);
  15503. + err = PTR_ERR(h_file);
  15504. + if (IS_ERR(h_file))
  15505. + goto out_fd;
  15506. +
  15507. + atomic_dec(&br->br_count); /* cf. au_h_open() */
  15508. + fd_install(fd, h_file);
  15509. + err = fd;
  15510. + goto out; /* success */
  15511. +
  15512. +out_fd:
  15513. + put_unused_fd(fd);
  15514. +out:
  15515. + AuTraceErr(err);
  15516. + return err;
  15517. +}
  15518. +
  15519. +/* ---------------------------------------------------------------------- */
  15520. +
  15521. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  15522. +{
  15523. + long err;
  15524. + struct dentry *dentry;
  15525. +
  15526. + switch (cmd) {
  15527. + case AUFS_CTL_RDU:
  15528. + case AUFS_CTL_RDU_INO:
  15529. + err = au_rdu_ioctl(file, cmd, arg);
  15530. + break;
  15531. +
  15532. + case AUFS_CTL_WBR_FD:
  15533. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  15534. + break;
  15535. +
  15536. + case AUFS_CTL_IBUSY:
  15537. + err = au_ibusy_ioctl(file, arg);
  15538. + break;
  15539. +
  15540. + case AUFS_CTL_BRINFO:
  15541. + err = au_brinfo_ioctl(file, arg);
  15542. + break;
  15543. +
  15544. + case AUFS_CTL_FHSM_FD:
  15545. + dentry = file->f_path.dentry;
  15546. + if (IS_ROOT(dentry))
  15547. + err = au_fhsm_fd(dentry->d_sb, arg);
  15548. + else
  15549. + err = -ENOTTY;
  15550. + break;
  15551. +
  15552. + default:
  15553. + /* do not call the lower */
  15554. + AuDbg("0x%x\n", cmd);
  15555. + err = -ENOTTY;
  15556. + }
  15557. +
  15558. + AuTraceErr(err);
  15559. + return err;
  15560. +}
  15561. +
  15562. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  15563. +{
  15564. + long err;
  15565. +
  15566. + switch (cmd) {
  15567. + case AUFS_CTL_MVDOWN:
  15568. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  15569. + break;
  15570. +
  15571. + case AUFS_CTL_WBR_FD:
  15572. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  15573. + break;
  15574. +
  15575. + default:
  15576. + /* do not call the lower */
  15577. + AuDbg("0x%x\n", cmd);
  15578. + err = -ENOTTY;
  15579. + }
  15580. +
  15581. + AuTraceErr(err);
  15582. + return err;
  15583. +}
  15584. +
  15585. +#ifdef CONFIG_COMPAT
  15586. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  15587. + unsigned long arg)
  15588. +{
  15589. + long err;
  15590. +
  15591. + switch (cmd) {
  15592. + case AUFS_CTL_RDU:
  15593. + case AUFS_CTL_RDU_INO:
  15594. + err = au_rdu_compat_ioctl(file, cmd, arg);
  15595. + break;
  15596. +
  15597. + case AUFS_CTL_IBUSY:
  15598. + err = au_ibusy_compat_ioctl(file, arg);
  15599. + break;
  15600. +
  15601. + case AUFS_CTL_BRINFO:
  15602. + err = au_brinfo_compat_ioctl(file, arg);
  15603. + break;
  15604. +
  15605. + default:
  15606. + err = aufs_ioctl_dir(file, cmd, arg);
  15607. + }
  15608. +
  15609. + AuTraceErr(err);
  15610. + return err;
  15611. +}
  15612. +
  15613. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  15614. + unsigned long arg)
  15615. +{
  15616. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  15617. +}
  15618. +#endif
  15619. diff -Nur linux-4.0.4.orig/fs/aufs/i_op_add.c linux-4.0.4/fs/aufs/i_op_add.c
  15620. --- linux-4.0.4.orig/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
  15621. +++ linux-4.0.4/fs/aufs/i_op_add.c 2015-05-30 22:11:31.000000000 +0200
  15622. @@ -0,0 +1,917 @@
  15623. +/*
  15624. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  15625. + */
  15626. +
  15627. +/*
  15628. + * inode operations (add entry)
  15629. + */
  15630. +
  15631. +#include "aufs.h"
  15632. +
  15633. +/*
  15634. + * final procedure of adding a new entry, except link(2).
  15635. + * remove whiteout, instantiate, copyup the parent dir's times and size
  15636. + * and update version.
  15637. + * if it failed, re-create the removed whiteout.
  15638. + */
  15639. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  15640. + struct dentry *wh_dentry, struct dentry *dentry)
  15641. +{
  15642. + int err, rerr;
  15643. + aufs_bindex_t bwh;
  15644. + struct path h_path;
  15645. + struct super_block *sb;
  15646. + struct inode *inode, *h_dir;
  15647. + struct dentry *wh;
  15648. +
  15649. + bwh = -1;
  15650. + sb = dir->i_sb;
  15651. + if (wh_dentry) {
  15652. + h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
  15653. + IMustLock(h_dir);
  15654. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  15655. + bwh = au_dbwh(dentry);
  15656. + h_path.dentry = wh_dentry;
  15657. + h_path.mnt = au_sbr_mnt(sb, bindex);
  15658. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  15659. + dentry);
  15660. + if (unlikely(err))
  15661. + goto out;
  15662. + }
  15663. +
  15664. + inode = au_new_inode(dentry, /*must_new*/1);
  15665. + if (!IS_ERR(inode)) {
  15666. + d_instantiate(dentry, inode);
  15667. + dir = dentry->d_parent->d_inode; /* dir inode is locked */
  15668. + IMustLock(dir);
  15669. + au_dir_ts(dir, bindex);
  15670. + dir->i_version++;
  15671. + au_fhsm_wrote(sb, bindex, /*force*/0);
  15672. + return 0; /* success */
  15673. + }
  15674. +
  15675. + err = PTR_ERR(inode);
  15676. + if (!wh_dentry)
  15677. + goto out;
  15678. +
  15679. + /* revert */
  15680. + /* dir inode is locked */
  15681. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  15682. + rerr = PTR_ERR(wh);
  15683. + if (IS_ERR(wh)) {
  15684. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  15685. + dentry, err, rerr);
  15686. + err = -EIO;
  15687. + } else
  15688. + dput(wh);
  15689. +
  15690. +out:
  15691. + return err;
  15692. +}
  15693. +
  15694. +static int au_d_may_add(struct dentry *dentry)
  15695. +{
  15696. + int err;
  15697. +
  15698. + err = 0;
  15699. + if (unlikely(d_unhashed(dentry)))
  15700. + err = -ENOENT;
  15701. + if (unlikely(dentry->d_inode))
  15702. + err = -EEXIST;
  15703. + return err;
  15704. +}
  15705. +
  15706. +/*
  15707. + * simple tests for the adding inode operations.
  15708. + * following the checks in vfs, plus the parent-child relationship.
  15709. + */
  15710. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  15711. + struct dentry *h_parent, int isdir)
  15712. +{
  15713. + int err;
  15714. + umode_t h_mode;
  15715. + struct dentry *h_dentry;
  15716. + struct inode *h_inode;
  15717. +
  15718. + err = -ENAMETOOLONG;
  15719. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  15720. + goto out;
  15721. +
  15722. + h_dentry = au_h_dptr(dentry, bindex);
  15723. + h_inode = h_dentry->d_inode;
  15724. + if (!dentry->d_inode) {
  15725. + err = -EEXIST;
  15726. + if (unlikely(h_inode))
  15727. + goto out;
  15728. + } else {
  15729. + /* rename(2) case */
  15730. + err = -EIO;
  15731. + if (unlikely(!h_inode || !h_inode->i_nlink))
  15732. + goto out;
  15733. +
  15734. + h_mode = h_inode->i_mode;
  15735. + if (!isdir) {
  15736. + err = -EISDIR;
  15737. + if (unlikely(S_ISDIR(h_mode)))
  15738. + goto out;
  15739. + } else if (unlikely(!S_ISDIR(h_mode))) {
  15740. + err = -ENOTDIR;
  15741. + goto out;
  15742. + }
  15743. + }
  15744. +
  15745. + err = 0;
  15746. + /* expected parent dir is locked */
  15747. + if (unlikely(h_parent != h_dentry->d_parent))
  15748. + err = -EIO;
  15749. +
  15750. +out:
  15751. + AuTraceErr(err);
  15752. + return err;
  15753. +}
  15754. +
  15755. +/*
  15756. + * initial procedure of adding a new entry.
  15757. + * prepare writable branch and the parent dir, lock it,
  15758. + * and lookup whiteout for the new entry.
  15759. + */
  15760. +static struct dentry*
  15761. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  15762. + struct dentry *src_dentry, struct au_pin *pin,
  15763. + struct au_wr_dir_args *wr_dir_args)
  15764. +{
  15765. + struct dentry *wh_dentry, *h_parent;
  15766. + struct super_block *sb;
  15767. + struct au_branch *br;
  15768. + int err;
  15769. + unsigned int udba;
  15770. + aufs_bindex_t bcpup;
  15771. +
  15772. + AuDbg("%pd\n", dentry);
  15773. +
  15774. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  15775. + bcpup = err;
  15776. + wh_dentry = ERR_PTR(err);
  15777. + if (unlikely(err < 0))
  15778. + goto out;
  15779. +
  15780. + sb = dentry->d_sb;
  15781. + udba = au_opt_udba(sb);
  15782. + err = au_pin(pin, dentry, bcpup, udba,
  15783. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  15784. + wh_dentry = ERR_PTR(err);
  15785. + if (unlikely(err))
  15786. + goto out;
  15787. +
  15788. + h_parent = au_pinned_h_parent(pin);
  15789. + if (udba != AuOpt_UDBA_NONE
  15790. + && au_dbstart(dentry) == bcpup)
  15791. + err = au_may_add(dentry, bcpup, h_parent,
  15792. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  15793. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  15794. + err = -ENAMETOOLONG;
  15795. + wh_dentry = ERR_PTR(err);
  15796. + if (unlikely(err))
  15797. + goto out_unpin;
  15798. +
  15799. + br = au_sbr(sb, bcpup);
  15800. + if (dt) {
  15801. + struct path tmp = {
  15802. + .dentry = h_parent,
  15803. + .mnt = au_br_mnt(br)
  15804. + };
  15805. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  15806. + }
  15807. +
  15808. + wh_dentry = NULL;
  15809. + if (bcpup != au_dbwh(dentry))
  15810. + goto out; /* success */
  15811. +
  15812. + /*
  15813. + * ENAMETOOLONG here means that if we allowed create such name, then it
  15814. + * would not be able to removed in the future. So we don't allow such
  15815. + * name here and we don't handle ENAMETOOLONG differently here.
  15816. + */
  15817. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  15818. +
  15819. +out_unpin:
  15820. + if (IS_ERR(wh_dentry))
  15821. + au_unpin(pin);
  15822. +out:
  15823. + return wh_dentry;
  15824. +}
  15825. +
  15826. +/* ---------------------------------------------------------------------- */
  15827. +
  15828. +enum { Mknod, Symlink, Creat };
  15829. +struct simple_arg {
  15830. + int type;
  15831. + union {
  15832. + struct {
  15833. + umode_t mode;
  15834. + bool want_excl;
  15835. + bool try_aopen;
  15836. + struct vfsub_aopen_args *aopen;
  15837. + } c;
  15838. + struct {
  15839. + const char *symname;
  15840. + } s;
  15841. + struct {
  15842. + umode_t mode;
  15843. + dev_t dev;
  15844. + } m;
  15845. + } u;
  15846. +};
  15847. +
  15848. +static int add_simple(struct inode *dir, struct dentry *dentry,
  15849. + struct simple_arg *arg)
  15850. +{
  15851. + int err, rerr;
  15852. + aufs_bindex_t bstart;
  15853. + unsigned char created;
  15854. + const unsigned char try_aopen
  15855. + = (arg->type == Creat && arg->u.c.try_aopen);
  15856. + struct dentry *wh_dentry, *parent;
  15857. + struct inode *h_dir;
  15858. + struct super_block *sb;
  15859. + struct au_branch *br;
  15860. + /* to reuduce stack size */
  15861. + struct {
  15862. + struct au_dtime dt;
  15863. + struct au_pin pin;
  15864. + struct path h_path;
  15865. + struct au_wr_dir_args wr_dir_args;
  15866. + } *a;
  15867. +
  15868. + AuDbg("%pd\n", dentry);
  15869. + IMustLock(dir);
  15870. +
  15871. + err = -ENOMEM;
  15872. + a = kmalloc(sizeof(*a), GFP_NOFS);
  15873. + if (unlikely(!a))
  15874. + goto out;
  15875. + a->wr_dir_args.force_btgt = -1;
  15876. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  15877. +
  15878. + parent = dentry->d_parent; /* dir inode is locked */
  15879. + if (!try_aopen) {
  15880. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  15881. + if (unlikely(err))
  15882. + goto out_free;
  15883. + }
  15884. + err = au_d_may_add(dentry);
  15885. + if (unlikely(err))
  15886. + goto out_unlock;
  15887. + if (!try_aopen)
  15888. + di_write_lock_parent(parent);
  15889. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  15890. + &a->pin, &a->wr_dir_args);
  15891. + err = PTR_ERR(wh_dentry);
  15892. + if (IS_ERR(wh_dentry))
  15893. + goto out_parent;
  15894. +
  15895. + bstart = au_dbstart(dentry);
  15896. + sb = dentry->d_sb;
  15897. + br = au_sbr(sb, bstart);
  15898. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  15899. + a->h_path.mnt = au_br_mnt(br);
  15900. + h_dir = au_pinned_h_dir(&a->pin);
  15901. + switch (arg->type) {
  15902. + case Creat:
  15903. + err = 0;
  15904. + if (!try_aopen || !h_dir->i_op->atomic_open)
  15905. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  15906. + arg->u.c.want_excl);
  15907. + else
  15908. + err = vfsub_atomic_open(h_dir, a->h_path.dentry,
  15909. + arg->u.c.aopen, br);
  15910. + break;
  15911. + case Symlink:
  15912. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  15913. + break;
  15914. + case Mknod:
  15915. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  15916. + arg->u.m.dev);
  15917. + break;
  15918. + default:
  15919. + BUG();
  15920. + }
  15921. + created = !err;
  15922. + if (!err)
  15923. + err = epilog(dir, bstart, wh_dentry, dentry);
  15924. +
  15925. + /* revert */
  15926. + if (unlikely(created && err && a->h_path.dentry->d_inode)) {
  15927. + /* no delegation since it is just created */
  15928. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  15929. + /*force*/0);
  15930. + if (rerr) {
  15931. + AuIOErr("%pd revert failure(%d, %d)\n",
  15932. + dentry, err, rerr);
  15933. + err = -EIO;
  15934. + }
  15935. + au_dtime_revert(&a->dt);
  15936. + }
  15937. +
  15938. + if (!err && try_aopen && !h_dir->i_op->atomic_open)
  15939. + *arg->u.c.aopen->opened |= FILE_CREATED;
  15940. +
  15941. + au_unpin(&a->pin);
  15942. + dput(wh_dentry);
  15943. +
  15944. +out_parent:
  15945. + if (!try_aopen)
  15946. + di_write_unlock(parent);
  15947. +out_unlock:
  15948. + if (unlikely(err)) {
  15949. + au_update_dbstart(dentry);
  15950. + d_drop(dentry);
  15951. + }
  15952. + if (!try_aopen)
  15953. + aufs_read_unlock(dentry, AuLock_DW);
  15954. +out_free:
  15955. + kfree(a);
  15956. +out:
  15957. + return err;
  15958. +}
  15959. +
  15960. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  15961. + dev_t dev)
  15962. +{
  15963. + struct simple_arg arg = {
  15964. + .type = Mknod,
  15965. + .u.m = {
  15966. + .mode = mode,
  15967. + .dev = dev
  15968. + }
  15969. + };
  15970. + return add_simple(dir, dentry, &arg);
  15971. +}
  15972. +
  15973. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  15974. +{
  15975. + struct simple_arg arg = {
  15976. + .type = Symlink,
  15977. + .u.s.symname = symname
  15978. + };
  15979. + return add_simple(dir, dentry, &arg);
  15980. +}
  15981. +
  15982. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  15983. + bool want_excl)
  15984. +{
  15985. + struct simple_arg arg = {
  15986. + .type = Creat,
  15987. + .u.c = {
  15988. + .mode = mode,
  15989. + .want_excl = want_excl
  15990. + }
  15991. + };
  15992. + return add_simple(dir, dentry, &arg);
  15993. +}
  15994. +
  15995. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  15996. + struct vfsub_aopen_args *aopen_args)
  15997. +{
  15998. + struct simple_arg arg = {
  15999. + .type = Creat,
  16000. + .u.c = {
  16001. + .mode = aopen_args->create_mode,
  16002. + .want_excl = aopen_args->open_flag & O_EXCL,
  16003. + .try_aopen = true,
  16004. + .aopen = aopen_args
  16005. + }
  16006. + };
  16007. + return add_simple(dir, dentry, &arg);
  16008. +}
  16009. +
  16010. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  16011. +{
  16012. + int err;
  16013. + aufs_bindex_t bindex;
  16014. + struct super_block *sb;
  16015. + struct dentry *parent, *h_parent, *h_dentry;
  16016. + struct inode *h_dir, *inode;
  16017. + struct vfsmount *h_mnt;
  16018. + struct au_wr_dir_args wr_dir_args = {
  16019. + .force_btgt = -1,
  16020. + .flags = AuWrDir_TMPFILE
  16021. + };
  16022. +
  16023. + /* copy-up may happen */
  16024. + mutex_lock(&dir->i_mutex);
  16025. +
  16026. + sb = dir->i_sb;
  16027. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16028. + if (unlikely(err))
  16029. + goto out;
  16030. +
  16031. + err = au_di_init(dentry);
  16032. + if (unlikely(err))
  16033. + goto out_si;
  16034. +
  16035. + err = -EBUSY;
  16036. + parent = d_find_any_alias(dir);
  16037. + AuDebugOn(!parent);
  16038. + di_write_lock_parent(parent);
  16039. + if (unlikely(parent->d_inode != dir))
  16040. + goto out_parent;
  16041. +
  16042. + err = au_digen_test(parent, au_sigen(sb));
  16043. + if (unlikely(err))
  16044. + goto out_parent;
  16045. +
  16046. + bindex = au_dbstart(parent);
  16047. + au_set_dbstart(dentry, bindex);
  16048. + au_set_dbend(dentry, bindex);
  16049. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  16050. + bindex = err;
  16051. + if (unlikely(err < 0))
  16052. + goto out_parent;
  16053. +
  16054. + err = -EOPNOTSUPP;
  16055. + h_dir = au_h_iptr(dir, bindex);
  16056. + if (unlikely(!h_dir->i_op->tmpfile))
  16057. + goto out_parent;
  16058. +
  16059. + h_mnt = au_sbr_mnt(sb, bindex);
  16060. + err = vfsub_mnt_want_write(h_mnt);
  16061. + if (unlikely(err))
  16062. + goto out_parent;
  16063. +
  16064. + h_parent = au_h_dptr(parent, bindex);
  16065. + err = inode_permission(h_parent->d_inode, MAY_WRITE | MAY_EXEC);
  16066. + if (unlikely(err))
  16067. + goto out_mnt;
  16068. +
  16069. + err = -ENOMEM;
  16070. + h_dentry = d_alloc(h_parent, &dentry->d_name);
  16071. + if (unlikely(!h_dentry))
  16072. + goto out_mnt;
  16073. +
  16074. + err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
  16075. + if (unlikely(err))
  16076. + goto out_dentry;
  16077. +
  16078. + au_set_dbstart(dentry, bindex);
  16079. + au_set_dbend(dentry, bindex);
  16080. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  16081. + inode = au_new_inode(dentry, /*must_new*/1);
  16082. + if (IS_ERR(inode)) {
  16083. + err = PTR_ERR(inode);
  16084. + au_set_h_dptr(dentry, bindex, NULL);
  16085. + au_set_dbstart(dentry, -1);
  16086. + au_set_dbend(dentry, -1);
  16087. + } else {
  16088. + if (!inode->i_nlink)
  16089. + set_nlink(inode, 1);
  16090. + d_tmpfile(dentry, inode);
  16091. + au_di(dentry)->di_tmpfile = 1;
  16092. +
  16093. + /* update without i_mutex */
  16094. + if (au_ibstart(dir) == au_dbstart(dentry))
  16095. + au_cpup_attr_timesizes(dir);
  16096. + }
  16097. +
  16098. +out_dentry:
  16099. + dput(h_dentry);
  16100. +out_mnt:
  16101. + vfsub_mnt_drop_write(h_mnt);
  16102. +out_parent:
  16103. + di_write_unlock(parent);
  16104. + dput(parent);
  16105. + di_write_unlock(dentry);
  16106. + if (!err)
  16107. +#if 0
  16108. + /* verbose coding for lock class name */
  16109. + au_rw_class(&au_di(dentry)->di_rwsem,
  16110. + au_lc_key + AuLcNonDir_DIINFO);
  16111. +#else
  16112. + ;
  16113. +#endif
  16114. + else {
  16115. + au_di_fin(dentry);
  16116. + dentry->d_fsdata = NULL;
  16117. + }
  16118. +out_si:
  16119. + si_read_unlock(sb);
  16120. +out:
  16121. + mutex_unlock(&dir->i_mutex);
  16122. + return err;
  16123. +}
  16124. +
  16125. +/* ---------------------------------------------------------------------- */
  16126. +
  16127. +struct au_link_args {
  16128. + aufs_bindex_t bdst, bsrc;
  16129. + struct au_pin pin;
  16130. + struct path h_path;
  16131. + struct dentry *src_parent, *parent;
  16132. +};
  16133. +
  16134. +static int au_cpup_before_link(struct dentry *src_dentry,
  16135. + struct au_link_args *a)
  16136. +{
  16137. + int err;
  16138. + struct dentry *h_src_dentry;
  16139. + struct au_cp_generic cpg = {
  16140. + .dentry = src_dentry,
  16141. + .bdst = a->bdst,
  16142. + .bsrc = a->bsrc,
  16143. + .len = -1,
  16144. + .pin = &a->pin,
  16145. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  16146. + };
  16147. +
  16148. + di_read_lock_parent(a->src_parent, AuLock_IR);
  16149. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  16150. + if (unlikely(err))
  16151. + goto out;
  16152. +
  16153. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  16154. + err = au_pin(&a->pin, src_dentry, a->bdst,
  16155. + au_opt_udba(src_dentry->d_sb),
  16156. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16157. + if (unlikely(err))
  16158. + goto out;
  16159. +
  16160. + err = au_sio_cpup_simple(&cpg);
  16161. + au_unpin(&a->pin);
  16162. +
  16163. +out:
  16164. + di_read_unlock(a->src_parent, AuLock_IR);
  16165. + return err;
  16166. +}
  16167. +
  16168. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  16169. + struct au_link_args *a)
  16170. +{
  16171. + int err;
  16172. + unsigned char plink;
  16173. + aufs_bindex_t bend;
  16174. + struct dentry *h_src_dentry;
  16175. + struct inode *h_inode, *inode, *delegated;
  16176. + struct super_block *sb;
  16177. + struct file *h_file;
  16178. +
  16179. + plink = 0;
  16180. + h_inode = NULL;
  16181. + sb = src_dentry->d_sb;
  16182. + inode = src_dentry->d_inode;
  16183. + if (au_ibstart(inode) <= a->bdst)
  16184. + h_inode = au_h_iptr(inode, a->bdst);
  16185. + if (!h_inode || !h_inode->i_nlink) {
  16186. + /* copyup src_dentry as the name of dentry. */
  16187. + bend = au_dbend(dentry);
  16188. + if (bend < a->bsrc)
  16189. + au_set_dbend(dentry, a->bsrc);
  16190. + au_set_h_dptr(dentry, a->bsrc,
  16191. + dget(au_h_dptr(src_dentry, a->bsrc)));
  16192. + dget(a->h_path.dentry);
  16193. + au_set_h_dptr(dentry, a->bdst, NULL);
  16194. + AuDbg("temporary d_inode...\n");
  16195. + spin_lock(&dentry->d_lock);
  16196. + dentry->d_inode = src_dentry->d_inode; /* tmp */
  16197. + spin_unlock(&dentry->d_lock);
  16198. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  16199. + if (IS_ERR(h_file))
  16200. + err = PTR_ERR(h_file);
  16201. + else {
  16202. + struct au_cp_generic cpg = {
  16203. + .dentry = dentry,
  16204. + .bdst = a->bdst,
  16205. + .bsrc = -1,
  16206. + .len = -1,
  16207. + .pin = &a->pin,
  16208. + .flags = AuCpup_KEEPLINO
  16209. + };
  16210. + err = au_sio_cpup_simple(&cpg);
  16211. + au_h_open_post(dentry, a->bsrc, h_file);
  16212. + if (!err) {
  16213. + dput(a->h_path.dentry);
  16214. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  16215. + } else
  16216. + au_set_h_dptr(dentry, a->bdst,
  16217. + a->h_path.dentry);
  16218. + }
  16219. + spin_lock(&dentry->d_lock);
  16220. + dentry->d_inode = NULL; /* restore */
  16221. + spin_unlock(&dentry->d_lock);
  16222. + AuDbg("temporary d_inode...done\n");
  16223. + au_set_h_dptr(dentry, a->bsrc, NULL);
  16224. + au_set_dbend(dentry, bend);
  16225. + } else {
  16226. + /* the inode of src_dentry already exists on a.bdst branch */
  16227. + h_src_dentry = d_find_alias(h_inode);
  16228. + if (!h_src_dentry && au_plink_test(inode)) {
  16229. + plink = 1;
  16230. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  16231. + err = PTR_ERR(h_src_dentry);
  16232. + if (IS_ERR(h_src_dentry))
  16233. + goto out;
  16234. +
  16235. + if (unlikely(!h_src_dentry->d_inode)) {
  16236. + dput(h_src_dentry);
  16237. + h_src_dentry = NULL;
  16238. + }
  16239. +
  16240. + }
  16241. + if (h_src_dentry) {
  16242. + delegated = NULL;
  16243. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  16244. + &a->h_path, &delegated);
  16245. + if (unlikely(err == -EWOULDBLOCK)) {
  16246. + pr_warn("cannot retry for NFSv4 delegation"
  16247. + " for an internal link\n");
  16248. + iput(delegated);
  16249. + }
  16250. + dput(h_src_dentry);
  16251. + } else {
  16252. + AuIOErr("no dentry found for hi%lu on b%d\n",
  16253. + h_inode->i_ino, a->bdst);
  16254. + err = -EIO;
  16255. + }
  16256. + }
  16257. +
  16258. + if (!err && !plink)
  16259. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  16260. +
  16261. +out:
  16262. + AuTraceErr(err);
  16263. + return err;
  16264. +}
  16265. +
  16266. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  16267. + struct dentry *dentry)
  16268. +{
  16269. + int err, rerr;
  16270. + struct au_dtime dt;
  16271. + struct au_link_args *a;
  16272. + struct dentry *wh_dentry, *h_src_dentry;
  16273. + struct inode *inode, *delegated;
  16274. + struct super_block *sb;
  16275. + struct au_wr_dir_args wr_dir_args = {
  16276. + /* .force_btgt = -1, */
  16277. + .flags = AuWrDir_ADD_ENTRY
  16278. + };
  16279. +
  16280. + IMustLock(dir);
  16281. + inode = src_dentry->d_inode;
  16282. + IMustLock(inode);
  16283. +
  16284. + err = -ENOMEM;
  16285. + a = kzalloc(sizeof(*a), GFP_NOFS);
  16286. + if (unlikely(!a))
  16287. + goto out;
  16288. +
  16289. + a->parent = dentry->d_parent; /* dir inode is locked */
  16290. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  16291. + AuLock_NOPLM | AuLock_GEN);
  16292. + if (unlikely(err))
  16293. + goto out_kfree;
  16294. + err = au_d_linkable(src_dentry);
  16295. + if (unlikely(err))
  16296. + goto out_unlock;
  16297. + err = au_d_may_add(dentry);
  16298. + if (unlikely(err))
  16299. + goto out_unlock;
  16300. +
  16301. + a->src_parent = dget_parent(src_dentry);
  16302. + wr_dir_args.force_btgt = au_ibstart(inode);
  16303. +
  16304. + di_write_lock_parent(a->parent);
  16305. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  16306. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  16307. + &wr_dir_args);
  16308. + err = PTR_ERR(wh_dentry);
  16309. + if (IS_ERR(wh_dentry))
  16310. + goto out_parent;
  16311. +
  16312. + err = 0;
  16313. + sb = dentry->d_sb;
  16314. + a->bdst = au_dbstart(dentry);
  16315. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  16316. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  16317. + a->bsrc = au_ibstart(inode);
  16318. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  16319. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  16320. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  16321. + if (!h_src_dentry) {
  16322. + a->bsrc = au_dbstart(src_dentry);
  16323. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  16324. + AuDebugOn(!h_src_dentry);
  16325. + } else if (IS_ERR(h_src_dentry)) {
  16326. + err = PTR_ERR(h_src_dentry);
  16327. + goto out_parent;
  16328. + }
  16329. +
  16330. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  16331. + if (a->bdst < a->bsrc
  16332. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  16333. + err = au_cpup_or_link(src_dentry, dentry, a);
  16334. + else {
  16335. + delegated = NULL;
  16336. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  16337. + &a->h_path, &delegated);
  16338. + if (unlikely(err == -EWOULDBLOCK)) {
  16339. + pr_warn("cannot retry for NFSv4 delegation"
  16340. + " for an internal link\n");
  16341. + iput(delegated);
  16342. + }
  16343. + }
  16344. + dput(h_src_dentry);
  16345. + } else {
  16346. + /*
  16347. + * copyup src_dentry to the branch we process,
  16348. + * and then link(2) to it.
  16349. + */
  16350. + dput(h_src_dentry);
  16351. + if (a->bdst < a->bsrc
  16352. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  16353. + au_unpin(&a->pin);
  16354. + di_write_unlock(a->parent);
  16355. + err = au_cpup_before_link(src_dentry, a);
  16356. + di_write_lock_parent(a->parent);
  16357. + if (!err)
  16358. + err = au_pin(&a->pin, dentry, a->bdst,
  16359. + au_opt_udba(sb),
  16360. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16361. + if (unlikely(err))
  16362. + goto out_wh;
  16363. + }
  16364. + if (!err) {
  16365. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  16366. + err = -ENOENT;
  16367. + if (h_src_dentry && h_src_dentry->d_inode) {
  16368. + delegated = NULL;
  16369. + err = vfsub_link(h_src_dentry,
  16370. + au_pinned_h_dir(&a->pin),
  16371. + &a->h_path, &delegated);
  16372. + if (unlikely(err == -EWOULDBLOCK)) {
  16373. + pr_warn("cannot retry"
  16374. + " for NFSv4 delegation"
  16375. + " for an internal link\n");
  16376. + iput(delegated);
  16377. + }
  16378. + }
  16379. + }
  16380. + }
  16381. + if (unlikely(err))
  16382. + goto out_unpin;
  16383. +
  16384. + if (wh_dentry) {
  16385. + a->h_path.dentry = wh_dentry;
  16386. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  16387. + dentry);
  16388. + if (unlikely(err))
  16389. + goto out_revert;
  16390. + }
  16391. +
  16392. + au_dir_ts(dir, a->bdst);
  16393. + dir->i_version++;
  16394. + inc_nlink(inode);
  16395. + inode->i_ctime = dir->i_ctime;
  16396. + d_instantiate(dentry, au_igrab(inode));
  16397. + if (d_unhashed(a->h_path.dentry))
  16398. + /* some filesystem calls d_drop() */
  16399. + d_drop(dentry);
  16400. + /* some filesystems consume an inode even hardlink */
  16401. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  16402. + goto out_unpin; /* success */
  16403. +
  16404. +out_revert:
  16405. + /* no delegation since it is just created */
  16406. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  16407. + /*delegated*/NULL, /*force*/0);
  16408. + if (unlikely(rerr)) {
  16409. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  16410. + err = -EIO;
  16411. + }
  16412. + au_dtime_revert(&dt);
  16413. +out_unpin:
  16414. + au_unpin(&a->pin);
  16415. +out_wh:
  16416. + dput(wh_dentry);
  16417. +out_parent:
  16418. + di_write_unlock(a->parent);
  16419. + dput(a->src_parent);
  16420. +out_unlock:
  16421. + if (unlikely(err)) {
  16422. + au_update_dbstart(dentry);
  16423. + d_drop(dentry);
  16424. + }
  16425. + aufs_read_and_write_unlock2(dentry, src_dentry);
  16426. +out_kfree:
  16427. + kfree(a);
  16428. +out:
  16429. + AuTraceErr(err);
  16430. + return err;
  16431. +}
  16432. +
  16433. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  16434. +{
  16435. + int err, rerr;
  16436. + aufs_bindex_t bindex;
  16437. + unsigned char diropq;
  16438. + struct path h_path;
  16439. + struct dentry *wh_dentry, *parent, *opq_dentry;
  16440. + struct mutex *h_mtx;
  16441. + struct super_block *sb;
  16442. + struct {
  16443. + struct au_pin pin;
  16444. + struct au_dtime dt;
  16445. + } *a; /* reduce the stack usage */
  16446. + struct au_wr_dir_args wr_dir_args = {
  16447. + .force_btgt = -1,
  16448. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  16449. + };
  16450. +
  16451. + IMustLock(dir);
  16452. +
  16453. + err = -ENOMEM;
  16454. + a = kmalloc(sizeof(*a), GFP_NOFS);
  16455. + if (unlikely(!a))
  16456. + goto out;
  16457. +
  16458. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  16459. + if (unlikely(err))
  16460. + goto out_free;
  16461. + err = au_d_may_add(dentry);
  16462. + if (unlikely(err))
  16463. + goto out_unlock;
  16464. +
  16465. + parent = dentry->d_parent; /* dir inode is locked */
  16466. + di_write_lock_parent(parent);
  16467. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  16468. + &a->pin, &wr_dir_args);
  16469. + err = PTR_ERR(wh_dentry);
  16470. + if (IS_ERR(wh_dentry))
  16471. + goto out_parent;
  16472. +
  16473. + sb = dentry->d_sb;
  16474. + bindex = au_dbstart(dentry);
  16475. + h_path.dentry = au_h_dptr(dentry, bindex);
  16476. + h_path.mnt = au_sbr_mnt(sb, bindex);
  16477. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  16478. + if (unlikely(err))
  16479. + goto out_unpin;
  16480. +
  16481. + /* make the dir opaque */
  16482. + diropq = 0;
  16483. + h_mtx = &h_path.dentry->d_inode->i_mutex;
  16484. + if (wh_dentry
  16485. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  16486. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  16487. + opq_dentry = au_diropq_create(dentry, bindex);
  16488. + mutex_unlock(h_mtx);
  16489. + err = PTR_ERR(opq_dentry);
  16490. + if (IS_ERR(opq_dentry))
  16491. + goto out_dir;
  16492. + dput(opq_dentry);
  16493. + diropq = 1;
  16494. + }
  16495. +
  16496. + err = epilog(dir, bindex, wh_dentry, dentry);
  16497. + if (!err) {
  16498. + inc_nlink(dir);
  16499. + goto out_unpin; /* success */
  16500. + }
  16501. +
  16502. + /* revert */
  16503. + if (diropq) {
  16504. + AuLabel(revert opq);
  16505. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  16506. + rerr = au_diropq_remove(dentry, bindex);
  16507. + mutex_unlock(h_mtx);
  16508. + if (rerr) {
  16509. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  16510. + dentry, err, rerr);
  16511. + err = -EIO;
  16512. + }
  16513. + }
  16514. +
  16515. +out_dir:
  16516. + AuLabel(revert dir);
  16517. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  16518. + if (rerr) {
  16519. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  16520. + dentry, err, rerr);
  16521. + err = -EIO;
  16522. + }
  16523. + au_dtime_revert(&a->dt);
  16524. +out_unpin:
  16525. + au_unpin(&a->pin);
  16526. + dput(wh_dentry);
  16527. +out_parent:
  16528. + di_write_unlock(parent);
  16529. +out_unlock:
  16530. + if (unlikely(err)) {
  16531. + au_update_dbstart(dentry);
  16532. + d_drop(dentry);
  16533. + }
  16534. + aufs_read_unlock(dentry, AuLock_DW);
  16535. +out_free:
  16536. + kfree(a);
  16537. +out:
  16538. + return err;
  16539. +}
  16540. diff -Nur linux-4.0.4.orig/fs/aufs/i_op.c linux-4.0.4/fs/aufs/i_op.c
  16541. --- linux-4.0.4.orig/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
  16542. +++ linux-4.0.4/fs/aufs/i_op.c 2015-05-30 22:11:31.000000000 +0200
  16543. @@ -0,0 +1,1432 @@
  16544. +/*
  16545. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  16546. + */
  16547. +
  16548. +/*
  16549. + * inode operations (except add/del/rename)
  16550. + */
  16551. +
  16552. +#include <linux/device_cgroup.h>
  16553. +#include <linux/fs_stack.h>
  16554. +#include <linux/mm.h>
  16555. +#include <linux/namei.h>
  16556. +#include <linux/security.h>
  16557. +#include "aufs.h"
  16558. +
  16559. +static int h_permission(struct inode *h_inode, int mask,
  16560. + struct vfsmount *h_mnt, int brperm)
  16561. +{
  16562. + int err;
  16563. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  16564. +
  16565. + err = -EACCES;
  16566. + if ((write_mask && IS_IMMUTABLE(h_inode))
  16567. + || ((mask & MAY_EXEC)
  16568. + && S_ISREG(h_inode->i_mode)
  16569. + && ((h_mnt->mnt_flags & MNT_NOEXEC)
  16570. + || !(h_inode->i_mode & S_IXUGO))))
  16571. + goto out;
  16572. +
  16573. + /*
  16574. + * - skip the lower fs test in the case of write to ro branch.
  16575. + * - nfs dir permission write check is optimized, but a policy for
  16576. + * link/rename requires a real check.
  16577. + * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
  16578. + * in this case, generic_permission() returns -EOPNOTSUPP.
  16579. + */
  16580. + if ((write_mask && !au_br_writable(brperm))
  16581. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  16582. + && write_mask && !(mask & MAY_READ))
  16583. + || !h_inode->i_op->permission) {
  16584. + /* AuLabel(generic_permission); */
  16585. + /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
  16586. + err = generic_permission(h_inode, mask);
  16587. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  16588. + err = h_inode->i_op->permission(h_inode, mask);
  16589. + AuTraceErr(err);
  16590. + } else {
  16591. + /* AuLabel(h_inode->permission); */
  16592. + err = h_inode->i_op->permission(h_inode, mask);
  16593. + AuTraceErr(err);
  16594. + }
  16595. +
  16596. + if (!err)
  16597. + err = devcgroup_inode_permission(h_inode, mask);
  16598. + if (!err)
  16599. + err = security_inode_permission(h_inode, mask);
  16600. +
  16601. +#if 0
  16602. + if (!err) {
  16603. + /* todo: do we need to call ima_path_check()? */
  16604. + struct path h_path = {
  16605. + .dentry =
  16606. + .mnt = h_mnt
  16607. + };
  16608. + err = ima_path_check(&h_path,
  16609. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  16610. + IMA_COUNT_LEAVE);
  16611. + }
  16612. +#endif
  16613. +
  16614. +out:
  16615. + return err;
  16616. +}
  16617. +
  16618. +static int aufs_permission(struct inode *inode, int mask)
  16619. +{
  16620. + int err;
  16621. + aufs_bindex_t bindex, bend;
  16622. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  16623. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  16624. + struct inode *h_inode;
  16625. + struct super_block *sb;
  16626. + struct au_branch *br;
  16627. +
  16628. + /* todo: support rcu-walk? */
  16629. + if (mask & MAY_NOT_BLOCK)
  16630. + return -ECHILD;
  16631. +
  16632. + sb = inode->i_sb;
  16633. + si_read_lock(sb, AuLock_FLUSH);
  16634. + ii_read_lock_child(inode);
  16635. +#if 0
  16636. + err = au_iigen_test(inode, au_sigen(sb));
  16637. + if (unlikely(err))
  16638. + goto out;
  16639. +#endif
  16640. +
  16641. + if (!isdir
  16642. + || write_mask
  16643. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  16644. + err = au_busy_or_stale();
  16645. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  16646. + if (unlikely(!h_inode
  16647. + || (h_inode->i_mode & S_IFMT)
  16648. + != (inode->i_mode & S_IFMT)))
  16649. + goto out;
  16650. +
  16651. + err = 0;
  16652. + bindex = au_ibstart(inode);
  16653. + br = au_sbr(sb, bindex);
  16654. + err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
  16655. + if (write_mask
  16656. + && !err
  16657. + && !special_file(h_inode->i_mode)) {
  16658. + /* test whether the upper writable branch exists */
  16659. + err = -EROFS;
  16660. + for (; bindex >= 0; bindex--)
  16661. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  16662. + err = 0;
  16663. + break;
  16664. + }
  16665. + }
  16666. + goto out;
  16667. + }
  16668. +
  16669. + /* non-write to dir */
  16670. + err = 0;
  16671. + bend = au_ibend(inode);
  16672. + for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
  16673. + h_inode = au_h_iptr(inode, bindex);
  16674. + if (h_inode) {
  16675. + err = au_busy_or_stale();
  16676. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  16677. + break;
  16678. +
  16679. + br = au_sbr(sb, bindex);
  16680. + err = h_permission(h_inode, mask, au_br_mnt(br),
  16681. + br->br_perm);
  16682. + }
  16683. + }
  16684. +
  16685. +out:
  16686. + ii_read_unlock(inode);
  16687. + si_read_unlock(sb);
  16688. + return err;
  16689. +}
  16690. +
  16691. +/* ---------------------------------------------------------------------- */
  16692. +
  16693. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  16694. + unsigned int flags)
  16695. +{
  16696. + struct dentry *ret, *parent;
  16697. + struct inode *inode;
  16698. + struct super_block *sb;
  16699. + int err, npositive;
  16700. +
  16701. + IMustLock(dir);
  16702. +
  16703. + /* todo: support rcu-walk? */
  16704. + ret = ERR_PTR(-ECHILD);
  16705. + if (flags & LOOKUP_RCU)
  16706. + goto out;
  16707. +
  16708. + ret = ERR_PTR(-ENAMETOOLONG);
  16709. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16710. + goto out;
  16711. +
  16712. + sb = dir->i_sb;
  16713. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16714. + ret = ERR_PTR(err);
  16715. + if (unlikely(err))
  16716. + goto out;
  16717. +
  16718. + err = au_di_init(dentry);
  16719. + ret = ERR_PTR(err);
  16720. + if (unlikely(err))
  16721. + goto out_si;
  16722. +
  16723. + inode = NULL;
  16724. + npositive = 0; /* suppress a warning */
  16725. + parent = dentry->d_parent; /* dir inode is locked */
  16726. + di_read_lock_parent(parent, AuLock_IR);
  16727. + err = au_alive_dir(parent);
  16728. + if (!err)
  16729. + err = au_digen_test(parent, au_sigen(sb));
  16730. + if (!err) {
  16731. + npositive = au_lkup_dentry(dentry, au_dbstart(parent),
  16732. + /*type*/0);
  16733. + err = npositive;
  16734. + }
  16735. + di_read_unlock(parent, AuLock_IR);
  16736. + ret = ERR_PTR(err);
  16737. + if (unlikely(err < 0))
  16738. + goto out_unlock;
  16739. +
  16740. + if (npositive) {
  16741. + inode = au_new_inode(dentry, /*must_new*/0);
  16742. + if (IS_ERR(inode)) {
  16743. + ret = (void *)inode;
  16744. + inode = NULL;
  16745. + goto out_unlock;
  16746. + }
  16747. + }
  16748. +
  16749. + if (inode)
  16750. + atomic_inc(&inode->i_count);
  16751. + ret = d_splice_alias(inode, dentry);
  16752. +#if 0
  16753. + if (unlikely(d_need_lookup(dentry))) {
  16754. + spin_lock(&dentry->d_lock);
  16755. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  16756. + spin_unlock(&dentry->d_lock);
  16757. + } else
  16758. +#endif
  16759. + if (inode) {
  16760. + if (!IS_ERR(ret)) {
  16761. + iput(inode);
  16762. + if (ret && ret != dentry)
  16763. + ii_write_unlock(inode);
  16764. + } else {
  16765. + ii_write_unlock(inode);
  16766. + iput(inode);
  16767. + inode = NULL;
  16768. + }
  16769. + }
  16770. +
  16771. +out_unlock:
  16772. + di_write_unlock(dentry);
  16773. + if (inode) {
  16774. + /* verbose coding for lock class name */
  16775. + if (unlikely(S_ISLNK(inode->i_mode)))
  16776. + au_rw_class(&au_di(dentry)->di_rwsem,
  16777. + au_lc_key + AuLcSymlink_DIINFO);
  16778. + else if (unlikely(S_ISDIR(inode->i_mode)))
  16779. + au_rw_class(&au_di(dentry)->di_rwsem,
  16780. + au_lc_key + AuLcDir_DIINFO);
  16781. + else /* likely */
  16782. + au_rw_class(&au_di(dentry)->di_rwsem,
  16783. + au_lc_key + AuLcNonDir_DIINFO);
  16784. + }
  16785. +out_si:
  16786. + si_read_unlock(sb);
  16787. +out:
  16788. + return ret;
  16789. +}
  16790. +
  16791. +/* ---------------------------------------------------------------------- */
  16792. +
  16793. +struct aopen_node {
  16794. + struct hlist_node hlist;
  16795. + struct file *file, *h_file;
  16796. +};
  16797. +
  16798. +static int au_do_aopen(struct inode *inode, struct file *file)
  16799. +{
  16800. + struct au_sphlhead *aopen;
  16801. + struct aopen_node *node;
  16802. + struct au_do_open_args args = {
  16803. + .no_lock = 1,
  16804. + .open = au_do_open_nondir
  16805. + };
  16806. +
  16807. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  16808. + spin_lock(&aopen->spin);
  16809. + hlist_for_each_entry(node, &aopen->head, hlist)
  16810. + if (node->file == file) {
  16811. + args.h_file = node->h_file;
  16812. + break;
  16813. + }
  16814. + spin_unlock(&aopen->spin);
  16815. + /* AuDebugOn(!args.h_file); */
  16816. +
  16817. + return au_do_open(file, &args);
  16818. +}
  16819. +
  16820. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  16821. + struct file *file, unsigned int open_flag,
  16822. + umode_t create_mode, int *opened)
  16823. +{
  16824. + int err, h_opened = *opened;
  16825. + struct dentry *parent;
  16826. + struct dentry *d;
  16827. + struct au_sphlhead *aopen;
  16828. + struct vfsub_aopen_args args = {
  16829. + .open_flag = open_flag,
  16830. + .create_mode = create_mode,
  16831. + .opened = &h_opened
  16832. + };
  16833. + struct aopen_node aopen_node = {
  16834. + .file = file
  16835. + };
  16836. +
  16837. + IMustLock(dir);
  16838. + AuDbg("open_flag 0x%x\n", open_flag);
  16839. + AuDbgDentry(dentry);
  16840. +
  16841. + err = 0;
  16842. + if (!au_di(dentry)) {
  16843. + d = aufs_lookup(dir, dentry, /*flags*/0);
  16844. + if (IS_ERR(d)) {
  16845. + err = PTR_ERR(d);
  16846. + goto out;
  16847. + } else if (d) {
  16848. + /*
  16849. + * obsoleted dentry found.
  16850. + * another error will be returned later.
  16851. + */
  16852. + d_drop(d);
  16853. + dput(d);
  16854. + AuDbgDentry(d);
  16855. + }
  16856. + AuDbgDentry(dentry);
  16857. + }
  16858. +
  16859. + if (d_is_positive(dentry)
  16860. + || d_unhashed(dentry)
  16861. + || d_unlinked(dentry)
  16862. + || !(open_flag & O_CREAT))
  16863. + goto out_no_open;
  16864. +
  16865. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  16866. + if (unlikely(err))
  16867. + goto out;
  16868. +
  16869. + parent = dentry->d_parent; /* dir is locked */
  16870. + di_write_lock_parent(parent);
  16871. + err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
  16872. + if (unlikely(err))
  16873. + goto out_unlock;
  16874. +
  16875. + AuDbgDentry(dentry);
  16876. + if (d_is_positive(dentry))
  16877. + goto out_unlock;
  16878. +
  16879. + args.file = get_empty_filp();
  16880. + err = PTR_ERR(args.file);
  16881. + if (IS_ERR(args.file))
  16882. + goto out_unlock;
  16883. +
  16884. + args.file->f_flags = file->f_flags;
  16885. + err = au_aopen_or_create(dir, dentry, &args);
  16886. + AuTraceErr(err);
  16887. + AuDbgFile(args.file);
  16888. + if (unlikely(err < 0)) {
  16889. + if (h_opened & FILE_OPENED)
  16890. + fput(args.file);
  16891. + else
  16892. + put_filp(args.file);
  16893. + goto out_unlock;
  16894. + }
  16895. +
  16896. + /* some filesystems don't set FILE_CREATED while succeeded? */
  16897. + *opened |= FILE_CREATED;
  16898. + if (h_opened & FILE_OPENED)
  16899. + aopen_node.h_file = args.file;
  16900. + else {
  16901. + put_filp(args.file);
  16902. + args.file = NULL;
  16903. + }
  16904. + aopen = &au_sbi(dir->i_sb)->si_aopen;
  16905. + au_sphl_add(&aopen_node.hlist, aopen);
  16906. + err = finish_open(file, dentry, au_do_aopen, opened);
  16907. + au_sphl_del(&aopen_node.hlist, aopen);
  16908. + AuTraceErr(err);
  16909. + AuDbgFile(file);
  16910. + if (aopen_node.h_file)
  16911. + fput(aopen_node.h_file);
  16912. +
  16913. +out_unlock:
  16914. + di_write_unlock(parent);
  16915. + aufs_read_unlock(dentry, AuLock_DW);
  16916. + AuDbgDentry(dentry);
  16917. + if (unlikely(err))
  16918. + goto out;
  16919. +out_no_open:
  16920. + if (!err && !(*opened & FILE_CREATED)) {
  16921. + AuLabel(out_no_open);
  16922. + dget(dentry);
  16923. + err = finish_no_open(file, dentry);
  16924. + }
  16925. +out:
  16926. + AuDbg("%pd%s%s\n", dentry,
  16927. + (*opened & FILE_CREATED) ? " created" : "",
  16928. + (*opened & FILE_OPENED) ? " opened" : "");
  16929. + AuTraceErr(err);
  16930. + return err;
  16931. +}
  16932. +
  16933. +
  16934. +/* ---------------------------------------------------------------------- */
  16935. +
  16936. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  16937. + const unsigned char add_entry, aufs_bindex_t bcpup,
  16938. + aufs_bindex_t bstart)
  16939. +{
  16940. + int err;
  16941. + struct dentry *h_parent;
  16942. + struct inode *h_dir;
  16943. +
  16944. + if (add_entry)
  16945. + IMustLock(parent->d_inode);
  16946. + else
  16947. + di_write_lock_parent(parent);
  16948. +
  16949. + err = 0;
  16950. + if (!au_h_dptr(parent, bcpup)) {
  16951. + if (bstart > bcpup)
  16952. + err = au_cpup_dirs(dentry, bcpup);
  16953. + else if (bstart < bcpup)
  16954. + err = au_cpdown_dirs(dentry, bcpup);
  16955. + else
  16956. + BUG();
  16957. + }
  16958. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  16959. + h_parent = au_h_dptr(parent, bcpup);
  16960. + h_dir = h_parent->d_inode;
  16961. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  16962. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  16963. + /* todo: no unlock here */
  16964. + mutex_unlock(&h_dir->i_mutex);
  16965. +
  16966. + AuDbg("bcpup %d\n", bcpup);
  16967. + if (!err) {
  16968. + if (!dentry->d_inode)
  16969. + au_set_h_dptr(dentry, bstart, NULL);
  16970. + au_update_dbrange(dentry, /*do_put_zero*/0);
  16971. + }
  16972. + }
  16973. +
  16974. + if (!add_entry)
  16975. + di_write_unlock(parent);
  16976. + if (!err)
  16977. + err = bcpup; /* success */
  16978. +
  16979. + AuTraceErr(err);
  16980. + return err;
  16981. +}
  16982. +
  16983. +/*
  16984. + * decide the branch and the parent dir where we will create a new entry.
  16985. + * returns new bindex or an error.
  16986. + * copyup the parent dir if needed.
  16987. + */
  16988. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  16989. + struct au_wr_dir_args *args)
  16990. +{
  16991. + int err;
  16992. + unsigned int flags;
  16993. + aufs_bindex_t bcpup, bstart, src_bstart;
  16994. + const unsigned char add_entry
  16995. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  16996. + | au_ftest_wrdir(args->flags, TMPFILE);
  16997. + struct super_block *sb;
  16998. + struct dentry *parent;
  16999. + struct au_sbinfo *sbinfo;
  17000. +
  17001. + sb = dentry->d_sb;
  17002. + sbinfo = au_sbi(sb);
  17003. + parent = dget_parent(dentry);
  17004. + bstart = au_dbstart(dentry);
  17005. + bcpup = bstart;
  17006. + if (args->force_btgt < 0) {
  17007. + if (src_dentry) {
  17008. + src_bstart = au_dbstart(src_dentry);
  17009. + if (src_bstart < bstart)
  17010. + bcpup = src_bstart;
  17011. + } else if (add_entry) {
  17012. + flags = 0;
  17013. + if (au_ftest_wrdir(args->flags, ISDIR))
  17014. + au_fset_wbr(flags, DIR);
  17015. + err = AuWbrCreate(sbinfo, dentry, flags);
  17016. + bcpup = err;
  17017. + }
  17018. +
  17019. + if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
  17020. + if (add_entry)
  17021. + err = AuWbrCopyup(sbinfo, dentry);
  17022. + else {
  17023. + if (!IS_ROOT(dentry)) {
  17024. + di_read_lock_parent(parent, !AuLock_IR);
  17025. + err = AuWbrCopyup(sbinfo, dentry);
  17026. + di_read_unlock(parent, !AuLock_IR);
  17027. + } else
  17028. + err = AuWbrCopyup(sbinfo, dentry);
  17029. + }
  17030. + bcpup = err;
  17031. + if (unlikely(err < 0))
  17032. + goto out;
  17033. + }
  17034. + } else {
  17035. + bcpup = args->force_btgt;
  17036. + AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
  17037. + }
  17038. +
  17039. + AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
  17040. + err = bcpup;
  17041. + if (bcpup == bstart)
  17042. + goto out; /* success */
  17043. +
  17044. + /* copyup the new parent into the branch we process */
  17045. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
  17046. + if (err >= 0) {
  17047. + if (!dentry->d_inode) {
  17048. + au_set_h_dptr(dentry, bstart, NULL);
  17049. + au_set_dbstart(dentry, bcpup);
  17050. + au_set_dbend(dentry, bcpup);
  17051. + }
  17052. + AuDebugOn(add_entry
  17053. + && !au_ftest_wrdir(args->flags, TMPFILE)
  17054. + && !au_h_dptr(dentry, bcpup));
  17055. + }
  17056. +
  17057. +out:
  17058. + dput(parent);
  17059. + return err;
  17060. +}
  17061. +
  17062. +/* ---------------------------------------------------------------------- */
  17063. +
  17064. +void au_pin_hdir_unlock(struct au_pin *p)
  17065. +{
  17066. + if (p->hdir)
  17067. + au_hn_imtx_unlock(p->hdir);
  17068. +}
  17069. +
  17070. +int au_pin_hdir_lock(struct au_pin *p)
  17071. +{
  17072. + int err;
  17073. +
  17074. + err = 0;
  17075. + if (!p->hdir)
  17076. + goto out;
  17077. +
  17078. + /* even if an error happens later, keep this lock */
  17079. + au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
  17080. +
  17081. + err = -EBUSY;
  17082. + if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
  17083. + goto out;
  17084. +
  17085. + err = 0;
  17086. + if (p->h_dentry)
  17087. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  17088. + p->h_parent, p->br);
  17089. +
  17090. +out:
  17091. + return err;
  17092. +}
  17093. +
  17094. +int au_pin_hdir_relock(struct au_pin *p)
  17095. +{
  17096. + int err, i;
  17097. + struct inode *h_i;
  17098. + struct dentry *h_d[] = {
  17099. + p->h_dentry,
  17100. + p->h_parent
  17101. + };
  17102. +
  17103. + err = au_pin_hdir_lock(p);
  17104. + if (unlikely(err))
  17105. + goto out;
  17106. +
  17107. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  17108. + if (!h_d[i])
  17109. + continue;
  17110. + h_i = h_d[i]->d_inode;
  17111. + if (h_i)
  17112. + err = !h_i->i_nlink;
  17113. + }
  17114. +
  17115. +out:
  17116. + return err;
  17117. +}
  17118. +
  17119. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  17120. +{
  17121. +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
  17122. + p->hdir->hi_inode->i_mutex.owner = task;
  17123. +#endif
  17124. +}
  17125. +
  17126. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  17127. +{
  17128. + if (p->hdir) {
  17129. + mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
  17130. + p->lsc_hi, 0, NULL, _RET_IP_);
  17131. + au_pin_hdir_set_owner(p, current);
  17132. + }
  17133. +}
  17134. +
  17135. +void au_pin_hdir_release(struct au_pin *p)
  17136. +{
  17137. + if (p->hdir) {
  17138. + au_pin_hdir_set_owner(p, p->task);
  17139. + mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
  17140. + }
  17141. +}
  17142. +
  17143. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  17144. +{
  17145. + if (pin && pin->parent)
  17146. + return au_h_dptr(pin->parent, pin->bindex);
  17147. + return NULL;
  17148. +}
  17149. +
  17150. +void au_unpin(struct au_pin *p)
  17151. +{
  17152. + if (p->hdir)
  17153. + au_pin_hdir_unlock(p);
  17154. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  17155. + vfsub_mnt_drop_write(p->h_mnt);
  17156. + if (!p->hdir)
  17157. + return;
  17158. +
  17159. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17160. + di_read_unlock(p->parent, AuLock_IR);
  17161. + iput(p->hdir->hi_inode);
  17162. + dput(p->parent);
  17163. + p->parent = NULL;
  17164. + p->hdir = NULL;
  17165. + p->h_mnt = NULL;
  17166. + /* do not clear p->task */
  17167. +}
  17168. +
  17169. +int au_do_pin(struct au_pin *p)
  17170. +{
  17171. + int err;
  17172. + struct super_block *sb;
  17173. + struct inode *h_dir;
  17174. +
  17175. + err = 0;
  17176. + sb = p->dentry->d_sb;
  17177. + p->br = au_sbr(sb, p->bindex);
  17178. + if (IS_ROOT(p->dentry)) {
  17179. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  17180. + p->h_mnt = au_br_mnt(p->br);
  17181. + err = vfsub_mnt_want_write(p->h_mnt);
  17182. + if (unlikely(err)) {
  17183. + au_fclr_pin(p->flags, MNT_WRITE);
  17184. + goto out_err;
  17185. + }
  17186. + }
  17187. + goto out;
  17188. + }
  17189. +
  17190. + p->h_dentry = NULL;
  17191. + if (p->bindex <= au_dbend(p->dentry))
  17192. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  17193. +
  17194. + p->parent = dget_parent(p->dentry);
  17195. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17196. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  17197. +
  17198. + h_dir = NULL;
  17199. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  17200. + p->hdir = au_hi(p->parent->d_inode, p->bindex);
  17201. + if (p->hdir)
  17202. + h_dir = p->hdir->hi_inode;
  17203. +
  17204. + /*
  17205. + * udba case, or
  17206. + * if DI_LOCKED is not set, then p->parent may be different
  17207. + * and h_parent can be NULL.
  17208. + */
  17209. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  17210. + err = -EBUSY;
  17211. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17212. + di_read_unlock(p->parent, AuLock_IR);
  17213. + dput(p->parent);
  17214. + p->parent = NULL;
  17215. + goto out_err;
  17216. + }
  17217. +
  17218. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  17219. + p->h_mnt = au_br_mnt(p->br);
  17220. + err = vfsub_mnt_want_write(p->h_mnt);
  17221. + if (unlikely(err)) {
  17222. + au_fclr_pin(p->flags, MNT_WRITE);
  17223. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17224. + di_read_unlock(p->parent, AuLock_IR);
  17225. + dput(p->parent);
  17226. + p->parent = NULL;
  17227. + goto out_err;
  17228. + }
  17229. + }
  17230. +
  17231. + au_igrab(h_dir);
  17232. + err = au_pin_hdir_lock(p);
  17233. + if (!err)
  17234. + goto out; /* success */
  17235. +
  17236. + au_unpin(p);
  17237. +
  17238. +out_err:
  17239. + pr_err("err %d\n", err);
  17240. + err = au_busy_or_stale();
  17241. +out:
  17242. + return err;
  17243. +}
  17244. +
  17245. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  17246. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  17247. + unsigned int udba, unsigned char flags)
  17248. +{
  17249. + p->dentry = dentry;
  17250. + p->udba = udba;
  17251. + p->lsc_di = lsc_di;
  17252. + p->lsc_hi = lsc_hi;
  17253. + p->flags = flags;
  17254. + p->bindex = bindex;
  17255. +
  17256. + p->parent = NULL;
  17257. + p->hdir = NULL;
  17258. + p->h_mnt = NULL;
  17259. +
  17260. + p->h_dentry = NULL;
  17261. + p->h_parent = NULL;
  17262. + p->br = NULL;
  17263. + p->task = current;
  17264. +}
  17265. +
  17266. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  17267. + unsigned int udba, unsigned char flags)
  17268. +{
  17269. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  17270. + udba, flags);
  17271. + return au_do_pin(pin);
  17272. +}
  17273. +
  17274. +/* ---------------------------------------------------------------------- */
  17275. +
  17276. +/*
  17277. + * ->setattr() and ->getattr() are called in various cases.
  17278. + * chmod, stat: dentry is revalidated.
  17279. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  17280. + * unhashed.
  17281. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  17282. + */
  17283. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  17284. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  17285. +{
  17286. + int err;
  17287. + struct inode *inode;
  17288. + struct dentry *parent;
  17289. +
  17290. + err = 0;
  17291. + inode = dentry->d_inode;
  17292. + if (au_digen_test(dentry, sigen)) {
  17293. + parent = dget_parent(dentry);
  17294. + di_read_lock_parent(parent, AuLock_IR);
  17295. + err = au_refresh_dentry(dentry, parent);
  17296. + di_read_unlock(parent, AuLock_IR);
  17297. + dput(parent);
  17298. + }
  17299. +
  17300. + AuTraceErr(err);
  17301. + return err;
  17302. +}
  17303. +
  17304. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  17305. + struct au_icpup_args *a)
  17306. +{
  17307. + int err;
  17308. + loff_t sz;
  17309. + aufs_bindex_t bstart, ibstart;
  17310. + struct dentry *hi_wh, *parent;
  17311. + struct inode *inode;
  17312. + struct au_wr_dir_args wr_dir_args = {
  17313. + .force_btgt = -1,
  17314. + .flags = 0
  17315. + };
  17316. +
  17317. + if (d_is_dir(dentry))
  17318. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  17319. + /* plink or hi_wh() case */
  17320. + bstart = au_dbstart(dentry);
  17321. + inode = dentry->d_inode;
  17322. + ibstart = au_ibstart(inode);
  17323. + if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
  17324. + wr_dir_args.force_btgt = ibstart;
  17325. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  17326. + if (unlikely(err < 0))
  17327. + goto out;
  17328. + a->btgt = err;
  17329. + if (err != bstart)
  17330. + au_fset_icpup(a->flags, DID_CPUP);
  17331. +
  17332. + err = 0;
  17333. + a->pin_flags = AuPin_MNT_WRITE;
  17334. + parent = NULL;
  17335. + if (!IS_ROOT(dentry)) {
  17336. + au_fset_pin(a->pin_flags, DI_LOCKED);
  17337. + parent = dget_parent(dentry);
  17338. + di_write_lock_parent(parent);
  17339. + }
  17340. +
  17341. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  17342. + if (unlikely(err))
  17343. + goto out_parent;
  17344. +
  17345. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  17346. + a->h_inode = a->h_path.dentry->d_inode;
  17347. + sz = -1;
  17348. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  17349. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  17350. + if (ia->ia_size < i_size_read(a->h_inode))
  17351. + sz = ia->ia_size;
  17352. + mutex_unlock(&a->h_inode->i_mutex);
  17353. + }
  17354. +
  17355. + hi_wh = NULL;
  17356. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  17357. + hi_wh = au_hi_wh(inode, a->btgt);
  17358. + if (!hi_wh) {
  17359. + struct au_cp_generic cpg = {
  17360. + .dentry = dentry,
  17361. + .bdst = a->btgt,
  17362. + .bsrc = -1,
  17363. + .len = sz,
  17364. + .pin = &a->pin
  17365. + };
  17366. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  17367. + if (unlikely(err))
  17368. + goto out_unlock;
  17369. + hi_wh = au_hi_wh(inode, a->btgt);
  17370. + /* todo: revalidate hi_wh? */
  17371. + }
  17372. + }
  17373. +
  17374. + if (parent) {
  17375. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  17376. + di_downgrade_lock(parent, AuLock_IR);
  17377. + dput(parent);
  17378. + parent = NULL;
  17379. + }
  17380. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  17381. + goto out; /* success */
  17382. +
  17383. + if (!d_unhashed(dentry)) {
  17384. + struct au_cp_generic cpg = {
  17385. + .dentry = dentry,
  17386. + .bdst = a->btgt,
  17387. + .bsrc = bstart,
  17388. + .len = sz,
  17389. + .pin = &a->pin,
  17390. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  17391. + };
  17392. + err = au_sio_cpup_simple(&cpg);
  17393. + if (!err)
  17394. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  17395. + } else if (!hi_wh)
  17396. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  17397. + else
  17398. + a->h_path.dentry = hi_wh; /* do not dget here */
  17399. +
  17400. +out_unlock:
  17401. + a->h_inode = a->h_path.dentry->d_inode;
  17402. + if (!err)
  17403. + goto out; /* success */
  17404. + au_unpin(&a->pin);
  17405. +out_parent:
  17406. + if (parent) {
  17407. + di_write_unlock(parent);
  17408. + dput(parent);
  17409. + }
  17410. +out:
  17411. + if (!err)
  17412. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  17413. + return err;
  17414. +}
  17415. +
  17416. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  17417. +{
  17418. + int err;
  17419. + struct inode *inode, *delegated;
  17420. + struct super_block *sb;
  17421. + struct file *file;
  17422. + struct au_icpup_args *a;
  17423. +
  17424. + inode = dentry->d_inode;
  17425. + IMustLock(inode);
  17426. +
  17427. + err = -ENOMEM;
  17428. + a = kzalloc(sizeof(*a), GFP_NOFS);
  17429. + if (unlikely(!a))
  17430. + goto out;
  17431. +
  17432. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  17433. + ia->ia_valid &= ~ATTR_MODE;
  17434. +
  17435. + file = NULL;
  17436. + sb = dentry->d_sb;
  17437. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  17438. + if (unlikely(err))
  17439. + goto out_kfree;
  17440. +
  17441. + if (ia->ia_valid & ATTR_FILE) {
  17442. + /* currently ftruncate(2) only */
  17443. + AuDebugOn(!d_is_reg(dentry));
  17444. + file = ia->ia_file;
  17445. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  17446. + if (unlikely(err))
  17447. + goto out_si;
  17448. + ia->ia_file = au_hf_top(file);
  17449. + a->udba = AuOpt_UDBA_NONE;
  17450. + } else {
  17451. + /* fchmod() doesn't pass ia_file */
  17452. + a->udba = au_opt_udba(sb);
  17453. + di_write_lock_child(dentry);
  17454. + /* no d_unlinked(), to set UDBA_NONE for root */
  17455. + if (d_unhashed(dentry))
  17456. + a->udba = AuOpt_UDBA_NONE;
  17457. + if (a->udba != AuOpt_UDBA_NONE) {
  17458. + AuDebugOn(IS_ROOT(dentry));
  17459. + err = au_reval_for_attr(dentry, au_sigen(sb));
  17460. + if (unlikely(err))
  17461. + goto out_dentry;
  17462. + }
  17463. + }
  17464. +
  17465. + err = au_pin_and_icpup(dentry, ia, a);
  17466. + if (unlikely(err < 0))
  17467. + goto out_dentry;
  17468. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  17469. + ia->ia_file = NULL;
  17470. + ia->ia_valid &= ~ATTR_FILE;
  17471. + }
  17472. +
  17473. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  17474. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  17475. + == (ATTR_MODE | ATTR_CTIME)) {
  17476. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  17477. + if (unlikely(err))
  17478. + goto out_unlock;
  17479. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  17480. + && (ia->ia_valid & ATTR_CTIME)) {
  17481. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  17482. + if (unlikely(err))
  17483. + goto out_unlock;
  17484. + }
  17485. +
  17486. + if (ia->ia_valid & ATTR_SIZE) {
  17487. + struct file *f;
  17488. +
  17489. + if (ia->ia_size < i_size_read(inode))
  17490. + /* unmap only */
  17491. + truncate_setsize(inode, ia->ia_size);
  17492. +
  17493. + f = NULL;
  17494. + if (ia->ia_valid & ATTR_FILE)
  17495. + f = ia->ia_file;
  17496. + mutex_unlock(&a->h_inode->i_mutex);
  17497. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  17498. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  17499. + } else {
  17500. + delegated = NULL;
  17501. + while (1) {
  17502. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  17503. + if (delegated) {
  17504. + err = break_deleg_wait(&delegated);
  17505. + if (!err)
  17506. + continue;
  17507. + }
  17508. + break;
  17509. + }
  17510. + }
  17511. + if (!err)
  17512. + au_cpup_attr_changeable(inode);
  17513. +
  17514. +out_unlock:
  17515. + mutex_unlock(&a->h_inode->i_mutex);
  17516. + au_unpin(&a->pin);
  17517. + if (unlikely(err))
  17518. + au_update_dbstart(dentry);
  17519. +out_dentry:
  17520. + di_write_unlock(dentry);
  17521. + if (file) {
  17522. + fi_write_unlock(file);
  17523. + ia->ia_file = file;
  17524. + ia->ia_valid |= ATTR_FILE;
  17525. + }
  17526. +out_si:
  17527. + si_read_unlock(sb);
  17528. +out_kfree:
  17529. + kfree(a);
  17530. +out:
  17531. + AuTraceErr(err);
  17532. + return err;
  17533. +}
  17534. +
  17535. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  17536. +static int au_h_path_to_set_attr(struct dentry *dentry,
  17537. + struct au_icpup_args *a, struct path *h_path)
  17538. +{
  17539. + int err;
  17540. + struct super_block *sb;
  17541. +
  17542. + sb = dentry->d_sb;
  17543. + a->udba = au_opt_udba(sb);
  17544. + /* no d_unlinked(), to set UDBA_NONE for root */
  17545. + if (d_unhashed(dentry))
  17546. + a->udba = AuOpt_UDBA_NONE;
  17547. + if (a->udba != AuOpt_UDBA_NONE) {
  17548. + AuDebugOn(IS_ROOT(dentry));
  17549. + err = au_reval_for_attr(dentry, au_sigen(sb));
  17550. + if (unlikely(err))
  17551. + goto out;
  17552. + }
  17553. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  17554. + if (unlikely(err < 0))
  17555. + goto out;
  17556. +
  17557. + h_path->dentry = a->h_path.dentry;
  17558. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  17559. +
  17560. +out:
  17561. + return err;
  17562. +}
  17563. +
  17564. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
  17565. +{
  17566. + int err;
  17567. + struct path h_path;
  17568. + struct super_block *sb;
  17569. + struct au_icpup_args *a;
  17570. + struct inode *inode, *h_inode;
  17571. +
  17572. + inode = dentry->d_inode;
  17573. + IMustLock(inode);
  17574. +
  17575. + err = -ENOMEM;
  17576. + a = kzalloc(sizeof(*a), GFP_NOFS);
  17577. + if (unlikely(!a))
  17578. + goto out;
  17579. +
  17580. + sb = dentry->d_sb;
  17581. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  17582. + if (unlikely(err))
  17583. + goto out_kfree;
  17584. +
  17585. + h_path.dentry = NULL; /* silence gcc */
  17586. + di_write_lock_child(dentry);
  17587. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  17588. + if (unlikely(err))
  17589. + goto out_di;
  17590. +
  17591. + mutex_unlock(&a->h_inode->i_mutex);
  17592. + switch (arg->type) {
  17593. + case AU_XATTR_SET:
  17594. + err = vfsub_setxattr(h_path.dentry,
  17595. + arg->u.set.name, arg->u.set.value,
  17596. + arg->u.set.size, arg->u.set.flags);
  17597. + break;
  17598. + case AU_XATTR_REMOVE:
  17599. + err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
  17600. + break;
  17601. + case AU_ACL_SET:
  17602. + err = -EOPNOTSUPP;
  17603. + h_inode = h_path.dentry->d_inode;
  17604. + if (h_inode->i_op->set_acl)
  17605. + err = h_inode->i_op->set_acl(h_inode,
  17606. + arg->u.acl_set.acl,
  17607. + arg->u.acl_set.type);
  17608. + break;
  17609. + }
  17610. + if (!err)
  17611. + au_cpup_attr_timesizes(inode);
  17612. +
  17613. + au_unpin(&a->pin);
  17614. + if (unlikely(err))
  17615. + au_update_dbstart(dentry);
  17616. +
  17617. +out_di:
  17618. + di_write_unlock(dentry);
  17619. + si_read_unlock(sb);
  17620. +out_kfree:
  17621. + kfree(a);
  17622. +out:
  17623. + AuTraceErr(err);
  17624. + return err;
  17625. +}
  17626. +#endif
  17627. +
  17628. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  17629. + unsigned int nlink)
  17630. +{
  17631. + unsigned int n;
  17632. +
  17633. + inode->i_mode = st->mode;
  17634. + /* don't i_[ug]id_write() here */
  17635. + inode->i_uid = st->uid;
  17636. + inode->i_gid = st->gid;
  17637. + inode->i_atime = st->atime;
  17638. + inode->i_mtime = st->mtime;
  17639. + inode->i_ctime = st->ctime;
  17640. +
  17641. + au_cpup_attr_nlink(inode, /*force*/0);
  17642. + if (S_ISDIR(inode->i_mode)) {
  17643. + n = inode->i_nlink;
  17644. + n -= nlink;
  17645. + n += st->nlink;
  17646. + smp_mb(); /* for i_nlink */
  17647. + /* 0 can happen */
  17648. + set_nlink(inode, n);
  17649. + }
  17650. +
  17651. + spin_lock(&inode->i_lock);
  17652. + inode->i_blocks = st->blocks;
  17653. + i_size_write(inode, st->size);
  17654. + spin_unlock(&inode->i_lock);
  17655. +}
  17656. +
  17657. +/*
  17658. + * common routine for aufs_getattr() and aufs_getxattr().
  17659. + * returns zero or negative (an error).
  17660. + * @dentry will be read-locked in success.
  17661. + */
  17662. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
  17663. +{
  17664. + int err;
  17665. + unsigned int mnt_flags, sigen;
  17666. + unsigned char udba_none;
  17667. + aufs_bindex_t bindex;
  17668. + struct super_block *sb, *h_sb;
  17669. + struct inode *inode;
  17670. +
  17671. + h_path->mnt = NULL;
  17672. + h_path->dentry = NULL;
  17673. +
  17674. + err = 0;
  17675. + sb = dentry->d_sb;
  17676. + mnt_flags = au_mntflags(sb);
  17677. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  17678. +
  17679. + /* support fstat(2) */
  17680. + if (!d_unlinked(dentry) && !udba_none) {
  17681. + sigen = au_sigen(sb);
  17682. + err = au_digen_test(dentry, sigen);
  17683. + if (!err) {
  17684. + di_read_lock_child(dentry, AuLock_IR);
  17685. + err = au_dbrange_test(dentry);
  17686. + if (unlikely(err)) {
  17687. + di_read_unlock(dentry, AuLock_IR);
  17688. + goto out;
  17689. + }
  17690. + } else {
  17691. + AuDebugOn(IS_ROOT(dentry));
  17692. + di_write_lock_child(dentry);
  17693. + err = au_dbrange_test(dentry);
  17694. + if (!err)
  17695. + err = au_reval_for_attr(dentry, sigen);
  17696. + if (!err)
  17697. + di_downgrade_lock(dentry, AuLock_IR);
  17698. + else {
  17699. + di_write_unlock(dentry);
  17700. + goto out;
  17701. + }
  17702. + }
  17703. + } else
  17704. + di_read_lock_child(dentry, AuLock_IR);
  17705. +
  17706. + inode = dentry->d_inode;
  17707. + bindex = au_ibstart(inode);
  17708. + h_path->mnt = au_sbr_mnt(sb, bindex);
  17709. + h_sb = h_path->mnt->mnt_sb;
  17710. + if (!force
  17711. + && !au_test_fs_bad_iattr(h_sb)
  17712. + && udba_none)
  17713. + goto out; /* success */
  17714. +
  17715. + if (au_dbstart(dentry) == bindex)
  17716. + h_path->dentry = au_h_dptr(dentry, bindex);
  17717. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  17718. + h_path->dentry = au_plink_lkup(inode, bindex);
  17719. + if (IS_ERR(h_path->dentry))
  17720. + /* pretending success */
  17721. + h_path->dentry = NULL;
  17722. + else
  17723. + dput(h_path->dentry);
  17724. + }
  17725. +
  17726. +out:
  17727. + return err;
  17728. +}
  17729. +
  17730. +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
  17731. + struct dentry *dentry, struct kstat *st)
  17732. +{
  17733. + int err;
  17734. + unsigned char positive;
  17735. + struct path h_path;
  17736. + struct inode *inode;
  17737. + struct super_block *sb;
  17738. +
  17739. + inode = dentry->d_inode;
  17740. + sb = dentry->d_sb;
  17741. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  17742. + if (unlikely(err))
  17743. + goto out;
  17744. + err = au_h_path_getattr(dentry, /*force*/0, &h_path);
  17745. + if (unlikely(err))
  17746. + goto out_si;
  17747. + if (unlikely(!h_path.dentry))
  17748. + /* illegally overlapped or something */
  17749. + goto out_fill; /* pretending success */
  17750. +
  17751. + positive = !!h_path.dentry->d_inode;
  17752. + if (positive)
  17753. + err = vfs_getattr(&h_path, st);
  17754. + if (!err) {
  17755. + if (positive)
  17756. + au_refresh_iattr(inode, st,
  17757. + h_path.dentry->d_inode->i_nlink);
  17758. + goto out_fill; /* success */
  17759. + }
  17760. + AuTraceErr(err);
  17761. + goto out_di;
  17762. +
  17763. +out_fill:
  17764. + generic_fillattr(inode, st);
  17765. +out_di:
  17766. + di_read_unlock(dentry, AuLock_IR);
  17767. +out_si:
  17768. + si_read_unlock(sb);
  17769. +out:
  17770. + AuTraceErr(err);
  17771. + return err;
  17772. +}
  17773. +
  17774. +/* ---------------------------------------------------------------------- */
  17775. +
  17776. +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
  17777. + int bufsiz)
  17778. +{
  17779. + int err;
  17780. + struct super_block *sb;
  17781. + struct dentry *h_dentry;
  17782. +
  17783. + err = -EINVAL;
  17784. + h_dentry = au_h_dptr(dentry, bindex);
  17785. + if (unlikely(!h_dentry->d_inode->i_op->readlink))
  17786. + goto out;
  17787. +
  17788. + err = security_inode_readlink(h_dentry);
  17789. + if (unlikely(err))
  17790. + goto out;
  17791. +
  17792. + sb = dentry->d_sb;
  17793. + if (!au_test_ro(sb, bindex, dentry->d_inode)) {
  17794. + vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
  17795. + fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
  17796. + }
  17797. + err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
  17798. +
  17799. +out:
  17800. + return err;
  17801. +}
  17802. +
  17803. +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  17804. +{
  17805. + int err;
  17806. +
  17807. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  17808. + if (unlikely(err))
  17809. + goto out;
  17810. + err = au_d_hashed_positive(dentry);
  17811. + if (!err)
  17812. + err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
  17813. + aufs_read_unlock(dentry, AuLock_IR);
  17814. +
  17815. +out:
  17816. + return err;
  17817. +}
  17818. +
  17819. +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
  17820. +{
  17821. + int err;
  17822. + mm_segment_t old_fs;
  17823. + union {
  17824. + char *k;
  17825. + char __user *u;
  17826. + } buf;
  17827. +
  17828. + err = -ENOMEM;
  17829. + buf.k = (void *)__get_free_page(GFP_NOFS);
  17830. + if (unlikely(!buf.k))
  17831. + goto out;
  17832. +
  17833. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  17834. + if (unlikely(err))
  17835. + goto out_name;
  17836. +
  17837. + err = au_d_hashed_positive(dentry);
  17838. + if (!err) {
  17839. + old_fs = get_fs();
  17840. + set_fs(KERNEL_DS);
  17841. + err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
  17842. + set_fs(old_fs);
  17843. + }
  17844. + aufs_read_unlock(dentry, AuLock_IR);
  17845. +
  17846. + if (err >= 0) {
  17847. + buf.k[err] = 0;
  17848. + /* will be freed by put_link */
  17849. + nd_set_link(nd, buf.k);
  17850. + return NULL; /* success */
  17851. + }
  17852. +
  17853. +out_name:
  17854. + free_page((unsigned long)buf.k);
  17855. +out:
  17856. + AuTraceErr(err);
  17857. + return ERR_PTR(err);
  17858. +}
  17859. +
  17860. +static void aufs_put_link(struct dentry *dentry __maybe_unused,
  17861. + struct nameidata *nd, void *cookie __maybe_unused)
  17862. +{
  17863. + char *p;
  17864. +
  17865. + p = nd_get_link(nd);
  17866. + if (!IS_ERR_OR_NULL(p))
  17867. + free_page((unsigned long)p);
  17868. +}
  17869. +
  17870. +/* ---------------------------------------------------------------------- */
  17871. +
  17872. +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
  17873. +{
  17874. + int err;
  17875. + struct super_block *sb;
  17876. + struct inode *h_inode;
  17877. +
  17878. + sb = inode->i_sb;
  17879. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  17880. + lockdep_off();
  17881. + si_read_lock(sb, AuLock_FLUSH);
  17882. + ii_write_lock_child(inode);
  17883. + lockdep_on();
  17884. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  17885. + err = vfsub_update_time(h_inode, ts, flags);
  17886. + lockdep_off();
  17887. + if (!err)
  17888. + au_cpup_attr_timesizes(inode);
  17889. + ii_write_unlock(inode);
  17890. + si_read_unlock(sb);
  17891. + lockdep_on();
  17892. +
  17893. + if (!err && (flags & S_VERSION))
  17894. + inode_inc_iversion(inode);
  17895. +
  17896. + return err;
  17897. +}
  17898. +
  17899. +/* ---------------------------------------------------------------------- */
  17900. +
  17901. +struct inode_operations aufs_symlink_iop = {
  17902. + .permission = aufs_permission,
  17903. +#ifdef CONFIG_FS_POSIX_ACL
  17904. + .get_acl = aufs_get_acl,
  17905. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  17906. +#endif
  17907. +
  17908. + .setattr = aufs_setattr,
  17909. + .getattr = aufs_getattr,
  17910. +
  17911. +#ifdef CONFIG_AUFS_XATTR
  17912. + .setxattr = aufs_setxattr,
  17913. + .getxattr = aufs_getxattr,
  17914. + .listxattr = aufs_listxattr,
  17915. + .removexattr = aufs_removexattr,
  17916. +#endif
  17917. +
  17918. + .readlink = aufs_readlink,
  17919. + .follow_link = aufs_follow_link,
  17920. + .put_link = aufs_put_link,
  17921. +
  17922. + /* .update_time = aufs_update_time */
  17923. +};
  17924. +
  17925. +struct inode_operations aufs_dir_iop = {
  17926. + .create = aufs_create,
  17927. + .lookup = aufs_lookup,
  17928. + .link = aufs_link,
  17929. + .unlink = aufs_unlink,
  17930. + .symlink = aufs_symlink,
  17931. + .mkdir = aufs_mkdir,
  17932. + .rmdir = aufs_rmdir,
  17933. + .mknod = aufs_mknod,
  17934. + .rename = aufs_rename,
  17935. +
  17936. + .permission = aufs_permission,
  17937. +#ifdef CONFIG_FS_POSIX_ACL
  17938. + .get_acl = aufs_get_acl,
  17939. + .set_acl = aufs_set_acl,
  17940. +#endif
  17941. +
  17942. + .setattr = aufs_setattr,
  17943. + .getattr = aufs_getattr,
  17944. +
  17945. +#ifdef CONFIG_AUFS_XATTR
  17946. + .setxattr = aufs_setxattr,
  17947. + .getxattr = aufs_getxattr,
  17948. + .listxattr = aufs_listxattr,
  17949. + .removexattr = aufs_removexattr,
  17950. +#endif
  17951. +
  17952. + .update_time = aufs_update_time,
  17953. + .atomic_open = aufs_atomic_open,
  17954. + .tmpfile = aufs_tmpfile
  17955. +};
  17956. +
  17957. +struct inode_operations aufs_iop = {
  17958. + .permission = aufs_permission,
  17959. +#ifdef CONFIG_FS_POSIX_ACL
  17960. + .get_acl = aufs_get_acl,
  17961. + .set_acl = aufs_set_acl,
  17962. +#endif
  17963. +
  17964. + .setattr = aufs_setattr,
  17965. + .getattr = aufs_getattr,
  17966. +
  17967. +#ifdef CONFIG_AUFS_XATTR
  17968. + .setxattr = aufs_setxattr,
  17969. + .getxattr = aufs_getxattr,
  17970. + .listxattr = aufs_listxattr,
  17971. + .removexattr = aufs_removexattr,
  17972. +#endif
  17973. +
  17974. + .update_time = aufs_update_time
  17975. +};
  17976. diff -Nur linux-4.0.4.orig/fs/aufs/i_op_del.c linux-4.0.4/fs/aufs/i_op_del.c
  17977. --- linux-4.0.4.orig/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
  17978. +++ linux-4.0.4/fs/aufs/i_op_del.c 2015-05-30 22:11:31.000000000 +0200
  17979. @@ -0,0 +1,493 @@
  17980. +/*
  17981. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  17982. + */
  17983. +
  17984. +/*
  17985. + * inode operations (del entry)
  17986. + */
  17987. +
  17988. +#include "aufs.h"
  17989. +
  17990. +/*
  17991. + * decide if a new whiteout for @dentry is necessary or not.
  17992. + * when it is necessary, prepare the parent dir for the upper branch whose
  17993. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  17994. + * be done by caller.
  17995. + * return value:
  17996. + * 0: wh is unnecessary
  17997. + * plus: wh is necessary
  17998. + * minus: error
  17999. + */
  18000. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  18001. +{
  18002. + int need_wh, err;
  18003. + aufs_bindex_t bstart;
  18004. + struct super_block *sb;
  18005. +
  18006. + sb = dentry->d_sb;
  18007. + bstart = au_dbstart(dentry);
  18008. + if (*bcpup < 0) {
  18009. + *bcpup = bstart;
  18010. + if (au_test_ro(sb, bstart, dentry->d_inode)) {
  18011. + err = AuWbrCopyup(au_sbi(sb), dentry);
  18012. + *bcpup = err;
  18013. + if (unlikely(err < 0))
  18014. + goto out;
  18015. + }
  18016. + } else
  18017. + AuDebugOn(bstart < *bcpup
  18018. + || au_test_ro(sb, *bcpup, dentry->d_inode));
  18019. + AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
  18020. +
  18021. + if (*bcpup != bstart) {
  18022. + err = au_cpup_dirs(dentry, *bcpup);
  18023. + if (unlikely(err))
  18024. + goto out;
  18025. + need_wh = 1;
  18026. + } else {
  18027. + struct au_dinfo *dinfo, *tmp;
  18028. +
  18029. + need_wh = -ENOMEM;
  18030. + dinfo = au_di(dentry);
  18031. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  18032. + if (tmp) {
  18033. + au_di_cp(tmp, dinfo);
  18034. + au_di_swap(tmp, dinfo);
  18035. + /* returns the number of positive dentries */
  18036. + need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
  18037. + au_di_swap(tmp, dinfo);
  18038. + au_rw_write_unlock(&tmp->di_rwsem);
  18039. + au_di_free(tmp);
  18040. + }
  18041. + }
  18042. + AuDbg("need_wh %d\n", need_wh);
  18043. + err = need_wh;
  18044. +
  18045. +out:
  18046. + return err;
  18047. +}
  18048. +
  18049. +/*
  18050. + * simple tests for the del-entry operations.
  18051. + * following the checks in vfs, plus the parent-child relationship.
  18052. + */
  18053. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  18054. + struct dentry *h_parent, int isdir)
  18055. +{
  18056. + int err;
  18057. + umode_t h_mode;
  18058. + struct dentry *h_dentry, *h_latest;
  18059. + struct inode *h_inode;
  18060. +
  18061. + h_dentry = au_h_dptr(dentry, bindex);
  18062. + h_inode = h_dentry->d_inode;
  18063. + if (dentry->d_inode) {
  18064. + err = -ENOENT;
  18065. + if (unlikely(!h_inode || !h_inode->i_nlink))
  18066. + goto out;
  18067. +
  18068. + h_mode = h_inode->i_mode;
  18069. + if (!isdir) {
  18070. + err = -EISDIR;
  18071. + if (unlikely(S_ISDIR(h_mode)))
  18072. + goto out;
  18073. + } else if (unlikely(!S_ISDIR(h_mode))) {
  18074. + err = -ENOTDIR;
  18075. + goto out;
  18076. + }
  18077. + } else {
  18078. + /* rename(2) case */
  18079. + err = -EIO;
  18080. + if (unlikely(h_inode))
  18081. + goto out;
  18082. + }
  18083. +
  18084. + err = -ENOENT;
  18085. + /* expected parent dir is locked */
  18086. + if (unlikely(h_parent != h_dentry->d_parent))
  18087. + goto out;
  18088. + err = 0;
  18089. +
  18090. + /*
  18091. + * rmdir a dir may break the consistency on some filesystem.
  18092. + * let's try heavy test.
  18093. + */
  18094. + err = -EACCES;
  18095. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  18096. + && au_test_h_perm(h_parent->d_inode,
  18097. + MAY_EXEC | MAY_WRITE)))
  18098. + goto out;
  18099. +
  18100. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  18101. + err = -EIO;
  18102. + if (IS_ERR(h_latest))
  18103. + goto out;
  18104. + if (h_latest == h_dentry)
  18105. + err = 0;
  18106. + dput(h_latest);
  18107. +
  18108. +out:
  18109. + return err;
  18110. +}
  18111. +
  18112. +/*
  18113. + * decide the branch where we operate for @dentry. the branch index will be set
  18114. + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
  18115. + * dir for reverting.
  18116. + * when a new whiteout is necessary, create it.
  18117. + */
  18118. +static struct dentry*
  18119. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  18120. + struct au_dtime *dt, struct au_pin *pin)
  18121. +{
  18122. + struct dentry *wh_dentry;
  18123. + struct super_block *sb;
  18124. + struct path h_path;
  18125. + int err, need_wh;
  18126. + unsigned int udba;
  18127. + aufs_bindex_t bcpup;
  18128. +
  18129. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  18130. + wh_dentry = ERR_PTR(need_wh);
  18131. + if (unlikely(need_wh < 0))
  18132. + goto out;
  18133. +
  18134. + sb = dentry->d_sb;
  18135. + udba = au_opt_udba(sb);
  18136. + bcpup = *rbcpup;
  18137. + err = au_pin(pin, dentry, bcpup, udba,
  18138. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  18139. + wh_dentry = ERR_PTR(err);
  18140. + if (unlikely(err))
  18141. + goto out;
  18142. +
  18143. + h_path.dentry = au_pinned_h_parent(pin);
  18144. + if (udba != AuOpt_UDBA_NONE
  18145. + && au_dbstart(dentry) == bcpup) {
  18146. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  18147. + wh_dentry = ERR_PTR(err);
  18148. + if (unlikely(err))
  18149. + goto out_unpin;
  18150. + }
  18151. +
  18152. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  18153. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  18154. + wh_dentry = NULL;
  18155. + if (!need_wh)
  18156. + goto out; /* success, no need to create whiteout */
  18157. +
  18158. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  18159. + if (IS_ERR(wh_dentry))
  18160. + goto out_unpin;
  18161. +
  18162. + /* returns with the parent is locked and wh_dentry is dget-ed */
  18163. + goto out; /* success */
  18164. +
  18165. +out_unpin:
  18166. + au_unpin(pin);
  18167. +out:
  18168. + return wh_dentry;
  18169. +}
  18170. +
  18171. +/*
  18172. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  18173. + * in order to be revertible and save time for removing many child whiteouts
  18174. + * under the dir.
  18175. + * returns 1 when there are too many child whiteout and caller should remove
  18176. + * them asynchronously. returns 0 when the number of children is enough small to
  18177. + * remove now or the branch fs is a remote fs.
  18178. + * otherwise return an error.
  18179. + */
  18180. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  18181. + struct au_nhash *whlist, struct inode *dir)
  18182. +{
  18183. + int rmdir_later, err, dirwh;
  18184. + struct dentry *h_dentry;
  18185. + struct super_block *sb;
  18186. +
  18187. + sb = dentry->d_sb;
  18188. + SiMustAnyLock(sb);
  18189. + h_dentry = au_h_dptr(dentry, bindex);
  18190. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  18191. + if (unlikely(err))
  18192. + goto out;
  18193. +
  18194. + /* stop monitoring */
  18195. + au_hn_free(au_hi(dentry->d_inode, bindex));
  18196. +
  18197. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  18198. + dirwh = au_sbi(sb)->si_dirwh;
  18199. + rmdir_later = (dirwh <= 1);
  18200. + if (!rmdir_later)
  18201. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  18202. + dirwh);
  18203. + if (rmdir_later)
  18204. + return rmdir_later;
  18205. + }
  18206. +
  18207. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  18208. + if (unlikely(err)) {
  18209. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  18210. + h_dentry, bindex, err);
  18211. + err = 0;
  18212. + }
  18213. +
  18214. +out:
  18215. + AuTraceErr(err);
  18216. + return err;
  18217. +}
  18218. +
  18219. +/*
  18220. + * final procedure for deleting a entry.
  18221. + * maintain dentry and iattr.
  18222. + */
  18223. +static void epilog(struct inode *dir, struct dentry *dentry,
  18224. + aufs_bindex_t bindex)
  18225. +{
  18226. + struct inode *inode;
  18227. +
  18228. + inode = dentry->d_inode;
  18229. + d_drop(dentry);
  18230. + inode->i_ctime = dir->i_ctime;
  18231. +
  18232. + au_dir_ts(dir, bindex);
  18233. + dir->i_version++;
  18234. +}
  18235. +
  18236. +/*
  18237. + * when an error happened, remove the created whiteout and revert everything.
  18238. + */
  18239. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  18240. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  18241. + struct dentry *dentry, struct au_dtime *dt)
  18242. +{
  18243. + int rerr;
  18244. + struct path h_path = {
  18245. + .dentry = wh_dentry,
  18246. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  18247. + };
  18248. +
  18249. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  18250. + if (!rerr) {
  18251. + au_set_dbwh(dentry, bwh);
  18252. + au_dtime_revert(dt);
  18253. + return 0;
  18254. + }
  18255. +
  18256. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  18257. + return -EIO;
  18258. +}
  18259. +
  18260. +/* ---------------------------------------------------------------------- */
  18261. +
  18262. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  18263. +{
  18264. + int err;
  18265. + aufs_bindex_t bwh, bindex, bstart;
  18266. + struct inode *inode, *h_dir, *delegated;
  18267. + struct dentry *parent, *wh_dentry;
  18268. + /* to reuduce stack size */
  18269. + struct {
  18270. + struct au_dtime dt;
  18271. + struct au_pin pin;
  18272. + struct path h_path;
  18273. + } *a;
  18274. +
  18275. + IMustLock(dir);
  18276. +
  18277. + err = -ENOMEM;
  18278. + a = kmalloc(sizeof(*a), GFP_NOFS);
  18279. + if (unlikely(!a))
  18280. + goto out;
  18281. +
  18282. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  18283. + if (unlikely(err))
  18284. + goto out_free;
  18285. + err = au_d_hashed_positive(dentry);
  18286. + if (unlikely(err))
  18287. + goto out_unlock;
  18288. + inode = dentry->d_inode;
  18289. + IMustLock(inode);
  18290. + err = -EISDIR;
  18291. + if (unlikely(d_is_dir(dentry)))
  18292. + goto out_unlock; /* possible? */
  18293. +
  18294. + bstart = au_dbstart(dentry);
  18295. + bwh = au_dbwh(dentry);
  18296. + bindex = -1;
  18297. + parent = dentry->d_parent; /* dir inode is locked */
  18298. + di_write_lock_parent(parent);
  18299. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  18300. + &a->pin);
  18301. + err = PTR_ERR(wh_dentry);
  18302. + if (IS_ERR(wh_dentry))
  18303. + goto out_parent;
  18304. +
  18305. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
  18306. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  18307. + dget(a->h_path.dentry);
  18308. + if (bindex == bstart) {
  18309. + h_dir = au_pinned_h_dir(&a->pin);
  18310. + delegated = NULL;
  18311. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  18312. + if (unlikely(err == -EWOULDBLOCK)) {
  18313. + pr_warn("cannot retry for NFSv4 delegation"
  18314. + " for an internal unlink\n");
  18315. + iput(delegated);
  18316. + }
  18317. + } else {
  18318. + /* dir inode is locked */
  18319. + h_dir = wh_dentry->d_parent->d_inode;
  18320. + IMustLock(h_dir);
  18321. + err = 0;
  18322. + }
  18323. +
  18324. + if (!err) {
  18325. + vfsub_drop_nlink(inode);
  18326. + epilog(dir, dentry, bindex);
  18327. +
  18328. + /* update target timestamps */
  18329. + if (bindex == bstart) {
  18330. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  18331. + /*ignore*/
  18332. + inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
  18333. + } else
  18334. + /* todo: this timestamp may be reverted later */
  18335. + inode->i_ctime = h_dir->i_ctime;
  18336. + goto out_unpin; /* success */
  18337. + }
  18338. +
  18339. + /* revert */
  18340. + if (wh_dentry) {
  18341. + int rerr;
  18342. +
  18343. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  18344. + &a->dt);
  18345. + if (rerr)
  18346. + err = rerr;
  18347. + }
  18348. +
  18349. +out_unpin:
  18350. + au_unpin(&a->pin);
  18351. + dput(wh_dentry);
  18352. + dput(a->h_path.dentry);
  18353. +out_parent:
  18354. + di_write_unlock(parent);
  18355. +out_unlock:
  18356. + aufs_read_unlock(dentry, AuLock_DW);
  18357. +out_free:
  18358. + kfree(a);
  18359. +out:
  18360. + return err;
  18361. +}
  18362. +
  18363. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  18364. +{
  18365. + int err, rmdir_later;
  18366. + aufs_bindex_t bwh, bindex, bstart;
  18367. + struct inode *inode;
  18368. + struct dentry *parent, *wh_dentry, *h_dentry;
  18369. + struct au_whtmp_rmdir *args;
  18370. + /* to reuduce stack size */
  18371. + struct {
  18372. + struct au_dtime dt;
  18373. + struct au_pin pin;
  18374. + } *a;
  18375. +
  18376. + IMustLock(dir);
  18377. +
  18378. + err = -ENOMEM;
  18379. + a = kmalloc(sizeof(*a), GFP_NOFS);
  18380. + if (unlikely(!a))
  18381. + goto out;
  18382. +
  18383. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  18384. + if (unlikely(err))
  18385. + goto out_free;
  18386. + err = au_alive_dir(dentry);
  18387. + if (unlikely(err))
  18388. + goto out_unlock;
  18389. + inode = dentry->d_inode;
  18390. + IMustLock(inode);
  18391. + err = -ENOTDIR;
  18392. + if (unlikely(!d_is_dir(dentry)))
  18393. + goto out_unlock; /* possible? */
  18394. +
  18395. + err = -ENOMEM;
  18396. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  18397. + if (unlikely(!args))
  18398. + goto out_unlock;
  18399. +
  18400. + parent = dentry->d_parent; /* dir inode is locked */
  18401. + di_write_lock_parent(parent);
  18402. + err = au_test_empty(dentry, &args->whlist);
  18403. + if (unlikely(err))
  18404. + goto out_parent;
  18405. +
  18406. + bstart = au_dbstart(dentry);
  18407. + bwh = au_dbwh(dentry);
  18408. + bindex = -1;
  18409. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  18410. + &a->pin);
  18411. + err = PTR_ERR(wh_dentry);
  18412. + if (IS_ERR(wh_dentry))
  18413. + goto out_parent;
  18414. +
  18415. + h_dentry = au_h_dptr(dentry, bstart);
  18416. + dget(h_dentry);
  18417. + rmdir_later = 0;
  18418. + if (bindex == bstart) {
  18419. + err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
  18420. + if (err > 0) {
  18421. + rmdir_later = err;
  18422. + err = 0;
  18423. + }
  18424. + } else {
  18425. + /* stop monitoring */
  18426. + au_hn_free(au_hi(inode, bstart));
  18427. +
  18428. + /* dir inode is locked */
  18429. + IMustLock(wh_dentry->d_parent->d_inode);
  18430. + err = 0;
  18431. + }
  18432. +
  18433. + if (!err) {
  18434. + vfsub_dead_dir(inode);
  18435. + au_set_dbdiropq(dentry, -1);
  18436. + epilog(dir, dentry, bindex);
  18437. +
  18438. + if (rmdir_later) {
  18439. + au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
  18440. + args = NULL;
  18441. + }
  18442. +
  18443. + goto out_unpin; /* success */
  18444. + }
  18445. +
  18446. + /* revert */
  18447. + AuLabel(revert);
  18448. + if (wh_dentry) {
  18449. + int rerr;
  18450. +
  18451. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  18452. + &a->dt);
  18453. + if (rerr)
  18454. + err = rerr;
  18455. + }
  18456. +
  18457. +out_unpin:
  18458. + au_unpin(&a->pin);
  18459. + dput(wh_dentry);
  18460. + dput(h_dentry);
  18461. +out_parent:
  18462. + di_write_unlock(parent);
  18463. + if (args)
  18464. + au_whtmp_rmdir_free(args);
  18465. +out_unlock:
  18466. + aufs_read_unlock(dentry, AuLock_DW);
  18467. +out_free:
  18468. + kfree(a);
  18469. +out:
  18470. + AuTraceErr(err);
  18471. + return err;
  18472. +}
  18473. diff -Nur linux-4.0.4.orig/fs/aufs/i_op_ren.c linux-4.0.4/fs/aufs/i_op_ren.c
  18474. --- linux-4.0.4.orig/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
  18475. +++ linux-4.0.4/fs/aufs/i_op_ren.c 2015-05-30 22:11:31.000000000 +0200
  18476. @@ -0,0 +1,1002 @@
  18477. +/*
  18478. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  18479. + */
  18480. +
  18481. +/*
  18482. + * inode operation (rename entry)
  18483. + * todo: this is crazy monster
  18484. + */
  18485. +
  18486. +#include "aufs.h"
  18487. +
  18488. +enum { AuSRC, AuDST, AuSrcDst };
  18489. +enum { AuPARENT, AuCHILD, AuParentChild };
  18490. +
  18491. +#define AuRen_ISDIR 1
  18492. +#define AuRen_ISSAMEDIR (1 << 1)
  18493. +#define AuRen_WHSRC (1 << 2)
  18494. +#define AuRen_WHDST (1 << 3)
  18495. +#define AuRen_MNT_WRITE (1 << 4)
  18496. +#define AuRen_DT_DSTDIR (1 << 5)
  18497. +#define AuRen_DIROPQ (1 << 6)
  18498. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  18499. +#define au_fset_ren(flags, name) \
  18500. + do { (flags) |= AuRen_##name; } while (0)
  18501. +#define au_fclr_ren(flags, name) \
  18502. + do { (flags) &= ~AuRen_##name; } while (0)
  18503. +
  18504. +struct au_ren_args {
  18505. + struct {
  18506. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  18507. + *wh_dentry;
  18508. + struct inode *dir, *inode;
  18509. + struct au_hinode *hdir;
  18510. + struct au_dtime dt[AuParentChild];
  18511. + aufs_bindex_t bstart;
  18512. + } sd[AuSrcDst];
  18513. +
  18514. +#define src_dentry sd[AuSRC].dentry
  18515. +#define src_dir sd[AuSRC].dir
  18516. +#define src_inode sd[AuSRC].inode
  18517. +#define src_h_dentry sd[AuSRC].h_dentry
  18518. +#define src_parent sd[AuSRC].parent
  18519. +#define src_h_parent sd[AuSRC].h_parent
  18520. +#define src_wh_dentry sd[AuSRC].wh_dentry
  18521. +#define src_hdir sd[AuSRC].hdir
  18522. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  18523. +#define src_dt sd[AuSRC].dt
  18524. +#define src_bstart sd[AuSRC].bstart
  18525. +
  18526. +#define dst_dentry sd[AuDST].dentry
  18527. +#define dst_dir sd[AuDST].dir
  18528. +#define dst_inode sd[AuDST].inode
  18529. +#define dst_h_dentry sd[AuDST].h_dentry
  18530. +#define dst_parent sd[AuDST].parent
  18531. +#define dst_h_parent sd[AuDST].h_parent
  18532. +#define dst_wh_dentry sd[AuDST].wh_dentry
  18533. +#define dst_hdir sd[AuDST].hdir
  18534. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  18535. +#define dst_dt sd[AuDST].dt
  18536. +#define dst_bstart sd[AuDST].bstart
  18537. +
  18538. + struct dentry *h_trap;
  18539. + struct au_branch *br;
  18540. + struct au_hinode *src_hinode;
  18541. + struct path h_path;
  18542. + struct au_nhash whlist;
  18543. + aufs_bindex_t btgt, src_bwh, src_bdiropq;
  18544. +
  18545. + unsigned int flags;
  18546. +
  18547. + struct au_whtmp_rmdir *thargs;
  18548. + struct dentry *h_dst;
  18549. +};
  18550. +
  18551. +/* ---------------------------------------------------------------------- */
  18552. +
  18553. +/*
  18554. + * functions for reverting.
  18555. + * when an error happened in a single rename systemcall, we should revert
  18556. + * everything as if nothing happend.
  18557. + * we don't need to revert the copied-up/down the parent dir since they are
  18558. + * harmless.
  18559. + */
  18560. +
  18561. +#define RevertFailure(fmt, ...) do { \
  18562. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  18563. + ##__VA_ARGS__, err, rerr); \
  18564. + err = -EIO; \
  18565. +} while (0)
  18566. +
  18567. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  18568. +{
  18569. + int rerr;
  18570. +
  18571. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  18572. + rerr = au_diropq_remove(a->src_dentry, a->btgt);
  18573. + au_hn_imtx_unlock(a->src_hinode);
  18574. + au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
  18575. + if (rerr)
  18576. + RevertFailure("remove diropq %pd", a->src_dentry);
  18577. +}
  18578. +
  18579. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  18580. +{
  18581. + int rerr;
  18582. + struct inode *delegated;
  18583. +
  18584. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  18585. + a->src_h_parent);
  18586. + rerr = PTR_ERR(a->h_path.dentry);
  18587. + if (IS_ERR(a->h_path.dentry)) {
  18588. + RevertFailure("lkup one %pd", a->src_dentry);
  18589. + return;
  18590. + }
  18591. +
  18592. + delegated = NULL;
  18593. + rerr = vfsub_rename(a->dst_h_dir,
  18594. + au_h_dptr(a->src_dentry, a->btgt),
  18595. + a->src_h_dir, &a->h_path, &delegated);
  18596. + if (unlikely(rerr == -EWOULDBLOCK)) {
  18597. + pr_warn("cannot retry for NFSv4 delegation"
  18598. + " for an internal rename\n");
  18599. + iput(delegated);
  18600. + }
  18601. + d_drop(a->h_path.dentry);
  18602. + dput(a->h_path.dentry);
  18603. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  18604. + if (rerr)
  18605. + RevertFailure("rename %pd", a->src_dentry);
  18606. +}
  18607. +
  18608. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  18609. +{
  18610. + int rerr;
  18611. + struct inode *delegated;
  18612. +
  18613. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  18614. + a->dst_h_parent);
  18615. + rerr = PTR_ERR(a->h_path.dentry);
  18616. + if (IS_ERR(a->h_path.dentry)) {
  18617. + RevertFailure("lkup one %pd", a->dst_dentry);
  18618. + return;
  18619. + }
  18620. + if (a->h_path.dentry->d_inode) {
  18621. + d_drop(a->h_path.dentry);
  18622. + dput(a->h_path.dentry);
  18623. + return;
  18624. + }
  18625. +
  18626. + delegated = NULL;
  18627. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  18628. + &delegated);
  18629. + if (unlikely(rerr == -EWOULDBLOCK)) {
  18630. + pr_warn("cannot retry for NFSv4 delegation"
  18631. + " for an internal rename\n");
  18632. + iput(delegated);
  18633. + }
  18634. + d_drop(a->h_path.dentry);
  18635. + dput(a->h_path.dentry);
  18636. + if (!rerr)
  18637. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  18638. + else
  18639. + RevertFailure("rename %pd", a->h_dst);
  18640. +}
  18641. +
  18642. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  18643. +{
  18644. + int rerr;
  18645. +
  18646. + a->h_path.dentry = a->src_wh_dentry;
  18647. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  18648. + au_set_dbwh(a->src_dentry, a->src_bwh);
  18649. + if (rerr)
  18650. + RevertFailure("unlink %pd", a->src_wh_dentry);
  18651. +}
  18652. +#undef RevertFailure
  18653. +
  18654. +/* ---------------------------------------------------------------------- */
  18655. +
  18656. +/*
  18657. + * when we have to copyup the renaming entry, do it with the rename-target name
  18658. + * in order to minimize the cost (the later actual rename is unnecessary).
  18659. + * otherwise rename it on the target branch.
  18660. + */
  18661. +static int au_ren_or_cpup(struct au_ren_args *a)
  18662. +{
  18663. + int err;
  18664. + struct dentry *d;
  18665. + struct inode *delegated;
  18666. +
  18667. + d = a->src_dentry;
  18668. + if (au_dbstart(d) == a->btgt) {
  18669. + a->h_path.dentry = a->dst_h_dentry;
  18670. + if (au_ftest_ren(a->flags, DIROPQ)
  18671. + && au_dbdiropq(d) == a->btgt)
  18672. + au_fclr_ren(a->flags, DIROPQ);
  18673. + AuDebugOn(au_dbstart(d) != a->btgt);
  18674. + delegated = NULL;
  18675. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  18676. + a->dst_h_dir, &a->h_path, &delegated);
  18677. + if (unlikely(err == -EWOULDBLOCK)) {
  18678. + pr_warn("cannot retry for NFSv4 delegation"
  18679. + " for an internal rename\n");
  18680. + iput(delegated);
  18681. + }
  18682. + } else
  18683. + BUG();
  18684. +
  18685. + if (!err && a->h_dst)
  18686. + /* it will be set to dinfo later */
  18687. + dget(a->h_dst);
  18688. +
  18689. + return err;
  18690. +}
  18691. +
  18692. +/* cf. aufs_rmdir() */
  18693. +static int au_ren_del_whtmp(struct au_ren_args *a)
  18694. +{
  18695. + int err;
  18696. + struct inode *dir;
  18697. +
  18698. + dir = a->dst_dir;
  18699. + SiMustAnyLock(dir->i_sb);
  18700. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  18701. + au_sbi(dir->i_sb)->si_dirwh)
  18702. + || au_test_fs_remote(a->h_dst->d_sb)) {
  18703. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  18704. + if (unlikely(err))
  18705. + pr_warn("failed removing whtmp dir %pd (%d), "
  18706. + "ignored.\n", a->h_dst, err);
  18707. + } else {
  18708. + au_nhash_wh_free(&a->thargs->whlist);
  18709. + a->thargs->whlist = a->whlist;
  18710. + a->whlist.nh_num = 0;
  18711. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  18712. + dput(a->h_dst);
  18713. + a->thargs = NULL;
  18714. + }
  18715. +
  18716. + return 0;
  18717. +}
  18718. +
  18719. +/* make it 'opaque' dir. */
  18720. +static int au_ren_diropq(struct au_ren_args *a)
  18721. +{
  18722. + int err;
  18723. + struct dentry *diropq;
  18724. +
  18725. + err = 0;
  18726. + a->src_bdiropq = au_dbdiropq(a->src_dentry);
  18727. + a->src_hinode = au_hi(a->src_inode, a->btgt);
  18728. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  18729. + diropq = au_diropq_create(a->src_dentry, a->btgt);
  18730. + au_hn_imtx_unlock(a->src_hinode);
  18731. + if (IS_ERR(diropq))
  18732. + err = PTR_ERR(diropq);
  18733. + else
  18734. + dput(diropq);
  18735. +
  18736. + return err;
  18737. +}
  18738. +
  18739. +static int do_rename(struct au_ren_args *a)
  18740. +{
  18741. + int err;
  18742. + struct dentry *d, *h_d;
  18743. +
  18744. + /* prepare workqueue args for asynchronous rmdir */
  18745. + h_d = a->dst_h_dentry;
  18746. + if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
  18747. + err = -ENOMEM;
  18748. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
  18749. + if (unlikely(!a->thargs))
  18750. + goto out;
  18751. + a->h_dst = dget(h_d);
  18752. + }
  18753. +
  18754. + /* create whiteout for src_dentry */
  18755. + if (au_ftest_ren(a->flags, WHSRC)) {
  18756. + a->src_bwh = au_dbwh(a->src_dentry);
  18757. + AuDebugOn(a->src_bwh >= 0);
  18758. + a->src_wh_dentry
  18759. + = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
  18760. + err = PTR_ERR(a->src_wh_dentry);
  18761. + if (IS_ERR(a->src_wh_dentry))
  18762. + goto out_thargs;
  18763. + }
  18764. +
  18765. + /* lookup whiteout for dentry */
  18766. + if (au_ftest_ren(a->flags, WHDST)) {
  18767. + h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
  18768. + a->br);
  18769. + err = PTR_ERR(h_d);
  18770. + if (IS_ERR(h_d))
  18771. + goto out_whsrc;
  18772. + if (!h_d->d_inode)
  18773. + dput(h_d);
  18774. + else
  18775. + a->dst_wh_dentry = h_d;
  18776. + }
  18777. +
  18778. + /* rename dentry to tmpwh */
  18779. + if (a->thargs) {
  18780. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  18781. + if (unlikely(err))
  18782. + goto out_whdst;
  18783. +
  18784. + d = a->dst_dentry;
  18785. + au_set_h_dptr(d, a->btgt, NULL);
  18786. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  18787. + if (unlikely(err))
  18788. + goto out_whtmp;
  18789. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  18790. + }
  18791. +
  18792. + BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
  18793. +
  18794. + /* rename by vfs_rename or cpup */
  18795. + d = a->dst_dentry;
  18796. + if (au_ftest_ren(a->flags, ISDIR)
  18797. + && (a->dst_wh_dentry
  18798. + || au_dbdiropq(d) == a->btgt
  18799. + /* hide the lower to keep xino */
  18800. + || a->btgt < au_dbend(d)
  18801. + || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
  18802. + au_fset_ren(a->flags, DIROPQ);
  18803. + err = au_ren_or_cpup(a);
  18804. + if (unlikely(err))
  18805. + /* leave the copied-up one */
  18806. + goto out_whtmp;
  18807. +
  18808. + /* make dir opaque */
  18809. + if (au_ftest_ren(a->flags, DIROPQ)) {
  18810. + err = au_ren_diropq(a);
  18811. + if (unlikely(err))
  18812. + goto out_rename;
  18813. + }
  18814. +
  18815. + /* update target timestamps */
  18816. + AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
  18817. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  18818. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  18819. + a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
  18820. +
  18821. + /* remove whiteout for dentry */
  18822. + if (a->dst_wh_dentry) {
  18823. + a->h_path.dentry = a->dst_wh_dentry;
  18824. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  18825. + a->dst_dentry);
  18826. + if (unlikely(err))
  18827. + goto out_diropq;
  18828. + }
  18829. +
  18830. + /* remove whtmp */
  18831. + if (a->thargs)
  18832. + au_ren_del_whtmp(a); /* ignore this error */
  18833. +
  18834. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  18835. + err = 0;
  18836. + goto out_success;
  18837. +
  18838. +out_diropq:
  18839. + if (au_ftest_ren(a->flags, DIROPQ))
  18840. + au_ren_rev_diropq(err, a);
  18841. +out_rename:
  18842. + au_ren_rev_rename(err, a);
  18843. + dput(a->h_dst);
  18844. +out_whtmp:
  18845. + if (a->thargs)
  18846. + au_ren_rev_whtmp(err, a);
  18847. +out_whdst:
  18848. + dput(a->dst_wh_dentry);
  18849. + a->dst_wh_dentry = NULL;
  18850. +out_whsrc:
  18851. + if (a->src_wh_dentry)
  18852. + au_ren_rev_whsrc(err, a);
  18853. +out_success:
  18854. + dput(a->src_wh_dentry);
  18855. + dput(a->dst_wh_dentry);
  18856. +out_thargs:
  18857. + if (a->thargs) {
  18858. + dput(a->h_dst);
  18859. + au_whtmp_rmdir_free(a->thargs);
  18860. + a->thargs = NULL;
  18861. + }
  18862. +out:
  18863. + return err;
  18864. +}
  18865. +
  18866. +/* ---------------------------------------------------------------------- */
  18867. +
  18868. +/*
  18869. + * test if @dentry dir can be rename destination or not.
  18870. + * success means, it is a logically empty dir.
  18871. + */
  18872. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  18873. +{
  18874. + return au_test_empty(dentry, whlist);
  18875. +}
  18876. +
  18877. +/*
  18878. + * test if @dentry dir can be rename source or not.
  18879. + * if it can, return 0 and @children is filled.
  18880. + * success means,
  18881. + * - it is a logically empty dir.
  18882. + * - or, it exists on writable branch and has no children including whiteouts
  18883. + * on the lower branch.
  18884. + */
  18885. +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
  18886. +{
  18887. + int err;
  18888. + unsigned int rdhash;
  18889. + aufs_bindex_t bstart;
  18890. +
  18891. + bstart = au_dbstart(dentry);
  18892. + if (bstart != btgt) {
  18893. + struct au_nhash whlist;
  18894. +
  18895. + SiMustAnyLock(dentry->d_sb);
  18896. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  18897. + if (!rdhash)
  18898. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  18899. + dentry));
  18900. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  18901. + if (unlikely(err))
  18902. + goto out;
  18903. + err = au_test_empty(dentry, &whlist);
  18904. + au_nhash_wh_free(&whlist);
  18905. + goto out;
  18906. + }
  18907. +
  18908. + if (bstart == au_dbtaildir(dentry))
  18909. + return 0; /* success */
  18910. +
  18911. + err = au_test_empty_lower(dentry);
  18912. +
  18913. +out:
  18914. + if (err == -ENOTEMPTY) {
  18915. + AuWarn1("renaming dir who has child(ren) on multiple branches,"
  18916. + " is not supported\n");
  18917. + err = -EXDEV;
  18918. + }
  18919. + return err;
  18920. +}
  18921. +
  18922. +/* side effect: sets whlist and h_dentry */
  18923. +static int au_ren_may_dir(struct au_ren_args *a)
  18924. +{
  18925. + int err;
  18926. + unsigned int rdhash;
  18927. + struct dentry *d;
  18928. +
  18929. + d = a->dst_dentry;
  18930. + SiMustAnyLock(d->d_sb);
  18931. +
  18932. + err = 0;
  18933. + if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
  18934. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  18935. + if (!rdhash)
  18936. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  18937. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  18938. + if (unlikely(err))
  18939. + goto out;
  18940. +
  18941. + au_set_dbstart(d, a->dst_bstart);
  18942. + err = may_rename_dstdir(d, &a->whlist);
  18943. + au_set_dbstart(d, a->btgt);
  18944. + }
  18945. + a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
  18946. + if (unlikely(err))
  18947. + goto out;
  18948. +
  18949. + d = a->src_dentry;
  18950. + a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
  18951. + if (au_ftest_ren(a->flags, ISDIR)) {
  18952. + err = may_rename_srcdir(d, a->btgt);
  18953. + if (unlikely(err)) {
  18954. + au_nhash_wh_free(&a->whlist);
  18955. + a->whlist.nh_num = 0;
  18956. + }
  18957. + }
  18958. +out:
  18959. + return err;
  18960. +}
  18961. +
  18962. +/* ---------------------------------------------------------------------- */
  18963. +
  18964. +/*
  18965. + * simple tests for rename.
  18966. + * following the checks in vfs, plus the parent-child relationship.
  18967. + */
  18968. +static int au_may_ren(struct au_ren_args *a)
  18969. +{
  18970. + int err, isdir;
  18971. + struct inode *h_inode;
  18972. +
  18973. + if (a->src_bstart == a->btgt) {
  18974. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  18975. + au_ftest_ren(a->flags, ISDIR));
  18976. + if (unlikely(err))
  18977. + goto out;
  18978. + err = -EINVAL;
  18979. + if (unlikely(a->src_h_dentry == a->h_trap))
  18980. + goto out;
  18981. + }
  18982. +
  18983. + err = 0;
  18984. + if (a->dst_bstart != a->btgt)
  18985. + goto out;
  18986. +
  18987. + err = -ENOTEMPTY;
  18988. + if (unlikely(a->dst_h_dentry == a->h_trap))
  18989. + goto out;
  18990. +
  18991. + err = -EIO;
  18992. + h_inode = a->dst_h_dentry->d_inode;
  18993. + isdir = !!au_ftest_ren(a->flags, ISDIR);
  18994. + if (!a->dst_dentry->d_inode) {
  18995. + if (unlikely(h_inode))
  18996. + goto out;
  18997. + err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
  18998. + isdir);
  18999. + } else {
  19000. + if (unlikely(!h_inode || !h_inode->i_nlink))
  19001. + goto out;
  19002. + err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
  19003. + isdir);
  19004. + if (unlikely(err))
  19005. + goto out;
  19006. + }
  19007. +
  19008. +out:
  19009. + if (unlikely(err == -ENOENT || err == -EEXIST))
  19010. + err = -EIO;
  19011. + AuTraceErr(err);
  19012. + return err;
  19013. +}
  19014. +
  19015. +/* ---------------------------------------------------------------------- */
  19016. +
  19017. +/*
  19018. + * locking order
  19019. + * (VFS)
  19020. + * - src_dir and dir by lock_rename()
  19021. + * - inode if exitsts
  19022. + * (aufs)
  19023. + * - lock all
  19024. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  19025. + * + si_read_lock
  19026. + * + di_write_lock2_child()
  19027. + * + di_write_lock_child()
  19028. + * + ii_write_lock_child()
  19029. + * + di_write_lock_child2()
  19030. + * + ii_write_lock_child2()
  19031. + * + src_parent and parent
  19032. + * + di_write_lock_parent()
  19033. + * + ii_write_lock_parent()
  19034. + * + di_write_lock_parent2()
  19035. + * + ii_write_lock_parent2()
  19036. + * + lower src_dir and dir by vfsub_lock_rename()
  19037. + * + verify the every relationships between child and parent. if any
  19038. + * of them failed, unlock all and return -EBUSY.
  19039. + */
  19040. +static void au_ren_unlock(struct au_ren_args *a)
  19041. +{
  19042. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  19043. + a->dst_h_parent, a->dst_hdir);
  19044. + if (au_ftest_ren(a->flags, MNT_WRITE))
  19045. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  19046. +}
  19047. +
  19048. +static int au_ren_lock(struct au_ren_args *a)
  19049. +{
  19050. + int err;
  19051. + unsigned int udba;
  19052. +
  19053. + err = 0;
  19054. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  19055. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  19056. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  19057. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  19058. +
  19059. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  19060. + if (unlikely(err))
  19061. + goto out;
  19062. + au_fset_ren(a->flags, MNT_WRITE);
  19063. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  19064. + a->dst_h_parent, a->dst_hdir);
  19065. + udba = au_opt_udba(a->src_dentry->d_sb);
  19066. + if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
  19067. + || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
  19068. + err = au_busy_or_stale();
  19069. + if (!err && au_dbstart(a->src_dentry) == a->btgt)
  19070. + err = au_h_verify(a->src_h_dentry, udba,
  19071. + a->src_h_parent->d_inode, a->src_h_parent,
  19072. + a->br);
  19073. + if (!err && au_dbstart(a->dst_dentry) == a->btgt)
  19074. + err = au_h_verify(a->dst_h_dentry, udba,
  19075. + a->dst_h_parent->d_inode, a->dst_h_parent,
  19076. + a->br);
  19077. + if (!err)
  19078. + goto out; /* success */
  19079. +
  19080. + err = au_busy_or_stale();
  19081. + au_ren_unlock(a);
  19082. +
  19083. +out:
  19084. + return err;
  19085. +}
  19086. +
  19087. +/* ---------------------------------------------------------------------- */
  19088. +
  19089. +static void au_ren_refresh_dir(struct au_ren_args *a)
  19090. +{
  19091. + struct inode *dir;
  19092. +
  19093. + dir = a->dst_dir;
  19094. + dir->i_version++;
  19095. + if (au_ftest_ren(a->flags, ISDIR)) {
  19096. + /* is this updating defined in POSIX? */
  19097. + au_cpup_attr_timesizes(a->src_inode);
  19098. + au_cpup_attr_nlink(dir, /*force*/1);
  19099. + }
  19100. +
  19101. + au_dir_ts(dir, a->btgt);
  19102. +
  19103. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  19104. + return;
  19105. +
  19106. + dir = a->src_dir;
  19107. + dir->i_version++;
  19108. + if (au_ftest_ren(a->flags, ISDIR))
  19109. + au_cpup_attr_nlink(dir, /*force*/1);
  19110. + au_dir_ts(dir, a->btgt);
  19111. +}
  19112. +
  19113. +static void au_ren_refresh(struct au_ren_args *a)
  19114. +{
  19115. + aufs_bindex_t bend, bindex;
  19116. + struct dentry *d, *h_d;
  19117. + struct inode *i, *h_i;
  19118. + struct super_block *sb;
  19119. +
  19120. + d = a->dst_dentry;
  19121. + d_drop(d);
  19122. + if (a->h_dst)
  19123. + /* already dget-ed by au_ren_or_cpup() */
  19124. + au_set_h_dptr(d, a->btgt, a->h_dst);
  19125. +
  19126. + i = a->dst_inode;
  19127. + if (i) {
  19128. + if (!au_ftest_ren(a->flags, ISDIR))
  19129. + vfsub_drop_nlink(i);
  19130. + else {
  19131. + vfsub_dead_dir(i);
  19132. + au_cpup_attr_timesizes(i);
  19133. + }
  19134. + au_update_dbrange(d, /*do_put_zero*/1);
  19135. + } else {
  19136. + bend = a->btgt;
  19137. + for (bindex = au_dbstart(d); bindex < bend; bindex++)
  19138. + au_set_h_dptr(d, bindex, NULL);
  19139. + bend = au_dbend(d);
  19140. + for (bindex = a->btgt + 1; bindex <= bend; bindex++)
  19141. + au_set_h_dptr(d, bindex, NULL);
  19142. + au_update_dbrange(d, /*do_put_zero*/0);
  19143. + }
  19144. +
  19145. + d = a->src_dentry;
  19146. + au_set_dbwh(d, -1);
  19147. + bend = au_dbend(d);
  19148. + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
  19149. + h_d = au_h_dptr(d, bindex);
  19150. + if (h_d)
  19151. + au_set_h_dptr(d, bindex, NULL);
  19152. + }
  19153. + au_set_dbend(d, a->btgt);
  19154. +
  19155. + sb = d->d_sb;
  19156. + i = a->src_inode;
  19157. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  19158. + return; /* success */
  19159. +
  19160. + bend = au_ibend(i);
  19161. + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
  19162. + h_i = au_h_iptr(i, bindex);
  19163. + if (h_i) {
  19164. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  19165. + /* ignore this error */
  19166. + au_set_h_iptr(i, bindex, NULL, 0);
  19167. + }
  19168. + }
  19169. + au_set_ibend(i, a->btgt);
  19170. +}
  19171. +
  19172. +/* ---------------------------------------------------------------------- */
  19173. +
  19174. +/* mainly for link(2) and rename(2) */
  19175. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  19176. +{
  19177. + aufs_bindex_t bdiropq, bwh;
  19178. + struct dentry *parent;
  19179. + struct au_branch *br;
  19180. +
  19181. + parent = dentry->d_parent;
  19182. + IMustLock(parent->d_inode); /* dir is locked */
  19183. +
  19184. + bdiropq = au_dbdiropq(parent);
  19185. + bwh = au_dbwh(dentry);
  19186. + br = au_sbr(dentry->d_sb, btgt);
  19187. + if (au_br_rdonly(br)
  19188. + || (0 <= bdiropq && bdiropq < btgt)
  19189. + || (0 <= bwh && bwh < btgt))
  19190. + btgt = -1;
  19191. +
  19192. + AuDbg("btgt %d\n", btgt);
  19193. + return btgt;
  19194. +}
  19195. +
  19196. +/* sets src_bstart, dst_bstart and btgt */
  19197. +static int au_ren_wbr(struct au_ren_args *a)
  19198. +{
  19199. + int err;
  19200. + struct au_wr_dir_args wr_dir_args = {
  19201. + /* .force_btgt = -1, */
  19202. + .flags = AuWrDir_ADD_ENTRY
  19203. + };
  19204. +
  19205. + a->src_bstart = au_dbstart(a->src_dentry);
  19206. + a->dst_bstart = au_dbstart(a->dst_dentry);
  19207. + if (au_ftest_ren(a->flags, ISDIR))
  19208. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  19209. + wr_dir_args.force_btgt = a->src_bstart;
  19210. + if (a->dst_inode && a->dst_bstart < a->src_bstart)
  19211. + wr_dir_args.force_btgt = a->dst_bstart;
  19212. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  19213. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  19214. + a->btgt = err;
  19215. +
  19216. + return err;
  19217. +}
  19218. +
  19219. +static void au_ren_dt(struct au_ren_args *a)
  19220. +{
  19221. + a->h_path.dentry = a->src_h_parent;
  19222. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  19223. + if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
  19224. + a->h_path.dentry = a->dst_h_parent;
  19225. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  19226. + }
  19227. +
  19228. + au_fclr_ren(a->flags, DT_DSTDIR);
  19229. + if (!au_ftest_ren(a->flags, ISDIR))
  19230. + return;
  19231. +
  19232. + a->h_path.dentry = a->src_h_dentry;
  19233. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  19234. + if (a->dst_h_dentry->d_inode) {
  19235. + au_fset_ren(a->flags, DT_DSTDIR);
  19236. + a->h_path.dentry = a->dst_h_dentry;
  19237. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  19238. + }
  19239. +}
  19240. +
  19241. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  19242. +{
  19243. + struct dentry *h_d;
  19244. + struct mutex *h_mtx;
  19245. +
  19246. + au_dtime_revert(a->src_dt + AuPARENT);
  19247. + if (!au_ftest_ren(a->flags, ISSAMEDIR))
  19248. + au_dtime_revert(a->dst_dt + AuPARENT);
  19249. +
  19250. + if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
  19251. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  19252. + h_mtx = &h_d->d_inode->i_mutex;
  19253. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  19254. + au_dtime_revert(a->src_dt + AuCHILD);
  19255. + mutex_unlock(h_mtx);
  19256. +
  19257. + if (au_ftest_ren(a->flags, DT_DSTDIR)) {
  19258. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  19259. + h_mtx = &h_d->d_inode->i_mutex;
  19260. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  19261. + au_dtime_revert(a->dst_dt + AuCHILD);
  19262. + mutex_unlock(h_mtx);
  19263. + }
  19264. + }
  19265. +}
  19266. +
  19267. +/* ---------------------------------------------------------------------- */
  19268. +
  19269. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  19270. + struct inode *_dst_dir, struct dentry *_dst_dentry)
  19271. +{
  19272. + int err, flags;
  19273. + /* reduce stack space */
  19274. + struct au_ren_args *a;
  19275. +
  19276. + AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
  19277. + IMustLock(_src_dir);
  19278. + IMustLock(_dst_dir);
  19279. +
  19280. + err = -ENOMEM;
  19281. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  19282. + a = kzalloc(sizeof(*a), GFP_NOFS);
  19283. + if (unlikely(!a))
  19284. + goto out;
  19285. +
  19286. + a->src_dir = _src_dir;
  19287. + a->src_dentry = _src_dentry;
  19288. + a->src_inode = a->src_dentry->d_inode;
  19289. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  19290. + a->dst_dir = _dst_dir;
  19291. + a->dst_dentry = _dst_dentry;
  19292. + a->dst_inode = a->dst_dentry->d_inode;
  19293. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  19294. + if (a->dst_inode) {
  19295. + IMustLock(a->dst_inode);
  19296. + au_igrab(a->dst_inode);
  19297. + }
  19298. +
  19299. + err = -ENOTDIR;
  19300. + flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  19301. + if (d_is_dir(a->src_dentry)) {
  19302. + au_fset_ren(a->flags, ISDIR);
  19303. + if (unlikely(d_is_positive(a->dst_dentry)
  19304. + && !d_is_dir(a->dst_dentry)))
  19305. + goto out_free;
  19306. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  19307. + AuLock_DIR | flags);
  19308. + } else
  19309. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  19310. + flags);
  19311. + if (unlikely(err))
  19312. + goto out_free;
  19313. +
  19314. + err = au_d_hashed_positive(a->src_dentry);
  19315. + if (unlikely(err))
  19316. + goto out_unlock;
  19317. + err = -ENOENT;
  19318. + if (a->dst_inode) {
  19319. + /*
  19320. + * If it is a dir, VFS unhash dst_dentry before this
  19321. + * function. It means we cannot rely upon d_unhashed().
  19322. + */
  19323. + if (unlikely(!a->dst_inode->i_nlink))
  19324. + goto out_unlock;
  19325. + if (!S_ISDIR(a->dst_inode->i_mode)) {
  19326. + err = au_d_hashed_positive(a->dst_dentry);
  19327. + if (unlikely(err))
  19328. + goto out_unlock;
  19329. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  19330. + goto out_unlock;
  19331. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  19332. + goto out_unlock;
  19333. +
  19334. + /*
  19335. + * is it possible?
  19336. + * yes, it happend (in linux-3.3-rcN) but I don't know why.
  19337. + * there may exist a problem somewhere else.
  19338. + */
  19339. + err = -EINVAL;
  19340. + if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
  19341. + goto out_unlock;
  19342. +
  19343. + au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
  19344. + di_write_lock_parent(a->dst_parent);
  19345. +
  19346. + /* which branch we process */
  19347. + err = au_ren_wbr(a);
  19348. + if (unlikely(err < 0))
  19349. + goto out_parent;
  19350. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  19351. + a->h_path.mnt = au_br_mnt(a->br);
  19352. +
  19353. + /* are they available to be renamed */
  19354. + err = au_ren_may_dir(a);
  19355. + if (unlikely(err))
  19356. + goto out_children;
  19357. +
  19358. + /* prepare the writable parent dir on the same branch */
  19359. + if (a->dst_bstart == a->btgt) {
  19360. + au_fset_ren(a->flags, WHDST);
  19361. + } else {
  19362. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  19363. + if (unlikely(err))
  19364. + goto out_children;
  19365. + }
  19366. +
  19367. + if (a->src_dir != a->dst_dir) {
  19368. + /*
  19369. + * this temporary unlock is safe,
  19370. + * because both dir->i_mutex are locked.
  19371. + */
  19372. + di_write_unlock(a->dst_parent);
  19373. + di_write_lock_parent(a->src_parent);
  19374. + err = au_wr_dir_need_wh(a->src_dentry,
  19375. + au_ftest_ren(a->flags, ISDIR),
  19376. + &a->btgt);
  19377. + di_write_unlock(a->src_parent);
  19378. + di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
  19379. + au_fclr_ren(a->flags, ISSAMEDIR);
  19380. + } else
  19381. + err = au_wr_dir_need_wh(a->src_dentry,
  19382. + au_ftest_ren(a->flags, ISDIR),
  19383. + &a->btgt);
  19384. + if (unlikely(err < 0))
  19385. + goto out_children;
  19386. + if (err)
  19387. + au_fset_ren(a->flags, WHSRC);
  19388. +
  19389. + /* cpup src */
  19390. + if (a->src_bstart != a->btgt) {
  19391. + struct au_pin pin;
  19392. +
  19393. + err = au_pin(&pin, a->src_dentry, a->btgt,
  19394. + au_opt_udba(a->src_dentry->d_sb),
  19395. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19396. + if (!err) {
  19397. + struct au_cp_generic cpg = {
  19398. + .dentry = a->src_dentry,
  19399. + .bdst = a->btgt,
  19400. + .bsrc = a->src_bstart,
  19401. + .len = -1,
  19402. + .pin = &pin,
  19403. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  19404. + };
  19405. + AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
  19406. + err = au_sio_cpup_simple(&cpg);
  19407. + au_unpin(&pin);
  19408. + }
  19409. + if (unlikely(err))
  19410. + goto out_children;
  19411. + a->src_bstart = a->btgt;
  19412. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  19413. + au_fset_ren(a->flags, WHSRC);
  19414. + }
  19415. +
  19416. + /* lock them all */
  19417. + err = au_ren_lock(a);
  19418. + if (unlikely(err))
  19419. + /* leave the copied-up one */
  19420. + goto out_children;
  19421. +
  19422. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  19423. + err = au_may_ren(a);
  19424. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  19425. + err = -ENAMETOOLONG;
  19426. + if (unlikely(err))
  19427. + goto out_hdir;
  19428. +
  19429. + /* store timestamps to be revertible */
  19430. + au_ren_dt(a);
  19431. +
  19432. + /* here we go */
  19433. + err = do_rename(a);
  19434. + if (unlikely(err))
  19435. + goto out_dt;
  19436. +
  19437. + /* update dir attributes */
  19438. + au_ren_refresh_dir(a);
  19439. +
  19440. + /* dput/iput all lower dentries */
  19441. + au_ren_refresh(a);
  19442. +
  19443. + goto out_hdir; /* success */
  19444. +
  19445. +out_dt:
  19446. + au_ren_rev_dt(err, a);
  19447. +out_hdir:
  19448. + au_ren_unlock(a);
  19449. +out_children:
  19450. + au_nhash_wh_free(&a->whlist);
  19451. + if (err && a->dst_inode && a->dst_bstart != a->btgt) {
  19452. + AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
  19453. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  19454. + au_set_dbstart(a->dst_dentry, a->dst_bstart);
  19455. + }
  19456. +out_parent:
  19457. + if (!err)
  19458. + d_move(a->src_dentry, a->dst_dentry);
  19459. + else {
  19460. + au_update_dbstart(a->dst_dentry);
  19461. + if (!a->dst_inode)
  19462. + d_drop(a->dst_dentry);
  19463. + }
  19464. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  19465. + di_write_unlock(a->dst_parent);
  19466. + else
  19467. + di_write_unlock2(a->src_parent, a->dst_parent);
  19468. +out_unlock:
  19469. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  19470. +out_free:
  19471. + iput(a->dst_inode);
  19472. + if (a->thargs)
  19473. + au_whtmp_rmdir_free(a->thargs);
  19474. + kfree(a);
  19475. +out:
  19476. + AuTraceErr(err);
  19477. + return err;
  19478. +}
  19479. diff -Nur linux-4.0.4.orig/fs/aufs/Kconfig linux-4.0.4/fs/aufs/Kconfig
  19480. --- linux-4.0.4.orig/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
  19481. +++ linux-4.0.4/fs/aufs/Kconfig 2015-05-30 22:11:30.000000000 +0200
  19482. @@ -0,0 +1,185 @@
  19483. +config AUFS_FS
  19484. + bool "Aufs (Advanced multi layered unification filesystem) support"
  19485. + help
  19486. + Aufs is a stackable unification filesystem such as Unionfs,
  19487. + which unifies several directories and provides a merged single
  19488. + directory.
  19489. + In the early days, aufs was entirely re-designed and
  19490. + re-implemented Unionfs Version 1.x series. Introducing many
  19491. + original ideas, approaches and improvements, it becomes totally
  19492. + different from Unionfs while keeping the basic features.
  19493. +
  19494. +if AUFS_FS
  19495. +choice
  19496. + prompt "Maximum number of branches"
  19497. + default AUFS_BRANCH_MAX_127
  19498. + help
  19499. + Specifies the maximum number of branches (or member directories)
  19500. + in a single aufs. The larger value consumes more system
  19501. + resources and has a minor impact to performance.
  19502. +config AUFS_BRANCH_MAX_127
  19503. + bool "127"
  19504. + help
  19505. + Specifies the maximum number of branches (or member directories)
  19506. + in a single aufs. The larger value consumes more system
  19507. + resources and has a minor impact to performance.
  19508. +config AUFS_BRANCH_MAX_511
  19509. + bool "511"
  19510. + help
  19511. + Specifies the maximum number of branches (or member directories)
  19512. + in a single aufs. The larger value consumes more system
  19513. + resources and has a minor impact to performance.
  19514. +config AUFS_BRANCH_MAX_1023
  19515. + bool "1023"
  19516. + help
  19517. + Specifies the maximum number of branches (or member directories)
  19518. + in a single aufs. The larger value consumes more system
  19519. + resources and has a minor impact to performance.
  19520. +config AUFS_BRANCH_MAX_32767
  19521. + bool "32767"
  19522. + help
  19523. + Specifies the maximum number of branches (or member directories)
  19524. + in a single aufs. The larger value consumes more system
  19525. + resources and has a minor impact to performance.
  19526. +endchoice
  19527. +
  19528. +config AUFS_SBILIST
  19529. + bool
  19530. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  19531. + default y
  19532. + help
  19533. + Automatic configuration for internal use.
  19534. + When aufs supports Magic SysRq or /proc, enabled automatically.
  19535. +
  19536. +config AUFS_HNOTIFY
  19537. + bool "Detect direct branch access (bypassing aufs)"
  19538. + help
  19539. + If you want to modify files on branches directly, eg. bypassing aufs,
  19540. + and want aufs to detect the changes of them fully, then enable this
  19541. + option and use 'udba=notify' mount option.
  19542. + Currently there is only one available configuration, "fsnotify".
  19543. + It will have a negative impact to the performance.
  19544. + See detail in aufs.5.
  19545. +
  19546. +choice
  19547. + prompt "method" if AUFS_HNOTIFY
  19548. + default AUFS_HFSNOTIFY
  19549. +config AUFS_HFSNOTIFY
  19550. + bool "fsnotify"
  19551. + select FSNOTIFY
  19552. +endchoice
  19553. +
  19554. +config AUFS_EXPORT
  19555. + bool "NFS-exportable aufs"
  19556. + depends on EXPORTFS = y
  19557. + help
  19558. + If you want to export your mounted aufs via NFS, then enable this
  19559. + option. There are several requirements for this configuration.
  19560. + See detail in aufs.5.
  19561. +
  19562. +config AUFS_INO_T_64
  19563. + bool
  19564. + depends on AUFS_EXPORT
  19565. + depends on 64BIT && !(ALPHA || S390)
  19566. + default y
  19567. + help
  19568. + Automatic configuration for internal use.
  19569. + /* typedef unsigned long/int __kernel_ino_t */
  19570. + /* alpha and s390x are int */
  19571. +
  19572. +config AUFS_XATTR
  19573. + bool "support for XATTR/EA (including Security Labels)"
  19574. + help
  19575. + If your branch fs supports XATTR/EA and you want to make them
  19576. + available in aufs too, then enable this opsion and specify the
  19577. + branch attributes for EA.
  19578. + See detail in aufs.5.
  19579. +
  19580. +config AUFS_FHSM
  19581. + bool "File-based Hierarchical Storage Management"
  19582. + help
  19583. + Hierarchical Storage Management (or HSM) is a well-known feature
  19584. + in the storage world. Aufs provides this feature as file-based.
  19585. + with multiple branches.
  19586. + These multiple branches are prioritized, ie. the topmost one
  19587. + should be the fastest drive and be used heavily.
  19588. +
  19589. +config AUFS_RDU
  19590. + bool "Readdir in userspace"
  19591. + help
  19592. + Aufs has two methods to provide a merged view for a directory,
  19593. + by a user-space library and by kernel-space natively. The latter
  19594. + is always enabled but sometimes large and slow.
  19595. + If you enable this option, install the library in aufs2-util
  19596. + package, and set some environment variables for your readdir(3),
  19597. + then the work will be handled in user-space which generally
  19598. + shows better performance in most cases.
  19599. + See detail in aufs.5.
  19600. +
  19601. +config AUFS_SHWH
  19602. + bool "Show whiteouts"
  19603. + help
  19604. + If you want to make the whiteouts in aufs visible, then enable
  19605. + this option and specify 'shwh' mount option. Although it may
  19606. + sounds like philosophy or something, but in technically it
  19607. + simply shows the name of whiteout with keeping its behaviour.
  19608. +
  19609. +config AUFS_BR_RAMFS
  19610. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  19611. + help
  19612. + If you want to use ramfs as an aufs branch fs, then enable this
  19613. + option. Generally tmpfs is recommended.
  19614. + Aufs prohibited them to be a branch fs by default, because
  19615. + initramfs becomes unusable after switch_root or something
  19616. + generally. If you sets initramfs as an aufs branch and boot your
  19617. + system by switch_root, you will meet a problem easily since the
  19618. + files in initramfs may be inaccessible.
  19619. + Unless you are going to use ramfs as an aufs branch fs without
  19620. + switch_root or something, leave it N.
  19621. +
  19622. +config AUFS_BR_FUSE
  19623. + bool "Fuse fs as an aufs branch"
  19624. + depends on FUSE_FS
  19625. + select AUFS_POLL
  19626. + help
  19627. + If you want to use fuse-based userspace filesystem as an aufs
  19628. + branch fs, then enable this option.
  19629. + It implements the internal poll(2) operation which is
  19630. + implemented by fuse only (curretnly).
  19631. +
  19632. +config AUFS_POLL
  19633. + bool
  19634. + help
  19635. + Automatic configuration for internal use.
  19636. +
  19637. +config AUFS_BR_HFSPLUS
  19638. + bool "Hfsplus as an aufs branch"
  19639. + depends on HFSPLUS_FS
  19640. + default y
  19641. + help
  19642. + If you want to use hfsplus fs as an aufs branch fs, then enable
  19643. + this option. This option introduces a small overhead at
  19644. + copying-up a file on hfsplus.
  19645. +
  19646. +config AUFS_BDEV_LOOP
  19647. + bool
  19648. + depends on BLK_DEV_LOOP
  19649. + default y
  19650. + help
  19651. + Automatic configuration for internal use.
  19652. + Convert =[ym] into =y.
  19653. +
  19654. +config AUFS_DEBUG
  19655. + bool "Debug aufs"
  19656. + help
  19657. + Enable this to compile aufs internal debug code.
  19658. + It will have a negative impact to the performance.
  19659. +
  19660. +config AUFS_MAGIC_SYSRQ
  19661. + bool
  19662. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  19663. + default y
  19664. + help
  19665. + Automatic configuration for internal use.
  19666. + When aufs supports Magic SysRq, enabled automatically.
  19667. +endif
  19668. diff -Nur linux-4.0.4.orig/fs/aufs/loop.c linux-4.0.4/fs/aufs/loop.c
  19669. --- linux-4.0.4.orig/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
  19670. +++ linux-4.0.4/fs/aufs/loop.c 2015-05-30 22:11:30.000000000 +0200
  19671. @@ -0,0 +1,132 @@
  19672. +/*
  19673. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  19674. + */
  19675. +
  19676. +/*
  19677. + * support for loopback block device as a branch
  19678. + */
  19679. +
  19680. +#include "aufs.h"
  19681. +
  19682. +/* added into drivers/block/loop.c */
  19683. +static struct file *(*backing_file_func)(struct super_block *sb);
  19684. +
  19685. +/*
  19686. + * test if two lower dentries have overlapping branches.
  19687. + */
  19688. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  19689. +{
  19690. + struct super_block *h_sb;
  19691. + struct file *backing_file;
  19692. +
  19693. + if (unlikely(!backing_file_func)) {
  19694. + /* don't load "loop" module here */
  19695. + backing_file_func = symbol_get(loop_backing_file);
  19696. + if (unlikely(!backing_file_func))
  19697. + /* "loop" module is not loaded */
  19698. + return 0;
  19699. + }
  19700. +
  19701. + h_sb = h_adding->d_sb;
  19702. + backing_file = backing_file_func(h_sb);
  19703. + if (!backing_file)
  19704. + return 0;
  19705. +
  19706. + h_adding = backing_file->f_path.dentry;
  19707. + /*
  19708. + * h_adding can be local NFS.
  19709. + * in this case aufs cannot detect the loop.
  19710. + */
  19711. + if (unlikely(h_adding->d_sb == sb))
  19712. + return 1;
  19713. + return !!au_test_subdir(h_adding, sb->s_root);
  19714. +}
  19715. +
  19716. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  19717. +int au_test_loopback_kthread(void)
  19718. +{
  19719. + int ret;
  19720. + struct task_struct *tsk = current;
  19721. + char c, comm[sizeof(tsk->comm)];
  19722. +
  19723. + ret = 0;
  19724. + if (tsk->flags & PF_KTHREAD) {
  19725. + get_task_comm(comm, tsk);
  19726. + c = comm[4];
  19727. + ret = ('0' <= c && c <= '9'
  19728. + && !strncmp(comm, "loop", 4));
  19729. + }
  19730. +
  19731. + return ret;
  19732. +}
  19733. +
  19734. +/* ---------------------------------------------------------------------- */
  19735. +
  19736. +#define au_warn_loopback_step 16
  19737. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  19738. +static unsigned long *au_warn_loopback_array;
  19739. +
  19740. +void au_warn_loopback(struct super_block *h_sb)
  19741. +{
  19742. + int i, new_nelem;
  19743. + unsigned long *a, magic;
  19744. + static DEFINE_SPINLOCK(spin);
  19745. +
  19746. + magic = h_sb->s_magic;
  19747. + spin_lock(&spin);
  19748. + a = au_warn_loopback_array;
  19749. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  19750. + if (a[i] == magic) {
  19751. + spin_unlock(&spin);
  19752. + return;
  19753. + }
  19754. +
  19755. + /* h_sb is new to us, print it */
  19756. + if (i < au_warn_loopback_nelem) {
  19757. + a[i] = magic;
  19758. + goto pr;
  19759. + }
  19760. +
  19761. + /* expand the array */
  19762. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  19763. + a = au_kzrealloc(au_warn_loopback_array,
  19764. + au_warn_loopback_nelem * sizeof(unsigned long),
  19765. + new_nelem * sizeof(unsigned long), GFP_ATOMIC);
  19766. + if (a) {
  19767. + au_warn_loopback_nelem = new_nelem;
  19768. + au_warn_loopback_array = a;
  19769. + a[i] = magic;
  19770. + goto pr;
  19771. + }
  19772. +
  19773. + spin_unlock(&spin);
  19774. + AuWarn1("realloc failed, ignored\n");
  19775. + return;
  19776. +
  19777. +pr:
  19778. + spin_unlock(&spin);
  19779. + pr_warn("you may want to try another patch for loopback file "
  19780. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  19781. +}
  19782. +
  19783. +int au_loopback_init(void)
  19784. +{
  19785. + int err;
  19786. + struct super_block *sb __maybe_unused;
  19787. +
  19788. + AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
  19789. +
  19790. + err = 0;
  19791. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  19792. + sizeof(unsigned long), GFP_NOFS);
  19793. + if (unlikely(!au_warn_loopback_array))
  19794. + err = -ENOMEM;
  19795. +
  19796. + return err;
  19797. +}
  19798. +
  19799. +void au_loopback_fin(void)
  19800. +{
  19801. + symbol_put(loop_backing_file);
  19802. + kfree(au_warn_loopback_array);
  19803. +}
  19804. diff -Nur linux-4.0.4.orig/fs/aufs/loop.h linux-4.0.4/fs/aufs/loop.h
  19805. --- linux-4.0.4.orig/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
  19806. +++ linux-4.0.4/fs/aufs/loop.h 2015-05-30 22:11:30.000000000 +0200
  19807. @@ -0,0 +1,39 @@
  19808. +/*
  19809. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  19810. + */
  19811. +
  19812. +/*
  19813. + * support for loopback mount as a branch
  19814. + */
  19815. +
  19816. +#ifndef __AUFS_LOOP_H__
  19817. +#define __AUFS_LOOP_H__
  19818. +
  19819. +#ifdef __KERNEL__
  19820. +
  19821. +struct dentry;
  19822. +struct super_block;
  19823. +
  19824. +#ifdef CONFIG_AUFS_BDEV_LOOP
  19825. +/* drivers/block/loop.c */
  19826. +struct file *loop_backing_file(struct super_block *sb);
  19827. +
  19828. +/* loop.c */
  19829. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  19830. +int au_test_loopback_kthread(void);
  19831. +void au_warn_loopback(struct super_block *h_sb);
  19832. +
  19833. +int au_loopback_init(void);
  19834. +void au_loopback_fin(void);
  19835. +#else
  19836. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  19837. + struct dentry *h_adding)
  19838. +AuStubInt0(au_test_loopback_kthread, void)
  19839. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  19840. +
  19841. +AuStubInt0(au_loopback_init, void)
  19842. +AuStubVoid(au_loopback_fin, void)
  19843. +#endif /* BLK_DEV_LOOP */
  19844. +
  19845. +#endif /* __KERNEL__ */
  19846. +#endif /* __AUFS_LOOP_H__ */
  19847. diff -Nur linux-4.0.4.orig/fs/aufs/magic.mk linux-4.0.4/fs/aufs/magic.mk
  19848. --- linux-4.0.4.orig/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
  19849. +++ linux-4.0.4/fs/aufs/magic.mk 2015-05-30 22:11:30.000000000 +0200
  19850. @@ -0,0 +1,30 @@
  19851. +
  19852. +# defined in ${srctree}/fs/fuse/inode.c
  19853. +# tristate
  19854. +ifdef CONFIG_FUSE_FS
  19855. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  19856. +endif
  19857. +
  19858. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  19859. +# tristate
  19860. +ifdef CONFIG_XFS_FS
  19861. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  19862. +endif
  19863. +
  19864. +# defined in ${srctree}/fs/configfs/mount.c
  19865. +# tristate
  19866. +ifdef CONFIG_CONFIGFS_FS
  19867. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  19868. +endif
  19869. +
  19870. +# defined in ${srctree}/fs/ubifs/ubifs.h
  19871. +# tristate
  19872. +ifdef CONFIG_UBIFS_FS
  19873. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  19874. +endif
  19875. +
  19876. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  19877. +# tristate
  19878. +ifdef CONFIG_HFSPLUS_FS
  19879. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  19880. +endif
  19881. diff -Nur linux-4.0.4.orig/fs/aufs/Makefile linux-4.0.4/fs/aufs/Makefile
  19882. --- linux-4.0.4.orig/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
  19883. +++ linux-4.0.4/fs/aufs/Makefile 2015-05-30 22:11:30.000000000 +0200
  19884. @@ -0,0 +1,36 @@
  19885. +
  19886. +include ${srctree}/${src}/magic.mk
  19887. +
  19888. +# cf. include/linux/kernel.h
  19889. +# enable pr_debug
  19890. +ccflags-y += -DDEBUG
  19891. +# sparse requires the full pathname
  19892. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  19893. +
  19894. +obj-$(CONFIG_AUFS_FS) += aufs.o
  19895. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  19896. + wkq.o vfsub.o dcsub.o \
  19897. + cpup.o whout.o wbr_policy.o \
  19898. + dinfo.o dentry.o \
  19899. + dynop.o \
  19900. + finfo.o file.o f_op.o \
  19901. + dir.o vdir.o \
  19902. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  19903. + mvdown.o ioctl.o
  19904. +
  19905. +# all are boolean
  19906. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  19907. +aufs-$(CONFIG_SYSFS) += sysfs.o
  19908. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  19909. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  19910. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  19911. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  19912. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  19913. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  19914. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  19915. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  19916. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  19917. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  19918. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  19919. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  19920. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  19921. diff -Nur linux-4.0.4.orig/fs/aufs/module.c linux-4.0.4/fs/aufs/module.c
  19922. --- linux-4.0.4.orig/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
  19923. +++ linux-4.0.4/fs/aufs/module.c 2015-05-30 22:11:30.000000000 +0200
  19924. @@ -0,0 +1,196 @@
  19925. +/*
  19926. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  19927. + */
  19928. +
  19929. +/*
  19930. + * module global variables and operations
  19931. + */
  19932. +
  19933. +#include <linux/module.h>
  19934. +#include <linux/seq_file.h>
  19935. +#include "aufs.h"
  19936. +
  19937. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
  19938. +{
  19939. + if (new_sz <= nused)
  19940. + return p;
  19941. +
  19942. + p = krealloc(p, new_sz, gfp);
  19943. + if (p)
  19944. + memset(p + nused, 0, new_sz - nused);
  19945. + return p;
  19946. +}
  19947. +
  19948. +/* ---------------------------------------------------------------------- */
  19949. +
  19950. +/*
  19951. + * aufs caches
  19952. + */
  19953. +struct kmem_cache *au_cachep[AuCache_Last];
  19954. +static int __init au_cache_init(void)
  19955. +{
  19956. + au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  19957. + if (au_cachep[AuCache_DINFO])
  19958. + /* SLAB_DESTROY_BY_RCU */
  19959. + au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  19960. + au_icntnr_init_once);
  19961. + if (au_cachep[AuCache_ICNTNR])
  19962. + au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
  19963. + au_fi_init_once);
  19964. + if (au_cachep[AuCache_FINFO])
  19965. + au_cachep[AuCache_VDIR] = AuCache(au_vdir);
  19966. + if (au_cachep[AuCache_VDIR])
  19967. + au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  19968. + if (au_cachep[AuCache_DEHSTR])
  19969. + return 0;
  19970. +
  19971. + return -ENOMEM;
  19972. +}
  19973. +
  19974. +static void au_cache_fin(void)
  19975. +{
  19976. + int i;
  19977. +
  19978. + /*
  19979. + * Make sure all delayed rcu free inodes are flushed before we
  19980. + * destroy cache.
  19981. + */
  19982. + rcu_barrier();
  19983. +
  19984. + /* excluding AuCache_HNOTIFY */
  19985. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  19986. + for (i = 0; i < AuCache_HNOTIFY; i++)
  19987. + if (au_cachep[i]) {
  19988. + kmem_cache_destroy(au_cachep[i]);
  19989. + au_cachep[i] = NULL;
  19990. + }
  19991. +}
  19992. +
  19993. +/* ---------------------------------------------------------------------- */
  19994. +
  19995. +int au_dir_roflags;
  19996. +
  19997. +#ifdef CONFIG_AUFS_SBILIST
  19998. +/*
  19999. + * iterate_supers_type() doesn't protect us from
  20000. + * remounting (branch management)
  20001. + */
  20002. +struct au_splhead au_sbilist;
  20003. +#endif
  20004. +
  20005. +struct lock_class_key au_lc_key[AuLcKey_Last];
  20006. +
  20007. +/*
  20008. + * functions for module interface.
  20009. + */
  20010. +MODULE_LICENSE("GPL");
  20011. +/* MODULE_LICENSE("GPL v2"); */
  20012. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  20013. +MODULE_DESCRIPTION(AUFS_NAME
  20014. + " -- Advanced multi layered unification filesystem");
  20015. +MODULE_VERSION(AUFS_VERSION);
  20016. +
  20017. +/* this module parameter has no meaning when SYSFS is disabled */
  20018. +int sysaufs_brs = 1;
  20019. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  20020. +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
  20021. +
  20022. +/* this module parameter has no meaning when USER_NS is disabled */
  20023. +static bool au_userns;
  20024. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  20025. +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
  20026. +
  20027. +/* ---------------------------------------------------------------------- */
  20028. +
  20029. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  20030. +
  20031. +int au_seq_path(struct seq_file *seq, struct path *path)
  20032. +{
  20033. + return seq_path(seq, path, au_esc_chars);
  20034. +}
  20035. +
  20036. +/* ---------------------------------------------------------------------- */
  20037. +
  20038. +static int __init aufs_init(void)
  20039. +{
  20040. + int err, i;
  20041. + char *p;
  20042. +
  20043. + p = au_esc_chars;
  20044. + for (i = 1; i <= ' '; i++)
  20045. + *p++ = i;
  20046. + *p++ = '\\';
  20047. + *p++ = '\x7f';
  20048. + *p = 0;
  20049. +
  20050. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  20051. +
  20052. + au_sbilist_init();
  20053. + sysaufs_brs_init();
  20054. + au_debug_init();
  20055. + au_dy_init();
  20056. + err = sysaufs_init();
  20057. + if (unlikely(err))
  20058. + goto out;
  20059. + err = au_procfs_init();
  20060. + if (unlikely(err))
  20061. + goto out_sysaufs;
  20062. + err = au_wkq_init();
  20063. + if (unlikely(err))
  20064. + goto out_procfs;
  20065. + err = au_loopback_init();
  20066. + if (unlikely(err))
  20067. + goto out_wkq;
  20068. + err = au_hnotify_init();
  20069. + if (unlikely(err))
  20070. + goto out_loopback;
  20071. + err = au_sysrq_init();
  20072. + if (unlikely(err))
  20073. + goto out_hin;
  20074. + err = au_cache_init();
  20075. + if (unlikely(err))
  20076. + goto out_sysrq;
  20077. +
  20078. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  20079. + err = register_filesystem(&aufs_fs_type);
  20080. + if (unlikely(err))
  20081. + goto out_cache;
  20082. +
  20083. + /* since we define pr_fmt, call printk directly */
  20084. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  20085. + goto out; /* success */
  20086. +
  20087. +out_cache:
  20088. + au_cache_fin();
  20089. +out_sysrq:
  20090. + au_sysrq_fin();
  20091. +out_hin:
  20092. + au_hnotify_fin();
  20093. +out_loopback:
  20094. + au_loopback_fin();
  20095. +out_wkq:
  20096. + au_wkq_fin();
  20097. +out_procfs:
  20098. + au_procfs_fin();
  20099. +out_sysaufs:
  20100. + sysaufs_fin();
  20101. + au_dy_fin();
  20102. +out:
  20103. + return err;
  20104. +}
  20105. +
  20106. +static void __exit aufs_exit(void)
  20107. +{
  20108. + unregister_filesystem(&aufs_fs_type);
  20109. + au_cache_fin();
  20110. + au_sysrq_fin();
  20111. + au_hnotify_fin();
  20112. + au_loopback_fin();
  20113. + au_wkq_fin();
  20114. + au_procfs_fin();
  20115. + sysaufs_fin();
  20116. + au_dy_fin();
  20117. +}
  20118. +
  20119. +module_init(aufs_init);
  20120. +module_exit(aufs_exit);
  20121. diff -Nur linux-4.0.4.orig/fs/aufs/module.h linux-4.0.4/fs/aufs/module.h
  20122. --- linux-4.0.4.orig/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
  20123. +++ linux-4.0.4/fs/aufs/module.h 2015-05-30 22:11:30.000000000 +0200
  20124. @@ -0,0 +1,91 @@
  20125. +/*
  20126. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  20127. + */
  20128. +
  20129. +/*
  20130. + * module initialization and module-global
  20131. + */
  20132. +
  20133. +#ifndef __AUFS_MODULE_H__
  20134. +#define __AUFS_MODULE_H__
  20135. +
  20136. +#ifdef __KERNEL__
  20137. +
  20138. +#include <linux/slab.h>
  20139. +
  20140. +struct path;
  20141. +struct seq_file;
  20142. +
  20143. +/* module parameters */
  20144. +extern int sysaufs_brs;
  20145. +
  20146. +/* ---------------------------------------------------------------------- */
  20147. +
  20148. +extern int au_dir_roflags;
  20149. +
  20150. +enum {
  20151. + AuLcNonDir_FIINFO,
  20152. + AuLcNonDir_DIINFO,
  20153. + AuLcNonDir_IIINFO,
  20154. +
  20155. + AuLcDir_FIINFO,
  20156. + AuLcDir_DIINFO,
  20157. + AuLcDir_IIINFO,
  20158. +
  20159. + AuLcSymlink_DIINFO,
  20160. + AuLcSymlink_IIINFO,
  20161. +
  20162. + AuLcKey_Last
  20163. +};
  20164. +extern struct lock_class_key au_lc_key[AuLcKey_Last];
  20165. +
  20166. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
  20167. +int au_seq_path(struct seq_file *seq, struct path *path);
  20168. +
  20169. +#ifdef CONFIG_PROC_FS
  20170. +/* procfs.c */
  20171. +int __init au_procfs_init(void);
  20172. +void au_procfs_fin(void);
  20173. +#else
  20174. +AuStubInt0(au_procfs_init, void);
  20175. +AuStubVoid(au_procfs_fin, void);
  20176. +#endif
  20177. +
  20178. +/* ---------------------------------------------------------------------- */
  20179. +
  20180. +/* kmem cache */
  20181. +enum {
  20182. + AuCache_DINFO,
  20183. + AuCache_ICNTNR,
  20184. + AuCache_FINFO,
  20185. + AuCache_VDIR,
  20186. + AuCache_DEHSTR,
  20187. + AuCache_HNOTIFY, /* must be last */
  20188. + AuCache_Last
  20189. +};
  20190. +
  20191. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  20192. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  20193. +#define AuCacheCtor(type, ctor) \
  20194. + kmem_cache_create(#type, sizeof(struct type), \
  20195. + __alignof__(struct type), AuCacheFlags, ctor)
  20196. +
  20197. +extern struct kmem_cache *au_cachep[];
  20198. +
  20199. +#define AuCacheFuncs(name, index) \
  20200. +static inline struct au_##name *au_cache_alloc_##name(void) \
  20201. +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
  20202. +static inline void au_cache_free_##name(struct au_##name *p) \
  20203. +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
  20204. +
  20205. +AuCacheFuncs(dinfo, DINFO);
  20206. +AuCacheFuncs(icntnr, ICNTNR);
  20207. +AuCacheFuncs(finfo, FINFO);
  20208. +AuCacheFuncs(vdir, VDIR);
  20209. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  20210. +#ifdef CONFIG_AUFS_HNOTIFY
  20211. +AuCacheFuncs(hnotify, HNOTIFY);
  20212. +#endif
  20213. +
  20214. +#endif /* __KERNEL__ */
  20215. +#endif /* __AUFS_MODULE_H__ */
  20216. diff -Nur linux-4.0.4.orig/fs/aufs/mvdown.c linux-4.0.4/fs/aufs/mvdown.c
  20217. --- linux-4.0.4.orig/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
  20218. +++ linux-4.0.4/fs/aufs/mvdown.c 2015-05-30 22:11:30.000000000 +0200
  20219. @@ -0,0 +1,681 @@
  20220. +/*
  20221. + * Copyright (C) 2011-2015 Junjiro R. Okajima
  20222. + */
  20223. +
  20224. +/*
  20225. + * move-down, opposite of copy-up
  20226. + */
  20227. +
  20228. +#include "aufs.h"
  20229. +
  20230. +struct au_mvd_args {
  20231. + struct {
  20232. + struct super_block *h_sb;
  20233. + struct dentry *h_parent;
  20234. + struct au_hinode *hdir;
  20235. + struct inode *h_dir, *h_inode;
  20236. + struct au_pin pin;
  20237. + } info[AUFS_MVDOWN_NARRAY];
  20238. +
  20239. + struct aufs_mvdown mvdown;
  20240. + struct dentry *dentry, *parent;
  20241. + struct inode *inode, *dir;
  20242. + struct super_block *sb;
  20243. + aufs_bindex_t bopq, bwh, bfound;
  20244. + unsigned char rename_lock;
  20245. +};
  20246. +
  20247. +#define mvd_errno mvdown.au_errno
  20248. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  20249. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  20250. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  20251. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  20252. +
  20253. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  20254. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  20255. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  20256. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  20257. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  20258. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  20259. +
  20260. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  20261. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  20262. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  20263. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  20264. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  20265. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  20266. +
  20267. +#define AU_MVD_PR(flag, ...) do { \
  20268. + if (flag) \
  20269. + pr_err(__VA_ARGS__); \
  20270. + } while (0)
  20271. +
  20272. +static int find_lower_writable(struct au_mvd_args *a)
  20273. +{
  20274. + struct super_block *sb;
  20275. + aufs_bindex_t bindex, bend;
  20276. + struct au_branch *br;
  20277. +
  20278. + sb = a->sb;
  20279. + bindex = a->mvd_bsrc;
  20280. + bend = au_sbend(sb);
  20281. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  20282. + for (bindex++; bindex <= bend; bindex++) {
  20283. + br = au_sbr(sb, bindex);
  20284. + if (au_br_fhsm(br->br_perm)
  20285. + && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
  20286. + return bindex;
  20287. + }
  20288. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  20289. + for (bindex++; bindex <= bend; bindex++) {
  20290. + br = au_sbr(sb, bindex);
  20291. + if (!au_br_rdonly(br))
  20292. + return bindex;
  20293. + }
  20294. + else
  20295. + for (bindex++; bindex <= bend; bindex++) {
  20296. + br = au_sbr(sb, bindex);
  20297. + if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
  20298. + if (au_br_rdonly(br))
  20299. + a->mvdown.flags
  20300. + |= AUFS_MVDOWN_ROLOWER_R;
  20301. + return bindex;
  20302. + }
  20303. + }
  20304. +
  20305. + return -1;
  20306. +}
  20307. +
  20308. +/* make the parent dir on bdst */
  20309. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  20310. +{
  20311. + int err;
  20312. +
  20313. + err = 0;
  20314. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  20315. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  20316. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  20317. + a->mvd_h_dst_parent = NULL;
  20318. + if (au_dbend(a->parent) >= a->mvd_bdst)
  20319. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  20320. + if (!a->mvd_h_dst_parent) {
  20321. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  20322. + if (unlikely(err)) {
  20323. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  20324. + goto out;
  20325. + }
  20326. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  20327. + }
  20328. +
  20329. +out:
  20330. + AuTraceErr(err);
  20331. + return err;
  20332. +}
  20333. +
  20334. +/* lock them all */
  20335. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  20336. +{
  20337. + int err;
  20338. + struct dentry *h_trap;
  20339. +
  20340. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  20341. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  20342. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  20343. + au_opt_udba(a->sb),
  20344. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  20345. + AuTraceErr(err);
  20346. + if (unlikely(err)) {
  20347. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  20348. + goto out;
  20349. + }
  20350. +
  20351. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  20352. + a->rename_lock = 0;
  20353. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  20354. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  20355. + au_opt_udba(a->sb),
  20356. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  20357. + err = au_do_pin(&a->mvd_pin_src);
  20358. + AuTraceErr(err);
  20359. + a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
  20360. + if (unlikely(err)) {
  20361. + AU_MVD_PR(dmsg, "pin_src failed\n");
  20362. + goto out_dst;
  20363. + }
  20364. + goto out; /* success */
  20365. + }
  20366. +
  20367. + a->rename_lock = 1;
  20368. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  20369. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  20370. + au_opt_udba(a->sb),
  20371. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  20372. + AuTraceErr(err);
  20373. + a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
  20374. + if (unlikely(err)) {
  20375. + AU_MVD_PR(dmsg, "pin_src failed\n");
  20376. + au_pin_hdir_lock(&a->mvd_pin_dst);
  20377. + goto out_dst;
  20378. + }
  20379. + au_pin_hdir_unlock(&a->mvd_pin_src);
  20380. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  20381. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  20382. + if (h_trap) {
  20383. + err = (h_trap != a->mvd_h_src_parent);
  20384. + if (err)
  20385. + err = (h_trap != a->mvd_h_dst_parent);
  20386. + }
  20387. + BUG_ON(err); /* it should never happen */
  20388. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  20389. + err = -EBUSY;
  20390. + AuTraceErr(err);
  20391. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  20392. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  20393. + au_pin_hdir_lock(&a->mvd_pin_src);
  20394. + au_unpin(&a->mvd_pin_src);
  20395. + au_pin_hdir_lock(&a->mvd_pin_dst);
  20396. + goto out_dst;
  20397. + }
  20398. + goto out; /* success */
  20399. +
  20400. +out_dst:
  20401. + au_unpin(&a->mvd_pin_dst);
  20402. +out:
  20403. + AuTraceErr(err);
  20404. + return err;
  20405. +}
  20406. +
  20407. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  20408. +{
  20409. + if (!a->rename_lock)
  20410. + au_unpin(&a->mvd_pin_src);
  20411. + else {
  20412. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  20413. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  20414. + au_pin_hdir_lock(&a->mvd_pin_src);
  20415. + au_unpin(&a->mvd_pin_src);
  20416. + au_pin_hdir_lock(&a->mvd_pin_dst);
  20417. + }
  20418. + au_unpin(&a->mvd_pin_dst);
  20419. +}
  20420. +
  20421. +/* copy-down the file */
  20422. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  20423. +{
  20424. + int err;
  20425. + struct au_cp_generic cpg = {
  20426. + .dentry = a->dentry,
  20427. + .bdst = a->mvd_bdst,
  20428. + .bsrc = a->mvd_bsrc,
  20429. + .len = -1,
  20430. + .pin = &a->mvd_pin_dst,
  20431. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  20432. + };
  20433. +
  20434. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  20435. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  20436. + au_fset_cpup(cpg.flags, OVERWRITE);
  20437. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  20438. + au_fset_cpup(cpg.flags, RWDST);
  20439. + err = au_sio_cpdown_simple(&cpg);
  20440. + if (unlikely(err))
  20441. + AU_MVD_PR(dmsg, "cpdown failed\n");
  20442. +
  20443. + AuTraceErr(err);
  20444. + return err;
  20445. +}
  20446. +
  20447. +/*
  20448. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  20449. + * were sleeping
  20450. + */
  20451. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  20452. +{
  20453. + int err;
  20454. + struct path h_path;
  20455. + struct au_branch *br;
  20456. + struct inode *delegated;
  20457. +
  20458. + br = au_sbr(a->sb, a->mvd_bdst);
  20459. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  20460. + err = PTR_ERR(h_path.dentry);
  20461. + if (IS_ERR(h_path.dentry)) {
  20462. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  20463. + goto out;
  20464. + }
  20465. +
  20466. + err = 0;
  20467. + if (h_path.dentry->d_inode) {
  20468. + h_path.mnt = au_br_mnt(br);
  20469. + delegated = NULL;
  20470. + err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
  20471. + &delegated, /*force*/0);
  20472. + if (unlikely(err == -EWOULDBLOCK)) {
  20473. + pr_warn("cannot retry for NFSv4 delegation"
  20474. + " for an internal unlink\n");
  20475. + iput(delegated);
  20476. + }
  20477. + if (unlikely(err))
  20478. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  20479. + }
  20480. + dput(h_path.dentry);
  20481. +
  20482. +out:
  20483. + AuTraceErr(err);
  20484. + return err;
  20485. +}
  20486. +
  20487. +/*
  20488. + * unlink the topmost h_dentry
  20489. + */
  20490. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  20491. +{
  20492. + int err;
  20493. + struct path h_path;
  20494. + struct inode *delegated;
  20495. +
  20496. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  20497. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  20498. + delegated = NULL;
  20499. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  20500. + if (unlikely(err == -EWOULDBLOCK)) {
  20501. + pr_warn("cannot retry for NFSv4 delegation"
  20502. + " for an internal unlink\n");
  20503. + iput(delegated);
  20504. + }
  20505. + if (unlikely(err))
  20506. + AU_MVD_PR(dmsg, "unlink failed\n");
  20507. +
  20508. + AuTraceErr(err);
  20509. + return err;
  20510. +}
  20511. +
  20512. +/* Since mvdown succeeded, we ignore an error of this function */
  20513. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  20514. +{
  20515. + int err;
  20516. + struct au_branch *br;
  20517. +
  20518. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  20519. + br = au_sbr(a->sb, a->mvd_bsrc);
  20520. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  20521. + if (!err) {
  20522. + br = au_sbr(a->sb, a->mvd_bdst);
  20523. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  20524. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  20525. + }
  20526. + if (!err)
  20527. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  20528. + else
  20529. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  20530. +}
  20531. +
  20532. +/*
  20533. + * copy-down the file and unlink the bsrc file.
  20534. + * - unlink the bdst whout if exist
  20535. + * - copy-down the file (with whtmp name and rename)
  20536. + * - unlink the bsrc file
  20537. + */
  20538. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  20539. +{
  20540. + int err;
  20541. +
  20542. + err = au_do_mkdir(dmsg, a);
  20543. + if (!err)
  20544. + err = au_do_lock(dmsg, a);
  20545. + if (unlikely(err))
  20546. + goto out;
  20547. +
  20548. + /*
  20549. + * do not revert the activities we made on bdst since they should be
  20550. + * harmless in aufs.
  20551. + */
  20552. +
  20553. + err = au_do_cpdown(dmsg, a);
  20554. + if (!err)
  20555. + err = au_do_unlink_wh(dmsg, a);
  20556. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  20557. + err = au_do_unlink(dmsg, a);
  20558. + if (unlikely(err))
  20559. + goto out_unlock;
  20560. +
  20561. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  20562. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  20563. + if (find_lower_writable(a) < 0)
  20564. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  20565. +
  20566. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  20567. + au_do_stfs(dmsg, a);
  20568. +
  20569. + /* maintain internal array */
  20570. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  20571. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  20572. + au_set_dbstart(a->dentry, a->mvd_bdst);
  20573. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  20574. + au_set_ibstart(a->inode, a->mvd_bdst);
  20575. + }
  20576. + if (au_dbend(a->dentry) < a->mvd_bdst)
  20577. + au_set_dbend(a->dentry, a->mvd_bdst);
  20578. + if (au_ibend(a->inode) < a->mvd_bdst)
  20579. + au_set_ibend(a->inode, a->mvd_bdst);
  20580. +
  20581. +out_unlock:
  20582. + au_do_unlock(dmsg, a);
  20583. +out:
  20584. + AuTraceErr(err);
  20585. + return err;
  20586. +}
  20587. +
  20588. +/* ---------------------------------------------------------------------- */
  20589. +
  20590. +/* make sure the file is idle */
  20591. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  20592. +{
  20593. + int err, plinked;
  20594. +
  20595. + err = 0;
  20596. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  20597. + if (au_dbstart(a->dentry) == a->mvd_bsrc
  20598. + && au_dcount(a->dentry) == 1
  20599. + && atomic_read(&a->inode->i_count) == 1
  20600. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  20601. + && (!plinked || !au_plink_test(a->inode))
  20602. + && a->inode->i_nlink == 1)
  20603. + goto out;
  20604. +
  20605. + err = -EBUSY;
  20606. + AU_MVD_PR(dmsg,
  20607. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  20608. + a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
  20609. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  20610. + a->mvd_h_src_inode->i_nlink,
  20611. + plinked, plinked ? au_plink_test(a->inode) : 0);
  20612. +
  20613. +out:
  20614. + AuTraceErr(err);
  20615. + return err;
  20616. +}
  20617. +
  20618. +/* make sure the parent dir is fine */
  20619. +static int au_mvd_args_parent(const unsigned char dmsg,
  20620. + struct au_mvd_args *a)
  20621. +{
  20622. + int err;
  20623. + aufs_bindex_t bindex;
  20624. +
  20625. + err = 0;
  20626. + if (unlikely(au_alive_dir(a->parent))) {
  20627. + err = -ENOENT;
  20628. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  20629. + goto out;
  20630. + }
  20631. +
  20632. + a->bopq = au_dbdiropq(a->parent);
  20633. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  20634. + AuDbg("b%d\n", bindex);
  20635. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  20636. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  20637. + err = -EINVAL;
  20638. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  20639. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  20640. + a->bopq, a->mvd_bdst);
  20641. + }
  20642. +
  20643. +out:
  20644. + AuTraceErr(err);
  20645. + return err;
  20646. +}
  20647. +
  20648. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  20649. + struct au_mvd_args *a)
  20650. +{
  20651. + int err;
  20652. + struct au_dinfo *dinfo, *tmp;
  20653. +
  20654. + /* lookup the next lower positive entry */
  20655. + err = -ENOMEM;
  20656. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  20657. + if (unlikely(!tmp))
  20658. + goto out;
  20659. +
  20660. + a->bfound = -1;
  20661. + a->bwh = -1;
  20662. + dinfo = au_di(a->dentry);
  20663. + au_di_cp(tmp, dinfo);
  20664. + au_di_swap(tmp, dinfo);
  20665. +
  20666. + /* returns the number of positive dentries */
  20667. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
  20668. + if (!err)
  20669. + a->bwh = au_dbwh(a->dentry);
  20670. + else if (err > 0)
  20671. + a->bfound = au_dbstart(a->dentry);
  20672. +
  20673. + au_di_swap(tmp, dinfo);
  20674. + au_rw_write_unlock(&tmp->di_rwsem);
  20675. + au_di_free(tmp);
  20676. + if (unlikely(err < 0))
  20677. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  20678. +
  20679. + /*
  20680. + * here, we have these cases.
  20681. + * bfound == -1
  20682. + * no positive dentry under bsrc. there are more sub-cases.
  20683. + * bwh < 0
  20684. + * there no whiteout, we can safely move-down.
  20685. + * bwh <= bsrc
  20686. + * impossible
  20687. + * bsrc < bwh && bwh < bdst
  20688. + * there is a whiteout on RO branch. cannot proceed.
  20689. + * bwh == bdst
  20690. + * there is a whiteout on the RW target branch. it should
  20691. + * be removed.
  20692. + * bdst < bwh
  20693. + * there is a whiteout somewhere unrelated branch.
  20694. + * -1 < bfound && bfound <= bsrc
  20695. + * impossible.
  20696. + * bfound < bdst
  20697. + * found, but it is on RO branch between bsrc and bdst. cannot
  20698. + * proceed.
  20699. + * bfound == bdst
  20700. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  20701. + * error.
  20702. + * bdst < bfound
  20703. + * found, after we create the file on bdst, it will be hidden.
  20704. + */
  20705. +
  20706. + AuDebugOn(a->bfound == -1
  20707. + && a->bwh != -1
  20708. + && a->bwh <= a->mvd_bsrc);
  20709. + AuDebugOn(-1 < a->bfound
  20710. + && a->bfound <= a->mvd_bsrc);
  20711. +
  20712. + err = -EINVAL;
  20713. + if (a->bfound == -1
  20714. + && a->mvd_bsrc < a->bwh
  20715. + && a->bwh != -1
  20716. + && a->bwh < a->mvd_bdst) {
  20717. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  20718. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  20719. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  20720. + goto out;
  20721. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  20722. + a->mvd_errno = EAU_MVDOWN_UPPER;
  20723. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  20724. + a->mvd_bdst, a->bfound);
  20725. + goto out;
  20726. + }
  20727. +
  20728. + err = 0; /* success */
  20729. +
  20730. +out:
  20731. + AuTraceErr(err);
  20732. + return err;
  20733. +}
  20734. +
  20735. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  20736. +{
  20737. + int err;
  20738. +
  20739. + err = 0;
  20740. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  20741. + && a->bfound == a->mvd_bdst)
  20742. + err = -EEXIST;
  20743. + AuTraceErr(err);
  20744. + return err;
  20745. +}
  20746. +
  20747. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  20748. +{
  20749. + int err;
  20750. + struct au_branch *br;
  20751. +
  20752. + err = -EISDIR;
  20753. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  20754. + goto out;
  20755. +
  20756. + err = -EINVAL;
  20757. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  20758. + a->mvd_bsrc = au_ibstart(a->inode);
  20759. + else {
  20760. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  20761. + if (unlikely(a->mvd_bsrc < 0
  20762. + || (a->mvd_bsrc < au_dbstart(a->dentry)
  20763. + || au_dbend(a->dentry) < a->mvd_bsrc
  20764. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  20765. + || (a->mvd_bsrc < au_ibstart(a->inode)
  20766. + || au_ibend(a->inode) < a->mvd_bsrc
  20767. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  20768. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  20769. + AU_MVD_PR(dmsg, "no upper\n");
  20770. + goto out;
  20771. + }
  20772. + }
  20773. + if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
  20774. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  20775. + AU_MVD_PR(dmsg, "on the bottom\n");
  20776. + goto out;
  20777. + }
  20778. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  20779. + br = au_sbr(a->sb, a->mvd_bsrc);
  20780. + err = au_br_rdonly(br);
  20781. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  20782. + if (unlikely(err))
  20783. + goto out;
  20784. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  20785. + || IS_APPEND(a->mvd_h_src_inode))) {
  20786. + if (err)
  20787. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  20788. + /* go on */
  20789. + } else
  20790. + goto out;
  20791. +
  20792. + err = -EINVAL;
  20793. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  20794. + a->mvd_bdst = find_lower_writable(a);
  20795. + if (unlikely(a->mvd_bdst < 0)) {
  20796. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  20797. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  20798. + goto out;
  20799. + }
  20800. + } else {
  20801. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  20802. + if (unlikely(a->mvd_bdst < 0
  20803. + || au_sbend(a->sb) < a->mvd_bdst)) {
  20804. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  20805. + AU_MVD_PR(dmsg, "no lower brid\n");
  20806. + goto out;
  20807. + }
  20808. + }
  20809. +
  20810. + err = au_mvd_args_busy(dmsg, a);
  20811. + if (!err)
  20812. + err = au_mvd_args_parent(dmsg, a);
  20813. + if (!err)
  20814. + err = au_mvd_args_intermediate(dmsg, a);
  20815. + if (!err)
  20816. + err = au_mvd_args_exist(dmsg, a);
  20817. + if (!err)
  20818. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  20819. +
  20820. +out:
  20821. + AuTraceErr(err);
  20822. + return err;
  20823. +}
  20824. +
  20825. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  20826. +{
  20827. + int err, e;
  20828. + unsigned char dmsg;
  20829. + struct au_mvd_args *args;
  20830. +
  20831. + err = -EPERM;
  20832. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  20833. + goto out;
  20834. +
  20835. + err = -ENOMEM;
  20836. + args = kmalloc(sizeof(*args), GFP_NOFS);
  20837. + if (unlikely(!args))
  20838. + goto out;
  20839. +
  20840. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  20841. + if (!err)
  20842. + err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
  20843. + if (unlikely(err)) {
  20844. + err = -EFAULT;
  20845. + AuTraceErr(err);
  20846. + goto out_free;
  20847. + }
  20848. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  20849. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  20850. + args->mvdown.au_errno = 0;
  20851. + args->dentry = dentry;
  20852. + args->inode = dentry->d_inode;
  20853. + args->sb = dentry->d_sb;
  20854. +
  20855. + err = -ENOENT;
  20856. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  20857. + args->parent = dget_parent(dentry);
  20858. + args->dir = args->parent->d_inode;
  20859. + mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
  20860. + dput(args->parent);
  20861. + if (unlikely(args->parent != dentry->d_parent)) {
  20862. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  20863. + goto out_dir;
  20864. + }
  20865. +
  20866. + mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
  20867. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
  20868. + if (unlikely(err))
  20869. + goto out_inode;
  20870. +
  20871. + di_write_lock_parent(args->parent);
  20872. + err = au_mvd_args(dmsg, args);
  20873. + if (unlikely(err))
  20874. + goto out_parent;
  20875. +
  20876. + err = au_do_mvdown(dmsg, args);
  20877. + if (unlikely(err))
  20878. + goto out_parent;
  20879. +
  20880. + au_cpup_attr_timesizes(args->dir);
  20881. + au_cpup_attr_timesizes(args->inode);
  20882. + au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
  20883. + /* au_digen_dec(dentry); */
  20884. +
  20885. +out_parent:
  20886. + di_write_unlock(args->parent);
  20887. + aufs_read_unlock(dentry, AuLock_DW);
  20888. +out_inode:
  20889. + mutex_unlock(&args->inode->i_mutex);
  20890. +out_dir:
  20891. + mutex_unlock(&args->dir->i_mutex);
  20892. +out_free:
  20893. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  20894. + if (unlikely(e))
  20895. + err = -EFAULT;
  20896. + kfree(args);
  20897. +out:
  20898. + AuTraceErr(err);
  20899. + return err;
  20900. +}
  20901. diff -Nur linux-4.0.4.orig/fs/aufs/opts.c linux-4.0.4/fs/aufs/opts.c
  20902. --- linux-4.0.4.orig/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
  20903. +++ linux-4.0.4/fs/aufs/opts.c 2015-05-30 22:11:30.000000000 +0200
  20904. @@ -0,0 +1,1822 @@
  20905. +/*
  20906. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  20907. + */
  20908. +
  20909. +/*
  20910. + * mount options/flags
  20911. + */
  20912. +
  20913. +#include <linux/namei.h>
  20914. +#include <linux/types.h> /* a distribution requires */
  20915. +#include <linux/parser.h>
  20916. +#include "aufs.h"
  20917. +
  20918. +/* ---------------------------------------------------------------------- */
  20919. +
  20920. +enum {
  20921. + Opt_br,
  20922. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  20923. + Opt_idel, Opt_imod,
  20924. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  20925. + Opt_rdblk_def, Opt_rdhash_def,
  20926. + Opt_xino, Opt_noxino,
  20927. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  20928. + Opt_trunc_xino_path, Opt_itrunc_xino,
  20929. + Opt_trunc_xib, Opt_notrunc_xib,
  20930. + Opt_shwh, Opt_noshwh,
  20931. + Opt_plink, Opt_noplink, Opt_list_plink,
  20932. + Opt_udba,
  20933. + Opt_dio, Opt_nodio,
  20934. + Opt_diropq_a, Opt_diropq_w,
  20935. + Opt_warn_perm, Opt_nowarn_perm,
  20936. + Opt_wbr_copyup, Opt_wbr_create,
  20937. + Opt_fhsm_sec,
  20938. + Opt_verbose, Opt_noverbose,
  20939. + Opt_sum, Opt_nosum, Opt_wsum,
  20940. + Opt_dirperm1, Opt_nodirperm1,
  20941. + Opt_acl, Opt_noacl,
  20942. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  20943. +};
  20944. +
  20945. +static match_table_t options = {
  20946. + {Opt_br, "br=%s"},
  20947. + {Opt_br, "br:%s"},
  20948. +
  20949. + {Opt_add, "add=%d:%s"},
  20950. + {Opt_add, "add:%d:%s"},
  20951. + {Opt_add, "ins=%d:%s"},
  20952. + {Opt_add, "ins:%d:%s"},
  20953. + {Opt_append, "append=%s"},
  20954. + {Opt_append, "append:%s"},
  20955. + {Opt_prepend, "prepend=%s"},
  20956. + {Opt_prepend, "prepend:%s"},
  20957. +
  20958. + {Opt_del, "del=%s"},
  20959. + {Opt_del, "del:%s"},
  20960. + /* {Opt_idel, "idel:%d"}, */
  20961. + {Opt_mod, "mod=%s"},
  20962. + {Opt_mod, "mod:%s"},
  20963. + /* {Opt_imod, "imod:%d:%s"}, */
  20964. +
  20965. + {Opt_dirwh, "dirwh=%d"},
  20966. +
  20967. + {Opt_xino, "xino=%s"},
  20968. + {Opt_noxino, "noxino"},
  20969. + {Opt_trunc_xino, "trunc_xino"},
  20970. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  20971. + {Opt_notrunc_xino, "notrunc_xino"},
  20972. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  20973. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  20974. + /* {Opt_zxino, "zxino=%s"}, */
  20975. + {Opt_trunc_xib, "trunc_xib"},
  20976. + {Opt_notrunc_xib, "notrunc_xib"},
  20977. +
  20978. +#ifdef CONFIG_PROC_FS
  20979. + {Opt_plink, "plink"},
  20980. +#else
  20981. + {Opt_ignore_silent, "plink"},
  20982. +#endif
  20983. +
  20984. + {Opt_noplink, "noplink"},
  20985. +
  20986. +#ifdef CONFIG_AUFS_DEBUG
  20987. + {Opt_list_plink, "list_plink"},
  20988. +#endif
  20989. +
  20990. + {Opt_udba, "udba=%s"},
  20991. +
  20992. + {Opt_dio, "dio"},
  20993. + {Opt_nodio, "nodio"},
  20994. +
  20995. +#ifdef CONFIG_AUFS_FHSM
  20996. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  20997. +#else
  20998. + {Opt_ignore_silent, "fhsm_sec=%d"},
  20999. +#endif
  21000. +
  21001. + {Opt_diropq_a, "diropq=always"},
  21002. + {Opt_diropq_a, "diropq=a"},
  21003. + {Opt_diropq_w, "diropq=whiteouted"},
  21004. + {Opt_diropq_w, "diropq=w"},
  21005. +
  21006. + {Opt_warn_perm, "warn_perm"},
  21007. + {Opt_nowarn_perm, "nowarn_perm"},
  21008. +
  21009. + /* keep them temporary */
  21010. + {Opt_ignore_silent, "nodlgt"},
  21011. + {Opt_ignore_silent, "clean_plink"},
  21012. +
  21013. +#ifdef CONFIG_AUFS_SHWH
  21014. + {Opt_shwh, "shwh"},
  21015. +#endif
  21016. + {Opt_noshwh, "noshwh"},
  21017. +
  21018. + {Opt_dirperm1, "dirperm1"},
  21019. + {Opt_nodirperm1, "nodirperm1"},
  21020. +
  21021. + {Opt_verbose, "verbose"},
  21022. + {Opt_verbose, "v"},
  21023. + {Opt_noverbose, "noverbose"},
  21024. + {Opt_noverbose, "quiet"},
  21025. + {Opt_noverbose, "q"},
  21026. + {Opt_noverbose, "silent"},
  21027. +
  21028. + {Opt_sum, "sum"},
  21029. + {Opt_nosum, "nosum"},
  21030. + {Opt_wsum, "wsum"},
  21031. +
  21032. + {Opt_rdcache, "rdcache=%d"},
  21033. + {Opt_rdblk, "rdblk=%d"},
  21034. + {Opt_rdblk_def, "rdblk=def"},
  21035. + {Opt_rdhash, "rdhash=%d"},
  21036. + {Opt_rdhash_def, "rdhash=def"},
  21037. +
  21038. + {Opt_wbr_create, "create=%s"},
  21039. + {Opt_wbr_create, "create_policy=%s"},
  21040. + {Opt_wbr_copyup, "cpup=%s"},
  21041. + {Opt_wbr_copyup, "copyup=%s"},
  21042. + {Opt_wbr_copyup, "copyup_policy=%s"},
  21043. +
  21044. + /* generic VFS flag */
  21045. +#ifdef CONFIG_FS_POSIX_ACL
  21046. + {Opt_acl, "acl"},
  21047. + {Opt_noacl, "noacl"},
  21048. +#else
  21049. + {Opt_ignore_silent, "acl"},
  21050. + {Opt_ignore_silent, "noacl"},
  21051. +#endif
  21052. +
  21053. + /* internal use for the scripts */
  21054. + {Opt_ignore_silent, "si=%s"},
  21055. +
  21056. + {Opt_br, "dirs=%s"},
  21057. + {Opt_ignore, "debug=%d"},
  21058. + {Opt_ignore, "delete=whiteout"},
  21059. + {Opt_ignore, "delete=all"},
  21060. + {Opt_ignore, "imap=%s"},
  21061. +
  21062. + /* temporary workaround, due to old mount(8)? */
  21063. + {Opt_ignore_silent, "relatime"},
  21064. +
  21065. + {Opt_err, NULL}
  21066. +};
  21067. +
  21068. +/* ---------------------------------------------------------------------- */
  21069. +
  21070. +static const char *au_parser_pattern(int val, match_table_t tbl)
  21071. +{
  21072. + struct match_token *p;
  21073. +
  21074. + p = tbl;
  21075. + while (p->pattern) {
  21076. + if (p->token == val)
  21077. + return p->pattern;
  21078. + p++;
  21079. + }
  21080. + BUG();
  21081. + return "??";
  21082. +}
  21083. +
  21084. +static const char *au_optstr(int *val, match_table_t tbl)
  21085. +{
  21086. + struct match_token *p;
  21087. + int v;
  21088. +
  21089. + v = *val;
  21090. + if (!v)
  21091. + goto out;
  21092. + p = tbl;
  21093. + while (p->pattern) {
  21094. + if (p->token
  21095. + && (v & p->token) == p->token) {
  21096. + *val &= ~p->token;
  21097. + return p->pattern;
  21098. + }
  21099. + p++;
  21100. + }
  21101. +
  21102. +out:
  21103. + return NULL;
  21104. +}
  21105. +
  21106. +/* ---------------------------------------------------------------------- */
  21107. +
  21108. +static match_table_t brperm = {
  21109. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  21110. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  21111. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  21112. + {0, NULL}
  21113. +};
  21114. +
  21115. +static match_table_t brattr = {
  21116. + /* general */
  21117. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  21118. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  21119. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  21120. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  21121. +#ifdef CONFIG_AUFS_FHSM
  21122. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  21123. +#endif
  21124. +#ifdef CONFIG_AUFS_XATTR
  21125. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  21126. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  21127. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  21128. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  21129. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  21130. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  21131. +#endif
  21132. +
  21133. + /* ro/rr branch */
  21134. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  21135. +
  21136. + /* rw branch */
  21137. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  21138. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  21139. +
  21140. + {0, NULL}
  21141. +};
  21142. +
  21143. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  21144. +{
  21145. + int attr, v;
  21146. + char *p;
  21147. +
  21148. + attr = 0;
  21149. + do {
  21150. + p = strchr(str, '+');
  21151. + if (p)
  21152. + *p = 0;
  21153. + v = match_token(str, table, args);
  21154. + if (v) {
  21155. + if (v & AuBrAttr_CMOO_Mask)
  21156. + attr &= ~AuBrAttr_CMOO_Mask;
  21157. + attr |= v;
  21158. + } else {
  21159. + if (p)
  21160. + *p = '+';
  21161. + pr_warn("ignored branch attribute %s\n", str);
  21162. + break;
  21163. + }
  21164. + if (p)
  21165. + str = p + 1;
  21166. + } while (p);
  21167. +
  21168. + return attr;
  21169. +}
  21170. +
  21171. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  21172. +{
  21173. + int sz;
  21174. + const char *p;
  21175. + char *q;
  21176. +
  21177. + q = str->a;
  21178. + *q = 0;
  21179. + p = au_optstr(&perm, brattr);
  21180. + if (p) {
  21181. + sz = strlen(p);
  21182. + memcpy(q, p, sz + 1);
  21183. + q += sz;
  21184. + } else
  21185. + goto out;
  21186. +
  21187. + do {
  21188. + p = au_optstr(&perm, brattr);
  21189. + if (p) {
  21190. + *q++ = '+';
  21191. + sz = strlen(p);
  21192. + memcpy(q, p, sz + 1);
  21193. + q += sz;
  21194. + }
  21195. + } while (p);
  21196. +
  21197. +out:
  21198. + return q - str->a;
  21199. +}
  21200. +
  21201. +static int noinline_for_stack br_perm_val(char *perm)
  21202. +{
  21203. + int val, bad, sz;
  21204. + char *p;
  21205. + substring_t args[MAX_OPT_ARGS];
  21206. + au_br_perm_str_t attr;
  21207. +
  21208. + p = strchr(perm, '+');
  21209. + if (p)
  21210. + *p = 0;
  21211. + val = match_token(perm, brperm, args);
  21212. + if (!val) {
  21213. + if (p)
  21214. + *p = '+';
  21215. + pr_warn("ignored branch permission %s\n", perm);
  21216. + val = AuBrPerm_RO;
  21217. + goto out;
  21218. + }
  21219. + if (!p)
  21220. + goto out;
  21221. +
  21222. + val |= br_attr_val(p + 1, brattr, args);
  21223. +
  21224. + bad = 0;
  21225. + switch (val & AuBrPerm_Mask) {
  21226. + case AuBrPerm_RO:
  21227. + case AuBrPerm_RR:
  21228. + bad = val & AuBrWAttr_Mask;
  21229. + val &= ~AuBrWAttr_Mask;
  21230. + break;
  21231. + case AuBrPerm_RW:
  21232. + bad = val & AuBrRAttr_Mask;
  21233. + val &= ~AuBrRAttr_Mask;
  21234. + break;
  21235. + }
  21236. +
  21237. + /*
  21238. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  21239. + * does not treat it as an error, just warning.
  21240. + * this is a tiny guard for the user operation.
  21241. + */
  21242. + if (val & AuBrAttr_UNPIN) {
  21243. + bad |= AuBrAttr_UNPIN;
  21244. + val &= ~AuBrAttr_UNPIN;
  21245. + }
  21246. +
  21247. + if (unlikely(bad)) {
  21248. + sz = au_do_optstr_br_attr(&attr, bad);
  21249. + AuDebugOn(!sz);
  21250. + pr_warn("ignored branch attribute %s\n", attr.a);
  21251. + }
  21252. +
  21253. +out:
  21254. + return val;
  21255. +}
  21256. +
  21257. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  21258. +{
  21259. + au_br_perm_str_t attr;
  21260. + const char *p;
  21261. + char *q;
  21262. + int sz;
  21263. +
  21264. + q = str->a;
  21265. + p = au_optstr(&perm, brperm);
  21266. + AuDebugOn(!p || !*p);
  21267. + sz = strlen(p);
  21268. + memcpy(q, p, sz + 1);
  21269. + q += sz;
  21270. +
  21271. + sz = au_do_optstr_br_attr(&attr, perm);
  21272. + if (sz) {
  21273. + *q++ = '+';
  21274. + memcpy(q, attr.a, sz + 1);
  21275. + }
  21276. +
  21277. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  21278. +}
  21279. +
  21280. +/* ---------------------------------------------------------------------- */
  21281. +
  21282. +static match_table_t udbalevel = {
  21283. + {AuOpt_UDBA_REVAL, "reval"},
  21284. + {AuOpt_UDBA_NONE, "none"},
  21285. +#ifdef CONFIG_AUFS_HNOTIFY
  21286. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  21287. +#ifdef CONFIG_AUFS_HFSNOTIFY
  21288. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  21289. +#endif
  21290. +#endif
  21291. + {-1, NULL}
  21292. +};
  21293. +
  21294. +static int noinline_for_stack udba_val(char *str)
  21295. +{
  21296. + substring_t args[MAX_OPT_ARGS];
  21297. +
  21298. + return match_token(str, udbalevel, args);
  21299. +}
  21300. +
  21301. +const char *au_optstr_udba(int udba)
  21302. +{
  21303. + return au_parser_pattern(udba, udbalevel);
  21304. +}
  21305. +
  21306. +/* ---------------------------------------------------------------------- */
  21307. +
  21308. +static match_table_t au_wbr_create_policy = {
  21309. + {AuWbrCreate_TDP, "tdp"},
  21310. + {AuWbrCreate_TDP, "top-down-parent"},
  21311. + {AuWbrCreate_RR, "rr"},
  21312. + {AuWbrCreate_RR, "round-robin"},
  21313. + {AuWbrCreate_MFS, "mfs"},
  21314. + {AuWbrCreate_MFS, "most-free-space"},
  21315. + {AuWbrCreate_MFSV, "mfs:%d"},
  21316. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  21317. +
  21318. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  21319. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  21320. + {AuWbrCreate_PMFS, "pmfs"},
  21321. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  21322. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  21323. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  21324. +
  21325. + {-1, NULL}
  21326. +};
  21327. +
  21328. +/*
  21329. + * cf. linux/lib/parser.c and cmdline.c
  21330. + * gave up calling memparse() since it uses simple_strtoull() instead of
  21331. + * kstrto...().
  21332. + */
  21333. +static int noinline_for_stack
  21334. +au_match_ull(substring_t *s, unsigned long long *result)
  21335. +{
  21336. + int err;
  21337. + unsigned int len;
  21338. + char a[32];
  21339. +
  21340. + err = -ERANGE;
  21341. + len = s->to - s->from;
  21342. + if (len + 1 <= sizeof(a)) {
  21343. + memcpy(a, s->from, len);
  21344. + a[len] = '\0';
  21345. + err = kstrtoull(a, 0, result);
  21346. + }
  21347. + return err;
  21348. +}
  21349. +
  21350. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  21351. + struct au_opt_wbr_create *create)
  21352. +{
  21353. + int err;
  21354. + unsigned long long ull;
  21355. +
  21356. + err = 0;
  21357. + if (!au_match_ull(arg, &ull))
  21358. + create->mfsrr_watermark = ull;
  21359. + else {
  21360. + pr_err("bad integer in %s\n", str);
  21361. + err = -EINVAL;
  21362. + }
  21363. +
  21364. + return err;
  21365. +}
  21366. +
  21367. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  21368. + struct au_opt_wbr_create *create)
  21369. +{
  21370. + int n, err;
  21371. +
  21372. + err = 0;
  21373. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  21374. + create->mfs_second = n;
  21375. + else {
  21376. + pr_err("bad integer in %s\n", str);
  21377. + err = -EINVAL;
  21378. + }
  21379. +
  21380. + return err;
  21381. +}
  21382. +
  21383. +static int noinline_for_stack
  21384. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  21385. +{
  21386. + int err, e;
  21387. + substring_t args[MAX_OPT_ARGS];
  21388. +
  21389. + err = match_token(str, au_wbr_create_policy, args);
  21390. + create->wbr_create = err;
  21391. + switch (err) {
  21392. + case AuWbrCreate_MFSRRV:
  21393. + case AuWbrCreate_PMFSRRV:
  21394. + e = au_wbr_mfs_wmark(&args[0], str, create);
  21395. + if (!e)
  21396. + e = au_wbr_mfs_sec(&args[1], str, create);
  21397. + if (unlikely(e))
  21398. + err = e;
  21399. + break;
  21400. + case AuWbrCreate_MFSRR:
  21401. + case AuWbrCreate_PMFSRR:
  21402. + e = au_wbr_mfs_wmark(&args[0], str, create);
  21403. + if (unlikely(e)) {
  21404. + err = e;
  21405. + break;
  21406. + }
  21407. + /*FALLTHROUGH*/
  21408. + case AuWbrCreate_MFS:
  21409. + case AuWbrCreate_PMFS:
  21410. + create->mfs_second = AUFS_MFS_DEF_SEC;
  21411. + break;
  21412. + case AuWbrCreate_MFSV:
  21413. + case AuWbrCreate_PMFSV:
  21414. + e = au_wbr_mfs_sec(&args[0], str, create);
  21415. + if (unlikely(e))
  21416. + err = e;
  21417. + break;
  21418. + }
  21419. +
  21420. + return err;
  21421. +}
  21422. +
  21423. +const char *au_optstr_wbr_create(int wbr_create)
  21424. +{
  21425. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  21426. +}
  21427. +
  21428. +static match_table_t au_wbr_copyup_policy = {
  21429. + {AuWbrCopyup_TDP, "tdp"},
  21430. + {AuWbrCopyup_TDP, "top-down-parent"},
  21431. + {AuWbrCopyup_BUP, "bup"},
  21432. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  21433. + {AuWbrCopyup_BU, "bu"},
  21434. + {AuWbrCopyup_BU, "bottom-up"},
  21435. + {-1, NULL}
  21436. +};
  21437. +
  21438. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  21439. +{
  21440. + substring_t args[MAX_OPT_ARGS];
  21441. +
  21442. + return match_token(str, au_wbr_copyup_policy, args);
  21443. +}
  21444. +
  21445. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  21446. +{
  21447. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  21448. +}
  21449. +
  21450. +/* ---------------------------------------------------------------------- */
  21451. +
  21452. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  21453. +
  21454. +static void dump_opts(struct au_opts *opts)
  21455. +{
  21456. +#ifdef CONFIG_AUFS_DEBUG
  21457. + /* reduce stack space */
  21458. + union {
  21459. + struct au_opt_add *add;
  21460. + struct au_opt_del *del;
  21461. + struct au_opt_mod *mod;
  21462. + struct au_opt_xino *xino;
  21463. + struct au_opt_xino_itrunc *xino_itrunc;
  21464. + struct au_opt_wbr_create *create;
  21465. + } u;
  21466. + struct au_opt *opt;
  21467. +
  21468. + opt = opts->opt;
  21469. + while (opt->type != Opt_tail) {
  21470. + switch (opt->type) {
  21471. + case Opt_add:
  21472. + u.add = &opt->add;
  21473. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  21474. + u.add->bindex, u.add->pathname, u.add->perm,
  21475. + u.add->path.dentry);
  21476. + break;
  21477. + case Opt_del:
  21478. + case Opt_idel:
  21479. + u.del = &opt->del;
  21480. + AuDbg("del {%s, %p}\n",
  21481. + u.del->pathname, u.del->h_path.dentry);
  21482. + break;
  21483. + case Opt_mod:
  21484. + case Opt_imod:
  21485. + u.mod = &opt->mod;
  21486. + AuDbg("mod {%s, 0x%x, %p}\n",
  21487. + u.mod->path, u.mod->perm, u.mod->h_root);
  21488. + break;
  21489. + case Opt_append:
  21490. + u.add = &opt->add;
  21491. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  21492. + u.add->bindex, u.add->pathname, u.add->perm,
  21493. + u.add->path.dentry);
  21494. + break;
  21495. + case Opt_prepend:
  21496. + u.add = &opt->add;
  21497. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  21498. + u.add->bindex, u.add->pathname, u.add->perm,
  21499. + u.add->path.dentry);
  21500. + break;
  21501. + case Opt_dirwh:
  21502. + AuDbg("dirwh %d\n", opt->dirwh);
  21503. + break;
  21504. + case Opt_rdcache:
  21505. + AuDbg("rdcache %d\n", opt->rdcache);
  21506. + break;
  21507. + case Opt_rdblk:
  21508. + AuDbg("rdblk %u\n", opt->rdblk);
  21509. + break;
  21510. + case Opt_rdblk_def:
  21511. + AuDbg("rdblk_def\n");
  21512. + break;
  21513. + case Opt_rdhash:
  21514. + AuDbg("rdhash %u\n", opt->rdhash);
  21515. + break;
  21516. + case Opt_rdhash_def:
  21517. + AuDbg("rdhash_def\n");
  21518. + break;
  21519. + case Opt_xino:
  21520. + u.xino = &opt->xino;
  21521. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  21522. + break;
  21523. + case Opt_trunc_xino:
  21524. + AuLabel(trunc_xino);
  21525. + break;
  21526. + case Opt_notrunc_xino:
  21527. + AuLabel(notrunc_xino);
  21528. + break;
  21529. + case Opt_trunc_xino_path:
  21530. + case Opt_itrunc_xino:
  21531. + u.xino_itrunc = &opt->xino_itrunc;
  21532. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  21533. + break;
  21534. + case Opt_noxino:
  21535. + AuLabel(noxino);
  21536. + break;
  21537. + case Opt_trunc_xib:
  21538. + AuLabel(trunc_xib);
  21539. + break;
  21540. + case Opt_notrunc_xib:
  21541. + AuLabel(notrunc_xib);
  21542. + break;
  21543. + case Opt_shwh:
  21544. + AuLabel(shwh);
  21545. + break;
  21546. + case Opt_noshwh:
  21547. + AuLabel(noshwh);
  21548. + break;
  21549. + case Opt_dirperm1:
  21550. + AuLabel(dirperm1);
  21551. + break;
  21552. + case Opt_nodirperm1:
  21553. + AuLabel(nodirperm1);
  21554. + break;
  21555. + case Opt_plink:
  21556. + AuLabel(plink);
  21557. + break;
  21558. + case Opt_noplink:
  21559. + AuLabel(noplink);
  21560. + break;
  21561. + case Opt_list_plink:
  21562. + AuLabel(list_plink);
  21563. + break;
  21564. + case Opt_udba:
  21565. + AuDbg("udba %d, %s\n",
  21566. + opt->udba, au_optstr_udba(opt->udba));
  21567. + break;
  21568. + case Opt_dio:
  21569. + AuLabel(dio);
  21570. + break;
  21571. + case Opt_nodio:
  21572. + AuLabel(nodio);
  21573. + break;
  21574. + case Opt_diropq_a:
  21575. + AuLabel(diropq_a);
  21576. + break;
  21577. + case Opt_diropq_w:
  21578. + AuLabel(diropq_w);
  21579. + break;
  21580. + case Opt_warn_perm:
  21581. + AuLabel(warn_perm);
  21582. + break;
  21583. + case Opt_nowarn_perm:
  21584. + AuLabel(nowarn_perm);
  21585. + break;
  21586. + case Opt_verbose:
  21587. + AuLabel(verbose);
  21588. + break;
  21589. + case Opt_noverbose:
  21590. + AuLabel(noverbose);
  21591. + break;
  21592. + case Opt_sum:
  21593. + AuLabel(sum);
  21594. + break;
  21595. + case Opt_nosum:
  21596. + AuLabel(nosum);
  21597. + break;
  21598. + case Opt_wsum:
  21599. + AuLabel(wsum);
  21600. + break;
  21601. + case Opt_wbr_create:
  21602. + u.create = &opt->wbr_create;
  21603. + AuDbg("create %d, %s\n", u.create->wbr_create,
  21604. + au_optstr_wbr_create(u.create->wbr_create));
  21605. + switch (u.create->wbr_create) {
  21606. + case AuWbrCreate_MFSV:
  21607. + case AuWbrCreate_PMFSV:
  21608. + AuDbg("%d sec\n", u.create->mfs_second);
  21609. + break;
  21610. + case AuWbrCreate_MFSRR:
  21611. + AuDbg("%llu watermark\n",
  21612. + u.create->mfsrr_watermark);
  21613. + break;
  21614. + case AuWbrCreate_MFSRRV:
  21615. + case AuWbrCreate_PMFSRRV:
  21616. + AuDbg("%llu watermark, %d sec\n",
  21617. + u.create->mfsrr_watermark,
  21618. + u.create->mfs_second);
  21619. + break;
  21620. + }
  21621. + break;
  21622. + case Opt_wbr_copyup:
  21623. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  21624. + au_optstr_wbr_copyup(opt->wbr_copyup));
  21625. + break;
  21626. + case Opt_fhsm_sec:
  21627. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  21628. + break;
  21629. + case Opt_acl:
  21630. + AuLabel(acl);
  21631. + break;
  21632. + case Opt_noacl:
  21633. + AuLabel(noacl);
  21634. + break;
  21635. + default:
  21636. + BUG();
  21637. + }
  21638. + opt++;
  21639. + }
  21640. +#endif
  21641. +}
  21642. +
  21643. +void au_opts_free(struct au_opts *opts)
  21644. +{
  21645. + struct au_opt *opt;
  21646. +
  21647. + opt = opts->opt;
  21648. + while (opt->type != Opt_tail) {
  21649. + switch (opt->type) {
  21650. + case Opt_add:
  21651. + case Opt_append:
  21652. + case Opt_prepend:
  21653. + path_put(&opt->add.path);
  21654. + break;
  21655. + case Opt_del:
  21656. + case Opt_idel:
  21657. + path_put(&opt->del.h_path);
  21658. + break;
  21659. + case Opt_mod:
  21660. + case Opt_imod:
  21661. + dput(opt->mod.h_root);
  21662. + break;
  21663. + case Opt_xino:
  21664. + fput(opt->xino.file);
  21665. + break;
  21666. + }
  21667. + opt++;
  21668. + }
  21669. +}
  21670. +
  21671. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  21672. + aufs_bindex_t bindex)
  21673. +{
  21674. + int err;
  21675. + struct au_opt_add *add = &opt->add;
  21676. + char *p;
  21677. +
  21678. + add->bindex = bindex;
  21679. + add->perm = AuBrPerm_RO;
  21680. + add->pathname = opt_str;
  21681. + p = strchr(opt_str, '=');
  21682. + if (p) {
  21683. + *p++ = 0;
  21684. + if (*p)
  21685. + add->perm = br_perm_val(p);
  21686. + }
  21687. +
  21688. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  21689. + if (!err) {
  21690. + if (!p) {
  21691. + add->perm = AuBrPerm_RO;
  21692. + if (au_test_fs_rr(add->path.dentry->d_sb))
  21693. + add->perm = AuBrPerm_RR;
  21694. + else if (!bindex && !(sb_flags & MS_RDONLY))
  21695. + add->perm = AuBrPerm_RW;
  21696. + }
  21697. + opt->type = Opt_add;
  21698. + goto out;
  21699. + }
  21700. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  21701. + err = -EINVAL;
  21702. +
  21703. +out:
  21704. + return err;
  21705. +}
  21706. +
  21707. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  21708. +{
  21709. + int err;
  21710. +
  21711. + del->pathname = args[0].from;
  21712. + AuDbg("del path %s\n", del->pathname);
  21713. +
  21714. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  21715. + if (unlikely(err))
  21716. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  21717. +
  21718. + return err;
  21719. +}
  21720. +
  21721. +#if 0 /* reserved for future use */
  21722. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  21723. + struct au_opt_del *del, substring_t args[])
  21724. +{
  21725. + int err;
  21726. + struct dentry *root;
  21727. +
  21728. + err = -EINVAL;
  21729. + root = sb->s_root;
  21730. + aufs_read_lock(root, AuLock_FLUSH);
  21731. + if (bindex < 0 || au_sbend(sb) < bindex) {
  21732. + pr_err("out of bounds, %d\n", bindex);
  21733. + goto out;
  21734. + }
  21735. +
  21736. + err = 0;
  21737. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  21738. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  21739. +
  21740. +out:
  21741. + aufs_read_unlock(root, !AuLock_IR);
  21742. + return err;
  21743. +}
  21744. +#endif
  21745. +
  21746. +static int noinline_for_stack
  21747. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  21748. +{
  21749. + int err;
  21750. + struct path path;
  21751. + char *p;
  21752. +
  21753. + err = -EINVAL;
  21754. + mod->path = args[0].from;
  21755. + p = strchr(mod->path, '=');
  21756. + if (unlikely(!p)) {
  21757. + pr_err("no permssion %s\n", args[0].from);
  21758. + goto out;
  21759. + }
  21760. +
  21761. + *p++ = 0;
  21762. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  21763. + if (unlikely(err)) {
  21764. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  21765. + goto out;
  21766. + }
  21767. +
  21768. + mod->perm = br_perm_val(p);
  21769. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  21770. + mod->h_root = dget(path.dentry);
  21771. + path_put(&path);
  21772. +
  21773. +out:
  21774. + return err;
  21775. +}
  21776. +
  21777. +#if 0 /* reserved for future use */
  21778. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  21779. + struct au_opt_mod *mod, substring_t args[])
  21780. +{
  21781. + int err;
  21782. + struct dentry *root;
  21783. +
  21784. + err = -EINVAL;
  21785. + root = sb->s_root;
  21786. + aufs_read_lock(root, AuLock_FLUSH);
  21787. + if (bindex < 0 || au_sbend(sb) < bindex) {
  21788. + pr_err("out of bounds, %d\n", bindex);
  21789. + goto out;
  21790. + }
  21791. +
  21792. + err = 0;
  21793. + mod->perm = br_perm_val(args[1].from);
  21794. + AuDbg("mod path %s, perm 0x%x, %s\n",
  21795. + mod->path, mod->perm, args[1].from);
  21796. + mod->h_root = dget(au_h_dptr(root, bindex));
  21797. +
  21798. +out:
  21799. + aufs_read_unlock(root, !AuLock_IR);
  21800. + return err;
  21801. +}
  21802. +#endif
  21803. +
  21804. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  21805. + substring_t args[])
  21806. +{
  21807. + int err;
  21808. + struct file *file;
  21809. +
  21810. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  21811. + err = PTR_ERR(file);
  21812. + if (IS_ERR(file))
  21813. + goto out;
  21814. +
  21815. + err = -EINVAL;
  21816. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  21817. + fput(file);
  21818. + pr_err("%s must be outside\n", args[0].from);
  21819. + goto out;
  21820. + }
  21821. +
  21822. + err = 0;
  21823. + xino->file = file;
  21824. + xino->path = args[0].from;
  21825. +
  21826. +out:
  21827. + return err;
  21828. +}
  21829. +
  21830. +static int noinline_for_stack
  21831. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  21832. + struct au_opt_xino_itrunc *xino_itrunc,
  21833. + substring_t args[])
  21834. +{
  21835. + int err;
  21836. + aufs_bindex_t bend, bindex;
  21837. + struct path path;
  21838. + struct dentry *root;
  21839. +
  21840. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  21841. + if (unlikely(err)) {
  21842. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  21843. + goto out;
  21844. + }
  21845. +
  21846. + xino_itrunc->bindex = -1;
  21847. + root = sb->s_root;
  21848. + aufs_read_lock(root, AuLock_FLUSH);
  21849. + bend = au_sbend(sb);
  21850. + for (bindex = 0; bindex <= bend; bindex++) {
  21851. + if (au_h_dptr(root, bindex) == path.dentry) {
  21852. + xino_itrunc->bindex = bindex;
  21853. + break;
  21854. + }
  21855. + }
  21856. + aufs_read_unlock(root, !AuLock_IR);
  21857. + path_put(&path);
  21858. +
  21859. + if (unlikely(xino_itrunc->bindex < 0)) {
  21860. + pr_err("no such branch %s\n", args[0].from);
  21861. + err = -EINVAL;
  21862. + }
  21863. +
  21864. +out:
  21865. + return err;
  21866. +}
  21867. +
  21868. +/* called without aufs lock */
  21869. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  21870. +{
  21871. + int err, n, token;
  21872. + aufs_bindex_t bindex;
  21873. + unsigned char skipped;
  21874. + struct dentry *root;
  21875. + struct au_opt *opt, *opt_tail;
  21876. + char *opt_str;
  21877. + /* reduce the stack space */
  21878. + union {
  21879. + struct au_opt_xino_itrunc *xino_itrunc;
  21880. + struct au_opt_wbr_create *create;
  21881. + } u;
  21882. + struct {
  21883. + substring_t args[MAX_OPT_ARGS];
  21884. + } *a;
  21885. +
  21886. + err = -ENOMEM;
  21887. + a = kmalloc(sizeof(*a), GFP_NOFS);
  21888. + if (unlikely(!a))
  21889. + goto out;
  21890. +
  21891. + root = sb->s_root;
  21892. + err = 0;
  21893. + bindex = 0;
  21894. + opt = opts->opt;
  21895. + opt_tail = opt + opts->max_opt - 1;
  21896. + opt->type = Opt_tail;
  21897. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  21898. + err = -EINVAL;
  21899. + skipped = 0;
  21900. + token = match_token(opt_str, options, a->args);
  21901. + switch (token) {
  21902. + case Opt_br:
  21903. + err = 0;
  21904. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  21905. + && *opt_str) {
  21906. + err = opt_add(opt, opt_str, opts->sb_flags,
  21907. + bindex++);
  21908. + if (unlikely(!err && ++opt > opt_tail)) {
  21909. + err = -E2BIG;
  21910. + break;
  21911. + }
  21912. + opt->type = Opt_tail;
  21913. + skipped = 1;
  21914. + }
  21915. + break;
  21916. + case Opt_add:
  21917. + if (unlikely(match_int(&a->args[0], &n))) {
  21918. + pr_err("bad integer in %s\n", opt_str);
  21919. + break;
  21920. + }
  21921. + bindex = n;
  21922. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  21923. + bindex);
  21924. + if (!err)
  21925. + opt->type = token;
  21926. + break;
  21927. + case Opt_append:
  21928. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  21929. + /*dummy bindex*/1);
  21930. + if (!err)
  21931. + opt->type = token;
  21932. + break;
  21933. + case Opt_prepend:
  21934. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  21935. + /*bindex*/0);
  21936. + if (!err)
  21937. + opt->type = token;
  21938. + break;
  21939. + case Opt_del:
  21940. + err = au_opts_parse_del(&opt->del, a->args);
  21941. + if (!err)
  21942. + opt->type = token;
  21943. + break;
  21944. +#if 0 /* reserved for future use */
  21945. + case Opt_idel:
  21946. + del->pathname = "(indexed)";
  21947. + if (unlikely(match_int(&args[0], &n))) {
  21948. + pr_err("bad integer in %s\n", opt_str);
  21949. + break;
  21950. + }
  21951. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  21952. + if (!err)
  21953. + opt->type = token;
  21954. + break;
  21955. +#endif
  21956. + case Opt_mod:
  21957. + err = au_opts_parse_mod(&opt->mod, a->args);
  21958. + if (!err)
  21959. + opt->type = token;
  21960. + break;
  21961. +#ifdef IMOD /* reserved for future use */
  21962. + case Opt_imod:
  21963. + u.mod->path = "(indexed)";
  21964. + if (unlikely(match_int(&a->args[0], &n))) {
  21965. + pr_err("bad integer in %s\n", opt_str);
  21966. + break;
  21967. + }
  21968. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  21969. + if (!err)
  21970. + opt->type = token;
  21971. + break;
  21972. +#endif
  21973. + case Opt_xino:
  21974. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  21975. + if (!err)
  21976. + opt->type = token;
  21977. + break;
  21978. +
  21979. + case Opt_trunc_xino_path:
  21980. + err = au_opts_parse_xino_itrunc_path
  21981. + (sb, &opt->xino_itrunc, a->args);
  21982. + if (!err)
  21983. + opt->type = token;
  21984. + break;
  21985. +
  21986. + case Opt_itrunc_xino:
  21987. + u.xino_itrunc = &opt->xino_itrunc;
  21988. + if (unlikely(match_int(&a->args[0], &n))) {
  21989. + pr_err("bad integer in %s\n", opt_str);
  21990. + break;
  21991. + }
  21992. + u.xino_itrunc->bindex = n;
  21993. + aufs_read_lock(root, AuLock_FLUSH);
  21994. + if (n < 0 || au_sbend(sb) < n) {
  21995. + pr_err("out of bounds, %d\n", n);
  21996. + aufs_read_unlock(root, !AuLock_IR);
  21997. + break;
  21998. + }
  21999. + aufs_read_unlock(root, !AuLock_IR);
  22000. + err = 0;
  22001. + opt->type = token;
  22002. + break;
  22003. +
  22004. + case Opt_dirwh:
  22005. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  22006. + break;
  22007. + err = 0;
  22008. + opt->type = token;
  22009. + break;
  22010. +
  22011. + case Opt_rdcache:
  22012. + if (unlikely(match_int(&a->args[0], &n))) {
  22013. + pr_err("bad integer in %s\n", opt_str);
  22014. + break;
  22015. + }
  22016. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  22017. + pr_err("rdcache must be smaller than %d\n",
  22018. + AUFS_RDCACHE_MAX);
  22019. + break;
  22020. + }
  22021. + opt->rdcache = n;
  22022. + err = 0;
  22023. + opt->type = token;
  22024. + break;
  22025. + case Opt_rdblk:
  22026. + if (unlikely(match_int(&a->args[0], &n)
  22027. + || n < 0
  22028. + || n > KMALLOC_MAX_SIZE)) {
  22029. + pr_err("bad integer in %s\n", opt_str);
  22030. + break;
  22031. + }
  22032. + if (unlikely(n && n < NAME_MAX)) {
  22033. + pr_err("rdblk must be larger than %d\n",
  22034. + NAME_MAX);
  22035. + break;
  22036. + }
  22037. + opt->rdblk = n;
  22038. + err = 0;
  22039. + opt->type = token;
  22040. + break;
  22041. + case Opt_rdhash:
  22042. + if (unlikely(match_int(&a->args[0], &n)
  22043. + || n < 0
  22044. + || n * sizeof(struct hlist_head)
  22045. + > KMALLOC_MAX_SIZE)) {
  22046. + pr_err("bad integer in %s\n", opt_str);
  22047. + break;
  22048. + }
  22049. + opt->rdhash = n;
  22050. + err = 0;
  22051. + opt->type = token;
  22052. + break;
  22053. +
  22054. + case Opt_trunc_xino:
  22055. + case Opt_notrunc_xino:
  22056. + case Opt_noxino:
  22057. + case Opt_trunc_xib:
  22058. + case Opt_notrunc_xib:
  22059. + case Opt_shwh:
  22060. + case Opt_noshwh:
  22061. + case Opt_dirperm1:
  22062. + case Opt_nodirperm1:
  22063. + case Opt_plink:
  22064. + case Opt_noplink:
  22065. + case Opt_list_plink:
  22066. + case Opt_dio:
  22067. + case Opt_nodio:
  22068. + case Opt_diropq_a:
  22069. + case Opt_diropq_w:
  22070. + case Opt_warn_perm:
  22071. + case Opt_nowarn_perm:
  22072. + case Opt_verbose:
  22073. + case Opt_noverbose:
  22074. + case Opt_sum:
  22075. + case Opt_nosum:
  22076. + case Opt_wsum:
  22077. + case Opt_rdblk_def:
  22078. + case Opt_rdhash_def:
  22079. + case Opt_acl:
  22080. + case Opt_noacl:
  22081. + err = 0;
  22082. + opt->type = token;
  22083. + break;
  22084. +
  22085. + case Opt_udba:
  22086. + opt->udba = udba_val(a->args[0].from);
  22087. + if (opt->udba >= 0) {
  22088. + err = 0;
  22089. + opt->type = token;
  22090. + } else
  22091. + pr_err("wrong value, %s\n", opt_str);
  22092. + break;
  22093. +
  22094. + case Opt_wbr_create:
  22095. + u.create = &opt->wbr_create;
  22096. + u.create->wbr_create
  22097. + = au_wbr_create_val(a->args[0].from, u.create);
  22098. + if (u.create->wbr_create >= 0) {
  22099. + err = 0;
  22100. + opt->type = token;
  22101. + } else
  22102. + pr_err("wrong value, %s\n", opt_str);
  22103. + break;
  22104. + case Opt_wbr_copyup:
  22105. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  22106. + if (opt->wbr_copyup >= 0) {
  22107. + err = 0;
  22108. + opt->type = token;
  22109. + } else
  22110. + pr_err("wrong value, %s\n", opt_str);
  22111. + break;
  22112. +
  22113. + case Opt_fhsm_sec:
  22114. + if (unlikely(match_int(&a->args[0], &n)
  22115. + || n < 0)) {
  22116. + pr_err("bad integer in %s\n", opt_str);
  22117. + break;
  22118. + }
  22119. + if (sysaufs_brs) {
  22120. + opt->fhsm_second = n;
  22121. + opt->type = token;
  22122. + } else
  22123. + pr_warn("ignored %s\n", opt_str);
  22124. + err = 0;
  22125. + break;
  22126. +
  22127. + case Opt_ignore:
  22128. + pr_warn("ignored %s\n", opt_str);
  22129. + /*FALLTHROUGH*/
  22130. + case Opt_ignore_silent:
  22131. + skipped = 1;
  22132. + err = 0;
  22133. + break;
  22134. + case Opt_err:
  22135. + pr_err("unknown option %s\n", opt_str);
  22136. + break;
  22137. + }
  22138. +
  22139. + if (!err && !skipped) {
  22140. + if (unlikely(++opt > opt_tail)) {
  22141. + err = -E2BIG;
  22142. + opt--;
  22143. + opt->type = Opt_tail;
  22144. + break;
  22145. + }
  22146. + opt->type = Opt_tail;
  22147. + }
  22148. + }
  22149. +
  22150. + kfree(a);
  22151. + dump_opts(opts);
  22152. + if (unlikely(err))
  22153. + au_opts_free(opts);
  22154. +
  22155. +out:
  22156. + return err;
  22157. +}
  22158. +
  22159. +static int au_opt_wbr_create(struct super_block *sb,
  22160. + struct au_opt_wbr_create *create)
  22161. +{
  22162. + int err;
  22163. + struct au_sbinfo *sbinfo;
  22164. +
  22165. + SiMustWriteLock(sb);
  22166. +
  22167. + err = 1; /* handled */
  22168. + sbinfo = au_sbi(sb);
  22169. + if (sbinfo->si_wbr_create_ops->fin) {
  22170. + err = sbinfo->si_wbr_create_ops->fin(sb);
  22171. + if (!err)
  22172. + err = 1;
  22173. + }
  22174. +
  22175. + sbinfo->si_wbr_create = create->wbr_create;
  22176. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  22177. + switch (create->wbr_create) {
  22178. + case AuWbrCreate_MFSRRV:
  22179. + case AuWbrCreate_MFSRR:
  22180. + case AuWbrCreate_PMFSRR:
  22181. + case AuWbrCreate_PMFSRRV:
  22182. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  22183. + /*FALLTHROUGH*/
  22184. + case AuWbrCreate_MFS:
  22185. + case AuWbrCreate_MFSV:
  22186. + case AuWbrCreate_PMFS:
  22187. + case AuWbrCreate_PMFSV:
  22188. + sbinfo->si_wbr_mfs.mfs_expire
  22189. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  22190. + break;
  22191. + }
  22192. +
  22193. + if (sbinfo->si_wbr_create_ops->init)
  22194. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  22195. +
  22196. + return err;
  22197. +}
  22198. +
  22199. +/*
  22200. + * returns,
  22201. + * plus: processed without an error
  22202. + * zero: unprocessed
  22203. + */
  22204. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  22205. + struct au_opts *opts)
  22206. +{
  22207. + int err;
  22208. + struct au_sbinfo *sbinfo;
  22209. +
  22210. + SiMustWriteLock(sb);
  22211. +
  22212. + err = 1; /* handled */
  22213. + sbinfo = au_sbi(sb);
  22214. + switch (opt->type) {
  22215. + case Opt_udba:
  22216. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  22217. + sbinfo->si_mntflags |= opt->udba;
  22218. + opts->given_udba |= opt->udba;
  22219. + break;
  22220. +
  22221. + case Opt_plink:
  22222. + au_opt_set(sbinfo->si_mntflags, PLINK);
  22223. + break;
  22224. + case Opt_noplink:
  22225. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  22226. + au_plink_put(sb, /*verbose*/1);
  22227. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  22228. + break;
  22229. + case Opt_list_plink:
  22230. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  22231. + au_plink_list(sb);
  22232. + break;
  22233. +
  22234. + case Opt_dio:
  22235. + au_opt_set(sbinfo->si_mntflags, DIO);
  22236. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  22237. + break;
  22238. + case Opt_nodio:
  22239. + au_opt_clr(sbinfo->si_mntflags, DIO);
  22240. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  22241. + break;
  22242. +
  22243. + case Opt_fhsm_sec:
  22244. + au_fhsm_set(sbinfo, opt->fhsm_second);
  22245. + break;
  22246. +
  22247. + case Opt_diropq_a:
  22248. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  22249. + break;
  22250. + case Opt_diropq_w:
  22251. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  22252. + break;
  22253. +
  22254. + case Opt_warn_perm:
  22255. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  22256. + break;
  22257. + case Opt_nowarn_perm:
  22258. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  22259. + break;
  22260. +
  22261. + case Opt_verbose:
  22262. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  22263. + break;
  22264. + case Opt_noverbose:
  22265. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  22266. + break;
  22267. +
  22268. + case Opt_sum:
  22269. + au_opt_set(sbinfo->si_mntflags, SUM);
  22270. + break;
  22271. + case Opt_wsum:
  22272. + au_opt_clr(sbinfo->si_mntflags, SUM);
  22273. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  22274. + case Opt_nosum:
  22275. + au_opt_clr(sbinfo->si_mntflags, SUM);
  22276. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  22277. + break;
  22278. +
  22279. + case Opt_wbr_create:
  22280. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  22281. + break;
  22282. + case Opt_wbr_copyup:
  22283. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  22284. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  22285. + break;
  22286. +
  22287. + case Opt_dirwh:
  22288. + sbinfo->si_dirwh = opt->dirwh;
  22289. + break;
  22290. +
  22291. + case Opt_rdcache:
  22292. + sbinfo->si_rdcache
  22293. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  22294. + break;
  22295. + case Opt_rdblk:
  22296. + sbinfo->si_rdblk = opt->rdblk;
  22297. + break;
  22298. + case Opt_rdblk_def:
  22299. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  22300. + break;
  22301. + case Opt_rdhash:
  22302. + sbinfo->si_rdhash = opt->rdhash;
  22303. + break;
  22304. + case Opt_rdhash_def:
  22305. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  22306. + break;
  22307. +
  22308. + case Opt_shwh:
  22309. + au_opt_set(sbinfo->si_mntflags, SHWH);
  22310. + break;
  22311. + case Opt_noshwh:
  22312. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  22313. + break;
  22314. +
  22315. + case Opt_dirperm1:
  22316. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  22317. + break;
  22318. + case Opt_nodirperm1:
  22319. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  22320. + break;
  22321. +
  22322. + case Opt_trunc_xino:
  22323. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  22324. + break;
  22325. + case Opt_notrunc_xino:
  22326. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  22327. + break;
  22328. +
  22329. + case Opt_trunc_xino_path:
  22330. + case Opt_itrunc_xino:
  22331. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
  22332. + if (!err)
  22333. + err = 1;
  22334. + break;
  22335. +
  22336. + case Opt_trunc_xib:
  22337. + au_fset_opts(opts->flags, TRUNC_XIB);
  22338. + break;
  22339. + case Opt_notrunc_xib:
  22340. + au_fclr_opts(opts->flags, TRUNC_XIB);
  22341. + break;
  22342. +
  22343. + case Opt_acl:
  22344. + sb->s_flags |= MS_POSIXACL;
  22345. + break;
  22346. + case Opt_noacl:
  22347. + sb->s_flags &= ~MS_POSIXACL;
  22348. + break;
  22349. +
  22350. + default:
  22351. + err = 0;
  22352. + break;
  22353. + }
  22354. +
  22355. + return err;
  22356. +}
  22357. +
  22358. +/*
  22359. + * returns tri-state.
  22360. + * plus: processed without an error
  22361. + * zero: unprocessed
  22362. + * minus: error
  22363. + */
  22364. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  22365. + struct au_opts *opts)
  22366. +{
  22367. + int err, do_refresh;
  22368. +
  22369. + err = 0;
  22370. + switch (opt->type) {
  22371. + case Opt_append:
  22372. + opt->add.bindex = au_sbend(sb) + 1;
  22373. + if (opt->add.bindex < 0)
  22374. + opt->add.bindex = 0;
  22375. + goto add;
  22376. + case Opt_prepend:
  22377. + opt->add.bindex = 0;
  22378. + add: /* indented label */
  22379. + case Opt_add:
  22380. + err = au_br_add(sb, &opt->add,
  22381. + au_ftest_opts(opts->flags, REMOUNT));
  22382. + if (!err) {
  22383. + err = 1;
  22384. + au_fset_opts(opts->flags, REFRESH);
  22385. + }
  22386. + break;
  22387. +
  22388. + case Opt_del:
  22389. + case Opt_idel:
  22390. + err = au_br_del(sb, &opt->del,
  22391. + au_ftest_opts(opts->flags, REMOUNT));
  22392. + if (!err) {
  22393. + err = 1;
  22394. + au_fset_opts(opts->flags, TRUNC_XIB);
  22395. + au_fset_opts(opts->flags, REFRESH);
  22396. + }
  22397. + break;
  22398. +
  22399. + case Opt_mod:
  22400. + case Opt_imod:
  22401. + err = au_br_mod(sb, &opt->mod,
  22402. + au_ftest_opts(opts->flags, REMOUNT),
  22403. + &do_refresh);
  22404. + if (!err) {
  22405. + err = 1;
  22406. + if (do_refresh)
  22407. + au_fset_opts(opts->flags, REFRESH);
  22408. + }
  22409. + break;
  22410. + }
  22411. +
  22412. + return err;
  22413. +}
  22414. +
  22415. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  22416. + struct au_opt_xino **opt_xino,
  22417. + struct au_opts *opts)
  22418. +{
  22419. + int err;
  22420. + aufs_bindex_t bend, bindex;
  22421. + struct dentry *root, *parent, *h_root;
  22422. +
  22423. + err = 0;
  22424. + switch (opt->type) {
  22425. + case Opt_xino:
  22426. + err = au_xino_set(sb, &opt->xino,
  22427. + !!au_ftest_opts(opts->flags, REMOUNT));
  22428. + if (unlikely(err))
  22429. + break;
  22430. +
  22431. + *opt_xino = &opt->xino;
  22432. + au_xino_brid_set(sb, -1);
  22433. +
  22434. + /* safe d_parent access */
  22435. + parent = opt->xino.file->f_path.dentry->d_parent;
  22436. + root = sb->s_root;
  22437. + bend = au_sbend(sb);
  22438. + for (bindex = 0; bindex <= bend; bindex++) {
  22439. + h_root = au_h_dptr(root, bindex);
  22440. + if (h_root == parent) {
  22441. + au_xino_brid_set(sb, au_sbr_id(sb, bindex));
  22442. + break;
  22443. + }
  22444. + }
  22445. + break;
  22446. +
  22447. + case Opt_noxino:
  22448. + au_xino_clr(sb);
  22449. + au_xino_brid_set(sb, -1);
  22450. + *opt_xino = (void *)-1;
  22451. + break;
  22452. + }
  22453. +
  22454. + return err;
  22455. +}
  22456. +
  22457. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  22458. + unsigned int pending)
  22459. +{
  22460. + int err, fhsm;
  22461. + aufs_bindex_t bindex, bend;
  22462. + unsigned char do_plink, skip, do_free;
  22463. + struct au_branch *br;
  22464. + struct au_wbr *wbr;
  22465. + struct dentry *root;
  22466. + struct inode *dir, *h_dir;
  22467. + struct au_sbinfo *sbinfo;
  22468. + struct au_hinode *hdir;
  22469. +
  22470. + SiMustAnyLock(sb);
  22471. +
  22472. + sbinfo = au_sbi(sb);
  22473. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  22474. +
  22475. + if (!(sb_flags & MS_RDONLY)) {
  22476. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  22477. + pr_warn("first branch should be rw\n");
  22478. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  22479. + pr_warn("shwh should be used with ro\n");
  22480. + }
  22481. +
  22482. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  22483. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  22484. + pr_warn("udba=*notify requires xino\n");
  22485. +
  22486. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  22487. + pr_warn("dirperm1 breaks the protection"
  22488. + " by the permission bits on the lower branch\n");
  22489. +
  22490. + err = 0;
  22491. + fhsm = 0;
  22492. + root = sb->s_root;
  22493. + dir = root->d_inode;
  22494. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  22495. + bend = au_sbend(sb);
  22496. + for (bindex = 0; !err && bindex <= bend; bindex++) {
  22497. + skip = 0;
  22498. + h_dir = au_h_iptr(dir, bindex);
  22499. + br = au_sbr(sb, bindex);
  22500. +
  22501. + if ((br->br_perm & AuBrAttr_ICEX)
  22502. + && !h_dir->i_op->listxattr)
  22503. + br->br_perm &= ~AuBrAttr_ICEX;
  22504. +#if 0
  22505. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  22506. + && (au_br_sb(br)->s_flags & MS_NOSEC))
  22507. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  22508. +#endif
  22509. +
  22510. + do_free = 0;
  22511. + wbr = br->br_wbr;
  22512. + if (wbr)
  22513. + wbr_wh_read_lock(wbr);
  22514. +
  22515. + if (!au_br_writable(br->br_perm)) {
  22516. + do_free = !!wbr;
  22517. + skip = (!wbr
  22518. + || (!wbr->wbr_whbase
  22519. + && !wbr->wbr_plink
  22520. + && !wbr->wbr_orph));
  22521. + } else if (!au_br_wh_linkable(br->br_perm)) {
  22522. + /* skip = (!br->br_whbase && !br->br_orph); */
  22523. + skip = (!wbr || !wbr->wbr_whbase);
  22524. + if (skip && wbr) {
  22525. + if (do_plink)
  22526. + skip = !!wbr->wbr_plink;
  22527. + else
  22528. + skip = !wbr->wbr_plink;
  22529. + }
  22530. + } else {
  22531. + /* skip = (br->br_whbase && br->br_ohph); */
  22532. + skip = (wbr && wbr->wbr_whbase);
  22533. + if (skip) {
  22534. + if (do_plink)
  22535. + skip = !!wbr->wbr_plink;
  22536. + else
  22537. + skip = !wbr->wbr_plink;
  22538. + }
  22539. + }
  22540. + if (wbr)
  22541. + wbr_wh_read_unlock(wbr);
  22542. +
  22543. + if (au_br_fhsm(br->br_perm)) {
  22544. + fhsm++;
  22545. + AuDebugOn(!br->br_fhsm);
  22546. + }
  22547. +
  22548. + if (skip)
  22549. + continue;
  22550. +
  22551. + hdir = au_hi(dir, bindex);
  22552. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  22553. + if (wbr)
  22554. + wbr_wh_write_lock(wbr);
  22555. + err = au_wh_init(br, sb);
  22556. + if (wbr)
  22557. + wbr_wh_write_unlock(wbr);
  22558. + au_hn_imtx_unlock(hdir);
  22559. +
  22560. + if (!err && do_free) {
  22561. + kfree(wbr);
  22562. + br->br_wbr = NULL;
  22563. + }
  22564. + }
  22565. +
  22566. + if (fhsm >= 2) {
  22567. + au_fset_si(sbinfo, FHSM);
  22568. + for (bindex = bend; bindex >= 0; bindex--) {
  22569. + br = au_sbr(sb, bindex);
  22570. + if (au_br_fhsm(br->br_perm)) {
  22571. + au_fhsm_set_bottom(sb, bindex);
  22572. + break;
  22573. + }
  22574. + }
  22575. + } else {
  22576. + au_fclr_si(sbinfo, FHSM);
  22577. + au_fhsm_set_bottom(sb, -1);
  22578. + }
  22579. +
  22580. + return err;
  22581. +}
  22582. +
  22583. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  22584. +{
  22585. + int err;
  22586. + unsigned int tmp;
  22587. + aufs_bindex_t bindex, bend;
  22588. + struct au_opt *opt;
  22589. + struct au_opt_xino *opt_xino, xino;
  22590. + struct au_sbinfo *sbinfo;
  22591. + struct au_branch *br;
  22592. + struct inode *dir;
  22593. +
  22594. + SiMustWriteLock(sb);
  22595. +
  22596. + err = 0;
  22597. + opt_xino = NULL;
  22598. + opt = opts->opt;
  22599. + while (err >= 0 && opt->type != Opt_tail)
  22600. + err = au_opt_simple(sb, opt++, opts);
  22601. + if (err > 0)
  22602. + err = 0;
  22603. + else if (unlikely(err < 0))
  22604. + goto out;
  22605. +
  22606. + /* disable xino and udba temporary */
  22607. + sbinfo = au_sbi(sb);
  22608. + tmp = sbinfo->si_mntflags;
  22609. + au_opt_clr(sbinfo->si_mntflags, XINO);
  22610. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  22611. +
  22612. + opt = opts->opt;
  22613. + while (err >= 0 && opt->type != Opt_tail)
  22614. + err = au_opt_br(sb, opt++, opts);
  22615. + if (err > 0)
  22616. + err = 0;
  22617. + else if (unlikely(err < 0))
  22618. + goto out;
  22619. +
  22620. + bend = au_sbend(sb);
  22621. + if (unlikely(bend < 0)) {
  22622. + err = -EINVAL;
  22623. + pr_err("no branches\n");
  22624. + goto out;
  22625. + }
  22626. +
  22627. + if (au_opt_test(tmp, XINO))
  22628. + au_opt_set(sbinfo->si_mntflags, XINO);
  22629. + opt = opts->opt;
  22630. + while (!err && opt->type != Opt_tail)
  22631. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  22632. + if (unlikely(err))
  22633. + goto out;
  22634. +
  22635. + err = au_opts_verify(sb, sb->s_flags, tmp);
  22636. + if (unlikely(err))
  22637. + goto out;
  22638. +
  22639. + /* restore xino */
  22640. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  22641. + xino.file = au_xino_def(sb);
  22642. + err = PTR_ERR(xino.file);
  22643. + if (IS_ERR(xino.file))
  22644. + goto out;
  22645. +
  22646. + err = au_xino_set(sb, &xino, /*remount*/0);
  22647. + fput(xino.file);
  22648. + if (unlikely(err))
  22649. + goto out;
  22650. + }
  22651. +
  22652. + /* restore udba */
  22653. + tmp &= AuOptMask_UDBA;
  22654. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  22655. + sbinfo->si_mntflags |= tmp;
  22656. + bend = au_sbend(sb);
  22657. + for (bindex = 0; bindex <= bend; bindex++) {
  22658. + br = au_sbr(sb, bindex);
  22659. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  22660. + if (unlikely(err))
  22661. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  22662. + bindex, err);
  22663. + /* go on even if err */
  22664. + }
  22665. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  22666. + dir = sb->s_root->d_inode;
  22667. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  22668. + }
  22669. +
  22670. +out:
  22671. + return err;
  22672. +}
  22673. +
  22674. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  22675. +{
  22676. + int err, rerr;
  22677. + struct inode *dir;
  22678. + struct au_opt_xino *opt_xino;
  22679. + struct au_opt *opt;
  22680. + struct au_sbinfo *sbinfo;
  22681. +
  22682. + SiMustWriteLock(sb);
  22683. +
  22684. + dir = sb->s_root->d_inode;
  22685. + sbinfo = au_sbi(sb);
  22686. + err = 0;
  22687. + opt_xino = NULL;
  22688. + opt = opts->opt;
  22689. + while (err >= 0 && opt->type != Opt_tail) {
  22690. + err = au_opt_simple(sb, opt, opts);
  22691. + if (!err)
  22692. + err = au_opt_br(sb, opt, opts);
  22693. + if (!err)
  22694. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  22695. + opt++;
  22696. + }
  22697. + if (err > 0)
  22698. + err = 0;
  22699. + AuTraceErr(err);
  22700. + /* go on even err */
  22701. +
  22702. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  22703. + if (unlikely(rerr && !err))
  22704. + err = rerr;
  22705. +
  22706. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  22707. + rerr = au_xib_trunc(sb);
  22708. + if (unlikely(rerr && !err))
  22709. + err = rerr;
  22710. + }
  22711. +
  22712. + /* will be handled by the caller */
  22713. + if (!au_ftest_opts(opts->flags, REFRESH)
  22714. + && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
  22715. + au_fset_opts(opts->flags, REFRESH);
  22716. +
  22717. + AuDbg("status 0x%x\n", opts->flags);
  22718. + return err;
  22719. +}
  22720. +
  22721. +/* ---------------------------------------------------------------------- */
  22722. +
  22723. +unsigned int au_opt_udba(struct super_block *sb)
  22724. +{
  22725. + return au_mntflags(sb) & AuOptMask_UDBA;
  22726. +}
  22727. diff -Nur linux-4.0.4.orig/fs/aufs/opts.h linux-4.0.4/fs/aufs/opts.h
  22728. --- linux-4.0.4.orig/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
  22729. +++ linux-4.0.4/fs/aufs/opts.h 2015-05-30 22:11:30.000000000 +0200
  22730. @@ -0,0 +1,197 @@
  22731. +/*
  22732. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  22733. + */
  22734. +
  22735. +/*
  22736. + * mount options/flags
  22737. + */
  22738. +
  22739. +#ifndef __AUFS_OPTS_H__
  22740. +#define __AUFS_OPTS_H__
  22741. +
  22742. +#ifdef __KERNEL__
  22743. +
  22744. +#include <linux/path.h>
  22745. +
  22746. +struct file;
  22747. +struct super_block;
  22748. +
  22749. +/* ---------------------------------------------------------------------- */
  22750. +
  22751. +/* mount flags */
  22752. +#define AuOpt_XINO 1 /* external inode number bitmap
  22753. + and translation table */
  22754. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  22755. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  22756. +#define AuOpt_UDBA_REVAL (1 << 3)
  22757. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  22758. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  22759. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  22760. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  22761. + bits */
  22762. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  22763. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  22764. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  22765. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  22766. +#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
  22767. +#define AuOpt_DIO (1 << 14) /* direct io */
  22768. +
  22769. +#ifndef CONFIG_AUFS_HNOTIFY
  22770. +#undef AuOpt_UDBA_HNOTIFY
  22771. +#define AuOpt_UDBA_HNOTIFY 0
  22772. +#endif
  22773. +#ifndef CONFIG_AUFS_SHWH
  22774. +#undef AuOpt_SHWH
  22775. +#define AuOpt_SHWH 0
  22776. +#endif
  22777. +
  22778. +#define AuOpt_Def (AuOpt_XINO \
  22779. + | AuOpt_UDBA_REVAL \
  22780. + | AuOpt_PLINK \
  22781. + /* | AuOpt_DIRPERM1 */ \
  22782. + | AuOpt_WARN_PERM)
  22783. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  22784. + | AuOpt_UDBA_REVAL \
  22785. + | AuOpt_UDBA_HNOTIFY)
  22786. +
  22787. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  22788. +#define au_opt_set(flags, name) do { \
  22789. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  22790. + ((flags) |= AuOpt_##name); \
  22791. +} while (0)
  22792. +#define au_opt_set_udba(flags, name) do { \
  22793. + (flags) &= ~AuOptMask_UDBA; \
  22794. + ((flags) |= AuOpt_##name); \
  22795. +} while (0)
  22796. +#define au_opt_clr(flags, name) do { \
  22797. + ((flags) &= ~AuOpt_##name); \
  22798. +} while (0)
  22799. +
  22800. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  22801. +{
  22802. +#ifdef CONFIG_PROC_FS
  22803. + return mntflags;
  22804. +#else
  22805. + return mntflags & ~AuOpt_PLINK;
  22806. +#endif
  22807. +}
  22808. +
  22809. +/* ---------------------------------------------------------------------- */
  22810. +
  22811. +/* policies to select one among multiple writable branches */
  22812. +enum {
  22813. + AuWbrCreate_TDP, /* top down parent */
  22814. + AuWbrCreate_RR, /* round robin */
  22815. + AuWbrCreate_MFS, /* most free space */
  22816. + AuWbrCreate_MFSV, /* mfs with seconds */
  22817. + AuWbrCreate_MFSRR, /* mfs then rr */
  22818. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  22819. + AuWbrCreate_PMFS, /* parent and mfs */
  22820. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  22821. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  22822. + AuWbrCreate_PMFSRRV, /* plus seconds */
  22823. +
  22824. + AuWbrCreate_Def = AuWbrCreate_TDP
  22825. +};
  22826. +
  22827. +enum {
  22828. + AuWbrCopyup_TDP, /* top down parent */
  22829. + AuWbrCopyup_BUP, /* bottom up parent */
  22830. + AuWbrCopyup_BU, /* bottom up */
  22831. +
  22832. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  22833. +};
  22834. +
  22835. +/* ---------------------------------------------------------------------- */
  22836. +
  22837. +struct au_opt_add {
  22838. + aufs_bindex_t bindex;
  22839. + char *pathname;
  22840. + int perm;
  22841. + struct path path;
  22842. +};
  22843. +
  22844. +struct au_opt_del {
  22845. + char *pathname;
  22846. + struct path h_path;
  22847. +};
  22848. +
  22849. +struct au_opt_mod {
  22850. + char *path;
  22851. + int perm;
  22852. + struct dentry *h_root;
  22853. +};
  22854. +
  22855. +struct au_opt_xino {
  22856. + char *path;
  22857. + struct file *file;
  22858. +};
  22859. +
  22860. +struct au_opt_xino_itrunc {
  22861. + aufs_bindex_t bindex;
  22862. +};
  22863. +
  22864. +struct au_opt_wbr_create {
  22865. + int wbr_create;
  22866. + int mfs_second;
  22867. + unsigned long long mfsrr_watermark;
  22868. +};
  22869. +
  22870. +struct au_opt {
  22871. + int type;
  22872. + union {
  22873. + struct au_opt_xino xino;
  22874. + struct au_opt_xino_itrunc xino_itrunc;
  22875. + struct au_opt_add add;
  22876. + struct au_opt_del del;
  22877. + struct au_opt_mod mod;
  22878. + int dirwh;
  22879. + int rdcache;
  22880. + unsigned int rdblk;
  22881. + unsigned int rdhash;
  22882. + int udba;
  22883. + struct au_opt_wbr_create wbr_create;
  22884. + int wbr_copyup;
  22885. + unsigned int fhsm_second;
  22886. + };
  22887. +};
  22888. +
  22889. +/* opts flags */
  22890. +#define AuOpts_REMOUNT 1
  22891. +#define AuOpts_REFRESH (1 << 1)
  22892. +#define AuOpts_TRUNC_XIB (1 << 2)
  22893. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  22894. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  22895. +#define au_fset_opts(flags, name) \
  22896. + do { (flags) |= AuOpts_##name; } while (0)
  22897. +#define au_fclr_opts(flags, name) \
  22898. + do { (flags) &= ~AuOpts_##name; } while (0)
  22899. +
  22900. +struct au_opts {
  22901. + struct au_opt *opt;
  22902. + int max_opt;
  22903. +
  22904. + unsigned int given_udba;
  22905. + unsigned int flags;
  22906. + unsigned long sb_flags;
  22907. +};
  22908. +
  22909. +/* ---------------------------------------------------------------------- */
  22910. +
  22911. +/* opts.c */
  22912. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  22913. +const char *au_optstr_udba(int udba);
  22914. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  22915. +const char *au_optstr_wbr_create(int wbr_create);
  22916. +
  22917. +void au_opts_free(struct au_opts *opts);
  22918. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  22919. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  22920. + unsigned int pending);
  22921. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  22922. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  22923. +
  22924. +unsigned int au_opt_udba(struct super_block *sb);
  22925. +
  22926. +#endif /* __KERNEL__ */
  22927. +#endif /* __AUFS_OPTS_H__ */
  22928. diff -Nur linux-4.0.4.orig/fs/aufs/plink.c linux-4.0.4/fs/aufs/plink.c
  22929. --- linux-4.0.4.orig/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
  22930. +++ linux-4.0.4/fs/aufs/plink.c 2015-05-30 22:11:30.000000000 +0200
  22931. @@ -0,0 +1,519 @@
  22932. +/*
  22933. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  22934. + */
  22935. +
  22936. +/*
  22937. + * pseudo-link
  22938. + */
  22939. +
  22940. +#include "aufs.h"
  22941. +
  22942. +/*
  22943. + * the pseudo-link maintenance mode.
  22944. + * during a user process maintains the pseudo-links,
  22945. + * prohibit adding a new plink and branch manipulation.
  22946. + *
  22947. + * Flags
  22948. + * NOPLM:
  22949. + * For entry functions which will handle plink, and i_mutex is already held
  22950. + * in VFS.
  22951. + * They cannot wait and should return an error at once.
  22952. + * Callers has to check the error.
  22953. + * NOPLMW:
  22954. + * For entry functions which will handle plink, but i_mutex is not held
  22955. + * in VFS.
  22956. + * They can wait the plink maintenance mode to finish.
  22957. + *
  22958. + * They behave like F_SETLK and F_SETLKW.
  22959. + * If the caller never handle plink, then both flags are unnecessary.
  22960. + */
  22961. +
  22962. +int au_plink_maint(struct super_block *sb, int flags)
  22963. +{
  22964. + int err;
  22965. + pid_t pid, ppid;
  22966. + struct au_sbinfo *sbi;
  22967. +
  22968. + SiMustAnyLock(sb);
  22969. +
  22970. + err = 0;
  22971. + if (!au_opt_test(au_mntflags(sb), PLINK))
  22972. + goto out;
  22973. +
  22974. + sbi = au_sbi(sb);
  22975. + pid = sbi->si_plink_maint_pid;
  22976. + if (!pid || pid == current->pid)
  22977. + goto out;
  22978. +
  22979. + /* todo: it highly depends upon /sbin/mount.aufs */
  22980. + rcu_read_lock();
  22981. + ppid = task_pid_vnr(rcu_dereference(current->real_parent));
  22982. + rcu_read_unlock();
  22983. + if (pid == ppid)
  22984. + goto out;
  22985. +
  22986. + if (au_ftest_lock(flags, NOPLMW)) {
  22987. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  22988. + /* AuDebugOn(!lockdep_depth(current)); */
  22989. + while (sbi->si_plink_maint_pid) {
  22990. + si_read_unlock(sb);
  22991. + /* gave up wake_up_bit() */
  22992. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  22993. +
  22994. + if (au_ftest_lock(flags, FLUSH))
  22995. + au_nwt_flush(&sbi->si_nowait);
  22996. + si_noflush_read_lock(sb);
  22997. + }
  22998. + } else if (au_ftest_lock(flags, NOPLM)) {
  22999. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  23000. + err = -EAGAIN;
  23001. + }
  23002. +
  23003. +out:
  23004. + return err;
  23005. +}
  23006. +
  23007. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  23008. +{
  23009. + spin_lock(&sbinfo->si_plink_maint_lock);
  23010. + sbinfo->si_plink_maint_pid = 0;
  23011. + spin_unlock(&sbinfo->si_plink_maint_lock);
  23012. + wake_up_all(&sbinfo->si_plink_wq);
  23013. +}
  23014. +
  23015. +int au_plink_maint_enter(struct super_block *sb)
  23016. +{
  23017. + int err;
  23018. + struct au_sbinfo *sbinfo;
  23019. +
  23020. + err = 0;
  23021. + sbinfo = au_sbi(sb);
  23022. + /* make sure i am the only one in this fs */
  23023. + si_write_lock(sb, AuLock_FLUSH);
  23024. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  23025. + spin_lock(&sbinfo->si_plink_maint_lock);
  23026. + if (!sbinfo->si_plink_maint_pid)
  23027. + sbinfo->si_plink_maint_pid = current->pid;
  23028. + else
  23029. + err = -EBUSY;
  23030. + spin_unlock(&sbinfo->si_plink_maint_lock);
  23031. + }
  23032. + si_write_unlock(sb);
  23033. +
  23034. + return err;
  23035. +}
  23036. +
  23037. +/* ---------------------------------------------------------------------- */
  23038. +
  23039. +#ifdef CONFIG_AUFS_DEBUG
  23040. +void au_plink_list(struct super_block *sb)
  23041. +{
  23042. + int i;
  23043. + struct au_sbinfo *sbinfo;
  23044. + struct hlist_head *plink_hlist;
  23045. + struct pseudo_link *plink;
  23046. +
  23047. + SiMustAnyLock(sb);
  23048. +
  23049. + sbinfo = au_sbi(sb);
  23050. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  23051. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  23052. +
  23053. + for (i = 0; i < AuPlink_NHASH; i++) {
  23054. + plink_hlist = &sbinfo->si_plink[i].head;
  23055. + rcu_read_lock();
  23056. + hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
  23057. + AuDbg("%lu\n", plink->inode->i_ino);
  23058. + rcu_read_unlock();
  23059. + }
  23060. +}
  23061. +#endif
  23062. +
  23063. +/* is the inode pseudo-linked? */
  23064. +int au_plink_test(struct inode *inode)
  23065. +{
  23066. + int found, i;
  23067. + struct au_sbinfo *sbinfo;
  23068. + struct hlist_head *plink_hlist;
  23069. + struct pseudo_link *plink;
  23070. +
  23071. + sbinfo = au_sbi(inode->i_sb);
  23072. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  23073. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  23074. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  23075. +
  23076. + found = 0;
  23077. + i = au_plink_hash(inode->i_ino);
  23078. + plink_hlist = &sbinfo->si_plink[i].head;
  23079. + rcu_read_lock();
  23080. + hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
  23081. + if (plink->inode == inode) {
  23082. + found = 1;
  23083. + break;
  23084. + }
  23085. + rcu_read_unlock();
  23086. + return found;
  23087. +}
  23088. +
  23089. +/* ---------------------------------------------------------------------- */
  23090. +
  23091. +/*
  23092. + * generate a name for plink.
  23093. + * the file will be stored under AUFS_WH_PLINKDIR.
  23094. + */
  23095. +/* 20 is max digits length of ulong 64 */
  23096. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  23097. +
  23098. +static int plink_name(char *name, int len, struct inode *inode,
  23099. + aufs_bindex_t bindex)
  23100. +{
  23101. + int rlen;
  23102. + struct inode *h_inode;
  23103. +
  23104. + h_inode = au_h_iptr(inode, bindex);
  23105. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  23106. + return rlen;
  23107. +}
  23108. +
  23109. +struct au_do_plink_lkup_args {
  23110. + struct dentry **errp;
  23111. + struct qstr *tgtname;
  23112. + struct dentry *h_parent;
  23113. + struct au_branch *br;
  23114. +};
  23115. +
  23116. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  23117. + struct dentry *h_parent,
  23118. + struct au_branch *br)
  23119. +{
  23120. + struct dentry *h_dentry;
  23121. + struct mutex *h_mtx;
  23122. +
  23123. + h_mtx = &h_parent->d_inode->i_mutex;
  23124. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  23125. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  23126. + mutex_unlock(h_mtx);
  23127. + return h_dentry;
  23128. +}
  23129. +
  23130. +static void au_call_do_plink_lkup(void *args)
  23131. +{
  23132. + struct au_do_plink_lkup_args *a = args;
  23133. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  23134. +}
  23135. +
  23136. +/* lookup the plink-ed @inode under the branch at @bindex */
  23137. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  23138. +{
  23139. + struct dentry *h_dentry, *h_parent;
  23140. + struct au_branch *br;
  23141. + struct inode *h_dir;
  23142. + int wkq_err;
  23143. + char a[PLINK_NAME_LEN];
  23144. + struct qstr tgtname = QSTR_INIT(a, 0);
  23145. +
  23146. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  23147. +
  23148. + br = au_sbr(inode->i_sb, bindex);
  23149. + h_parent = br->br_wbr->wbr_plink;
  23150. + h_dir = h_parent->d_inode;
  23151. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  23152. +
  23153. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  23154. + struct au_do_plink_lkup_args args = {
  23155. + .errp = &h_dentry,
  23156. + .tgtname = &tgtname,
  23157. + .h_parent = h_parent,
  23158. + .br = br
  23159. + };
  23160. +
  23161. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  23162. + if (unlikely(wkq_err))
  23163. + h_dentry = ERR_PTR(wkq_err);
  23164. + } else
  23165. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  23166. +
  23167. + return h_dentry;
  23168. +}
  23169. +
  23170. +/* create a pseudo-link */
  23171. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  23172. + struct dentry *h_dentry, struct au_branch *br)
  23173. +{
  23174. + int err;
  23175. + struct path h_path = {
  23176. + .mnt = au_br_mnt(br)
  23177. + };
  23178. + struct inode *h_dir, *delegated;
  23179. +
  23180. + h_dir = h_parent->d_inode;
  23181. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
  23182. +again:
  23183. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  23184. + err = PTR_ERR(h_path.dentry);
  23185. + if (IS_ERR(h_path.dentry))
  23186. + goto out;
  23187. +
  23188. + err = 0;
  23189. + /* wh.plink dir is not monitored */
  23190. + /* todo: is it really safe? */
  23191. + if (h_path.dentry->d_inode
  23192. + && h_path.dentry->d_inode != h_dentry->d_inode) {
  23193. + delegated = NULL;
  23194. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  23195. + if (unlikely(err == -EWOULDBLOCK)) {
  23196. + pr_warn("cannot retry for NFSv4 delegation"
  23197. + " for an internal unlink\n");
  23198. + iput(delegated);
  23199. + }
  23200. + dput(h_path.dentry);
  23201. + h_path.dentry = NULL;
  23202. + if (!err)
  23203. + goto again;
  23204. + }
  23205. + if (!err && !h_path.dentry->d_inode) {
  23206. + delegated = NULL;
  23207. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  23208. + if (unlikely(err == -EWOULDBLOCK)) {
  23209. + pr_warn("cannot retry for NFSv4 delegation"
  23210. + " for an internal link\n");
  23211. + iput(delegated);
  23212. + }
  23213. + }
  23214. + dput(h_path.dentry);
  23215. +
  23216. +out:
  23217. + mutex_unlock(&h_dir->i_mutex);
  23218. + return err;
  23219. +}
  23220. +
  23221. +struct do_whplink_args {
  23222. + int *errp;
  23223. + struct qstr *tgt;
  23224. + struct dentry *h_parent;
  23225. + struct dentry *h_dentry;
  23226. + struct au_branch *br;
  23227. +};
  23228. +
  23229. +static void call_do_whplink(void *args)
  23230. +{
  23231. + struct do_whplink_args *a = args;
  23232. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  23233. +}
  23234. +
  23235. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  23236. + aufs_bindex_t bindex, struct au_branch *br)
  23237. +{
  23238. + int err, wkq_err;
  23239. + struct au_wbr *wbr;
  23240. + struct dentry *h_parent;
  23241. + struct inode *h_dir;
  23242. + char a[PLINK_NAME_LEN];
  23243. + struct qstr tgtname = QSTR_INIT(a, 0);
  23244. +
  23245. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  23246. + h_parent = wbr->wbr_plink;
  23247. + h_dir = h_parent->d_inode;
  23248. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  23249. +
  23250. + /* always superio. */
  23251. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  23252. + struct do_whplink_args args = {
  23253. + .errp = &err,
  23254. + .tgt = &tgtname,
  23255. + .h_parent = h_parent,
  23256. + .h_dentry = h_dentry,
  23257. + .br = br
  23258. + };
  23259. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  23260. + if (unlikely(wkq_err))
  23261. + err = wkq_err;
  23262. + } else
  23263. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  23264. +
  23265. + return err;
  23266. +}
  23267. +
  23268. +/* free a single plink */
  23269. +static void do_put_plink(struct pseudo_link *plink, int do_del)
  23270. +{
  23271. + if (do_del)
  23272. + hlist_del(&plink->hlist);
  23273. + iput(plink->inode);
  23274. + kfree(plink);
  23275. +}
  23276. +
  23277. +static void do_put_plink_rcu(struct rcu_head *rcu)
  23278. +{
  23279. + struct pseudo_link *plink;
  23280. +
  23281. + plink = container_of(rcu, struct pseudo_link, rcu);
  23282. + iput(plink->inode);
  23283. + kfree(plink);
  23284. +}
  23285. +
  23286. +/*
  23287. + * create a new pseudo-link for @h_dentry on @bindex.
  23288. + * the linked inode is held in aufs @inode.
  23289. + */
  23290. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  23291. + struct dentry *h_dentry)
  23292. +{
  23293. + struct super_block *sb;
  23294. + struct au_sbinfo *sbinfo;
  23295. + struct hlist_head *plink_hlist;
  23296. + struct pseudo_link *plink, *tmp;
  23297. + struct au_sphlhead *sphl;
  23298. + int found, err, cnt, i;
  23299. +
  23300. + sb = inode->i_sb;
  23301. + sbinfo = au_sbi(sb);
  23302. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  23303. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  23304. +
  23305. + found = au_plink_test(inode);
  23306. + if (found)
  23307. + return;
  23308. +
  23309. + i = au_plink_hash(inode->i_ino);
  23310. + sphl = sbinfo->si_plink + i;
  23311. + plink_hlist = &sphl->head;
  23312. + tmp = kmalloc(sizeof(*plink), GFP_NOFS);
  23313. + if (tmp)
  23314. + tmp->inode = au_igrab(inode);
  23315. + else {
  23316. + err = -ENOMEM;
  23317. + goto out;
  23318. + }
  23319. +
  23320. + spin_lock(&sphl->spin);
  23321. + hlist_for_each_entry(plink, plink_hlist, hlist) {
  23322. + if (plink->inode == inode) {
  23323. + found = 1;
  23324. + break;
  23325. + }
  23326. + }
  23327. + if (!found)
  23328. + hlist_add_head_rcu(&tmp->hlist, plink_hlist);
  23329. + spin_unlock(&sphl->spin);
  23330. + if (!found) {
  23331. + cnt = au_sphl_count(sphl);
  23332. +#define msg "unexpectedly unblanced or too many pseudo-links"
  23333. + if (cnt > AUFS_PLINK_WARN)
  23334. + AuWarn1(msg ", %d\n", cnt);
  23335. +#undef msg
  23336. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  23337. + } else {
  23338. + do_put_plink(tmp, 0);
  23339. + return;
  23340. + }
  23341. +
  23342. +out:
  23343. + if (unlikely(err)) {
  23344. + pr_warn("err %d, damaged pseudo link.\n", err);
  23345. + if (tmp) {
  23346. + au_sphl_del_rcu(&tmp->hlist, sphl);
  23347. + call_rcu(&tmp->rcu, do_put_plink_rcu);
  23348. + }
  23349. + }
  23350. +}
  23351. +
  23352. +/* free all plinks */
  23353. +void au_plink_put(struct super_block *sb, int verbose)
  23354. +{
  23355. + int i, warned;
  23356. + struct au_sbinfo *sbinfo;
  23357. + struct hlist_head *plink_hlist;
  23358. + struct hlist_node *tmp;
  23359. + struct pseudo_link *plink;
  23360. +
  23361. + SiMustWriteLock(sb);
  23362. +
  23363. + sbinfo = au_sbi(sb);
  23364. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  23365. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  23366. +
  23367. + /* no spin_lock since sbinfo is write-locked */
  23368. + warned = 0;
  23369. + for (i = 0; i < AuPlink_NHASH; i++) {
  23370. + plink_hlist = &sbinfo->si_plink[i].head;
  23371. + if (!warned && verbose && !hlist_empty(plink_hlist)) {
  23372. + pr_warn("pseudo-link is not flushed");
  23373. + warned = 1;
  23374. + }
  23375. + hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
  23376. + do_put_plink(plink, 0);
  23377. + INIT_HLIST_HEAD(plink_hlist);
  23378. + }
  23379. +}
  23380. +
  23381. +void au_plink_clean(struct super_block *sb, int verbose)
  23382. +{
  23383. + struct dentry *root;
  23384. +
  23385. + root = sb->s_root;
  23386. + aufs_write_lock(root);
  23387. + if (au_opt_test(au_mntflags(sb), PLINK))
  23388. + au_plink_put(sb, verbose);
  23389. + aufs_write_unlock(root);
  23390. +}
  23391. +
  23392. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  23393. +{
  23394. + int do_put;
  23395. + aufs_bindex_t bstart, bend, bindex;
  23396. +
  23397. + do_put = 0;
  23398. + bstart = au_ibstart(inode);
  23399. + bend = au_ibend(inode);
  23400. + if (bstart >= 0) {
  23401. + for (bindex = bstart; bindex <= bend; bindex++) {
  23402. + if (!au_h_iptr(inode, bindex)
  23403. + || au_ii_br_id(inode, bindex) != br_id)
  23404. + continue;
  23405. + au_set_h_iptr(inode, bindex, NULL, 0);
  23406. + do_put = 1;
  23407. + break;
  23408. + }
  23409. + if (do_put)
  23410. + for (bindex = bstart; bindex <= bend; bindex++)
  23411. + if (au_h_iptr(inode, bindex)) {
  23412. + do_put = 0;
  23413. + break;
  23414. + }
  23415. + } else
  23416. + do_put = 1;
  23417. +
  23418. + return do_put;
  23419. +}
  23420. +
  23421. +/* free the plinks on a branch specified by @br_id */
  23422. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  23423. +{
  23424. + struct au_sbinfo *sbinfo;
  23425. + struct hlist_head *plink_hlist;
  23426. + struct hlist_node *tmp;
  23427. + struct pseudo_link *plink;
  23428. + struct inode *inode;
  23429. + int i, do_put;
  23430. +
  23431. + SiMustWriteLock(sb);
  23432. +
  23433. + sbinfo = au_sbi(sb);
  23434. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  23435. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  23436. +
  23437. + /* no spin_lock since sbinfo is write-locked */
  23438. + for (i = 0; i < AuPlink_NHASH; i++) {
  23439. + plink_hlist = &sbinfo->si_plink[i].head;
  23440. + hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
  23441. + inode = au_igrab(plink->inode);
  23442. + ii_write_lock_child(inode);
  23443. + do_put = au_plink_do_half_refresh(inode, br_id);
  23444. + if (do_put)
  23445. + do_put_plink(plink, 1);
  23446. + ii_write_unlock(inode);
  23447. + iput(inode);
  23448. + }
  23449. + }
  23450. +}
  23451. diff -Nur linux-4.0.4.orig/fs/aufs/poll.c linux-4.0.4/fs/aufs/poll.c
  23452. --- linux-4.0.4.orig/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
  23453. +++ linux-4.0.4/fs/aufs/poll.c 2015-05-30 22:11:30.000000000 +0200
  23454. @@ -0,0 +1,42 @@
  23455. +/*
  23456. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23457. + */
  23458. +
  23459. +/*
  23460. + * poll operation
  23461. + * There is only one filesystem which implements ->poll operation, currently.
  23462. + */
  23463. +
  23464. +#include "aufs.h"
  23465. +
  23466. +unsigned int aufs_poll(struct file *file, poll_table *wait)
  23467. +{
  23468. + unsigned int mask;
  23469. + int err;
  23470. + struct file *h_file;
  23471. + struct dentry *dentry;
  23472. + struct super_block *sb;
  23473. +
  23474. + /* We should pretend an error happened. */
  23475. + mask = POLLERR /* | POLLIN | POLLOUT */;
  23476. + dentry = file->f_path.dentry;
  23477. + sb = dentry->d_sb;
  23478. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  23479. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  23480. + if (unlikely(err))
  23481. + goto out;
  23482. +
  23483. + /* it is not an error if h_file has no operation */
  23484. + mask = DEFAULT_POLLMASK;
  23485. + h_file = au_hf_top(file);
  23486. + if (h_file->f_op->poll)
  23487. + mask = h_file->f_op->poll(h_file, wait);
  23488. +
  23489. + di_read_unlock(dentry, AuLock_IR);
  23490. + fi_read_unlock(file);
  23491. +
  23492. +out:
  23493. + si_read_unlock(sb);
  23494. + AuTraceErr((int)mask);
  23495. + return mask;
  23496. +}
  23497. diff -Nur linux-4.0.4.orig/fs/aufs/posix_acl.c linux-4.0.4/fs/aufs/posix_acl.c
  23498. --- linux-4.0.4.orig/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
  23499. +++ linux-4.0.4/fs/aufs/posix_acl.c 2015-05-30 22:11:30.000000000 +0200
  23500. @@ -0,0 +1,86 @@
  23501. +/*
  23502. + * Copyright (C) 2014-2015 Junjiro R. Okajima
  23503. + */
  23504. +
  23505. +/*
  23506. + * posix acl operations
  23507. + */
  23508. +
  23509. +#include <linux/fs.h>
  23510. +#include <linux/posix_acl.h>
  23511. +#include "aufs.h"
  23512. +
  23513. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  23514. +{
  23515. + struct posix_acl *acl;
  23516. + int err;
  23517. + aufs_bindex_t bindex;
  23518. + struct inode *h_inode;
  23519. + struct super_block *sb;
  23520. +
  23521. + acl = NULL;
  23522. + sb = inode->i_sb;
  23523. + si_read_lock(sb, AuLock_FLUSH);
  23524. + ii_read_lock_child(inode);
  23525. + if (!(sb->s_flags & MS_POSIXACL))
  23526. + goto out;
  23527. +
  23528. + bindex = au_ibstart(inode);
  23529. + h_inode = au_h_iptr(inode, bindex);
  23530. + if (unlikely(!h_inode
  23531. + || ((h_inode->i_mode & S_IFMT)
  23532. + != (inode->i_mode & S_IFMT)))) {
  23533. + err = au_busy_or_stale();
  23534. + acl = ERR_PTR(err);
  23535. + goto out;
  23536. + }
  23537. +
  23538. + /* always topmost only */
  23539. + acl = get_acl(h_inode, type);
  23540. +
  23541. +out:
  23542. + ii_read_unlock(inode);
  23543. + si_read_unlock(sb);
  23544. +
  23545. + AuTraceErrPtr(acl);
  23546. + return acl;
  23547. +}
  23548. +
  23549. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  23550. +{
  23551. + int err;
  23552. + ssize_t ssz;
  23553. + struct dentry *dentry;
  23554. + struct au_srxattr arg = {
  23555. + .type = AU_ACL_SET,
  23556. + .u.acl_set = {
  23557. + .acl = acl,
  23558. + .type = type
  23559. + },
  23560. + };
  23561. +
  23562. + mutex_lock(&inode->i_mutex);
  23563. + if (inode->i_ino == AUFS_ROOT_INO)
  23564. + dentry = dget(inode->i_sb->s_root);
  23565. + else {
  23566. + dentry = d_find_alias(inode);
  23567. + if (!dentry)
  23568. + dentry = d_find_any_alias(inode);
  23569. + if (!dentry) {
  23570. + pr_warn("cannot handle this inode, "
  23571. + "please report to aufs-users ML\n");
  23572. + err = -ENOENT;
  23573. + goto out;
  23574. + }
  23575. + }
  23576. +
  23577. + ssz = au_srxattr(dentry, &arg);
  23578. + dput(dentry);
  23579. + err = ssz;
  23580. + if (ssz >= 0)
  23581. + err = 0;
  23582. +
  23583. +out:
  23584. + mutex_unlock(&inode->i_mutex);
  23585. + return err;
  23586. +}
  23587. diff -Nur linux-4.0.4.orig/fs/aufs/procfs.c linux-4.0.4/fs/aufs/procfs.c
  23588. --- linux-4.0.4.orig/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
  23589. +++ linux-4.0.4/fs/aufs/procfs.c 2015-05-30 22:11:30.000000000 +0200
  23590. @@ -0,0 +1,156 @@
  23591. +/*
  23592. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  23593. + */
  23594. +
  23595. +/*
  23596. + * procfs interfaces
  23597. + */
  23598. +
  23599. +#include <linux/proc_fs.h>
  23600. +#include "aufs.h"
  23601. +
  23602. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  23603. +{
  23604. + struct au_sbinfo *sbinfo;
  23605. +
  23606. + sbinfo = file->private_data;
  23607. + if (sbinfo) {
  23608. + au_plink_maint_leave(sbinfo);
  23609. + kobject_put(&sbinfo->si_kobj);
  23610. + }
  23611. +
  23612. + return 0;
  23613. +}
  23614. +
  23615. +static void au_procfs_plm_write_clean(struct file *file)
  23616. +{
  23617. + struct au_sbinfo *sbinfo;
  23618. +
  23619. + sbinfo = file->private_data;
  23620. + if (sbinfo)
  23621. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  23622. +}
  23623. +
  23624. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  23625. +{
  23626. + int err;
  23627. + struct super_block *sb;
  23628. + struct au_sbinfo *sbinfo;
  23629. +
  23630. + err = -EBUSY;
  23631. + if (unlikely(file->private_data))
  23632. + goto out;
  23633. +
  23634. + sb = NULL;
  23635. + /* don't use au_sbilist_lock() here */
  23636. + spin_lock(&au_sbilist.spin);
  23637. + list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  23638. + if (id == sysaufs_si_id(sbinfo)) {
  23639. + kobject_get(&sbinfo->si_kobj);
  23640. + sb = sbinfo->si_sb;
  23641. + break;
  23642. + }
  23643. + spin_unlock(&au_sbilist.spin);
  23644. +
  23645. + err = -EINVAL;
  23646. + if (unlikely(!sb))
  23647. + goto out;
  23648. +
  23649. + err = au_plink_maint_enter(sb);
  23650. + if (!err)
  23651. + /* keep kobject_get() */
  23652. + file->private_data = sbinfo;
  23653. + else
  23654. + kobject_put(&sbinfo->si_kobj);
  23655. +out:
  23656. + return err;
  23657. +}
  23658. +
  23659. +/*
  23660. + * Accept a valid "si=xxxx" only.
  23661. + * Once it is accepted successfully, accept "clean" too.
  23662. + */
  23663. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  23664. + size_t count, loff_t *ppos)
  23665. +{
  23666. + ssize_t err;
  23667. + unsigned long id;
  23668. + /* last newline is allowed */
  23669. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  23670. +
  23671. + err = -EACCES;
  23672. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  23673. + goto out;
  23674. +
  23675. + err = -EINVAL;
  23676. + if (unlikely(count > sizeof(buf)))
  23677. + goto out;
  23678. +
  23679. + err = copy_from_user(buf, ubuf, count);
  23680. + if (unlikely(err)) {
  23681. + err = -EFAULT;
  23682. + goto out;
  23683. + }
  23684. + buf[count] = 0;
  23685. +
  23686. + err = -EINVAL;
  23687. + if (!strcmp("clean", buf)) {
  23688. + au_procfs_plm_write_clean(file);
  23689. + goto out_success;
  23690. + } else if (unlikely(strncmp("si=", buf, 3)))
  23691. + goto out;
  23692. +
  23693. + err = kstrtoul(buf + 3, 16, &id);
  23694. + if (unlikely(err))
  23695. + goto out;
  23696. +
  23697. + err = au_procfs_plm_write_si(file, id);
  23698. + if (unlikely(err))
  23699. + goto out;
  23700. +
  23701. +out_success:
  23702. + err = count; /* success */
  23703. +out:
  23704. + return err;
  23705. +}
  23706. +
  23707. +static const struct file_operations au_procfs_plm_fop = {
  23708. + .write = au_procfs_plm_write,
  23709. + .release = au_procfs_plm_release,
  23710. + .owner = THIS_MODULE
  23711. +};
  23712. +
  23713. +/* ---------------------------------------------------------------------- */
  23714. +
  23715. +static struct proc_dir_entry *au_procfs_dir;
  23716. +
  23717. +void au_procfs_fin(void)
  23718. +{
  23719. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  23720. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  23721. +}
  23722. +
  23723. +int __init au_procfs_init(void)
  23724. +{
  23725. + int err;
  23726. + struct proc_dir_entry *entry;
  23727. +
  23728. + err = -ENOMEM;
  23729. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  23730. + if (unlikely(!au_procfs_dir))
  23731. + goto out;
  23732. +
  23733. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
  23734. + au_procfs_dir, &au_procfs_plm_fop);
  23735. + if (unlikely(!entry))
  23736. + goto out_dir;
  23737. +
  23738. + err = 0;
  23739. + goto out; /* success */
  23740. +
  23741. +
  23742. +out_dir:
  23743. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  23744. +out:
  23745. + return err;
  23746. +}
  23747. diff -Nur linux-4.0.4.orig/fs/aufs/rdu.c linux-4.0.4/fs/aufs/rdu.c
  23748. --- linux-4.0.4.orig/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
  23749. +++ linux-4.0.4/fs/aufs/rdu.c 2015-05-30 22:11:30.000000000 +0200
  23750. @@ -0,0 +1,375 @@
  23751. +/*
  23752. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23753. + */
  23754. +
  23755. +/*
  23756. + * readdir in userspace.
  23757. + */
  23758. +
  23759. +#include <linux/compat.h>
  23760. +#include <linux/fs_stack.h>
  23761. +#include <linux/security.h>
  23762. +#include "aufs.h"
  23763. +
  23764. +/* bits for struct aufs_rdu.flags */
  23765. +#define AuRdu_CALLED 1
  23766. +#define AuRdu_CONT (1 << 1)
  23767. +#define AuRdu_FULL (1 << 2)
  23768. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  23769. +#define au_fset_rdu(flags, name) \
  23770. + do { (flags) |= AuRdu_##name; } while (0)
  23771. +#define au_fclr_rdu(flags, name) \
  23772. + do { (flags) &= ~AuRdu_##name; } while (0)
  23773. +
  23774. +struct au_rdu_arg {
  23775. + struct dir_context ctx;
  23776. + struct aufs_rdu *rdu;
  23777. + union au_rdu_ent_ul ent;
  23778. + unsigned long end;
  23779. +
  23780. + struct super_block *sb;
  23781. + int err;
  23782. +};
  23783. +
  23784. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  23785. + loff_t offset, u64 h_ino, unsigned int d_type)
  23786. +{
  23787. + int err, len;
  23788. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  23789. + struct aufs_rdu *rdu = arg->rdu;
  23790. + struct au_rdu_ent ent;
  23791. +
  23792. + err = 0;
  23793. + arg->err = 0;
  23794. + au_fset_rdu(rdu->cookie.flags, CALLED);
  23795. + len = au_rdu_len(nlen);
  23796. + if (arg->ent.ul + len < arg->end) {
  23797. + ent.ino = h_ino;
  23798. + ent.bindex = rdu->cookie.bindex;
  23799. + ent.type = d_type;
  23800. + ent.nlen = nlen;
  23801. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  23802. + ent.type = DT_UNKNOWN;
  23803. +
  23804. + /* unnecessary to support mmap_sem since this is a dir */
  23805. + err = -EFAULT;
  23806. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  23807. + goto out;
  23808. + if (copy_to_user(arg->ent.e->name, name, nlen))
  23809. + goto out;
  23810. + /* the terminating NULL */
  23811. + if (__put_user(0, arg->ent.e->name + nlen))
  23812. + goto out;
  23813. + err = 0;
  23814. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  23815. + arg->ent.ul += len;
  23816. + rdu->rent++;
  23817. + } else {
  23818. + err = -EFAULT;
  23819. + au_fset_rdu(rdu->cookie.flags, FULL);
  23820. + rdu->full = 1;
  23821. + rdu->tail = arg->ent;
  23822. + }
  23823. +
  23824. +out:
  23825. + /* AuTraceErr(err); */
  23826. + return err;
  23827. +}
  23828. +
  23829. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  23830. +{
  23831. + int err;
  23832. + loff_t offset;
  23833. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  23834. +
  23835. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  23836. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  23837. + err = offset;
  23838. + if (unlikely(offset != cookie->h_pos))
  23839. + goto out;
  23840. +
  23841. + err = 0;
  23842. + do {
  23843. + arg->err = 0;
  23844. + au_fclr_rdu(cookie->flags, CALLED);
  23845. + /* smp_mb(); */
  23846. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  23847. + if (err >= 0)
  23848. + err = arg->err;
  23849. + } while (!err
  23850. + && au_ftest_rdu(cookie->flags, CALLED)
  23851. + && !au_ftest_rdu(cookie->flags, FULL));
  23852. + cookie->h_pos = h_file->f_pos;
  23853. +
  23854. +out:
  23855. + AuTraceErr(err);
  23856. + return err;
  23857. +}
  23858. +
  23859. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  23860. +{
  23861. + int err;
  23862. + aufs_bindex_t bend;
  23863. + struct au_rdu_arg arg = {
  23864. + .ctx = {
  23865. + .actor = au_rdu_fill
  23866. + }
  23867. + };
  23868. + struct dentry *dentry;
  23869. + struct inode *inode;
  23870. + struct file *h_file;
  23871. + struct au_rdu_cookie *cookie = &rdu->cookie;
  23872. +
  23873. + err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
  23874. + if (unlikely(err)) {
  23875. + err = -EFAULT;
  23876. + AuTraceErr(err);
  23877. + goto out;
  23878. + }
  23879. + rdu->rent = 0;
  23880. + rdu->tail = rdu->ent;
  23881. + rdu->full = 0;
  23882. + arg.rdu = rdu;
  23883. + arg.ent = rdu->ent;
  23884. + arg.end = arg.ent.ul;
  23885. + arg.end += rdu->sz;
  23886. +
  23887. + err = -ENOTDIR;
  23888. + if (unlikely(!file->f_op->iterate))
  23889. + goto out;
  23890. +
  23891. + err = security_file_permission(file, MAY_READ);
  23892. + AuTraceErr(err);
  23893. + if (unlikely(err))
  23894. + goto out;
  23895. +
  23896. + dentry = file->f_path.dentry;
  23897. + inode = dentry->d_inode;
  23898. +#if 1
  23899. + mutex_lock(&inode->i_mutex);
  23900. +#else
  23901. + err = mutex_lock_killable(&inode->i_mutex);
  23902. + AuTraceErr(err);
  23903. + if (unlikely(err))
  23904. + goto out;
  23905. +#endif
  23906. +
  23907. + arg.sb = inode->i_sb;
  23908. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  23909. + if (unlikely(err))
  23910. + goto out_mtx;
  23911. + err = au_alive_dir(dentry);
  23912. + if (unlikely(err))
  23913. + goto out_si;
  23914. + /* todo: reval? */
  23915. + fi_read_lock(file);
  23916. +
  23917. + err = -EAGAIN;
  23918. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  23919. + && cookie->generation != au_figen(file)))
  23920. + goto out_unlock;
  23921. +
  23922. + err = 0;
  23923. + if (!rdu->blk) {
  23924. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  23925. + if (!rdu->blk)
  23926. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  23927. + }
  23928. + bend = au_fbstart(file);
  23929. + if (cookie->bindex < bend)
  23930. + cookie->bindex = bend;
  23931. + bend = au_fbend_dir(file);
  23932. + /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
  23933. + for (; !err && cookie->bindex <= bend;
  23934. + cookie->bindex++, cookie->h_pos = 0) {
  23935. + h_file = au_hf_dir(file, cookie->bindex);
  23936. + if (!h_file)
  23937. + continue;
  23938. +
  23939. + au_fclr_rdu(cookie->flags, FULL);
  23940. + err = au_rdu_do(h_file, &arg);
  23941. + AuTraceErr(err);
  23942. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  23943. + break;
  23944. + }
  23945. + AuDbg("rent %llu\n", rdu->rent);
  23946. +
  23947. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  23948. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  23949. + au_fset_rdu(cookie->flags, CONT);
  23950. + cookie->generation = au_figen(file);
  23951. + }
  23952. +
  23953. + ii_read_lock_child(inode);
  23954. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
  23955. + ii_read_unlock(inode);
  23956. +
  23957. +out_unlock:
  23958. + fi_read_unlock(file);
  23959. +out_si:
  23960. + si_read_unlock(arg.sb);
  23961. +out_mtx:
  23962. + mutex_unlock(&inode->i_mutex);
  23963. +out:
  23964. + AuTraceErr(err);
  23965. + return err;
  23966. +}
  23967. +
  23968. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  23969. +{
  23970. + int err;
  23971. + ino_t ino;
  23972. + unsigned long long nent;
  23973. + union au_rdu_ent_ul *u;
  23974. + struct au_rdu_ent ent;
  23975. + struct super_block *sb;
  23976. +
  23977. + err = 0;
  23978. + nent = rdu->nent;
  23979. + u = &rdu->ent;
  23980. + sb = file->f_path.dentry->d_sb;
  23981. + si_read_lock(sb, AuLock_FLUSH);
  23982. + while (nent-- > 0) {
  23983. + /* unnecessary to support mmap_sem since this is a dir */
  23984. + err = copy_from_user(&ent, u->e, sizeof(ent));
  23985. + if (!err)
  23986. + err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
  23987. + if (unlikely(err)) {
  23988. + err = -EFAULT;
  23989. + AuTraceErr(err);
  23990. + break;
  23991. + }
  23992. +
  23993. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  23994. + if (!ent.wh)
  23995. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  23996. + else
  23997. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  23998. + &ino);
  23999. + if (unlikely(err)) {
  24000. + AuTraceErr(err);
  24001. + break;
  24002. + }
  24003. +
  24004. + err = __put_user(ino, &u->e->ino);
  24005. + if (unlikely(err)) {
  24006. + err = -EFAULT;
  24007. + AuTraceErr(err);
  24008. + break;
  24009. + }
  24010. + u->ul += au_rdu_len(ent.nlen);
  24011. + }
  24012. + si_read_unlock(sb);
  24013. +
  24014. + return err;
  24015. +}
  24016. +
  24017. +/* ---------------------------------------------------------------------- */
  24018. +
  24019. +static int au_rdu_verify(struct aufs_rdu *rdu)
  24020. +{
  24021. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  24022. + "%llu, b%d, 0x%x, g%u}\n",
  24023. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  24024. + rdu->blk,
  24025. + rdu->rent, rdu->shwh, rdu->full,
  24026. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  24027. + rdu->cookie.generation);
  24028. +
  24029. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  24030. + return 0;
  24031. +
  24032. + AuDbg("%u:%u\n",
  24033. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  24034. + return -EINVAL;
  24035. +}
  24036. +
  24037. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  24038. +{
  24039. + long err, e;
  24040. + struct aufs_rdu rdu;
  24041. + void __user *p = (void __user *)arg;
  24042. +
  24043. + err = copy_from_user(&rdu, p, sizeof(rdu));
  24044. + if (unlikely(err)) {
  24045. + err = -EFAULT;
  24046. + AuTraceErr(err);
  24047. + goto out;
  24048. + }
  24049. + err = au_rdu_verify(&rdu);
  24050. + if (unlikely(err))
  24051. + goto out;
  24052. +
  24053. + switch (cmd) {
  24054. + case AUFS_CTL_RDU:
  24055. + err = au_rdu(file, &rdu);
  24056. + if (unlikely(err))
  24057. + break;
  24058. +
  24059. + e = copy_to_user(p, &rdu, sizeof(rdu));
  24060. + if (unlikely(e)) {
  24061. + err = -EFAULT;
  24062. + AuTraceErr(err);
  24063. + }
  24064. + break;
  24065. + case AUFS_CTL_RDU_INO:
  24066. + err = au_rdu_ino(file, &rdu);
  24067. + break;
  24068. +
  24069. + default:
  24070. + /* err = -ENOTTY; */
  24071. + err = -EINVAL;
  24072. + }
  24073. +
  24074. +out:
  24075. + AuTraceErr(err);
  24076. + return err;
  24077. +}
  24078. +
  24079. +#ifdef CONFIG_COMPAT
  24080. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  24081. +{
  24082. + long err, e;
  24083. + struct aufs_rdu rdu;
  24084. + void __user *p = compat_ptr(arg);
  24085. +
  24086. + /* todo: get_user()? */
  24087. + err = copy_from_user(&rdu, p, sizeof(rdu));
  24088. + if (unlikely(err)) {
  24089. + err = -EFAULT;
  24090. + AuTraceErr(err);
  24091. + goto out;
  24092. + }
  24093. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  24094. + err = au_rdu_verify(&rdu);
  24095. + if (unlikely(err))
  24096. + goto out;
  24097. +
  24098. + switch (cmd) {
  24099. + case AUFS_CTL_RDU:
  24100. + err = au_rdu(file, &rdu);
  24101. + if (unlikely(err))
  24102. + break;
  24103. +
  24104. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  24105. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  24106. + e = copy_to_user(p, &rdu, sizeof(rdu));
  24107. + if (unlikely(e)) {
  24108. + err = -EFAULT;
  24109. + AuTraceErr(err);
  24110. + }
  24111. + break;
  24112. + case AUFS_CTL_RDU_INO:
  24113. + err = au_rdu_ino(file, &rdu);
  24114. + break;
  24115. +
  24116. + default:
  24117. + /* err = -ENOTTY; */
  24118. + err = -EINVAL;
  24119. + }
  24120. +
  24121. +out:
  24122. + AuTraceErr(err);
  24123. + return err;
  24124. +}
  24125. +#endif
  24126. diff -Nur linux-4.0.4.orig/fs/aufs/rwsem.h linux-4.0.4/fs/aufs/rwsem.h
  24127. --- linux-4.0.4.orig/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
  24128. +++ linux-4.0.4/fs/aufs/rwsem.h 2015-05-30 22:11:30.000000000 +0200
  24129. @@ -0,0 +1,178 @@
  24130. +/*
  24131. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24132. + */
  24133. +
  24134. +/*
  24135. + * simple read-write semaphore wrappers
  24136. + */
  24137. +
  24138. +#ifndef __AUFS_RWSEM_H__
  24139. +#define __AUFS_RWSEM_H__
  24140. +
  24141. +#ifdef __KERNEL__
  24142. +
  24143. +#include "debug.h"
  24144. +
  24145. +struct au_rwsem {
  24146. + struct rw_semaphore rwsem;
  24147. +#ifdef CONFIG_AUFS_DEBUG
  24148. + /* just for debugging, not almighty counter */
  24149. + atomic_t rcnt, wcnt;
  24150. +#endif
  24151. +};
  24152. +
  24153. +#ifdef CONFIG_AUFS_DEBUG
  24154. +#define AuDbgCntInit(rw) do { \
  24155. + atomic_set(&(rw)->rcnt, 0); \
  24156. + atomic_set(&(rw)->wcnt, 0); \
  24157. + smp_mb(); /* atomic set */ \
  24158. +} while (0)
  24159. +
  24160. +#define AuDbgRcntInc(rw) atomic_inc(&(rw)->rcnt)
  24161. +#define AuDbgRcntDec(rw) WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
  24162. +#define AuDbgWcntInc(rw) atomic_inc(&(rw)->wcnt)
  24163. +#define AuDbgWcntDec(rw) WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
  24164. +#else
  24165. +#define AuDbgCntInit(rw) do {} while (0)
  24166. +#define AuDbgRcntInc(rw) do {} while (0)
  24167. +#define AuDbgRcntDec(rw) do {} while (0)
  24168. +#define AuDbgWcntInc(rw) do {} while (0)
  24169. +#define AuDbgWcntDec(rw) do {} while (0)
  24170. +#endif /* CONFIG_AUFS_DEBUG */
  24171. +
  24172. +/* to debug easier, do not make them inlined functions */
  24173. +#define AuRwMustNoWaiters(rw) AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
  24174. +/* rwsem_is_locked() is unusable */
  24175. +#define AuRwMustReadLock(rw) AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
  24176. +#define AuRwMustWriteLock(rw) AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
  24177. +#define AuRwMustAnyLock(rw) AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
  24178. + && atomic_read(&(rw)->wcnt) <= 0)
  24179. +#define AuRwDestroy(rw) AuDebugOn(atomic_read(&(rw)->rcnt) \
  24180. + || atomic_read(&(rw)->wcnt))
  24181. +
  24182. +#define au_rw_class(rw, key) lockdep_set_class(&(rw)->rwsem, key)
  24183. +
  24184. +static inline void au_rw_init(struct au_rwsem *rw)
  24185. +{
  24186. + AuDbgCntInit(rw);
  24187. + init_rwsem(&rw->rwsem);
  24188. +}
  24189. +
  24190. +static inline void au_rw_init_wlock(struct au_rwsem *rw)
  24191. +{
  24192. + au_rw_init(rw);
  24193. + down_write(&rw->rwsem);
  24194. + AuDbgWcntInc(rw);
  24195. +}
  24196. +
  24197. +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
  24198. + unsigned int lsc)
  24199. +{
  24200. + au_rw_init(rw);
  24201. + down_write_nested(&rw->rwsem, lsc);
  24202. + AuDbgWcntInc(rw);
  24203. +}
  24204. +
  24205. +static inline void au_rw_read_lock(struct au_rwsem *rw)
  24206. +{
  24207. + down_read(&rw->rwsem);
  24208. + AuDbgRcntInc(rw);
  24209. +}
  24210. +
  24211. +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
  24212. +{
  24213. + down_read_nested(&rw->rwsem, lsc);
  24214. + AuDbgRcntInc(rw);
  24215. +}
  24216. +
  24217. +static inline void au_rw_read_unlock(struct au_rwsem *rw)
  24218. +{
  24219. + AuRwMustReadLock(rw);
  24220. + AuDbgRcntDec(rw);
  24221. + up_read(&rw->rwsem);
  24222. +}
  24223. +
  24224. +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
  24225. +{
  24226. + AuRwMustWriteLock(rw);
  24227. + AuDbgRcntInc(rw);
  24228. + AuDbgWcntDec(rw);
  24229. + downgrade_write(&rw->rwsem);
  24230. +}
  24231. +
  24232. +static inline void au_rw_write_lock(struct au_rwsem *rw)
  24233. +{
  24234. + down_write(&rw->rwsem);
  24235. + AuDbgWcntInc(rw);
  24236. +}
  24237. +
  24238. +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
  24239. + unsigned int lsc)
  24240. +{
  24241. + down_write_nested(&rw->rwsem, lsc);
  24242. + AuDbgWcntInc(rw);
  24243. +}
  24244. +
  24245. +static inline void au_rw_write_unlock(struct au_rwsem *rw)
  24246. +{
  24247. + AuRwMustWriteLock(rw);
  24248. + AuDbgWcntDec(rw);
  24249. + up_write(&rw->rwsem);
  24250. +}
  24251. +
  24252. +/* why is not _nested version defined */
  24253. +static inline int au_rw_read_trylock(struct au_rwsem *rw)
  24254. +{
  24255. + int ret;
  24256. +
  24257. + ret = down_read_trylock(&rw->rwsem);
  24258. + if (ret)
  24259. + AuDbgRcntInc(rw);
  24260. + return ret;
  24261. +}
  24262. +
  24263. +static inline int au_rw_write_trylock(struct au_rwsem *rw)
  24264. +{
  24265. + int ret;
  24266. +
  24267. + ret = down_write_trylock(&rw->rwsem);
  24268. + if (ret)
  24269. + AuDbgWcntInc(rw);
  24270. + return ret;
  24271. +}
  24272. +
  24273. +#undef AuDbgCntInit
  24274. +#undef AuDbgRcntInc
  24275. +#undef AuDbgRcntDec
  24276. +#undef AuDbgWcntInc
  24277. +#undef AuDbgWcntDec
  24278. +
  24279. +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  24280. +static inline void prefix##_read_lock(param) \
  24281. +{ au_rw_read_lock(rwsem); } \
  24282. +static inline void prefix##_write_lock(param) \
  24283. +{ au_rw_write_lock(rwsem); } \
  24284. +static inline int prefix##_read_trylock(param) \
  24285. +{ return au_rw_read_trylock(rwsem); } \
  24286. +static inline int prefix##_write_trylock(param) \
  24287. +{ return au_rw_write_trylock(rwsem); }
  24288. +/* why is not _nested version defined */
  24289. +/* static inline void prefix##_read_trylock_nested(param, lsc)
  24290. +{ au_rw_read_trylock_nested(rwsem, lsc)); }
  24291. +static inline void prefix##_write_trylock_nestd(param, lsc)
  24292. +{ au_rw_write_trylock_nested(rwsem, lsc); } */
  24293. +
  24294. +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
  24295. +static inline void prefix##_read_unlock(param) \
  24296. +{ au_rw_read_unlock(rwsem); } \
  24297. +static inline void prefix##_write_unlock(param) \
  24298. +{ au_rw_write_unlock(rwsem); } \
  24299. +static inline void prefix##_downgrade_lock(param) \
  24300. +{ au_rw_dgrade_lock(rwsem); }
  24301. +
  24302. +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
  24303. + AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  24304. + AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
  24305. +
  24306. +#endif /* __KERNEL__ */
  24307. +#endif /* __AUFS_RWSEM_H__ */
  24308. diff -Nur linux-4.0.4.orig/fs/aufs/sbinfo.c linux-4.0.4/fs/aufs/sbinfo.c
  24309. --- linux-4.0.4.orig/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
  24310. +++ linux-4.0.4/fs/aufs/sbinfo.c 2015-05-30 22:11:31.000000000 +0200
  24311. @@ -0,0 +1,341 @@
  24312. +/*
  24313. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24314. + */
  24315. +
  24316. +/*
  24317. + * superblock private data
  24318. + */
  24319. +
  24320. +#include "aufs.h"
  24321. +
  24322. +/*
  24323. + * they are necessary regardless sysfs is disabled.
  24324. + */
  24325. +void au_si_free(struct kobject *kobj)
  24326. +{
  24327. + int i;
  24328. + struct au_sbinfo *sbinfo;
  24329. + char *locked __maybe_unused; /* debug only */
  24330. +
  24331. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  24332. + for (i = 0; i < AuPlink_NHASH; i++)
  24333. + AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
  24334. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  24335. +
  24336. + au_rw_write_lock(&sbinfo->si_rwsem);
  24337. + au_br_free(sbinfo);
  24338. + au_rw_write_unlock(&sbinfo->si_rwsem);
  24339. +
  24340. + AuDebugOn(radix_tree_gang_lookup
  24341. + (&sbinfo->au_si_pid.tree, (void **)&locked,
  24342. + /*first_index*/PID_MAX_DEFAULT - 1,
  24343. + /*max_items*/sizeof(locked)/sizeof(*locked)));
  24344. +
  24345. + kfree(sbinfo->si_branch);
  24346. + kfree(sbinfo->au_si_pid.bitmap);
  24347. + mutex_destroy(&sbinfo->si_xib_mtx);
  24348. + AuRwDestroy(&sbinfo->si_rwsem);
  24349. +
  24350. + kfree(sbinfo);
  24351. +}
  24352. +
  24353. +int au_si_alloc(struct super_block *sb)
  24354. +{
  24355. + int err, i;
  24356. + struct au_sbinfo *sbinfo;
  24357. + static struct lock_class_key aufs_si;
  24358. +
  24359. + err = -ENOMEM;
  24360. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  24361. + if (unlikely(!sbinfo))
  24362. + goto out;
  24363. +
  24364. + BUILD_BUG_ON(sizeof(unsigned long) !=
  24365. + sizeof(*sbinfo->au_si_pid.bitmap));
  24366. + sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
  24367. + sizeof(*sbinfo->au_si_pid.bitmap),
  24368. + GFP_NOFS);
  24369. + if (unlikely(!sbinfo->au_si_pid.bitmap))
  24370. + goto out_sbinfo;
  24371. +
  24372. + /* will be reallocated separately */
  24373. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  24374. + if (unlikely(!sbinfo->si_branch))
  24375. + goto out_pidmap;
  24376. +
  24377. + err = sysaufs_si_init(sbinfo);
  24378. + if (unlikely(err))
  24379. + goto out_br;
  24380. +
  24381. + au_nwt_init(&sbinfo->si_nowait);
  24382. + au_rw_init_wlock(&sbinfo->si_rwsem);
  24383. + au_rw_class(&sbinfo->si_rwsem, &aufs_si);
  24384. + spin_lock_init(&sbinfo->au_si_pid.tree_lock);
  24385. + INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
  24386. +
  24387. + atomic_long_set(&sbinfo->si_ninodes, 0);
  24388. + atomic_long_set(&sbinfo->si_nfiles, 0);
  24389. +
  24390. + sbinfo->si_bend = -1;
  24391. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  24392. +
  24393. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  24394. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  24395. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  24396. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  24397. +
  24398. + au_fhsm_init(sbinfo);
  24399. +
  24400. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  24401. +
  24402. + sbinfo->si_xino_jiffy = jiffies;
  24403. + sbinfo->si_xino_expire
  24404. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  24405. + mutex_init(&sbinfo->si_xib_mtx);
  24406. + sbinfo->si_xino_brid = -1;
  24407. + /* leave si_xib_last_pindex and si_xib_next_bit */
  24408. +
  24409. + au_sphl_init(&sbinfo->si_aopen);
  24410. +
  24411. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  24412. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  24413. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  24414. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  24415. +
  24416. + for (i = 0; i < AuPlink_NHASH; i++)
  24417. + au_sphl_init(sbinfo->si_plink + i);
  24418. + init_waitqueue_head(&sbinfo->si_plink_wq);
  24419. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  24420. +
  24421. + au_sphl_init(&sbinfo->si_files);
  24422. +
  24423. + /* leave other members for sysaufs and si_mnt. */
  24424. + sbinfo->si_sb = sb;
  24425. + sb->s_fs_info = sbinfo;
  24426. + si_pid_set(sb);
  24427. + return 0; /* success */
  24428. +
  24429. +out_br:
  24430. + kfree(sbinfo->si_branch);
  24431. +out_pidmap:
  24432. + kfree(sbinfo->au_si_pid.bitmap);
  24433. +out_sbinfo:
  24434. + kfree(sbinfo);
  24435. +out:
  24436. + return err;
  24437. +}
  24438. +
  24439. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
  24440. +{
  24441. + int err, sz;
  24442. + struct au_branch **brp;
  24443. +
  24444. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  24445. +
  24446. + err = -ENOMEM;
  24447. + sz = sizeof(*brp) * (sbinfo->si_bend + 1);
  24448. + if (unlikely(!sz))
  24449. + sz = sizeof(*brp);
  24450. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
  24451. + if (brp) {
  24452. + sbinfo->si_branch = brp;
  24453. + err = 0;
  24454. + }
  24455. +
  24456. + return err;
  24457. +}
  24458. +
  24459. +/* ---------------------------------------------------------------------- */
  24460. +
  24461. +unsigned int au_sigen_inc(struct super_block *sb)
  24462. +{
  24463. + unsigned int gen;
  24464. +
  24465. + SiMustWriteLock(sb);
  24466. +
  24467. + gen = ++au_sbi(sb)->si_generation;
  24468. + au_update_digen(sb->s_root);
  24469. + au_update_iigen(sb->s_root->d_inode, /*half*/0);
  24470. + sb->s_root->d_inode->i_version++;
  24471. + return gen;
  24472. +}
  24473. +
  24474. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  24475. +{
  24476. + aufs_bindex_t br_id;
  24477. + int i;
  24478. + struct au_sbinfo *sbinfo;
  24479. +
  24480. + SiMustWriteLock(sb);
  24481. +
  24482. + sbinfo = au_sbi(sb);
  24483. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  24484. + br_id = ++sbinfo->si_last_br_id;
  24485. + AuDebugOn(br_id < 0);
  24486. + if (br_id && au_br_index(sb, br_id) < 0)
  24487. + return br_id;
  24488. + }
  24489. +
  24490. + return -1;
  24491. +}
  24492. +
  24493. +/* ---------------------------------------------------------------------- */
  24494. +
  24495. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  24496. +int si_read_lock(struct super_block *sb, int flags)
  24497. +{
  24498. + int err;
  24499. +
  24500. + err = 0;
  24501. + if (au_ftest_lock(flags, FLUSH))
  24502. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  24503. +
  24504. + si_noflush_read_lock(sb);
  24505. + err = au_plink_maint(sb, flags);
  24506. + if (unlikely(err))
  24507. + si_read_unlock(sb);
  24508. +
  24509. + return err;
  24510. +}
  24511. +
  24512. +int si_write_lock(struct super_block *sb, int flags)
  24513. +{
  24514. + int err;
  24515. +
  24516. + if (au_ftest_lock(flags, FLUSH))
  24517. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  24518. +
  24519. + si_noflush_write_lock(sb);
  24520. + err = au_plink_maint(sb, flags);
  24521. + if (unlikely(err))
  24522. + si_write_unlock(sb);
  24523. +
  24524. + return err;
  24525. +}
  24526. +
  24527. +/* dentry and super_block lock. call at entry point */
  24528. +int aufs_read_lock(struct dentry *dentry, int flags)
  24529. +{
  24530. + int err;
  24531. + struct super_block *sb;
  24532. +
  24533. + sb = dentry->d_sb;
  24534. + err = si_read_lock(sb, flags);
  24535. + if (unlikely(err))
  24536. + goto out;
  24537. +
  24538. + if (au_ftest_lock(flags, DW))
  24539. + di_write_lock_child(dentry);
  24540. + else
  24541. + di_read_lock_child(dentry, flags);
  24542. +
  24543. + if (au_ftest_lock(flags, GEN)) {
  24544. + err = au_digen_test(dentry, au_sigen(sb));
  24545. + AuDebugOn(!err && au_dbrange_test(dentry));
  24546. + if (unlikely(err))
  24547. + aufs_read_unlock(dentry, flags);
  24548. + }
  24549. +
  24550. +out:
  24551. + return err;
  24552. +}
  24553. +
  24554. +void aufs_read_unlock(struct dentry *dentry, int flags)
  24555. +{
  24556. + if (au_ftest_lock(flags, DW))
  24557. + di_write_unlock(dentry);
  24558. + else
  24559. + di_read_unlock(dentry, flags);
  24560. + si_read_unlock(dentry->d_sb);
  24561. +}
  24562. +
  24563. +void aufs_write_lock(struct dentry *dentry)
  24564. +{
  24565. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  24566. + di_write_lock_child(dentry);
  24567. +}
  24568. +
  24569. +void aufs_write_unlock(struct dentry *dentry)
  24570. +{
  24571. + di_write_unlock(dentry);
  24572. + si_write_unlock(dentry->d_sb);
  24573. +}
  24574. +
  24575. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  24576. +{
  24577. + int err;
  24578. + unsigned int sigen;
  24579. + struct super_block *sb;
  24580. +
  24581. + sb = d1->d_sb;
  24582. + err = si_read_lock(sb, flags);
  24583. + if (unlikely(err))
  24584. + goto out;
  24585. +
  24586. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
  24587. +
  24588. + if (au_ftest_lock(flags, GEN)) {
  24589. + sigen = au_sigen(sb);
  24590. + err = au_digen_test(d1, sigen);
  24591. + AuDebugOn(!err && au_dbrange_test(d1));
  24592. + if (!err) {
  24593. + err = au_digen_test(d2, sigen);
  24594. + AuDebugOn(!err && au_dbrange_test(d2));
  24595. + }
  24596. + if (unlikely(err))
  24597. + aufs_read_and_write_unlock2(d1, d2);
  24598. + }
  24599. +
  24600. +out:
  24601. + return err;
  24602. +}
  24603. +
  24604. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  24605. +{
  24606. + di_write_unlock2(d1, d2);
  24607. + si_read_unlock(d1->d_sb);
  24608. +}
  24609. +
  24610. +/* ---------------------------------------------------------------------- */
  24611. +
  24612. +int si_pid_test_slow(struct super_block *sb)
  24613. +{
  24614. + void *p;
  24615. +
  24616. + rcu_read_lock();
  24617. + p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
  24618. + rcu_read_unlock();
  24619. +
  24620. + return (long)!!p;
  24621. +}
  24622. +
  24623. +void si_pid_set_slow(struct super_block *sb)
  24624. +{
  24625. + int err;
  24626. + struct au_sbinfo *sbinfo;
  24627. +
  24628. + AuDebugOn(si_pid_test_slow(sb));
  24629. +
  24630. + sbinfo = au_sbi(sb);
  24631. + err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  24632. + AuDebugOn(err);
  24633. + spin_lock(&sbinfo->au_si_pid.tree_lock);
  24634. + err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
  24635. + /*any valid ptr*/sb);
  24636. + spin_unlock(&sbinfo->au_si_pid.tree_lock);
  24637. + AuDebugOn(err);
  24638. + radix_tree_preload_end();
  24639. +}
  24640. +
  24641. +void si_pid_clr_slow(struct super_block *sb)
  24642. +{
  24643. + void *p;
  24644. + struct au_sbinfo *sbinfo;
  24645. +
  24646. + AuDebugOn(!si_pid_test_slow(sb));
  24647. +
  24648. + sbinfo = au_sbi(sb);
  24649. + spin_lock(&sbinfo->au_si_pid.tree_lock);
  24650. + p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
  24651. + spin_unlock(&sbinfo->au_si_pid.tree_lock);
  24652. +}
  24653. diff -Nur linux-4.0.4.orig/fs/aufs/spl.h linux-4.0.4/fs/aufs/spl.h
  24654. --- linux-4.0.4.orig/fs/aufs/spl.h 1970-01-01 01:00:00.000000000 +0100
  24655. +++ linux-4.0.4/fs/aufs/spl.h 2015-05-30 22:11:30.000000000 +0200
  24656. @@ -0,0 +1,98 @@
  24657. +/*
  24658. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24659. + */
  24660. +
  24661. +/*
  24662. + * simple list protected by a spinlock
  24663. + */
  24664. +
  24665. +#ifndef __AUFS_SPL_H__
  24666. +#define __AUFS_SPL_H__
  24667. +
  24668. +#ifdef __KERNEL__
  24669. +
  24670. +struct au_splhead {
  24671. + spinlock_t spin;
  24672. + struct list_head head;
  24673. +};
  24674. +
  24675. +static inline void au_spl_init(struct au_splhead *spl)
  24676. +{
  24677. + spin_lock_init(&spl->spin);
  24678. + INIT_LIST_HEAD(&spl->head);
  24679. +}
  24680. +
  24681. +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
  24682. +{
  24683. + spin_lock(&spl->spin);
  24684. + list_add(list, &spl->head);
  24685. + spin_unlock(&spl->spin);
  24686. +}
  24687. +
  24688. +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
  24689. +{
  24690. + spin_lock(&spl->spin);
  24691. + list_del(list);
  24692. + spin_unlock(&spl->spin);
  24693. +}
  24694. +
  24695. +static inline void au_spl_del_rcu(struct list_head *list,
  24696. + struct au_splhead *spl)
  24697. +{
  24698. + spin_lock(&spl->spin);
  24699. + list_del_rcu(list);
  24700. + spin_unlock(&spl->spin);
  24701. +}
  24702. +
  24703. +/* ---------------------------------------------------------------------- */
  24704. +
  24705. +struct au_sphlhead {
  24706. + spinlock_t spin;
  24707. + struct hlist_head head;
  24708. +};
  24709. +
  24710. +static inline void au_sphl_init(struct au_sphlhead *sphl)
  24711. +{
  24712. + spin_lock_init(&sphl->spin);
  24713. + INIT_HLIST_HEAD(&sphl->head);
  24714. +}
  24715. +
  24716. +static inline void au_sphl_add(struct hlist_node *hlist,
  24717. + struct au_sphlhead *sphl)
  24718. +{
  24719. + spin_lock(&sphl->spin);
  24720. + hlist_add_head(hlist, &sphl->head);
  24721. + spin_unlock(&sphl->spin);
  24722. +}
  24723. +
  24724. +static inline void au_sphl_del(struct hlist_node *hlist,
  24725. + struct au_sphlhead *sphl)
  24726. +{
  24727. + spin_lock(&sphl->spin);
  24728. + hlist_del(hlist);
  24729. + spin_unlock(&sphl->spin);
  24730. +}
  24731. +
  24732. +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
  24733. + struct au_sphlhead *sphl)
  24734. +{
  24735. + spin_lock(&sphl->spin);
  24736. + hlist_del_rcu(hlist);
  24737. + spin_unlock(&sphl->spin);
  24738. +}
  24739. +
  24740. +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
  24741. +{
  24742. + unsigned long cnt;
  24743. + struct hlist_node *pos;
  24744. +
  24745. + cnt = 0;
  24746. + spin_lock(&sphl->spin);
  24747. + hlist_for_each(pos, &sphl->head)
  24748. + cnt++;
  24749. + spin_unlock(&sphl->spin);
  24750. + return cnt;
  24751. +}
  24752. +
  24753. +#endif /* __KERNEL__ */
  24754. +#endif /* __AUFS_SPL_H__ */
  24755. diff -Nur linux-4.0.4.orig/fs/aufs/super.c linux-4.0.4/fs/aufs/super.c
  24756. --- linux-4.0.4.orig/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
  24757. +++ linux-4.0.4/fs/aufs/super.c 2015-05-30 22:11:31.000000000 +0200
  24758. @@ -0,0 +1,993 @@
  24759. +/*
  24760. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24761. + */
  24762. +
  24763. +/*
  24764. + * mount and super_block operations
  24765. + */
  24766. +
  24767. +#include <linux/mm.h>
  24768. +#include <linux/seq_file.h>
  24769. +#include <linux/statfs.h>
  24770. +#include <linux/vmalloc.h>
  24771. +#include "aufs.h"
  24772. +
  24773. +/*
  24774. + * super_operations
  24775. + */
  24776. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  24777. +{
  24778. + struct au_icntnr *c;
  24779. +
  24780. + c = au_cache_alloc_icntnr();
  24781. + if (c) {
  24782. + au_icntnr_init(c);
  24783. + c->vfs_inode.i_version = 1; /* sigen(sb); */
  24784. + c->iinfo.ii_hinode = NULL;
  24785. + return &c->vfs_inode;
  24786. + }
  24787. + return NULL;
  24788. +}
  24789. +
  24790. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  24791. +{
  24792. + struct inode *inode = container_of(head, struct inode, i_rcu);
  24793. +
  24794. + INIT_HLIST_HEAD(&inode->i_dentry);
  24795. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  24796. +}
  24797. +
  24798. +static void aufs_destroy_inode(struct inode *inode)
  24799. +{
  24800. + au_iinfo_fin(inode);
  24801. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  24802. +}
  24803. +
  24804. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  24805. +{
  24806. + struct inode *inode;
  24807. + int err;
  24808. +
  24809. + inode = iget_locked(sb, ino);
  24810. + if (unlikely(!inode)) {
  24811. + inode = ERR_PTR(-ENOMEM);
  24812. + goto out;
  24813. + }
  24814. + if (!(inode->i_state & I_NEW))
  24815. + goto out;
  24816. +
  24817. + err = au_xigen_new(inode);
  24818. + if (!err)
  24819. + err = au_iinfo_init(inode);
  24820. + if (!err)
  24821. + inode->i_version++;
  24822. + else {
  24823. + iget_failed(inode);
  24824. + inode = ERR_PTR(err);
  24825. + }
  24826. +
  24827. +out:
  24828. + /* never return NULL */
  24829. + AuDebugOn(!inode);
  24830. + AuTraceErrPtr(inode);
  24831. + return inode;
  24832. +}
  24833. +
  24834. +/* lock free root dinfo */
  24835. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  24836. +{
  24837. + int err;
  24838. + aufs_bindex_t bindex, bend;
  24839. + struct path path;
  24840. + struct au_hdentry *hdp;
  24841. + struct au_branch *br;
  24842. + au_br_perm_str_t perm;
  24843. +
  24844. + err = 0;
  24845. + bend = au_sbend(sb);
  24846. + hdp = au_di(sb->s_root)->di_hdentry;
  24847. + for (bindex = 0; !err && bindex <= bend; bindex++) {
  24848. + br = au_sbr(sb, bindex);
  24849. + path.mnt = au_br_mnt(br);
  24850. + path.dentry = hdp[bindex].hd_dentry;
  24851. + err = au_seq_path(seq, &path);
  24852. + if (err > 0) {
  24853. + au_optstr_br_perm(&perm, br->br_perm);
  24854. + err = seq_printf(seq, "=%s", perm.a);
  24855. + if (err == -1)
  24856. + err = -E2BIG;
  24857. + }
  24858. + if (!err && bindex != bend)
  24859. + err = seq_putc(seq, ':');
  24860. + }
  24861. +
  24862. + return err;
  24863. +}
  24864. +
  24865. +static void au_show_wbr_create(struct seq_file *m, int v,
  24866. + struct au_sbinfo *sbinfo)
  24867. +{
  24868. + const char *pat;
  24869. +
  24870. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  24871. +
  24872. + seq_puts(m, ",create=");
  24873. + pat = au_optstr_wbr_create(v);
  24874. + switch (v) {
  24875. + case AuWbrCreate_TDP:
  24876. + case AuWbrCreate_RR:
  24877. + case AuWbrCreate_MFS:
  24878. + case AuWbrCreate_PMFS:
  24879. + seq_puts(m, pat);
  24880. + break;
  24881. + case AuWbrCreate_MFSV:
  24882. + seq_printf(m, /*pat*/"mfs:%lu",
  24883. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  24884. + / MSEC_PER_SEC);
  24885. + break;
  24886. + case AuWbrCreate_PMFSV:
  24887. + seq_printf(m, /*pat*/"pmfs:%lu",
  24888. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  24889. + / MSEC_PER_SEC);
  24890. + break;
  24891. + case AuWbrCreate_MFSRR:
  24892. + seq_printf(m, /*pat*/"mfsrr:%llu",
  24893. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  24894. + break;
  24895. + case AuWbrCreate_MFSRRV:
  24896. + seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
  24897. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  24898. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  24899. + / MSEC_PER_SEC);
  24900. + break;
  24901. + case AuWbrCreate_PMFSRR:
  24902. + seq_printf(m, /*pat*/"pmfsrr:%llu",
  24903. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  24904. + break;
  24905. + case AuWbrCreate_PMFSRRV:
  24906. + seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
  24907. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  24908. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  24909. + / MSEC_PER_SEC);
  24910. + break;
  24911. + }
  24912. +}
  24913. +
  24914. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  24915. +{
  24916. +#ifdef CONFIG_SYSFS
  24917. + return 0;
  24918. +#else
  24919. + int err;
  24920. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  24921. + aufs_bindex_t bindex, brid;
  24922. + struct qstr *name;
  24923. + struct file *f;
  24924. + struct dentry *d, *h_root;
  24925. + struct au_hdentry *hdp;
  24926. +
  24927. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  24928. +
  24929. + err = 0;
  24930. + f = au_sbi(sb)->si_xib;
  24931. + if (!f)
  24932. + goto out;
  24933. +
  24934. + /* stop printing the default xino path on the first writable branch */
  24935. + h_root = NULL;
  24936. + brid = au_xino_brid(sb);
  24937. + if (brid >= 0) {
  24938. + bindex = au_br_index(sb, brid);
  24939. + hdp = au_di(sb->s_root)->di_hdentry;
  24940. + h_root = hdp[0 + bindex].hd_dentry;
  24941. + }
  24942. + d = f->f_path.dentry;
  24943. + name = &d->d_name;
  24944. + /* safe ->d_parent because the file is unlinked */
  24945. + if (d->d_parent == h_root
  24946. + && name->len == len
  24947. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  24948. + goto out;
  24949. +
  24950. + seq_puts(seq, ",xino=");
  24951. + err = au_xino_path(seq, f);
  24952. +
  24953. +out:
  24954. + return err;
  24955. +#endif
  24956. +}
  24957. +
  24958. +/* seq_file will re-call me in case of too long string */
  24959. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  24960. +{
  24961. + int err;
  24962. + unsigned int mnt_flags, v;
  24963. + struct super_block *sb;
  24964. + struct au_sbinfo *sbinfo;
  24965. +
  24966. +#define AuBool(name, str) do { \
  24967. + v = au_opt_test(mnt_flags, name); \
  24968. + if (v != au_opt_test(AuOpt_Def, name)) \
  24969. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  24970. +} while (0)
  24971. +
  24972. +#define AuStr(name, str) do { \
  24973. + v = mnt_flags & AuOptMask_##name; \
  24974. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  24975. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  24976. +} while (0)
  24977. +
  24978. +#define AuUInt(name, str, val) do { \
  24979. + if (val != AUFS_##name##_DEF) \
  24980. + seq_printf(m, "," #str "=%u", val); \
  24981. +} while (0)
  24982. +
  24983. + sb = dentry->d_sb;
  24984. + if (sb->s_flags & MS_POSIXACL)
  24985. + seq_puts(m, ",acl");
  24986. +
  24987. + /* lock free root dinfo */
  24988. + si_noflush_read_lock(sb);
  24989. + sbinfo = au_sbi(sb);
  24990. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  24991. +
  24992. + mnt_flags = au_mntflags(sb);
  24993. + if (au_opt_test(mnt_flags, XINO)) {
  24994. + err = au_show_xino(m, sb);
  24995. + if (unlikely(err))
  24996. + goto out;
  24997. + } else
  24998. + seq_puts(m, ",noxino");
  24999. +
  25000. + AuBool(TRUNC_XINO, trunc_xino);
  25001. + AuStr(UDBA, udba);
  25002. + AuBool(SHWH, shwh);
  25003. + AuBool(PLINK, plink);
  25004. + AuBool(DIO, dio);
  25005. + AuBool(DIRPERM1, dirperm1);
  25006. +
  25007. + v = sbinfo->si_wbr_create;
  25008. + if (v != AuWbrCreate_Def)
  25009. + au_show_wbr_create(m, v, sbinfo);
  25010. +
  25011. + v = sbinfo->si_wbr_copyup;
  25012. + if (v != AuWbrCopyup_Def)
  25013. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  25014. +
  25015. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  25016. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  25017. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  25018. +
  25019. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  25020. +
  25021. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  25022. + AuUInt(RDCACHE, rdcache, v);
  25023. +
  25024. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  25025. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  25026. +
  25027. + au_fhsm_show(m, sbinfo);
  25028. +
  25029. + AuBool(SUM, sum);
  25030. + /* AuBool(SUM_W, wsum); */
  25031. + AuBool(WARN_PERM, warn_perm);
  25032. + AuBool(VERBOSE, verbose);
  25033. +
  25034. +out:
  25035. + /* be sure to print "br:" last */
  25036. + if (!sysaufs_brs) {
  25037. + seq_puts(m, ",br:");
  25038. + au_show_brs(m, sb);
  25039. + }
  25040. + si_read_unlock(sb);
  25041. + return 0;
  25042. +
  25043. +#undef AuBool
  25044. +#undef AuStr
  25045. +#undef AuUInt
  25046. +}
  25047. +
  25048. +/* ---------------------------------------------------------------------- */
  25049. +
  25050. +/* sum mode which returns the summation for statfs(2) */
  25051. +
  25052. +static u64 au_add_till_max(u64 a, u64 b)
  25053. +{
  25054. + u64 old;
  25055. +
  25056. + old = a;
  25057. + a += b;
  25058. + if (old <= a)
  25059. + return a;
  25060. + return ULLONG_MAX;
  25061. +}
  25062. +
  25063. +static u64 au_mul_till_max(u64 a, long mul)
  25064. +{
  25065. + u64 old;
  25066. +
  25067. + old = a;
  25068. + a *= mul;
  25069. + if (old <= a)
  25070. + return a;
  25071. + return ULLONG_MAX;
  25072. +}
  25073. +
  25074. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  25075. +{
  25076. + int err;
  25077. + long bsize, factor;
  25078. + u64 blocks, bfree, bavail, files, ffree;
  25079. + aufs_bindex_t bend, bindex, i;
  25080. + unsigned char shared;
  25081. + struct path h_path;
  25082. + struct super_block *h_sb;
  25083. +
  25084. + err = 0;
  25085. + bsize = LONG_MAX;
  25086. + files = 0;
  25087. + ffree = 0;
  25088. + blocks = 0;
  25089. + bfree = 0;
  25090. + bavail = 0;
  25091. + bend = au_sbend(sb);
  25092. + for (bindex = 0; bindex <= bend; bindex++) {
  25093. + h_path.mnt = au_sbr_mnt(sb, bindex);
  25094. + h_sb = h_path.mnt->mnt_sb;
  25095. + shared = 0;
  25096. + for (i = 0; !shared && i < bindex; i++)
  25097. + shared = (au_sbr_sb(sb, i) == h_sb);
  25098. + if (shared)
  25099. + continue;
  25100. +
  25101. + /* sb->s_root for NFS is unreliable */
  25102. + h_path.dentry = h_path.mnt->mnt_root;
  25103. + err = vfs_statfs(&h_path, buf);
  25104. + if (unlikely(err))
  25105. + goto out;
  25106. +
  25107. + if (bsize > buf->f_bsize) {
  25108. + /*
  25109. + * we will reduce bsize, so we have to expand blocks
  25110. + * etc. to match them again
  25111. + */
  25112. + factor = (bsize / buf->f_bsize);
  25113. + blocks = au_mul_till_max(blocks, factor);
  25114. + bfree = au_mul_till_max(bfree, factor);
  25115. + bavail = au_mul_till_max(bavail, factor);
  25116. + bsize = buf->f_bsize;
  25117. + }
  25118. +
  25119. + factor = (buf->f_bsize / bsize);
  25120. + blocks = au_add_till_max(blocks,
  25121. + au_mul_till_max(buf->f_blocks, factor));
  25122. + bfree = au_add_till_max(bfree,
  25123. + au_mul_till_max(buf->f_bfree, factor));
  25124. + bavail = au_add_till_max(bavail,
  25125. + au_mul_till_max(buf->f_bavail, factor));
  25126. + files = au_add_till_max(files, buf->f_files);
  25127. + ffree = au_add_till_max(ffree, buf->f_ffree);
  25128. + }
  25129. +
  25130. + buf->f_bsize = bsize;
  25131. + buf->f_blocks = blocks;
  25132. + buf->f_bfree = bfree;
  25133. + buf->f_bavail = bavail;
  25134. + buf->f_files = files;
  25135. + buf->f_ffree = ffree;
  25136. + buf->f_frsize = 0;
  25137. +
  25138. +out:
  25139. + return err;
  25140. +}
  25141. +
  25142. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  25143. +{
  25144. + int err;
  25145. + struct path h_path;
  25146. + struct super_block *sb;
  25147. +
  25148. + /* lock free root dinfo */
  25149. + sb = dentry->d_sb;
  25150. + si_noflush_read_lock(sb);
  25151. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  25152. + /* sb->s_root for NFS is unreliable */
  25153. + h_path.mnt = au_sbr_mnt(sb, 0);
  25154. + h_path.dentry = h_path.mnt->mnt_root;
  25155. + err = vfs_statfs(&h_path, buf);
  25156. + } else
  25157. + err = au_statfs_sum(sb, buf);
  25158. + si_read_unlock(sb);
  25159. +
  25160. + if (!err) {
  25161. + buf->f_type = AUFS_SUPER_MAGIC;
  25162. + buf->f_namelen = AUFS_MAX_NAMELEN;
  25163. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  25164. + }
  25165. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  25166. +
  25167. + return err;
  25168. +}
  25169. +
  25170. +/* ---------------------------------------------------------------------- */
  25171. +
  25172. +static int aufs_sync_fs(struct super_block *sb, int wait)
  25173. +{
  25174. + int err, e;
  25175. + aufs_bindex_t bend, bindex;
  25176. + struct au_branch *br;
  25177. + struct super_block *h_sb;
  25178. +
  25179. + err = 0;
  25180. + si_noflush_read_lock(sb);
  25181. + bend = au_sbend(sb);
  25182. + for (bindex = 0; bindex <= bend; bindex++) {
  25183. + br = au_sbr(sb, bindex);
  25184. + if (!au_br_writable(br->br_perm))
  25185. + continue;
  25186. +
  25187. + h_sb = au_sbr_sb(sb, bindex);
  25188. + if (h_sb->s_op->sync_fs) {
  25189. + e = h_sb->s_op->sync_fs(h_sb, wait);
  25190. + if (unlikely(e && !err))
  25191. + err = e;
  25192. + /* go on even if an error happens */
  25193. + }
  25194. + }
  25195. + si_read_unlock(sb);
  25196. +
  25197. + return err;
  25198. +}
  25199. +
  25200. +/* ---------------------------------------------------------------------- */
  25201. +
  25202. +/* final actions when unmounting a file system */
  25203. +static void aufs_put_super(struct super_block *sb)
  25204. +{
  25205. + struct au_sbinfo *sbinfo;
  25206. +
  25207. + sbinfo = au_sbi(sb);
  25208. + if (!sbinfo)
  25209. + return;
  25210. +
  25211. + dbgaufs_si_fin(sbinfo);
  25212. + kobject_put(&sbinfo->si_kobj);
  25213. +}
  25214. +
  25215. +/* ---------------------------------------------------------------------- */
  25216. +
  25217. +void au_array_free(void *array)
  25218. +{
  25219. + if (array) {
  25220. + if (!is_vmalloc_addr(array))
  25221. + kfree(array);
  25222. + else
  25223. + vfree(array);
  25224. + }
  25225. +}
  25226. +
  25227. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
  25228. +{
  25229. + void *array;
  25230. + unsigned long long n, sz;
  25231. +
  25232. + array = NULL;
  25233. + n = 0;
  25234. + if (!*hint)
  25235. + goto out;
  25236. +
  25237. + if (*hint > ULLONG_MAX / sizeof(array)) {
  25238. + array = ERR_PTR(-EMFILE);
  25239. + pr_err("hint %llu\n", *hint);
  25240. + goto out;
  25241. + }
  25242. +
  25243. + sz = sizeof(array) * *hint;
  25244. + array = kzalloc(sz, GFP_NOFS);
  25245. + if (unlikely(!array))
  25246. + array = vzalloc(sz);
  25247. + if (unlikely(!array)) {
  25248. + array = ERR_PTR(-ENOMEM);
  25249. + goto out;
  25250. + }
  25251. +
  25252. + n = cb(array, *hint, arg);
  25253. + AuDebugOn(n > *hint);
  25254. +
  25255. +out:
  25256. + *hint = n;
  25257. + return array;
  25258. +}
  25259. +
  25260. +static unsigned long long au_iarray_cb(void *a,
  25261. + unsigned long long max __maybe_unused,
  25262. + void *arg)
  25263. +{
  25264. + unsigned long long n;
  25265. + struct inode **p, *inode;
  25266. + struct list_head *head;
  25267. +
  25268. + n = 0;
  25269. + p = a;
  25270. + head = arg;
  25271. + spin_lock(&inode_sb_list_lock);
  25272. + list_for_each_entry(inode, head, i_sb_list) {
  25273. + if (!is_bad_inode(inode)
  25274. + && au_ii(inode)->ii_bstart >= 0) {
  25275. + spin_lock(&inode->i_lock);
  25276. + if (atomic_read(&inode->i_count)) {
  25277. + au_igrab(inode);
  25278. + *p++ = inode;
  25279. + n++;
  25280. + AuDebugOn(n > max);
  25281. + }
  25282. + spin_unlock(&inode->i_lock);
  25283. + }
  25284. + }
  25285. + spin_unlock(&inode_sb_list_lock);
  25286. +
  25287. + return n;
  25288. +}
  25289. +
  25290. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  25291. +{
  25292. + *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
  25293. + return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
  25294. +}
  25295. +
  25296. +void au_iarray_free(struct inode **a, unsigned long long max)
  25297. +{
  25298. + unsigned long long ull;
  25299. +
  25300. + for (ull = 0; ull < max; ull++)
  25301. + iput(a[ull]);
  25302. + au_array_free(a);
  25303. +}
  25304. +
  25305. +/* ---------------------------------------------------------------------- */
  25306. +
  25307. +/*
  25308. + * refresh dentry and inode at remount time.
  25309. + */
  25310. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  25311. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  25312. + struct dentry *parent)
  25313. +{
  25314. + int err;
  25315. +
  25316. + di_write_lock_child(dentry);
  25317. + di_read_lock_parent(parent, AuLock_IR);
  25318. + err = au_refresh_dentry(dentry, parent);
  25319. + if (!err && dir_flags)
  25320. + au_hn_reset(dentry->d_inode, dir_flags);
  25321. + di_read_unlock(parent, AuLock_IR);
  25322. + di_write_unlock(dentry);
  25323. +
  25324. + return err;
  25325. +}
  25326. +
  25327. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  25328. + struct au_sbinfo *sbinfo,
  25329. + const unsigned int dir_flags)
  25330. +{
  25331. + int err;
  25332. + struct dentry *parent;
  25333. + struct inode *inode;
  25334. +
  25335. + err = 0;
  25336. + parent = dget_parent(dentry);
  25337. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  25338. + inode = dentry->d_inode;
  25339. + if (inode) {
  25340. + if (!S_ISDIR(inode->i_mode))
  25341. + err = au_do_refresh(dentry, /*dir_flags*/0,
  25342. + parent);
  25343. + else {
  25344. + err = au_do_refresh(dentry, dir_flags, parent);
  25345. + if (unlikely(err))
  25346. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  25347. + }
  25348. + } else
  25349. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  25350. + AuDbgDentry(dentry);
  25351. + }
  25352. + dput(parent);
  25353. +
  25354. + AuTraceErr(err);
  25355. + return err;
  25356. +}
  25357. +
  25358. +static int au_refresh_d(struct super_block *sb)
  25359. +{
  25360. + int err, i, j, ndentry, e;
  25361. + unsigned int sigen;
  25362. + struct au_dcsub_pages dpages;
  25363. + struct au_dpage *dpage;
  25364. + struct dentry **dentries, *d;
  25365. + struct au_sbinfo *sbinfo;
  25366. + struct dentry *root = sb->s_root;
  25367. + const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
  25368. +
  25369. + err = au_dpages_init(&dpages, GFP_NOFS);
  25370. + if (unlikely(err))
  25371. + goto out;
  25372. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  25373. + if (unlikely(err))
  25374. + goto out_dpages;
  25375. +
  25376. + sigen = au_sigen(sb);
  25377. + sbinfo = au_sbi(sb);
  25378. + for (i = 0; i < dpages.ndpage; i++) {
  25379. + dpage = dpages.dpages + i;
  25380. + dentries = dpage->dentries;
  25381. + ndentry = dpage->ndentry;
  25382. + for (j = 0; j < ndentry; j++) {
  25383. + d = dentries[j];
  25384. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
  25385. + if (unlikely(e && !err))
  25386. + err = e;
  25387. + /* go on even err */
  25388. + }
  25389. + }
  25390. +
  25391. +out_dpages:
  25392. + au_dpages_free(&dpages);
  25393. +out:
  25394. + return err;
  25395. +}
  25396. +
  25397. +static int au_refresh_i(struct super_block *sb)
  25398. +{
  25399. + int err, e;
  25400. + unsigned int sigen;
  25401. + unsigned long long max, ull;
  25402. + struct inode *inode, **array;
  25403. +
  25404. + array = au_iarray_alloc(sb, &max);
  25405. + err = PTR_ERR(array);
  25406. + if (IS_ERR(array))
  25407. + goto out;
  25408. +
  25409. + err = 0;
  25410. + sigen = au_sigen(sb);
  25411. + for (ull = 0; ull < max; ull++) {
  25412. + inode = array[ull];
  25413. + if (unlikely(!inode))
  25414. + break;
  25415. + if (au_iigen(inode, NULL) != sigen) {
  25416. + ii_write_lock_child(inode);
  25417. + e = au_refresh_hinode_self(inode);
  25418. + ii_write_unlock(inode);
  25419. + if (unlikely(e)) {
  25420. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  25421. + if (!err)
  25422. + err = e;
  25423. + /* go on even if err */
  25424. + }
  25425. + }
  25426. + }
  25427. +
  25428. + au_iarray_free(array, max);
  25429. +
  25430. +out:
  25431. + return err;
  25432. +}
  25433. +
  25434. +static void au_remount_refresh(struct super_block *sb)
  25435. +{
  25436. + int err, e;
  25437. + unsigned int udba;
  25438. + aufs_bindex_t bindex, bend;
  25439. + struct dentry *root;
  25440. + struct inode *inode;
  25441. + struct au_branch *br;
  25442. +
  25443. + au_sigen_inc(sb);
  25444. + au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
  25445. +
  25446. + root = sb->s_root;
  25447. + DiMustNoWaiters(root);
  25448. + inode = root->d_inode;
  25449. + IiMustNoWaiters(inode);
  25450. +
  25451. + udba = au_opt_udba(sb);
  25452. + bend = au_sbend(sb);
  25453. + for (bindex = 0; bindex <= bend; bindex++) {
  25454. + br = au_sbr(sb, bindex);
  25455. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  25456. + if (unlikely(err))
  25457. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  25458. + bindex, err);
  25459. + /* go on even if err */
  25460. + }
  25461. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  25462. +
  25463. + di_write_unlock(root);
  25464. + err = au_refresh_d(sb);
  25465. + e = au_refresh_i(sb);
  25466. + if (unlikely(e && !err))
  25467. + err = e;
  25468. + /* aufs_write_lock() calls ..._child() */
  25469. + di_write_lock_child(root);
  25470. +
  25471. + au_cpup_attr_all(inode, /*force*/1);
  25472. +
  25473. + if (unlikely(err))
  25474. + AuIOErr("refresh failed, ignored, %d\n", err);
  25475. +}
  25476. +
  25477. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  25478. +static int cvt_err(int err)
  25479. +{
  25480. + AuTraceErr(err);
  25481. +
  25482. + switch (err) {
  25483. + case -ENOENT:
  25484. + case -ENOTDIR:
  25485. + case -EEXIST:
  25486. + case -EIO:
  25487. + err = -EINVAL;
  25488. + }
  25489. + return err;
  25490. +}
  25491. +
  25492. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  25493. +{
  25494. + int err, do_dx;
  25495. + unsigned int mntflags;
  25496. + struct au_opts opts;
  25497. + struct dentry *root;
  25498. + struct inode *inode;
  25499. + struct au_sbinfo *sbinfo;
  25500. +
  25501. + err = 0;
  25502. + root = sb->s_root;
  25503. + if (!data || !*data) {
  25504. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  25505. + if (!err) {
  25506. + di_write_lock_child(root);
  25507. + err = au_opts_verify(sb, *flags, /*pending*/0);
  25508. + aufs_write_unlock(root);
  25509. + }
  25510. + goto out;
  25511. + }
  25512. +
  25513. + err = -ENOMEM;
  25514. + memset(&opts, 0, sizeof(opts));
  25515. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  25516. + if (unlikely(!opts.opt))
  25517. + goto out;
  25518. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  25519. + opts.flags = AuOpts_REMOUNT;
  25520. + opts.sb_flags = *flags;
  25521. +
  25522. + /* parse it before aufs lock */
  25523. + err = au_opts_parse(sb, data, &opts);
  25524. + if (unlikely(err))
  25525. + goto out_opts;
  25526. +
  25527. + sbinfo = au_sbi(sb);
  25528. + inode = root->d_inode;
  25529. + mutex_lock(&inode->i_mutex);
  25530. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  25531. + if (unlikely(err))
  25532. + goto out_mtx;
  25533. + di_write_lock_child(root);
  25534. +
  25535. + /* au_opts_remount() may return an error */
  25536. + err = au_opts_remount(sb, &opts);
  25537. + au_opts_free(&opts);
  25538. +
  25539. + if (au_ftest_opts(opts.flags, REFRESH))
  25540. + au_remount_refresh(sb);
  25541. +
  25542. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  25543. + mntflags = au_mntflags(sb);
  25544. + do_dx = !!au_opt_test(mntflags, DIO);
  25545. + au_dy_arefresh(do_dx);
  25546. + }
  25547. +
  25548. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  25549. + aufs_write_unlock(root);
  25550. +
  25551. +out_mtx:
  25552. + mutex_unlock(&inode->i_mutex);
  25553. +out_opts:
  25554. + free_page((unsigned long)opts.opt);
  25555. +out:
  25556. + err = cvt_err(err);
  25557. + AuTraceErr(err);
  25558. + return err;
  25559. +}
  25560. +
  25561. +static const struct super_operations aufs_sop = {
  25562. + .alloc_inode = aufs_alloc_inode,
  25563. + .destroy_inode = aufs_destroy_inode,
  25564. + /* always deleting, no clearing */
  25565. + .drop_inode = generic_delete_inode,
  25566. + .show_options = aufs_show_options,
  25567. + .statfs = aufs_statfs,
  25568. + .put_super = aufs_put_super,
  25569. + .sync_fs = aufs_sync_fs,
  25570. + .remount_fs = aufs_remount_fs
  25571. +};
  25572. +
  25573. +/* ---------------------------------------------------------------------- */
  25574. +
  25575. +static int alloc_root(struct super_block *sb)
  25576. +{
  25577. + int err;
  25578. + struct inode *inode;
  25579. + struct dentry *root;
  25580. +
  25581. + err = -ENOMEM;
  25582. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  25583. + err = PTR_ERR(inode);
  25584. + if (IS_ERR(inode))
  25585. + goto out;
  25586. +
  25587. + inode->i_op = &aufs_dir_iop;
  25588. + inode->i_fop = &aufs_dir_fop;
  25589. + inode->i_mode = S_IFDIR;
  25590. + set_nlink(inode, 2);
  25591. + unlock_new_inode(inode);
  25592. +
  25593. + root = d_make_root(inode);
  25594. + if (unlikely(!root))
  25595. + goto out;
  25596. + err = PTR_ERR(root);
  25597. + if (IS_ERR(root))
  25598. + goto out;
  25599. +
  25600. + err = au_di_init(root);
  25601. + if (!err) {
  25602. + sb->s_root = root;
  25603. + return 0; /* success */
  25604. + }
  25605. + dput(root);
  25606. +
  25607. +out:
  25608. + return err;
  25609. +}
  25610. +
  25611. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  25612. + int silent __maybe_unused)
  25613. +{
  25614. + int err;
  25615. + struct au_opts opts;
  25616. + struct dentry *root;
  25617. + struct inode *inode;
  25618. + char *arg = raw_data;
  25619. +
  25620. + if (unlikely(!arg || !*arg)) {
  25621. + err = -EINVAL;
  25622. + pr_err("no arg\n");
  25623. + goto out;
  25624. + }
  25625. +
  25626. + err = -ENOMEM;
  25627. + memset(&opts, 0, sizeof(opts));
  25628. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  25629. + if (unlikely(!opts.opt))
  25630. + goto out;
  25631. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  25632. + opts.sb_flags = sb->s_flags;
  25633. +
  25634. + err = au_si_alloc(sb);
  25635. + if (unlikely(err))
  25636. + goto out_opts;
  25637. +
  25638. + /* all timestamps always follow the ones on the branch */
  25639. + sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
  25640. + sb->s_op = &aufs_sop;
  25641. + sb->s_d_op = &aufs_dop;
  25642. + sb->s_magic = AUFS_SUPER_MAGIC;
  25643. + sb->s_maxbytes = 0;
  25644. + sb->s_stack_depth = 1;
  25645. + au_export_init(sb);
  25646. + /* au_xattr_init(sb); */
  25647. +
  25648. + err = alloc_root(sb);
  25649. + if (unlikely(err)) {
  25650. + si_write_unlock(sb);
  25651. + goto out_info;
  25652. + }
  25653. + root = sb->s_root;
  25654. + inode = root->d_inode;
  25655. +
  25656. + /*
  25657. + * actually we can parse options regardless aufs lock here.
  25658. + * but at remount time, parsing must be done before aufs lock.
  25659. + * so we follow the same rule.
  25660. + */
  25661. + ii_write_lock_parent(inode);
  25662. + aufs_write_unlock(root);
  25663. + err = au_opts_parse(sb, arg, &opts);
  25664. + if (unlikely(err))
  25665. + goto out_root;
  25666. +
  25667. + /* lock vfs_inode first, then aufs. */
  25668. + mutex_lock(&inode->i_mutex);
  25669. + aufs_write_lock(root);
  25670. + err = au_opts_mount(sb, &opts);
  25671. + au_opts_free(&opts);
  25672. + aufs_write_unlock(root);
  25673. + mutex_unlock(&inode->i_mutex);
  25674. + if (!err)
  25675. + goto out_opts; /* success */
  25676. +
  25677. +out_root:
  25678. + dput(root);
  25679. + sb->s_root = NULL;
  25680. +out_info:
  25681. + dbgaufs_si_fin(au_sbi(sb));
  25682. + kobject_put(&au_sbi(sb)->si_kobj);
  25683. + sb->s_fs_info = NULL;
  25684. +out_opts:
  25685. + free_page((unsigned long)opts.opt);
  25686. +out:
  25687. + AuTraceErr(err);
  25688. + err = cvt_err(err);
  25689. + AuTraceErr(err);
  25690. + return err;
  25691. +}
  25692. +
  25693. +/* ---------------------------------------------------------------------- */
  25694. +
  25695. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  25696. + const char *dev_name __maybe_unused,
  25697. + void *raw_data)
  25698. +{
  25699. + struct dentry *root;
  25700. + struct super_block *sb;
  25701. +
  25702. + /* all timestamps always follow the ones on the branch */
  25703. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  25704. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  25705. + if (IS_ERR(root))
  25706. + goto out;
  25707. +
  25708. + sb = root->d_sb;
  25709. + si_write_lock(sb, !AuLock_FLUSH);
  25710. + sysaufs_brs_add(sb, 0);
  25711. + si_write_unlock(sb);
  25712. + au_sbilist_add(sb);
  25713. +
  25714. +out:
  25715. + return root;
  25716. +}
  25717. +
  25718. +static void aufs_kill_sb(struct super_block *sb)
  25719. +{
  25720. + struct au_sbinfo *sbinfo;
  25721. +
  25722. + sbinfo = au_sbi(sb);
  25723. + if (sbinfo) {
  25724. + au_sbilist_del(sb);
  25725. + aufs_write_lock(sb->s_root);
  25726. + au_fhsm_fin(sb);
  25727. + if (sbinfo->si_wbr_create_ops->fin)
  25728. + sbinfo->si_wbr_create_ops->fin(sb);
  25729. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  25730. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  25731. + au_remount_refresh(sb);
  25732. + }
  25733. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  25734. + au_plink_put(sb, /*verbose*/1);
  25735. + au_xino_clr(sb);
  25736. + sbinfo->si_sb = NULL;
  25737. + aufs_write_unlock(sb->s_root);
  25738. + au_nwt_flush(&sbinfo->si_nowait);
  25739. + }
  25740. + kill_anon_super(sb);
  25741. +}
  25742. +
  25743. +struct file_system_type aufs_fs_type = {
  25744. + .name = AUFS_FSTYPE,
  25745. + /* a race between rename and others */
  25746. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  25747. + .mount = aufs_mount,
  25748. + .kill_sb = aufs_kill_sb,
  25749. + /* no need to __module_get() and module_put(). */
  25750. + .owner = THIS_MODULE,
  25751. +};
  25752. diff -Nur linux-4.0.4.orig/fs/aufs/super.h linux-4.0.4/fs/aufs/super.h
  25753. --- linux-4.0.4.orig/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
  25754. +++ linux-4.0.4/fs/aufs/super.h 2015-05-30 22:11:31.000000000 +0200
  25755. @@ -0,0 +1,626 @@
  25756. +/*
  25757. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25758. + */
  25759. +
  25760. +/*
  25761. + * super_block operations
  25762. + */
  25763. +
  25764. +#ifndef __AUFS_SUPER_H__
  25765. +#define __AUFS_SUPER_H__
  25766. +
  25767. +#ifdef __KERNEL__
  25768. +
  25769. +#include <linux/fs.h>
  25770. +#include <linux/kobject.h>
  25771. +#include "rwsem.h"
  25772. +#include "spl.h"
  25773. +#include "wkq.h"
  25774. +
  25775. +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
  25776. +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
  25777. + loff_t *);
  25778. +
  25779. +/* policies to select one among multiple writable branches */
  25780. +struct au_wbr_copyup_operations {
  25781. + int (*copyup)(struct dentry *dentry);
  25782. +};
  25783. +
  25784. +#define AuWbr_DIR 1 /* target is a dir */
  25785. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  25786. +
  25787. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  25788. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  25789. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  25790. +
  25791. +struct au_wbr_create_operations {
  25792. + int (*create)(struct dentry *dentry, unsigned int flags);
  25793. + int (*init)(struct super_block *sb);
  25794. + int (*fin)(struct super_block *sb);
  25795. +};
  25796. +
  25797. +struct au_wbr_mfs {
  25798. + struct mutex mfs_lock; /* protect this structure */
  25799. + unsigned long mfs_jiffy;
  25800. + unsigned long mfs_expire;
  25801. + aufs_bindex_t mfs_bindex;
  25802. +
  25803. + unsigned long long mfsrr_bytes;
  25804. + unsigned long long mfsrr_watermark;
  25805. +};
  25806. +
  25807. +struct pseudo_link {
  25808. + union {
  25809. + struct hlist_node hlist;
  25810. + struct rcu_head rcu;
  25811. + };
  25812. + struct inode *inode;
  25813. +};
  25814. +
  25815. +#define AuPlink_NHASH 100
  25816. +static inline int au_plink_hash(ino_t ino)
  25817. +{
  25818. + return ino % AuPlink_NHASH;
  25819. +}
  25820. +
  25821. +/* File-based Hierarchical Storage Management */
  25822. +struct au_fhsm {
  25823. +#ifdef CONFIG_AUFS_FHSM
  25824. + /* allow only one process who can receive the notification */
  25825. + spinlock_t fhsm_spin;
  25826. + pid_t fhsm_pid;
  25827. + wait_queue_head_t fhsm_wqh;
  25828. + atomic_t fhsm_readable;
  25829. +
  25830. + /* these are protected by si_rwsem */
  25831. + unsigned long fhsm_expire;
  25832. + aufs_bindex_t fhsm_bottom;
  25833. +#endif
  25834. +};
  25835. +
  25836. +struct au_branch;
  25837. +struct au_sbinfo {
  25838. + /* nowait tasks in the system-wide workqueue */
  25839. + struct au_nowait_tasks si_nowait;
  25840. +
  25841. + /*
  25842. + * tried sb->s_umount, but failed due to the dependecy between i_mutex.
  25843. + * rwsem for au_sbinfo is necessary.
  25844. + */
  25845. + struct au_rwsem si_rwsem;
  25846. +
  25847. + /* prevent recursive locking in deleting inode */
  25848. + struct {
  25849. + unsigned long *bitmap;
  25850. + spinlock_t tree_lock;
  25851. + struct radix_tree_root tree;
  25852. + } au_si_pid;
  25853. +
  25854. + /*
  25855. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  25856. + * remount.
  25857. + */
  25858. + atomic_long_t si_ninodes, si_nfiles;
  25859. +
  25860. + /* branch management */
  25861. + unsigned int si_generation;
  25862. +
  25863. + /* see AuSi_ flags */
  25864. + unsigned char au_si_status;
  25865. +
  25866. + aufs_bindex_t si_bend;
  25867. +
  25868. + /* dirty trick to keep br_id plus */
  25869. + unsigned int si_last_br_id :
  25870. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  25871. + struct au_branch **si_branch;
  25872. +
  25873. + /* policy to select a writable branch */
  25874. + unsigned char si_wbr_copyup;
  25875. + unsigned char si_wbr_create;
  25876. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  25877. + struct au_wbr_create_operations *si_wbr_create_ops;
  25878. +
  25879. + /* round robin */
  25880. + atomic_t si_wbr_rr_next;
  25881. +
  25882. + /* most free space */
  25883. + struct au_wbr_mfs si_wbr_mfs;
  25884. +
  25885. + /* File-based Hierarchical Storage Management */
  25886. + struct au_fhsm si_fhsm;
  25887. +
  25888. + /* mount flags */
  25889. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  25890. + unsigned int si_mntflags;
  25891. +
  25892. + /* external inode number (bitmap and translation table) */
  25893. + au_readf_t si_xread;
  25894. + au_writef_t si_xwrite;
  25895. + struct file *si_xib;
  25896. + struct mutex si_xib_mtx; /* protect xib members */
  25897. + unsigned long *si_xib_buf;
  25898. + unsigned long si_xib_last_pindex;
  25899. + int si_xib_next_bit;
  25900. + aufs_bindex_t si_xino_brid;
  25901. + unsigned long si_xino_jiffy;
  25902. + unsigned long si_xino_expire;
  25903. + /* reserved for future use */
  25904. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  25905. +
  25906. +#ifdef CONFIG_AUFS_EXPORT
  25907. + /* i_generation */
  25908. + struct file *si_xigen;
  25909. + atomic_t si_xigen_next;
  25910. +#endif
  25911. +
  25912. + /* dirty trick to suppoer atomic_open */
  25913. + struct au_sphlhead si_aopen;
  25914. +
  25915. + /* vdir parameters */
  25916. + unsigned long si_rdcache; /* max cache time in jiffies */
  25917. + unsigned int si_rdblk; /* deblk size */
  25918. + unsigned int si_rdhash; /* hash size */
  25919. +
  25920. + /*
  25921. + * If the number of whiteouts are larger than si_dirwh, leave all of
  25922. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  25923. + * future fsck.aufs or kernel thread will remove them later.
  25924. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  25925. + */
  25926. + unsigned int si_dirwh;
  25927. +
  25928. + /* pseudo_link list */
  25929. + struct au_sphlhead si_plink[AuPlink_NHASH];
  25930. + wait_queue_head_t si_plink_wq;
  25931. + spinlock_t si_plink_maint_lock;
  25932. + pid_t si_plink_maint_pid;
  25933. +
  25934. + /* file list */
  25935. + struct au_sphlhead si_files;
  25936. +
  25937. + /*
  25938. + * sysfs and lifetime management.
  25939. + * this is not a small structure and it may be a waste of memory in case
  25940. + * of sysfs is disabled, particulary when many aufs-es are mounted.
  25941. + * but using sysfs is majority.
  25942. + */
  25943. + struct kobject si_kobj;
  25944. +#ifdef CONFIG_DEBUG_FS
  25945. + struct dentry *si_dbgaufs;
  25946. + struct dentry *si_dbgaufs_plink;
  25947. + struct dentry *si_dbgaufs_xib;
  25948. +#ifdef CONFIG_AUFS_EXPORT
  25949. + struct dentry *si_dbgaufs_xigen;
  25950. +#endif
  25951. +#endif
  25952. +
  25953. +#ifdef CONFIG_AUFS_SBILIST
  25954. + struct list_head si_list;
  25955. +#endif
  25956. +
  25957. + /* dirty, necessary for unmounting, sysfs and sysrq */
  25958. + struct super_block *si_sb;
  25959. +};
  25960. +
  25961. +/* sbinfo status flags */
  25962. +/*
  25963. + * set true when refresh_dirs() failed at remount time.
  25964. + * then try refreshing dirs at access time again.
  25965. + * if it is false, refreshing dirs at access time is unnecesary
  25966. + */
  25967. +#define AuSi_FAILED_REFRESH_DIR 1
  25968. +
  25969. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  25970. +
  25971. +#ifndef CONFIG_AUFS_FHSM
  25972. +#undef AuSi_FHSM
  25973. +#define AuSi_FHSM 0
  25974. +#endif
  25975. +
  25976. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  25977. + unsigned int flag)
  25978. +{
  25979. + AuRwMustAnyLock(&sbi->si_rwsem);
  25980. + return sbi->au_si_status & flag;
  25981. +}
  25982. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  25983. +#define au_fset_si(sbinfo, name) do { \
  25984. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  25985. + (sbinfo)->au_si_status |= AuSi_##name; \
  25986. +} while (0)
  25987. +#define au_fclr_si(sbinfo, name) do { \
  25988. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  25989. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  25990. +} while (0)
  25991. +
  25992. +/* ---------------------------------------------------------------------- */
  25993. +
  25994. +/* policy to select one among writable branches */
  25995. +#define AuWbrCopyup(sbinfo, ...) \
  25996. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  25997. +#define AuWbrCreate(sbinfo, ...) \
  25998. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  25999. +
  26000. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  26001. +#define AuLock_DW 1 /* write-lock dentry */
  26002. +#define AuLock_IR (1 << 1) /* read-lock inode */
  26003. +#define AuLock_IW (1 << 2) /* write-lock inode */
  26004. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  26005. +#define AuLock_DIR (1 << 4) /* target is a dir */
  26006. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  26007. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  26008. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  26009. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  26010. +#define au_fset_lock(flags, name) \
  26011. + do { (flags) |= AuLock_##name; } while (0)
  26012. +#define au_fclr_lock(flags, name) \
  26013. + do { (flags) &= ~AuLock_##name; } while (0)
  26014. +
  26015. +/* ---------------------------------------------------------------------- */
  26016. +
  26017. +/* super.c */
  26018. +extern struct file_system_type aufs_fs_type;
  26019. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  26020. +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
  26021. + void *arg);
  26022. +void au_array_free(void *array);
  26023. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
  26024. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  26025. +void au_iarray_free(struct inode **a, unsigned long long max);
  26026. +
  26027. +/* sbinfo.c */
  26028. +void au_si_free(struct kobject *kobj);
  26029. +int au_si_alloc(struct super_block *sb);
  26030. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
  26031. +
  26032. +unsigned int au_sigen_inc(struct super_block *sb);
  26033. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  26034. +
  26035. +int si_read_lock(struct super_block *sb, int flags);
  26036. +int si_write_lock(struct super_block *sb, int flags);
  26037. +int aufs_read_lock(struct dentry *dentry, int flags);
  26038. +void aufs_read_unlock(struct dentry *dentry, int flags);
  26039. +void aufs_write_lock(struct dentry *dentry);
  26040. +void aufs_write_unlock(struct dentry *dentry);
  26041. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  26042. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  26043. +
  26044. +int si_pid_test_slow(struct super_block *sb);
  26045. +void si_pid_set_slow(struct super_block *sb);
  26046. +void si_pid_clr_slow(struct super_block *sb);
  26047. +
  26048. +/* wbr_policy.c */
  26049. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  26050. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  26051. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  26052. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  26053. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
  26054. +
  26055. +/* mvdown.c */
  26056. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  26057. +
  26058. +#ifdef CONFIG_AUFS_FHSM
  26059. +/* fhsm.c */
  26060. +
  26061. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  26062. +{
  26063. + pid_t pid;
  26064. +
  26065. + spin_lock(&fhsm->fhsm_spin);
  26066. + pid = fhsm->fhsm_pid;
  26067. + spin_unlock(&fhsm->fhsm_spin);
  26068. +
  26069. + return pid;
  26070. +}
  26071. +
  26072. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  26073. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  26074. +int au_fhsm_fd(struct super_block *sb, int oflags);
  26075. +int au_fhsm_br_alloc(struct au_branch *br);
  26076. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  26077. +void au_fhsm_fin(struct super_block *sb);
  26078. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  26079. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  26080. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  26081. +#else
  26082. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  26083. + int force)
  26084. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  26085. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  26086. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  26087. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  26088. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  26089. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  26090. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  26091. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  26092. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  26093. +#endif
  26094. +
  26095. +/* ---------------------------------------------------------------------- */
  26096. +
  26097. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  26098. +{
  26099. + return sb->s_fs_info;
  26100. +}
  26101. +
  26102. +/* ---------------------------------------------------------------------- */
  26103. +
  26104. +#ifdef CONFIG_AUFS_EXPORT
  26105. +int au_test_nfsd(void);
  26106. +void au_export_init(struct super_block *sb);
  26107. +void au_xigen_inc(struct inode *inode);
  26108. +int au_xigen_new(struct inode *inode);
  26109. +int au_xigen_set(struct super_block *sb, struct file *base);
  26110. +void au_xigen_clr(struct super_block *sb);
  26111. +
  26112. +static inline int au_busy_or_stale(void)
  26113. +{
  26114. + if (!au_test_nfsd())
  26115. + return -EBUSY;
  26116. + return -ESTALE;
  26117. +}
  26118. +#else
  26119. +AuStubInt0(au_test_nfsd, void)
  26120. +AuStubVoid(au_export_init, struct super_block *sb)
  26121. +AuStubVoid(au_xigen_inc, struct inode *inode)
  26122. +AuStubInt0(au_xigen_new, struct inode *inode)
  26123. +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
  26124. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  26125. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  26126. +#endif /* CONFIG_AUFS_EXPORT */
  26127. +
  26128. +/* ---------------------------------------------------------------------- */
  26129. +
  26130. +#ifdef CONFIG_AUFS_SBILIST
  26131. +/* module.c */
  26132. +extern struct au_splhead au_sbilist;
  26133. +
  26134. +static inline void au_sbilist_init(void)
  26135. +{
  26136. + au_spl_init(&au_sbilist);
  26137. +}
  26138. +
  26139. +static inline void au_sbilist_add(struct super_block *sb)
  26140. +{
  26141. + au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
  26142. +}
  26143. +
  26144. +static inline void au_sbilist_del(struct super_block *sb)
  26145. +{
  26146. + au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
  26147. +}
  26148. +
  26149. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  26150. +static inline void au_sbilist_lock(void)
  26151. +{
  26152. + spin_lock(&au_sbilist.spin);
  26153. +}
  26154. +
  26155. +static inline void au_sbilist_unlock(void)
  26156. +{
  26157. + spin_unlock(&au_sbilist.spin);
  26158. +}
  26159. +#define AuGFP_SBILIST GFP_ATOMIC
  26160. +#else
  26161. +AuStubVoid(au_sbilist_lock, void)
  26162. +AuStubVoid(au_sbilist_unlock, void)
  26163. +#define AuGFP_SBILIST GFP_NOFS
  26164. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  26165. +#else
  26166. +AuStubVoid(au_sbilist_init, void)
  26167. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  26168. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  26169. +AuStubVoid(au_sbilist_lock, void)
  26170. +AuStubVoid(au_sbilist_unlock, void)
  26171. +#define AuGFP_SBILIST GFP_NOFS
  26172. +#endif
  26173. +
  26174. +/* ---------------------------------------------------------------------- */
  26175. +
  26176. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  26177. +{
  26178. + /*
  26179. + * This function is a dynamic '__init' function actually,
  26180. + * so the tiny check for si_rwsem is unnecessary.
  26181. + */
  26182. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  26183. +#ifdef CONFIG_DEBUG_FS
  26184. + sbinfo->si_dbgaufs = NULL;
  26185. + sbinfo->si_dbgaufs_plink = NULL;
  26186. + sbinfo->si_dbgaufs_xib = NULL;
  26187. +#ifdef CONFIG_AUFS_EXPORT
  26188. + sbinfo->si_dbgaufs_xigen = NULL;
  26189. +#endif
  26190. +#endif
  26191. +}
  26192. +
  26193. +/* ---------------------------------------------------------------------- */
  26194. +
  26195. +static inline pid_t si_pid_bit(void)
  26196. +{
  26197. + /* the origin of pid is 1, but the bitmap's is 0 */
  26198. + return current->pid - 1;
  26199. +}
  26200. +
  26201. +static inline int si_pid_test(struct super_block *sb)
  26202. +{
  26203. + pid_t bit;
  26204. +
  26205. + bit = si_pid_bit();
  26206. + if (bit < PID_MAX_DEFAULT)
  26207. + return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  26208. + return si_pid_test_slow(sb);
  26209. +}
  26210. +
  26211. +static inline void si_pid_set(struct super_block *sb)
  26212. +{
  26213. + pid_t bit;
  26214. +
  26215. + bit = si_pid_bit();
  26216. + if (bit < PID_MAX_DEFAULT) {
  26217. + AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
  26218. + set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  26219. + /* smp_mb(); */
  26220. + } else
  26221. + si_pid_set_slow(sb);
  26222. +}
  26223. +
  26224. +static inline void si_pid_clr(struct super_block *sb)
  26225. +{
  26226. + pid_t bit;
  26227. +
  26228. + bit = si_pid_bit();
  26229. + if (bit < PID_MAX_DEFAULT) {
  26230. + AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
  26231. + clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  26232. + /* smp_mb(); */
  26233. + } else
  26234. + si_pid_clr_slow(sb);
  26235. +}
  26236. +
  26237. +/* ---------------------------------------------------------------------- */
  26238. +
  26239. +/* lock superblock. mainly for entry point functions */
  26240. +/*
  26241. + * __si_read_lock, __si_write_lock,
  26242. + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
  26243. + */
  26244. +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
  26245. +
  26246. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  26247. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  26248. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  26249. +
  26250. +static inline void si_noflush_read_lock(struct super_block *sb)
  26251. +{
  26252. + __si_read_lock(sb);
  26253. + si_pid_set(sb);
  26254. +}
  26255. +
  26256. +static inline int si_noflush_read_trylock(struct super_block *sb)
  26257. +{
  26258. + int locked;
  26259. +
  26260. + locked = __si_read_trylock(sb);
  26261. + if (locked)
  26262. + si_pid_set(sb);
  26263. + return locked;
  26264. +}
  26265. +
  26266. +static inline void si_noflush_write_lock(struct super_block *sb)
  26267. +{
  26268. + __si_write_lock(sb);
  26269. + si_pid_set(sb);
  26270. +}
  26271. +
  26272. +static inline int si_noflush_write_trylock(struct super_block *sb)
  26273. +{
  26274. + int locked;
  26275. +
  26276. + locked = __si_write_trylock(sb);
  26277. + if (locked)
  26278. + si_pid_set(sb);
  26279. + return locked;
  26280. +}
  26281. +
  26282. +#if 0 /* reserved */
  26283. +static inline int si_read_trylock(struct super_block *sb, int flags)
  26284. +{
  26285. + if (au_ftest_lock(flags, FLUSH))
  26286. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  26287. + return si_noflush_read_trylock(sb);
  26288. +}
  26289. +#endif
  26290. +
  26291. +static inline void si_read_unlock(struct super_block *sb)
  26292. +{
  26293. + si_pid_clr(sb);
  26294. + __si_read_unlock(sb);
  26295. +}
  26296. +
  26297. +#if 0 /* reserved */
  26298. +static inline int si_write_trylock(struct super_block *sb, int flags)
  26299. +{
  26300. + if (au_ftest_lock(flags, FLUSH))
  26301. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  26302. + return si_noflush_write_trylock(sb);
  26303. +}
  26304. +#endif
  26305. +
  26306. +static inline void si_write_unlock(struct super_block *sb)
  26307. +{
  26308. + si_pid_clr(sb);
  26309. + __si_write_unlock(sb);
  26310. +}
  26311. +
  26312. +#if 0 /* reserved */
  26313. +static inline void si_downgrade_lock(struct super_block *sb)
  26314. +{
  26315. + __si_downgrade_lock(sb);
  26316. +}
  26317. +#endif
  26318. +
  26319. +/* ---------------------------------------------------------------------- */
  26320. +
  26321. +static inline aufs_bindex_t au_sbend(struct super_block *sb)
  26322. +{
  26323. + SiMustAnyLock(sb);
  26324. + return au_sbi(sb)->si_bend;
  26325. +}
  26326. +
  26327. +static inline unsigned int au_mntflags(struct super_block *sb)
  26328. +{
  26329. + SiMustAnyLock(sb);
  26330. + return au_sbi(sb)->si_mntflags;
  26331. +}
  26332. +
  26333. +static inline unsigned int au_sigen(struct super_block *sb)
  26334. +{
  26335. + SiMustAnyLock(sb);
  26336. + return au_sbi(sb)->si_generation;
  26337. +}
  26338. +
  26339. +static inline void au_ninodes_inc(struct super_block *sb)
  26340. +{
  26341. + atomic_long_inc(&au_sbi(sb)->si_ninodes);
  26342. +}
  26343. +
  26344. +static inline void au_ninodes_dec(struct super_block *sb)
  26345. +{
  26346. + AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
  26347. + atomic_long_dec(&au_sbi(sb)->si_ninodes);
  26348. +}
  26349. +
  26350. +static inline void au_nfiles_inc(struct super_block *sb)
  26351. +{
  26352. + atomic_long_inc(&au_sbi(sb)->si_nfiles);
  26353. +}
  26354. +
  26355. +static inline void au_nfiles_dec(struct super_block *sb)
  26356. +{
  26357. + AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
  26358. + atomic_long_dec(&au_sbi(sb)->si_nfiles);
  26359. +}
  26360. +
  26361. +static inline struct au_branch *au_sbr(struct super_block *sb,
  26362. + aufs_bindex_t bindex)
  26363. +{
  26364. + SiMustAnyLock(sb);
  26365. + return au_sbi(sb)->si_branch[0 + bindex];
  26366. +}
  26367. +
  26368. +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
  26369. +{
  26370. + SiMustWriteLock(sb);
  26371. + au_sbi(sb)->si_xino_brid = brid;
  26372. +}
  26373. +
  26374. +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
  26375. +{
  26376. + SiMustAnyLock(sb);
  26377. + return au_sbi(sb)->si_xino_brid;
  26378. +}
  26379. +
  26380. +#endif /* __KERNEL__ */
  26381. +#endif /* __AUFS_SUPER_H__ */
  26382. diff -Nur linux-4.0.4.orig/fs/aufs/sysaufs.c linux-4.0.4/fs/aufs/sysaufs.c
  26383. --- linux-4.0.4.orig/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
  26384. +++ linux-4.0.4/fs/aufs/sysaufs.c 2015-05-30 22:11:30.000000000 +0200
  26385. @@ -0,0 +1,91 @@
  26386. +/*
  26387. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26388. + */
  26389. +
  26390. +/*
  26391. + * sysfs interface and lifetime management
  26392. + * they are necessary regardless sysfs is disabled.
  26393. + */
  26394. +
  26395. +#include <linux/random.h>
  26396. +#include "aufs.h"
  26397. +
  26398. +unsigned long sysaufs_si_mask;
  26399. +struct kset *sysaufs_kset;
  26400. +
  26401. +#define AuSiAttr(_name) { \
  26402. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  26403. + .show = sysaufs_si_##_name, \
  26404. +}
  26405. +
  26406. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  26407. +struct attribute *sysaufs_si_attrs[] = {
  26408. + &sysaufs_si_attr_xi_path.attr,
  26409. + NULL,
  26410. +};
  26411. +
  26412. +static const struct sysfs_ops au_sbi_ops = {
  26413. + .show = sysaufs_si_show
  26414. +};
  26415. +
  26416. +static struct kobj_type au_sbi_ktype = {
  26417. + .release = au_si_free,
  26418. + .sysfs_ops = &au_sbi_ops,
  26419. + .default_attrs = sysaufs_si_attrs
  26420. +};
  26421. +
  26422. +/* ---------------------------------------------------------------------- */
  26423. +
  26424. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  26425. +{
  26426. + int err;
  26427. +
  26428. + sbinfo->si_kobj.kset = sysaufs_kset;
  26429. + /* cf. sysaufs_name() */
  26430. + err = kobject_init_and_add
  26431. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  26432. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  26433. +
  26434. + dbgaufs_si_null(sbinfo);
  26435. + if (!err) {
  26436. + err = dbgaufs_si_init(sbinfo);
  26437. + if (unlikely(err))
  26438. + kobject_put(&sbinfo->si_kobj);
  26439. + }
  26440. + return err;
  26441. +}
  26442. +
  26443. +void sysaufs_fin(void)
  26444. +{
  26445. + dbgaufs_fin();
  26446. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  26447. + kset_unregister(sysaufs_kset);
  26448. +}
  26449. +
  26450. +int __init sysaufs_init(void)
  26451. +{
  26452. + int err;
  26453. +
  26454. + do {
  26455. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  26456. + } while (!sysaufs_si_mask);
  26457. +
  26458. + err = -EINVAL;
  26459. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  26460. + if (unlikely(!sysaufs_kset))
  26461. + goto out;
  26462. + err = PTR_ERR(sysaufs_kset);
  26463. + if (IS_ERR(sysaufs_kset))
  26464. + goto out;
  26465. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  26466. + if (unlikely(err)) {
  26467. + kset_unregister(sysaufs_kset);
  26468. + goto out;
  26469. + }
  26470. +
  26471. + err = dbgaufs_init();
  26472. + if (unlikely(err))
  26473. + sysaufs_fin();
  26474. +out:
  26475. + return err;
  26476. +}
  26477. diff -Nur linux-4.0.4.orig/fs/aufs/sysaufs.h linux-4.0.4/fs/aufs/sysaufs.h
  26478. --- linux-4.0.4.orig/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
  26479. +++ linux-4.0.4/fs/aufs/sysaufs.h 2015-05-30 22:11:30.000000000 +0200
  26480. @@ -0,0 +1,88 @@
  26481. +/*
  26482. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26483. + */
  26484. +
  26485. +/*
  26486. + * sysfs interface and mount lifetime management
  26487. + */
  26488. +
  26489. +#ifndef __SYSAUFS_H__
  26490. +#define __SYSAUFS_H__
  26491. +
  26492. +#ifdef __KERNEL__
  26493. +
  26494. +#include <linux/sysfs.h>
  26495. +#include "module.h"
  26496. +
  26497. +struct super_block;
  26498. +struct au_sbinfo;
  26499. +
  26500. +struct sysaufs_si_attr {
  26501. + struct attribute attr;
  26502. + int (*show)(struct seq_file *seq, struct super_block *sb);
  26503. +};
  26504. +
  26505. +/* ---------------------------------------------------------------------- */
  26506. +
  26507. +/* sysaufs.c */
  26508. +extern unsigned long sysaufs_si_mask;
  26509. +extern struct kset *sysaufs_kset;
  26510. +extern struct attribute *sysaufs_si_attrs[];
  26511. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  26512. +int __init sysaufs_init(void);
  26513. +void sysaufs_fin(void);
  26514. +
  26515. +/* ---------------------------------------------------------------------- */
  26516. +
  26517. +/* some people doesn't like to show a pointer in kernel */
  26518. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  26519. +{
  26520. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  26521. +}
  26522. +
  26523. +#define SysaufsSiNamePrefix "si_"
  26524. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  26525. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  26526. +{
  26527. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  26528. + sysaufs_si_id(sbinfo));
  26529. +}
  26530. +
  26531. +struct au_branch;
  26532. +#ifdef CONFIG_SYSFS
  26533. +/* sysfs.c */
  26534. +extern struct attribute_group *sysaufs_attr_group;
  26535. +
  26536. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  26537. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  26538. + char *buf);
  26539. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  26540. +#ifdef CONFIG_COMPAT
  26541. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  26542. +#endif
  26543. +
  26544. +void sysaufs_br_init(struct au_branch *br);
  26545. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  26546. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  26547. +
  26548. +#define sysaufs_brs_init() do {} while (0)
  26549. +
  26550. +#else
  26551. +#define sysaufs_attr_group NULL
  26552. +
  26553. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  26554. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  26555. + struct attribute *attr, char *buf)
  26556. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  26557. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  26558. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  26559. +
  26560. +static inline void sysaufs_brs_init(void)
  26561. +{
  26562. + sysaufs_brs = 0;
  26563. +}
  26564. +
  26565. +#endif /* CONFIG_SYSFS */
  26566. +
  26567. +#endif /* __KERNEL__ */
  26568. +#endif /* __SYSAUFS_H__ */
  26569. diff -Nur linux-4.0.4.orig/fs/aufs/sysfs.c linux-4.0.4/fs/aufs/sysfs.c
  26570. --- linux-4.0.4.orig/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
  26571. +++ linux-4.0.4/fs/aufs/sysfs.c 2015-05-30 22:11:30.000000000 +0200
  26572. @@ -0,0 +1,336 @@
  26573. +/*
  26574. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26575. + */
  26576. +
  26577. +/*
  26578. + * sysfs interface
  26579. + */
  26580. +
  26581. +#include <linux/compat.h>
  26582. +#include <linux/seq_file.h>
  26583. +#include "aufs.h"
  26584. +
  26585. +static struct attribute *au_attr[] = {
  26586. + NULL, /* need to NULL terminate the list of attributes */
  26587. +};
  26588. +
  26589. +static struct attribute_group sysaufs_attr_group_body = {
  26590. + .attrs = au_attr
  26591. +};
  26592. +
  26593. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  26594. +
  26595. +/* ---------------------------------------------------------------------- */
  26596. +
  26597. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  26598. +{
  26599. + int err;
  26600. +
  26601. + SiMustAnyLock(sb);
  26602. +
  26603. + err = 0;
  26604. + if (au_opt_test(au_mntflags(sb), XINO)) {
  26605. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  26606. + seq_putc(seq, '\n');
  26607. + }
  26608. + return err;
  26609. +}
  26610. +
  26611. +/*
  26612. + * the lifetime of branch is independent from the entry under sysfs.
  26613. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  26614. + * unlinked.
  26615. + */
  26616. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  26617. + aufs_bindex_t bindex, int idx)
  26618. +{
  26619. + int err;
  26620. + struct path path;
  26621. + struct dentry *root;
  26622. + struct au_branch *br;
  26623. + au_br_perm_str_t perm;
  26624. +
  26625. + AuDbg("b%d\n", bindex);
  26626. +
  26627. + err = 0;
  26628. + root = sb->s_root;
  26629. + di_read_lock_parent(root, !AuLock_IR);
  26630. + br = au_sbr(sb, bindex);
  26631. +
  26632. + switch (idx) {
  26633. + case AuBrSysfs_BR:
  26634. + path.mnt = au_br_mnt(br);
  26635. + path.dentry = au_h_dptr(root, bindex);
  26636. + au_seq_path(seq, &path);
  26637. + au_optstr_br_perm(&perm, br->br_perm);
  26638. + err = seq_printf(seq, "=%s\n", perm.a);
  26639. + break;
  26640. + case AuBrSysfs_BRID:
  26641. + err = seq_printf(seq, "%d\n", br->br_id);
  26642. + break;
  26643. + }
  26644. + di_read_unlock(root, !AuLock_IR);
  26645. + if (err == -1)
  26646. + err = -E2BIG;
  26647. +
  26648. + return err;
  26649. +}
  26650. +
  26651. +/* ---------------------------------------------------------------------- */
  26652. +
  26653. +static struct seq_file *au_seq(char *p, ssize_t len)
  26654. +{
  26655. + struct seq_file *seq;
  26656. +
  26657. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  26658. + if (seq) {
  26659. + /* mutex_init(&seq.lock); */
  26660. + seq->buf = p;
  26661. + seq->size = len;
  26662. + return seq; /* success */
  26663. + }
  26664. +
  26665. + seq = ERR_PTR(-ENOMEM);
  26666. + return seq;
  26667. +}
  26668. +
  26669. +#define SysaufsBr_PREFIX "br"
  26670. +#define SysaufsBrid_PREFIX "brid"
  26671. +
  26672. +/* todo: file size may exceed PAGE_SIZE */
  26673. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  26674. + char *buf)
  26675. +{
  26676. + ssize_t err;
  26677. + int idx;
  26678. + long l;
  26679. + aufs_bindex_t bend;
  26680. + struct au_sbinfo *sbinfo;
  26681. + struct super_block *sb;
  26682. + struct seq_file *seq;
  26683. + char *name;
  26684. + struct attribute **cattr;
  26685. +
  26686. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  26687. + sb = sbinfo->si_sb;
  26688. +
  26689. + /*
  26690. + * prevent a race condition between sysfs and aufs.
  26691. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  26692. + * prohibits maintaining the sysfs entries.
  26693. + * hew we acquire read lock after sysfs_get_active_two().
  26694. + * on the other hand, the remount process may maintain the sysfs/aufs
  26695. + * entries after acquiring write lock.
  26696. + * it can cause a deadlock.
  26697. + * simply we gave up processing read here.
  26698. + */
  26699. + err = -EBUSY;
  26700. + if (unlikely(!si_noflush_read_trylock(sb)))
  26701. + goto out;
  26702. +
  26703. + seq = au_seq(buf, PAGE_SIZE);
  26704. + err = PTR_ERR(seq);
  26705. + if (IS_ERR(seq))
  26706. + goto out_unlock;
  26707. +
  26708. + name = (void *)attr->name;
  26709. + cattr = sysaufs_si_attrs;
  26710. + while (*cattr) {
  26711. + if (!strcmp(name, (*cattr)->name)) {
  26712. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  26713. + ->show(seq, sb);
  26714. + goto out_seq;
  26715. + }
  26716. + cattr++;
  26717. + }
  26718. +
  26719. + if (!strncmp(name, SysaufsBrid_PREFIX,
  26720. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  26721. + idx = AuBrSysfs_BRID;
  26722. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  26723. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  26724. + sizeof(SysaufsBr_PREFIX) - 1)) {
  26725. + idx = AuBrSysfs_BR;
  26726. + name += sizeof(SysaufsBr_PREFIX) - 1;
  26727. + } else
  26728. + BUG();
  26729. +
  26730. + err = kstrtol(name, 10, &l);
  26731. + if (!err) {
  26732. + bend = au_sbend(sb);
  26733. + if (l <= bend)
  26734. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  26735. + else
  26736. + err = -ENOENT;
  26737. + }
  26738. +
  26739. +out_seq:
  26740. + if (!err) {
  26741. + err = seq->count;
  26742. + /* sysfs limit */
  26743. + if (unlikely(err == PAGE_SIZE))
  26744. + err = -EFBIG;
  26745. + }
  26746. + kfree(seq);
  26747. +out_unlock:
  26748. + si_read_unlock(sb);
  26749. +out:
  26750. + return err;
  26751. +}
  26752. +
  26753. +/* ---------------------------------------------------------------------- */
  26754. +
  26755. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  26756. +{
  26757. + int err;
  26758. + int16_t brid;
  26759. + aufs_bindex_t bindex, bend;
  26760. + size_t sz;
  26761. + char *buf;
  26762. + struct seq_file *seq;
  26763. + struct au_branch *br;
  26764. +
  26765. + si_read_lock(sb, AuLock_FLUSH);
  26766. + bend = au_sbend(sb);
  26767. + err = bend + 1;
  26768. + if (!arg)
  26769. + goto out;
  26770. +
  26771. + err = -ENOMEM;
  26772. + buf = (void *)__get_free_page(GFP_NOFS);
  26773. + if (unlikely(!buf))
  26774. + goto out;
  26775. +
  26776. + seq = au_seq(buf, PAGE_SIZE);
  26777. + err = PTR_ERR(seq);
  26778. + if (IS_ERR(seq))
  26779. + goto out_buf;
  26780. +
  26781. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  26782. + for (bindex = 0; bindex <= bend; bindex++, arg++) {
  26783. + err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
  26784. + if (unlikely(err))
  26785. + break;
  26786. +
  26787. + br = au_sbr(sb, bindex);
  26788. + brid = br->br_id;
  26789. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  26790. + err = __put_user(brid, &arg->id);
  26791. + if (unlikely(err))
  26792. + break;
  26793. +
  26794. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  26795. + err = __put_user(br->br_perm, &arg->perm);
  26796. + if (unlikely(err))
  26797. + break;
  26798. +
  26799. + au_seq_path(seq, &br->br_path);
  26800. + err = seq_putc(seq, '\0');
  26801. + if (!err && seq->count <= sz) {
  26802. + err = copy_to_user(arg->path, seq->buf, seq->count);
  26803. + seq->count = 0;
  26804. + if (unlikely(err))
  26805. + break;
  26806. + } else {
  26807. + err = -E2BIG;
  26808. + goto out_seq;
  26809. + }
  26810. + }
  26811. + if (unlikely(err))
  26812. + err = -EFAULT;
  26813. +
  26814. +out_seq:
  26815. + kfree(seq);
  26816. +out_buf:
  26817. + free_page((unsigned long)buf);
  26818. +out:
  26819. + si_read_unlock(sb);
  26820. + return err;
  26821. +}
  26822. +
  26823. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  26824. +{
  26825. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  26826. +}
  26827. +
  26828. +#ifdef CONFIG_COMPAT
  26829. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  26830. +{
  26831. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  26832. +}
  26833. +#endif
  26834. +
  26835. +/* ---------------------------------------------------------------------- */
  26836. +
  26837. +void sysaufs_br_init(struct au_branch *br)
  26838. +{
  26839. + int i;
  26840. + struct au_brsysfs *br_sysfs;
  26841. + struct attribute *attr;
  26842. +
  26843. + br_sysfs = br->br_sysfs;
  26844. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  26845. + attr = &br_sysfs->attr;
  26846. + sysfs_attr_init(attr);
  26847. + attr->name = br_sysfs->name;
  26848. + attr->mode = S_IRUGO;
  26849. + br_sysfs++;
  26850. + }
  26851. +}
  26852. +
  26853. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  26854. +{
  26855. + struct au_branch *br;
  26856. + struct kobject *kobj;
  26857. + struct au_brsysfs *br_sysfs;
  26858. + int i;
  26859. + aufs_bindex_t bend;
  26860. +
  26861. + dbgaufs_brs_del(sb, bindex);
  26862. +
  26863. + if (!sysaufs_brs)
  26864. + return;
  26865. +
  26866. + kobj = &au_sbi(sb)->si_kobj;
  26867. + bend = au_sbend(sb);
  26868. + for (; bindex <= bend; bindex++) {
  26869. + br = au_sbr(sb, bindex);
  26870. + br_sysfs = br->br_sysfs;
  26871. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  26872. + sysfs_remove_file(kobj, &br_sysfs->attr);
  26873. + br_sysfs++;
  26874. + }
  26875. + }
  26876. +}
  26877. +
  26878. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  26879. +{
  26880. + int err, i;
  26881. + aufs_bindex_t bend;
  26882. + struct kobject *kobj;
  26883. + struct au_branch *br;
  26884. + struct au_brsysfs *br_sysfs;
  26885. +
  26886. + dbgaufs_brs_add(sb, bindex);
  26887. +
  26888. + if (!sysaufs_brs)
  26889. + return;
  26890. +
  26891. + kobj = &au_sbi(sb)->si_kobj;
  26892. + bend = au_sbend(sb);
  26893. + for (; bindex <= bend; bindex++) {
  26894. + br = au_sbr(sb, bindex);
  26895. + br_sysfs = br->br_sysfs;
  26896. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  26897. + SysaufsBr_PREFIX "%d", bindex);
  26898. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  26899. + SysaufsBrid_PREFIX "%d", bindex);
  26900. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  26901. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  26902. + if (unlikely(err))
  26903. + pr_warn("failed %s under sysfs(%d)\n",
  26904. + br_sysfs->name, err);
  26905. + br_sysfs++;
  26906. + }
  26907. + }
  26908. +}
  26909. diff -Nur linux-4.0.4.orig/fs/aufs/sysrq.c linux-4.0.4/fs/aufs/sysrq.c
  26910. --- linux-4.0.4.orig/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
  26911. +++ linux-4.0.4/fs/aufs/sysrq.c 2015-05-30 22:11:30.000000000 +0200
  26912. @@ -0,0 +1,144 @@
  26913. +/*
  26914. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26915. + */
  26916. +
  26917. +/*
  26918. + * magic sysrq hanlder
  26919. + */
  26920. +
  26921. +/* #include <linux/sysrq.h> */
  26922. +#include <linux/writeback.h>
  26923. +#include "aufs.h"
  26924. +
  26925. +/* ---------------------------------------------------------------------- */
  26926. +
  26927. +static void sysrq_sb(struct super_block *sb)
  26928. +{
  26929. + char *plevel;
  26930. + struct au_sbinfo *sbinfo;
  26931. + struct file *file;
  26932. + struct au_sphlhead *files;
  26933. + struct au_finfo *finfo;
  26934. +
  26935. + plevel = au_plevel;
  26936. + au_plevel = KERN_WARNING;
  26937. +
  26938. + /* since we define pr_fmt, call printk directly */
  26939. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  26940. +
  26941. + sbinfo = au_sbi(sb);
  26942. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  26943. + pr("superblock\n");
  26944. + au_dpri_sb(sb);
  26945. +
  26946. +#if 0
  26947. + pr("root dentry\n");
  26948. + au_dpri_dentry(sb->s_root);
  26949. + pr("root inode\n");
  26950. + au_dpri_inode(sb->s_root->d_inode);
  26951. +#endif
  26952. +
  26953. +#if 0
  26954. + do {
  26955. + int err, i, j, ndentry;
  26956. + struct au_dcsub_pages dpages;
  26957. + struct au_dpage *dpage;
  26958. +
  26959. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  26960. + if (unlikely(err))
  26961. + break;
  26962. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  26963. + if (!err)
  26964. + for (i = 0; i < dpages.ndpage; i++) {
  26965. + dpage = dpages.dpages + i;
  26966. + ndentry = dpage->ndentry;
  26967. + for (j = 0; j < ndentry; j++)
  26968. + au_dpri_dentry(dpage->dentries[j]);
  26969. + }
  26970. + au_dpages_free(&dpages);
  26971. + } while (0);
  26972. +#endif
  26973. +
  26974. +#if 1
  26975. + {
  26976. + struct inode *i;
  26977. +
  26978. + pr("isolated inode\n");
  26979. + spin_lock(&inode_sb_list_lock);
  26980. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  26981. + spin_lock(&i->i_lock);
  26982. + if (1 || hlist_empty(&i->i_dentry))
  26983. + au_dpri_inode(i);
  26984. + spin_unlock(&i->i_lock);
  26985. + }
  26986. + spin_unlock(&inode_sb_list_lock);
  26987. + }
  26988. +#endif
  26989. + pr("files\n");
  26990. + files = &au_sbi(sb)->si_files;
  26991. + spin_lock(&files->spin);
  26992. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  26993. + umode_t mode;
  26994. +
  26995. + file = finfo->fi_file;
  26996. + mode = file_inode(file)->i_mode;
  26997. + if (!special_file(mode))
  26998. + au_dpri_file(file);
  26999. + }
  27000. + spin_unlock(&files->spin);
  27001. + pr("done\n");
  27002. +
  27003. +#undef pr
  27004. + au_plevel = plevel;
  27005. +}
  27006. +
  27007. +/* ---------------------------------------------------------------------- */
  27008. +
  27009. +/* module parameter */
  27010. +static char *aufs_sysrq_key = "a";
  27011. +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
  27012. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  27013. +
  27014. +static void au_sysrq(int key __maybe_unused)
  27015. +{
  27016. + struct au_sbinfo *sbinfo;
  27017. +
  27018. + lockdep_off();
  27019. + au_sbilist_lock();
  27020. + list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  27021. + sysrq_sb(sbinfo->si_sb);
  27022. + au_sbilist_unlock();
  27023. + lockdep_on();
  27024. +}
  27025. +
  27026. +static struct sysrq_key_op au_sysrq_op = {
  27027. + .handler = au_sysrq,
  27028. + .help_msg = "Aufs",
  27029. + .action_msg = "Aufs",
  27030. + .enable_mask = SYSRQ_ENABLE_DUMP
  27031. +};
  27032. +
  27033. +/* ---------------------------------------------------------------------- */
  27034. +
  27035. +int __init au_sysrq_init(void)
  27036. +{
  27037. + int err;
  27038. + char key;
  27039. +
  27040. + err = -1;
  27041. + key = *aufs_sysrq_key;
  27042. + if ('a' <= key && key <= 'z')
  27043. + err = register_sysrq_key(key, &au_sysrq_op);
  27044. + if (unlikely(err))
  27045. + pr_err("err %d, sysrq=%c\n", err, key);
  27046. + return err;
  27047. +}
  27048. +
  27049. +void au_sysrq_fin(void)
  27050. +{
  27051. + int err;
  27052. +
  27053. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  27054. + if (unlikely(err))
  27055. + pr_err("err %d (ignored)\n", err);
  27056. +}
  27057. diff -Nur linux-4.0.4.orig/fs/aufs/vdir.c linux-4.0.4/fs/aufs/vdir.c
  27058. --- linux-4.0.4.orig/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
  27059. +++ linux-4.0.4/fs/aufs/vdir.c 2015-05-30 22:11:30.000000000 +0200
  27060. @@ -0,0 +1,876 @@
  27061. +/*
  27062. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27063. + */
  27064. +
  27065. +/*
  27066. + * virtual or vertical directory
  27067. + */
  27068. +
  27069. +#include "aufs.h"
  27070. +
  27071. +static unsigned int calc_size(int nlen)
  27072. +{
  27073. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  27074. +}
  27075. +
  27076. +static int set_deblk_end(union au_vdir_deblk_p *p,
  27077. + union au_vdir_deblk_p *deblk_end)
  27078. +{
  27079. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  27080. + p->de->de_str.len = 0;
  27081. + /* smp_mb(); */
  27082. + return 0;
  27083. + }
  27084. + return -1; /* error */
  27085. +}
  27086. +
  27087. +/* returns true or false */
  27088. +static int is_deblk_end(union au_vdir_deblk_p *p,
  27089. + union au_vdir_deblk_p *deblk_end)
  27090. +{
  27091. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  27092. + return !p->de->de_str.len;
  27093. + return 1;
  27094. +}
  27095. +
  27096. +static unsigned char *last_deblk(struct au_vdir *vdir)
  27097. +{
  27098. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  27099. +}
  27100. +
  27101. +/* ---------------------------------------------------------------------- */
  27102. +
  27103. +/* estimate the apropriate size for name hash table */
  27104. +unsigned int au_rdhash_est(loff_t sz)
  27105. +{
  27106. + unsigned int n;
  27107. +
  27108. + n = UINT_MAX;
  27109. + sz >>= 10;
  27110. + if (sz < n)
  27111. + n = sz;
  27112. + if (sz < AUFS_RDHASH_DEF)
  27113. + n = AUFS_RDHASH_DEF;
  27114. + /* pr_info("n %u\n", n); */
  27115. + return n;
  27116. +}
  27117. +
  27118. +/*
  27119. + * the allocated memory has to be freed by
  27120. + * au_nhash_wh_free() or au_nhash_de_free().
  27121. + */
  27122. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  27123. +{
  27124. + struct hlist_head *head;
  27125. + unsigned int u;
  27126. + size_t sz;
  27127. +
  27128. + sz = sizeof(*nhash->nh_head) * num_hash;
  27129. + head = kmalloc(sz, gfp);
  27130. + if (head) {
  27131. + nhash->nh_num = num_hash;
  27132. + nhash->nh_head = head;
  27133. + for (u = 0; u < num_hash; u++)
  27134. + INIT_HLIST_HEAD(head++);
  27135. + return 0; /* success */
  27136. + }
  27137. +
  27138. + return -ENOMEM;
  27139. +}
  27140. +
  27141. +static void nhash_count(struct hlist_head *head)
  27142. +{
  27143. +#if 0
  27144. + unsigned long n;
  27145. + struct hlist_node *pos;
  27146. +
  27147. + n = 0;
  27148. + hlist_for_each(pos, head)
  27149. + n++;
  27150. + pr_info("%lu\n", n);
  27151. +#endif
  27152. +}
  27153. +
  27154. +static void au_nhash_wh_do_free(struct hlist_head *head)
  27155. +{
  27156. + struct au_vdir_wh *pos;
  27157. + struct hlist_node *node;
  27158. +
  27159. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  27160. + kfree(pos);
  27161. +}
  27162. +
  27163. +static void au_nhash_de_do_free(struct hlist_head *head)
  27164. +{
  27165. + struct au_vdir_dehstr *pos;
  27166. + struct hlist_node *node;
  27167. +
  27168. + hlist_for_each_entry_safe(pos, node, head, hash)
  27169. + au_cache_free_vdir_dehstr(pos);
  27170. +}
  27171. +
  27172. +static void au_nhash_do_free(struct au_nhash *nhash,
  27173. + void (*free)(struct hlist_head *head))
  27174. +{
  27175. + unsigned int n;
  27176. + struct hlist_head *head;
  27177. +
  27178. + n = nhash->nh_num;
  27179. + if (!n)
  27180. + return;
  27181. +
  27182. + head = nhash->nh_head;
  27183. + while (n-- > 0) {
  27184. + nhash_count(head);
  27185. + free(head++);
  27186. + }
  27187. + kfree(nhash->nh_head);
  27188. +}
  27189. +
  27190. +void au_nhash_wh_free(struct au_nhash *whlist)
  27191. +{
  27192. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  27193. +}
  27194. +
  27195. +static void au_nhash_de_free(struct au_nhash *delist)
  27196. +{
  27197. + au_nhash_do_free(delist, au_nhash_de_do_free);
  27198. +}
  27199. +
  27200. +/* ---------------------------------------------------------------------- */
  27201. +
  27202. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  27203. + int limit)
  27204. +{
  27205. + int num;
  27206. + unsigned int u, n;
  27207. + struct hlist_head *head;
  27208. + struct au_vdir_wh *pos;
  27209. +
  27210. + num = 0;
  27211. + n = whlist->nh_num;
  27212. + head = whlist->nh_head;
  27213. + for (u = 0; u < n; u++, head++)
  27214. + hlist_for_each_entry(pos, head, wh_hash)
  27215. + if (pos->wh_bindex == btgt && ++num > limit)
  27216. + return 1;
  27217. + return 0;
  27218. +}
  27219. +
  27220. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  27221. + unsigned char *name,
  27222. + unsigned int len)
  27223. +{
  27224. + unsigned int v;
  27225. + /* const unsigned int magic_bit = 12; */
  27226. +
  27227. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  27228. +
  27229. + v = 0;
  27230. + while (len--)
  27231. + v += *name++;
  27232. + /* v = hash_long(v, magic_bit); */
  27233. + v %= nhash->nh_num;
  27234. + return nhash->nh_head + v;
  27235. +}
  27236. +
  27237. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  27238. + int nlen)
  27239. +{
  27240. + return str->len == nlen && !memcmp(str->name, name, nlen);
  27241. +}
  27242. +
  27243. +/* returns found or not */
  27244. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  27245. +{
  27246. + struct hlist_head *head;
  27247. + struct au_vdir_wh *pos;
  27248. + struct au_vdir_destr *str;
  27249. +
  27250. + head = au_name_hash(whlist, name, nlen);
  27251. + hlist_for_each_entry(pos, head, wh_hash) {
  27252. + str = &pos->wh_str;
  27253. + AuDbg("%.*s\n", str->len, str->name);
  27254. + if (au_nhash_test_name(str, name, nlen))
  27255. + return 1;
  27256. + }
  27257. + return 0;
  27258. +}
  27259. +
  27260. +/* returns found(true) or not */
  27261. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  27262. +{
  27263. + struct hlist_head *head;
  27264. + struct au_vdir_dehstr *pos;
  27265. + struct au_vdir_destr *str;
  27266. +
  27267. + head = au_name_hash(delist, name, nlen);
  27268. + hlist_for_each_entry(pos, head, hash) {
  27269. + str = pos->str;
  27270. + AuDbg("%.*s\n", str->len, str->name);
  27271. + if (au_nhash_test_name(str, name, nlen))
  27272. + return 1;
  27273. + }
  27274. + return 0;
  27275. +}
  27276. +
  27277. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  27278. + unsigned char d_type)
  27279. +{
  27280. +#ifdef CONFIG_AUFS_SHWH
  27281. + wh->wh_ino = ino;
  27282. + wh->wh_type = d_type;
  27283. +#endif
  27284. +}
  27285. +
  27286. +/* ---------------------------------------------------------------------- */
  27287. +
  27288. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  27289. + unsigned int d_type, aufs_bindex_t bindex,
  27290. + unsigned char shwh)
  27291. +{
  27292. + int err;
  27293. + struct au_vdir_destr *str;
  27294. + struct au_vdir_wh *wh;
  27295. +
  27296. + AuDbg("%.*s\n", nlen, name);
  27297. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  27298. +
  27299. + err = -ENOMEM;
  27300. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  27301. + if (unlikely(!wh))
  27302. + goto out;
  27303. +
  27304. + err = 0;
  27305. + wh->wh_bindex = bindex;
  27306. + if (shwh)
  27307. + au_shwh_init_wh(wh, ino, d_type);
  27308. + str = &wh->wh_str;
  27309. + str->len = nlen;
  27310. + memcpy(str->name, name, nlen);
  27311. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  27312. + /* smp_mb(); */
  27313. +
  27314. +out:
  27315. + return err;
  27316. +}
  27317. +
  27318. +static int append_deblk(struct au_vdir *vdir)
  27319. +{
  27320. + int err;
  27321. + unsigned long ul;
  27322. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  27323. + union au_vdir_deblk_p p, deblk_end;
  27324. + unsigned char **o;
  27325. +
  27326. + err = -ENOMEM;
  27327. + o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  27328. + GFP_NOFS);
  27329. + if (unlikely(!o))
  27330. + goto out;
  27331. +
  27332. + vdir->vd_deblk = o;
  27333. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  27334. + if (p.deblk) {
  27335. + ul = vdir->vd_nblk++;
  27336. + vdir->vd_deblk[ul] = p.deblk;
  27337. + vdir->vd_last.ul = ul;
  27338. + vdir->vd_last.p.deblk = p.deblk;
  27339. + deblk_end.deblk = p.deblk + deblk_sz;
  27340. + err = set_deblk_end(&p, &deblk_end);
  27341. + }
  27342. +
  27343. +out:
  27344. + return err;
  27345. +}
  27346. +
  27347. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  27348. + unsigned int d_type, struct au_nhash *delist)
  27349. +{
  27350. + int err;
  27351. + unsigned int sz;
  27352. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  27353. + union au_vdir_deblk_p p, *room, deblk_end;
  27354. + struct au_vdir_dehstr *dehstr;
  27355. +
  27356. + p.deblk = last_deblk(vdir);
  27357. + deblk_end.deblk = p.deblk + deblk_sz;
  27358. + room = &vdir->vd_last.p;
  27359. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  27360. + || !is_deblk_end(room, &deblk_end));
  27361. +
  27362. + sz = calc_size(nlen);
  27363. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  27364. + err = append_deblk(vdir);
  27365. + if (unlikely(err))
  27366. + goto out;
  27367. +
  27368. + p.deblk = last_deblk(vdir);
  27369. + deblk_end.deblk = p.deblk + deblk_sz;
  27370. + /* smp_mb(); */
  27371. + AuDebugOn(room->deblk != p.deblk);
  27372. + }
  27373. +
  27374. + err = -ENOMEM;
  27375. + dehstr = au_cache_alloc_vdir_dehstr();
  27376. + if (unlikely(!dehstr))
  27377. + goto out;
  27378. +
  27379. + dehstr->str = &room->de->de_str;
  27380. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  27381. + room->de->de_ino = ino;
  27382. + room->de->de_type = d_type;
  27383. + room->de->de_str.len = nlen;
  27384. + memcpy(room->de->de_str.name, name, nlen);
  27385. +
  27386. + err = 0;
  27387. + room->deblk += sz;
  27388. + if (unlikely(set_deblk_end(room, &deblk_end)))
  27389. + err = append_deblk(vdir);
  27390. + /* smp_mb(); */
  27391. +
  27392. +out:
  27393. + return err;
  27394. +}
  27395. +
  27396. +/* ---------------------------------------------------------------------- */
  27397. +
  27398. +void au_vdir_free(struct au_vdir *vdir)
  27399. +{
  27400. + unsigned char **deblk;
  27401. +
  27402. + deblk = vdir->vd_deblk;
  27403. + while (vdir->vd_nblk--)
  27404. + kfree(*deblk++);
  27405. + kfree(vdir->vd_deblk);
  27406. + au_cache_free_vdir(vdir);
  27407. +}
  27408. +
  27409. +static struct au_vdir *alloc_vdir(struct file *file)
  27410. +{
  27411. + struct au_vdir *vdir;
  27412. + struct super_block *sb;
  27413. + int err;
  27414. +
  27415. + sb = file->f_path.dentry->d_sb;
  27416. + SiMustAnyLock(sb);
  27417. +
  27418. + err = -ENOMEM;
  27419. + vdir = au_cache_alloc_vdir();
  27420. + if (unlikely(!vdir))
  27421. + goto out;
  27422. +
  27423. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  27424. + if (unlikely(!vdir->vd_deblk))
  27425. + goto out_free;
  27426. +
  27427. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  27428. + if (!vdir->vd_deblk_sz) {
  27429. + /* estimate the apropriate size for deblk */
  27430. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  27431. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  27432. + }
  27433. + vdir->vd_nblk = 0;
  27434. + vdir->vd_version = 0;
  27435. + vdir->vd_jiffy = 0;
  27436. + err = append_deblk(vdir);
  27437. + if (!err)
  27438. + return vdir; /* success */
  27439. +
  27440. + kfree(vdir->vd_deblk);
  27441. +
  27442. +out_free:
  27443. + au_cache_free_vdir(vdir);
  27444. +out:
  27445. + vdir = ERR_PTR(err);
  27446. + return vdir;
  27447. +}
  27448. +
  27449. +static int reinit_vdir(struct au_vdir *vdir)
  27450. +{
  27451. + int err;
  27452. + union au_vdir_deblk_p p, deblk_end;
  27453. +
  27454. + while (vdir->vd_nblk > 1) {
  27455. + kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
  27456. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  27457. + vdir->vd_nblk--;
  27458. + }
  27459. + p.deblk = vdir->vd_deblk[0];
  27460. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  27461. + err = set_deblk_end(&p, &deblk_end);
  27462. + /* keep vd_dblk_sz */
  27463. + vdir->vd_last.ul = 0;
  27464. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  27465. + vdir->vd_version = 0;
  27466. + vdir->vd_jiffy = 0;
  27467. + /* smp_mb(); */
  27468. + return err;
  27469. +}
  27470. +
  27471. +/* ---------------------------------------------------------------------- */
  27472. +
  27473. +#define AuFillVdir_CALLED 1
  27474. +#define AuFillVdir_WHABLE (1 << 1)
  27475. +#define AuFillVdir_SHWH (1 << 2)
  27476. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  27477. +#define au_fset_fillvdir(flags, name) \
  27478. + do { (flags) |= AuFillVdir_##name; } while (0)
  27479. +#define au_fclr_fillvdir(flags, name) \
  27480. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  27481. +
  27482. +#ifndef CONFIG_AUFS_SHWH
  27483. +#undef AuFillVdir_SHWH
  27484. +#define AuFillVdir_SHWH 0
  27485. +#endif
  27486. +
  27487. +struct fillvdir_arg {
  27488. + struct dir_context ctx;
  27489. + struct file *file;
  27490. + struct au_vdir *vdir;
  27491. + struct au_nhash delist;
  27492. + struct au_nhash whlist;
  27493. + aufs_bindex_t bindex;
  27494. + unsigned int flags;
  27495. + int err;
  27496. +};
  27497. +
  27498. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  27499. + loff_t offset __maybe_unused, u64 h_ino,
  27500. + unsigned int d_type)
  27501. +{
  27502. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  27503. + char *name = (void *)__name;
  27504. + struct super_block *sb;
  27505. + ino_t ino;
  27506. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  27507. +
  27508. + arg->err = 0;
  27509. + sb = arg->file->f_path.dentry->d_sb;
  27510. + au_fset_fillvdir(arg->flags, CALLED);
  27511. + /* smp_mb(); */
  27512. + if (nlen <= AUFS_WH_PFX_LEN
  27513. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  27514. + if (test_known(&arg->delist, name, nlen)
  27515. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  27516. + goto out; /* already exists or whiteouted */
  27517. +
  27518. + sb = arg->file->f_path.dentry->d_sb;
  27519. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  27520. + if (!arg->err) {
  27521. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  27522. + d_type = DT_UNKNOWN;
  27523. + arg->err = append_de(arg->vdir, name, nlen, ino,
  27524. + d_type, &arg->delist);
  27525. + }
  27526. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  27527. + name += AUFS_WH_PFX_LEN;
  27528. + nlen -= AUFS_WH_PFX_LEN;
  27529. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  27530. + goto out; /* already whiteouted */
  27531. +
  27532. + if (shwh)
  27533. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  27534. + &ino);
  27535. + if (!arg->err) {
  27536. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  27537. + d_type = DT_UNKNOWN;
  27538. + arg->err = au_nhash_append_wh
  27539. + (&arg->whlist, name, nlen, ino, d_type,
  27540. + arg->bindex, shwh);
  27541. + }
  27542. + }
  27543. +
  27544. +out:
  27545. + if (!arg->err)
  27546. + arg->vdir->vd_jiffy = jiffies;
  27547. + /* smp_mb(); */
  27548. + AuTraceErr(arg->err);
  27549. + return arg->err;
  27550. +}
  27551. +
  27552. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  27553. + struct au_nhash *whlist, struct au_nhash *delist)
  27554. +{
  27555. +#ifdef CONFIG_AUFS_SHWH
  27556. + int err;
  27557. + unsigned int nh, u;
  27558. + struct hlist_head *head;
  27559. + struct au_vdir_wh *pos;
  27560. + struct hlist_node *n;
  27561. + char *p, *o;
  27562. + struct au_vdir_destr *destr;
  27563. +
  27564. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  27565. +
  27566. + err = -ENOMEM;
  27567. + o = p = (void *)__get_free_page(GFP_NOFS);
  27568. + if (unlikely(!p))
  27569. + goto out;
  27570. +
  27571. + err = 0;
  27572. + nh = whlist->nh_num;
  27573. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  27574. + p += AUFS_WH_PFX_LEN;
  27575. + for (u = 0; u < nh; u++) {
  27576. + head = whlist->nh_head + u;
  27577. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  27578. + destr = &pos->wh_str;
  27579. + memcpy(p, destr->name, destr->len);
  27580. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  27581. + pos->wh_ino, pos->wh_type, delist);
  27582. + if (unlikely(err))
  27583. + break;
  27584. + }
  27585. + }
  27586. +
  27587. + free_page((unsigned long)o);
  27588. +
  27589. +out:
  27590. + AuTraceErr(err);
  27591. + return err;
  27592. +#else
  27593. + return 0;
  27594. +#endif
  27595. +}
  27596. +
  27597. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  27598. +{
  27599. + int err;
  27600. + unsigned int rdhash;
  27601. + loff_t offset;
  27602. + aufs_bindex_t bend, bindex, bstart;
  27603. + unsigned char shwh;
  27604. + struct file *hf, *file;
  27605. + struct super_block *sb;
  27606. +
  27607. + file = arg->file;
  27608. + sb = file->f_path.dentry->d_sb;
  27609. + SiMustAnyLock(sb);
  27610. +
  27611. + rdhash = au_sbi(sb)->si_rdhash;
  27612. + if (!rdhash)
  27613. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  27614. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  27615. + if (unlikely(err))
  27616. + goto out;
  27617. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  27618. + if (unlikely(err))
  27619. + goto out_delist;
  27620. +
  27621. + err = 0;
  27622. + arg->flags = 0;
  27623. + shwh = 0;
  27624. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  27625. + shwh = 1;
  27626. + au_fset_fillvdir(arg->flags, SHWH);
  27627. + }
  27628. + bstart = au_fbstart(file);
  27629. + bend = au_fbend_dir(file);
  27630. + for (bindex = bstart; !err && bindex <= bend; bindex++) {
  27631. + hf = au_hf_dir(file, bindex);
  27632. + if (!hf)
  27633. + continue;
  27634. +
  27635. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  27636. + err = offset;
  27637. + if (unlikely(offset))
  27638. + break;
  27639. +
  27640. + arg->bindex = bindex;
  27641. + au_fclr_fillvdir(arg->flags, WHABLE);
  27642. + if (shwh
  27643. + || (bindex != bend
  27644. + && au_br_whable(au_sbr_perm(sb, bindex))))
  27645. + au_fset_fillvdir(arg->flags, WHABLE);
  27646. + do {
  27647. + arg->err = 0;
  27648. + au_fclr_fillvdir(arg->flags, CALLED);
  27649. + /* smp_mb(); */
  27650. + err = vfsub_iterate_dir(hf, &arg->ctx);
  27651. + if (err >= 0)
  27652. + err = arg->err;
  27653. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  27654. +
  27655. + /*
  27656. + * dir_relax() may be good for concurrency, but aufs should not
  27657. + * use it since it will cause a lockdep problem.
  27658. + */
  27659. + }
  27660. +
  27661. + if (!err && shwh)
  27662. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  27663. +
  27664. + au_nhash_wh_free(&arg->whlist);
  27665. +
  27666. +out_delist:
  27667. + au_nhash_de_free(&arg->delist);
  27668. +out:
  27669. + return err;
  27670. +}
  27671. +
  27672. +static int read_vdir(struct file *file, int may_read)
  27673. +{
  27674. + int err;
  27675. + unsigned long expire;
  27676. + unsigned char do_read;
  27677. + struct fillvdir_arg arg = {
  27678. + .ctx = {
  27679. + .actor = fillvdir
  27680. + }
  27681. + };
  27682. + struct inode *inode;
  27683. + struct au_vdir *vdir, *allocated;
  27684. +
  27685. + err = 0;
  27686. + inode = file_inode(file);
  27687. + IMustLock(inode);
  27688. + SiMustAnyLock(inode->i_sb);
  27689. +
  27690. + allocated = NULL;
  27691. + do_read = 0;
  27692. + expire = au_sbi(inode->i_sb)->si_rdcache;
  27693. + vdir = au_ivdir(inode);
  27694. + if (!vdir) {
  27695. + do_read = 1;
  27696. + vdir = alloc_vdir(file);
  27697. + err = PTR_ERR(vdir);
  27698. + if (IS_ERR(vdir))
  27699. + goto out;
  27700. + err = 0;
  27701. + allocated = vdir;
  27702. + } else if (may_read
  27703. + && (inode->i_version != vdir->vd_version
  27704. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  27705. + do_read = 1;
  27706. + err = reinit_vdir(vdir);
  27707. + if (unlikely(err))
  27708. + goto out;
  27709. + }
  27710. +
  27711. + if (!do_read)
  27712. + return 0; /* success */
  27713. +
  27714. + arg.file = file;
  27715. + arg.vdir = vdir;
  27716. + err = au_do_read_vdir(&arg);
  27717. + if (!err) {
  27718. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  27719. + vdir->vd_version = inode->i_version;
  27720. + vdir->vd_last.ul = 0;
  27721. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  27722. + if (allocated)
  27723. + au_set_ivdir(inode, allocated);
  27724. + } else if (allocated)
  27725. + au_vdir_free(allocated);
  27726. +
  27727. +out:
  27728. + return err;
  27729. +}
  27730. +
  27731. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  27732. +{
  27733. + int err, rerr;
  27734. + unsigned long ul, n;
  27735. + const unsigned int deblk_sz = src->vd_deblk_sz;
  27736. +
  27737. + AuDebugOn(tgt->vd_nblk != 1);
  27738. +
  27739. + err = -ENOMEM;
  27740. + if (tgt->vd_nblk < src->vd_nblk) {
  27741. + unsigned char **p;
  27742. +
  27743. + p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  27744. + GFP_NOFS);
  27745. + if (unlikely(!p))
  27746. + goto out;
  27747. + tgt->vd_deblk = p;
  27748. + }
  27749. +
  27750. + if (tgt->vd_deblk_sz != deblk_sz) {
  27751. + unsigned char *p;
  27752. +
  27753. + tgt->vd_deblk_sz = deblk_sz;
  27754. + p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
  27755. + if (unlikely(!p))
  27756. + goto out;
  27757. + tgt->vd_deblk[0] = p;
  27758. + }
  27759. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  27760. + tgt->vd_version = src->vd_version;
  27761. + tgt->vd_jiffy = src->vd_jiffy;
  27762. +
  27763. + n = src->vd_nblk;
  27764. + for (ul = 1; ul < n; ul++) {
  27765. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  27766. + GFP_NOFS);
  27767. + if (unlikely(!tgt->vd_deblk[ul]))
  27768. + goto out;
  27769. + tgt->vd_nblk++;
  27770. + }
  27771. + tgt->vd_nblk = n;
  27772. + tgt->vd_last.ul = tgt->vd_last.ul;
  27773. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  27774. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  27775. + - src->vd_deblk[src->vd_last.ul];
  27776. + /* smp_mb(); */
  27777. + return 0; /* success */
  27778. +
  27779. +out:
  27780. + rerr = reinit_vdir(tgt);
  27781. + BUG_ON(rerr);
  27782. + return err;
  27783. +}
  27784. +
  27785. +int au_vdir_init(struct file *file)
  27786. +{
  27787. + int err;
  27788. + struct inode *inode;
  27789. + struct au_vdir *vdir_cache, *allocated;
  27790. +
  27791. + /* test file->f_pos here instead of ctx->pos */
  27792. + err = read_vdir(file, !file->f_pos);
  27793. + if (unlikely(err))
  27794. + goto out;
  27795. +
  27796. + allocated = NULL;
  27797. + vdir_cache = au_fvdir_cache(file);
  27798. + if (!vdir_cache) {
  27799. + vdir_cache = alloc_vdir(file);
  27800. + err = PTR_ERR(vdir_cache);
  27801. + if (IS_ERR(vdir_cache))
  27802. + goto out;
  27803. + allocated = vdir_cache;
  27804. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  27805. + /* test file->f_pos here instead of ctx->pos */
  27806. + err = reinit_vdir(vdir_cache);
  27807. + if (unlikely(err))
  27808. + goto out;
  27809. + } else
  27810. + return 0; /* success */
  27811. +
  27812. + inode = file_inode(file);
  27813. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  27814. + if (!err) {
  27815. + file->f_version = inode->i_version;
  27816. + if (allocated)
  27817. + au_set_fvdir_cache(file, allocated);
  27818. + } else if (allocated)
  27819. + au_vdir_free(allocated);
  27820. +
  27821. +out:
  27822. + return err;
  27823. +}
  27824. +
  27825. +static loff_t calc_offset(struct au_vdir *vdir)
  27826. +{
  27827. + loff_t offset;
  27828. + union au_vdir_deblk_p p;
  27829. +
  27830. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  27831. + offset = vdir->vd_last.p.deblk - p.deblk;
  27832. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  27833. + return offset;
  27834. +}
  27835. +
  27836. +/* returns true or false */
  27837. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  27838. +{
  27839. + int valid;
  27840. + unsigned int deblk_sz;
  27841. + unsigned long ul, n;
  27842. + loff_t offset;
  27843. + union au_vdir_deblk_p p, deblk_end;
  27844. + struct au_vdir *vdir_cache;
  27845. +
  27846. + valid = 1;
  27847. + vdir_cache = au_fvdir_cache(file);
  27848. + offset = calc_offset(vdir_cache);
  27849. + AuDbg("offset %lld\n", offset);
  27850. + if (ctx->pos == offset)
  27851. + goto out;
  27852. +
  27853. + vdir_cache->vd_last.ul = 0;
  27854. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  27855. + if (!ctx->pos)
  27856. + goto out;
  27857. +
  27858. + valid = 0;
  27859. + deblk_sz = vdir_cache->vd_deblk_sz;
  27860. + ul = div64_u64(ctx->pos, deblk_sz);
  27861. + AuDbg("ul %lu\n", ul);
  27862. + if (ul >= vdir_cache->vd_nblk)
  27863. + goto out;
  27864. +
  27865. + n = vdir_cache->vd_nblk;
  27866. + for (; ul < n; ul++) {
  27867. + p.deblk = vdir_cache->vd_deblk[ul];
  27868. + deblk_end.deblk = p.deblk + deblk_sz;
  27869. + offset = ul;
  27870. + offset *= deblk_sz;
  27871. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  27872. + unsigned int l;
  27873. +
  27874. + l = calc_size(p.de->de_str.len);
  27875. + offset += l;
  27876. + p.deblk += l;
  27877. + }
  27878. + if (!is_deblk_end(&p, &deblk_end)) {
  27879. + valid = 1;
  27880. + vdir_cache->vd_last.ul = ul;
  27881. + vdir_cache->vd_last.p = p;
  27882. + break;
  27883. + }
  27884. + }
  27885. +
  27886. +out:
  27887. + /* smp_mb(); */
  27888. + AuTraceErr(!valid);
  27889. + return valid;
  27890. +}
  27891. +
  27892. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  27893. +{
  27894. + unsigned int l, deblk_sz;
  27895. + union au_vdir_deblk_p deblk_end;
  27896. + struct au_vdir *vdir_cache;
  27897. + struct au_vdir_de *de;
  27898. +
  27899. + vdir_cache = au_fvdir_cache(file);
  27900. + if (!seek_vdir(file, ctx))
  27901. + return 0;
  27902. +
  27903. + deblk_sz = vdir_cache->vd_deblk_sz;
  27904. + while (1) {
  27905. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  27906. + deblk_end.deblk += deblk_sz;
  27907. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  27908. + de = vdir_cache->vd_last.p.de;
  27909. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  27910. + de->de_str.len, de->de_str.name, ctx->pos,
  27911. + (unsigned long)de->de_ino, de->de_type);
  27912. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  27913. + de->de_str.len, de->de_ino,
  27914. + de->de_type))) {
  27915. + /* todo: ignore the error caused by udba? */
  27916. + /* return err; */
  27917. + return 0;
  27918. + }
  27919. +
  27920. + l = calc_size(de->de_str.len);
  27921. + vdir_cache->vd_last.p.deblk += l;
  27922. + ctx->pos += l;
  27923. + }
  27924. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  27925. + vdir_cache->vd_last.ul++;
  27926. + vdir_cache->vd_last.p.deblk
  27927. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  27928. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  27929. + continue;
  27930. + }
  27931. + break;
  27932. + }
  27933. +
  27934. + /* smp_mb(); */
  27935. + return 0;
  27936. +}
  27937. diff -Nur linux-4.0.4.orig/fs/aufs/vfsub.c linux-4.0.4/fs/aufs/vfsub.c
  27938. --- linux-4.0.4.orig/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
  27939. +++ linux-4.0.4/fs/aufs/vfsub.c 2015-05-30 22:11:31.000000000 +0200
  27940. @@ -0,0 +1,833 @@
  27941. +/*
  27942. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27943. + */
  27944. +
  27945. +/*
  27946. + * sub-routines for VFS
  27947. + */
  27948. +
  27949. +#include <linux/namei.h>
  27950. +#include <linux/security.h>
  27951. +#include <linux/splice.h>
  27952. +#include "aufs.h"
  27953. +
  27954. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  27955. +{
  27956. + int err;
  27957. + struct kstat st;
  27958. + struct super_block *h_sb;
  27959. +
  27960. + /* for remote fs, leave work for its getattr or d_revalidate */
  27961. + /* for bad i_attr fs, handle them in aufs_getattr() */
  27962. + /* still some fs may acquire i_mutex. we need to skip them */
  27963. + err = 0;
  27964. + if (!did)
  27965. + did = &err;
  27966. + h_sb = h_path->dentry->d_sb;
  27967. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  27968. + if (*did)
  27969. + err = vfs_getattr(h_path, &st);
  27970. +
  27971. + return err;
  27972. +}
  27973. +
  27974. +/* ---------------------------------------------------------------------- */
  27975. +
  27976. +struct file *vfsub_dentry_open(struct path *path, int flags)
  27977. +{
  27978. + struct file *file;
  27979. +
  27980. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  27981. + current_cred());
  27982. + if (!IS_ERR_OR_NULL(file)
  27983. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  27984. + i_readcount_inc(path->dentry->d_inode);
  27985. +
  27986. + return file;
  27987. +}
  27988. +
  27989. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  27990. +{
  27991. + struct file *file;
  27992. +
  27993. + lockdep_off();
  27994. + file = filp_open(path,
  27995. + oflags /* | __FMODE_NONOTIFY */,
  27996. + mode);
  27997. + lockdep_on();
  27998. + if (IS_ERR(file))
  27999. + goto out;
  28000. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  28001. +
  28002. +out:
  28003. + return file;
  28004. +}
  28005. +
  28006. +/*
  28007. + * Ideally this function should call VFS:do_last() in order to keep all its
  28008. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  28009. + * structure such as nameidata. This is a second (or third) best approach.
  28010. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  28011. + */
  28012. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  28013. + struct vfsub_aopen_args *args, struct au_branch *br)
  28014. +{
  28015. + int err;
  28016. + struct file *file = args->file;
  28017. + /* copied from linux/fs/namei.c:atomic_open() */
  28018. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  28019. +
  28020. + IMustLock(dir);
  28021. + AuDebugOn(!dir->i_op->atomic_open);
  28022. +
  28023. + err = au_br_test_oflag(args->open_flag, br);
  28024. + if (unlikely(err))
  28025. + goto out;
  28026. +
  28027. + args->file->f_path.dentry = DENTRY_NOT_SET;
  28028. + args->file->f_path.mnt = au_br_mnt(br);
  28029. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  28030. + args->create_mode, args->opened);
  28031. + if (err >= 0) {
  28032. + /* some filesystems don't set FILE_CREATED while succeeded? */
  28033. + if (*args->opened & FILE_CREATED)
  28034. + fsnotify_create(dir, dentry);
  28035. + } else
  28036. + goto out;
  28037. +
  28038. +
  28039. + if (!err) {
  28040. + /* todo: call VFS:may_open() here */
  28041. + err = open_check_o_direct(file);
  28042. + /* todo: ima_file_check() too? */
  28043. + if (!err && (args->open_flag & __FMODE_EXEC))
  28044. + err = deny_write_access(file);
  28045. + if (unlikely(err))
  28046. + /* note that the file is created and still opened */
  28047. + goto out;
  28048. + }
  28049. +
  28050. + atomic_inc(&br->br_count);
  28051. + fsnotify_open(file);
  28052. +
  28053. +out:
  28054. + return err;
  28055. +}
  28056. +
  28057. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  28058. +{
  28059. + int err;
  28060. +
  28061. + err = kern_path(name, flags, path);
  28062. + if (!err && path->dentry->d_inode)
  28063. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  28064. + return err;
  28065. +}
  28066. +
  28067. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  28068. + int len)
  28069. +{
  28070. + struct path path = {
  28071. + .mnt = NULL
  28072. + };
  28073. +
  28074. + /* VFS checks it too, but by WARN_ON_ONCE() */
  28075. + IMustLock(parent->d_inode);
  28076. +
  28077. + path.dentry = lookup_one_len(name, parent, len);
  28078. + if (IS_ERR(path.dentry))
  28079. + goto out;
  28080. + if (path.dentry->d_inode)
  28081. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  28082. +
  28083. +out:
  28084. + AuTraceErrPtr(path.dentry);
  28085. + return path.dentry;
  28086. +}
  28087. +
  28088. +void vfsub_call_lkup_one(void *args)
  28089. +{
  28090. + struct vfsub_lkup_one_args *a = args;
  28091. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  28092. +}
  28093. +
  28094. +/* ---------------------------------------------------------------------- */
  28095. +
  28096. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  28097. + struct dentry *d2, struct au_hinode *hdir2)
  28098. +{
  28099. + struct dentry *d;
  28100. +
  28101. + lockdep_off();
  28102. + d = lock_rename(d1, d2);
  28103. + lockdep_on();
  28104. + au_hn_suspend(hdir1);
  28105. + if (hdir1 != hdir2)
  28106. + au_hn_suspend(hdir2);
  28107. +
  28108. + return d;
  28109. +}
  28110. +
  28111. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  28112. + struct dentry *d2, struct au_hinode *hdir2)
  28113. +{
  28114. + au_hn_resume(hdir1);
  28115. + if (hdir1 != hdir2)
  28116. + au_hn_resume(hdir2);
  28117. + lockdep_off();
  28118. + unlock_rename(d1, d2);
  28119. + lockdep_on();
  28120. +}
  28121. +
  28122. +/* ---------------------------------------------------------------------- */
  28123. +
  28124. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  28125. +{
  28126. + int err;
  28127. + struct dentry *d;
  28128. +
  28129. + IMustLock(dir);
  28130. +
  28131. + d = path->dentry;
  28132. + path->dentry = d->d_parent;
  28133. + err = security_path_mknod(path, d, mode, 0);
  28134. + path->dentry = d;
  28135. + if (unlikely(err))
  28136. + goto out;
  28137. +
  28138. + lockdep_off();
  28139. + err = vfs_create(dir, path->dentry, mode, want_excl);
  28140. + lockdep_on();
  28141. + if (!err) {
  28142. + struct path tmp = *path;
  28143. + int did;
  28144. +
  28145. + vfsub_update_h_iattr(&tmp, &did);
  28146. + if (did) {
  28147. + tmp.dentry = path->dentry->d_parent;
  28148. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28149. + }
  28150. + /*ignore*/
  28151. + }
  28152. +
  28153. +out:
  28154. + return err;
  28155. +}
  28156. +
  28157. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  28158. +{
  28159. + int err;
  28160. + struct dentry *d;
  28161. +
  28162. + IMustLock(dir);
  28163. +
  28164. + d = path->dentry;
  28165. + path->dentry = d->d_parent;
  28166. + err = security_path_symlink(path, d, symname);
  28167. + path->dentry = d;
  28168. + if (unlikely(err))
  28169. + goto out;
  28170. +
  28171. + lockdep_off();
  28172. + err = vfs_symlink(dir, path->dentry, symname);
  28173. + lockdep_on();
  28174. + if (!err) {
  28175. + struct path tmp = *path;
  28176. + int did;
  28177. +
  28178. + vfsub_update_h_iattr(&tmp, &did);
  28179. + if (did) {
  28180. + tmp.dentry = path->dentry->d_parent;
  28181. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28182. + }
  28183. + /*ignore*/
  28184. + }
  28185. +
  28186. +out:
  28187. + return err;
  28188. +}
  28189. +
  28190. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  28191. +{
  28192. + int err;
  28193. + struct dentry *d;
  28194. +
  28195. + IMustLock(dir);
  28196. +
  28197. + d = path->dentry;
  28198. + path->dentry = d->d_parent;
  28199. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  28200. + path->dentry = d;
  28201. + if (unlikely(err))
  28202. + goto out;
  28203. +
  28204. + lockdep_off();
  28205. + err = vfs_mknod(dir, path->dentry, mode, dev);
  28206. + lockdep_on();
  28207. + if (!err) {
  28208. + struct path tmp = *path;
  28209. + int did;
  28210. +
  28211. + vfsub_update_h_iattr(&tmp, &did);
  28212. + if (did) {
  28213. + tmp.dentry = path->dentry->d_parent;
  28214. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28215. + }
  28216. + /*ignore*/
  28217. + }
  28218. +
  28219. +out:
  28220. + return err;
  28221. +}
  28222. +
  28223. +static int au_test_nlink(struct inode *inode)
  28224. +{
  28225. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  28226. +
  28227. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  28228. + || inode->i_nlink < link_max)
  28229. + return 0;
  28230. + return -EMLINK;
  28231. +}
  28232. +
  28233. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  28234. + struct inode **delegated_inode)
  28235. +{
  28236. + int err;
  28237. + struct dentry *d;
  28238. +
  28239. + IMustLock(dir);
  28240. +
  28241. + err = au_test_nlink(src_dentry->d_inode);
  28242. + if (unlikely(err))
  28243. + return err;
  28244. +
  28245. + /* we don't call may_linkat() */
  28246. + d = path->dentry;
  28247. + path->dentry = d->d_parent;
  28248. + err = security_path_link(src_dentry, path, d);
  28249. + path->dentry = d;
  28250. + if (unlikely(err))
  28251. + goto out;
  28252. +
  28253. + lockdep_off();
  28254. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  28255. + lockdep_on();
  28256. + if (!err) {
  28257. + struct path tmp = *path;
  28258. + int did;
  28259. +
  28260. + /* fuse has different memory inode for the same inumber */
  28261. + vfsub_update_h_iattr(&tmp, &did);
  28262. + if (did) {
  28263. + tmp.dentry = path->dentry->d_parent;
  28264. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28265. + tmp.dentry = src_dentry;
  28266. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28267. + }
  28268. + /*ignore*/
  28269. + }
  28270. +
  28271. +out:
  28272. + return err;
  28273. +}
  28274. +
  28275. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  28276. + struct inode *dir, struct path *path,
  28277. + struct inode **delegated_inode)
  28278. +{
  28279. + int err;
  28280. + struct path tmp = {
  28281. + .mnt = path->mnt
  28282. + };
  28283. + struct dentry *d;
  28284. +
  28285. + IMustLock(dir);
  28286. + IMustLock(src_dir);
  28287. +
  28288. + d = path->dentry;
  28289. + path->dentry = d->d_parent;
  28290. + tmp.dentry = src_dentry->d_parent;
  28291. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  28292. + path->dentry = d;
  28293. + if (unlikely(err))
  28294. + goto out;
  28295. +
  28296. + lockdep_off();
  28297. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  28298. + delegated_inode, /*flags*/0);
  28299. + lockdep_on();
  28300. + if (!err) {
  28301. + int did;
  28302. +
  28303. + tmp.dentry = d->d_parent;
  28304. + vfsub_update_h_iattr(&tmp, &did);
  28305. + if (did) {
  28306. + tmp.dentry = src_dentry;
  28307. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28308. + tmp.dentry = src_dentry->d_parent;
  28309. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28310. + }
  28311. + /*ignore*/
  28312. + }
  28313. +
  28314. +out:
  28315. + return err;
  28316. +}
  28317. +
  28318. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  28319. +{
  28320. + int err;
  28321. + struct dentry *d;
  28322. +
  28323. + IMustLock(dir);
  28324. +
  28325. + d = path->dentry;
  28326. + path->dentry = d->d_parent;
  28327. + err = security_path_mkdir(path, d, mode);
  28328. + path->dentry = d;
  28329. + if (unlikely(err))
  28330. + goto out;
  28331. +
  28332. + lockdep_off();
  28333. + err = vfs_mkdir(dir, path->dentry, mode);
  28334. + lockdep_on();
  28335. + if (!err) {
  28336. + struct path tmp = *path;
  28337. + int did;
  28338. +
  28339. + vfsub_update_h_iattr(&tmp, &did);
  28340. + if (did) {
  28341. + tmp.dentry = path->dentry->d_parent;
  28342. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  28343. + }
  28344. + /*ignore*/
  28345. + }
  28346. +
  28347. +out:
  28348. + return err;
  28349. +}
  28350. +
  28351. +int vfsub_rmdir(struct inode *dir, struct path *path)
  28352. +{
  28353. + int err;
  28354. + struct dentry *d;
  28355. +
  28356. + IMustLock(dir);
  28357. +
  28358. + d = path->dentry;
  28359. + path->dentry = d->d_parent;
  28360. + err = security_path_rmdir(path, d);
  28361. + path->dentry = d;
  28362. + if (unlikely(err))
  28363. + goto out;
  28364. +
  28365. + lockdep_off();
  28366. + err = vfs_rmdir(dir, path->dentry);
  28367. + lockdep_on();
  28368. + if (!err) {
  28369. + struct path tmp = {
  28370. + .dentry = path->dentry->d_parent,
  28371. + .mnt = path->mnt
  28372. + };
  28373. +
  28374. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  28375. + }
  28376. +
  28377. +out:
  28378. + return err;
  28379. +}
  28380. +
  28381. +/* ---------------------------------------------------------------------- */
  28382. +
  28383. +/* todo: support mmap_sem? */
  28384. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  28385. + loff_t *ppos)
  28386. +{
  28387. + ssize_t err;
  28388. +
  28389. + lockdep_off();
  28390. + err = vfs_read(file, ubuf, count, ppos);
  28391. + lockdep_on();
  28392. + if (err >= 0)
  28393. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  28394. + return err;
  28395. +}
  28396. +
  28397. +/* todo: kernel_read()? */
  28398. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  28399. + loff_t *ppos)
  28400. +{
  28401. + ssize_t err;
  28402. + mm_segment_t oldfs;
  28403. + union {
  28404. + void *k;
  28405. + char __user *u;
  28406. + } buf;
  28407. +
  28408. + buf.k = kbuf;
  28409. + oldfs = get_fs();
  28410. + set_fs(KERNEL_DS);
  28411. + err = vfsub_read_u(file, buf.u, count, ppos);
  28412. + set_fs(oldfs);
  28413. + return err;
  28414. +}
  28415. +
  28416. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  28417. + loff_t *ppos)
  28418. +{
  28419. + ssize_t err;
  28420. +
  28421. + lockdep_off();
  28422. + err = vfs_write(file, ubuf, count, ppos);
  28423. + lockdep_on();
  28424. + if (err >= 0)
  28425. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  28426. + return err;
  28427. +}
  28428. +
  28429. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  28430. +{
  28431. + ssize_t err;
  28432. + mm_segment_t oldfs;
  28433. + union {
  28434. + void *k;
  28435. + const char __user *u;
  28436. + } buf;
  28437. +
  28438. + buf.k = kbuf;
  28439. + oldfs = get_fs();
  28440. + set_fs(KERNEL_DS);
  28441. + err = vfsub_write_u(file, buf.u, count, ppos);
  28442. + set_fs(oldfs);
  28443. + return err;
  28444. +}
  28445. +
  28446. +int vfsub_flush(struct file *file, fl_owner_t id)
  28447. +{
  28448. + int err;
  28449. +
  28450. + err = 0;
  28451. + if (file->f_op->flush) {
  28452. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  28453. + err = file->f_op->flush(file, id);
  28454. + else {
  28455. + lockdep_off();
  28456. + err = file->f_op->flush(file, id);
  28457. + lockdep_on();
  28458. + }
  28459. + if (!err)
  28460. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  28461. + /*ignore*/
  28462. + }
  28463. + return err;
  28464. +}
  28465. +
  28466. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  28467. +{
  28468. + int err;
  28469. +
  28470. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  28471. +
  28472. + lockdep_off();
  28473. + err = iterate_dir(file, ctx);
  28474. + lockdep_on();
  28475. + if (err >= 0)
  28476. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  28477. + return err;
  28478. +}
  28479. +
  28480. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  28481. + struct pipe_inode_info *pipe, size_t len,
  28482. + unsigned int flags)
  28483. +{
  28484. + long err;
  28485. +
  28486. + lockdep_off();
  28487. + err = do_splice_to(in, ppos, pipe, len, flags);
  28488. + lockdep_on();
  28489. + file_accessed(in);
  28490. + if (err >= 0)
  28491. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  28492. + return err;
  28493. +}
  28494. +
  28495. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  28496. + loff_t *ppos, size_t len, unsigned int flags)
  28497. +{
  28498. + long err;
  28499. +
  28500. + lockdep_off();
  28501. + err = do_splice_from(pipe, out, ppos, len, flags);
  28502. + lockdep_on();
  28503. + if (err >= 0)
  28504. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  28505. + return err;
  28506. +}
  28507. +
  28508. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  28509. +{
  28510. + int err;
  28511. +
  28512. + /* file can be NULL */
  28513. + lockdep_off();
  28514. + err = vfs_fsync(file, datasync);
  28515. + lockdep_on();
  28516. + if (!err) {
  28517. + if (!path) {
  28518. + AuDebugOn(!file);
  28519. + path = &file->f_path;
  28520. + }
  28521. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  28522. + }
  28523. + return err;
  28524. +}
  28525. +
  28526. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  28527. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  28528. + struct file *h_file)
  28529. +{
  28530. + int err;
  28531. + struct inode *h_inode;
  28532. + struct super_block *h_sb;
  28533. +
  28534. + if (!h_file) {
  28535. + err = vfsub_truncate(h_path, length);
  28536. + goto out;
  28537. + }
  28538. +
  28539. + h_inode = h_path->dentry->d_inode;
  28540. + h_sb = h_inode->i_sb;
  28541. + lockdep_off();
  28542. + sb_start_write(h_sb);
  28543. + lockdep_on();
  28544. + err = locks_verify_truncate(h_inode, h_file, length);
  28545. + if (!err)
  28546. + err = security_path_truncate(h_path);
  28547. + if (!err) {
  28548. + lockdep_off();
  28549. + err = do_truncate(h_path->dentry, length, attr, h_file);
  28550. + lockdep_on();
  28551. + }
  28552. + lockdep_off();
  28553. + sb_end_write(h_sb);
  28554. + lockdep_on();
  28555. +
  28556. +out:
  28557. + return err;
  28558. +}
  28559. +
  28560. +/* ---------------------------------------------------------------------- */
  28561. +
  28562. +struct au_vfsub_mkdir_args {
  28563. + int *errp;
  28564. + struct inode *dir;
  28565. + struct path *path;
  28566. + int mode;
  28567. +};
  28568. +
  28569. +static void au_call_vfsub_mkdir(void *args)
  28570. +{
  28571. + struct au_vfsub_mkdir_args *a = args;
  28572. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  28573. +}
  28574. +
  28575. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  28576. +{
  28577. + int err, do_sio, wkq_err;
  28578. +
  28579. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  28580. + if (!do_sio) {
  28581. + lockdep_off();
  28582. + err = vfsub_mkdir(dir, path, mode);
  28583. + lockdep_on();
  28584. + } else {
  28585. + struct au_vfsub_mkdir_args args = {
  28586. + .errp = &err,
  28587. + .dir = dir,
  28588. + .path = path,
  28589. + .mode = mode
  28590. + };
  28591. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  28592. + if (unlikely(wkq_err))
  28593. + err = wkq_err;
  28594. + }
  28595. +
  28596. + return err;
  28597. +}
  28598. +
  28599. +struct au_vfsub_rmdir_args {
  28600. + int *errp;
  28601. + struct inode *dir;
  28602. + struct path *path;
  28603. +};
  28604. +
  28605. +static void au_call_vfsub_rmdir(void *args)
  28606. +{
  28607. + struct au_vfsub_rmdir_args *a = args;
  28608. + *a->errp = vfsub_rmdir(a->dir, a->path);
  28609. +}
  28610. +
  28611. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  28612. +{
  28613. + int err, do_sio, wkq_err;
  28614. +
  28615. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  28616. + if (!do_sio) {
  28617. + lockdep_off();
  28618. + err = vfsub_rmdir(dir, path);
  28619. + lockdep_on();
  28620. + } else {
  28621. + struct au_vfsub_rmdir_args args = {
  28622. + .errp = &err,
  28623. + .dir = dir,
  28624. + .path = path
  28625. + };
  28626. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  28627. + if (unlikely(wkq_err))
  28628. + err = wkq_err;
  28629. + }
  28630. +
  28631. + return err;
  28632. +}
  28633. +
  28634. +/* ---------------------------------------------------------------------- */
  28635. +
  28636. +struct notify_change_args {
  28637. + int *errp;
  28638. + struct path *path;
  28639. + struct iattr *ia;
  28640. + struct inode **delegated_inode;
  28641. +};
  28642. +
  28643. +static void call_notify_change(void *args)
  28644. +{
  28645. + struct notify_change_args *a = args;
  28646. + struct inode *h_inode;
  28647. +
  28648. + h_inode = a->path->dentry->d_inode;
  28649. + IMustLock(h_inode);
  28650. +
  28651. + *a->errp = -EPERM;
  28652. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  28653. + lockdep_off();
  28654. + *a->errp = notify_change(a->path->dentry, a->ia,
  28655. + a->delegated_inode);
  28656. + lockdep_on();
  28657. + if (!*a->errp)
  28658. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  28659. + }
  28660. + AuTraceErr(*a->errp);
  28661. +}
  28662. +
  28663. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  28664. + struct inode **delegated_inode)
  28665. +{
  28666. + int err;
  28667. + struct notify_change_args args = {
  28668. + .errp = &err,
  28669. + .path = path,
  28670. + .ia = ia,
  28671. + .delegated_inode = delegated_inode
  28672. + };
  28673. +
  28674. + call_notify_change(&args);
  28675. +
  28676. + return err;
  28677. +}
  28678. +
  28679. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  28680. + struct inode **delegated_inode)
  28681. +{
  28682. + int err, wkq_err;
  28683. + struct notify_change_args args = {
  28684. + .errp = &err,
  28685. + .path = path,
  28686. + .ia = ia,
  28687. + .delegated_inode = delegated_inode
  28688. + };
  28689. +
  28690. + wkq_err = au_wkq_wait(call_notify_change, &args);
  28691. + if (unlikely(wkq_err))
  28692. + err = wkq_err;
  28693. +
  28694. + return err;
  28695. +}
  28696. +
  28697. +/* ---------------------------------------------------------------------- */
  28698. +
  28699. +struct unlink_args {
  28700. + int *errp;
  28701. + struct inode *dir;
  28702. + struct path *path;
  28703. + struct inode **delegated_inode;
  28704. +};
  28705. +
  28706. +static void call_unlink(void *args)
  28707. +{
  28708. + struct unlink_args *a = args;
  28709. + struct dentry *d = a->path->dentry;
  28710. + struct inode *h_inode;
  28711. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  28712. + && au_dcount(d) == 1);
  28713. +
  28714. + IMustLock(a->dir);
  28715. +
  28716. + a->path->dentry = d->d_parent;
  28717. + *a->errp = security_path_unlink(a->path, d);
  28718. + a->path->dentry = d;
  28719. + if (unlikely(*a->errp))
  28720. + return;
  28721. +
  28722. + if (!stop_sillyrename)
  28723. + dget(d);
  28724. + h_inode = d->d_inode;
  28725. + if (h_inode)
  28726. + ihold(h_inode);
  28727. +
  28728. + lockdep_off();
  28729. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  28730. + lockdep_on();
  28731. + if (!*a->errp) {
  28732. + struct path tmp = {
  28733. + .dentry = d->d_parent,
  28734. + .mnt = a->path->mnt
  28735. + };
  28736. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  28737. + }
  28738. +
  28739. + if (!stop_sillyrename)
  28740. + dput(d);
  28741. + if (h_inode)
  28742. + iput(h_inode);
  28743. +
  28744. + AuTraceErr(*a->errp);
  28745. +}
  28746. +
  28747. +/*
  28748. + * @dir: must be locked.
  28749. + * @dentry: target dentry.
  28750. + */
  28751. +int vfsub_unlink(struct inode *dir, struct path *path,
  28752. + struct inode **delegated_inode, int force)
  28753. +{
  28754. + int err;
  28755. + struct unlink_args args = {
  28756. + .errp = &err,
  28757. + .dir = dir,
  28758. + .path = path,
  28759. + .delegated_inode = delegated_inode
  28760. + };
  28761. +
  28762. + if (!force)
  28763. + call_unlink(&args);
  28764. + else {
  28765. + int wkq_err;
  28766. +
  28767. + wkq_err = au_wkq_wait(call_unlink, &args);
  28768. + if (unlikely(wkq_err))
  28769. + err = wkq_err;
  28770. + }
  28771. +
  28772. + return err;
  28773. +}
  28774. diff -Nur linux-4.0.4.orig/fs/aufs/vfsub.h linux-4.0.4/fs/aufs/vfsub.h
  28775. --- linux-4.0.4.orig/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
  28776. +++ linux-4.0.4/fs/aufs/vfsub.h 2015-05-30 22:11:31.000000000 +0200
  28777. @@ -0,0 +1,273 @@
  28778. +/*
  28779. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  28780. + */
  28781. +
  28782. +/*
  28783. + * sub-routines for VFS
  28784. + */
  28785. +
  28786. +#ifndef __AUFS_VFSUB_H__
  28787. +#define __AUFS_VFSUB_H__
  28788. +
  28789. +#ifdef __KERNEL__
  28790. +
  28791. +#include <linux/fs.h>
  28792. +#include <linux/mount.h>
  28793. +#include <linux/xattr.h>
  28794. +#include "debug.h"
  28795. +
  28796. +/* copied from linux/fs/internal.h */
  28797. +/* todo: BAD approach!! */
  28798. +extern void __mnt_drop_write(struct vfsmount *);
  28799. +extern spinlock_t inode_sb_list_lock;
  28800. +extern int open_check_o_direct(struct file *f);
  28801. +
  28802. +/* ---------------------------------------------------------------------- */
  28803. +
  28804. +/* lock subclass for lower inode */
  28805. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  28806. +/* reduce? gave up. */
  28807. +enum {
  28808. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  28809. + AuLsc_I_PARENT, /* lower inode, parent first */
  28810. + AuLsc_I_PARENT2, /* copyup dirs */
  28811. + AuLsc_I_PARENT3, /* copyup wh */
  28812. + AuLsc_I_CHILD,
  28813. + AuLsc_I_CHILD2,
  28814. + AuLsc_I_End
  28815. +};
  28816. +
  28817. +/* to debug easier, do not make them inlined functions */
  28818. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  28819. +#define IMustLock(i) MtxMustLock(&(i)->i_mutex)
  28820. +
  28821. +/* ---------------------------------------------------------------------- */
  28822. +
  28823. +static inline void vfsub_drop_nlink(struct inode *inode)
  28824. +{
  28825. + AuDebugOn(!inode->i_nlink);
  28826. + drop_nlink(inode);
  28827. +}
  28828. +
  28829. +static inline void vfsub_dead_dir(struct inode *inode)
  28830. +{
  28831. + AuDebugOn(!S_ISDIR(inode->i_mode));
  28832. + inode->i_flags |= S_DEAD;
  28833. + clear_nlink(inode);
  28834. +}
  28835. +
  28836. +static inline int vfsub_native_ro(struct inode *inode)
  28837. +{
  28838. + return (inode->i_sb->s_flags & MS_RDONLY)
  28839. + || IS_RDONLY(inode)
  28840. + /* || IS_APPEND(inode) */
  28841. + || IS_IMMUTABLE(inode);
  28842. +}
  28843. +
  28844. +/* ---------------------------------------------------------------------- */
  28845. +
  28846. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  28847. +struct file *vfsub_dentry_open(struct path *path, int flags);
  28848. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  28849. +struct vfsub_aopen_args {
  28850. + struct file *file;
  28851. + unsigned int open_flag;
  28852. + umode_t create_mode;
  28853. + int *opened;
  28854. +};
  28855. +struct au_branch;
  28856. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  28857. + struct vfsub_aopen_args *args, struct au_branch *br);
  28858. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  28859. +
  28860. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  28861. + int len);
  28862. +
  28863. +struct vfsub_lkup_one_args {
  28864. + struct dentry **errp;
  28865. + struct qstr *name;
  28866. + struct dentry *parent;
  28867. +};
  28868. +
  28869. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  28870. + struct dentry *parent)
  28871. +{
  28872. + return vfsub_lookup_one_len(name->name, parent, name->len);
  28873. +}
  28874. +
  28875. +void vfsub_call_lkup_one(void *args);
  28876. +
  28877. +/* ---------------------------------------------------------------------- */
  28878. +
  28879. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  28880. +{
  28881. + int err;
  28882. +
  28883. + lockdep_off();
  28884. + err = mnt_want_write(mnt);
  28885. + lockdep_on();
  28886. + return err;
  28887. +}
  28888. +
  28889. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  28890. +{
  28891. + lockdep_off();
  28892. + mnt_drop_write(mnt);
  28893. + lockdep_on();
  28894. +}
  28895. +
  28896. +#if 0 /* reserved */
  28897. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  28898. +{
  28899. + lockdep_off();
  28900. + mnt_drop_write_file(file);
  28901. + lockdep_on();
  28902. +}
  28903. +#endif
  28904. +
  28905. +/* ---------------------------------------------------------------------- */
  28906. +
  28907. +struct au_hinode;
  28908. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  28909. + struct dentry *d2, struct au_hinode *hdir2);
  28910. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  28911. + struct dentry *d2, struct au_hinode *hdir2);
  28912. +
  28913. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  28914. + bool want_excl);
  28915. +int vfsub_symlink(struct inode *dir, struct path *path,
  28916. + const char *symname);
  28917. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  28918. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  28919. + struct path *path, struct inode **delegated_inode);
  28920. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  28921. + struct inode *hdir, struct path *path,
  28922. + struct inode **delegated_inode);
  28923. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  28924. +int vfsub_rmdir(struct inode *dir, struct path *path);
  28925. +
  28926. +/* ---------------------------------------------------------------------- */
  28927. +
  28928. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  28929. + loff_t *ppos);
  28930. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  28931. + loff_t *ppos);
  28932. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  28933. + loff_t *ppos);
  28934. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  28935. + loff_t *ppos);
  28936. +int vfsub_flush(struct file *file, fl_owner_t id);
  28937. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  28938. +
  28939. +static inline loff_t vfsub_f_size_read(struct file *file)
  28940. +{
  28941. + return i_size_read(file_inode(file));
  28942. +}
  28943. +
  28944. +static inline unsigned int vfsub_file_flags(struct file *file)
  28945. +{
  28946. + unsigned int flags;
  28947. +
  28948. + spin_lock(&file->f_lock);
  28949. + flags = file->f_flags;
  28950. + spin_unlock(&file->f_lock);
  28951. +
  28952. + return flags;
  28953. +}
  28954. +
  28955. +#if 0 /* reserved */
  28956. +static inline void vfsub_file_accessed(struct file *h_file)
  28957. +{
  28958. + file_accessed(h_file);
  28959. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  28960. +}
  28961. +#endif
  28962. +
  28963. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  28964. + struct dentry *h_dentry)
  28965. +{
  28966. + struct path h_path = {
  28967. + .dentry = h_dentry,
  28968. + .mnt = h_mnt
  28969. + };
  28970. + touch_atime(&h_path);
  28971. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  28972. +}
  28973. +
  28974. +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
  28975. + int flags)
  28976. +{
  28977. + return generic_update_time(h_inode, ts, flags);
  28978. + /* no vfsub_update_h_iattr() since we don't have struct path */
  28979. +}
  28980. +
  28981. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  28982. + struct pipe_inode_info *pipe, size_t len,
  28983. + unsigned int flags);
  28984. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  28985. + loff_t *ppos, size_t len, unsigned int flags);
  28986. +
  28987. +static inline long vfsub_truncate(struct path *path, loff_t length)
  28988. +{
  28989. + long err;
  28990. +
  28991. + lockdep_off();
  28992. + err = vfs_truncate(path, length);
  28993. + lockdep_on();
  28994. + return err;
  28995. +}
  28996. +
  28997. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  28998. + struct file *h_file);
  28999. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  29000. +
  29001. +/* ---------------------------------------------------------------------- */
  29002. +
  29003. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  29004. +{
  29005. + loff_t err;
  29006. +
  29007. + lockdep_off();
  29008. + err = vfs_llseek(file, offset, origin);
  29009. + lockdep_on();
  29010. + return err;
  29011. +}
  29012. +
  29013. +/* ---------------------------------------------------------------------- */
  29014. +
  29015. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  29016. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  29017. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  29018. + struct inode **delegated_inode);
  29019. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  29020. + struct inode **delegated_inode);
  29021. +int vfsub_unlink(struct inode *dir, struct path *path,
  29022. + struct inode **delegated_inode, int force);
  29023. +
  29024. +/* ---------------------------------------------------------------------- */
  29025. +
  29026. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  29027. + const void *value, size_t size, int flags)
  29028. +{
  29029. + int err;
  29030. +
  29031. + lockdep_off();
  29032. + err = vfs_setxattr(dentry, name, value, size, flags);
  29033. + lockdep_on();
  29034. +
  29035. + return err;
  29036. +}
  29037. +
  29038. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  29039. +{
  29040. + int err;
  29041. +
  29042. + lockdep_off();
  29043. + err = vfs_removexattr(dentry, name);
  29044. + lockdep_on();
  29045. +
  29046. + return err;
  29047. +}
  29048. +
  29049. +#endif /* __KERNEL__ */
  29050. +#endif /* __AUFS_VFSUB_H__ */
  29051. diff -Nur linux-4.0.4.orig/fs/aufs/wbr_policy.c linux-4.0.4/fs/aufs/wbr_policy.c
  29052. --- linux-4.0.4.orig/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
  29053. +++ linux-4.0.4/fs/aufs/wbr_policy.c 2015-05-30 22:11:30.000000000 +0200
  29054. @@ -0,0 +1,752 @@
  29055. +/*
  29056. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  29057. + */
  29058. +
  29059. +/*
  29060. + * policies for selecting one among multiple writable branches
  29061. + */
  29062. +
  29063. +#include <linux/statfs.h>
  29064. +#include "aufs.h"
  29065. +
  29066. +/* subset of cpup_attr() */
  29067. +static noinline_for_stack
  29068. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  29069. +{
  29070. + int err, sbits;
  29071. + struct iattr ia;
  29072. + struct inode *h_isrc;
  29073. +
  29074. + h_isrc = h_src->d_inode;
  29075. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  29076. + ia.ia_mode = h_isrc->i_mode;
  29077. + ia.ia_uid = h_isrc->i_uid;
  29078. + ia.ia_gid = h_isrc->i_gid;
  29079. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  29080. + au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
  29081. + /* no delegation since it is just created */
  29082. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  29083. +
  29084. + /* is this nfs only? */
  29085. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  29086. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  29087. + ia.ia_mode = h_isrc->i_mode;
  29088. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  29089. + }
  29090. +
  29091. + return err;
  29092. +}
  29093. +
  29094. +#define AuCpdown_PARENT_OPQ 1
  29095. +#define AuCpdown_WHED (1 << 1)
  29096. +#define AuCpdown_MADE_DIR (1 << 2)
  29097. +#define AuCpdown_DIROPQ (1 << 3)
  29098. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  29099. +#define au_fset_cpdown(flags, name) \
  29100. + do { (flags) |= AuCpdown_##name; } while (0)
  29101. +#define au_fclr_cpdown(flags, name) \
  29102. + do { (flags) &= ~AuCpdown_##name; } while (0)
  29103. +
  29104. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  29105. + unsigned int *flags)
  29106. +{
  29107. + int err;
  29108. + struct dentry *opq_dentry;
  29109. +
  29110. + opq_dentry = au_diropq_create(dentry, bdst);
  29111. + err = PTR_ERR(opq_dentry);
  29112. + if (IS_ERR(opq_dentry))
  29113. + goto out;
  29114. + dput(opq_dentry);
  29115. + au_fset_cpdown(*flags, DIROPQ);
  29116. +
  29117. +out:
  29118. + return err;
  29119. +}
  29120. +
  29121. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  29122. + struct inode *dir, aufs_bindex_t bdst)
  29123. +{
  29124. + int err;
  29125. + struct path h_path;
  29126. + struct au_branch *br;
  29127. +
  29128. + br = au_sbr(dentry->d_sb, bdst);
  29129. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  29130. + err = PTR_ERR(h_path.dentry);
  29131. + if (IS_ERR(h_path.dentry))
  29132. + goto out;
  29133. +
  29134. + err = 0;
  29135. + if (h_path.dentry->d_inode) {
  29136. + h_path.mnt = au_br_mnt(br);
  29137. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  29138. + dentry);
  29139. + }
  29140. + dput(h_path.dentry);
  29141. +
  29142. +out:
  29143. + return err;
  29144. +}
  29145. +
  29146. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  29147. + struct au_pin *pin,
  29148. + struct dentry *h_parent, void *arg)
  29149. +{
  29150. + int err, rerr;
  29151. + aufs_bindex_t bopq, bstart;
  29152. + struct path h_path;
  29153. + struct dentry *parent;
  29154. + struct inode *h_dir, *h_inode, *inode, *dir;
  29155. + unsigned int *flags = arg;
  29156. +
  29157. + bstart = au_dbstart(dentry);
  29158. + /* dentry is di-locked */
  29159. + parent = dget_parent(dentry);
  29160. + dir = parent->d_inode;
  29161. + h_dir = h_parent->d_inode;
  29162. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  29163. + IMustLock(h_dir);
  29164. +
  29165. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  29166. + if (unlikely(err < 0))
  29167. + goto out;
  29168. + h_path.dentry = au_h_dptr(dentry, bdst);
  29169. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  29170. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
  29171. + S_IRWXU | S_IRUGO | S_IXUGO);
  29172. + if (unlikely(err))
  29173. + goto out_put;
  29174. + au_fset_cpdown(*flags, MADE_DIR);
  29175. +
  29176. + bopq = au_dbdiropq(dentry);
  29177. + au_fclr_cpdown(*flags, WHED);
  29178. + au_fclr_cpdown(*flags, DIROPQ);
  29179. + if (au_dbwh(dentry) == bdst)
  29180. + au_fset_cpdown(*flags, WHED);
  29181. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  29182. + au_fset_cpdown(*flags, PARENT_OPQ);
  29183. + h_inode = h_path.dentry->d_inode;
  29184. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  29185. + if (au_ftest_cpdown(*flags, WHED)) {
  29186. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  29187. + if (unlikely(err)) {
  29188. + mutex_unlock(&h_inode->i_mutex);
  29189. + goto out_dir;
  29190. + }
  29191. + }
  29192. +
  29193. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
  29194. + mutex_unlock(&h_inode->i_mutex);
  29195. + if (unlikely(err))
  29196. + goto out_opq;
  29197. +
  29198. + if (au_ftest_cpdown(*flags, WHED)) {
  29199. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  29200. + if (unlikely(err))
  29201. + goto out_opq;
  29202. + }
  29203. +
  29204. + inode = dentry->d_inode;
  29205. + if (au_ibend(inode) < bdst)
  29206. + au_set_ibend(inode, bdst);
  29207. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  29208. + au_hi_flags(inode, /*isdir*/1));
  29209. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  29210. + goto out; /* success */
  29211. +
  29212. + /* revert */
  29213. +out_opq:
  29214. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  29215. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  29216. + rerr = au_diropq_remove(dentry, bdst);
  29217. + mutex_unlock(&h_inode->i_mutex);
  29218. + if (unlikely(rerr)) {
  29219. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  29220. + dentry, bdst, rerr);
  29221. + err = -EIO;
  29222. + goto out;
  29223. + }
  29224. + }
  29225. +out_dir:
  29226. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  29227. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  29228. + if (unlikely(rerr)) {
  29229. + AuIOErr("failed removing %pd b%d (%d)\n",
  29230. + dentry, bdst, rerr);
  29231. + err = -EIO;
  29232. + }
  29233. + }
  29234. +out_put:
  29235. + au_set_h_dptr(dentry, bdst, NULL);
  29236. + if (au_dbend(dentry) == bdst)
  29237. + au_update_dbend(dentry);
  29238. +out:
  29239. + dput(parent);
  29240. + return err;
  29241. +}
  29242. +
  29243. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  29244. +{
  29245. + int err;
  29246. + unsigned int flags;
  29247. +
  29248. + flags = 0;
  29249. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  29250. +
  29251. + return err;
  29252. +}
  29253. +
  29254. +/* ---------------------------------------------------------------------- */
  29255. +
  29256. +/* policies for create */
  29257. +
  29258. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  29259. +{
  29260. + int err, i, j, ndentry;
  29261. + aufs_bindex_t bopq;
  29262. + struct au_dcsub_pages dpages;
  29263. + struct au_dpage *dpage;
  29264. + struct dentry **dentries, *parent, *d;
  29265. +
  29266. + err = au_dpages_init(&dpages, GFP_NOFS);
  29267. + if (unlikely(err))
  29268. + goto out;
  29269. + parent = dget_parent(dentry);
  29270. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  29271. + if (unlikely(err))
  29272. + goto out_free;
  29273. +
  29274. + err = bindex;
  29275. + for (i = 0; i < dpages.ndpage; i++) {
  29276. + dpage = dpages.dpages + i;
  29277. + dentries = dpage->dentries;
  29278. + ndentry = dpage->ndentry;
  29279. + for (j = 0; j < ndentry; j++) {
  29280. + d = dentries[j];
  29281. + di_read_lock_parent2(d, !AuLock_IR);
  29282. + bopq = au_dbdiropq(d);
  29283. + di_read_unlock(d, !AuLock_IR);
  29284. + if (bopq >= 0 && bopq < err)
  29285. + err = bopq;
  29286. + }
  29287. + }
  29288. +
  29289. +out_free:
  29290. + dput(parent);
  29291. + au_dpages_free(&dpages);
  29292. +out:
  29293. + return err;
  29294. +}
  29295. +
  29296. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  29297. +{
  29298. + for (; bindex >= 0; bindex--)
  29299. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  29300. + return bindex;
  29301. + return -EROFS;
  29302. +}
  29303. +
  29304. +/* top down parent */
  29305. +static int au_wbr_create_tdp(struct dentry *dentry,
  29306. + unsigned int flags __maybe_unused)
  29307. +{
  29308. + int err;
  29309. + aufs_bindex_t bstart, bindex;
  29310. + struct super_block *sb;
  29311. + struct dentry *parent, *h_parent;
  29312. +
  29313. + sb = dentry->d_sb;
  29314. + bstart = au_dbstart(dentry);
  29315. + err = bstart;
  29316. + if (!au_br_rdonly(au_sbr(sb, bstart)))
  29317. + goto out;
  29318. +
  29319. + err = -EROFS;
  29320. + parent = dget_parent(dentry);
  29321. + for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
  29322. + h_parent = au_h_dptr(parent, bindex);
  29323. + if (!h_parent || !h_parent->d_inode)
  29324. + continue;
  29325. +
  29326. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  29327. + err = bindex;
  29328. + break;
  29329. + }
  29330. + }
  29331. + dput(parent);
  29332. +
  29333. + /* bottom up here */
  29334. + if (unlikely(err < 0)) {
  29335. + err = au_wbr_bu(sb, bstart - 1);
  29336. + if (err >= 0)
  29337. + err = au_wbr_nonopq(dentry, err);
  29338. + }
  29339. +
  29340. +out:
  29341. + AuDbg("b%d\n", err);
  29342. + return err;
  29343. +}
  29344. +
  29345. +/* ---------------------------------------------------------------------- */
  29346. +
  29347. +/* an exception for the policy other than tdp */
  29348. +static int au_wbr_create_exp(struct dentry *dentry)
  29349. +{
  29350. + int err;
  29351. + aufs_bindex_t bwh, bdiropq;
  29352. + struct dentry *parent;
  29353. +
  29354. + err = -1;
  29355. + bwh = au_dbwh(dentry);
  29356. + parent = dget_parent(dentry);
  29357. + bdiropq = au_dbdiropq(parent);
  29358. + if (bwh >= 0) {
  29359. + if (bdiropq >= 0)
  29360. + err = min(bdiropq, bwh);
  29361. + else
  29362. + err = bwh;
  29363. + AuDbg("%d\n", err);
  29364. + } else if (bdiropq >= 0) {
  29365. + err = bdiropq;
  29366. + AuDbg("%d\n", err);
  29367. + }
  29368. + dput(parent);
  29369. +
  29370. + if (err >= 0)
  29371. + err = au_wbr_nonopq(dentry, err);
  29372. +
  29373. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  29374. + err = -1;
  29375. +
  29376. + AuDbg("%d\n", err);
  29377. + return err;
  29378. +}
  29379. +
  29380. +/* ---------------------------------------------------------------------- */
  29381. +
  29382. +/* round robin */
  29383. +static int au_wbr_create_init_rr(struct super_block *sb)
  29384. +{
  29385. + int err;
  29386. +
  29387. + err = au_wbr_bu(sb, au_sbend(sb));
  29388. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  29389. + /* smp_mb(); */
  29390. +
  29391. + AuDbg("b%d\n", err);
  29392. + return err;
  29393. +}
  29394. +
  29395. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  29396. +{
  29397. + int err, nbr;
  29398. + unsigned int u;
  29399. + aufs_bindex_t bindex, bend;
  29400. + struct super_block *sb;
  29401. + atomic_t *next;
  29402. +
  29403. + err = au_wbr_create_exp(dentry);
  29404. + if (err >= 0)
  29405. + goto out;
  29406. +
  29407. + sb = dentry->d_sb;
  29408. + next = &au_sbi(sb)->si_wbr_rr_next;
  29409. + bend = au_sbend(sb);
  29410. + nbr = bend + 1;
  29411. + for (bindex = 0; bindex <= bend; bindex++) {
  29412. + if (!au_ftest_wbr(flags, DIR)) {
  29413. + err = atomic_dec_return(next) + 1;
  29414. + /* modulo for 0 is meaningless */
  29415. + if (unlikely(!err))
  29416. + err = atomic_dec_return(next) + 1;
  29417. + } else
  29418. + err = atomic_read(next);
  29419. + AuDbg("%d\n", err);
  29420. + u = err;
  29421. + err = u % nbr;
  29422. + AuDbg("%d\n", err);
  29423. + if (!au_br_rdonly(au_sbr(sb, err)))
  29424. + break;
  29425. + err = -EROFS;
  29426. + }
  29427. +
  29428. + if (err >= 0)
  29429. + err = au_wbr_nonopq(dentry, err);
  29430. +
  29431. +out:
  29432. + AuDbg("%d\n", err);
  29433. + return err;
  29434. +}
  29435. +
  29436. +/* ---------------------------------------------------------------------- */
  29437. +
  29438. +/* most free space */
  29439. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  29440. +{
  29441. + struct super_block *sb;
  29442. + struct au_branch *br;
  29443. + struct au_wbr_mfs *mfs;
  29444. + struct dentry *h_parent;
  29445. + aufs_bindex_t bindex, bend;
  29446. + int err;
  29447. + unsigned long long b, bavail;
  29448. + struct path h_path;
  29449. + /* reduce the stack usage */
  29450. + struct kstatfs *st;
  29451. +
  29452. + st = kmalloc(sizeof(*st), GFP_NOFS);
  29453. + if (unlikely(!st)) {
  29454. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  29455. + return;
  29456. + }
  29457. +
  29458. + bavail = 0;
  29459. + sb = dentry->d_sb;
  29460. + mfs = &au_sbi(sb)->si_wbr_mfs;
  29461. + MtxMustLock(&mfs->mfs_lock);
  29462. + mfs->mfs_bindex = -EROFS;
  29463. + mfs->mfsrr_bytes = 0;
  29464. + if (!parent) {
  29465. + bindex = 0;
  29466. + bend = au_sbend(sb);
  29467. + } else {
  29468. + bindex = au_dbstart(parent);
  29469. + bend = au_dbtaildir(parent);
  29470. + }
  29471. +
  29472. + for (; bindex <= bend; bindex++) {
  29473. + if (parent) {
  29474. + h_parent = au_h_dptr(parent, bindex);
  29475. + if (!h_parent || !h_parent->d_inode)
  29476. + continue;
  29477. + }
  29478. + br = au_sbr(sb, bindex);
  29479. + if (au_br_rdonly(br))
  29480. + continue;
  29481. +
  29482. + /* sb->s_root for NFS is unreliable */
  29483. + h_path.mnt = au_br_mnt(br);
  29484. + h_path.dentry = h_path.mnt->mnt_root;
  29485. + err = vfs_statfs(&h_path, st);
  29486. + if (unlikely(err)) {
  29487. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  29488. + continue;
  29489. + }
  29490. +
  29491. + /* when the available size is equal, select the lower one */
  29492. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  29493. + || sizeof(b) < sizeof(st->f_bsize));
  29494. + b = st->f_bavail * st->f_bsize;
  29495. + br->br_wbr->wbr_bytes = b;
  29496. + if (b >= bavail) {
  29497. + bavail = b;
  29498. + mfs->mfs_bindex = bindex;
  29499. + mfs->mfs_jiffy = jiffies;
  29500. + }
  29501. + }
  29502. +
  29503. + mfs->mfsrr_bytes = bavail;
  29504. + AuDbg("b%d\n", mfs->mfs_bindex);
  29505. + kfree(st);
  29506. +}
  29507. +
  29508. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  29509. +{
  29510. + int err;
  29511. + struct dentry *parent;
  29512. + struct super_block *sb;
  29513. + struct au_wbr_mfs *mfs;
  29514. +
  29515. + err = au_wbr_create_exp(dentry);
  29516. + if (err >= 0)
  29517. + goto out;
  29518. +
  29519. + sb = dentry->d_sb;
  29520. + parent = NULL;
  29521. + if (au_ftest_wbr(flags, PARENT))
  29522. + parent = dget_parent(dentry);
  29523. + mfs = &au_sbi(sb)->si_wbr_mfs;
  29524. + mutex_lock(&mfs->mfs_lock);
  29525. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  29526. + || mfs->mfs_bindex < 0
  29527. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  29528. + au_mfs(dentry, parent);
  29529. + mutex_unlock(&mfs->mfs_lock);
  29530. + err = mfs->mfs_bindex;
  29531. + dput(parent);
  29532. +
  29533. + if (err >= 0)
  29534. + err = au_wbr_nonopq(dentry, err);
  29535. +
  29536. +out:
  29537. + AuDbg("b%d\n", err);
  29538. + return err;
  29539. +}
  29540. +
  29541. +static int au_wbr_create_init_mfs(struct super_block *sb)
  29542. +{
  29543. + struct au_wbr_mfs *mfs;
  29544. +
  29545. + mfs = &au_sbi(sb)->si_wbr_mfs;
  29546. + mutex_init(&mfs->mfs_lock);
  29547. + mfs->mfs_jiffy = 0;
  29548. + mfs->mfs_bindex = -EROFS;
  29549. +
  29550. + return 0;
  29551. +}
  29552. +
  29553. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  29554. +{
  29555. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  29556. + return 0;
  29557. +}
  29558. +
  29559. +/* ---------------------------------------------------------------------- */
  29560. +
  29561. +/* most free space and then round robin */
  29562. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  29563. +{
  29564. + int err;
  29565. + struct au_wbr_mfs *mfs;
  29566. +
  29567. + err = au_wbr_create_mfs(dentry, flags);
  29568. + if (err >= 0) {
  29569. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  29570. + mutex_lock(&mfs->mfs_lock);
  29571. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  29572. + err = au_wbr_create_rr(dentry, flags);
  29573. + mutex_unlock(&mfs->mfs_lock);
  29574. + }
  29575. +
  29576. + AuDbg("b%d\n", err);
  29577. + return err;
  29578. +}
  29579. +
  29580. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  29581. +{
  29582. + int err;
  29583. +
  29584. + au_wbr_create_init_mfs(sb); /* ignore */
  29585. + err = au_wbr_create_init_rr(sb);
  29586. +
  29587. + return err;
  29588. +}
  29589. +
  29590. +/* ---------------------------------------------------------------------- */
  29591. +
  29592. +/* top down parent and most free space */
  29593. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  29594. +{
  29595. + int err, e2;
  29596. + unsigned long long b;
  29597. + aufs_bindex_t bindex, bstart, bend;
  29598. + struct super_block *sb;
  29599. + struct dentry *parent, *h_parent;
  29600. + struct au_branch *br;
  29601. +
  29602. + err = au_wbr_create_tdp(dentry, flags);
  29603. + if (unlikely(err < 0))
  29604. + goto out;
  29605. + parent = dget_parent(dentry);
  29606. + bstart = au_dbstart(parent);
  29607. + bend = au_dbtaildir(parent);
  29608. + if (bstart == bend)
  29609. + goto out_parent; /* success */
  29610. +
  29611. + e2 = au_wbr_create_mfs(dentry, flags);
  29612. + if (e2 < 0)
  29613. + goto out_parent; /* success */
  29614. +
  29615. + /* when the available size is equal, select upper one */
  29616. + sb = dentry->d_sb;
  29617. + br = au_sbr(sb, err);
  29618. + b = br->br_wbr->wbr_bytes;
  29619. + AuDbg("b%d, %llu\n", err, b);
  29620. +
  29621. + for (bindex = bstart; bindex <= bend; bindex++) {
  29622. + h_parent = au_h_dptr(parent, bindex);
  29623. + if (!h_parent || !h_parent->d_inode)
  29624. + continue;
  29625. +
  29626. + br = au_sbr(sb, bindex);
  29627. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  29628. + b = br->br_wbr->wbr_bytes;
  29629. + err = bindex;
  29630. + AuDbg("b%d, %llu\n", err, b);
  29631. + }
  29632. + }
  29633. +
  29634. + if (err >= 0)
  29635. + err = au_wbr_nonopq(dentry, err);
  29636. +
  29637. +out_parent:
  29638. + dput(parent);
  29639. +out:
  29640. + AuDbg("b%d\n", err);
  29641. + return err;
  29642. +}
  29643. +
  29644. +/* ---------------------------------------------------------------------- */
  29645. +
  29646. +/*
  29647. + * - top down parent
  29648. + * - most free space with parent
  29649. + * - most free space round-robin regardless parent
  29650. + */
  29651. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  29652. +{
  29653. + int err;
  29654. + unsigned long long watermark;
  29655. + struct super_block *sb;
  29656. + struct au_branch *br;
  29657. + struct au_wbr_mfs *mfs;
  29658. +
  29659. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  29660. + if (unlikely(err < 0))
  29661. + goto out;
  29662. +
  29663. + sb = dentry->d_sb;
  29664. + br = au_sbr(sb, err);
  29665. + mfs = &au_sbi(sb)->si_wbr_mfs;
  29666. + mutex_lock(&mfs->mfs_lock);
  29667. + watermark = mfs->mfsrr_watermark;
  29668. + mutex_unlock(&mfs->mfs_lock);
  29669. + if (br->br_wbr->wbr_bytes < watermark)
  29670. + /* regardless the parent dir */
  29671. + err = au_wbr_create_mfsrr(dentry, flags);
  29672. +
  29673. +out:
  29674. + AuDbg("b%d\n", err);
  29675. + return err;
  29676. +}
  29677. +
  29678. +/* ---------------------------------------------------------------------- */
  29679. +
  29680. +/* policies for copyup */
  29681. +
  29682. +/* top down parent */
  29683. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  29684. +{
  29685. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  29686. +}
  29687. +
  29688. +/* bottom up parent */
  29689. +static int au_wbr_copyup_bup(struct dentry *dentry)
  29690. +{
  29691. + int err;
  29692. + aufs_bindex_t bindex, bstart;
  29693. + struct dentry *parent, *h_parent;
  29694. + struct super_block *sb;
  29695. +
  29696. + err = -EROFS;
  29697. + sb = dentry->d_sb;
  29698. + parent = dget_parent(dentry);
  29699. + bstart = au_dbstart(parent);
  29700. + for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
  29701. + h_parent = au_h_dptr(parent, bindex);
  29702. + if (!h_parent || !h_parent->d_inode)
  29703. + continue;
  29704. +
  29705. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  29706. + err = bindex;
  29707. + break;
  29708. + }
  29709. + }
  29710. + dput(parent);
  29711. +
  29712. + /* bottom up here */
  29713. + if (unlikely(err < 0))
  29714. + err = au_wbr_bu(sb, bstart - 1);
  29715. +
  29716. + AuDbg("b%d\n", err);
  29717. + return err;
  29718. +}
  29719. +
  29720. +/* bottom up */
  29721. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
  29722. +{
  29723. + int err;
  29724. +
  29725. + err = au_wbr_bu(dentry->d_sb, bstart);
  29726. + AuDbg("b%d\n", err);
  29727. + if (err > bstart)
  29728. + err = au_wbr_nonopq(dentry, err);
  29729. +
  29730. + AuDbg("b%d\n", err);
  29731. + return err;
  29732. +}
  29733. +
  29734. +static int au_wbr_copyup_bu(struct dentry *dentry)
  29735. +{
  29736. + int err;
  29737. + aufs_bindex_t bstart;
  29738. +
  29739. + bstart = au_dbstart(dentry);
  29740. + err = au_wbr_do_copyup_bu(dentry, bstart);
  29741. + return err;
  29742. +}
  29743. +
  29744. +/* ---------------------------------------------------------------------- */
  29745. +
  29746. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  29747. + [AuWbrCopyup_TDP] = {
  29748. + .copyup = au_wbr_copyup_tdp
  29749. + },
  29750. + [AuWbrCopyup_BUP] = {
  29751. + .copyup = au_wbr_copyup_bup
  29752. + },
  29753. + [AuWbrCopyup_BU] = {
  29754. + .copyup = au_wbr_copyup_bu
  29755. + }
  29756. +};
  29757. +
  29758. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  29759. + [AuWbrCreate_TDP] = {
  29760. + .create = au_wbr_create_tdp
  29761. + },
  29762. + [AuWbrCreate_RR] = {
  29763. + .create = au_wbr_create_rr,
  29764. + .init = au_wbr_create_init_rr
  29765. + },
  29766. + [AuWbrCreate_MFS] = {
  29767. + .create = au_wbr_create_mfs,
  29768. + .init = au_wbr_create_init_mfs,
  29769. + .fin = au_wbr_create_fin_mfs
  29770. + },
  29771. + [AuWbrCreate_MFSV] = {
  29772. + .create = au_wbr_create_mfs,
  29773. + .init = au_wbr_create_init_mfs,
  29774. + .fin = au_wbr_create_fin_mfs
  29775. + },
  29776. + [AuWbrCreate_MFSRR] = {
  29777. + .create = au_wbr_create_mfsrr,
  29778. + .init = au_wbr_create_init_mfsrr,
  29779. + .fin = au_wbr_create_fin_mfs
  29780. + },
  29781. + [AuWbrCreate_MFSRRV] = {
  29782. + .create = au_wbr_create_mfsrr,
  29783. + .init = au_wbr_create_init_mfsrr,
  29784. + .fin = au_wbr_create_fin_mfs
  29785. + },
  29786. + [AuWbrCreate_PMFS] = {
  29787. + .create = au_wbr_create_pmfs,
  29788. + .init = au_wbr_create_init_mfs,
  29789. + .fin = au_wbr_create_fin_mfs
  29790. + },
  29791. + [AuWbrCreate_PMFSV] = {
  29792. + .create = au_wbr_create_pmfs,
  29793. + .init = au_wbr_create_init_mfs,
  29794. + .fin = au_wbr_create_fin_mfs
  29795. + },
  29796. + [AuWbrCreate_PMFSRR] = {
  29797. + .create = au_wbr_create_pmfsrr,
  29798. + .init = au_wbr_create_init_mfsrr,
  29799. + .fin = au_wbr_create_fin_mfs
  29800. + },
  29801. + [AuWbrCreate_PMFSRRV] = {
  29802. + .create = au_wbr_create_pmfsrr,
  29803. + .init = au_wbr_create_init_mfsrr,
  29804. + .fin = au_wbr_create_fin_mfs
  29805. + }
  29806. +};
  29807. diff -Nur linux-4.0.4.orig/fs/aufs/whout.c linux-4.0.4/fs/aufs/whout.c
  29808. --- linux-4.0.4.orig/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
  29809. +++ linux-4.0.4/fs/aufs/whout.c 2015-05-30 22:11:30.000000000 +0200
  29810. @@ -0,0 +1,1051 @@
  29811. +/*
  29812. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  29813. + */
  29814. +
  29815. +/*
  29816. + * whiteout for logical deletion and opaque directory
  29817. + */
  29818. +
  29819. +#include "aufs.h"
  29820. +
  29821. +#define WH_MASK S_IRUGO
  29822. +
  29823. +/*
  29824. + * If a directory contains this file, then it is opaque. We start with the
  29825. + * .wh. flag so that it is blocked by lookup.
  29826. + */
  29827. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  29828. + sizeof(AUFS_WH_DIROPQ) - 1);
  29829. +
  29830. +/*
  29831. + * generate whiteout name, which is NOT terminated by NULL.
  29832. + * @name: original d_name.name
  29833. + * @len: original d_name.len
  29834. + * @wh: whiteout qstr
  29835. + * returns zero when succeeds, otherwise error.
  29836. + * succeeded value as wh->name should be freed by kfree().
  29837. + */
  29838. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  29839. +{
  29840. + char *p;
  29841. +
  29842. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  29843. + return -ENAMETOOLONG;
  29844. +
  29845. + wh->len = name->len + AUFS_WH_PFX_LEN;
  29846. + p = kmalloc(wh->len, GFP_NOFS);
  29847. + wh->name = p;
  29848. + if (p) {
  29849. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  29850. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  29851. + /* smp_mb(); */
  29852. + return 0;
  29853. + }
  29854. + return -ENOMEM;
  29855. +}
  29856. +
  29857. +/* ---------------------------------------------------------------------- */
  29858. +
  29859. +/*
  29860. + * test if the @wh_name exists under @h_parent.
  29861. + * @try_sio specifies the necessary of super-io.
  29862. + */
  29863. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  29864. +{
  29865. + int err;
  29866. + struct dentry *wh_dentry;
  29867. +
  29868. + if (!try_sio)
  29869. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  29870. + else
  29871. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  29872. + err = PTR_ERR(wh_dentry);
  29873. + if (IS_ERR(wh_dentry)) {
  29874. + if (err == -ENAMETOOLONG)
  29875. + err = 0;
  29876. + goto out;
  29877. + }
  29878. +
  29879. + err = 0;
  29880. + if (!wh_dentry->d_inode)
  29881. + goto out_wh; /* success */
  29882. +
  29883. + err = 1;
  29884. + if (d_is_reg(wh_dentry))
  29885. + goto out_wh; /* success */
  29886. +
  29887. + err = -EIO;
  29888. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  29889. + wh_dentry, wh_dentry->d_inode->i_mode);
  29890. +
  29891. +out_wh:
  29892. + dput(wh_dentry);
  29893. +out:
  29894. + return err;
  29895. +}
  29896. +
  29897. +/*
  29898. + * test if the @h_dentry sets opaque or not.
  29899. + */
  29900. +int au_diropq_test(struct dentry *h_dentry)
  29901. +{
  29902. + int err;
  29903. + struct inode *h_dir;
  29904. +
  29905. + h_dir = h_dentry->d_inode;
  29906. + err = au_wh_test(h_dentry, &diropq_name,
  29907. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  29908. + return err;
  29909. +}
  29910. +
  29911. +/*
  29912. + * returns a negative dentry whose name is unique and temporary.
  29913. + */
  29914. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  29915. + struct qstr *prefix)
  29916. +{
  29917. + struct dentry *dentry;
  29918. + int i;
  29919. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  29920. + *name, *p;
  29921. + /* strict atomic_t is unnecessary here */
  29922. + static unsigned short cnt;
  29923. + struct qstr qs;
  29924. +
  29925. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  29926. +
  29927. + name = defname;
  29928. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  29929. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  29930. + dentry = ERR_PTR(-ENAMETOOLONG);
  29931. + if (unlikely(qs.len > NAME_MAX))
  29932. + goto out;
  29933. + dentry = ERR_PTR(-ENOMEM);
  29934. + name = kmalloc(qs.len + 1, GFP_NOFS);
  29935. + if (unlikely(!name))
  29936. + goto out;
  29937. + }
  29938. +
  29939. + /* doubly whiteout-ed */
  29940. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  29941. + p = name + AUFS_WH_PFX_LEN * 2;
  29942. + memcpy(p, prefix->name, prefix->len);
  29943. + p += prefix->len;
  29944. + *p++ = '.';
  29945. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  29946. +
  29947. + qs.name = name;
  29948. + for (i = 0; i < 3; i++) {
  29949. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  29950. + dentry = au_sio_lkup_one(&qs, h_parent);
  29951. + if (IS_ERR(dentry) || !dentry->d_inode)
  29952. + goto out_name;
  29953. + dput(dentry);
  29954. + }
  29955. + /* pr_warn("could not get random name\n"); */
  29956. + dentry = ERR_PTR(-EEXIST);
  29957. + AuDbg("%.*s\n", AuLNPair(&qs));
  29958. + BUG();
  29959. +
  29960. +out_name:
  29961. + if (name != defname)
  29962. + kfree(name);
  29963. +out:
  29964. + AuTraceErrPtr(dentry);
  29965. + return dentry;
  29966. +}
  29967. +
  29968. +/*
  29969. + * rename the @h_dentry on @br to the whiteouted temporary name.
  29970. + */
  29971. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  29972. +{
  29973. + int err;
  29974. + struct path h_path = {
  29975. + .mnt = au_br_mnt(br)
  29976. + };
  29977. + struct inode *h_dir, *delegated;
  29978. + struct dentry *h_parent;
  29979. +
  29980. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  29981. + h_dir = h_parent->d_inode;
  29982. + IMustLock(h_dir);
  29983. +
  29984. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  29985. + err = PTR_ERR(h_path.dentry);
  29986. + if (IS_ERR(h_path.dentry))
  29987. + goto out;
  29988. +
  29989. + /* under the same dir, no need to lock_rename() */
  29990. + delegated = NULL;
  29991. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
  29992. + AuTraceErr(err);
  29993. + if (unlikely(err == -EWOULDBLOCK)) {
  29994. + pr_warn("cannot retry for NFSv4 delegation"
  29995. + " for an internal rename\n");
  29996. + iput(delegated);
  29997. + }
  29998. + dput(h_path.dentry);
  29999. +
  30000. +out:
  30001. + AuTraceErr(err);
  30002. + return err;
  30003. +}
  30004. +
  30005. +/* ---------------------------------------------------------------------- */
  30006. +/*
  30007. + * functions for removing a whiteout
  30008. + */
  30009. +
  30010. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  30011. +{
  30012. + int err, force;
  30013. + struct inode *delegated;
  30014. +
  30015. + /*
  30016. + * forces superio when the dir has a sticky bit.
  30017. + * this may be a violation of unix fs semantics.
  30018. + */
  30019. + force = (h_dir->i_mode & S_ISVTX)
  30020. + && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
  30021. + delegated = NULL;
  30022. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  30023. + if (unlikely(err == -EWOULDBLOCK)) {
  30024. + pr_warn("cannot retry for NFSv4 delegation"
  30025. + " for an internal unlink\n");
  30026. + iput(delegated);
  30027. + }
  30028. + return err;
  30029. +}
  30030. +
  30031. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  30032. + struct dentry *dentry)
  30033. +{
  30034. + int err;
  30035. +
  30036. + err = do_unlink_wh(h_dir, h_path);
  30037. + if (!err && dentry)
  30038. + au_set_dbwh(dentry, -1);
  30039. +
  30040. + return err;
  30041. +}
  30042. +
  30043. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  30044. + struct au_branch *br)
  30045. +{
  30046. + int err;
  30047. + struct path h_path = {
  30048. + .mnt = au_br_mnt(br)
  30049. + };
  30050. +
  30051. + err = 0;
  30052. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  30053. + if (IS_ERR(h_path.dentry))
  30054. + err = PTR_ERR(h_path.dentry);
  30055. + else {
  30056. + if (h_path.dentry->d_inode
  30057. + && d_is_reg(h_path.dentry))
  30058. + err = do_unlink_wh(h_parent->d_inode, &h_path);
  30059. + dput(h_path.dentry);
  30060. + }
  30061. +
  30062. + return err;
  30063. +}
  30064. +
  30065. +/* ---------------------------------------------------------------------- */
  30066. +/*
  30067. + * initialize/clean whiteout for a branch
  30068. + */
  30069. +
  30070. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  30071. + const int isdir)
  30072. +{
  30073. + int err;
  30074. + struct inode *delegated;
  30075. +
  30076. + if (!whpath->dentry->d_inode)
  30077. + return;
  30078. +
  30079. + if (isdir)
  30080. + err = vfsub_rmdir(h_dir, whpath);
  30081. + else {
  30082. + delegated = NULL;
  30083. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  30084. + if (unlikely(err == -EWOULDBLOCK)) {
  30085. + pr_warn("cannot retry for NFSv4 delegation"
  30086. + " for an internal unlink\n");
  30087. + iput(delegated);
  30088. + }
  30089. + }
  30090. + if (unlikely(err))
  30091. + pr_warn("failed removing %pd (%d), ignored.\n",
  30092. + whpath->dentry, err);
  30093. +}
  30094. +
  30095. +static int test_linkable(struct dentry *h_root)
  30096. +{
  30097. + struct inode *h_dir = h_root->d_inode;
  30098. +
  30099. + if (h_dir->i_op->link)
  30100. + return 0;
  30101. +
  30102. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  30103. + h_root, au_sbtype(h_root->d_sb));
  30104. + return -ENOSYS;
  30105. +}
  30106. +
  30107. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  30108. +static int au_whdir(struct inode *h_dir, struct path *path)
  30109. +{
  30110. + int err;
  30111. +
  30112. + err = -EEXIST;
  30113. + if (!path->dentry->d_inode) {
  30114. + int mode = S_IRWXU;
  30115. +
  30116. + if (au_test_nfs(path->dentry->d_sb))
  30117. + mode |= S_IXUGO;
  30118. + err = vfsub_mkdir(h_dir, path, mode);
  30119. + } else if (d_is_dir(path->dentry))
  30120. + err = 0;
  30121. + else
  30122. + pr_err("unknown %pd exists\n", path->dentry);
  30123. +
  30124. + return err;
  30125. +}
  30126. +
  30127. +struct au_wh_base {
  30128. + const struct qstr *name;
  30129. + struct dentry *dentry;
  30130. +};
  30131. +
  30132. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  30133. + struct path *h_path)
  30134. +{
  30135. + h_path->dentry = base[AuBrWh_BASE].dentry;
  30136. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  30137. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  30138. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  30139. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  30140. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  30141. +}
  30142. +
  30143. +/*
  30144. + * returns tri-state,
  30145. + * minus: error, caller should print the message
  30146. + * zero: succuess
  30147. + * plus: error, caller should NOT print the message
  30148. + */
  30149. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  30150. + int do_plink, struct au_wh_base base[],
  30151. + struct path *h_path)
  30152. +{
  30153. + int err;
  30154. + struct inode *h_dir;
  30155. +
  30156. + h_dir = h_root->d_inode;
  30157. + h_path->dentry = base[AuBrWh_BASE].dentry;
  30158. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  30159. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  30160. + if (do_plink) {
  30161. + err = test_linkable(h_root);
  30162. + if (unlikely(err)) {
  30163. + err = 1;
  30164. + goto out;
  30165. + }
  30166. +
  30167. + err = au_whdir(h_dir, h_path);
  30168. + if (unlikely(err))
  30169. + goto out;
  30170. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  30171. + } else
  30172. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  30173. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  30174. + err = au_whdir(h_dir, h_path);
  30175. + if (unlikely(err))
  30176. + goto out;
  30177. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  30178. +
  30179. +out:
  30180. + return err;
  30181. +}
  30182. +
  30183. +/*
  30184. + * for the moment, aufs supports the branch filesystem which does not support
  30185. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  30186. + * copyup failed. finally, such filesystem will not be used as the writable
  30187. + * branch.
  30188. + *
  30189. + * returns tri-state, see above.
  30190. + */
  30191. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  30192. + int do_plink, struct au_wh_base base[],
  30193. + struct path *h_path)
  30194. +{
  30195. + int err;
  30196. + struct inode *h_dir;
  30197. +
  30198. + WbrWhMustWriteLock(wbr);
  30199. +
  30200. + err = test_linkable(h_root);
  30201. + if (unlikely(err)) {
  30202. + err = 1;
  30203. + goto out;
  30204. + }
  30205. +
  30206. + /*
  30207. + * todo: should this create be done in /sbin/mount.aufs helper?
  30208. + */
  30209. + err = -EEXIST;
  30210. + h_dir = h_root->d_inode;
  30211. + if (!base[AuBrWh_BASE].dentry->d_inode) {
  30212. + h_path->dentry = base[AuBrWh_BASE].dentry;
  30213. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  30214. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  30215. + err = 0;
  30216. + else
  30217. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  30218. + if (unlikely(err))
  30219. + goto out;
  30220. +
  30221. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  30222. + if (do_plink) {
  30223. + err = au_whdir(h_dir, h_path);
  30224. + if (unlikely(err))
  30225. + goto out;
  30226. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  30227. + } else
  30228. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  30229. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  30230. +
  30231. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  30232. + err = au_whdir(h_dir, h_path);
  30233. + if (unlikely(err))
  30234. + goto out;
  30235. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  30236. +
  30237. +out:
  30238. + return err;
  30239. +}
  30240. +
  30241. +/*
  30242. + * initialize the whiteout base file/dir for @br.
  30243. + */
  30244. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  30245. +{
  30246. + int err, i;
  30247. + const unsigned char do_plink
  30248. + = !!au_opt_test(au_mntflags(sb), PLINK);
  30249. + struct inode *h_dir;
  30250. + struct path path = br->br_path;
  30251. + struct dentry *h_root = path.dentry;
  30252. + struct au_wbr *wbr = br->br_wbr;
  30253. + static const struct qstr base_name[] = {
  30254. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  30255. + sizeof(AUFS_BASE_NAME) - 1),
  30256. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  30257. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  30258. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  30259. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  30260. + };
  30261. + struct au_wh_base base[] = {
  30262. + [AuBrWh_BASE] = {
  30263. + .name = base_name + AuBrWh_BASE,
  30264. + .dentry = NULL
  30265. + },
  30266. + [AuBrWh_PLINK] = {
  30267. + .name = base_name + AuBrWh_PLINK,
  30268. + .dentry = NULL
  30269. + },
  30270. + [AuBrWh_ORPH] = {
  30271. + .name = base_name + AuBrWh_ORPH,
  30272. + .dentry = NULL
  30273. + }
  30274. + };
  30275. +
  30276. + if (wbr)
  30277. + WbrWhMustWriteLock(wbr);
  30278. +
  30279. + for (i = 0; i < AuBrWh_Last; i++) {
  30280. + /* doubly whiteouted */
  30281. + struct dentry *d;
  30282. +
  30283. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  30284. + err = PTR_ERR(d);
  30285. + if (IS_ERR(d))
  30286. + goto out;
  30287. +
  30288. + base[i].dentry = d;
  30289. + AuDebugOn(wbr
  30290. + && wbr->wbr_wh[i]
  30291. + && wbr->wbr_wh[i] != base[i].dentry);
  30292. + }
  30293. +
  30294. + if (wbr)
  30295. + for (i = 0; i < AuBrWh_Last; i++) {
  30296. + dput(wbr->wbr_wh[i]);
  30297. + wbr->wbr_wh[i] = NULL;
  30298. + }
  30299. +
  30300. + err = 0;
  30301. + if (!au_br_writable(br->br_perm)) {
  30302. + h_dir = h_root->d_inode;
  30303. + au_wh_init_ro(h_dir, base, &path);
  30304. + } else if (!au_br_wh_linkable(br->br_perm)) {
  30305. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  30306. + if (err > 0)
  30307. + goto out;
  30308. + else if (err)
  30309. + goto out_err;
  30310. + } else {
  30311. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  30312. + if (err > 0)
  30313. + goto out;
  30314. + else if (err)
  30315. + goto out_err;
  30316. + }
  30317. + goto out; /* success */
  30318. +
  30319. +out_err:
  30320. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  30321. + err, h_root, au_sbtype(h_root->d_sb));
  30322. +out:
  30323. + for (i = 0; i < AuBrWh_Last; i++)
  30324. + dput(base[i].dentry);
  30325. + return err;
  30326. +}
  30327. +
  30328. +/* ---------------------------------------------------------------------- */
  30329. +/*
  30330. + * whiteouts are all hard-linked usually.
  30331. + * when its link count reaches a ceiling, we create a new whiteout base
  30332. + * asynchronously.
  30333. + */
  30334. +
  30335. +struct reinit_br_wh {
  30336. + struct super_block *sb;
  30337. + struct au_branch *br;
  30338. +};
  30339. +
  30340. +static void reinit_br_wh(void *arg)
  30341. +{
  30342. + int err;
  30343. + aufs_bindex_t bindex;
  30344. + struct path h_path;
  30345. + struct reinit_br_wh *a = arg;
  30346. + struct au_wbr *wbr;
  30347. + struct inode *dir, *delegated;
  30348. + struct dentry *h_root;
  30349. + struct au_hinode *hdir;
  30350. +
  30351. + err = 0;
  30352. + wbr = a->br->br_wbr;
  30353. + /* big aufs lock */
  30354. + si_noflush_write_lock(a->sb);
  30355. + if (!au_br_writable(a->br->br_perm))
  30356. + goto out;
  30357. + bindex = au_br_index(a->sb, a->br->br_id);
  30358. + if (unlikely(bindex < 0))
  30359. + goto out;
  30360. +
  30361. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  30362. + dir = a->sb->s_root->d_inode;
  30363. + hdir = au_hi(dir, bindex);
  30364. + h_root = au_h_dptr(a->sb->s_root, bindex);
  30365. + AuDebugOn(h_root != au_br_dentry(a->br));
  30366. +
  30367. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  30368. + wbr_wh_write_lock(wbr);
  30369. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  30370. + h_root, a->br);
  30371. + if (!err) {
  30372. + h_path.dentry = wbr->wbr_whbase;
  30373. + h_path.mnt = au_br_mnt(a->br);
  30374. + delegated = NULL;
  30375. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  30376. + /*force*/0);
  30377. + if (unlikely(err == -EWOULDBLOCK)) {
  30378. + pr_warn("cannot retry for NFSv4 delegation"
  30379. + " for an internal unlink\n");
  30380. + iput(delegated);
  30381. + }
  30382. + } else {
  30383. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  30384. + err = 0;
  30385. + }
  30386. + dput(wbr->wbr_whbase);
  30387. + wbr->wbr_whbase = NULL;
  30388. + if (!err)
  30389. + err = au_wh_init(a->br, a->sb);
  30390. + wbr_wh_write_unlock(wbr);
  30391. + au_hn_imtx_unlock(hdir);
  30392. + di_read_unlock(a->sb->s_root, AuLock_IR);
  30393. + if (!err)
  30394. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  30395. +
  30396. +out:
  30397. + if (wbr)
  30398. + atomic_dec(&wbr->wbr_wh_running);
  30399. + atomic_dec(&a->br->br_count);
  30400. + si_write_unlock(a->sb);
  30401. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  30402. + kfree(arg);
  30403. + if (unlikely(err))
  30404. + AuIOErr("err %d\n", err);
  30405. +}
  30406. +
  30407. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  30408. +{
  30409. + int do_dec, wkq_err;
  30410. + struct reinit_br_wh *arg;
  30411. +
  30412. + do_dec = 1;
  30413. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  30414. + goto out;
  30415. +
  30416. + /* ignore ENOMEM */
  30417. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  30418. + if (arg) {
  30419. + /*
  30420. + * dec(wh_running), kfree(arg) and dec(br_count)
  30421. + * in reinit function
  30422. + */
  30423. + arg->sb = sb;
  30424. + arg->br = br;
  30425. + atomic_inc(&br->br_count);
  30426. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  30427. + if (unlikely(wkq_err)) {
  30428. + atomic_dec(&br->br_wbr->wbr_wh_running);
  30429. + atomic_dec(&br->br_count);
  30430. + kfree(arg);
  30431. + }
  30432. + do_dec = 0;
  30433. + }
  30434. +
  30435. +out:
  30436. + if (do_dec)
  30437. + atomic_dec(&br->br_wbr->wbr_wh_running);
  30438. +}
  30439. +
  30440. +/* ---------------------------------------------------------------------- */
  30441. +
  30442. +/*
  30443. + * create the whiteout @wh.
  30444. + */
  30445. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  30446. + struct dentry *wh)
  30447. +{
  30448. + int err;
  30449. + struct path h_path = {
  30450. + .dentry = wh
  30451. + };
  30452. + struct au_branch *br;
  30453. + struct au_wbr *wbr;
  30454. + struct dentry *h_parent;
  30455. + struct inode *h_dir, *delegated;
  30456. +
  30457. + h_parent = wh->d_parent; /* dir inode is locked */
  30458. + h_dir = h_parent->d_inode;
  30459. + IMustLock(h_dir);
  30460. +
  30461. + br = au_sbr(sb, bindex);
  30462. + h_path.mnt = au_br_mnt(br);
  30463. + wbr = br->br_wbr;
  30464. + wbr_wh_read_lock(wbr);
  30465. + if (wbr->wbr_whbase) {
  30466. + delegated = NULL;
  30467. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  30468. + if (unlikely(err == -EWOULDBLOCK)) {
  30469. + pr_warn("cannot retry for NFSv4 delegation"
  30470. + " for an internal link\n");
  30471. + iput(delegated);
  30472. + }
  30473. + if (!err || err != -EMLINK)
  30474. + goto out;
  30475. +
  30476. + /* link count full. re-initialize br_whbase. */
  30477. + kick_reinit_br_wh(sb, br);
  30478. + }
  30479. +
  30480. + /* return this error in this context */
  30481. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  30482. + if (!err)
  30483. + au_fhsm_wrote(sb, bindex, /*force*/0);
  30484. +
  30485. +out:
  30486. + wbr_wh_read_unlock(wbr);
  30487. + return err;
  30488. +}
  30489. +
  30490. +/* ---------------------------------------------------------------------- */
  30491. +
  30492. +/*
  30493. + * create or remove the diropq.
  30494. + */
  30495. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  30496. + unsigned int flags)
  30497. +{
  30498. + struct dentry *opq_dentry, *h_dentry;
  30499. + struct super_block *sb;
  30500. + struct au_branch *br;
  30501. + int err;
  30502. +
  30503. + sb = dentry->d_sb;
  30504. + br = au_sbr(sb, bindex);
  30505. + h_dentry = au_h_dptr(dentry, bindex);
  30506. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  30507. + if (IS_ERR(opq_dentry))
  30508. + goto out;
  30509. +
  30510. + if (au_ftest_diropq(flags, CREATE)) {
  30511. + err = link_or_create_wh(sb, bindex, opq_dentry);
  30512. + if (!err) {
  30513. + au_set_dbdiropq(dentry, bindex);
  30514. + goto out; /* success */
  30515. + }
  30516. + } else {
  30517. + struct path tmp = {
  30518. + .dentry = opq_dentry,
  30519. + .mnt = au_br_mnt(br)
  30520. + };
  30521. + err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
  30522. + if (!err)
  30523. + au_set_dbdiropq(dentry, -1);
  30524. + }
  30525. + dput(opq_dentry);
  30526. + opq_dentry = ERR_PTR(err);
  30527. +
  30528. +out:
  30529. + return opq_dentry;
  30530. +}
  30531. +
  30532. +struct do_diropq_args {
  30533. + struct dentry **errp;
  30534. + struct dentry *dentry;
  30535. + aufs_bindex_t bindex;
  30536. + unsigned int flags;
  30537. +};
  30538. +
  30539. +static void call_do_diropq(void *args)
  30540. +{
  30541. + struct do_diropq_args *a = args;
  30542. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  30543. +}
  30544. +
  30545. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  30546. + unsigned int flags)
  30547. +{
  30548. + struct dentry *diropq, *h_dentry;
  30549. +
  30550. + h_dentry = au_h_dptr(dentry, bindex);
  30551. + if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
  30552. + diropq = do_diropq(dentry, bindex, flags);
  30553. + else {
  30554. + int wkq_err;
  30555. + struct do_diropq_args args = {
  30556. + .errp = &diropq,
  30557. + .dentry = dentry,
  30558. + .bindex = bindex,
  30559. + .flags = flags
  30560. + };
  30561. +
  30562. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  30563. + if (unlikely(wkq_err))
  30564. + diropq = ERR_PTR(wkq_err);
  30565. + }
  30566. +
  30567. + return diropq;
  30568. +}
  30569. +
  30570. +/* ---------------------------------------------------------------------- */
  30571. +
  30572. +/*
  30573. + * lookup whiteout dentry.
  30574. + * @h_parent: lower parent dentry which must exist and be locked
  30575. + * @base_name: name of dentry which will be whiteouted
  30576. + * returns dentry for whiteout.
  30577. + */
  30578. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  30579. + struct au_branch *br)
  30580. +{
  30581. + int err;
  30582. + struct qstr wh_name;
  30583. + struct dentry *wh_dentry;
  30584. +
  30585. + err = au_wh_name_alloc(&wh_name, base_name);
  30586. + wh_dentry = ERR_PTR(err);
  30587. + if (!err) {
  30588. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  30589. + kfree(wh_name.name);
  30590. + }
  30591. + return wh_dentry;
  30592. +}
  30593. +
  30594. +/*
  30595. + * link/create a whiteout for @dentry on @bindex.
  30596. + */
  30597. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  30598. + struct dentry *h_parent)
  30599. +{
  30600. + struct dentry *wh_dentry;
  30601. + struct super_block *sb;
  30602. + int err;
  30603. +
  30604. + sb = dentry->d_sb;
  30605. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  30606. + if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
  30607. + err = link_or_create_wh(sb, bindex, wh_dentry);
  30608. + if (!err) {
  30609. + au_set_dbwh(dentry, bindex);
  30610. + au_fhsm_wrote(sb, bindex, /*force*/0);
  30611. + } else {
  30612. + dput(wh_dentry);
  30613. + wh_dentry = ERR_PTR(err);
  30614. + }
  30615. + }
  30616. +
  30617. + return wh_dentry;
  30618. +}
  30619. +
  30620. +/* ---------------------------------------------------------------------- */
  30621. +
  30622. +/* Delete all whiteouts in this directory on branch bindex. */
  30623. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  30624. + aufs_bindex_t bindex, struct au_branch *br)
  30625. +{
  30626. + int err;
  30627. + unsigned long ul, n;
  30628. + struct qstr wh_name;
  30629. + char *p;
  30630. + struct hlist_head *head;
  30631. + struct au_vdir_wh *pos;
  30632. + struct au_vdir_destr *str;
  30633. +
  30634. + err = -ENOMEM;
  30635. + p = (void *)__get_free_page(GFP_NOFS);
  30636. + wh_name.name = p;
  30637. + if (unlikely(!wh_name.name))
  30638. + goto out;
  30639. +
  30640. + err = 0;
  30641. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  30642. + p += AUFS_WH_PFX_LEN;
  30643. + n = whlist->nh_num;
  30644. + head = whlist->nh_head;
  30645. + for (ul = 0; !err && ul < n; ul++, head++) {
  30646. + hlist_for_each_entry(pos, head, wh_hash) {
  30647. + if (pos->wh_bindex != bindex)
  30648. + continue;
  30649. +
  30650. + str = &pos->wh_str;
  30651. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  30652. + memcpy(p, str->name, str->len);
  30653. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  30654. + err = unlink_wh_name(h_dentry, &wh_name, br);
  30655. + if (!err)
  30656. + continue;
  30657. + break;
  30658. + }
  30659. + AuIOErr("whiteout name too long %.*s\n",
  30660. + str->len, str->name);
  30661. + err = -EIO;
  30662. + break;
  30663. + }
  30664. + }
  30665. + free_page((unsigned long)wh_name.name);
  30666. +
  30667. +out:
  30668. + return err;
  30669. +}
  30670. +
  30671. +struct del_wh_children_args {
  30672. + int *errp;
  30673. + struct dentry *h_dentry;
  30674. + struct au_nhash *whlist;
  30675. + aufs_bindex_t bindex;
  30676. + struct au_branch *br;
  30677. +};
  30678. +
  30679. +static void call_del_wh_children(void *args)
  30680. +{
  30681. + struct del_wh_children_args *a = args;
  30682. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  30683. +}
  30684. +
  30685. +/* ---------------------------------------------------------------------- */
  30686. +
  30687. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  30688. +{
  30689. + struct au_whtmp_rmdir *whtmp;
  30690. + int err;
  30691. + unsigned int rdhash;
  30692. +
  30693. + SiMustAnyLock(sb);
  30694. +
  30695. + whtmp = kmalloc(sizeof(*whtmp), gfp);
  30696. + if (unlikely(!whtmp)) {
  30697. + whtmp = ERR_PTR(-ENOMEM);
  30698. + goto out;
  30699. + }
  30700. +
  30701. + whtmp->dir = NULL;
  30702. + whtmp->br = NULL;
  30703. + whtmp->wh_dentry = NULL;
  30704. + /* no estimation for dir size */
  30705. + rdhash = au_sbi(sb)->si_rdhash;
  30706. + if (!rdhash)
  30707. + rdhash = AUFS_RDHASH_DEF;
  30708. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  30709. + if (unlikely(err)) {
  30710. + kfree(whtmp);
  30711. + whtmp = ERR_PTR(err);
  30712. + }
  30713. +
  30714. +out:
  30715. + return whtmp;
  30716. +}
  30717. +
  30718. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  30719. +{
  30720. + if (whtmp->br)
  30721. + atomic_dec(&whtmp->br->br_count);
  30722. + dput(whtmp->wh_dentry);
  30723. + iput(whtmp->dir);
  30724. + au_nhash_wh_free(&whtmp->whlist);
  30725. + kfree(whtmp);
  30726. +}
  30727. +
  30728. +/*
  30729. + * rmdir the whiteouted temporary named dir @h_dentry.
  30730. + * @whlist: whiteouted children.
  30731. + */
  30732. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30733. + struct dentry *wh_dentry, struct au_nhash *whlist)
  30734. +{
  30735. + int err;
  30736. + unsigned int h_nlink;
  30737. + struct path h_tmp;
  30738. + struct inode *wh_inode, *h_dir;
  30739. + struct au_branch *br;
  30740. +
  30741. + h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
  30742. + IMustLock(h_dir);
  30743. +
  30744. + br = au_sbr(dir->i_sb, bindex);
  30745. + wh_inode = wh_dentry->d_inode;
  30746. + mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
  30747. +
  30748. + /*
  30749. + * someone else might change some whiteouts while we were sleeping.
  30750. + * it means this whlist may have an obsoleted entry.
  30751. + */
  30752. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  30753. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  30754. + else {
  30755. + int wkq_err;
  30756. + struct del_wh_children_args args = {
  30757. + .errp = &err,
  30758. + .h_dentry = wh_dentry,
  30759. + .whlist = whlist,
  30760. + .bindex = bindex,
  30761. + .br = br
  30762. + };
  30763. +
  30764. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  30765. + if (unlikely(wkq_err))
  30766. + err = wkq_err;
  30767. + }
  30768. + mutex_unlock(&wh_inode->i_mutex);
  30769. +
  30770. + if (!err) {
  30771. + h_tmp.dentry = wh_dentry;
  30772. + h_tmp.mnt = au_br_mnt(br);
  30773. + h_nlink = h_dir->i_nlink;
  30774. + err = vfsub_rmdir(h_dir, &h_tmp);
  30775. + /* some fs doesn't change the parent nlink in some cases */
  30776. + h_nlink -= h_dir->i_nlink;
  30777. + }
  30778. +
  30779. + if (!err) {
  30780. + if (au_ibstart(dir) == bindex) {
  30781. + /* todo: dir->i_mutex is necessary */
  30782. + au_cpup_attr_timesizes(dir);
  30783. + if (h_nlink)
  30784. + vfsub_drop_nlink(dir);
  30785. + }
  30786. + return 0; /* success */
  30787. + }
  30788. +
  30789. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  30790. + return err;
  30791. +}
  30792. +
  30793. +static void call_rmdir_whtmp(void *args)
  30794. +{
  30795. + int err;
  30796. + aufs_bindex_t bindex;
  30797. + struct au_whtmp_rmdir *a = args;
  30798. + struct super_block *sb;
  30799. + struct dentry *h_parent;
  30800. + struct inode *h_dir;
  30801. + struct au_hinode *hdir;
  30802. +
  30803. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  30804. + /* mutex_lock(&a->dir->i_mutex); */
  30805. + err = -EROFS;
  30806. + sb = a->dir->i_sb;
  30807. + si_read_lock(sb, !AuLock_FLUSH);
  30808. + if (!au_br_writable(a->br->br_perm))
  30809. + goto out;
  30810. + bindex = au_br_index(sb, a->br->br_id);
  30811. + if (unlikely(bindex < 0))
  30812. + goto out;
  30813. +
  30814. + err = -EIO;
  30815. + ii_write_lock_parent(a->dir);
  30816. + h_parent = dget_parent(a->wh_dentry);
  30817. + h_dir = h_parent->d_inode;
  30818. + hdir = au_hi(a->dir, bindex);
  30819. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  30820. + if (unlikely(err))
  30821. + goto out_mnt;
  30822. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  30823. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  30824. + a->br);
  30825. + if (!err)
  30826. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  30827. + au_hn_imtx_unlock(hdir);
  30828. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  30829. +
  30830. +out_mnt:
  30831. + dput(h_parent);
  30832. + ii_write_unlock(a->dir);
  30833. +out:
  30834. + /* mutex_unlock(&a->dir->i_mutex); */
  30835. + au_whtmp_rmdir_free(a);
  30836. + si_read_unlock(sb);
  30837. + au_nwt_done(&au_sbi(sb)->si_nowait);
  30838. + if (unlikely(err))
  30839. + AuIOErr("err %d\n", err);
  30840. +}
  30841. +
  30842. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30843. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  30844. +{
  30845. + int wkq_err;
  30846. + struct super_block *sb;
  30847. +
  30848. + IMustLock(dir);
  30849. +
  30850. + /* all post-process will be done in do_rmdir_whtmp(). */
  30851. + sb = dir->i_sb;
  30852. + args->dir = au_igrab(dir);
  30853. + args->br = au_sbr(sb, bindex);
  30854. + atomic_inc(&args->br->br_count);
  30855. + args->wh_dentry = dget(wh_dentry);
  30856. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  30857. + if (unlikely(wkq_err)) {
  30858. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  30859. + au_whtmp_rmdir_free(args);
  30860. + }
  30861. +}
  30862. diff -Nur linux-4.0.4.orig/fs/aufs/whout.h linux-4.0.4/fs/aufs/whout.h
  30863. --- linux-4.0.4.orig/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
  30864. +++ linux-4.0.4/fs/aufs/whout.h 2015-05-30 22:11:30.000000000 +0200
  30865. @@ -0,0 +1,72 @@
  30866. +/*
  30867. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30868. + */
  30869. +
  30870. +/*
  30871. + * whiteout for logical deletion and opaque directory
  30872. + */
  30873. +
  30874. +#ifndef __AUFS_WHOUT_H__
  30875. +#define __AUFS_WHOUT_H__
  30876. +
  30877. +#ifdef __KERNEL__
  30878. +
  30879. +#include "dir.h"
  30880. +
  30881. +/* whout.c */
  30882. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  30883. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  30884. +int au_diropq_test(struct dentry *h_dentry);
  30885. +struct au_branch;
  30886. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  30887. + struct qstr *prefix);
  30888. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  30889. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  30890. + struct dentry *dentry);
  30891. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  30892. +
  30893. +/* diropq flags */
  30894. +#define AuDiropq_CREATE 1
  30895. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  30896. +#define au_fset_diropq(flags, name) \
  30897. + do { (flags) |= AuDiropq_##name; } while (0)
  30898. +#define au_fclr_diropq(flags, name) \
  30899. + do { (flags) &= ~AuDiropq_##name; } while (0)
  30900. +
  30901. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  30902. + unsigned int flags);
  30903. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  30904. + struct au_branch *br);
  30905. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  30906. + struct dentry *h_parent);
  30907. +
  30908. +/* real rmdir for the whiteout-ed dir */
  30909. +struct au_whtmp_rmdir {
  30910. + struct inode *dir;
  30911. + struct au_branch *br;
  30912. + struct dentry *wh_dentry;
  30913. + struct au_nhash whlist;
  30914. +};
  30915. +
  30916. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  30917. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  30918. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30919. + struct dentry *wh_dentry, struct au_nhash *whlist);
  30920. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30921. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  30922. +
  30923. +/* ---------------------------------------------------------------------- */
  30924. +
  30925. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  30926. + aufs_bindex_t bindex)
  30927. +{
  30928. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  30929. +}
  30930. +
  30931. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  30932. +{
  30933. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  30934. +}
  30935. +
  30936. +#endif /* __KERNEL__ */
  30937. +#endif /* __AUFS_WHOUT_H__ */
  30938. diff -Nur linux-4.0.4.orig/fs/aufs/wkq.c linux-4.0.4/fs/aufs/wkq.c
  30939. --- linux-4.0.4.orig/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
  30940. +++ linux-4.0.4/fs/aufs/wkq.c 2015-05-30 22:11:30.000000000 +0200
  30941. @@ -0,0 +1,200 @@
  30942. +/*
  30943. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30944. + */
  30945. +
  30946. +/*
  30947. + * workqueue for asynchronous/super-io operations
  30948. + * todo: try new dredential scheme
  30949. + */
  30950. +
  30951. +#include <linux/module.h>
  30952. +#include "aufs.h"
  30953. +
  30954. +/* internal workqueue named AUFS_WKQ_NAME */
  30955. +
  30956. +static struct workqueue_struct *au_wkq;
  30957. +
  30958. +struct au_wkinfo {
  30959. + struct work_struct wk;
  30960. + struct kobject *kobj;
  30961. +
  30962. + unsigned int flags; /* see wkq.h */
  30963. +
  30964. + au_wkq_func_t func;
  30965. + void *args;
  30966. +
  30967. + struct completion *comp;
  30968. +};
  30969. +
  30970. +/* ---------------------------------------------------------------------- */
  30971. +
  30972. +static void wkq_func(struct work_struct *wk)
  30973. +{
  30974. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  30975. +
  30976. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  30977. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  30978. +
  30979. + wkinfo->func(wkinfo->args);
  30980. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  30981. + complete(wkinfo->comp);
  30982. + else {
  30983. + kobject_put(wkinfo->kobj);
  30984. + module_put(THIS_MODULE); /* todo: ?? */
  30985. + kfree(wkinfo);
  30986. + }
  30987. +}
  30988. +
  30989. +/*
  30990. + * Since struct completion is large, try allocating it dynamically.
  30991. + */
  30992. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  30993. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  30994. +
  30995. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  30996. +{
  30997. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  30998. + if (*comp) {
  30999. + init_completion(*comp);
  31000. + wkinfo->comp = *comp;
  31001. + return 0;
  31002. + }
  31003. + return -ENOMEM;
  31004. +}
  31005. +
  31006. +static void au_wkq_comp_free(struct completion *comp)
  31007. +{
  31008. + kfree(comp);
  31009. +}
  31010. +
  31011. +#else
  31012. +
  31013. +/* no braces */
  31014. +#define AuWkqCompDeclare(name) \
  31015. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  31016. + struct completion *comp = &_ ## name
  31017. +
  31018. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  31019. +{
  31020. + wkinfo->comp = *comp;
  31021. + return 0;
  31022. +}
  31023. +
  31024. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  31025. +{
  31026. + /* empty */
  31027. +}
  31028. +#endif /* 4KSTACKS */
  31029. +
  31030. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  31031. +{
  31032. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  31033. + if (au_wkq_test()) {
  31034. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  31035. + " due to a dead dir by UDBA?\n");
  31036. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  31037. + }
  31038. + } else
  31039. + au_dbg_verify_kthread();
  31040. +
  31041. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  31042. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  31043. + queue_work(au_wkq, &wkinfo->wk);
  31044. + } else {
  31045. + INIT_WORK(&wkinfo->wk, wkq_func);
  31046. + schedule_work(&wkinfo->wk);
  31047. + }
  31048. +}
  31049. +
  31050. +/*
  31051. + * Be careful. It is easy to make deadlock happen.
  31052. + * processA: lock, wkq and wait
  31053. + * processB: wkq and wait, lock in wkq
  31054. + * --> deadlock
  31055. + */
  31056. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  31057. +{
  31058. + int err;
  31059. + AuWkqCompDeclare(comp);
  31060. + struct au_wkinfo wkinfo = {
  31061. + .flags = flags,
  31062. + .func = func,
  31063. + .args = args
  31064. + };
  31065. +
  31066. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  31067. + if (!err) {
  31068. + au_wkq_run(&wkinfo);
  31069. + /* no timeout, no interrupt */
  31070. + wait_for_completion(wkinfo.comp);
  31071. + au_wkq_comp_free(comp);
  31072. + destroy_work_on_stack(&wkinfo.wk);
  31073. + }
  31074. +
  31075. + return err;
  31076. +
  31077. +}
  31078. +
  31079. +/*
  31080. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  31081. + * problem in a concurrent umounting.
  31082. + */
  31083. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  31084. + unsigned int flags)
  31085. +{
  31086. + int err;
  31087. + struct au_wkinfo *wkinfo;
  31088. +
  31089. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  31090. +
  31091. + /*
  31092. + * wkq_func() must free this wkinfo.
  31093. + * it highly depends upon the implementation of workqueue.
  31094. + */
  31095. + err = 0;
  31096. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  31097. + if (wkinfo) {
  31098. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  31099. + wkinfo->flags = flags & ~AuWkq_WAIT;
  31100. + wkinfo->func = func;
  31101. + wkinfo->args = args;
  31102. + wkinfo->comp = NULL;
  31103. + kobject_get(wkinfo->kobj);
  31104. + __module_get(THIS_MODULE); /* todo: ?? */
  31105. +
  31106. + au_wkq_run(wkinfo);
  31107. + } else {
  31108. + err = -ENOMEM;
  31109. + au_nwt_done(&au_sbi(sb)->si_nowait);
  31110. + }
  31111. +
  31112. + return err;
  31113. +}
  31114. +
  31115. +/* ---------------------------------------------------------------------- */
  31116. +
  31117. +void au_nwt_init(struct au_nowait_tasks *nwt)
  31118. +{
  31119. + atomic_set(&nwt->nw_len, 0);
  31120. + /* smp_mb(); */ /* atomic_set */
  31121. + init_waitqueue_head(&nwt->nw_wq);
  31122. +}
  31123. +
  31124. +void au_wkq_fin(void)
  31125. +{
  31126. + destroy_workqueue(au_wkq);
  31127. +}
  31128. +
  31129. +int __init au_wkq_init(void)
  31130. +{
  31131. + int err;
  31132. +
  31133. + err = 0;
  31134. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  31135. + if (IS_ERR(au_wkq))
  31136. + err = PTR_ERR(au_wkq);
  31137. + else if (!au_wkq)
  31138. + err = -ENOMEM;
  31139. +
  31140. + return err;
  31141. +}
  31142. diff -Nur linux-4.0.4.orig/fs/aufs/wkq.h linux-4.0.4/fs/aufs/wkq.h
  31143. --- linux-4.0.4.orig/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
  31144. +++ linux-4.0.4/fs/aufs/wkq.h 2015-05-30 22:11:30.000000000 +0200
  31145. @@ -0,0 +1,78 @@
  31146. +/*
  31147. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  31148. + */
  31149. +
  31150. +/*
  31151. + * workqueue for asynchronous/super-io operations
  31152. + * todo: try new credentials management scheme
  31153. + */
  31154. +
  31155. +#ifndef __AUFS_WKQ_H__
  31156. +#define __AUFS_WKQ_H__
  31157. +
  31158. +#ifdef __KERNEL__
  31159. +
  31160. +struct super_block;
  31161. +
  31162. +/* ---------------------------------------------------------------------- */
  31163. +
  31164. +/*
  31165. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  31166. + */
  31167. +struct au_nowait_tasks {
  31168. + atomic_t nw_len;
  31169. + wait_queue_head_t nw_wq;
  31170. +};
  31171. +
  31172. +/* ---------------------------------------------------------------------- */
  31173. +
  31174. +typedef void (*au_wkq_func_t)(void *args);
  31175. +
  31176. +/* wkq flags */
  31177. +#define AuWkq_WAIT 1
  31178. +#define AuWkq_NEST (1 << 1)
  31179. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  31180. +#define au_fset_wkq(flags, name) \
  31181. + do { (flags) |= AuWkq_##name; } while (0)
  31182. +#define au_fclr_wkq(flags, name) \
  31183. + do { (flags) &= ~AuWkq_##name; } while (0)
  31184. +
  31185. +#ifndef CONFIG_AUFS_HNOTIFY
  31186. +#undef AuWkq_NEST
  31187. +#define AuWkq_NEST 0
  31188. +#endif
  31189. +
  31190. +/* wkq.c */
  31191. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  31192. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  31193. + unsigned int flags);
  31194. +void au_nwt_init(struct au_nowait_tasks *nwt);
  31195. +int __init au_wkq_init(void);
  31196. +void au_wkq_fin(void);
  31197. +
  31198. +/* ---------------------------------------------------------------------- */
  31199. +
  31200. +static inline int au_wkq_test(void)
  31201. +{
  31202. + return current->flags & PF_WQ_WORKER;
  31203. +}
  31204. +
  31205. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  31206. +{
  31207. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  31208. +}
  31209. +
  31210. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  31211. +{
  31212. + if (atomic_dec_and_test(&nwt->nw_len))
  31213. + wake_up_all(&nwt->nw_wq);
  31214. +}
  31215. +
  31216. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  31217. +{
  31218. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  31219. + return 0;
  31220. +}
  31221. +
  31222. +#endif /* __KERNEL__ */
  31223. +#endif /* __AUFS_WKQ_H__ */
  31224. diff -Nur linux-4.0.4.orig/fs/aufs/xattr.c linux-4.0.4/fs/aufs/xattr.c
  31225. --- linux-4.0.4.orig/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
  31226. +++ linux-4.0.4/fs/aufs/xattr.c 2015-05-30 22:11:31.000000000 +0200
  31227. @@ -0,0 +1,331 @@
  31228. +/*
  31229. + * Copyright (C) 2014-2015 Junjiro R. Okajima
  31230. + */
  31231. +
  31232. +/*
  31233. + * handling xattr functions
  31234. + */
  31235. +
  31236. +#include <linux/xattr.h>
  31237. +#include "aufs.h"
  31238. +
  31239. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  31240. +{
  31241. + if (!ignore_flags)
  31242. + goto out;
  31243. + switch (err) {
  31244. + case -ENOMEM:
  31245. + case -EDQUOT:
  31246. + goto out;
  31247. + }
  31248. +
  31249. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  31250. + err = 0;
  31251. + goto out;
  31252. + }
  31253. +
  31254. +#define cmp(brattr, prefix) do { \
  31255. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  31256. + XATTR_##prefix##_PREFIX_LEN)) { \
  31257. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  31258. + err = 0; \
  31259. + goto out; \
  31260. + } \
  31261. + } while (0)
  31262. +
  31263. + cmp(SEC, SECURITY);
  31264. + cmp(SYS, SYSTEM);
  31265. + cmp(TR, TRUSTED);
  31266. + cmp(USR, USER);
  31267. +#undef cmp
  31268. +
  31269. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  31270. + err = 0;
  31271. +
  31272. +out:
  31273. + return err;
  31274. +}
  31275. +
  31276. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  31277. +
  31278. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  31279. + char *name, char **buf, unsigned int ignore_flags,
  31280. + unsigned int verbose)
  31281. +{
  31282. + int err;
  31283. + ssize_t ssz;
  31284. + struct inode *h_idst;
  31285. +
  31286. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  31287. + err = ssz;
  31288. + if (unlikely(err <= 0)) {
  31289. + if (err == -ENODATA
  31290. + || (err == -EOPNOTSUPP
  31291. + && ((ignore_flags & au_xattr_out_of_list)
  31292. + || (au_test_nfs_noacl(h_src->d_inode)
  31293. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  31294. + || !strcmp(name,
  31295. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  31296. + ))
  31297. + err = 0;
  31298. + if (err && (verbose || au_debug_test()))
  31299. + pr_err("%s, err %d\n", name, err);
  31300. + goto out;
  31301. + }
  31302. +
  31303. + /* unlock it temporary */
  31304. + h_idst = h_dst->d_inode;
  31305. + mutex_unlock(&h_idst->i_mutex);
  31306. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  31307. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  31308. + if (unlikely(err)) {
  31309. + if (verbose || au_debug_test())
  31310. + pr_err("%s, err %d\n", name, err);
  31311. + err = au_xattr_ignore(err, name, ignore_flags);
  31312. + }
  31313. +
  31314. +out:
  31315. + return err;
  31316. +}
  31317. +
  31318. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  31319. + unsigned int verbose)
  31320. +{
  31321. + int err, unlocked, acl_access, acl_default;
  31322. + ssize_t ssz;
  31323. + struct inode *h_isrc, *h_idst;
  31324. + char *value, *p, *o, *e;
  31325. +
  31326. + /* try stopping to update the source inode while we are referencing */
  31327. + /* there should not be the parent-child relationship between them */
  31328. + h_isrc = h_src->d_inode;
  31329. + h_idst = h_dst->d_inode;
  31330. + mutex_unlock(&h_idst->i_mutex);
  31331. + mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
  31332. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  31333. + unlocked = 0;
  31334. +
  31335. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  31336. + ssz = vfs_listxattr(h_src, NULL, 0);
  31337. + err = ssz;
  31338. + if (unlikely(err < 0)) {
  31339. + AuTraceErr(err);
  31340. + if (err == -ENODATA
  31341. + || err == -EOPNOTSUPP)
  31342. + err = 0; /* ignore */
  31343. + goto out;
  31344. + }
  31345. +
  31346. + err = 0;
  31347. + p = NULL;
  31348. + o = NULL;
  31349. + if (ssz) {
  31350. + err = -ENOMEM;
  31351. + p = kmalloc(ssz, GFP_NOFS);
  31352. + o = p;
  31353. + if (unlikely(!p))
  31354. + goto out;
  31355. + err = vfs_listxattr(h_src, p, ssz);
  31356. + }
  31357. + mutex_unlock(&h_isrc->i_mutex);
  31358. + unlocked = 1;
  31359. + AuDbg("err %d, ssz %zd\n", err, ssz);
  31360. + if (unlikely(err < 0))
  31361. + goto out_free;
  31362. +
  31363. + err = 0;
  31364. + e = p + ssz;
  31365. + value = NULL;
  31366. + acl_access = 0;
  31367. + acl_default = 0;
  31368. + while (!err && p < e) {
  31369. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  31370. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  31371. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  31372. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  31373. + - 1);
  31374. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  31375. + verbose);
  31376. + p += strlen(p) + 1;
  31377. + }
  31378. + AuTraceErr(err);
  31379. + ignore_flags |= au_xattr_out_of_list;
  31380. + if (!err && !acl_access) {
  31381. + err = au_do_cpup_xattr(h_dst, h_src,
  31382. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  31383. + ignore_flags, verbose);
  31384. + AuTraceErr(err);
  31385. + }
  31386. + if (!err && !acl_default) {
  31387. + err = au_do_cpup_xattr(h_dst, h_src,
  31388. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  31389. + ignore_flags, verbose);
  31390. + AuTraceErr(err);
  31391. + }
  31392. +
  31393. + kfree(value);
  31394. +
  31395. +out_free:
  31396. + kfree(o);
  31397. +out:
  31398. + if (!unlocked)
  31399. + mutex_unlock(&h_isrc->i_mutex);
  31400. + AuTraceErr(err);
  31401. + return err;
  31402. +}
  31403. +
  31404. +/* ---------------------------------------------------------------------- */
  31405. +
  31406. +enum {
  31407. + AU_XATTR_LIST,
  31408. + AU_XATTR_GET
  31409. +};
  31410. +
  31411. +struct au_lgxattr {
  31412. + int type;
  31413. + union {
  31414. + struct {
  31415. + char *list;
  31416. + size_t size;
  31417. + } list;
  31418. + struct {
  31419. + const char *name;
  31420. + void *value;
  31421. + size_t size;
  31422. + } get;
  31423. + } u;
  31424. +};
  31425. +
  31426. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  31427. +{
  31428. + ssize_t err;
  31429. + struct path h_path;
  31430. + struct super_block *sb;
  31431. +
  31432. + sb = dentry->d_sb;
  31433. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  31434. + if (unlikely(err))
  31435. + goto out;
  31436. + err = au_h_path_getattr(dentry, /*force*/1, &h_path);
  31437. + if (unlikely(err))
  31438. + goto out_si;
  31439. + if (unlikely(!h_path.dentry))
  31440. + /* illegally overlapped or something */
  31441. + goto out_di; /* pretending success */
  31442. +
  31443. + /* always topmost entry only */
  31444. + switch (arg->type) {
  31445. + case AU_XATTR_LIST:
  31446. + err = vfs_listxattr(h_path.dentry,
  31447. + arg->u.list.list, arg->u.list.size);
  31448. + break;
  31449. + case AU_XATTR_GET:
  31450. + err = vfs_getxattr(h_path.dentry,
  31451. + arg->u.get.name, arg->u.get.value,
  31452. + arg->u.get.size);
  31453. + break;
  31454. + }
  31455. +
  31456. +out_di:
  31457. + di_read_unlock(dentry, AuLock_IR);
  31458. +out_si:
  31459. + si_read_unlock(sb);
  31460. +out:
  31461. + AuTraceErr(err);
  31462. + return err;
  31463. +}
  31464. +
  31465. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  31466. +{
  31467. + struct au_lgxattr arg = {
  31468. + .type = AU_XATTR_LIST,
  31469. + .u.list = {
  31470. + .list = list,
  31471. + .size = size
  31472. + },
  31473. + };
  31474. +
  31475. + return au_lgxattr(dentry, &arg);
  31476. +}
  31477. +
  31478. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  31479. + size_t size)
  31480. +{
  31481. + struct au_lgxattr arg = {
  31482. + .type = AU_XATTR_GET,
  31483. + .u.get = {
  31484. + .name = name,
  31485. + .value = value,
  31486. + .size = size
  31487. + },
  31488. + };
  31489. +
  31490. + return au_lgxattr(dentry, &arg);
  31491. +}
  31492. +
  31493. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  31494. + size_t size, int flags)
  31495. +{
  31496. + struct au_srxattr arg = {
  31497. + .type = AU_XATTR_SET,
  31498. + .u.set = {
  31499. + .name = name,
  31500. + .value = value,
  31501. + .size = size,
  31502. + .flags = flags
  31503. + },
  31504. + };
  31505. +
  31506. + return au_srxattr(dentry, &arg);
  31507. +}
  31508. +
  31509. +int aufs_removexattr(struct dentry *dentry, const char *name)
  31510. +{
  31511. + struct au_srxattr arg = {
  31512. + .type = AU_XATTR_REMOVE,
  31513. + .u.remove = {
  31514. + .name = name
  31515. + },
  31516. + };
  31517. +
  31518. + return au_srxattr(dentry, &arg);
  31519. +}
  31520. +
  31521. +/* ---------------------------------------------------------------------- */
  31522. +
  31523. +#if 0
  31524. +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  31525. + const char *name, size_t name_len, int type)
  31526. +{
  31527. + return aufs_listxattr(dentry, list, list_size);
  31528. +}
  31529. +
  31530. +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
  31531. + size_t size, int type)
  31532. +{
  31533. + return aufs_getxattr(dentry, name, buffer, size);
  31534. +}
  31535. +
  31536. +static int au_xattr_set(struct dentry *dentry, const char *name,
  31537. + const void *value, size_t size, int flags, int type)
  31538. +{
  31539. + return aufs_setxattr(dentry, name, value, size, flags);
  31540. +}
  31541. +
  31542. +static const struct xattr_handler au_xattr_handler = {
  31543. + /* no prefix, no flags */
  31544. + .list = au_xattr_list,
  31545. + .get = au_xattr_get,
  31546. + .set = au_xattr_set
  31547. + /* why no remove? */
  31548. +};
  31549. +
  31550. +static const struct xattr_handler *au_xattr_handlers[] = {
  31551. + &au_xattr_handler
  31552. +};
  31553. +
  31554. +void au_xattr_init(struct super_block *sb)
  31555. +{
  31556. + /* sb->s_xattr = au_xattr_handlers; */
  31557. +}
  31558. +#endif
  31559. diff -Nur linux-4.0.4.orig/fs/aufs/xino.c linux-4.0.4/fs/aufs/xino.c
  31560. --- linux-4.0.4.orig/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
  31561. +++ linux-4.0.4/fs/aufs/xino.c 2015-05-30 22:11:30.000000000 +0200
  31562. @@ -0,0 +1,1305 @@
  31563. +/*
  31564. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  31565. + */
  31566. +
  31567. +/*
  31568. + * external inode number translation table and bitmap
  31569. + */
  31570. +
  31571. +#include <linux/seq_file.h>
  31572. +#include <linux/statfs.h>
  31573. +#include "aufs.h"
  31574. +
  31575. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  31576. +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
  31577. + loff_t *pos)
  31578. +{
  31579. + ssize_t err;
  31580. + mm_segment_t oldfs;
  31581. + union {
  31582. + void *k;
  31583. + char __user *u;
  31584. + } buf;
  31585. +
  31586. + buf.k = kbuf;
  31587. + oldfs = get_fs();
  31588. + set_fs(KERNEL_DS);
  31589. + do {
  31590. + /* todo: signal_pending? */
  31591. + err = func(file, buf.u, size, pos);
  31592. + } while (err == -EAGAIN || err == -EINTR);
  31593. + set_fs(oldfs);
  31594. +
  31595. +#if 0 /* reserved for future use */
  31596. + if (err > 0)
  31597. + fsnotify_access(file->f_path.dentry);
  31598. +#endif
  31599. +
  31600. + return err;
  31601. +}
  31602. +
  31603. +/* ---------------------------------------------------------------------- */
  31604. +
  31605. +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
  31606. + size_t size, loff_t *pos)
  31607. +{
  31608. + ssize_t err;
  31609. + mm_segment_t oldfs;
  31610. + union {
  31611. + void *k;
  31612. + const char __user *u;
  31613. + } buf;
  31614. +
  31615. + buf.k = kbuf;
  31616. + oldfs = get_fs();
  31617. + set_fs(KERNEL_DS);
  31618. + do {
  31619. + /* todo: signal_pending? */
  31620. + err = func(file, buf.u, size, pos);
  31621. + } while (err == -EAGAIN || err == -EINTR);
  31622. + set_fs(oldfs);
  31623. +
  31624. +#if 0 /* reserved for future use */
  31625. + if (err > 0)
  31626. + fsnotify_modify(file->f_path.dentry);
  31627. +#endif
  31628. +
  31629. + return err;
  31630. +}
  31631. +
  31632. +struct do_xino_fwrite_args {
  31633. + ssize_t *errp;
  31634. + au_writef_t func;
  31635. + struct file *file;
  31636. + void *buf;
  31637. + size_t size;
  31638. + loff_t *pos;
  31639. +};
  31640. +
  31641. +static void call_do_xino_fwrite(void *args)
  31642. +{
  31643. + struct do_xino_fwrite_args *a = args;
  31644. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  31645. +}
  31646. +
  31647. +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
  31648. + loff_t *pos)
  31649. +{
  31650. + ssize_t err;
  31651. +
  31652. + /* todo: signal block and no wkq? */
  31653. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  31654. + lockdep_off();
  31655. + err = do_xino_fwrite(func, file, buf, size, pos);
  31656. + lockdep_on();
  31657. + } else {
  31658. + /*
  31659. + * it breaks RLIMIT_FSIZE and normal user's limit,
  31660. + * users should care about quota and real 'filesystem full.'
  31661. + */
  31662. + int wkq_err;
  31663. + struct do_xino_fwrite_args args = {
  31664. + .errp = &err,
  31665. + .func = func,
  31666. + .file = file,
  31667. + .buf = buf,
  31668. + .size = size,
  31669. + .pos = pos
  31670. + };
  31671. +
  31672. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  31673. + if (unlikely(wkq_err))
  31674. + err = wkq_err;
  31675. + }
  31676. +
  31677. + return err;
  31678. +}
  31679. +
  31680. +/* ---------------------------------------------------------------------- */
  31681. +
  31682. +/*
  31683. + * create a new xinofile at the same place/path as @base_file.
  31684. + */
  31685. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
  31686. +{
  31687. + struct file *file;
  31688. + struct dentry *base, *parent;
  31689. + struct inode *dir, *delegated;
  31690. + struct qstr *name;
  31691. + struct path path;
  31692. + int err;
  31693. +
  31694. + base = base_file->f_path.dentry;
  31695. + parent = base->d_parent; /* dir inode is locked */
  31696. + dir = parent->d_inode;
  31697. + IMustLock(dir);
  31698. +
  31699. + file = ERR_PTR(-EINVAL);
  31700. + name = &base->d_name;
  31701. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  31702. + if (IS_ERR(path.dentry)) {
  31703. + file = (void *)path.dentry;
  31704. + pr_err("%pd lookup err %ld\n",
  31705. + base, PTR_ERR(path.dentry));
  31706. + goto out;
  31707. + }
  31708. +
  31709. + /* no need to mnt_want_write() since we call dentry_open() later */
  31710. + err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
  31711. + if (unlikely(err)) {
  31712. + file = ERR_PTR(err);
  31713. + pr_err("%pd create err %d\n", base, err);
  31714. + goto out_dput;
  31715. + }
  31716. +
  31717. + path.mnt = base_file->f_path.mnt;
  31718. + file = vfsub_dentry_open(&path,
  31719. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  31720. + /* | __FMODE_NONOTIFY */);
  31721. + if (IS_ERR(file)) {
  31722. + pr_err("%pd open err %ld\n", base, PTR_ERR(file));
  31723. + goto out_dput;
  31724. + }
  31725. +
  31726. + delegated = NULL;
  31727. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  31728. + if (unlikely(err == -EWOULDBLOCK)) {
  31729. + pr_warn("cannot retry for NFSv4 delegation"
  31730. + " for an internal unlink\n");
  31731. + iput(delegated);
  31732. + }
  31733. + if (unlikely(err)) {
  31734. + pr_err("%pd unlink err %d\n", base, err);
  31735. + goto out_fput;
  31736. + }
  31737. +
  31738. + if (copy_src) {
  31739. + /* no one can touch copy_src xino */
  31740. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  31741. + if (unlikely(err)) {
  31742. + pr_err("%pd copy err %d\n", base, err);
  31743. + goto out_fput;
  31744. + }
  31745. + }
  31746. + goto out_dput; /* success */
  31747. +
  31748. +out_fput:
  31749. + fput(file);
  31750. + file = ERR_PTR(err);
  31751. +out_dput:
  31752. + dput(path.dentry);
  31753. +out:
  31754. + return file;
  31755. +}
  31756. +
  31757. +struct au_xino_lock_dir {
  31758. + struct au_hinode *hdir;
  31759. + struct dentry *parent;
  31760. + struct mutex *mtx;
  31761. +};
  31762. +
  31763. +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
  31764. + struct au_xino_lock_dir *ldir)
  31765. +{
  31766. + aufs_bindex_t brid, bindex;
  31767. +
  31768. + ldir->hdir = NULL;
  31769. + bindex = -1;
  31770. + brid = au_xino_brid(sb);
  31771. + if (brid >= 0)
  31772. + bindex = au_br_index(sb, brid);
  31773. + if (bindex >= 0) {
  31774. + ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
  31775. + au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  31776. + } else {
  31777. + ldir->parent = dget_parent(xino->f_path.dentry);
  31778. + ldir->mtx = &ldir->parent->d_inode->i_mutex;
  31779. + mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
  31780. + }
  31781. +}
  31782. +
  31783. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  31784. +{
  31785. + if (ldir->hdir)
  31786. + au_hn_imtx_unlock(ldir->hdir);
  31787. + else {
  31788. + mutex_unlock(ldir->mtx);
  31789. + dput(ldir->parent);
  31790. + }
  31791. +}
  31792. +
  31793. +/* ---------------------------------------------------------------------- */
  31794. +
  31795. +/* trucate xino files asynchronously */
  31796. +
  31797. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
  31798. +{
  31799. + int err;
  31800. + unsigned long jiffy;
  31801. + blkcnt_t blocks;
  31802. + aufs_bindex_t bi, bend;
  31803. + struct kstatfs *st;
  31804. + struct au_branch *br;
  31805. + struct file *new_xino, *file;
  31806. + struct super_block *h_sb;
  31807. + struct au_xino_lock_dir ldir;
  31808. +
  31809. + err = -ENOMEM;
  31810. + st = kzalloc(sizeof(*st), GFP_NOFS);
  31811. + if (unlikely(!st))
  31812. + goto out;
  31813. +
  31814. + err = -EINVAL;
  31815. + bend = au_sbend(sb);
  31816. + if (unlikely(bindex < 0 || bend < bindex))
  31817. + goto out_st;
  31818. + br = au_sbr(sb, bindex);
  31819. + file = br->br_xino.xi_file;
  31820. + if (!file)
  31821. + goto out_st;
  31822. +
  31823. + err = vfs_statfs(&file->f_path, st);
  31824. + if (unlikely(err))
  31825. + AuErr1("statfs err %d, ignored\n", err);
  31826. + jiffy = jiffies;
  31827. + blocks = file_inode(file)->i_blocks;
  31828. + pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  31829. + bindex, (u64)blocks, st->f_bfree, st->f_blocks);
  31830. +
  31831. + au_xino_lock_dir(sb, file, &ldir);
  31832. + /* mnt_want_write() is unnecessary here */
  31833. + new_xino = au_xino_create2(file, file);
  31834. + au_xino_unlock_dir(&ldir);
  31835. + err = PTR_ERR(new_xino);
  31836. + if (IS_ERR(new_xino)) {
  31837. + pr_err("err %d, ignored\n", err);
  31838. + goto out_st;
  31839. + }
  31840. + err = 0;
  31841. + fput(file);
  31842. + br->br_xino.xi_file = new_xino;
  31843. +
  31844. + h_sb = au_br_sb(br);
  31845. + for (bi = 0; bi <= bend; bi++) {
  31846. + if (unlikely(bi == bindex))
  31847. + continue;
  31848. + br = au_sbr(sb, bi);
  31849. + if (au_br_sb(br) != h_sb)
  31850. + continue;
  31851. +
  31852. + fput(br->br_xino.xi_file);
  31853. + br->br_xino.xi_file = new_xino;
  31854. + get_file(new_xino);
  31855. + }
  31856. +
  31857. + err = vfs_statfs(&new_xino->f_path, st);
  31858. + if (!err) {
  31859. + pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  31860. + bindex, (u64)file_inode(new_xino)->i_blocks,
  31861. + st->f_bfree, st->f_blocks);
  31862. + if (file_inode(new_xino)->i_blocks < blocks)
  31863. + au_sbi(sb)->si_xino_jiffy = jiffy;
  31864. + } else
  31865. + AuErr1("statfs err %d, ignored\n", err);
  31866. +
  31867. +out_st:
  31868. + kfree(st);
  31869. +out:
  31870. + return err;
  31871. +}
  31872. +
  31873. +struct xino_do_trunc_args {
  31874. + struct super_block *sb;
  31875. + struct au_branch *br;
  31876. +};
  31877. +
  31878. +static void xino_do_trunc(void *_args)
  31879. +{
  31880. + struct xino_do_trunc_args *args = _args;
  31881. + struct super_block *sb;
  31882. + struct au_branch *br;
  31883. + struct inode *dir;
  31884. + int err;
  31885. + aufs_bindex_t bindex;
  31886. +
  31887. + err = 0;
  31888. + sb = args->sb;
  31889. + dir = sb->s_root->d_inode;
  31890. + br = args->br;
  31891. +
  31892. + si_noflush_write_lock(sb);
  31893. + ii_read_lock_parent(dir);
  31894. + bindex = au_br_index(sb, br->br_id);
  31895. + err = au_xino_trunc(sb, bindex);
  31896. + ii_read_unlock(dir);
  31897. + if (unlikely(err))
  31898. + pr_warn("err b%d, (%d)\n", bindex, err);
  31899. + atomic_dec(&br->br_xino_running);
  31900. + atomic_dec(&br->br_count);
  31901. + si_write_unlock(sb);
  31902. + au_nwt_done(&au_sbi(sb)->si_nowait);
  31903. + kfree(args);
  31904. +}
  31905. +
  31906. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  31907. +{
  31908. + int err;
  31909. + struct kstatfs st;
  31910. + struct au_sbinfo *sbinfo;
  31911. +
  31912. + /* todo: si_xino_expire and the ratio should be customizable */
  31913. + sbinfo = au_sbi(sb);
  31914. + if (time_before(jiffies,
  31915. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  31916. + return 0;
  31917. +
  31918. + /* truncation border */
  31919. + err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
  31920. + if (unlikely(err)) {
  31921. + AuErr1("statfs err %d, ignored\n", err);
  31922. + return 0;
  31923. + }
  31924. + if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
  31925. + return 0;
  31926. +
  31927. + return 1;
  31928. +}
  31929. +
  31930. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  31931. +{
  31932. + struct xino_do_trunc_args *args;
  31933. + int wkq_err;
  31934. +
  31935. + if (!xino_trunc_test(sb, br))
  31936. + return;
  31937. +
  31938. + if (atomic_inc_return(&br->br_xino_running) > 1)
  31939. + goto out;
  31940. +
  31941. + /* lock and kfree() will be called in trunc_xino() */
  31942. + args = kmalloc(sizeof(*args), GFP_NOFS);
  31943. + if (unlikely(!args)) {
  31944. + AuErr1("no memory\n");
  31945. + goto out_args;
  31946. + }
  31947. +
  31948. + atomic_inc(&br->br_count);
  31949. + args->sb = sb;
  31950. + args->br = br;
  31951. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  31952. + if (!wkq_err)
  31953. + return; /* success */
  31954. +
  31955. + pr_err("wkq %d\n", wkq_err);
  31956. + atomic_dec(&br->br_count);
  31957. +
  31958. +out_args:
  31959. + kfree(args);
  31960. +out:
  31961. + atomic_dec(&br->br_xino_running);
  31962. +}
  31963. +
  31964. +/* ---------------------------------------------------------------------- */
  31965. +
  31966. +static int au_xino_do_write(au_writef_t write, struct file *file,
  31967. + ino_t h_ino, ino_t ino)
  31968. +{
  31969. + loff_t pos;
  31970. + ssize_t sz;
  31971. +
  31972. + pos = h_ino;
  31973. + if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
  31974. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  31975. + return -EFBIG;
  31976. + }
  31977. + pos *= sizeof(ino);
  31978. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
  31979. + if (sz == sizeof(ino))
  31980. + return 0; /* success */
  31981. +
  31982. + AuIOErr("write failed (%zd)\n", sz);
  31983. + return -EIO;
  31984. +}
  31985. +
  31986. +/*
  31987. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  31988. + * at the position of @h_ino.
  31989. + * even if @ino is zero, it is written to the xinofile and means no entry.
  31990. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  31991. + * try truncating it.
  31992. + */
  31993. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  31994. + ino_t ino)
  31995. +{
  31996. + int err;
  31997. + unsigned int mnt_flags;
  31998. + struct au_branch *br;
  31999. +
  32000. + BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
  32001. + || ((loff_t)-1) > 0);
  32002. + SiMustAnyLock(sb);
  32003. +
  32004. + mnt_flags = au_mntflags(sb);
  32005. + if (!au_opt_test(mnt_flags, XINO))
  32006. + return 0;
  32007. +
  32008. + br = au_sbr(sb, bindex);
  32009. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  32010. + h_ino, ino);
  32011. + if (!err) {
  32012. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  32013. + && au_test_fs_trunc_xino(au_br_sb(br)))
  32014. + xino_try_trunc(sb, br);
  32015. + return 0; /* success */
  32016. + }
  32017. +
  32018. + AuIOErr("write failed (%d)\n", err);
  32019. + return -EIO;
  32020. +}
  32021. +
  32022. +/* ---------------------------------------------------------------------- */
  32023. +
  32024. +/* aufs inode number bitmap */
  32025. +
  32026. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  32027. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  32028. +{
  32029. + ino_t ino;
  32030. +
  32031. + AuDebugOn(bit < 0 || page_bits <= bit);
  32032. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  32033. + return ino;
  32034. +}
  32035. +
  32036. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  32037. +{
  32038. + AuDebugOn(ino < AUFS_FIRST_INO);
  32039. + ino -= AUFS_FIRST_INO;
  32040. + *pindex = ino / page_bits;
  32041. + *bit = ino % page_bits;
  32042. +}
  32043. +
  32044. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  32045. +{
  32046. + int err;
  32047. + loff_t pos;
  32048. + ssize_t sz;
  32049. + struct au_sbinfo *sbinfo;
  32050. + struct file *xib;
  32051. + unsigned long *p;
  32052. +
  32053. + sbinfo = au_sbi(sb);
  32054. + MtxMustLock(&sbinfo->si_xib_mtx);
  32055. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  32056. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  32057. +
  32058. + if (pindex == sbinfo->si_xib_last_pindex)
  32059. + return 0;
  32060. +
  32061. + xib = sbinfo->si_xib;
  32062. + p = sbinfo->si_xib_buf;
  32063. + pos = sbinfo->si_xib_last_pindex;
  32064. + pos *= PAGE_SIZE;
  32065. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  32066. + if (unlikely(sz != PAGE_SIZE))
  32067. + goto out;
  32068. +
  32069. + pos = pindex;
  32070. + pos *= PAGE_SIZE;
  32071. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  32072. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  32073. + else {
  32074. + memset(p, 0, PAGE_SIZE);
  32075. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  32076. + }
  32077. + if (sz == PAGE_SIZE) {
  32078. + sbinfo->si_xib_last_pindex = pindex;
  32079. + return 0; /* success */
  32080. + }
  32081. +
  32082. +out:
  32083. + AuIOErr1("write failed (%zd)\n", sz);
  32084. + err = sz;
  32085. + if (sz >= 0)
  32086. + err = -EIO;
  32087. + return err;
  32088. +}
  32089. +
  32090. +/* ---------------------------------------------------------------------- */
  32091. +
  32092. +static void au_xib_clear_bit(struct inode *inode)
  32093. +{
  32094. + int err, bit;
  32095. + unsigned long pindex;
  32096. + struct super_block *sb;
  32097. + struct au_sbinfo *sbinfo;
  32098. +
  32099. + AuDebugOn(inode->i_nlink);
  32100. +
  32101. + sb = inode->i_sb;
  32102. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  32103. + AuDebugOn(page_bits <= bit);
  32104. + sbinfo = au_sbi(sb);
  32105. + mutex_lock(&sbinfo->si_xib_mtx);
  32106. + err = xib_pindex(sb, pindex);
  32107. + if (!err) {
  32108. + clear_bit(bit, sbinfo->si_xib_buf);
  32109. + sbinfo->si_xib_next_bit = bit;
  32110. + }
  32111. + mutex_unlock(&sbinfo->si_xib_mtx);
  32112. +}
  32113. +
  32114. +/* for s_op->delete_inode() */
  32115. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  32116. +{
  32117. + int err;
  32118. + unsigned int mnt_flags;
  32119. + aufs_bindex_t bindex, bend, bi;
  32120. + unsigned char try_trunc;
  32121. + struct au_iinfo *iinfo;
  32122. + struct super_block *sb;
  32123. + struct au_hinode *hi;
  32124. + struct inode *h_inode;
  32125. + struct au_branch *br;
  32126. + au_writef_t xwrite;
  32127. +
  32128. + sb = inode->i_sb;
  32129. + mnt_flags = au_mntflags(sb);
  32130. + if (!au_opt_test(mnt_flags, XINO)
  32131. + || inode->i_ino == AUFS_ROOT_INO)
  32132. + return;
  32133. +
  32134. + if (unlinked) {
  32135. + au_xigen_inc(inode);
  32136. + au_xib_clear_bit(inode);
  32137. + }
  32138. +
  32139. + iinfo = au_ii(inode);
  32140. + if (!iinfo)
  32141. + return;
  32142. +
  32143. + bindex = iinfo->ii_bstart;
  32144. + if (bindex < 0)
  32145. + return;
  32146. +
  32147. + xwrite = au_sbi(sb)->si_xwrite;
  32148. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  32149. + hi = iinfo->ii_hinode + bindex;
  32150. + bend = iinfo->ii_bend;
  32151. + for (; bindex <= bend; bindex++, hi++) {
  32152. + h_inode = hi->hi_inode;
  32153. + if (!h_inode
  32154. + || (!unlinked && h_inode->i_nlink))
  32155. + continue;
  32156. +
  32157. + /* inode may not be revalidated */
  32158. + bi = au_br_index(sb, hi->hi_id);
  32159. + if (bi < 0)
  32160. + continue;
  32161. +
  32162. + br = au_sbr(sb, bi);
  32163. + err = au_xino_do_write(xwrite, br->br_xino.xi_file,
  32164. + h_inode->i_ino, /*ino*/0);
  32165. + if (!err && try_trunc
  32166. + && au_test_fs_trunc_xino(au_br_sb(br)))
  32167. + xino_try_trunc(sb, br);
  32168. + }
  32169. +}
  32170. +
  32171. +/* get an unused inode number from bitmap */
  32172. +ino_t au_xino_new_ino(struct super_block *sb)
  32173. +{
  32174. + ino_t ino;
  32175. + unsigned long *p, pindex, ul, pend;
  32176. + struct au_sbinfo *sbinfo;
  32177. + struct file *file;
  32178. + int free_bit, err;
  32179. +
  32180. + if (!au_opt_test(au_mntflags(sb), XINO))
  32181. + return iunique(sb, AUFS_FIRST_INO);
  32182. +
  32183. + sbinfo = au_sbi(sb);
  32184. + mutex_lock(&sbinfo->si_xib_mtx);
  32185. + p = sbinfo->si_xib_buf;
  32186. + free_bit = sbinfo->si_xib_next_bit;
  32187. + if (free_bit < page_bits && !test_bit(free_bit, p))
  32188. + goto out; /* success */
  32189. + free_bit = find_first_zero_bit(p, page_bits);
  32190. + if (free_bit < page_bits)
  32191. + goto out; /* success */
  32192. +
  32193. + pindex = sbinfo->si_xib_last_pindex;
  32194. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  32195. + err = xib_pindex(sb, ul);
  32196. + if (unlikely(err))
  32197. + goto out_err;
  32198. + free_bit = find_first_zero_bit(p, page_bits);
  32199. + if (free_bit < page_bits)
  32200. + goto out; /* success */
  32201. + }
  32202. +
  32203. + file = sbinfo->si_xib;
  32204. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  32205. + for (ul = pindex + 1; ul <= pend; ul++) {
  32206. + err = xib_pindex(sb, ul);
  32207. + if (unlikely(err))
  32208. + goto out_err;
  32209. + free_bit = find_first_zero_bit(p, page_bits);
  32210. + if (free_bit < page_bits)
  32211. + goto out; /* success */
  32212. + }
  32213. + BUG();
  32214. +
  32215. +out:
  32216. + set_bit(free_bit, p);
  32217. + sbinfo->si_xib_next_bit = free_bit + 1;
  32218. + pindex = sbinfo->si_xib_last_pindex;
  32219. + mutex_unlock(&sbinfo->si_xib_mtx);
  32220. + ino = xib_calc_ino(pindex, free_bit);
  32221. + AuDbg("i%lu\n", (unsigned long)ino);
  32222. + return ino;
  32223. +out_err:
  32224. + mutex_unlock(&sbinfo->si_xib_mtx);
  32225. + AuDbg("i0\n");
  32226. + return 0;
  32227. +}
  32228. +
  32229. +/*
  32230. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  32231. + * at the position of @h_ino.
  32232. + * if @ino does not exist and @do_new is true, get new one.
  32233. + */
  32234. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  32235. + ino_t *ino)
  32236. +{
  32237. + int err;
  32238. + ssize_t sz;
  32239. + loff_t pos;
  32240. + struct file *file;
  32241. + struct au_sbinfo *sbinfo;
  32242. +
  32243. + *ino = 0;
  32244. + if (!au_opt_test(au_mntflags(sb), XINO))
  32245. + return 0; /* no xino */
  32246. +
  32247. + err = 0;
  32248. + sbinfo = au_sbi(sb);
  32249. + pos = h_ino;
  32250. + if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
  32251. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  32252. + return -EFBIG;
  32253. + }
  32254. + pos *= sizeof(*ino);
  32255. +
  32256. + file = au_sbr(sb, bindex)->br_xino.xi_file;
  32257. + if (vfsub_f_size_read(file) < pos + sizeof(*ino))
  32258. + return 0; /* no ino */
  32259. +
  32260. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
  32261. + if (sz == sizeof(*ino))
  32262. + return 0; /* success */
  32263. +
  32264. + err = sz;
  32265. + if (unlikely(sz >= 0)) {
  32266. + err = -EIO;
  32267. + AuIOErr("xino read error (%zd)\n", sz);
  32268. + }
  32269. +
  32270. + return err;
  32271. +}
  32272. +
  32273. +/* ---------------------------------------------------------------------- */
  32274. +
  32275. +/* create and set a new xino file */
  32276. +
  32277. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
  32278. +{
  32279. + struct file *file;
  32280. + struct dentry *h_parent, *d;
  32281. + struct inode *h_dir;
  32282. + int err;
  32283. +
  32284. + /*
  32285. + * at mount-time, and the xino file is the default path,
  32286. + * hnotify is disabled so we have no notify events to ignore.
  32287. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  32288. + */
  32289. + file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  32290. + /* | __FMODE_NONOTIFY */,
  32291. + S_IRUGO | S_IWUGO);
  32292. + if (IS_ERR(file)) {
  32293. + if (!silent)
  32294. + pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
  32295. + return file;
  32296. + }
  32297. +
  32298. + /* keep file count */
  32299. + h_parent = dget_parent(file->f_path.dentry);
  32300. + h_dir = h_parent->d_inode;
  32301. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  32302. + /* mnt_want_write() is unnecessary here */
  32303. + /* no delegation since it is just created */
  32304. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL, /*force*/0);
  32305. + mutex_unlock(&h_dir->i_mutex);
  32306. + dput(h_parent);
  32307. + if (unlikely(err)) {
  32308. + if (!silent)
  32309. + pr_err("unlink %s(%d)\n", fname, err);
  32310. + goto out;
  32311. + }
  32312. +
  32313. + err = -EINVAL;
  32314. + d = file->f_path.dentry;
  32315. + if (unlikely(sb == d->d_sb)) {
  32316. + if (!silent)
  32317. + pr_err("%s must be outside\n", fname);
  32318. + goto out;
  32319. + }
  32320. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  32321. + if (!silent)
  32322. + pr_err("xino doesn't support %s(%s)\n",
  32323. + fname, au_sbtype(d->d_sb));
  32324. + goto out;
  32325. + }
  32326. + return file; /* success */
  32327. +
  32328. +out:
  32329. + fput(file);
  32330. + file = ERR_PTR(err);
  32331. + return file;
  32332. +}
  32333. +
  32334. +/*
  32335. + * find another branch who is on the same filesystem of the specified
  32336. + * branch{@btgt}. search until @bend.
  32337. + */
  32338. +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  32339. + aufs_bindex_t bend)
  32340. +{
  32341. + aufs_bindex_t bindex;
  32342. + struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
  32343. +
  32344. + for (bindex = 0; bindex < btgt; bindex++)
  32345. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  32346. + return bindex;
  32347. + for (bindex++; bindex <= bend; bindex++)
  32348. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  32349. + return bindex;
  32350. + return -1;
  32351. +}
  32352. +
  32353. +/* ---------------------------------------------------------------------- */
  32354. +
  32355. +/*
  32356. + * initialize the xinofile for the specified branch @br
  32357. + * at the place/path where @base_file indicates.
  32358. + * test whether another branch is on the same filesystem or not,
  32359. + * if @do_test is true.
  32360. + */
  32361. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  32362. + struct file *base_file, int do_test)
  32363. +{
  32364. + int err;
  32365. + ino_t ino;
  32366. + aufs_bindex_t bend, bindex;
  32367. + struct au_branch *shared_br, *b;
  32368. + struct file *file;
  32369. + struct super_block *tgt_sb;
  32370. +
  32371. + shared_br = NULL;
  32372. + bend = au_sbend(sb);
  32373. + if (do_test) {
  32374. + tgt_sb = au_br_sb(br);
  32375. + for (bindex = 0; bindex <= bend; bindex++) {
  32376. + b = au_sbr(sb, bindex);
  32377. + if (tgt_sb == au_br_sb(b)) {
  32378. + shared_br = b;
  32379. + break;
  32380. + }
  32381. + }
  32382. + }
  32383. +
  32384. + if (!shared_br || !shared_br->br_xino.xi_file) {
  32385. + struct au_xino_lock_dir ldir;
  32386. +
  32387. + au_xino_lock_dir(sb, base_file, &ldir);
  32388. + /* mnt_want_write() is unnecessary here */
  32389. + file = au_xino_create2(base_file, NULL);
  32390. + au_xino_unlock_dir(&ldir);
  32391. + err = PTR_ERR(file);
  32392. + if (IS_ERR(file))
  32393. + goto out;
  32394. + br->br_xino.xi_file = file;
  32395. + } else {
  32396. + br->br_xino.xi_file = shared_br->br_xino.xi_file;
  32397. + get_file(br->br_xino.xi_file);
  32398. + }
  32399. +
  32400. + ino = AUFS_ROOT_INO;
  32401. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  32402. + h_ino, ino);
  32403. + if (unlikely(err)) {
  32404. + fput(br->br_xino.xi_file);
  32405. + br->br_xino.xi_file = NULL;
  32406. + }
  32407. +
  32408. +out:
  32409. + return err;
  32410. +}
  32411. +
  32412. +/* ---------------------------------------------------------------------- */
  32413. +
  32414. +/* trucate a xino bitmap file */
  32415. +
  32416. +/* todo: slow */
  32417. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  32418. +{
  32419. + int err, bit;
  32420. + ssize_t sz;
  32421. + unsigned long pindex;
  32422. + loff_t pos, pend;
  32423. + struct au_sbinfo *sbinfo;
  32424. + au_readf_t func;
  32425. + ino_t *ino;
  32426. + unsigned long *p;
  32427. +
  32428. + err = 0;
  32429. + sbinfo = au_sbi(sb);
  32430. + MtxMustLock(&sbinfo->si_xib_mtx);
  32431. + p = sbinfo->si_xib_buf;
  32432. + func = sbinfo->si_xread;
  32433. + pend = vfsub_f_size_read(file);
  32434. + pos = 0;
  32435. + while (pos < pend) {
  32436. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  32437. + err = sz;
  32438. + if (unlikely(sz <= 0))
  32439. + goto out;
  32440. +
  32441. + err = 0;
  32442. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  32443. + if (unlikely(*ino < AUFS_FIRST_INO))
  32444. + continue;
  32445. +
  32446. + xib_calc_bit(*ino, &pindex, &bit);
  32447. + AuDebugOn(page_bits <= bit);
  32448. + err = xib_pindex(sb, pindex);
  32449. + if (!err)
  32450. + set_bit(bit, p);
  32451. + else
  32452. + goto out;
  32453. + }
  32454. + }
  32455. +
  32456. +out:
  32457. + return err;
  32458. +}
  32459. +
  32460. +static int xib_restore(struct super_block *sb)
  32461. +{
  32462. + int err;
  32463. + aufs_bindex_t bindex, bend;
  32464. + void *page;
  32465. +
  32466. + err = -ENOMEM;
  32467. + page = (void *)__get_free_page(GFP_NOFS);
  32468. + if (unlikely(!page))
  32469. + goto out;
  32470. +
  32471. + err = 0;
  32472. + bend = au_sbend(sb);
  32473. + for (bindex = 0; !err && bindex <= bend; bindex++)
  32474. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
  32475. + err = do_xib_restore
  32476. + (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
  32477. + else
  32478. + AuDbg("b%d\n", bindex);
  32479. + free_page((unsigned long)page);
  32480. +
  32481. +out:
  32482. + return err;
  32483. +}
  32484. +
  32485. +int au_xib_trunc(struct super_block *sb)
  32486. +{
  32487. + int err;
  32488. + ssize_t sz;
  32489. + loff_t pos;
  32490. + struct au_xino_lock_dir ldir;
  32491. + struct au_sbinfo *sbinfo;
  32492. + unsigned long *p;
  32493. + struct file *file;
  32494. +
  32495. + SiMustWriteLock(sb);
  32496. +
  32497. + err = 0;
  32498. + sbinfo = au_sbi(sb);
  32499. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  32500. + goto out;
  32501. +
  32502. + file = sbinfo->si_xib;
  32503. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  32504. + goto out;
  32505. +
  32506. + au_xino_lock_dir(sb, file, &ldir);
  32507. + /* mnt_want_write() is unnecessary here */
  32508. + file = au_xino_create2(sbinfo->si_xib, NULL);
  32509. + au_xino_unlock_dir(&ldir);
  32510. + err = PTR_ERR(file);
  32511. + if (IS_ERR(file))
  32512. + goto out;
  32513. + fput(sbinfo->si_xib);
  32514. + sbinfo->si_xib = file;
  32515. +
  32516. + p = sbinfo->si_xib_buf;
  32517. + memset(p, 0, PAGE_SIZE);
  32518. + pos = 0;
  32519. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  32520. + if (unlikely(sz != PAGE_SIZE)) {
  32521. + err = sz;
  32522. + AuIOErr("err %d\n", err);
  32523. + if (sz >= 0)
  32524. + err = -EIO;
  32525. + goto out;
  32526. + }
  32527. +
  32528. + mutex_lock(&sbinfo->si_xib_mtx);
  32529. + /* mnt_want_write() is unnecessary here */
  32530. + err = xib_restore(sb);
  32531. + mutex_unlock(&sbinfo->si_xib_mtx);
  32532. +
  32533. +out:
  32534. + return err;
  32535. +}
  32536. +
  32537. +/* ---------------------------------------------------------------------- */
  32538. +
  32539. +/*
  32540. + * xino mount option handlers
  32541. + */
  32542. +static au_readf_t find_readf(struct file *h_file)
  32543. +{
  32544. + const struct file_operations *fop = h_file->f_op;
  32545. +
  32546. + if (fop->read)
  32547. + return fop->read;
  32548. + if (fop->aio_read)
  32549. + return do_sync_read;
  32550. + if (fop->read_iter)
  32551. + return new_sync_read;
  32552. + return ERR_PTR(-ENOSYS);
  32553. +}
  32554. +
  32555. +static au_writef_t find_writef(struct file *h_file)
  32556. +{
  32557. + const struct file_operations *fop = h_file->f_op;
  32558. +
  32559. + if (fop->write)
  32560. + return fop->write;
  32561. + if (fop->aio_write)
  32562. + return do_sync_write;
  32563. + if (fop->write_iter)
  32564. + return new_sync_write;
  32565. + return ERR_PTR(-ENOSYS);
  32566. +}
  32567. +
  32568. +/* xino bitmap */
  32569. +static void xino_clear_xib(struct super_block *sb)
  32570. +{
  32571. + struct au_sbinfo *sbinfo;
  32572. +
  32573. + SiMustWriteLock(sb);
  32574. +
  32575. + sbinfo = au_sbi(sb);
  32576. + sbinfo->si_xread = NULL;
  32577. + sbinfo->si_xwrite = NULL;
  32578. + if (sbinfo->si_xib)
  32579. + fput(sbinfo->si_xib);
  32580. + sbinfo->si_xib = NULL;
  32581. + free_page((unsigned long)sbinfo->si_xib_buf);
  32582. + sbinfo->si_xib_buf = NULL;
  32583. +}
  32584. +
  32585. +static int au_xino_set_xib(struct super_block *sb, struct file *base)
  32586. +{
  32587. + int err;
  32588. + loff_t pos;
  32589. + struct au_sbinfo *sbinfo;
  32590. + struct file *file;
  32591. +
  32592. + SiMustWriteLock(sb);
  32593. +
  32594. + sbinfo = au_sbi(sb);
  32595. + file = au_xino_create2(base, sbinfo->si_xib);
  32596. + err = PTR_ERR(file);
  32597. + if (IS_ERR(file))
  32598. + goto out;
  32599. + if (sbinfo->si_xib)
  32600. + fput(sbinfo->si_xib);
  32601. + sbinfo->si_xib = file;
  32602. + sbinfo->si_xread = find_readf(file);
  32603. + sbinfo->si_xwrite = find_writef(file);
  32604. +
  32605. + err = -ENOMEM;
  32606. + if (!sbinfo->si_xib_buf)
  32607. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  32608. + if (unlikely(!sbinfo->si_xib_buf))
  32609. + goto out_unset;
  32610. +
  32611. + sbinfo->si_xib_last_pindex = 0;
  32612. + sbinfo->si_xib_next_bit = 0;
  32613. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  32614. + pos = 0;
  32615. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  32616. + PAGE_SIZE, &pos);
  32617. + if (unlikely(err != PAGE_SIZE))
  32618. + goto out_free;
  32619. + }
  32620. + err = 0;
  32621. + goto out; /* success */
  32622. +
  32623. +out_free:
  32624. + free_page((unsigned long)sbinfo->si_xib_buf);
  32625. + sbinfo->si_xib_buf = NULL;
  32626. + if (err >= 0)
  32627. + err = -EIO;
  32628. +out_unset:
  32629. + fput(sbinfo->si_xib);
  32630. + sbinfo->si_xib = NULL;
  32631. + sbinfo->si_xread = NULL;
  32632. + sbinfo->si_xwrite = NULL;
  32633. +out:
  32634. + return err;
  32635. +}
  32636. +
  32637. +/* xino for each branch */
  32638. +static void xino_clear_br(struct super_block *sb)
  32639. +{
  32640. + aufs_bindex_t bindex, bend;
  32641. + struct au_branch *br;
  32642. +
  32643. + bend = au_sbend(sb);
  32644. + for (bindex = 0; bindex <= bend; bindex++) {
  32645. + br = au_sbr(sb, bindex);
  32646. + if (!br || !br->br_xino.xi_file)
  32647. + continue;
  32648. +
  32649. + fput(br->br_xino.xi_file);
  32650. + br->br_xino.xi_file = NULL;
  32651. + }
  32652. +}
  32653. +
  32654. +static int au_xino_set_br(struct super_block *sb, struct file *base)
  32655. +{
  32656. + int err;
  32657. + ino_t ino;
  32658. + aufs_bindex_t bindex, bend, bshared;
  32659. + struct {
  32660. + struct file *old, *new;
  32661. + } *fpair, *p;
  32662. + struct au_branch *br;
  32663. + struct inode *inode;
  32664. + au_writef_t writef;
  32665. +
  32666. + SiMustWriteLock(sb);
  32667. +
  32668. + err = -ENOMEM;
  32669. + bend = au_sbend(sb);
  32670. + fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
  32671. + if (unlikely(!fpair))
  32672. + goto out;
  32673. +
  32674. + inode = sb->s_root->d_inode;
  32675. + ino = AUFS_ROOT_INO;
  32676. + writef = au_sbi(sb)->si_xwrite;
  32677. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
  32678. + br = au_sbr(sb, bindex);
  32679. + bshared = is_sb_shared(sb, bindex, bindex - 1);
  32680. + if (bshared >= 0) {
  32681. + /* shared xino */
  32682. + *p = fpair[bshared];
  32683. + get_file(p->new);
  32684. + }
  32685. +
  32686. + if (!p->new) {
  32687. + /* new xino */
  32688. + p->old = br->br_xino.xi_file;
  32689. + p->new = au_xino_create2(base, br->br_xino.xi_file);
  32690. + err = PTR_ERR(p->new);
  32691. + if (IS_ERR(p->new)) {
  32692. + p->new = NULL;
  32693. + goto out_pair;
  32694. + }
  32695. + }
  32696. +
  32697. + err = au_xino_do_write(writef, p->new,
  32698. + au_h_iptr(inode, bindex)->i_ino, ino);
  32699. + if (unlikely(err))
  32700. + goto out_pair;
  32701. + }
  32702. +
  32703. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
  32704. + br = au_sbr(sb, bindex);
  32705. + if (br->br_xino.xi_file)
  32706. + fput(br->br_xino.xi_file);
  32707. + get_file(p->new);
  32708. + br->br_xino.xi_file = p->new;
  32709. + }
  32710. +
  32711. +out_pair:
  32712. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
  32713. + if (p->new)
  32714. + fput(p->new);
  32715. + else
  32716. + break;
  32717. + kfree(fpair);
  32718. +out:
  32719. + return err;
  32720. +}
  32721. +
  32722. +void au_xino_clr(struct super_block *sb)
  32723. +{
  32724. + struct au_sbinfo *sbinfo;
  32725. +
  32726. + au_xigen_clr(sb);
  32727. + xino_clear_xib(sb);
  32728. + xino_clear_br(sb);
  32729. + sbinfo = au_sbi(sb);
  32730. + /* lvalue, do not call au_mntflags() */
  32731. + au_opt_clr(sbinfo->si_mntflags, XINO);
  32732. +}
  32733. +
  32734. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
  32735. +{
  32736. + int err, skip;
  32737. + struct dentry *parent, *cur_parent;
  32738. + struct qstr *dname, *cur_name;
  32739. + struct file *cur_xino;
  32740. + struct inode *dir;
  32741. + struct au_sbinfo *sbinfo;
  32742. +
  32743. + SiMustWriteLock(sb);
  32744. +
  32745. + err = 0;
  32746. + sbinfo = au_sbi(sb);
  32747. + parent = dget_parent(xino->file->f_path.dentry);
  32748. + if (remount) {
  32749. + skip = 0;
  32750. + dname = &xino->file->f_path.dentry->d_name;
  32751. + cur_xino = sbinfo->si_xib;
  32752. + if (cur_xino) {
  32753. + cur_parent = dget_parent(cur_xino->f_path.dentry);
  32754. + cur_name = &cur_xino->f_path.dentry->d_name;
  32755. + skip = (cur_parent == parent
  32756. + && au_qstreq(dname, cur_name));
  32757. + dput(cur_parent);
  32758. + }
  32759. + if (skip)
  32760. + goto out;
  32761. + }
  32762. +
  32763. + au_opt_set(sbinfo->si_mntflags, XINO);
  32764. + dir = parent->d_inode;
  32765. + mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
  32766. + /* mnt_want_write() is unnecessary here */
  32767. + err = au_xino_set_xib(sb, xino->file);
  32768. + if (!err)
  32769. + err = au_xigen_set(sb, xino->file);
  32770. + if (!err)
  32771. + err = au_xino_set_br(sb, xino->file);
  32772. + mutex_unlock(&dir->i_mutex);
  32773. + if (!err)
  32774. + goto out; /* success */
  32775. +
  32776. + /* reset all */
  32777. + AuIOErr("failed creating xino(%d).\n", err);
  32778. + au_xigen_clr(sb);
  32779. + xino_clear_xib(sb);
  32780. +
  32781. +out:
  32782. + dput(parent);
  32783. + return err;
  32784. +}
  32785. +
  32786. +/* ---------------------------------------------------------------------- */
  32787. +
  32788. +/*
  32789. + * create a xinofile at the default place/path.
  32790. + */
  32791. +struct file *au_xino_def(struct super_block *sb)
  32792. +{
  32793. + struct file *file;
  32794. + char *page, *p;
  32795. + struct au_branch *br;
  32796. + struct super_block *h_sb;
  32797. + struct path path;
  32798. + aufs_bindex_t bend, bindex, bwr;
  32799. +
  32800. + br = NULL;
  32801. + bend = au_sbend(sb);
  32802. + bwr = -1;
  32803. + for (bindex = 0; bindex <= bend; bindex++) {
  32804. + br = au_sbr(sb, bindex);
  32805. + if (au_br_writable(br->br_perm)
  32806. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  32807. + bwr = bindex;
  32808. + break;
  32809. + }
  32810. + }
  32811. +
  32812. + if (bwr >= 0) {
  32813. + file = ERR_PTR(-ENOMEM);
  32814. + page = (void *)__get_free_page(GFP_NOFS);
  32815. + if (unlikely(!page))
  32816. + goto out;
  32817. + path.mnt = au_br_mnt(br);
  32818. + path.dentry = au_h_dptr(sb->s_root, bwr);
  32819. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  32820. + file = (void *)p;
  32821. + if (!IS_ERR(p)) {
  32822. + strcat(p, "/" AUFS_XINO_FNAME);
  32823. + AuDbg("%s\n", p);
  32824. + file = au_xino_create(sb, p, /*silent*/0);
  32825. + if (!IS_ERR(file))
  32826. + au_xino_brid_set(sb, br->br_id);
  32827. + }
  32828. + free_page((unsigned long)page);
  32829. + } else {
  32830. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  32831. + if (IS_ERR(file))
  32832. + goto out;
  32833. + h_sb = file->f_path.dentry->d_sb;
  32834. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  32835. + pr_err("xino doesn't support %s(%s)\n",
  32836. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  32837. + fput(file);
  32838. + file = ERR_PTR(-EINVAL);
  32839. + }
  32840. + if (!IS_ERR(file))
  32841. + au_xino_brid_set(sb, -1);
  32842. + }
  32843. +
  32844. +out:
  32845. + return file;
  32846. +}
  32847. +
  32848. +/* ---------------------------------------------------------------------- */
  32849. +
  32850. +int au_xino_path(struct seq_file *seq, struct file *file)
  32851. +{
  32852. + int err;
  32853. +
  32854. + err = au_seq_path(seq, &file->f_path);
  32855. + if (unlikely(err < 0))
  32856. + goto out;
  32857. +
  32858. + err = 0;
  32859. +#define Deleted "\\040(deleted)"
  32860. + seq->count -= sizeof(Deleted) - 1;
  32861. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  32862. + sizeof(Deleted) - 1));
  32863. +#undef Deleted
  32864. +
  32865. +out:
  32866. + return err;
  32867. +}
  32868. diff -Nur linux-4.0.4.orig/fs/buffer.c linux-4.0.4/fs/buffer.c
  32869. --- linux-4.0.4.orig/fs/buffer.c 2015-05-17 18:55:21.000000000 +0200
  32870. +++ linux-4.0.4/fs/buffer.c 2015-05-30 22:11:30.000000000 +0200
  32871. @@ -2450,7 +2450,7 @@
  32872. * Update file times before taking page lock. We may end up failing the
  32873. * fault so this update may be superfluous but who really cares...
  32874. */
  32875. - file_update_time(vma->vm_file);
  32876. + vma_file_update_time(vma);
  32877. ret = __block_page_mkwrite(vma, vmf, get_block);
  32878. sb_end_pagefault(sb);
  32879. diff -Nur linux-4.0.4.orig/fs/dcache.c linux-4.0.4/fs/dcache.c
  32880. --- linux-4.0.4.orig/fs/dcache.c 2015-05-17 18:55:21.000000000 +0200
  32881. +++ linux-4.0.4/fs/dcache.c 2015-05-30 22:11:30.000000000 +0200
  32882. @@ -1130,7 +1130,7 @@
  32883. *
  32884. * The @enter() and @finish() callbacks are called with d_lock held.
  32885. */
  32886. -static void d_walk(struct dentry *parent, void *data,
  32887. +void d_walk(struct dentry *parent, void *data,
  32888. enum d_walk_ret (*enter)(void *, struct dentry *),
  32889. void (*finish)(void *))
  32890. {
  32891. diff -Nur linux-4.0.4.orig/fs/Kconfig linux-4.0.4/fs/Kconfig
  32892. --- linux-4.0.4.orig/fs/Kconfig 2015-05-17 18:55:21.000000000 +0200
  32893. +++ linux-4.0.4/fs/Kconfig 2015-05-30 22:11:31.000000000 +0200
  32894. @@ -218,6 +218,7 @@
  32895. source "fs/ufs/Kconfig"
  32896. source "fs/exofs/Kconfig"
  32897. source "fs/f2fs/Kconfig"
  32898. +source "fs/aufs/Kconfig"
  32899. endif # MISC_FILESYSTEMS
  32900. diff -Nur linux-4.0.4.orig/fs/Makefile linux-4.0.4/fs/Makefile
  32901. --- linux-4.0.4.orig/fs/Makefile 2015-05-17 18:55:21.000000000 +0200
  32902. +++ linux-4.0.4/fs/Makefile 2015-05-30 22:11:31.000000000 +0200
  32903. @@ -126,3 +126,4 @@
  32904. obj-$(CONFIG_CEPH_FS) += ceph/
  32905. obj-$(CONFIG_PSTORE) += pstore/
  32906. obj-$(CONFIG_EFIVAR_FS) += efivarfs/
  32907. +obj-$(CONFIG_AUFS_FS) += aufs/
  32908. diff -Nur linux-4.0.4.orig/fs/proc/base.c linux-4.0.4/fs/proc/base.c
  32909. --- linux-4.0.4.orig/fs/proc/base.c 2015-05-17 18:55:21.000000000 +0200
  32910. +++ linux-4.0.4/fs/proc/base.c 2015-05-30 22:11:30.000000000 +0200
  32911. @@ -1735,7 +1735,7 @@
  32912. down_read(&mm->mmap_sem);
  32913. vma = find_exact_vma(mm, vm_start, vm_end);
  32914. if (vma && vma->vm_file) {
  32915. - *path = vma->vm_file->f_path;
  32916. + *path = vma_pr_or_file(vma)->f_path;
  32917. path_get(path);
  32918. rc = 0;
  32919. }
  32920. diff -Nur linux-4.0.4.orig/fs/proc/nommu.c linux-4.0.4/fs/proc/nommu.c
  32921. --- linux-4.0.4.orig/fs/proc/nommu.c 2015-05-17 18:55:21.000000000 +0200
  32922. +++ linux-4.0.4/fs/proc/nommu.c 2015-05-30 22:11:30.000000000 +0200
  32923. @@ -45,7 +45,10 @@
  32924. file = region->vm_file;
  32925. if (file) {
  32926. - struct inode *inode = file_inode(region->vm_file);
  32927. + struct inode *inode;
  32928. +
  32929. + file = vmr_pr_or_file(region);
  32930. + inode = file_inode(file);
  32931. dev = inode->i_sb->s_dev;
  32932. ino = inode->i_ino;
  32933. }
  32934. diff -Nur linux-4.0.4.orig/fs/proc/task_mmu.c linux-4.0.4/fs/proc/task_mmu.c
  32935. --- linux-4.0.4.orig/fs/proc/task_mmu.c 2015-05-17 18:55:21.000000000 +0200
  32936. +++ linux-4.0.4/fs/proc/task_mmu.c 2015-05-30 22:11:30.000000000 +0200
  32937. @@ -279,7 +279,10 @@
  32938. const char *name = NULL;
  32939. if (file) {
  32940. - struct inode *inode = file_inode(vma->vm_file);
  32941. + struct inode *inode;
  32942. +
  32943. + file = vma_pr_or_file(vma);
  32944. + inode = file_inode(file);
  32945. dev = inode->i_sb->s_dev;
  32946. ino = inode->i_ino;
  32947. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  32948. @@ -1479,7 +1482,7 @@
  32949. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  32950. struct vm_area_struct *vma = v;
  32951. struct numa_maps *md = &numa_priv->md;
  32952. - struct file *file = vma->vm_file;
  32953. + struct file *file = vma_pr_or_file(vma);
  32954. struct mm_struct *mm = vma->vm_mm;
  32955. struct mm_walk walk = {
  32956. .hugetlb_entry = gather_hugetlb_stats,
  32957. diff -Nur linux-4.0.4.orig/fs/proc/task_nommu.c linux-4.0.4/fs/proc/task_nommu.c
  32958. --- linux-4.0.4.orig/fs/proc/task_nommu.c 2015-05-17 18:55:21.000000000 +0200
  32959. +++ linux-4.0.4/fs/proc/task_nommu.c 2015-05-30 22:11:30.000000000 +0200
  32960. @@ -160,7 +160,10 @@
  32961. file = vma->vm_file;
  32962. if (file) {
  32963. - struct inode *inode = file_inode(vma->vm_file);
  32964. + struct inode *inode;
  32965. +
  32966. + file = vma_pr_or_file(file);
  32967. + inode = file_inode(file);
  32968. dev = inode->i_sb->s_dev;
  32969. ino = inode->i_ino;
  32970. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  32971. diff -Nur linux-4.0.4.orig/fs/splice.c linux-4.0.4/fs/splice.c
  32972. --- linux-4.0.4.orig/fs/splice.c 2015-05-17 18:55:21.000000000 +0200
  32973. +++ linux-4.0.4/fs/splice.c 2015-05-30 22:11:30.000000000 +0200
  32974. @@ -1099,8 +1099,8 @@
  32975. /*
  32976. * Attempt to initiate a splice from pipe to file.
  32977. */
  32978. -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32979. - loff_t *ppos, size_t len, unsigned int flags)
  32980. +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32981. + loff_t *ppos, size_t len, unsigned int flags)
  32982. {
  32983. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  32984. loff_t *, size_t, unsigned int);
  32985. @@ -1116,9 +1116,9 @@
  32986. /*
  32987. * Attempt to initiate a splice from a file to a pipe.
  32988. */
  32989. -static long do_splice_to(struct file *in, loff_t *ppos,
  32990. - struct pipe_inode_info *pipe, size_t len,
  32991. - unsigned int flags)
  32992. +long do_splice_to(struct file *in, loff_t *ppos,
  32993. + struct pipe_inode_info *pipe, size_t len,
  32994. + unsigned int flags)
  32995. {
  32996. ssize_t (*splice_read)(struct file *, loff_t *,
  32997. struct pipe_inode_info *, size_t, unsigned int);
  32998. diff -Nur linux-4.0.4.orig/include/linux/file.h linux-4.0.4/include/linux/file.h
  32999. --- linux-4.0.4.orig/include/linux/file.h 2015-05-17 18:55:21.000000000 +0200
  33000. +++ linux-4.0.4/include/linux/file.h 2015-05-30 22:11:31.000000000 +0200
  33001. @@ -19,6 +19,7 @@
  33002. struct path;
  33003. extern struct file *alloc_file(struct path *, fmode_t mode,
  33004. const struct file_operations *fop);
  33005. +extern struct file *get_empty_filp(void);
  33006. static inline void fput_light(struct file *file, int fput_needed)
  33007. {
  33008. diff -Nur linux-4.0.4.orig/include/linux/mm.h linux-4.0.4/include/linux/mm.h
  33009. --- linux-4.0.4.orig/include/linux/mm.h 2015-05-17 18:55:21.000000000 +0200
  33010. +++ linux-4.0.4/include/linux/mm.h 2015-05-30 22:11:30.000000000 +0200
  33011. @@ -1254,6 +1254,28 @@
  33012. }
  33013. #endif
  33014. +#ifdef CONFIG_MMU
  33015. +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
  33016. +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
  33017. + int);
  33018. +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
  33019. +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
  33020. +
  33021. +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
  33022. + __LINE__)
  33023. +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
  33024. + __LINE__)
  33025. +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
  33026. +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
  33027. +#else
  33028. +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
  33029. +extern void vmr_do_fput(struct vm_region *, const char[], int);
  33030. +
  33031. +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
  33032. + __LINE__)
  33033. +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
  33034. +#endif /* CONFIG_MMU */
  33035. +
  33036. extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
  33037. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  33038. void *buf, int len, int write);
  33039. diff -Nur linux-4.0.4.orig/include/linux/mm_types.h linux-4.0.4/include/linux/mm_types.h
  33040. --- linux-4.0.4.orig/include/linux/mm_types.h 2015-05-17 18:55:21.000000000 +0200
  33041. +++ linux-4.0.4/include/linux/mm_types.h 2015-05-30 22:11:30.000000000 +0200
  33042. @@ -241,6 +241,7 @@
  33043. unsigned long vm_top; /* region allocated to here */
  33044. unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
  33045. struct file *vm_file; /* the backing file or NULL */
  33046. + struct file *vm_prfile; /* the virtual backing file or NULL */
  33047. int vm_usage; /* region usage count (access under nommu_region_sem) */
  33048. bool vm_icache_flushed : 1; /* true if the icache has been flushed for
  33049. @@ -305,6 +306,7 @@
  33050. unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
  33051. units, *not* PAGE_CACHE_SIZE */
  33052. struct file * vm_file; /* File we map to (can be NULL). */
  33053. + struct file *vm_prfile; /* shadow of vm_file */
  33054. void * vm_private_data; /* was vm_pte (shared mem) */
  33055. #ifndef CONFIG_MMU
  33056. diff -Nur linux-4.0.4.orig/include/linux/splice.h linux-4.0.4/include/linux/splice.h
  33057. --- linux-4.0.4.orig/include/linux/splice.h 2015-05-17 18:55:21.000000000 +0200
  33058. +++ linux-4.0.4/include/linux/splice.h 2015-05-30 22:11:30.000000000 +0200
  33059. @@ -83,4 +83,10 @@
  33060. extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
  33061. extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
  33062. +
  33063. +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  33064. + loff_t *ppos, size_t len, unsigned int flags);
  33065. +extern long do_splice_to(struct file *in, loff_t *ppos,
  33066. + struct pipe_inode_info *pipe, size_t len,
  33067. + unsigned int flags);
  33068. #endif
  33069. diff -Nur linux-4.0.4.orig/include/uapi/linux/aufs_type.h linux-4.0.4/include/uapi/linux/aufs_type.h
  33070. --- linux-4.0.4.orig/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
  33071. +++ linux-4.0.4/include/uapi/linux/aufs_type.h 2015-05-30 22:11:31.000000000 +0200
  33072. @@ -0,0 +1,406 @@
  33073. +/*
  33074. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  33075. + */
  33076. +
  33077. +#ifndef __AUFS_TYPE_H__
  33078. +#define __AUFS_TYPE_H__
  33079. +
  33080. +#define AUFS_NAME "aufs"
  33081. +
  33082. +#ifdef __KERNEL__
  33083. +/*
  33084. + * define it before including all other headers.
  33085. + * sched.h may use pr_* macros before defining "current", so define the
  33086. + * no-current version first, and re-define later.
  33087. + */
  33088. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  33089. +#include <linux/sched.h>
  33090. +#undef pr_fmt
  33091. +#define pr_fmt(fmt) \
  33092. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  33093. + (int)sizeof(current->comm), current->comm, current->pid
  33094. +#else
  33095. +#include <stdint.h>
  33096. +#include <sys/types.h>
  33097. +#endif /* __KERNEL__ */
  33098. +
  33099. +#include <linux/limits.h>
  33100. +
  33101. +#define AUFS_VERSION "4.0"
  33102. +
  33103. +/* todo? move this to linux-2.6.19/include/magic.h */
  33104. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  33105. +
  33106. +/* ---------------------------------------------------------------------- */
  33107. +
  33108. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  33109. +typedef int8_t aufs_bindex_t;
  33110. +#define AUFS_BRANCH_MAX 127
  33111. +#else
  33112. +typedef int16_t aufs_bindex_t;
  33113. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  33114. +#define AUFS_BRANCH_MAX 511
  33115. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  33116. +#define AUFS_BRANCH_MAX 1023
  33117. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  33118. +#define AUFS_BRANCH_MAX 32767
  33119. +#endif
  33120. +#endif
  33121. +
  33122. +#ifdef __KERNEL__
  33123. +#ifndef AUFS_BRANCH_MAX
  33124. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  33125. +#endif
  33126. +#endif /* __KERNEL__ */
  33127. +
  33128. +/* ---------------------------------------------------------------------- */
  33129. +
  33130. +#define AUFS_FSTYPE AUFS_NAME
  33131. +
  33132. +#define AUFS_ROOT_INO 2
  33133. +#define AUFS_FIRST_INO 11
  33134. +
  33135. +#define AUFS_WH_PFX ".wh."
  33136. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  33137. +#define AUFS_WH_TMP_LEN 4
  33138. +/* a limit for rmdir/rename a dir and copyup */
  33139. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  33140. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  33141. + - 1 /* dot */\
  33142. + - AUFS_WH_TMP_LEN) /* hex */
  33143. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  33144. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  33145. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  33146. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  33147. +#define AUFS_DIRWH_DEF 3
  33148. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  33149. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  33150. +#define AUFS_RDBLK_DEF 512 /* bytes */
  33151. +#define AUFS_RDHASH_DEF 32
  33152. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  33153. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  33154. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  33155. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  33156. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  33157. +
  33158. +/* pseudo-link maintenace under /proc */
  33159. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  33160. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  33161. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  33162. +
  33163. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  33164. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  33165. +
  33166. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  33167. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  33168. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  33169. +
  33170. +/* doubly whiteouted */
  33171. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  33172. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  33173. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  33174. +
  33175. +/* branch permissions and attributes */
  33176. +#define AUFS_BRPERM_RW "rw"
  33177. +#define AUFS_BRPERM_RO "ro"
  33178. +#define AUFS_BRPERM_RR "rr"
  33179. +#define AUFS_BRATTR_COO_REG "coo_reg"
  33180. +#define AUFS_BRATTR_COO_ALL "coo_all"
  33181. +#define AUFS_BRATTR_FHSM "fhsm"
  33182. +#define AUFS_BRATTR_UNPIN "unpin"
  33183. +#define AUFS_BRATTR_ICEX "icex"
  33184. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  33185. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  33186. +#define AUFS_BRATTR_ICEX_TR "icextr"
  33187. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  33188. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  33189. +#define AUFS_BRRATTR_WH "wh"
  33190. +#define AUFS_BRWATTR_NLWH "nolwh"
  33191. +#define AUFS_BRWATTR_MOO "moo"
  33192. +
  33193. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  33194. +#define AuBrPerm_RO (1 << 1) /* readonly */
  33195. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  33196. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  33197. +
  33198. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  33199. +#define AuBrAttr_COO_ALL (1 << 4)
  33200. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  33201. +
  33202. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  33203. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  33204. + branch. meaningless since
  33205. + linux-3.18-rc1 */
  33206. +
  33207. +/* ignore error in copying XATTR */
  33208. +#define AuBrAttr_ICEX_SEC (1 << 7)
  33209. +#define AuBrAttr_ICEX_SYS (1 << 8)
  33210. +#define AuBrAttr_ICEX_TR (1 << 9)
  33211. +#define AuBrAttr_ICEX_USR (1 << 10)
  33212. +#define AuBrAttr_ICEX_OTH (1 << 11)
  33213. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  33214. + | AuBrAttr_ICEX_SYS \
  33215. + | AuBrAttr_ICEX_TR \
  33216. + | AuBrAttr_ICEX_USR \
  33217. + | AuBrAttr_ICEX_OTH)
  33218. +
  33219. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  33220. +#define AuBrRAttr_Mask AuBrRAttr_WH
  33221. +
  33222. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  33223. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  33224. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  33225. +
  33226. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  33227. +
  33228. +/* #warning test userspace */
  33229. +#ifdef __KERNEL__
  33230. +#ifndef CONFIG_AUFS_FHSM
  33231. +#undef AuBrAttr_FHSM
  33232. +#define AuBrAttr_FHSM 0
  33233. +#endif
  33234. +#ifndef CONFIG_AUFS_XATTR
  33235. +#undef AuBrAttr_ICEX
  33236. +#define AuBrAttr_ICEX 0
  33237. +#undef AuBrAttr_ICEX_SEC
  33238. +#define AuBrAttr_ICEX_SEC 0
  33239. +#undef AuBrAttr_ICEX_SYS
  33240. +#define AuBrAttr_ICEX_SYS 0
  33241. +#undef AuBrAttr_ICEX_TR
  33242. +#define AuBrAttr_ICEX_TR 0
  33243. +#undef AuBrAttr_ICEX_USR
  33244. +#define AuBrAttr_ICEX_USR 0
  33245. +#undef AuBrAttr_ICEX_OTH
  33246. +#define AuBrAttr_ICEX_OTH 0
  33247. +#endif
  33248. +#endif
  33249. +
  33250. +/* the longest combination */
  33251. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  33252. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  33253. + "+" AUFS_BRATTR_COO_REG \
  33254. + "+" AUFS_BRATTR_FHSM \
  33255. + "+" AUFS_BRATTR_UNPIN \
  33256. + "+" AUFS_BRATTR_ICEX_SEC \
  33257. + "+" AUFS_BRATTR_ICEX_SYS \
  33258. + "+" AUFS_BRATTR_ICEX_USR \
  33259. + "+" AUFS_BRATTR_ICEX_OTH \
  33260. + "+" AUFS_BRWATTR_NLWH)
  33261. +
  33262. +typedef struct {
  33263. + char a[AuBrPermStrSz];
  33264. +} au_br_perm_str_t;
  33265. +
  33266. +static inline int au_br_writable(int brperm)
  33267. +{
  33268. + return brperm & AuBrPerm_RW;
  33269. +}
  33270. +
  33271. +static inline int au_br_whable(int brperm)
  33272. +{
  33273. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  33274. +}
  33275. +
  33276. +static inline int au_br_wh_linkable(int brperm)
  33277. +{
  33278. + return !(brperm & AuBrWAttr_NoLinkWH);
  33279. +}
  33280. +
  33281. +static inline int au_br_cmoo(int brperm)
  33282. +{
  33283. + return brperm & AuBrAttr_CMOO_Mask;
  33284. +}
  33285. +
  33286. +static inline int au_br_fhsm(int brperm)
  33287. +{
  33288. + return brperm & AuBrAttr_FHSM;
  33289. +}
  33290. +
  33291. +/* ---------------------------------------------------------------------- */
  33292. +
  33293. +/* ioctl */
  33294. +enum {
  33295. + /* readdir in userspace */
  33296. + AuCtl_RDU,
  33297. + AuCtl_RDU_INO,
  33298. +
  33299. + AuCtl_WBR_FD, /* pathconf wrapper */
  33300. + AuCtl_IBUSY, /* busy inode */
  33301. + AuCtl_MVDOWN, /* move-down */
  33302. + AuCtl_BR, /* info about branches */
  33303. + AuCtl_FHSM_FD /* connection for fhsm */
  33304. +};
  33305. +
  33306. +/* borrowed from linux/include/linux/kernel.h */
  33307. +#ifndef ALIGN
  33308. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  33309. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  33310. +#endif
  33311. +
  33312. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  33313. +#ifndef __aligned
  33314. +#define __aligned(x) __attribute__((aligned(x)))
  33315. +#endif
  33316. +
  33317. +#ifdef __KERNEL__
  33318. +#ifndef __packed
  33319. +#define __packed __attribute__((packed))
  33320. +#endif
  33321. +#endif
  33322. +
  33323. +struct au_rdu_cookie {
  33324. + uint64_t h_pos;
  33325. + int16_t bindex;
  33326. + uint8_t flags;
  33327. + uint8_t pad;
  33328. + uint32_t generation;
  33329. +} __aligned(8);
  33330. +
  33331. +struct au_rdu_ent {
  33332. + uint64_t ino;
  33333. + int16_t bindex;
  33334. + uint8_t type;
  33335. + uint8_t nlen;
  33336. + uint8_t wh;
  33337. + char name[0];
  33338. +} __aligned(8);
  33339. +
  33340. +static inline int au_rdu_len(int nlen)
  33341. +{
  33342. + /* include the terminating NULL */
  33343. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  33344. + sizeof(uint64_t));
  33345. +}
  33346. +
  33347. +union au_rdu_ent_ul {
  33348. + struct au_rdu_ent __user *e;
  33349. + uint64_t ul;
  33350. +};
  33351. +
  33352. +enum {
  33353. + AufsCtlRduV_SZ,
  33354. + AufsCtlRduV_End
  33355. +};
  33356. +
  33357. +struct aufs_rdu {
  33358. + /* input */
  33359. + union {
  33360. + uint64_t sz; /* AuCtl_RDU */
  33361. + uint64_t nent; /* AuCtl_RDU_INO */
  33362. + };
  33363. + union au_rdu_ent_ul ent;
  33364. + uint16_t verify[AufsCtlRduV_End];
  33365. +
  33366. + /* input/output */
  33367. + uint32_t blk;
  33368. +
  33369. + /* output */
  33370. + union au_rdu_ent_ul tail;
  33371. + /* number of entries which were added in a single call */
  33372. + uint64_t rent;
  33373. + uint8_t full;
  33374. + uint8_t shwh;
  33375. +
  33376. + struct au_rdu_cookie cookie;
  33377. +} __aligned(8);
  33378. +
  33379. +/* ---------------------------------------------------------------------- */
  33380. +
  33381. +struct aufs_wbr_fd {
  33382. + uint32_t oflags;
  33383. + int16_t brid;
  33384. +} __aligned(8);
  33385. +
  33386. +/* ---------------------------------------------------------------------- */
  33387. +
  33388. +struct aufs_ibusy {
  33389. + uint64_t ino, h_ino;
  33390. + int16_t bindex;
  33391. +} __aligned(8);
  33392. +
  33393. +/* ---------------------------------------------------------------------- */
  33394. +
  33395. +/* error code for move-down */
  33396. +/* the actual message strings are implemented in aufs-util.git */
  33397. +enum {
  33398. + EAU_MVDOWN_OPAQUE = 1,
  33399. + EAU_MVDOWN_WHITEOUT,
  33400. + EAU_MVDOWN_UPPER,
  33401. + EAU_MVDOWN_BOTTOM,
  33402. + EAU_MVDOWN_NOUPPER,
  33403. + EAU_MVDOWN_NOLOWERBR,
  33404. + EAU_Last
  33405. +};
  33406. +
  33407. +/* flags for move-down */
  33408. +#define AUFS_MVDOWN_DMSG 1
  33409. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  33410. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  33411. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  33412. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  33413. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  33414. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  33415. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  33416. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  33417. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  33418. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  33419. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  33420. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  33421. +
  33422. +/* index for move-down */
  33423. +enum {
  33424. + AUFS_MVDOWN_UPPER,
  33425. + AUFS_MVDOWN_LOWER,
  33426. + AUFS_MVDOWN_NARRAY
  33427. +};
  33428. +
  33429. +/*
  33430. + * additional info of move-down
  33431. + * number of free blocks and inodes.
  33432. + * subset of struct kstatfs, but smaller and always 64bit.
  33433. + */
  33434. +struct aufs_stfs {
  33435. + uint64_t f_blocks;
  33436. + uint64_t f_bavail;
  33437. + uint64_t f_files;
  33438. + uint64_t f_ffree;
  33439. +};
  33440. +
  33441. +struct aufs_stbr {
  33442. + int16_t brid; /* optional input */
  33443. + int16_t bindex; /* output */
  33444. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  33445. +} __aligned(8);
  33446. +
  33447. +struct aufs_mvdown {
  33448. + uint32_t flags; /* input/output */
  33449. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  33450. + int8_t au_errno; /* output */
  33451. +} __aligned(8);
  33452. +
  33453. +/* ---------------------------------------------------------------------- */
  33454. +
  33455. +union aufs_brinfo {
  33456. + /* PATH_MAX may differ between kernel-space and user-space */
  33457. + char _spacer[4096];
  33458. + struct {
  33459. + int16_t id;
  33460. + int perm;
  33461. + char path[0];
  33462. + };
  33463. +} __aligned(8);
  33464. +
  33465. +/* ---------------------------------------------------------------------- */
  33466. +
  33467. +#define AuCtlType 'A'
  33468. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  33469. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  33470. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  33471. + struct aufs_wbr_fd)
  33472. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  33473. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  33474. + struct aufs_mvdown)
  33475. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  33476. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  33477. +
  33478. +#endif /* __AUFS_TYPE_H__ */
  33479. diff -Nur linux-4.0.4.orig/include/uapi/linux/Kbuild linux-4.0.4/include/uapi/linux/Kbuild
  33480. --- linux-4.0.4.orig/include/uapi/linux/Kbuild 2015-05-17 18:55:21.000000000 +0200
  33481. +++ linux-4.0.4/include/uapi/linux/Kbuild 2015-05-30 22:11:31.000000000 +0200
  33482. @@ -58,6 +58,7 @@
  33483. header-y += atm_tcp.h
  33484. header-y += atm_zatm.h
  33485. header-y += audit.h
  33486. +header-y += aufs_type.h
  33487. header-y += auto_fs4.h
  33488. header-y += auto_fs.h
  33489. header-y += auxvec.h
  33490. diff -Nur linux-4.0.4.orig/kernel/fork.c linux-4.0.4/kernel/fork.c
  33491. --- linux-4.0.4.orig/kernel/fork.c 2015-05-17 18:55:21.000000000 +0200
  33492. +++ linux-4.0.4/kernel/fork.c 2015-05-30 22:11:30.000000000 +0200
  33493. @@ -430,7 +430,7 @@
  33494. struct inode *inode = file_inode(file);
  33495. struct address_space *mapping = file->f_mapping;
  33496. - get_file(file);
  33497. + vma_get_file(tmp);
  33498. if (tmp->vm_flags & VM_DENYWRITE)
  33499. atomic_dec(&inode->i_writecount);
  33500. i_mmap_lock_write(mapping);
  33501. diff -Nur linux-4.0.4.orig/MAINTAINERS linux-4.0.4/MAINTAINERS
  33502. --- linux-4.0.4.orig/MAINTAINERS 2015-05-17 18:55:21.000000000 +0200
  33503. +++ linux-4.0.4/MAINTAINERS 2015-05-30 22:11:31.000000000 +0200
  33504. @@ -1864,6 +1864,19 @@
  33505. F: include/uapi/linux/audit.h
  33506. F: kernel/audit*
  33507. +AUFS (advanced multi layered unification filesystem) FILESYSTEM
  33508. +M: "J. R. Okajima" <hooanon05g@gmail.com>
  33509. +L: linux-unionfs@vger.kernel.org
  33510. +L: aufs-users@lists.sourceforge.net (members only)
  33511. +W: http://aufs.sourceforge.net
  33512. +T: git://github.com/sfjro/aufs4-linux.git
  33513. +S: Supported
  33514. +F: Documentation/filesystems/aufs/
  33515. +F: Documentation/ABI/testing/debugfs-aufs
  33516. +F: Documentation/ABI/testing/sysfs-aufs
  33517. +F: fs/aufs/
  33518. +F: include/uapi/linux/aufs_type.h
  33519. +
  33520. AUXILIARY DISPLAY DRIVERS
  33521. M: Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
  33522. W: http://miguelojeda.es/auxdisplay.htm
  33523. diff -Nur linux-4.0.4.orig/mm/filemap.c linux-4.0.4/mm/filemap.c
  33524. --- linux-4.0.4.orig/mm/filemap.c 2015-05-17 18:55:21.000000000 +0200
  33525. +++ linux-4.0.4/mm/filemap.c 2015-05-30 22:11:30.000000000 +0200
  33526. @@ -2064,7 +2064,7 @@
  33527. int ret = VM_FAULT_LOCKED;
  33528. sb_start_pagefault(inode->i_sb);
  33529. - file_update_time(vma->vm_file);
  33530. + vma_file_update_time(vma);
  33531. lock_page(page);
  33532. if (page->mapping != inode->i_mapping) {
  33533. unlock_page(page);
  33534. diff -Nur linux-4.0.4.orig/mm/madvise.c linux-4.0.4/mm/madvise.c
  33535. --- linux-4.0.4.orig/mm/madvise.c 2015-05-17 18:55:21.000000000 +0200
  33536. +++ linux-4.0.4/mm/madvise.c 2015-05-30 22:11:30.000000000 +0200
  33537. @@ -320,12 +320,12 @@
  33538. * vma's reference to the file) can go away as soon as we drop
  33539. * mmap_sem.
  33540. */
  33541. - get_file(f);
  33542. + vma_get_file(vma);
  33543. up_read(&current->mm->mmap_sem);
  33544. error = vfs_fallocate(f,
  33545. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  33546. offset, end - start);
  33547. - fput(f);
  33548. + vma_fput(vma);
  33549. down_read(&current->mm->mmap_sem);
  33550. return error;
  33551. }
  33552. diff -Nur linux-4.0.4.orig/mm/Makefile linux-4.0.4/mm/Makefile
  33553. --- linux-4.0.4.orig/mm/Makefile 2015-05-17 18:55:21.000000000 +0200
  33554. +++ linux-4.0.4/mm/Makefile 2015-05-30 22:11:30.000000000 +0200
  33555. @@ -21,7 +21,7 @@
  33556. mm_init.o mmu_context.o percpu.o slab_common.o \
  33557. compaction.o vmacache.o \
  33558. interval_tree.o list_lru.o workingset.o \
  33559. - debug.o $(mmu-y)
  33560. + prfile.o debug.o $(mmu-y)
  33561. obj-y += init-mm.o
  33562. diff -Nur linux-4.0.4.orig/mm/memory.c linux-4.0.4/mm/memory.c
  33563. --- linux-4.0.4.orig/mm/memory.c 2015-05-17 18:55:21.000000000 +0200
  33564. +++ linux-4.0.4/mm/memory.c 2015-05-30 22:11:30.000000000 +0200
  33565. @@ -2132,7 +2132,7 @@
  33566. }
  33567. if (!page_mkwrite)
  33568. - file_update_time(vma->vm_file);
  33569. + vma_file_update_time(vma);
  33570. }
  33571. return ret;
  33572. diff -Nur linux-4.0.4.orig/mm/mmap.c linux-4.0.4/mm/mmap.c
  33573. --- linux-4.0.4.orig/mm/mmap.c 2015-05-17 18:55:21.000000000 +0200
  33574. +++ linux-4.0.4/mm/mmap.c 2015-05-30 22:11:30.000000000 +0200
  33575. @@ -274,7 +274,7 @@
  33576. if (vma->vm_ops && vma->vm_ops->close)
  33577. vma->vm_ops->close(vma);
  33578. if (vma->vm_file)
  33579. - fput(vma->vm_file);
  33580. + vma_fput(vma);
  33581. mpol_put(vma_policy(vma));
  33582. kmem_cache_free(vm_area_cachep, vma);
  33583. return next;
  33584. @@ -886,7 +886,7 @@
  33585. if (remove_next) {
  33586. if (file) {
  33587. uprobe_munmap(next, next->vm_start, next->vm_end);
  33588. - fput(file);
  33589. + vma_fput(vma);
  33590. }
  33591. if (next->anon_vma)
  33592. anon_vma_merge(vma, next);
  33593. @@ -1671,8 +1671,8 @@
  33594. return addr;
  33595. unmap_and_free_vma:
  33596. + vma_fput(vma);
  33597. vma->vm_file = NULL;
  33598. - fput(file);
  33599. /* Undo any partial mapping done by a device driver. */
  33600. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  33601. @@ -2473,7 +2473,7 @@
  33602. goto out_free_mpol;
  33603. if (new->vm_file)
  33604. - get_file(new->vm_file);
  33605. + vma_get_file(new);
  33606. if (new->vm_ops && new->vm_ops->open)
  33607. new->vm_ops->open(new);
  33608. @@ -2492,7 +2492,7 @@
  33609. if (new->vm_ops && new->vm_ops->close)
  33610. new->vm_ops->close(new);
  33611. if (new->vm_file)
  33612. - fput(new->vm_file);
  33613. + vma_fput(new);
  33614. unlink_anon_vmas(new);
  33615. out_free_mpol:
  33616. mpol_put(vma_policy(new));
  33617. @@ -2635,7 +2635,6 @@
  33618. struct vm_area_struct *vma;
  33619. unsigned long populate = 0;
  33620. unsigned long ret = -EINVAL;
  33621. - struct file *file;
  33622. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
  33623. "See Documentation/vm/remap_file_pages.txt.\n",
  33624. @@ -2679,10 +2678,10 @@
  33625. munlock_vma_pages_range(vma, start, start + size);
  33626. }
  33627. - file = get_file(vma->vm_file);
  33628. + vma_get_file(vma);
  33629. ret = do_mmap_pgoff(vma->vm_file, start, size,
  33630. prot, flags, pgoff, &populate);
  33631. - fput(file);
  33632. + vma_fput(vma);
  33633. out:
  33634. up_write(&mm->mmap_sem);
  33635. if (populate)
  33636. @@ -2950,7 +2949,7 @@
  33637. if (anon_vma_clone(new_vma, vma))
  33638. goto out_free_mempol;
  33639. if (new_vma->vm_file)
  33640. - get_file(new_vma->vm_file);
  33641. + vma_get_file(new_vma);
  33642. if (new_vma->vm_ops && new_vma->vm_ops->open)
  33643. new_vma->vm_ops->open(new_vma);
  33644. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  33645. diff -Nur linux-4.0.4.orig/mm/msync.c linux-4.0.4/mm/msync.c
  33646. --- linux-4.0.4.orig/mm/msync.c 2015-05-17 18:55:21.000000000 +0200
  33647. +++ linux-4.0.4/mm/msync.c 2015-05-30 22:11:30.000000000 +0200
  33648. @@ -84,10 +84,10 @@
  33649. start = vma->vm_end;
  33650. if ((flags & MS_SYNC) && file &&
  33651. (vma->vm_flags & VM_SHARED)) {
  33652. - get_file(file);
  33653. + vma_get_file(vma);
  33654. up_read(&mm->mmap_sem);
  33655. error = vfs_fsync_range(file, fstart, fend, 1);
  33656. - fput(file);
  33657. + vma_fput(vma);
  33658. if (error || start >= end)
  33659. goto out;
  33660. down_read(&mm->mmap_sem);
  33661. diff -Nur linux-4.0.4.orig/mm/nommu.c linux-4.0.4/mm/nommu.c
  33662. --- linux-4.0.4.orig/mm/nommu.c 2015-05-17 18:55:21.000000000 +0200
  33663. +++ linux-4.0.4/mm/nommu.c 2015-05-30 22:11:30.000000000 +0200
  33664. @@ -693,7 +693,7 @@
  33665. up_write(&nommu_region_sem);
  33666. if (region->vm_file)
  33667. - fput(region->vm_file);
  33668. + vmr_fput(region);
  33669. /* IO memory and memory shared directly out of the pagecache
  33670. * from ramfs/tmpfs mustn't be released here */
  33671. @@ -858,7 +858,7 @@
  33672. if (vma->vm_ops && vma->vm_ops->close)
  33673. vma->vm_ops->close(vma);
  33674. if (vma->vm_file)
  33675. - fput(vma->vm_file);
  33676. + vma_fput(vma);
  33677. put_nommu_region(vma->vm_region);
  33678. kmem_cache_free(vm_area_cachep, vma);
  33679. }
  33680. @@ -1398,7 +1398,7 @@
  33681. goto error_just_free;
  33682. }
  33683. }
  33684. - fput(region->vm_file);
  33685. + vmr_fput(region);
  33686. kmem_cache_free(vm_region_jar, region);
  33687. region = pregion;
  33688. result = start;
  33689. @@ -1474,10 +1474,10 @@
  33690. up_write(&nommu_region_sem);
  33691. error:
  33692. if (region->vm_file)
  33693. - fput(region->vm_file);
  33694. + vmr_fput(region);
  33695. kmem_cache_free(vm_region_jar, region);
  33696. if (vma->vm_file)
  33697. - fput(vma->vm_file);
  33698. + vma_fput(vma);
  33699. kmem_cache_free(vm_area_cachep, vma);
  33700. kleave(" = %d", ret);
  33701. return ret;
  33702. diff -Nur linux-4.0.4.orig/mm/prfile.c linux-4.0.4/mm/prfile.c
  33703. --- linux-4.0.4.orig/mm/prfile.c 1970-01-01 01:00:00.000000000 +0100
  33704. +++ linux-4.0.4/mm/prfile.c 2015-05-30 22:11:30.000000000 +0200
  33705. @@ -0,0 +1,86 @@
  33706. +/*
  33707. + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
  33708. + * in /proc/PID/maps.
  33709. + * Call these functions via macros defined in linux/mm.h.
  33710. + *
  33711. + * See Documentation/filesystems/aufs/design/06mmap.txt
  33712. + *
  33713. + * Copyright (c) 2014 Junjro R. Okajima
  33714. + * Copyright (c) 2014 Ian Campbell
  33715. + */
  33716. +
  33717. +#include <linux/mm.h>
  33718. +#include <linux/file.h>
  33719. +#include <linux/fs.h>
  33720. +
  33721. +/* #define PRFILE_TRACE */
  33722. +static inline void prfile_trace(struct file *f, struct file *pr,
  33723. + const char func[], int line, const char func2[])
  33724. +{
  33725. +#ifdef PRFILE_TRACE
  33726. + if (pr)
  33727. + pr_info("%s:%d: %s, %p\n", func, line, func2,
  33728. + f ? (char *)f->f_path.dentry->d_name.name : "(null)");
  33729. +#endif
  33730. +}
  33731. +
  33732. +#ifdef CONFIG_MMU
  33733. +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
  33734. + int line)
  33735. +{
  33736. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33737. +
  33738. + prfile_trace(f, pr, func, line, __func__);
  33739. + file_update_time(f);
  33740. + if (f && pr)
  33741. + file_update_time(pr);
  33742. +}
  33743. +
  33744. +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
  33745. + int line)
  33746. +{
  33747. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33748. +
  33749. + prfile_trace(f, pr, func, line, __func__);
  33750. + return (f && pr) ? pr : f;
  33751. +}
  33752. +
  33753. +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
  33754. +{
  33755. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33756. +
  33757. + prfile_trace(f, pr, func, line, __func__);
  33758. + get_file(f);
  33759. + if (f && pr)
  33760. + get_file(pr);
  33761. +}
  33762. +
  33763. +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
  33764. +{
  33765. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33766. +
  33767. + prfile_trace(f, pr, func, line, __func__);
  33768. + fput(f);
  33769. + if (f && pr)
  33770. + fput(pr);
  33771. +}
  33772. +#else
  33773. +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
  33774. + int line)
  33775. +{
  33776. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  33777. +
  33778. + prfile_trace(f, pr, func, line, __func__);
  33779. + return (f && pr) ? pr : f;
  33780. +}
  33781. +
  33782. +void vmr_do_fput(struct vm_region *region, const char func[], int line)
  33783. +{
  33784. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  33785. +
  33786. + prfile_trace(f, pr, func, line, __func__);
  33787. + fput(f);
  33788. + if (f && pr)
  33789. + fput(pr);
  33790. +}
  33791. +#endif /* CONFIG_MMU */