aufs4.patch 827 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447
  1. diff -Nur linux-4.0.5.orig/drivers/block/loop.c linux-4.0.5/drivers/block/loop.c
  2. --- linux-4.0.5.orig/drivers/block/loop.c 2015-06-06 10:21:22.000000000 -0500
  3. +++ linux-4.0.5/drivers/block/loop.c 2015-06-21 14:08:17.146432521 -0500
  4. @@ -592,6 +592,24 @@
  5. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  6. }
  7. +/*
  8. + * for AUFS
  9. + * no get/put for file.
  10. + */
  11. +struct file *loop_backing_file(struct super_block *sb)
  12. +{
  13. + struct file *ret;
  14. + struct loop_device *l;
  15. +
  16. + ret = NULL;
  17. + if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
  18. + l = sb->s_bdev->bd_disk->private_data;
  19. + ret = l->lo_backing_file;
  20. + }
  21. + return ret;
  22. +}
  23. +EXPORT_SYMBOL(loop_backing_file);
  24. +
  25. /* loop sysfs attributes */
  26. static ssize_t loop_attr_show(struct device *dev, char *page,
  27. diff -Nur linux-4.0.5.orig/fs/aufs/aufs.h linux-4.0.5/fs/aufs/aufs.h
  28. --- linux-4.0.5.orig/fs/aufs/aufs.h 1969-12-31 18:00:00.000000000 -0600
  29. +++ linux-4.0.5/fs/aufs/aufs.h 2015-06-21 14:07:48.682432781 -0500
  30. @@ -0,0 +1,59 @@
  31. +/*
  32. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  33. + *
  34. + * This program, aufs is free software; you can redistribute it and/or modify
  35. + * it under the terms of the GNU General Public License as published by
  36. + * the Free Software Foundation; either version 2 of the License, or
  37. + * (at your option) any later version.
  38. + *
  39. + * This program is distributed in the hope that it will be useful,
  40. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. + * GNU General Public License for more details.
  43. + *
  44. + * You should have received a copy of the GNU General Public License
  45. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  46. + */
  47. +
  48. +/*
  49. + * all header files
  50. + */
  51. +
  52. +#ifndef __AUFS_H__
  53. +#define __AUFS_H__
  54. +
  55. +#ifdef __KERNEL__
  56. +
  57. +#define AuStub(type, name, body, ...) \
  58. + static inline type name(__VA_ARGS__) { body; }
  59. +
  60. +#define AuStubVoid(name, ...) \
  61. + AuStub(void, name, , __VA_ARGS__)
  62. +#define AuStubInt0(name, ...) \
  63. + AuStub(int, name, return 0, __VA_ARGS__)
  64. +
  65. +#include "debug.h"
  66. +
  67. +#include "branch.h"
  68. +#include "cpup.h"
  69. +#include "dcsub.h"
  70. +#include "dbgaufs.h"
  71. +#include "dentry.h"
  72. +#include "dir.h"
  73. +#include "dynop.h"
  74. +#include "file.h"
  75. +#include "fstype.h"
  76. +#include "inode.h"
  77. +#include "loop.h"
  78. +#include "module.h"
  79. +#include "opts.h"
  80. +#include "rwsem.h"
  81. +#include "spl.h"
  82. +#include "super.h"
  83. +#include "sysaufs.h"
  84. +#include "vfsub.h"
  85. +#include "whout.h"
  86. +#include "wkq.h"
  87. +
  88. +#endif /* __KERNEL__ */
  89. +#endif /* __AUFS_H__ */
  90. diff -Nur linux-4.0.5.orig/fs/aufs/branch.c linux-4.0.5/fs/aufs/branch.c
  91. --- linux-4.0.5.orig/fs/aufs/branch.c 1969-12-31 18:00:00.000000000 -0600
  92. +++ linux-4.0.5/fs/aufs/branch.c 2015-06-21 14:07:48.682432781 -0500
  93. @@ -0,0 +1,1406 @@
  94. +/*
  95. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  96. + *
  97. + * This program, aufs is free software; you can redistribute it and/or modify
  98. + * it under the terms of the GNU General Public License as published by
  99. + * the Free Software Foundation; either version 2 of the License, or
  100. + * (at your option) any later version.
  101. + *
  102. + * This program is distributed in the hope that it will be useful,
  103. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  104. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  105. + * GNU General Public License for more details.
  106. + *
  107. + * You should have received a copy of the GNU General Public License
  108. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  109. + */
  110. +
  111. +/*
  112. + * branch management
  113. + */
  114. +
  115. +#include <linux/compat.h>
  116. +#include <linux/statfs.h>
  117. +#include "aufs.h"
  118. +
  119. +/*
  120. + * free a single branch
  121. + */
  122. +static void au_br_do_free(struct au_branch *br)
  123. +{
  124. + int i;
  125. + struct au_wbr *wbr;
  126. + struct au_dykey **key;
  127. +
  128. + au_hnotify_fin_br(br);
  129. +
  130. + if (br->br_xino.xi_file)
  131. + fput(br->br_xino.xi_file);
  132. + mutex_destroy(&br->br_xino.xi_nondir_mtx);
  133. +
  134. + AuDebugOn(atomic_read(&br->br_count));
  135. +
  136. + wbr = br->br_wbr;
  137. + if (wbr) {
  138. + for (i = 0; i < AuBrWh_Last; i++)
  139. + dput(wbr->wbr_wh[i]);
  140. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  141. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  142. + }
  143. +
  144. + if (br->br_fhsm) {
  145. + au_br_fhsm_fin(br->br_fhsm);
  146. + kfree(br->br_fhsm);
  147. + }
  148. +
  149. + key = br->br_dykey;
  150. + for (i = 0; i < AuBrDynOp; i++, key++)
  151. + if (*key)
  152. + au_dy_put(*key);
  153. + else
  154. + break;
  155. +
  156. + /* recursive lock, s_umount of branch's */
  157. + lockdep_off();
  158. + path_put(&br->br_path);
  159. + lockdep_on();
  160. + kfree(wbr);
  161. + kfree(br);
  162. +}
  163. +
  164. +/*
  165. + * frees all branches
  166. + */
  167. +void au_br_free(struct au_sbinfo *sbinfo)
  168. +{
  169. + aufs_bindex_t bmax;
  170. + struct au_branch **br;
  171. +
  172. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  173. +
  174. + bmax = sbinfo->si_bend + 1;
  175. + br = sbinfo->si_branch;
  176. + while (bmax--)
  177. + au_br_do_free(*br++);
  178. +}
  179. +
  180. +/*
  181. + * find the index of a branch which is specified by @br_id.
  182. + */
  183. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  184. +{
  185. + aufs_bindex_t bindex, bend;
  186. +
  187. + bend = au_sbend(sb);
  188. + for (bindex = 0; bindex <= bend; bindex++)
  189. + if (au_sbr_id(sb, bindex) == br_id)
  190. + return bindex;
  191. + return -1;
  192. +}
  193. +
  194. +/* ---------------------------------------------------------------------- */
  195. +
  196. +/*
  197. + * add a branch
  198. + */
  199. +
  200. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  201. + struct dentry *h_root)
  202. +{
  203. + if (unlikely(h_adding == h_root
  204. + || au_test_loopback_overlap(sb, h_adding)))
  205. + return 1;
  206. + if (h_adding->d_sb != h_root->d_sb)
  207. + return 0;
  208. + return au_test_subdir(h_adding, h_root)
  209. + || au_test_subdir(h_root, h_adding);
  210. +}
  211. +
  212. +/*
  213. + * returns a newly allocated branch. @new_nbranch is a number of branches
  214. + * after adding a branch.
  215. + */
  216. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  217. + int perm)
  218. +{
  219. + struct au_branch *add_branch;
  220. + struct dentry *root;
  221. + int err;
  222. +
  223. + err = -ENOMEM;
  224. + root = sb->s_root;
  225. + add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
  226. + if (unlikely(!add_branch))
  227. + goto out;
  228. +
  229. + err = au_hnotify_init_br(add_branch, perm);
  230. + if (unlikely(err))
  231. + goto out_br;
  232. +
  233. + add_branch->br_wbr = NULL;
  234. + if (au_br_writable(perm)) {
  235. + /* may be freed separately at changing the branch permission */
  236. + add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
  237. + GFP_NOFS);
  238. + if (unlikely(!add_branch->br_wbr))
  239. + goto out_hnotify;
  240. + }
  241. +
  242. + add_branch->br_fhsm = NULL;
  243. + if (au_br_fhsm(perm)) {
  244. + err = au_fhsm_br_alloc(add_branch);
  245. + if (unlikely(err))
  246. + goto out_wbr;
  247. + }
  248. +
  249. + err = au_sbr_realloc(au_sbi(sb), new_nbranch);
  250. + if (!err)
  251. + err = au_di_realloc(au_di(root), new_nbranch);
  252. + if (!err)
  253. + err = au_ii_realloc(au_ii(root->d_inode), new_nbranch);
  254. + if (!err)
  255. + return add_branch; /* success */
  256. +
  257. +out_wbr:
  258. + kfree(add_branch->br_wbr);
  259. +out_hnotify:
  260. + au_hnotify_fin_br(add_branch);
  261. +out_br:
  262. + kfree(add_branch);
  263. +out:
  264. + return ERR_PTR(err);
  265. +}
  266. +
  267. +/*
  268. + * test if the branch permission is legal or not.
  269. + */
  270. +static int test_br(struct inode *inode, int brperm, char *path)
  271. +{
  272. + int err;
  273. +
  274. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  275. + if (!err)
  276. + goto out;
  277. +
  278. + err = -EINVAL;
  279. + pr_err("write permission for readonly mount or inode, %s\n", path);
  280. +
  281. +out:
  282. + return err;
  283. +}
  284. +
  285. +/*
  286. + * returns:
  287. + * 0: success, the caller will add it
  288. + * plus: success, it is already unified, the caller should ignore it
  289. + * minus: error
  290. + */
  291. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  292. +{
  293. + int err;
  294. + aufs_bindex_t bend, bindex;
  295. + struct dentry *root;
  296. + struct inode *inode, *h_inode;
  297. +
  298. + root = sb->s_root;
  299. + bend = au_sbend(sb);
  300. + if (unlikely(bend >= 0
  301. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  302. + err = 1;
  303. + if (!remount) {
  304. + err = -EINVAL;
  305. + pr_err("%s duplicated\n", add->pathname);
  306. + }
  307. + goto out;
  308. + }
  309. +
  310. + err = -ENOSPC; /* -E2BIG; */
  311. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  312. + || AUFS_BRANCH_MAX - 1 <= bend)) {
  313. + pr_err("number of branches exceeded %s\n", add->pathname);
  314. + goto out;
  315. + }
  316. +
  317. + err = -EDOM;
  318. + if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
  319. + pr_err("bad index %d\n", add->bindex);
  320. + goto out;
  321. + }
  322. +
  323. + inode = add->path.dentry->d_inode;
  324. + err = -ENOENT;
  325. + if (unlikely(!inode->i_nlink)) {
  326. + pr_err("no existence %s\n", add->pathname);
  327. + goto out;
  328. + }
  329. +
  330. + err = -EINVAL;
  331. + if (unlikely(inode->i_sb == sb)) {
  332. + pr_err("%s must be outside\n", add->pathname);
  333. + goto out;
  334. + }
  335. +
  336. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  337. + pr_err("unsupported filesystem, %s (%s)\n",
  338. + add->pathname, au_sbtype(inode->i_sb));
  339. + goto out;
  340. + }
  341. +
  342. + if (unlikely(inode->i_sb->s_stack_depth)) {
  343. + pr_err("already stacked, %s (%s)\n",
  344. + add->pathname, au_sbtype(inode->i_sb));
  345. + goto out;
  346. + }
  347. +
  348. + err = test_br(add->path.dentry->d_inode, add->perm, add->pathname);
  349. + if (unlikely(err))
  350. + goto out;
  351. +
  352. + if (bend < 0)
  353. + return 0; /* success */
  354. +
  355. + err = -EINVAL;
  356. + for (bindex = 0; bindex <= bend; bindex++)
  357. + if (unlikely(test_overlap(sb, add->path.dentry,
  358. + au_h_dptr(root, bindex)))) {
  359. + pr_err("%s is overlapped\n", add->pathname);
  360. + goto out;
  361. + }
  362. +
  363. + err = 0;
  364. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  365. + h_inode = au_h_dptr(root, 0)->d_inode;
  366. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  367. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  368. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  369. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  370. + add->pathname,
  371. + i_uid_read(inode), i_gid_read(inode),
  372. + (inode->i_mode & S_IALLUGO),
  373. + i_uid_read(h_inode), i_gid_read(h_inode),
  374. + (h_inode->i_mode & S_IALLUGO));
  375. + }
  376. +
  377. +out:
  378. + return err;
  379. +}
  380. +
  381. +/*
  382. + * initialize or clean the whiteouts for an adding branch
  383. + */
  384. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  385. + int new_perm)
  386. +{
  387. + int err, old_perm;
  388. + aufs_bindex_t bindex;
  389. + struct mutex *h_mtx;
  390. + struct au_wbr *wbr;
  391. + struct au_hinode *hdir;
  392. +
  393. + err = vfsub_mnt_want_write(au_br_mnt(br));
  394. + if (unlikely(err))
  395. + goto out;
  396. +
  397. + wbr = br->br_wbr;
  398. + old_perm = br->br_perm;
  399. + br->br_perm = new_perm;
  400. + hdir = NULL;
  401. + h_mtx = NULL;
  402. + bindex = au_br_index(sb, br->br_id);
  403. + if (0 <= bindex) {
  404. + hdir = au_hi(sb->s_root->d_inode, bindex);
  405. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  406. + } else {
  407. + h_mtx = &au_br_dentry(br)->d_inode->i_mutex;
  408. + mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
  409. + }
  410. + if (!wbr)
  411. + err = au_wh_init(br, sb);
  412. + else {
  413. + wbr_wh_write_lock(wbr);
  414. + err = au_wh_init(br, sb);
  415. + wbr_wh_write_unlock(wbr);
  416. + }
  417. + if (hdir)
  418. + au_hn_imtx_unlock(hdir);
  419. + else
  420. + mutex_unlock(h_mtx);
  421. + vfsub_mnt_drop_write(au_br_mnt(br));
  422. + br->br_perm = old_perm;
  423. +
  424. + if (!err && wbr && !au_br_writable(new_perm)) {
  425. + kfree(wbr);
  426. + br->br_wbr = NULL;
  427. + }
  428. +
  429. +out:
  430. + return err;
  431. +}
  432. +
  433. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  434. + int perm)
  435. +{
  436. + int err;
  437. + struct kstatfs kst;
  438. + struct au_wbr *wbr;
  439. +
  440. + wbr = br->br_wbr;
  441. + au_rw_init(&wbr->wbr_wh_rwsem);
  442. + memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
  443. + atomic_set(&wbr->wbr_wh_running, 0);
  444. + wbr->wbr_bytes = 0;
  445. +
  446. + /*
  447. + * a limit for rmdir/rename a dir
  448. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  449. + */
  450. + err = vfs_statfs(&br->br_path, &kst);
  451. + if (unlikely(err))
  452. + goto out;
  453. + err = -EINVAL;
  454. + if (kst.f_namelen >= NAME_MAX)
  455. + err = au_br_init_wh(sb, br, perm);
  456. + else
  457. + pr_err("%pd(%s), unsupported namelen %ld\n",
  458. + au_br_dentry(br),
  459. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  460. +
  461. +out:
  462. + return err;
  463. +}
  464. +
  465. +/* initialize a new branch */
  466. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  467. + struct au_opt_add *add)
  468. +{
  469. + int err;
  470. +
  471. + err = 0;
  472. + memset(&br->br_xino, 0, sizeof(br->br_xino));
  473. + mutex_init(&br->br_xino.xi_nondir_mtx);
  474. + br->br_perm = add->perm;
  475. + br->br_path = add->path; /* set first, path_get() later */
  476. + spin_lock_init(&br->br_dykey_lock);
  477. + memset(br->br_dykey, 0, sizeof(br->br_dykey));
  478. + atomic_set(&br->br_count, 0);
  479. + atomic_set(&br->br_xino_running, 0);
  480. + br->br_id = au_new_br_id(sb);
  481. + AuDebugOn(br->br_id < 0);
  482. +
  483. + if (au_br_writable(add->perm)) {
  484. + err = au_wbr_init(br, sb, add->perm);
  485. + if (unlikely(err))
  486. + goto out_err;
  487. + }
  488. +
  489. + if (au_opt_test(au_mntflags(sb), XINO)) {
  490. + err = au_xino_br(sb, br, add->path.dentry->d_inode->i_ino,
  491. + au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
  492. + if (unlikely(err)) {
  493. + AuDebugOn(br->br_xino.xi_file);
  494. + goto out_err;
  495. + }
  496. + }
  497. +
  498. + sysaufs_br_init(br);
  499. + path_get(&br->br_path);
  500. + goto out; /* success */
  501. +
  502. +out_err:
  503. + memset(&br->br_path, 0, sizeof(br->br_path));
  504. +out:
  505. + return err;
  506. +}
  507. +
  508. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  509. + struct au_branch *br, aufs_bindex_t bend,
  510. + aufs_bindex_t amount)
  511. +{
  512. + struct au_branch **brp;
  513. +
  514. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  515. +
  516. + brp = sbinfo->si_branch + bindex;
  517. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  518. + *brp = br;
  519. + sbinfo->si_bend++;
  520. + if (unlikely(bend < 0))
  521. + sbinfo->si_bend = 0;
  522. +}
  523. +
  524. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  525. + aufs_bindex_t bend, aufs_bindex_t amount)
  526. +{
  527. + struct au_hdentry *hdp;
  528. +
  529. + AuRwMustWriteLock(&dinfo->di_rwsem);
  530. +
  531. + hdp = dinfo->di_hdentry + bindex;
  532. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  533. + au_h_dentry_init(hdp);
  534. + dinfo->di_bend++;
  535. + if (unlikely(bend < 0))
  536. + dinfo->di_bstart = 0;
  537. +}
  538. +
  539. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  540. + aufs_bindex_t bend, aufs_bindex_t amount)
  541. +{
  542. + struct au_hinode *hip;
  543. +
  544. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  545. +
  546. + hip = iinfo->ii_hinode + bindex;
  547. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  548. + hip->hi_inode = NULL;
  549. + au_hn_init(hip);
  550. + iinfo->ii_bend++;
  551. + if (unlikely(bend < 0))
  552. + iinfo->ii_bstart = 0;
  553. +}
  554. +
  555. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  556. + aufs_bindex_t bindex)
  557. +{
  558. + struct dentry *root, *h_dentry;
  559. + struct inode *root_inode;
  560. + aufs_bindex_t bend, amount;
  561. +
  562. + root = sb->s_root;
  563. + root_inode = root->d_inode;
  564. + bend = au_sbend(sb);
  565. + amount = bend + 1 - bindex;
  566. + h_dentry = au_br_dentry(br);
  567. + au_sbilist_lock();
  568. + au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
  569. + au_br_do_add_hdp(au_di(root), bindex, bend, amount);
  570. + au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
  571. + au_set_h_dptr(root, bindex, dget(h_dentry));
  572. + au_set_h_iptr(root_inode, bindex, au_igrab(h_dentry->d_inode),
  573. + /*flags*/0);
  574. + au_sbilist_unlock();
  575. +}
  576. +
  577. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  578. +{
  579. + int err;
  580. + aufs_bindex_t bend, add_bindex;
  581. + struct dentry *root, *h_dentry;
  582. + struct inode *root_inode;
  583. + struct au_branch *add_branch;
  584. +
  585. + root = sb->s_root;
  586. + root_inode = root->d_inode;
  587. + IMustLock(root_inode);
  588. + err = test_add(sb, add, remount);
  589. + if (unlikely(err < 0))
  590. + goto out;
  591. + if (err) {
  592. + err = 0;
  593. + goto out; /* success */
  594. + }
  595. +
  596. + bend = au_sbend(sb);
  597. + add_branch = au_br_alloc(sb, bend + 2, add->perm);
  598. + err = PTR_ERR(add_branch);
  599. + if (IS_ERR(add_branch))
  600. + goto out;
  601. +
  602. + err = au_br_init(add_branch, sb, add);
  603. + if (unlikely(err)) {
  604. + au_br_do_free(add_branch);
  605. + goto out;
  606. + }
  607. +
  608. + add_bindex = add->bindex;
  609. + if (!remount)
  610. + au_br_do_add(sb, add_branch, add_bindex);
  611. + else {
  612. + sysaufs_brs_del(sb, add_bindex);
  613. + au_br_do_add(sb, add_branch, add_bindex);
  614. + sysaufs_brs_add(sb, add_bindex);
  615. + }
  616. +
  617. + h_dentry = add->path.dentry;
  618. + if (!add_bindex) {
  619. + au_cpup_attr_all(root_inode, /*force*/1);
  620. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  621. + } else
  622. + au_add_nlink(root_inode, h_dentry->d_inode);
  623. +
  624. + /*
  625. + * this test/set prevents aufs from handling unnecesary notify events
  626. + * of xino files, in case of re-adding a writable branch which was
  627. + * once detached from aufs.
  628. + */
  629. + if (au_xino_brid(sb) < 0
  630. + && au_br_writable(add_branch->br_perm)
  631. + && !au_test_fs_bad_xino(h_dentry->d_sb)
  632. + && add_branch->br_xino.xi_file
  633. + && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
  634. + au_xino_brid_set(sb, add_branch->br_id);
  635. +
  636. +out:
  637. + return err;
  638. +}
  639. +
  640. +/* ---------------------------------------------------------------------- */
  641. +
  642. +static unsigned long long au_farray_cb(void *a,
  643. + unsigned long long max __maybe_unused,
  644. + void *arg)
  645. +{
  646. + unsigned long long n;
  647. + struct file **p, *f;
  648. + struct au_sphlhead *files;
  649. + struct au_finfo *finfo;
  650. + struct super_block *sb = arg;
  651. +
  652. + n = 0;
  653. + p = a;
  654. + files = &au_sbi(sb)->si_files;
  655. + spin_lock(&files->spin);
  656. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  657. + f = finfo->fi_file;
  658. + if (file_count(f)
  659. + && !special_file(file_inode(f)->i_mode)) {
  660. + get_file(f);
  661. + *p++ = f;
  662. + n++;
  663. + AuDebugOn(n > max);
  664. + }
  665. + }
  666. + spin_unlock(&files->spin);
  667. +
  668. + return n;
  669. +}
  670. +
  671. +static struct file **au_farray_alloc(struct super_block *sb,
  672. + unsigned long long *max)
  673. +{
  674. + *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
  675. + return au_array_alloc(max, au_farray_cb, sb);
  676. +}
  677. +
  678. +static void au_farray_free(struct file **a, unsigned long long max)
  679. +{
  680. + unsigned long long ull;
  681. +
  682. + for (ull = 0; ull < max; ull++)
  683. + if (a[ull])
  684. + fput(a[ull]);
  685. + au_array_free(a);
  686. +}
  687. +
  688. +/* ---------------------------------------------------------------------- */
  689. +
  690. +/*
  691. + * delete a branch
  692. + */
  693. +
  694. +/* to show the line number, do not make it inlined function */
  695. +#define AuVerbose(do_info, fmt, ...) do { \
  696. + if (do_info) \
  697. + pr_info(fmt, ##__VA_ARGS__); \
  698. +} while (0)
  699. +
  700. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
  701. + aufs_bindex_t bend)
  702. +{
  703. + return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
  704. +}
  705. +
  706. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
  707. + aufs_bindex_t bend)
  708. +{
  709. + return au_test_ibusy(dentry->d_inode, bstart, bend);
  710. +}
  711. +
  712. +/*
  713. + * test if the branch is deletable or not.
  714. + */
  715. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  716. + unsigned int sigen, const unsigned int verbose)
  717. +{
  718. + int err, i, j, ndentry;
  719. + aufs_bindex_t bstart, bend;
  720. + struct au_dcsub_pages dpages;
  721. + struct au_dpage *dpage;
  722. + struct dentry *d;
  723. +
  724. + err = au_dpages_init(&dpages, GFP_NOFS);
  725. + if (unlikely(err))
  726. + goto out;
  727. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  728. + if (unlikely(err))
  729. + goto out_dpages;
  730. +
  731. + for (i = 0; !err && i < dpages.ndpage; i++) {
  732. + dpage = dpages.dpages + i;
  733. + ndentry = dpage->ndentry;
  734. + for (j = 0; !err && j < ndentry; j++) {
  735. + d = dpage->dentries[j];
  736. + AuDebugOn(au_dcount(d) <= 0);
  737. + if (!au_digen_test(d, sigen)) {
  738. + di_read_lock_child(d, AuLock_IR);
  739. + if (unlikely(au_dbrange_test(d))) {
  740. + di_read_unlock(d, AuLock_IR);
  741. + continue;
  742. + }
  743. + } else {
  744. + di_write_lock_child(d);
  745. + if (unlikely(au_dbrange_test(d))) {
  746. + di_write_unlock(d);
  747. + continue;
  748. + }
  749. + err = au_reval_dpath(d, sigen);
  750. + if (!err)
  751. + di_downgrade_lock(d, AuLock_IR);
  752. + else {
  753. + di_write_unlock(d);
  754. + break;
  755. + }
  756. + }
  757. +
  758. + /* AuDbgDentry(d); */
  759. + bstart = au_dbstart(d);
  760. + bend = au_dbend(d);
  761. + if (bstart <= bindex
  762. + && bindex <= bend
  763. + && au_h_dptr(d, bindex)
  764. + && au_test_dbusy(d, bstart, bend)) {
  765. + err = -EBUSY;
  766. + AuVerbose(verbose, "busy %pd\n", d);
  767. + AuDbgDentry(d);
  768. + }
  769. + di_read_unlock(d, AuLock_IR);
  770. + }
  771. + }
  772. +
  773. +out_dpages:
  774. + au_dpages_free(&dpages);
  775. +out:
  776. + return err;
  777. +}
  778. +
  779. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  780. + unsigned int sigen, const unsigned int verbose)
  781. +{
  782. + int err;
  783. + unsigned long long max, ull;
  784. + struct inode *i, **array;
  785. + aufs_bindex_t bstart, bend;
  786. +
  787. + array = au_iarray_alloc(sb, &max);
  788. + err = PTR_ERR(array);
  789. + if (IS_ERR(array))
  790. + goto out;
  791. +
  792. + err = 0;
  793. + AuDbg("b%d\n", bindex);
  794. + for (ull = 0; !err && ull < max; ull++) {
  795. + i = array[ull];
  796. + if (unlikely(!i))
  797. + break;
  798. + if (i->i_ino == AUFS_ROOT_INO)
  799. + continue;
  800. +
  801. + /* AuDbgInode(i); */
  802. + if (au_iigen(i, NULL) == sigen)
  803. + ii_read_lock_child(i);
  804. + else {
  805. + ii_write_lock_child(i);
  806. + err = au_refresh_hinode_self(i);
  807. + au_iigen_dec(i);
  808. + if (!err)
  809. + ii_downgrade_lock(i);
  810. + else {
  811. + ii_write_unlock(i);
  812. + break;
  813. + }
  814. + }
  815. +
  816. + bstart = au_ibstart(i);
  817. + bend = au_ibend(i);
  818. + if (bstart <= bindex
  819. + && bindex <= bend
  820. + && au_h_iptr(i, bindex)
  821. + && au_test_ibusy(i, bstart, bend)) {
  822. + err = -EBUSY;
  823. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  824. + AuDbgInode(i);
  825. + }
  826. + ii_read_unlock(i);
  827. + }
  828. + au_iarray_free(array, max);
  829. +
  830. +out:
  831. + return err;
  832. +}
  833. +
  834. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  835. + const unsigned int verbose)
  836. +{
  837. + int err;
  838. + unsigned int sigen;
  839. +
  840. + sigen = au_sigen(root->d_sb);
  841. + DiMustNoWaiters(root);
  842. + IiMustNoWaiters(root->d_inode);
  843. + di_write_unlock(root);
  844. + err = test_dentry_busy(root, bindex, sigen, verbose);
  845. + if (!err)
  846. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  847. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  848. +
  849. + return err;
  850. +}
  851. +
  852. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  853. + struct file **to_free, int *idx)
  854. +{
  855. + int err;
  856. + unsigned char matched, root;
  857. + aufs_bindex_t bindex, bend;
  858. + struct au_fidir *fidir;
  859. + struct au_hfile *hfile;
  860. +
  861. + err = 0;
  862. + root = IS_ROOT(file->f_path.dentry);
  863. + if (root) {
  864. + get_file(file);
  865. + to_free[*idx] = file;
  866. + (*idx)++;
  867. + goto out;
  868. + }
  869. +
  870. + matched = 0;
  871. + fidir = au_fi(file)->fi_hdir;
  872. + AuDebugOn(!fidir);
  873. + bend = au_fbend_dir(file);
  874. + for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
  875. + hfile = fidir->fd_hfile + bindex;
  876. + if (!hfile->hf_file)
  877. + continue;
  878. +
  879. + if (hfile->hf_br->br_id == br_id) {
  880. + matched = 1;
  881. + break;
  882. + }
  883. + }
  884. + if (matched)
  885. + err = -EBUSY;
  886. +
  887. +out:
  888. + return err;
  889. +}
  890. +
  891. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  892. + struct file **to_free, int opened)
  893. +{
  894. + int err, idx;
  895. + unsigned long long ull, max;
  896. + aufs_bindex_t bstart;
  897. + struct file *file, **array;
  898. + struct dentry *root;
  899. + struct au_hfile *hfile;
  900. +
  901. + array = au_farray_alloc(sb, &max);
  902. + err = PTR_ERR(array);
  903. + if (IS_ERR(array))
  904. + goto out;
  905. +
  906. + err = 0;
  907. + idx = 0;
  908. + root = sb->s_root;
  909. + di_write_unlock(root);
  910. + for (ull = 0; ull < max; ull++) {
  911. + file = array[ull];
  912. + if (unlikely(!file))
  913. + break;
  914. +
  915. + /* AuDbg("%pD\n", file); */
  916. + fi_read_lock(file);
  917. + bstart = au_fbstart(file);
  918. + if (!d_is_dir(file->f_path.dentry)) {
  919. + hfile = &au_fi(file)->fi_htop;
  920. + if (hfile->hf_br->br_id == br_id)
  921. + err = -EBUSY;
  922. + } else
  923. + err = test_dir_busy(file, br_id, to_free, &idx);
  924. + fi_read_unlock(file);
  925. + if (unlikely(err))
  926. + break;
  927. + }
  928. + di_write_lock_child(root);
  929. + au_farray_free(array, max);
  930. + AuDebugOn(idx > opened);
  931. +
  932. +out:
  933. + return err;
  934. +}
  935. +
  936. +static void br_del_file(struct file **to_free, unsigned long long opened,
  937. + aufs_bindex_t br_id)
  938. +{
  939. + unsigned long long ull;
  940. + aufs_bindex_t bindex, bstart, bend, bfound;
  941. + struct file *file;
  942. + struct au_fidir *fidir;
  943. + struct au_hfile *hfile;
  944. +
  945. + for (ull = 0; ull < opened; ull++) {
  946. + file = to_free[ull];
  947. + if (unlikely(!file))
  948. + break;
  949. +
  950. + /* AuDbg("%pD\n", file); */
  951. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  952. + bfound = -1;
  953. + fidir = au_fi(file)->fi_hdir;
  954. + AuDebugOn(!fidir);
  955. + fi_write_lock(file);
  956. + bstart = au_fbstart(file);
  957. + bend = au_fbend_dir(file);
  958. + for (bindex = bstart; bindex <= bend; bindex++) {
  959. + hfile = fidir->fd_hfile + bindex;
  960. + if (!hfile->hf_file)
  961. + continue;
  962. +
  963. + if (hfile->hf_br->br_id == br_id) {
  964. + bfound = bindex;
  965. + break;
  966. + }
  967. + }
  968. + AuDebugOn(bfound < 0);
  969. + au_set_h_fptr(file, bfound, NULL);
  970. + if (bfound == bstart) {
  971. + for (bstart++; bstart <= bend; bstart++)
  972. + if (au_hf_dir(file, bstart)) {
  973. + au_set_fbstart(file, bstart);
  974. + break;
  975. + }
  976. + }
  977. + fi_write_unlock(file);
  978. + }
  979. +}
  980. +
  981. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  982. + const aufs_bindex_t bindex,
  983. + const aufs_bindex_t bend)
  984. +{
  985. + struct au_branch **brp, **p;
  986. +
  987. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  988. +
  989. + brp = sbinfo->si_branch + bindex;
  990. + if (bindex < bend)
  991. + memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
  992. + sbinfo->si_branch[0 + bend] = NULL;
  993. + sbinfo->si_bend--;
  994. +
  995. + p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
  996. + if (p)
  997. + sbinfo->si_branch = p;
  998. + /* harmless error */
  999. +}
  1000. +
  1001. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  1002. + const aufs_bindex_t bend)
  1003. +{
  1004. + struct au_hdentry *hdp, *p;
  1005. +
  1006. + AuRwMustWriteLock(&dinfo->di_rwsem);
  1007. +
  1008. + hdp = dinfo->di_hdentry;
  1009. + if (bindex < bend)
  1010. + memmove(hdp + bindex, hdp + bindex + 1,
  1011. + sizeof(*hdp) * (bend - bindex));
  1012. + hdp[0 + bend].hd_dentry = NULL;
  1013. + dinfo->di_bend--;
  1014. +
  1015. + p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
  1016. + if (p)
  1017. + dinfo->di_hdentry = p;
  1018. + /* harmless error */
  1019. +}
  1020. +
  1021. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  1022. + const aufs_bindex_t bend)
  1023. +{
  1024. + struct au_hinode *hip, *p;
  1025. +
  1026. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  1027. +
  1028. + hip = iinfo->ii_hinode + bindex;
  1029. + if (bindex < bend)
  1030. + memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
  1031. + iinfo->ii_hinode[0 + bend].hi_inode = NULL;
  1032. + au_hn_init(iinfo->ii_hinode + bend);
  1033. + iinfo->ii_bend--;
  1034. +
  1035. + p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
  1036. + if (p)
  1037. + iinfo->ii_hinode = p;
  1038. + /* harmless error */
  1039. +}
  1040. +
  1041. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  1042. + struct au_branch *br)
  1043. +{
  1044. + aufs_bindex_t bend;
  1045. + struct au_sbinfo *sbinfo;
  1046. + struct dentry *root, *h_root;
  1047. + struct inode *inode, *h_inode;
  1048. + struct au_hinode *hinode;
  1049. +
  1050. + SiMustWriteLock(sb);
  1051. +
  1052. + root = sb->s_root;
  1053. + inode = root->d_inode;
  1054. + sbinfo = au_sbi(sb);
  1055. + bend = sbinfo->si_bend;
  1056. +
  1057. + h_root = au_h_dptr(root, bindex);
  1058. + hinode = au_hi(inode, bindex);
  1059. + h_inode = au_igrab(hinode->hi_inode);
  1060. + au_hiput(hinode);
  1061. +
  1062. + au_sbilist_lock();
  1063. + au_br_do_del_brp(sbinfo, bindex, bend);
  1064. + au_br_do_del_hdp(au_di(root), bindex, bend);
  1065. + au_br_do_del_hip(au_ii(inode), bindex, bend);
  1066. + au_sbilist_unlock();
  1067. +
  1068. + dput(h_root);
  1069. + iput(h_inode);
  1070. + au_br_do_free(br);
  1071. +}
  1072. +
  1073. +static unsigned long long empty_cb(void *array, unsigned long long max,
  1074. + void *arg)
  1075. +{
  1076. + return max;
  1077. +}
  1078. +
  1079. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  1080. +{
  1081. + int err, rerr, i;
  1082. + unsigned long long opened;
  1083. + unsigned int mnt_flags;
  1084. + aufs_bindex_t bindex, bend, br_id;
  1085. + unsigned char do_wh, verbose;
  1086. + struct au_branch *br;
  1087. + struct au_wbr *wbr;
  1088. + struct dentry *root;
  1089. + struct file **to_free;
  1090. +
  1091. + err = 0;
  1092. + opened = 0;
  1093. + to_free = NULL;
  1094. + root = sb->s_root;
  1095. + bindex = au_find_dbindex(root, del->h_path.dentry);
  1096. + if (bindex < 0) {
  1097. + if (remount)
  1098. + goto out; /* success */
  1099. + err = -ENOENT;
  1100. + pr_err("%s no such branch\n", del->pathname);
  1101. + goto out;
  1102. + }
  1103. + AuDbg("bindex b%d\n", bindex);
  1104. +
  1105. + err = -EBUSY;
  1106. + mnt_flags = au_mntflags(sb);
  1107. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  1108. + bend = au_sbend(sb);
  1109. + if (unlikely(!bend)) {
  1110. + AuVerbose(verbose, "no more branches left\n");
  1111. + goto out;
  1112. + }
  1113. + br = au_sbr(sb, bindex);
  1114. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  1115. +
  1116. + br_id = br->br_id;
  1117. + opened = atomic_read(&br->br_count);
  1118. + if (unlikely(opened)) {
  1119. + to_free = au_array_alloc(&opened, empty_cb, NULL);
  1120. + err = PTR_ERR(to_free);
  1121. + if (IS_ERR(to_free))
  1122. + goto out;
  1123. +
  1124. + err = test_file_busy(sb, br_id, to_free, opened);
  1125. + if (unlikely(err)) {
  1126. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  1127. + goto out;
  1128. + }
  1129. + }
  1130. +
  1131. + wbr = br->br_wbr;
  1132. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  1133. + if (do_wh) {
  1134. + /* instead of WbrWhMustWriteLock(wbr) */
  1135. + SiMustWriteLock(sb);
  1136. + for (i = 0; i < AuBrWh_Last; i++) {
  1137. + dput(wbr->wbr_wh[i]);
  1138. + wbr->wbr_wh[i] = NULL;
  1139. + }
  1140. + }
  1141. +
  1142. + err = test_children_busy(root, bindex, verbose);
  1143. + if (unlikely(err)) {
  1144. + if (do_wh)
  1145. + goto out_wh;
  1146. + goto out;
  1147. + }
  1148. +
  1149. + err = 0;
  1150. + if (to_free) {
  1151. + /*
  1152. + * now we confirmed the branch is deletable.
  1153. + * let's free the remaining opened dirs on the branch.
  1154. + */
  1155. + di_write_unlock(root);
  1156. + br_del_file(to_free, opened, br_id);
  1157. + di_write_lock_child(root);
  1158. + }
  1159. +
  1160. + if (!remount)
  1161. + au_br_do_del(sb, bindex, br);
  1162. + else {
  1163. + sysaufs_brs_del(sb, bindex);
  1164. + au_br_do_del(sb, bindex, br);
  1165. + sysaufs_brs_add(sb, bindex);
  1166. + }
  1167. +
  1168. + if (!bindex) {
  1169. + au_cpup_attr_all(root->d_inode, /*force*/1);
  1170. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  1171. + } else
  1172. + au_sub_nlink(root->d_inode, del->h_path.dentry->d_inode);
  1173. + if (au_opt_test(mnt_flags, PLINK))
  1174. + au_plink_half_refresh(sb, br_id);
  1175. +
  1176. + if (au_xino_brid(sb) == br_id)
  1177. + au_xino_brid_set(sb, -1);
  1178. + goto out; /* success */
  1179. +
  1180. +out_wh:
  1181. + /* revert */
  1182. + rerr = au_br_init_wh(sb, br, br->br_perm);
  1183. + if (rerr)
  1184. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  1185. + del->pathname, rerr);
  1186. +out:
  1187. + if (to_free)
  1188. + au_farray_free(to_free, opened);
  1189. + return err;
  1190. +}
  1191. +
  1192. +/* ---------------------------------------------------------------------- */
  1193. +
  1194. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  1195. +{
  1196. + int err;
  1197. + aufs_bindex_t bstart, bend;
  1198. + struct aufs_ibusy ibusy;
  1199. + struct inode *inode, *h_inode;
  1200. +
  1201. + err = -EPERM;
  1202. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  1203. + goto out;
  1204. +
  1205. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  1206. + if (!err)
  1207. + err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
  1208. + if (unlikely(err)) {
  1209. + err = -EFAULT;
  1210. + AuTraceErr(err);
  1211. + goto out;
  1212. + }
  1213. +
  1214. + err = -EINVAL;
  1215. + si_read_lock(sb, AuLock_FLUSH);
  1216. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
  1217. + goto out_unlock;
  1218. +
  1219. + err = 0;
  1220. + ibusy.h_ino = 0; /* invalid */
  1221. + inode = ilookup(sb, ibusy.ino);
  1222. + if (!inode
  1223. + || inode->i_ino == AUFS_ROOT_INO
  1224. + || is_bad_inode(inode))
  1225. + goto out_unlock;
  1226. +
  1227. + ii_read_lock_child(inode);
  1228. + bstart = au_ibstart(inode);
  1229. + bend = au_ibend(inode);
  1230. + if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
  1231. + h_inode = au_h_iptr(inode, ibusy.bindex);
  1232. + if (h_inode && au_test_ibusy(inode, bstart, bend))
  1233. + ibusy.h_ino = h_inode->i_ino;
  1234. + }
  1235. + ii_read_unlock(inode);
  1236. + iput(inode);
  1237. +
  1238. +out_unlock:
  1239. + si_read_unlock(sb);
  1240. + if (!err) {
  1241. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  1242. + if (unlikely(err)) {
  1243. + err = -EFAULT;
  1244. + AuTraceErr(err);
  1245. + }
  1246. + }
  1247. +out:
  1248. + return err;
  1249. +}
  1250. +
  1251. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  1252. +{
  1253. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  1254. +}
  1255. +
  1256. +#ifdef CONFIG_COMPAT
  1257. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  1258. +{
  1259. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  1260. +}
  1261. +#endif
  1262. +
  1263. +/* ---------------------------------------------------------------------- */
  1264. +
  1265. +/*
  1266. + * change a branch permission
  1267. + */
  1268. +
  1269. +static void au_warn_ima(void)
  1270. +{
  1271. +#ifdef CONFIG_IMA
  1272. + /* since it doesn't support mark_files_ro() */
  1273. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  1274. +#endif
  1275. +}
  1276. +
  1277. +static int do_need_sigen_inc(int a, int b)
  1278. +{
  1279. + return au_br_whable(a) && !au_br_whable(b);
  1280. +}
  1281. +
  1282. +static int need_sigen_inc(int old, int new)
  1283. +{
  1284. + return do_need_sigen_inc(old, new)
  1285. + || do_need_sigen_inc(new, old);
  1286. +}
  1287. +
  1288. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  1289. +{
  1290. + int err, do_warn;
  1291. + unsigned int mnt_flags;
  1292. + unsigned long long ull, max;
  1293. + aufs_bindex_t br_id;
  1294. + unsigned char verbose, writer;
  1295. + struct file *file, *hf, **array;
  1296. + struct au_hfile *hfile;
  1297. +
  1298. + mnt_flags = au_mntflags(sb);
  1299. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  1300. +
  1301. + array = au_farray_alloc(sb, &max);
  1302. + err = PTR_ERR(array);
  1303. + if (IS_ERR(array))
  1304. + goto out;
  1305. +
  1306. + do_warn = 0;
  1307. + br_id = au_sbr_id(sb, bindex);
  1308. + for (ull = 0; ull < max; ull++) {
  1309. + file = array[ull];
  1310. + if (unlikely(!file))
  1311. + break;
  1312. +
  1313. + /* AuDbg("%pD\n", file); */
  1314. + fi_read_lock(file);
  1315. + if (unlikely(au_test_mmapped(file))) {
  1316. + err = -EBUSY;
  1317. + AuVerbose(verbose, "mmapped %pD\n", file);
  1318. + AuDbgFile(file);
  1319. + FiMustNoWaiters(file);
  1320. + fi_read_unlock(file);
  1321. + goto out_array;
  1322. + }
  1323. +
  1324. + hfile = &au_fi(file)->fi_htop;
  1325. + hf = hfile->hf_file;
  1326. + if (!d_is_reg(file->f_path.dentry)
  1327. + || !(file->f_mode & FMODE_WRITE)
  1328. + || hfile->hf_br->br_id != br_id
  1329. + || !(hf->f_mode & FMODE_WRITE))
  1330. + array[ull] = NULL;
  1331. + else {
  1332. + do_warn = 1;
  1333. + get_file(file);
  1334. + }
  1335. +
  1336. + FiMustNoWaiters(file);
  1337. + fi_read_unlock(file);
  1338. + fput(file);
  1339. + }
  1340. +
  1341. + err = 0;
  1342. + if (do_warn)
  1343. + au_warn_ima();
  1344. +
  1345. + for (ull = 0; ull < max; ull++) {
  1346. + file = array[ull];
  1347. + if (!file)
  1348. + continue;
  1349. +
  1350. + /* todo: already flushed? */
  1351. + /*
  1352. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  1353. + * approach which resets f_mode and calls mnt_drop_write() and
  1354. + * file_release_write() for each file, because the branch
  1355. + * attribute in aufs world is totally different from the native
  1356. + * fs rw/ro mode.
  1357. + */
  1358. + /* fi_read_lock(file); */
  1359. + hfile = &au_fi(file)->fi_htop;
  1360. + hf = hfile->hf_file;
  1361. + /* fi_read_unlock(file); */
  1362. + spin_lock(&hf->f_lock);
  1363. + writer = !!(hf->f_mode & FMODE_WRITER);
  1364. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  1365. + spin_unlock(&hf->f_lock);
  1366. + if (writer) {
  1367. + put_write_access(file_inode(hf));
  1368. + __mnt_drop_write(hf->f_path.mnt);
  1369. + }
  1370. + }
  1371. +
  1372. +out_array:
  1373. + au_farray_free(array, max);
  1374. +out:
  1375. + AuTraceErr(err);
  1376. + return err;
  1377. +}
  1378. +
  1379. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  1380. + int *do_refresh)
  1381. +{
  1382. + int err, rerr;
  1383. + aufs_bindex_t bindex;
  1384. + struct dentry *root;
  1385. + struct au_branch *br;
  1386. + struct au_br_fhsm *bf;
  1387. +
  1388. + root = sb->s_root;
  1389. + bindex = au_find_dbindex(root, mod->h_root);
  1390. + if (bindex < 0) {
  1391. + if (remount)
  1392. + return 0; /* success */
  1393. + err = -ENOENT;
  1394. + pr_err("%s no such branch\n", mod->path);
  1395. + goto out;
  1396. + }
  1397. + AuDbg("bindex b%d\n", bindex);
  1398. +
  1399. + err = test_br(mod->h_root->d_inode, mod->perm, mod->path);
  1400. + if (unlikely(err))
  1401. + goto out;
  1402. +
  1403. + br = au_sbr(sb, bindex);
  1404. + AuDebugOn(mod->h_root != au_br_dentry(br));
  1405. + if (br->br_perm == mod->perm)
  1406. + return 0; /* success */
  1407. +
  1408. + /* pre-allocate for non-fhsm --> fhsm */
  1409. + bf = NULL;
  1410. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  1411. + err = au_fhsm_br_alloc(br);
  1412. + if (unlikely(err))
  1413. + goto out;
  1414. + bf = br->br_fhsm;
  1415. + br->br_fhsm = NULL;
  1416. + }
  1417. +
  1418. + if (au_br_writable(br->br_perm)) {
  1419. + /* remove whiteout base */
  1420. + err = au_br_init_wh(sb, br, mod->perm);
  1421. + if (unlikely(err))
  1422. + goto out_bf;
  1423. +
  1424. + if (!au_br_writable(mod->perm)) {
  1425. + /* rw --> ro, file might be mmapped */
  1426. + DiMustNoWaiters(root);
  1427. + IiMustNoWaiters(root->d_inode);
  1428. + di_write_unlock(root);
  1429. + err = au_br_mod_files_ro(sb, bindex);
  1430. + /* aufs_write_lock() calls ..._child() */
  1431. + di_write_lock_child(root);
  1432. +
  1433. + if (unlikely(err)) {
  1434. + rerr = -ENOMEM;
  1435. + br->br_wbr = kmalloc(sizeof(*br->br_wbr),
  1436. + GFP_NOFS);
  1437. + if (br->br_wbr)
  1438. + rerr = au_wbr_init(br, sb, br->br_perm);
  1439. + if (unlikely(rerr)) {
  1440. + AuIOErr("nested error %d (%d)\n",
  1441. + rerr, err);
  1442. + br->br_perm = mod->perm;
  1443. + }
  1444. + }
  1445. + }
  1446. + } else if (au_br_writable(mod->perm)) {
  1447. + /* ro --> rw */
  1448. + err = -ENOMEM;
  1449. + br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
  1450. + if (br->br_wbr) {
  1451. + err = au_wbr_init(br, sb, mod->perm);
  1452. + if (unlikely(err)) {
  1453. + kfree(br->br_wbr);
  1454. + br->br_wbr = NULL;
  1455. + }
  1456. + }
  1457. + }
  1458. + if (unlikely(err))
  1459. + goto out_bf;
  1460. +
  1461. + if (au_br_fhsm(br->br_perm)) {
  1462. + if (!au_br_fhsm(mod->perm)) {
  1463. + /* fhsm --> non-fhsm */
  1464. + au_br_fhsm_fin(br->br_fhsm);
  1465. + kfree(br->br_fhsm);
  1466. + br->br_fhsm = NULL;
  1467. + }
  1468. + } else if (au_br_fhsm(mod->perm))
  1469. + /* non-fhsm --> fhsm */
  1470. + br->br_fhsm = bf;
  1471. +
  1472. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  1473. + br->br_perm = mod->perm;
  1474. + goto out; /* success */
  1475. +
  1476. +out_bf:
  1477. + kfree(bf);
  1478. +out:
  1479. + AuTraceErr(err);
  1480. + return err;
  1481. +}
  1482. +
  1483. +/* ---------------------------------------------------------------------- */
  1484. +
  1485. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  1486. +{
  1487. + int err;
  1488. + struct kstatfs kstfs;
  1489. +
  1490. + err = vfs_statfs(&br->br_path, &kstfs);
  1491. + if (!err) {
  1492. + stfs->f_blocks = kstfs.f_blocks;
  1493. + stfs->f_bavail = kstfs.f_bavail;
  1494. + stfs->f_files = kstfs.f_files;
  1495. + stfs->f_ffree = kstfs.f_ffree;
  1496. + }
  1497. +
  1498. + return err;
  1499. +}
  1500. diff -Nur linux-4.0.5.orig/fs/aufs/branch.h linux-4.0.5/fs/aufs/branch.h
  1501. --- linux-4.0.5.orig/fs/aufs/branch.h 1969-12-31 18:00:00.000000000 -0600
  1502. +++ linux-4.0.5/fs/aufs/branch.h 2015-06-21 14:07:48.682432781 -0500
  1503. @@ -0,0 +1,279 @@
  1504. +/*
  1505. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  1506. + *
  1507. + * This program, aufs is free software; you can redistribute it and/or modify
  1508. + * it under the terms of the GNU General Public License as published by
  1509. + * the Free Software Foundation; either version 2 of the License, or
  1510. + * (at your option) any later version.
  1511. + *
  1512. + * This program is distributed in the hope that it will be useful,
  1513. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1514. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1515. + * GNU General Public License for more details.
  1516. + *
  1517. + * You should have received a copy of the GNU General Public License
  1518. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1519. + */
  1520. +
  1521. +/*
  1522. + * branch filesystems and xino for them
  1523. + */
  1524. +
  1525. +#ifndef __AUFS_BRANCH_H__
  1526. +#define __AUFS_BRANCH_H__
  1527. +
  1528. +#ifdef __KERNEL__
  1529. +
  1530. +#include <linux/mount.h>
  1531. +#include "dynop.h"
  1532. +#include "rwsem.h"
  1533. +#include "super.h"
  1534. +
  1535. +/* ---------------------------------------------------------------------- */
  1536. +
  1537. +/* a xino file */
  1538. +struct au_xino_file {
  1539. + struct file *xi_file;
  1540. + struct mutex xi_nondir_mtx;
  1541. +
  1542. + /* todo: make xino files an array to support huge inode number */
  1543. +
  1544. +#ifdef CONFIG_DEBUG_FS
  1545. + struct dentry *xi_dbgaufs;
  1546. +#endif
  1547. +};
  1548. +
  1549. +/* File-based Hierarchical Storage Management */
  1550. +struct au_br_fhsm {
  1551. +#ifdef CONFIG_AUFS_FHSM
  1552. + struct mutex bf_lock;
  1553. + unsigned long bf_jiffy;
  1554. + struct aufs_stfs bf_stfs;
  1555. + int bf_readable;
  1556. +#endif
  1557. +};
  1558. +
  1559. +/* members for writable branch only */
  1560. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  1561. +struct au_wbr {
  1562. + struct au_rwsem wbr_wh_rwsem;
  1563. + struct dentry *wbr_wh[AuBrWh_Last];
  1564. + atomic_t wbr_wh_running;
  1565. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  1566. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  1567. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  1568. +
  1569. + /* mfs mode */
  1570. + unsigned long long wbr_bytes;
  1571. +};
  1572. +
  1573. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  1574. +#define AuBrDynOp (AuDyLast * 4)
  1575. +
  1576. +#ifdef CONFIG_AUFS_HFSNOTIFY
  1577. +/* support for asynchronous destruction */
  1578. +struct au_br_hfsnotify {
  1579. + struct fsnotify_group *hfsn_group;
  1580. +};
  1581. +#endif
  1582. +
  1583. +/* sysfs entries */
  1584. +struct au_brsysfs {
  1585. + char name[16];
  1586. + struct attribute attr;
  1587. +};
  1588. +
  1589. +enum {
  1590. + AuBrSysfs_BR,
  1591. + AuBrSysfs_BRID,
  1592. + AuBrSysfs_Last
  1593. +};
  1594. +
  1595. +/* protected by superblock rwsem */
  1596. +struct au_branch {
  1597. + struct au_xino_file br_xino;
  1598. +
  1599. + aufs_bindex_t br_id;
  1600. +
  1601. + int br_perm;
  1602. + struct path br_path;
  1603. + spinlock_t br_dykey_lock;
  1604. + struct au_dykey *br_dykey[AuBrDynOp];
  1605. + atomic_t br_count;
  1606. +
  1607. + struct au_wbr *br_wbr;
  1608. + struct au_br_fhsm *br_fhsm;
  1609. +
  1610. + /* xino truncation */
  1611. + atomic_t br_xino_running;
  1612. +
  1613. +#ifdef CONFIG_AUFS_HFSNOTIFY
  1614. + struct au_br_hfsnotify *br_hfsn;
  1615. +#endif
  1616. +
  1617. +#ifdef CONFIG_SYSFS
  1618. + /* entries under sysfs per mount-point */
  1619. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  1620. +#endif
  1621. +};
  1622. +
  1623. +/* ---------------------------------------------------------------------- */
  1624. +
  1625. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  1626. +{
  1627. + return br->br_path.mnt;
  1628. +}
  1629. +
  1630. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  1631. +{
  1632. + return br->br_path.dentry;
  1633. +}
  1634. +
  1635. +static inline struct super_block *au_br_sb(struct au_branch *br)
  1636. +{
  1637. + return au_br_mnt(br)->mnt_sb;
  1638. +}
  1639. +
  1640. +static inline int au_br_rdonly(struct au_branch *br)
  1641. +{
  1642. + return ((au_br_sb(br)->s_flags & MS_RDONLY)
  1643. + || !au_br_writable(br->br_perm))
  1644. + ? -EROFS : 0;
  1645. +}
  1646. +
  1647. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  1648. +{
  1649. +#ifdef CONFIG_AUFS_HNOTIFY
  1650. + return !(brperm & AuBrPerm_RR);
  1651. +#else
  1652. + return 0;
  1653. +#endif
  1654. +}
  1655. +
  1656. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  1657. +{
  1658. + int err, exec_flag;
  1659. +
  1660. + err = 0;
  1661. + exec_flag = oflag & __FMODE_EXEC;
  1662. + if (unlikely(exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC)))
  1663. + err = -EACCES;
  1664. +
  1665. + return err;
  1666. +}
  1667. +
  1668. +/* ---------------------------------------------------------------------- */
  1669. +
  1670. +/* branch.c */
  1671. +struct au_sbinfo;
  1672. +void au_br_free(struct au_sbinfo *sinfo);
  1673. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  1674. +struct au_opt_add;
  1675. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  1676. +struct au_opt_del;
  1677. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  1678. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  1679. +#ifdef CONFIG_COMPAT
  1680. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  1681. +#endif
  1682. +struct au_opt_mod;
  1683. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  1684. + int *do_refresh);
  1685. +struct aufs_stfs;
  1686. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  1687. +
  1688. +/* xino.c */
  1689. +static const loff_t au_loff_max = LLONG_MAX;
  1690. +
  1691. +int au_xib_trunc(struct super_block *sb);
  1692. +ssize_t xino_fread(au_readf_t func, struct file *file, void *buf, size_t size,
  1693. + loff_t *pos);
  1694. +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
  1695. + loff_t *pos);
  1696. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
  1697. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
  1698. +ino_t au_xino_new_ino(struct super_block *sb);
  1699. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  1700. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  1701. + ino_t ino);
  1702. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  1703. + ino_t *ino);
  1704. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  1705. + struct file *base_file, int do_test);
  1706. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
  1707. +
  1708. +struct au_opt_xino;
  1709. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
  1710. +void au_xino_clr(struct super_block *sb);
  1711. +struct file *au_xino_def(struct super_block *sb);
  1712. +int au_xino_path(struct seq_file *seq, struct file *file);
  1713. +
  1714. +/* ---------------------------------------------------------------------- */
  1715. +
  1716. +/* Superblock to branch */
  1717. +static inline
  1718. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  1719. +{
  1720. + return au_sbr(sb, bindex)->br_id;
  1721. +}
  1722. +
  1723. +static inline
  1724. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  1725. +{
  1726. + return au_br_mnt(au_sbr(sb, bindex));
  1727. +}
  1728. +
  1729. +static inline
  1730. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  1731. +{
  1732. + return au_br_sb(au_sbr(sb, bindex));
  1733. +}
  1734. +
  1735. +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
  1736. +{
  1737. + atomic_dec(&au_sbr(sb, bindex)->br_count);
  1738. +}
  1739. +
  1740. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  1741. +{
  1742. + return au_sbr(sb, bindex)->br_perm;
  1743. +}
  1744. +
  1745. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  1746. +{
  1747. + return au_br_whable(au_sbr_perm(sb, bindex));
  1748. +}
  1749. +
  1750. +/* ---------------------------------------------------------------------- */
  1751. +
  1752. +/*
  1753. + * wbr_wh_read_lock, wbr_wh_write_lock
  1754. + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
  1755. + */
  1756. +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
  1757. +
  1758. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
  1759. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
  1760. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
  1761. +
  1762. +/* ---------------------------------------------------------------------- */
  1763. +
  1764. +#ifdef CONFIG_AUFS_FHSM
  1765. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  1766. +{
  1767. + mutex_init(&brfhsm->bf_lock);
  1768. + brfhsm->bf_jiffy = 0;
  1769. + brfhsm->bf_readable = 0;
  1770. +}
  1771. +
  1772. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  1773. +{
  1774. + mutex_destroy(&brfhsm->bf_lock);
  1775. +}
  1776. +#else
  1777. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  1778. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  1779. +#endif
  1780. +
  1781. +#endif /* __KERNEL__ */
  1782. +#endif /* __AUFS_BRANCH_H__ */
  1783. diff -Nur linux-4.0.5.orig/fs/aufs/conf.mk linux-4.0.5/fs/aufs/conf.mk
  1784. --- linux-4.0.5.orig/fs/aufs/conf.mk 1969-12-31 18:00:00.000000000 -0600
  1785. +++ linux-4.0.5/fs/aufs/conf.mk 2015-06-21 14:07:48.682432781 -0500
  1786. @@ -0,0 +1,38 @@
  1787. +
  1788. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  1789. +
  1790. +define AuConf
  1791. +ifdef ${1}
  1792. +AuConfStr += ${1}=${${1}}
  1793. +endif
  1794. +endef
  1795. +
  1796. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  1797. + SBILIST \
  1798. + HNOTIFY HFSNOTIFY \
  1799. + EXPORT INO_T_64 \
  1800. + XATTR \
  1801. + FHSM \
  1802. + RDU \
  1803. + SHWH \
  1804. + BR_RAMFS \
  1805. + BR_FUSE POLL \
  1806. + BR_HFSPLUS \
  1807. + BDEV_LOOP \
  1808. + DEBUG MAGIC_SYSRQ
  1809. +$(foreach i, ${AuConfAll}, \
  1810. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  1811. +
  1812. +AuConfName = ${obj}/conf.str
  1813. +${AuConfName}.tmp: FORCE
  1814. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  1815. +${AuConfName}: ${AuConfName}.tmp
  1816. + @diff -q $< $@ > /dev/null 2>&1 || { \
  1817. + echo ' GEN ' $@; \
  1818. + cp -p $< $@; \
  1819. + }
  1820. +FORCE:
  1821. +clean-files += ${AuConfName} ${AuConfName}.tmp
  1822. +${obj}/sysfs.o: ${AuConfName}
  1823. +
  1824. +-include ${srctree}/${src}/conf_priv.mk
  1825. diff -Nur linux-4.0.5.orig/fs/aufs/cpup.c linux-4.0.5/fs/aufs/cpup.c
  1826. --- linux-4.0.5.orig/fs/aufs/cpup.c 1969-12-31 18:00:00.000000000 -0600
  1827. +++ linux-4.0.5/fs/aufs/cpup.c 2015-06-21 14:07:48.682432781 -0500
  1828. @@ -0,0 +1,1308 @@
  1829. +/*
  1830. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  1831. + *
  1832. + * This program, aufs is free software; you can redistribute it and/or modify
  1833. + * it under the terms of the GNU General Public License as published by
  1834. + * the Free Software Foundation; either version 2 of the License, or
  1835. + * (at your option) any later version.
  1836. + *
  1837. + * This program is distributed in the hope that it will be useful,
  1838. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1839. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1840. + * GNU General Public License for more details.
  1841. + *
  1842. + * You should have received a copy of the GNU General Public License
  1843. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1844. + */
  1845. +
  1846. +/*
  1847. + * copy-up functions, see wbr_policy.c for copy-down
  1848. + */
  1849. +
  1850. +#include <linux/fs_stack.h>
  1851. +#include <linux/mm.h>
  1852. +#include "aufs.h"
  1853. +
  1854. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  1855. +{
  1856. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  1857. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  1858. +
  1859. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  1860. +
  1861. + dst->i_flags |= iflags & ~mask;
  1862. + if (au_test_fs_notime(dst->i_sb))
  1863. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  1864. +}
  1865. +
  1866. +void au_cpup_attr_timesizes(struct inode *inode)
  1867. +{
  1868. + struct inode *h_inode;
  1869. +
  1870. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  1871. + fsstack_copy_attr_times(inode, h_inode);
  1872. + fsstack_copy_inode_size(inode, h_inode);
  1873. +}
  1874. +
  1875. +void au_cpup_attr_nlink(struct inode *inode, int force)
  1876. +{
  1877. + struct inode *h_inode;
  1878. + struct super_block *sb;
  1879. + aufs_bindex_t bindex, bend;
  1880. +
  1881. + sb = inode->i_sb;
  1882. + bindex = au_ibstart(inode);
  1883. + h_inode = au_h_iptr(inode, bindex);
  1884. + if (!force
  1885. + && !S_ISDIR(h_inode->i_mode)
  1886. + && au_opt_test(au_mntflags(sb), PLINK)
  1887. + && au_plink_test(inode))
  1888. + return;
  1889. +
  1890. + /*
  1891. + * 0 can happen in revalidating.
  1892. + * h_inode->i_mutex may not be held here, but it is harmless since once
  1893. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  1894. + * case.
  1895. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  1896. + * the incorrect link count.
  1897. + */
  1898. + set_nlink(inode, h_inode->i_nlink);
  1899. +
  1900. + /*
  1901. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  1902. + * it may includes whplink directory.
  1903. + */
  1904. + if (S_ISDIR(h_inode->i_mode)) {
  1905. + bend = au_ibend(inode);
  1906. + for (bindex++; bindex <= bend; bindex++) {
  1907. + h_inode = au_h_iptr(inode, bindex);
  1908. + if (h_inode)
  1909. + au_add_nlink(inode, h_inode);
  1910. + }
  1911. + }
  1912. +}
  1913. +
  1914. +void au_cpup_attr_changeable(struct inode *inode)
  1915. +{
  1916. + struct inode *h_inode;
  1917. +
  1918. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  1919. + inode->i_mode = h_inode->i_mode;
  1920. + inode->i_uid = h_inode->i_uid;
  1921. + inode->i_gid = h_inode->i_gid;
  1922. + au_cpup_attr_timesizes(inode);
  1923. + au_cpup_attr_flags(inode, h_inode->i_flags);
  1924. +}
  1925. +
  1926. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  1927. +{
  1928. + struct au_iinfo *iinfo = au_ii(inode);
  1929. +
  1930. + IiMustWriteLock(inode);
  1931. +
  1932. + iinfo->ii_higen = h_inode->i_generation;
  1933. + iinfo->ii_hsb1 = h_inode->i_sb;
  1934. +}
  1935. +
  1936. +void au_cpup_attr_all(struct inode *inode, int force)
  1937. +{
  1938. + struct inode *h_inode;
  1939. +
  1940. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  1941. + au_cpup_attr_changeable(inode);
  1942. + if (inode->i_nlink > 0)
  1943. + au_cpup_attr_nlink(inode, force);
  1944. + inode->i_rdev = h_inode->i_rdev;
  1945. + inode->i_blkbits = h_inode->i_blkbits;
  1946. + au_cpup_igen(inode, h_inode);
  1947. +}
  1948. +
  1949. +/* ---------------------------------------------------------------------- */
  1950. +
  1951. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  1952. +
  1953. +/* keep the timestamps of the parent dir when cpup */
  1954. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  1955. + struct path *h_path)
  1956. +{
  1957. + struct inode *h_inode;
  1958. +
  1959. + dt->dt_dentry = dentry;
  1960. + dt->dt_h_path = *h_path;
  1961. + h_inode = h_path->dentry->d_inode;
  1962. + dt->dt_atime = h_inode->i_atime;
  1963. + dt->dt_mtime = h_inode->i_mtime;
  1964. + /* smp_mb(); */
  1965. +}
  1966. +
  1967. +void au_dtime_revert(struct au_dtime *dt)
  1968. +{
  1969. + struct iattr attr;
  1970. + int err;
  1971. +
  1972. + attr.ia_atime = dt->dt_atime;
  1973. + attr.ia_mtime = dt->dt_mtime;
  1974. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  1975. + | ATTR_ATIME | ATTR_ATIME_SET;
  1976. +
  1977. + /* no delegation since this is a directory */
  1978. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  1979. + if (unlikely(err))
  1980. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  1981. +}
  1982. +
  1983. +/* ---------------------------------------------------------------------- */
  1984. +
  1985. +/* internal use only */
  1986. +struct au_cpup_reg_attr {
  1987. + int valid;
  1988. + struct kstat st;
  1989. + unsigned int iflags; /* inode->i_flags */
  1990. +};
  1991. +
  1992. +static noinline_for_stack
  1993. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  1994. + struct au_cpup_reg_attr *h_src_attr)
  1995. +{
  1996. + int err, sbits, icex;
  1997. + unsigned int mnt_flags;
  1998. + unsigned char verbose;
  1999. + struct iattr ia;
  2000. + struct path h_path;
  2001. + struct inode *h_isrc, *h_idst;
  2002. + struct kstat *h_st;
  2003. + struct au_branch *br;
  2004. +
  2005. + h_path.dentry = au_h_dptr(dst, bindex);
  2006. + h_idst = h_path.dentry->d_inode;
  2007. + br = au_sbr(dst->d_sb, bindex);
  2008. + h_path.mnt = au_br_mnt(br);
  2009. + h_isrc = h_src->d_inode;
  2010. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  2011. + | ATTR_ATIME | ATTR_MTIME
  2012. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  2013. + if (h_src_attr && h_src_attr->valid) {
  2014. + h_st = &h_src_attr->st;
  2015. + ia.ia_uid = h_st->uid;
  2016. + ia.ia_gid = h_st->gid;
  2017. + ia.ia_atime = h_st->atime;
  2018. + ia.ia_mtime = h_st->mtime;
  2019. + if (h_idst->i_mode != h_st->mode
  2020. + && !S_ISLNK(h_idst->i_mode)) {
  2021. + ia.ia_valid |= ATTR_MODE;
  2022. + ia.ia_mode = h_st->mode;
  2023. + }
  2024. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  2025. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  2026. + } else {
  2027. + ia.ia_uid = h_isrc->i_uid;
  2028. + ia.ia_gid = h_isrc->i_gid;
  2029. + ia.ia_atime = h_isrc->i_atime;
  2030. + ia.ia_mtime = h_isrc->i_mtime;
  2031. + if (h_idst->i_mode != h_isrc->i_mode
  2032. + && !S_ISLNK(h_idst->i_mode)) {
  2033. + ia.ia_valid |= ATTR_MODE;
  2034. + ia.ia_mode = h_isrc->i_mode;
  2035. + }
  2036. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  2037. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  2038. + }
  2039. + /* no delegation since it is just created */
  2040. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  2041. +
  2042. + /* is this nfs only? */
  2043. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  2044. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  2045. + ia.ia_mode = h_isrc->i_mode;
  2046. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  2047. + }
  2048. +
  2049. + icex = br->br_perm & AuBrAttr_ICEX;
  2050. + if (!err) {
  2051. + mnt_flags = au_mntflags(dst->d_sb);
  2052. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2053. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  2054. + }
  2055. +
  2056. + return err;
  2057. +}
  2058. +
  2059. +/* ---------------------------------------------------------------------- */
  2060. +
  2061. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  2062. + char *buf, unsigned long blksize)
  2063. +{
  2064. + int err;
  2065. + size_t sz, rbytes, wbytes;
  2066. + unsigned char all_zero;
  2067. + char *p, *zp;
  2068. + struct mutex *h_mtx;
  2069. + /* reduce stack usage */
  2070. + struct iattr *ia;
  2071. +
  2072. + zp = page_address(ZERO_PAGE(0));
  2073. + if (unlikely(!zp))
  2074. + return -ENOMEM; /* possible? */
  2075. +
  2076. + err = 0;
  2077. + all_zero = 0;
  2078. + while (len) {
  2079. + AuDbg("len %lld\n", len);
  2080. + sz = blksize;
  2081. + if (len < blksize)
  2082. + sz = len;
  2083. +
  2084. + rbytes = 0;
  2085. + /* todo: signal_pending? */
  2086. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  2087. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  2088. + err = rbytes;
  2089. + }
  2090. + if (unlikely(err < 0))
  2091. + break;
  2092. +
  2093. + all_zero = 0;
  2094. + if (len >= rbytes && rbytes == blksize)
  2095. + all_zero = !memcmp(buf, zp, rbytes);
  2096. + if (!all_zero) {
  2097. + wbytes = rbytes;
  2098. + p = buf;
  2099. + while (wbytes) {
  2100. + size_t b;
  2101. +
  2102. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  2103. + err = b;
  2104. + /* todo: signal_pending? */
  2105. + if (unlikely(err == -EAGAIN || err == -EINTR))
  2106. + continue;
  2107. + if (unlikely(err < 0))
  2108. + break;
  2109. + wbytes -= b;
  2110. + p += b;
  2111. + }
  2112. + if (unlikely(err < 0))
  2113. + break;
  2114. + } else {
  2115. + loff_t res;
  2116. +
  2117. + AuLabel(hole);
  2118. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  2119. + err = res;
  2120. + if (unlikely(res < 0))
  2121. + break;
  2122. + }
  2123. + len -= rbytes;
  2124. + err = 0;
  2125. + }
  2126. +
  2127. + /* the last block may be a hole */
  2128. + if (!err && all_zero) {
  2129. + AuLabel(last hole);
  2130. +
  2131. + err = 1;
  2132. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  2133. + /* nfs requires this step to make last hole */
  2134. + /* is this only nfs? */
  2135. + do {
  2136. + /* todo: signal_pending? */
  2137. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  2138. + } while (err == -EAGAIN || err == -EINTR);
  2139. + if (err == 1)
  2140. + dst->f_pos--;
  2141. + }
  2142. +
  2143. + if (err == 1) {
  2144. + ia = (void *)buf;
  2145. + ia->ia_size = dst->f_pos;
  2146. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  2147. + ia->ia_file = dst;
  2148. + h_mtx = &file_inode(dst)->i_mutex;
  2149. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  2150. + /* no delegation since it is just created */
  2151. + err = vfsub_notify_change(&dst->f_path, ia,
  2152. + /*delegated*/NULL);
  2153. + mutex_unlock(h_mtx);
  2154. + }
  2155. + }
  2156. +
  2157. + return err;
  2158. +}
  2159. +
  2160. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  2161. +{
  2162. + int err;
  2163. + unsigned long blksize;
  2164. + unsigned char do_kfree;
  2165. + char *buf;
  2166. +
  2167. + err = -ENOMEM;
  2168. + blksize = dst->f_path.dentry->d_sb->s_blocksize;
  2169. + if (!blksize || PAGE_SIZE < blksize)
  2170. + blksize = PAGE_SIZE;
  2171. + AuDbg("blksize %lu\n", blksize);
  2172. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  2173. + if (do_kfree)
  2174. + buf = kmalloc(blksize, GFP_NOFS);
  2175. + else
  2176. + buf = (void *)__get_free_page(GFP_NOFS);
  2177. + if (unlikely(!buf))
  2178. + goto out;
  2179. +
  2180. + if (len > (1 << 22))
  2181. + AuDbg("copying a large file %lld\n", (long long)len);
  2182. +
  2183. + src->f_pos = 0;
  2184. + dst->f_pos = 0;
  2185. + err = au_do_copy_file(dst, src, len, buf, blksize);
  2186. + if (do_kfree)
  2187. + kfree(buf);
  2188. + else
  2189. + free_page((unsigned long)buf);
  2190. +
  2191. +out:
  2192. + return err;
  2193. +}
  2194. +
  2195. +/*
  2196. + * to support a sparse file which is opened with O_APPEND,
  2197. + * we need to close the file.
  2198. + */
  2199. +static int au_cp_regular(struct au_cp_generic *cpg)
  2200. +{
  2201. + int err, i;
  2202. + enum { SRC, DST };
  2203. + struct {
  2204. + aufs_bindex_t bindex;
  2205. + unsigned int flags;
  2206. + struct dentry *dentry;
  2207. + int force_wr;
  2208. + struct file *file;
  2209. + void *label;
  2210. + } *f, file[] = {
  2211. + {
  2212. + .bindex = cpg->bsrc,
  2213. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  2214. + .label = &&out
  2215. + },
  2216. + {
  2217. + .bindex = cpg->bdst,
  2218. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  2219. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  2220. + .label = &&out_src
  2221. + }
  2222. + };
  2223. + struct super_block *sb;
  2224. +
  2225. + /* bsrc branch can be ro/rw. */
  2226. + sb = cpg->dentry->d_sb;
  2227. + f = file;
  2228. + for (i = 0; i < 2; i++, f++) {
  2229. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  2230. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  2231. + /*file*/NULL, f->force_wr);
  2232. + err = PTR_ERR(f->file);
  2233. + if (IS_ERR(f->file))
  2234. + goto *f->label;
  2235. + }
  2236. +
  2237. + /* try stopping to update while we copyup */
  2238. + IMustLock(file[SRC].dentry->d_inode);
  2239. + err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
  2240. +
  2241. + fput(file[DST].file);
  2242. + au_sbr_put(sb, file[DST].bindex);
  2243. +
  2244. +out_src:
  2245. + fput(file[SRC].file);
  2246. + au_sbr_put(sb, file[SRC].bindex);
  2247. +out:
  2248. + return err;
  2249. +}
  2250. +
  2251. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  2252. + struct au_cpup_reg_attr *h_src_attr)
  2253. +{
  2254. + int err, rerr;
  2255. + loff_t l;
  2256. + struct path h_path;
  2257. + struct inode *h_src_inode, *h_dst_inode;
  2258. +
  2259. + err = 0;
  2260. + h_src_inode = au_h_iptr(cpg->dentry->d_inode, cpg->bsrc);
  2261. + l = i_size_read(h_src_inode);
  2262. + if (cpg->len == -1 || l < cpg->len)
  2263. + cpg->len = l;
  2264. + if (cpg->len) {
  2265. + /* try stopping to update while we are referencing */
  2266. + mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
  2267. + au_pin_hdir_unlock(cpg->pin);
  2268. +
  2269. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  2270. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  2271. + h_src_attr->iflags = h_src_inode->i_flags;
  2272. + err = vfs_getattr(&h_path, &h_src_attr->st);
  2273. + if (unlikely(err)) {
  2274. + mutex_unlock(&h_src_inode->i_mutex);
  2275. + goto out;
  2276. + }
  2277. + h_src_attr->valid = 1;
  2278. + err = au_cp_regular(cpg);
  2279. + mutex_unlock(&h_src_inode->i_mutex);
  2280. + rerr = au_pin_hdir_relock(cpg->pin);
  2281. + if (!err && rerr)
  2282. + err = rerr;
  2283. + }
  2284. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  2285. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  2286. + h_dst_inode = h_path.dentry->d_inode;
  2287. + spin_lock(&h_dst_inode->i_lock);
  2288. + h_dst_inode->i_state |= I_LINKABLE;
  2289. + spin_unlock(&h_dst_inode->i_lock);
  2290. + }
  2291. +
  2292. +out:
  2293. + return err;
  2294. +}
  2295. +
  2296. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  2297. + struct inode *h_dir)
  2298. +{
  2299. + int err, symlen;
  2300. + mm_segment_t old_fs;
  2301. + union {
  2302. + char *k;
  2303. + char __user *u;
  2304. + } sym;
  2305. +
  2306. + err = -ENOSYS;
  2307. + if (unlikely(!h_src->d_inode->i_op->readlink))
  2308. + goto out;
  2309. +
  2310. + err = -ENOMEM;
  2311. + sym.k = (void *)__get_free_page(GFP_NOFS);
  2312. + if (unlikely(!sym.k))
  2313. + goto out;
  2314. +
  2315. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  2316. + old_fs = get_fs();
  2317. + set_fs(KERNEL_DS);
  2318. + symlen = h_src->d_inode->i_op->readlink(h_src, sym.u, PATH_MAX);
  2319. + err = symlen;
  2320. + set_fs(old_fs);
  2321. +
  2322. + if (symlen > 0) {
  2323. + sym.k[symlen] = 0;
  2324. + err = vfsub_symlink(h_dir, h_path, sym.k);
  2325. + }
  2326. + free_page((unsigned long)sym.k);
  2327. +
  2328. +out:
  2329. + return err;
  2330. +}
  2331. +
  2332. +static noinline_for_stack
  2333. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  2334. + struct au_cpup_reg_attr *h_src_attr)
  2335. +{
  2336. + int err;
  2337. + umode_t mode;
  2338. + unsigned int mnt_flags;
  2339. + unsigned char isdir, isreg, force;
  2340. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  2341. + struct au_dtime dt;
  2342. + struct path h_path;
  2343. + struct dentry *h_src, *h_dst, *h_parent;
  2344. + struct inode *h_inode, *h_dir;
  2345. + struct super_block *sb;
  2346. +
  2347. + /* bsrc branch can be ro/rw. */
  2348. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  2349. + h_inode = h_src->d_inode;
  2350. + AuDebugOn(h_inode != au_h_iptr(cpg->dentry->d_inode, cpg->bsrc));
  2351. +
  2352. + /* try stopping to be referenced while we are creating */
  2353. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  2354. + if (au_ftest_cpup(cpg->flags, RENAME))
  2355. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  2356. + AUFS_WH_PFX_LEN));
  2357. + h_parent = h_dst->d_parent; /* dir inode is locked */
  2358. + h_dir = h_parent->d_inode;
  2359. + IMustLock(h_dir);
  2360. + AuDebugOn(h_parent != h_dst->d_parent);
  2361. +
  2362. + sb = cpg->dentry->d_sb;
  2363. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  2364. + if (do_dt) {
  2365. + h_path.dentry = h_parent;
  2366. + au_dtime_store(&dt, dst_parent, &h_path);
  2367. + }
  2368. + h_path.dentry = h_dst;
  2369. +
  2370. + isreg = 0;
  2371. + isdir = 0;
  2372. + mode = h_inode->i_mode;
  2373. + switch (mode & S_IFMT) {
  2374. + case S_IFREG:
  2375. + isreg = 1;
  2376. + err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
  2377. + /*want_excl*/true);
  2378. + if (!err)
  2379. + err = au_do_cpup_regular(cpg, h_src_attr);
  2380. + break;
  2381. + case S_IFDIR:
  2382. + isdir = 1;
  2383. + err = vfsub_mkdir(h_dir, &h_path, mode);
  2384. + if (!err) {
  2385. + /*
  2386. + * strange behaviour from the users view,
  2387. + * particularry setattr case
  2388. + */
  2389. + if (au_ibstart(dst_parent->d_inode) == cpg->bdst)
  2390. + au_cpup_attr_nlink(dst_parent->d_inode,
  2391. + /*force*/1);
  2392. + au_cpup_attr_nlink(cpg->dentry->d_inode, /*force*/1);
  2393. + }
  2394. + break;
  2395. + case S_IFLNK:
  2396. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  2397. + break;
  2398. + case S_IFCHR:
  2399. + case S_IFBLK:
  2400. + AuDebugOn(!capable(CAP_MKNOD));
  2401. + /*FALLTHROUGH*/
  2402. + case S_IFIFO:
  2403. + case S_IFSOCK:
  2404. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  2405. + break;
  2406. + default:
  2407. + AuIOErr("Unknown inode type 0%o\n", mode);
  2408. + err = -EIO;
  2409. + }
  2410. +
  2411. + mnt_flags = au_mntflags(sb);
  2412. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  2413. + && !isdir
  2414. + && au_opt_test(mnt_flags, XINO)
  2415. + && (h_inode->i_nlink == 1
  2416. + || (h_inode->i_state & I_LINKABLE))
  2417. + /* todo: unnecessary? */
  2418. + /* && cpg->dentry->d_inode->i_nlink == 1 */
  2419. + && cpg->bdst < cpg->bsrc
  2420. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  2421. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  2422. + /* ignore this error */
  2423. +
  2424. + if (!err) {
  2425. + force = 0;
  2426. + if (isreg) {
  2427. + force = !!cpg->len;
  2428. + if (cpg->len == -1)
  2429. + force = !!i_size_read(h_inode);
  2430. + }
  2431. + au_fhsm_wrote(sb, cpg->bdst, force);
  2432. + }
  2433. +
  2434. + if (do_dt)
  2435. + au_dtime_revert(&dt);
  2436. + return err;
  2437. +}
  2438. +
  2439. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  2440. +{
  2441. + int err;
  2442. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  2443. + struct inode *h_dir;
  2444. + aufs_bindex_t bdst;
  2445. +
  2446. + dentry = cpg->dentry;
  2447. + bdst = cpg->bdst;
  2448. + h_dentry = au_h_dptr(dentry, bdst);
  2449. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  2450. + dget(h_dentry);
  2451. + au_set_h_dptr(dentry, bdst, NULL);
  2452. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  2453. + if (!err)
  2454. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  2455. + au_set_h_dptr(dentry, bdst, h_dentry);
  2456. + } else {
  2457. + err = 0;
  2458. + parent = dget_parent(dentry);
  2459. + h_parent = au_h_dptr(parent, bdst);
  2460. + dput(parent);
  2461. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  2462. + if (IS_ERR(h_path->dentry))
  2463. + err = PTR_ERR(h_path->dentry);
  2464. + }
  2465. + if (unlikely(err))
  2466. + goto out;
  2467. +
  2468. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  2469. + h_dir = h_parent->d_inode;
  2470. + IMustLock(h_dir);
  2471. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  2472. + /* no delegation since it is just created */
  2473. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
  2474. + dput(h_path->dentry);
  2475. +
  2476. +out:
  2477. + return err;
  2478. +}
  2479. +
  2480. +/*
  2481. + * copyup the @dentry from @bsrc to @bdst.
  2482. + * the caller must set the both of lower dentries.
  2483. + * @len is for truncating when it is -1 copyup the entire file.
  2484. + * in link/rename cases, @dst_parent may be different from the real one.
  2485. + * basic->bsrc can be larger than basic->bdst.
  2486. + */
  2487. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  2488. +{
  2489. + int err, rerr;
  2490. + aufs_bindex_t old_ibstart;
  2491. + unsigned char isdir, plink;
  2492. + struct dentry *h_src, *h_dst, *h_parent;
  2493. + struct inode *dst_inode, *h_dir, *inode, *delegated;
  2494. + struct super_block *sb;
  2495. + struct au_branch *br;
  2496. + /* to reuduce stack size */
  2497. + struct {
  2498. + struct au_dtime dt;
  2499. + struct path h_path;
  2500. + struct au_cpup_reg_attr h_src_attr;
  2501. + } *a;
  2502. +
  2503. + err = -ENOMEM;
  2504. + a = kmalloc(sizeof(*a), GFP_NOFS);
  2505. + if (unlikely(!a))
  2506. + goto out;
  2507. + a->h_src_attr.valid = 0;
  2508. +
  2509. + sb = cpg->dentry->d_sb;
  2510. + br = au_sbr(sb, cpg->bdst);
  2511. + a->h_path.mnt = au_br_mnt(br);
  2512. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  2513. + h_parent = h_dst->d_parent; /* dir inode is locked */
  2514. + h_dir = h_parent->d_inode;
  2515. + IMustLock(h_dir);
  2516. +
  2517. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  2518. + inode = cpg->dentry->d_inode;
  2519. +
  2520. + if (!dst_parent)
  2521. + dst_parent = dget_parent(cpg->dentry);
  2522. + else
  2523. + dget(dst_parent);
  2524. +
  2525. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  2526. + dst_inode = au_h_iptr(inode, cpg->bdst);
  2527. + if (dst_inode) {
  2528. + if (unlikely(!plink)) {
  2529. + err = -EIO;
  2530. + AuIOErr("hi%lu(i%lu) exists on b%d "
  2531. + "but plink is disabled\n",
  2532. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  2533. + goto out_parent;
  2534. + }
  2535. +
  2536. + if (dst_inode->i_nlink) {
  2537. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  2538. +
  2539. + h_src = au_plink_lkup(inode, cpg->bdst);
  2540. + err = PTR_ERR(h_src);
  2541. + if (IS_ERR(h_src))
  2542. + goto out_parent;
  2543. + if (unlikely(!h_src->d_inode)) {
  2544. + err = -EIO;
  2545. + AuIOErr("i%lu exists on a upper branch "
  2546. + "but not pseudo-linked\n",
  2547. + inode->i_ino);
  2548. + dput(h_src);
  2549. + goto out_parent;
  2550. + }
  2551. +
  2552. + if (do_dt) {
  2553. + a->h_path.dentry = h_parent;
  2554. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  2555. + }
  2556. +
  2557. + a->h_path.dentry = h_dst;
  2558. + delegated = NULL;
  2559. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  2560. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  2561. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  2562. + if (do_dt)
  2563. + au_dtime_revert(&a->dt);
  2564. + if (unlikely(err == -EWOULDBLOCK)) {
  2565. + pr_warn("cannot retry for NFSv4 delegation"
  2566. + " for an internal link\n");
  2567. + iput(delegated);
  2568. + }
  2569. + dput(h_src);
  2570. + goto out_parent;
  2571. + } else
  2572. + /* todo: cpup_wh_file? */
  2573. + /* udba work */
  2574. + au_update_ibrange(inode, /*do_put_zero*/1);
  2575. + }
  2576. +
  2577. + isdir = S_ISDIR(inode->i_mode);
  2578. + old_ibstart = au_ibstart(inode);
  2579. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  2580. + if (unlikely(err))
  2581. + goto out_rev;
  2582. + dst_inode = h_dst->d_inode;
  2583. + mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
  2584. + /* todo: necessary? */
  2585. + /* au_pin_hdir_unlock(cpg->pin); */
  2586. +
  2587. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  2588. + if (unlikely(err)) {
  2589. + /* todo: necessary? */
  2590. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  2591. + mutex_unlock(&dst_inode->i_mutex);
  2592. + goto out_rev;
  2593. + }
  2594. +
  2595. + if (cpg->bdst < old_ibstart) {
  2596. + if (S_ISREG(inode->i_mode)) {
  2597. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  2598. + if (unlikely(err)) {
  2599. + /* ignore an error */
  2600. + /* au_pin_hdir_relock(cpg->pin); */
  2601. + mutex_unlock(&dst_inode->i_mutex);
  2602. + goto out_rev;
  2603. + }
  2604. + }
  2605. + au_set_ibstart(inode, cpg->bdst);
  2606. + } else
  2607. + au_set_ibend(inode, cpg->bdst);
  2608. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  2609. + au_hi_flags(inode, isdir));
  2610. +
  2611. + /* todo: necessary? */
  2612. + /* err = au_pin_hdir_relock(cpg->pin); */
  2613. + mutex_unlock(&dst_inode->i_mutex);
  2614. + if (unlikely(err))
  2615. + goto out_rev;
  2616. +
  2617. + if (!isdir
  2618. + && (h_src->d_inode->i_nlink > 1
  2619. + || h_src->d_inode->i_state & I_LINKABLE)
  2620. + && plink)
  2621. + au_plink_append(inode, cpg->bdst, h_dst);
  2622. +
  2623. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  2624. + a->h_path.dentry = h_dst;
  2625. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  2626. + }
  2627. + if (!err)
  2628. + goto out_parent; /* success */
  2629. +
  2630. + /* revert */
  2631. +out_rev:
  2632. + a->h_path.dentry = h_parent;
  2633. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  2634. + a->h_path.dentry = h_dst;
  2635. + rerr = 0;
  2636. + if (h_dst->d_inode) {
  2637. + if (!isdir) {
  2638. + /* no delegation since it is just created */
  2639. + rerr = vfsub_unlink(h_dir, &a->h_path,
  2640. + /*delegated*/NULL, /*force*/0);
  2641. + } else
  2642. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  2643. + }
  2644. + au_dtime_revert(&a->dt);
  2645. + if (rerr) {
  2646. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  2647. + err = -EIO;
  2648. + }
  2649. +out_parent:
  2650. + dput(dst_parent);
  2651. + kfree(a);
  2652. +out:
  2653. + return err;
  2654. +}
  2655. +
  2656. +#if 0 /* reserved */
  2657. +struct au_cpup_single_args {
  2658. + int *errp;
  2659. + struct au_cp_generic *cpg;
  2660. + struct dentry *dst_parent;
  2661. +};
  2662. +
  2663. +static void au_call_cpup_single(void *args)
  2664. +{
  2665. + struct au_cpup_single_args *a = args;
  2666. +
  2667. + au_pin_hdir_acquire_nest(a->cpg->pin);
  2668. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  2669. + au_pin_hdir_release(a->cpg->pin);
  2670. +}
  2671. +#endif
  2672. +
  2673. +/*
  2674. + * prevent SIGXFSZ in copy-up.
  2675. + * testing CAP_MKNOD is for generic fs,
  2676. + * but CAP_FSETID is for xfs only, currently.
  2677. + */
  2678. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  2679. +{
  2680. + int do_sio;
  2681. + struct super_block *sb;
  2682. + struct inode *h_dir;
  2683. +
  2684. + do_sio = 0;
  2685. + sb = au_pinned_parent(pin)->d_sb;
  2686. + if (!au_wkq_test()
  2687. + && (!au_sbi(sb)->si_plink_maint_pid
  2688. + || au_plink_maint(sb, AuLock_NOPLM))) {
  2689. + switch (mode & S_IFMT) {
  2690. + case S_IFREG:
  2691. + /* no condition about RLIMIT_FSIZE and the file size */
  2692. + do_sio = 1;
  2693. + break;
  2694. + case S_IFCHR:
  2695. + case S_IFBLK:
  2696. + do_sio = !capable(CAP_MKNOD);
  2697. + break;
  2698. + }
  2699. + if (!do_sio)
  2700. + do_sio = ((mode & (S_ISUID | S_ISGID))
  2701. + && !capable(CAP_FSETID));
  2702. + /* this workaround may be removed in the future */
  2703. + if (!do_sio) {
  2704. + h_dir = au_pinned_h_dir(pin);
  2705. + do_sio = h_dir->i_mode & S_ISVTX;
  2706. + }
  2707. + }
  2708. +
  2709. + return do_sio;
  2710. +}
  2711. +
  2712. +#if 0 /* reserved */
  2713. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  2714. +{
  2715. + int err, wkq_err;
  2716. + struct dentry *h_dentry;
  2717. +
  2718. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  2719. + if (!au_cpup_sio_test(pin, h_dentry->d_inode->i_mode))
  2720. + err = au_cpup_single(cpg, dst_parent);
  2721. + else {
  2722. + struct au_cpup_single_args args = {
  2723. + .errp = &err,
  2724. + .cpg = cpg,
  2725. + .dst_parent = dst_parent
  2726. + };
  2727. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  2728. + if (unlikely(wkq_err))
  2729. + err = wkq_err;
  2730. + }
  2731. +
  2732. + return err;
  2733. +}
  2734. +#endif
  2735. +
  2736. +/*
  2737. + * copyup the @dentry from the first active lower branch to @bdst,
  2738. + * using au_cpup_single().
  2739. + */
  2740. +static int au_cpup_simple(struct au_cp_generic *cpg)
  2741. +{
  2742. + int err;
  2743. + unsigned int flags_orig;
  2744. + struct dentry *dentry;
  2745. +
  2746. + AuDebugOn(cpg->bsrc < 0);
  2747. +
  2748. + dentry = cpg->dentry;
  2749. + DiMustWriteLock(dentry);
  2750. +
  2751. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  2752. + if (!err) {
  2753. + flags_orig = cpg->flags;
  2754. + au_fset_cpup(cpg->flags, RENAME);
  2755. + err = au_cpup_single(cpg, NULL);
  2756. + cpg->flags = flags_orig;
  2757. + if (!err)
  2758. + return 0; /* success */
  2759. +
  2760. + /* revert */
  2761. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  2762. + au_set_dbstart(dentry, cpg->bsrc);
  2763. + }
  2764. +
  2765. + return err;
  2766. +}
  2767. +
  2768. +struct au_cpup_simple_args {
  2769. + int *errp;
  2770. + struct au_cp_generic *cpg;
  2771. +};
  2772. +
  2773. +static void au_call_cpup_simple(void *args)
  2774. +{
  2775. + struct au_cpup_simple_args *a = args;
  2776. +
  2777. + au_pin_hdir_acquire_nest(a->cpg->pin);
  2778. + *a->errp = au_cpup_simple(a->cpg);
  2779. + au_pin_hdir_release(a->cpg->pin);
  2780. +}
  2781. +
  2782. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  2783. +{
  2784. + int err, wkq_err;
  2785. + struct dentry *dentry, *parent;
  2786. + struct file *h_file;
  2787. + struct inode *h_dir;
  2788. +
  2789. + dentry = cpg->dentry;
  2790. + h_file = NULL;
  2791. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  2792. + AuDebugOn(cpg->bsrc < 0);
  2793. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  2794. + err = PTR_ERR(h_file);
  2795. + if (IS_ERR(h_file))
  2796. + goto out;
  2797. + }
  2798. +
  2799. + parent = dget_parent(dentry);
  2800. + h_dir = au_h_iptr(parent->d_inode, cpg->bdst);
  2801. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  2802. + && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
  2803. + err = au_cpup_simple(cpg);
  2804. + else {
  2805. + struct au_cpup_simple_args args = {
  2806. + .errp = &err,
  2807. + .cpg = cpg
  2808. + };
  2809. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  2810. + if (unlikely(wkq_err))
  2811. + err = wkq_err;
  2812. + }
  2813. +
  2814. + dput(parent);
  2815. + if (h_file)
  2816. + au_h_open_post(dentry, cpg->bsrc, h_file);
  2817. +
  2818. +out:
  2819. + return err;
  2820. +}
  2821. +
  2822. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  2823. +{
  2824. + aufs_bindex_t bsrc, bend;
  2825. + struct dentry *dentry, *h_dentry;
  2826. +
  2827. + if (cpg->bsrc < 0) {
  2828. + dentry = cpg->dentry;
  2829. + bend = au_dbend(dentry);
  2830. + for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
  2831. + h_dentry = au_h_dptr(dentry, bsrc);
  2832. + if (h_dentry) {
  2833. + AuDebugOn(!h_dentry->d_inode);
  2834. + break;
  2835. + }
  2836. + }
  2837. + AuDebugOn(bsrc > bend);
  2838. + cpg->bsrc = bsrc;
  2839. + }
  2840. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  2841. + return au_do_sio_cpup_simple(cpg);
  2842. +}
  2843. +
  2844. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  2845. +{
  2846. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  2847. + return au_do_sio_cpup_simple(cpg);
  2848. +}
  2849. +
  2850. +/* ---------------------------------------------------------------------- */
  2851. +
  2852. +/*
  2853. + * copyup the deleted file for writing.
  2854. + */
  2855. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  2856. + struct file *file)
  2857. +{
  2858. + int err;
  2859. + unsigned int flags_orig;
  2860. + aufs_bindex_t bsrc_orig;
  2861. + struct dentry *h_d_dst, *h_d_start;
  2862. + struct au_dinfo *dinfo;
  2863. + struct au_hdentry *hdp;
  2864. +
  2865. + dinfo = au_di(cpg->dentry);
  2866. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2867. +
  2868. + bsrc_orig = cpg->bsrc;
  2869. + cpg->bsrc = dinfo->di_bstart;
  2870. + hdp = dinfo->di_hdentry;
  2871. + h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
  2872. + dinfo->di_bstart = cpg->bdst;
  2873. + hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
  2874. + h_d_start = NULL;
  2875. + if (file) {
  2876. + h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
  2877. + hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
  2878. + }
  2879. + flags_orig = cpg->flags;
  2880. + cpg->flags = !AuCpup_DTIME;
  2881. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  2882. + cpg->flags = flags_orig;
  2883. + if (file) {
  2884. + if (!err)
  2885. + err = au_reopen_nondir(file);
  2886. + hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
  2887. + }
  2888. + hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
  2889. + dinfo->di_bstart = cpg->bsrc;
  2890. + cpg->bsrc = bsrc_orig;
  2891. +
  2892. + return err;
  2893. +}
  2894. +
  2895. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  2896. +{
  2897. + int err;
  2898. + aufs_bindex_t bdst;
  2899. + struct au_dtime dt;
  2900. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  2901. + struct au_branch *br;
  2902. + struct path h_path;
  2903. +
  2904. + dentry = cpg->dentry;
  2905. + bdst = cpg->bdst;
  2906. + br = au_sbr(dentry->d_sb, bdst);
  2907. + parent = dget_parent(dentry);
  2908. + h_parent = au_h_dptr(parent, bdst);
  2909. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  2910. + err = PTR_ERR(wh_dentry);
  2911. + if (IS_ERR(wh_dentry))
  2912. + goto out;
  2913. +
  2914. + h_path.dentry = h_parent;
  2915. + h_path.mnt = au_br_mnt(br);
  2916. + au_dtime_store(&dt, parent, &h_path);
  2917. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  2918. + if (unlikely(err))
  2919. + goto out_wh;
  2920. +
  2921. + dget(wh_dentry);
  2922. + h_path.dentry = wh_dentry;
  2923. + if (!d_is_dir(wh_dentry)) {
  2924. + /* no delegation since it is just created */
  2925. + err = vfsub_unlink(h_parent->d_inode, &h_path,
  2926. + /*delegated*/NULL, /*force*/0);
  2927. + } else
  2928. + err = vfsub_rmdir(h_parent->d_inode, &h_path);
  2929. + if (unlikely(err)) {
  2930. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  2931. + wh_dentry, err);
  2932. + err = -EIO;
  2933. + }
  2934. + au_dtime_revert(&dt);
  2935. + au_set_hi_wh(dentry->d_inode, bdst, wh_dentry);
  2936. +
  2937. +out_wh:
  2938. + dput(wh_dentry);
  2939. +out:
  2940. + dput(parent);
  2941. + return err;
  2942. +}
  2943. +
  2944. +struct au_cpup_wh_args {
  2945. + int *errp;
  2946. + struct au_cp_generic *cpg;
  2947. + struct file *file;
  2948. +};
  2949. +
  2950. +static void au_call_cpup_wh(void *args)
  2951. +{
  2952. + struct au_cpup_wh_args *a = args;
  2953. +
  2954. + au_pin_hdir_acquire_nest(a->cpg->pin);
  2955. + *a->errp = au_cpup_wh(a->cpg, a->file);
  2956. + au_pin_hdir_release(a->cpg->pin);
  2957. +}
  2958. +
  2959. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  2960. +{
  2961. + int err, wkq_err;
  2962. + aufs_bindex_t bdst;
  2963. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  2964. + struct inode *dir, *h_dir, *h_tmpdir;
  2965. + struct au_wbr *wbr;
  2966. + struct au_pin wh_pin, *pin_orig;
  2967. +
  2968. + dentry = cpg->dentry;
  2969. + bdst = cpg->bdst;
  2970. + parent = dget_parent(dentry);
  2971. + dir = parent->d_inode;
  2972. + h_orph = NULL;
  2973. + h_parent = NULL;
  2974. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  2975. + h_tmpdir = h_dir;
  2976. + pin_orig = NULL;
  2977. + if (!h_dir->i_nlink) {
  2978. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  2979. + h_orph = wbr->wbr_orph;
  2980. +
  2981. + h_parent = dget(au_h_dptr(parent, bdst));
  2982. + au_set_h_dptr(parent, bdst, dget(h_orph));
  2983. + h_tmpdir = h_orph->d_inode;
  2984. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  2985. +
  2986. + mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
  2987. + /* todo: au_h_open_pre()? */
  2988. +
  2989. + pin_orig = cpg->pin;
  2990. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  2991. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  2992. + cpg->pin = &wh_pin;
  2993. + }
  2994. +
  2995. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  2996. + && !au_cpup_sio_test(cpg->pin, dentry->d_inode->i_mode))
  2997. + err = au_cpup_wh(cpg, file);
  2998. + else {
  2999. + struct au_cpup_wh_args args = {
  3000. + .errp = &err,
  3001. + .cpg = cpg,
  3002. + .file = file
  3003. + };
  3004. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  3005. + if (unlikely(wkq_err))
  3006. + err = wkq_err;
  3007. + }
  3008. +
  3009. + if (h_orph) {
  3010. + mutex_unlock(&h_tmpdir->i_mutex);
  3011. + /* todo: au_h_open_post()? */
  3012. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  3013. + au_set_h_dptr(parent, bdst, h_parent);
  3014. + AuDebugOn(!pin_orig);
  3015. + cpg->pin = pin_orig;
  3016. + }
  3017. + iput(h_dir);
  3018. + dput(parent);
  3019. +
  3020. + return err;
  3021. +}
  3022. +
  3023. +/* ---------------------------------------------------------------------- */
  3024. +
  3025. +/*
  3026. + * generic routine for both of copy-up and copy-down.
  3027. + */
  3028. +/* cf. revalidate function in file.c */
  3029. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  3030. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  3031. + struct au_pin *pin,
  3032. + struct dentry *h_parent, void *arg),
  3033. + void *arg)
  3034. +{
  3035. + int err;
  3036. + struct au_pin pin;
  3037. + struct dentry *d, *parent, *h_parent, *real_parent;
  3038. +
  3039. + err = 0;
  3040. + parent = dget_parent(dentry);
  3041. + if (IS_ROOT(parent))
  3042. + goto out;
  3043. +
  3044. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  3045. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  3046. +
  3047. + /* do not use au_dpage */
  3048. + real_parent = parent;
  3049. + while (1) {
  3050. + dput(parent);
  3051. + parent = dget_parent(dentry);
  3052. + h_parent = au_h_dptr(parent, bdst);
  3053. + if (h_parent)
  3054. + goto out; /* success */
  3055. +
  3056. + /* find top dir which is necessary to cpup */
  3057. + do {
  3058. + d = parent;
  3059. + dput(parent);
  3060. + parent = dget_parent(d);
  3061. + di_read_lock_parent3(parent, !AuLock_IR);
  3062. + h_parent = au_h_dptr(parent, bdst);
  3063. + di_read_unlock(parent, !AuLock_IR);
  3064. + } while (!h_parent);
  3065. +
  3066. + if (d != real_parent)
  3067. + di_write_lock_child3(d);
  3068. +
  3069. + /* somebody else might create while we were sleeping */
  3070. + if (!au_h_dptr(d, bdst) || !au_h_dptr(d, bdst)->d_inode) {
  3071. + if (au_h_dptr(d, bdst))
  3072. + au_update_dbstart(d);
  3073. +
  3074. + au_pin_set_dentry(&pin, d);
  3075. + err = au_do_pin(&pin);
  3076. + if (!err) {
  3077. + err = cp(d, bdst, &pin, h_parent, arg);
  3078. + au_unpin(&pin);
  3079. + }
  3080. + }
  3081. +
  3082. + if (d != real_parent)
  3083. + di_write_unlock(d);
  3084. + if (unlikely(err))
  3085. + break;
  3086. + }
  3087. +
  3088. +out:
  3089. + dput(parent);
  3090. + return err;
  3091. +}
  3092. +
  3093. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  3094. + struct au_pin *pin,
  3095. + struct dentry *h_parent __maybe_unused,
  3096. + void *arg __maybe_unused)
  3097. +{
  3098. + struct au_cp_generic cpg = {
  3099. + .dentry = dentry,
  3100. + .bdst = bdst,
  3101. + .bsrc = -1,
  3102. + .len = 0,
  3103. + .pin = pin,
  3104. + .flags = AuCpup_DTIME
  3105. + };
  3106. + return au_sio_cpup_simple(&cpg);
  3107. +}
  3108. +
  3109. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  3110. +{
  3111. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  3112. +}
  3113. +
  3114. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  3115. +{
  3116. + int err;
  3117. + struct dentry *parent;
  3118. + struct inode *dir;
  3119. +
  3120. + parent = dget_parent(dentry);
  3121. + dir = parent->d_inode;
  3122. + err = 0;
  3123. + if (au_h_iptr(dir, bdst))
  3124. + goto out;
  3125. +
  3126. + di_read_unlock(parent, AuLock_IR);
  3127. + di_write_lock_parent(parent);
  3128. + /* someone else might change our inode while we were sleeping */
  3129. + if (!au_h_iptr(dir, bdst))
  3130. + err = au_cpup_dirs(dentry, bdst);
  3131. + di_downgrade_lock(parent, AuLock_IR);
  3132. +
  3133. +out:
  3134. + dput(parent);
  3135. + return err;
  3136. +}
  3137. diff -Nur linux-4.0.5.orig/fs/aufs/cpup.h linux-4.0.5/fs/aufs/cpup.h
  3138. --- linux-4.0.5.orig/fs/aufs/cpup.h 1969-12-31 18:00:00.000000000 -0600
  3139. +++ linux-4.0.5/fs/aufs/cpup.h 2015-06-21 14:07:48.682432781 -0500
  3140. @@ -0,0 +1,94 @@
  3141. +/*
  3142. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3143. + *
  3144. + * This program, aufs is free software; you can redistribute it and/or modify
  3145. + * it under the terms of the GNU General Public License as published by
  3146. + * the Free Software Foundation; either version 2 of the License, or
  3147. + * (at your option) any later version.
  3148. + *
  3149. + * This program is distributed in the hope that it will be useful,
  3150. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3151. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3152. + * GNU General Public License for more details.
  3153. + *
  3154. + * You should have received a copy of the GNU General Public License
  3155. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3156. + */
  3157. +
  3158. +/*
  3159. + * copy-up/down functions
  3160. + */
  3161. +
  3162. +#ifndef __AUFS_CPUP_H__
  3163. +#define __AUFS_CPUP_H__
  3164. +
  3165. +#ifdef __KERNEL__
  3166. +
  3167. +#include <linux/path.h>
  3168. +
  3169. +struct inode;
  3170. +struct file;
  3171. +struct au_pin;
  3172. +
  3173. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  3174. +void au_cpup_attr_timesizes(struct inode *inode);
  3175. +void au_cpup_attr_nlink(struct inode *inode, int force);
  3176. +void au_cpup_attr_changeable(struct inode *inode);
  3177. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  3178. +void au_cpup_attr_all(struct inode *inode, int force);
  3179. +
  3180. +/* ---------------------------------------------------------------------- */
  3181. +
  3182. +struct au_cp_generic {
  3183. + struct dentry *dentry;
  3184. + aufs_bindex_t bdst, bsrc;
  3185. + loff_t len;
  3186. + struct au_pin *pin;
  3187. + unsigned int flags;
  3188. +};
  3189. +
  3190. +/* cpup flags */
  3191. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  3192. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  3193. + for link(2) */
  3194. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  3195. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  3196. + cpup */
  3197. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  3198. + existing entry */
  3199. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  3200. + the branch is marked as RO */
  3201. +
  3202. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  3203. +#define au_fset_cpup(flags, name) \
  3204. + do { (flags) |= AuCpup_##name; } while (0)
  3205. +#define au_fclr_cpup(flags, name) \
  3206. + do { (flags) &= ~AuCpup_##name; } while (0)
  3207. +
  3208. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  3209. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  3210. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  3211. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  3212. +
  3213. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  3214. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  3215. + struct au_pin *pin,
  3216. + struct dentry *h_parent, void *arg),
  3217. + void *arg);
  3218. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  3219. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  3220. +
  3221. +/* ---------------------------------------------------------------------- */
  3222. +
  3223. +/* keep timestamps when copyup */
  3224. +struct au_dtime {
  3225. + struct dentry *dt_dentry;
  3226. + struct path dt_h_path;
  3227. + struct timespec dt_atime, dt_mtime;
  3228. +};
  3229. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3230. + struct path *h_path);
  3231. +void au_dtime_revert(struct au_dtime *dt);
  3232. +
  3233. +#endif /* __KERNEL__ */
  3234. +#endif /* __AUFS_CPUP_H__ */
  3235. diff -Nur linux-4.0.5.orig/fs/aufs/dbgaufs.c linux-4.0.5/fs/aufs/dbgaufs.c
  3236. --- linux-4.0.5.orig/fs/aufs/dbgaufs.c 1969-12-31 18:00:00.000000000 -0600
  3237. +++ linux-4.0.5/fs/aufs/dbgaufs.c 2015-06-21 14:07:48.682432781 -0500
  3238. @@ -0,0 +1,432 @@
  3239. +/*
  3240. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3241. + *
  3242. + * This program, aufs is free software; you can redistribute it and/or modify
  3243. + * it under the terms of the GNU General Public License as published by
  3244. + * the Free Software Foundation; either version 2 of the License, or
  3245. + * (at your option) any later version.
  3246. + *
  3247. + * This program is distributed in the hope that it will be useful,
  3248. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3249. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3250. + * GNU General Public License for more details.
  3251. + *
  3252. + * You should have received a copy of the GNU General Public License
  3253. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3254. + */
  3255. +
  3256. +/*
  3257. + * debugfs interface
  3258. + */
  3259. +
  3260. +#include <linux/debugfs.h>
  3261. +#include "aufs.h"
  3262. +
  3263. +#ifndef CONFIG_SYSFS
  3264. +#error DEBUG_FS depends upon SYSFS
  3265. +#endif
  3266. +
  3267. +static struct dentry *dbgaufs;
  3268. +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
  3269. +
  3270. +/* 20 is max digits length of ulong 64 */
  3271. +struct dbgaufs_arg {
  3272. + int n;
  3273. + char a[20 * 4];
  3274. +};
  3275. +
  3276. +/*
  3277. + * common function for all XINO files
  3278. + */
  3279. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  3280. + struct file *file)
  3281. +{
  3282. + kfree(file->private_data);
  3283. + return 0;
  3284. +}
  3285. +
  3286. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
  3287. +{
  3288. + int err;
  3289. + struct kstat st;
  3290. + struct dbgaufs_arg *p;
  3291. +
  3292. + err = -ENOMEM;
  3293. + p = kmalloc(sizeof(*p), GFP_NOFS);
  3294. + if (unlikely(!p))
  3295. + goto out;
  3296. +
  3297. + err = 0;
  3298. + p->n = 0;
  3299. + file->private_data = p;
  3300. + if (!xf)
  3301. + goto out;
  3302. +
  3303. + err = vfs_getattr(&xf->f_path, &st);
  3304. + if (!err) {
  3305. + if (do_fcnt)
  3306. + p->n = snprintf
  3307. + (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
  3308. + (long)file_count(xf), st.blocks, st.blksize,
  3309. + (long long)st.size);
  3310. + else
  3311. + p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
  3312. + st.blocks, st.blksize,
  3313. + (long long)st.size);
  3314. + AuDebugOn(p->n >= sizeof(p->a));
  3315. + } else {
  3316. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  3317. + err = 0;
  3318. + }
  3319. +
  3320. +out:
  3321. + return err;
  3322. +
  3323. +}
  3324. +
  3325. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  3326. + size_t count, loff_t *ppos)
  3327. +{
  3328. + struct dbgaufs_arg *p;
  3329. +
  3330. + p = file->private_data;
  3331. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  3332. +}
  3333. +
  3334. +/* ---------------------------------------------------------------------- */
  3335. +
  3336. +struct dbgaufs_plink_arg {
  3337. + int n;
  3338. + char a[];
  3339. +};
  3340. +
  3341. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  3342. + struct file *file)
  3343. +{
  3344. + free_page((unsigned long)file->private_data);
  3345. + return 0;
  3346. +}
  3347. +
  3348. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  3349. +{
  3350. + int err, i, limit;
  3351. + unsigned long n, sum;
  3352. + struct dbgaufs_plink_arg *p;
  3353. + struct au_sbinfo *sbinfo;
  3354. + struct super_block *sb;
  3355. + struct au_sphlhead *sphl;
  3356. +
  3357. + err = -ENOMEM;
  3358. + p = (void *)get_zeroed_page(GFP_NOFS);
  3359. + if (unlikely(!p))
  3360. + goto out;
  3361. +
  3362. + err = -EFBIG;
  3363. + sbinfo = inode->i_private;
  3364. + sb = sbinfo->si_sb;
  3365. + si_noflush_read_lock(sb);
  3366. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  3367. + limit = PAGE_SIZE - sizeof(p->n);
  3368. +
  3369. + /* the number of buckets */
  3370. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  3371. + p->n += n;
  3372. + limit -= n;
  3373. +
  3374. + sum = 0;
  3375. + for (i = 0, sphl = sbinfo->si_plink;
  3376. + i < AuPlink_NHASH;
  3377. + i++, sphl++) {
  3378. + n = au_sphl_count(sphl);
  3379. + sum += n;
  3380. +
  3381. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  3382. + p->n += n;
  3383. + limit -= n;
  3384. + if (unlikely(limit <= 0))
  3385. + goto out_free;
  3386. + }
  3387. + p->a[p->n - 1] = '\n';
  3388. +
  3389. + /* the sum of plinks */
  3390. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  3391. + p->n += n;
  3392. + limit -= n;
  3393. + if (unlikely(limit <= 0))
  3394. + goto out_free;
  3395. + } else {
  3396. +#define str "1\n0\n0\n"
  3397. + p->n = sizeof(str) - 1;
  3398. + strcpy(p->a, str);
  3399. +#undef str
  3400. + }
  3401. + si_read_unlock(sb);
  3402. +
  3403. + err = 0;
  3404. + file->private_data = p;
  3405. + goto out; /* success */
  3406. +
  3407. +out_free:
  3408. + free_page((unsigned long)p);
  3409. +out:
  3410. + return err;
  3411. +}
  3412. +
  3413. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  3414. + size_t count, loff_t *ppos)
  3415. +{
  3416. + struct dbgaufs_plink_arg *p;
  3417. +
  3418. + p = file->private_data;
  3419. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  3420. +}
  3421. +
  3422. +static const struct file_operations dbgaufs_plink_fop = {
  3423. + .owner = THIS_MODULE,
  3424. + .open = dbgaufs_plink_open,
  3425. + .release = dbgaufs_plink_release,
  3426. + .read = dbgaufs_plink_read
  3427. +};
  3428. +
  3429. +/* ---------------------------------------------------------------------- */
  3430. +
  3431. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  3432. +{
  3433. + int err;
  3434. + struct au_sbinfo *sbinfo;
  3435. + struct super_block *sb;
  3436. +
  3437. + sbinfo = inode->i_private;
  3438. + sb = sbinfo->si_sb;
  3439. + si_noflush_read_lock(sb);
  3440. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
  3441. + si_read_unlock(sb);
  3442. + return err;
  3443. +}
  3444. +
  3445. +static const struct file_operations dbgaufs_xib_fop = {
  3446. + .owner = THIS_MODULE,
  3447. + .open = dbgaufs_xib_open,
  3448. + .release = dbgaufs_xi_release,
  3449. + .read = dbgaufs_xi_read
  3450. +};
  3451. +
  3452. +/* ---------------------------------------------------------------------- */
  3453. +
  3454. +#define DbgaufsXi_PREFIX "xi"
  3455. +
  3456. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  3457. +{
  3458. + int err;
  3459. + long l;
  3460. + struct au_sbinfo *sbinfo;
  3461. + struct super_block *sb;
  3462. + struct file *xf;
  3463. + struct qstr *name;
  3464. +
  3465. + err = -ENOENT;
  3466. + xf = NULL;
  3467. + name = &file->f_path.dentry->d_name;
  3468. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  3469. + || memcmp(name->name, DbgaufsXi_PREFIX,
  3470. + sizeof(DbgaufsXi_PREFIX) - 1)))
  3471. + goto out;
  3472. + err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  3473. + if (unlikely(err))
  3474. + goto out;
  3475. +
  3476. + sbinfo = inode->i_private;
  3477. + sb = sbinfo->si_sb;
  3478. + si_noflush_read_lock(sb);
  3479. + if (l <= au_sbend(sb)) {
  3480. + xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
  3481. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
  3482. + } else
  3483. + err = -ENOENT;
  3484. + si_read_unlock(sb);
  3485. +
  3486. +out:
  3487. + return err;
  3488. +}
  3489. +
  3490. +static const struct file_operations dbgaufs_xino_fop = {
  3491. + .owner = THIS_MODULE,
  3492. + .open = dbgaufs_xino_open,
  3493. + .release = dbgaufs_xi_release,
  3494. + .read = dbgaufs_xi_read
  3495. +};
  3496. +
  3497. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  3498. +{
  3499. + aufs_bindex_t bend;
  3500. + struct au_branch *br;
  3501. + struct au_xino_file *xi;
  3502. +
  3503. + if (!au_sbi(sb)->si_dbgaufs)
  3504. + return;
  3505. +
  3506. + bend = au_sbend(sb);
  3507. + for (; bindex <= bend; bindex++) {
  3508. + br = au_sbr(sb, bindex);
  3509. + xi = &br->br_xino;
  3510. + debugfs_remove(xi->xi_dbgaufs);
  3511. + xi->xi_dbgaufs = NULL;
  3512. + }
  3513. +}
  3514. +
  3515. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  3516. +{
  3517. + struct au_sbinfo *sbinfo;
  3518. + struct dentry *parent;
  3519. + struct au_branch *br;
  3520. + struct au_xino_file *xi;
  3521. + aufs_bindex_t bend;
  3522. + char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
  3523. +
  3524. + sbinfo = au_sbi(sb);
  3525. + parent = sbinfo->si_dbgaufs;
  3526. + if (!parent)
  3527. + return;
  3528. +
  3529. + bend = au_sbend(sb);
  3530. + for (; bindex <= bend; bindex++) {
  3531. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  3532. + br = au_sbr(sb, bindex);
  3533. + xi = &br->br_xino;
  3534. + AuDebugOn(xi->xi_dbgaufs);
  3535. + xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  3536. + sbinfo, &dbgaufs_xino_fop);
  3537. + /* ignore an error */
  3538. + if (unlikely(!xi->xi_dbgaufs))
  3539. + AuWarn1("failed %s under debugfs\n", name);
  3540. + }
  3541. +}
  3542. +
  3543. +/* ---------------------------------------------------------------------- */
  3544. +
  3545. +#ifdef CONFIG_AUFS_EXPORT
  3546. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  3547. +{
  3548. + int err;
  3549. + struct au_sbinfo *sbinfo;
  3550. + struct super_block *sb;
  3551. +
  3552. + sbinfo = inode->i_private;
  3553. + sb = sbinfo->si_sb;
  3554. + si_noflush_read_lock(sb);
  3555. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
  3556. + si_read_unlock(sb);
  3557. + return err;
  3558. +}
  3559. +
  3560. +static const struct file_operations dbgaufs_xigen_fop = {
  3561. + .owner = THIS_MODULE,
  3562. + .open = dbgaufs_xigen_open,
  3563. + .release = dbgaufs_xi_release,
  3564. + .read = dbgaufs_xi_read
  3565. +};
  3566. +
  3567. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  3568. +{
  3569. + int err;
  3570. +
  3571. + /*
  3572. + * This function is a dynamic '__init' function actually,
  3573. + * so the tiny check for si_rwsem is unnecessary.
  3574. + */
  3575. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  3576. +
  3577. + err = -EIO;
  3578. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  3579. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  3580. + &dbgaufs_xigen_fop);
  3581. + if (sbinfo->si_dbgaufs_xigen)
  3582. + err = 0;
  3583. +
  3584. + return err;
  3585. +}
  3586. +#else
  3587. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  3588. +{
  3589. + return 0;
  3590. +}
  3591. +#endif /* CONFIG_AUFS_EXPORT */
  3592. +
  3593. +/* ---------------------------------------------------------------------- */
  3594. +
  3595. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  3596. +{
  3597. + /*
  3598. + * This function is a dynamic '__fin' function actually,
  3599. + * so the tiny check for si_rwsem is unnecessary.
  3600. + */
  3601. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  3602. +
  3603. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  3604. + sbinfo->si_dbgaufs = NULL;
  3605. + kobject_put(&sbinfo->si_kobj);
  3606. +}
  3607. +
  3608. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  3609. +{
  3610. + int err;
  3611. + char name[SysaufsSiNameLen];
  3612. +
  3613. + /*
  3614. + * This function is a dynamic '__init' function actually,
  3615. + * so the tiny check for si_rwsem is unnecessary.
  3616. + */
  3617. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  3618. +
  3619. + err = -ENOENT;
  3620. + if (!dbgaufs) {
  3621. + AuErr1("/debug/aufs is uninitialized\n");
  3622. + goto out;
  3623. + }
  3624. +
  3625. + err = -EIO;
  3626. + sysaufs_name(sbinfo, name);
  3627. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  3628. + if (unlikely(!sbinfo->si_dbgaufs))
  3629. + goto out;
  3630. + kobject_get(&sbinfo->si_kobj);
  3631. +
  3632. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  3633. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  3634. + &dbgaufs_xib_fop);
  3635. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  3636. + goto out_dir;
  3637. +
  3638. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  3639. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  3640. + &dbgaufs_plink_fop);
  3641. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  3642. + goto out_dir;
  3643. +
  3644. + err = dbgaufs_xigen_init(sbinfo);
  3645. + if (!err)
  3646. + goto out; /* success */
  3647. +
  3648. +out_dir:
  3649. + dbgaufs_si_fin(sbinfo);
  3650. +out:
  3651. + return err;
  3652. +}
  3653. +
  3654. +/* ---------------------------------------------------------------------- */
  3655. +
  3656. +void dbgaufs_fin(void)
  3657. +{
  3658. + debugfs_remove(dbgaufs);
  3659. +}
  3660. +
  3661. +int __init dbgaufs_init(void)
  3662. +{
  3663. + int err;
  3664. +
  3665. + err = -EIO;
  3666. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  3667. + if (dbgaufs)
  3668. + err = 0;
  3669. + return err;
  3670. +}
  3671. diff -Nur linux-4.0.5.orig/fs/aufs/dbgaufs.h linux-4.0.5/fs/aufs/dbgaufs.h
  3672. --- linux-4.0.5.orig/fs/aufs/dbgaufs.h 1969-12-31 18:00:00.000000000 -0600
  3673. +++ linux-4.0.5/fs/aufs/dbgaufs.h 2015-06-21 14:07:48.682432781 -0500
  3674. @@ -0,0 +1,48 @@
  3675. +/*
  3676. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3677. + *
  3678. + * This program, aufs is free software; you can redistribute it and/or modify
  3679. + * it under the terms of the GNU General Public License as published by
  3680. + * the Free Software Foundation; either version 2 of the License, or
  3681. + * (at your option) any later version.
  3682. + *
  3683. + * This program is distributed in the hope that it will be useful,
  3684. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3685. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3686. + * GNU General Public License for more details.
  3687. + *
  3688. + * You should have received a copy of the GNU General Public License
  3689. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3690. + */
  3691. +
  3692. +/*
  3693. + * debugfs interface
  3694. + */
  3695. +
  3696. +#ifndef __DBGAUFS_H__
  3697. +#define __DBGAUFS_H__
  3698. +
  3699. +#ifdef __KERNEL__
  3700. +
  3701. +struct super_block;
  3702. +struct au_sbinfo;
  3703. +
  3704. +#ifdef CONFIG_DEBUG_FS
  3705. +/* dbgaufs.c */
  3706. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  3707. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  3708. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  3709. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  3710. +void dbgaufs_fin(void);
  3711. +int __init dbgaufs_init(void);
  3712. +#else
  3713. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  3714. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  3715. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  3716. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  3717. +AuStubVoid(dbgaufs_fin, void)
  3718. +AuStubInt0(__init dbgaufs_init, void)
  3719. +#endif /* CONFIG_DEBUG_FS */
  3720. +
  3721. +#endif /* __KERNEL__ */
  3722. +#endif /* __DBGAUFS_H__ */
  3723. diff -Nur linux-4.0.5.orig/fs/aufs/dcsub.c linux-4.0.5/fs/aufs/dcsub.c
  3724. --- linux-4.0.5.orig/fs/aufs/dcsub.c 1969-12-31 18:00:00.000000000 -0600
  3725. +++ linux-4.0.5/fs/aufs/dcsub.c 2015-06-21 14:07:48.682432781 -0500
  3726. @@ -0,0 +1,224 @@
  3727. +/*
  3728. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3729. + *
  3730. + * This program, aufs is free software; you can redistribute it and/or modify
  3731. + * it under the terms of the GNU General Public License as published by
  3732. + * the Free Software Foundation; either version 2 of the License, or
  3733. + * (at your option) any later version.
  3734. + *
  3735. + * This program is distributed in the hope that it will be useful,
  3736. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3737. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3738. + * GNU General Public License for more details.
  3739. + *
  3740. + * You should have received a copy of the GNU General Public License
  3741. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3742. + */
  3743. +
  3744. +/*
  3745. + * sub-routines for dentry cache
  3746. + */
  3747. +
  3748. +#include "aufs.h"
  3749. +
  3750. +static void au_dpage_free(struct au_dpage *dpage)
  3751. +{
  3752. + int i;
  3753. + struct dentry **p;
  3754. +
  3755. + p = dpage->dentries;
  3756. + for (i = 0; i < dpage->ndentry; i++)
  3757. + dput(*p++);
  3758. + free_page((unsigned long)dpage->dentries);
  3759. +}
  3760. +
  3761. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  3762. +{
  3763. + int err;
  3764. + void *p;
  3765. +
  3766. + err = -ENOMEM;
  3767. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  3768. + if (unlikely(!dpages->dpages))
  3769. + goto out;
  3770. +
  3771. + p = (void *)__get_free_page(gfp);
  3772. + if (unlikely(!p))
  3773. + goto out_dpages;
  3774. +
  3775. + dpages->dpages[0].ndentry = 0;
  3776. + dpages->dpages[0].dentries = p;
  3777. + dpages->ndpage = 1;
  3778. + return 0; /* success */
  3779. +
  3780. +out_dpages:
  3781. + kfree(dpages->dpages);
  3782. +out:
  3783. + return err;
  3784. +}
  3785. +
  3786. +void au_dpages_free(struct au_dcsub_pages *dpages)
  3787. +{
  3788. + int i;
  3789. + struct au_dpage *p;
  3790. +
  3791. + p = dpages->dpages;
  3792. + for (i = 0; i < dpages->ndpage; i++)
  3793. + au_dpage_free(p++);
  3794. + kfree(dpages->dpages);
  3795. +}
  3796. +
  3797. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  3798. + struct dentry *dentry, gfp_t gfp)
  3799. +{
  3800. + int err, sz;
  3801. + struct au_dpage *dpage;
  3802. + void *p;
  3803. +
  3804. + dpage = dpages->dpages + dpages->ndpage - 1;
  3805. + sz = PAGE_SIZE / sizeof(dentry);
  3806. + if (unlikely(dpage->ndentry >= sz)) {
  3807. + AuLabel(new dpage);
  3808. + err = -ENOMEM;
  3809. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  3810. + p = au_kzrealloc(dpages->dpages, sz,
  3811. + sz + sizeof(*dpages->dpages), gfp);
  3812. + if (unlikely(!p))
  3813. + goto out;
  3814. +
  3815. + dpages->dpages = p;
  3816. + dpage = dpages->dpages + dpages->ndpage;
  3817. + p = (void *)__get_free_page(gfp);
  3818. + if (unlikely(!p))
  3819. + goto out;
  3820. +
  3821. + dpage->ndentry = 0;
  3822. + dpage->dentries = p;
  3823. + dpages->ndpage++;
  3824. + }
  3825. +
  3826. + AuDebugOn(au_dcount(dentry) <= 0);
  3827. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  3828. + return 0; /* success */
  3829. +
  3830. +out:
  3831. + return err;
  3832. +}
  3833. +
  3834. +/* todo: BAD approach */
  3835. +/* copied from linux/fs/dcache.c */
  3836. +enum d_walk_ret {
  3837. + D_WALK_CONTINUE,
  3838. + D_WALK_QUIT,
  3839. + D_WALK_NORETRY,
  3840. + D_WALK_SKIP,
  3841. +};
  3842. +
  3843. +extern void d_walk(struct dentry *parent, void *data,
  3844. + enum d_walk_ret (*enter)(void *, struct dentry *),
  3845. + void (*finish)(void *));
  3846. +
  3847. +struct ac_dpages_arg {
  3848. + int err;
  3849. + struct au_dcsub_pages *dpages;
  3850. + struct super_block *sb;
  3851. + au_dpages_test test;
  3852. + void *arg;
  3853. +};
  3854. +
  3855. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  3856. +{
  3857. + enum d_walk_ret ret;
  3858. + struct ac_dpages_arg *arg = _arg;
  3859. +
  3860. + ret = D_WALK_CONTINUE;
  3861. + if (dentry->d_sb == arg->sb
  3862. + && !IS_ROOT(dentry)
  3863. + && au_dcount(dentry) > 0
  3864. + && au_di(dentry)
  3865. + && (!arg->test || arg->test(dentry, arg->arg))) {
  3866. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  3867. + if (unlikely(arg->err))
  3868. + ret = D_WALK_QUIT;
  3869. + }
  3870. +
  3871. + return ret;
  3872. +}
  3873. +
  3874. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  3875. + au_dpages_test test, void *arg)
  3876. +{
  3877. + struct ac_dpages_arg args = {
  3878. + .err = 0,
  3879. + .dpages = dpages,
  3880. + .sb = root->d_sb,
  3881. + .test = test,
  3882. + .arg = arg
  3883. + };
  3884. +
  3885. + d_walk(root, &args, au_call_dpages_append, NULL);
  3886. +
  3887. + return args.err;
  3888. +}
  3889. +
  3890. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  3891. + int do_include, au_dpages_test test, void *arg)
  3892. +{
  3893. + int err;
  3894. +
  3895. + err = 0;
  3896. + write_seqlock(&rename_lock);
  3897. + spin_lock(&dentry->d_lock);
  3898. + if (do_include
  3899. + && au_dcount(dentry) > 0
  3900. + && (!test || test(dentry, arg)))
  3901. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  3902. + spin_unlock(&dentry->d_lock);
  3903. + if (unlikely(err))
  3904. + goto out;
  3905. +
  3906. + /*
  3907. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  3908. + * mount
  3909. + */
  3910. + while (!IS_ROOT(dentry)) {
  3911. + dentry = dentry->d_parent; /* rename_lock is locked */
  3912. + spin_lock(&dentry->d_lock);
  3913. + if (au_dcount(dentry) > 0
  3914. + && (!test || test(dentry, arg)))
  3915. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  3916. + spin_unlock(&dentry->d_lock);
  3917. + if (unlikely(err))
  3918. + break;
  3919. + }
  3920. +
  3921. +out:
  3922. + write_sequnlock(&rename_lock);
  3923. + return err;
  3924. +}
  3925. +
  3926. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  3927. +{
  3928. + return au_di(dentry) && dentry->d_sb == arg;
  3929. +}
  3930. +
  3931. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  3932. + struct dentry *dentry, int do_include)
  3933. +{
  3934. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  3935. + au_dcsub_dpages_aufs, dentry->d_sb);
  3936. +}
  3937. +
  3938. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  3939. +{
  3940. + struct path path[2] = {
  3941. + {
  3942. + .dentry = d1
  3943. + },
  3944. + {
  3945. + .dentry = d2
  3946. + }
  3947. + };
  3948. +
  3949. + return path_is_under(path + 0, path + 1);
  3950. +}
  3951. diff -Nur linux-4.0.5.orig/fs/aufs/dcsub.h linux-4.0.5/fs/aufs/dcsub.h
  3952. --- linux-4.0.5.orig/fs/aufs/dcsub.h 1969-12-31 18:00:00.000000000 -0600
  3953. +++ linux-4.0.5/fs/aufs/dcsub.h 2015-06-21 14:07:48.682432781 -0500
  3954. @@ -0,0 +1,132 @@
  3955. +/*
  3956. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3957. + *
  3958. + * This program, aufs is free software; you can redistribute it and/or modify
  3959. + * it under the terms of the GNU General Public License as published by
  3960. + * the Free Software Foundation; either version 2 of the License, or
  3961. + * (at your option) any later version.
  3962. + *
  3963. + * This program is distributed in the hope that it will be useful,
  3964. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3965. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3966. + * GNU General Public License for more details.
  3967. + *
  3968. + * You should have received a copy of the GNU General Public License
  3969. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3970. + */
  3971. +
  3972. +/*
  3973. + * sub-routines for dentry cache
  3974. + */
  3975. +
  3976. +#ifndef __AUFS_DCSUB_H__
  3977. +#define __AUFS_DCSUB_H__
  3978. +
  3979. +#ifdef __KERNEL__
  3980. +
  3981. +#include <linux/dcache.h>
  3982. +#include <linux/fs.h>
  3983. +
  3984. +struct au_dpage {
  3985. + int ndentry;
  3986. + struct dentry **dentries;
  3987. +};
  3988. +
  3989. +struct au_dcsub_pages {
  3990. + int ndpage;
  3991. + struct au_dpage *dpages;
  3992. +};
  3993. +
  3994. +/* ---------------------------------------------------------------------- */
  3995. +
  3996. +/* dcsub.c */
  3997. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  3998. +void au_dpages_free(struct au_dcsub_pages *dpages);
  3999. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  4000. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  4001. + au_dpages_test test, void *arg);
  4002. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  4003. + int do_include, au_dpages_test test, void *arg);
  4004. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  4005. + struct dentry *dentry, int do_include);
  4006. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  4007. +
  4008. +/* ---------------------------------------------------------------------- */
  4009. +
  4010. +/*
  4011. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  4012. + * include/linux/dcache.h. Try them (in the future).
  4013. + */
  4014. +
  4015. +static inline int au_d_hashed_positive(struct dentry *d)
  4016. +{
  4017. + int err;
  4018. + struct inode *inode = d->d_inode;
  4019. +
  4020. + err = 0;
  4021. + if (unlikely(d_unhashed(d) || !inode || !inode->i_nlink))
  4022. + err = -ENOENT;
  4023. + return err;
  4024. +}
  4025. +
  4026. +static inline int au_d_linkable(struct dentry *d)
  4027. +{
  4028. + int err;
  4029. + struct inode *inode = d->d_inode;
  4030. +
  4031. + err = au_d_hashed_positive(d);
  4032. + if (err
  4033. + && inode
  4034. + && (inode->i_state & I_LINKABLE))
  4035. + err = 0;
  4036. + return err;
  4037. +}
  4038. +
  4039. +static inline int au_d_alive(struct dentry *d)
  4040. +{
  4041. + int err;
  4042. + struct inode *inode;
  4043. +
  4044. + err = 0;
  4045. + if (!IS_ROOT(d))
  4046. + err = au_d_hashed_positive(d);
  4047. + else {
  4048. + inode = d->d_inode;
  4049. + if (unlikely(d_unlinked(d) || !inode || !inode->i_nlink))
  4050. + err = -ENOENT;
  4051. + }
  4052. + return err;
  4053. +}
  4054. +
  4055. +static inline int au_alive_dir(struct dentry *d)
  4056. +{
  4057. + int err;
  4058. +
  4059. + err = au_d_alive(d);
  4060. + if (unlikely(err || IS_DEADDIR(d->d_inode)))
  4061. + err = -ENOENT;
  4062. + return err;
  4063. +}
  4064. +
  4065. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  4066. +{
  4067. + return a->len == b->len
  4068. + && !memcmp(a->name, b->name, a->len);
  4069. +}
  4070. +
  4071. +/*
  4072. + * by the commit
  4073. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  4074. + * taking d_lock
  4075. + * the type of d_lockref.count became int, but the inlined function d_count()
  4076. + * still returns unsigned int.
  4077. + * I don't know why. Maybe it is for every d_count() users?
  4078. + * Anyway au_dcount() lives on.
  4079. + */
  4080. +static inline int au_dcount(struct dentry *d)
  4081. +{
  4082. + return (int)d_count(d);
  4083. +}
  4084. +
  4085. +#endif /* __KERNEL__ */
  4086. +#endif /* __AUFS_DCSUB_H__ */
  4087. diff -Nur linux-4.0.5.orig/fs/aufs/debug.c linux-4.0.5/fs/aufs/debug.c
  4088. --- linux-4.0.5.orig/fs/aufs/debug.c 1969-12-31 18:00:00.000000000 -0600
  4089. +++ linux-4.0.5/fs/aufs/debug.c 2015-06-21 14:07:48.682432781 -0500
  4090. @@ -0,0 +1,438 @@
  4091. +/*
  4092. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4093. + *
  4094. + * This program, aufs is free software; you can redistribute it and/or modify
  4095. + * it under the terms of the GNU General Public License as published by
  4096. + * the Free Software Foundation; either version 2 of the License, or
  4097. + * (at your option) any later version.
  4098. + *
  4099. + * This program is distributed in the hope that it will be useful,
  4100. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4101. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4102. + * GNU General Public License for more details.
  4103. + *
  4104. + * You should have received a copy of the GNU General Public License
  4105. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  4106. + */
  4107. +
  4108. +/*
  4109. + * debug print functions
  4110. + */
  4111. +
  4112. +#include "aufs.h"
  4113. +
  4114. +/* Returns 0, or -errno. arg is in kp->arg. */
  4115. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  4116. +{
  4117. + int err, n;
  4118. +
  4119. + err = kstrtoint(val, 0, &n);
  4120. + if (!err) {
  4121. + if (n > 0)
  4122. + au_debug_on();
  4123. + else
  4124. + au_debug_off();
  4125. + }
  4126. + return err;
  4127. +}
  4128. +
  4129. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  4130. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  4131. +{
  4132. + atomic_t *a;
  4133. +
  4134. + a = kp->arg;
  4135. + return sprintf(buffer, "%d", atomic_read(a));
  4136. +}
  4137. +
  4138. +static struct kernel_param_ops param_ops_atomic_t = {
  4139. + .set = param_atomic_t_set,
  4140. + .get = param_atomic_t_get
  4141. + /* void (*free)(void *arg) */
  4142. +};
  4143. +
  4144. +atomic_t aufs_debug = ATOMIC_INIT(0);
  4145. +MODULE_PARM_DESC(debug, "debug print");
  4146. +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
  4147. +
  4148. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  4149. +char *au_plevel = KERN_DEBUG;
  4150. +#define dpri(fmt, ...) do { \
  4151. + if ((au_plevel \
  4152. + && strcmp(au_plevel, KERN_DEBUG)) \
  4153. + || au_debug_test()) \
  4154. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  4155. +} while (0)
  4156. +
  4157. +/* ---------------------------------------------------------------------- */
  4158. +
  4159. +void au_dpri_whlist(struct au_nhash *whlist)
  4160. +{
  4161. + unsigned long ul, n;
  4162. + struct hlist_head *head;
  4163. + struct au_vdir_wh *pos;
  4164. +
  4165. + n = whlist->nh_num;
  4166. + head = whlist->nh_head;
  4167. + for (ul = 0; ul < n; ul++) {
  4168. + hlist_for_each_entry(pos, head, wh_hash)
  4169. + dpri("b%d, %.*s, %d\n",
  4170. + pos->wh_bindex,
  4171. + pos->wh_str.len, pos->wh_str.name,
  4172. + pos->wh_str.len);
  4173. + head++;
  4174. + }
  4175. +}
  4176. +
  4177. +void au_dpri_vdir(struct au_vdir *vdir)
  4178. +{
  4179. + unsigned long ul;
  4180. + union au_vdir_deblk_p p;
  4181. + unsigned char *o;
  4182. +
  4183. + if (!vdir || IS_ERR(vdir)) {
  4184. + dpri("err %ld\n", PTR_ERR(vdir));
  4185. + return;
  4186. + }
  4187. +
  4188. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
  4189. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  4190. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  4191. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  4192. + p.deblk = vdir->vd_deblk[ul];
  4193. + o = p.deblk;
  4194. + dpri("[%lu]: %p\n", ul, o);
  4195. + }
  4196. +}
  4197. +
  4198. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  4199. + struct dentry *wh)
  4200. +{
  4201. + char *n = NULL;
  4202. + int l = 0;
  4203. +
  4204. + if (!inode || IS_ERR(inode)) {
  4205. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  4206. + return -1;
  4207. + }
  4208. +
  4209. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  4210. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  4211. + && sizeof(inode->i_blocks) != sizeof(u64));
  4212. + if (wh) {
  4213. + n = (void *)wh->d_name.name;
  4214. + l = wh->d_name.len;
  4215. + }
  4216. +
  4217. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  4218. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  4219. + bindex, inode,
  4220. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  4221. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  4222. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  4223. + hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
  4224. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  4225. + inode->i_state, inode->i_flags, inode->i_version,
  4226. + inode->i_generation,
  4227. + l ? ", wh " : "", l, n);
  4228. + return 0;
  4229. +}
  4230. +
  4231. +void au_dpri_inode(struct inode *inode)
  4232. +{
  4233. + struct au_iinfo *iinfo;
  4234. + aufs_bindex_t bindex;
  4235. + int err, hn;
  4236. +
  4237. + err = do_pri_inode(-1, inode, -1, NULL);
  4238. + if (err || !au_test_aufs(inode->i_sb))
  4239. + return;
  4240. +
  4241. + iinfo = au_ii(inode);
  4242. + if (!iinfo)
  4243. + return;
  4244. + dpri("i-1: bstart %d, bend %d, gen %d\n",
  4245. + iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
  4246. + if (iinfo->ii_bstart < 0)
  4247. + return;
  4248. + hn = 0;
  4249. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
  4250. + hn = !!au_hn(iinfo->ii_hinode + bindex);
  4251. + do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
  4252. + iinfo->ii_hinode[0 + bindex].hi_whdentry);
  4253. + }
  4254. +}
  4255. +
  4256. +void au_dpri_dalias(struct inode *inode)
  4257. +{
  4258. + struct dentry *d;
  4259. +
  4260. + spin_lock(&inode->i_lock);
  4261. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  4262. + au_dpri_dentry(d);
  4263. + spin_unlock(&inode->i_lock);
  4264. +}
  4265. +
  4266. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  4267. +{
  4268. + struct dentry *wh = NULL;
  4269. + int hn;
  4270. + struct au_iinfo *iinfo;
  4271. +
  4272. + if (!dentry || IS_ERR(dentry)) {
  4273. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  4274. + return -1;
  4275. + }
  4276. + /* do not call dget_parent() here */
  4277. + /* note: access d_xxx without d_lock */
  4278. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  4279. + bindex, dentry, dentry,
  4280. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  4281. + au_dcount(dentry), dentry->d_flags,
  4282. + d_unhashed(dentry) ? "un" : "");
  4283. + hn = -1;
  4284. + if (bindex >= 0 && dentry->d_inode && au_test_aufs(dentry->d_sb)) {
  4285. + iinfo = au_ii(dentry->d_inode);
  4286. + if (iinfo) {
  4287. + hn = !!au_hn(iinfo->ii_hinode + bindex);
  4288. + wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
  4289. + }
  4290. + }
  4291. + do_pri_inode(bindex, dentry->d_inode, hn, wh);
  4292. + return 0;
  4293. +}
  4294. +
  4295. +void au_dpri_dentry(struct dentry *dentry)
  4296. +{
  4297. + struct au_dinfo *dinfo;
  4298. + aufs_bindex_t bindex;
  4299. + int err;
  4300. + struct au_hdentry *hdp;
  4301. +
  4302. + err = do_pri_dentry(-1, dentry);
  4303. + if (err || !au_test_aufs(dentry->d_sb))
  4304. + return;
  4305. +
  4306. + dinfo = au_di(dentry);
  4307. + if (!dinfo)
  4308. + return;
  4309. + dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  4310. + dinfo->di_bstart, dinfo->di_bend,
  4311. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  4312. + dinfo->di_tmpfile);
  4313. + if (dinfo->di_bstart < 0)
  4314. + return;
  4315. + hdp = dinfo->di_hdentry;
  4316. + for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
  4317. + do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
  4318. +}
  4319. +
  4320. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  4321. +{
  4322. + char a[32];
  4323. +
  4324. + if (!file || IS_ERR(file)) {
  4325. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  4326. + return -1;
  4327. + }
  4328. + a[0] = 0;
  4329. + if (bindex < 0
  4330. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  4331. + && au_test_aufs(file->f_path.dentry->d_sb)
  4332. + && au_fi(file))
  4333. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  4334. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  4335. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  4336. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  4337. + file->f_version, file->f_pos, a);
  4338. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  4339. + do_pri_dentry(bindex, file->f_path.dentry);
  4340. + return 0;
  4341. +}
  4342. +
  4343. +void au_dpri_file(struct file *file)
  4344. +{
  4345. + struct au_finfo *finfo;
  4346. + struct au_fidir *fidir;
  4347. + struct au_hfile *hfile;
  4348. + aufs_bindex_t bindex;
  4349. + int err;
  4350. +
  4351. + err = do_pri_file(-1, file);
  4352. + if (err
  4353. + || IS_ERR_OR_NULL(file->f_path.dentry)
  4354. + || !au_test_aufs(file->f_path.dentry->d_sb))
  4355. + return;
  4356. +
  4357. + finfo = au_fi(file);
  4358. + if (!finfo)
  4359. + return;
  4360. + if (finfo->fi_btop < 0)
  4361. + return;
  4362. + fidir = finfo->fi_hdir;
  4363. + if (!fidir)
  4364. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  4365. + else
  4366. + for (bindex = finfo->fi_btop;
  4367. + bindex >= 0 && bindex <= fidir->fd_bbot;
  4368. + bindex++) {
  4369. + hfile = fidir->fd_hfile + bindex;
  4370. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  4371. + }
  4372. +}
  4373. +
  4374. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  4375. +{
  4376. + struct vfsmount *mnt;
  4377. + struct super_block *sb;
  4378. +
  4379. + if (!br || IS_ERR(br))
  4380. + goto out;
  4381. + mnt = au_br_mnt(br);
  4382. + if (!mnt || IS_ERR(mnt))
  4383. + goto out;
  4384. + sb = mnt->mnt_sb;
  4385. + if (!sb || IS_ERR(sb))
  4386. + goto out;
  4387. +
  4388. + dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
  4389. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  4390. + "xino %d\n",
  4391. + bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
  4392. + br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  4393. + sb->s_flags, sb->s_count,
  4394. + atomic_read(&sb->s_active), !!br->br_xino.xi_file);
  4395. + return 0;
  4396. +
  4397. +out:
  4398. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  4399. + return -1;
  4400. +}
  4401. +
  4402. +void au_dpri_sb(struct super_block *sb)
  4403. +{
  4404. + struct au_sbinfo *sbinfo;
  4405. + aufs_bindex_t bindex;
  4406. + int err;
  4407. + /* to reuduce stack size */
  4408. + struct {
  4409. + struct vfsmount mnt;
  4410. + struct au_branch fake;
  4411. + } *a;
  4412. +
  4413. + /* this function can be called from magic sysrq */
  4414. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  4415. + if (unlikely(!a)) {
  4416. + dpri("no memory\n");
  4417. + return;
  4418. + }
  4419. +
  4420. + a->mnt.mnt_sb = sb;
  4421. + a->fake.br_perm = 0;
  4422. + a->fake.br_path.mnt = &a->mnt;
  4423. + a->fake.br_xino.xi_file = NULL;
  4424. + atomic_set(&a->fake.br_count, 0);
  4425. + smp_mb(); /* atomic_set */
  4426. + err = do_pri_br(-1, &a->fake);
  4427. + kfree(a);
  4428. + dpri("dev 0x%x\n", sb->s_dev);
  4429. + if (err || !au_test_aufs(sb))
  4430. + return;
  4431. +
  4432. + sbinfo = au_sbi(sb);
  4433. + if (!sbinfo)
  4434. + return;
  4435. + dpri("nw %d, gen %u, kobj %d\n",
  4436. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  4437. + atomic_read(&sbinfo->si_kobj.kref.refcount));
  4438. + for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
  4439. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  4440. +}
  4441. +
  4442. +/* ---------------------------------------------------------------------- */
  4443. +
  4444. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  4445. +{
  4446. + struct inode *h_inode, *inode = dentry->d_inode;
  4447. + struct dentry *h_dentry;
  4448. + aufs_bindex_t bindex, bend, bi;
  4449. +
  4450. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  4451. + return;
  4452. +
  4453. + bend = au_dbend(dentry);
  4454. + bi = au_ibend(inode);
  4455. + if (bi < bend)
  4456. + bend = bi;
  4457. + bindex = au_dbstart(dentry);
  4458. + bi = au_ibstart(inode);
  4459. + if (bi > bindex)
  4460. + bindex = bi;
  4461. +
  4462. + for (; bindex <= bend; bindex++) {
  4463. + h_dentry = au_h_dptr(dentry, bindex);
  4464. + if (!h_dentry)
  4465. + continue;
  4466. + h_inode = au_h_iptr(inode, bindex);
  4467. + if (unlikely(h_inode != h_dentry->d_inode)) {
  4468. + au_debug_on();
  4469. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  4470. + AuDbgDentry(dentry);
  4471. + AuDbgInode(inode);
  4472. + au_debug_off();
  4473. + BUG();
  4474. + }
  4475. + }
  4476. +}
  4477. +
  4478. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  4479. +{
  4480. + int err, i, j;
  4481. + struct au_dcsub_pages dpages;
  4482. + struct au_dpage *dpage;
  4483. + struct dentry **dentries;
  4484. +
  4485. + err = au_dpages_init(&dpages, GFP_NOFS);
  4486. + AuDebugOn(err);
  4487. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  4488. + AuDebugOn(err);
  4489. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  4490. + dpage = dpages.dpages + i;
  4491. + dentries = dpage->dentries;
  4492. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  4493. + AuDebugOn(au_digen_test(dentries[j], sigen));
  4494. + }
  4495. + au_dpages_free(&dpages);
  4496. +}
  4497. +
  4498. +void au_dbg_verify_kthread(void)
  4499. +{
  4500. + if (au_wkq_test()) {
  4501. + au_dbg_blocked();
  4502. + /*
  4503. + * It may be recursive, but udba=notify between two aufs mounts,
  4504. + * where a single ro branch is shared, is not a problem.
  4505. + */
  4506. + /* WARN_ON(1); */
  4507. + }
  4508. +}
  4509. +
  4510. +/* ---------------------------------------------------------------------- */
  4511. +
  4512. +int __init au_debug_init(void)
  4513. +{
  4514. + aufs_bindex_t bindex;
  4515. + struct au_vdir_destr destr;
  4516. +
  4517. + bindex = -1;
  4518. + AuDebugOn(bindex >= 0);
  4519. +
  4520. + destr.len = -1;
  4521. + AuDebugOn(destr.len < NAME_MAX);
  4522. +
  4523. +#ifdef CONFIG_4KSTACKS
  4524. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  4525. +#endif
  4526. +
  4527. + return 0;
  4528. +}
  4529. diff -Nur linux-4.0.5.orig/fs/aufs/debug.h linux-4.0.5/fs/aufs/debug.h
  4530. --- linux-4.0.5.orig/fs/aufs/debug.h 1969-12-31 18:00:00.000000000 -0600
  4531. +++ linux-4.0.5/fs/aufs/debug.h 2015-06-21 14:07:48.682432781 -0500
  4532. @@ -0,0 +1,225 @@
  4533. +/*
  4534. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4535. + *
  4536. + * This program, aufs is free software; you can redistribute it and/or modify
  4537. + * it under the terms of the GNU General Public License as published by
  4538. + * the Free Software Foundation; either version 2 of the License, or
  4539. + * (at your option) any later version.
  4540. + *
  4541. + * This program is distributed in the hope that it will be useful,
  4542. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4543. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4544. + * GNU General Public License for more details.
  4545. + *
  4546. + * You should have received a copy of the GNU General Public License
  4547. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  4548. + */
  4549. +
  4550. +/*
  4551. + * debug print functions
  4552. + */
  4553. +
  4554. +#ifndef __AUFS_DEBUG_H__
  4555. +#define __AUFS_DEBUG_H__
  4556. +
  4557. +#ifdef __KERNEL__
  4558. +
  4559. +#include <linux/atomic.h>
  4560. +#include <linux/module.h>
  4561. +#include <linux/kallsyms.h>
  4562. +#include <linux/sysrq.h>
  4563. +
  4564. +#ifdef CONFIG_AUFS_DEBUG
  4565. +#define AuDebugOn(a) BUG_ON(a)
  4566. +
  4567. +/* module parameter */
  4568. +extern atomic_t aufs_debug;
  4569. +static inline void au_debug_on(void)
  4570. +{
  4571. + atomic_inc(&aufs_debug);
  4572. +}
  4573. +static inline void au_debug_off(void)
  4574. +{
  4575. + atomic_dec_if_positive(&aufs_debug);
  4576. +}
  4577. +
  4578. +static inline int au_debug_test(void)
  4579. +{
  4580. + return atomic_read(&aufs_debug) > 0;
  4581. +}
  4582. +#else
  4583. +#define AuDebugOn(a) do {} while (0)
  4584. +AuStubVoid(au_debug_on, void)
  4585. +AuStubVoid(au_debug_off, void)
  4586. +AuStubInt0(au_debug_test, void)
  4587. +#endif /* CONFIG_AUFS_DEBUG */
  4588. +
  4589. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  4590. +
  4591. +/* ---------------------------------------------------------------------- */
  4592. +
  4593. +/* debug print */
  4594. +
  4595. +#define AuDbg(fmt, ...) do { \
  4596. + if (au_debug_test()) \
  4597. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  4598. +} while (0)
  4599. +#define AuLabel(l) AuDbg(#l "\n")
  4600. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  4601. +#define AuWarn1(fmt, ...) do { \
  4602. + static unsigned char _c; \
  4603. + if (!_c++) \
  4604. + pr_warn(fmt, ##__VA_ARGS__); \
  4605. +} while (0)
  4606. +
  4607. +#define AuErr1(fmt, ...) do { \
  4608. + static unsigned char _c; \
  4609. + if (!_c++) \
  4610. + pr_err(fmt, ##__VA_ARGS__); \
  4611. +} while (0)
  4612. +
  4613. +#define AuIOErr1(fmt, ...) do { \
  4614. + static unsigned char _c; \
  4615. + if (!_c++) \
  4616. + AuIOErr(fmt, ##__VA_ARGS__); \
  4617. +} while (0)
  4618. +
  4619. +#define AuUnsupportMsg "This operation is not supported." \
  4620. + " Please report this application to aufs-users ML."
  4621. +#define AuUnsupport(fmt, ...) do { \
  4622. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  4623. + dump_stack(); \
  4624. +} while (0)
  4625. +
  4626. +#define AuTraceErr(e) do { \
  4627. + if (unlikely((e) < 0)) \
  4628. + AuDbg("err %d\n", (int)(e)); \
  4629. +} while (0)
  4630. +
  4631. +#define AuTraceErrPtr(p) do { \
  4632. + if (IS_ERR(p)) \
  4633. + AuDbg("err %ld\n", PTR_ERR(p)); \
  4634. +} while (0)
  4635. +
  4636. +/* dirty macros for debug print, use with "%.*s" and caution */
  4637. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  4638. +
  4639. +/* ---------------------------------------------------------------------- */
  4640. +
  4641. +struct dentry;
  4642. +#ifdef CONFIG_AUFS_DEBUG
  4643. +extern struct mutex au_dbg_mtx;
  4644. +extern char *au_plevel;
  4645. +struct au_nhash;
  4646. +void au_dpri_whlist(struct au_nhash *whlist);
  4647. +struct au_vdir;
  4648. +void au_dpri_vdir(struct au_vdir *vdir);
  4649. +struct inode;
  4650. +void au_dpri_inode(struct inode *inode);
  4651. +void au_dpri_dalias(struct inode *inode);
  4652. +void au_dpri_dentry(struct dentry *dentry);
  4653. +struct file;
  4654. +void au_dpri_file(struct file *filp);
  4655. +struct super_block;
  4656. +void au_dpri_sb(struct super_block *sb);
  4657. +
  4658. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  4659. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  4660. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  4661. +void au_dbg_verify_kthread(void);
  4662. +
  4663. +int __init au_debug_init(void);
  4664. +
  4665. +#define AuDbgWhlist(w) do { \
  4666. + mutex_lock(&au_dbg_mtx); \
  4667. + AuDbg(#w "\n"); \
  4668. + au_dpri_whlist(w); \
  4669. + mutex_unlock(&au_dbg_mtx); \
  4670. +} while (0)
  4671. +
  4672. +#define AuDbgVdir(v) do { \
  4673. + mutex_lock(&au_dbg_mtx); \
  4674. + AuDbg(#v "\n"); \
  4675. + au_dpri_vdir(v); \
  4676. + mutex_unlock(&au_dbg_mtx); \
  4677. +} while (0)
  4678. +
  4679. +#define AuDbgInode(i) do { \
  4680. + mutex_lock(&au_dbg_mtx); \
  4681. + AuDbg(#i "\n"); \
  4682. + au_dpri_inode(i); \
  4683. + mutex_unlock(&au_dbg_mtx); \
  4684. +} while (0)
  4685. +
  4686. +#define AuDbgDAlias(i) do { \
  4687. + mutex_lock(&au_dbg_mtx); \
  4688. + AuDbg(#i "\n"); \
  4689. + au_dpri_dalias(i); \
  4690. + mutex_unlock(&au_dbg_mtx); \
  4691. +} while (0)
  4692. +
  4693. +#define AuDbgDentry(d) do { \
  4694. + mutex_lock(&au_dbg_mtx); \
  4695. + AuDbg(#d "\n"); \
  4696. + au_dpri_dentry(d); \
  4697. + mutex_unlock(&au_dbg_mtx); \
  4698. +} while (0)
  4699. +
  4700. +#define AuDbgFile(f) do { \
  4701. + mutex_lock(&au_dbg_mtx); \
  4702. + AuDbg(#f "\n"); \
  4703. + au_dpri_file(f); \
  4704. + mutex_unlock(&au_dbg_mtx); \
  4705. +} while (0)
  4706. +
  4707. +#define AuDbgSb(sb) do { \
  4708. + mutex_lock(&au_dbg_mtx); \
  4709. + AuDbg(#sb "\n"); \
  4710. + au_dpri_sb(sb); \
  4711. + mutex_unlock(&au_dbg_mtx); \
  4712. +} while (0)
  4713. +
  4714. +#define AuDbgSym(addr) do { \
  4715. + char sym[KSYM_SYMBOL_LEN]; \
  4716. + sprint_symbol(sym, (unsigned long)addr); \
  4717. + AuDbg("%s\n", sym); \
  4718. +} while (0)
  4719. +#else
  4720. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  4721. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  4722. +AuStubVoid(au_dbg_verify_kthread, void)
  4723. +AuStubInt0(__init au_debug_init, void)
  4724. +
  4725. +#define AuDbgWhlist(w) do {} while (0)
  4726. +#define AuDbgVdir(v) do {} while (0)
  4727. +#define AuDbgInode(i) do {} while (0)
  4728. +#define AuDbgDAlias(i) do {} while (0)
  4729. +#define AuDbgDentry(d) do {} while (0)
  4730. +#define AuDbgFile(f) do {} while (0)
  4731. +#define AuDbgSb(sb) do {} while (0)
  4732. +#define AuDbgSym(addr) do {} while (0)
  4733. +#endif /* CONFIG_AUFS_DEBUG */
  4734. +
  4735. +/* ---------------------------------------------------------------------- */
  4736. +
  4737. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  4738. +int __init au_sysrq_init(void);
  4739. +void au_sysrq_fin(void);
  4740. +
  4741. +#ifdef CONFIG_HW_CONSOLE
  4742. +#define au_dbg_blocked() do { \
  4743. + WARN_ON(1); \
  4744. + handle_sysrq('w'); \
  4745. +} while (0)
  4746. +#else
  4747. +AuStubVoid(au_dbg_blocked, void)
  4748. +#endif
  4749. +
  4750. +#else
  4751. +AuStubInt0(__init au_sysrq_init, void)
  4752. +AuStubVoid(au_sysrq_fin, void)
  4753. +AuStubVoid(au_dbg_blocked, void)
  4754. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  4755. +
  4756. +#endif /* __KERNEL__ */
  4757. +#endif /* __AUFS_DEBUG_H__ */
  4758. diff -Nur linux-4.0.5.orig/fs/aufs/dentry.c linux-4.0.5/fs/aufs/dentry.c
  4759. --- linux-4.0.5.orig/fs/aufs/dentry.c 1969-12-31 18:00:00.000000000 -0600
  4760. +++ linux-4.0.5/fs/aufs/dentry.c 2015-06-21 14:07:48.682432781 -0500
  4761. @@ -0,0 +1,1097 @@
  4762. +/*
  4763. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4764. + *
  4765. + * This program, aufs is free software; you can redistribute it and/or modify
  4766. + * it under the terms of the GNU General Public License as published by
  4767. + * the Free Software Foundation; either version 2 of the License, or
  4768. + * (at your option) any later version.
  4769. + *
  4770. + * This program is distributed in the hope that it will be useful,
  4771. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4772. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4773. + * GNU General Public License for more details.
  4774. + *
  4775. + * You should have received a copy of the GNU General Public License
  4776. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  4777. + */
  4778. +
  4779. +/*
  4780. + * lookup and dentry operations
  4781. + */
  4782. +
  4783. +#include <linux/namei.h>
  4784. +#include "aufs.h"
  4785. +
  4786. +#define AuLkup_ALLOW_NEG 1
  4787. +#define AuLkup_IGNORE_PERM (1 << 1)
  4788. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  4789. +#define au_fset_lkup(flags, name) \
  4790. + do { (flags) |= AuLkup_##name; } while (0)
  4791. +#define au_fclr_lkup(flags, name) \
  4792. + do { (flags) &= ~AuLkup_##name; } while (0)
  4793. +
  4794. +struct au_do_lookup_args {
  4795. + unsigned int flags;
  4796. + mode_t type;
  4797. +};
  4798. +
  4799. +/*
  4800. + * returns positive/negative dentry, NULL or an error.
  4801. + * NULL means whiteout-ed or not-found.
  4802. + */
  4803. +static struct dentry*
  4804. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  4805. + aufs_bindex_t bindex, struct qstr *wh_name,
  4806. + struct au_do_lookup_args *args)
  4807. +{
  4808. + struct dentry *h_dentry;
  4809. + struct inode *h_inode;
  4810. + struct au_branch *br;
  4811. + int wh_found, opq;
  4812. + unsigned char wh_able;
  4813. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  4814. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  4815. + IGNORE_PERM);
  4816. +
  4817. + wh_found = 0;
  4818. + br = au_sbr(dentry->d_sb, bindex);
  4819. + wh_able = !!au_br_whable(br->br_perm);
  4820. + if (wh_able)
  4821. + wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
  4822. + h_dentry = ERR_PTR(wh_found);
  4823. + if (!wh_found)
  4824. + goto real_lookup;
  4825. + if (unlikely(wh_found < 0))
  4826. + goto out;
  4827. +
  4828. + /* We found a whiteout */
  4829. + /* au_set_dbend(dentry, bindex); */
  4830. + au_set_dbwh(dentry, bindex);
  4831. + if (!allow_neg)
  4832. + return NULL; /* success */
  4833. +
  4834. +real_lookup:
  4835. + if (!ignore_perm)
  4836. + h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  4837. + else
  4838. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  4839. + if (IS_ERR(h_dentry)) {
  4840. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  4841. + && !allow_neg)
  4842. + h_dentry = NULL;
  4843. + goto out;
  4844. + }
  4845. +
  4846. + h_inode = h_dentry->d_inode;
  4847. + if (!h_inode) {
  4848. + if (!allow_neg)
  4849. + goto out_neg;
  4850. + } else if (wh_found
  4851. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  4852. + goto out_neg;
  4853. +
  4854. + if (au_dbend(dentry) <= bindex)
  4855. + au_set_dbend(dentry, bindex);
  4856. + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
  4857. + au_set_dbstart(dentry, bindex);
  4858. + au_set_h_dptr(dentry, bindex, h_dentry);
  4859. +
  4860. + if (!d_is_dir(h_dentry)
  4861. + || !wh_able
  4862. + || (d_is_positive(dentry) && !d_is_dir(dentry)))
  4863. + goto out; /* success */
  4864. +
  4865. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  4866. + opq = au_diropq_test(h_dentry);
  4867. + mutex_unlock(&h_inode->i_mutex);
  4868. + if (opq > 0)
  4869. + au_set_dbdiropq(dentry, bindex);
  4870. + else if (unlikely(opq < 0)) {
  4871. + au_set_h_dptr(dentry, bindex, NULL);
  4872. + h_dentry = ERR_PTR(opq);
  4873. + }
  4874. + goto out;
  4875. +
  4876. +out_neg:
  4877. + dput(h_dentry);
  4878. + h_dentry = NULL;
  4879. +out:
  4880. + return h_dentry;
  4881. +}
  4882. +
  4883. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  4884. +{
  4885. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  4886. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  4887. + return -EPERM;
  4888. + return 0;
  4889. +}
  4890. +
  4891. +/*
  4892. + * returns the number of lower positive dentries,
  4893. + * otherwise an error.
  4894. + * can be called at unlinking with @type is zero.
  4895. + */
  4896. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
  4897. +{
  4898. + int npositive, err;
  4899. + aufs_bindex_t bindex, btail, bdiropq;
  4900. + unsigned char isdir, dirperm1;
  4901. + struct qstr whname;
  4902. + struct au_do_lookup_args args = {
  4903. + .flags = 0,
  4904. + .type = type
  4905. + };
  4906. + const struct qstr *name = &dentry->d_name;
  4907. + struct dentry *parent;
  4908. + struct inode *inode;
  4909. + struct super_block *sb;
  4910. +
  4911. + sb = dentry->d_sb;
  4912. + err = au_test_shwh(sb, name);
  4913. + if (unlikely(err))
  4914. + goto out;
  4915. +
  4916. + err = au_wh_name_alloc(&whname, name);
  4917. + if (unlikely(err))
  4918. + goto out;
  4919. +
  4920. + inode = dentry->d_inode;
  4921. + isdir = !!d_is_dir(dentry);
  4922. + if (!type)
  4923. + au_fset_lkup(args.flags, ALLOW_NEG);
  4924. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  4925. +
  4926. + npositive = 0;
  4927. + parent = dget_parent(dentry);
  4928. + btail = au_dbtaildir(parent);
  4929. + for (bindex = bstart; bindex <= btail; bindex++) {
  4930. + struct dentry *h_parent, *h_dentry;
  4931. + struct inode *h_inode, *h_dir;
  4932. +
  4933. + h_dentry = au_h_dptr(dentry, bindex);
  4934. + if (h_dentry) {
  4935. + if (h_dentry->d_inode)
  4936. + npositive++;
  4937. + if (type != S_IFDIR)
  4938. + break;
  4939. + continue;
  4940. + }
  4941. + h_parent = au_h_dptr(parent, bindex);
  4942. + if (!h_parent || !d_is_dir(h_parent))
  4943. + continue;
  4944. +
  4945. + h_dir = h_parent->d_inode;
  4946. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  4947. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
  4948. + &args);
  4949. + mutex_unlock(&h_dir->i_mutex);
  4950. + err = PTR_ERR(h_dentry);
  4951. + if (IS_ERR(h_dentry))
  4952. + goto out_parent;
  4953. + if (h_dentry)
  4954. + au_fclr_lkup(args.flags, ALLOW_NEG);
  4955. + if (dirperm1)
  4956. + au_fset_lkup(args.flags, IGNORE_PERM);
  4957. +
  4958. + if (au_dbwh(dentry) >= 0)
  4959. + break;
  4960. + if (!h_dentry)
  4961. + continue;
  4962. + h_inode = h_dentry->d_inode;
  4963. + if (!h_inode)
  4964. + continue;
  4965. + npositive++;
  4966. + if (!args.type)
  4967. + args.type = h_inode->i_mode & S_IFMT;
  4968. + if (args.type != S_IFDIR)
  4969. + break;
  4970. + else if (isdir) {
  4971. + /* the type of lower may be different */
  4972. + bdiropq = au_dbdiropq(dentry);
  4973. + if (bdiropq >= 0 && bdiropq <= bindex)
  4974. + break;
  4975. + }
  4976. + }
  4977. +
  4978. + if (npositive) {
  4979. + AuLabel(positive);
  4980. + au_update_dbstart(dentry);
  4981. + }
  4982. + err = npositive;
  4983. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  4984. + && au_dbstart(dentry) < 0)) {
  4985. + err = -EIO;
  4986. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  4987. + dentry, err);
  4988. + }
  4989. +
  4990. +out_parent:
  4991. + dput(parent);
  4992. + kfree(whname.name);
  4993. +out:
  4994. + return err;
  4995. +}
  4996. +
  4997. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  4998. +{
  4999. + struct dentry *dentry;
  5000. + int wkq_err;
  5001. +
  5002. + if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
  5003. + dentry = vfsub_lkup_one(name, parent);
  5004. + else {
  5005. + struct vfsub_lkup_one_args args = {
  5006. + .errp = &dentry,
  5007. + .name = name,
  5008. + .parent = parent
  5009. + };
  5010. +
  5011. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  5012. + if (unlikely(wkq_err))
  5013. + dentry = ERR_PTR(wkq_err);
  5014. + }
  5015. +
  5016. + return dentry;
  5017. +}
  5018. +
  5019. +/*
  5020. + * lookup @dentry on @bindex which should be negative.
  5021. + */
  5022. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  5023. +{
  5024. + int err;
  5025. + struct dentry *parent, *h_parent, *h_dentry;
  5026. + struct au_branch *br;
  5027. +
  5028. + parent = dget_parent(dentry);
  5029. + h_parent = au_h_dptr(parent, bindex);
  5030. + br = au_sbr(dentry->d_sb, bindex);
  5031. + if (wh)
  5032. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  5033. + else
  5034. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  5035. + err = PTR_ERR(h_dentry);
  5036. + if (IS_ERR(h_dentry))
  5037. + goto out;
  5038. + if (unlikely(h_dentry->d_inode)) {
  5039. + err = -EIO;
  5040. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  5041. + dput(h_dentry);
  5042. + goto out;
  5043. + }
  5044. +
  5045. + err = 0;
  5046. + if (bindex < au_dbstart(dentry))
  5047. + au_set_dbstart(dentry, bindex);
  5048. + if (au_dbend(dentry) < bindex)
  5049. + au_set_dbend(dentry, bindex);
  5050. + au_set_h_dptr(dentry, bindex, h_dentry);
  5051. +
  5052. +out:
  5053. + dput(parent);
  5054. + return err;
  5055. +}
  5056. +
  5057. +/* ---------------------------------------------------------------------- */
  5058. +
  5059. +/* subset of struct inode */
  5060. +struct au_iattr {
  5061. + unsigned long i_ino;
  5062. + /* unsigned int i_nlink; */
  5063. + kuid_t i_uid;
  5064. + kgid_t i_gid;
  5065. + u64 i_version;
  5066. +/*
  5067. + loff_t i_size;
  5068. + blkcnt_t i_blocks;
  5069. +*/
  5070. + umode_t i_mode;
  5071. +};
  5072. +
  5073. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  5074. +{
  5075. + ia->i_ino = h_inode->i_ino;
  5076. + /* ia->i_nlink = h_inode->i_nlink; */
  5077. + ia->i_uid = h_inode->i_uid;
  5078. + ia->i_gid = h_inode->i_gid;
  5079. + ia->i_version = h_inode->i_version;
  5080. +/*
  5081. + ia->i_size = h_inode->i_size;
  5082. + ia->i_blocks = h_inode->i_blocks;
  5083. +*/
  5084. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  5085. +}
  5086. +
  5087. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  5088. +{
  5089. + return ia->i_ino != h_inode->i_ino
  5090. + /* || ia->i_nlink != h_inode->i_nlink */
  5091. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  5092. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  5093. + || ia->i_version != h_inode->i_version
  5094. +/*
  5095. + || ia->i_size != h_inode->i_size
  5096. + || ia->i_blocks != h_inode->i_blocks
  5097. +*/
  5098. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  5099. +}
  5100. +
  5101. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  5102. + struct au_branch *br)
  5103. +{
  5104. + int err;
  5105. + struct au_iattr ia;
  5106. + struct inode *h_inode;
  5107. + struct dentry *h_d;
  5108. + struct super_block *h_sb;
  5109. +
  5110. + err = 0;
  5111. + memset(&ia, -1, sizeof(ia));
  5112. + h_sb = h_dentry->d_sb;
  5113. + h_inode = h_dentry->d_inode;
  5114. + if (h_inode)
  5115. + au_iattr_save(&ia, h_inode);
  5116. + else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  5117. + /* nfs d_revalidate may return 0 for negative dentry */
  5118. + /* fuse d_revalidate always return 0 for negative dentry */
  5119. + goto out;
  5120. +
  5121. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  5122. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  5123. + err = PTR_ERR(h_d);
  5124. + if (IS_ERR(h_d))
  5125. + goto out;
  5126. +
  5127. + err = 0;
  5128. + if (unlikely(h_d != h_dentry
  5129. + || h_d->d_inode != h_inode
  5130. + || (h_inode && au_iattr_test(&ia, h_inode))))
  5131. + err = au_busy_or_stale();
  5132. + dput(h_d);
  5133. +
  5134. +out:
  5135. + AuTraceErr(err);
  5136. + return err;
  5137. +}
  5138. +
  5139. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  5140. + struct dentry *h_parent, struct au_branch *br)
  5141. +{
  5142. + int err;
  5143. +
  5144. + err = 0;
  5145. + if (udba == AuOpt_UDBA_REVAL
  5146. + && !au_test_fs_remote(h_dentry->d_sb)) {
  5147. + IMustLock(h_dir);
  5148. + err = (h_dentry->d_parent->d_inode != h_dir);
  5149. + } else if (udba != AuOpt_UDBA_NONE)
  5150. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  5151. +
  5152. + return err;
  5153. +}
  5154. +
  5155. +/* ---------------------------------------------------------------------- */
  5156. +
  5157. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  5158. +{
  5159. + int err;
  5160. + aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
  5161. + struct au_hdentry tmp, *p, *q;
  5162. + struct au_dinfo *dinfo;
  5163. + struct super_block *sb;
  5164. +
  5165. + DiMustWriteLock(dentry);
  5166. +
  5167. + sb = dentry->d_sb;
  5168. + dinfo = au_di(dentry);
  5169. + bend = dinfo->di_bend;
  5170. + bwh = dinfo->di_bwh;
  5171. + bdiropq = dinfo->di_bdiropq;
  5172. + p = dinfo->di_hdentry + dinfo->di_bstart;
  5173. + for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
  5174. + if (!p->hd_dentry)
  5175. + continue;
  5176. +
  5177. + new_bindex = au_br_index(sb, p->hd_id);
  5178. + if (new_bindex == bindex)
  5179. + continue;
  5180. +
  5181. + if (dinfo->di_bwh == bindex)
  5182. + bwh = new_bindex;
  5183. + if (dinfo->di_bdiropq == bindex)
  5184. + bdiropq = new_bindex;
  5185. + if (new_bindex < 0) {
  5186. + au_hdput(p);
  5187. + p->hd_dentry = NULL;
  5188. + continue;
  5189. + }
  5190. +
  5191. + /* swap two lower dentries, and loop again */
  5192. + q = dinfo->di_hdentry + new_bindex;
  5193. + tmp = *q;
  5194. + *q = *p;
  5195. + *p = tmp;
  5196. + if (tmp.hd_dentry) {
  5197. + bindex--;
  5198. + p--;
  5199. + }
  5200. + }
  5201. +
  5202. + dinfo->di_bwh = -1;
  5203. + if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
  5204. + dinfo->di_bwh = bwh;
  5205. +
  5206. + dinfo->di_bdiropq = -1;
  5207. + if (bdiropq >= 0
  5208. + && bdiropq <= au_sbend(sb)
  5209. + && au_sbr_whable(sb, bdiropq))
  5210. + dinfo->di_bdiropq = bdiropq;
  5211. +
  5212. + err = -EIO;
  5213. + dinfo->di_bstart = -1;
  5214. + dinfo->di_bend = -1;
  5215. + bend = au_dbend(parent);
  5216. + p = dinfo->di_hdentry;
  5217. + for (bindex = 0; bindex <= bend; bindex++, p++)
  5218. + if (p->hd_dentry) {
  5219. + dinfo->di_bstart = bindex;
  5220. + break;
  5221. + }
  5222. +
  5223. + if (dinfo->di_bstart >= 0) {
  5224. + p = dinfo->di_hdentry + bend;
  5225. + for (bindex = bend; bindex >= 0; bindex--, p--)
  5226. + if (p->hd_dentry) {
  5227. + dinfo->di_bend = bindex;
  5228. + err = 0;
  5229. + break;
  5230. + }
  5231. + }
  5232. +
  5233. + return err;
  5234. +}
  5235. +
  5236. +static void au_do_hide(struct dentry *dentry)
  5237. +{
  5238. + struct inode *inode;
  5239. +
  5240. + inode = dentry->d_inode;
  5241. + if (inode) {
  5242. + if (!S_ISDIR(inode->i_mode)) {
  5243. + if (inode->i_nlink && !d_unhashed(dentry))
  5244. + drop_nlink(inode);
  5245. + } else {
  5246. + clear_nlink(inode);
  5247. + /* stop next lookup */
  5248. + inode->i_flags |= S_DEAD;
  5249. + }
  5250. + smp_mb(); /* necessary? */
  5251. + }
  5252. + d_drop(dentry);
  5253. +}
  5254. +
  5255. +static int au_hide_children(struct dentry *parent)
  5256. +{
  5257. + int err, i, j, ndentry;
  5258. + struct au_dcsub_pages dpages;
  5259. + struct au_dpage *dpage;
  5260. + struct dentry *dentry;
  5261. +
  5262. + err = au_dpages_init(&dpages, GFP_NOFS);
  5263. + if (unlikely(err))
  5264. + goto out;
  5265. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  5266. + if (unlikely(err))
  5267. + goto out_dpages;
  5268. +
  5269. + /* in reverse order */
  5270. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  5271. + dpage = dpages.dpages + i;
  5272. + ndentry = dpage->ndentry;
  5273. + for (j = ndentry - 1; j >= 0; j--) {
  5274. + dentry = dpage->dentries[j];
  5275. + if (dentry != parent)
  5276. + au_do_hide(dentry);
  5277. + }
  5278. + }
  5279. +
  5280. +out_dpages:
  5281. + au_dpages_free(&dpages);
  5282. +out:
  5283. + return err;
  5284. +}
  5285. +
  5286. +static void au_hide(struct dentry *dentry)
  5287. +{
  5288. + int err;
  5289. +
  5290. + AuDbgDentry(dentry);
  5291. + if (d_is_dir(dentry)) {
  5292. + /* shrink_dcache_parent(dentry); */
  5293. + err = au_hide_children(dentry);
  5294. + if (unlikely(err))
  5295. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  5296. + dentry, err);
  5297. + }
  5298. + au_do_hide(dentry);
  5299. +}
  5300. +
  5301. +/*
  5302. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  5303. + *
  5304. + * a dirty branch is added
  5305. + * - on the top of layers
  5306. + * - in the middle of layers
  5307. + * - to the bottom of layers
  5308. + *
  5309. + * on the added branch there exists
  5310. + * - a whiteout
  5311. + * - a diropq
  5312. + * - a same named entry
  5313. + * + exist
  5314. + * * negative --> positive
  5315. + * * positive --> positive
  5316. + * - type is unchanged
  5317. + * - type is changed
  5318. + * + doesn't exist
  5319. + * * negative --> negative
  5320. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  5321. + * - none
  5322. + */
  5323. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  5324. + struct au_dinfo *tmp)
  5325. +{
  5326. + int err;
  5327. + aufs_bindex_t bindex, bend;
  5328. + struct {
  5329. + struct dentry *dentry;
  5330. + struct inode *inode;
  5331. + mode_t mode;
  5332. + } orig_h, tmp_h;
  5333. + struct au_hdentry *hd;
  5334. + struct inode *inode, *h_inode;
  5335. + struct dentry *h_dentry;
  5336. +
  5337. + err = 0;
  5338. + AuDebugOn(dinfo->di_bstart < 0);
  5339. + orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
  5340. + orig_h.inode = orig_h.dentry->d_inode;
  5341. + orig_h.mode = 0;
  5342. + if (orig_h.inode)
  5343. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  5344. + memset(&tmp_h, 0, sizeof(tmp_h));
  5345. + if (tmp->di_bstart >= 0) {
  5346. + tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
  5347. + tmp_h.inode = tmp_h.dentry->d_inode;
  5348. + if (tmp_h.inode)
  5349. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  5350. + }
  5351. +
  5352. + inode = dentry->d_inode;
  5353. + if (!orig_h.inode) {
  5354. + AuDbg("nagative originally\n");
  5355. + if (inode) {
  5356. + au_hide(dentry);
  5357. + goto out;
  5358. + }
  5359. + AuDebugOn(inode);
  5360. + AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
  5361. + AuDebugOn(dinfo->di_bdiropq != -1);
  5362. +
  5363. + if (!tmp_h.inode) {
  5364. + AuDbg("negative --> negative\n");
  5365. + /* should have only one negative lower */
  5366. + if (tmp->di_bstart >= 0
  5367. + && tmp->di_bstart < dinfo->di_bstart) {
  5368. + AuDebugOn(tmp->di_bstart != tmp->di_bend);
  5369. + AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
  5370. + au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
  5371. + au_di_cp(dinfo, tmp);
  5372. + hd = tmp->di_hdentry + tmp->di_bstart;
  5373. + au_set_h_dptr(dentry, tmp->di_bstart,
  5374. + dget(hd->hd_dentry));
  5375. + }
  5376. + au_dbg_verify_dinode(dentry);
  5377. + } else {
  5378. + AuDbg("negative --> positive\n");
  5379. + /*
  5380. + * similar to the behaviour of creating with bypassing
  5381. + * aufs.
  5382. + * unhash it in order to force an error in the
  5383. + * succeeding create operation.
  5384. + * we should not set S_DEAD here.
  5385. + */
  5386. + d_drop(dentry);
  5387. + /* au_di_swap(tmp, dinfo); */
  5388. + au_dbg_verify_dinode(dentry);
  5389. + }
  5390. + } else {
  5391. + AuDbg("positive originally\n");
  5392. + /* inode may be NULL */
  5393. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  5394. + if (!tmp_h.inode) {
  5395. + AuDbg("positive --> negative\n");
  5396. + /* or bypassing aufs */
  5397. + au_hide(dentry);
  5398. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
  5399. + dinfo->di_bwh = tmp->di_bwh;
  5400. + if (inode)
  5401. + err = au_refresh_hinode_self(inode);
  5402. + au_dbg_verify_dinode(dentry);
  5403. + } else if (orig_h.mode == tmp_h.mode) {
  5404. + AuDbg("positive --> positive, same type\n");
  5405. + if (!S_ISDIR(orig_h.mode)
  5406. + && dinfo->di_bstart > tmp->di_bstart) {
  5407. + /*
  5408. + * similar to the behaviour of removing and
  5409. + * creating.
  5410. + */
  5411. + au_hide(dentry);
  5412. + if (inode)
  5413. + err = au_refresh_hinode_self(inode);
  5414. + au_dbg_verify_dinode(dentry);
  5415. + } else {
  5416. + /* fill empty slots */
  5417. + if (dinfo->di_bstart > tmp->di_bstart)
  5418. + dinfo->di_bstart = tmp->di_bstart;
  5419. + if (dinfo->di_bend < tmp->di_bend)
  5420. + dinfo->di_bend = tmp->di_bend;
  5421. + dinfo->di_bwh = tmp->di_bwh;
  5422. + dinfo->di_bdiropq = tmp->di_bdiropq;
  5423. + hd = tmp->di_hdentry;
  5424. + bend = dinfo->di_bend;
  5425. + for (bindex = tmp->di_bstart; bindex <= bend;
  5426. + bindex++) {
  5427. + if (au_h_dptr(dentry, bindex))
  5428. + continue;
  5429. + h_dentry = hd[bindex].hd_dentry;
  5430. + if (!h_dentry)
  5431. + continue;
  5432. + h_inode = h_dentry->d_inode;
  5433. + AuDebugOn(!h_inode);
  5434. + AuDebugOn(orig_h.mode
  5435. + != (h_inode->i_mode
  5436. + & S_IFMT));
  5437. + au_set_h_dptr(dentry, bindex,
  5438. + dget(h_dentry));
  5439. + }
  5440. + err = au_refresh_hinode(inode, dentry);
  5441. + au_dbg_verify_dinode(dentry);
  5442. + }
  5443. + } else {
  5444. + AuDbg("positive --> positive, different type\n");
  5445. + /* similar to the behaviour of removing and creating */
  5446. + au_hide(dentry);
  5447. + if (inode)
  5448. + err = au_refresh_hinode_self(inode);
  5449. + au_dbg_verify_dinode(dentry);
  5450. + }
  5451. + }
  5452. +
  5453. +out:
  5454. + return err;
  5455. +}
  5456. +
  5457. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  5458. +{
  5459. + int err, ebrange;
  5460. + unsigned int sigen;
  5461. + struct au_dinfo *dinfo, *tmp;
  5462. + struct super_block *sb;
  5463. + struct inode *inode;
  5464. +
  5465. + DiMustWriteLock(dentry);
  5466. + AuDebugOn(IS_ROOT(dentry));
  5467. + AuDebugOn(!parent->d_inode);
  5468. +
  5469. + sb = dentry->d_sb;
  5470. + inode = dentry->d_inode;
  5471. + sigen = au_sigen(sb);
  5472. + err = au_digen_test(parent, sigen);
  5473. + if (unlikely(err))
  5474. + goto out;
  5475. +
  5476. + dinfo = au_di(dentry);
  5477. + err = au_di_realloc(dinfo, au_sbend(sb) + 1);
  5478. + if (unlikely(err))
  5479. + goto out;
  5480. + ebrange = au_dbrange_test(dentry);
  5481. + if (!ebrange)
  5482. + ebrange = au_do_refresh_hdentry(dentry, parent);
  5483. +
  5484. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  5485. + AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
  5486. + if (inode)
  5487. + err = au_refresh_hinode_self(inode);
  5488. + au_dbg_verify_dinode(dentry);
  5489. + if (!err)
  5490. + goto out_dgen; /* success */
  5491. + goto out;
  5492. + }
  5493. +
  5494. + /* temporary dinfo */
  5495. + AuDbgDentry(dentry);
  5496. + err = -ENOMEM;
  5497. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  5498. + if (unlikely(!tmp))
  5499. + goto out;
  5500. + au_di_swap(tmp, dinfo);
  5501. + /* returns the number of positive dentries */
  5502. + /*
  5503. + * if current working dir is removed, it returns an error.
  5504. + * but the dentry is legal.
  5505. + */
  5506. + err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
  5507. + AuDbgDentry(dentry);
  5508. + au_di_swap(tmp, dinfo);
  5509. + if (err == -ENOENT)
  5510. + err = 0;
  5511. + if (err >= 0) {
  5512. + /* compare/refresh by dinfo */
  5513. + AuDbgDentry(dentry);
  5514. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  5515. + au_dbg_verify_dinode(dentry);
  5516. + AuTraceErr(err);
  5517. + }
  5518. + au_rw_write_unlock(&tmp->di_rwsem);
  5519. + au_di_free(tmp);
  5520. + if (unlikely(err))
  5521. + goto out;
  5522. +
  5523. +out_dgen:
  5524. + au_update_digen(dentry);
  5525. +out:
  5526. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  5527. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  5528. + AuDbgDentry(dentry);
  5529. + }
  5530. + AuTraceErr(err);
  5531. + return err;
  5532. +}
  5533. +
  5534. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  5535. + struct dentry *dentry, aufs_bindex_t bindex)
  5536. +{
  5537. + int err, valid;
  5538. +
  5539. + err = 0;
  5540. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  5541. + goto out;
  5542. +
  5543. + AuDbg("b%d\n", bindex);
  5544. + /*
  5545. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  5546. + * due to whiteout and branch permission.
  5547. + */
  5548. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  5549. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  5550. + /* it may return tri-state */
  5551. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  5552. +
  5553. + if (unlikely(valid < 0))
  5554. + err = valid;
  5555. + else if (!valid)
  5556. + err = -EINVAL;
  5557. +
  5558. +out:
  5559. + AuTraceErr(err);
  5560. + return err;
  5561. +}
  5562. +
  5563. +/* todo: remove this */
  5564. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  5565. + unsigned int flags, int do_udba)
  5566. +{
  5567. + int err;
  5568. + umode_t mode, h_mode;
  5569. + aufs_bindex_t bindex, btail, bstart, ibs, ibe;
  5570. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  5571. + struct inode *h_inode, *h_cached_inode;
  5572. + struct dentry *h_dentry;
  5573. + struct qstr *name, *h_name;
  5574. +
  5575. + err = 0;
  5576. + plus = 0;
  5577. + mode = 0;
  5578. + ibs = -1;
  5579. + ibe = -1;
  5580. + unhashed = !!d_unhashed(dentry);
  5581. + is_root = !!IS_ROOT(dentry);
  5582. + name = &dentry->d_name;
  5583. + tmpfile = au_di(dentry)->di_tmpfile;
  5584. +
  5585. + /*
  5586. + * Theoretically, REVAL test should be unnecessary in case of
  5587. + * {FS,I}NOTIFY.
  5588. + * But {fs,i}notify doesn't fire some necessary events,
  5589. + * IN_ATTRIB for atime/nlink/pageio
  5590. + * Let's do REVAL test too.
  5591. + */
  5592. + if (do_udba && inode) {
  5593. + mode = (inode->i_mode & S_IFMT);
  5594. + plus = (inode->i_nlink > 0);
  5595. + ibs = au_ibstart(inode);
  5596. + ibe = au_ibend(inode);
  5597. + }
  5598. +
  5599. + bstart = au_dbstart(dentry);
  5600. + btail = bstart;
  5601. + if (inode && S_ISDIR(inode->i_mode))
  5602. + btail = au_dbtaildir(dentry);
  5603. + for (bindex = bstart; bindex <= btail; bindex++) {
  5604. + h_dentry = au_h_dptr(dentry, bindex);
  5605. + if (!h_dentry)
  5606. + continue;
  5607. +
  5608. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  5609. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  5610. + spin_lock(&h_dentry->d_lock);
  5611. + h_name = &h_dentry->d_name;
  5612. + if (unlikely(do_udba
  5613. + && !is_root
  5614. + && ((!h_nfs
  5615. + && (unhashed != !!d_unhashed(h_dentry)
  5616. + || (!tmpfile
  5617. + && !au_qstreq(name, h_name))
  5618. + ))
  5619. + || (h_nfs
  5620. + && !(flags & LOOKUP_OPEN)
  5621. + && (h_dentry->d_flags
  5622. + & DCACHE_NFSFS_RENAMED)))
  5623. + )) {
  5624. + int h_unhashed;
  5625. +
  5626. + h_unhashed = d_unhashed(h_dentry);
  5627. + spin_unlock(&h_dentry->d_lock);
  5628. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  5629. + unhashed, h_unhashed, dentry, h_dentry);
  5630. + goto err;
  5631. + }
  5632. + spin_unlock(&h_dentry->d_lock);
  5633. +
  5634. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  5635. + if (unlikely(err))
  5636. + /* do not goto err, to keep the errno */
  5637. + break;
  5638. +
  5639. + /* todo: plink too? */
  5640. + if (!do_udba)
  5641. + continue;
  5642. +
  5643. + /* UDBA tests */
  5644. + h_inode = h_dentry->d_inode;
  5645. + if (unlikely(!!inode != !!h_inode))
  5646. + goto err;
  5647. +
  5648. + h_plus = plus;
  5649. + h_mode = mode;
  5650. + h_cached_inode = h_inode;
  5651. + if (h_inode) {
  5652. + h_mode = (h_inode->i_mode & S_IFMT);
  5653. + h_plus = (h_inode->i_nlink > 0);
  5654. + }
  5655. + if (inode && ibs <= bindex && bindex <= ibe)
  5656. + h_cached_inode = au_h_iptr(inode, bindex);
  5657. +
  5658. + if (!h_nfs) {
  5659. + if (unlikely(plus != h_plus && !tmpfile))
  5660. + goto err;
  5661. + } else {
  5662. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  5663. + && !is_root
  5664. + && !IS_ROOT(h_dentry)
  5665. + && unhashed != d_unhashed(h_dentry)))
  5666. + goto err;
  5667. + }
  5668. + if (unlikely(mode != h_mode
  5669. + || h_cached_inode != h_inode))
  5670. + goto err;
  5671. + continue;
  5672. +
  5673. +err:
  5674. + err = -EINVAL;
  5675. + break;
  5676. + }
  5677. +
  5678. + AuTraceErr(err);
  5679. + return err;
  5680. +}
  5681. +
  5682. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  5683. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  5684. +{
  5685. + int err;
  5686. + struct dentry *parent;
  5687. +
  5688. + if (!au_digen_test(dentry, sigen))
  5689. + return 0;
  5690. +
  5691. + parent = dget_parent(dentry);
  5692. + di_read_lock_parent(parent, AuLock_IR);
  5693. + AuDebugOn(au_digen_test(parent, sigen));
  5694. + au_dbg_verify_gen(parent, sigen);
  5695. + err = au_refresh_dentry(dentry, parent);
  5696. + di_read_unlock(parent, AuLock_IR);
  5697. + dput(parent);
  5698. + AuTraceErr(err);
  5699. + return err;
  5700. +}
  5701. +
  5702. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  5703. +{
  5704. + int err;
  5705. + struct dentry *d, *parent;
  5706. + struct inode *inode;
  5707. +
  5708. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  5709. + return simple_reval_dpath(dentry, sigen);
  5710. +
  5711. + /* slow loop, keep it simple and stupid */
  5712. + /* cf: au_cpup_dirs() */
  5713. + err = 0;
  5714. + parent = NULL;
  5715. + while (au_digen_test(dentry, sigen)) {
  5716. + d = dentry;
  5717. + while (1) {
  5718. + dput(parent);
  5719. + parent = dget_parent(d);
  5720. + if (!au_digen_test(parent, sigen))
  5721. + break;
  5722. + d = parent;
  5723. + }
  5724. +
  5725. + inode = d->d_inode;
  5726. + if (d != dentry)
  5727. + di_write_lock_child2(d);
  5728. +
  5729. + /* someone might update our dentry while we were sleeping */
  5730. + if (au_digen_test(d, sigen)) {
  5731. + /*
  5732. + * todo: consolidate with simple_reval_dpath(),
  5733. + * do_refresh() and au_reval_for_attr().
  5734. + */
  5735. + di_read_lock_parent(parent, AuLock_IR);
  5736. + err = au_refresh_dentry(d, parent);
  5737. + di_read_unlock(parent, AuLock_IR);
  5738. + }
  5739. +
  5740. + if (d != dentry)
  5741. + di_write_unlock(d);
  5742. + dput(parent);
  5743. + if (unlikely(err))
  5744. + break;
  5745. + }
  5746. +
  5747. + return err;
  5748. +}
  5749. +
  5750. +/*
  5751. + * if valid returns 1, otherwise 0.
  5752. + */
  5753. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  5754. +{
  5755. + int valid, err;
  5756. + unsigned int sigen;
  5757. + unsigned char do_udba;
  5758. + struct super_block *sb;
  5759. + struct inode *inode;
  5760. +
  5761. + /* todo: support rcu-walk? */
  5762. + if (flags & LOOKUP_RCU)
  5763. + return -ECHILD;
  5764. +
  5765. + valid = 0;
  5766. + if (unlikely(!au_di(dentry)))
  5767. + goto out;
  5768. +
  5769. + valid = 1;
  5770. + sb = dentry->d_sb;
  5771. + /*
  5772. + * todo: very ugly
  5773. + * i_mutex of parent dir may be held,
  5774. + * but we should not return 'invalid' due to busy.
  5775. + */
  5776. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  5777. + if (unlikely(err)) {
  5778. + valid = err;
  5779. + AuTraceErr(err);
  5780. + goto out;
  5781. + }
  5782. + inode = dentry->d_inode;
  5783. + if (unlikely(inode && is_bad_inode(inode))) {
  5784. + err = -EINVAL;
  5785. + AuTraceErr(err);
  5786. + goto out_dgrade;
  5787. + }
  5788. + if (unlikely(au_dbrange_test(dentry))) {
  5789. + err = -EINVAL;
  5790. + AuTraceErr(err);
  5791. + goto out_dgrade;
  5792. + }
  5793. +
  5794. + sigen = au_sigen(sb);
  5795. + if (au_digen_test(dentry, sigen)) {
  5796. + AuDebugOn(IS_ROOT(dentry));
  5797. + err = au_reval_dpath(dentry, sigen);
  5798. + if (unlikely(err)) {
  5799. + AuTraceErr(err);
  5800. + goto out_dgrade;
  5801. + }
  5802. + }
  5803. + di_downgrade_lock(dentry, AuLock_IR);
  5804. +
  5805. + err = -EINVAL;
  5806. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  5807. + && inode
  5808. + && !(inode->i_state && I_LINKABLE)
  5809. + && (IS_DEADDIR(inode) || !inode->i_nlink))
  5810. + goto out_inval;
  5811. +
  5812. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  5813. + if (do_udba && inode) {
  5814. + aufs_bindex_t bstart = au_ibstart(inode);
  5815. + struct inode *h_inode;
  5816. +
  5817. + if (bstart >= 0) {
  5818. + h_inode = au_h_iptr(inode, bstart);
  5819. + if (h_inode && au_test_higen(inode, h_inode))
  5820. + goto out_inval;
  5821. + }
  5822. + }
  5823. +
  5824. + err = h_d_revalidate(dentry, inode, flags, do_udba);
  5825. + if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
  5826. + err = -EIO;
  5827. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  5828. + dentry, err);
  5829. + }
  5830. + goto out_inval;
  5831. +
  5832. +out_dgrade:
  5833. + di_downgrade_lock(dentry, AuLock_IR);
  5834. +out_inval:
  5835. + aufs_read_unlock(dentry, AuLock_IR);
  5836. + AuTraceErr(err);
  5837. + valid = !err;
  5838. +out:
  5839. + if (!valid) {
  5840. + AuDbg("%pd invalid, %d\n", dentry, valid);
  5841. + d_drop(dentry);
  5842. + }
  5843. + return valid;
  5844. +}
  5845. +
  5846. +static void aufs_d_release(struct dentry *dentry)
  5847. +{
  5848. + if (au_di(dentry)) {
  5849. + au_di_fin(dentry);
  5850. + au_hn_di_reinit(dentry);
  5851. + }
  5852. +}
  5853. +
  5854. +const struct dentry_operations aufs_dop = {
  5855. + .d_revalidate = aufs_d_revalidate,
  5856. + .d_weak_revalidate = aufs_d_revalidate,
  5857. + .d_release = aufs_d_release
  5858. +};
  5859. diff -Nur linux-4.0.5.orig/fs/aufs/dentry.h linux-4.0.5/fs/aufs/dentry.h
  5860. --- linux-4.0.5.orig/fs/aufs/dentry.h 1969-12-31 18:00:00.000000000 -0600
  5861. +++ linux-4.0.5/fs/aufs/dentry.h 2015-06-21 14:07:48.682432781 -0500
  5862. @@ -0,0 +1,233 @@
  5863. +/*
  5864. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5865. + *
  5866. + * This program, aufs is free software; you can redistribute it and/or modify
  5867. + * it under the terms of the GNU General Public License as published by
  5868. + * the Free Software Foundation; either version 2 of the License, or
  5869. + * (at your option) any later version.
  5870. + *
  5871. + * This program is distributed in the hope that it will be useful,
  5872. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5873. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5874. + * GNU General Public License for more details.
  5875. + *
  5876. + * You should have received a copy of the GNU General Public License
  5877. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5878. + */
  5879. +
  5880. +/*
  5881. + * lookup and dentry operations
  5882. + */
  5883. +
  5884. +#ifndef __AUFS_DENTRY_H__
  5885. +#define __AUFS_DENTRY_H__
  5886. +
  5887. +#ifdef __KERNEL__
  5888. +
  5889. +#include <linux/dcache.h>
  5890. +#include "rwsem.h"
  5891. +
  5892. +struct au_hdentry {
  5893. + struct dentry *hd_dentry;
  5894. + aufs_bindex_t hd_id;
  5895. +};
  5896. +
  5897. +struct au_dinfo {
  5898. + atomic_t di_generation;
  5899. +
  5900. + struct au_rwsem di_rwsem;
  5901. + aufs_bindex_t di_bstart, di_bend, di_bwh, di_bdiropq;
  5902. + unsigned char di_tmpfile; /* to allow the different name */
  5903. + struct au_hdentry *di_hdentry;
  5904. +} ____cacheline_aligned_in_smp;
  5905. +
  5906. +/* ---------------------------------------------------------------------- */
  5907. +
  5908. +/* dentry.c */
  5909. +extern const struct dentry_operations aufs_dop;
  5910. +struct au_branch;
  5911. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  5912. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  5913. + struct dentry *h_parent, struct au_branch *br);
  5914. +
  5915. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
  5916. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  5917. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  5918. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  5919. +
  5920. +/* dinfo.c */
  5921. +void au_di_init_once(void *_di);
  5922. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  5923. +void au_di_free(struct au_dinfo *dinfo);
  5924. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  5925. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  5926. +int au_di_init(struct dentry *dentry);
  5927. +void au_di_fin(struct dentry *dentry);
  5928. +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
  5929. +
  5930. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  5931. +void di_read_unlock(struct dentry *d, int flags);
  5932. +void di_downgrade_lock(struct dentry *d, int flags);
  5933. +void di_write_lock(struct dentry *d, unsigned int lsc);
  5934. +void di_write_unlock(struct dentry *d);
  5935. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  5936. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  5937. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  5938. +
  5939. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  5940. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  5941. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  5942. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  5943. +
  5944. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  5945. + struct dentry *h_dentry);
  5946. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  5947. +int au_dbrange_test(struct dentry *dentry);
  5948. +void au_update_digen(struct dentry *dentry);
  5949. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  5950. +void au_update_dbstart(struct dentry *dentry);
  5951. +void au_update_dbend(struct dentry *dentry);
  5952. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  5953. +
  5954. +/* ---------------------------------------------------------------------- */
  5955. +
  5956. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  5957. +{
  5958. + return dentry->d_fsdata;
  5959. +}
  5960. +
  5961. +/* ---------------------------------------------------------------------- */
  5962. +
  5963. +/* lock subclass for dinfo */
  5964. +enum {
  5965. + AuLsc_DI_CHILD, /* child first */
  5966. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  5967. + AuLsc_DI_CHILD3, /* copyup dirs */
  5968. + AuLsc_DI_PARENT,
  5969. + AuLsc_DI_PARENT2,
  5970. + AuLsc_DI_PARENT3,
  5971. + AuLsc_DI_TMP /* temp for replacing dinfo */
  5972. +};
  5973. +
  5974. +/*
  5975. + * di_read_lock_child, di_write_lock_child,
  5976. + * di_read_lock_child2, di_write_lock_child2,
  5977. + * di_read_lock_child3, di_write_lock_child3,
  5978. + * di_read_lock_parent, di_write_lock_parent,
  5979. + * di_read_lock_parent2, di_write_lock_parent2,
  5980. + * di_read_lock_parent3, di_write_lock_parent3,
  5981. + */
  5982. +#define AuReadLockFunc(name, lsc) \
  5983. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  5984. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  5985. +
  5986. +#define AuWriteLockFunc(name, lsc) \
  5987. +static inline void di_write_lock_##name(struct dentry *d) \
  5988. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  5989. +
  5990. +#define AuRWLockFuncs(name, lsc) \
  5991. + AuReadLockFunc(name, lsc) \
  5992. + AuWriteLockFunc(name, lsc)
  5993. +
  5994. +AuRWLockFuncs(child, CHILD);
  5995. +AuRWLockFuncs(child2, CHILD2);
  5996. +AuRWLockFuncs(child3, CHILD3);
  5997. +AuRWLockFuncs(parent, PARENT);
  5998. +AuRWLockFuncs(parent2, PARENT2);
  5999. +AuRWLockFuncs(parent3, PARENT3);
  6000. +
  6001. +#undef AuReadLockFunc
  6002. +#undef AuWriteLockFunc
  6003. +#undef AuRWLockFuncs
  6004. +
  6005. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  6006. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  6007. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  6008. +
  6009. +/* ---------------------------------------------------------------------- */
  6010. +
  6011. +/* todo: memory barrier? */
  6012. +static inline unsigned int au_digen(struct dentry *d)
  6013. +{
  6014. + return atomic_read(&au_di(d)->di_generation);
  6015. +}
  6016. +
  6017. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  6018. +{
  6019. + hdentry->hd_dentry = NULL;
  6020. +}
  6021. +
  6022. +static inline void au_hdput(struct au_hdentry *hd)
  6023. +{
  6024. + if (hd)
  6025. + dput(hd->hd_dentry);
  6026. +}
  6027. +
  6028. +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
  6029. +{
  6030. + DiMustAnyLock(dentry);
  6031. + return au_di(dentry)->di_bstart;
  6032. +}
  6033. +
  6034. +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
  6035. +{
  6036. + DiMustAnyLock(dentry);
  6037. + return au_di(dentry)->di_bend;
  6038. +}
  6039. +
  6040. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  6041. +{
  6042. + DiMustAnyLock(dentry);
  6043. + return au_di(dentry)->di_bwh;
  6044. +}
  6045. +
  6046. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  6047. +{
  6048. + DiMustAnyLock(dentry);
  6049. + return au_di(dentry)->di_bdiropq;
  6050. +}
  6051. +
  6052. +/* todo: hard/soft set? */
  6053. +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
  6054. +{
  6055. + DiMustWriteLock(dentry);
  6056. + au_di(dentry)->di_bstart = bindex;
  6057. +}
  6058. +
  6059. +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
  6060. +{
  6061. + DiMustWriteLock(dentry);
  6062. + au_di(dentry)->di_bend = bindex;
  6063. +}
  6064. +
  6065. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  6066. +{
  6067. + DiMustWriteLock(dentry);
  6068. + /* dbwh can be outside of bstart - bend range */
  6069. + au_di(dentry)->di_bwh = bindex;
  6070. +}
  6071. +
  6072. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  6073. +{
  6074. + DiMustWriteLock(dentry);
  6075. + au_di(dentry)->di_bdiropq = bindex;
  6076. +}
  6077. +
  6078. +/* ---------------------------------------------------------------------- */
  6079. +
  6080. +#ifdef CONFIG_AUFS_HNOTIFY
  6081. +static inline void au_digen_dec(struct dentry *d)
  6082. +{
  6083. + atomic_dec(&au_di(d)->di_generation);
  6084. +}
  6085. +
  6086. +static inline void au_hn_di_reinit(struct dentry *dentry)
  6087. +{
  6088. + dentry->d_fsdata = NULL;
  6089. +}
  6090. +#else
  6091. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  6092. +#endif /* CONFIG_AUFS_HNOTIFY */
  6093. +
  6094. +#endif /* __KERNEL__ */
  6095. +#endif /* __AUFS_DENTRY_H__ */
  6096. diff -Nur linux-4.0.5.orig/fs/aufs/dinfo.c linux-4.0.5/fs/aufs/dinfo.c
  6097. --- linux-4.0.5.orig/fs/aufs/dinfo.c 1969-12-31 18:00:00.000000000 -0600
  6098. +++ linux-4.0.5/fs/aufs/dinfo.c 2015-06-21 14:07:48.682432781 -0500
  6099. @@ -0,0 +1,544 @@
  6100. +/*
  6101. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  6102. + *
  6103. + * This program, aufs is free software; you can redistribute it and/or modify
  6104. + * it under the terms of the GNU General Public License as published by
  6105. + * the Free Software Foundation; either version 2 of the License, or
  6106. + * (at your option) any later version.
  6107. + *
  6108. + * This program is distributed in the hope that it will be useful,
  6109. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6110. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6111. + * GNU General Public License for more details.
  6112. + *
  6113. + * You should have received a copy of the GNU General Public License
  6114. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6115. + */
  6116. +
  6117. +/*
  6118. + * dentry private data
  6119. + */
  6120. +
  6121. +#include "aufs.h"
  6122. +
  6123. +void au_di_init_once(void *_dinfo)
  6124. +{
  6125. + struct au_dinfo *dinfo = _dinfo;
  6126. + static struct lock_class_key aufs_di;
  6127. +
  6128. + au_rw_init(&dinfo->di_rwsem);
  6129. + au_rw_class(&dinfo->di_rwsem, &aufs_di);
  6130. +}
  6131. +
  6132. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  6133. +{
  6134. + struct au_dinfo *dinfo;
  6135. + int nbr, i;
  6136. +
  6137. + dinfo = au_cache_alloc_dinfo();
  6138. + if (unlikely(!dinfo))
  6139. + goto out;
  6140. +
  6141. + nbr = au_sbend(sb) + 1;
  6142. + if (nbr <= 0)
  6143. + nbr = 1;
  6144. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  6145. + if (dinfo->di_hdentry) {
  6146. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  6147. + dinfo->di_bstart = -1;
  6148. + dinfo->di_bend = -1;
  6149. + dinfo->di_bwh = -1;
  6150. + dinfo->di_bdiropq = -1;
  6151. + dinfo->di_tmpfile = 0;
  6152. + for (i = 0; i < nbr; i++)
  6153. + dinfo->di_hdentry[i].hd_id = -1;
  6154. + goto out;
  6155. + }
  6156. +
  6157. + au_cache_free_dinfo(dinfo);
  6158. + dinfo = NULL;
  6159. +
  6160. +out:
  6161. + return dinfo;
  6162. +}
  6163. +
  6164. +void au_di_free(struct au_dinfo *dinfo)
  6165. +{
  6166. + struct au_hdentry *p;
  6167. + aufs_bindex_t bend, bindex;
  6168. +
  6169. + /* dentry may not be revalidated */
  6170. + bindex = dinfo->di_bstart;
  6171. + if (bindex >= 0) {
  6172. + bend = dinfo->di_bend;
  6173. + p = dinfo->di_hdentry + bindex;
  6174. + while (bindex++ <= bend)
  6175. + au_hdput(p++);
  6176. + }
  6177. + kfree(dinfo->di_hdentry);
  6178. + au_cache_free_dinfo(dinfo);
  6179. +}
  6180. +
  6181. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  6182. +{
  6183. + struct au_hdentry *p;
  6184. + aufs_bindex_t bi;
  6185. +
  6186. + AuRwMustWriteLock(&a->di_rwsem);
  6187. + AuRwMustWriteLock(&b->di_rwsem);
  6188. +
  6189. +#define DiSwap(v, name) \
  6190. + do { \
  6191. + v = a->di_##name; \
  6192. + a->di_##name = b->di_##name; \
  6193. + b->di_##name = v; \
  6194. + } while (0)
  6195. +
  6196. + DiSwap(p, hdentry);
  6197. + DiSwap(bi, bstart);
  6198. + DiSwap(bi, bend);
  6199. + DiSwap(bi, bwh);
  6200. + DiSwap(bi, bdiropq);
  6201. + /* smp_mb(); */
  6202. +
  6203. +#undef DiSwap
  6204. +}
  6205. +
  6206. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  6207. +{
  6208. + AuRwMustWriteLock(&dst->di_rwsem);
  6209. + AuRwMustWriteLock(&src->di_rwsem);
  6210. +
  6211. + dst->di_bstart = src->di_bstart;
  6212. + dst->di_bend = src->di_bend;
  6213. + dst->di_bwh = src->di_bwh;
  6214. + dst->di_bdiropq = src->di_bdiropq;
  6215. + /* smp_mb(); */
  6216. +}
  6217. +
  6218. +int au_di_init(struct dentry *dentry)
  6219. +{
  6220. + int err;
  6221. + struct super_block *sb;
  6222. + struct au_dinfo *dinfo;
  6223. +
  6224. + err = 0;
  6225. + sb = dentry->d_sb;
  6226. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  6227. + if (dinfo) {
  6228. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  6229. + /* smp_mb(); */ /* atomic_set */
  6230. + dentry->d_fsdata = dinfo;
  6231. + } else
  6232. + err = -ENOMEM;
  6233. +
  6234. + return err;
  6235. +}
  6236. +
  6237. +void au_di_fin(struct dentry *dentry)
  6238. +{
  6239. + struct au_dinfo *dinfo;
  6240. +
  6241. + dinfo = au_di(dentry);
  6242. + AuRwDestroy(&dinfo->di_rwsem);
  6243. + au_di_free(dinfo);
  6244. +}
  6245. +
  6246. +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
  6247. +{
  6248. + int err, sz;
  6249. + struct au_hdentry *hdp;
  6250. +
  6251. + AuRwMustWriteLock(&dinfo->di_rwsem);
  6252. +
  6253. + err = -ENOMEM;
  6254. + sz = sizeof(*hdp) * (dinfo->di_bend + 1);
  6255. + if (!sz)
  6256. + sz = sizeof(*hdp);
  6257. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
  6258. + if (hdp) {
  6259. + dinfo->di_hdentry = hdp;
  6260. + err = 0;
  6261. + }
  6262. +
  6263. + return err;
  6264. +}
  6265. +
  6266. +/* ---------------------------------------------------------------------- */
  6267. +
  6268. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  6269. +{
  6270. + switch (lsc) {
  6271. + case AuLsc_DI_CHILD:
  6272. + ii_write_lock_child(inode);
  6273. + break;
  6274. + case AuLsc_DI_CHILD2:
  6275. + ii_write_lock_child2(inode);
  6276. + break;
  6277. + case AuLsc_DI_CHILD3:
  6278. + ii_write_lock_child3(inode);
  6279. + break;
  6280. + case AuLsc_DI_PARENT:
  6281. + ii_write_lock_parent(inode);
  6282. + break;
  6283. + case AuLsc_DI_PARENT2:
  6284. + ii_write_lock_parent2(inode);
  6285. + break;
  6286. + case AuLsc_DI_PARENT3:
  6287. + ii_write_lock_parent3(inode);
  6288. + break;
  6289. + default:
  6290. + BUG();
  6291. + }
  6292. +}
  6293. +
  6294. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  6295. +{
  6296. + switch (lsc) {
  6297. + case AuLsc_DI_CHILD:
  6298. + ii_read_lock_child(inode);
  6299. + break;
  6300. + case AuLsc_DI_CHILD2:
  6301. + ii_read_lock_child2(inode);
  6302. + break;
  6303. + case AuLsc_DI_CHILD3:
  6304. + ii_read_lock_child3(inode);
  6305. + break;
  6306. + case AuLsc_DI_PARENT:
  6307. + ii_read_lock_parent(inode);
  6308. + break;
  6309. + case AuLsc_DI_PARENT2:
  6310. + ii_read_lock_parent2(inode);
  6311. + break;
  6312. + case AuLsc_DI_PARENT3:
  6313. + ii_read_lock_parent3(inode);
  6314. + break;
  6315. + default:
  6316. + BUG();
  6317. + }
  6318. +}
  6319. +
  6320. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  6321. +{
  6322. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  6323. + if (d->d_inode) {
  6324. + if (au_ftest_lock(flags, IW))
  6325. + do_ii_write_lock(d->d_inode, lsc);
  6326. + else if (au_ftest_lock(flags, IR))
  6327. + do_ii_read_lock(d->d_inode, lsc);
  6328. + }
  6329. +}
  6330. +
  6331. +void di_read_unlock(struct dentry *d, int flags)
  6332. +{
  6333. + if (d->d_inode) {
  6334. + if (au_ftest_lock(flags, IW)) {
  6335. + au_dbg_verify_dinode(d);
  6336. + ii_write_unlock(d->d_inode);
  6337. + } else if (au_ftest_lock(flags, IR)) {
  6338. + au_dbg_verify_dinode(d);
  6339. + ii_read_unlock(d->d_inode);
  6340. + }
  6341. + }
  6342. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  6343. +}
  6344. +
  6345. +void di_downgrade_lock(struct dentry *d, int flags)
  6346. +{
  6347. + if (d->d_inode && au_ftest_lock(flags, IR))
  6348. + ii_downgrade_lock(d->d_inode);
  6349. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  6350. +}
  6351. +
  6352. +void di_write_lock(struct dentry *d, unsigned int lsc)
  6353. +{
  6354. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  6355. + if (d->d_inode)
  6356. + do_ii_write_lock(d->d_inode, lsc);
  6357. +}
  6358. +
  6359. +void di_write_unlock(struct dentry *d)
  6360. +{
  6361. + au_dbg_verify_dinode(d);
  6362. + if (d->d_inode)
  6363. + ii_write_unlock(d->d_inode);
  6364. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  6365. +}
  6366. +
  6367. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  6368. +{
  6369. + AuDebugOn(d1 == d2
  6370. + || d1->d_inode == d2->d_inode
  6371. + || d1->d_sb != d2->d_sb);
  6372. +
  6373. + if (isdir && au_test_subdir(d1, d2)) {
  6374. + di_write_lock_child(d1);
  6375. + di_write_lock_child2(d2);
  6376. + } else {
  6377. + /* there should be no races */
  6378. + di_write_lock_child(d2);
  6379. + di_write_lock_child2(d1);
  6380. + }
  6381. +}
  6382. +
  6383. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  6384. +{
  6385. + AuDebugOn(d1 == d2
  6386. + || d1->d_inode == d2->d_inode
  6387. + || d1->d_sb != d2->d_sb);
  6388. +
  6389. + if (isdir && au_test_subdir(d1, d2)) {
  6390. + di_write_lock_parent(d1);
  6391. + di_write_lock_parent2(d2);
  6392. + } else {
  6393. + /* there should be no races */
  6394. + di_write_lock_parent(d2);
  6395. + di_write_lock_parent2(d1);
  6396. + }
  6397. +}
  6398. +
  6399. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  6400. +{
  6401. + di_write_unlock(d1);
  6402. + if (d1->d_inode == d2->d_inode)
  6403. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  6404. + else
  6405. + di_write_unlock(d2);
  6406. +}
  6407. +
  6408. +/* ---------------------------------------------------------------------- */
  6409. +
  6410. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  6411. +{
  6412. + struct dentry *d;
  6413. +
  6414. + DiMustAnyLock(dentry);
  6415. +
  6416. + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
  6417. + return NULL;
  6418. + AuDebugOn(bindex < 0);
  6419. + d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
  6420. + AuDebugOn(d && au_dcount(d) <= 0);
  6421. + return d;
  6422. +}
  6423. +
  6424. +/*
  6425. + * extended version of au_h_dptr().
  6426. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  6427. + * error.
  6428. + */
  6429. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  6430. +{
  6431. + struct dentry *h_dentry;
  6432. + struct inode *inode, *h_inode;
  6433. +
  6434. + inode = dentry->d_inode;
  6435. + AuDebugOn(!inode);
  6436. +
  6437. + h_dentry = NULL;
  6438. + if (au_dbstart(dentry) <= bindex
  6439. + && bindex <= au_dbend(dentry))
  6440. + h_dentry = au_h_dptr(dentry, bindex);
  6441. + if (h_dentry && !au_d_linkable(h_dentry)) {
  6442. + dget(h_dentry);
  6443. + goto out; /* success */
  6444. + }
  6445. +
  6446. + AuDebugOn(bindex < au_ibstart(inode));
  6447. + AuDebugOn(au_ibend(inode) < bindex);
  6448. + h_inode = au_h_iptr(inode, bindex);
  6449. + h_dentry = d_find_alias(h_inode);
  6450. + if (h_dentry) {
  6451. + if (!IS_ERR(h_dentry)) {
  6452. + if (!au_d_linkable(h_dentry))
  6453. + goto out; /* success */
  6454. + dput(h_dentry);
  6455. + } else
  6456. + goto out;
  6457. + }
  6458. +
  6459. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  6460. + h_dentry = au_plink_lkup(inode, bindex);
  6461. + AuDebugOn(!h_dentry);
  6462. + if (!IS_ERR(h_dentry)) {
  6463. + if (!au_d_hashed_positive(h_dentry))
  6464. + goto out; /* success */
  6465. + dput(h_dentry);
  6466. + h_dentry = NULL;
  6467. + }
  6468. + }
  6469. +
  6470. +out:
  6471. + AuDbgDentry(h_dentry);
  6472. + return h_dentry;
  6473. +}
  6474. +
  6475. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  6476. +{
  6477. + aufs_bindex_t bend, bwh;
  6478. +
  6479. + bend = au_dbend(dentry);
  6480. + if (0 <= bend) {
  6481. + bwh = au_dbwh(dentry);
  6482. + if (!bwh)
  6483. + return bwh;
  6484. + if (0 < bwh && bwh < bend)
  6485. + return bwh - 1;
  6486. + }
  6487. + return bend;
  6488. +}
  6489. +
  6490. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  6491. +{
  6492. + aufs_bindex_t bend, bopq;
  6493. +
  6494. + bend = au_dbtail(dentry);
  6495. + if (0 <= bend) {
  6496. + bopq = au_dbdiropq(dentry);
  6497. + if (0 <= bopq && bopq < bend)
  6498. + bend = bopq;
  6499. + }
  6500. + return bend;
  6501. +}
  6502. +
  6503. +/* ---------------------------------------------------------------------- */
  6504. +
  6505. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  6506. + struct dentry *h_dentry)
  6507. +{
  6508. + struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
  6509. + struct au_branch *br;
  6510. +
  6511. + DiMustWriteLock(dentry);
  6512. +
  6513. + au_hdput(hd);
  6514. + hd->hd_dentry = h_dentry;
  6515. + if (h_dentry) {
  6516. + br = au_sbr(dentry->d_sb, bindex);
  6517. + hd->hd_id = br->br_id;
  6518. + }
  6519. +}
  6520. +
  6521. +int au_dbrange_test(struct dentry *dentry)
  6522. +{
  6523. + int err;
  6524. + aufs_bindex_t bstart, bend;
  6525. +
  6526. + err = 0;
  6527. + bstart = au_dbstart(dentry);
  6528. + bend = au_dbend(dentry);
  6529. + if (bstart >= 0)
  6530. + AuDebugOn(bend < 0 && bstart > bend);
  6531. + else {
  6532. + err = -EIO;
  6533. + AuDebugOn(bend >= 0);
  6534. + }
  6535. +
  6536. + return err;
  6537. +}
  6538. +
  6539. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  6540. +{
  6541. + int err;
  6542. +
  6543. + err = 0;
  6544. + if (unlikely(au_digen(dentry) != sigen
  6545. + || au_iigen_test(dentry->d_inode, sigen)))
  6546. + err = -EIO;
  6547. +
  6548. + return err;
  6549. +}
  6550. +
  6551. +void au_update_digen(struct dentry *dentry)
  6552. +{
  6553. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  6554. + /* smp_mb(); */ /* atomic_set */
  6555. +}
  6556. +
  6557. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  6558. +{
  6559. + struct au_dinfo *dinfo;
  6560. + struct dentry *h_d;
  6561. + struct au_hdentry *hdp;
  6562. +
  6563. + DiMustWriteLock(dentry);
  6564. +
  6565. + dinfo = au_di(dentry);
  6566. + if (!dinfo || dinfo->di_bstart < 0)
  6567. + return;
  6568. +
  6569. + hdp = dinfo->di_hdentry;
  6570. + if (do_put_zero) {
  6571. + aufs_bindex_t bindex, bend;
  6572. +
  6573. + bend = dinfo->di_bend;
  6574. + for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
  6575. + h_d = hdp[0 + bindex].hd_dentry;
  6576. + if (h_d && !h_d->d_inode)
  6577. + au_set_h_dptr(dentry, bindex, NULL);
  6578. + }
  6579. + }
  6580. +
  6581. + dinfo->di_bstart = -1;
  6582. + while (++dinfo->di_bstart <= dinfo->di_bend)
  6583. + if (hdp[0 + dinfo->di_bstart].hd_dentry)
  6584. + break;
  6585. + if (dinfo->di_bstart > dinfo->di_bend) {
  6586. + dinfo->di_bstart = -1;
  6587. + dinfo->di_bend = -1;
  6588. + return;
  6589. + }
  6590. +
  6591. + dinfo->di_bend++;
  6592. + while (0 <= --dinfo->di_bend)
  6593. + if (hdp[0 + dinfo->di_bend].hd_dentry)
  6594. + break;
  6595. + AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
  6596. +}
  6597. +
  6598. +void au_update_dbstart(struct dentry *dentry)
  6599. +{
  6600. + aufs_bindex_t bindex, bend;
  6601. + struct dentry *h_dentry;
  6602. +
  6603. + bend = au_dbend(dentry);
  6604. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
  6605. + h_dentry = au_h_dptr(dentry, bindex);
  6606. + if (!h_dentry)
  6607. + continue;
  6608. + if (h_dentry->d_inode) {
  6609. + au_set_dbstart(dentry, bindex);
  6610. + return;
  6611. + }
  6612. + au_set_h_dptr(dentry, bindex, NULL);
  6613. + }
  6614. +}
  6615. +
  6616. +void au_update_dbend(struct dentry *dentry)
  6617. +{
  6618. + aufs_bindex_t bindex, bstart;
  6619. + struct dentry *h_dentry;
  6620. +
  6621. + bstart = au_dbstart(dentry);
  6622. + for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
  6623. + h_dentry = au_h_dptr(dentry, bindex);
  6624. + if (!h_dentry)
  6625. + continue;
  6626. + if (h_dentry->d_inode) {
  6627. + au_set_dbend(dentry, bindex);
  6628. + return;
  6629. + }
  6630. + au_set_h_dptr(dentry, bindex, NULL);
  6631. + }
  6632. +}
  6633. +
  6634. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  6635. +{
  6636. + aufs_bindex_t bindex, bend;
  6637. +
  6638. + bend = au_dbend(dentry);
  6639. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
  6640. + if (au_h_dptr(dentry, bindex) == h_dentry)
  6641. + return bindex;
  6642. + return -1;
  6643. +}
  6644. diff -Nur linux-4.0.5.orig/fs/aufs/dir.c linux-4.0.5/fs/aufs/dir.c
  6645. --- linux-4.0.5.orig/fs/aufs/dir.c 1969-12-31 18:00:00.000000000 -0600
  6646. +++ linux-4.0.5/fs/aufs/dir.c 2015-06-21 14:07:48.682432781 -0500
  6647. @@ -0,0 +1,751 @@
  6648. +/*
  6649. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  6650. + *
  6651. + * This program, aufs is free software; you can redistribute it and/or modify
  6652. + * it under the terms of the GNU General Public License as published by
  6653. + * the Free Software Foundation; either version 2 of the License, or
  6654. + * (at your option) any later version.
  6655. + *
  6656. + * This program is distributed in the hope that it will be useful,
  6657. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6658. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6659. + * GNU General Public License for more details.
  6660. + *
  6661. + * You should have received a copy of the GNU General Public License
  6662. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6663. + */
  6664. +
  6665. +/*
  6666. + * directory operations
  6667. + */
  6668. +
  6669. +#include <linux/fs_stack.h>
  6670. +#include "aufs.h"
  6671. +
  6672. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  6673. +{
  6674. + unsigned int nlink;
  6675. +
  6676. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  6677. +
  6678. + nlink = dir->i_nlink;
  6679. + nlink += h_dir->i_nlink - 2;
  6680. + if (h_dir->i_nlink < 2)
  6681. + nlink += 2;
  6682. + smp_mb(); /* for i_nlink */
  6683. + /* 0 can happen in revaliding */
  6684. + set_nlink(dir, nlink);
  6685. +}
  6686. +
  6687. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  6688. +{
  6689. + unsigned int nlink;
  6690. +
  6691. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  6692. +
  6693. + nlink = dir->i_nlink;
  6694. + nlink -= h_dir->i_nlink - 2;
  6695. + if (h_dir->i_nlink < 2)
  6696. + nlink -= 2;
  6697. + smp_mb(); /* for i_nlink */
  6698. + /* nlink == 0 means the branch-fs is broken */
  6699. + set_nlink(dir, nlink);
  6700. +}
  6701. +
  6702. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  6703. +{
  6704. + loff_t sz;
  6705. + aufs_bindex_t bindex, bend;
  6706. + struct file *h_file;
  6707. + struct dentry *h_dentry;
  6708. +
  6709. + sz = 0;
  6710. + if (file) {
  6711. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  6712. +
  6713. + bend = au_fbend_dir(file);
  6714. + for (bindex = au_fbstart(file);
  6715. + bindex <= bend && sz < KMALLOC_MAX_SIZE;
  6716. + bindex++) {
  6717. + h_file = au_hf_dir(file, bindex);
  6718. + if (h_file && file_inode(h_file))
  6719. + sz += vfsub_f_size_read(h_file);
  6720. + }
  6721. + } else {
  6722. + AuDebugOn(!dentry);
  6723. + AuDebugOn(!d_is_dir(dentry));
  6724. +
  6725. + bend = au_dbtaildir(dentry);
  6726. + for (bindex = au_dbstart(dentry);
  6727. + bindex <= bend && sz < KMALLOC_MAX_SIZE;
  6728. + bindex++) {
  6729. + h_dentry = au_h_dptr(dentry, bindex);
  6730. + if (h_dentry && h_dentry->d_inode)
  6731. + sz += i_size_read(h_dentry->d_inode);
  6732. + }
  6733. + }
  6734. + if (sz < KMALLOC_MAX_SIZE)
  6735. + sz = roundup_pow_of_two(sz);
  6736. + if (sz > KMALLOC_MAX_SIZE)
  6737. + sz = KMALLOC_MAX_SIZE;
  6738. + else if (sz < NAME_MAX) {
  6739. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  6740. + sz = AUFS_RDBLK_DEF;
  6741. + }
  6742. + return sz;
  6743. +}
  6744. +
  6745. +struct au_dir_ts_arg {
  6746. + struct dentry *dentry;
  6747. + aufs_bindex_t brid;
  6748. +};
  6749. +
  6750. +static void au_do_dir_ts(void *arg)
  6751. +{
  6752. + struct au_dir_ts_arg *a = arg;
  6753. + struct au_dtime dt;
  6754. + struct path h_path;
  6755. + struct inode *dir, *h_dir;
  6756. + struct super_block *sb;
  6757. + struct au_branch *br;
  6758. + struct au_hinode *hdir;
  6759. + int err;
  6760. + aufs_bindex_t bstart, bindex;
  6761. +
  6762. + sb = a->dentry->d_sb;
  6763. + dir = a->dentry->d_inode;
  6764. + if (!dir)
  6765. + goto out;
  6766. + /* no dir->i_mutex lock */
  6767. + aufs_read_lock(a->dentry, AuLock_DW | AuLock_DIR); /* noflush */
  6768. +
  6769. + bstart = au_ibstart(dir);
  6770. + bindex = au_br_index(sb, a->brid);
  6771. + if (bindex < bstart)
  6772. + goto out_unlock;
  6773. +
  6774. + br = au_sbr(sb, bindex);
  6775. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  6776. + if (!h_path.dentry)
  6777. + goto out_unlock;
  6778. + h_path.mnt = au_br_mnt(br);
  6779. + au_dtime_store(&dt, a->dentry, &h_path);
  6780. +
  6781. + br = au_sbr(sb, bstart);
  6782. + if (!au_br_writable(br->br_perm))
  6783. + goto out_unlock;
  6784. + h_path.dentry = au_h_dptr(a->dentry, bstart);
  6785. + h_path.mnt = au_br_mnt(br);
  6786. + err = vfsub_mnt_want_write(h_path.mnt);
  6787. + if (err)
  6788. + goto out_unlock;
  6789. + hdir = au_hi(dir, bstart);
  6790. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  6791. + h_dir = au_h_iptr(dir, bstart);
  6792. + if (h_dir->i_nlink
  6793. + && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  6794. + dt.dt_h_path = h_path;
  6795. + au_dtime_revert(&dt);
  6796. + }
  6797. + au_hn_imtx_unlock(hdir);
  6798. + vfsub_mnt_drop_write(h_path.mnt);
  6799. + au_cpup_attr_timesizes(dir);
  6800. +
  6801. +out_unlock:
  6802. + aufs_read_unlock(a->dentry, AuLock_DW);
  6803. +out:
  6804. + dput(a->dentry);
  6805. + au_nwt_done(&au_sbi(sb)->si_nowait);
  6806. + kfree(arg);
  6807. +}
  6808. +
  6809. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  6810. +{
  6811. + int perm, wkq_err;
  6812. + aufs_bindex_t bstart;
  6813. + struct au_dir_ts_arg *arg;
  6814. + struct dentry *dentry;
  6815. + struct super_block *sb;
  6816. +
  6817. + IMustLock(dir);
  6818. +
  6819. + dentry = d_find_any_alias(dir);
  6820. + AuDebugOn(!dentry);
  6821. + sb = dentry->d_sb;
  6822. + bstart = au_ibstart(dir);
  6823. + if (bstart == bindex) {
  6824. + au_cpup_attr_timesizes(dir);
  6825. + goto out;
  6826. + }
  6827. +
  6828. + perm = au_sbr_perm(sb, bstart);
  6829. + if (!au_br_writable(perm))
  6830. + goto out;
  6831. +
  6832. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  6833. + if (!arg)
  6834. + goto out;
  6835. +
  6836. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  6837. + arg->brid = au_sbr_id(sb, bindex);
  6838. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  6839. + if (unlikely(wkq_err)) {
  6840. + pr_err("wkq %d\n", wkq_err);
  6841. + dput(dentry);
  6842. + kfree(arg);
  6843. + }
  6844. +
  6845. +out:
  6846. + dput(dentry);
  6847. +}
  6848. +
  6849. +/* ---------------------------------------------------------------------- */
  6850. +
  6851. +static int reopen_dir(struct file *file)
  6852. +{
  6853. + int err;
  6854. + unsigned int flags;
  6855. + aufs_bindex_t bindex, btail, bstart;
  6856. + struct dentry *dentry, *h_dentry;
  6857. + struct file *h_file;
  6858. +
  6859. + /* open all lower dirs */
  6860. + dentry = file->f_path.dentry;
  6861. + bstart = au_dbstart(dentry);
  6862. + for (bindex = au_fbstart(file); bindex < bstart; bindex++)
  6863. + au_set_h_fptr(file, bindex, NULL);
  6864. + au_set_fbstart(file, bstart);
  6865. +
  6866. + btail = au_dbtaildir(dentry);
  6867. + for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
  6868. + au_set_h_fptr(file, bindex, NULL);
  6869. + au_set_fbend_dir(file, btail);
  6870. +
  6871. + flags = vfsub_file_flags(file);
  6872. + for (bindex = bstart; bindex <= btail; bindex++) {
  6873. + h_dentry = au_h_dptr(dentry, bindex);
  6874. + if (!h_dentry)
  6875. + continue;
  6876. + h_file = au_hf_dir(file, bindex);
  6877. + if (h_file)
  6878. + continue;
  6879. +
  6880. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  6881. + err = PTR_ERR(h_file);
  6882. + if (IS_ERR(h_file))
  6883. + goto out; /* close all? */
  6884. + au_set_h_fptr(file, bindex, h_file);
  6885. + }
  6886. + au_update_figen(file);
  6887. + /* todo: necessary? */
  6888. + /* file->f_ra = h_file->f_ra; */
  6889. + err = 0;
  6890. +
  6891. +out:
  6892. + return err;
  6893. +}
  6894. +
  6895. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  6896. +{
  6897. + int err;
  6898. + aufs_bindex_t bindex, btail;
  6899. + struct dentry *dentry, *h_dentry;
  6900. +
  6901. + FiMustWriteLock(file);
  6902. + AuDebugOn(h_file);
  6903. +
  6904. + err = 0;
  6905. + dentry = file->f_path.dentry;
  6906. + file->f_version = dentry->d_inode->i_version;
  6907. + bindex = au_dbstart(dentry);
  6908. + au_set_fbstart(file, bindex);
  6909. + btail = au_dbtaildir(dentry);
  6910. + au_set_fbend_dir(file, btail);
  6911. + for (; !err && bindex <= btail; bindex++) {
  6912. + h_dentry = au_h_dptr(dentry, bindex);
  6913. + if (!h_dentry)
  6914. + continue;
  6915. +
  6916. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  6917. + if (IS_ERR(h_file)) {
  6918. + err = PTR_ERR(h_file);
  6919. + break;
  6920. + }
  6921. + au_set_h_fptr(file, bindex, h_file);
  6922. + }
  6923. + au_update_figen(file);
  6924. + /* todo: necessary? */
  6925. + /* file->f_ra = h_file->f_ra; */
  6926. + if (!err)
  6927. + return 0; /* success */
  6928. +
  6929. + /* close all */
  6930. + for (bindex = au_fbstart(file); bindex <= btail; bindex++)
  6931. + au_set_h_fptr(file, bindex, NULL);
  6932. + au_set_fbstart(file, -1);
  6933. + au_set_fbend_dir(file, -1);
  6934. +
  6935. + return err;
  6936. +}
  6937. +
  6938. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  6939. + struct file *file)
  6940. +{
  6941. + int err;
  6942. + struct super_block *sb;
  6943. + struct au_fidir *fidir;
  6944. +
  6945. + err = -ENOMEM;
  6946. + sb = file->f_path.dentry->d_sb;
  6947. + si_read_lock(sb, AuLock_FLUSH);
  6948. + fidir = au_fidir_alloc(sb);
  6949. + if (fidir) {
  6950. + struct au_do_open_args args = {
  6951. + .open = do_open_dir,
  6952. + .fidir = fidir
  6953. + };
  6954. + err = au_do_open(file, &args);
  6955. + if (unlikely(err))
  6956. + kfree(fidir);
  6957. + }
  6958. + si_read_unlock(sb);
  6959. + return err;
  6960. +}
  6961. +
  6962. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  6963. + struct file *file)
  6964. +{
  6965. + struct au_vdir *vdir_cache;
  6966. + struct au_finfo *finfo;
  6967. + struct au_fidir *fidir;
  6968. + aufs_bindex_t bindex, bend;
  6969. +
  6970. + finfo = au_fi(file);
  6971. + fidir = finfo->fi_hdir;
  6972. + if (fidir) {
  6973. + au_sphl_del(&finfo->fi_hlist,
  6974. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  6975. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  6976. + if (vdir_cache)
  6977. + au_vdir_free(vdir_cache);
  6978. +
  6979. + bindex = finfo->fi_btop;
  6980. + if (bindex >= 0) {
  6981. + /*
  6982. + * calls fput() instead of filp_close(),
  6983. + * since no dnotify or lock for the lower file.
  6984. + */
  6985. + bend = fidir->fd_bbot;
  6986. + for (; bindex <= bend; bindex++)
  6987. + au_set_h_fptr(file, bindex, NULL);
  6988. + }
  6989. + kfree(fidir);
  6990. + finfo->fi_hdir = NULL;
  6991. + }
  6992. + au_finfo_fin(file);
  6993. + return 0;
  6994. +}
  6995. +
  6996. +/* ---------------------------------------------------------------------- */
  6997. +
  6998. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  6999. +{
  7000. + int err;
  7001. + aufs_bindex_t bindex, bend;
  7002. + struct file *h_file;
  7003. +
  7004. + err = 0;
  7005. + bend = au_fbend_dir(file);
  7006. + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
  7007. + h_file = au_hf_dir(file, bindex);
  7008. + if (h_file)
  7009. + err = vfsub_flush(h_file, id);
  7010. + }
  7011. + return err;
  7012. +}
  7013. +
  7014. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  7015. +{
  7016. + return au_do_flush(file, id, au_do_flush_dir);
  7017. +}
  7018. +
  7019. +/* ---------------------------------------------------------------------- */
  7020. +
  7021. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  7022. +{
  7023. + int err;
  7024. + aufs_bindex_t bend, bindex;
  7025. + struct inode *inode;
  7026. + struct super_block *sb;
  7027. +
  7028. + err = 0;
  7029. + sb = dentry->d_sb;
  7030. + inode = dentry->d_inode;
  7031. + IMustLock(inode);
  7032. + bend = au_dbend(dentry);
  7033. + for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
  7034. + struct path h_path;
  7035. +
  7036. + if (au_test_ro(sb, bindex, inode))
  7037. + continue;
  7038. + h_path.dentry = au_h_dptr(dentry, bindex);
  7039. + if (!h_path.dentry)
  7040. + continue;
  7041. +
  7042. + h_path.mnt = au_sbr_mnt(sb, bindex);
  7043. + err = vfsub_fsync(NULL, &h_path, datasync);
  7044. + }
  7045. +
  7046. + return err;
  7047. +}
  7048. +
  7049. +static int au_do_fsync_dir(struct file *file, int datasync)
  7050. +{
  7051. + int err;
  7052. + aufs_bindex_t bend, bindex;
  7053. + struct file *h_file;
  7054. + struct super_block *sb;
  7055. + struct inode *inode;
  7056. +
  7057. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  7058. + if (unlikely(err))
  7059. + goto out;
  7060. +
  7061. + inode = file_inode(file);
  7062. + sb = inode->i_sb;
  7063. + bend = au_fbend_dir(file);
  7064. + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
  7065. + h_file = au_hf_dir(file, bindex);
  7066. + if (!h_file || au_test_ro(sb, bindex, inode))
  7067. + continue;
  7068. +
  7069. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  7070. + }
  7071. +
  7072. +out:
  7073. + return err;
  7074. +}
  7075. +
  7076. +/*
  7077. + * @file may be NULL
  7078. + */
  7079. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  7080. + int datasync)
  7081. +{
  7082. + int err;
  7083. + struct dentry *dentry;
  7084. + struct super_block *sb;
  7085. + struct mutex *mtx;
  7086. +
  7087. + err = 0;
  7088. + dentry = file->f_path.dentry;
  7089. + mtx = &dentry->d_inode->i_mutex;
  7090. + mutex_lock(mtx);
  7091. + sb = dentry->d_sb;
  7092. + si_noflush_read_lock(sb);
  7093. + if (file)
  7094. + err = au_do_fsync_dir(file, datasync);
  7095. + else {
  7096. + di_write_lock_child(dentry);
  7097. + err = au_do_fsync_dir_no_file(dentry, datasync);
  7098. + }
  7099. + au_cpup_attr_timesizes(dentry->d_inode);
  7100. + di_write_unlock(dentry);
  7101. + if (file)
  7102. + fi_write_unlock(file);
  7103. +
  7104. + si_read_unlock(sb);
  7105. + mutex_unlock(mtx);
  7106. + return err;
  7107. +}
  7108. +
  7109. +/* ---------------------------------------------------------------------- */
  7110. +
  7111. +static int aufs_iterate(struct file *file, struct dir_context *ctx)
  7112. +{
  7113. + int err;
  7114. + struct dentry *dentry;
  7115. + struct inode *inode, *h_inode;
  7116. + struct super_block *sb;
  7117. +
  7118. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  7119. +
  7120. + dentry = file->f_path.dentry;
  7121. + inode = dentry->d_inode;
  7122. + IMustLock(inode);
  7123. +
  7124. + sb = dentry->d_sb;
  7125. + si_read_lock(sb, AuLock_FLUSH);
  7126. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  7127. + if (unlikely(err))
  7128. + goto out;
  7129. + err = au_alive_dir(dentry);
  7130. + if (!err)
  7131. + err = au_vdir_init(file);
  7132. + di_downgrade_lock(dentry, AuLock_IR);
  7133. + if (unlikely(err))
  7134. + goto out_unlock;
  7135. +
  7136. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  7137. + if (!au_test_nfsd()) {
  7138. + err = au_vdir_fill_de(file, ctx);
  7139. + fsstack_copy_attr_atime(inode, h_inode);
  7140. + } else {
  7141. + /*
  7142. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  7143. + * encode_fh() and others.
  7144. + */
  7145. + atomic_inc(&h_inode->i_count);
  7146. + di_read_unlock(dentry, AuLock_IR);
  7147. + si_read_unlock(sb);
  7148. + err = au_vdir_fill_de(file, ctx);
  7149. + fsstack_copy_attr_atime(inode, h_inode);
  7150. + fi_write_unlock(file);
  7151. + iput(h_inode);
  7152. +
  7153. + AuTraceErr(err);
  7154. + return err;
  7155. + }
  7156. +
  7157. +out_unlock:
  7158. + di_read_unlock(dentry, AuLock_IR);
  7159. + fi_write_unlock(file);
  7160. +out:
  7161. + si_read_unlock(sb);
  7162. + return err;
  7163. +}
  7164. +
  7165. +/* ---------------------------------------------------------------------- */
  7166. +
  7167. +#define AuTestEmpty_WHONLY 1
  7168. +#define AuTestEmpty_CALLED (1 << 1)
  7169. +#define AuTestEmpty_SHWH (1 << 2)
  7170. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  7171. +#define au_fset_testempty(flags, name) \
  7172. + do { (flags) |= AuTestEmpty_##name; } while (0)
  7173. +#define au_fclr_testempty(flags, name) \
  7174. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  7175. +
  7176. +#ifndef CONFIG_AUFS_SHWH
  7177. +#undef AuTestEmpty_SHWH
  7178. +#define AuTestEmpty_SHWH 0
  7179. +#endif
  7180. +
  7181. +struct test_empty_arg {
  7182. + struct dir_context ctx;
  7183. + struct au_nhash *whlist;
  7184. + unsigned int flags;
  7185. + int err;
  7186. + aufs_bindex_t bindex;
  7187. +};
  7188. +
  7189. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  7190. + int namelen, loff_t offset __maybe_unused, u64 ino,
  7191. + unsigned int d_type)
  7192. +{
  7193. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  7194. + ctx);
  7195. + char *name = (void *)__name;
  7196. +
  7197. + arg->err = 0;
  7198. + au_fset_testempty(arg->flags, CALLED);
  7199. + /* smp_mb(); */
  7200. + if (name[0] == '.'
  7201. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  7202. + goto out; /* success */
  7203. +
  7204. + if (namelen <= AUFS_WH_PFX_LEN
  7205. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  7206. + if (au_ftest_testempty(arg->flags, WHONLY)
  7207. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  7208. + arg->err = -ENOTEMPTY;
  7209. + goto out;
  7210. + }
  7211. +
  7212. + name += AUFS_WH_PFX_LEN;
  7213. + namelen -= AUFS_WH_PFX_LEN;
  7214. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  7215. + arg->err = au_nhash_append_wh
  7216. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  7217. + au_ftest_testempty(arg->flags, SHWH));
  7218. +
  7219. +out:
  7220. + /* smp_mb(); */
  7221. + AuTraceErr(arg->err);
  7222. + return arg->err;
  7223. +}
  7224. +
  7225. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  7226. +{
  7227. + int err;
  7228. + struct file *h_file;
  7229. +
  7230. + h_file = au_h_open(dentry, arg->bindex,
  7231. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  7232. + /*file*/NULL, /*force_wr*/0);
  7233. + err = PTR_ERR(h_file);
  7234. + if (IS_ERR(h_file))
  7235. + goto out;
  7236. +
  7237. + err = 0;
  7238. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  7239. + && !file_inode(h_file)->i_nlink)
  7240. + goto out_put;
  7241. +
  7242. + do {
  7243. + arg->err = 0;
  7244. + au_fclr_testempty(arg->flags, CALLED);
  7245. + /* smp_mb(); */
  7246. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  7247. + if (err >= 0)
  7248. + err = arg->err;
  7249. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  7250. +
  7251. +out_put:
  7252. + fput(h_file);
  7253. + au_sbr_put(dentry->d_sb, arg->bindex);
  7254. +out:
  7255. + return err;
  7256. +}
  7257. +
  7258. +struct do_test_empty_args {
  7259. + int *errp;
  7260. + struct dentry *dentry;
  7261. + struct test_empty_arg *arg;
  7262. +};
  7263. +
  7264. +static void call_do_test_empty(void *args)
  7265. +{
  7266. + struct do_test_empty_args *a = args;
  7267. + *a->errp = do_test_empty(a->dentry, a->arg);
  7268. +}
  7269. +
  7270. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  7271. +{
  7272. + int err, wkq_err;
  7273. + struct dentry *h_dentry;
  7274. + struct inode *h_inode;
  7275. +
  7276. + h_dentry = au_h_dptr(dentry, arg->bindex);
  7277. + h_inode = h_dentry->d_inode;
  7278. + /* todo: i_mode changes anytime? */
  7279. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  7280. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  7281. + mutex_unlock(&h_inode->i_mutex);
  7282. + if (!err)
  7283. + err = do_test_empty(dentry, arg);
  7284. + else {
  7285. + struct do_test_empty_args args = {
  7286. + .errp = &err,
  7287. + .dentry = dentry,
  7288. + .arg = arg
  7289. + };
  7290. + unsigned int flags = arg->flags;
  7291. +
  7292. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  7293. + if (unlikely(wkq_err))
  7294. + err = wkq_err;
  7295. + arg->flags = flags;
  7296. + }
  7297. +
  7298. + return err;
  7299. +}
  7300. +
  7301. +int au_test_empty_lower(struct dentry *dentry)
  7302. +{
  7303. + int err;
  7304. + unsigned int rdhash;
  7305. + aufs_bindex_t bindex, bstart, btail;
  7306. + struct au_nhash whlist;
  7307. + struct test_empty_arg arg = {
  7308. + .ctx = {
  7309. + .actor = test_empty_cb
  7310. + }
  7311. + };
  7312. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  7313. +
  7314. + SiMustAnyLock(dentry->d_sb);
  7315. +
  7316. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  7317. + if (!rdhash)
  7318. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  7319. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  7320. + if (unlikely(err))
  7321. + goto out;
  7322. +
  7323. + arg.flags = 0;
  7324. + arg.whlist = &whlist;
  7325. + bstart = au_dbstart(dentry);
  7326. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  7327. + au_fset_testempty(arg.flags, SHWH);
  7328. + test_empty = do_test_empty;
  7329. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  7330. + test_empty = sio_test_empty;
  7331. + arg.bindex = bstart;
  7332. + err = test_empty(dentry, &arg);
  7333. + if (unlikely(err))
  7334. + goto out_whlist;
  7335. +
  7336. + au_fset_testempty(arg.flags, WHONLY);
  7337. + btail = au_dbtaildir(dentry);
  7338. + for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
  7339. + struct dentry *h_dentry;
  7340. +
  7341. + h_dentry = au_h_dptr(dentry, bindex);
  7342. + if (h_dentry && h_dentry->d_inode) {
  7343. + arg.bindex = bindex;
  7344. + err = test_empty(dentry, &arg);
  7345. + }
  7346. + }
  7347. +
  7348. +out_whlist:
  7349. + au_nhash_wh_free(&whlist);
  7350. +out:
  7351. + return err;
  7352. +}
  7353. +
  7354. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  7355. +{
  7356. + int err;
  7357. + struct test_empty_arg arg = {
  7358. + .ctx = {
  7359. + .actor = test_empty_cb
  7360. + }
  7361. + };
  7362. + aufs_bindex_t bindex, btail;
  7363. +
  7364. + err = 0;
  7365. + arg.whlist = whlist;
  7366. + arg.flags = AuTestEmpty_WHONLY;
  7367. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  7368. + au_fset_testempty(arg.flags, SHWH);
  7369. + btail = au_dbtaildir(dentry);
  7370. + for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
  7371. + struct dentry *h_dentry;
  7372. +
  7373. + h_dentry = au_h_dptr(dentry, bindex);
  7374. + if (h_dentry && h_dentry->d_inode) {
  7375. + arg.bindex = bindex;
  7376. + err = sio_test_empty(dentry, &arg);
  7377. + }
  7378. + }
  7379. +
  7380. + return err;
  7381. +}
  7382. +
  7383. +/* ---------------------------------------------------------------------- */
  7384. +
  7385. +const struct file_operations aufs_dir_fop = {
  7386. + .owner = THIS_MODULE,
  7387. + .llseek = default_llseek,
  7388. + .read = generic_read_dir,
  7389. + .iterate = aufs_iterate,
  7390. + .unlocked_ioctl = aufs_ioctl_dir,
  7391. +#ifdef CONFIG_COMPAT
  7392. + .compat_ioctl = aufs_compat_ioctl_dir,
  7393. +#endif
  7394. + .open = aufs_open_dir,
  7395. + .release = aufs_release_dir,
  7396. + .flush = aufs_flush_dir,
  7397. + .fsync = aufs_fsync_dir
  7398. +};
  7399. diff -Nur linux-4.0.5.orig/fs/aufs/dir.h linux-4.0.5/fs/aufs/dir.h
  7400. --- linux-4.0.5.orig/fs/aufs/dir.h 1969-12-31 18:00:00.000000000 -0600
  7401. +++ linux-4.0.5/fs/aufs/dir.h 2015-06-21 14:07:48.682432781 -0500
  7402. @@ -0,0 +1,131 @@
  7403. +/*
  7404. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7405. + *
  7406. + * This program, aufs is free software; you can redistribute it and/or modify
  7407. + * it under the terms of the GNU General Public License as published by
  7408. + * the Free Software Foundation; either version 2 of the License, or
  7409. + * (at your option) any later version.
  7410. + *
  7411. + * This program is distributed in the hope that it will be useful,
  7412. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7413. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7414. + * GNU General Public License for more details.
  7415. + *
  7416. + * You should have received a copy of the GNU General Public License
  7417. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7418. + */
  7419. +
  7420. +/*
  7421. + * directory operations
  7422. + */
  7423. +
  7424. +#ifndef __AUFS_DIR_H__
  7425. +#define __AUFS_DIR_H__
  7426. +
  7427. +#ifdef __KERNEL__
  7428. +
  7429. +#include <linux/fs.h>
  7430. +
  7431. +/* ---------------------------------------------------------------------- */
  7432. +
  7433. +/* need to be faster and smaller */
  7434. +
  7435. +struct au_nhash {
  7436. + unsigned int nh_num;
  7437. + struct hlist_head *nh_head;
  7438. +};
  7439. +
  7440. +struct au_vdir_destr {
  7441. + unsigned char len;
  7442. + unsigned char name[0];
  7443. +} __packed;
  7444. +
  7445. +struct au_vdir_dehstr {
  7446. + struct hlist_node hash;
  7447. + struct au_vdir_destr *str;
  7448. +} ____cacheline_aligned_in_smp;
  7449. +
  7450. +struct au_vdir_de {
  7451. + ino_t de_ino;
  7452. + unsigned char de_type;
  7453. + /* caution: packed */
  7454. + struct au_vdir_destr de_str;
  7455. +} __packed;
  7456. +
  7457. +struct au_vdir_wh {
  7458. + struct hlist_node wh_hash;
  7459. +#ifdef CONFIG_AUFS_SHWH
  7460. + ino_t wh_ino;
  7461. + aufs_bindex_t wh_bindex;
  7462. + unsigned char wh_type;
  7463. +#else
  7464. + aufs_bindex_t wh_bindex;
  7465. +#endif
  7466. + /* caution: packed */
  7467. + struct au_vdir_destr wh_str;
  7468. +} __packed;
  7469. +
  7470. +union au_vdir_deblk_p {
  7471. + unsigned char *deblk;
  7472. + struct au_vdir_de *de;
  7473. +};
  7474. +
  7475. +struct au_vdir {
  7476. + unsigned char **vd_deblk;
  7477. + unsigned long vd_nblk;
  7478. + struct {
  7479. + unsigned long ul;
  7480. + union au_vdir_deblk_p p;
  7481. + } vd_last;
  7482. +
  7483. + unsigned long vd_version;
  7484. + unsigned int vd_deblk_sz;
  7485. + unsigned long vd_jiffy;
  7486. +} ____cacheline_aligned_in_smp;
  7487. +
  7488. +/* ---------------------------------------------------------------------- */
  7489. +
  7490. +/* dir.c */
  7491. +extern const struct file_operations aufs_dir_fop;
  7492. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  7493. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  7494. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  7495. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  7496. +int au_test_empty_lower(struct dentry *dentry);
  7497. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  7498. +
  7499. +/* vdir.c */
  7500. +unsigned int au_rdhash_est(loff_t sz);
  7501. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  7502. +void au_nhash_wh_free(struct au_nhash *whlist);
  7503. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  7504. + int limit);
  7505. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  7506. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  7507. + unsigned int d_type, aufs_bindex_t bindex,
  7508. + unsigned char shwh);
  7509. +void au_vdir_free(struct au_vdir *vdir);
  7510. +int au_vdir_init(struct file *file);
  7511. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  7512. +
  7513. +/* ioctl.c */
  7514. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  7515. +
  7516. +#ifdef CONFIG_AUFS_RDU
  7517. +/* rdu.c */
  7518. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  7519. +#ifdef CONFIG_COMPAT
  7520. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  7521. + unsigned long arg);
  7522. +#endif
  7523. +#else
  7524. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  7525. + unsigned int cmd, unsigned long arg)
  7526. +#ifdef CONFIG_COMPAT
  7527. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  7528. + unsigned int cmd, unsigned long arg)
  7529. +#endif
  7530. +#endif
  7531. +
  7532. +#endif /* __KERNEL__ */
  7533. +#endif /* __AUFS_DIR_H__ */
  7534. diff -Nur linux-4.0.5.orig/fs/aufs/dynop.c linux-4.0.5/fs/aufs/dynop.c
  7535. --- linux-4.0.5.orig/fs/aufs/dynop.c 1969-12-31 18:00:00.000000000 -0600
  7536. +++ linux-4.0.5/fs/aufs/dynop.c 2015-06-21 14:07:48.682432781 -0500
  7537. @@ -0,0 +1,369 @@
  7538. +/*
  7539. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  7540. + *
  7541. + * This program, aufs is free software; you can redistribute it and/or modify
  7542. + * it under the terms of the GNU General Public License as published by
  7543. + * the Free Software Foundation; either version 2 of the License, or
  7544. + * (at your option) any later version.
  7545. + *
  7546. + * This program is distributed in the hope that it will be useful,
  7547. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7548. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7549. + * GNU General Public License for more details.
  7550. + *
  7551. + * You should have received a copy of the GNU General Public License
  7552. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7553. + */
  7554. +
  7555. +/*
  7556. + * dynamically customizable operations for regular files
  7557. + */
  7558. +
  7559. +#include "aufs.h"
  7560. +
  7561. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  7562. +
  7563. +/*
  7564. + * How large will these lists be?
  7565. + * Usually just a few elements, 20-30 at most for each, I guess.
  7566. + */
  7567. +static struct au_splhead dynop[AuDyLast];
  7568. +
  7569. +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
  7570. +{
  7571. + struct au_dykey *key, *tmp;
  7572. + struct list_head *head;
  7573. +
  7574. + key = NULL;
  7575. + head = &spl->head;
  7576. + rcu_read_lock();
  7577. + list_for_each_entry_rcu(tmp, head, dk_list)
  7578. + if (tmp->dk_op.dy_hop == h_op) {
  7579. + key = tmp;
  7580. + kref_get(&key->dk_kref);
  7581. + break;
  7582. + }
  7583. + rcu_read_unlock();
  7584. +
  7585. + return key;
  7586. +}
  7587. +
  7588. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  7589. +{
  7590. + struct au_dykey **k, *found;
  7591. + const void *h_op = key->dk_op.dy_hop;
  7592. + int i;
  7593. +
  7594. + found = NULL;
  7595. + k = br->br_dykey;
  7596. + for (i = 0; i < AuBrDynOp; i++)
  7597. + if (k[i]) {
  7598. + if (k[i]->dk_op.dy_hop == h_op) {
  7599. + found = k[i];
  7600. + break;
  7601. + }
  7602. + } else
  7603. + break;
  7604. + if (!found) {
  7605. + spin_lock(&br->br_dykey_lock);
  7606. + for (; i < AuBrDynOp; i++)
  7607. + if (k[i]) {
  7608. + if (k[i]->dk_op.dy_hop == h_op) {
  7609. + found = k[i];
  7610. + break;
  7611. + }
  7612. + } else {
  7613. + k[i] = key;
  7614. + break;
  7615. + }
  7616. + spin_unlock(&br->br_dykey_lock);
  7617. + BUG_ON(i == AuBrDynOp); /* expand the array */
  7618. + }
  7619. +
  7620. + return found;
  7621. +}
  7622. +
  7623. +/* kref_get() if @key is already added */
  7624. +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
  7625. +{
  7626. + struct au_dykey *tmp, *found;
  7627. + struct list_head *head;
  7628. + const void *h_op = key->dk_op.dy_hop;
  7629. +
  7630. + found = NULL;
  7631. + head = &spl->head;
  7632. + spin_lock(&spl->spin);
  7633. + list_for_each_entry(tmp, head, dk_list)
  7634. + if (tmp->dk_op.dy_hop == h_op) {
  7635. + kref_get(&tmp->dk_kref);
  7636. + found = tmp;
  7637. + break;
  7638. + }
  7639. + if (!found)
  7640. + list_add_rcu(&key->dk_list, head);
  7641. + spin_unlock(&spl->spin);
  7642. +
  7643. + if (!found)
  7644. + DyPrSym(key);
  7645. + return found;
  7646. +}
  7647. +
  7648. +static void dy_free_rcu(struct rcu_head *rcu)
  7649. +{
  7650. + struct au_dykey *key;
  7651. +
  7652. + key = container_of(rcu, struct au_dykey, dk_rcu);
  7653. + DyPrSym(key);
  7654. + kfree(key);
  7655. +}
  7656. +
  7657. +static void dy_free(struct kref *kref)
  7658. +{
  7659. + struct au_dykey *key;
  7660. + struct au_splhead *spl;
  7661. +
  7662. + key = container_of(kref, struct au_dykey, dk_kref);
  7663. + spl = dynop + key->dk_op.dy_type;
  7664. + au_spl_del_rcu(&key->dk_list, spl);
  7665. + call_rcu(&key->dk_rcu, dy_free_rcu);
  7666. +}
  7667. +
  7668. +void au_dy_put(struct au_dykey *key)
  7669. +{
  7670. + kref_put(&key->dk_kref, dy_free);
  7671. +}
  7672. +
  7673. +/* ---------------------------------------------------------------------- */
  7674. +
  7675. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  7676. +
  7677. +#ifdef CONFIG_AUFS_DEBUG
  7678. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  7679. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  7680. +#else
  7681. +#define DyDbgDeclare(cnt) do {} while (0)
  7682. +#define DyDbgInc(cnt) do {} while (0)
  7683. +#endif
  7684. +
  7685. +#define DySet(func, dst, src, h_op, h_sb) do { \
  7686. + DyDbgInc(cnt); \
  7687. + if (h_op->func) { \
  7688. + if (src.func) \
  7689. + dst.func = src.func; \
  7690. + else \
  7691. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  7692. + } \
  7693. +} while (0)
  7694. +
  7695. +#define DySetForce(func, dst, src) do { \
  7696. + AuDebugOn(!src.func); \
  7697. + DyDbgInc(cnt); \
  7698. + dst.func = src.func; \
  7699. +} while (0)
  7700. +
  7701. +#define DySetAop(func) \
  7702. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  7703. +#define DySetAopForce(func) \
  7704. + DySetForce(func, dyaop->da_op, aufs_aop)
  7705. +
  7706. +static void dy_aop(struct au_dykey *key, const void *h_op,
  7707. + struct super_block *h_sb __maybe_unused)
  7708. +{
  7709. + struct au_dyaop *dyaop = (void *)key;
  7710. + const struct address_space_operations *h_aop = h_op;
  7711. + DyDbgDeclare(cnt);
  7712. +
  7713. + AuDbg("%s\n", au_sbtype(h_sb));
  7714. +
  7715. + DySetAop(writepage);
  7716. + DySetAopForce(readpage); /* force */
  7717. + DySetAop(writepages);
  7718. + DySetAop(set_page_dirty);
  7719. + DySetAop(readpages);
  7720. + DySetAop(write_begin);
  7721. + DySetAop(write_end);
  7722. + DySetAop(bmap);
  7723. + DySetAop(invalidatepage);
  7724. + DySetAop(releasepage);
  7725. + DySetAop(freepage);
  7726. + /* this one will be changed according to an aufs mount option */
  7727. + DySetAop(direct_IO);
  7728. + DySetAop(migratepage);
  7729. + DySetAop(launder_page);
  7730. + DySetAop(is_partially_uptodate);
  7731. + DySetAop(is_dirty_writeback);
  7732. + DySetAop(error_remove_page);
  7733. + DySetAop(swap_activate);
  7734. + DySetAop(swap_deactivate);
  7735. +
  7736. + DyDbgSize(cnt, *h_aop);
  7737. +}
  7738. +
  7739. +/* ---------------------------------------------------------------------- */
  7740. +
  7741. +static void dy_bug(struct kref *kref)
  7742. +{
  7743. + BUG();
  7744. +}
  7745. +
  7746. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  7747. +{
  7748. + struct au_dykey *key, *old;
  7749. + struct au_splhead *spl;
  7750. + struct op {
  7751. + unsigned int sz;
  7752. + void (*set)(struct au_dykey *key, const void *h_op,
  7753. + struct super_block *h_sb __maybe_unused);
  7754. + };
  7755. + static const struct op a[] = {
  7756. + [AuDy_AOP] = {
  7757. + .sz = sizeof(struct au_dyaop),
  7758. + .set = dy_aop
  7759. + }
  7760. + };
  7761. + const struct op *p;
  7762. +
  7763. + spl = dynop + op->dy_type;
  7764. + key = dy_gfind_get(spl, op->dy_hop);
  7765. + if (key)
  7766. + goto out_add; /* success */
  7767. +
  7768. + p = a + op->dy_type;
  7769. + key = kzalloc(p->sz, GFP_NOFS);
  7770. + if (unlikely(!key)) {
  7771. + key = ERR_PTR(-ENOMEM);
  7772. + goto out;
  7773. + }
  7774. +
  7775. + key->dk_op.dy_hop = op->dy_hop;
  7776. + kref_init(&key->dk_kref);
  7777. + p->set(key, op->dy_hop, au_br_sb(br));
  7778. + old = dy_gadd(spl, key);
  7779. + if (old) {
  7780. + kfree(key);
  7781. + key = old;
  7782. + }
  7783. +
  7784. +out_add:
  7785. + old = dy_bradd(br, key);
  7786. + if (old)
  7787. + /* its ref-count should never be zero here */
  7788. + kref_put(&key->dk_kref, dy_bug);
  7789. +out:
  7790. + return key;
  7791. +}
  7792. +
  7793. +/* ---------------------------------------------------------------------- */
  7794. +/*
  7795. + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
  7796. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  7797. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  7798. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  7799. + * See the aufs manual in detail.
  7800. + */
  7801. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  7802. +{
  7803. + if (!do_dx)
  7804. + dyaop->da_op.direct_IO = NULL;
  7805. + else
  7806. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  7807. +}
  7808. +
  7809. +static struct au_dyaop *dy_aget(struct au_branch *br,
  7810. + const struct address_space_operations *h_aop,
  7811. + int do_dx)
  7812. +{
  7813. + struct au_dyaop *dyaop;
  7814. + struct au_dynop op;
  7815. +
  7816. + op.dy_type = AuDy_AOP;
  7817. + op.dy_haop = h_aop;
  7818. + dyaop = (void *)dy_get(&op, br);
  7819. + if (IS_ERR(dyaop))
  7820. + goto out;
  7821. + dy_adx(dyaop, do_dx);
  7822. +
  7823. +out:
  7824. + return dyaop;
  7825. +}
  7826. +
  7827. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  7828. + struct inode *h_inode)
  7829. +{
  7830. + int err, do_dx;
  7831. + struct super_block *sb;
  7832. + struct au_branch *br;
  7833. + struct au_dyaop *dyaop;
  7834. +
  7835. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  7836. + IiMustWriteLock(inode);
  7837. +
  7838. + sb = inode->i_sb;
  7839. + br = au_sbr(sb, bindex);
  7840. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  7841. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  7842. + err = PTR_ERR(dyaop);
  7843. + if (IS_ERR(dyaop))
  7844. + /* unnecessary to call dy_fput() */
  7845. + goto out;
  7846. +
  7847. + err = 0;
  7848. + inode->i_mapping->a_ops = &dyaop->da_op;
  7849. +
  7850. +out:
  7851. + return err;
  7852. +}
  7853. +
  7854. +/*
  7855. + * Is it safe to replace a_ops during the inode/file is in operation?
  7856. + * Yes, I hope so.
  7857. + */
  7858. +int au_dy_irefresh(struct inode *inode)
  7859. +{
  7860. + int err;
  7861. + aufs_bindex_t bstart;
  7862. + struct inode *h_inode;
  7863. +
  7864. + err = 0;
  7865. + if (S_ISREG(inode->i_mode)) {
  7866. + bstart = au_ibstart(inode);
  7867. + h_inode = au_h_iptr(inode, bstart);
  7868. + err = au_dy_iaop(inode, bstart, h_inode);
  7869. + }
  7870. + return err;
  7871. +}
  7872. +
  7873. +void au_dy_arefresh(int do_dx)
  7874. +{
  7875. + struct au_splhead *spl;
  7876. + struct list_head *head;
  7877. + struct au_dykey *key;
  7878. +
  7879. + spl = dynop + AuDy_AOP;
  7880. + head = &spl->head;
  7881. + spin_lock(&spl->spin);
  7882. + list_for_each_entry(key, head, dk_list)
  7883. + dy_adx((void *)key, do_dx);
  7884. + spin_unlock(&spl->spin);
  7885. +}
  7886. +
  7887. +/* ---------------------------------------------------------------------- */
  7888. +
  7889. +void __init au_dy_init(void)
  7890. +{
  7891. + int i;
  7892. +
  7893. + /* make sure that 'struct au_dykey *' can be any type */
  7894. + BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
  7895. +
  7896. + for (i = 0; i < AuDyLast; i++)
  7897. + au_spl_init(dynop + i);
  7898. +}
  7899. +
  7900. +void au_dy_fin(void)
  7901. +{
  7902. + int i;
  7903. +
  7904. + for (i = 0; i < AuDyLast; i++)
  7905. + WARN_ON(!list_empty(&dynop[i].head));
  7906. +}
  7907. diff -Nur linux-4.0.5.orig/fs/aufs/dynop.h linux-4.0.5/fs/aufs/dynop.h
  7908. --- linux-4.0.5.orig/fs/aufs/dynop.h 1969-12-31 18:00:00.000000000 -0600
  7909. +++ linux-4.0.5/fs/aufs/dynop.h 2015-06-21 14:07:48.682432781 -0500
  7910. @@ -0,0 +1,74 @@
  7911. +/*
  7912. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  7913. + *
  7914. + * This program, aufs is free software; you can redistribute it and/or modify
  7915. + * it under the terms of the GNU General Public License as published by
  7916. + * the Free Software Foundation; either version 2 of the License, or
  7917. + * (at your option) any later version.
  7918. + *
  7919. + * This program is distributed in the hope that it will be useful,
  7920. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7921. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7922. + * GNU General Public License for more details.
  7923. + *
  7924. + * You should have received a copy of the GNU General Public License
  7925. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7926. + */
  7927. +
  7928. +/*
  7929. + * dynamically customizable operations (for regular files only)
  7930. + */
  7931. +
  7932. +#ifndef __AUFS_DYNOP_H__
  7933. +#define __AUFS_DYNOP_H__
  7934. +
  7935. +#ifdef __KERNEL__
  7936. +
  7937. +#include <linux/fs.h>
  7938. +#include <linux/kref.h>
  7939. +
  7940. +enum {AuDy_AOP, AuDyLast};
  7941. +
  7942. +struct au_dynop {
  7943. + int dy_type;
  7944. + union {
  7945. + const void *dy_hop;
  7946. + const struct address_space_operations *dy_haop;
  7947. + };
  7948. +};
  7949. +
  7950. +struct au_dykey {
  7951. + union {
  7952. + struct list_head dk_list;
  7953. + struct rcu_head dk_rcu;
  7954. + };
  7955. + struct au_dynop dk_op;
  7956. +
  7957. + /*
  7958. + * during I am in the branch local array, kref is gotten. when the
  7959. + * branch is removed, kref is put.
  7960. + */
  7961. + struct kref dk_kref;
  7962. +};
  7963. +
  7964. +/* stop unioning since their sizes are very different from each other */
  7965. +struct au_dyaop {
  7966. + struct au_dykey da_key;
  7967. + struct address_space_operations da_op; /* not const */
  7968. +};
  7969. +
  7970. +/* ---------------------------------------------------------------------- */
  7971. +
  7972. +/* dynop.c */
  7973. +struct au_branch;
  7974. +void au_dy_put(struct au_dykey *key);
  7975. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  7976. + struct inode *h_inode);
  7977. +int au_dy_irefresh(struct inode *inode);
  7978. +void au_dy_arefresh(int do_dio);
  7979. +
  7980. +void __init au_dy_init(void);
  7981. +void au_dy_fin(void);
  7982. +
  7983. +#endif /* __KERNEL__ */
  7984. +#endif /* __AUFS_DYNOP_H__ */
  7985. diff -Nur linux-4.0.5.orig/fs/aufs/export.c linux-4.0.5/fs/aufs/export.c
  7986. --- linux-4.0.5.orig/fs/aufs/export.c 1969-12-31 18:00:00.000000000 -0600
  7987. +++ linux-4.0.5/fs/aufs/export.c 2015-06-21 14:07:48.682432781 -0500
  7988. @@ -0,0 +1,831 @@
  7989. +/*
  7990. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7991. + *
  7992. + * This program, aufs is free software; you can redistribute it and/or modify
  7993. + * it under the terms of the GNU General Public License as published by
  7994. + * the Free Software Foundation; either version 2 of the License, or
  7995. + * (at your option) any later version.
  7996. + *
  7997. + * This program is distributed in the hope that it will be useful,
  7998. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7999. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8000. + * GNU General Public License for more details.
  8001. + *
  8002. + * You should have received a copy of the GNU General Public License
  8003. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8004. + */
  8005. +
  8006. +/*
  8007. + * export via nfs
  8008. + */
  8009. +
  8010. +#include <linux/exportfs.h>
  8011. +#include <linux/fs_struct.h>
  8012. +#include <linux/namei.h>
  8013. +#include <linux/nsproxy.h>
  8014. +#include <linux/random.h>
  8015. +#include <linux/writeback.h>
  8016. +#include "../fs/mount.h"
  8017. +#include "aufs.h"
  8018. +
  8019. +union conv {
  8020. +#ifdef CONFIG_AUFS_INO_T_64
  8021. + __u32 a[2];
  8022. +#else
  8023. + __u32 a[1];
  8024. +#endif
  8025. + ino_t ino;
  8026. +};
  8027. +
  8028. +static ino_t decode_ino(__u32 *a)
  8029. +{
  8030. + union conv u;
  8031. +
  8032. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  8033. + u.a[0] = a[0];
  8034. +#ifdef CONFIG_AUFS_INO_T_64
  8035. + u.a[1] = a[1];
  8036. +#endif
  8037. + return u.ino;
  8038. +}
  8039. +
  8040. +static void encode_ino(__u32 *a, ino_t ino)
  8041. +{
  8042. + union conv u;
  8043. +
  8044. + u.ino = ino;
  8045. + a[0] = u.a[0];
  8046. +#ifdef CONFIG_AUFS_INO_T_64
  8047. + a[1] = u.a[1];
  8048. +#endif
  8049. +}
  8050. +
  8051. +/* NFS file handle */
  8052. +enum {
  8053. + Fh_br_id,
  8054. + Fh_sigen,
  8055. +#ifdef CONFIG_AUFS_INO_T_64
  8056. + /* support 64bit inode number */
  8057. + Fh_ino1,
  8058. + Fh_ino2,
  8059. + Fh_dir_ino1,
  8060. + Fh_dir_ino2,
  8061. +#else
  8062. + Fh_ino1,
  8063. + Fh_dir_ino1,
  8064. +#endif
  8065. + Fh_igen,
  8066. + Fh_h_type,
  8067. + Fh_tail,
  8068. +
  8069. + Fh_ino = Fh_ino1,
  8070. + Fh_dir_ino = Fh_dir_ino1
  8071. +};
  8072. +
  8073. +static int au_test_anon(struct dentry *dentry)
  8074. +{
  8075. + /* note: read d_flags without d_lock */
  8076. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  8077. +}
  8078. +
  8079. +int au_test_nfsd(void)
  8080. +{
  8081. + int ret;
  8082. + struct task_struct *tsk = current;
  8083. + char comm[sizeof(tsk->comm)];
  8084. +
  8085. + ret = 0;
  8086. + if (tsk->flags & PF_KTHREAD) {
  8087. + get_task_comm(comm, tsk);
  8088. + ret = !strcmp(comm, "nfsd");
  8089. + }
  8090. +
  8091. + return ret;
  8092. +}
  8093. +
  8094. +/* ---------------------------------------------------------------------- */
  8095. +/* inode generation external table */
  8096. +
  8097. +void au_xigen_inc(struct inode *inode)
  8098. +{
  8099. + loff_t pos;
  8100. + ssize_t sz;
  8101. + __u32 igen;
  8102. + struct super_block *sb;
  8103. + struct au_sbinfo *sbinfo;
  8104. +
  8105. + sb = inode->i_sb;
  8106. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  8107. +
  8108. + sbinfo = au_sbi(sb);
  8109. + pos = inode->i_ino;
  8110. + pos *= sizeof(igen);
  8111. + igen = inode->i_generation + 1;
  8112. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  8113. + sizeof(igen), &pos);
  8114. + if (sz == sizeof(igen))
  8115. + return; /* success */
  8116. +
  8117. + if (unlikely(sz >= 0))
  8118. + AuIOErr("xigen error (%zd)\n", sz);
  8119. +}
  8120. +
  8121. +int au_xigen_new(struct inode *inode)
  8122. +{
  8123. + int err;
  8124. + loff_t pos;
  8125. + ssize_t sz;
  8126. + struct super_block *sb;
  8127. + struct au_sbinfo *sbinfo;
  8128. + struct file *file;
  8129. +
  8130. + err = 0;
  8131. + /* todo: dirty, at mount time */
  8132. + if (inode->i_ino == AUFS_ROOT_INO)
  8133. + goto out;
  8134. + sb = inode->i_sb;
  8135. + SiMustAnyLock(sb);
  8136. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  8137. + goto out;
  8138. +
  8139. + err = -EFBIG;
  8140. + pos = inode->i_ino;
  8141. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  8142. + AuIOErr1("too large i%lld\n", pos);
  8143. + goto out;
  8144. + }
  8145. + pos *= sizeof(inode->i_generation);
  8146. +
  8147. + err = 0;
  8148. + sbinfo = au_sbi(sb);
  8149. + file = sbinfo->si_xigen;
  8150. + BUG_ON(!file);
  8151. +
  8152. + if (vfsub_f_size_read(file)
  8153. + < pos + sizeof(inode->i_generation)) {
  8154. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  8155. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  8156. + sizeof(inode->i_generation), &pos);
  8157. + } else
  8158. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  8159. + sizeof(inode->i_generation), &pos);
  8160. + if (sz == sizeof(inode->i_generation))
  8161. + goto out; /* success */
  8162. +
  8163. + err = sz;
  8164. + if (unlikely(sz >= 0)) {
  8165. + err = -EIO;
  8166. + AuIOErr("xigen error (%zd)\n", sz);
  8167. + }
  8168. +
  8169. +out:
  8170. + return err;
  8171. +}
  8172. +
  8173. +int au_xigen_set(struct super_block *sb, struct file *base)
  8174. +{
  8175. + int err;
  8176. + struct au_sbinfo *sbinfo;
  8177. + struct file *file;
  8178. +
  8179. + SiMustWriteLock(sb);
  8180. +
  8181. + sbinfo = au_sbi(sb);
  8182. + file = au_xino_create2(base, sbinfo->si_xigen);
  8183. + err = PTR_ERR(file);
  8184. + if (IS_ERR(file))
  8185. + goto out;
  8186. + err = 0;
  8187. + if (sbinfo->si_xigen)
  8188. + fput(sbinfo->si_xigen);
  8189. + sbinfo->si_xigen = file;
  8190. +
  8191. +out:
  8192. + return err;
  8193. +}
  8194. +
  8195. +void au_xigen_clr(struct super_block *sb)
  8196. +{
  8197. + struct au_sbinfo *sbinfo;
  8198. +
  8199. + SiMustWriteLock(sb);
  8200. +
  8201. + sbinfo = au_sbi(sb);
  8202. + if (sbinfo->si_xigen) {
  8203. + fput(sbinfo->si_xigen);
  8204. + sbinfo->si_xigen = NULL;
  8205. + }
  8206. +}
  8207. +
  8208. +/* ---------------------------------------------------------------------- */
  8209. +
  8210. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  8211. + ino_t dir_ino)
  8212. +{
  8213. + struct dentry *dentry, *d;
  8214. + struct inode *inode;
  8215. + unsigned int sigen;
  8216. +
  8217. + dentry = NULL;
  8218. + inode = ilookup(sb, ino);
  8219. + if (!inode)
  8220. + goto out;
  8221. +
  8222. + dentry = ERR_PTR(-ESTALE);
  8223. + sigen = au_sigen(sb);
  8224. + if (unlikely(is_bad_inode(inode)
  8225. + || IS_DEADDIR(inode)
  8226. + || sigen != au_iigen(inode, NULL)))
  8227. + goto out_iput;
  8228. +
  8229. + dentry = NULL;
  8230. + if (!dir_ino || S_ISDIR(inode->i_mode))
  8231. + dentry = d_find_alias(inode);
  8232. + else {
  8233. + spin_lock(&inode->i_lock);
  8234. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  8235. + spin_lock(&d->d_lock);
  8236. + if (!au_test_anon(d)
  8237. + && d->d_parent->d_inode->i_ino == dir_ino) {
  8238. + dentry = dget_dlock(d);
  8239. + spin_unlock(&d->d_lock);
  8240. + break;
  8241. + }
  8242. + spin_unlock(&d->d_lock);
  8243. + }
  8244. + spin_unlock(&inode->i_lock);
  8245. + }
  8246. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  8247. + /* need to refresh */
  8248. + dput(dentry);
  8249. + dentry = NULL;
  8250. + }
  8251. +
  8252. +out_iput:
  8253. + iput(inode);
  8254. +out:
  8255. + AuTraceErrPtr(dentry);
  8256. + return dentry;
  8257. +}
  8258. +
  8259. +/* ---------------------------------------------------------------------- */
  8260. +
  8261. +/* todo: dirty? */
  8262. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  8263. +
  8264. +struct au_compare_mnt_args {
  8265. + /* input */
  8266. + struct super_block *sb;
  8267. +
  8268. + /* output */
  8269. + struct vfsmount *mnt;
  8270. +};
  8271. +
  8272. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  8273. +{
  8274. + struct au_compare_mnt_args *a = arg;
  8275. +
  8276. + if (mnt->mnt_sb != a->sb)
  8277. + return 0;
  8278. + a->mnt = mntget(mnt);
  8279. + return 1;
  8280. +}
  8281. +
  8282. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  8283. +{
  8284. + int err;
  8285. + struct path root;
  8286. + struct au_compare_mnt_args args = {
  8287. + .sb = sb
  8288. + };
  8289. +
  8290. + get_fs_root(current->fs, &root);
  8291. + rcu_read_lock();
  8292. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  8293. + rcu_read_unlock();
  8294. + path_put(&root);
  8295. + AuDebugOn(!err);
  8296. + AuDebugOn(!args.mnt);
  8297. + return args.mnt;
  8298. +}
  8299. +
  8300. +struct au_nfsd_si_lock {
  8301. + unsigned int sigen;
  8302. + aufs_bindex_t bindex, br_id;
  8303. + unsigned char force_lock;
  8304. +};
  8305. +
  8306. +static int si_nfsd_read_lock(struct super_block *sb,
  8307. + struct au_nfsd_si_lock *nsi_lock)
  8308. +{
  8309. + int err;
  8310. + aufs_bindex_t bindex;
  8311. +
  8312. + si_read_lock(sb, AuLock_FLUSH);
  8313. +
  8314. + /* branch id may be wrapped around */
  8315. + err = 0;
  8316. + bindex = au_br_index(sb, nsi_lock->br_id);
  8317. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  8318. + goto out; /* success */
  8319. +
  8320. + err = -ESTALE;
  8321. + bindex = -1;
  8322. + if (!nsi_lock->force_lock)
  8323. + si_read_unlock(sb);
  8324. +
  8325. +out:
  8326. + nsi_lock->bindex = bindex;
  8327. + return err;
  8328. +}
  8329. +
  8330. +struct find_name_by_ino {
  8331. + struct dir_context ctx;
  8332. + int called, found;
  8333. + ino_t ino;
  8334. + char *name;
  8335. + int namelen;
  8336. +};
  8337. +
  8338. +static int
  8339. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  8340. + loff_t offset, u64 ino, unsigned int d_type)
  8341. +{
  8342. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  8343. + ctx);
  8344. +
  8345. + a->called++;
  8346. + if (a->ino != ino)
  8347. + return 0;
  8348. +
  8349. + memcpy(a->name, name, namelen);
  8350. + a->namelen = namelen;
  8351. + a->found = 1;
  8352. + return 1;
  8353. +}
  8354. +
  8355. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  8356. + struct au_nfsd_si_lock *nsi_lock)
  8357. +{
  8358. + struct dentry *dentry, *parent;
  8359. + struct file *file;
  8360. + struct inode *dir;
  8361. + struct find_name_by_ino arg = {
  8362. + .ctx = {
  8363. + .actor = find_name_by_ino
  8364. + }
  8365. + };
  8366. + int err;
  8367. +
  8368. + parent = path->dentry;
  8369. + if (nsi_lock)
  8370. + si_read_unlock(parent->d_sb);
  8371. + file = vfsub_dentry_open(path, au_dir_roflags);
  8372. + dentry = (void *)file;
  8373. + if (IS_ERR(file))
  8374. + goto out;
  8375. +
  8376. + dentry = ERR_PTR(-ENOMEM);
  8377. + arg.name = (void *)__get_free_page(GFP_NOFS);
  8378. + if (unlikely(!arg.name))
  8379. + goto out_file;
  8380. + arg.ino = ino;
  8381. + arg.found = 0;
  8382. + do {
  8383. + arg.called = 0;
  8384. + /* smp_mb(); */
  8385. + err = vfsub_iterate_dir(file, &arg.ctx);
  8386. + } while (!err && !arg.found && arg.called);
  8387. + dentry = ERR_PTR(err);
  8388. + if (unlikely(err))
  8389. + goto out_name;
  8390. + /* instead of ENOENT */
  8391. + dentry = ERR_PTR(-ESTALE);
  8392. + if (!arg.found)
  8393. + goto out_name;
  8394. +
  8395. + /* do not call vfsub_lkup_one() */
  8396. + dir = parent->d_inode;
  8397. + mutex_lock(&dir->i_mutex);
  8398. + dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
  8399. + mutex_unlock(&dir->i_mutex);
  8400. + AuTraceErrPtr(dentry);
  8401. + if (IS_ERR(dentry))
  8402. + goto out_name;
  8403. + AuDebugOn(au_test_anon(dentry));
  8404. + if (unlikely(!dentry->d_inode)) {
  8405. + dput(dentry);
  8406. + dentry = ERR_PTR(-ENOENT);
  8407. + }
  8408. +
  8409. +out_name:
  8410. + free_page((unsigned long)arg.name);
  8411. +out_file:
  8412. + fput(file);
  8413. +out:
  8414. + if (unlikely(nsi_lock
  8415. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  8416. + if (!IS_ERR(dentry)) {
  8417. + dput(dentry);
  8418. + dentry = ERR_PTR(-ESTALE);
  8419. + }
  8420. + AuTraceErrPtr(dentry);
  8421. + return dentry;
  8422. +}
  8423. +
  8424. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  8425. + ino_t dir_ino,
  8426. + struct au_nfsd_si_lock *nsi_lock)
  8427. +{
  8428. + struct dentry *dentry;
  8429. + struct path path;
  8430. +
  8431. + if (dir_ino != AUFS_ROOT_INO) {
  8432. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  8433. + dentry = path.dentry;
  8434. + if (!path.dentry || IS_ERR(path.dentry))
  8435. + goto out;
  8436. + AuDebugOn(au_test_anon(path.dentry));
  8437. + } else
  8438. + path.dentry = dget(sb->s_root);
  8439. +
  8440. + path.mnt = au_mnt_get(sb);
  8441. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  8442. + path_put(&path);
  8443. +
  8444. +out:
  8445. + AuTraceErrPtr(dentry);
  8446. + return dentry;
  8447. +}
  8448. +
  8449. +/* ---------------------------------------------------------------------- */
  8450. +
  8451. +static int h_acceptable(void *expv, struct dentry *dentry)
  8452. +{
  8453. + return 1;
  8454. +}
  8455. +
  8456. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  8457. + char *buf, int len, struct super_block *sb)
  8458. +{
  8459. + char *p;
  8460. + int n;
  8461. + struct path path;
  8462. +
  8463. + p = d_path(h_rootpath, buf, len);
  8464. + if (IS_ERR(p))
  8465. + goto out;
  8466. + n = strlen(p);
  8467. +
  8468. + path.mnt = h_rootpath->mnt;
  8469. + path.dentry = h_parent;
  8470. + p = d_path(&path, buf, len);
  8471. + if (IS_ERR(p))
  8472. + goto out;
  8473. + if (n != 1)
  8474. + p += n;
  8475. +
  8476. + path.mnt = au_mnt_get(sb);
  8477. + path.dentry = sb->s_root;
  8478. + p = d_path(&path, buf, len - strlen(p));
  8479. + mntput(path.mnt);
  8480. + if (IS_ERR(p))
  8481. + goto out;
  8482. + if (n != 1)
  8483. + p[strlen(p)] = '/';
  8484. +
  8485. +out:
  8486. + AuTraceErrPtr(p);
  8487. + return p;
  8488. +}
  8489. +
  8490. +static
  8491. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  8492. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  8493. +{
  8494. + struct dentry *dentry, *h_parent, *root;
  8495. + struct super_block *h_sb;
  8496. + char *pathname, *p;
  8497. + struct vfsmount *h_mnt;
  8498. + struct au_branch *br;
  8499. + int err;
  8500. + struct path path;
  8501. +
  8502. + br = au_sbr(sb, nsi_lock->bindex);
  8503. + h_mnt = au_br_mnt(br);
  8504. + h_sb = h_mnt->mnt_sb;
  8505. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  8506. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  8507. + fh_len - Fh_tail, fh[Fh_h_type],
  8508. + h_acceptable, /*context*/NULL);
  8509. + dentry = h_parent;
  8510. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  8511. + AuWarn1("%s decode_fh failed, %ld\n",
  8512. + au_sbtype(h_sb), PTR_ERR(h_parent));
  8513. + goto out;
  8514. + }
  8515. + dentry = NULL;
  8516. + if (unlikely(au_test_anon(h_parent))) {
  8517. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  8518. + au_sbtype(h_sb));
  8519. + goto out_h_parent;
  8520. + }
  8521. +
  8522. + dentry = ERR_PTR(-ENOMEM);
  8523. + pathname = (void *)__get_free_page(GFP_NOFS);
  8524. + if (unlikely(!pathname))
  8525. + goto out_h_parent;
  8526. +
  8527. + root = sb->s_root;
  8528. + path.mnt = h_mnt;
  8529. + di_read_lock_parent(root, !AuLock_IR);
  8530. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  8531. + di_read_unlock(root, !AuLock_IR);
  8532. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  8533. + dentry = (void *)p;
  8534. + if (IS_ERR(p))
  8535. + goto out_pathname;
  8536. +
  8537. + si_read_unlock(sb);
  8538. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  8539. + dentry = ERR_PTR(err);
  8540. + if (unlikely(err))
  8541. + goto out_relock;
  8542. +
  8543. + dentry = ERR_PTR(-ENOENT);
  8544. + AuDebugOn(au_test_anon(path.dentry));
  8545. + if (unlikely(!path.dentry->d_inode))
  8546. + goto out_path;
  8547. +
  8548. + if (ino != path.dentry->d_inode->i_ino)
  8549. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  8550. + else
  8551. + dentry = dget(path.dentry);
  8552. +
  8553. +out_path:
  8554. + path_put(&path);
  8555. +out_relock:
  8556. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  8557. + if (!IS_ERR(dentry)) {
  8558. + dput(dentry);
  8559. + dentry = ERR_PTR(-ESTALE);
  8560. + }
  8561. +out_pathname:
  8562. + free_page((unsigned long)pathname);
  8563. +out_h_parent:
  8564. + dput(h_parent);
  8565. +out:
  8566. + AuTraceErrPtr(dentry);
  8567. + return dentry;
  8568. +}
  8569. +
  8570. +/* ---------------------------------------------------------------------- */
  8571. +
  8572. +static struct dentry *
  8573. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  8574. + int fh_type)
  8575. +{
  8576. + struct dentry *dentry;
  8577. + __u32 *fh = fid->raw;
  8578. + struct au_branch *br;
  8579. + ino_t ino, dir_ino;
  8580. + struct au_nfsd_si_lock nsi_lock = {
  8581. + .force_lock = 0
  8582. + };
  8583. +
  8584. + dentry = ERR_PTR(-ESTALE);
  8585. + /* it should never happen, but the file handle is unreliable */
  8586. + if (unlikely(fh_len < Fh_tail))
  8587. + goto out;
  8588. + nsi_lock.sigen = fh[Fh_sigen];
  8589. + nsi_lock.br_id = fh[Fh_br_id];
  8590. +
  8591. + /* branch id may be wrapped around */
  8592. + br = NULL;
  8593. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  8594. + goto out;
  8595. + nsi_lock.force_lock = 1;
  8596. +
  8597. + /* is this inode still cached? */
  8598. + ino = decode_ino(fh + Fh_ino);
  8599. + /* it should never happen */
  8600. + if (unlikely(ino == AUFS_ROOT_INO))
  8601. + goto out;
  8602. +
  8603. + dir_ino = decode_ino(fh + Fh_dir_ino);
  8604. + dentry = decode_by_ino(sb, ino, dir_ino);
  8605. + if (IS_ERR(dentry))
  8606. + goto out_unlock;
  8607. + if (dentry)
  8608. + goto accept;
  8609. +
  8610. + /* is the parent dir cached? */
  8611. + br = au_sbr(sb, nsi_lock.bindex);
  8612. + atomic_inc(&br->br_count);
  8613. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  8614. + if (IS_ERR(dentry))
  8615. + goto out_unlock;
  8616. + if (dentry)
  8617. + goto accept;
  8618. +
  8619. + /* lookup path */
  8620. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  8621. + if (IS_ERR(dentry))
  8622. + goto out_unlock;
  8623. + if (unlikely(!dentry))
  8624. + /* todo?: make it ESTALE */
  8625. + goto out_unlock;
  8626. +
  8627. +accept:
  8628. + if (!au_digen_test(dentry, au_sigen(sb))
  8629. + && dentry->d_inode->i_generation == fh[Fh_igen])
  8630. + goto out_unlock; /* success */
  8631. +
  8632. + dput(dentry);
  8633. + dentry = ERR_PTR(-ESTALE);
  8634. +out_unlock:
  8635. + if (br)
  8636. + atomic_dec(&br->br_count);
  8637. + si_read_unlock(sb);
  8638. +out:
  8639. + AuTraceErrPtr(dentry);
  8640. + return dentry;
  8641. +}
  8642. +
  8643. +#if 0 /* reserved for future use */
  8644. +/* support subtreecheck option */
  8645. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  8646. + int fh_len, int fh_type)
  8647. +{
  8648. + struct dentry *parent;
  8649. + __u32 *fh = fid->raw;
  8650. + ino_t dir_ino;
  8651. +
  8652. + dir_ino = decode_ino(fh + Fh_dir_ino);
  8653. + parent = decode_by_ino(sb, dir_ino, 0);
  8654. + if (IS_ERR(parent))
  8655. + goto out;
  8656. + if (!parent)
  8657. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  8658. + dir_ino, fh, fh_len);
  8659. +
  8660. +out:
  8661. + AuTraceErrPtr(parent);
  8662. + return parent;
  8663. +}
  8664. +#endif
  8665. +
  8666. +/* ---------------------------------------------------------------------- */
  8667. +
  8668. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  8669. + struct inode *dir)
  8670. +{
  8671. + int err;
  8672. + aufs_bindex_t bindex;
  8673. + struct super_block *sb, *h_sb;
  8674. + struct dentry *dentry, *parent, *h_parent;
  8675. + struct inode *h_dir;
  8676. + struct au_branch *br;
  8677. +
  8678. + err = -ENOSPC;
  8679. + if (unlikely(*max_len <= Fh_tail)) {
  8680. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  8681. + goto out;
  8682. + }
  8683. +
  8684. + err = FILEID_ROOT;
  8685. + if (inode->i_ino == AUFS_ROOT_INO) {
  8686. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  8687. + goto out;
  8688. + }
  8689. +
  8690. + h_parent = NULL;
  8691. + sb = inode->i_sb;
  8692. + err = si_read_lock(sb, AuLock_FLUSH);
  8693. + if (unlikely(err))
  8694. + goto out;
  8695. +
  8696. +#ifdef CONFIG_AUFS_DEBUG
  8697. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  8698. + AuWarn1("NFS-exporting requires xino\n");
  8699. +#endif
  8700. + err = -EIO;
  8701. + parent = NULL;
  8702. + ii_read_lock_child(inode);
  8703. + bindex = au_ibstart(inode);
  8704. + if (!dir) {
  8705. + dentry = d_find_any_alias(inode);
  8706. + if (unlikely(!dentry))
  8707. + goto out_unlock;
  8708. + AuDebugOn(au_test_anon(dentry));
  8709. + parent = dget_parent(dentry);
  8710. + dput(dentry);
  8711. + if (unlikely(!parent))
  8712. + goto out_unlock;
  8713. + dir = parent->d_inode;
  8714. + }
  8715. +
  8716. + ii_read_lock_parent(dir);
  8717. + h_dir = au_h_iptr(dir, bindex);
  8718. + ii_read_unlock(dir);
  8719. + if (unlikely(!h_dir))
  8720. + goto out_parent;
  8721. + h_parent = d_find_any_alias(h_dir);
  8722. + if (unlikely(!h_parent))
  8723. + goto out_hparent;
  8724. +
  8725. + err = -EPERM;
  8726. + br = au_sbr(sb, bindex);
  8727. + h_sb = au_br_sb(br);
  8728. + if (unlikely(!h_sb->s_export_op)) {
  8729. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  8730. + goto out_hparent;
  8731. + }
  8732. +
  8733. + fh[Fh_br_id] = br->br_id;
  8734. + fh[Fh_sigen] = au_sigen(sb);
  8735. + encode_ino(fh + Fh_ino, inode->i_ino);
  8736. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  8737. + fh[Fh_igen] = inode->i_generation;
  8738. +
  8739. + *max_len -= Fh_tail;
  8740. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  8741. + max_len,
  8742. + /*connectable or subtreecheck*/0);
  8743. + err = fh[Fh_h_type];
  8744. + *max_len += Fh_tail;
  8745. + /* todo: macros? */
  8746. + if (err != FILEID_INVALID)
  8747. + err = 99;
  8748. + else
  8749. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  8750. +
  8751. +out_hparent:
  8752. + dput(h_parent);
  8753. +out_parent:
  8754. + dput(parent);
  8755. +out_unlock:
  8756. + ii_read_unlock(inode);
  8757. + si_read_unlock(sb);
  8758. +out:
  8759. + if (unlikely(err < 0))
  8760. + err = FILEID_INVALID;
  8761. + return err;
  8762. +}
  8763. +
  8764. +/* ---------------------------------------------------------------------- */
  8765. +
  8766. +static int aufs_commit_metadata(struct inode *inode)
  8767. +{
  8768. + int err;
  8769. + aufs_bindex_t bindex;
  8770. + struct super_block *sb;
  8771. + struct inode *h_inode;
  8772. + int (*f)(struct inode *inode);
  8773. +
  8774. + sb = inode->i_sb;
  8775. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  8776. + ii_write_lock_child(inode);
  8777. + bindex = au_ibstart(inode);
  8778. + AuDebugOn(bindex < 0);
  8779. + h_inode = au_h_iptr(inode, bindex);
  8780. +
  8781. + f = h_inode->i_sb->s_export_op->commit_metadata;
  8782. + if (f)
  8783. + err = f(h_inode);
  8784. + else {
  8785. + struct writeback_control wbc = {
  8786. + .sync_mode = WB_SYNC_ALL,
  8787. + .nr_to_write = 0 /* metadata only */
  8788. + };
  8789. +
  8790. + err = sync_inode(h_inode, &wbc);
  8791. + }
  8792. +
  8793. + au_cpup_attr_timesizes(inode);
  8794. + ii_write_unlock(inode);
  8795. + si_read_unlock(sb);
  8796. + return err;
  8797. +}
  8798. +
  8799. +/* ---------------------------------------------------------------------- */
  8800. +
  8801. +static struct export_operations aufs_export_op = {
  8802. + .fh_to_dentry = aufs_fh_to_dentry,
  8803. + /* .fh_to_parent = aufs_fh_to_parent, */
  8804. + .encode_fh = aufs_encode_fh,
  8805. + .commit_metadata = aufs_commit_metadata
  8806. +};
  8807. +
  8808. +void au_export_init(struct super_block *sb)
  8809. +{
  8810. + struct au_sbinfo *sbinfo;
  8811. + __u32 u;
  8812. +
  8813. + sb->s_export_op = &aufs_export_op;
  8814. + sbinfo = au_sbi(sb);
  8815. + sbinfo->si_xigen = NULL;
  8816. + get_random_bytes(&u, sizeof(u));
  8817. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  8818. + atomic_set(&sbinfo->si_xigen_next, u);
  8819. +}
  8820. diff -Nur linux-4.0.5.orig/fs/aufs/fhsm.c linux-4.0.5/fs/aufs/fhsm.c
  8821. --- linux-4.0.5.orig/fs/aufs/fhsm.c 1969-12-31 18:00:00.000000000 -0600
  8822. +++ linux-4.0.5/fs/aufs/fhsm.c 2015-06-21 14:07:48.686432781 -0500
  8823. @@ -0,0 +1,426 @@
  8824. +/*
  8825. + * Copyright (C) 2011-2015 Junjiro R. Okajima
  8826. + *
  8827. + * This program, aufs is free software; you can redistribute it and/or modify
  8828. + * it under the terms of the GNU General Public License as published by
  8829. + * the Free Software Foundation; either version 2 of the License, or
  8830. + * (at your option) any later version.
  8831. + *
  8832. + * This program is distributed in the hope that it will be useful,
  8833. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8834. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8835. + * GNU General Public License for more details.
  8836. + *
  8837. + * You should have received a copy of the GNU General Public License
  8838. + * along with this program; if not, write to the Free Software
  8839. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  8840. + */
  8841. +
  8842. +/*
  8843. + * File-based Hierarchy Storage Management
  8844. + */
  8845. +
  8846. +#include <linux/anon_inodes.h>
  8847. +#include <linux/poll.h>
  8848. +#include <linux/seq_file.h>
  8849. +#include <linux/statfs.h>
  8850. +#include "aufs.h"
  8851. +
  8852. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  8853. +{
  8854. + struct au_sbinfo *sbinfo;
  8855. + struct au_fhsm *fhsm;
  8856. +
  8857. + SiMustAnyLock(sb);
  8858. +
  8859. + sbinfo = au_sbi(sb);
  8860. + fhsm = &sbinfo->si_fhsm;
  8861. + AuDebugOn(!fhsm);
  8862. + return fhsm->fhsm_bottom;
  8863. +}
  8864. +
  8865. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  8866. +{
  8867. + struct au_sbinfo *sbinfo;
  8868. + struct au_fhsm *fhsm;
  8869. +
  8870. + SiMustWriteLock(sb);
  8871. +
  8872. + sbinfo = au_sbi(sb);
  8873. + fhsm = &sbinfo->si_fhsm;
  8874. + AuDebugOn(!fhsm);
  8875. + fhsm->fhsm_bottom = bindex;
  8876. +}
  8877. +
  8878. +/* ---------------------------------------------------------------------- */
  8879. +
  8880. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  8881. +{
  8882. + struct au_br_fhsm *bf;
  8883. +
  8884. + bf = br->br_fhsm;
  8885. + MtxMustLock(&bf->bf_lock);
  8886. +
  8887. + return !bf->bf_readable
  8888. + || time_after(jiffies,
  8889. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  8890. +}
  8891. +
  8892. +/* ---------------------------------------------------------------------- */
  8893. +
  8894. +static void au_fhsm_notify(struct super_block *sb, int val)
  8895. +{
  8896. + struct au_sbinfo *sbinfo;
  8897. + struct au_fhsm *fhsm;
  8898. +
  8899. + SiMustAnyLock(sb);
  8900. +
  8901. + sbinfo = au_sbi(sb);
  8902. + fhsm = &sbinfo->si_fhsm;
  8903. + if (au_fhsm_pid(fhsm)
  8904. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  8905. + atomic_set(&fhsm->fhsm_readable, val);
  8906. + if (val)
  8907. + wake_up(&fhsm->fhsm_wqh);
  8908. + }
  8909. +}
  8910. +
  8911. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  8912. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  8913. +{
  8914. + int err;
  8915. + struct au_branch *br;
  8916. + struct au_br_fhsm *bf;
  8917. +
  8918. + br = au_sbr(sb, bindex);
  8919. + AuDebugOn(au_br_rdonly(br));
  8920. + bf = br->br_fhsm;
  8921. + AuDebugOn(!bf);
  8922. +
  8923. + if (do_lock)
  8924. + mutex_lock(&bf->bf_lock);
  8925. + else
  8926. + MtxMustLock(&bf->bf_lock);
  8927. +
  8928. + /* sb->s_root for NFS is unreliable */
  8929. + err = au_br_stfs(br, &bf->bf_stfs);
  8930. + if (unlikely(err)) {
  8931. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  8932. + goto out;
  8933. + }
  8934. +
  8935. + bf->bf_jiffy = jiffies;
  8936. + bf->bf_readable = 1;
  8937. + if (do_notify)
  8938. + au_fhsm_notify(sb, /*val*/1);
  8939. + if (rstfs)
  8940. + *rstfs = bf->bf_stfs;
  8941. +
  8942. +out:
  8943. + if (do_lock)
  8944. + mutex_unlock(&bf->bf_lock);
  8945. + au_fhsm_notify(sb, /*val*/1);
  8946. +
  8947. + return err;
  8948. +}
  8949. +
  8950. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  8951. +{
  8952. + int err;
  8953. + struct au_sbinfo *sbinfo;
  8954. + struct au_fhsm *fhsm;
  8955. + struct au_branch *br;
  8956. + struct au_br_fhsm *bf;
  8957. +
  8958. + AuDbg("b%d, force %d\n", bindex, force);
  8959. + SiMustAnyLock(sb);
  8960. +
  8961. + sbinfo = au_sbi(sb);
  8962. + fhsm = &sbinfo->si_fhsm;
  8963. + if (!au_ftest_si(sbinfo, FHSM)
  8964. + || fhsm->fhsm_bottom == bindex)
  8965. + return;
  8966. +
  8967. + br = au_sbr(sb, bindex);
  8968. + bf = br->br_fhsm;
  8969. + AuDebugOn(!bf);
  8970. + mutex_lock(&bf->bf_lock);
  8971. + if (force
  8972. + || au_fhsm_pid(fhsm)
  8973. + || au_fhsm_test_jiffy(sbinfo, br))
  8974. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  8975. + /*do_notify*/1);
  8976. + mutex_unlock(&bf->bf_lock);
  8977. +}
  8978. +
  8979. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  8980. +{
  8981. + aufs_bindex_t bindex, bend;
  8982. + struct au_branch *br;
  8983. +
  8984. + /* exclude the bottom */
  8985. + bend = au_fhsm_bottom(sb);
  8986. + for (bindex = 0; bindex < bend; bindex++) {
  8987. + br = au_sbr(sb, bindex);
  8988. + if (au_br_fhsm(br->br_perm))
  8989. + au_fhsm_wrote(sb, bindex, force);
  8990. + }
  8991. +}
  8992. +
  8993. +/* ---------------------------------------------------------------------- */
  8994. +
  8995. +static unsigned int au_fhsm_poll(struct file *file,
  8996. + struct poll_table_struct *wait)
  8997. +{
  8998. + unsigned int mask;
  8999. + struct au_sbinfo *sbinfo;
  9000. + struct au_fhsm *fhsm;
  9001. +
  9002. + mask = 0;
  9003. + sbinfo = file->private_data;
  9004. + fhsm = &sbinfo->si_fhsm;
  9005. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  9006. + if (atomic_read(&fhsm->fhsm_readable))
  9007. + mask = POLLIN /* | POLLRDNORM */;
  9008. +
  9009. + AuTraceErr((int)mask);
  9010. + return mask;
  9011. +}
  9012. +
  9013. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  9014. + struct aufs_stfs *stfs, __s16 brid)
  9015. +{
  9016. + int err;
  9017. +
  9018. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  9019. + if (!err)
  9020. + err = __put_user(brid, &stbr->brid);
  9021. + if (unlikely(err))
  9022. + err = -EFAULT;
  9023. +
  9024. + return err;
  9025. +}
  9026. +
  9027. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  9028. + struct aufs_stbr __user *stbr, size_t count)
  9029. +{
  9030. + ssize_t err;
  9031. + int nstbr;
  9032. + aufs_bindex_t bindex, bend;
  9033. + struct au_branch *br;
  9034. + struct au_br_fhsm *bf;
  9035. +
  9036. + /* except the bottom branch */
  9037. + err = 0;
  9038. + nstbr = 0;
  9039. + bend = au_fhsm_bottom(sb);
  9040. + for (bindex = 0; !err && bindex < bend; bindex++) {
  9041. + br = au_sbr(sb, bindex);
  9042. + if (!au_br_fhsm(br->br_perm))
  9043. + continue;
  9044. +
  9045. + bf = br->br_fhsm;
  9046. + mutex_lock(&bf->bf_lock);
  9047. + if (bf->bf_readable) {
  9048. + err = -EFAULT;
  9049. + if (count >= sizeof(*stbr))
  9050. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  9051. + br->br_id);
  9052. + if (!err) {
  9053. + bf->bf_readable = 0;
  9054. + count -= sizeof(*stbr);
  9055. + nstbr++;
  9056. + }
  9057. + }
  9058. + mutex_unlock(&bf->bf_lock);
  9059. + }
  9060. + if (!err)
  9061. + err = sizeof(*stbr) * nstbr;
  9062. +
  9063. + return err;
  9064. +}
  9065. +
  9066. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  9067. + loff_t *pos)
  9068. +{
  9069. + ssize_t err;
  9070. + int readable;
  9071. + aufs_bindex_t nfhsm, bindex, bend;
  9072. + struct au_sbinfo *sbinfo;
  9073. + struct au_fhsm *fhsm;
  9074. + struct au_branch *br;
  9075. + struct super_block *sb;
  9076. +
  9077. + err = 0;
  9078. + sbinfo = file->private_data;
  9079. + fhsm = &sbinfo->si_fhsm;
  9080. +need_data:
  9081. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  9082. + if (!atomic_read(&fhsm->fhsm_readable)) {
  9083. + if (vfsub_file_flags(file) & O_NONBLOCK)
  9084. + err = -EAGAIN;
  9085. + else
  9086. + err = wait_event_interruptible_locked_irq
  9087. + (fhsm->fhsm_wqh,
  9088. + atomic_read(&fhsm->fhsm_readable));
  9089. + }
  9090. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  9091. + if (unlikely(err))
  9092. + goto out;
  9093. +
  9094. + /* sb may already be dead */
  9095. + au_rw_read_lock(&sbinfo->si_rwsem);
  9096. + readable = atomic_read(&fhsm->fhsm_readable);
  9097. + if (readable > 0) {
  9098. + sb = sbinfo->si_sb;
  9099. + AuDebugOn(!sb);
  9100. + /* exclude the bottom branch */
  9101. + nfhsm = 0;
  9102. + bend = au_fhsm_bottom(sb);
  9103. + for (bindex = 0; bindex < bend; bindex++) {
  9104. + br = au_sbr(sb, bindex);
  9105. + if (au_br_fhsm(br->br_perm))
  9106. + nfhsm++;
  9107. + }
  9108. + err = -EMSGSIZE;
  9109. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  9110. + atomic_set(&fhsm->fhsm_readable, 0);
  9111. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  9112. + count);
  9113. + }
  9114. + }
  9115. + au_rw_read_unlock(&sbinfo->si_rwsem);
  9116. + if (!readable)
  9117. + goto need_data;
  9118. +
  9119. +out:
  9120. + return err;
  9121. +}
  9122. +
  9123. +static int au_fhsm_release(struct inode *inode, struct file *file)
  9124. +{
  9125. + struct au_sbinfo *sbinfo;
  9126. + struct au_fhsm *fhsm;
  9127. +
  9128. + /* sb may already be dead */
  9129. + sbinfo = file->private_data;
  9130. + fhsm = &sbinfo->si_fhsm;
  9131. + spin_lock(&fhsm->fhsm_spin);
  9132. + fhsm->fhsm_pid = 0;
  9133. + spin_unlock(&fhsm->fhsm_spin);
  9134. + kobject_put(&sbinfo->si_kobj);
  9135. +
  9136. + return 0;
  9137. +}
  9138. +
  9139. +static const struct file_operations au_fhsm_fops = {
  9140. + .owner = THIS_MODULE,
  9141. + .llseek = noop_llseek,
  9142. + .read = au_fhsm_read,
  9143. + .poll = au_fhsm_poll,
  9144. + .release = au_fhsm_release
  9145. +};
  9146. +
  9147. +int au_fhsm_fd(struct super_block *sb, int oflags)
  9148. +{
  9149. + int err, fd;
  9150. + struct au_sbinfo *sbinfo;
  9151. + struct au_fhsm *fhsm;
  9152. +
  9153. + err = -EPERM;
  9154. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  9155. + goto out;
  9156. +
  9157. + err = -EINVAL;
  9158. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  9159. + goto out;
  9160. +
  9161. + err = 0;
  9162. + sbinfo = au_sbi(sb);
  9163. + fhsm = &sbinfo->si_fhsm;
  9164. + spin_lock(&fhsm->fhsm_spin);
  9165. + if (!fhsm->fhsm_pid)
  9166. + fhsm->fhsm_pid = current->pid;
  9167. + else
  9168. + err = -EBUSY;
  9169. + spin_unlock(&fhsm->fhsm_spin);
  9170. + if (unlikely(err))
  9171. + goto out;
  9172. +
  9173. + oflags |= O_RDONLY;
  9174. + /* oflags |= FMODE_NONOTIFY; */
  9175. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  9176. + err = fd;
  9177. + if (unlikely(fd < 0))
  9178. + goto out_pid;
  9179. +
  9180. + /* succeed reglardless 'fhsm' status */
  9181. + kobject_get(&sbinfo->si_kobj);
  9182. + si_noflush_read_lock(sb);
  9183. + if (au_ftest_si(sbinfo, FHSM))
  9184. + au_fhsm_wrote_all(sb, /*force*/0);
  9185. + si_read_unlock(sb);
  9186. + goto out; /* success */
  9187. +
  9188. +out_pid:
  9189. + spin_lock(&fhsm->fhsm_spin);
  9190. + fhsm->fhsm_pid = 0;
  9191. + spin_unlock(&fhsm->fhsm_spin);
  9192. +out:
  9193. + AuTraceErr(err);
  9194. + return err;
  9195. +}
  9196. +
  9197. +/* ---------------------------------------------------------------------- */
  9198. +
  9199. +int au_fhsm_br_alloc(struct au_branch *br)
  9200. +{
  9201. + int err;
  9202. +
  9203. + err = 0;
  9204. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  9205. + if (br->br_fhsm)
  9206. + au_br_fhsm_init(br->br_fhsm);
  9207. + else
  9208. + err = -ENOMEM;
  9209. +
  9210. + return err;
  9211. +}
  9212. +
  9213. +/* ---------------------------------------------------------------------- */
  9214. +
  9215. +void au_fhsm_fin(struct super_block *sb)
  9216. +{
  9217. + au_fhsm_notify(sb, /*val*/-1);
  9218. +}
  9219. +
  9220. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  9221. +{
  9222. + struct au_fhsm *fhsm;
  9223. +
  9224. + fhsm = &sbinfo->si_fhsm;
  9225. + spin_lock_init(&fhsm->fhsm_spin);
  9226. + init_waitqueue_head(&fhsm->fhsm_wqh);
  9227. + atomic_set(&fhsm->fhsm_readable, 0);
  9228. + fhsm->fhsm_expire
  9229. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  9230. + fhsm->fhsm_bottom = -1;
  9231. +}
  9232. +
  9233. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  9234. +{
  9235. + sbinfo->si_fhsm.fhsm_expire
  9236. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  9237. +}
  9238. +
  9239. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  9240. +{
  9241. + unsigned int u;
  9242. +
  9243. + if (!au_ftest_si(sbinfo, FHSM))
  9244. + return;
  9245. +
  9246. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  9247. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  9248. + seq_printf(seq, ",fhsm_sec=%u", u);
  9249. +}
  9250. diff -Nur linux-4.0.5.orig/fs/aufs/file.c linux-4.0.5/fs/aufs/file.c
  9251. --- linux-4.0.5.orig/fs/aufs/file.c 1969-12-31 18:00:00.000000000 -0600
  9252. +++ linux-4.0.5/fs/aufs/file.c 2015-06-21 14:07:48.686432781 -0500
  9253. @@ -0,0 +1,844 @@
  9254. +/*
  9255. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  9256. + *
  9257. + * This program, aufs is free software; you can redistribute it and/or modify
  9258. + * it under the terms of the GNU General Public License as published by
  9259. + * the Free Software Foundation; either version 2 of the License, or
  9260. + * (at your option) any later version.
  9261. + *
  9262. + * This program is distributed in the hope that it will be useful,
  9263. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9264. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9265. + * GNU General Public License for more details.
  9266. + *
  9267. + * You should have received a copy of the GNU General Public License
  9268. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9269. + */
  9270. +
  9271. +/*
  9272. + * handling file/dir, and address_space operation
  9273. + */
  9274. +
  9275. +#ifdef CONFIG_AUFS_DEBUG
  9276. +#include <linux/migrate.h>
  9277. +#endif
  9278. +#include <linux/pagemap.h>
  9279. +#include "aufs.h"
  9280. +
  9281. +/* drop flags for writing */
  9282. +unsigned int au_file_roflags(unsigned int flags)
  9283. +{
  9284. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  9285. + flags |= O_RDONLY | O_NOATIME;
  9286. + return flags;
  9287. +}
  9288. +
  9289. +/* common functions to regular file and dir */
  9290. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  9291. + struct file *file, int force_wr)
  9292. +{
  9293. + struct file *h_file;
  9294. + struct dentry *h_dentry;
  9295. + struct inode *h_inode;
  9296. + struct super_block *sb;
  9297. + struct au_branch *br;
  9298. + struct path h_path;
  9299. + int err;
  9300. +
  9301. + /* a race condition can happen between open and unlink/rmdir */
  9302. + h_file = ERR_PTR(-ENOENT);
  9303. + h_dentry = au_h_dptr(dentry, bindex);
  9304. + if (au_test_nfsd() && !h_dentry)
  9305. + goto out;
  9306. + h_inode = h_dentry->d_inode;
  9307. + if (au_test_nfsd() && !h_inode)
  9308. + goto out;
  9309. + spin_lock(&h_dentry->d_lock);
  9310. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  9311. + || !h_inode
  9312. + /* || !dentry->d_inode->i_nlink */
  9313. + ;
  9314. + spin_unlock(&h_dentry->d_lock);
  9315. + if (unlikely(err))
  9316. + goto out;
  9317. +
  9318. + sb = dentry->d_sb;
  9319. + br = au_sbr(sb, bindex);
  9320. + err = au_br_test_oflag(flags, br);
  9321. + h_file = ERR_PTR(err);
  9322. + if (unlikely(err))
  9323. + goto out;
  9324. +
  9325. + /* drop flags for writing */
  9326. + if (au_test_ro(sb, bindex, dentry->d_inode)) {
  9327. + if (force_wr && !(flags & O_WRONLY))
  9328. + force_wr = 0;
  9329. + flags = au_file_roflags(flags);
  9330. + if (force_wr) {
  9331. + h_file = ERR_PTR(-EROFS);
  9332. + flags = au_file_roflags(flags);
  9333. + if (unlikely(vfsub_native_ro(h_inode)
  9334. + || IS_APPEND(h_inode)))
  9335. + goto out;
  9336. + flags &= ~O_ACCMODE;
  9337. + flags |= O_WRONLY;
  9338. + }
  9339. + }
  9340. + flags &= ~O_CREAT;
  9341. + atomic_inc(&br->br_count);
  9342. + h_path.dentry = h_dentry;
  9343. + h_path.mnt = au_br_mnt(br);
  9344. + h_file = vfsub_dentry_open(&h_path, flags);
  9345. + if (IS_ERR(h_file))
  9346. + goto out_br;
  9347. +
  9348. + if (flags & __FMODE_EXEC) {
  9349. + err = deny_write_access(h_file);
  9350. + if (unlikely(err)) {
  9351. + fput(h_file);
  9352. + h_file = ERR_PTR(err);
  9353. + goto out_br;
  9354. + }
  9355. + }
  9356. + fsnotify_open(h_file);
  9357. + goto out; /* success */
  9358. +
  9359. +out_br:
  9360. + atomic_dec(&br->br_count);
  9361. +out:
  9362. + return h_file;
  9363. +}
  9364. +
  9365. +static int au_cmoo(struct dentry *dentry)
  9366. +{
  9367. + int err, cmoo;
  9368. + unsigned int udba;
  9369. + struct path h_path;
  9370. + struct au_pin pin;
  9371. + struct au_cp_generic cpg = {
  9372. + .dentry = dentry,
  9373. + .bdst = -1,
  9374. + .bsrc = -1,
  9375. + .len = -1,
  9376. + .pin = &pin,
  9377. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  9378. + };
  9379. + struct inode *delegated;
  9380. + struct super_block *sb;
  9381. + struct au_sbinfo *sbinfo;
  9382. + struct au_fhsm *fhsm;
  9383. + pid_t pid;
  9384. + struct au_branch *br;
  9385. + struct dentry *parent;
  9386. + struct au_hinode *hdir;
  9387. +
  9388. + DiMustWriteLock(dentry);
  9389. + IiMustWriteLock(dentry->d_inode);
  9390. +
  9391. + err = 0;
  9392. + if (IS_ROOT(dentry))
  9393. + goto out;
  9394. + cpg.bsrc = au_dbstart(dentry);
  9395. + if (!cpg.bsrc)
  9396. + goto out;
  9397. +
  9398. + sb = dentry->d_sb;
  9399. + sbinfo = au_sbi(sb);
  9400. + fhsm = &sbinfo->si_fhsm;
  9401. + pid = au_fhsm_pid(fhsm);
  9402. + if (pid
  9403. + && (current->pid == pid
  9404. + || current->real_parent->pid == pid))
  9405. + goto out;
  9406. +
  9407. + br = au_sbr(sb, cpg.bsrc);
  9408. + cmoo = au_br_cmoo(br->br_perm);
  9409. + if (!cmoo)
  9410. + goto out;
  9411. + if (!d_is_reg(dentry))
  9412. + cmoo &= AuBrAttr_COO_ALL;
  9413. + if (!cmoo)
  9414. + goto out;
  9415. +
  9416. + parent = dget_parent(dentry);
  9417. + di_write_lock_parent(parent);
  9418. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  9419. + cpg.bdst = err;
  9420. + if (unlikely(err < 0)) {
  9421. + err = 0; /* there is no upper writable branch */
  9422. + goto out_dgrade;
  9423. + }
  9424. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  9425. +
  9426. + /* do not respect the coo attrib for the target branch */
  9427. + err = au_cpup_dirs(dentry, cpg.bdst);
  9428. + if (unlikely(err))
  9429. + goto out_dgrade;
  9430. +
  9431. + di_downgrade_lock(parent, AuLock_IR);
  9432. + udba = au_opt_udba(sb);
  9433. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  9434. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  9435. + if (unlikely(err))
  9436. + goto out_parent;
  9437. +
  9438. + err = au_sio_cpup_simple(&cpg);
  9439. + au_unpin(&pin);
  9440. + if (unlikely(err))
  9441. + goto out_parent;
  9442. + if (!(cmoo & AuBrWAttr_MOO))
  9443. + goto out_parent; /* success */
  9444. +
  9445. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  9446. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  9447. + if (unlikely(err))
  9448. + goto out_parent;
  9449. +
  9450. + h_path.mnt = au_br_mnt(br);
  9451. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  9452. + hdir = au_hi(parent->d_inode, cpg.bsrc);
  9453. + delegated = NULL;
  9454. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  9455. + au_unpin(&pin);
  9456. + /* todo: keep h_dentry or not? */
  9457. + if (unlikely(err == -EWOULDBLOCK)) {
  9458. + pr_warn("cannot retry for NFSv4 delegation"
  9459. + " for an internal unlink\n");
  9460. + iput(delegated);
  9461. + }
  9462. + if (unlikely(err)) {
  9463. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  9464. + dentry, err);
  9465. + err = 0;
  9466. + }
  9467. + goto out_parent; /* success */
  9468. +
  9469. +out_dgrade:
  9470. + di_downgrade_lock(parent, AuLock_IR);
  9471. +out_parent:
  9472. + di_read_unlock(parent, AuLock_IR);
  9473. + dput(parent);
  9474. +out:
  9475. + AuTraceErr(err);
  9476. + return err;
  9477. +}
  9478. +
  9479. +int au_do_open(struct file *file, struct au_do_open_args *args)
  9480. +{
  9481. + int err, no_lock = args->no_lock;
  9482. + struct dentry *dentry;
  9483. + struct au_finfo *finfo;
  9484. +
  9485. + if (!no_lock)
  9486. + err = au_finfo_init(file, args->fidir);
  9487. + else {
  9488. + lockdep_off();
  9489. + err = au_finfo_init(file, args->fidir);
  9490. + lockdep_on();
  9491. + }
  9492. + if (unlikely(err))
  9493. + goto out;
  9494. +
  9495. + dentry = file->f_path.dentry;
  9496. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  9497. + if (!no_lock) {
  9498. + di_write_lock_child(dentry);
  9499. + err = au_cmoo(dentry);
  9500. + di_downgrade_lock(dentry, AuLock_IR);
  9501. + if (!err)
  9502. + err = args->open(file, vfsub_file_flags(file), NULL);
  9503. + di_read_unlock(dentry, AuLock_IR);
  9504. + } else {
  9505. + err = au_cmoo(dentry);
  9506. + if (!err)
  9507. + err = args->open(file, vfsub_file_flags(file),
  9508. + args->h_file);
  9509. + if (!err && au_fbstart(file) != au_dbstart(dentry))
  9510. + /*
  9511. + * cmoo happens after h_file was opened.
  9512. + * need to refresh file later.
  9513. + */
  9514. + atomic_dec(&au_fi(file)->fi_generation);
  9515. + }
  9516. +
  9517. + finfo = au_fi(file);
  9518. + if (!err) {
  9519. + finfo->fi_file = file;
  9520. + au_sphl_add(&finfo->fi_hlist,
  9521. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  9522. + }
  9523. + if (!no_lock)
  9524. + fi_write_unlock(file);
  9525. + else {
  9526. + lockdep_off();
  9527. + fi_write_unlock(file);
  9528. + lockdep_on();
  9529. + }
  9530. + if (unlikely(err)) {
  9531. + finfo->fi_hdir = NULL;
  9532. + au_finfo_fin(file);
  9533. + }
  9534. +
  9535. +out:
  9536. + return err;
  9537. +}
  9538. +
  9539. +int au_reopen_nondir(struct file *file)
  9540. +{
  9541. + int err;
  9542. + aufs_bindex_t bstart;
  9543. + struct dentry *dentry;
  9544. + struct file *h_file, *h_file_tmp;
  9545. +
  9546. + dentry = file->f_path.dentry;
  9547. + bstart = au_dbstart(dentry);
  9548. + h_file_tmp = NULL;
  9549. + if (au_fbstart(file) == bstart) {
  9550. + h_file = au_hf_top(file);
  9551. + if (file->f_mode == h_file->f_mode)
  9552. + return 0; /* success */
  9553. + h_file_tmp = h_file;
  9554. + get_file(h_file_tmp);
  9555. + au_set_h_fptr(file, bstart, NULL);
  9556. + }
  9557. + AuDebugOn(au_fi(file)->fi_hdir);
  9558. + /*
  9559. + * it can happen
  9560. + * file exists on both of rw and ro
  9561. + * open --> dbstart and fbstart are both 0
  9562. + * prepend a branch as rw, "rw" become ro
  9563. + * remove rw/file
  9564. + * delete the top branch, "rw" becomes rw again
  9565. + * --> dbstart is 1, fbstart is still 0
  9566. + * write --> fbstart is 0 but dbstart is 1
  9567. + */
  9568. + /* AuDebugOn(au_fbstart(file) < bstart); */
  9569. +
  9570. + h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
  9571. + file, /*force_wr*/0);
  9572. + err = PTR_ERR(h_file);
  9573. + if (IS_ERR(h_file)) {
  9574. + if (h_file_tmp) {
  9575. + atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
  9576. + au_set_h_fptr(file, bstart, h_file_tmp);
  9577. + h_file_tmp = NULL;
  9578. + }
  9579. + goto out; /* todo: close all? */
  9580. + }
  9581. +
  9582. + err = 0;
  9583. + au_set_fbstart(file, bstart);
  9584. + au_set_h_fptr(file, bstart, h_file);
  9585. + au_update_figen(file);
  9586. + /* todo: necessary? */
  9587. + /* file->f_ra = h_file->f_ra; */
  9588. +
  9589. +out:
  9590. + if (h_file_tmp)
  9591. + fput(h_file_tmp);
  9592. + return err;
  9593. +}
  9594. +
  9595. +/* ---------------------------------------------------------------------- */
  9596. +
  9597. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  9598. + struct dentry *hi_wh)
  9599. +{
  9600. + int err;
  9601. + aufs_bindex_t bstart;
  9602. + struct au_dinfo *dinfo;
  9603. + struct dentry *h_dentry;
  9604. + struct au_hdentry *hdp;
  9605. +
  9606. + dinfo = au_di(file->f_path.dentry);
  9607. + AuRwMustWriteLock(&dinfo->di_rwsem);
  9608. +
  9609. + bstart = dinfo->di_bstart;
  9610. + dinfo->di_bstart = btgt;
  9611. + hdp = dinfo->di_hdentry;
  9612. + h_dentry = hdp[0 + btgt].hd_dentry;
  9613. + hdp[0 + btgt].hd_dentry = hi_wh;
  9614. + err = au_reopen_nondir(file);
  9615. + hdp[0 + btgt].hd_dentry = h_dentry;
  9616. + dinfo->di_bstart = bstart;
  9617. +
  9618. + return err;
  9619. +}
  9620. +
  9621. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  9622. + aufs_bindex_t bcpup, struct au_pin *pin)
  9623. +{
  9624. + int err;
  9625. + struct inode *inode, *h_inode;
  9626. + struct dentry *h_dentry, *hi_wh;
  9627. + struct au_cp_generic cpg = {
  9628. + .dentry = file->f_path.dentry,
  9629. + .bdst = bcpup,
  9630. + .bsrc = -1,
  9631. + .len = len,
  9632. + .pin = pin
  9633. + };
  9634. +
  9635. + au_update_dbstart(cpg.dentry);
  9636. + inode = cpg.dentry->d_inode;
  9637. + h_inode = NULL;
  9638. + if (au_dbstart(cpg.dentry) <= bcpup
  9639. + && au_dbend(cpg.dentry) >= bcpup) {
  9640. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  9641. + if (h_dentry)
  9642. + h_inode = h_dentry->d_inode;
  9643. + }
  9644. + hi_wh = au_hi_wh(inode, bcpup);
  9645. + if (!hi_wh && !h_inode)
  9646. + err = au_sio_cpup_wh(&cpg, file);
  9647. + else
  9648. + /* already copied-up after unlink */
  9649. + err = au_reopen_wh(file, bcpup, hi_wh);
  9650. +
  9651. + if (!err
  9652. + && (inode->i_nlink > 1
  9653. + || (inode->i_state & I_LINKABLE))
  9654. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  9655. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  9656. +
  9657. + return err;
  9658. +}
  9659. +
  9660. +/*
  9661. + * prepare the @file for writing.
  9662. + */
  9663. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  9664. +{
  9665. + int err;
  9666. + aufs_bindex_t dbstart;
  9667. + struct dentry *parent;
  9668. + struct inode *inode;
  9669. + struct super_block *sb;
  9670. + struct file *h_file;
  9671. + struct au_cp_generic cpg = {
  9672. + .dentry = file->f_path.dentry,
  9673. + .bdst = -1,
  9674. + .bsrc = -1,
  9675. + .len = len,
  9676. + .pin = pin,
  9677. + .flags = AuCpup_DTIME
  9678. + };
  9679. +
  9680. + sb = cpg.dentry->d_sb;
  9681. + inode = cpg.dentry->d_inode;
  9682. + cpg.bsrc = au_fbstart(file);
  9683. + err = au_test_ro(sb, cpg.bsrc, inode);
  9684. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  9685. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  9686. + /*flags*/0);
  9687. + goto out;
  9688. + }
  9689. +
  9690. + /* need to cpup or reopen */
  9691. + parent = dget_parent(cpg.dentry);
  9692. + di_write_lock_parent(parent);
  9693. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  9694. + cpg.bdst = err;
  9695. + if (unlikely(err < 0))
  9696. + goto out_dgrade;
  9697. + err = 0;
  9698. +
  9699. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  9700. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  9701. + if (unlikely(err))
  9702. + goto out_dgrade;
  9703. + }
  9704. +
  9705. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  9706. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  9707. + if (unlikely(err))
  9708. + goto out_dgrade;
  9709. +
  9710. + dbstart = au_dbstart(cpg.dentry);
  9711. + if (dbstart <= cpg.bdst)
  9712. + cpg.bsrc = cpg.bdst;
  9713. +
  9714. + if (dbstart <= cpg.bdst /* just reopen */
  9715. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  9716. + ) {
  9717. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  9718. + if (IS_ERR(h_file))
  9719. + err = PTR_ERR(h_file);
  9720. + else {
  9721. + di_downgrade_lock(parent, AuLock_IR);
  9722. + if (dbstart > cpg.bdst)
  9723. + err = au_sio_cpup_simple(&cpg);
  9724. + if (!err)
  9725. + err = au_reopen_nondir(file);
  9726. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  9727. + }
  9728. + } else { /* copyup as wh and reopen */
  9729. + /*
  9730. + * since writable hfsplus branch is not supported,
  9731. + * h_open_pre/post() are unnecessary.
  9732. + */
  9733. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  9734. + di_downgrade_lock(parent, AuLock_IR);
  9735. + }
  9736. +
  9737. + if (!err) {
  9738. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  9739. + goto out_dput; /* success */
  9740. + }
  9741. + au_unpin(pin);
  9742. + goto out_unlock;
  9743. +
  9744. +out_dgrade:
  9745. + di_downgrade_lock(parent, AuLock_IR);
  9746. +out_unlock:
  9747. + di_read_unlock(parent, AuLock_IR);
  9748. +out_dput:
  9749. + dput(parent);
  9750. +out:
  9751. + return err;
  9752. +}
  9753. +
  9754. +/* ---------------------------------------------------------------------- */
  9755. +
  9756. +int au_do_flush(struct file *file, fl_owner_t id,
  9757. + int (*flush)(struct file *file, fl_owner_t id))
  9758. +{
  9759. + int err;
  9760. + struct super_block *sb;
  9761. + struct inode *inode;
  9762. +
  9763. + inode = file_inode(file);
  9764. + sb = inode->i_sb;
  9765. + si_noflush_read_lock(sb);
  9766. + fi_read_lock(file);
  9767. + ii_read_lock_child(inode);
  9768. +
  9769. + err = flush(file, id);
  9770. + au_cpup_attr_timesizes(inode);
  9771. +
  9772. + ii_read_unlock(inode);
  9773. + fi_read_unlock(file);
  9774. + si_read_unlock(sb);
  9775. + return err;
  9776. +}
  9777. +
  9778. +/* ---------------------------------------------------------------------- */
  9779. +
  9780. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  9781. +{
  9782. + int err;
  9783. + struct au_pin pin;
  9784. + struct au_finfo *finfo;
  9785. + struct dentry *parent, *hi_wh;
  9786. + struct inode *inode;
  9787. + struct super_block *sb;
  9788. + struct au_cp_generic cpg = {
  9789. + .dentry = file->f_path.dentry,
  9790. + .bdst = -1,
  9791. + .bsrc = -1,
  9792. + .len = -1,
  9793. + .pin = &pin,
  9794. + .flags = AuCpup_DTIME
  9795. + };
  9796. +
  9797. + FiMustWriteLock(file);
  9798. +
  9799. + err = 0;
  9800. + finfo = au_fi(file);
  9801. + sb = cpg.dentry->d_sb;
  9802. + inode = cpg.dentry->d_inode;
  9803. + cpg.bdst = au_ibstart(inode);
  9804. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  9805. + goto out;
  9806. +
  9807. + parent = dget_parent(cpg.dentry);
  9808. + if (au_test_ro(sb, cpg.bdst, inode)) {
  9809. + di_read_lock_parent(parent, !AuLock_IR);
  9810. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  9811. + cpg.bdst = err;
  9812. + di_read_unlock(parent, !AuLock_IR);
  9813. + if (unlikely(err < 0))
  9814. + goto out_parent;
  9815. + err = 0;
  9816. + }
  9817. +
  9818. + di_read_lock_parent(parent, AuLock_IR);
  9819. + hi_wh = au_hi_wh(inode, cpg.bdst);
  9820. + if (!S_ISDIR(inode->i_mode)
  9821. + && au_opt_test(au_mntflags(sb), PLINK)
  9822. + && au_plink_test(inode)
  9823. + && !d_unhashed(cpg.dentry)
  9824. + && cpg.bdst < au_dbstart(cpg.dentry)) {
  9825. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  9826. + if (unlikely(err))
  9827. + goto out_unlock;
  9828. +
  9829. + /* always superio. */
  9830. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  9831. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  9832. + if (!err) {
  9833. + err = au_sio_cpup_simple(&cpg);
  9834. + au_unpin(&pin);
  9835. + }
  9836. + } else if (hi_wh) {
  9837. + /* already copied-up after unlink */
  9838. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  9839. + *need_reopen = 0;
  9840. + }
  9841. +
  9842. +out_unlock:
  9843. + di_read_unlock(parent, AuLock_IR);
  9844. +out_parent:
  9845. + dput(parent);
  9846. +out:
  9847. + return err;
  9848. +}
  9849. +
  9850. +static void au_do_refresh_dir(struct file *file)
  9851. +{
  9852. + aufs_bindex_t bindex, bend, new_bindex, brid;
  9853. + struct au_hfile *p, tmp, *q;
  9854. + struct au_finfo *finfo;
  9855. + struct super_block *sb;
  9856. + struct au_fidir *fidir;
  9857. +
  9858. + FiMustWriteLock(file);
  9859. +
  9860. + sb = file->f_path.dentry->d_sb;
  9861. + finfo = au_fi(file);
  9862. + fidir = finfo->fi_hdir;
  9863. + AuDebugOn(!fidir);
  9864. + p = fidir->fd_hfile + finfo->fi_btop;
  9865. + brid = p->hf_br->br_id;
  9866. + bend = fidir->fd_bbot;
  9867. + for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
  9868. + if (!p->hf_file)
  9869. + continue;
  9870. +
  9871. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  9872. + if (new_bindex == bindex)
  9873. + continue;
  9874. + if (new_bindex < 0) {
  9875. + au_set_h_fptr(file, bindex, NULL);
  9876. + continue;
  9877. + }
  9878. +
  9879. + /* swap two lower inode, and loop again */
  9880. + q = fidir->fd_hfile + new_bindex;
  9881. + tmp = *q;
  9882. + *q = *p;
  9883. + *p = tmp;
  9884. + if (tmp.hf_file) {
  9885. + bindex--;
  9886. + p--;
  9887. + }
  9888. + }
  9889. +
  9890. + p = fidir->fd_hfile;
  9891. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  9892. + bend = au_sbend(sb);
  9893. + for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
  9894. + finfo->fi_btop++, p++)
  9895. + if (p->hf_file) {
  9896. + if (file_inode(p->hf_file))
  9897. + break;
  9898. + au_hfput(p, file);
  9899. + }
  9900. + } else {
  9901. + bend = au_br_index(sb, brid);
  9902. + for (finfo->fi_btop = 0; finfo->fi_btop < bend;
  9903. + finfo->fi_btop++, p++)
  9904. + if (p->hf_file)
  9905. + au_hfput(p, file);
  9906. + bend = au_sbend(sb);
  9907. + }
  9908. +
  9909. + p = fidir->fd_hfile + bend;
  9910. + for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
  9911. + fidir->fd_bbot--, p--)
  9912. + if (p->hf_file) {
  9913. + if (file_inode(p->hf_file))
  9914. + break;
  9915. + au_hfput(p, file);
  9916. + }
  9917. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  9918. +}
  9919. +
  9920. +/*
  9921. + * after branch manipulating, refresh the file.
  9922. + */
  9923. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  9924. +{
  9925. + int err, need_reopen;
  9926. + aufs_bindex_t bend, bindex;
  9927. + struct dentry *dentry;
  9928. + struct au_finfo *finfo;
  9929. + struct au_hfile *hfile;
  9930. +
  9931. + dentry = file->f_path.dentry;
  9932. + finfo = au_fi(file);
  9933. + if (!finfo->fi_hdir) {
  9934. + hfile = &finfo->fi_htop;
  9935. + AuDebugOn(!hfile->hf_file);
  9936. + bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
  9937. + AuDebugOn(bindex < 0);
  9938. + if (bindex != finfo->fi_btop)
  9939. + au_set_fbstart(file, bindex);
  9940. + } else {
  9941. + err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
  9942. + if (unlikely(err))
  9943. + goto out;
  9944. + au_do_refresh_dir(file);
  9945. + }
  9946. +
  9947. + err = 0;
  9948. + need_reopen = 1;
  9949. + if (!au_test_mmapped(file))
  9950. + err = au_file_refresh_by_inode(file, &need_reopen);
  9951. + if (!err && need_reopen && !d_unlinked(dentry))
  9952. + err = reopen(file);
  9953. + if (!err) {
  9954. + au_update_figen(file);
  9955. + goto out; /* success */
  9956. + }
  9957. +
  9958. + /* error, close all lower files */
  9959. + if (finfo->fi_hdir) {
  9960. + bend = au_fbend_dir(file);
  9961. + for (bindex = au_fbstart(file); bindex <= bend; bindex++)
  9962. + au_set_h_fptr(file, bindex, NULL);
  9963. + }
  9964. +
  9965. +out:
  9966. + return err;
  9967. +}
  9968. +
  9969. +/* common function to regular file and dir */
  9970. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  9971. + int wlock)
  9972. +{
  9973. + int err;
  9974. + unsigned int sigen, figen;
  9975. + aufs_bindex_t bstart;
  9976. + unsigned char pseudo_link;
  9977. + struct dentry *dentry;
  9978. + struct inode *inode;
  9979. +
  9980. + err = 0;
  9981. + dentry = file->f_path.dentry;
  9982. + inode = dentry->d_inode;
  9983. + sigen = au_sigen(dentry->d_sb);
  9984. + fi_write_lock(file);
  9985. + figen = au_figen(file);
  9986. + di_write_lock_child(dentry);
  9987. + bstart = au_dbstart(dentry);
  9988. + pseudo_link = (bstart != au_ibstart(inode));
  9989. + if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
  9990. + if (!wlock) {
  9991. + di_downgrade_lock(dentry, AuLock_IR);
  9992. + fi_downgrade_lock(file);
  9993. + }
  9994. + goto out; /* success */
  9995. + }
  9996. +
  9997. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  9998. + if (au_digen_test(dentry, sigen)) {
  9999. + err = au_reval_dpath(dentry, sigen);
  10000. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  10001. + }
  10002. +
  10003. + if (!err)
  10004. + err = refresh_file(file, reopen);
  10005. + if (!err) {
  10006. + if (!wlock) {
  10007. + di_downgrade_lock(dentry, AuLock_IR);
  10008. + fi_downgrade_lock(file);
  10009. + }
  10010. + } else {
  10011. + di_write_unlock(dentry);
  10012. + fi_write_unlock(file);
  10013. + }
  10014. +
  10015. +out:
  10016. + return err;
  10017. +}
  10018. +
  10019. +/* ---------------------------------------------------------------------- */
  10020. +
  10021. +/* cf. aufs_nopage() */
  10022. +/* for madvise(2) */
  10023. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  10024. +{
  10025. + unlock_page(page);
  10026. + return 0;
  10027. +}
  10028. +
  10029. +/* it will never be called, but necessary to support O_DIRECT */
  10030. +static ssize_t aufs_direct_IO(int rw, struct kiocb *iocb,
  10031. + struct iov_iter *iter, loff_t offset)
  10032. +{ BUG(); return 0; }
  10033. +
  10034. +/* they will never be called. */
  10035. +#ifdef CONFIG_AUFS_DEBUG
  10036. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  10037. + loff_t pos, unsigned len, unsigned flags,
  10038. + struct page **pagep, void **fsdata)
  10039. +{ AuUnsupport(); return 0; }
  10040. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  10041. + loff_t pos, unsigned len, unsigned copied,
  10042. + struct page *page, void *fsdata)
  10043. +{ AuUnsupport(); return 0; }
  10044. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  10045. +{ AuUnsupport(); return 0; }
  10046. +
  10047. +static int aufs_set_page_dirty(struct page *page)
  10048. +{ AuUnsupport(); return 0; }
  10049. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  10050. + unsigned int length)
  10051. +{ AuUnsupport(); }
  10052. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  10053. +{ AuUnsupport(); return 0; }
  10054. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  10055. + struct page *page, enum migrate_mode mode)
  10056. +{ AuUnsupport(); return 0; }
  10057. +static int aufs_launder_page(struct page *page)
  10058. +{ AuUnsupport(); return 0; }
  10059. +static int aufs_is_partially_uptodate(struct page *page,
  10060. + unsigned long from,
  10061. + unsigned long count)
  10062. +{ AuUnsupport(); return 0; }
  10063. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  10064. + bool *writeback)
  10065. +{ AuUnsupport(); }
  10066. +static int aufs_error_remove_page(struct address_space *mapping,
  10067. + struct page *page)
  10068. +{ AuUnsupport(); return 0; }
  10069. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  10070. + sector_t *span)
  10071. +{ AuUnsupport(); return 0; }
  10072. +static void aufs_swap_deactivate(struct file *file)
  10073. +{ AuUnsupport(); }
  10074. +#endif /* CONFIG_AUFS_DEBUG */
  10075. +
  10076. +const struct address_space_operations aufs_aop = {
  10077. + .readpage = aufs_readpage,
  10078. + .direct_IO = aufs_direct_IO,
  10079. +#ifdef CONFIG_AUFS_DEBUG
  10080. + .writepage = aufs_writepage,
  10081. + /* no writepages, because of writepage */
  10082. + .set_page_dirty = aufs_set_page_dirty,
  10083. + /* no readpages, because of readpage */
  10084. + .write_begin = aufs_write_begin,
  10085. + .write_end = aufs_write_end,
  10086. + /* no bmap, no block device */
  10087. + .invalidatepage = aufs_invalidatepage,
  10088. + .releasepage = aufs_releasepage,
  10089. + .migratepage = aufs_migratepage,
  10090. + .launder_page = aufs_launder_page,
  10091. + .is_partially_uptodate = aufs_is_partially_uptodate,
  10092. + .is_dirty_writeback = aufs_is_dirty_writeback,
  10093. + .error_remove_page = aufs_error_remove_page,
  10094. + .swap_activate = aufs_swap_activate,
  10095. + .swap_deactivate = aufs_swap_deactivate
  10096. +#endif /* CONFIG_AUFS_DEBUG */
  10097. +};
  10098. diff -Nur linux-4.0.5.orig/fs/aufs/file.h linux-4.0.5/fs/aufs/file.h
  10099. --- linux-4.0.5.orig/fs/aufs/file.h 1969-12-31 18:00:00.000000000 -0600
  10100. +++ linux-4.0.5/fs/aufs/file.h 2015-06-21 14:07:48.686432781 -0500
  10101. @@ -0,0 +1,291 @@
  10102. +/*
  10103. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  10104. + *
  10105. + * This program, aufs is free software; you can redistribute it and/or modify
  10106. + * it under the terms of the GNU General Public License as published by
  10107. + * the Free Software Foundation; either version 2 of the License, or
  10108. + * (at your option) any later version.
  10109. + *
  10110. + * This program is distributed in the hope that it will be useful,
  10111. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10112. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10113. + * GNU General Public License for more details.
  10114. + *
  10115. + * You should have received a copy of the GNU General Public License
  10116. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  10117. + */
  10118. +
  10119. +/*
  10120. + * file operations
  10121. + */
  10122. +
  10123. +#ifndef __AUFS_FILE_H__
  10124. +#define __AUFS_FILE_H__
  10125. +
  10126. +#ifdef __KERNEL__
  10127. +
  10128. +#include <linux/file.h>
  10129. +#include <linux/fs.h>
  10130. +#include <linux/poll.h>
  10131. +#include "rwsem.h"
  10132. +
  10133. +struct au_branch;
  10134. +struct au_hfile {
  10135. + struct file *hf_file;
  10136. + struct au_branch *hf_br;
  10137. +};
  10138. +
  10139. +struct au_vdir;
  10140. +struct au_fidir {
  10141. + aufs_bindex_t fd_bbot;
  10142. + aufs_bindex_t fd_nent;
  10143. + struct au_vdir *fd_vdir_cache;
  10144. + struct au_hfile fd_hfile[];
  10145. +};
  10146. +
  10147. +static inline int au_fidir_sz(int nent)
  10148. +{
  10149. + AuDebugOn(nent < 0);
  10150. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  10151. +}
  10152. +
  10153. +struct au_finfo {
  10154. + atomic_t fi_generation;
  10155. +
  10156. + struct au_rwsem fi_rwsem;
  10157. + aufs_bindex_t fi_btop;
  10158. +
  10159. + /* do not union them */
  10160. + struct { /* for non-dir */
  10161. + struct au_hfile fi_htop;
  10162. + atomic_t fi_mmapped;
  10163. + };
  10164. + struct au_fidir *fi_hdir; /* for dir only */
  10165. +
  10166. + struct hlist_node fi_hlist;
  10167. + struct file *fi_file; /* very ugly */
  10168. +} ____cacheline_aligned_in_smp;
  10169. +
  10170. +/* ---------------------------------------------------------------------- */
  10171. +
  10172. +/* file.c */
  10173. +extern const struct address_space_operations aufs_aop;
  10174. +unsigned int au_file_roflags(unsigned int flags);
  10175. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  10176. + struct file *file, int force_wr);
  10177. +struct au_do_open_args {
  10178. + int no_lock;
  10179. + int (*open)(struct file *file, int flags,
  10180. + struct file *h_file);
  10181. + struct au_fidir *fidir;
  10182. + struct file *h_file;
  10183. +};
  10184. +int au_do_open(struct file *file, struct au_do_open_args *args);
  10185. +int au_reopen_nondir(struct file *file);
  10186. +struct au_pin;
  10187. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  10188. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  10189. + int wlock);
  10190. +int au_do_flush(struct file *file, fl_owner_t id,
  10191. + int (*flush)(struct file *file, fl_owner_t id));
  10192. +
  10193. +/* poll.c */
  10194. +#ifdef CONFIG_AUFS_POLL
  10195. +unsigned int aufs_poll(struct file *file, poll_table *wait);
  10196. +#endif
  10197. +
  10198. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  10199. +/* hfsplus.c */
  10200. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  10201. + int force_wr);
  10202. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  10203. + struct file *h_file);
  10204. +#else
  10205. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  10206. + aufs_bindex_t bindex, int force_wr)
  10207. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  10208. + struct file *h_file);
  10209. +#endif
  10210. +
  10211. +/* f_op.c */
  10212. +extern const struct file_operations aufs_file_fop;
  10213. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  10214. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  10215. +struct file *au_read_pre(struct file *file, int keep_fi);
  10216. +
  10217. +/* finfo.c */
  10218. +void au_hfput(struct au_hfile *hf, struct file *file);
  10219. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  10220. + struct file *h_file);
  10221. +
  10222. +void au_update_figen(struct file *file);
  10223. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  10224. +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
  10225. +
  10226. +void au_fi_init_once(void *_fi);
  10227. +void au_finfo_fin(struct file *file);
  10228. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  10229. +
  10230. +/* ioctl.c */
  10231. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  10232. +#ifdef CONFIG_COMPAT
  10233. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  10234. + unsigned long arg);
  10235. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  10236. + unsigned long arg);
  10237. +#endif
  10238. +
  10239. +/* ---------------------------------------------------------------------- */
  10240. +
  10241. +static inline struct au_finfo *au_fi(struct file *file)
  10242. +{
  10243. + return file->private_data;
  10244. +}
  10245. +
  10246. +/* ---------------------------------------------------------------------- */
  10247. +
  10248. +/*
  10249. + * fi_read_lock, fi_write_lock,
  10250. + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
  10251. + */
  10252. +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
  10253. +
  10254. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  10255. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  10256. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  10257. +
  10258. +/* ---------------------------------------------------------------------- */
  10259. +
  10260. +/* todo: hard/soft set? */
  10261. +static inline aufs_bindex_t au_fbstart(struct file *file)
  10262. +{
  10263. + FiMustAnyLock(file);
  10264. + return au_fi(file)->fi_btop;
  10265. +}
  10266. +
  10267. +static inline aufs_bindex_t au_fbend_dir(struct file *file)
  10268. +{
  10269. + FiMustAnyLock(file);
  10270. + AuDebugOn(!au_fi(file)->fi_hdir);
  10271. + return au_fi(file)->fi_hdir->fd_bbot;
  10272. +}
  10273. +
  10274. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  10275. +{
  10276. + FiMustAnyLock(file);
  10277. + AuDebugOn(!au_fi(file)->fi_hdir);
  10278. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  10279. +}
  10280. +
  10281. +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
  10282. +{
  10283. + FiMustWriteLock(file);
  10284. + au_fi(file)->fi_btop = bindex;
  10285. +}
  10286. +
  10287. +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
  10288. +{
  10289. + FiMustWriteLock(file);
  10290. + AuDebugOn(!au_fi(file)->fi_hdir);
  10291. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  10292. +}
  10293. +
  10294. +static inline void au_set_fvdir_cache(struct file *file,
  10295. + struct au_vdir *vdir_cache)
  10296. +{
  10297. + FiMustWriteLock(file);
  10298. + AuDebugOn(!au_fi(file)->fi_hdir);
  10299. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  10300. +}
  10301. +
  10302. +static inline struct file *au_hf_top(struct file *file)
  10303. +{
  10304. + FiMustAnyLock(file);
  10305. + AuDebugOn(au_fi(file)->fi_hdir);
  10306. + return au_fi(file)->fi_htop.hf_file;
  10307. +}
  10308. +
  10309. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  10310. +{
  10311. + FiMustAnyLock(file);
  10312. + AuDebugOn(!au_fi(file)->fi_hdir);
  10313. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  10314. +}
  10315. +
  10316. +/* todo: memory barrier? */
  10317. +static inline unsigned int au_figen(struct file *f)
  10318. +{
  10319. + return atomic_read(&au_fi(f)->fi_generation);
  10320. +}
  10321. +
  10322. +static inline void au_set_mmapped(struct file *f)
  10323. +{
  10324. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  10325. + return;
  10326. + pr_warn("fi_mmapped wrapped around\n");
  10327. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  10328. + ;
  10329. +}
  10330. +
  10331. +static inline void au_unset_mmapped(struct file *f)
  10332. +{
  10333. + atomic_dec(&au_fi(f)->fi_mmapped);
  10334. +}
  10335. +
  10336. +static inline int au_test_mmapped(struct file *f)
  10337. +{
  10338. + return atomic_read(&au_fi(f)->fi_mmapped);
  10339. +}
  10340. +
  10341. +/* customize vma->vm_file */
  10342. +
  10343. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  10344. + struct file *file)
  10345. +{
  10346. + struct file *f;
  10347. +
  10348. + f = vma->vm_file;
  10349. + get_file(file);
  10350. + vma->vm_file = file;
  10351. + fput(f);
  10352. +}
  10353. +
  10354. +#ifdef CONFIG_MMU
  10355. +#define AuDbgVmRegion(file, vma) do {} while (0)
  10356. +
  10357. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  10358. + struct file *file)
  10359. +{
  10360. + au_do_vm_file_reset(vma, file);
  10361. +}
  10362. +#else
  10363. +#define AuDbgVmRegion(file, vma) \
  10364. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  10365. +
  10366. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  10367. + struct file *file)
  10368. +{
  10369. + struct file *f;
  10370. +
  10371. + au_do_vm_file_reset(vma, file);
  10372. + f = vma->vm_region->vm_file;
  10373. + get_file(file);
  10374. + vma->vm_region->vm_file = file;
  10375. + fput(f);
  10376. +}
  10377. +#endif /* CONFIG_MMU */
  10378. +
  10379. +/* handle vma->vm_prfile */
  10380. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  10381. + struct file *file)
  10382. +{
  10383. + get_file(file);
  10384. + vma->vm_prfile = file;
  10385. +#ifndef CONFIG_MMU
  10386. + get_file(file);
  10387. + vma->vm_region->vm_prfile = file;
  10388. +#endif
  10389. +}
  10390. +
  10391. +#endif /* __KERNEL__ */
  10392. +#endif /* __AUFS_FILE_H__ */
  10393. diff -Nur linux-4.0.5.orig/fs/aufs/finfo.c linux-4.0.5/fs/aufs/finfo.c
  10394. --- linux-4.0.5.orig/fs/aufs/finfo.c 1969-12-31 18:00:00.000000000 -0600
  10395. +++ linux-4.0.5/fs/aufs/finfo.c 2015-06-21 14:07:48.686432781 -0500
  10396. @@ -0,0 +1,157 @@
  10397. +/*
  10398. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  10399. + *
  10400. + * This program, aufs is free software; you can redistribute it and/or modify
  10401. + * it under the terms of the GNU General Public License as published by
  10402. + * the Free Software Foundation; either version 2 of the License, or
  10403. + * (at your option) any later version.
  10404. + *
  10405. + * This program is distributed in the hope that it will be useful,
  10406. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10407. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10408. + * GNU General Public License for more details.
  10409. + *
  10410. + * You should have received a copy of the GNU General Public License
  10411. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  10412. + */
  10413. +
  10414. +/*
  10415. + * file private data
  10416. + */
  10417. +
  10418. +#include "aufs.h"
  10419. +
  10420. +void au_hfput(struct au_hfile *hf, struct file *file)
  10421. +{
  10422. + /* todo: direct access f_flags */
  10423. + if (vfsub_file_flags(file) & __FMODE_EXEC)
  10424. + allow_write_access(hf->hf_file);
  10425. + fput(hf->hf_file);
  10426. + hf->hf_file = NULL;
  10427. + atomic_dec(&hf->hf_br->br_count);
  10428. + hf->hf_br = NULL;
  10429. +}
  10430. +
  10431. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  10432. +{
  10433. + struct au_finfo *finfo = au_fi(file);
  10434. + struct au_hfile *hf;
  10435. + struct au_fidir *fidir;
  10436. +
  10437. + fidir = finfo->fi_hdir;
  10438. + if (!fidir) {
  10439. + AuDebugOn(finfo->fi_btop != bindex);
  10440. + hf = &finfo->fi_htop;
  10441. + } else
  10442. + hf = fidir->fd_hfile + bindex;
  10443. +
  10444. + if (hf && hf->hf_file)
  10445. + au_hfput(hf, file);
  10446. + if (val) {
  10447. + FiMustWriteLock(file);
  10448. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  10449. + hf->hf_file = val;
  10450. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  10451. + }
  10452. +}
  10453. +
  10454. +void au_update_figen(struct file *file)
  10455. +{
  10456. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  10457. + /* smp_mb(); */ /* atomic_set */
  10458. +}
  10459. +
  10460. +/* ---------------------------------------------------------------------- */
  10461. +
  10462. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  10463. +{
  10464. + struct au_fidir *fidir;
  10465. + int nbr;
  10466. +
  10467. + nbr = au_sbend(sb) + 1;
  10468. + if (nbr < 2)
  10469. + nbr = 2; /* initial allocate for 2 branches */
  10470. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  10471. + if (fidir) {
  10472. + fidir->fd_bbot = -1;
  10473. + fidir->fd_nent = nbr;
  10474. + fidir->fd_vdir_cache = NULL;
  10475. + }
  10476. +
  10477. + return fidir;
  10478. +}
  10479. +
  10480. +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
  10481. +{
  10482. + int err;
  10483. + struct au_fidir *fidir, *p;
  10484. +
  10485. + AuRwMustWriteLock(&finfo->fi_rwsem);
  10486. + fidir = finfo->fi_hdir;
  10487. + AuDebugOn(!fidir);
  10488. +
  10489. + err = -ENOMEM;
  10490. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  10491. + GFP_NOFS);
  10492. + if (p) {
  10493. + p->fd_nent = nbr;
  10494. + finfo->fi_hdir = p;
  10495. + err = 0;
  10496. + }
  10497. +
  10498. + return err;
  10499. +}
  10500. +
  10501. +/* ---------------------------------------------------------------------- */
  10502. +
  10503. +void au_finfo_fin(struct file *file)
  10504. +{
  10505. + struct au_finfo *finfo;
  10506. +
  10507. + au_nfiles_dec(file->f_path.dentry->d_sb);
  10508. +
  10509. + finfo = au_fi(file);
  10510. + AuDebugOn(finfo->fi_hdir);
  10511. + AuRwDestroy(&finfo->fi_rwsem);
  10512. + au_cache_free_finfo(finfo);
  10513. +}
  10514. +
  10515. +void au_fi_init_once(void *_finfo)
  10516. +{
  10517. + struct au_finfo *finfo = _finfo;
  10518. + static struct lock_class_key aufs_fi;
  10519. +
  10520. + au_rw_init(&finfo->fi_rwsem);
  10521. + au_rw_class(&finfo->fi_rwsem, &aufs_fi);
  10522. +}
  10523. +
  10524. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  10525. +{
  10526. + int err;
  10527. + struct au_finfo *finfo;
  10528. + struct dentry *dentry;
  10529. +
  10530. + err = -ENOMEM;
  10531. + dentry = file->f_path.dentry;
  10532. + finfo = au_cache_alloc_finfo();
  10533. + if (unlikely(!finfo))
  10534. + goto out;
  10535. +
  10536. + err = 0;
  10537. + au_nfiles_inc(dentry->d_sb);
  10538. + /* verbose coding for lock class name */
  10539. + if (!fidir)
  10540. + au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
  10541. + else
  10542. + au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
  10543. + au_rw_write_lock(&finfo->fi_rwsem);
  10544. + finfo->fi_btop = -1;
  10545. + finfo->fi_hdir = fidir;
  10546. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  10547. + /* smp_mb(); */ /* atomic_set */
  10548. +
  10549. + file->private_data = finfo;
  10550. +
  10551. +out:
  10552. + return err;
  10553. +}
  10554. diff -Nur linux-4.0.5.orig/fs/aufs/f_op.c linux-4.0.5/fs/aufs/f_op.c
  10555. --- linux-4.0.5.orig/fs/aufs/f_op.c 1969-12-31 18:00:00.000000000 -0600
  10556. +++ linux-4.0.5/fs/aufs/f_op.c 2015-06-21 14:07:48.682432781 -0500
  10557. @@ -0,0 +1,748 @@
  10558. +/*
  10559. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  10560. + *
  10561. + * This program, aufs is free software; you can redistribute it and/or modify
  10562. + * it under the terms of the GNU General Public License as published by
  10563. + * the Free Software Foundation; either version 2 of the License, or
  10564. + * (at your option) any later version.
  10565. + *
  10566. + * This program is distributed in the hope that it will be useful,
  10567. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10568. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10569. + * GNU General Public License for more details.
  10570. + *
  10571. + * You should have received a copy of the GNU General Public License
  10572. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  10573. + */
  10574. +
  10575. +/*
  10576. + * file and vm operations
  10577. + */
  10578. +
  10579. +#include <linux/aio.h>
  10580. +#include <linux/fs_stack.h>
  10581. +#include <linux/mman.h>
  10582. +#include <linux/security.h>
  10583. +#include "aufs.h"
  10584. +
  10585. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  10586. +{
  10587. + int err;
  10588. + aufs_bindex_t bindex;
  10589. + struct dentry *dentry;
  10590. + struct au_finfo *finfo;
  10591. + struct inode *h_inode;
  10592. +
  10593. + FiMustWriteLock(file);
  10594. +
  10595. + err = 0;
  10596. + dentry = file->f_path.dentry;
  10597. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  10598. + finfo = au_fi(file);
  10599. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  10600. + atomic_set(&finfo->fi_mmapped, 0);
  10601. + bindex = au_dbstart(dentry);
  10602. + if (!h_file)
  10603. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  10604. + else
  10605. + get_file(h_file);
  10606. + if (IS_ERR(h_file))
  10607. + err = PTR_ERR(h_file);
  10608. + else {
  10609. + if ((flags & __O_TMPFILE)
  10610. + && !(flags & O_EXCL)) {
  10611. + h_inode = file_inode(h_file);
  10612. + spin_lock(&h_inode->i_lock);
  10613. + h_inode->i_state |= I_LINKABLE;
  10614. + spin_unlock(&h_inode->i_lock);
  10615. + }
  10616. + au_set_fbstart(file, bindex);
  10617. + au_set_h_fptr(file, bindex, h_file);
  10618. + au_update_figen(file);
  10619. + /* todo: necessary? */
  10620. + /* file->f_ra = h_file->f_ra; */
  10621. + }
  10622. +
  10623. + return err;
  10624. +}
  10625. +
  10626. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  10627. + struct file *file)
  10628. +{
  10629. + int err;
  10630. + struct super_block *sb;
  10631. + struct au_do_open_args args = {
  10632. + .open = au_do_open_nondir
  10633. + };
  10634. +
  10635. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  10636. + file, vfsub_file_flags(file), file->f_mode);
  10637. +
  10638. + sb = file->f_path.dentry->d_sb;
  10639. + si_read_lock(sb, AuLock_FLUSH);
  10640. + err = au_do_open(file, &args);
  10641. + si_read_unlock(sb);
  10642. + return err;
  10643. +}
  10644. +
  10645. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  10646. +{
  10647. + struct au_finfo *finfo;
  10648. + aufs_bindex_t bindex;
  10649. +
  10650. + finfo = au_fi(file);
  10651. + au_sphl_del(&finfo->fi_hlist,
  10652. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  10653. + bindex = finfo->fi_btop;
  10654. + if (bindex >= 0)
  10655. + au_set_h_fptr(file, bindex, NULL);
  10656. +
  10657. + au_finfo_fin(file);
  10658. + return 0;
  10659. +}
  10660. +
  10661. +/* ---------------------------------------------------------------------- */
  10662. +
  10663. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  10664. +{
  10665. + int err;
  10666. + struct file *h_file;
  10667. +
  10668. + err = 0;
  10669. + h_file = au_hf_top(file);
  10670. + if (h_file)
  10671. + err = vfsub_flush(h_file, id);
  10672. + return err;
  10673. +}
  10674. +
  10675. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  10676. +{
  10677. + return au_do_flush(file, id, au_do_flush_nondir);
  10678. +}
  10679. +
  10680. +/* ---------------------------------------------------------------------- */
  10681. +/*
  10682. + * read and write functions acquire [fdi]_rwsem once, but release before
  10683. + * mmap_sem. This is because to stop a race condition between mmap(2).
  10684. + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
  10685. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  10686. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  10687. + */
  10688. +
  10689. +/* Callers should call au_read_post() or fput() in the end */
  10690. +struct file *au_read_pre(struct file *file, int keep_fi)
  10691. +{
  10692. + struct file *h_file;
  10693. + int err;
  10694. +
  10695. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  10696. + if (!err) {
  10697. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  10698. + h_file = au_hf_top(file);
  10699. + get_file(h_file);
  10700. + if (!keep_fi)
  10701. + fi_read_unlock(file);
  10702. + } else
  10703. + h_file = ERR_PTR(err);
  10704. +
  10705. + return h_file;
  10706. +}
  10707. +
  10708. +static void au_read_post(struct inode *inode, struct file *h_file)
  10709. +{
  10710. + /* update without lock, I don't think it a problem */
  10711. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  10712. + fput(h_file);
  10713. +}
  10714. +
  10715. +struct au_write_pre {
  10716. + blkcnt_t blks;
  10717. + aufs_bindex_t bstart;
  10718. +};
  10719. +
  10720. +/*
  10721. + * return with iinfo is write-locked
  10722. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  10723. + * end
  10724. + */
  10725. +static struct file *au_write_pre(struct file *file, int do_ready,
  10726. + struct au_write_pre *wpre)
  10727. +{
  10728. + struct file *h_file;
  10729. + struct dentry *dentry;
  10730. + int err;
  10731. + struct au_pin pin;
  10732. +
  10733. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  10734. + h_file = ERR_PTR(err);
  10735. + if (unlikely(err))
  10736. + goto out;
  10737. +
  10738. + dentry = file->f_path.dentry;
  10739. + if (do_ready) {
  10740. + err = au_ready_to_write(file, -1, &pin);
  10741. + if (unlikely(err)) {
  10742. + h_file = ERR_PTR(err);
  10743. + di_write_unlock(dentry);
  10744. + goto out_fi;
  10745. + }
  10746. + }
  10747. +
  10748. + di_downgrade_lock(dentry, /*flags*/0);
  10749. + if (wpre)
  10750. + wpre->bstart = au_fbstart(file);
  10751. + h_file = au_hf_top(file);
  10752. + get_file(h_file);
  10753. + if (wpre)
  10754. + wpre->blks = file_inode(h_file)->i_blocks;
  10755. + if (do_ready)
  10756. + au_unpin(&pin);
  10757. + di_read_unlock(dentry, /*flags*/0);
  10758. +
  10759. +out_fi:
  10760. + fi_write_unlock(file);
  10761. +out:
  10762. + return h_file;
  10763. +}
  10764. +
  10765. +static void au_write_post(struct inode *inode, struct file *h_file,
  10766. + struct au_write_pre *wpre, ssize_t written)
  10767. +{
  10768. + struct inode *h_inode;
  10769. +
  10770. + au_cpup_attr_timesizes(inode);
  10771. + AuDebugOn(au_ibstart(inode) != wpre->bstart);
  10772. + h_inode = file_inode(h_file);
  10773. + inode->i_mode = h_inode->i_mode;
  10774. + ii_write_unlock(inode);
  10775. + fput(h_file);
  10776. +
  10777. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  10778. + if (written > 0)
  10779. + au_fhsm_wrote(inode->i_sb, wpre->bstart,
  10780. + /*force*/h_inode->i_blocks > wpre->blks);
  10781. +}
  10782. +
  10783. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  10784. + loff_t *ppos)
  10785. +{
  10786. + ssize_t err;
  10787. + struct inode *inode;
  10788. + struct file *h_file;
  10789. + struct super_block *sb;
  10790. +
  10791. + inode = file_inode(file);
  10792. + sb = inode->i_sb;
  10793. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10794. +
  10795. + h_file = au_read_pre(file, /*keep_fi*/0);
  10796. + err = PTR_ERR(h_file);
  10797. + if (IS_ERR(h_file))
  10798. + goto out;
  10799. +
  10800. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  10801. + err = vfsub_read_u(h_file, buf, count, ppos);
  10802. + /* todo: necessary? */
  10803. + /* file->f_ra = h_file->f_ra; */
  10804. + au_read_post(inode, h_file);
  10805. +
  10806. +out:
  10807. + si_read_unlock(sb);
  10808. + return err;
  10809. +}
  10810. +
  10811. +/*
  10812. + * todo: very ugly
  10813. + * it locks both of i_mutex and si_rwsem for read in safe.
  10814. + * if the plink maintenance mode continues forever (that is the problem),
  10815. + * may loop forever.
  10816. + */
  10817. +static void au_mtx_and_read_lock(struct inode *inode)
  10818. +{
  10819. + int err;
  10820. + struct super_block *sb = inode->i_sb;
  10821. +
  10822. + while (1) {
  10823. + mutex_lock(&inode->i_mutex);
  10824. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  10825. + if (!err)
  10826. + break;
  10827. + mutex_unlock(&inode->i_mutex);
  10828. + si_read_lock(sb, AuLock_NOPLMW);
  10829. + si_read_unlock(sb);
  10830. + }
  10831. +}
  10832. +
  10833. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  10834. + size_t count, loff_t *ppos)
  10835. +{
  10836. + ssize_t err;
  10837. + struct au_write_pre wpre;
  10838. + struct inode *inode;
  10839. + struct file *h_file;
  10840. + char __user *buf = (char __user *)ubuf;
  10841. +
  10842. + inode = file_inode(file);
  10843. + au_mtx_and_read_lock(inode);
  10844. +
  10845. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  10846. + err = PTR_ERR(h_file);
  10847. + if (IS_ERR(h_file))
  10848. + goto out;
  10849. +
  10850. + err = vfsub_write_u(h_file, buf, count, ppos);
  10851. + au_write_post(inode, h_file, &wpre, err);
  10852. +
  10853. +out:
  10854. + si_read_unlock(inode->i_sb);
  10855. + mutex_unlock(&inode->i_mutex);
  10856. + return err;
  10857. +}
  10858. +
  10859. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  10860. + struct iov_iter *iov_iter)
  10861. +{
  10862. + ssize_t err;
  10863. + struct file *file;
  10864. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  10865. + ssize_t (*aio)(struct kiocb *, const struct iovec *, unsigned long,
  10866. + loff_t);
  10867. +
  10868. + err = security_file_permission(h_file, rw);
  10869. + if (unlikely(err))
  10870. + goto out;
  10871. +
  10872. + err = -ENOSYS;
  10873. + iter = NULL;
  10874. + aio = NULL;
  10875. + if (rw == MAY_READ) {
  10876. + iter = h_file->f_op->read_iter;
  10877. + aio = h_file->f_op->aio_read;
  10878. + } else if (rw == MAY_WRITE) {
  10879. + iter = h_file->f_op->write_iter;
  10880. + aio = h_file->f_op->aio_write;
  10881. + }
  10882. +
  10883. + file = kio->ki_filp;
  10884. + kio->ki_filp = h_file;
  10885. + if (iter) {
  10886. + lockdep_off();
  10887. + err = iter(kio, iov_iter);
  10888. + lockdep_on();
  10889. + } else if (aio) {
  10890. + lockdep_off();
  10891. + err = aio(kio, iov_iter->iov, iov_iter->nr_segs, kio->ki_pos);
  10892. + lockdep_on();
  10893. + } else
  10894. + /* currently there is no such fs */
  10895. + WARN_ON_ONCE(1);
  10896. + kio->ki_filp = file;
  10897. +
  10898. +out:
  10899. + return err;
  10900. +}
  10901. +
  10902. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  10903. +{
  10904. + ssize_t err;
  10905. + struct file *file, *h_file;
  10906. + struct inode *inode;
  10907. + struct super_block *sb;
  10908. +
  10909. + file = kio->ki_filp;
  10910. + inode = file_inode(file);
  10911. + sb = inode->i_sb;
  10912. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10913. +
  10914. + h_file = au_read_pre(file, /*keep_fi*/0);
  10915. + err = PTR_ERR(h_file);
  10916. + if (IS_ERR(h_file))
  10917. + goto out;
  10918. +
  10919. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  10920. + /* todo: necessary? */
  10921. + /* file->f_ra = h_file->f_ra; */
  10922. + au_read_post(inode, h_file);
  10923. +
  10924. +out:
  10925. + si_read_unlock(sb);
  10926. + return err;
  10927. +}
  10928. +
  10929. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  10930. +{
  10931. + ssize_t err;
  10932. + struct au_write_pre wpre;
  10933. + struct inode *inode;
  10934. + struct file *file, *h_file;
  10935. +
  10936. + file = kio->ki_filp;
  10937. + inode = file_inode(file);
  10938. + au_mtx_and_read_lock(inode);
  10939. +
  10940. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  10941. + err = PTR_ERR(h_file);
  10942. + if (IS_ERR(h_file))
  10943. + goto out;
  10944. +
  10945. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  10946. + au_write_post(inode, h_file, &wpre, err);
  10947. +
  10948. +out:
  10949. + si_read_unlock(inode->i_sb);
  10950. + mutex_unlock(&inode->i_mutex);
  10951. + return err;
  10952. +}
  10953. +
  10954. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  10955. + struct pipe_inode_info *pipe, size_t len,
  10956. + unsigned int flags)
  10957. +{
  10958. + ssize_t err;
  10959. + struct file *h_file;
  10960. + struct inode *inode;
  10961. + struct super_block *sb;
  10962. +
  10963. + inode = file_inode(file);
  10964. + sb = inode->i_sb;
  10965. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10966. +
  10967. + h_file = au_read_pre(file, /*keep_fi*/1);
  10968. + err = PTR_ERR(h_file);
  10969. + if (IS_ERR(h_file))
  10970. + goto out;
  10971. +
  10972. + if (au_test_loopback_kthread()) {
  10973. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  10974. + if (file->f_mapping != h_file->f_mapping) {
  10975. + file->f_mapping = h_file->f_mapping;
  10976. + smp_mb(); /* unnecessary? */
  10977. + }
  10978. + }
  10979. + fi_read_unlock(file);
  10980. +
  10981. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  10982. + /* todo: necessasry? */
  10983. + /* file->f_ra = h_file->f_ra; */
  10984. + au_read_post(inode, h_file);
  10985. +
  10986. +out:
  10987. + si_read_unlock(sb);
  10988. + return err;
  10989. +}
  10990. +
  10991. +static ssize_t
  10992. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  10993. + size_t len, unsigned int flags)
  10994. +{
  10995. + ssize_t err;
  10996. + struct au_write_pre wpre;
  10997. + struct inode *inode;
  10998. + struct file *h_file;
  10999. +
  11000. + inode = file_inode(file);
  11001. + au_mtx_and_read_lock(inode);
  11002. +
  11003. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  11004. + err = PTR_ERR(h_file);
  11005. + if (IS_ERR(h_file))
  11006. + goto out;
  11007. +
  11008. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  11009. + au_write_post(inode, h_file, &wpre, err);
  11010. +
  11011. +out:
  11012. + si_read_unlock(inode->i_sb);
  11013. + mutex_unlock(&inode->i_mutex);
  11014. + return err;
  11015. +}
  11016. +
  11017. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  11018. + loff_t len)
  11019. +{
  11020. + long err;
  11021. + struct au_write_pre wpre;
  11022. + struct inode *inode;
  11023. + struct file *h_file;
  11024. +
  11025. + inode = file_inode(file);
  11026. + au_mtx_and_read_lock(inode);
  11027. +
  11028. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  11029. + err = PTR_ERR(h_file);
  11030. + if (IS_ERR(h_file))
  11031. + goto out;
  11032. +
  11033. + lockdep_off();
  11034. + err = vfs_fallocate(h_file, mode, offset, len);
  11035. + lockdep_on();
  11036. + au_write_post(inode, h_file, &wpre, /*written*/1);
  11037. +
  11038. +out:
  11039. + si_read_unlock(inode->i_sb);
  11040. + mutex_unlock(&inode->i_mutex);
  11041. + return err;
  11042. +}
  11043. +
  11044. +/* ---------------------------------------------------------------------- */
  11045. +
  11046. +/*
  11047. + * The locking order around current->mmap_sem.
  11048. + * - in most and regular cases
  11049. + * file I/O syscall -- aufs_read() or something
  11050. + * -- si_rwsem for read -- mmap_sem
  11051. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  11052. + * - in mmap case
  11053. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  11054. + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
  11055. + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
  11056. + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  11057. + * It means that when aufs acquires si_rwsem for write, the process should never
  11058. + * acquire mmap_sem.
  11059. + *
  11060. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  11061. + * problem either since any directory is not able to be mmap-ed.
  11062. + * The similar scenario is applied to aufs_readlink() too.
  11063. + */
  11064. +
  11065. +#if 0 /* stop calling security_file_mmap() */
  11066. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  11067. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  11068. +
  11069. +static unsigned long au_arch_prot_conv(unsigned long flags)
  11070. +{
  11071. + /* currently ppc64 only */
  11072. +#ifdef CONFIG_PPC64
  11073. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  11074. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  11075. + return AuConv_VM_PROT(flags, SAO);
  11076. +#else
  11077. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  11078. + return 0;
  11079. +#endif
  11080. +}
  11081. +
  11082. +static unsigned long au_prot_conv(unsigned long flags)
  11083. +{
  11084. + return AuConv_VM_PROT(flags, READ)
  11085. + | AuConv_VM_PROT(flags, WRITE)
  11086. + | AuConv_VM_PROT(flags, EXEC)
  11087. + | au_arch_prot_conv(flags);
  11088. +}
  11089. +
  11090. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  11091. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  11092. +
  11093. +static unsigned long au_flag_conv(unsigned long flags)
  11094. +{
  11095. + return AuConv_VM_MAP(flags, GROWSDOWN)
  11096. + | AuConv_VM_MAP(flags, DENYWRITE)
  11097. + | AuConv_VM_MAP(flags, LOCKED);
  11098. +}
  11099. +#endif
  11100. +
  11101. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  11102. +{
  11103. + int err;
  11104. + const unsigned char wlock
  11105. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  11106. + struct super_block *sb;
  11107. + struct file *h_file;
  11108. + struct inode *inode;
  11109. +
  11110. + AuDbgVmRegion(file, vma);
  11111. +
  11112. + inode = file_inode(file);
  11113. + sb = inode->i_sb;
  11114. + lockdep_off();
  11115. + si_read_lock(sb, AuLock_NOPLMW);
  11116. +
  11117. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  11118. + lockdep_on();
  11119. + err = PTR_ERR(h_file);
  11120. + if (IS_ERR(h_file))
  11121. + goto out;
  11122. +
  11123. + err = 0;
  11124. + au_set_mmapped(file);
  11125. + au_vm_file_reset(vma, h_file);
  11126. + /*
  11127. + * we cannot call security_mmap_file() here since it may acquire
  11128. + * mmap_sem or i_mutex.
  11129. + *
  11130. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  11131. + * au_flag_conv(vma->vm_flags));
  11132. + */
  11133. + if (!err)
  11134. + err = h_file->f_op->mmap(h_file, vma);
  11135. + if (!err) {
  11136. + au_vm_prfile_set(vma, file);
  11137. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  11138. + goto out_fput; /* success */
  11139. + }
  11140. + au_unset_mmapped(file);
  11141. + au_vm_file_reset(vma, file);
  11142. +
  11143. +out_fput:
  11144. + lockdep_off();
  11145. + ii_write_unlock(inode);
  11146. + lockdep_on();
  11147. + fput(h_file);
  11148. +out:
  11149. + lockdep_off();
  11150. + si_read_unlock(sb);
  11151. + lockdep_on();
  11152. + AuTraceErr(err);
  11153. + return err;
  11154. +}
  11155. +
  11156. +/* ---------------------------------------------------------------------- */
  11157. +
  11158. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  11159. + int datasync)
  11160. +{
  11161. + int err;
  11162. + struct au_write_pre wpre;
  11163. + struct inode *inode;
  11164. + struct file *h_file;
  11165. +
  11166. + err = 0; /* -EBADF; */ /* posix? */
  11167. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  11168. + goto out;
  11169. +
  11170. + inode = file_inode(file);
  11171. + au_mtx_and_read_lock(inode);
  11172. +
  11173. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  11174. + err = PTR_ERR(h_file);
  11175. + if (IS_ERR(h_file))
  11176. + goto out_unlock;
  11177. +
  11178. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  11179. + au_write_post(inode, h_file, &wpre, /*written*/0);
  11180. +
  11181. +out_unlock:
  11182. + si_read_unlock(inode->i_sb);
  11183. + mutex_unlock(&inode->i_mutex);
  11184. +out:
  11185. + return err;
  11186. +}
  11187. +
  11188. +/* no one supports this operation, currently */
  11189. +#if 0
  11190. +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
  11191. +{
  11192. + int err;
  11193. + struct au_write_pre wpre;
  11194. + struct inode *inode;
  11195. + struct file *file, *h_file;
  11196. +
  11197. + err = 0; /* -EBADF; */ /* posix? */
  11198. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  11199. + goto out;
  11200. +
  11201. + file = kio->ki_filp;
  11202. + inode = file_inode(file);
  11203. + au_mtx_and_read_lock(inode);
  11204. +
  11205. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  11206. + err = PTR_ERR(h_file);
  11207. + if (IS_ERR(h_file))
  11208. + goto out_unlock;
  11209. +
  11210. + err = -ENOSYS;
  11211. + h_file = au_hf_top(file);
  11212. + if (h_file->f_op->aio_fsync) {
  11213. + struct mutex *h_mtx;
  11214. +
  11215. + h_mtx = &file_inode(h_file)->i_mutex;
  11216. + if (!is_sync_kiocb(kio)) {
  11217. + get_file(h_file);
  11218. + fput(file);
  11219. + }
  11220. + kio->ki_filp = h_file;
  11221. + err = h_file->f_op->aio_fsync(kio, datasync);
  11222. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  11223. + if (!err)
  11224. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
  11225. + /*ignore*/
  11226. + mutex_unlock(h_mtx);
  11227. + }
  11228. + au_write_post(inode, h_file, &wpre, /*written*/0);
  11229. +
  11230. +out_unlock:
  11231. + si_read_unlock(inode->sb);
  11232. + mutex_unlock(&inode->i_mutex);
  11233. +out:
  11234. + return err;
  11235. +}
  11236. +#endif
  11237. +
  11238. +static int aufs_fasync(int fd, struct file *file, int flag)
  11239. +{
  11240. + int err;
  11241. + struct file *h_file;
  11242. + struct super_block *sb;
  11243. +
  11244. + sb = file->f_path.dentry->d_sb;
  11245. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  11246. +
  11247. + h_file = au_read_pre(file, /*keep_fi*/0);
  11248. + err = PTR_ERR(h_file);
  11249. + if (IS_ERR(h_file))
  11250. + goto out;
  11251. +
  11252. + if (h_file->f_op->fasync)
  11253. + err = h_file->f_op->fasync(fd, h_file, flag);
  11254. + fput(h_file); /* instead of au_read_post() */
  11255. +
  11256. +out:
  11257. + si_read_unlock(sb);
  11258. + return err;
  11259. +}
  11260. +
  11261. +/* ---------------------------------------------------------------------- */
  11262. +
  11263. +/* no one supports this operation, currently */
  11264. +#if 0
  11265. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  11266. + size_t len, loff_t *pos, int more)
  11267. +{
  11268. +}
  11269. +#endif
  11270. +
  11271. +/* ---------------------------------------------------------------------- */
  11272. +
  11273. +const struct file_operations aufs_file_fop = {
  11274. + .owner = THIS_MODULE,
  11275. +
  11276. + .llseek = default_llseek,
  11277. +
  11278. + .read = aufs_read,
  11279. + .write = aufs_write,
  11280. + .read_iter = aufs_read_iter,
  11281. + .write_iter = aufs_write_iter,
  11282. +
  11283. +#ifdef CONFIG_AUFS_POLL
  11284. + .poll = aufs_poll,
  11285. +#endif
  11286. + .unlocked_ioctl = aufs_ioctl_nondir,
  11287. +#ifdef CONFIG_COMPAT
  11288. + .compat_ioctl = aufs_compat_ioctl_nondir,
  11289. +#endif
  11290. + .mmap = aufs_mmap,
  11291. + .open = aufs_open_nondir,
  11292. + .flush = aufs_flush_nondir,
  11293. + .release = aufs_release_nondir,
  11294. + .fsync = aufs_fsync_nondir,
  11295. + /* .aio_fsync = aufs_aio_fsync_nondir, */
  11296. + .fasync = aufs_fasync,
  11297. + /* .sendpage = aufs_sendpage, */
  11298. + .splice_write = aufs_splice_write,
  11299. + .splice_read = aufs_splice_read,
  11300. +#if 0
  11301. + .aio_splice_write = aufs_aio_splice_write,
  11302. + .aio_splice_read = aufs_aio_splice_read,
  11303. +#endif
  11304. + .fallocate = aufs_fallocate
  11305. +};
  11306. diff -Nur linux-4.0.5.orig/fs/aufs/fstype.h linux-4.0.5/fs/aufs/fstype.h
  11307. --- linux-4.0.5.orig/fs/aufs/fstype.h 1969-12-31 18:00:00.000000000 -0600
  11308. +++ linux-4.0.5/fs/aufs/fstype.h 2015-06-21 14:07:48.686432781 -0500
  11309. @@ -0,0 +1,400 @@
  11310. +/*
  11311. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11312. + *
  11313. + * This program, aufs is free software; you can redistribute it and/or modify
  11314. + * it under the terms of the GNU General Public License as published by
  11315. + * the Free Software Foundation; either version 2 of the License, or
  11316. + * (at your option) any later version.
  11317. + *
  11318. + * This program is distributed in the hope that it will be useful,
  11319. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11320. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11321. + * GNU General Public License for more details.
  11322. + *
  11323. + * You should have received a copy of the GNU General Public License
  11324. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11325. + */
  11326. +
  11327. +/*
  11328. + * judging filesystem type
  11329. + */
  11330. +
  11331. +#ifndef __AUFS_FSTYPE_H__
  11332. +#define __AUFS_FSTYPE_H__
  11333. +
  11334. +#ifdef __KERNEL__
  11335. +
  11336. +#include <linux/fs.h>
  11337. +#include <linux/magic.h>
  11338. +#include <linux/romfs_fs.h>
  11339. +#include <linux/nfs_fs.h>
  11340. +
  11341. +static inline int au_test_aufs(struct super_block *sb)
  11342. +{
  11343. + return sb->s_magic == AUFS_SUPER_MAGIC;
  11344. +}
  11345. +
  11346. +static inline const char *au_sbtype(struct super_block *sb)
  11347. +{
  11348. + return sb->s_type->name;
  11349. +}
  11350. +
  11351. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  11352. +{
  11353. +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
  11354. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  11355. +#else
  11356. + return 0;
  11357. +#endif
  11358. +}
  11359. +
  11360. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  11361. +{
  11362. +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
  11363. + return sb->s_magic == ROMFS_MAGIC;
  11364. +#else
  11365. + return 0;
  11366. +#endif
  11367. +}
  11368. +
  11369. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  11370. +{
  11371. +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
  11372. + return sb->s_magic == CRAMFS_MAGIC;
  11373. +#endif
  11374. + return 0;
  11375. +}
  11376. +
  11377. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  11378. +{
  11379. +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
  11380. + return sb->s_magic == NFS_SUPER_MAGIC;
  11381. +#else
  11382. + return 0;
  11383. +#endif
  11384. +}
  11385. +
  11386. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  11387. +{
  11388. +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
  11389. + return sb->s_magic == FUSE_SUPER_MAGIC;
  11390. +#else
  11391. + return 0;
  11392. +#endif
  11393. +}
  11394. +
  11395. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  11396. +{
  11397. +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
  11398. + return sb->s_magic == XFS_SB_MAGIC;
  11399. +#else
  11400. + return 0;
  11401. +#endif
  11402. +}
  11403. +
  11404. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  11405. +{
  11406. +#ifdef CONFIG_TMPFS
  11407. + return sb->s_magic == TMPFS_MAGIC;
  11408. +#else
  11409. + return 0;
  11410. +#endif
  11411. +}
  11412. +
  11413. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  11414. +{
  11415. +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
  11416. + return !strcmp(au_sbtype(sb), "ecryptfs");
  11417. +#else
  11418. + return 0;
  11419. +#endif
  11420. +}
  11421. +
  11422. +static inline int au_test_ramfs(struct super_block *sb)
  11423. +{
  11424. + return sb->s_magic == RAMFS_MAGIC;
  11425. +}
  11426. +
  11427. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  11428. +{
  11429. +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
  11430. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  11431. +#else
  11432. + return 0;
  11433. +#endif
  11434. +}
  11435. +
  11436. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  11437. +{
  11438. +#ifdef CONFIG_PROC_FS
  11439. + return sb->s_magic == PROC_SUPER_MAGIC;
  11440. +#else
  11441. + return 0;
  11442. +#endif
  11443. +}
  11444. +
  11445. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  11446. +{
  11447. +#ifdef CONFIG_SYSFS
  11448. + return sb->s_magic == SYSFS_MAGIC;
  11449. +#else
  11450. + return 0;
  11451. +#endif
  11452. +}
  11453. +
  11454. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  11455. +{
  11456. +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
  11457. + return sb->s_magic == CONFIGFS_MAGIC;
  11458. +#else
  11459. + return 0;
  11460. +#endif
  11461. +}
  11462. +
  11463. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  11464. +{
  11465. +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
  11466. + return sb->s_magic == MINIX3_SUPER_MAGIC
  11467. + || sb->s_magic == MINIX2_SUPER_MAGIC
  11468. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  11469. + || sb->s_magic == MINIX_SUPER_MAGIC
  11470. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  11471. +#else
  11472. + return 0;
  11473. +#endif
  11474. +}
  11475. +
  11476. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  11477. +{
  11478. +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
  11479. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  11480. +#else
  11481. + return 0;
  11482. +#endif
  11483. +}
  11484. +
  11485. +static inline int au_test_msdos(struct super_block *sb)
  11486. +{
  11487. + return au_test_fat(sb);
  11488. +}
  11489. +
  11490. +static inline int au_test_vfat(struct super_block *sb)
  11491. +{
  11492. + return au_test_fat(sb);
  11493. +}
  11494. +
  11495. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  11496. +{
  11497. +#ifdef CONFIG_SECURITYFS
  11498. + return sb->s_magic == SECURITYFS_MAGIC;
  11499. +#else
  11500. + return 0;
  11501. +#endif
  11502. +}
  11503. +
  11504. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  11505. +{
  11506. +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
  11507. + return sb->s_magic == SQUASHFS_MAGIC;
  11508. +#else
  11509. + return 0;
  11510. +#endif
  11511. +}
  11512. +
  11513. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  11514. +{
  11515. +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
  11516. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  11517. +#else
  11518. + return 0;
  11519. +#endif
  11520. +}
  11521. +
  11522. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  11523. +{
  11524. +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
  11525. + return sb->s_magic == XENFS_SUPER_MAGIC;
  11526. +#else
  11527. + return 0;
  11528. +#endif
  11529. +}
  11530. +
  11531. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  11532. +{
  11533. +#ifdef CONFIG_DEBUG_FS
  11534. + return sb->s_magic == DEBUGFS_MAGIC;
  11535. +#else
  11536. + return 0;
  11537. +#endif
  11538. +}
  11539. +
  11540. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  11541. +{
  11542. +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
  11543. + return sb->s_magic == NILFS_SUPER_MAGIC;
  11544. +#else
  11545. + return 0;
  11546. +#endif
  11547. +}
  11548. +
  11549. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  11550. +{
  11551. +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
  11552. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  11553. +#else
  11554. + return 0;
  11555. +#endif
  11556. +}
  11557. +
  11558. +/* ---------------------------------------------------------------------- */
  11559. +/*
  11560. + * they can't be an aufs branch.
  11561. + */
  11562. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  11563. +{
  11564. + return
  11565. +#ifndef CONFIG_AUFS_BR_RAMFS
  11566. + au_test_ramfs(sb) ||
  11567. +#endif
  11568. + au_test_procfs(sb)
  11569. + || au_test_sysfs(sb)
  11570. + || au_test_configfs(sb)
  11571. + || au_test_debugfs(sb)
  11572. + || au_test_securityfs(sb)
  11573. + || au_test_xenfs(sb)
  11574. + || au_test_ecryptfs(sb)
  11575. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  11576. + || au_test_aufs(sb); /* will be supported in next version */
  11577. +}
  11578. +
  11579. +static inline int au_test_fs_remote(struct super_block *sb)
  11580. +{
  11581. + return !au_test_tmpfs(sb)
  11582. +#ifdef CONFIG_AUFS_BR_RAMFS
  11583. + && !au_test_ramfs(sb)
  11584. +#endif
  11585. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  11586. +}
  11587. +
  11588. +/* ---------------------------------------------------------------------- */
  11589. +
  11590. +/*
  11591. + * Note: these functions (below) are created after reading ->getattr() in all
  11592. + * filesystems under linux/fs. it means we have to do so in every update...
  11593. + */
  11594. +
  11595. +/*
  11596. + * some filesystems require getattr to refresh the inode attributes before
  11597. + * referencing.
  11598. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  11599. + * and leave the work for d_revalidate()
  11600. + */
  11601. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  11602. +{
  11603. + return au_test_nfs(sb)
  11604. + || au_test_fuse(sb)
  11605. + /* || au_test_btrfs(sb) */ /* untested */
  11606. + ;
  11607. +}
  11608. +
  11609. +/*
  11610. + * filesystems which don't maintain i_size or i_blocks.
  11611. + */
  11612. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  11613. +{
  11614. + return au_test_xfs(sb)
  11615. + || au_test_btrfs(sb)
  11616. + || au_test_ubifs(sb)
  11617. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  11618. + /* || au_test_minix(sb) */ /* untested */
  11619. + ;
  11620. +}
  11621. +
  11622. +/*
  11623. + * filesystems which don't store the correct value in some of their inode
  11624. + * attributes.
  11625. + */
  11626. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  11627. +{
  11628. + return au_test_fs_bad_iattr_size(sb)
  11629. + || au_test_fat(sb)
  11630. + || au_test_msdos(sb)
  11631. + || au_test_vfat(sb);
  11632. +}
  11633. +
  11634. +/* they don't check i_nlink in link(2) */
  11635. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  11636. +{
  11637. + return au_test_tmpfs(sb)
  11638. +#ifdef CONFIG_AUFS_BR_RAMFS
  11639. + || au_test_ramfs(sb)
  11640. +#endif
  11641. + || au_test_ubifs(sb)
  11642. + || au_test_hfsplus(sb);
  11643. +}
  11644. +
  11645. +/*
  11646. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  11647. + */
  11648. +static inline int au_test_fs_notime(struct super_block *sb)
  11649. +{
  11650. + return au_test_nfs(sb)
  11651. + || au_test_fuse(sb)
  11652. + || au_test_ubifs(sb)
  11653. + ;
  11654. +}
  11655. +
  11656. +/* temporary support for i#1 in cramfs */
  11657. +static inline int au_test_fs_unique_ino(struct inode *inode)
  11658. +{
  11659. + if (au_test_cramfs(inode->i_sb))
  11660. + return inode->i_ino != 1;
  11661. + return 1;
  11662. +}
  11663. +
  11664. +/* ---------------------------------------------------------------------- */
  11665. +
  11666. +/*
  11667. + * the filesystem where the xino files placed must support i/o after unlink and
  11668. + * maintain i_size and i_blocks.
  11669. + */
  11670. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  11671. +{
  11672. + return au_test_fs_remote(sb)
  11673. + || au_test_fs_bad_iattr_size(sb)
  11674. + /* don't want unnecessary work for xino */
  11675. + || au_test_aufs(sb)
  11676. + || au_test_ecryptfs(sb)
  11677. + || au_test_nilfs(sb);
  11678. +}
  11679. +
  11680. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  11681. +{
  11682. + return au_test_tmpfs(sb)
  11683. + || au_test_ramfs(sb);
  11684. +}
  11685. +
  11686. +/*
  11687. + * test if the @sb is real-readonly.
  11688. + */
  11689. +static inline int au_test_fs_rr(struct super_block *sb)
  11690. +{
  11691. + return au_test_squashfs(sb)
  11692. + || au_test_iso9660(sb)
  11693. + || au_test_cramfs(sb)
  11694. + || au_test_romfs(sb);
  11695. +}
  11696. +
  11697. +/*
  11698. + * test if the @inode is nfs with 'noacl' option
  11699. + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
  11700. + */
  11701. +static inline int au_test_nfs_noacl(struct inode *inode)
  11702. +{
  11703. + return au_test_nfs(inode->i_sb)
  11704. + /* && IS_POSIXACL(inode) */
  11705. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  11706. +}
  11707. +
  11708. +#endif /* __KERNEL__ */
  11709. +#endif /* __AUFS_FSTYPE_H__ */
  11710. diff -Nur linux-4.0.5.orig/fs/aufs/hfsnotify.c linux-4.0.5/fs/aufs/hfsnotify.c
  11711. --- linux-4.0.5.orig/fs/aufs/hfsnotify.c 1969-12-31 18:00:00.000000000 -0600
  11712. +++ linux-4.0.5/fs/aufs/hfsnotify.c 2015-06-21 14:07:48.686432781 -0500
  11713. @@ -0,0 +1,288 @@
  11714. +/*
  11715. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11716. + *
  11717. + * This program, aufs is free software; you can redistribute it and/or modify
  11718. + * it under the terms of the GNU General Public License as published by
  11719. + * the Free Software Foundation; either version 2 of the License, or
  11720. + * (at your option) any later version.
  11721. + *
  11722. + * This program is distributed in the hope that it will be useful,
  11723. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11724. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11725. + * GNU General Public License for more details.
  11726. + *
  11727. + * You should have received a copy of the GNU General Public License
  11728. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11729. + */
  11730. +
  11731. +/*
  11732. + * fsnotify for the lower directories
  11733. + */
  11734. +
  11735. +#include "aufs.h"
  11736. +
  11737. +/* FS_IN_IGNORED is unnecessary */
  11738. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  11739. + | FS_CREATE | FS_EVENT_ON_CHILD);
  11740. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  11741. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  11742. +
  11743. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  11744. +{
  11745. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  11746. + hn_mark);
  11747. + AuDbg("here\n");
  11748. + au_cache_free_hnotify(hn);
  11749. + smp_mb__before_atomic();
  11750. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  11751. + wake_up(&au_hfsn_wq);
  11752. +}
  11753. +
  11754. +static int au_hfsn_alloc(struct au_hinode *hinode)
  11755. +{
  11756. + int err;
  11757. + struct au_hnotify *hn;
  11758. + struct super_block *sb;
  11759. + struct au_branch *br;
  11760. + struct fsnotify_mark *mark;
  11761. + aufs_bindex_t bindex;
  11762. +
  11763. + hn = hinode->hi_notify;
  11764. + sb = hn->hn_aufs_inode->i_sb;
  11765. + bindex = au_br_index(sb, hinode->hi_id);
  11766. + br = au_sbr(sb, bindex);
  11767. + AuDebugOn(!br->br_hfsn);
  11768. +
  11769. + mark = &hn->hn_mark;
  11770. + fsnotify_init_mark(mark, au_hfsn_free_mark);
  11771. + mark->mask = AuHfsnMask;
  11772. + /*
  11773. + * by udba rename or rmdir, aufs assign a new inode to the known
  11774. + * h_inode, so specify 1 to allow dups.
  11775. + */
  11776. + lockdep_off();
  11777. + err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
  11778. + /*mnt*/NULL, /*allow_dups*/1);
  11779. + /* even if err */
  11780. + fsnotify_put_mark(mark);
  11781. + lockdep_on();
  11782. +
  11783. + return err;
  11784. +}
  11785. +
  11786. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  11787. +{
  11788. + struct fsnotify_mark *mark;
  11789. + unsigned long long ull;
  11790. + struct fsnotify_group *group;
  11791. +
  11792. + ull = atomic64_inc_return(&au_hfsn_ifree);
  11793. + BUG_ON(!ull);
  11794. +
  11795. + mark = &hn->hn_mark;
  11796. + spin_lock(&mark->lock);
  11797. + group = mark->group;
  11798. + fsnotify_get_group(group);
  11799. + spin_unlock(&mark->lock);
  11800. + lockdep_off();
  11801. + fsnotify_destroy_mark(mark, group);
  11802. + fsnotify_put_group(group);
  11803. + lockdep_on();
  11804. +
  11805. + /* free hn by myself */
  11806. + return 0;
  11807. +}
  11808. +
  11809. +/* ---------------------------------------------------------------------- */
  11810. +
  11811. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  11812. +{
  11813. + struct fsnotify_mark *mark;
  11814. +
  11815. + mark = &hinode->hi_notify->hn_mark;
  11816. + spin_lock(&mark->lock);
  11817. + if (do_set) {
  11818. + AuDebugOn(mark->mask & AuHfsnMask);
  11819. + mark->mask |= AuHfsnMask;
  11820. + } else {
  11821. + AuDebugOn(!(mark->mask & AuHfsnMask));
  11822. + mark->mask &= ~AuHfsnMask;
  11823. + }
  11824. + spin_unlock(&mark->lock);
  11825. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  11826. +}
  11827. +
  11828. +/* ---------------------------------------------------------------------- */
  11829. +
  11830. +/* #define AuDbgHnotify */
  11831. +#ifdef AuDbgHnotify
  11832. +static char *au_hfsn_name(u32 mask)
  11833. +{
  11834. +#ifdef CONFIG_AUFS_DEBUG
  11835. +#define test_ret(flag) \
  11836. + do { \
  11837. + if (mask & flag) \
  11838. + return #flag; \
  11839. + } while (0)
  11840. + test_ret(FS_ACCESS);
  11841. + test_ret(FS_MODIFY);
  11842. + test_ret(FS_ATTRIB);
  11843. + test_ret(FS_CLOSE_WRITE);
  11844. + test_ret(FS_CLOSE_NOWRITE);
  11845. + test_ret(FS_OPEN);
  11846. + test_ret(FS_MOVED_FROM);
  11847. + test_ret(FS_MOVED_TO);
  11848. + test_ret(FS_CREATE);
  11849. + test_ret(FS_DELETE);
  11850. + test_ret(FS_DELETE_SELF);
  11851. + test_ret(FS_MOVE_SELF);
  11852. + test_ret(FS_UNMOUNT);
  11853. + test_ret(FS_Q_OVERFLOW);
  11854. + test_ret(FS_IN_IGNORED);
  11855. + test_ret(FS_ISDIR);
  11856. + test_ret(FS_IN_ONESHOT);
  11857. + test_ret(FS_EVENT_ON_CHILD);
  11858. + return "";
  11859. +#undef test_ret
  11860. +#else
  11861. + return "??";
  11862. +#endif
  11863. +}
  11864. +#endif
  11865. +
  11866. +/* ---------------------------------------------------------------------- */
  11867. +
  11868. +static void au_hfsn_free_group(struct fsnotify_group *group)
  11869. +{
  11870. + struct au_br_hfsnotify *hfsn = group->private;
  11871. +
  11872. + AuDbg("here\n");
  11873. + kfree(hfsn);
  11874. +}
  11875. +
  11876. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  11877. + struct inode *inode,
  11878. + struct fsnotify_mark *inode_mark,
  11879. + struct fsnotify_mark *vfsmount_mark,
  11880. + u32 mask, void *data, int data_type,
  11881. + const unsigned char *file_name, u32 cookie)
  11882. +{
  11883. + int err;
  11884. + struct au_hnotify *hnotify;
  11885. + struct inode *h_dir, *h_inode;
  11886. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  11887. +
  11888. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  11889. +
  11890. + err = 0;
  11891. + /* if FS_UNMOUNT happens, there must be another bug */
  11892. + AuDebugOn(mask & FS_UNMOUNT);
  11893. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  11894. + goto out;
  11895. +
  11896. + h_dir = inode;
  11897. + h_inode = NULL;
  11898. +#ifdef AuDbgHnotify
  11899. + au_debug_on();
  11900. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  11901. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  11902. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  11903. + h_dir->i_ino, mask, au_hfsn_name(mask),
  11904. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  11905. + /* WARN_ON(1); */
  11906. + }
  11907. + au_debug_off();
  11908. +#endif
  11909. +
  11910. + AuDebugOn(!inode_mark);
  11911. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  11912. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  11913. +
  11914. +out:
  11915. + return err;
  11916. +}
  11917. +
  11918. +static struct fsnotify_ops au_hfsn_ops = {
  11919. + .handle_event = au_hfsn_handle_event,
  11920. + .free_group_priv = au_hfsn_free_group
  11921. +};
  11922. +
  11923. +/* ---------------------------------------------------------------------- */
  11924. +
  11925. +static void au_hfsn_fin_br(struct au_branch *br)
  11926. +{
  11927. + struct au_br_hfsnotify *hfsn;
  11928. +
  11929. + hfsn = br->br_hfsn;
  11930. + if (hfsn) {
  11931. + lockdep_off();
  11932. + fsnotify_put_group(hfsn->hfsn_group);
  11933. + lockdep_on();
  11934. + }
  11935. +}
  11936. +
  11937. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  11938. +{
  11939. + int err;
  11940. + struct fsnotify_group *group;
  11941. + struct au_br_hfsnotify *hfsn;
  11942. +
  11943. + err = 0;
  11944. + br->br_hfsn = NULL;
  11945. + if (!au_br_hnotifyable(perm))
  11946. + goto out;
  11947. +
  11948. + err = -ENOMEM;
  11949. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  11950. + if (unlikely(!hfsn))
  11951. + goto out;
  11952. +
  11953. + err = 0;
  11954. + group = fsnotify_alloc_group(&au_hfsn_ops);
  11955. + if (IS_ERR(group)) {
  11956. + err = PTR_ERR(group);
  11957. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  11958. + goto out_hfsn;
  11959. + }
  11960. +
  11961. + group->private = hfsn;
  11962. + hfsn->hfsn_group = group;
  11963. + br->br_hfsn = hfsn;
  11964. + goto out; /* success */
  11965. +
  11966. +out_hfsn:
  11967. + kfree(hfsn);
  11968. +out:
  11969. + return err;
  11970. +}
  11971. +
  11972. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  11973. +{
  11974. + int err;
  11975. +
  11976. + err = 0;
  11977. + if (!br->br_hfsn)
  11978. + err = au_hfsn_init_br(br, perm);
  11979. +
  11980. + return err;
  11981. +}
  11982. +
  11983. +/* ---------------------------------------------------------------------- */
  11984. +
  11985. +static void au_hfsn_fin(void)
  11986. +{
  11987. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  11988. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  11989. +}
  11990. +
  11991. +const struct au_hnotify_op au_hnotify_op = {
  11992. + .ctl = au_hfsn_ctl,
  11993. + .alloc = au_hfsn_alloc,
  11994. + .free = au_hfsn_free,
  11995. +
  11996. + .fin = au_hfsn_fin,
  11997. +
  11998. + .reset_br = au_hfsn_reset_br,
  11999. + .fin_br = au_hfsn_fin_br,
  12000. + .init_br = au_hfsn_init_br
  12001. +};
  12002. diff -Nur linux-4.0.5.orig/fs/aufs/hfsplus.c linux-4.0.5/fs/aufs/hfsplus.c
  12003. --- linux-4.0.5.orig/fs/aufs/hfsplus.c 1969-12-31 18:00:00.000000000 -0600
  12004. +++ linux-4.0.5/fs/aufs/hfsplus.c 2015-06-21 14:07:48.686432781 -0500
  12005. @@ -0,0 +1,56 @@
  12006. +/*
  12007. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  12008. + *
  12009. + * This program, aufs is free software; you can redistribute it and/or modify
  12010. + * it under the terms of the GNU General Public License as published by
  12011. + * the Free Software Foundation; either version 2 of the License, or
  12012. + * (at your option) any later version.
  12013. + *
  12014. + * This program is distributed in the hope that it will be useful,
  12015. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12016. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12017. + * GNU General Public License for more details.
  12018. + *
  12019. + * You should have received a copy of the GNU General Public License
  12020. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12021. + */
  12022. +
  12023. +/*
  12024. + * special support for filesystems which aqucires an inode mutex
  12025. + * at final closing a file, eg, hfsplus.
  12026. + *
  12027. + * This trick is very simple and stupid, just to open the file before really
  12028. + * neceeary open to tell hfsplus that this is not the final closing.
  12029. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  12030. + * and au_h_open_post() after releasing it.
  12031. + */
  12032. +
  12033. +#include "aufs.h"
  12034. +
  12035. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  12036. + int force_wr)
  12037. +{
  12038. + struct file *h_file;
  12039. + struct dentry *h_dentry;
  12040. +
  12041. + h_dentry = au_h_dptr(dentry, bindex);
  12042. + AuDebugOn(!h_dentry);
  12043. + AuDebugOn(!h_dentry->d_inode);
  12044. +
  12045. + h_file = NULL;
  12046. + if (au_test_hfsplus(h_dentry->d_sb)
  12047. + && d_is_reg(h_dentry))
  12048. + h_file = au_h_open(dentry, bindex,
  12049. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  12050. + /*file*/NULL, force_wr);
  12051. + return h_file;
  12052. +}
  12053. +
  12054. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  12055. + struct file *h_file)
  12056. +{
  12057. + if (h_file) {
  12058. + fput(h_file);
  12059. + au_sbr_put(dentry->d_sb, bindex);
  12060. + }
  12061. +}
  12062. diff -Nur linux-4.0.5.orig/fs/aufs/hnotify.c linux-4.0.5/fs/aufs/hnotify.c
  12063. --- linux-4.0.5.orig/fs/aufs/hnotify.c 1969-12-31 18:00:00.000000000 -0600
  12064. +++ linux-4.0.5/fs/aufs/hnotify.c 2015-06-21 14:07:48.686432781 -0500
  12065. @@ -0,0 +1,714 @@
  12066. +/*
  12067. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  12068. + *
  12069. + * This program, aufs is free software; you can redistribute it and/or modify
  12070. + * it under the terms of the GNU General Public License as published by
  12071. + * the Free Software Foundation; either version 2 of the License, or
  12072. + * (at your option) any later version.
  12073. + *
  12074. + * This program is distributed in the hope that it will be useful,
  12075. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12076. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12077. + * GNU General Public License for more details.
  12078. + *
  12079. + * You should have received a copy of the GNU General Public License
  12080. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12081. + */
  12082. +
  12083. +/*
  12084. + * abstraction to notify the direct changes on lower directories
  12085. + */
  12086. +
  12087. +#include "aufs.h"
  12088. +
  12089. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  12090. +{
  12091. + int err;
  12092. + struct au_hnotify *hn;
  12093. +
  12094. + err = -ENOMEM;
  12095. + hn = au_cache_alloc_hnotify();
  12096. + if (hn) {
  12097. + hn->hn_aufs_inode = inode;
  12098. + hinode->hi_notify = hn;
  12099. + err = au_hnotify_op.alloc(hinode);
  12100. + AuTraceErr(err);
  12101. + if (unlikely(err)) {
  12102. + hinode->hi_notify = NULL;
  12103. + au_cache_free_hnotify(hn);
  12104. + /*
  12105. + * The upper dir was removed by udba, but the same named
  12106. + * dir left. In this case, aufs assignes a new inode
  12107. + * number and set the monitor again.
  12108. + * For the lower dir, the old monitnor is still left.
  12109. + */
  12110. + if (err == -EEXIST)
  12111. + err = 0;
  12112. + }
  12113. + }
  12114. +
  12115. + AuTraceErr(err);
  12116. + return err;
  12117. +}
  12118. +
  12119. +void au_hn_free(struct au_hinode *hinode)
  12120. +{
  12121. + struct au_hnotify *hn;
  12122. +
  12123. + hn = hinode->hi_notify;
  12124. + if (hn) {
  12125. + hinode->hi_notify = NULL;
  12126. + if (au_hnotify_op.free(hinode, hn))
  12127. + au_cache_free_hnotify(hn);
  12128. + }
  12129. +}
  12130. +
  12131. +/* ---------------------------------------------------------------------- */
  12132. +
  12133. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  12134. +{
  12135. + if (hinode->hi_notify)
  12136. + au_hnotify_op.ctl(hinode, do_set);
  12137. +}
  12138. +
  12139. +void au_hn_reset(struct inode *inode, unsigned int flags)
  12140. +{
  12141. + aufs_bindex_t bindex, bend;
  12142. + struct inode *hi;
  12143. + struct dentry *iwhdentry;
  12144. +
  12145. + bend = au_ibend(inode);
  12146. + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
  12147. + hi = au_h_iptr(inode, bindex);
  12148. + if (!hi)
  12149. + continue;
  12150. +
  12151. + /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
  12152. + iwhdentry = au_hi_wh(inode, bindex);
  12153. + if (iwhdentry)
  12154. + dget(iwhdentry);
  12155. + au_igrab(hi);
  12156. + au_set_h_iptr(inode, bindex, NULL, 0);
  12157. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  12158. + flags & ~AuHi_XINO);
  12159. + iput(hi);
  12160. + dput(iwhdentry);
  12161. + /* mutex_unlock(&hi->i_mutex); */
  12162. + }
  12163. +}
  12164. +
  12165. +/* ---------------------------------------------------------------------- */
  12166. +
  12167. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  12168. +{
  12169. + int err;
  12170. + aufs_bindex_t bindex, bend, bfound, bstart;
  12171. + struct inode *h_i;
  12172. +
  12173. + err = 0;
  12174. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  12175. + pr_warn("branch root dir was changed\n");
  12176. + goto out;
  12177. + }
  12178. +
  12179. + bfound = -1;
  12180. + bend = au_ibend(inode);
  12181. + bstart = au_ibstart(inode);
  12182. +#if 0 /* reserved for future use */
  12183. + if (bindex == bend) {
  12184. + /* keep this ino in rename case */
  12185. + goto out;
  12186. + }
  12187. +#endif
  12188. + for (bindex = bstart; bindex <= bend; bindex++)
  12189. + if (au_h_iptr(inode, bindex) == h_inode) {
  12190. + bfound = bindex;
  12191. + break;
  12192. + }
  12193. + if (bfound < 0)
  12194. + goto out;
  12195. +
  12196. + for (bindex = bstart; bindex <= bend; bindex++) {
  12197. + h_i = au_h_iptr(inode, bindex);
  12198. + if (!h_i)
  12199. + continue;
  12200. +
  12201. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  12202. + /* ignore this error */
  12203. + /* bad action? */
  12204. + }
  12205. +
  12206. + /* children inode number will be broken */
  12207. +
  12208. +out:
  12209. + AuTraceErr(err);
  12210. + return err;
  12211. +}
  12212. +
  12213. +static int hn_gen_tree(struct dentry *dentry)
  12214. +{
  12215. + int err, i, j, ndentry;
  12216. + struct au_dcsub_pages dpages;
  12217. + struct au_dpage *dpage;
  12218. + struct dentry **dentries;
  12219. +
  12220. + err = au_dpages_init(&dpages, GFP_NOFS);
  12221. + if (unlikely(err))
  12222. + goto out;
  12223. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  12224. + if (unlikely(err))
  12225. + goto out_dpages;
  12226. +
  12227. + for (i = 0; i < dpages.ndpage; i++) {
  12228. + dpage = dpages.dpages + i;
  12229. + dentries = dpage->dentries;
  12230. + ndentry = dpage->ndentry;
  12231. + for (j = 0; j < ndentry; j++) {
  12232. + struct dentry *d;
  12233. +
  12234. + d = dentries[j];
  12235. + if (IS_ROOT(d))
  12236. + continue;
  12237. +
  12238. + au_digen_dec(d);
  12239. + if (d->d_inode)
  12240. + /* todo: reset children xino?
  12241. + cached children only? */
  12242. + au_iigen_dec(d->d_inode);
  12243. + }
  12244. + }
  12245. +
  12246. +out_dpages:
  12247. + au_dpages_free(&dpages);
  12248. +
  12249. +#if 0
  12250. + /* discard children */
  12251. + dentry_unhash(dentry);
  12252. + dput(dentry);
  12253. +#endif
  12254. +out:
  12255. + return err;
  12256. +}
  12257. +
  12258. +/*
  12259. + * return 0 if processed.
  12260. + */
  12261. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  12262. + const unsigned int isdir)
  12263. +{
  12264. + int err;
  12265. + struct dentry *d;
  12266. + struct qstr *dname;
  12267. +
  12268. + err = 1;
  12269. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  12270. + pr_warn("branch root dir was changed\n");
  12271. + err = 0;
  12272. + goto out;
  12273. + }
  12274. +
  12275. + if (!isdir) {
  12276. + AuDebugOn(!name);
  12277. + au_iigen_dec(inode);
  12278. + spin_lock(&inode->i_lock);
  12279. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  12280. + spin_lock(&d->d_lock);
  12281. + dname = &d->d_name;
  12282. + if (dname->len != nlen
  12283. + && memcmp(dname->name, name, nlen)) {
  12284. + spin_unlock(&d->d_lock);
  12285. + continue;
  12286. + }
  12287. + err = 0;
  12288. + au_digen_dec(d);
  12289. + spin_unlock(&d->d_lock);
  12290. + break;
  12291. + }
  12292. + spin_unlock(&inode->i_lock);
  12293. + } else {
  12294. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  12295. + d = d_find_any_alias(inode);
  12296. + if (!d) {
  12297. + au_iigen_dec(inode);
  12298. + goto out;
  12299. + }
  12300. +
  12301. + spin_lock(&d->d_lock);
  12302. + dname = &d->d_name;
  12303. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  12304. + spin_unlock(&d->d_lock);
  12305. + err = hn_gen_tree(d);
  12306. + spin_lock(&d->d_lock);
  12307. + }
  12308. + spin_unlock(&d->d_lock);
  12309. + dput(d);
  12310. + }
  12311. +
  12312. +out:
  12313. + AuTraceErr(err);
  12314. + return err;
  12315. +}
  12316. +
  12317. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  12318. +{
  12319. + int err;
  12320. + struct inode *inode;
  12321. +
  12322. + inode = dentry->d_inode;
  12323. + if (IS_ROOT(dentry)
  12324. + /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
  12325. + ) {
  12326. + pr_warn("branch root dir was changed\n");
  12327. + return 0;
  12328. + }
  12329. +
  12330. + err = 0;
  12331. + if (!isdir) {
  12332. + au_digen_dec(dentry);
  12333. + if (inode)
  12334. + au_iigen_dec(inode);
  12335. + } else {
  12336. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  12337. + if (inode)
  12338. + err = hn_gen_tree(dentry);
  12339. + }
  12340. +
  12341. + AuTraceErr(err);
  12342. + return err;
  12343. +}
  12344. +
  12345. +/* ---------------------------------------------------------------------- */
  12346. +
  12347. +/* hnotify job flags */
  12348. +#define AuHnJob_XINO0 1
  12349. +#define AuHnJob_GEN (1 << 1)
  12350. +#define AuHnJob_DIRENT (1 << 2)
  12351. +#define AuHnJob_ISDIR (1 << 3)
  12352. +#define AuHnJob_TRYXINO0 (1 << 4)
  12353. +#define AuHnJob_MNTPNT (1 << 5)
  12354. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  12355. +#define au_fset_hnjob(flags, name) \
  12356. + do { (flags) |= AuHnJob_##name; } while (0)
  12357. +#define au_fclr_hnjob(flags, name) \
  12358. + do { (flags) &= ~AuHnJob_##name; } while (0)
  12359. +
  12360. +enum {
  12361. + AuHn_CHILD,
  12362. + AuHn_PARENT,
  12363. + AuHnLast
  12364. +};
  12365. +
  12366. +struct au_hnotify_args {
  12367. + struct inode *h_dir, *dir, *h_child_inode;
  12368. + u32 mask;
  12369. + unsigned int flags[AuHnLast];
  12370. + unsigned int h_child_nlen;
  12371. + char h_child_name[];
  12372. +};
  12373. +
  12374. +struct hn_job_args {
  12375. + unsigned int flags;
  12376. + struct inode *inode, *h_inode, *dir, *h_dir;
  12377. + struct dentry *dentry;
  12378. + char *h_name;
  12379. + int h_nlen;
  12380. +};
  12381. +
  12382. +static int hn_job(struct hn_job_args *a)
  12383. +{
  12384. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  12385. + int e;
  12386. +
  12387. + /* reset xino */
  12388. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  12389. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  12390. +
  12391. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  12392. + && a->inode
  12393. + && a->h_inode) {
  12394. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  12395. + if (!a->h_inode->i_nlink
  12396. + && !(a->h_inode->i_state & I_LINKABLE))
  12397. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  12398. + mutex_unlock(&a->h_inode->i_mutex);
  12399. + }
  12400. +
  12401. + /* make the generation obsolete */
  12402. + if (au_ftest_hnjob(a->flags, GEN)) {
  12403. + e = -1;
  12404. + if (a->inode)
  12405. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  12406. + isdir);
  12407. + if (e && a->dentry)
  12408. + hn_gen_by_name(a->dentry, isdir);
  12409. + /* ignore this error */
  12410. + }
  12411. +
  12412. + /* make dir entries obsolete */
  12413. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  12414. + struct au_vdir *vdir;
  12415. +
  12416. + vdir = au_ivdir(a->inode);
  12417. + if (vdir)
  12418. + vdir->vd_jiffy = 0;
  12419. + /* IMustLock(a->inode); */
  12420. + /* a->inode->i_version++; */
  12421. + }
  12422. +
  12423. + /* can do nothing but warn */
  12424. + if (au_ftest_hnjob(a->flags, MNTPNT)
  12425. + && a->dentry
  12426. + && d_mountpoint(a->dentry))
  12427. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  12428. +
  12429. + return 0;
  12430. +}
  12431. +
  12432. +/* ---------------------------------------------------------------------- */
  12433. +
  12434. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  12435. + struct inode *dir)
  12436. +{
  12437. + struct dentry *dentry, *d, *parent;
  12438. + struct qstr *dname;
  12439. +
  12440. + parent = d_find_any_alias(dir);
  12441. + if (!parent)
  12442. + return NULL;
  12443. +
  12444. + dentry = NULL;
  12445. + spin_lock(&parent->d_lock);
  12446. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  12447. + /* AuDbg("%pd\n", d); */
  12448. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  12449. + dname = &d->d_name;
  12450. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  12451. + goto cont_unlock;
  12452. + if (au_di(d))
  12453. + au_digen_dec(d);
  12454. + else
  12455. + goto cont_unlock;
  12456. + if (au_dcount(d) > 0) {
  12457. + dentry = dget_dlock(d);
  12458. + spin_unlock(&d->d_lock);
  12459. + break;
  12460. + }
  12461. +
  12462. +cont_unlock:
  12463. + spin_unlock(&d->d_lock);
  12464. + }
  12465. + spin_unlock(&parent->d_lock);
  12466. + dput(parent);
  12467. +
  12468. + if (dentry)
  12469. + di_write_lock_child(dentry);
  12470. +
  12471. + return dentry;
  12472. +}
  12473. +
  12474. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  12475. + aufs_bindex_t bindex, ino_t h_ino)
  12476. +{
  12477. + struct inode *inode;
  12478. + ino_t ino;
  12479. + int err;
  12480. +
  12481. + inode = NULL;
  12482. + err = au_xino_read(sb, bindex, h_ino, &ino);
  12483. + if (!err && ino)
  12484. + inode = ilookup(sb, ino);
  12485. + if (!inode)
  12486. + goto out;
  12487. +
  12488. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  12489. + pr_warn("wrong root branch\n");
  12490. + iput(inode);
  12491. + inode = NULL;
  12492. + goto out;
  12493. + }
  12494. +
  12495. + ii_write_lock_child(inode);
  12496. +
  12497. +out:
  12498. + return inode;
  12499. +}
  12500. +
  12501. +static void au_hn_bh(void *_args)
  12502. +{
  12503. + struct au_hnotify_args *a = _args;
  12504. + struct super_block *sb;
  12505. + aufs_bindex_t bindex, bend, bfound;
  12506. + unsigned char xino, try_iput;
  12507. + int err;
  12508. + struct inode *inode;
  12509. + ino_t h_ino;
  12510. + struct hn_job_args args;
  12511. + struct dentry *dentry;
  12512. + struct au_sbinfo *sbinfo;
  12513. +
  12514. + AuDebugOn(!_args);
  12515. + AuDebugOn(!a->h_dir);
  12516. + AuDebugOn(!a->dir);
  12517. + AuDebugOn(!a->mask);
  12518. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  12519. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  12520. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  12521. +
  12522. + inode = NULL;
  12523. + dentry = NULL;
  12524. + /*
  12525. + * do not lock a->dir->i_mutex here
  12526. + * because of d_revalidate() may cause a deadlock.
  12527. + */
  12528. + sb = a->dir->i_sb;
  12529. + AuDebugOn(!sb);
  12530. + sbinfo = au_sbi(sb);
  12531. + AuDebugOn(!sbinfo);
  12532. + si_write_lock(sb, AuLock_NOPLMW);
  12533. +
  12534. + ii_read_lock_parent(a->dir);
  12535. + bfound = -1;
  12536. + bend = au_ibend(a->dir);
  12537. + for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
  12538. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  12539. + bfound = bindex;
  12540. + break;
  12541. + }
  12542. + ii_read_unlock(a->dir);
  12543. + if (unlikely(bfound < 0))
  12544. + goto out;
  12545. +
  12546. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  12547. + h_ino = 0;
  12548. + if (a->h_child_inode)
  12549. + h_ino = a->h_child_inode->i_ino;
  12550. +
  12551. + if (a->h_child_nlen
  12552. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  12553. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  12554. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  12555. + a->dir);
  12556. + try_iput = 0;
  12557. + if (dentry)
  12558. + inode = dentry->d_inode;
  12559. + if (xino && !inode && h_ino
  12560. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  12561. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  12562. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  12563. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  12564. + try_iput = 1;
  12565. + }
  12566. +
  12567. + args.flags = a->flags[AuHn_CHILD];
  12568. + args.dentry = dentry;
  12569. + args.inode = inode;
  12570. + args.h_inode = a->h_child_inode;
  12571. + args.dir = a->dir;
  12572. + args.h_dir = a->h_dir;
  12573. + args.h_name = a->h_child_name;
  12574. + args.h_nlen = a->h_child_nlen;
  12575. + err = hn_job(&args);
  12576. + if (dentry) {
  12577. + if (au_di(dentry))
  12578. + di_write_unlock(dentry);
  12579. + dput(dentry);
  12580. + }
  12581. + if (inode && try_iput) {
  12582. + ii_write_unlock(inode);
  12583. + iput(inode);
  12584. + }
  12585. +
  12586. + ii_write_lock_parent(a->dir);
  12587. + args.flags = a->flags[AuHn_PARENT];
  12588. + args.dentry = NULL;
  12589. + args.inode = a->dir;
  12590. + args.h_inode = a->h_dir;
  12591. + args.dir = NULL;
  12592. + args.h_dir = NULL;
  12593. + args.h_name = NULL;
  12594. + args.h_nlen = 0;
  12595. + err = hn_job(&args);
  12596. + ii_write_unlock(a->dir);
  12597. +
  12598. +out:
  12599. + iput(a->h_child_inode);
  12600. + iput(a->h_dir);
  12601. + iput(a->dir);
  12602. + si_write_unlock(sb);
  12603. + au_nwt_done(&sbinfo->si_nowait);
  12604. + kfree(a);
  12605. +}
  12606. +
  12607. +/* ---------------------------------------------------------------------- */
  12608. +
  12609. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  12610. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  12611. +{
  12612. + int err, len;
  12613. + unsigned int flags[AuHnLast], f;
  12614. + unsigned char isdir, isroot, wh;
  12615. + struct inode *dir;
  12616. + struct au_hnotify_args *args;
  12617. + char *p, *h_child_name;
  12618. +
  12619. + err = 0;
  12620. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  12621. + dir = igrab(hnotify->hn_aufs_inode);
  12622. + if (!dir)
  12623. + goto out;
  12624. +
  12625. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  12626. + wh = 0;
  12627. + h_child_name = (void *)h_child_qstr->name;
  12628. + len = h_child_qstr->len;
  12629. + if (h_child_name) {
  12630. + if (len > AUFS_WH_PFX_LEN
  12631. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  12632. + h_child_name += AUFS_WH_PFX_LEN;
  12633. + len -= AUFS_WH_PFX_LEN;
  12634. + wh = 1;
  12635. + }
  12636. + }
  12637. +
  12638. + isdir = 0;
  12639. + if (h_child_inode)
  12640. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  12641. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  12642. + flags[AuHn_CHILD] = 0;
  12643. + if (isdir)
  12644. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  12645. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  12646. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  12647. + switch (mask & FS_EVENTS_POSS_ON_CHILD) {
  12648. + case FS_MOVED_FROM:
  12649. + case FS_MOVED_TO:
  12650. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  12651. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  12652. + /*FALLTHROUGH*/
  12653. + case FS_CREATE:
  12654. + AuDebugOn(!h_child_name);
  12655. + break;
  12656. +
  12657. + case FS_DELETE:
  12658. + /*
  12659. + * aufs never be able to get this child inode.
  12660. + * revalidation should be in d_revalidate()
  12661. + * by checking i_nlink, i_generation or d_unhashed().
  12662. + */
  12663. + AuDebugOn(!h_child_name);
  12664. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  12665. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  12666. + break;
  12667. +
  12668. + default:
  12669. + AuDebugOn(1);
  12670. + }
  12671. +
  12672. + if (wh)
  12673. + h_child_inode = NULL;
  12674. +
  12675. + err = -ENOMEM;
  12676. + /* iput() and kfree() will be called in au_hnotify() */
  12677. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  12678. + if (unlikely(!args)) {
  12679. + AuErr1("no memory\n");
  12680. + iput(dir);
  12681. + goto out;
  12682. + }
  12683. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  12684. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  12685. + args->mask = mask;
  12686. + args->dir = dir;
  12687. + args->h_dir = igrab(h_dir);
  12688. + if (h_child_inode)
  12689. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  12690. + args->h_child_inode = h_child_inode;
  12691. + args->h_child_nlen = len;
  12692. + if (len) {
  12693. + p = (void *)args;
  12694. + p += sizeof(*args);
  12695. + memcpy(p, h_child_name, len);
  12696. + p[len] = 0;
  12697. + }
  12698. +
  12699. + /* NFS fires the event for silly-renamed one from kworker */
  12700. + f = 0;
  12701. + if (!dir->i_nlink
  12702. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  12703. + f = AuWkq_NEST;
  12704. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  12705. + if (unlikely(err)) {
  12706. + pr_err("wkq %d\n", err);
  12707. + iput(args->h_child_inode);
  12708. + iput(args->h_dir);
  12709. + iput(args->dir);
  12710. + kfree(args);
  12711. + }
  12712. +
  12713. +out:
  12714. + return err;
  12715. +}
  12716. +
  12717. +/* ---------------------------------------------------------------------- */
  12718. +
  12719. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  12720. +{
  12721. + int err;
  12722. +
  12723. + AuDebugOn(!(udba & AuOptMask_UDBA));
  12724. +
  12725. + err = 0;
  12726. + if (au_hnotify_op.reset_br)
  12727. + err = au_hnotify_op.reset_br(udba, br, perm);
  12728. +
  12729. + return err;
  12730. +}
  12731. +
  12732. +int au_hnotify_init_br(struct au_branch *br, int perm)
  12733. +{
  12734. + int err;
  12735. +
  12736. + err = 0;
  12737. + if (au_hnotify_op.init_br)
  12738. + err = au_hnotify_op.init_br(br, perm);
  12739. +
  12740. + return err;
  12741. +}
  12742. +
  12743. +void au_hnotify_fin_br(struct au_branch *br)
  12744. +{
  12745. + if (au_hnotify_op.fin_br)
  12746. + au_hnotify_op.fin_br(br);
  12747. +}
  12748. +
  12749. +static void au_hn_destroy_cache(void)
  12750. +{
  12751. + kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
  12752. + au_cachep[AuCache_HNOTIFY] = NULL;
  12753. +}
  12754. +
  12755. +int __init au_hnotify_init(void)
  12756. +{
  12757. + int err;
  12758. +
  12759. + err = -ENOMEM;
  12760. + au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
  12761. + if (au_cachep[AuCache_HNOTIFY]) {
  12762. + err = 0;
  12763. + if (au_hnotify_op.init)
  12764. + err = au_hnotify_op.init();
  12765. + if (unlikely(err))
  12766. + au_hn_destroy_cache();
  12767. + }
  12768. + AuTraceErr(err);
  12769. + return err;
  12770. +}
  12771. +
  12772. +void au_hnotify_fin(void)
  12773. +{
  12774. + if (au_hnotify_op.fin)
  12775. + au_hnotify_op.fin();
  12776. + /* cf. au_cache_fin() */
  12777. + if (au_cachep[AuCache_HNOTIFY])
  12778. + au_hn_destroy_cache();
  12779. +}
  12780. diff -Nur linux-4.0.5.orig/fs/aufs/iinfo.c linux-4.0.5/fs/aufs/iinfo.c
  12781. --- linux-4.0.5.orig/fs/aufs/iinfo.c 1969-12-31 18:00:00.000000000 -0600
  12782. +++ linux-4.0.5/fs/aufs/iinfo.c 2015-06-21 14:07:48.686432781 -0500
  12783. @@ -0,0 +1,277 @@
  12784. +/*
  12785. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  12786. + *
  12787. + * This program, aufs is free software; you can redistribute it and/or modify
  12788. + * it under the terms of the GNU General Public License as published by
  12789. + * the Free Software Foundation; either version 2 of the License, or
  12790. + * (at your option) any later version.
  12791. + *
  12792. + * This program is distributed in the hope that it will be useful,
  12793. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12794. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12795. + * GNU General Public License for more details.
  12796. + *
  12797. + * You should have received a copy of the GNU General Public License
  12798. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12799. + */
  12800. +
  12801. +/*
  12802. + * inode private data
  12803. + */
  12804. +
  12805. +#include "aufs.h"
  12806. +
  12807. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  12808. +{
  12809. + struct inode *h_inode;
  12810. +
  12811. + IiMustAnyLock(inode);
  12812. +
  12813. + h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
  12814. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  12815. + return h_inode;
  12816. +}
  12817. +
  12818. +/* todo: hard/soft set? */
  12819. +void au_hiput(struct au_hinode *hinode)
  12820. +{
  12821. + au_hn_free(hinode);
  12822. + dput(hinode->hi_whdentry);
  12823. + iput(hinode->hi_inode);
  12824. +}
  12825. +
  12826. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  12827. +{
  12828. + unsigned int flags;
  12829. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  12830. +
  12831. + flags = 0;
  12832. + if (au_opt_test(mnt_flags, XINO))
  12833. + au_fset_hi(flags, XINO);
  12834. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  12835. + au_fset_hi(flags, HNOTIFY);
  12836. + return flags;
  12837. +}
  12838. +
  12839. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  12840. + struct inode *h_inode, unsigned int flags)
  12841. +{
  12842. + struct au_hinode *hinode;
  12843. + struct inode *hi;
  12844. + struct au_iinfo *iinfo = au_ii(inode);
  12845. +
  12846. + IiMustWriteLock(inode);
  12847. +
  12848. + hinode = iinfo->ii_hinode + bindex;
  12849. + hi = hinode->hi_inode;
  12850. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  12851. +
  12852. + if (hi)
  12853. + au_hiput(hinode);
  12854. + hinode->hi_inode = h_inode;
  12855. + if (h_inode) {
  12856. + int err;
  12857. + struct super_block *sb = inode->i_sb;
  12858. + struct au_branch *br;
  12859. +
  12860. + AuDebugOn(inode->i_mode
  12861. + && (h_inode->i_mode & S_IFMT)
  12862. + != (inode->i_mode & S_IFMT));
  12863. + if (bindex == iinfo->ii_bstart)
  12864. + au_cpup_igen(inode, h_inode);
  12865. + br = au_sbr(sb, bindex);
  12866. + hinode->hi_id = br->br_id;
  12867. + if (au_ftest_hi(flags, XINO)) {
  12868. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  12869. + inode->i_ino);
  12870. + if (unlikely(err))
  12871. + AuIOErr1("failed au_xino_write() %d\n", err);
  12872. + }
  12873. +
  12874. + if (au_ftest_hi(flags, HNOTIFY)
  12875. + && au_br_hnotifyable(br->br_perm)) {
  12876. + err = au_hn_alloc(hinode, inode);
  12877. + if (unlikely(err))
  12878. + AuIOErr1("au_hn_alloc() %d\n", err);
  12879. + }
  12880. + }
  12881. +}
  12882. +
  12883. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  12884. + struct dentry *h_wh)
  12885. +{
  12886. + struct au_hinode *hinode;
  12887. +
  12888. + IiMustWriteLock(inode);
  12889. +
  12890. + hinode = au_ii(inode)->ii_hinode + bindex;
  12891. + AuDebugOn(hinode->hi_whdentry);
  12892. + hinode->hi_whdentry = h_wh;
  12893. +}
  12894. +
  12895. +void au_update_iigen(struct inode *inode, int half)
  12896. +{
  12897. + struct au_iinfo *iinfo;
  12898. + struct au_iigen *iigen;
  12899. + unsigned int sigen;
  12900. +
  12901. + sigen = au_sigen(inode->i_sb);
  12902. + iinfo = au_ii(inode);
  12903. + iigen = &iinfo->ii_generation;
  12904. + spin_lock(&iinfo->ii_genspin);
  12905. + iigen->ig_generation = sigen;
  12906. + if (half)
  12907. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  12908. + else
  12909. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  12910. + spin_unlock(&iinfo->ii_genspin);
  12911. +}
  12912. +
  12913. +/* it may be called at remount time, too */
  12914. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  12915. +{
  12916. + struct au_iinfo *iinfo;
  12917. + aufs_bindex_t bindex, bend;
  12918. +
  12919. + iinfo = au_ii(inode);
  12920. + if (!iinfo)
  12921. + return;
  12922. +
  12923. + IiMustWriteLock(inode);
  12924. +
  12925. + if (do_put_zero && iinfo->ii_bstart >= 0) {
  12926. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
  12927. + bindex++) {
  12928. + struct inode *h_i;
  12929. +
  12930. + h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
  12931. + if (h_i
  12932. + && !h_i->i_nlink
  12933. + && !(h_i->i_state & I_LINKABLE))
  12934. + au_set_h_iptr(inode, bindex, NULL, 0);
  12935. + }
  12936. + }
  12937. +
  12938. + iinfo->ii_bstart = -1;
  12939. + iinfo->ii_bend = -1;
  12940. + bend = au_sbend(inode->i_sb);
  12941. + for (bindex = 0; bindex <= bend; bindex++)
  12942. + if (iinfo->ii_hinode[0 + bindex].hi_inode) {
  12943. + iinfo->ii_bstart = bindex;
  12944. + break;
  12945. + }
  12946. + if (iinfo->ii_bstart >= 0)
  12947. + for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
  12948. + if (iinfo->ii_hinode[0 + bindex].hi_inode) {
  12949. + iinfo->ii_bend = bindex;
  12950. + break;
  12951. + }
  12952. + AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
  12953. +}
  12954. +
  12955. +/* ---------------------------------------------------------------------- */
  12956. +
  12957. +void au_icntnr_init_once(void *_c)
  12958. +{
  12959. + struct au_icntnr *c = _c;
  12960. + struct au_iinfo *iinfo = &c->iinfo;
  12961. + static struct lock_class_key aufs_ii;
  12962. +
  12963. + spin_lock_init(&iinfo->ii_genspin);
  12964. + au_rw_init(&iinfo->ii_rwsem);
  12965. + au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
  12966. + inode_init_once(&c->vfs_inode);
  12967. +}
  12968. +
  12969. +int au_iinfo_init(struct inode *inode)
  12970. +{
  12971. + struct au_iinfo *iinfo;
  12972. + struct super_block *sb;
  12973. + int nbr, i;
  12974. +
  12975. + sb = inode->i_sb;
  12976. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  12977. + nbr = au_sbend(sb) + 1;
  12978. + if (unlikely(nbr <= 0))
  12979. + nbr = 1;
  12980. + iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  12981. + if (iinfo->ii_hinode) {
  12982. + au_ninodes_inc(sb);
  12983. + for (i = 0; i < nbr; i++)
  12984. + iinfo->ii_hinode[i].hi_id = -1;
  12985. +
  12986. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  12987. + iinfo->ii_bstart = -1;
  12988. + iinfo->ii_bend = -1;
  12989. + iinfo->ii_vdir = NULL;
  12990. + return 0;
  12991. + }
  12992. + return -ENOMEM;
  12993. +}
  12994. +
  12995. +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
  12996. +{
  12997. + int err, sz;
  12998. + struct au_hinode *hip;
  12999. +
  13000. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  13001. +
  13002. + err = -ENOMEM;
  13003. + sz = sizeof(*hip) * (iinfo->ii_bend + 1);
  13004. + if (!sz)
  13005. + sz = sizeof(*hip);
  13006. + hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
  13007. + if (hip) {
  13008. + iinfo->ii_hinode = hip;
  13009. + err = 0;
  13010. + }
  13011. +
  13012. + return err;
  13013. +}
  13014. +
  13015. +void au_iinfo_fin(struct inode *inode)
  13016. +{
  13017. + struct au_iinfo *iinfo;
  13018. + struct au_hinode *hi;
  13019. + struct super_block *sb;
  13020. + aufs_bindex_t bindex, bend;
  13021. + const unsigned char unlinked = !inode->i_nlink;
  13022. +
  13023. + iinfo = au_ii(inode);
  13024. + /* bad_inode case */
  13025. + if (!iinfo)
  13026. + return;
  13027. +
  13028. + sb = inode->i_sb;
  13029. + au_ninodes_dec(sb);
  13030. + if (si_pid_test(sb))
  13031. + au_xino_delete_inode(inode, unlinked);
  13032. + else {
  13033. + /*
  13034. + * it is safe to hide the dependency between sbinfo and
  13035. + * sb->s_umount.
  13036. + */
  13037. + lockdep_off();
  13038. + si_noflush_read_lock(sb);
  13039. + au_xino_delete_inode(inode, unlinked);
  13040. + si_read_unlock(sb);
  13041. + lockdep_on();
  13042. + }
  13043. +
  13044. + if (iinfo->ii_vdir)
  13045. + au_vdir_free(iinfo->ii_vdir);
  13046. +
  13047. + bindex = iinfo->ii_bstart;
  13048. + if (bindex >= 0) {
  13049. + hi = iinfo->ii_hinode + bindex;
  13050. + bend = iinfo->ii_bend;
  13051. + while (bindex++ <= bend) {
  13052. + if (hi->hi_inode)
  13053. + au_hiput(hi);
  13054. + hi++;
  13055. + }
  13056. + }
  13057. + kfree(iinfo->ii_hinode);
  13058. + iinfo->ii_hinode = NULL;
  13059. + AuRwDestroy(&iinfo->ii_rwsem);
  13060. +}
  13061. diff -Nur linux-4.0.5.orig/fs/aufs/inode.c linux-4.0.5/fs/aufs/inode.c
  13062. --- linux-4.0.5.orig/fs/aufs/inode.c 1969-12-31 18:00:00.000000000 -0600
  13063. +++ linux-4.0.5/fs/aufs/inode.c 2015-06-21 14:07:48.686432781 -0500
  13064. @@ -0,0 +1,495 @@
  13065. +/*
  13066. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13067. + *
  13068. + * This program, aufs is free software; you can redistribute it and/or modify
  13069. + * it under the terms of the GNU General Public License as published by
  13070. + * the Free Software Foundation; either version 2 of the License, or
  13071. + * (at your option) any later version.
  13072. + *
  13073. + * This program is distributed in the hope that it will be useful,
  13074. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13075. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13076. + * GNU General Public License for more details.
  13077. + *
  13078. + * You should have received a copy of the GNU General Public License
  13079. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13080. + */
  13081. +
  13082. +/*
  13083. + * inode functions
  13084. + */
  13085. +
  13086. +#include "aufs.h"
  13087. +
  13088. +struct inode *au_igrab(struct inode *inode)
  13089. +{
  13090. + if (inode) {
  13091. + AuDebugOn(!atomic_read(&inode->i_count));
  13092. + ihold(inode);
  13093. + }
  13094. + return inode;
  13095. +}
  13096. +
  13097. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  13098. +{
  13099. + au_cpup_attr_all(inode, /*force*/0);
  13100. + au_update_iigen(inode, /*half*/1);
  13101. + if (do_version)
  13102. + inode->i_version++;
  13103. +}
  13104. +
  13105. +static int au_ii_refresh(struct inode *inode, int *update)
  13106. +{
  13107. + int err, e;
  13108. + umode_t type;
  13109. + aufs_bindex_t bindex, new_bindex;
  13110. + struct super_block *sb;
  13111. + struct au_iinfo *iinfo;
  13112. + struct au_hinode *p, *q, tmp;
  13113. +
  13114. + IiMustWriteLock(inode);
  13115. +
  13116. + *update = 0;
  13117. + sb = inode->i_sb;
  13118. + type = inode->i_mode & S_IFMT;
  13119. + iinfo = au_ii(inode);
  13120. + err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
  13121. + if (unlikely(err))
  13122. + goto out;
  13123. +
  13124. + AuDebugOn(iinfo->ii_bstart < 0);
  13125. + p = iinfo->ii_hinode + iinfo->ii_bstart;
  13126. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
  13127. + bindex++, p++) {
  13128. + if (!p->hi_inode)
  13129. + continue;
  13130. +
  13131. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  13132. + new_bindex = au_br_index(sb, p->hi_id);
  13133. + if (new_bindex == bindex)
  13134. + continue;
  13135. +
  13136. + if (new_bindex < 0) {
  13137. + *update = 1;
  13138. + au_hiput(p);
  13139. + p->hi_inode = NULL;
  13140. + continue;
  13141. + }
  13142. +
  13143. + if (new_bindex < iinfo->ii_bstart)
  13144. + iinfo->ii_bstart = new_bindex;
  13145. + if (iinfo->ii_bend < new_bindex)
  13146. + iinfo->ii_bend = new_bindex;
  13147. + /* swap two lower inode, and loop again */
  13148. + q = iinfo->ii_hinode + new_bindex;
  13149. + tmp = *q;
  13150. + *q = *p;
  13151. + *p = tmp;
  13152. + if (tmp.hi_inode) {
  13153. + bindex--;
  13154. + p--;
  13155. + }
  13156. + }
  13157. + au_update_ibrange(inode, /*do_put_zero*/0);
  13158. + e = au_dy_irefresh(inode);
  13159. + if (unlikely(e && !err))
  13160. + err = e;
  13161. +
  13162. +out:
  13163. + AuTraceErr(err);
  13164. + return err;
  13165. +}
  13166. +
  13167. +int au_refresh_hinode_self(struct inode *inode)
  13168. +{
  13169. + int err, update;
  13170. +
  13171. + err = au_ii_refresh(inode, &update);
  13172. + if (!err)
  13173. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  13174. +
  13175. + AuTraceErr(err);
  13176. + return err;
  13177. +}
  13178. +
  13179. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  13180. +{
  13181. + int err, e, update;
  13182. + unsigned int flags;
  13183. + umode_t mode;
  13184. + aufs_bindex_t bindex, bend;
  13185. + unsigned char isdir;
  13186. + struct au_hinode *p;
  13187. + struct au_iinfo *iinfo;
  13188. +
  13189. + err = au_ii_refresh(inode, &update);
  13190. + if (unlikely(err))
  13191. + goto out;
  13192. +
  13193. + update = 0;
  13194. + iinfo = au_ii(inode);
  13195. + p = iinfo->ii_hinode + iinfo->ii_bstart;
  13196. + mode = (inode->i_mode & S_IFMT);
  13197. + isdir = S_ISDIR(mode);
  13198. + flags = au_hi_flags(inode, isdir);
  13199. + bend = au_dbend(dentry);
  13200. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
  13201. + struct inode *h_i;
  13202. + struct dentry *h_d;
  13203. +
  13204. + h_d = au_h_dptr(dentry, bindex);
  13205. + if (!h_d || !h_d->d_inode)
  13206. + continue;
  13207. +
  13208. + AuDebugOn(mode != (h_d->d_inode->i_mode & S_IFMT));
  13209. + if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
  13210. + h_i = au_h_iptr(inode, bindex);
  13211. + if (h_i) {
  13212. + if (h_i == h_d->d_inode)
  13213. + continue;
  13214. + err = -EIO;
  13215. + break;
  13216. + }
  13217. + }
  13218. + if (bindex < iinfo->ii_bstart)
  13219. + iinfo->ii_bstart = bindex;
  13220. + if (iinfo->ii_bend < bindex)
  13221. + iinfo->ii_bend = bindex;
  13222. + au_set_h_iptr(inode, bindex, au_igrab(h_d->d_inode), flags);
  13223. + update = 1;
  13224. + }
  13225. + au_update_ibrange(inode, /*do_put_zero*/0);
  13226. + e = au_dy_irefresh(inode);
  13227. + if (unlikely(e && !err))
  13228. + err = e;
  13229. + if (!err)
  13230. + au_refresh_hinode_attr(inode, update && isdir);
  13231. +
  13232. +out:
  13233. + AuTraceErr(err);
  13234. + return err;
  13235. +}
  13236. +
  13237. +static int set_inode(struct inode *inode, struct dentry *dentry)
  13238. +{
  13239. + int err;
  13240. + unsigned int flags;
  13241. + umode_t mode;
  13242. + aufs_bindex_t bindex, bstart, btail;
  13243. + unsigned char isdir;
  13244. + struct dentry *h_dentry;
  13245. + struct inode *h_inode;
  13246. + struct au_iinfo *iinfo;
  13247. +
  13248. + IiMustWriteLock(inode);
  13249. +
  13250. + err = 0;
  13251. + isdir = 0;
  13252. + bstart = au_dbstart(dentry);
  13253. + h_inode = au_h_dptr(dentry, bstart)->d_inode;
  13254. + mode = h_inode->i_mode;
  13255. + switch (mode & S_IFMT) {
  13256. + case S_IFREG:
  13257. + btail = au_dbtail(dentry);
  13258. + inode->i_op = &aufs_iop;
  13259. + inode->i_fop = &aufs_file_fop;
  13260. + err = au_dy_iaop(inode, bstart, h_inode);
  13261. + if (unlikely(err))
  13262. + goto out;
  13263. + break;
  13264. + case S_IFDIR:
  13265. + isdir = 1;
  13266. + btail = au_dbtaildir(dentry);
  13267. + inode->i_op = &aufs_dir_iop;
  13268. + inode->i_fop = &aufs_dir_fop;
  13269. + break;
  13270. + case S_IFLNK:
  13271. + btail = au_dbtail(dentry);
  13272. + inode->i_op = &aufs_symlink_iop;
  13273. + break;
  13274. + case S_IFBLK:
  13275. + case S_IFCHR:
  13276. + case S_IFIFO:
  13277. + case S_IFSOCK:
  13278. + btail = au_dbtail(dentry);
  13279. + inode->i_op = &aufs_iop;
  13280. + init_special_inode(inode, mode, h_inode->i_rdev);
  13281. + break;
  13282. + default:
  13283. + AuIOErr("Unknown file type 0%o\n", mode);
  13284. + err = -EIO;
  13285. + goto out;
  13286. + }
  13287. +
  13288. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  13289. + flags = au_hi_flags(inode, isdir);
  13290. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  13291. + && au_ftest_hi(flags, HNOTIFY)
  13292. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  13293. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  13294. + au_fclr_hi(flags, HNOTIFY);
  13295. + iinfo = au_ii(inode);
  13296. + iinfo->ii_bstart = bstart;
  13297. + iinfo->ii_bend = btail;
  13298. + for (bindex = bstart; bindex <= btail; bindex++) {
  13299. + h_dentry = au_h_dptr(dentry, bindex);
  13300. + if (h_dentry)
  13301. + au_set_h_iptr(inode, bindex,
  13302. + au_igrab(h_dentry->d_inode), flags);
  13303. + }
  13304. + au_cpup_attr_all(inode, /*force*/1);
  13305. + /*
  13306. + * to force calling aufs_get_acl() every time,
  13307. + * do not call cache_no_acl() for aufs inode.
  13308. + */
  13309. +
  13310. +out:
  13311. + return err;
  13312. +}
  13313. +
  13314. +/*
  13315. + * successful returns with iinfo write_locked
  13316. + * minus: errno
  13317. + * zero: success, matched
  13318. + * plus: no error, but unmatched
  13319. + */
  13320. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  13321. +{
  13322. + int err;
  13323. + unsigned int gen;
  13324. + struct au_iigen iigen;
  13325. + aufs_bindex_t bindex, bend;
  13326. + struct inode *h_inode, *h_dinode;
  13327. +
  13328. + /*
  13329. + * before this function, if aufs got any iinfo lock, it must be only
  13330. + * one, the parent dir.
  13331. + * it can happen by UDBA and the obsoleted inode number.
  13332. + */
  13333. + err = -EIO;
  13334. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  13335. + goto out;
  13336. +
  13337. + err = 1;
  13338. + ii_write_lock_new_child(inode);
  13339. + h_dinode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
  13340. + bend = au_ibend(inode);
  13341. + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
  13342. + h_inode = au_h_iptr(inode, bindex);
  13343. + if (!h_inode || h_inode != h_dinode)
  13344. + continue;
  13345. +
  13346. + err = 0;
  13347. + gen = au_iigen(inode, &iigen);
  13348. + if (gen == au_digen(dentry)
  13349. + && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
  13350. + break;
  13351. +
  13352. + /* fully refresh inode using dentry */
  13353. + err = au_refresh_hinode(inode, dentry);
  13354. + if (!err)
  13355. + au_update_iigen(inode, /*half*/0);
  13356. + break;
  13357. + }
  13358. +
  13359. + if (unlikely(err))
  13360. + ii_write_unlock(inode);
  13361. +out:
  13362. + return err;
  13363. +}
  13364. +
  13365. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  13366. + unsigned int d_type, ino_t *ino)
  13367. +{
  13368. + int err;
  13369. + struct mutex *mtx;
  13370. +
  13371. + /* prevent hardlinked inode number from race condition */
  13372. + mtx = NULL;
  13373. + if (d_type != DT_DIR) {
  13374. + mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
  13375. + mutex_lock(mtx);
  13376. + }
  13377. + err = au_xino_read(sb, bindex, h_ino, ino);
  13378. + if (unlikely(err))
  13379. + goto out;
  13380. +
  13381. + if (!*ino) {
  13382. + err = -EIO;
  13383. + *ino = au_xino_new_ino(sb);
  13384. + if (unlikely(!*ino))
  13385. + goto out;
  13386. + err = au_xino_write(sb, bindex, h_ino, *ino);
  13387. + if (unlikely(err))
  13388. + goto out;
  13389. + }
  13390. +
  13391. +out:
  13392. + if (mtx)
  13393. + mutex_unlock(mtx);
  13394. + return err;
  13395. +}
  13396. +
  13397. +/* successful returns with iinfo write_locked */
  13398. +/* todo: return with unlocked? */
  13399. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  13400. +{
  13401. + struct inode *inode;
  13402. + struct dentry *h_dentry;
  13403. + struct super_block *sb;
  13404. + struct mutex *mtx;
  13405. + ino_t h_ino, ino;
  13406. + int err;
  13407. + aufs_bindex_t bstart;
  13408. +
  13409. + sb = dentry->d_sb;
  13410. + bstart = au_dbstart(dentry);
  13411. + h_dentry = au_h_dptr(dentry, bstart);
  13412. + h_ino = h_dentry->d_inode->i_ino;
  13413. +
  13414. + /*
  13415. + * stop 'race'-ing between hardlinks under different
  13416. + * parents.
  13417. + */
  13418. + mtx = NULL;
  13419. + if (!d_is_dir(h_dentry))
  13420. + mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
  13421. +
  13422. +new_ino:
  13423. + if (mtx)
  13424. + mutex_lock(mtx);
  13425. + err = au_xino_read(sb, bstart, h_ino, &ino);
  13426. + inode = ERR_PTR(err);
  13427. + if (unlikely(err))
  13428. + goto out;
  13429. +
  13430. + if (!ino) {
  13431. + ino = au_xino_new_ino(sb);
  13432. + if (unlikely(!ino)) {
  13433. + inode = ERR_PTR(-EIO);
  13434. + goto out;
  13435. + }
  13436. + }
  13437. +
  13438. + AuDbg("i%lu\n", (unsigned long)ino);
  13439. + inode = au_iget_locked(sb, ino);
  13440. + err = PTR_ERR(inode);
  13441. + if (IS_ERR(inode))
  13442. + goto out;
  13443. +
  13444. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  13445. + if (inode->i_state & I_NEW) {
  13446. + /* verbose coding for lock class name */
  13447. + if (unlikely(d_is_symlink(h_dentry)))
  13448. + au_rw_class(&au_ii(inode)->ii_rwsem,
  13449. + au_lc_key + AuLcSymlink_IIINFO);
  13450. + else if (unlikely(d_is_dir(h_dentry)))
  13451. + au_rw_class(&au_ii(inode)->ii_rwsem,
  13452. + au_lc_key + AuLcDir_IIINFO);
  13453. + else /* likely */
  13454. + au_rw_class(&au_ii(inode)->ii_rwsem,
  13455. + au_lc_key + AuLcNonDir_IIINFO);
  13456. +
  13457. + ii_write_lock_new_child(inode);
  13458. + err = set_inode(inode, dentry);
  13459. + if (!err) {
  13460. + unlock_new_inode(inode);
  13461. + goto out; /* success */
  13462. + }
  13463. +
  13464. + /*
  13465. + * iget_failed() calls iput(), but we need to call
  13466. + * ii_write_unlock() after iget_failed(). so dirty hack for
  13467. + * i_count.
  13468. + */
  13469. + atomic_inc(&inode->i_count);
  13470. + iget_failed(inode);
  13471. + ii_write_unlock(inode);
  13472. + au_xino_write(sb, bstart, h_ino, /*ino*/0);
  13473. + /* ignore this error */
  13474. + goto out_iput;
  13475. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  13476. + /*
  13477. + * horrible race condition between lookup, readdir and copyup
  13478. + * (or something).
  13479. + */
  13480. + if (mtx)
  13481. + mutex_unlock(mtx);
  13482. + err = reval_inode(inode, dentry);
  13483. + if (unlikely(err < 0)) {
  13484. + mtx = NULL;
  13485. + goto out_iput;
  13486. + }
  13487. +
  13488. + if (!err) {
  13489. + mtx = NULL;
  13490. + goto out; /* success */
  13491. + } else if (mtx)
  13492. + mutex_lock(mtx);
  13493. + }
  13494. +
  13495. + if (unlikely(au_test_fs_unique_ino(h_dentry->d_inode)))
  13496. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  13497. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  13498. + bstart, au_sbtype(h_dentry->d_sb), dentry,
  13499. + (unsigned long)h_ino, (unsigned long)ino);
  13500. + ino = 0;
  13501. + err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
  13502. + if (!err) {
  13503. + iput(inode);
  13504. + if (mtx)
  13505. + mutex_unlock(mtx);
  13506. + goto new_ino;
  13507. + }
  13508. +
  13509. +out_iput:
  13510. + iput(inode);
  13511. + inode = ERR_PTR(err);
  13512. +out:
  13513. + if (mtx)
  13514. + mutex_unlock(mtx);
  13515. + return inode;
  13516. +}
  13517. +
  13518. +/* ---------------------------------------------------------------------- */
  13519. +
  13520. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  13521. + struct inode *inode)
  13522. +{
  13523. + int err;
  13524. + struct inode *hi;
  13525. +
  13526. + err = au_br_rdonly(au_sbr(sb, bindex));
  13527. +
  13528. + /* pseudo-link after flushed may happen out of bounds */
  13529. + if (!err
  13530. + && inode
  13531. + && au_ibstart(inode) <= bindex
  13532. + && bindex <= au_ibend(inode)) {
  13533. + /*
  13534. + * permission check is unnecessary since vfsub routine
  13535. + * will be called later
  13536. + */
  13537. + hi = au_h_iptr(inode, bindex);
  13538. + if (hi)
  13539. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  13540. + }
  13541. +
  13542. + return err;
  13543. +}
  13544. +
  13545. +int au_test_h_perm(struct inode *h_inode, int mask)
  13546. +{
  13547. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  13548. + return 0;
  13549. + return inode_permission(h_inode, mask);
  13550. +}
  13551. +
  13552. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  13553. +{
  13554. + if (au_test_nfs(h_inode->i_sb)
  13555. + && (mask & MAY_WRITE)
  13556. + && S_ISDIR(h_inode->i_mode))
  13557. + mask |= MAY_READ; /* force permission check */
  13558. + return au_test_h_perm(h_inode, mask);
  13559. +}
  13560. diff -Nur linux-4.0.5.orig/fs/aufs/inode.h linux-4.0.5/fs/aufs/inode.h
  13561. --- linux-4.0.5.orig/fs/aufs/inode.h 1969-12-31 18:00:00.000000000 -0600
  13562. +++ linux-4.0.5/fs/aufs/inode.h 2015-06-21 14:07:48.686432781 -0500
  13563. @@ -0,0 +1,673 @@
  13564. +/*
  13565. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13566. + *
  13567. + * This program, aufs is free software; you can redistribute it and/or modify
  13568. + * it under the terms of the GNU General Public License as published by
  13569. + * the Free Software Foundation; either version 2 of the License, or
  13570. + * (at your option) any later version.
  13571. + *
  13572. + * This program is distributed in the hope that it will be useful,
  13573. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13574. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13575. + * GNU General Public License for more details.
  13576. + *
  13577. + * You should have received a copy of the GNU General Public License
  13578. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13579. + */
  13580. +
  13581. +/*
  13582. + * inode operations
  13583. + */
  13584. +
  13585. +#ifndef __AUFS_INODE_H__
  13586. +#define __AUFS_INODE_H__
  13587. +
  13588. +#ifdef __KERNEL__
  13589. +
  13590. +#include <linux/fsnotify.h>
  13591. +#include "rwsem.h"
  13592. +
  13593. +struct vfsmount;
  13594. +
  13595. +struct au_hnotify {
  13596. +#ifdef CONFIG_AUFS_HNOTIFY
  13597. +#ifdef CONFIG_AUFS_HFSNOTIFY
  13598. + /* never use fsnotify_add_vfsmount_mark() */
  13599. + struct fsnotify_mark hn_mark;
  13600. +#endif
  13601. + struct inode *hn_aufs_inode; /* no get/put */
  13602. +#endif
  13603. +} ____cacheline_aligned_in_smp;
  13604. +
  13605. +struct au_hinode {
  13606. + struct inode *hi_inode;
  13607. + aufs_bindex_t hi_id;
  13608. +#ifdef CONFIG_AUFS_HNOTIFY
  13609. + struct au_hnotify *hi_notify;
  13610. +#endif
  13611. +
  13612. + /* reference to the copied-up whiteout with get/put */
  13613. + struct dentry *hi_whdentry;
  13614. +};
  13615. +
  13616. +/* ig_flags */
  13617. +#define AuIG_HALF_REFRESHED 1
  13618. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  13619. +#define au_ig_fset(flags, name) \
  13620. + do { (flags) |= AuIG_##name; } while (0)
  13621. +#define au_ig_fclr(flags, name) \
  13622. + do { (flags) &= ~AuIG_##name; } while (0)
  13623. +
  13624. +struct au_iigen {
  13625. + __u32 ig_generation, ig_flags;
  13626. +};
  13627. +
  13628. +struct au_vdir;
  13629. +struct au_iinfo {
  13630. + spinlock_t ii_genspin;
  13631. + struct au_iigen ii_generation;
  13632. + struct super_block *ii_hsb1; /* no get/put */
  13633. +
  13634. + struct au_rwsem ii_rwsem;
  13635. + aufs_bindex_t ii_bstart, ii_bend;
  13636. + __u32 ii_higen;
  13637. + struct au_hinode *ii_hinode;
  13638. + struct au_vdir *ii_vdir;
  13639. +};
  13640. +
  13641. +struct au_icntnr {
  13642. + struct au_iinfo iinfo;
  13643. + struct inode vfs_inode;
  13644. +} ____cacheline_aligned_in_smp;
  13645. +
  13646. +/* au_pin flags */
  13647. +#define AuPin_DI_LOCKED 1
  13648. +#define AuPin_MNT_WRITE (1 << 1)
  13649. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  13650. +#define au_fset_pin(flags, name) \
  13651. + do { (flags) |= AuPin_##name; } while (0)
  13652. +#define au_fclr_pin(flags, name) \
  13653. + do { (flags) &= ~AuPin_##name; } while (0)
  13654. +
  13655. +struct au_pin {
  13656. + /* input */
  13657. + struct dentry *dentry;
  13658. + unsigned int udba;
  13659. + unsigned char lsc_di, lsc_hi, flags;
  13660. + aufs_bindex_t bindex;
  13661. +
  13662. + /* output */
  13663. + struct dentry *parent;
  13664. + struct au_hinode *hdir;
  13665. + struct vfsmount *h_mnt;
  13666. +
  13667. + /* temporary unlock/relock for copyup */
  13668. + struct dentry *h_dentry, *h_parent;
  13669. + struct au_branch *br;
  13670. + struct task_struct *task;
  13671. +};
  13672. +
  13673. +void au_pin_hdir_unlock(struct au_pin *p);
  13674. +int au_pin_hdir_lock(struct au_pin *p);
  13675. +int au_pin_hdir_relock(struct au_pin *p);
  13676. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
  13677. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  13678. +void au_pin_hdir_release(struct au_pin *p);
  13679. +
  13680. +/* ---------------------------------------------------------------------- */
  13681. +
  13682. +static inline struct au_iinfo *au_ii(struct inode *inode)
  13683. +{
  13684. + struct au_iinfo *iinfo;
  13685. +
  13686. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  13687. + if (iinfo->ii_hinode)
  13688. + return iinfo;
  13689. + return NULL; /* debugging bad_inode case */
  13690. +}
  13691. +
  13692. +/* ---------------------------------------------------------------------- */
  13693. +
  13694. +/* inode.c */
  13695. +struct inode *au_igrab(struct inode *inode);
  13696. +int au_refresh_hinode_self(struct inode *inode);
  13697. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  13698. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  13699. + unsigned int d_type, ino_t *ino);
  13700. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  13701. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  13702. + struct inode *inode);
  13703. +int au_test_h_perm(struct inode *h_inode, int mask);
  13704. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  13705. +
  13706. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  13707. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  13708. +{
  13709. +#ifdef CONFIG_AUFS_SHWH
  13710. + return au_ino(sb, bindex, h_ino, d_type, ino);
  13711. +#else
  13712. + return 0;
  13713. +#endif
  13714. +}
  13715. +
  13716. +/* i_op.c */
  13717. +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
  13718. +
  13719. +/* au_wr_dir flags */
  13720. +#define AuWrDir_ADD_ENTRY 1
  13721. +#define AuWrDir_ISDIR (1 << 1)
  13722. +#define AuWrDir_TMPFILE (1 << 2)
  13723. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  13724. +#define au_fset_wrdir(flags, name) \
  13725. + do { (flags) |= AuWrDir_##name; } while (0)
  13726. +#define au_fclr_wrdir(flags, name) \
  13727. + do { (flags) &= ~AuWrDir_##name; } while (0)
  13728. +
  13729. +struct au_wr_dir_args {
  13730. + aufs_bindex_t force_btgt;
  13731. + unsigned char flags;
  13732. +};
  13733. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  13734. + struct au_wr_dir_args *args);
  13735. +
  13736. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  13737. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  13738. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  13739. + unsigned int udba, unsigned char flags);
  13740. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  13741. + unsigned int udba, unsigned char flags) __must_check;
  13742. +int au_do_pin(struct au_pin *pin) __must_check;
  13743. +void au_unpin(struct au_pin *pin);
  13744. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  13745. +
  13746. +#define AuIcpup_DID_CPUP 1
  13747. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  13748. +#define au_fset_icpup(flags, name) \
  13749. + do { (flags) |= AuIcpup_##name; } while (0)
  13750. +#define au_fclr_icpup(flags, name) \
  13751. + do { (flags) &= ~AuIcpup_##name; } while (0)
  13752. +
  13753. +struct au_icpup_args {
  13754. + unsigned char flags;
  13755. + unsigned char pin_flags;
  13756. + aufs_bindex_t btgt;
  13757. + unsigned int udba;
  13758. + struct au_pin pin;
  13759. + struct path h_path;
  13760. + struct inode *h_inode;
  13761. +};
  13762. +
  13763. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  13764. + struct au_icpup_args *a);
  13765. +
  13766. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
  13767. +
  13768. +/* i_op_add.c */
  13769. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  13770. + struct dentry *h_parent, int isdir);
  13771. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  13772. + dev_t dev);
  13773. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  13774. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  13775. + bool want_excl);
  13776. +struct vfsub_aopen_args;
  13777. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  13778. + struct vfsub_aopen_args *args);
  13779. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  13780. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  13781. + struct dentry *dentry);
  13782. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  13783. +
  13784. +/* i_op_del.c */
  13785. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  13786. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  13787. + struct dentry *h_parent, int isdir);
  13788. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  13789. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  13790. +
  13791. +/* i_op_ren.c */
  13792. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  13793. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  13794. + struct inode *dir, struct dentry *dentry);
  13795. +
  13796. +/* iinfo.c */
  13797. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  13798. +void au_hiput(struct au_hinode *hinode);
  13799. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  13800. + struct dentry *h_wh);
  13801. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  13802. +
  13803. +/* hinode flags */
  13804. +#define AuHi_XINO 1
  13805. +#define AuHi_HNOTIFY (1 << 1)
  13806. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  13807. +#define au_fset_hi(flags, name) \
  13808. + do { (flags) |= AuHi_##name; } while (0)
  13809. +#define au_fclr_hi(flags, name) \
  13810. + do { (flags) &= ~AuHi_##name; } while (0)
  13811. +
  13812. +#ifndef CONFIG_AUFS_HNOTIFY
  13813. +#undef AuHi_HNOTIFY
  13814. +#define AuHi_HNOTIFY 0
  13815. +#endif
  13816. +
  13817. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  13818. + struct inode *h_inode, unsigned int flags);
  13819. +
  13820. +void au_update_iigen(struct inode *inode, int half);
  13821. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  13822. +
  13823. +void au_icntnr_init_once(void *_c);
  13824. +int au_iinfo_init(struct inode *inode);
  13825. +void au_iinfo_fin(struct inode *inode);
  13826. +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
  13827. +
  13828. +#ifdef CONFIG_PROC_FS
  13829. +/* plink.c */
  13830. +int au_plink_maint(struct super_block *sb, int flags);
  13831. +struct au_sbinfo;
  13832. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  13833. +int au_plink_maint_enter(struct super_block *sb);
  13834. +#ifdef CONFIG_AUFS_DEBUG
  13835. +void au_plink_list(struct super_block *sb);
  13836. +#else
  13837. +AuStubVoid(au_plink_list, struct super_block *sb)
  13838. +#endif
  13839. +int au_plink_test(struct inode *inode);
  13840. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  13841. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  13842. + struct dentry *h_dentry);
  13843. +void au_plink_put(struct super_block *sb, int verbose);
  13844. +void au_plink_clean(struct super_block *sb, int verbose);
  13845. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  13846. +#else
  13847. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  13848. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  13849. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  13850. +AuStubVoid(au_plink_list, struct super_block *sb);
  13851. +AuStubInt0(au_plink_test, struct inode *inode);
  13852. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  13853. + struct inode *inode, aufs_bindex_t bindex);
  13854. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  13855. + struct dentry *h_dentry);
  13856. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  13857. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  13858. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  13859. +#endif /* CONFIG_PROC_FS */
  13860. +
  13861. +#ifdef CONFIG_AUFS_XATTR
  13862. +/* xattr.c */
  13863. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  13864. + unsigned int verbose);
  13865. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  13866. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  13867. + size_t size);
  13868. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  13869. + size_t size, int flags);
  13870. +int aufs_removexattr(struct dentry *dentry, const char *name);
  13871. +
  13872. +/* void au_xattr_init(struct super_block *sb); */
  13873. +#else
  13874. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  13875. + int ignore_flags, unsigned int verbose);
  13876. +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
  13877. +#endif
  13878. +
  13879. +#ifdef CONFIG_FS_POSIX_ACL
  13880. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  13881. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  13882. +#endif
  13883. +
  13884. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  13885. +enum {
  13886. + AU_XATTR_SET,
  13887. + AU_XATTR_REMOVE,
  13888. + AU_ACL_SET
  13889. +};
  13890. +
  13891. +struct au_srxattr {
  13892. + int type;
  13893. + union {
  13894. + struct {
  13895. + const char *name;
  13896. + const void *value;
  13897. + size_t size;
  13898. + int flags;
  13899. + } set;
  13900. + struct {
  13901. + const char *name;
  13902. + } remove;
  13903. + struct {
  13904. + struct posix_acl *acl;
  13905. + int type;
  13906. + } acl_set;
  13907. + } u;
  13908. +};
  13909. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
  13910. +#endif
  13911. +
  13912. +/* ---------------------------------------------------------------------- */
  13913. +
  13914. +/* lock subclass for iinfo */
  13915. +enum {
  13916. + AuLsc_II_CHILD, /* child first */
  13917. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  13918. + AuLsc_II_CHILD3, /* copyup dirs */
  13919. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  13920. + AuLsc_II_PARENT2,
  13921. + AuLsc_II_PARENT3, /* copyup dirs */
  13922. + AuLsc_II_NEW_CHILD
  13923. +};
  13924. +
  13925. +/*
  13926. + * ii_read_lock_child, ii_write_lock_child,
  13927. + * ii_read_lock_child2, ii_write_lock_child2,
  13928. + * ii_read_lock_child3, ii_write_lock_child3,
  13929. + * ii_read_lock_parent, ii_write_lock_parent,
  13930. + * ii_read_lock_parent2, ii_write_lock_parent2,
  13931. + * ii_read_lock_parent3, ii_write_lock_parent3,
  13932. + * ii_read_lock_new_child, ii_write_lock_new_child,
  13933. + */
  13934. +#define AuReadLockFunc(name, lsc) \
  13935. +static inline void ii_read_lock_##name(struct inode *i) \
  13936. +{ \
  13937. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  13938. +}
  13939. +
  13940. +#define AuWriteLockFunc(name, lsc) \
  13941. +static inline void ii_write_lock_##name(struct inode *i) \
  13942. +{ \
  13943. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  13944. +}
  13945. +
  13946. +#define AuRWLockFuncs(name, lsc) \
  13947. + AuReadLockFunc(name, lsc) \
  13948. + AuWriteLockFunc(name, lsc)
  13949. +
  13950. +AuRWLockFuncs(child, CHILD);
  13951. +AuRWLockFuncs(child2, CHILD2);
  13952. +AuRWLockFuncs(child3, CHILD3);
  13953. +AuRWLockFuncs(parent, PARENT);
  13954. +AuRWLockFuncs(parent2, PARENT2);
  13955. +AuRWLockFuncs(parent3, PARENT3);
  13956. +AuRWLockFuncs(new_child, NEW_CHILD);
  13957. +
  13958. +#undef AuReadLockFunc
  13959. +#undef AuWriteLockFunc
  13960. +#undef AuRWLockFuncs
  13961. +
  13962. +/*
  13963. + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
  13964. + */
  13965. +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
  13966. +
  13967. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  13968. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  13969. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  13970. +
  13971. +/* ---------------------------------------------------------------------- */
  13972. +
  13973. +static inline void au_icntnr_init(struct au_icntnr *c)
  13974. +{
  13975. +#ifdef CONFIG_AUFS_DEBUG
  13976. + c->vfs_inode.i_mode = 0;
  13977. +#endif
  13978. +}
  13979. +
  13980. +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
  13981. +{
  13982. + unsigned int gen;
  13983. + struct au_iinfo *iinfo;
  13984. +
  13985. + iinfo = au_ii(inode);
  13986. + spin_lock(&iinfo->ii_genspin);
  13987. + if (iigen)
  13988. + *iigen = iinfo->ii_generation;
  13989. + gen = iinfo->ii_generation.ig_generation;
  13990. + spin_unlock(&iinfo->ii_genspin);
  13991. +
  13992. + return gen;
  13993. +}
  13994. +
  13995. +/* tiny test for inode number */
  13996. +/* tmpfs generation is too rough */
  13997. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  13998. +{
  13999. + struct au_iinfo *iinfo;
  14000. +
  14001. + iinfo = au_ii(inode);
  14002. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  14003. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  14004. + && iinfo->ii_higen == h_inode->i_generation);
  14005. +}
  14006. +
  14007. +static inline void au_iigen_dec(struct inode *inode)
  14008. +{
  14009. + struct au_iinfo *iinfo;
  14010. +
  14011. + iinfo = au_ii(inode);
  14012. + spin_lock(&iinfo->ii_genspin);
  14013. + iinfo->ii_generation.ig_generation--;
  14014. + spin_unlock(&iinfo->ii_genspin);
  14015. +}
  14016. +
  14017. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  14018. +{
  14019. + int err;
  14020. +
  14021. + err = 0;
  14022. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  14023. + err = -EIO;
  14024. +
  14025. + return err;
  14026. +}
  14027. +
  14028. +/* ---------------------------------------------------------------------- */
  14029. +
  14030. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  14031. + aufs_bindex_t bindex)
  14032. +{
  14033. + IiMustAnyLock(inode);
  14034. + return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
  14035. +}
  14036. +
  14037. +static inline aufs_bindex_t au_ibstart(struct inode *inode)
  14038. +{
  14039. + IiMustAnyLock(inode);
  14040. + return au_ii(inode)->ii_bstart;
  14041. +}
  14042. +
  14043. +static inline aufs_bindex_t au_ibend(struct inode *inode)
  14044. +{
  14045. + IiMustAnyLock(inode);
  14046. + return au_ii(inode)->ii_bend;
  14047. +}
  14048. +
  14049. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  14050. +{
  14051. + IiMustAnyLock(inode);
  14052. + return au_ii(inode)->ii_vdir;
  14053. +}
  14054. +
  14055. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  14056. +{
  14057. + IiMustAnyLock(inode);
  14058. + return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
  14059. +}
  14060. +
  14061. +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
  14062. +{
  14063. + IiMustWriteLock(inode);
  14064. + au_ii(inode)->ii_bstart = bindex;
  14065. +}
  14066. +
  14067. +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
  14068. +{
  14069. + IiMustWriteLock(inode);
  14070. + au_ii(inode)->ii_bend = bindex;
  14071. +}
  14072. +
  14073. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  14074. +{
  14075. + IiMustWriteLock(inode);
  14076. + au_ii(inode)->ii_vdir = vdir;
  14077. +}
  14078. +
  14079. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  14080. +{
  14081. + IiMustAnyLock(inode);
  14082. + return au_ii(inode)->ii_hinode + bindex;
  14083. +}
  14084. +
  14085. +/* ---------------------------------------------------------------------- */
  14086. +
  14087. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  14088. +{
  14089. + if (pin)
  14090. + return pin->parent;
  14091. + return NULL;
  14092. +}
  14093. +
  14094. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  14095. +{
  14096. + if (pin && pin->hdir)
  14097. + return pin->hdir->hi_inode;
  14098. + return NULL;
  14099. +}
  14100. +
  14101. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  14102. +{
  14103. + if (pin)
  14104. + return pin->hdir;
  14105. + return NULL;
  14106. +}
  14107. +
  14108. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  14109. +{
  14110. + if (pin)
  14111. + pin->dentry = dentry;
  14112. +}
  14113. +
  14114. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  14115. + unsigned char lflag)
  14116. +{
  14117. + if (pin) {
  14118. + if (lflag)
  14119. + au_fset_pin(pin->flags, DI_LOCKED);
  14120. + else
  14121. + au_fclr_pin(pin->flags, DI_LOCKED);
  14122. + }
  14123. +}
  14124. +
  14125. +#if 0 /* reserved */
  14126. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  14127. +{
  14128. + if (pin) {
  14129. + dput(pin->parent);
  14130. + pin->parent = dget(parent);
  14131. + }
  14132. +}
  14133. +#endif
  14134. +
  14135. +/* ---------------------------------------------------------------------- */
  14136. +
  14137. +struct au_branch;
  14138. +#ifdef CONFIG_AUFS_HNOTIFY
  14139. +struct au_hnotify_op {
  14140. + void (*ctl)(struct au_hinode *hinode, int do_set);
  14141. + int (*alloc)(struct au_hinode *hinode);
  14142. +
  14143. + /*
  14144. + * if it returns true, the the caller should free hinode->hi_notify,
  14145. + * otherwise ->free() frees it.
  14146. + */
  14147. + int (*free)(struct au_hinode *hinode,
  14148. + struct au_hnotify *hn) __must_check;
  14149. +
  14150. + void (*fin)(void);
  14151. + int (*init)(void);
  14152. +
  14153. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  14154. + void (*fin_br)(struct au_branch *br);
  14155. + int (*init_br)(struct au_branch *br, int perm);
  14156. +};
  14157. +
  14158. +/* hnotify.c */
  14159. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  14160. +void au_hn_free(struct au_hinode *hinode);
  14161. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  14162. +void au_hn_reset(struct inode *inode, unsigned int flags);
  14163. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  14164. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  14165. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  14166. +int au_hnotify_init_br(struct au_branch *br, int perm);
  14167. +void au_hnotify_fin_br(struct au_branch *br);
  14168. +int __init au_hnotify_init(void);
  14169. +void au_hnotify_fin(void);
  14170. +
  14171. +/* hfsnotify.c */
  14172. +extern const struct au_hnotify_op au_hnotify_op;
  14173. +
  14174. +static inline
  14175. +void au_hn_init(struct au_hinode *hinode)
  14176. +{
  14177. + hinode->hi_notify = NULL;
  14178. +}
  14179. +
  14180. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  14181. +{
  14182. + return hinode->hi_notify;
  14183. +}
  14184. +
  14185. +#else
  14186. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  14187. + struct au_hinode *hinode __maybe_unused,
  14188. + struct inode *inode __maybe_unused)
  14189. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  14190. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  14191. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  14192. + int do_set __maybe_unused)
  14193. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  14194. + unsigned int flags __maybe_unused)
  14195. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  14196. + struct au_branch *br __maybe_unused,
  14197. + int perm __maybe_unused)
  14198. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  14199. + int perm __maybe_unused)
  14200. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  14201. +AuStubInt0(__init au_hnotify_init, void)
  14202. +AuStubVoid(au_hnotify_fin, void)
  14203. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  14204. +#endif /* CONFIG_AUFS_HNOTIFY */
  14205. +
  14206. +static inline void au_hn_suspend(struct au_hinode *hdir)
  14207. +{
  14208. + au_hn_ctl(hdir, /*do_set*/0);
  14209. +}
  14210. +
  14211. +static inline void au_hn_resume(struct au_hinode *hdir)
  14212. +{
  14213. + au_hn_ctl(hdir, /*do_set*/1);
  14214. +}
  14215. +
  14216. +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
  14217. +{
  14218. + mutex_lock(&hdir->hi_inode->i_mutex);
  14219. + au_hn_suspend(hdir);
  14220. +}
  14221. +
  14222. +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
  14223. + unsigned int sc __maybe_unused)
  14224. +{
  14225. + mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
  14226. + au_hn_suspend(hdir);
  14227. +}
  14228. +
  14229. +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
  14230. +{
  14231. + au_hn_resume(hdir);
  14232. + mutex_unlock(&hdir->hi_inode->i_mutex);
  14233. +}
  14234. +
  14235. +#endif /* __KERNEL__ */
  14236. +#endif /* __AUFS_INODE_H__ */
  14237. diff -Nur linux-4.0.5.orig/fs/aufs/ioctl.c linux-4.0.5/fs/aufs/ioctl.c
  14238. --- linux-4.0.5.orig/fs/aufs/ioctl.c 1969-12-31 18:00:00.000000000 -0600
  14239. +++ linux-4.0.5/fs/aufs/ioctl.c 2015-06-21 14:07:48.686432781 -0500
  14240. @@ -0,0 +1,219 @@
  14241. +/*
  14242. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  14243. + *
  14244. + * This program, aufs is free software; you can redistribute it and/or modify
  14245. + * it under the terms of the GNU General Public License as published by
  14246. + * the Free Software Foundation; either version 2 of the License, or
  14247. + * (at your option) any later version.
  14248. + *
  14249. + * This program is distributed in the hope that it will be useful,
  14250. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14251. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14252. + * GNU General Public License for more details.
  14253. + *
  14254. + * You should have received a copy of the GNU General Public License
  14255. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14256. + */
  14257. +
  14258. +/*
  14259. + * ioctl
  14260. + * plink-management and readdir in userspace.
  14261. + * assist the pathconf(3) wrapper library.
  14262. + * move-down
  14263. + * File-based Hierarchical Storage Management.
  14264. + */
  14265. +
  14266. +#include <linux/compat.h>
  14267. +#include <linux/file.h>
  14268. +#include "aufs.h"
  14269. +
  14270. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  14271. +{
  14272. + int err, fd;
  14273. + aufs_bindex_t wbi, bindex, bend;
  14274. + struct file *h_file;
  14275. + struct super_block *sb;
  14276. + struct dentry *root;
  14277. + struct au_branch *br;
  14278. + struct aufs_wbr_fd wbrfd = {
  14279. + .oflags = au_dir_roflags,
  14280. + .brid = -1
  14281. + };
  14282. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  14283. + | O_NOATIME | O_CLOEXEC;
  14284. +
  14285. + AuDebugOn(wbrfd.oflags & ~valid);
  14286. +
  14287. + if (arg) {
  14288. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  14289. + if (unlikely(err)) {
  14290. + err = -EFAULT;
  14291. + goto out;
  14292. + }
  14293. +
  14294. + err = -EINVAL;
  14295. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  14296. + wbrfd.oflags |= au_dir_roflags;
  14297. + AuDbg("0%o\n", wbrfd.oflags);
  14298. + if (unlikely(wbrfd.oflags & ~valid))
  14299. + goto out;
  14300. + }
  14301. +
  14302. + fd = get_unused_fd_flags(0);
  14303. + err = fd;
  14304. + if (unlikely(fd < 0))
  14305. + goto out;
  14306. +
  14307. + h_file = ERR_PTR(-EINVAL);
  14308. + wbi = 0;
  14309. + br = NULL;
  14310. + sb = path->dentry->d_sb;
  14311. + root = sb->s_root;
  14312. + aufs_read_lock(root, AuLock_IR);
  14313. + bend = au_sbend(sb);
  14314. + if (wbrfd.brid >= 0) {
  14315. + wbi = au_br_index(sb, wbrfd.brid);
  14316. + if (unlikely(wbi < 0 || wbi > bend))
  14317. + goto out_unlock;
  14318. + }
  14319. +
  14320. + h_file = ERR_PTR(-ENOENT);
  14321. + br = au_sbr(sb, wbi);
  14322. + if (!au_br_writable(br->br_perm)) {
  14323. + if (arg)
  14324. + goto out_unlock;
  14325. +
  14326. + bindex = wbi + 1;
  14327. + wbi = -1;
  14328. + for (; bindex <= bend; bindex++) {
  14329. + br = au_sbr(sb, bindex);
  14330. + if (au_br_writable(br->br_perm)) {
  14331. + wbi = bindex;
  14332. + br = au_sbr(sb, wbi);
  14333. + break;
  14334. + }
  14335. + }
  14336. + }
  14337. + AuDbg("wbi %d\n", wbi);
  14338. + if (wbi >= 0)
  14339. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  14340. + /*force_wr*/0);
  14341. +
  14342. +out_unlock:
  14343. + aufs_read_unlock(root, AuLock_IR);
  14344. + err = PTR_ERR(h_file);
  14345. + if (IS_ERR(h_file))
  14346. + goto out_fd;
  14347. +
  14348. + atomic_dec(&br->br_count); /* cf. au_h_open() */
  14349. + fd_install(fd, h_file);
  14350. + err = fd;
  14351. + goto out; /* success */
  14352. +
  14353. +out_fd:
  14354. + put_unused_fd(fd);
  14355. +out:
  14356. + AuTraceErr(err);
  14357. + return err;
  14358. +}
  14359. +
  14360. +/* ---------------------------------------------------------------------- */
  14361. +
  14362. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  14363. +{
  14364. + long err;
  14365. + struct dentry *dentry;
  14366. +
  14367. + switch (cmd) {
  14368. + case AUFS_CTL_RDU:
  14369. + case AUFS_CTL_RDU_INO:
  14370. + err = au_rdu_ioctl(file, cmd, arg);
  14371. + break;
  14372. +
  14373. + case AUFS_CTL_WBR_FD:
  14374. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  14375. + break;
  14376. +
  14377. + case AUFS_CTL_IBUSY:
  14378. + err = au_ibusy_ioctl(file, arg);
  14379. + break;
  14380. +
  14381. + case AUFS_CTL_BRINFO:
  14382. + err = au_brinfo_ioctl(file, arg);
  14383. + break;
  14384. +
  14385. + case AUFS_CTL_FHSM_FD:
  14386. + dentry = file->f_path.dentry;
  14387. + if (IS_ROOT(dentry))
  14388. + err = au_fhsm_fd(dentry->d_sb, arg);
  14389. + else
  14390. + err = -ENOTTY;
  14391. + break;
  14392. +
  14393. + default:
  14394. + /* do not call the lower */
  14395. + AuDbg("0x%x\n", cmd);
  14396. + err = -ENOTTY;
  14397. + }
  14398. +
  14399. + AuTraceErr(err);
  14400. + return err;
  14401. +}
  14402. +
  14403. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  14404. +{
  14405. + long err;
  14406. +
  14407. + switch (cmd) {
  14408. + case AUFS_CTL_MVDOWN:
  14409. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  14410. + break;
  14411. +
  14412. + case AUFS_CTL_WBR_FD:
  14413. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  14414. + break;
  14415. +
  14416. + default:
  14417. + /* do not call the lower */
  14418. + AuDbg("0x%x\n", cmd);
  14419. + err = -ENOTTY;
  14420. + }
  14421. +
  14422. + AuTraceErr(err);
  14423. + return err;
  14424. +}
  14425. +
  14426. +#ifdef CONFIG_COMPAT
  14427. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  14428. + unsigned long arg)
  14429. +{
  14430. + long err;
  14431. +
  14432. + switch (cmd) {
  14433. + case AUFS_CTL_RDU:
  14434. + case AUFS_CTL_RDU_INO:
  14435. + err = au_rdu_compat_ioctl(file, cmd, arg);
  14436. + break;
  14437. +
  14438. + case AUFS_CTL_IBUSY:
  14439. + err = au_ibusy_compat_ioctl(file, arg);
  14440. + break;
  14441. +
  14442. + case AUFS_CTL_BRINFO:
  14443. + err = au_brinfo_compat_ioctl(file, arg);
  14444. + break;
  14445. +
  14446. + default:
  14447. + err = aufs_ioctl_dir(file, cmd, arg);
  14448. + }
  14449. +
  14450. + AuTraceErr(err);
  14451. + return err;
  14452. +}
  14453. +
  14454. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  14455. + unsigned long arg)
  14456. +{
  14457. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  14458. +}
  14459. +#endif
  14460. diff -Nur linux-4.0.5.orig/fs/aufs/i_op_add.c linux-4.0.5/fs/aufs/i_op_add.c
  14461. --- linux-4.0.5.orig/fs/aufs/i_op_add.c 1969-12-31 18:00:00.000000000 -0600
  14462. +++ linux-4.0.5/fs/aufs/i_op_add.c 2015-06-21 14:07:48.686432781 -0500
  14463. @@ -0,0 +1,930 @@
  14464. +/*
  14465. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  14466. + *
  14467. + * This program, aufs is free software; you can redistribute it and/or modify
  14468. + * it under the terms of the GNU General Public License as published by
  14469. + * the Free Software Foundation; either version 2 of the License, or
  14470. + * (at your option) any later version.
  14471. + *
  14472. + * This program is distributed in the hope that it will be useful,
  14473. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14474. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14475. + * GNU General Public License for more details.
  14476. + *
  14477. + * You should have received a copy of the GNU General Public License
  14478. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14479. + */
  14480. +
  14481. +/*
  14482. + * inode operations (add entry)
  14483. + */
  14484. +
  14485. +#include "aufs.h"
  14486. +
  14487. +/*
  14488. + * final procedure of adding a new entry, except link(2).
  14489. + * remove whiteout, instantiate, copyup the parent dir's times and size
  14490. + * and update version.
  14491. + * if it failed, re-create the removed whiteout.
  14492. + */
  14493. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  14494. + struct dentry *wh_dentry, struct dentry *dentry)
  14495. +{
  14496. + int err, rerr;
  14497. + aufs_bindex_t bwh;
  14498. + struct path h_path;
  14499. + struct super_block *sb;
  14500. + struct inode *inode, *h_dir;
  14501. + struct dentry *wh;
  14502. +
  14503. + bwh = -1;
  14504. + sb = dir->i_sb;
  14505. + if (wh_dentry) {
  14506. + h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
  14507. + IMustLock(h_dir);
  14508. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  14509. + bwh = au_dbwh(dentry);
  14510. + h_path.dentry = wh_dentry;
  14511. + h_path.mnt = au_sbr_mnt(sb, bindex);
  14512. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  14513. + dentry);
  14514. + if (unlikely(err))
  14515. + goto out;
  14516. + }
  14517. +
  14518. + inode = au_new_inode(dentry, /*must_new*/1);
  14519. + if (!IS_ERR(inode)) {
  14520. + d_instantiate(dentry, inode);
  14521. + dir = dentry->d_parent->d_inode; /* dir inode is locked */
  14522. + IMustLock(dir);
  14523. + au_dir_ts(dir, bindex);
  14524. + dir->i_version++;
  14525. + au_fhsm_wrote(sb, bindex, /*force*/0);
  14526. + return 0; /* success */
  14527. + }
  14528. +
  14529. + err = PTR_ERR(inode);
  14530. + if (!wh_dentry)
  14531. + goto out;
  14532. +
  14533. + /* revert */
  14534. + /* dir inode is locked */
  14535. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  14536. + rerr = PTR_ERR(wh);
  14537. + if (IS_ERR(wh)) {
  14538. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  14539. + dentry, err, rerr);
  14540. + err = -EIO;
  14541. + } else
  14542. + dput(wh);
  14543. +
  14544. +out:
  14545. + return err;
  14546. +}
  14547. +
  14548. +static int au_d_may_add(struct dentry *dentry)
  14549. +{
  14550. + int err;
  14551. +
  14552. + err = 0;
  14553. + if (unlikely(d_unhashed(dentry)))
  14554. + err = -ENOENT;
  14555. + if (unlikely(dentry->d_inode))
  14556. + err = -EEXIST;
  14557. + return err;
  14558. +}
  14559. +
  14560. +/*
  14561. + * simple tests for the adding inode operations.
  14562. + * following the checks in vfs, plus the parent-child relationship.
  14563. + */
  14564. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  14565. + struct dentry *h_parent, int isdir)
  14566. +{
  14567. + int err;
  14568. + umode_t h_mode;
  14569. + struct dentry *h_dentry;
  14570. + struct inode *h_inode;
  14571. +
  14572. + err = -ENAMETOOLONG;
  14573. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  14574. + goto out;
  14575. +
  14576. + h_dentry = au_h_dptr(dentry, bindex);
  14577. + h_inode = h_dentry->d_inode;
  14578. + if (!dentry->d_inode) {
  14579. + err = -EEXIST;
  14580. + if (unlikely(h_inode))
  14581. + goto out;
  14582. + } else {
  14583. + /* rename(2) case */
  14584. + err = -EIO;
  14585. + if (unlikely(!h_inode || !h_inode->i_nlink))
  14586. + goto out;
  14587. +
  14588. + h_mode = h_inode->i_mode;
  14589. + if (!isdir) {
  14590. + err = -EISDIR;
  14591. + if (unlikely(S_ISDIR(h_mode)))
  14592. + goto out;
  14593. + } else if (unlikely(!S_ISDIR(h_mode))) {
  14594. + err = -ENOTDIR;
  14595. + goto out;
  14596. + }
  14597. + }
  14598. +
  14599. + err = 0;
  14600. + /* expected parent dir is locked */
  14601. + if (unlikely(h_parent != h_dentry->d_parent))
  14602. + err = -EIO;
  14603. +
  14604. +out:
  14605. + AuTraceErr(err);
  14606. + return err;
  14607. +}
  14608. +
  14609. +/*
  14610. + * initial procedure of adding a new entry.
  14611. + * prepare writable branch and the parent dir, lock it,
  14612. + * and lookup whiteout for the new entry.
  14613. + */
  14614. +static struct dentry*
  14615. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  14616. + struct dentry *src_dentry, struct au_pin *pin,
  14617. + struct au_wr_dir_args *wr_dir_args)
  14618. +{
  14619. + struct dentry *wh_dentry, *h_parent;
  14620. + struct super_block *sb;
  14621. + struct au_branch *br;
  14622. + int err;
  14623. + unsigned int udba;
  14624. + aufs_bindex_t bcpup;
  14625. +
  14626. + AuDbg("%pd\n", dentry);
  14627. +
  14628. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  14629. + bcpup = err;
  14630. + wh_dentry = ERR_PTR(err);
  14631. + if (unlikely(err < 0))
  14632. + goto out;
  14633. +
  14634. + sb = dentry->d_sb;
  14635. + udba = au_opt_udba(sb);
  14636. + err = au_pin(pin, dentry, bcpup, udba,
  14637. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  14638. + wh_dentry = ERR_PTR(err);
  14639. + if (unlikely(err))
  14640. + goto out;
  14641. +
  14642. + h_parent = au_pinned_h_parent(pin);
  14643. + if (udba != AuOpt_UDBA_NONE
  14644. + && au_dbstart(dentry) == bcpup)
  14645. + err = au_may_add(dentry, bcpup, h_parent,
  14646. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  14647. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  14648. + err = -ENAMETOOLONG;
  14649. + wh_dentry = ERR_PTR(err);
  14650. + if (unlikely(err))
  14651. + goto out_unpin;
  14652. +
  14653. + br = au_sbr(sb, bcpup);
  14654. + if (dt) {
  14655. + struct path tmp = {
  14656. + .dentry = h_parent,
  14657. + .mnt = au_br_mnt(br)
  14658. + };
  14659. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  14660. + }
  14661. +
  14662. + wh_dentry = NULL;
  14663. + if (bcpup != au_dbwh(dentry))
  14664. + goto out; /* success */
  14665. +
  14666. + /*
  14667. + * ENAMETOOLONG here means that if we allowed create such name, then it
  14668. + * would not be able to removed in the future. So we don't allow such
  14669. + * name here and we don't handle ENAMETOOLONG differently here.
  14670. + */
  14671. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  14672. +
  14673. +out_unpin:
  14674. + if (IS_ERR(wh_dentry))
  14675. + au_unpin(pin);
  14676. +out:
  14677. + return wh_dentry;
  14678. +}
  14679. +
  14680. +/* ---------------------------------------------------------------------- */
  14681. +
  14682. +enum { Mknod, Symlink, Creat };
  14683. +struct simple_arg {
  14684. + int type;
  14685. + union {
  14686. + struct {
  14687. + umode_t mode;
  14688. + bool want_excl;
  14689. + bool try_aopen;
  14690. + struct vfsub_aopen_args *aopen;
  14691. + } c;
  14692. + struct {
  14693. + const char *symname;
  14694. + } s;
  14695. + struct {
  14696. + umode_t mode;
  14697. + dev_t dev;
  14698. + } m;
  14699. + } u;
  14700. +};
  14701. +
  14702. +static int add_simple(struct inode *dir, struct dentry *dentry,
  14703. + struct simple_arg *arg)
  14704. +{
  14705. + int err, rerr;
  14706. + aufs_bindex_t bstart;
  14707. + unsigned char created;
  14708. + const unsigned char try_aopen
  14709. + = (arg->type == Creat && arg->u.c.try_aopen);
  14710. + struct dentry *wh_dentry, *parent;
  14711. + struct inode *h_dir;
  14712. + struct super_block *sb;
  14713. + struct au_branch *br;
  14714. + /* to reuduce stack size */
  14715. + struct {
  14716. + struct au_dtime dt;
  14717. + struct au_pin pin;
  14718. + struct path h_path;
  14719. + struct au_wr_dir_args wr_dir_args;
  14720. + } *a;
  14721. +
  14722. + AuDbg("%pd\n", dentry);
  14723. + IMustLock(dir);
  14724. +
  14725. + err = -ENOMEM;
  14726. + a = kmalloc(sizeof(*a), GFP_NOFS);
  14727. + if (unlikely(!a))
  14728. + goto out;
  14729. + a->wr_dir_args.force_btgt = -1;
  14730. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  14731. +
  14732. + parent = dentry->d_parent; /* dir inode is locked */
  14733. + if (!try_aopen) {
  14734. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  14735. + if (unlikely(err))
  14736. + goto out_free;
  14737. + }
  14738. + err = au_d_may_add(dentry);
  14739. + if (unlikely(err))
  14740. + goto out_unlock;
  14741. + if (!try_aopen)
  14742. + di_write_lock_parent(parent);
  14743. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  14744. + &a->pin, &a->wr_dir_args);
  14745. + err = PTR_ERR(wh_dentry);
  14746. + if (IS_ERR(wh_dentry))
  14747. + goto out_parent;
  14748. +
  14749. + bstart = au_dbstart(dentry);
  14750. + sb = dentry->d_sb;
  14751. + br = au_sbr(sb, bstart);
  14752. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  14753. + a->h_path.mnt = au_br_mnt(br);
  14754. + h_dir = au_pinned_h_dir(&a->pin);
  14755. + switch (arg->type) {
  14756. + case Creat:
  14757. + err = 0;
  14758. + if (!try_aopen || !h_dir->i_op->atomic_open)
  14759. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  14760. + arg->u.c.want_excl);
  14761. + else
  14762. + err = vfsub_atomic_open(h_dir, a->h_path.dentry,
  14763. + arg->u.c.aopen, br);
  14764. + break;
  14765. + case Symlink:
  14766. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  14767. + break;
  14768. + case Mknod:
  14769. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  14770. + arg->u.m.dev);
  14771. + break;
  14772. + default:
  14773. + BUG();
  14774. + }
  14775. + created = !err;
  14776. + if (!err)
  14777. + err = epilog(dir, bstart, wh_dentry, dentry);
  14778. +
  14779. + /* revert */
  14780. + if (unlikely(created && err && a->h_path.dentry->d_inode)) {
  14781. + /* no delegation since it is just created */
  14782. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  14783. + /*force*/0);
  14784. + if (rerr) {
  14785. + AuIOErr("%pd revert failure(%d, %d)\n",
  14786. + dentry, err, rerr);
  14787. + err = -EIO;
  14788. + }
  14789. + au_dtime_revert(&a->dt);
  14790. + }
  14791. +
  14792. + if (!err && try_aopen && !h_dir->i_op->atomic_open)
  14793. + *arg->u.c.aopen->opened |= FILE_CREATED;
  14794. +
  14795. + au_unpin(&a->pin);
  14796. + dput(wh_dentry);
  14797. +
  14798. +out_parent:
  14799. + if (!try_aopen)
  14800. + di_write_unlock(parent);
  14801. +out_unlock:
  14802. + if (unlikely(err)) {
  14803. + au_update_dbstart(dentry);
  14804. + d_drop(dentry);
  14805. + }
  14806. + if (!try_aopen)
  14807. + aufs_read_unlock(dentry, AuLock_DW);
  14808. +out_free:
  14809. + kfree(a);
  14810. +out:
  14811. + return err;
  14812. +}
  14813. +
  14814. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  14815. + dev_t dev)
  14816. +{
  14817. + struct simple_arg arg = {
  14818. + .type = Mknod,
  14819. + .u.m = {
  14820. + .mode = mode,
  14821. + .dev = dev
  14822. + }
  14823. + };
  14824. + return add_simple(dir, dentry, &arg);
  14825. +}
  14826. +
  14827. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  14828. +{
  14829. + struct simple_arg arg = {
  14830. + .type = Symlink,
  14831. + .u.s.symname = symname
  14832. + };
  14833. + return add_simple(dir, dentry, &arg);
  14834. +}
  14835. +
  14836. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  14837. + bool want_excl)
  14838. +{
  14839. + struct simple_arg arg = {
  14840. + .type = Creat,
  14841. + .u.c = {
  14842. + .mode = mode,
  14843. + .want_excl = want_excl
  14844. + }
  14845. + };
  14846. + return add_simple(dir, dentry, &arg);
  14847. +}
  14848. +
  14849. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  14850. + struct vfsub_aopen_args *aopen_args)
  14851. +{
  14852. + struct simple_arg arg = {
  14853. + .type = Creat,
  14854. + .u.c = {
  14855. + .mode = aopen_args->create_mode,
  14856. + .want_excl = aopen_args->open_flag & O_EXCL,
  14857. + .try_aopen = true,
  14858. + .aopen = aopen_args
  14859. + }
  14860. + };
  14861. + return add_simple(dir, dentry, &arg);
  14862. +}
  14863. +
  14864. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  14865. +{
  14866. + int err;
  14867. + aufs_bindex_t bindex;
  14868. + struct super_block *sb;
  14869. + struct dentry *parent, *h_parent, *h_dentry;
  14870. + struct inode *h_dir, *inode;
  14871. + struct vfsmount *h_mnt;
  14872. + struct au_wr_dir_args wr_dir_args = {
  14873. + .force_btgt = -1,
  14874. + .flags = AuWrDir_TMPFILE
  14875. + };
  14876. +
  14877. + /* copy-up may happen */
  14878. + mutex_lock(&dir->i_mutex);
  14879. +
  14880. + sb = dir->i_sb;
  14881. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  14882. + if (unlikely(err))
  14883. + goto out;
  14884. +
  14885. + err = au_di_init(dentry);
  14886. + if (unlikely(err))
  14887. + goto out_si;
  14888. +
  14889. + err = -EBUSY;
  14890. + parent = d_find_any_alias(dir);
  14891. + AuDebugOn(!parent);
  14892. + di_write_lock_parent(parent);
  14893. + if (unlikely(parent->d_inode != dir))
  14894. + goto out_parent;
  14895. +
  14896. + err = au_digen_test(parent, au_sigen(sb));
  14897. + if (unlikely(err))
  14898. + goto out_parent;
  14899. +
  14900. + bindex = au_dbstart(parent);
  14901. + au_set_dbstart(dentry, bindex);
  14902. + au_set_dbend(dentry, bindex);
  14903. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  14904. + bindex = err;
  14905. + if (unlikely(err < 0))
  14906. + goto out_parent;
  14907. +
  14908. + err = -EOPNOTSUPP;
  14909. + h_dir = au_h_iptr(dir, bindex);
  14910. + if (unlikely(!h_dir->i_op->tmpfile))
  14911. + goto out_parent;
  14912. +
  14913. + h_mnt = au_sbr_mnt(sb, bindex);
  14914. + err = vfsub_mnt_want_write(h_mnt);
  14915. + if (unlikely(err))
  14916. + goto out_parent;
  14917. +
  14918. + h_parent = au_h_dptr(parent, bindex);
  14919. + err = inode_permission(h_parent->d_inode, MAY_WRITE | MAY_EXEC);
  14920. + if (unlikely(err))
  14921. + goto out_mnt;
  14922. +
  14923. + err = -ENOMEM;
  14924. + h_dentry = d_alloc(h_parent, &dentry->d_name);
  14925. + if (unlikely(!h_dentry))
  14926. + goto out_mnt;
  14927. +
  14928. + err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
  14929. + if (unlikely(err))
  14930. + goto out_dentry;
  14931. +
  14932. + au_set_dbstart(dentry, bindex);
  14933. + au_set_dbend(dentry, bindex);
  14934. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  14935. + inode = au_new_inode(dentry, /*must_new*/1);
  14936. + if (IS_ERR(inode)) {
  14937. + err = PTR_ERR(inode);
  14938. + au_set_h_dptr(dentry, bindex, NULL);
  14939. + au_set_dbstart(dentry, -1);
  14940. + au_set_dbend(dentry, -1);
  14941. + } else {
  14942. + if (!inode->i_nlink)
  14943. + set_nlink(inode, 1);
  14944. + d_tmpfile(dentry, inode);
  14945. + au_di(dentry)->di_tmpfile = 1;
  14946. +
  14947. + /* update without i_mutex */
  14948. + if (au_ibstart(dir) == au_dbstart(dentry))
  14949. + au_cpup_attr_timesizes(dir);
  14950. + }
  14951. +
  14952. +out_dentry:
  14953. + dput(h_dentry);
  14954. +out_mnt:
  14955. + vfsub_mnt_drop_write(h_mnt);
  14956. +out_parent:
  14957. + di_write_unlock(parent);
  14958. + dput(parent);
  14959. + di_write_unlock(dentry);
  14960. + if (!err)
  14961. +#if 0
  14962. + /* verbose coding for lock class name */
  14963. + au_rw_class(&au_di(dentry)->di_rwsem,
  14964. + au_lc_key + AuLcNonDir_DIINFO);
  14965. +#else
  14966. + ;
  14967. +#endif
  14968. + else {
  14969. + au_di_fin(dentry);
  14970. + dentry->d_fsdata = NULL;
  14971. + }
  14972. +out_si:
  14973. + si_read_unlock(sb);
  14974. +out:
  14975. + mutex_unlock(&dir->i_mutex);
  14976. + return err;
  14977. +}
  14978. +
  14979. +/* ---------------------------------------------------------------------- */
  14980. +
  14981. +struct au_link_args {
  14982. + aufs_bindex_t bdst, bsrc;
  14983. + struct au_pin pin;
  14984. + struct path h_path;
  14985. + struct dentry *src_parent, *parent;
  14986. +};
  14987. +
  14988. +static int au_cpup_before_link(struct dentry *src_dentry,
  14989. + struct au_link_args *a)
  14990. +{
  14991. + int err;
  14992. + struct dentry *h_src_dentry;
  14993. + struct au_cp_generic cpg = {
  14994. + .dentry = src_dentry,
  14995. + .bdst = a->bdst,
  14996. + .bsrc = a->bsrc,
  14997. + .len = -1,
  14998. + .pin = &a->pin,
  14999. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  15000. + };
  15001. +
  15002. + di_read_lock_parent(a->src_parent, AuLock_IR);
  15003. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  15004. + if (unlikely(err))
  15005. + goto out;
  15006. +
  15007. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  15008. + err = au_pin(&a->pin, src_dentry, a->bdst,
  15009. + au_opt_udba(src_dentry->d_sb),
  15010. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  15011. + if (unlikely(err))
  15012. + goto out;
  15013. +
  15014. + err = au_sio_cpup_simple(&cpg);
  15015. + au_unpin(&a->pin);
  15016. +
  15017. +out:
  15018. + di_read_unlock(a->src_parent, AuLock_IR);
  15019. + return err;
  15020. +}
  15021. +
  15022. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  15023. + struct au_link_args *a)
  15024. +{
  15025. + int err;
  15026. + unsigned char plink;
  15027. + aufs_bindex_t bend;
  15028. + struct dentry *h_src_dentry;
  15029. + struct inode *h_inode, *inode, *delegated;
  15030. + struct super_block *sb;
  15031. + struct file *h_file;
  15032. +
  15033. + plink = 0;
  15034. + h_inode = NULL;
  15035. + sb = src_dentry->d_sb;
  15036. + inode = src_dentry->d_inode;
  15037. + if (au_ibstart(inode) <= a->bdst)
  15038. + h_inode = au_h_iptr(inode, a->bdst);
  15039. + if (!h_inode || !h_inode->i_nlink) {
  15040. + /* copyup src_dentry as the name of dentry. */
  15041. + bend = au_dbend(dentry);
  15042. + if (bend < a->bsrc)
  15043. + au_set_dbend(dentry, a->bsrc);
  15044. + au_set_h_dptr(dentry, a->bsrc,
  15045. + dget(au_h_dptr(src_dentry, a->bsrc)));
  15046. + dget(a->h_path.dentry);
  15047. + au_set_h_dptr(dentry, a->bdst, NULL);
  15048. + AuDbg("temporary d_inode...\n");
  15049. + spin_lock(&dentry->d_lock);
  15050. + dentry->d_inode = src_dentry->d_inode; /* tmp */
  15051. + spin_unlock(&dentry->d_lock);
  15052. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  15053. + if (IS_ERR(h_file))
  15054. + err = PTR_ERR(h_file);
  15055. + else {
  15056. + struct au_cp_generic cpg = {
  15057. + .dentry = dentry,
  15058. + .bdst = a->bdst,
  15059. + .bsrc = -1,
  15060. + .len = -1,
  15061. + .pin = &a->pin,
  15062. + .flags = AuCpup_KEEPLINO
  15063. + };
  15064. + err = au_sio_cpup_simple(&cpg);
  15065. + au_h_open_post(dentry, a->bsrc, h_file);
  15066. + if (!err) {
  15067. + dput(a->h_path.dentry);
  15068. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  15069. + } else
  15070. + au_set_h_dptr(dentry, a->bdst,
  15071. + a->h_path.dentry);
  15072. + }
  15073. + spin_lock(&dentry->d_lock);
  15074. + dentry->d_inode = NULL; /* restore */
  15075. + spin_unlock(&dentry->d_lock);
  15076. + AuDbg("temporary d_inode...done\n");
  15077. + au_set_h_dptr(dentry, a->bsrc, NULL);
  15078. + au_set_dbend(dentry, bend);
  15079. + } else {
  15080. + /* the inode of src_dentry already exists on a.bdst branch */
  15081. + h_src_dentry = d_find_alias(h_inode);
  15082. + if (!h_src_dentry && au_plink_test(inode)) {
  15083. + plink = 1;
  15084. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  15085. + err = PTR_ERR(h_src_dentry);
  15086. + if (IS_ERR(h_src_dentry))
  15087. + goto out;
  15088. +
  15089. + if (unlikely(!h_src_dentry->d_inode)) {
  15090. + dput(h_src_dentry);
  15091. + h_src_dentry = NULL;
  15092. + }
  15093. +
  15094. + }
  15095. + if (h_src_dentry) {
  15096. + delegated = NULL;
  15097. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  15098. + &a->h_path, &delegated);
  15099. + if (unlikely(err == -EWOULDBLOCK)) {
  15100. + pr_warn("cannot retry for NFSv4 delegation"
  15101. + " for an internal link\n");
  15102. + iput(delegated);
  15103. + }
  15104. + dput(h_src_dentry);
  15105. + } else {
  15106. + AuIOErr("no dentry found for hi%lu on b%d\n",
  15107. + h_inode->i_ino, a->bdst);
  15108. + err = -EIO;
  15109. + }
  15110. + }
  15111. +
  15112. + if (!err && !plink)
  15113. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  15114. +
  15115. +out:
  15116. + AuTraceErr(err);
  15117. + return err;
  15118. +}
  15119. +
  15120. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  15121. + struct dentry *dentry)
  15122. +{
  15123. + int err, rerr;
  15124. + struct au_dtime dt;
  15125. + struct au_link_args *a;
  15126. + struct dentry *wh_dentry, *h_src_dentry;
  15127. + struct inode *inode, *delegated;
  15128. + struct super_block *sb;
  15129. + struct au_wr_dir_args wr_dir_args = {
  15130. + /* .force_btgt = -1, */
  15131. + .flags = AuWrDir_ADD_ENTRY
  15132. + };
  15133. +
  15134. + IMustLock(dir);
  15135. + inode = src_dentry->d_inode;
  15136. + IMustLock(inode);
  15137. +
  15138. + err = -ENOMEM;
  15139. + a = kzalloc(sizeof(*a), GFP_NOFS);
  15140. + if (unlikely(!a))
  15141. + goto out;
  15142. +
  15143. + a->parent = dentry->d_parent; /* dir inode is locked */
  15144. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  15145. + AuLock_NOPLM | AuLock_GEN);
  15146. + if (unlikely(err))
  15147. + goto out_kfree;
  15148. + err = au_d_linkable(src_dentry);
  15149. + if (unlikely(err))
  15150. + goto out_unlock;
  15151. + err = au_d_may_add(dentry);
  15152. + if (unlikely(err))
  15153. + goto out_unlock;
  15154. +
  15155. + a->src_parent = dget_parent(src_dentry);
  15156. + wr_dir_args.force_btgt = au_ibstart(inode);
  15157. +
  15158. + di_write_lock_parent(a->parent);
  15159. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  15160. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  15161. + &wr_dir_args);
  15162. + err = PTR_ERR(wh_dentry);
  15163. + if (IS_ERR(wh_dentry))
  15164. + goto out_parent;
  15165. +
  15166. + err = 0;
  15167. + sb = dentry->d_sb;
  15168. + a->bdst = au_dbstart(dentry);
  15169. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  15170. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  15171. + a->bsrc = au_ibstart(inode);
  15172. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  15173. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  15174. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  15175. + if (!h_src_dentry) {
  15176. + a->bsrc = au_dbstart(src_dentry);
  15177. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  15178. + AuDebugOn(!h_src_dentry);
  15179. + } else if (IS_ERR(h_src_dentry)) {
  15180. + err = PTR_ERR(h_src_dentry);
  15181. + goto out_parent;
  15182. + }
  15183. +
  15184. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  15185. + if (a->bdst < a->bsrc
  15186. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  15187. + err = au_cpup_or_link(src_dentry, dentry, a);
  15188. + else {
  15189. + delegated = NULL;
  15190. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  15191. + &a->h_path, &delegated);
  15192. + if (unlikely(err == -EWOULDBLOCK)) {
  15193. + pr_warn("cannot retry for NFSv4 delegation"
  15194. + " for an internal link\n");
  15195. + iput(delegated);
  15196. + }
  15197. + }
  15198. + dput(h_src_dentry);
  15199. + } else {
  15200. + /*
  15201. + * copyup src_dentry to the branch we process,
  15202. + * and then link(2) to it.
  15203. + */
  15204. + dput(h_src_dentry);
  15205. + if (a->bdst < a->bsrc
  15206. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  15207. + au_unpin(&a->pin);
  15208. + di_write_unlock(a->parent);
  15209. + err = au_cpup_before_link(src_dentry, a);
  15210. + di_write_lock_parent(a->parent);
  15211. + if (!err)
  15212. + err = au_pin(&a->pin, dentry, a->bdst,
  15213. + au_opt_udba(sb),
  15214. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  15215. + if (unlikely(err))
  15216. + goto out_wh;
  15217. + }
  15218. + if (!err) {
  15219. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  15220. + err = -ENOENT;
  15221. + if (h_src_dentry && h_src_dentry->d_inode) {
  15222. + delegated = NULL;
  15223. + err = vfsub_link(h_src_dentry,
  15224. + au_pinned_h_dir(&a->pin),
  15225. + &a->h_path, &delegated);
  15226. + if (unlikely(err == -EWOULDBLOCK)) {
  15227. + pr_warn("cannot retry"
  15228. + " for NFSv4 delegation"
  15229. + " for an internal link\n");
  15230. + iput(delegated);
  15231. + }
  15232. + }
  15233. + }
  15234. + }
  15235. + if (unlikely(err))
  15236. + goto out_unpin;
  15237. +
  15238. + if (wh_dentry) {
  15239. + a->h_path.dentry = wh_dentry;
  15240. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  15241. + dentry);
  15242. + if (unlikely(err))
  15243. + goto out_revert;
  15244. + }
  15245. +
  15246. + au_dir_ts(dir, a->bdst);
  15247. + dir->i_version++;
  15248. + inc_nlink(inode);
  15249. + inode->i_ctime = dir->i_ctime;
  15250. + d_instantiate(dentry, au_igrab(inode));
  15251. + if (d_unhashed(a->h_path.dentry))
  15252. + /* some filesystem calls d_drop() */
  15253. + d_drop(dentry);
  15254. + /* some filesystems consume an inode even hardlink */
  15255. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  15256. + goto out_unpin; /* success */
  15257. +
  15258. +out_revert:
  15259. + /* no delegation since it is just created */
  15260. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  15261. + /*delegated*/NULL, /*force*/0);
  15262. + if (unlikely(rerr)) {
  15263. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  15264. + err = -EIO;
  15265. + }
  15266. + au_dtime_revert(&dt);
  15267. +out_unpin:
  15268. + au_unpin(&a->pin);
  15269. +out_wh:
  15270. + dput(wh_dentry);
  15271. +out_parent:
  15272. + di_write_unlock(a->parent);
  15273. + dput(a->src_parent);
  15274. +out_unlock:
  15275. + if (unlikely(err)) {
  15276. + au_update_dbstart(dentry);
  15277. + d_drop(dentry);
  15278. + }
  15279. + aufs_read_and_write_unlock2(dentry, src_dentry);
  15280. +out_kfree:
  15281. + kfree(a);
  15282. +out:
  15283. + AuTraceErr(err);
  15284. + return err;
  15285. +}
  15286. +
  15287. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  15288. +{
  15289. + int err, rerr;
  15290. + aufs_bindex_t bindex;
  15291. + unsigned char diropq;
  15292. + struct path h_path;
  15293. + struct dentry *wh_dentry, *parent, *opq_dentry;
  15294. + struct mutex *h_mtx;
  15295. + struct super_block *sb;
  15296. + struct {
  15297. + struct au_pin pin;
  15298. + struct au_dtime dt;
  15299. + } *a; /* reduce the stack usage */
  15300. + struct au_wr_dir_args wr_dir_args = {
  15301. + .force_btgt = -1,
  15302. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  15303. + };
  15304. +
  15305. + IMustLock(dir);
  15306. +
  15307. + err = -ENOMEM;
  15308. + a = kmalloc(sizeof(*a), GFP_NOFS);
  15309. + if (unlikely(!a))
  15310. + goto out;
  15311. +
  15312. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  15313. + if (unlikely(err))
  15314. + goto out_free;
  15315. + err = au_d_may_add(dentry);
  15316. + if (unlikely(err))
  15317. + goto out_unlock;
  15318. +
  15319. + parent = dentry->d_parent; /* dir inode is locked */
  15320. + di_write_lock_parent(parent);
  15321. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  15322. + &a->pin, &wr_dir_args);
  15323. + err = PTR_ERR(wh_dentry);
  15324. + if (IS_ERR(wh_dentry))
  15325. + goto out_parent;
  15326. +
  15327. + sb = dentry->d_sb;
  15328. + bindex = au_dbstart(dentry);
  15329. + h_path.dentry = au_h_dptr(dentry, bindex);
  15330. + h_path.mnt = au_sbr_mnt(sb, bindex);
  15331. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  15332. + if (unlikely(err))
  15333. + goto out_unpin;
  15334. +
  15335. + /* make the dir opaque */
  15336. + diropq = 0;
  15337. + h_mtx = &h_path.dentry->d_inode->i_mutex;
  15338. + if (wh_dentry
  15339. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  15340. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  15341. + opq_dentry = au_diropq_create(dentry, bindex);
  15342. + mutex_unlock(h_mtx);
  15343. + err = PTR_ERR(opq_dentry);
  15344. + if (IS_ERR(opq_dentry))
  15345. + goto out_dir;
  15346. + dput(opq_dentry);
  15347. + diropq = 1;
  15348. + }
  15349. +
  15350. + err = epilog(dir, bindex, wh_dentry, dentry);
  15351. + if (!err) {
  15352. + inc_nlink(dir);
  15353. + goto out_unpin; /* success */
  15354. + }
  15355. +
  15356. + /* revert */
  15357. + if (diropq) {
  15358. + AuLabel(revert opq);
  15359. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  15360. + rerr = au_diropq_remove(dentry, bindex);
  15361. + mutex_unlock(h_mtx);
  15362. + if (rerr) {
  15363. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  15364. + dentry, err, rerr);
  15365. + err = -EIO;
  15366. + }
  15367. + }
  15368. +
  15369. +out_dir:
  15370. + AuLabel(revert dir);
  15371. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  15372. + if (rerr) {
  15373. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  15374. + dentry, err, rerr);
  15375. + err = -EIO;
  15376. + }
  15377. + au_dtime_revert(&a->dt);
  15378. +out_unpin:
  15379. + au_unpin(&a->pin);
  15380. + dput(wh_dentry);
  15381. +out_parent:
  15382. + di_write_unlock(parent);
  15383. +out_unlock:
  15384. + if (unlikely(err)) {
  15385. + au_update_dbstart(dentry);
  15386. + d_drop(dentry);
  15387. + }
  15388. + aufs_read_unlock(dentry, AuLock_DW);
  15389. +out_free:
  15390. + kfree(a);
  15391. +out:
  15392. + return err;
  15393. +}
  15394. diff -Nur linux-4.0.5.orig/fs/aufs/i_op.c linux-4.0.5/fs/aufs/i_op.c
  15395. --- linux-4.0.5.orig/fs/aufs/i_op.c 1969-12-31 18:00:00.000000000 -0600
  15396. +++ linux-4.0.5/fs/aufs/i_op.c 2015-06-21 14:07:48.686432781 -0500
  15397. @@ -0,0 +1,1445 @@
  15398. +/*
  15399. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  15400. + *
  15401. + * This program, aufs is free software; you can redistribute it and/or modify
  15402. + * it under the terms of the GNU General Public License as published by
  15403. + * the Free Software Foundation; either version 2 of the License, or
  15404. + * (at your option) any later version.
  15405. + *
  15406. + * This program is distributed in the hope that it will be useful,
  15407. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15408. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15409. + * GNU General Public License for more details.
  15410. + *
  15411. + * You should have received a copy of the GNU General Public License
  15412. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15413. + */
  15414. +
  15415. +/*
  15416. + * inode operations (except add/del/rename)
  15417. + */
  15418. +
  15419. +#include <linux/device_cgroup.h>
  15420. +#include <linux/fs_stack.h>
  15421. +#include <linux/mm.h>
  15422. +#include <linux/namei.h>
  15423. +#include <linux/security.h>
  15424. +#include "aufs.h"
  15425. +
  15426. +static int h_permission(struct inode *h_inode, int mask,
  15427. + struct vfsmount *h_mnt, int brperm)
  15428. +{
  15429. + int err;
  15430. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  15431. +
  15432. + err = -EACCES;
  15433. + if ((write_mask && IS_IMMUTABLE(h_inode))
  15434. + || ((mask & MAY_EXEC)
  15435. + && S_ISREG(h_inode->i_mode)
  15436. + && ((h_mnt->mnt_flags & MNT_NOEXEC)
  15437. + || !(h_inode->i_mode & S_IXUGO))))
  15438. + goto out;
  15439. +
  15440. + /*
  15441. + * - skip the lower fs test in the case of write to ro branch.
  15442. + * - nfs dir permission write check is optimized, but a policy for
  15443. + * link/rename requires a real check.
  15444. + * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
  15445. + * in this case, generic_permission() returns -EOPNOTSUPP.
  15446. + */
  15447. + if ((write_mask && !au_br_writable(brperm))
  15448. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  15449. + && write_mask && !(mask & MAY_READ))
  15450. + || !h_inode->i_op->permission) {
  15451. + /* AuLabel(generic_permission); */
  15452. + /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
  15453. + err = generic_permission(h_inode, mask);
  15454. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  15455. + err = h_inode->i_op->permission(h_inode, mask);
  15456. + AuTraceErr(err);
  15457. + } else {
  15458. + /* AuLabel(h_inode->permission); */
  15459. + err = h_inode->i_op->permission(h_inode, mask);
  15460. + AuTraceErr(err);
  15461. + }
  15462. +
  15463. + if (!err)
  15464. + err = devcgroup_inode_permission(h_inode, mask);
  15465. + if (!err)
  15466. + err = security_inode_permission(h_inode, mask);
  15467. +
  15468. +#if 0
  15469. + if (!err) {
  15470. + /* todo: do we need to call ima_path_check()? */
  15471. + struct path h_path = {
  15472. + .dentry =
  15473. + .mnt = h_mnt
  15474. + };
  15475. + err = ima_path_check(&h_path,
  15476. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  15477. + IMA_COUNT_LEAVE);
  15478. + }
  15479. +#endif
  15480. +
  15481. +out:
  15482. + return err;
  15483. +}
  15484. +
  15485. +static int aufs_permission(struct inode *inode, int mask)
  15486. +{
  15487. + int err;
  15488. + aufs_bindex_t bindex, bend;
  15489. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  15490. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  15491. + struct inode *h_inode;
  15492. + struct super_block *sb;
  15493. + struct au_branch *br;
  15494. +
  15495. + /* todo: support rcu-walk? */
  15496. + if (mask & MAY_NOT_BLOCK)
  15497. + return -ECHILD;
  15498. +
  15499. + sb = inode->i_sb;
  15500. + si_read_lock(sb, AuLock_FLUSH);
  15501. + ii_read_lock_child(inode);
  15502. +#if 0
  15503. + err = au_iigen_test(inode, au_sigen(sb));
  15504. + if (unlikely(err))
  15505. + goto out;
  15506. +#endif
  15507. +
  15508. + if (!isdir
  15509. + || write_mask
  15510. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  15511. + err = au_busy_or_stale();
  15512. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  15513. + if (unlikely(!h_inode
  15514. + || (h_inode->i_mode & S_IFMT)
  15515. + != (inode->i_mode & S_IFMT)))
  15516. + goto out;
  15517. +
  15518. + err = 0;
  15519. + bindex = au_ibstart(inode);
  15520. + br = au_sbr(sb, bindex);
  15521. + err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
  15522. + if (write_mask
  15523. + && !err
  15524. + && !special_file(h_inode->i_mode)) {
  15525. + /* test whether the upper writable branch exists */
  15526. + err = -EROFS;
  15527. + for (; bindex >= 0; bindex--)
  15528. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  15529. + err = 0;
  15530. + break;
  15531. + }
  15532. + }
  15533. + goto out;
  15534. + }
  15535. +
  15536. + /* non-write to dir */
  15537. + err = 0;
  15538. + bend = au_ibend(inode);
  15539. + for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
  15540. + h_inode = au_h_iptr(inode, bindex);
  15541. + if (h_inode) {
  15542. + err = au_busy_or_stale();
  15543. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  15544. + break;
  15545. +
  15546. + br = au_sbr(sb, bindex);
  15547. + err = h_permission(h_inode, mask, au_br_mnt(br),
  15548. + br->br_perm);
  15549. + }
  15550. + }
  15551. +
  15552. +out:
  15553. + ii_read_unlock(inode);
  15554. + si_read_unlock(sb);
  15555. + return err;
  15556. +}
  15557. +
  15558. +/* ---------------------------------------------------------------------- */
  15559. +
  15560. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  15561. + unsigned int flags)
  15562. +{
  15563. + struct dentry *ret, *parent;
  15564. + struct inode *inode;
  15565. + struct super_block *sb;
  15566. + int err, npositive;
  15567. +
  15568. + IMustLock(dir);
  15569. +
  15570. + /* todo: support rcu-walk? */
  15571. + ret = ERR_PTR(-ECHILD);
  15572. + if (flags & LOOKUP_RCU)
  15573. + goto out;
  15574. +
  15575. + ret = ERR_PTR(-ENAMETOOLONG);
  15576. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  15577. + goto out;
  15578. +
  15579. + sb = dir->i_sb;
  15580. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  15581. + ret = ERR_PTR(err);
  15582. + if (unlikely(err))
  15583. + goto out;
  15584. +
  15585. + err = au_di_init(dentry);
  15586. + ret = ERR_PTR(err);
  15587. + if (unlikely(err))
  15588. + goto out_si;
  15589. +
  15590. + inode = NULL;
  15591. + npositive = 0; /* suppress a warning */
  15592. + parent = dentry->d_parent; /* dir inode is locked */
  15593. + di_read_lock_parent(parent, AuLock_IR);
  15594. + err = au_alive_dir(parent);
  15595. + if (!err)
  15596. + err = au_digen_test(parent, au_sigen(sb));
  15597. + if (!err) {
  15598. + npositive = au_lkup_dentry(dentry, au_dbstart(parent),
  15599. + /*type*/0);
  15600. + err = npositive;
  15601. + }
  15602. + di_read_unlock(parent, AuLock_IR);
  15603. + ret = ERR_PTR(err);
  15604. + if (unlikely(err < 0))
  15605. + goto out_unlock;
  15606. +
  15607. + if (npositive) {
  15608. + inode = au_new_inode(dentry, /*must_new*/0);
  15609. + if (IS_ERR(inode)) {
  15610. + ret = (void *)inode;
  15611. + inode = NULL;
  15612. + goto out_unlock;
  15613. + }
  15614. + }
  15615. +
  15616. + if (inode)
  15617. + atomic_inc(&inode->i_count);
  15618. + ret = d_splice_alias(inode, dentry);
  15619. +#if 0
  15620. + if (unlikely(d_need_lookup(dentry))) {
  15621. + spin_lock(&dentry->d_lock);
  15622. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  15623. + spin_unlock(&dentry->d_lock);
  15624. + } else
  15625. +#endif
  15626. + if (inode) {
  15627. + if (!IS_ERR(ret)) {
  15628. + iput(inode);
  15629. + if (ret && ret != dentry)
  15630. + ii_write_unlock(inode);
  15631. + } else {
  15632. + ii_write_unlock(inode);
  15633. + iput(inode);
  15634. + inode = NULL;
  15635. + }
  15636. + }
  15637. +
  15638. +out_unlock:
  15639. + di_write_unlock(dentry);
  15640. + if (inode) {
  15641. + /* verbose coding for lock class name */
  15642. + if (unlikely(S_ISLNK(inode->i_mode)))
  15643. + au_rw_class(&au_di(dentry)->di_rwsem,
  15644. + au_lc_key + AuLcSymlink_DIINFO);
  15645. + else if (unlikely(S_ISDIR(inode->i_mode)))
  15646. + au_rw_class(&au_di(dentry)->di_rwsem,
  15647. + au_lc_key + AuLcDir_DIINFO);
  15648. + else /* likely */
  15649. + au_rw_class(&au_di(dentry)->di_rwsem,
  15650. + au_lc_key + AuLcNonDir_DIINFO);
  15651. + }
  15652. +out_si:
  15653. + si_read_unlock(sb);
  15654. +out:
  15655. + return ret;
  15656. +}
  15657. +
  15658. +/* ---------------------------------------------------------------------- */
  15659. +
  15660. +struct aopen_node {
  15661. + struct hlist_node hlist;
  15662. + struct file *file, *h_file;
  15663. +};
  15664. +
  15665. +static int au_do_aopen(struct inode *inode, struct file *file)
  15666. +{
  15667. + struct au_sphlhead *aopen;
  15668. + struct aopen_node *node;
  15669. + struct au_do_open_args args = {
  15670. + .no_lock = 1,
  15671. + .open = au_do_open_nondir
  15672. + };
  15673. +
  15674. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  15675. + spin_lock(&aopen->spin);
  15676. + hlist_for_each_entry(node, &aopen->head, hlist)
  15677. + if (node->file == file) {
  15678. + args.h_file = node->h_file;
  15679. + break;
  15680. + }
  15681. + spin_unlock(&aopen->spin);
  15682. + /* AuDebugOn(!args.h_file); */
  15683. +
  15684. + return au_do_open(file, &args);
  15685. +}
  15686. +
  15687. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  15688. + struct file *file, unsigned int open_flag,
  15689. + umode_t create_mode, int *opened)
  15690. +{
  15691. + int err, h_opened = *opened;
  15692. + struct dentry *parent;
  15693. + struct dentry *d;
  15694. + struct au_sphlhead *aopen;
  15695. + struct vfsub_aopen_args args = {
  15696. + .open_flag = open_flag,
  15697. + .create_mode = create_mode,
  15698. + .opened = &h_opened
  15699. + };
  15700. + struct aopen_node aopen_node = {
  15701. + .file = file
  15702. + };
  15703. +
  15704. + IMustLock(dir);
  15705. + AuDbg("open_flag 0x%x\n", open_flag);
  15706. + AuDbgDentry(dentry);
  15707. +
  15708. + err = 0;
  15709. + if (!au_di(dentry)) {
  15710. + d = aufs_lookup(dir, dentry, /*flags*/0);
  15711. + if (IS_ERR(d)) {
  15712. + err = PTR_ERR(d);
  15713. + goto out;
  15714. + } else if (d) {
  15715. + /*
  15716. + * obsoleted dentry found.
  15717. + * another error will be returned later.
  15718. + */
  15719. + d_drop(d);
  15720. + dput(d);
  15721. + AuDbgDentry(d);
  15722. + }
  15723. + AuDbgDentry(dentry);
  15724. + }
  15725. +
  15726. + if (d_is_positive(dentry)
  15727. + || d_unhashed(dentry)
  15728. + || d_unlinked(dentry)
  15729. + || !(open_flag & O_CREAT))
  15730. + goto out_no_open;
  15731. +
  15732. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  15733. + if (unlikely(err))
  15734. + goto out;
  15735. +
  15736. + parent = dentry->d_parent; /* dir is locked */
  15737. + di_write_lock_parent(parent);
  15738. + err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
  15739. + if (unlikely(err))
  15740. + goto out_unlock;
  15741. +
  15742. + AuDbgDentry(dentry);
  15743. + if (d_is_positive(dentry))
  15744. + goto out_unlock;
  15745. +
  15746. + args.file = get_empty_filp();
  15747. + err = PTR_ERR(args.file);
  15748. + if (IS_ERR(args.file))
  15749. + goto out_unlock;
  15750. +
  15751. + args.file->f_flags = file->f_flags;
  15752. + err = au_aopen_or_create(dir, dentry, &args);
  15753. + AuTraceErr(err);
  15754. + AuDbgFile(args.file);
  15755. + if (unlikely(err < 0)) {
  15756. + if (h_opened & FILE_OPENED)
  15757. + fput(args.file);
  15758. + else
  15759. + put_filp(args.file);
  15760. + goto out_unlock;
  15761. + }
  15762. +
  15763. + /* some filesystems don't set FILE_CREATED while succeeded? */
  15764. + *opened |= FILE_CREATED;
  15765. + if (h_opened & FILE_OPENED)
  15766. + aopen_node.h_file = args.file;
  15767. + else {
  15768. + put_filp(args.file);
  15769. + args.file = NULL;
  15770. + }
  15771. + aopen = &au_sbi(dir->i_sb)->si_aopen;
  15772. + au_sphl_add(&aopen_node.hlist, aopen);
  15773. + err = finish_open(file, dentry, au_do_aopen, opened);
  15774. + au_sphl_del(&aopen_node.hlist, aopen);
  15775. + AuTraceErr(err);
  15776. + AuDbgFile(file);
  15777. + if (aopen_node.h_file)
  15778. + fput(aopen_node.h_file);
  15779. +
  15780. +out_unlock:
  15781. + di_write_unlock(parent);
  15782. + aufs_read_unlock(dentry, AuLock_DW);
  15783. + AuDbgDentry(dentry);
  15784. + if (unlikely(err))
  15785. + goto out;
  15786. +out_no_open:
  15787. + if (!err && !(*opened & FILE_CREATED)) {
  15788. + AuLabel(out_no_open);
  15789. + dget(dentry);
  15790. + err = finish_no_open(file, dentry);
  15791. + }
  15792. +out:
  15793. + AuDbg("%pd%s%s\n", dentry,
  15794. + (*opened & FILE_CREATED) ? " created" : "",
  15795. + (*opened & FILE_OPENED) ? " opened" : "");
  15796. + AuTraceErr(err);
  15797. + return err;
  15798. +}
  15799. +
  15800. +
  15801. +/* ---------------------------------------------------------------------- */
  15802. +
  15803. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  15804. + const unsigned char add_entry, aufs_bindex_t bcpup,
  15805. + aufs_bindex_t bstart)
  15806. +{
  15807. + int err;
  15808. + struct dentry *h_parent;
  15809. + struct inode *h_dir;
  15810. +
  15811. + if (add_entry)
  15812. + IMustLock(parent->d_inode);
  15813. + else
  15814. + di_write_lock_parent(parent);
  15815. +
  15816. + err = 0;
  15817. + if (!au_h_dptr(parent, bcpup)) {
  15818. + if (bstart > bcpup)
  15819. + err = au_cpup_dirs(dentry, bcpup);
  15820. + else if (bstart < bcpup)
  15821. + err = au_cpdown_dirs(dentry, bcpup);
  15822. + else
  15823. + BUG();
  15824. + }
  15825. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  15826. + h_parent = au_h_dptr(parent, bcpup);
  15827. + h_dir = h_parent->d_inode;
  15828. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  15829. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  15830. + /* todo: no unlock here */
  15831. + mutex_unlock(&h_dir->i_mutex);
  15832. +
  15833. + AuDbg("bcpup %d\n", bcpup);
  15834. + if (!err) {
  15835. + if (!dentry->d_inode)
  15836. + au_set_h_dptr(dentry, bstart, NULL);
  15837. + au_update_dbrange(dentry, /*do_put_zero*/0);
  15838. + }
  15839. + }
  15840. +
  15841. + if (!add_entry)
  15842. + di_write_unlock(parent);
  15843. + if (!err)
  15844. + err = bcpup; /* success */
  15845. +
  15846. + AuTraceErr(err);
  15847. + return err;
  15848. +}
  15849. +
  15850. +/*
  15851. + * decide the branch and the parent dir where we will create a new entry.
  15852. + * returns new bindex or an error.
  15853. + * copyup the parent dir if needed.
  15854. + */
  15855. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  15856. + struct au_wr_dir_args *args)
  15857. +{
  15858. + int err;
  15859. + unsigned int flags;
  15860. + aufs_bindex_t bcpup, bstart, src_bstart;
  15861. + const unsigned char add_entry
  15862. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  15863. + | au_ftest_wrdir(args->flags, TMPFILE);
  15864. + struct super_block *sb;
  15865. + struct dentry *parent;
  15866. + struct au_sbinfo *sbinfo;
  15867. +
  15868. + sb = dentry->d_sb;
  15869. + sbinfo = au_sbi(sb);
  15870. + parent = dget_parent(dentry);
  15871. + bstart = au_dbstart(dentry);
  15872. + bcpup = bstart;
  15873. + if (args->force_btgt < 0) {
  15874. + if (src_dentry) {
  15875. + src_bstart = au_dbstart(src_dentry);
  15876. + if (src_bstart < bstart)
  15877. + bcpup = src_bstart;
  15878. + } else if (add_entry) {
  15879. + flags = 0;
  15880. + if (au_ftest_wrdir(args->flags, ISDIR))
  15881. + au_fset_wbr(flags, DIR);
  15882. + err = AuWbrCreate(sbinfo, dentry, flags);
  15883. + bcpup = err;
  15884. + }
  15885. +
  15886. + if (bcpup < 0 || au_test_ro(sb, bcpup, dentry->d_inode)) {
  15887. + if (add_entry)
  15888. + err = AuWbrCopyup(sbinfo, dentry);
  15889. + else {
  15890. + if (!IS_ROOT(dentry)) {
  15891. + di_read_lock_parent(parent, !AuLock_IR);
  15892. + err = AuWbrCopyup(sbinfo, dentry);
  15893. + di_read_unlock(parent, !AuLock_IR);
  15894. + } else
  15895. + err = AuWbrCopyup(sbinfo, dentry);
  15896. + }
  15897. + bcpup = err;
  15898. + if (unlikely(err < 0))
  15899. + goto out;
  15900. + }
  15901. + } else {
  15902. + bcpup = args->force_btgt;
  15903. + AuDebugOn(au_test_ro(sb, bcpup, dentry->d_inode));
  15904. + }
  15905. +
  15906. + AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
  15907. + err = bcpup;
  15908. + if (bcpup == bstart)
  15909. + goto out; /* success */
  15910. +
  15911. + /* copyup the new parent into the branch we process */
  15912. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
  15913. + if (err >= 0) {
  15914. + if (!dentry->d_inode) {
  15915. + au_set_h_dptr(dentry, bstart, NULL);
  15916. + au_set_dbstart(dentry, bcpup);
  15917. + au_set_dbend(dentry, bcpup);
  15918. + }
  15919. + AuDebugOn(add_entry
  15920. + && !au_ftest_wrdir(args->flags, TMPFILE)
  15921. + && !au_h_dptr(dentry, bcpup));
  15922. + }
  15923. +
  15924. +out:
  15925. + dput(parent);
  15926. + return err;
  15927. +}
  15928. +
  15929. +/* ---------------------------------------------------------------------- */
  15930. +
  15931. +void au_pin_hdir_unlock(struct au_pin *p)
  15932. +{
  15933. + if (p->hdir)
  15934. + au_hn_imtx_unlock(p->hdir);
  15935. +}
  15936. +
  15937. +int au_pin_hdir_lock(struct au_pin *p)
  15938. +{
  15939. + int err;
  15940. +
  15941. + err = 0;
  15942. + if (!p->hdir)
  15943. + goto out;
  15944. +
  15945. + /* even if an error happens later, keep this lock */
  15946. + au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
  15947. +
  15948. + err = -EBUSY;
  15949. + if (unlikely(p->hdir->hi_inode != p->h_parent->d_inode))
  15950. + goto out;
  15951. +
  15952. + err = 0;
  15953. + if (p->h_dentry)
  15954. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  15955. + p->h_parent, p->br);
  15956. +
  15957. +out:
  15958. + return err;
  15959. +}
  15960. +
  15961. +int au_pin_hdir_relock(struct au_pin *p)
  15962. +{
  15963. + int err, i;
  15964. + struct inode *h_i;
  15965. + struct dentry *h_d[] = {
  15966. + p->h_dentry,
  15967. + p->h_parent
  15968. + };
  15969. +
  15970. + err = au_pin_hdir_lock(p);
  15971. + if (unlikely(err))
  15972. + goto out;
  15973. +
  15974. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  15975. + if (!h_d[i])
  15976. + continue;
  15977. + h_i = h_d[i]->d_inode;
  15978. + if (h_i)
  15979. + err = !h_i->i_nlink;
  15980. + }
  15981. +
  15982. +out:
  15983. + return err;
  15984. +}
  15985. +
  15986. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  15987. +{
  15988. +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
  15989. + p->hdir->hi_inode->i_mutex.owner = task;
  15990. +#endif
  15991. +}
  15992. +
  15993. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  15994. +{
  15995. + if (p->hdir) {
  15996. + mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
  15997. + p->lsc_hi, 0, NULL, _RET_IP_);
  15998. + au_pin_hdir_set_owner(p, current);
  15999. + }
  16000. +}
  16001. +
  16002. +void au_pin_hdir_release(struct au_pin *p)
  16003. +{
  16004. + if (p->hdir) {
  16005. + au_pin_hdir_set_owner(p, p->task);
  16006. + mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
  16007. + }
  16008. +}
  16009. +
  16010. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  16011. +{
  16012. + if (pin && pin->parent)
  16013. + return au_h_dptr(pin->parent, pin->bindex);
  16014. + return NULL;
  16015. +}
  16016. +
  16017. +void au_unpin(struct au_pin *p)
  16018. +{
  16019. + if (p->hdir)
  16020. + au_pin_hdir_unlock(p);
  16021. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  16022. + vfsub_mnt_drop_write(p->h_mnt);
  16023. + if (!p->hdir)
  16024. + return;
  16025. +
  16026. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  16027. + di_read_unlock(p->parent, AuLock_IR);
  16028. + iput(p->hdir->hi_inode);
  16029. + dput(p->parent);
  16030. + p->parent = NULL;
  16031. + p->hdir = NULL;
  16032. + p->h_mnt = NULL;
  16033. + /* do not clear p->task */
  16034. +}
  16035. +
  16036. +int au_do_pin(struct au_pin *p)
  16037. +{
  16038. + int err;
  16039. + struct super_block *sb;
  16040. + struct inode *h_dir;
  16041. +
  16042. + err = 0;
  16043. + sb = p->dentry->d_sb;
  16044. + p->br = au_sbr(sb, p->bindex);
  16045. + if (IS_ROOT(p->dentry)) {
  16046. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  16047. + p->h_mnt = au_br_mnt(p->br);
  16048. + err = vfsub_mnt_want_write(p->h_mnt);
  16049. + if (unlikely(err)) {
  16050. + au_fclr_pin(p->flags, MNT_WRITE);
  16051. + goto out_err;
  16052. + }
  16053. + }
  16054. + goto out;
  16055. + }
  16056. +
  16057. + p->h_dentry = NULL;
  16058. + if (p->bindex <= au_dbend(p->dentry))
  16059. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  16060. +
  16061. + p->parent = dget_parent(p->dentry);
  16062. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  16063. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  16064. +
  16065. + h_dir = NULL;
  16066. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  16067. + p->hdir = au_hi(p->parent->d_inode, p->bindex);
  16068. + if (p->hdir)
  16069. + h_dir = p->hdir->hi_inode;
  16070. +
  16071. + /*
  16072. + * udba case, or
  16073. + * if DI_LOCKED is not set, then p->parent may be different
  16074. + * and h_parent can be NULL.
  16075. + */
  16076. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  16077. + err = -EBUSY;
  16078. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  16079. + di_read_unlock(p->parent, AuLock_IR);
  16080. + dput(p->parent);
  16081. + p->parent = NULL;
  16082. + goto out_err;
  16083. + }
  16084. +
  16085. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  16086. + p->h_mnt = au_br_mnt(p->br);
  16087. + err = vfsub_mnt_want_write(p->h_mnt);
  16088. + if (unlikely(err)) {
  16089. + au_fclr_pin(p->flags, MNT_WRITE);
  16090. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  16091. + di_read_unlock(p->parent, AuLock_IR);
  16092. + dput(p->parent);
  16093. + p->parent = NULL;
  16094. + goto out_err;
  16095. + }
  16096. + }
  16097. +
  16098. + au_igrab(h_dir);
  16099. + err = au_pin_hdir_lock(p);
  16100. + if (!err)
  16101. + goto out; /* success */
  16102. +
  16103. + au_unpin(p);
  16104. +
  16105. +out_err:
  16106. + pr_err("err %d\n", err);
  16107. + err = au_busy_or_stale();
  16108. +out:
  16109. + return err;
  16110. +}
  16111. +
  16112. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  16113. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  16114. + unsigned int udba, unsigned char flags)
  16115. +{
  16116. + p->dentry = dentry;
  16117. + p->udba = udba;
  16118. + p->lsc_di = lsc_di;
  16119. + p->lsc_hi = lsc_hi;
  16120. + p->flags = flags;
  16121. + p->bindex = bindex;
  16122. +
  16123. + p->parent = NULL;
  16124. + p->hdir = NULL;
  16125. + p->h_mnt = NULL;
  16126. +
  16127. + p->h_dentry = NULL;
  16128. + p->h_parent = NULL;
  16129. + p->br = NULL;
  16130. + p->task = current;
  16131. +}
  16132. +
  16133. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  16134. + unsigned int udba, unsigned char flags)
  16135. +{
  16136. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  16137. + udba, flags);
  16138. + return au_do_pin(pin);
  16139. +}
  16140. +
  16141. +/* ---------------------------------------------------------------------- */
  16142. +
  16143. +/*
  16144. + * ->setattr() and ->getattr() are called in various cases.
  16145. + * chmod, stat: dentry is revalidated.
  16146. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  16147. + * unhashed.
  16148. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  16149. + */
  16150. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  16151. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  16152. +{
  16153. + int err;
  16154. + struct inode *inode;
  16155. + struct dentry *parent;
  16156. +
  16157. + err = 0;
  16158. + inode = dentry->d_inode;
  16159. + if (au_digen_test(dentry, sigen)) {
  16160. + parent = dget_parent(dentry);
  16161. + di_read_lock_parent(parent, AuLock_IR);
  16162. + err = au_refresh_dentry(dentry, parent);
  16163. + di_read_unlock(parent, AuLock_IR);
  16164. + dput(parent);
  16165. + }
  16166. +
  16167. + AuTraceErr(err);
  16168. + return err;
  16169. +}
  16170. +
  16171. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  16172. + struct au_icpup_args *a)
  16173. +{
  16174. + int err;
  16175. + loff_t sz;
  16176. + aufs_bindex_t bstart, ibstart;
  16177. + struct dentry *hi_wh, *parent;
  16178. + struct inode *inode;
  16179. + struct au_wr_dir_args wr_dir_args = {
  16180. + .force_btgt = -1,
  16181. + .flags = 0
  16182. + };
  16183. +
  16184. + if (d_is_dir(dentry))
  16185. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  16186. + /* plink or hi_wh() case */
  16187. + bstart = au_dbstart(dentry);
  16188. + inode = dentry->d_inode;
  16189. + ibstart = au_ibstart(inode);
  16190. + if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
  16191. + wr_dir_args.force_btgt = ibstart;
  16192. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  16193. + if (unlikely(err < 0))
  16194. + goto out;
  16195. + a->btgt = err;
  16196. + if (err != bstart)
  16197. + au_fset_icpup(a->flags, DID_CPUP);
  16198. +
  16199. + err = 0;
  16200. + a->pin_flags = AuPin_MNT_WRITE;
  16201. + parent = NULL;
  16202. + if (!IS_ROOT(dentry)) {
  16203. + au_fset_pin(a->pin_flags, DI_LOCKED);
  16204. + parent = dget_parent(dentry);
  16205. + di_write_lock_parent(parent);
  16206. + }
  16207. +
  16208. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  16209. + if (unlikely(err))
  16210. + goto out_parent;
  16211. +
  16212. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  16213. + a->h_inode = a->h_path.dentry->d_inode;
  16214. + sz = -1;
  16215. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  16216. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  16217. + if (ia->ia_size < i_size_read(a->h_inode))
  16218. + sz = ia->ia_size;
  16219. + mutex_unlock(&a->h_inode->i_mutex);
  16220. + }
  16221. +
  16222. + hi_wh = NULL;
  16223. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  16224. + hi_wh = au_hi_wh(inode, a->btgt);
  16225. + if (!hi_wh) {
  16226. + struct au_cp_generic cpg = {
  16227. + .dentry = dentry,
  16228. + .bdst = a->btgt,
  16229. + .bsrc = -1,
  16230. + .len = sz,
  16231. + .pin = &a->pin
  16232. + };
  16233. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  16234. + if (unlikely(err))
  16235. + goto out_unlock;
  16236. + hi_wh = au_hi_wh(inode, a->btgt);
  16237. + /* todo: revalidate hi_wh? */
  16238. + }
  16239. + }
  16240. +
  16241. + if (parent) {
  16242. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  16243. + di_downgrade_lock(parent, AuLock_IR);
  16244. + dput(parent);
  16245. + parent = NULL;
  16246. + }
  16247. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  16248. + goto out; /* success */
  16249. +
  16250. + if (!d_unhashed(dentry)) {
  16251. + struct au_cp_generic cpg = {
  16252. + .dentry = dentry,
  16253. + .bdst = a->btgt,
  16254. + .bsrc = bstart,
  16255. + .len = sz,
  16256. + .pin = &a->pin,
  16257. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  16258. + };
  16259. + err = au_sio_cpup_simple(&cpg);
  16260. + if (!err)
  16261. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  16262. + } else if (!hi_wh)
  16263. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  16264. + else
  16265. + a->h_path.dentry = hi_wh; /* do not dget here */
  16266. +
  16267. +out_unlock:
  16268. + a->h_inode = a->h_path.dentry->d_inode;
  16269. + if (!err)
  16270. + goto out; /* success */
  16271. + au_unpin(&a->pin);
  16272. +out_parent:
  16273. + if (parent) {
  16274. + di_write_unlock(parent);
  16275. + dput(parent);
  16276. + }
  16277. +out:
  16278. + if (!err)
  16279. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  16280. + return err;
  16281. +}
  16282. +
  16283. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  16284. +{
  16285. + int err;
  16286. + struct inode *inode, *delegated;
  16287. + struct super_block *sb;
  16288. + struct file *file;
  16289. + struct au_icpup_args *a;
  16290. +
  16291. + inode = dentry->d_inode;
  16292. + IMustLock(inode);
  16293. +
  16294. + err = -ENOMEM;
  16295. + a = kzalloc(sizeof(*a), GFP_NOFS);
  16296. + if (unlikely(!a))
  16297. + goto out;
  16298. +
  16299. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  16300. + ia->ia_valid &= ~ATTR_MODE;
  16301. +
  16302. + file = NULL;
  16303. + sb = dentry->d_sb;
  16304. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16305. + if (unlikely(err))
  16306. + goto out_kfree;
  16307. +
  16308. + if (ia->ia_valid & ATTR_FILE) {
  16309. + /* currently ftruncate(2) only */
  16310. + AuDebugOn(!d_is_reg(dentry));
  16311. + file = ia->ia_file;
  16312. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  16313. + if (unlikely(err))
  16314. + goto out_si;
  16315. + ia->ia_file = au_hf_top(file);
  16316. + a->udba = AuOpt_UDBA_NONE;
  16317. + } else {
  16318. + /* fchmod() doesn't pass ia_file */
  16319. + a->udba = au_opt_udba(sb);
  16320. + di_write_lock_child(dentry);
  16321. + /* no d_unlinked(), to set UDBA_NONE for root */
  16322. + if (d_unhashed(dentry))
  16323. + a->udba = AuOpt_UDBA_NONE;
  16324. + if (a->udba != AuOpt_UDBA_NONE) {
  16325. + AuDebugOn(IS_ROOT(dentry));
  16326. + err = au_reval_for_attr(dentry, au_sigen(sb));
  16327. + if (unlikely(err))
  16328. + goto out_dentry;
  16329. + }
  16330. + }
  16331. +
  16332. + err = au_pin_and_icpup(dentry, ia, a);
  16333. + if (unlikely(err < 0))
  16334. + goto out_dentry;
  16335. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  16336. + ia->ia_file = NULL;
  16337. + ia->ia_valid &= ~ATTR_FILE;
  16338. + }
  16339. +
  16340. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  16341. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  16342. + == (ATTR_MODE | ATTR_CTIME)) {
  16343. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  16344. + if (unlikely(err))
  16345. + goto out_unlock;
  16346. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  16347. + && (ia->ia_valid & ATTR_CTIME)) {
  16348. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  16349. + if (unlikely(err))
  16350. + goto out_unlock;
  16351. + }
  16352. +
  16353. + if (ia->ia_valid & ATTR_SIZE) {
  16354. + struct file *f;
  16355. +
  16356. + if (ia->ia_size < i_size_read(inode))
  16357. + /* unmap only */
  16358. + truncate_setsize(inode, ia->ia_size);
  16359. +
  16360. + f = NULL;
  16361. + if (ia->ia_valid & ATTR_FILE)
  16362. + f = ia->ia_file;
  16363. + mutex_unlock(&a->h_inode->i_mutex);
  16364. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  16365. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  16366. + } else {
  16367. + delegated = NULL;
  16368. + while (1) {
  16369. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  16370. + if (delegated) {
  16371. + err = break_deleg_wait(&delegated);
  16372. + if (!err)
  16373. + continue;
  16374. + }
  16375. + break;
  16376. + }
  16377. + }
  16378. + if (!err)
  16379. + au_cpup_attr_changeable(inode);
  16380. +
  16381. +out_unlock:
  16382. + mutex_unlock(&a->h_inode->i_mutex);
  16383. + au_unpin(&a->pin);
  16384. + if (unlikely(err))
  16385. + au_update_dbstart(dentry);
  16386. +out_dentry:
  16387. + di_write_unlock(dentry);
  16388. + if (file) {
  16389. + fi_write_unlock(file);
  16390. + ia->ia_file = file;
  16391. + ia->ia_valid |= ATTR_FILE;
  16392. + }
  16393. +out_si:
  16394. + si_read_unlock(sb);
  16395. +out_kfree:
  16396. + kfree(a);
  16397. +out:
  16398. + AuTraceErr(err);
  16399. + return err;
  16400. +}
  16401. +
  16402. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  16403. +static int au_h_path_to_set_attr(struct dentry *dentry,
  16404. + struct au_icpup_args *a, struct path *h_path)
  16405. +{
  16406. + int err;
  16407. + struct super_block *sb;
  16408. +
  16409. + sb = dentry->d_sb;
  16410. + a->udba = au_opt_udba(sb);
  16411. + /* no d_unlinked(), to set UDBA_NONE for root */
  16412. + if (d_unhashed(dentry))
  16413. + a->udba = AuOpt_UDBA_NONE;
  16414. + if (a->udba != AuOpt_UDBA_NONE) {
  16415. + AuDebugOn(IS_ROOT(dentry));
  16416. + err = au_reval_for_attr(dentry, au_sigen(sb));
  16417. + if (unlikely(err))
  16418. + goto out;
  16419. + }
  16420. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  16421. + if (unlikely(err < 0))
  16422. + goto out;
  16423. +
  16424. + h_path->dentry = a->h_path.dentry;
  16425. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  16426. +
  16427. +out:
  16428. + return err;
  16429. +}
  16430. +
  16431. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
  16432. +{
  16433. + int err;
  16434. + struct path h_path;
  16435. + struct super_block *sb;
  16436. + struct au_icpup_args *a;
  16437. + struct inode *inode, *h_inode;
  16438. +
  16439. + inode = dentry->d_inode;
  16440. + IMustLock(inode);
  16441. +
  16442. + err = -ENOMEM;
  16443. + a = kzalloc(sizeof(*a), GFP_NOFS);
  16444. + if (unlikely(!a))
  16445. + goto out;
  16446. +
  16447. + sb = dentry->d_sb;
  16448. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16449. + if (unlikely(err))
  16450. + goto out_kfree;
  16451. +
  16452. + h_path.dentry = NULL; /* silence gcc */
  16453. + di_write_lock_child(dentry);
  16454. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  16455. + if (unlikely(err))
  16456. + goto out_di;
  16457. +
  16458. + mutex_unlock(&a->h_inode->i_mutex);
  16459. + switch (arg->type) {
  16460. + case AU_XATTR_SET:
  16461. + err = vfsub_setxattr(h_path.dentry,
  16462. + arg->u.set.name, arg->u.set.value,
  16463. + arg->u.set.size, arg->u.set.flags);
  16464. + break;
  16465. + case AU_XATTR_REMOVE:
  16466. + err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
  16467. + break;
  16468. + case AU_ACL_SET:
  16469. + err = -EOPNOTSUPP;
  16470. + h_inode = h_path.dentry->d_inode;
  16471. + if (h_inode->i_op->set_acl)
  16472. + err = h_inode->i_op->set_acl(h_inode,
  16473. + arg->u.acl_set.acl,
  16474. + arg->u.acl_set.type);
  16475. + break;
  16476. + }
  16477. + if (!err)
  16478. + au_cpup_attr_timesizes(inode);
  16479. +
  16480. + au_unpin(&a->pin);
  16481. + if (unlikely(err))
  16482. + au_update_dbstart(dentry);
  16483. +
  16484. +out_di:
  16485. + di_write_unlock(dentry);
  16486. + si_read_unlock(sb);
  16487. +out_kfree:
  16488. + kfree(a);
  16489. +out:
  16490. + AuTraceErr(err);
  16491. + return err;
  16492. +}
  16493. +#endif
  16494. +
  16495. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  16496. + unsigned int nlink)
  16497. +{
  16498. + unsigned int n;
  16499. +
  16500. + inode->i_mode = st->mode;
  16501. + /* don't i_[ug]id_write() here */
  16502. + inode->i_uid = st->uid;
  16503. + inode->i_gid = st->gid;
  16504. + inode->i_atime = st->atime;
  16505. + inode->i_mtime = st->mtime;
  16506. + inode->i_ctime = st->ctime;
  16507. +
  16508. + au_cpup_attr_nlink(inode, /*force*/0);
  16509. + if (S_ISDIR(inode->i_mode)) {
  16510. + n = inode->i_nlink;
  16511. + n -= nlink;
  16512. + n += st->nlink;
  16513. + smp_mb(); /* for i_nlink */
  16514. + /* 0 can happen */
  16515. + set_nlink(inode, n);
  16516. + }
  16517. +
  16518. + spin_lock(&inode->i_lock);
  16519. + inode->i_blocks = st->blocks;
  16520. + i_size_write(inode, st->size);
  16521. + spin_unlock(&inode->i_lock);
  16522. +}
  16523. +
  16524. +/*
  16525. + * common routine for aufs_getattr() and aufs_getxattr().
  16526. + * returns zero or negative (an error).
  16527. + * @dentry will be read-locked in success.
  16528. + */
  16529. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
  16530. +{
  16531. + int err;
  16532. + unsigned int mnt_flags, sigen;
  16533. + unsigned char udba_none;
  16534. + aufs_bindex_t bindex;
  16535. + struct super_block *sb, *h_sb;
  16536. + struct inode *inode;
  16537. +
  16538. + h_path->mnt = NULL;
  16539. + h_path->dentry = NULL;
  16540. +
  16541. + err = 0;
  16542. + sb = dentry->d_sb;
  16543. + mnt_flags = au_mntflags(sb);
  16544. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  16545. +
  16546. + /* support fstat(2) */
  16547. + if (!d_unlinked(dentry) && !udba_none) {
  16548. + sigen = au_sigen(sb);
  16549. + err = au_digen_test(dentry, sigen);
  16550. + if (!err) {
  16551. + di_read_lock_child(dentry, AuLock_IR);
  16552. + err = au_dbrange_test(dentry);
  16553. + if (unlikely(err)) {
  16554. + di_read_unlock(dentry, AuLock_IR);
  16555. + goto out;
  16556. + }
  16557. + } else {
  16558. + AuDebugOn(IS_ROOT(dentry));
  16559. + di_write_lock_child(dentry);
  16560. + err = au_dbrange_test(dentry);
  16561. + if (!err)
  16562. + err = au_reval_for_attr(dentry, sigen);
  16563. + if (!err)
  16564. + di_downgrade_lock(dentry, AuLock_IR);
  16565. + else {
  16566. + di_write_unlock(dentry);
  16567. + goto out;
  16568. + }
  16569. + }
  16570. + } else
  16571. + di_read_lock_child(dentry, AuLock_IR);
  16572. +
  16573. + inode = dentry->d_inode;
  16574. + bindex = au_ibstart(inode);
  16575. + h_path->mnt = au_sbr_mnt(sb, bindex);
  16576. + h_sb = h_path->mnt->mnt_sb;
  16577. + if (!force
  16578. + && !au_test_fs_bad_iattr(h_sb)
  16579. + && udba_none)
  16580. + goto out; /* success */
  16581. +
  16582. + if (au_dbstart(dentry) == bindex)
  16583. + h_path->dentry = au_h_dptr(dentry, bindex);
  16584. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  16585. + h_path->dentry = au_plink_lkup(inode, bindex);
  16586. + if (IS_ERR(h_path->dentry))
  16587. + /* pretending success */
  16588. + h_path->dentry = NULL;
  16589. + else
  16590. + dput(h_path->dentry);
  16591. + }
  16592. +
  16593. +out:
  16594. + return err;
  16595. +}
  16596. +
  16597. +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
  16598. + struct dentry *dentry, struct kstat *st)
  16599. +{
  16600. + int err;
  16601. + unsigned char positive;
  16602. + struct path h_path;
  16603. + struct inode *inode;
  16604. + struct super_block *sb;
  16605. +
  16606. + inode = dentry->d_inode;
  16607. + sb = dentry->d_sb;
  16608. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16609. + if (unlikely(err))
  16610. + goto out;
  16611. + err = au_h_path_getattr(dentry, /*force*/0, &h_path);
  16612. + if (unlikely(err))
  16613. + goto out_si;
  16614. + if (unlikely(!h_path.dentry))
  16615. + /* illegally overlapped or something */
  16616. + goto out_fill; /* pretending success */
  16617. +
  16618. + positive = !!h_path.dentry->d_inode;
  16619. + if (positive)
  16620. + err = vfs_getattr(&h_path, st);
  16621. + if (!err) {
  16622. + if (positive)
  16623. + au_refresh_iattr(inode, st,
  16624. + h_path.dentry->d_inode->i_nlink);
  16625. + goto out_fill; /* success */
  16626. + }
  16627. + AuTraceErr(err);
  16628. + goto out_di;
  16629. +
  16630. +out_fill:
  16631. + generic_fillattr(inode, st);
  16632. +out_di:
  16633. + di_read_unlock(dentry, AuLock_IR);
  16634. +out_si:
  16635. + si_read_unlock(sb);
  16636. +out:
  16637. + AuTraceErr(err);
  16638. + return err;
  16639. +}
  16640. +
  16641. +/* ---------------------------------------------------------------------- */
  16642. +
  16643. +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
  16644. + int bufsiz)
  16645. +{
  16646. + int err;
  16647. + struct super_block *sb;
  16648. + struct dentry *h_dentry;
  16649. +
  16650. + err = -EINVAL;
  16651. + h_dentry = au_h_dptr(dentry, bindex);
  16652. + if (unlikely(!h_dentry->d_inode->i_op->readlink))
  16653. + goto out;
  16654. +
  16655. + err = security_inode_readlink(h_dentry);
  16656. + if (unlikely(err))
  16657. + goto out;
  16658. +
  16659. + sb = dentry->d_sb;
  16660. + if (!au_test_ro(sb, bindex, dentry->d_inode)) {
  16661. + vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
  16662. + fsstack_copy_attr_atime(dentry->d_inode, h_dentry->d_inode);
  16663. + }
  16664. + err = h_dentry->d_inode->i_op->readlink(h_dentry, buf, bufsiz);
  16665. +
  16666. +out:
  16667. + return err;
  16668. +}
  16669. +
  16670. +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  16671. +{
  16672. + int err;
  16673. +
  16674. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  16675. + if (unlikely(err))
  16676. + goto out;
  16677. + err = au_d_hashed_positive(dentry);
  16678. + if (!err)
  16679. + err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
  16680. + aufs_read_unlock(dentry, AuLock_IR);
  16681. +
  16682. +out:
  16683. + return err;
  16684. +}
  16685. +
  16686. +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
  16687. +{
  16688. + int err;
  16689. + mm_segment_t old_fs;
  16690. + union {
  16691. + char *k;
  16692. + char __user *u;
  16693. + } buf;
  16694. +
  16695. + err = -ENOMEM;
  16696. + buf.k = (void *)__get_free_page(GFP_NOFS);
  16697. + if (unlikely(!buf.k))
  16698. + goto out;
  16699. +
  16700. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  16701. + if (unlikely(err))
  16702. + goto out_name;
  16703. +
  16704. + err = au_d_hashed_positive(dentry);
  16705. + if (!err) {
  16706. + old_fs = get_fs();
  16707. + set_fs(KERNEL_DS);
  16708. + err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
  16709. + set_fs(old_fs);
  16710. + }
  16711. + aufs_read_unlock(dentry, AuLock_IR);
  16712. +
  16713. + if (err >= 0) {
  16714. + buf.k[err] = 0;
  16715. + /* will be freed by put_link */
  16716. + nd_set_link(nd, buf.k);
  16717. + return NULL; /* success */
  16718. + }
  16719. +
  16720. +out_name:
  16721. + free_page((unsigned long)buf.k);
  16722. +out:
  16723. + AuTraceErr(err);
  16724. + return ERR_PTR(err);
  16725. +}
  16726. +
  16727. +static void aufs_put_link(struct dentry *dentry __maybe_unused,
  16728. + struct nameidata *nd, void *cookie __maybe_unused)
  16729. +{
  16730. + char *p;
  16731. +
  16732. + p = nd_get_link(nd);
  16733. + if (!IS_ERR_OR_NULL(p))
  16734. + free_page((unsigned long)p);
  16735. +}
  16736. +
  16737. +/* ---------------------------------------------------------------------- */
  16738. +
  16739. +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
  16740. +{
  16741. + int err;
  16742. + struct super_block *sb;
  16743. + struct inode *h_inode;
  16744. +
  16745. + sb = inode->i_sb;
  16746. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  16747. + lockdep_off();
  16748. + si_read_lock(sb, AuLock_FLUSH);
  16749. + ii_write_lock_child(inode);
  16750. + lockdep_on();
  16751. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  16752. + err = vfsub_update_time(h_inode, ts, flags);
  16753. + lockdep_off();
  16754. + if (!err)
  16755. + au_cpup_attr_timesizes(inode);
  16756. + ii_write_unlock(inode);
  16757. + si_read_unlock(sb);
  16758. + lockdep_on();
  16759. +
  16760. + if (!err && (flags & S_VERSION))
  16761. + inode_inc_iversion(inode);
  16762. +
  16763. + return err;
  16764. +}
  16765. +
  16766. +/* ---------------------------------------------------------------------- */
  16767. +
  16768. +struct inode_operations aufs_symlink_iop = {
  16769. + .permission = aufs_permission,
  16770. +#ifdef CONFIG_FS_POSIX_ACL
  16771. + .get_acl = aufs_get_acl,
  16772. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  16773. +#endif
  16774. +
  16775. + .setattr = aufs_setattr,
  16776. + .getattr = aufs_getattr,
  16777. +
  16778. +#ifdef CONFIG_AUFS_XATTR
  16779. + .setxattr = aufs_setxattr,
  16780. + .getxattr = aufs_getxattr,
  16781. + .listxattr = aufs_listxattr,
  16782. + .removexattr = aufs_removexattr,
  16783. +#endif
  16784. +
  16785. + .readlink = aufs_readlink,
  16786. + .follow_link = aufs_follow_link,
  16787. + .put_link = aufs_put_link,
  16788. +
  16789. + /* .update_time = aufs_update_time */
  16790. +};
  16791. +
  16792. +struct inode_operations aufs_dir_iop = {
  16793. + .create = aufs_create,
  16794. + .lookup = aufs_lookup,
  16795. + .link = aufs_link,
  16796. + .unlink = aufs_unlink,
  16797. + .symlink = aufs_symlink,
  16798. + .mkdir = aufs_mkdir,
  16799. + .rmdir = aufs_rmdir,
  16800. + .mknod = aufs_mknod,
  16801. + .rename = aufs_rename,
  16802. +
  16803. + .permission = aufs_permission,
  16804. +#ifdef CONFIG_FS_POSIX_ACL
  16805. + .get_acl = aufs_get_acl,
  16806. + .set_acl = aufs_set_acl,
  16807. +#endif
  16808. +
  16809. + .setattr = aufs_setattr,
  16810. + .getattr = aufs_getattr,
  16811. +
  16812. +#ifdef CONFIG_AUFS_XATTR
  16813. + .setxattr = aufs_setxattr,
  16814. + .getxattr = aufs_getxattr,
  16815. + .listxattr = aufs_listxattr,
  16816. + .removexattr = aufs_removexattr,
  16817. +#endif
  16818. +
  16819. + .update_time = aufs_update_time,
  16820. + .atomic_open = aufs_atomic_open,
  16821. + .tmpfile = aufs_tmpfile
  16822. +};
  16823. +
  16824. +struct inode_operations aufs_iop = {
  16825. + .permission = aufs_permission,
  16826. +#ifdef CONFIG_FS_POSIX_ACL
  16827. + .get_acl = aufs_get_acl,
  16828. + .set_acl = aufs_set_acl,
  16829. +#endif
  16830. +
  16831. + .setattr = aufs_setattr,
  16832. + .getattr = aufs_getattr,
  16833. +
  16834. +#ifdef CONFIG_AUFS_XATTR
  16835. + .setxattr = aufs_setxattr,
  16836. + .getxattr = aufs_getxattr,
  16837. + .listxattr = aufs_listxattr,
  16838. + .removexattr = aufs_removexattr,
  16839. +#endif
  16840. +
  16841. + .update_time = aufs_update_time
  16842. +};
  16843. diff -Nur linux-4.0.5.orig/fs/aufs/i_op_del.c linux-4.0.5/fs/aufs/i_op_del.c
  16844. --- linux-4.0.5.orig/fs/aufs/i_op_del.c 1969-12-31 18:00:00.000000000 -0600
  16845. +++ linux-4.0.5/fs/aufs/i_op_del.c 2015-06-21 14:07:48.686432781 -0500
  16846. @@ -0,0 +1,506 @@
  16847. +/*
  16848. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  16849. + *
  16850. + * This program, aufs is free software; you can redistribute it and/or modify
  16851. + * it under the terms of the GNU General Public License as published by
  16852. + * the Free Software Foundation; either version 2 of the License, or
  16853. + * (at your option) any later version.
  16854. + *
  16855. + * This program is distributed in the hope that it will be useful,
  16856. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16857. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16858. + * GNU General Public License for more details.
  16859. + *
  16860. + * You should have received a copy of the GNU General Public License
  16861. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16862. + */
  16863. +
  16864. +/*
  16865. + * inode operations (del entry)
  16866. + */
  16867. +
  16868. +#include "aufs.h"
  16869. +
  16870. +/*
  16871. + * decide if a new whiteout for @dentry is necessary or not.
  16872. + * when it is necessary, prepare the parent dir for the upper branch whose
  16873. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  16874. + * be done by caller.
  16875. + * return value:
  16876. + * 0: wh is unnecessary
  16877. + * plus: wh is necessary
  16878. + * minus: error
  16879. + */
  16880. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  16881. +{
  16882. + int need_wh, err;
  16883. + aufs_bindex_t bstart;
  16884. + struct super_block *sb;
  16885. +
  16886. + sb = dentry->d_sb;
  16887. + bstart = au_dbstart(dentry);
  16888. + if (*bcpup < 0) {
  16889. + *bcpup = bstart;
  16890. + if (au_test_ro(sb, bstart, dentry->d_inode)) {
  16891. + err = AuWbrCopyup(au_sbi(sb), dentry);
  16892. + *bcpup = err;
  16893. + if (unlikely(err < 0))
  16894. + goto out;
  16895. + }
  16896. + } else
  16897. + AuDebugOn(bstart < *bcpup
  16898. + || au_test_ro(sb, *bcpup, dentry->d_inode));
  16899. + AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
  16900. +
  16901. + if (*bcpup != bstart) {
  16902. + err = au_cpup_dirs(dentry, *bcpup);
  16903. + if (unlikely(err))
  16904. + goto out;
  16905. + need_wh = 1;
  16906. + } else {
  16907. + struct au_dinfo *dinfo, *tmp;
  16908. +
  16909. + need_wh = -ENOMEM;
  16910. + dinfo = au_di(dentry);
  16911. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  16912. + if (tmp) {
  16913. + au_di_cp(tmp, dinfo);
  16914. + au_di_swap(tmp, dinfo);
  16915. + /* returns the number of positive dentries */
  16916. + need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
  16917. + au_di_swap(tmp, dinfo);
  16918. + au_rw_write_unlock(&tmp->di_rwsem);
  16919. + au_di_free(tmp);
  16920. + }
  16921. + }
  16922. + AuDbg("need_wh %d\n", need_wh);
  16923. + err = need_wh;
  16924. +
  16925. +out:
  16926. + return err;
  16927. +}
  16928. +
  16929. +/*
  16930. + * simple tests for the del-entry operations.
  16931. + * following the checks in vfs, plus the parent-child relationship.
  16932. + */
  16933. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  16934. + struct dentry *h_parent, int isdir)
  16935. +{
  16936. + int err;
  16937. + umode_t h_mode;
  16938. + struct dentry *h_dentry, *h_latest;
  16939. + struct inode *h_inode;
  16940. +
  16941. + h_dentry = au_h_dptr(dentry, bindex);
  16942. + h_inode = h_dentry->d_inode;
  16943. + if (dentry->d_inode) {
  16944. + err = -ENOENT;
  16945. + if (unlikely(!h_inode || !h_inode->i_nlink))
  16946. + goto out;
  16947. +
  16948. + h_mode = h_inode->i_mode;
  16949. + if (!isdir) {
  16950. + err = -EISDIR;
  16951. + if (unlikely(S_ISDIR(h_mode)))
  16952. + goto out;
  16953. + } else if (unlikely(!S_ISDIR(h_mode))) {
  16954. + err = -ENOTDIR;
  16955. + goto out;
  16956. + }
  16957. + } else {
  16958. + /* rename(2) case */
  16959. + err = -EIO;
  16960. + if (unlikely(h_inode))
  16961. + goto out;
  16962. + }
  16963. +
  16964. + err = -ENOENT;
  16965. + /* expected parent dir is locked */
  16966. + if (unlikely(h_parent != h_dentry->d_parent))
  16967. + goto out;
  16968. + err = 0;
  16969. +
  16970. + /*
  16971. + * rmdir a dir may break the consistency on some filesystem.
  16972. + * let's try heavy test.
  16973. + */
  16974. + err = -EACCES;
  16975. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  16976. + && au_test_h_perm(h_parent->d_inode,
  16977. + MAY_EXEC | MAY_WRITE)))
  16978. + goto out;
  16979. +
  16980. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  16981. + err = -EIO;
  16982. + if (IS_ERR(h_latest))
  16983. + goto out;
  16984. + if (h_latest == h_dentry)
  16985. + err = 0;
  16986. + dput(h_latest);
  16987. +
  16988. +out:
  16989. + return err;
  16990. +}
  16991. +
  16992. +/*
  16993. + * decide the branch where we operate for @dentry. the branch index will be set
  16994. + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
  16995. + * dir for reverting.
  16996. + * when a new whiteout is necessary, create it.
  16997. + */
  16998. +static struct dentry*
  16999. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  17000. + struct au_dtime *dt, struct au_pin *pin)
  17001. +{
  17002. + struct dentry *wh_dentry;
  17003. + struct super_block *sb;
  17004. + struct path h_path;
  17005. + int err, need_wh;
  17006. + unsigned int udba;
  17007. + aufs_bindex_t bcpup;
  17008. +
  17009. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  17010. + wh_dentry = ERR_PTR(need_wh);
  17011. + if (unlikely(need_wh < 0))
  17012. + goto out;
  17013. +
  17014. + sb = dentry->d_sb;
  17015. + udba = au_opt_udba(sb);
  17016. + bcpup = *rbcpup;
  17017. + err = au_pin(pin, dentry, bcpup, udba,
  17018. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  17019. + wh_dentry = ERR_PTR(err);
  17020. + if (unlikely(err))
  17021. + goto out;
  17022. +
  17023. + h_path.dentry = au_pinned_h_parent(pin);
  17024. + if (udba != AuOpt_UDBA_NONE
  17025. + && au_dbstart(dentry) == bcpup) {
  17026. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  17027. + wh_dentry = ERR_PTR(err);
  17028. + if (unlikely(err))
  17029. + goto out_unpin;
  17030. + }
  17031. +
  17032. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  17033. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  17034. + wh_dentry = NULL;
  17035. + if (!need_wh)
  17036. + goto out; /* success, no need to create whiteout */
  17037. +
  17038. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  17039. + if (IS_ERR(wh_dentry))
  17040. + goto out_unpin;
  17041. +
  17042. + /* returns with the parent is locked and wh_dentry is dget-ed */
  17043. + goto out; /* success */
  17044. +
  17045. +out_unpin:
  17046. + au_unpin(pin);
  17047. +out:
  17048. + return wh_dentry;
  17049. +}
  17050. +
  17051. +/*
  17052. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  17053. + * in order to be revertible and save time for removing many child whiteouts
  17054. + * under the dir.
  17055. + * returns 1 when there are too many child whiteout and caller should remove
  17056. + * them asynchronously. returns 0 when the number of children is enough small to
  17057. + * remove now or the branch fs is a remote fs.
  17058. + * otherwise return an error.
  17059. + */
  17060. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  17061. + struct au_nhash *whlist, struct inode *dir)
  17062. +{
  17063. + int rmdir_later, err, dirwh;
  17064. + struct dentry *h_dentry;
  17065. + struct super_block *sb;
  17066. +
  17067. + sb = dentry->d_sb;
  17068. + SiMustAnyLock(sb);
  17069. + h_dentry = au_h_dptr(dentry, bindex);
  17070. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  17071. + if (unlikely(err))
  17072. + goto out;
  17073. +
  17074. + /* stop monitoring */
  17075. + au_hn_free(au_hi(dentry->d_inode, bindex));
  17076. +
  17077. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  17078. + dirwh = au_sbi(sb)->si_dirwh;
  17079. + rmdir_later = (dirwh <= 1);
  17080. + if (!rmdir_later)
  17081. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  17082. + dirwh);
  17083. + if (rmdir_later)
  17084. + return rmdir_later;
  17085. + }
  17086. +
  17087. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  17088. + if (unlikely(err)) {
  17089. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  17090. + h_dentry, bindex, err);
  17091. + err = 0;
  17092. + }
  17093. +
  17094. +out:
  17095. + AuTraceErr(err);
  17096. + return err;
  17097. +}
  17098. +
  17099. +/*
  17100. + * final procedure for deleting a entry.
  17101. + * maintain dentry and iattr.
  17102. + */
  17103. +static void epilog(struct inode *dir, struct dentry *dentry,
  17104. + aufs_bindex_t bindex)
  17105. +{
  17106. + struct inode *inode;
  17107. +
  17108. + inode = dentry->d_inode;
  17109. + d_drop(dentry);
  17110. + inode->i_ctime = dir->i_ctime;
  17111. +
  17112. + au_dir_ts(dir, bindex);
  17113. + dir->i_version++;
  17114. +}
  17115. +
  17116. +/*
  17117. + * when an error happened, remove the created whiteout and revert everything.
  17118. + */
  17119. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  17120. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  17121. + struct dentry *dentry, struct au_dtime *dt)
  17122. +{
  17123. + int rerr;
  17124. + struct path h_path = {
  17125. + .dentry = wh_dentry,
  17126. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  17127. + };
  17128. +
  17129. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  17130. + if (!rerr) {
  17131. + au_set_dbwh(dentry, bwh);
  17132. + au_dtime_revert(dt);
  17133. + return 0;
  17134. + }
  17135. +
  17136. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  17137. + return -EIO;
  17138. +}
  17139. +
  17140. +/* ---------------------------------------------------------------------- */
  17141. +
  17142. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  17143. +{
  17144. + int err;
  17145. + aufs_bindex_t bwh, bindex, bstart;
  17146. + struct inode *inode, *h_dir, *delegated;
  17147. + struct dentry *parent, *wh_dentry;
  17148. + /* to reuduce stack size */
  17149. + struct {
  17150. + struct au_dtime dt;
  17151. + struct au_pin pin;
  17152. + struct path h_path;
  17153. + } *a;
  17154. +
  17155. + IMustLock(dir);
  17156. +
  17157. + err = -ENOMEM;
  17158. + a = kmalloc(sizeof(*a), GFP_NOFS);
  17159. + if (unlikely(!a))
  17160. + goto out;
  17161. +
  17162. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  17163. + if (unlikely(err))
  17164. + goto out_free;
  17165. + err = au_d_hashed_positive(dentry);
  17166. + if (unlikely(err))
  17167. + goto out_unlock;
  17168. + inode = dentry->d_inode;
  17169. + IMustLock(inode);
  17170. + err = -EISDIR;
  17171. + if (unlikely(d_is_dir(dentry)))
  17172. + goto out_unlock; /* possible? */
  17173. +
  17174. + bstart = au_dbstart(dentry);
  17175. + bwh = au_dbwh(dentry);
  17176. + bindex = -1;
  17177. + parent = dentry->d_parent; /* dir inode is locked */
  17178. + di_write_lock_parent(parent);
  17179. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  17180. + &a->pin);
  17181. + err = PTR_ERR(wh_dentry);
  17182. + if (IS_ERR(wh_dentry))
  17183. + goto out_parent;
  17184. +
  17185. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
  17186. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  17187. + dget(a->h_path.dentry);
  17188. + if (bindex == bstart) {
  17189. + h_dir = au_pinned_h_dir(&a->pin);
  17190. + delegated = NULL;
  17191. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  17192. + if (unlikely(err == -EWOULDBLOCK)) {
  17193. + pr_warn("cannot retry for NFSv4 delegation"
  17194. + " for an internal unlink\n");
  17195. + iput(delegated);
  17196. + }
  17197. + } else {
  17198. + /* dir inode is locked */
  17199. + h_dir = wh_dentry->d_parent->d_inode;
  17200. + IMustLock(h_dir);
  17201. + err = 0;
  17202. + }
  17203. +
  17204. + if (!err) {
  17205. + vfsub_drop_nlink(inode);
  17206. + epilog(dir, dentry, bindex);
  17207. +
  17208. + /* update target timestamps */
  17209. + if (bindex == bstart) {
  17210. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  17211. + /*ignore*/
  17212. + inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
  17213. + } else
  17214. + /* todo: this timestamp may be reverted later */
  17215. + inode->i_ctime = h_dir->i_ctime;
  17216. + goto out_unpin; /* success */
  17217. + }
  17218. +
  17219. + /* revert */
  17220. + if (wh_dentry) {
  17221. + int rerr;
  17222. +
  17223. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  17224. + &a->dt);
  17225. + if (rerr)
  17226. + err = rerr;
  17227. + }
  17228. +
  17229. +out_unpin:
  17230. + au_unpin(&a->pin);
  17231. + dput(wh_dentry);
  17232. + dput(a->h_path.dentry);
  17233. +out_parent:
  17234. + di_write_unlock(parent);
  17235. +out_unlock:
  17236. + aufs_read_unlock(dentry, AuLock_DW);
  17237. +out_free:
  17238. + kfree(a);
  17239. +out:
  17240. + return err;
  17241. +}
  17242. +
  17243. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  17244. +{
  17245. + int err, rmdir_later;
  17246. + aufs_bindex_t bwh, bindex, bstart;
  17247. + struct inode *inode;
  17248. + struct dentry *parent, *wh_dentry, *h_dentry;
  17249. + struct au_whtmp_rmdir *args;
  17250. + /* to reuduce stack size */
  17251. + struct {
  17252. + struct au_dtime dt;
  17253. + struct au_pin pin;
  17254. + } *a;
  17255. +
  17256. + IMustLock(dir);
  17257. +
  17258. + err = -ENOMEM;
  17259. + a = kmalloc(sizeof(*a), GFP_NOFS);
  17260. + if (unlikely(!a))
  17261. + goto out;
  17262. +
  17263. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  17264. + if (unlikely(err))
  17265. + goto out_free;
  17266. + err = au_alive_dir(dentry);
  17267. + if (unlikely(err))
  17268. + goto out_unlock;
  17269. + inode = dentry->d_inode;
  17270. + IMustLock(inode);
  17271. + err = -ENOTDIR;
  17272. + if (unlikely(!d_is_dir(dentry)))
  17273. + goto out_unlock; /* possible? */
  17274. +
  17275. + err = -ENOMEM;
  17276. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  17277. + if (unlikely(!args))
  17278. + goto out_unlock;
  17279. +
  17280. + parent = dentry->d_parent; /* dir inode is locked */
  17281. + di_write_lock_parent(parent);
  17282. + err = au_test_empty(dentry, &args->whlist);
  17283. + if (unlikely(err))
  17284. + goto out_parent;
  17285. +
  17286. + bstart = au_dbstart(dentry);
  17287. + bwh = au_dbwh(dentry);
  17288. + bindex = -1;
  17289. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  17290. + &a->pin);
  17291. + err = PTR_ERR(wh_dentry);
  17292. + if (IS_ERR(wh_dentry))
  17293. + goto out_parent;
  17294. +
  17295. + h_dentry = au_h_dptr(dentry, bstart);
  17296. + dget(h_dentry);
  17297. + rmdir_later = 0;
  17298. + if (bindex == bstart) {
  17299. + err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
  17300. + if (err > 0) {
  17301. + rmdir_later = err;
  17302. + err = 0;
  17303. + }
  17304. + } else {
  17305. + /* stop monitoring */
  17306. + au_hn_free(au_hi(inode, bstart));
  17307. +
  17308. + /* dir inode is locked */
  17309. + IMustLock(wh_dentry->d_parent->d_inode);
  17310. + err = 0;
  17311. + }
  17312. +
  17313. + if (!err) {
  17314. + vfsub_dead_dir(inode);
  17315. + au_set_dbdiropq(dentry, -1);
  17316. + epilog(dir, dentry, bindex);
  17317. +
  17318. + if (rmdir_later) {
  17319. + au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
  17320. + args = NULL;
  17321. + }
  17322. +
  17323. + goto out_unpin; /* success */
  17324. + }
  17325. +
  17326. + /* revert */
  17327. + AuLabel(revert);
  17328. + if (wh_dentry) {
  17329. + int rerr;
  17330. +
  17331. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  17332. + &a->dt);
  17333. + if (rerr)
  17334. + err = rerr;
  17335. + }
  17336. +
  17337. +out_unpin:
  17338. + au_unpin(&a->pin);
  17339. + dput(wh_dentry);
  17340. + dput(h_dentry);
  17341. +out_parent:
  17342. + di_write_unlock(parent);
  17343. + if (args)
  17344. + au_whtmp_rmdir_free(args);
  17345. +out_unlock:
  17346. + aufs_read_unlock(dentry, AuLock_DW);
  17347. +out_free:
  17348. + kfree(a);
  17349. +out:
  17350. + AuTraceErr(err);
  17351. + return err;
  17352. +}
  17353. diff -Nur linux-4.0.5.orig/fs/aufs/i_op_ren.c linux-4.0.5/fs/aufs/i_op_ren.c
  17354. --- linux-4.0.5.orig/fs/aufs/i_op_ren.c 1969-12-31 18:00:00.000000000 -0600
  17355. +++ linux-4.0.5/fs/aufs/i_op_ren.c 2015-06-21 14:07:48.686432781 -0500
  17356. @@ -0,0 +1,1015 @@
  17357. +/*
  17358. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  17359. + *
  17360. + * This program, aufs is free software; you can redistribute it and/or modify
  17361. + * it under the terms of the GNU General Public License as published by
  17362. + * the Free Software Foundation; either version 2 of the License, or
  17363. + * (at your option) any later version.
  17364. + *
  17365. + * This program is distributed in the hope that it will be useful,
  17366. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17367. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17368. + * GNU General Public License for more details.
  17369. + *
  17370. + * You should have received a copy of the GNU General Public License
  17371. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17372. + */
  17373. +
  17374. +/*
  17375. + * inode operation (rename entry)
  17376. + * todo: this is crazy monster
  17377. + */
  17378. +
  17379. +#include "aufs.h"
  17380. +
  17381. +enum { AuSRC, AuDST, AuSrcDst };
  17382. +enum { AuPARENT, AuCHILD, AuParentChild };
  17383. +
  17384. +#define AuRen_ISDIR 1
  17385. +#define AuRen_ISSAMEDIR (1 << 1)
  17386. +#define AuRen_WHSRC (1 << 2)
  17387. +#define AuRen_WHDST (1 << 3)
  17388. +#define AuRen_MNT_WRITE (1 << 4)
  17389. +#define AuRen_DT_DSTDIR (1 << 5)
  17390. +#define AuRen_DIROPQ (1 << 6)
  17391. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  17392. +#define au_fset_ren(flags, name) \
  17393. + do { (flags) |= AuRen_##name; } while (0)
  17394. +#define au_fclr_ren(flags, name) \
  17395. + do { (flags) &= ~AuRen_##name; } while (0)
  17396. +
  17397. +struct au_ren_args {
  17398. + struct {
  17399. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  17400. + *wh_dentry;
  17401. + struct inode *dir, *inode;
  17402. + struct au_hinode *hdir;
  17403. + struct au_dtime dt[AuParentChild];
  17404. + aufs_bindex_t bstart;
  17405. + } sd[AuSrcDst];
  17406. +
  17407. +#define src_dentry sd[AuSRC].dentry
  17408. +#define src_dir sd[AuSRC].dir
  17409. +#define src_inode sd[AuSRC].inode
  17410. +#define src_h_dentry sd[AuSRC].h_dentry
  17411. +#define src_parent sd[AuSRC].parent
  17412. +#define src_h_parent sd[AuSRC].h_parent
  17413. +#define src_wh_dentry sd[AuSRC].wh_dentry
  17414. +#define src_hdir sd[AuSRC].hdir
  17415. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  17416. +#define src_dt sd[AuSRC].dt
  17417. +#define src_bstart sd[AuSRC].bstart
  17418. +
  17419. +#define dst_dentry sd[AuDST].dentry
  17420. +#define dst_dir sd[AuDST].dir
  17421. +#define dst_inode sd[AuDST].inode
  17422. +#define dst_h_dentry sd[AuDST].h_dentry
  17423. +#define dst_parent sd[AuDST].parent
  17424. +#define dst_h_parent sd[AuDST].h_parent
  17425. +#define dst_wh_dentry sd[AuDST].wh_dentry
  17426. +#define dst_hdir sd[AuDST].hdir
  17427. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  17428. +#define dst_dt sd[AuDST].dt
  17429. +#define dst_bstart sd[AuDST].bstart
  17430. +
  17431. + struct dentry *h_trap;
  17432. + struct au_branch *br;
  17433. + struct au_hinode *src_hinode;
  17434. + struct path h_path;
  17435. + struct au_nhash whlist;
  17436. + aufs_bindex_t btgt, src_bwh, src_bdiropq;
  17437. +
  17438. + unsigned int flags;
  17439. +
  17440. + struct au_whtmp_rmdir *thargs;
  17441. + struct dentry *h_dst;
  17442. +};
  17443. +
  17444. +/* ---------------------------------------------------------------------- */
  17445. +
  17446. +/*
  17447. + * functions for reverting.
  17448. + * when an error happened in a single rename systemcall, we should revert
  17449. + * everything as if nothing happend.
  17450. + * we don't need to revert the copied-up/down the parent dir since they are
  17451. + * harmless.
  17452. + */
  17453. +
  17454. +#define RevertFailure(fmt, ...) do { \
  17455. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  17456. + ##__VA_ARGS__, err, rerr); \
  17457. + err = -EIO; \
  17458. +} while (0)
  17459. +
  17460. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  17461. +{
  17462. + int rerr;
  17463. +
  17464. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  17465. + rerr = au_diropq_remove(a->src_dentry, a->btgt);
  17466. + au_hn_imtx_unlock(a->src_hinode);
  17467. + au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
  17468. + if (rerr)
  17469. + RevertFailure("remove diropq %pd", a->src_dentry);
  17470. +}
  17471. +
  17472. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  17473. +{
  17474. + int rerr;
  17475. + struct inode *delegated;
  17476. +
  17477. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  17478. + a->src_h_parent);
  17479. + rerr = PTR_ERR(a->h_path.dentry);
  17480. + if (IS_ERR(a->h_path.dentry)) {
  17481. + RevertFailure("lkup one %pd", a->src_dentry);
  17482. + return;
  17483. + }
  17484. +
  17485. + delegated = NULL;
  17486. + rerr = vfsub_rename(a->dst_h_dir,
  17487. + au_h_dptr(a->src_dentry, a->btgt),
  17488. + a->src_h_dir, &a->h_path, &delegated);
  17489. + if (unlikely(rerr == -EWOULDBLOCK)) {
  17490. + pr_warn("cannot retry for NFSv4 delegation"
  17491. + " for an internal rename\n");
  17492. + iput(delegated);
  17493. + }
  17494. + d_drop(a->h_path.dentry);
  17495. + dput(a->h_path.dentry);
  17496. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  17497. + if (rerr)
  17498. + RevertFailure("rename %pd", a->src_dentry);
  17499. +}
  17500. +
  17501. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  17502. +{
  17503. + int rerr;
  17504. + struct inode *delegated;
  17505. +
  17506. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  17507. + a->dst_h_parent);
  17508. + rerr = PTR_ERR(a->h_path.dentry);
  17509. + if (IS_ERR(a->h_path.dentry)) {
  17510. + RevertFailure("lkup one %pd", a->dst_dentry);
  17511. + return;
  17512. + }
  17513. + if (a->h_path.dentry->d_inode) {
  17514. + d_drop(a->h_path.dentry);
  17515. + dput(a->h_path.dentry);
  17516. + return;
  17517. + }
  17518. +
  17519. + delegated = NULL;
  17520. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  17521. + &delegated);
  17522. + if (unlikely(rerr == -EWOULDBLOCK)) {
  17523. + pr_warn("cannot retry for NFSv4 delegation"
  17524. + " for an internal rename\n");
  17525. + iput(delegated);
  17526. + }
  17527. + d_drop(a->h_path.dentry);
  17528. + dput(a->h_path.dentry);
  17529. + if (!rerr)
  17530. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  17531. + else
  17532. + RevertFailure("rename %pd", a->h_dst);
  17533. +}
  17534. +
  17535. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  17536. +{
  17537. + int rerr;
  17538. +
  17539. + a->h_path.dentry = a->src_wh_dentry;
  17540. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  17541. + au_set_dbwh(a->src_dentry, a->src_bwh);
  17542. + if (rerr)
  17543. + RevertFailure("unlink %pd", a->src_wh_dentry);
  17544. +}
  17545. +#undef RevertFailure
  17546. +
  17547. +/* ---------------------------------------------------------------------- */
  17548. +
  17549. +/*
  17550. + * when we have to copyup the renaming entry, do it with the rename-target name
  17551. + * in order to minimize the cost (the later actual rename is unnecessary).
  17552. + * otherwise rename it on the target branch.
  17553. + */
  17554. +static int au_ren_or_cpup(struct au_ren_args *a)
  17555. +{
  17556. + int err;
  17557. + struct dentry *d;
  17558. + struct inode *delegated;
  17559. +
  17560. + d = a->src_dentry;
  17561. + if (au_dbstart(d) == a->btgt) {
  17562. + a->h_path.dentry = a->dst_h_dentry;
  17563. + if (au_ftest_ren(a->flags, DIROPQ)
  17564. + && au_dbdiropq(d) == a->btgt)
  17565. + au_fclr_ren(a->flags, DIROPQ);
  17566. + AuDebugOn(au_dbstart(d) != a->btgt);
  17567. + delegated = NULL;
  17568. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  17569. + a->dst_h_dir, &a->h_path, &delegated);
  17570. + if (unlikely(err == -EWOULDBLOCK)) {
  17571. + pr_warn("cannot retry for NFSv4 delegation"
  17572. + " for an internal rename\n");
  17573. + iput(delegated);
  17574. + }
  17575. + } else
  17576. + BUG();
  17577. +
  17578. + if (!err && a->h_dst)
  17579. + /* it will be set to dinfo later */
  17580. + dget(a->h_dst);
  17581. +
  17582. + return err;
  17583. +}
  17584. +
  17585. +/* cf. aufs_rmdir() */
  17586. +static int au_ren_del_whtmp(struct au_ren_args *a)
  17587. +{
  17588. + int err;
  17589. + struct inode *dir;
  17590. +
  17591. + dir = a->dst_dir;
  17592. + SiMustAnyLock(dir->i_sb);
  17593. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  17594. + au_sbi(dir->i_sb)->si_dirwh)
  17595. + || au_test_fs_remote(a->h_dst->d_sb)) {
  17596. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  17597. + if (unlikely(err))
  17598. + pr_warn("failed removing whtmp dir %pd (%d), "
  17599. + "ignored.\n", a->h_dst, err);
  17600. + } else {
  17601. + au_nhash_wh_free(&a->thargs->whlist);
  17602. + a->thargs->whlist = a->whlist;
  17603. + a->whlist.nh_num = 0;
  17604. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  17605. + dput(a->h_dst);
  17606. + a->thargs = NULL;
  17607. + }
  17608. +
  17609. + return 0;
  17610. +}
  17611. +
  17612. +/* make it 'opaque' dir. */
  17613. +static int au_ren_diropq(struct au_ren_args *a)
  17614. +{
  17615. + int err;
  17616. + struct dentry *diropq;
  17617. +
  17618. + err = 0;
  17619. + a->src_bdiropq = au_dbdiropq(a->src_dentry);
  17620. + a->src_hinode = au_hi(a->src_inode, a->btgt);
  17621. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  17622. + diropq = au_diropq_create(a->src_dentry, a->btgt);
  17623. + au_hn_imtx_unlock(a->src_hinode);
  17624. + if (IS_ERR(diropq))
  17625. + err = PTR_ERR(diropq);
  17626. + else
  17627. + dput(diropq);
  17628. +
  17629. + return err;
  17630. +}
  17631. +
  17632. +static int do_rename(struct au_ren_args *a)
  17633. +{
  17634. + int err;
  17635. + struct dentry *d, *h_d;
  17636. +
  17637. + /* prepare workqueue args for asynchronous rmdir */
  17638. + h_d = a->dst_h_dentry;
  17639. + if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
  17640. + err = -ENOMEM;
  17641. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
  17642. + if (unlikely(!a->thargs))
  17643. + goto out;
  17644. + a->h_dst = dget(h_d);
  17645. + }
  17646. +
  17647. + /* create whiteout for src_dentry */
  17648. + if (au_ftest_ren(a->flags, WHSRC)) {
  17649. + a->src_bwh = au_dbwh(a->src_dentry);
  17650. + AuDebugOn(a->src_bwh >= 0);
  17651. + a->src_wh_dentry
  17652. + = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
  17653. + err = PTR_ERR(a->src_wh_dentry);
  17654. + if (IS_ERR(a->src_wh_dentry))
  17655. + goto out_thargs;
  17656. + }
  17657. +
  17658. + /* lookup whiteout for dentry */
  17659. + if (au_ftest_ren(a->flags, WHDST)) {
  17660. + h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
  17661. + a->br);
  17662. + err = PTR_ERR(h_d);
  17663. + if (IS_ERR(h_d))
  17664. + goto out_whsrc;
  17665. + if (!h_d->d_inode)
  17666. + dput(h_d);
  17667. + else
  17668. + a->dst_wh_dentry = h_d;
  17669. + }
  17670. +
  17671. + /* rename dentry to tmpwh */
  17672. + if (a->thargs) {
  17673. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  17674. + if (unlikely(err))
  17675. + goto out_whdst;
  17676. +
  17677. + d = a->dst_dentry;
  17678. + au_set_h_dptr(d, a->btgt, NULL);
  17679. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  17680. + if (unlikely(err))
  17681. + goto out_whtmp;
  17682. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  17683. + }
  17684. +
  17685. + BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
  17686. +
  17687. + /* rename by vfs_rename or cpup */
  17688. + d = a->dst_dentry;
  17689. + if (au_ftest_ren(a->flags, ISDIR)
  17690. + && (a->dst_wh_dentry
  17691. + || au_dbdiropq(d) == a->btgt
  17692. + /* hide the lower to keep xino */
  17693. + || a->btgt < au_dbend(d)
  17694. + || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
  17695. + au_fset_ren(a->flags, DIROPQ);
  17696. + err = au_ren_or_cpup(a);
  17697. + if (unlikely(err))
  17698. + /* leave the copied-up one */
  17699. + goto out_whtmp;
  17700. +
  17701. + /* make dir opaque */
  17702. + if (au_ftest_ren(a->flags, DIROPQ)) {
  17703. + err = au_ren_diropq(a);
  17704. + if (unlikely(err))
  17705. + goto out_rename;
  17706. + }
  17707. +
  17708. + /* update target timestamps */
  17709. + AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
  17710. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  17711. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  17712. + a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
  17713. +
  17714. + /* remove whiteout for dentry */
  17715. + if (a->dst_wh_dentry) {
  17716. + a->h_path.dentry = a->dst_wh_dentry;
  17717. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  17718. + a->dst_dentry);
  17719. + if (unlikely(err))
  17720. + goto out_diropq;
  17721. + }
  17722. +
  17723. + /* remove whtmp */
  17724. + if (a->thargs)
  17725. + au_ren_del_whtmp(a); /* ignore this error */
  17726. +
  17727. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  17728. + err = 0;
  17729. + goto out_success;
  17730. +
  17731. +out_diropq:
  17732. + if (au_ftest_ren(a->flags, DIROPQ))
  17733. + au_ren_rev_diropq(err, a);
  17734. +out_rename:
  17735. + au_ren_rev_rename(err, a);
  17736. + dput(a->h_dst);
  17737. +out_whtmp:
  17738. + if (a->thargs)
  17739. + au_ren_rev_whtmp(err, a);
  17740. +out_whdst:
  17741. + dput(a->dst_wh_dentry);
  17742. + a->dst_wh_dentry = NULL;
  17743. +out_whsrc:
  17744. + if (a->src_wh_dentry)
  17745. + au_ren_rev_whsrc(err, a);
  17746. +out_success:
  17747. + dput(a->src_wh_dentry);
  17748. + dput(a->dst_wh_dentry);
  17749. +out_thargs:
  17750. + if (a->thargs) {
  17751. + dput(a->h_dst);
  17752. + au_whtmp_rmdir_free(a->thargs);
  17753. + a->thargs = NULL;
  17754. + }
  17755. +out:
  17756. + return err;
  17757. +}
  17758. +
  17759. +/* ---------------------------------------------------------------------- */
  17760. +
  17761. +/*
  17762. + * test if @dentry dir can be rename destination or not.
  17763. + * success means, it is a logically empty dir.
  17764. + */
  17765. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  17766. +{
  17767. + return au_test_empty(dentry, whlist);
  17768. +}
  17769. +
  17770. +/*
  17771. + * test if @dentry dir can be rename source or not.
  17772. + * if it can, return 0 and @children is filled.
  17773. + * success means,
  17774. + * - it is a logically empty dir.
  17775. + * - or, it exists on writable branch and has no children including whiteouts
  17776. + * on the lower branch.
  17777. + */
  17778. +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
  17779. +{
  17780. + int err;
  17781. + unsigned int rdhash;
  17782. + aufs_bindex_t bstart;
  17783. +
  17784. + bstart = au_dbstart(dentry);
  17785. + if (bstart != btgt) {
  17786. + struct au_nhash whlist;
  17787. +
  17788. + SiMustAnyLock(dentry->d_sb);
  17789. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  17790. + if (!rdhash)
  17791. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  17792. + dentry));
  17793. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  17794. + if (unlikely(err))
  17795. + goto out;
  17796. + err = au_test_empty(dentry, &whlist);
  17797. + au_nhash_wh_free(&whlist);
  17798. + goto out;
  17799. + }
  17800. +
  17801. + if (bstart == au_dbtaildir(dentry))
  17802. + return 0; /* success */
  17803. +
  17804. + err = au_test_empty_lower(dentry);
  17805. +
  17806. +out:
  17807. + if (err == -ENOTEMPTY) {
  17808. + AuWarn1("renaming dir who has child(ren) on multiple branches,"
  17809. + " is not supported\n");
  17810. + err = -EXDEV;
  17811. + }
  17812. + return err;
  17813. +}
  17814. +
  17815. +/* side effect: sets whlist and h_dentry */
  17816. +static int au_ren_may_dir(struct au_ren_args *a)
  17817. +{
  17818. + int err;
  17819. + unsigned int rdhash;
  17820. + struct dentry *d;
  17821. +
  17822. + d = a->dst_dentry;
  17823. + SiMustAnyLock(d->d_sb);
  17824. +
  17825. + err = 0;
  17826. + if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
  17827. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  17828. + if (!rdhash)
  17829. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  17830. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  17831. + if (unlikely(err))
  17832. + goto out;
  17833. +
  17834. + au_set_dbstart(d, a->dst_bstart);
  17835. + err = may_rename_dstdir(d, &a->whlist);
  17836. + au_set_dbstart(d, a->btgt);
  17837. + }
  17838. + a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
  17839. + if (unlikely(err))
  17840. + goto out;
  17841. +
  17842. + d = a->src_dentry;
  17843. + a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
  17844. + if (au_ftest_ren(a->flags, ISDIR)) {
  17845. + err = may_rename_srcdir(d, a->btgt);
  17846. + if (unlikely(err)) {
  17847. + au_nhash_wh_free(&a->whlist);
  17848. + a->whlist.nh_num = 0;
  17849. + }
  17850. + }
  17851. +out:
  17852. + return err;
  17853. +}
  17854. +
  17855. +/* ---------------------------------------------------------------------- */
  17856. +
  17857. +/*
  17858. + * simple tests for rename.
  17859. + * following the checks in vfs, plus the parent-child relationship.
  17860. + */
  17861. +static int au_may_ren(struct au_ren_args *a)
  17862. +{
  17863. + int err, isdir;
  17864. + struct inode *h_inode;
  17865. +
  17866. + if (a->src_bstart == a->btgt) {
  17867. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  17868. + au_ftest_ren(a->flags, ISDIR));
  17869. + if (unlikely(err))
  17870. + goto out;
  17871. + err = -EINVAL;
  17872. + if (unlikely(a->src_h_dentry == a->h_trap))
  17873. + goto out;
  17874. + }
  17875. +
  17876. + err = 0;
  17877. + if (a->dst_bstart != a->btgt)
  17878. + goto out;
  17879. +
  17880. + err = -ENOTEMPTY;
  17881. + if (unlikely(a->dst_h_dentry == a->h_trap))
  17882. + goto out;
  17883. +
  17884. + err = -EIO;
  17885. + h_inode = a->dst_h_dentry->d_inode;
  17886. + isdir = !!au_ftest_ren(a->flags, ISDIR);
  17887. + if (!a->dst_dentry->d_inode) {
  17888. + if (unlikely(h_inode))
  17889. + goto out;
  17890. + err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
  17891. + isdir);
  17892. + } else {
  17893. + if (unlikely(!h_inode || !h_inode->i_nlink))
  17894. + goto out;
  17895. + err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
  17896. + isdir);
  17897. + if (unlikely(err))
  17898. + goto out;
  17899. + }
  17900. +
  17901. +out:
  17902. + if (unlikely(err == -ENOENT || err == -EEXIST))
  17903. + err = -EIO;
  17904. + AuTraceErr(err);
  17905. + return err;
  17906. +}
  17907. +
  17908. +/* ---------------------------------------------------------------------- */
  17909. +
  17910. +/*
  17911. + * locking order
  17912. + * (VFS)
  17913. + * - src_dir and dir by lock_rename()
  17914. + * - inode if exitsts
  17915. + * (aufs)
  17916. + * - lock all
  17917. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  17918. + * + si_read_lock
  17919. + * + di_write_lock2_child()
  17920. + * + di_write_lock_child()
  17921. + * + ii_write_lock_child()
  17922. + * + di_write_lock_child2()
  17923. + * + ii_write_lock_child2()
  17924. + * + src_parent and parent
  17925. + * + di_write_lock_parent()
  17926. + * + ii_write_lock_parent()
  17927. + * + di_write_lock_parent2()
  17928. + * + ii_write_lock_parent2()
  17929. + * + lower src_dir and dir by vfsub_lock_rename()
  17930. + * + verify the every relationships between child and parent. if any
  17931. + * of them failed, unlock all and return -EBUSY.
  17932. + */
  17933. +static void au_ren_unlock(struct au_ren_args *a)
  17934. +{
  17935. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  17936. + a->dst_h_parent, a->dst_hdir);
  17937. + if (au_ftest_ren(a->flags, MNT_WRITE))
  17938. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  17939. +}
  17940. +
  17941. +static int au_ren_lock(struct au_ren_args *a)
  17942. +{
  17943. + int err;
  17944. + unsigned int udba;
  17945. +
  17946. + err = 0;
  17947. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  17948. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  17949. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  17950. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  17951. +
  17952. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  17953. + if (unlikely(err))
  17954. + goto out;
  17955. + au_fset_ren(a->flags, MNT_WRITE);
  17956. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  17957. + a->dst_h_parent, a->dst_hdir);
  17958. + udba = au_opt_udba(a->src_dentry->d_sb);
  17959. + if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
  17960. + || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
  17961. + err = au_busy_or_stale();
  17962. + if (!err && au_dbstart(a->src_dentry) == a->btgt)
  17963. + err = au_h_verify(a->src_h_dentry, udba,
  17964. + a->src_h_parent->d_inode, a->src_h_parent,
  17965. + a->br);
  17966. + if (!err && au_dbstart(a->dst_dentry) == a->btgt)
  17967. + err = au_h_verify(a->dst_h_dentry, udba,
  17968. + a->dst_h_parent->d_inode, a->dst_h_parent,
  17969. + a->br);
  17970. + if (!err)
  17971. + goto out; /* success */
  17972. +
  17973. + err = au_busy_or_stale();
  17974. + au_ren_unlock(a);
  17975. +
  17976. +out:
  17977. + return err;
  17978. +}
  17979. +
  17980. +/* ---------------------------------------------------------------------- */
  17981. +
  17982. +static void au_ren_refresh_dir(struct au_ren_args *a)
  17983. +{
  17984. + struct inode *dir;
  17985. +
  17986. + dir = a->dst_dir;
  17987. + dir->i_version++;
  17988. + if (au_ftest_ren(a->flags, ISDIR)) {
  17989. + /* is this updating defined in POSIX? */
  17990. + au_cpup_attr_timesizes(a->src_inode);
  17991. + au_cpup_attr_nlink(dir, /*force*/1);
  17992. + }
  17993. +
  17994. + au_dir_ts(dir, a->btgt);
  17995. +
  17996. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  17997. + return;
  17998. +
  17999. + dir = a->src_dir;
  18000. + dir->i_version++;
  18001. + if (au_ftest_ren(a->flags, ISDIR))
  18002. + au_cpup_attr_nlink(dir, /*force*/1);
  18003. + au_dir_ts(dir, a->btgt);
  18004. +}
  18005. +
  18006. +static void au_ren_refresh(struct au_ren_args *a)
  18007. +{
  18008. + aufs_bindex_t bend, bindex;
  18009. + struct dentry *d, *h_d;
  18010. + struct inode *i, *h_i;
  18011. + struct super_block *sb;
  18012. +
  18013. + d = a->dst_dentry;
  18014. + d_drop(d);
  18015. + if (a->h_dst)
  18016. + /* already dget-ed by au_ren_or_cpup() */
  18017. + au_set_h_dptr(d, a->btgt, a->h_dst);
  18018. +
  18019. + i = a->dst_inode;
  18020. + if (i) {
  18021. + if (!au_ftest_ren(a->flags, ISDIR))
  18022. + vfsub_drop_nlink(i);
  18023. + else {
  18024. + vfsub_dead_dir(i);
  18025. + au_cpup_attr_timesizes(i);
  18026. + }
  18027. + au_update_dbrange(d, /*do_put_zero*/1);
  18028. + } else {
  18029. + bend = a->btgt;
  18030. + for (bindex = au_dbstart(d); bindex < bend; bindex++)
  18031. + au_set_h_dptr(d, bindex, NULL);
  18032. + bend = au_dbend(d);
  18033. + for (bindex = a->btgt + 1; bindex <= bend; bindex++)
  18034. + au_set_h_dptr(d, bindex, NULL);
  18035. + au_update_dbrange(d, /*do_put_zero*/0);
  18036. + }
  18037. +
  18038. + d = a->src_dentry;
  18039. + au_set_dbwh(d, -1);
  18040. + bend = au_dbend(d);
  18041. + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
  18042. + h_d = au_h_dptr(d, bindex);
  18043. + if (h_d)
  18044. + au_set_h_dptr(d, bindex, NULL);
  18045. + }
  18046. + au_set_dbend(d, a->btgt);
  18047. +
  18048. + sb = d->d_sb;
  18049. + i = a->src_inode;
  18050. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  18051. + return; /* success */
  18052. +
  18053. + bend = au_ibend(i);
  18054. + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
  18055. + h_i = au_h_iptr(i, bindex);
  18056. + if (h_i) {
  18057. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  18058. + /* ignore this error */
  18059. + au_set_h_iptr(i, bindex, NULL, 0);
  18060. + }
  18061. + }
  18062. + au_set_ibend(i, a->btgt);
  18063. +}
  18064. +
  18065. +/* ---------------------------------------------------------------------- */
  18066. +
  18067. +/* mainly for link(2) and rename(2) */
  18068. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  18069. +{
  18070. + aufs_bindex_t bdiropq, bwh;
  18071. + struct dentry *parent;
  18072. + struct au_branch *br;
  18073. +
  18074. + parent = dentry->d_parent;
  18075. + IMustLock(parent->d_inode); /* dir is locked */
  18076. +
  18077. + bdiropq = au_dbdiropq(parent);
  18078. + bwh = au_dbwh(dentry);
  18079. + br = au_sbr(dentry->d_sb, btgt);
  18080. + if (au_br_rdonly(br)
  18081. + || (0 <= bdiropq && bdiropq < btgt)
  18082. + || (0 <= bwh && bwh < btgt))
  18083. + btgt = -1;
  18084. +
  18085. + AuDbg("btgt %d\n", btgt);
  18086. + return btgt;
  18087. +}
  18088. +
  18089. +/* sets src_bstart, dst_bstart and btgt */
  18090. +static int au_ren_wbr(struct au_ren_args *a)
  18091. +{
  18092. + int err;
  18093. + struct au_wr_dir_args wr_dir_args = {
  18094. + /* .force_btgt = -1, */
  18095. + .flags = AuWrDir_ADD_ENTRY
  18096. + };
  18097. +
  18098. + a->src_bstart = au_dbstart(a->src_dentry);
  18099. + a->dst_bstart = au_dbstart(a->dst_dentry);
  18100. + if (au_ftest_ren(a->flags, ISDIR))
  18101. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  18102. + wr_dir_args.force_btgt = a->src_bstart;
  18103. + if (a->dst_inode && a->dst_bstart < a->src_bstart)
  18104. + wr_dir_args.force_btgt = a->dst_bstart;
  18105. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  18106. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  18107. + a->btgt = err;
  18108. +
  18109. + return err;
  18110. +}
  18111. +
  18112. +static void au_ren_dt(struct au_ren_args *a)
  18113. +{
  18114. + a->h_path.dentry = a->src_h_parent;
  18115. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  18116. + if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
  18117. + a->h_path.dentry = a->dst_h_parent;
  18118. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  18119. + }
  18120. +
  18121. + au_fclr_ren(a->flags, DT_DSTDIR);
  18122. + if (!au_ftest_ren(a->flags, ISDIR))
  18123. + return;
  18124. +
  18125. + a->h_path.dentry = a->src_h_dentry;
  18126. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  18127. + if (a->dst_h_dentry->d_inode) {
  18128. + au_fset_ren(a->flags, DT_DSTDIR);
  18129. + a->h_path.dentry = a->dst_h_dentry;
  18130. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  18131. + }
  18132. +}
  18133. +
  18134. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  18135. +{
  18136. + struct dentry *h_d;
  18137. + struct mutex *h_mtx;
  18138. +
  18139. + au_dtime_revert(a->src_dt + AuPARENT);
  18140. + if (!au_ftest_ren(a->flags, ISSAMEDIR))
  18141. + au_dtime_revert(a->dst_dt + AuPARENT);
  18142. +
  18143. + if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
  18144. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  18145. + h_mtx = &h_d->d_inode->i_mutex;
  18146. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  18147. + au_dtime_revert(a->src_dt + AuCHILD);
  18148. + mutex_unlock(h_mtx);
  18149. +
  18150. + if (au_ftest_ren(a->flags, DT_DSTDIR)) {
  18151. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  18152. + h_mtx = &h_d->d_inode->i_mutex;
  18153. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  18154. + au_dtime_revert(a->dst_dt + AuCHILD);
  18155. + mutex_unlock(h_mtx);
  18156. + }
  18157. + }
  18158. +}
  18159. +
  18160. +/* ---------------------------------------------------------------------- */
  18161. +
  18162. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  18163. + struct inode *_dst_dir, struct dentry *_dst_dentry)
  18164. +{
  18165. + int err, flags;
  18166. + /* reduce stack space */
  18167. + struct au_ren_args *a;
  18168. +
  18169. + AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
  18170. + IMustLock(_src_dir);
  18171. + IMustLock(_dst_dir);
  18172. +
  18173. + err = -ENOMEM;
  18174. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  18175. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18176. + if (unlikely(!a))
  18177. + goto out;
  18178. +
  18179. + a->src_dir = _src_dir;
  18180. + a->src_dentry = _src_dentry;
  18181. + a->src_inode = a->src_dentry->d_inode;
  18182. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  18183. + a->dst_dir = _dst_dir;
  18184. + a->dst_dentry = _dst_dentry;
  18185. + a->dst_inode = a->dst_dentry->d_inode;
  18186. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  18187. + if (a->dst_inode) {
  18188. + IMustLock(a->dst_inode);
  18189. + au_igrab(a->dst_inode);
  18190. + }
  18191. +
  18192. + err = -ENOTDIR;
  18193. + flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  18194. + if (d_is_dir(a->src_dentry)) {
  18195. + au_fset_ren(a->flags, ISDIR);
  18196. + if (unlikely(d_is_positive(a->dst_dentry)
  18197. + && !d_is_dir(a->dst_dentry)))
  18198. + goto out_free;
  18199. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  18200. + AuLock_DIR | flags);
  18201. + } else
  18202. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  18203. + flags);
  18204. + if (unlikely(err))
  18205. + goto out_free;
  18206. +
  18207. + err = au_d_hashed_positive(a->src_dentry);
  18208. + if (unlikely(err))
  18209. + goto out_unlock;
  18210. + err = -ENOENT;
  18211. + if (a->dst_inode) {
  18212. + /*
  18213. + * If it is a dir, VFS unhash dst_dentry before this
  18214. + * function. It means we cannot rely upon d_unhashed().
  18215. + */
  18216. + if (unlikely(!a->dst_inode->i_nlink))
  18217. + goto out_unlock;
  18218. + if (!S_ISDIR(a->dst_inode->i_mode)) {
  18219. + err = au_d_hashed_positive(a->dst_dentry);
  18220. + if (unlikely(err))
  18221. + goto out_unlock;
  18222. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  18223. + goto out_unlock;
  18224. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  18225. + goto out_unlock;
  18226. +
  18227. + /*
  18228. + * is it possible?
  18229. + * yes, it happend (in linux-3.3-rcN) but I don't know why.
  18230. + * there may exist a problem somewhere else.
  18231. + */
  18232. + err = -EINVAL;
  18233. + if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
  18234. + goto out_unlock;
  18235. +
  18236. + au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
  18237. + di_write_lock_parent(a->dst_parent);
  18238. +
  18239. + /* which branch we process */
  18240. + err = au_ren_wbr(a);
  18241. + if (unlikely(err < 0))
  18242. + goto out_parent;
  18243. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  18244. + a->h_path.mnt = au_br_mnt(a->br);
  18245. +
  18246. + /* are they available to be renamed */
  18247. + err = au_ren_may_dir(a);
  18248. + if (unlikely(err))
  18249. + goto out_children;
  18250. +
  18251. + /* prepare the writable parent dir on the same branch */
  18252. + if (a->dst_bstart == a->btgt) {
  18253. + au_fset_ren(a->flags, WHDST);
  18254. + } else {
  18255. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  18256. + if (unlikely(err))
  18257. + goto out_children;
  18258. + }
  18259. +
  18260. + if (a->src_dir != a->dst_dir) {
  18261. + /*
  18262. + * this temporary unlock is safe,
  18263. + * because both dir->i_mutex are locked.
  18264. + */
  18265. + di_write_unlock(a->dst_parent);
  18266. + di_write_lock_parent(a->src_parent);
  18267. + err = au_wr_dir_need_wh(a->src_dentry,
  18268. + au_ftest_ren(a->flags, ISDIR),
  18269. + &a->btgt);
  18270. + di_write_unlock(a->src_parent);
  18271. + di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
  18272. + au_fclr_ren(a->flags, ISSAMEDIR);
  18273. + } else
  18274. + err = au_wr_dir_need_wh(a->src_dentry,
  18275. + au_ftest_ren(a->flags, ISDIR),
  18276. + &a->btgt);
  18277. + if (unlikely(err < 0))
  18278. + goto out_children;
  18279. + if (err)
  18280. + au_fset_ren(a->flags, WHSRC);
  18281. +
  18282. + /* cpup src */
  18283. + if (a->src_bstart != a->btgt) {
  18284. + struct au_pin pin;
  18285. +
  18286. + err = au_pin(&pin, a->src_dentry, a->btgt,
  18287. + au_opt_udba(a->src_dentry->d_sb),
  18288. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  18289. + if (!err) {
  18290. + struct au_cp_generic cpg = {
  18291. + .dentry = a->src_dentry,
  18292. + .bdst = a->btgt,
  18293. + .bsrc = a->src_bstart,
  18294. + .len = -1,
  18295. + .pin = &pin,
  18296. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  18297. + };
  18298. + AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
  18299. + err = au_sio_cpup_simple(&cpg);
  18300. + au_unpin(&pin);
  18301. + }
  18302. + if (unlikely(err))
  18303. + goto out_children;
  18304. + a->src_bstart = a->btgt;
  18305. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  18306. + au_fset_ren(a->flags, WHSRC);
  18307. + }
  18308. +
  18309. + /* lock them all */
  18310. + err = au_ren_lock(a);
  18311. + if (unlikely(err))
  18312. + /* leave the copied-up one */
  18313. + goto out_children;
  18314. +
  18315. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  18316. + err = au_may_ren(a);
  18317. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  18318. + err = -ENAMETOOLONG;
  18319. + if (unlikely(err))
  18320. + goto out_hdir;
  18321. +
  18322. + /* store timestamps to be revertible */
  18323. + au_ren_dt(a);
  18324. +
  18325. + /* here we go */
  18326. + err = do_rename(a);
  18327. + if (unlikely(err))
  18328. + goto out_dt;
  18329. +
  18330. + /* update dir attributes */
  18331. + au_ren_refresh_dir(a);
  18332. +
  18333. + /* dput/iput all lower dentries */
  18334. + au_ren_refresh(a);
  18335. +
  18336. + goto out_hdir; /* success */
  18337. +
  18338. +out_dt:
  18339. + au_ren_rev_dt(err, a);
  18340. +out_hdir:
  18341. + au_ren_unlock(a);
  18342. +out_children:
  18343. + au_nhash_wh_free(&a->whlist);
  18344. + if (err && a->dst_inode && a->dst_bstart != a->btgt) {
  18345. + AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
  18346. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  18347. + au_set_dbstart(a->dst_dentry, a->dst_bstart);
  18348. + }
  18349. +out_parent:
  18350. + if (!err)
  18351. + d_move(a->src_dentry, a->dst_dentry);
  18352. + else {
  18353. + au_update_dbstart(a->dst_dentry);
  18354. + if (!a->dst_inode)
  18355. + d_drop(a->dst_dentry);
  18356. + }
  18357. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  18358. + di_write_unlock(a->dst_parent);
  18359. + else
  18360. + di_write_unlock2(a->src_parent, a->dst_parent);
  18361. +out_unlock:
  18362. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  18363. +out_free:
  18364. + iput(a->dst_inode);
  18365. + if (a->thargs)
  18366. + au_whtmp_rmdir_free(a->thargs);
  18367. + kfree(a);
  18368. +out:
  18369. + AuTraceErr(err);
  18370. + return err;
  18371. +}
  18372. diff -Nur linux-4.0.5.orig/fs/aufs/Kconfig linux-4.0.5/fs/aufs/Kconfig
  18373. --- linux-4.0.5.orig/fs/aufs/Kconfig 1969-12-31 18:00:00.000000000 -0600
  18374. +++ linux-4.0.5/fs/aufs/Kconfig 2015-06-21 14:07:48.682432781 -0500
  18375. @@ -0,0 +1,185 @@
  18376. +config AUFS_FS
  18377. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  18378. + help
  18379. + Aufs is a stackable unification filesystem such as Unionfs,
  18380. + which unifies several directories and provides a merged single
  18381. + directory.
  18382. + In the early days, aufs was entirely re-designed and
  18383. + re-implemented Unionfs Version 1.x series. Introducing many
  18384. + original ideas, approaches and improvements, it becomes totally
  18385. + different from Unionfs while keeping the basic features.
  18386. +
  18387. +if AUFS_FS
  18388. +choice
  18389. + prompt "Maximum number of branches"
  18390. + default AUFS_BRANCH_MAX_127
  18391. + help
  18392. + Specifies the maximum number of branches (or member directories)
  18393. + in a single aufs. The larger value consumes more system
  18394. + resources and has a minor impact to performance.
  18395. +config AUFS_BRANCH_MAX_127
  18396. + bool "127"
  18397. + help
  18398. + Specifies the maximum number of branches (or member directories)
  18399. + in a single aufs. The larger value consumes more system
  18400. + resources and has a minor impact to performance.
  18401. +config AUFS_BRANCH_MAX_511
  18402. + bool "511"
  18403. + help
  18404. + Specifies the maximum number of branches (or member directories)
  18405. + in a single aufs. The larger value consumes more system
  18406. + resources and has a minor impact to performance.
  18407. +config AUFS_BRANCH_MAX_1023
  18408. + bool "1023"
  18409. + help
  18410. + Specifies the maximum number of branches (or member directories)
  18411. + in a single aufs. The larger value consumes more system
  18412. + resources and has a minor impact to performance.
  18413. +config AUFS_BRANCH_MAX_32767
  18414. + bool "32767"
  18415. + help
  18416. + Specifies the maximum number of branches (or member directories)
  18417. + in a single aufs. The larger value consumes more system
  18418. + resources and has a minor impact to performance.
  18419. +endchoice
  18420. +
  18421. +config AUFS_SBILIST
  18422. + bool
  18423. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  18424. + default y
  18425. + help
  18426. + Automatic configuration for internal use.
  18427. + When aufs supports Magic SysRq or /proc, enabled automatically.
  18428. +
  18429. +config AUFS_HNOTIFY
  18430. + bool "Detect direct branch access (bypassing aufs)"
  18431. + help
  18432. + If you want to modify files on branches directly, eg. bypassing aufs,
  18433. + and want aufs to detect the changes of them fully, then enable this
  18434. + option and use 'udba=notify' mount option.
  18435. + Currently there is only one available configuration, "fsnotify".
  18436. + It will have a negative impact to the performance.
  18437. + See detail in aufs.5.
  18438. +
  18439. +choice
  18440. + prompt "method" if AUFS_HNOTIFY
  18441. + default AUFS_HFSNOTIFY
  18442. +config AUFS_HFSNOTIFY
  18443. + bool "fsnotify"
  18444. + select FSNOTIFY
  18445. +endchoice
  18446. +
  18447. +config AUFS_EXPORT
  18448. + bool "NFS-exportable aufs"
  18449. + depends on EXPORTFS
  18450. + help
  18451. + If you want to export your mounted aufs via NFS, then enable this
  18452. + option. There are several requirements for this configuration.
  18453. + See detail in aufs.5.
  18454. +
  18455. +config AUFS_INO_T_64
  18456. + bool
  18457. + depends on AUFS_EXPORT
  18458. + depends on 64BIT && !(ALPHA || S390)
  18459. + default y
  18460. + help
  18461. + Automatic configuration for internal use.
  18462. + /* typedef unsigned long/int __kernel_ino_t */
  18463. + /* alpha and s390x are int */
  18464. +
  18465. +config AUFS_XATTR
  18466. + bool "support for XATTR/EA (including Security Labels)"
  18467. + help
  18468. + If your branch fs supports XATTR/EA and you want to make them
  18469. + available in aufs too, then enable this opsion and specify the
  18470. + branch attributes for EA.
  18471. + See detail in aufs.5.
  18472. +
  18473. +config AUFS_FHSM
  18474. + bool "File-based Hierarchical Storage Management"
  18475. + help
  18476. + Hierarchical Storage Management (or HSM) is a well-known feature
  18477. + in the storage world. Aufs provides this feature as file-based.
  18478. + with multiple branches.
  18479. + These multiple branches are prioritized, ie. the topmost one
  18480. + should be the fastest drive and be used heavily.
  18481. +
  18482. +config AUFS_RDU
  18483. + bool "Readdir in userspace"
  18484. + help
  18485. + Aufs has two methods to provide a merged view for a directory,
  18486. + by a user-space library and by kernel-space natively. The latter
  18487. + is always enabled but sometimes large and slow.
  18488. + If you enable this option, install the library in aufs2-util
  18489. + package, and set some environment variables for your readdir(3),
  18490. + then the work will be handled in user-space which generally
  18491. + shows better performance in most cases.
  18492. + See detail in aufs.5.
  18493. +
  18494. +config AUFS_SHWH
  18495. + bool "Show whiteouts"
  18496. + help
  18497. + If you want to make the whiteouts in aufs visible, then enable
  18498. + this option and specify 'shwh' mount option. Although it may
  18499. + sounds like philosophy or something, but in technically it
  18500. + simply shows the name of whiteout with keeping its behaviour.
  18501. +
  18502. +config AUFS_BR_RAMFS
  18503. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  18504. + help
  18505. + If you want to use ramfs as an aufs branch fs, then enable this
  18506. + option. Generally tmpfs is recommended.
  18507. + Aufs prohibited them to be a branch fs by default, because
  18508. + initramfs becomes unusable after switch_root or something
  18509. + generally. If you sets initramfs as an aufs branch and boot your
  18510. + system by switch_root, you will meet a problem easily since the
  18511. + files in initramfs may be inaccessible.
  18512. + Unless you are going to use ramfs as an aufs branch fs without
  18513. + switch_root or something, leave it N.
  18514. +
  18515. +config AUFS_BR_FUSE
  18516. + bool "Fuse fs as an aufs branch"
  18517. + depends on FUSE_FS
  18518. + select AUFS_POLL
  18519. + help
  18520. + If you want to use fuse-based userspace filesystem as an aufs
  18521. + branch fs, then enable this option.
  18522. + It implements the internal poll(2) operation which is
  18523. + implemented by fuse only (curretnly).
  18524. +
  18525. +config AUFS_POLL
  18526. + bool
  18527. + help
  18528. + Automatic configuration for internal use.
  18529. +
  18530. +config AUFS_BR_HFSPLUS
  18531. + bool "Hfsplus as an aufs branch"
  18532. + depends on HFSPLUS_FS
  18533. + default y
  18534. + help
  18535. + If you want to use hfsplus fs as an aufs branch fs, then enable
  18536. + this option. This option introduces a small overhead at
  18537. + copying-up a file on hfsplus.
  18538. +
  18539. +config AUFS_BDEV_LOOP
  18540. + bool
  18541. + depends on BLK_DEV_LOOP
  18542. + default y
  18543. + help
  18544. + Automatic configuration for internal use.
  18545. + Convert =[ym] into =y.
  18546. +
  18547. +config AUFS_DEBUG
  18548. + bool "Debug aufs"
  18549. + help
  18550. + Enable this to compile aufs internal debug code.
  18551. + It will have a negative impact to the performance.
  18552. +
  18553. +config AUFS_MAGIC_SYSRQ
  18554. + bool
  18555. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  18556. + default y
  18557. + help
  18558. + Automatic configuration for internal use.
  18559. + When aufs supports Magic SysRq, enabled automatically.
  18560. +endif
  18561. diff -Nur linux-4.0.5.orig/fs/aufs/loop.c linux-4.0.5/fs/aufs/loop.c
  18562. --- linux-4.0.5.orig/fs/aufs/loop.c 1969-12-31 18:00:00.000000000 -0600
  18563. +++ linux-4.0.5/fs/aufs/loop.c 2015-06-21 14:07:48.686432781 -0500
  18564. @@ -0,0 +1,145 @@
  18565. +/*
  18566. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  18567. + *
  18568. + * This program, aufs is free software; you can redistribute it and/or modify
  18569. + * it under the terms of the GNU General Public License as published by
  18570. + * the Free Software Foundation; either version 2 of the License, or
  18571. + * (at your option) any later version.
  18572. + *
  18573. + * This program is distributed in the hope that it will be useful,
  18574. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18575. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18576. + * GNU General Public License for more details.
  18577. + *
  18578. + * You should have received a copy of the GNU General Public License
  18579. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18580. + */
  18581. +
  18582. +/*
  18583. + * support for loopback block device as a branch
  18584. + */
  18585. +
  18586. +#include "aufs.h"
  18587. +
  18588. +/* added into drivers/block/loop.c */
  18589. +static struct file *(*backing_file_func)(struct super_block *sb);
  18590. +
  18591. +/*
  18592. + * test if two lower dentries have overlapping branches.
  18593. + */
  18594. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  18595. +{
  18596. + struct super_block *h_sb;
  18597. + struct file *backing_file;
  18598. +
  18599. + if (unlikely(!backing_file_func)) {
  18600. + /* don't load "loop" module here */
  18601. + backing_file_func = symbol_get(loop_backing_file);
  18602. + if (unlikely(!backing_file_func))
  18603. + /* "loop" module is not loaded */
  18604. + return 0;
  18605. + }
  18606. +
  18607. + h_sb = h_adding->d_sb;
  18608. + backing_file = backing_file_func(h_sb);
  18609. + if (!backing_file)
  18610. + return 0;
  18611. +
  18612. + h_adding = backing_file->f_path.dentry;
  18613. + /*
  18614. + * h_adding can be local NFS.
  18615. + * in this case aufs cannot detect the loop.
  18616. + */
  18617. + if (unlikely(h_adding->d_sb == sb))
  18618. + return 1;
  18619. + return !!au_test_subdir(h_adding, sb->s_root);
  18620. +}
  18621. +
  18622. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  18623. +int au_test_loopback_kthread(void)
  18624. +{
  18625. + int ret;
  18626. + struct task_struct *tsk = current;
  18627. + char c, comm[sizeof(tsk->comm)];
  18628. +
  18629. + ret = 0;
  18630. + if (tsk->flags & PF_KTHREAD) {
  18631. + get_task_comm(comm, tsk);
  18632. + c = comm[4];
  18633. + ret = ('0' <= c && c <= '9'
  18634. + && !strncmp(comm, "loop", 4));
  18635. + }
  18636. +
  18637. + return ret;
  18638. +}
  18639. +
  18640. +/* ---------------------------------------------------------------------- */
  18641. +
  18642. +#define au_warn_loopback_step 16
  18643. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  18644. +static unsigned long *au_warn_loopback_array;
  18645. +
  18646. +void au_warn_loopback(struct super_block *h_sb)
  18647. +{
  18648. + int i, new_nelem;
  18649. + unsigned long *a, magic;
  18650. + static DEFINE_SPINLOCK(spin);
  18651. +
  18652. + magic = h_sb->s_magic;
  18653. + spin_lock(&spin);
  18654. + a = au_warn_loopback_array;
  18655. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  18656. + if (a[i] == magic) {
  18657. + spin_unlock(&spin);
  18658. + return;
  18659. + }
  18660. +
  18661. + /* h_sb is new to us, print it */
  18662. + if (i < au_warn_loopback_nelem) {
  18663. + a[i] = magic;
  18664. + goto pr;
  18665. + }
  18666. +
  18667. + /* expand the array */
  18668. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  18669. + a = au_kzrealloc(au_warn_loopback_array,
  18670. + au_warn_loopback_nelem * sizeof(unsigned long),
  18671. + new_nelem * sizeof(unsigned long), GFP_ATOMIC);
  18672. + if (a) {
  18673. + au_warn_loopback_nelem = new_nelem;
  18674. + au_warn_loopback_array = a;
  18675. + a[i] = magic;
  18676. + goto pr;
  18677. + }
  18678. +
  18679. + spin_unlock(&spin);
  18680. + AuWarn1("realloc failed, ignored\n");
  18681. + return;
  18682. +
  18683. +pr:
  18684. + spin_unlock(&spin);
  18685. + pr_warn("you may want to try another patch for loopback file "
  18686. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  18687. +}
  18688. +
  18689. +int au_loopback_init(void)
  18690. +{
  18691. + int err;
  18692. + struct super_block *sb __maybe_unused;
  18693. +
  18694. + AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
  18695. +
  18696. + err = 0;
  18697. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  18698. + sizeof(unsigned long), GFP_NOFS);
  18699. + if (unlikely(!au_warn_loopback_array))
  18700. + err = -ENOMEM;
  18701. +
  18702. + return err;
  18703. +}
  18704. +
  18705. +void au_loopback_fin(void)
  18706. +{
  18707. + symbol_put(loop_backing_file);
  18708. + kfree(au_warn_loopback_array);
  18709. +}
  18710. diff -Nur linux-4.0.5.orig/fs/aufs/loop.h linux-4.0.5/fs/aufs/loop.h
  18711. --- linux-4.0.5.orig/fs/aufs/loop.h 1969-12-31 18:00:00.000000000 -0600
  18712. +++ linux-4.0.5/fs/aufs/loop.h 2015-06-21 14:07:48.686432781 -0500
  18713. @@ -0,0 +1,52 @@
  18714. +/*
  18715. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  18716. + *
  18717. + * This program, aufs is free software; you can redistribute it and/or modify
  18718. + * it under the terms of the GNU General Public License as published by
  18719. + * the Free Software Foundation; either version 2 of the License, or
  18720. + * (at your option) any later version.
  18721. + *
  18722. + * This program is distributed in the hope that it will be useful,
  18723. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18724. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18725. + * GNU General Public License for more details.
  18726. + *
  18727. + * You should have received a copy of the GNU General Public License
  18728. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18729. + */
  18730. +
  18731. +/*
  18732. + * support for loopback mount as a branch
  18733. + */
  18734. +
  18735. +#ifndef __AUFS_LOOP_H__
  18736. +#define __AUFS_LOOP_H__
  18737. +
  18738. +#ifdef __KERNEL__
  18739. +
  18740. +struct dentry;
  18741. +struct super_block;
  18742. +
  18743. +#ifdef CONFIG_AUFS_BDEV_LOOP
  18744. +/* drivers/block/loop.c */
  18745. +struct file *loop_backing_file(struct super_block *sb);
  18746. +
  18747. +/* loop.c */
  18748. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  18749. +int au_test_loopback_kthread(void);
  18750. +void au_warn_loopback(struct super_block *h_sb);
  18751. +
  18752. +int au_loopback_init(void);
  18753. +void au_loopback_fin(void);
  18754. +#else
  18755. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  18756. + struct dentry *h_adding)
  18757. +AuStubInt0(au_test_loopback_kthread, void)
  18758. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  18759. +
  18760. +AuStubInt0(au_loopback_init, void)
  18761. +AuStubVoid(au_loopback_fin, void)
  18762. +#endif /* BLK_DEV_LOOP */
  18763. +
  18764. +#endif /* __KERNEL__ */
  18765. +#endif /* __AUFS_LOOP_H__ */
  18766. diff -Nur linux-4.0.5.orig/fs/aufs/magic.mk linux-4.0.5/fs/aufs/magic.mk
  18767. --- linux-4.0.5.orig/fs/aufs/magic.mk 1969-12-31 18:00:00.000000000 -0600
  18768. +++ linux-4.0.5/fs/aufs/magic.mk 2015-06-21 14:07:48.686432781 -0500
  18769. @@ -0,0 +1,30 @@
  18770. +
  18771. +# defined in ${srctree}/fs/fuse/inode.c
  18772. +# tristate
  18773. +ifdef CONFIG_FUSE_FS
  18774. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  18775. +endif
  18776. +
  18777. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  18778. +# tristate
  18779. +ifdef CONFIG_XFS_FS
  18780. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  18781. +endif
  18782. +
  18783. +# defined in ${srctree}/fs/configfs/mount.c
  18784. +# tristate
  18785. +ifdef CONFIG_CONFIGFS_FS
  18786. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  18787. +endif
  18788. +
  18789. +# defined in ${srctree}/fs/ubifs/ubifs.h
  18790. +# tristate
  18791. +ifdef CONFIG_UBIFS_FS
  18792. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  18793. +endif
  18794. +
  18795. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  18796. +# tristate
  18797. +ifdef CONFIG_HFSPLUS_FS
  18798. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  18799. +endif
  18800. diff -Nur linux-4.0.5.orig/fs/aufs/Makefile linux-4.0.5/fs/aufs/Makefile
  18801. --- linux-4.0.5.orig/fs/aufs/Makefile 1969-12-31 18:00:00.000000000 -0600
  18802. +++ linux-4.0.5/fs/aufs/Makefile 2015-06-21 14:07:48.682432781 -0500
  18803. @@ -0,0 +1,44 @@
  18804. +
  18805. +include ${src}/magic.mk
  18806. +ifeq (${CONFIG_AUFS_FS},m)
  18807. +include ${src}/conf.mk
  18808. +endif
  18809. +-include ${src}/priv_def.mk
  18810. +
  18811. +# cf. include/linux/kernel.h
  18812. +# enable pr_debug
  18813. +ccflags-y += -DDEBUG
  18814. +# sparse requires the full pathname
  18815. +ifdef M
  18816. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  18817. +else
  18818. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  18819. +endif
  18820. +
  18821. +obj-$(CONFIG_AUFS_FS) += aufs.o
  18822. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  18823. + wkq.o vfsub.o dcsub.o \
  18824. + cpup.o whout.o wbr_policy.o \
  18825. + dinfo.o dentry.o \
  18826. + dynop.o \
  18827. + finfo.o file.o f_op.o \
  18828. + dir.o vdir.o \
  18829. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  18830. + mvdown.o ioctl.o
  18831. +
  18832. +# all are boolean
  18833. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  18834. +aufs-$(CONFIG_SYSFS) += sysfs.o
  18835. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  18836. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  18837. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  18838. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  18839. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  18840. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  18841. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  18842. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  18843. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  18844. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  18845. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  18846. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  18847. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  18848. diff -Nur linux-4.0.5.orig/fs/aufs/module.c linux-4.0.5/fs/aufs/module.c
  18849. --- linux-4.0.5.orig/fs/aufs/module.c 1969-12-31 18:00:00.000000000 -0600
  18850. +++ linux-4.0.5/fs/aufs/module.c 2015-06-21 14:07:48.686432781 -0500
  18851. @@ -0,0 +1,210 @@
  18852. +/*
  18853. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  18854. + *
  18855. + * This program, aufs is free software; you can redistribute it and/or modify
  18856. + * it under the terms of the GNU General Public License as published by
  18857. + * the Free Software Foundation; either version 2 of the License, or
  18858. + * (at your option) any later version.
  18859. + *
  18860. + * This program is distributed in the hope that it will be useful,
  18861. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18862. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18863. + * GNU General Public License for more details.
  18864. + *
  18865. + * You should have received a copy of the GNU General Public License
  18866. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18867. + */
  18868. +
  18869. +/*
  18870. + * module global variables and operations
  18871. + */
  18872. +
  18873. +#include <linux/module.h>
  18874. +#include <linux/seq_file.h>
  18875. +#include "aufs.h"
  18876. +
  18877. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
  18878. +{
  18879. + if (new_sz <= nused)
  18880. + return p;
  18881. +
  18882. + p = krealloc(p, new_sz, gfp);
  18883. + if (p)
  18884. + memset(p + nused, 0, new_sz - nused);
  18885. + return p;
  18886. +}
  18887. +
  18888. +/* ---------------------------------------------------------------------- */
  18889. +
  18890. +/*
  18891. + * aufs caches
  18892. + */
  18893. +struct kmem_cache *au_cachep[AuCache_Last];
  18894. +static int __init au_cache_init(void)
  18895. +{
  18896. + au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  18897. + if (au_cachep[AuCache_DINFO])
  18898. + /* SLAB_DESTROY_BY_RCU */
  18899. + au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  18900. + au_icntnr_init_once);
  18901. + if (au_cachep[AuCache_ICNTNR])
  18902. + au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
  18903. + au_fi_init_once);
  18904. + if (au_cachep[AuCache_FINFO])
  18905. + au_cachep[AuCache_VDIR] = AuCache(au_vdir);
  18906. + if (au_cachep[AuCache_VDIR])
  18907. + au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  18908. + if (au_cachep[AuCache_DEHSTR])
  18909. + return 0;
  18910. +
  18911. + return -ENOMEM;
  18912. +}
  18913. +
  18914. +static void au_cache_fin(void)
  18915. +{
  18916. + int i;
  18917. +
  18918. + /*
  18919. + * Make sure all delayed rcu free inodes are flushed before we
  18920. + * destroy cache.
  18921. + */
  18922. + rcu_barrier();
  18923. +
  18924. + /* excluding AuCache_HNOTIFY */
  18925. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  18926. + for (i = 0; i < AuCache_HNOTIFY; i++)
  18927. + if (au_cachep[i]) {
  18928. + kmem_cache_destroy(au_cachep[i]);
  18929. + au_cachep[i] = NULL;
  18930. + }
  18931. +}
  18932. +
  18933. +/* ---------------------------------------------------------------------- */
  18934. +
  18935. +int au_dir_roflags;
  18936. +
  18937. +#ifdef CONFIG_AUFS_SBILIST
  18938. +/*
  18939. + * iterate_supers_type() doesn't protect us from
  18940. + * remounting (branch management)
  18941. + */
  18942. +struct au_splhead au_sbilist;
  18943. +#endif
  18944. +
  18945. +struct lock_class_key au_lc_key[AuLcKey_Last];
  18946. +
  18947. +/*
  18948. + * functions for module interface.
  18949. + */
  18950. +MODULE_LICENSE("GPL");
  18951. +/* MODULE_LICENSE("GPL v2"); */
  18952. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  18953. +MODULE_DESCRIPTION(AUFS_NAME
  18954. + " -- Advanced multi layered unification filesystem");
  18955. +MODULE_VERSION(AUFS_VERSION);
  18956. +MODULE_ALIAS_FS(AUFS_NAME);
  18957. +
  18958. +/* this module parameter has no meaning when SYSFS is disabled */
  18959. +int sysaufs_brs = 1;
  18960. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  18961. +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
  18962. +
  18963. +/* this module parameter has no meaning when USER_NS is disabled */
  18964. +static bool au_userns;
  18965. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  18966. +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
  18967. +
  18968. +/* ---------------------------------------------------------------------- */
  18969. +
  18970. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  18971. +
  18972. +int au_seq_path(struct seq_file *seq, struct path *path)
  18973. +{
  18974. + return seq_path(seq, path, au_esc_chars);
  18975. +}
  18976. +
  18977. +/* ---------------------------------------------------------------------- */
  18978. +
  18979. +static int __init aufs_init(void)
  18980. +{
  18981. + int err, i;
  18982. + char *p;
  18983. +
  18984. + p = au_esc_chars;
  18985. + for (i = 1; i <= ' '; i++)
  18986. + *p++ = i;
  18987. + *p++ = '\\';
  18988. + *p++ = '\x7f';
  18989. + *p = 0;
  18990. +
  18991. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  18992. +
  18993. + au_sbilist_init();
  18994. + sysaufs_brs_init();
  18995. + au_debug_init();
  18996. + au_dy_init();
  18997. + err = sysaufs_init();
  18998. + if (unlikely(err))
  18999. + goto out;
  19000. + err = au_procfs_init();
  19001. + if (unlikely(err))
  19002. + goto out_sysaufs;
  19003. + err = au_wkq_init();
  19004. + if (unlikely(err))
  19005. + goto out_procfs;
  19006. + err = au_loopback_init();
  19007. + if (unlikely(err))
  19008. + goto out_wkq;
  19009. + err = au_hnotify_init();
  19010. + if (unlikely(err))
  19011. + goto out_loopback;
  19012. + err = au_sysrq_init();
  19013. + if (unlikely(err))
  19014. + goto out_hin;
  19015. + err = au_cache_init();
  19016. + if (unlikely(err))
  19017. + goto out_sysrq;
  19018. +
  19019. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  19020. + err = register_filesystem(&aufs_fs_type);
  19021. + if (unlikely(err))
  19022. + goto out_cache;
  19023. +
  19024. + /* since we define pr_fmt, call printk directly */
  19025. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  19026. + goto out; /* success */
  19027. +
  19028. +out_cache:
  19029. + au_cache_fin();
  19030. +out_sysrq:
  19031. + au_sysrq_fin();
  19032. +out_hin:
  19033. + au_hnotify_fin();
  19034. +out_loopback:
  19035. + au_loopback_fin();
  19036. +out_wkq:
  19037. + au_wkq_fin();
  19038. +out_procfs:
  19039. + au_procfs_fin();
  19040. +out_sysaufs:
  19041. + sysaufs_fin();
  19042. + au_dy_fin();
  19043. +out:
  19044. + return err;
  19045. +}
  19046. +
  19047. +static void __exit aufs_exit(void)
  19048. +{
  19049. + unregister_filesystem(&aufs_fs_type);
  19050. + au_cache_fin();
  19051. + au_sysrq_fin();
  19052. + au_hnotify_fin();
  19053. + au_loopback_fin();
  19054. + au_wkq_fin();
  19055. + au_procfs_fin();
  19056. + sysaufs_fin();
  19057. + au_dy_fin();
  19058. +}
  19059. +
  19060. +module_init(aufs_init);
  19061. +module_exit(aufs_exit);
  19062. diff -Nur linux-4.0.5.orig/fs/aufs/module.h linux-4.0.5/fs/aufs/module.h
  19063. --- linux-4.0.5.orig/fs/aufs/module.h 1969-12-31 18:00:00.000000000 -0600
  19064. +++ linux-4.0.5/fs/aufs/module.h 2015-06-21 14:07:48.686432781 -0500
  19065. @@ -0,0 +1,104 @@
  19066. +/*
  19067. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  19068. + *
  19069. + * This program, aufs is free software; you can redistribute it and/or modify
  19070. + * it under the terms of the GNU General Public License as published by
  19071. + * the Free Software Foundation; either version 2 of the License, or
  19072. + * (at your option) any later version.
  19073. + *
  19074. + * This program is distributed in the hope that it will be useful,
  19075. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19076. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19077. + * GNU General Public License for more details.
  19078. + *
  19079. + * You should have received a copy of the GNU General Public License
  19080. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19081. + */
  19082. +
  19083. +/*
  19084. + * module initialization and module-global
  19085. + */
  19086. +
  19087. +#ifndef __AUFS_MODULE_H__
  19088. +#define __AUFS_MODULE_H__
  19089. +
  19090. +#ifdef __KERNEL__
  19091. +
  19092. +#include <linux/slab.h>
  19093. +
  19094. +struct path;
  19095. +struct seq_file;
  19096. +
  19097. +/* module parameters */
  19098. +extern int sysaufs_brs;
  19099. +
  19100. +/* ---------------------------------------------------------------------- */
  19101. +
  19102. +extern int au_dir_roflags;
  19103. +
  19104. +enum {
  19105. + AuLcNonDir_FIINFO,
  19106. + AuLcNonDir_DIINFO,
  19107. + AuLcNonDir_IIINFO,
  19108. +
  19109. + AuLcDir_FIINFO,
  19110. + AuLcDir_DIINFO,
  19111. + AuLcDir_IIINFO,
  19112. +
  19113. + AuLcSymlink_DIINFO,
  19114. + AuLcSymlink_IIINFO,
  19115. +
  19116. + AuLcKey_Last
  19117. +};
  19118. +extern struct lock_class_key au_lc_key[AuLcKey_Last];
  19119. +
  19120. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
  19121. +int au_seq_path(struct seq_file *seq, struct path *path);
  19122. +
  19123. +#ifdef CONFIG_PROC_FS
  19124. +/* procfs.c */
  19125. +int __init au_procfs_init(void);
  19126. +void au_procfs_fin(void);
  19127. +#else
  19128. +AuStubInt0(au_procfs_init, void);
  19129. +AuStubVoid(au_procfs_fin, void);
  19130. +#endif
  19131. +
  19132. +/* ---------------------------------------------------------------------- */
  19133. +
  19134. +/* kmem cache */
  19135. +enum {
  19136. + AuCache_DINFO,
  19137. + AuCache_ICNTNR,
  19138. + AuCache_FINFO,
  19139. + AuCache_VDIR,
  19140. + AuCache_DEHSTR,
  19141. + AuCache_HNOTIFY, /* must be last */
  19142. + AuCache_Last
  19143. +};
  19144. +
  19145. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  19146. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  19147. +#define AuCacheCtor(type, ctor) \
  19148. + kmem_cache_create(#type, sizeof(struct type), \
  19149. + __alignof__(struct type), AuCacheFlags, ctor)
  19150. +
  19151. +extern struct kmem_cache *au_cachep[];
  19152. +
  19153. +#define AuCacheFuncs(name, index) \
  19154. +static inline struct au_##name *au_cache_alloc_##name(void) \
  19155. +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
  19156. +static inline void au_cache_free_##name(struct au_##name *p) \
  19157. +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
  19158. +
  19159. +AuCacheFuncs(dinfo, DINFO);
  19160. +AuCacheFuncs(icntnr, ICNTNR);
  19161. +AuCacheFuncs(finfo, FINFO);
  19162. +AuCacheFuncs(vdir, VDIR);
  19163. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  19164. +#ifdef CONFIG_AUFS_HNOTIFY
  19165. +AuCacheFuncs(hnotify, HNOTIFY);
  19166. +#endif
  19167. +
  19168. +#endif /* __KERNEL__ */
  19169. +#endif /* __AUFS_MODULE_H__ */
  19170. diff -Nur linux-4.0.5.orig/fs/aufs/mvdown.c linux-4.0.5/fs/aufs/mvdown.c
  19171. --- linux-4.0.5.orig/fs/aufs/mvdown.c 1969-12-31 18:00:00.000000000 -0600
  19172. +++ linux-4.0.5/fs/aufs/mvdown.c 2015-06-21 14:07:48.686432781 -0500
  19173. @@ -0,0 +1,694 @@
  19174. +/*
  19175. + * Copyright (C) 2011-2015 Junjiro R. Okajima
  19176. + *
  19177. + * This program, aufs is free software; you can redistribute it and/or modify
  19178. + * it under the terms of the GNU General Public License as published by
  19179. + * the Free Software Foundation; either version 2 of the License, or
  19180. + * (at your option) any later version.
  19181. + *
  19182. + * This program is distributed in the hope that it will be useful,
  19183. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19184. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19185. + * GNU General Public License for more details.
  19186. + *
  19187. + * You should have received a copy of the GNU General Public License
  19188. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19189. + */
  19190. +
  19191. +/*
  19192. + * move-down, opposite of copy-up
  19193. + */
  19194. +
  19195. +#include "aufs.h"
  19196. +
  19197. +struct au_mvd_args {
  19198. + struct {
  19199. + struct super_block *h_sb;
  19200. + struct dentry *h_parent;
  19201. + struct au_hinode *hdir;
  19202. + struct inode *h_dir, *h_inode;
  19203. + struct au_pin pin;
  19204. + } info[AUFS_MVDOWN_NARRAY];
  19205. +
  19206. + struct aufs_mvdown mvdown;
  19207. + struct dentry *dentry, *parent;
  19208. + struct inode *inode, *dir;
  19209. + struct super_block *sb;
  19210. + aufs_bindex_t bopq, bwh, bfound;
  19211. + unsigned char rename_lock;
  19212. +};
  19213. +
  19214. +#define mvd_errno mvdown.au_errno
  19215. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  19216. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  19217. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  19218. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  19219. +
  19220. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  19221. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  19222. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  19223. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  19224. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  19225. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  19226. +
  19227. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  19228. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  19229. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  19230. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  19231. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  19232. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  19233. +
  19234. +#define AU_MVD_PR(flag, ...) do { \
  19235. + if (flag) \
  19236. + pr_err(__VA_ARGS__); \
  19237. + } while (0)
  19238. +
  19239. +static int find_lower_writable(struct au_mvd_args *a)
  19240. +{
  19241. + struct super_block *sb;
  19242. + aufs_bindex_t bindex, bend;
  19243. + struct au_branch *br;
  19244. +
  19245. + sb = a->sb;
  19246. + bindex = a->mvd_bsrc;
  19247. + bend = au_sbend(sb);
  19248. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  19249. + for (bindex++; bindex <= bend; bindex++) {
  19250. + br = au_sbr(sb, bindex);
  19251. + if (au_br_fhsm(br->br_perm)
  19252. + && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
  19253. + return bindex;
  19254. + }
  19255. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  19256. + for (bindex++; bindex <= bend; bindex++) {
  19257. + br = au_sbr(sb, bindex);
  19258. + if (!au_br_rdonly(br))
  19259. + return bindex;
  19260. + }
  19261. + else
  19262. + for (bindex++; bindex <= bend; bindex++) {
  19263. + br = au_sbr(sb, bindex);
  19264. + if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
  19265. + if (au_br_rdonly(br))
  19266. + a->mvdown.flags
  19267. + |= AUFS_MVDOWN_ROLOWER_R;
  19268. + return bindex;
  19269. + }
  19270. + }
  19271. +
  19272. + return -1;
  19273. +}
  19274. +
  19275. +/* make the parent dir on bdst */
  19276. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  19277. +{
  19278. + int err;
  19279. +
  19280. + err = 0;
  19281. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  19282. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  19283. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  19284. + a->mvd_h_dst_parent = NULL;
  19285. + if (au_dbend(a->parent) >= a->mvd_bdst)
  19286. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  19287. + if (!a->mvd_h_dst_parent) {
  19288. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  19289. + if (unlikely(err)) {
  19290. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  19291. + goto out;
  19292. + }
  19293. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  19294. + }
  19295. +
  19296. +out:
  19297. + AuTraceErr(err);
  19298. + return err;
  19299. +}
  19300. +
  19301. +/* lock them all */
  19302. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  19303. +{
  19304. + int err;
  19305. + struct dentry *h_trap;
  19306. +
  19307. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  19308. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  19309. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  19310. + au_opt_udba(a->sb),
  19311. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  19312. + AuTraceErr(err);
  19313. + if (unlikely(err)) {
  19314. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  19315. + goto out;
  19316. + }
  19317. +
  19318. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  19319. + a->rename_lock = 0;
  19320. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  19321. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  19322. + au_opt_udba(a->sb),
  19323. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  19324. + err = au_do_pin(&a->mvd_pin_src);
  19325. + AuTraceErr(err);
  19326. + a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
  19327. + if (unlikely(err)) {
  19328. + AU_MVD_PR(dmsg, "pin_src failed\n");
  19329. + goto out_dst;
  19330. + }
  19331. + goto out; /* success */
  19332. + }
  19333. +
  19334. + a->rename_lock = 1;
  19335. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  19336. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  19337. + au_opt_udba(a->sb),
  19338. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  19339. + AuTraceErr(err);
  19340. + a->mvd_h_src_dir = a->mvd_h_src_parent->d_inode;
  19341. + if (unlikely(err)) {
  19342. + AU_MVD_PR(dmsg, "pin_src failed\n");
  19343. + au_pin_hdir_lock(&a->mvd_pin_dst);
  19344. + goto out_dst;
  19345. + }
  19346. + au_pin_hdir_unlock(&a->mvd_pin_src);
  19347. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  19348. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  19349. + if (h_trap) {
  19350. + err = (h_trap != a->mvd_h_src_parent);
  19351. + if (err)
  19352. + err = (h_trap != a->mvd_h_dst_parent);
  19353. + }
  19354. + BUG_ON(err); /* it should never happen */
  19355. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  19356. + err = -EBUSY;
  19357. + AuTraceErr(err);
  19358. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  19359. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  19360. + au_pin_hdir_lock(&a->mvd_pin_src);
  19361. + au_unpin(&a->mvd_pin_src);
  19362. + au_pin_hdir_lock(&a->mvd_pin_dst);
  19363. + goto out_dst;
  19364. + }
  19365. + goto out; /* success */
  19366. +
  19367. +out_dst:
  19368. + au_unpin(&a->mvd_pin_dst);
  19369. +out:
  19370. + AuTraceErr(err);
  19371. + return err;
  19372. +}
  19373. +
  19374. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  19375. +{
  19376. + if (!a->rename_lock)
  19377. + au_unpin(&a->mvd_pin_src);
  19378. + else {
  19379. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  19380. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  19381. + au_pin_hdir_lock(&a->mvd_pin_src);
  19382. + au_unpin(&a->mvd_pin_src);
  19383. + au_pin_hdir_lock(&a->mvd_pin_dst);
  19384. + }
  19385. + au_unpin(&a->mvd_pin_dst);
  19386. +}
  19387. +
  19388. +/* copy-down the file */
  19389. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  19390. +{
  19391. + int err;
  19392. + struct au_cp_generic cpg = {
  19393. + .dentry = a->dentry,
  19394. + .bdst = a->mvd_bdst,
  19395. + .bsrc = a->mvd_bsrc,
  19396. + .len = -1,
  19397. + .pin = &a->mvd_pin_dst,
  19398. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  19399. + };
  19400. +
  19401. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  19402. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  19403. + au_fset_cpup(cpg.flags, OVERWRITE);
  19404. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  19405. + au_fset_cpup(cpg.flags, RWDST);
  19406. + err = au_sio_cpdown_simple(&cpg);
  19407. + if (unlikely(err))
  19408. + AU_MVD_PR(dmsg, "cpdown failed\n");
  19409. +
  19410. + AuTraceErr(err);
  19411. + return err;
  19412. +}
  19413. +
  19414. +/*
  19415. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  19416. + * were sleeping
  19417. + */
  19418. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  19419. +{
  19420. + int err;
  19421. + struct path h_path;
  19422. + struct au_branch *br;
  19423. + struct inode *delegated;
  19424. +
  19425. + br = au_sbr(a->sb, a->mvd_bdst);
  19426. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  19427. + err = PTR_ERR(h_path.dentry);
  19428. + if (IS_ERR(h_path.dentry)) {
  19429. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  19430. + goto out;
  19431. + }
  19432. +
  19433. + err = 0;
  19434. + if (h_path.dentry->d_inode) {
  19435. + h_path.mnt = au_br_mnt(br);
  19436. + delegated = NULL;
  19437. + err = vfsub_unlink(a->mvd_h_dst_parent->d_inode, &h_path,
  19438. + &delegated, /*force*/0);
  19439. + if (unlikely(err == -EWOULDBLOCK)) {
  19440. + pr_warn("cannot retry for NFSv4 delegation"
  19441. + " for an internal unlink\n");
  19442. + iput(delegated);
  19443. + }
  19444. + if (unlikely(err))
  19445. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  19446. + }
  19447. + dput(h_path.dentry);
  19448. +
  19449. +out:
  19450. + AuTraceErr(err);
  19451. + return err;
  19452. +}
  19453. +
  19454. +/*
  19455. + * unlink the topmost h_dentry
  19456. + */
  19457. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  19458. +{
  19459. + int err;
  19460. + struct path h_path;
  19461. + struct inode *delegated;
  19462. +
  19463. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  19464. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  19465. + delegated = NULL;
  19466. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  19467. + if (unlikely(err == -EWOULDBLOCK)) {
  19468. + pr_warn("cannot retry for NFSv4 delegation"
  19469. + " for an internal unlink\n");
  19470. + iput(delegated);
  19471. + }
  19472. + if (unlikely(err))
  19473. + AU_MVD_PR(dmsg, "unlink failed\n");
  19474. +
  19475. + AuTraceErr(err);
  19476. + return err;
  19477. +}
  19478. +
  19479. +/* Since mvdown succeeded, we ignore an error of this function */
  19480. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  19481. +{
  19482. + int err;
  19483. + struct au_branch *br;
  19484. +
  19485. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  19486. + br = au_sbr(a->sb, a->mvd_bsrc);
  19487. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  19488. + if (!err) {
  19489. + br = au_sbr(a->sb, a->mvd_bdst);
  19490. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  19491. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  19492. + }
  19493. + if (!err)
  19494. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  19495. + else
  19496. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  19497. +}
  19498. +
  19499. +/*
  19500. + * copy-down the file and unlink the bsrc file.
  19501. + * - unlink the bdst whout if exist
  19502. + * - copy-down the file (with whtmp name and rename)
  19503. + * - unlink the bsrc file
  19504. + */
  19505. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  19506. +{
  19507. + int err;
  19508. +
  19509. + err = au_do_mkdir(dmsg, a);
  19510. + if (!err)
  19511. + err = au_do_lock(dmsg, a);
  19512. + if (unlikely(err))
  19513. + goto out;
  19514. +
  19515. + /*
  19516. + * do not revert the activities we made on bdst since they should be
  19517. + * harmless in aufs.
  19518. + */
  19519. +
  19520. + err = au_do_cpdown(dmsg, a);
  19521. + if (!err)
  19522. + err = au_do_unlink_wh(dmsg, a);
  19523. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  19524. + err = au_do_unlink(dmsg, a);
  19525. + if (unlikely(err))
  19526. + goto out_unlock;
  19527. +
  19528. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  19529. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  19530. + if (find_lower_writable(a) < 0)
  19531. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  19532. +
  19533. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  19534. + au_do_stfs(dmsg, a);
  19535. +
  19536. + /* maintain internal array */
  19537. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  19538. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  19539. + au_set_dbstart(a->dentry, a->mvd_bdst);
  19540. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  19541. + au_set_ibstart(a->inode, a->mvd_bdst);
  19542. + }
  19543. + if (au_dbend(a->dentry) < a->mvd_bdst)
  19544. + au_set_dbend(a->dentry, a->mvd_bdst);
  19545. + if (au_ibend(a->inode) < a->mvd_bdst)
  19546. + au_set_ibend(a->inode, a->mvd_bdst);
  19547. +
  19548. +out_unlock:
  19549. + au_do_unlock(dmsg, a);
  19550. +out:
  19551. + AuTraceErr(err);
  19552. + return err;
  19553. +}
  19554. +
  19555. +/* ---------------------------------------------------------------------- */
  19556. +
  19557. +/* make sure the file is idle */
  19558. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  19559. +{
  19560. + int err, plinked;
  19561. +
  19562. + err = 0;
  19563. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  19564. + if (au_dbstart(a->dentry) == a->mvd_bsrc
  19565. + && au_dcount(a->dentry) == 1
  19566. + && atomic_read(&a->inode->i_count) == 1
  19567. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  19568. + && (!plinked || !au_plink_test(a->inode))
  19569. + && a->inode->i_nlink == 1)
  19570. + goto out;
  19571. +
  19572. + err = -EBUSY;
  19573. + AU_MVD_PR(dmsg,
  19574. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  19575. + a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
  19576. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  19577. + a->mvd_h_src_inode->i_nlink,
  19578. + plinked, plinked ? au_plink_test(a->inode) : 0);
  19579. +
  19580. +out:
  19581. + AuTraceErr(err);
  19582. + return err;
  19583. +}
  19584. +
  19585. +/* make sure the parent dir is fine */
  19586. +static int au_mvd_args_parent(const unsigned char dmsg,
  19587. + struct au_mvd_args *a)
  19588. +{
  19589. + int err;
  19590. + aufs_bindex_t bindex;
  19591. +
  19592. + err = 0;
  19593. + if (unlikely(au_alive_dir(a->parent))) {
  19594. + err = -ENOENT;
  19595. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  19596. + goto out;
  19597. + }
  19598. +
  19599. + a->bopq = au_dbdiropq(a->parent);
  19600. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  19601. + AuDbg("b%d\n", bindex);
  19602. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  19603. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  19604. + err = -EINVAL;
  19605. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  19606. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  19607. + a->bopq, a->mvd_bdst);
  19608. + }
  19609. +
  19610. +out:
  19611. + AuTraceErr(err);
  19612. + return err;
  19613. +}
  19614. +
  19615. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  19616. + struct au_mvd_args *a)
  19617. +{
  19618. + int err;
  19619. + struct au_dinfo *dinfo, *tmp;
  19620. +
  19621. + /* lookup the next lower positive entry */
  19622. + err = -ENOMEM;
  19623. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  19624. + if (unlikely(!tmp))
  19625. + goto out;
  19626. +
  19627. + a->bfound = -1;
  19628. + a->bwh = -1;
  19629. + dinfo = au_di(a->dentry);
  19630. + au_di_cp(tmp, dinfo);
  19631. + au_di_swap(tmp, dinfo);
  19632. +
  19633. + /* returns the number of positive dentries */
  19634. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
  19635. + if (!err)
  19636. + a->bwh = au_dbwh(a->dentry);
  19637. + else if (err > 0)
  19638. + a->bfound = au_dbstart(a->dentry);
  19639. +
  19640. + au_di_swap(tmp, dinfo);
  19641. + au_rw_write_unlock(&tmp->di_rwsem);
  19642. + au_di_free(tmp);
  19643. + if (unlikely(err < 0))
  19644. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  19645. +
  19646. + /*
  19647. + * here, we have these cases.
  19648. + * bfound == -1
  19649. + * no positive dentry under bsrc. there are more sub-cases.
  19650. + * bwh < 0
  19651. + * there no whiteout, we can safely move-down.
  19652. + * bwh <= bsrc
  19653. + * impossible
  19654. + * bsrc < bwh && bwh < bdst
  19655. + * there is a whiteout on RO branch. cannot proceed.
  19656. + * bwh == bdst
  19657. + * there is a whiteout on the RW target branch. it should
  19658. + * be removed.
  19659. + * bdst < bwh
  19660. + * there is a whiteout somewhere unrelated branch.
  19661. + * -1 < bfound && bfound <= bsrc
  19662. + * impossible.
  19663. + * bfound < bdst
  19664. + * found, but it is on RO branch between bsrc and bdst. cannot
  19665. + * proceed.
  19666. + * bfound == bdst
  19667. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  19668. + * error.
  19669. + * bdst < bfound
  19670. + * found, after we create the file on bdst, it will be hidden.
  19671. + */
  19672. +
  19673. + AuDebugOn(a->bfound == -1
  19674. + && a->bwh != -1
  19675. + && a->bwh <= a->mvd_bsrc);
  19676. + AuDebugOn(-1 < a->bfound
  19677. + && a->bfound <= a->mvd_bsrc);
  19678. +
  19679. + err = -EINVAL;
  19680. + if (a->bfound == -1
  19681. + && a->mvd_bsrc < a->bwh
  19682. + && a->bwh != -1
  19683. + && a->bwh < a->mvd_bdst) {
  19684. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  19685. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  19686. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  19687. + goto out;
  19688. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  19689. + a->mvd_errno = EAU_MVDOWN_UPPER;
  19690. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  19691. + a->mvd_bdst, a->bfound);
  19692. + goto out;
  19693. + }
  19694. +
  19695. + err = 0; /* success */
  19696. +
  19697. +out:
  19698. + AuTraceErr(err);
  19699. + return err;
  19700. +}
  19701. +
  19702. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  19703. +{
  19704. + int err;
  19705. +
  19706. + err = 0;
  19707. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  19708. + && a->bfound == a->mvd_bdst)
  19709. + err = -EEXIST;
  19710. + AuTraceErr(err);
  19711. + return err;
  19712. +}
  19713. +
  19714. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  19715. +{
  19716. + int err;
  19717. + struct au_branch *br;
  19718. +
  19719. + err = -EISDIR;
  19720. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  19721. + goto out;
  19722. +
  19723. + err = -EINVAL;
  19724. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  19725. + a->mvd_bsrc = au_ibstart(a->inode);
  19726. + else {
  19727. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  19728. + if (unlikely(a->mvd_bsrc < 0
  19729. + || (a->mvd_bsrc < au_dbstart(a->dentry)
  19730. + || au_dbend(a->dentry) < a->mvd_bsrc
  19731. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  19732. + || (a->mvd_bsrc < au_ibstart(a->inode)
  19733. + || au_ibend(a->inode) < a->mvd_bsrc
  19734. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  19735. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  19736. + AU_MVD_PR(dmsg, "no upper\n");
  19737. + goto out;
  19738. + }
  19739. + }
  19740. + if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
  19741. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  19742. + AU_MVD_PR(dmsg, "on the bottom\n");
  19743. + goto out;
  19744. + }
  19745. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  19746. + br = au_sbr(a->sb, a->mvd_bsrc);
  19747. + err = au_br_rdonly(br);
  19748. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  19749. + if (unlikely(err))
  19750. + goto out;
  19751. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  19752. + || IS_APPEND(a->mvd_h_src_inode))) {
  19753. + if (err)
  19754. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  19755. + /* go on */
  19756. + } else
  19757. + goto out;
  19758. +
  19759. + err = -EINVAL;
  19760. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  19761. + a->mvd_bdst = find_lower_writable(a);
  19762. + if (unlikely(a->mvd_bdst < 0)) {
  19763. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  19764. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  19765. + goto out;
  19766. + }
  19767. + } else {
  19768. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  19769. + if (unlikely(a->mvd_bdst < 0
  19770. + || au_sbend(a->sb) < a->mvd_bdst)) {
  19771. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  19772. + AU_MVD_PR(dmsg, "no lower brid\n");
  19773. + goto out;
  19774. + }
  19775. + }
  19776. +
  19777. + err = au_mvd_args_busy(dmsg, a);
  19778. + if (!err)
  19779. + err = au_mvd_args_parent(dmsg, a);
  19780. + if (!err)
  19781. + err = au_mvd_args_intermediate(dmsg, a);
  19782. + if (!err)
  19783. + err = au_mvd_args_exist(dmsg, a);
  19784. + if (!err)
  19785. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  19786. +
  19787. +out:
  19788. + AuTraceErr(err);
  19789. + return err;
  19790. +}
  19791. +
  19792. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  19793. +{
  19794. + int err, e;
  19795. + unsigned char dmsg;
  19796. + struct au_mvd_args *args;
  19797. +
  19798. + err = -EPERM;
  19799. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  19800. + goto out;
  19801. +
  19802. + err = -ENOMEM;
  19803. + args = kmalloc(sizeof(*args), GFP_NOFS);
  19804. + if (unlikely(!args))
  19805. + goto out;
  19806. +
  19807. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  19808. + if (!err)
  19809. + err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
  19810. + if (unlikely(err)) {
  19811. + err = -EFAULT;
  19812. + AuTraceErr(err);
  19813. + goto out_free;
  19814. + }
  19815. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  19816. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  19817. + args->mvdown.au_errno = 0;
  19818. + args->dentry = dentry;
  19819. + args->inode = dentry->d_inode;
  19820. + args->sb = dentry->d_sb;
  19821. +
  19822. + err = -ENOENT;
  19823. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  19824. + args->parent = dget_parent(dentry);
  19825. + args->dir = args->parent->d_inode;
  19826. + mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
  19827. + dput(args->parent);
  19828. + if (unlikely(args->parent != dentry->d_parent)) {
  19829. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  19830. + goto out_dir;
  19831. + }
  19832. +
  19833. + mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
  19834. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
  19835. + if (unlikely(err))
  19836. + goto out_inode;
  19837. +
  19838. + di_write_lock_parent(args->parent);
  19839. + err = au_mvd_args(dmsg, args);
  19840. + if (unlikely(err))
  19841. + goto out_parent;
  19842. +
  19843. + err = au_do_mvdown(dmsg, args);
  19844. + if (unlikely(err))
  19845. + goto out_parent;
  19846. +
  19847. + au_cpup_attr_timesizes(args->dir);
  19848. + au_cpup_attr_timesizes(args->inode);
  19849. + au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
  19850. + /* au_digen_dec(dentry); */
  19851. +
  19852. +out_parent:
  19853. + di_write_unlock(args->parent);
  19854. + aufs_read_unlock(dentry, AuLock_DW);
  19855. +out_inode:
  19856. + mutex_unlock(&args->inode->i_mutex);
  19857. +out_dir:
  19858. + mutex_unlock(&args->dir->i_mutex);
  19859. +out_free:
  19860. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  19861. + if (unlikely(e))
  19862. + err = -EFAULT;
  19863. + kfree(args);
  19864. +out:
  19865. + AuTraceErr(err);
  19866. + return err;
  19867. +}
  19868. diff -Nur linux-4.0.5.orig/fs/aufs/opts.c linux-4.0.5/fs/aufs/opts.c
  19869. --- linux-4.0.5.orig/fs/aufs/opts.c 1969-12-31 18:00:00.000000000 -0600
  19870. +++ linux-4.0.5/fs/aufs/opts.c 2015-06-21 14:07:48.686432781 -0500
  19871. @@ -0,0 +1,1835 @@
  19872. +/*
  19873. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  19874. + *
  19875. + * This program, aufs is free software; you can redistribute it and/or modify
  19876. + * it under the terms of the GNU General Public License as published by
  19877. + * the Free Software Foundation; either version 2 of the License, or
  19878. + * (at your option) any later version.
  19879. + *
  19880. + * This program is distributed in the hope that it will be useful,
  19881. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19882. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19883. + * GNU General Public License for more details.
  19884. + *
  19885. + * You should have received a copy of the GNU General Public License
  19886. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19887. + */
  19888. +
  19889. +/*
  19890. + * mount options/flags
  19891. + */
  19892. +
  19893. +#include <linux/namei.h>
  19894. +#include <linux/types.h> /* a distribution requires */
  19895. +#include <linux/parser.h>
  19896. +#include "aufs.h"
  19897. +
  19898. +/* ---------------------------------------------------------------------- */
  19899. +
  19900. +enum {
  19901. + Opt_br,
  19902. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  19903. + Opt_idel, Opt_imod,
  19904. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  19905. + Opt_rdblk_def, Opt_rdhash_def,
  19906. + Opt_xino, Opt_noxino,
  19907. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  19908. + Opt_trunc_xino_path, Opt_itrunc_xino,
  19909. + Opt_trunc_xib, Opt_notrunc_xib,
  19910. + Opt_shwh, Opt_noshwh,
  19911. + Opt_plink, Opt_noplink, Opt_list_plink,
  19912. + Opt_udba,
  19913. + Opt_dio, Opt_nodio,
  19914. + Opt_diropq_a, Opt_diropq_w,
  19915. + Opt_warn_perm, Opt_nowarn_perm,
  19916. + Opt_wbr_copyup, Opt_wbr_create,
  19917. + Opt_fhsm_sec,
  19918. + Opt_verbose, Opt_noverbose,
  19919. + Opt_sum, Opt_nosum, Opt_wsum,
  19920. + Opt_dirperm1, Opt_nodirperm1,
  19921. + Opt_acl, Opt_noacl,
  19922. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  19923. +};
  19924. +
  19925. +static match_table_t options = {
  19926. + {Opt_br, "br=%s"},
  19927. + {Opt_br, "br:%s"},
  19928. +
  19929. + {Opt_add, "add=%d:%s"},
  19930. + {Opt_add, "add:%d:%s"},
  19931. + {Opt_add, "ins=%d:%s"},
  19932. + {Opt_add, "ins:%d:%s"},
  19933. + {Opt_append, "append=%s"},
  19934. + {Opt_append, "append:%s"},
  19935. + {Opt_prepend, "prepend=%s"},
  19936. + {Opt_prepend, "prepend:%s"},
  19937. +
  19938. + {Opt_del, "del=%s"},
  19939. + {Opt_del, "del:%s"},
  19940. + /* {Opt_idel, "idel:%d"}, */
  19941. + {Opt_mod, "mod=%s"},
  19942. + {Opt_mod, "mod:%s"},
  19943. + /* {Opt_imod, "imod:%d:%s"}, */
  19944. +
  19945. + {Opt_dirwh, "dirwh=%d"},
  19946. +
  19947. + {Opt_xino, "xino=%s"},
  19948. + {Opt_noxino, "noxino"},
  19949. + {Opt_trunc_xino, "trunc_xino"},
  19950. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  19951. + {Opt_notrunc_xino, "notrunc_xino"},
  19952. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  19953. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  19954. + /* {Opt_zxino, "zxino=%s"}, */
  19955. + {Opt_trunc_xib, "trunc_xib"},
  19956. + {Opt_notrunc_xib, "notrunc_xib"},
  19957. +
  19958. +#ifdef CONFIG_PROC_FS
  19959. + {Opt_plink, "plink"},
  19960. +#else
  19961. + {Opt_ignore_silent, "plink"},
  19962. +#endif
  19963. +
  19964. + {Opt_noplink, "noplink"},
  19965. +
  19966. +#ifdef CONFIG_AUFS_DEBUG
  19967. + {Opt_list_plink, "list_plink"},
  19968. +#endif
  19969. +
  19970. + {Opt_udba, "udba=%s"},
  19971. +
  19972. + {Opt_dio, "dio"},
  19973. + {Opt_nodio, "nodio"},
  19974. +
  19975. +#ifdef CONFIG_AUFS_FHSM
  19976. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  19977. +#else
  19978. + {Opt_ignore_silent, "fhsm_sec=%d"},
  19979. +#endif
  19980. +
  19981. + {Opt_diropq_a, "diropq=always"},
  19982. + {Opt_diropq_a, "diropq=a"},
  19983. + {Opt_diropq_w, "diropq=whiteouted"},
  19984. + {Opt_diropq_w, "diropq=w"},
  19985. +
  19986. + {Opt_warn_perm, "warn_perm"},
  19987. + {Opt_nowarn_perm, "nowarn_perm"},
  19988. +
  19989. + /* keep them temporary */
  19990. + {Opt_ignore_silent, "nodlgt"},
  19991. + {Opt_ignore_silent, "clean_plink"},
  19992. +
  19993. +#ifdef CONFIG_AUFS_SHWH
  19994. + {Opt_shwh, "shwh"},
  19995. +#endif
  19996. + {Opt_noshwh, "noshwh"},
  19997. +
  19998. + {Opt_dirperm1, "dirperm1"},
  19999. + {Opt_nodirperm1, "nodirperm1"},
  20000. +
  20001. + {Opt_verbose, "verbose"},
  20002. + {Opt_verbose, "v"},
  20003. + {Opt_noverbose, "noverbose"},
  20004. + {Opt_noverbose, "quiet"},
  20005. + {Opt_noverbose, "q"},
  20006. + {Opt_noverbose, "silent"},
  20007. +
  20008. + {Opt_sum, "sum"},
  20009. + {Opt_nosum, "nosum"},
  20010. + {Opt_wsum, "wsum"},
  20011. +
  20012. + {Opt_rdcache, "rdcache=%d"},
  20013. + {Opt_rdblk, "rdblk=%d"},
  20014. + {Opt_rdblk_def, "rdblk=def"},
  20015. + {Opt_rdhash, "rdhash=%d"},
  20016. + {Opt_rdhash_def, "rdhash=def"},
  20017. +
  20018. + {Opt_wbr_create, "create=%s"},
  20019. + {Opt_wbr_create, "create_policy=%s"},
  20020. + {Opt_wbr_copyup, "cpup=%s"},
  20021. + {Opt_wbr_copyup, "copyup=%s"},
  20022. + {Opt_wbr_copyup, "copyup_policy=%s"},
  20023. +
  20024. + /* generic VFS flag */
  20025. +#ifdef CONFIG_FS_POSIX_ACL
  20026. + {Opt_acl, "acl"},
  20027. + {Opt_noacl, "noacl"},
  20028. +#else
  20029. + {Opt_ignore_silent, "acl"},
  20030. + {Opt_ignore_silent, "noacl"},
  20031. +#endif
  20032. +
  20033. + /* internal use for the scripts */
  20034. + {Opt_ignore_silent, "si=%s"},
  20035. +
  20036. + {Opt_br, "dirs=%s"},
  20037. + {Opt_ignore, "debug=%d"},
  20038. + {Opt_ignore, "delete=whiteout"},
  20039. + {Opt_ignore, "delete=all"},
  20040. + {Opt_ignore, "imap=%s"},
  20041. +
  20042. + /* temporary workaround, due to old mount(8)? */
  20043. + {Opt_ignore_silent, "relatime"},
  20044. +
  20045. + {Opt_err, NULL}
  20046. +};
  20047. +
  20048. +/* ---------------------------------------------------------------------- */
  20049. +
  20050. +static const char *au_parser_pattern(int val, match_table_t tbl)
  20051. +{
  20052. + struct match_token *p;
  20053. +
  20054. + p = tbl;
  20055. + while (p->pattern) {
  20056. + if (p->token == val)
  20057. + return p->pattern;
  20058. + p++;
  20059. + }
  20060. + BUG();
  20061. + return "??";
  20062. +}
  20063. +
  20064. +static const char *au_optstr(int *val, match_table_t tbl)
  20065. +{
  20066. + struct match_token *p;
  20067. + int v;
  20068. +
  20069. + v = *val;
  20070. + if (!v)
  20071. + goto out;
  20072. + p = tbl;
  20073. + while (p->pattern) {
  20074. + if (p->token
  20075. + && (v & p->token) == p->token) {
  20076. + *val &= ~p->token;
  20077. + return p->pattern;
  20078. + }
  20079. + p++;
  20080. + }
  20081. +
  20082. +out:
  20083. + return NULL;
  20084. +}
  20085. +
  20086. +/* ---------------------------------------------------------------------- */
  20087. +
  20088. +static match_table_t brperm = {
  20089. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  20090. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  20091. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  20092. + {0, NULL}
  20093. +};
  20094. +
  20095. +static match_table_t brattr = {
  20096. + /* general */
  20097. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  20098. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  20099. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  20100. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  20101. +#ifdef CONFIG_AUFS_FHSM
  20102. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  20103. +#endif
  20104. +#ifdef CONFIG_AUFS_XATTR
  20105. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  20106. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  20107. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  20108. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  20109. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  20110. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  20111. +#endif
  20112. +
  20113. + /* ro/rr branch */
  20114. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  20115. +
  20116. + /* rw branch */
  20117. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  20118. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  20119. +
  20120. + {0, NULL}
  20121. +};
  20122. +
  20123. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  20124. +{
  20125. + int attr, v;
  20126. + char *p;
  20127. +
  20128. + attr = 0;
  20129. + do {
  20130. + p = strchr(str, '+');
  20131. + if (p)
  20132. + *p = 0;
  20133. + v = match_token(str, table, args);
  20134. + if (v) {
  20135. + if (v & AuBrAttr_CMOO_Mask)
  20136. + attr &= ~AuBrAttr_CMOO_Mask;
  20137. + attr |= v;
  20138. + } else {
  20139. + if (p)
  20140. + *p = '+';
  20141. + pr_warn("ignored branch attribute %s\n", str);
  20142. + break;
  20143. + }
  20144. + if (p)
  20145. + str = p + 1;
  20146. + } while (p);
  20147. +
  20148. + return attr;
  20149. +}
  20150. +
  20151. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  20152. +{
  20153. + int sz;
  20154. + const char *p;
  20155. + char *q;
  20156. +
  20157. + q = str->a;
  20158. + *q = 0;
  20159. + p = au_optstr(&perm, brattr);
  20160. + if (p) {
  20161. + sz = strlen(p);
  20162. + memcpy(q, p, sz + 1);
  20163. + q += sz;
  20164. + } else
  20165. + goto out;
  20166. +
  20167. + do {
  20168. + p = au_optstr(&perm, brattr);
  20169. + if (p) {
  20170. + *q++ = '+';
  20171. + sz = strlen(p);
  20172. + memcpy(q, p, sz + 1);
  20173. + q += sz;
  20174. + }
  20175. + } while (p);
  20176. +
  20177. +out:
  20178. + return q - str->a;
  20179. +}
  20180. +
  20181. +static int noinline_for_stack br_perm_val(char *perm)
  20182. +{
  20183. + int val, bad, sz;
  20184. + char *p;
  20185. + substring_t args[MAX_OPT_ARGS];
  20186. + au_br_perm_str_t attr;
  20187. +
  20188. + p = strchr(perm, '+');
  20189. + if (p)
  20190. + *p = 0;
  20191. + val = match_token(perm, brperm, args);
  20192. + if (!val) {
  20193. + if (p)
  20194. + *p = '+';
  20195. + pr_warn("ignored branch permission %s\n", perm);
  20196. + val = AuBrPerm_RO;
  20197. + goto out;
  20198. + }
  20199. + if (!p)
  20200. + goto out;
  20201. +
  20202. + val |= br_attr_val(p + 1, brattr, args);
  20203. +
  20204. + bad = 0;
  20205. + switch (val & AuBrPerm_Mask) {
  20206. + case AuBrPerm_RO:
  20207. + case AuBrPerm_RR:
  20208. + bad = val & AuBrWAttr_Mask;
  20209. + val &= ~AuBrWAttr_Mask;
  20210. + break;
  20211. + case AuBrPerm_RW:
  20212. + bad = val & AuBrRAttr_Mask;
  20213. + val &= ~AuBrRAttr_Mask;
  20214. + break;
  20215. + }
  20216. +
  20217. + /*
  20218. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  20219. + * does not treat it as an error, just warning.
  20220. + * this is a tiny guard for the user operation.
  20221. + */
  20222. + if (val & AuBrAttr_UNPIN) {
  20223. + bad |= AuBrAttr_UNPIN;
  20224. + val &= ~AuBrAttr_UNPIN;
  20225. + }
  20226. +
  20227. + if (unlikely(bad)) {
  20228. + sz = au_do_optstr_br_attr(&attr, bad);
  20229. + AuDebugOn(!sz);
  20230. + pr_warn("ignored branch attribute %s\n", attr.a);
  20231. + }
  20232. +
  20233. +out:
  20234. + return val;
  20235. +}
  20236. +
  20237. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  20238. +{
  20239. + au_br_perm_str_t attr;
  20240. + const char *p;
  20241. + char *q;
  20242. + int sz;
  20243. +
  20244. + q = str->a;
  20245. + p = au_optstr(&perm, brperm);
  20246. + AuDebugOn(!p || !*p);
  20247. + sz = strlen(p);
  20248. + memcpy(q, p, sz + 1);
  20249. + q += sz;
  20250. +
  20251. + sz = au_do_optstr_br_attr(&attr, perm);
  20252. + if (sz) {
  20253. + *q++ = '+';
  20254. + memcpy(q, attr.a, sz + 1);
  20255. + }
  20256. +
  20257. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  20258. +}
  20259. +
  20260. +/* ---------------------------------------------------------------------- */
  20261. +
  20262. +static match_table_t udbalevel = {
  20263. + {AuOpt_UDBA_REVAL, "reval"},
  20264. + {AuOpt_UDBA_NONE, "none"},
  20265. +#ifdef CONFIG_AUFS_HNOTIFY
  20266. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  20267. +#ifdef CONFIG_AUFS_HFSNOTIFY
  20268. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  20269. +#endif
  20270. +#endif
  20271. + {-1, NULL}
  20272. +};
  20273. +
  20274. +static int noinline_for_stack udba_val(char *str)
  20275. +{
  20276. + substring_t args[MAX_OPT_ARGS];
  20277. +
  20278. + return match_token(str, udbalevel, args);
  20279. +}
  20280. +
  20281. +const char *au_optstr_udba(int udba)
  20282. +{
  20283. + return au_parser_pattern(udba, udbalevel);
  20284. +}
  20285. +
  20286. +/* ---------------------------------------------------------------------- */
  20287. +
  20288. +static match_table_t au_wbr_create_policy = {
  20289. + {AuWbrCreate_TDP, "tdp"},
  20290. + {AuWbrCreate_TDP, "top-down-parent"},
  20291. + {AuWbrCreate_RR, "rr"},
  20292. + {AuWbrCreate_RR, "round-robin"},
  20293. + {AuWbrCreate_MFS, "mfs"},
  20294. + {AuWbrCreate_MFS, "most-free-space"},
  20295. + {AuWbrCreate_MFSV, "mfs:%d"},
  20296. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  20297. +
  20298. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  20299. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  20300. + {AuWbrCreate_PMFS, "pmfs"},
  20301. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  20302. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  20303. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  20304. +
  20305. + {-1, NULL}
  20306. +};
  20307. +
  20308. +/*
  20309. + * cf. linux/lib/parser.c and cmdline.c
  20310. + * gave up calling memparse() since it uses simple_strtoull() instead of
  20311. + * kstrto...().
  20312. + */
  20313. +static int noinline_for_stack
  20314. +au_match_ull(substring_t *s, unsigned long long *result)
  20315. +{
  20316. + int err;
  20317. + unsigned int len;
  20318. + char a[32];
  20319. +
  20320. + err = -ERANGE;
  20321. + len = s->to - s->from;
  20322. + if (len + 1 <= sizeof(a)) {
  20323. + memcpy(a, s->from, len);
  20324. + a[len] = '\0';
  20325. + err = kstrtoull(a, 0, result);
  20326. + }
  20327. + return err;
  20328. +}
  20329. +
  20330. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  20331. + struct au_opt_wbr_create *create)
  20332. +{
  20333. + int err;
  20334. + unsigned long long ull;
  20335. +
  20336. + err = 0;
  20337. + if (!au_match_ull(arg, &ull))
  20338. + create->mfsrr_watermark = ull;
  20339. + else {
  20340. + pr_err("bad integer in %s\n", str);
  20341. + err = -EINVAL;
  20342. + }
  20343. +
  20344. + return err;
  20345. +}
  20346. +
  20347. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  20348. + struct au_opt_wbr_create *create)
  20349. +{
  20350. + int n, err;
  20351. +
  20352. + err = 0;
  20353. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  20354. + create->mfs_second = n;
  20355. + else {
  20356. + pr_err("bad integer in %s\n", str);
  20357. + err = -EINVAL;
  20358. + }
  20359. +
  20360. + return err;
  20361. +}
  20362. +
  20363. +static int noinline_for_stack
  20364. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  20365. +{
  20366. + int err, e;
  20367. + substring_t args[MAX_OPT_ARGS];
  20368. +
  20369. + err = match_token(str, au_wbr_create_policy, args);
  20370. + create->wbr_create = err;
  20371. + switch (err) {
  20372. + case AuWbrCreate_MFSRRV:
  20373. + case AuWbrCreate_PMFSRRV:
  20374. + e = au_wbr_mfs_wmark(&args[0], str, create);
  20375. + if (!e)
  20376. + e = au_wbr_mfs_sec(&args[1], str, create);
  20377. + if (unlikely(e))
  20378. + err = e;
  20379. + break;
  20380. + case AuWbrCreate_MFSRR:
  20381. + case AuWbrCreate_PMFSRR:
  20382. + e = au_wbr_mfs_wmark(&args[0], str, create);
  20383. + if (unlikely(e)) {
  20384. + err = e;
  20385. + break;
  20386. + }
  20387. + /*FALLTHROUGH*/
  20388. + case AuWbrCreate_MFS:
  20389. + case AuWbrCreate_PMFS:
  20390. + create->mfs_second = AUFS_MFS_DEF_SEC;
  20391. + break;
  20392. + case AuWbrCreate_MFSV:
  20393. + case AuWbrCreate_PMFSV:
  20394. + e = au_wbr_mfs_sec(&args[0], str, create);
  20395. + if (unlikely(e))
  20396. + err = e;
  20397. + break;
  20398. + }
  20399. +
  20400. + return err;
  20401. +}
  20402. +
  20403. +const char *au_optstr_wbr_create(int wbr_create)
  20404. +{
  20405. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  20406. +}
  20407. +
  20408. +static match_table_t au_wbr_copyup_policy = {
  20409. + {AuWbrCopyup_TDP, "tdp"},
  20410. + {AuWbrCopyup_TDP, "top-down-parent"},
  20411. + {AuWbrCopyup_BUP, "bup"},
  20412. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  20413. + {AuWbrCopyup_BU, "bu"},
  20414. + {AuWbrCopyup_BU, "bottom-up"},
  20415. + {-1, NULL}
  20416. +};
  20417. +
  20418. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  20419. +{
  20420. + substring_t args[MAX_OPT_ARGS];
  20421. +
  20422. + return match_token(str, au_wbr_copyup_policy, args);
  20423. +}
  20424. +
  20425. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  20426. +{
  20427. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  20428. +}
  20429. +
  20430. +/* ---------------------------------------------------------------------- */
  20431. +
  20432. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  20433. +
  20434. +static void dump_opts(struct au_opts *opts)
  20435. +{
  20436. +#ifdef CONFIG_AUFS_DEBUG
  20437. + /* reduce stack space */
  20438. + union {
  20439. + struct au_opt_add *add;
  20440. + struct au_opt_del *del;
  20441. + struct au_opt_mod *mod;
  20442. + struct au_opt_xino *xino;
  20443. + struct au_opt_xino_itrunc *xino_itrunc;
  20444. + struct au_opt_wbr_create *create;
  20445. + } u;
  20446. + struct au_opt *opt;
  20447. +
  20448. + opt = opts->opt;
  20449. + while (opt->type != Opt_tail) {
  20450. + switch (opt->type) {
  20451. + case Opt_add:
  20452. + u.add = &opt->add;
  20453. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  20454. + u.add->bindex, u.add->pathname, u.add->perm,
  20455. + u.add->path.dentry);
  20456. + break;
  20457. + case Opt_del:
  20458. + case Opt_idel:
  20459. + u.del = &opt->del;
  20460. + AuDbg("del {%s, %p}\n",
  20461. + u.del->pathname, u.del->h_path.dentry);
  20462. + break;
  20463. + case Opt_mod:
  20464. + case Opt_imod:
  20465. + u.mod = &opt->mod;
  20466. + AuDbg("mod {%s, 0x%x, %p}\n",
  20467. + u.mod->path, u.mod->perm, u.mod->h_root);
  20468. + break;
  20469. + case Opt_append:
  20470. + u.add = &opt->add;
  20471. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  20472. + u.add->bindex, u.add->pathname, u.add->perm,
  20473. + u.add->path.dentry);
  20474. + break;
  20475. + case Opt_prepend:
  20476. + u.add = &opt->add;
  20477. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  20478. + u.add->bindex, u.add->pathname, u.add->perm,
  20479. + u.add->path.dentry);
  20480. + break;
  20481. + case Opt_dirwh:
  20482. + AuDbg("dirwh %d\n", opt->dirwh);
  20483. + break;
  20484. + case Opt_rdcache:
  20485. + AuDbg("rdcache %d\n", opt->rdcache);
  20486. + break;
  20487. + case Opt_rdblk:
  20488. + AuDbg("rdblk %u\n", opt->rdblk);
  20489. + break;
  20490. + case Opt_rdblk_def:
  20491. + AuDbg("rdblk_def\n");
  20492. + break;
  20493. + case Opt_rdhash:
  20494. + AuDbg("rdhash %u\n", opt->rdhash);
  20495. + break;
  20496. + case Opt_rdhash_def:
  20497. + AuDbg("rdhash_def\n");
  20498. + break;
  20499. + case Opt_xino:
  20500. + u.xino = &opt->xino;
  20501. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  20502. + break;
  20503. + case Opt_trunc_xino:
  20504. + AuLabel(trunc_xino);
  20505. + break;
  20506. + case Opt_notrunc_xino:
  20507. + AuLabel(notrunc_xino);
  20508. + break;
  20509. + case Opt_trunc_xino_path:
  20510. + case Opt_itrunc_xino:
  20511. + u.xino_itrunc = &opt->xino_itrunc;
  20512. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  20513. + break;
  20514. + case Opt_noxino:
  20515. + AuLabel(noxino);
  20516. + break;
  20517. + case Opt_trunc_xib:
  20518. + AuLabel(trunc_xib);
  20519. + break;
  20520. + case Opt_notrunc_xib:
  20521. + AuLabel(notrunc_xib);
  20522. + break;
  20523. + case Opt_shwh:
  20524. + AuLabel(shwh);
  20525. + break;
  20526. + case Opt_noshwh:
  20527. + AuLabel(noshwh);
  20528. + break;
  20529. + case Opt_dirperm1:
  20530. + AuLabel(dirperm1);
  20531. + break;
  20532. + case Opt_nodirperm1:
  20533. + AuLabel(nodirperm1);
  20534. + break;
  20535. + case Opt_plink:
  20536. + AuLabel(plink);
  20537. + break;
  20538. + case Opt_noplink:
  20539. + AuLabel(noplink);
  20540. + break;
  20541. + case Opt_list_plink:
  20542. + AuLabel(list_plink);
  20543. + break;
  20544. + case Opt_udba:
  20545. + AuDbg("udba %d, %s\n",
  20546. + opt->udba, au_optstr_udba(opt->udba));
  20547. + break;
  20548. + case Opt_dio:
  20549. + AuLabel(dio);
  20550. + break;
  20551. + case Opt_nodio:
  20552. + AuLabel(nodio);
  20553. + break;
  20554. + case Opt_diropq_a:
  20555. + AuLabel(diropq_a);
  20556. + break;
  20557. + case Opt_diropq_w:
  20558. + AuLabel(diropq_w);
  20559. + break;
  20560. + case Opt_warn_perm:
  20561. + AuLabel(warn_perm);
  20562. + break;
  20563. + case Opt_nowarn_perm:
  20564. + AuLabel(nowarn_perm);
  20565. + break;
  20566. + case Opt_verbose:
  20567. + AuLabel(verbose);
  20568. + break;
  20569. + case Opt_noverbose:
  20570. + AuLabel(noverbose);
  20571. + break;
  20572. + case Opt_sum:
  20573. + AuLabel(sum);
  20574. + break;
  20575. + case Opt_nosum:
  20576. + AuLabel(nosum);
  20577. + break;
  20578. + case Opt_wsum:
  20579. + AuLabel(wsum);
  20580. + break;
  20581. + case Opt_wbr_create:
  20582. + u.create = &opt->wbr_create;
  20583. + AuDbg("create %d, %s\n", u.create->wbr_create,
  20584. + au_optstr_wbr_create(u.create->wbr_create));
  20585. + switch (u.create->wbr_create) {
  20586. + case AuWbrCreate_MFSV:
  20587. + case AuWbrCreate_PMFSV:
  20588. + AuDbg("%d sec\n", u.create->mfs_second);
  20589. + break;
  20590. + case AuWbrCreate_MFSRR:
  20591. + AuDbg("%llu watermark\n",
  20592. + u.create->mfsrr_watermark);
  20593. + break;
  20594. + case AuWbrCreate_MFSRRV:
  20595. + case AuWbrCreate_PMFSRRV:
  20596. + AuDbg("%llu watermark, %d sec\n",
  20597. + u.create->mfsrr_watermark,
  20598. + u.create->mfs_second);
  20599. + break;
  20600. + }
  20601. + break;
  20602. + case Opt_wbr_copyup:
  20603. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  20604. + au_optstr_wbr_copyup(opt->wbr_copyup));
  20605. + break;
  20606. + case Opt_fhsm_sec:
  20607. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  20608. + break;
  20609. + case Opt_acl:
  20610. + AuLabel(acl);
  20611. + break;
  20612. + case Opt_noacl:
  20613. + AuLabel(noacl);
  20614. + break;
  20615. + default:
  20616. + BUG();
  20617. + }
  20618. + opt++;
  20619. + }
  20620. +#endif
  20621. +}
  20622. +
  20623. +void au_opts_free(struct au_opts *opts)
  20624. +{
  20625. + struct au_opt *opt;
  20626. +
  20627. + opt = opts->opt;
  20628. + while (opt->type != Opt_tail) {
  20629. + switch (opt->type) {
  20630. + case Opt_add:
  20631. + case Opt_append:
  20632. + case Opt_prepend:
  20633. + path_put(&opt->add.path);
  20634. + break;
  20635. + case Opt_del:
  20636. + case Opt_idel:
  20637. + path_put(&opt->del.h_path);
  20638. + break;
  20639. + case Opt_mod:
  20640. + case Opt_imod:
  20641. + dput(opt->mod.h_root);
  20642. + break;
  20643. + case Opt_xino:
  20644. + fput(opt->xino.file);
  20645. + break;
  20646. + }
  20647. + opt++;
  20648. + }
  20649. +}
  20650. +
  20651. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  20652. + aufs_bindex_t bindex)
  20653. +{
  20654. + int err;
  20655. + struct au_opt_add *add = &opt->add;
  20656. + char *p;
  20657. +
  20658. + add->bindex = bindex;
  20659. + add->perm = AuBrPerm_RO;
  20660. + add->pathname = opt_str;
  20661. + p = strchr(opt_str, '=');
  20662. + if (p) {
  20663. + *p++ = 0;
  20664. + if (*p)
  20665. + add->perm = br_perm_val(p);
  20666. + }
  20667. +
  20668. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  20669. + if (!err) {
  20670. + if (!p) {
  20671. + add->perm = AuBrPerm_RO;
  20672. + if (au_test_fs_rr(add->path.dentry->d_sb))
  20673. + add->perm = AuBrPerm_RR;
  20674. + else if (!bindex && !(sb_flags & MS_RDONLY))
  20675. + add->perm = AuBrPerm_RW;
  20676. + }
  20677. + opt->type = Opt_add;
  20678. + goto out;
  20679. + }
  20680. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  20681. + err = -EINVAL;
  20682. +
  20683. +out:
  20684. + return err;
  20685. +}
  20686. +
  20687. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  20688. +{
  20689. + int err;
  20690. +
  20691. + del->pathname = args[0].from;
  20692. + AuDbg("del path %s\n", del->pathname);
  20693. +
  20694. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  20695. + if (unlikely(err))
  20696. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  20697. +
  20698. + return err;
  20699. +}
  20700. +
  20701. +#if 0 /* reserved for future use */
  20702. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  20703. + struct au_opt_del *del, substring_t args[])
  20704. +{
  20705. + int err;
  20706. + struct dentry *root;
  20707. +
  20708. + err = -EINVAL;
  20709. + root = sb->s_root;
  20710. + aufs_read_lock(root, AuLock_FLUSH);
  20711. + if (bindex < 0 || au_sbend(sb) < bindex) {
  20712. + pr_err("out of bounds, %d\n", bindex);
  20713. + goto out;
  20714. + }
  20715. +
  20716. + err = 0;
  20717. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  20718. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  20719. +
  20720. +out:
  20721. + aufs_read_unlock(root, !AuLock_IR);
  20722. + return err;
  20723. +}
  20724. +#endif
  20725. +
  20726. +static int noinline_for_stack
  20727. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  20728. +{
  20729. + int err;
  20730. + struct path path;
  20731. + char *p;
  20732. +
  20733. + err = -EINVAL;
  20734. + mod->path = args[0].from;
  20735. + p = strchr(mod->path, '=');
  20736. + if (unlikely(!p)) {
  20737. + pr_err("no permssion %s\n", args[0].from);
  20738. + goto out;
  20739. + }
  20740. +
  20741. + *p++ = 0;
  20742. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  20743. + if (unlikely(err)) {
  20744. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  20745. + goto out;
  20746. + }
  20747. +
  20748. + mod->perm = br_perm_val(p);
  20749. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  20750. + mod->h_root = dget(path.dentry);
  20751. + path_put(&path);
  20752. +
  20753. +out:
  20754. + return err;
  20755. +}
  20756. +
  20757. +#if 0 /* reserved for future use */
  20758. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  20759. + struct au_opt_mod *mod, substring_t args[])
  20760. +{
  20761. + int err;
  20762. + struct dentry *root;
  20763. +
  20764. + err = -EINVAL;
  20765. + root = sb->s_root;
  20766. + aufs_read_lock(root, AuLock_FLUSH);
  20767. + if (bindex < 0 || au_sbend(sb) < bindex) {
  20768. + pr_err("out of bounds, %d\n", bindex);
  20769. + goto out;
  20770. + }
  20771. +
  20772. + err = 0;
  20773. + mod->perm = br_perm_val(args[1].from);
  20774. + AuDbg("mod path %s, perm 0x%x, %s\n",
  20775. + mod->path, mod->perm, args[1].from);
  20776. + mod->h_root = dget(au_h_dptr(root, bindex));
  20777. +
  20778. +out:
  20779. + aufs_read_unlock(root, !AuLock_IR);
  20780. + return err;
  20781. +}
  20782. +#endif
  20783. +
  20784. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  20785. + substring_t args[])
  20786. +{
  20787. + int err;
  20788. + struct file *file;
  20789. +
  20790. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  20791. + err = PTR_ERR(file);
  20792. + if (IS_ERR(file))
  20793. + goto out;
  20794. +
  20795. + err = -EINVAL;
  20796. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  20797. + fput(file);
  20798. + pr_err("%s must be outside\n", args[0].from);
  20799. + goto out;
  20800. + }
  20801. +
  20802. + err = 0;
  20803. + xino->file = file;
  20804. + xino->path = args[0].from;
  20805. +
  20806. +out:
  20807. + return err;
  20808. +}
  20809. +
  20810. +static int noinline_for_stack
  20811. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  20812. + struct au_opt_xino_itrunc *xino_itrunc,
  20813. + substring_t args[])
  20814. +{
  20815. + int err;
  20816. + aufs_bindex_t bend, bindex;
  20817. + struct path path;
  20818. + struct dentry *root;
  20819. +
  20820. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  20821. + if (unlikely(err)) {
  20822. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  20823. + goto out;
  20824. + }
  20825. +
  20826. + xino_itrunc->bindex = -1;
  20827. + root = sb->s_root;
  20828. + aufs_read_lock(root, AuLock_FLUSH);
  20829. + bend = au_sbend(sb);
  20830. + for (bindex = 0; bindex <= bend; bindex++) {
  20831. + if (au_h_dptr(root, bindex) == path.dentry) {
  20832. + xino_itrunc->bindex = bindex;
  20833. + break;
  20834. + }
  20835. + }
  20836. + aufs_read_unlock(root, !AuLock_IR);
  20837. + path_put(&path);
  20838. +
  20839. + if (unlikely(xino_itrunc->bindex < 0)) {
  20840. + pr_err("no such branch %s\n", args[0].from);
  20841. + err = -EINVAL;
  20842. + }
  20843. +
  20844. +out:
  20845. + return err;
  20846. +}
  20847. +
  20848. +/* called without aufs lock */
  20849. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  20850. +{
  20851. + int err, n, token;
  20852. + aufs_bindex_t bindex;
  20853. + unsigned char skipped;
  20854. + struct dentry *root;
  20855. + struct au_opt *opt, *opt_tail;
  20856. + char *opt_str;
  20857. + /* reduce the stack space */
  20858. + union {
  20859. + struct au_opt_xino_itrunc *xino_itrunc;
  20860. + struct au_opt_wbr_create *create;
  20861. + } u;
  20862. + struct {
  20863. + substring_t args[MAX_OPT_ARGS];
  20864. + } *a;
  20865. +
  20866. + err = -ENOMEM;
  20867. + a = kmalloc(sizeof(*a), GFP_NOFS);
  20868. + if (unlikely(!a))
  20869. + goto out;
  20870. +
  20871. + root = sb->s_root;
  20872. + err = 0;
  20873. + bindex = 0;
  20874. + opt = opts->opt;
  20875. + opt_tail = opt + opts->max_opt - 1;
  20876. + opt->type = Opt_tail;
  20877. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  20878. + err = -EINVAL;
  20879. + skipped = 0;
  20880. + token = match_token(opt_str, options, a->args);
  20881. + switch (token) {
  20882. + case Opt_br:
  20883. + err = 0;
  20884. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  20885. + && *opt_str) {
  20886. + err = opt_add(opt, opt_str, opts->sb_flags,
  20887. + bindex++);
  20888. + if (unlikely(!err && ++opt > opt_tail)) {
  20889. + err = -E2BIG;
  20890. + break;
  20891. + }
  20892. + opt->type = Opt_tail;
  20893. + skipped = 1;
  20894. + }
  20895. + break;
  20896. + case Opt_add:
  20897. + if (unlikely(match_int(&a->args[0], &n))) {
  20898. + pr_err("bad integer in %s\n", opt_str);
  20899. + break;
  20900. + }
  20901. + bindex = n;
  20902. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  20903. + bindex);
  20904. + if (!err)
  20905. + opt->type = token;
  20906. + break;
  20907. + case Opt_append:
  20908. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  20909. + /*dummy bindex*/1);
  20910. + if (!err)
  20911. + opt->type = token;
  20912. + break;
  20913. + case Opt_prepend:
  20914. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  20915. + /*bindex*/0);
  20916. + if (!err)
  20917. + opt->type = token;
  20918. + break;
  20919. + case Opt_del:
  20920. + err = au_opts_parse_del(&opt->del, a->args);
  20921. + if (!err)
  20922. + opt->type = token;
  20923. + break;
  20924. +#if 0 /* reserved for future use */
  20925. + case Opt_idel:
  20926. + del->pathname = "(indexed)";
  20927. + if (unlikely(match_int(&args[0], &n))) {
  20928. + pr_err("bad integer in %s\n", opt_str);
  20929. + break;
  20930. + }
  20931. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  20932. + if (!err)
  20933. + opt->type = token;
  20934. + break;
  20935. +#endif
  20936. + case Opt_mod:
  20937. + err = au_opts_parse_mod(&opt->mod, a->args);
  20938. + if (!err)
  20939. + opt->type = token;
  20940. + break;
  20941. +#ifdef IMOD /* reserved for future use */
  20942. + case Opt_imod:
  20943. + u.mod->path = "(indexed)";
  20944. + if (unlikely(match_int(&a->args[0], &n))) {
  20945. + pr_err("bad integer in %s\n", opt_str);
  20946. + break;
  20947. + }
  20948. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  20949. + if (!err)
  20950. + opt->type = token;
  20951. + break;
  20952. +#endif
  20953. + case Opt_xino:
  20954. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  20955. + if (!err)
  20956. + opt->type = token;
  20957. + break;
  20958. +
  20959. + case Opt_trunc_xino_path:
  20960. + err = au_opts_parse_xino_itrunc_path
  20961. + (sb, &opt->xino_itrunc, a->args);
  20962. + if (!err)
  20963. + opt->type = token;
  20964. + break;
  20965. +
  20966. + case Opt_itrunc_xino:
  20967. + u.xino_itrunc = &opt->xino_itrunc;
  20968. + if (unlikely(match_int(&a->args[0], &n))) {
  20969. + pr_err("bad integer in %s\n", opt_str);
  20970. + break;
  20971. + }
  20972. + u.xino_itrunc->bindex = n;
  20973. + aufs_read_lock(root, AuLock_FLUSH);
  20974. + if (n < 0 || au_sbend(sb) < n) {
  20975. + pr_err("out of bounds, %d\n", n);
  20976. + aufs_read_unlock(root, !AuLock_IR);
  20977. + break;
  20978. + }
  20979. + aufs_read_unlock(root, !AuLock_IR);
  20980. + err = 0;
  20981. + opt->type = token;
  20982. + break;
  20983. +
  20984. + case Opt_dirwh:
  20985. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  20986. + break;
  20987. + err = 0;
  20988. + opt->type = token;
  20989. + break;
  20990. +
  20991. + case Opt_rdcache:
  20992. + if (unlikely(match_int(&a->args[0], &n))) {
  20993. + pr_err("bad integer in %s\n", opt_str);
  20994. + break;
  20995. + }
  20996. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  20997. + pr_err("rdcache must be smaller than %d\n",
  20998. + AUFS_RDCACHE_MAX);
  20999. + break;
  21000. + }
  21001. + opt->rdcache = n;
  21002. + err = 0;
  21003. + opt->type = token;
  21004. + break;
  21005. + case Opt_rdblk:
  21006. + if (unlikely(match_int(&a->args[0], &n)
  21007. + || n < 0
  21008. + || n > KMALLOC_MAX_SIZE)) {
  21009. + pr_err("bad integer in %s\n", opt_str);
  21010. + break;
  21011. + }
  21012. + if (unlikely(n && n < NAME_MAX)) {
  21013. + pr_err("rdblk must be larger than %d\n",
  21014. + NAME_MAX);
  21015. + break;
  21016. + }
  21017. + opt->rdblk = n;
  21018. + err = 0;
  21019. + opt->type = token;
  21020. + break;
  21021. + case Opt_rdhash:
  21022. + if (unlikely(match_int(&a->args[0], &n)
  21023. + || n < 0
  21024. + || n * sizeof(struct hlist_head)
  21025. + > KMALLOC_MAX_SIZE)) {
  21026. + pr_err("bad integer in %s\n", opt_str);
  21027. + break;
  21028. + }
  21029. + opt->rdhash = n;
  21030. + err = 0;
  21031. + opt->type = token;
  21032. + break;
  21033. +
  21034. + case Opt_trunc_xino:
  21035. + case Opt_notrunc_xino:
  21036. + case Opt_noxino:
  21037. + case Opt_trunc_xib:
  21038. + case Opt_notrunc_xib:
  21039. + case Opt_shwh:
  21040. + case Opt_noshwh:
  21041. + case Opt_dirperm1:
  21042. + case Opt_nodirperm1:
  21043. + case Opt_plink:
  21044. + case Opt_noplink:
  21045. + case Opt_list_plink:
  21046. + case Opt_dio:
  21047. + case Opt_nodio:
  21048. + case Opt_diropq_a:
  21049. + case Opt_diropq_w:
  21050. + case Opt_warn_perm:
  21051. + case Opt_nowarn_perm:
  21052. + case Opt_verbose:
  21053. + case Opt_noverbose:
  21054. + case Opt_sum:
  21055. + case Opt_nosum:
  21056. + case Opt_wsum:
  21057. + case Opt_rdblk_def:
  21058. + case Opt_rdhash_def:
  21059. + case Opt_acl:
  21060. + case Opt_noacl:
  21061. + err = 0;
  21062. + opt->type = token;
  21063. + break;
  21064. +
  21065. + case Opt_udba:
  21066. + opt->udba = udba_val(a->args[0].from);
  21067. + if (opt->udba >= 0) {
  21068. + err = 0;
  21069. + opt->type = token;
  21070. + } else
  21071. + pr_err("wrong value, %s\n", opt_str);
  21072. + break;
  21073. +
  21074. + case Opt_wbr_create:
  21075. + u.create = &opt->wbr_create;
  21076. + u.create->wbr_create
  21077. + = au_wbr_create_val(a->args[0].from, u.create);
  21078. + if (u.create->wbr_create >= 0) {
  21079. + err = 0;
  21080. + opt->type = token;
  21081. + } else
  21082. + pr_err("wrong value, %s\n", opt_str);
  21083. + break;
  21084. + case Opt_wbr_copyup:
  21085. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  21086. + if (opt->wbr_copyup >= 0) {
  21087. + err = 0;
  21088. + opt->type = token;
  21089. + } else
  21090. + pr_err("wrong value, %s\n", opt_str);
  21091. + break;
  21092. +
  21093. + case Opt_fhsm_sec:
  21094. + if (unlikely(match_int(&a->args[0], &n)
  21095. + || n < 0)) {
  21096. + pr_err("bad integer in %s\n", opt_str);
  21097. + break;
  21098. + }
  21099. + if (sysaufs_brs) {
  21100. + opt->fhsm_second = n;
  21101. + opt->type = token;
  21102. + } else
  21103. + pr_warn("ignored %s\n", opt_str);
  21104. + err = 0;
  21105. + break;
  21106. +
  21107. + case Opt_ignore:
  21108. + pr_warn("ignored %s\n", opt_str);
  21109. + /*FALLTHROUGH*/
  21110. + case Opt_ignore_silent:
  21111. + skipped = 1;
  21112. + err = 0;
  21113. + break;
  21114. + case Opt_err:
  21115. + pr_err("unknown option %s\n", opt_str);
  21116. + break;
  21117. + }
  21118. +
  21119. + if (!err && !skipped) {
  21120. + if (unlikely(++opt > opt_tail)) {
  21121. + err = -E2BIG;
  21122. + opt--;
  21123. + opt->type = Opt_tail;
  21124. + break;
  21125. + }
  21126. + opt->type = Opt_tail;
  21127. + }
  21128. + }
  21129. +
  21130. + kfree(a);
  21131. + dump_opts(opts);
  21132. + if (unlikely(err))
  21133. + au_opts_free(opts);
  21134. +
  21135. +out:
  21136. + return err;
  21137. +}
  21138. +
  21139. +static int au_opt_wbr_create(struct super_block *sb,
  21140. + struct au_opt_wbr_create *create)
  21141. +{
  21142. + int err;
  21143. + struct au_sbinfo *sbinfo;
  21144. +
  21145. + SiMustWriteLock(sb);
  21146. +
  21147. + err = 1; /* handled */
  21148. + sbinfo = au_sbi(sb);
  21149. + if (sbinfo->si_wbr_create_ops->fin) {
  21150. + err = sbinfo->si_wbr_create_ops->fin(sb);
  21151. + if (!err)
  21152. + err = 1;
  21153. + }
  21154. +
  21155. + sbinfo->si_wbr_create = create->wbr_create;
  21156. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  21157. + switch (create->wbr_create) {
  21158. + case AuWbrCreate_MFSRRV:
  21159. + case AuWbrCreate_MFSRR:
  21160. + case AuWbrCreate_PMFSRR:
  21161. + case AuWbrCreate_PMFSRRV:
  21162. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  21163. + /*FALLTHROUGH*/
  21164. + case AuWbrCreate_MFS:
  21165. + case AuWbrCreate_MFSV:
  21166. + case AuWbrCreate_PMFS:
  21167. + case AuWbrCreate_PMFSV:
  21168. + sbinfo->si_wbr_mfs.mfs_expire
  21169. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  21170. + break;
  21171. + }
  21172. +
  21173. + if (sbinfo->si_wbr_create_ops->init)
  21174. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  21175. +
  21176. + return err;
  21177. +}
  21178. +
  21179. +/*
  21180. + * returns,
  21181. + * plus: processed without an error
  21182. + * zero: unprocessed
  21183. + */
  21184. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  21185. + struct au_opts *opts)
  21186. +{
  21187. + int err;
  21188. + struct au_sbinfo *sbinfo;
  21189. +
  21190. + SiMustWriteLock(sb);
  21191. +
  21192. + err = 1; /* handled */
  21193. + sbinfo = au_sbi(sb);
  21194. + switch (opt->type) {
  21195. + case Opt_udba:
  21196. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  21197. + sbinfo->si_mntflags |= opt->udba;
  21198. + opts->given_udba |= opt->udba;
  21199. + break;
  21200. +
  21201. + case Opt_plink:
  21202. + au_opt_set(sbinfo->si_mntflags, PLINK);
  21203. + break;
  21204. + case Opt_noplink:
  21205. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  21206. + au_plink_put(sb, /*verbose*/1);
  21207. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  21208. + break;
  21209. + case Opt_list_plink:
  21210. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  21211. + au_plink_list(sb);
  21212. + break;
  21213. +
  21214. + case Opt_dio:
  21215. + au_opt_set(sbinfo->si_mntflags, DIO);
  21216. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  21217. + break;
  21218. + case Opt_nodio:
  21219. + au_opt_clr(sbinfo->si_mntflags, DIO);
  21220. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  21221. + break;
  21222. +
  21223. + case Opt_fhsm_sec:
  21224. + au_fhsm_set(sbinfo, opt->fhsm_second);
  21225. + break;
  21226. +
  21227. + case Opt_diropq_a:
  21228. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  21229. + break;
  21230. + case Opt_diropq_w:
  21231. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  21232. + break;
  21233. +
  21234. + case Opt_warn_perm:
  21235. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  21236. + break;
  21237. + case Opt_nowarn_perm:
  21238. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  21239. + break;
  21240. +
  21241. + case Opt_verbose:
  21242. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  21243. + break;
  21244. + case Opt_noverbose:
  21245. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  21246. + break;
  21247. +
  21248. + case Opt_sum:
  21249. + au_opt_set(sbinfo->si_mntflags, SUM);
  21250. + break;
  21251. + case Opt_wsum:
  21252. + au_opt_clr(sbinfo->si_mntflags, SUM);
  21253. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  21254. + case Opt_nosum:
  21255. + au_opt_clr(sbinfo->si_mntflags, SUM);
  21256. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  21257. + break;
  21258. +
  21259. + case Opt_wbr_create:
  21260. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  21261. + break;
  21262. + case Opt_wbr_copyup:
  21263. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  21264. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  21265. + break;
  21266. +
  21267. + case Opt_dirwh:
  21268. + sbinfo->si_dirwh = opt->dirwh;
  21269. + break;
  21270. +
  21271. + case Opt_rdcache:
  21272. + sbinfo->si_rdcache
  21273. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  21274. + break;
  21275. + case Opt_rdblk:
  21276. + sbinfo->si_rdblk = opt->rdblk;
  21277. + break;
  21278. + case Opt_rdblk_def:
  21279. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  21280. + break;
  21281. + case Opt_rdhash:
  21282. + sbinfo->si_rdhash = opt->rdhash;
  21283. + break;
  21284. + case Opt_rdhash_def:
  21285. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  21286. + break;
  21287. +
  21288. + case Opt_shwh:
  21289. + au_opt_set(sbinfo->si_mntflags, SHWH);
  21290. + break;
  21291. + case Opt_noshwh:
  21292. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  21293. + break;
  21294. +
  21295. + case Opt_dirperm1:
  21296. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  21297. + break;
  21298. + case Opt_nodirperm1:
  21299. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  21300. + break;
  21301. +
  21302. + case Opt_trunc_xino:
  21303. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  21304. + break;
  21305. + case Opt_notrunc_xino:
  21306. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  21307. + break;
  21308. +
  21309. + case Opt_trunc_xino_path:
  21310. + case Opt_itrunc_xino:
  21311. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
  21312. + if (!err)
  21313. + err = 1;
  21314. + break;
  21315. +
  21316. + case Opt_trunc_xib:
  21317. + au_fset_opts(opts->flags, TRUNC_XIB);
  21318. + break;
  21319. + case Opt_notrunc_xib:
  21320. + au_fclr_opts(opts->flags, TRUNC_XIB);
  21321. + break;
  21322. +
  21323. + case Opt_acl:
  21324. + sb->s_flags |= MS_POSIXACL;
  21325. + break;
  21326. + case Opt_noacl:
  21327. + sb->s_flags &= ~MS_POSIXACL;
  21328. + break;
  21329. +
  21330. + default:
  21331. + err = 0;
  21332. + break;
  21333. + }
  21334. +
  21335. + return err;
  21336. +}
  21337. +
  21338. +/*
  21339. + * returns tri-state.
  21340. + * plus: processed without an error
  21341. + * zero: unprocessed
  21342. + * minus: error
  21343. + */
  21344. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  21345. + struct au_opts *opts)
  21346. +{
  21347. + int err, do_refresh;
  21348. +
  21349. + err = 0;
  21350. + switch (opt->type) {
  21351. + case Opt_append:
  21352. + opt->add.bindex = au_sbend(sb) + 1;
  21353. + if (opt->add.bindex < 0)
  21354. + opt->add.bindex = 0;
  21355. + goto add;
  21356. + case Opt_prepend:
  21357. + opt->add.bindex = 0;
  21358. + add: /* indented label */
  21359. + case Opt_add:
  21360. + err = au_br_add(sb, &opt->add,
  21361. + au_ftest_opts(opts->flags, REMOUNT));
  21362. + if (!err) {
  21363. + err = 1;
  21364. + au_fset_opts(opts->flags, REFRESH);
  21365. + }
  21366. + break;
  21367. +
  21368. + case Opt_del:
  21369. + case Opt_idel:
  21370. + err = au_br_del(sb, &opt->del,
  21371. + au_ftest_opts(opts->flags, REMOUNT));
  21372. + if (!err) {
  21373. + err = 1;
  21374. + au_fset_opts(opts->flags, TRUNC_XIB);
  21375. + au_fset_opts(opts->flags, REFRESH);
  21376. + }
  21377. + break;
  21378. +
  21379. + case Opt_mod:
  21380. + case Opt_imod:
  21381. + err = au_br_mod(sb, &opt->mod,
  21382. + au_ftest_opts(opts->flags, REMOUNT),
  21383. + &do_refresh);
  21384. + if (!err) {
  21385. + err = 1;
  21386. + if (do_refresh)
  21387. + au_fset_opts(opts->flags, REFRESH);
  21388. + }
  21389. + break;
  21390. + }
  21391. +
  21392. + return err;
  21393. +}
  21394. +
  21395. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  21396. + struct au_opt_xino **opt_xino,
  21397. + struct au_opts *opts)
  21398. +{
  21399. + int err;
  21400. + aufs_bindex_t bend, bindex;
  21401. + struct dentry *root, *parent, *h_root;
  21402. +
  21403. + err = 0;
  21404. + switch (opt->type) {
  21405. + case Opt_xino:
  21406. + err = au_xino_set(sb, &opt->xino,
  21407. + !!au_ftest_opts(opts->flags, REMOUNT));
  21408. + if (unlikely(err))
  21409. + break;
  21410. +
  21411. + *opt_xino = &opt->xino;
  21412. + au_xino_brid_set(sb, -1);
  21413. +
  21414. + /* safe d_parent access */
  21415. + parent = opt->xino.file->f_path.dentry->d_parent;
  21416. + root = sb->s_root;
  21417. + bend = au_sbend(sb);
  21418. + for (bindex = 0; bindex <= bend; bindex++) {
  21419. + h_root = au_h_dptr(root, bindex);
  21420. + if (h_root == parent) {
  21421. + au_xino_brid_set(sb, au_sbr_id(sb, bindex));
  21422. + break;
  21423. + }
  21424. + }
  21425. + break;
  21426. +
  21427. + case Opt_noxino:
  21428. + au_xino_clr(sb);
  21429. + au_xino_brid_set(sb, -1);
  21430. + *opt_xino = (void *)-1;
  21431. + break;
  21432. + }
  21433. +
  21434. + return err;
  21435. +}
  21436. +
  21437. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  21438. + unsigned int pending)
  21439. +{
  21440. + int err, fhsm;
  21441. + aufs_bindex_t bindex, bend;
  21442. + unsigned char do_plink, skip, do_free;
  21443. + struct au_branch *br;
  21444. + struct au_wbr *wbr;
  21445. + struct dentry *root;
  21446. + struct inode *dir, *h_dir;
  21447. + struct au_sbinfo *sbinfo;
  21448. + struct au_hinode *hdir;
  21449. +
  21450. + SiMustAnyLock(sb);
  21451. +
  21452. + sbinfo = au_sbi(sb);
  21453. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  21454. +
  21455. + if (!(sb_flags & MS_RDONLY)) {
  21456. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  21457. + pr_warn("first branch should be rw\n");
  21458. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  21459. + pr_warn("shwh should be used with ro\n");
  21460. + }
  21461. +
  21462. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  21463. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  21464. + pr_warn("udba=*notify requires xino\n");
  21465. +
  21466. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  21467. + pr_warn("dirperm1 breaks the protection"
  21468. + " by the permission bits on the lower branch\n");
  21469. +
  21470. + err = 0;
  21471. + fhsm = 0;
  21472. + root = sb->s_root;
  21473. + dir = root->d_inode;
  21474. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  21475. + bend = au_sbend(sb);
  21476. + for (bindex = 0; !err && bindex <= bend; bindex++) {
  21477. + skip = 0;
  21478. + h_dir = au_h_iptr(dir, bindex);
  21479. + br = au_sbr(sb, bindex);
  21480. +
  21481. + if ((br->br_perm & AuBrAttr_ICEX)
  21482. + && !h_dir->i_op->listxattr)
  21483. + br->br_perm &= ~AuBrAttr_ICEX;
  21484. +#if 0
  21485. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  21486. + && (au_br_sb(br)->s_flags & MS_NOSEC))
  21487. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  21488. +#endif
  21489. +
  21490. + do_free = 0;
  21491. + wbr = br->br_wbr;
  21492. + if (wbr)
  21493. + wbr_wh_read_lock(wbr);
  21494. +
  21495. + if (!au_br_writable(br->br_perm)) {
  21496. + do_free = !!wbr;
  21497. + skip = (!wbr
  21498. + || (!wbr->wbr_whbase
  21499. + && !wbr->wbr_plink
  21500. + && !wbr->wbr_orph));
  21501. + } else if (!au_br_wh_linkable(br->br_perm)) {
  21502. + /* skip = (!br->br_whbase && !br->br_orph); */
  21503. + skip = (!wbr || !wbr->wbr_whbase);
  21504. + if (skip && wbr) {
  21505. + if (do_plink)
  21506. + skip = !!wbr->wbr_plink;
  21507. + else
  21508. + skip = !wbr->wbr_plink;
  21509. + }
  21510. + } else {
  21511. + /* skip = (br->br_whbase && br->br_ohph); */
  21512. + skip = (wbr && wbr->wbr_whbase);
  21513. + if (skip) {
  21514. + if (do_plink)
  21515. + skip = !!wbr->wbr_plink;
  21516. + else
  21517. + skip = !wbr->wbr_plink;
  21518. + }
  21519. + }
  21520. + if (wbr)
  21521. + wbr_wh_read_unlock(wbr);
  21522. +
  21523. + if (au_br_fhsm(br->br_perm)) {
  21524. + fhsm++;
  21525. + AuDebugOn(!br->br_fhsm);
  21526. + }
  21527. +
  21528. + if (skip)
  21529. + continue;
  21530. +
  21531. + hdir = au_hi(dir, bindex);
  21532. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  21533. + if (wbr)
  21534. + wbr_wh_write_lock(wbr);
  21535. + err = au_wh_init(br, sb);
  21536. + if (wbr)
  21537. + wbr_wh_write_unlock(wbr);
  21538. + au_hn_imtx_unlock(hdir);
  21539. +
  21540. + if (!err && do_free) {
  21541. + kfree(wbr);
  21542. + br->br_wbr = NULL;
  21543. + }
  21544. + }
  21545. +
  21546. + if (fhsm >= 2) {
  21547. + au_fset_si(sbinfo, FHSM);
  21548. + for (bindex = bend; bindex >= 0; bindex--) {
  21549. + br = au_sbr(sb, bindex);
  21550. + if (au_br_fhsm(br->br_perm)) {
  21551. + au_fhsm_set_bottom(sb, bindex);
  21552. + break;
  21553. + }
  21554. + }
  21555. + } else {
  21556. + au_fclr_si(sbinfo, FHSM);
  21557. + au_fhsm_set_bottom(sb, -1);
  21558. + }
  21559. +
  21560. + return err;
  21561. +}
  21562. +
  21563. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  21564. +{
  21565. + int err;
  21566. + unsigned int tmp;
  21567. + aufs_bindex_t bindex, bend;
  21568. + struct au_opt *opt;
  21569. + struct au_opt_xino *opt_xino, xino;
  21570. + struct au_sbinfo *sbinfo;
  21571. + struct au_branch *br;
  21572. + struct inode *dir;
  21573. +
  21574. + SiMustWriteLock(sb);
  21575. +
  21576. + err = 0;
  21577. + opt_xino = NULL;
  21578. + opt = opts->opt;
  21579. + while (err >= 0 && opt->type != Opt_tail)
  21580. + err = au_opt_simple(sb, opt++, opts);
  21581. + if (err > 0)
  21582. + err = 0;
  21583. + else if (unlikely(err < 0))
  21584. + goto out;
  21585. +
  21586. + /* disable xino and udba temporary */
  21587. + sbinfo = au_sbi(sb);
  21588. + tmp = sbinfo->si_mntflags;
  21589. + au_opt_clr(sbinfo->si_mntflags, XINO);
  21590. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  21591. +
  21592. + opt = opts->opt;
  21593. + while (err >= 0 && opt->type != Opt_tail)
  21594. + err = au_opt_br(sb, opt++, opts);
  21595. + if (err > 0)
  21596. + err = 0;
  21597. + else if (unlikely(err < 0))
  21598. + goto out;
  21599. +
  21600. + bend = au_sbend(sb);
  21601. + if (unlikely(bend < 0)) {
  21602. + err = -EINVAL;
  21603. + pr_err("no branches\n");
  21604. + goto out;
  21605. + }
  21606. +
  21607. + if (au_opt_test(tmp, XINO))
  21608. + au_opt_set(sbinfo->si_mntflags, XINO);
  21609. + opt = opts->opt;
  21610. + while (!err && opt->type != Opt_tail)
  21611. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  21612. + if (unlikely(err))
  21613. + goto out;
  21614. +
  21615. + err = au_opts_verify(sb, sb->s_flags, tmp);
  21616. + if (unlikely(err))
  21617. + goto out;
  21618. +
  21619. + /* restore xino */
  21620. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  21621. + xino.file = au_xino_def(sb);
  21622. + err = PTR_ERR(xino.file);
  21623. + if (IS_ERR(xino.file))
  21624. + goto out;
  21625. +
  21626. + err = au_xino_set(sb, &xino, /*remount*/0);
  21627. + fput(xino.file);
  21628. + if (unlikely(err))
  21629. + goto out;
  21630. + }
  21631. +
  21632. + /* restore udba */
  21633. + tmp &= AuOptMask_UDBA;
  21634. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  21635. + sbinfo->si_mntflags |= tmp;
  21636. + bend = au_sbend(sb);
  21637. + for (bindex = 0; bindex <= bend; bindex++) {
  21638. + br = au_sbr(sb, bindex);
  21639. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  21640. + if (unlikely(err))
  21641. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  21642. + bindex, err);
  21643. + /* go on even if err */
  21644. + }
  21645. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  21646. + dir = sb->s_root->d_inode;
  21647. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  21648. + }
  21649. +
  21650. +out:
  21651. + return err;
  21652. +}
  21653. +
  21654. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  21655. +{
  21656. + int err, rerr;
  21657. + struct inode *dir;
  21658. + struct au_opt_xino *opt_xino;
  21659. + struct au_opt *opt;
  21660. + struct au_sbinfo *sbinfo;
  21661. +
  21662. + SiMustWriteLock(sb);
  21663. +
  21664. + dir = sb->s_root->d_inode;
  21665. + sbinfo = au_sbi(sb);
  21666. + err = 0;
  21667. + opt_xino = NULL;
  21668. + opt = opts->opt;
  21669. + while (err >= 0 && opt->type != Opt_tail) {
  21670. + err = au_opt_simple(sb, opt, opts);
  21671. + if (!err)
  21672. + err = au_opt_br(sb, opt, opts);
  21673. + if (!err)
  21674. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  21675. + opt++;
  21676. + }
  21677. + if (err > 0)
  21678. + err = 0;
  21679. + AuTraceErr(err);
  21680. + /* go on even err */
  21681. +
  21682. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  21683. + if (unlikely(rerr && !err))
  21684. + err = rerr;
  21685. +
  21686. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  21687. + rerr = au_xib_trunc(sb);
  21688. + if (unlikely(rerr && !err))
  21689. + err = rerr;
  21690. + }
  21691. +
  21692. + /* will be handled by the caller */
  21693. + if (!au_ftest_opts(opts->flags, REFRESH)
  21694. + && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
  21695. + au_fset_opts(opts->flags, REFRESH);
  21696. +
  21697. + AuDbg("status 0x%x\n", opts->flags);
  21698. + return err;
  21699. +}
  21700. +
  21701. +/* ---------------------------------------------------------------------- */
  21702. +
  21703. +unsigned int au_opt_udba(struct super_block *sb)
  21704. +{
  21705. + return au_mntflags(sb) & AuOptMask_UDBA;
  21706. +}
  21707. diff -Nur linux-4.0.5.orig/fs/aufs/opts.h linux-4.0.5/fs/aufs/opts.h
  21708. --- linux-4.0.5.orig/fs/aufs/opts.h 1969-12-31 18:00:00.000000000 -0600
  21709. +++ linux-4.0.5/fs/aufs/opts.h 2015-06-21 14:07:48.686432781 -0500
  21710. @@ -0,0 +1,210 @@
  21711. +/*
  21712. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  21713. + *
  21714. + * This program, aufs is free software; you can redistribute it and/or modify
  21715. + * it under the terms of the GNU General Public License as published by
  21716. + * the Free Software Foundation; either version 2 of the License, or
  21717. + * (at your option) any later version.
  21718. + *
  21719. + * This program is distributed in the hope that it will be useful,
  21720. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21721. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21722. + * GNU General Public License for more details.
  21723. + *
  21724. + * You should have received a copy of the GNU General Public License
  21725. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21726. + */
  21727. +
  21728. +/*
  21729. + * mount options/flags
  21730. + */
  21731. +
  21732. +#ifndef __AUFS_OPTS_H__
  21733. +#define __AUFS_OPTS_H__
  21734. +
  21735. +#ifdef __KERNEL__
  21736. +
  21737. +#include <linux/path.h>
  21738. +
  21739. +struct file;
  21740. +struct super_block;
  21741. +
  21742. +/* ---------------------------------------------------------------------- */
  21743. +
  21744. +/* mount flags */
  21745. +#define AuOpt_XINO 1 /* external inode number bitmap
  21746. + and translation table */
  21747. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  21748. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  21749. +#define AuOpt_UDBA_REVAL (1 << 3)
  21750. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  21751. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  21752. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  21753. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  21754. + bits */
  21755. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  21756. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  21757. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  21758. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  21759. +#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
  21760. +#define AuOpt_DIO (1 << 14) /* direct io */
  21761. +
  21762. +#ifndef CONFIG_AUFS_HNOTIFY
  21763. +#undef AuOpt_UDBA_HNOTIFY
  21764. +#define AuOpt_UDBA_HNOTIFY 0
  21765. +#endif
  21766. +#ifndef CONFIG_AUFS_SHWH
  21767. +#undef AuOpt_SHWH
  21768. +#define AuOpt_SHWH 0
  21769. +#endif
  21770. +
  21771. +#define AuOpt_Def (AuOpt_XINO \
  21772. + | AuOpt_UDBA_REVAL \
  21773. + | AuOpt_PLINK \
  21774. + /* | AuOpt_DIRPERM1 */ \
  21775. + | AuOpt_WARN_PERM)
  21776. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  21777. + | AuOpt_UDBA_REVAL \
  21778. + | AuOpt_UDBA_HNOTIFY)
  21779. +
  21780. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  21781. +#define au_opt_set(flags, name) do { \
  21782. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  21783. + ((flags) |= AuOpt_##name); \
  21784. +} while (0)
  21785. +#define au_opt_set_udba(flags, name) do { \
  21786. + (flags) &= ~AuOptMask_UDBA; \
  21787. + ((flags) |= AuOpt_##name); \
  21788. +} while (0)
  21789. +#define au_opt_clr(flags, name) do { \
  21790. + ((flags) &= ~AuOpt_##name); \
  21791. +} while (0)
  21792. +
  21793. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  21794. +{
  21795. +#ifdef CONFIG_PROC_FS
  21796. + return mntflags;
  21797. +#else
  21798. + return mntflags & ~AuOpt_PLINK;
  21799. +#endif
  21800. +}
  21801. +
  21802. +/* ---------------------------------------------------------------------- */
  21803. +
  21804. +/* policies to select one among multiple writable branches */
  21805. +enum {
  21806. + AuWbrCreate_TDP, /* top down parent */
  21807. + AuWbrCreate_RR, /* round robin */
  21808. + AuWbrCreate_MFS, /* most free space */
  21809. + AuWbrCreate_MFSV, /* mfs with seconds */
  21810. + AuWbrCreate_MFSRR, /* mfs then rr */
  21811. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  21812. + AuWbrCreate_PMFS, /* parent and mfs */
  21813. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  21814. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  21815. + AuWbrCreate_PMFSRRV, /* plus seconds */
  21816. +
  21817. + AuWbrCreate_Def = AuWbrCreate_TDP
  21818. +};
  21819. +
  21820. +enum {
  21821. + AuWbrCopyup_TDP, /* top down parent */
  21822. + AuWbrCopyup_BUP, /* bottom up parent */
  21823. + AuWbrCopyup_BU, /* bottom up */
  21824. +
  21825. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  21826. +};
  21827. +
  21828. +/* ---------------------------------------------------------------------- */
  21829. +
  21830. +struct au_opt_add {
  21831. + aufs_bindex_t bindex;
  21832. + char *pathname;
  21833. + int perm;
  21834. + struct path path;
  21835. +};
  21836. +
  21837. +struct au_opt_del {
  21838. + char *pathname;
  21839. + struct path h_path;
  21840. +};
  21841. +
  21842. +struct au_opt_mod {
  21843. + char *path;
  21844. + int perm;
  21845. + struct dentry *h_root;
  21846. +};
  21847. +
  21848. +struct au_opt_xino {
  21849. + char *path;
  21850. + struct file *file;
  21851. +};
  21852. +
  21853. +struct au_opt_xino_itrunc {
  21854. + aufs_bindex_t bindex;
  21855. +};
  21856. +
  21857. +struct au_opt_wbr_create {
  21858. + int wbr_create;
  21859. + int mfs_second;
  21860. + unsigned long long mfsrr_watermark;
  21861. +};
  21862. +
  21863. +struct au_opt {
  21864. + int type;
  21865. + union {
  21866. + struct au_opt_xino xino;
  21867. + struct au_opt_xino_itrunc xino_itrunc;
  21868. + struct au_opt_add add;
  21869. + struct au_opt_del del;
  21870. + struct au_opt_mod mod;
  21871. + int dirwh;
  21872. + int rdcache;
  21873. + unsigned int rdblk;
  21874. + unsigned int rdhash;
  21875. + int udba;
  21876. + struct au_opt_wbr_create wbr_create;
  21877. + int wbr_copyup;
  21878. + unsigned int fhsm_second;
  21879. + };
  21880. +};
  21881. +
  21882. +/* opts flags */
  21883. +#define AuOpts_REMOUNT 1
  21884. +#define AuOpts_REFRESH (1 << 1)
  21885. +#define AuOpts_TRUNC_XIB (1 << 2)
  21886. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  21887. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  21888. +#define au_fset_opts(flags, name) \
  21889. + do { (flags) |= AuOpts_##name; } while (0)
  21890. +#define au_fclr_opts(flags, name) \
  21891. + do { (flags) &= ~AuOpts_##name; } while (0)
  21892. +
  21893. +struct au_opts {
  21894. + struct au_opt *opt;
  21895. + int max_opt;
  21896. +
  21897. + unsigned int given_udba;
  21898. + unsigned int flags;
  21899. + unsigned long sb_flags;
  21900. +};
  21901. +
  21902. +/* ---------------------------------------------------------------------- */
  21903. +
  21904. +/* opts.c */
  21905. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  21906. +const char *au_optstr_udba(int udba);
  21907. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  21908. +const char *au_optstr_wbr_create(int wbr_create);
  21909. +
  21910. +void au_opts_free(struct au_opts *opts);
  21911. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  21912. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  21913. + unsigned int pending);
  21914. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  21915. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  21916. +
  21917. +unsigned int au_opt_udba(struct super_block *sb);
  21918. +
  21919. +#endif /* __KERNEL__ */
  21920. +#endif /* __AUFS_OPTS_H__ */
  21921. diff -Nur linux-4.0.5.orig/fs/aufs/plink.c linux-4.0.5/fs/aufs/plink.c
  21922. --- linux-4.0.5.orig/fs/aufs/plink.c 1969-12-31 18:00:00.000000000 -0600
  21923. +++ linux-4.0.5/fs/aufs/plink.c 2015-06-21 14:07:48.686432781 -0500
  21924. @@ -0,0 +1,532 @@
  21925. +/*
  21926. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  21927. + *
  21928. + * This program, aufs is free software; you can redistribute it and/or modify
  21929. + * it under the terms of the GNU General Public License as published by
  21930. + * the Free Software Foundation; either version 2 of the License, or
  21931. + * (at your option) any later version.
  21932. + *
  21933. + * This program is distributed in the hope that it will be useful,
  21934. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21935. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21936. + * GNU General Public License for more details.
  21937. + *
  21938. + * You should have received a copy of the GNU General Public License
  21939. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21940. + */
  21941. +
  21942. +/*
  21943. + * pseudo-link
  21944. + */
  21945. +
  21946. +#include "aufs.h"
  21947. +
  21948. +/*
  21949. + * the pseudo-link maintenance mode.
  21950. + * during a user process maintains the pseudo-links,
  21951. + * prohibit adding a new plink and branch manipulation.
  21952. + *
  21953. + * Flags
  21954. + * NOPLM:
  21955. + * For entry functions which will handle plink, and i_mutex is already held
  21956. + * in VFS.
  21957. + * They cannot wait and should return an error at once.
  21958. + * Callers has to check the error.
  21959. + * NOPLMW:
  21960. + * For entry functions which will handle plink, but i_mutex is not held
  21961. + * in VFS.
  21962. + * They can wait the plink maintenance mode to finish.
  21963. + *
  21964. + * They behave like F_SETLK and F_SETLKW.
  21965. + * If the caller never handle plink, then both flags are unnecessary.
  21966. + */
  21967. +
  21968. +int au_plink_maint(struct super_block *sb, int flags)
  21969. +{
  21970. + int err;
  21971. + pid_t pid, ppid;
  21972. + struct au_sbinfo *sbi;
  21973. +
  21974. + SiMustAnyLock(sb);
  21975. +
  21976. + err = 0;
  21977. + if (!au_opt_test(au_mntflags(sb), PLINK))
  21978. + goto out;
  21979. +
  21980. + sbi = au_sbi(sb);
  21981. + pid = sbi->si_plink_maint_pid;
  21982. + if (!pid || pid == current->pid)
  21983. + goto out;
  21984. +
  21985. + /* todo: it highly depends upon /sbin/mount.aufs */
  21986. + rcu_read_lock();
  21987. + ppid = task_pid_vnr(rcu_dereference(current->real_parent));
  21988. + rcu_read_unlock();
  21989. + if (pid == ppid)
  21990. + goto out;
  21991. +
  21992. + if (au_ftest_lock(flags, NOPLMW)) {
  21993. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  21994. + /* AuDebugOn(!lockdep_depth(current)); */
  21995. + while (sbi->si_plink_maint_pid) {
  21996. + si_read_unlock(sb);
  21997. + /* gave up wake_up_bit() */
  21998. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  21999. +
  22000. + if (au_ftest_lock(flags, FLUSH))
  22001. + au_nwt_flush(&sbi->si_nowait);
  22002. + si_noflush_read_lock(sb);
  22003. + }
  22004. + } else if (au_ftest_lock(flags, NOPLM)) {
  22005. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  22006. + err = -EAGAIN;
  22007. + }
  22008. +
  22009. +out:
  22010. + return err;
  22011. +}
  22012. +
  22013. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  22014. +{
  22015. + spin_lock(&sbinfo->si_plink_maint_lock);
  22016. + sbinfo->si_plink_maint_pid = 0;
  22017. + spin_unlock(&sbinfo->si_plink_maint_lock);
  22018. + wake_up_all(&sbinfo->si_plink_wq);
  22019. +}
  22020. +
  22021. +int au_plink_maint_enter(struct super_block *sb)
  22022. +{
  22023. + int err;
  22024. + struct au_sbinfo *sbinfo;
  22025. +
  22026. + err = 0;
  22027. + sbinfo = au_sbi(sb);
  22028. + /* make sure i am the only one in this fs */
  22029. + si_write_lock(sb, AuLock_FLUSH);
  22030. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  22031. + spin_lock(&sbinfo->si_plink_maint_lock);
  22032. + if (!sbinfo->si_plink_maint_pid)
  22033. + sbinfo->si_plink_maint_pid = current->pid;
  22034. + else
  22035. + err = -EBUSY;
  22036. + spin_unlock(&sbinfo->si_plink_maint_lock);
  22037. + }
  22038. + si_write_unlock(sb);
  22039. +
  22040. + return err;
  22041. +}
  22042. +
  22043. +/* ---------------------------------------------------------------------- */
  22044. +
  22045. +#ifdef CONFIG_AUFS_DEBUG
  22046. +void au_plink_list(struct super_block *sb)
  22047. +{
  22048. + int i;
  22049. + struct au_sbinfo *sbinfo;
  22050. + struct hlist_head *plink_hlist;
  22051. + struct pseudo_link *plink;
  22052. +
  22053. + SiMustAnyLock(sb);
  22054. +
  22055. + sbinfo = au_sbi(sb);
  22056. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  22057. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  22058. +
  22059. + for (i = 0; i < AuPlink_NHASH; i++) {
  22060. + plink_hlist = &sbinfo->si_plink[i].head;
  22061. + rcu_read_lock();
  22062. + hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
  22063. + AuDbg("%lu\n", plink->inode->i_ino);
  22064. + rcu_read_unlock();
  22065. + }
  22066. +}
  22067. +#endif
  22068. +
  22069. +/* is the inode pseudo-linked? */
  22070. +int au_plink_test(struct inode *inode)
  22071. +{
  22072. + int found, i;
  22073. + struct au_sbinfo *sbinfo;
  22074. + struct hlist_head *plink_hlist;
  22075. + struct pseudo_link *plink;
  22076. +
  22077. + sbinfo = au_sbi(inode->i_sb);
  22078. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  22079. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  22080. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  22081. +
  22082. + found = 0;
  22083. + i = au_plink_hash(inode->i_ino);
  22084. + plink_hlist = &sbinfo->si_plink[i].head;
  22085. + rcu_read_lock();
  22086. + hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
  22087. + if (plink->inode == inode) {
  22088. + found = 1;
  22089. + break;
  22090. + }
  22091. + rcu_read_unlock();
  22092. + return found;
  22093. +}
  22094. +
  22095. +/* ---------------------------------------------------------------------- */
  22096. +
  22097. +/*
  22098. + * generate a name for plink.
  22099. + * the file will be stored under AUFS_WH_PLINKDIR.
  22100. + */
  22101. +/* 20 is max digits length of ulong 64 */
  22102. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  22103. +
  22104. +static int plink_name(char *name, int len, struct inode *inode,
  22105. + aufs_bindex_t bindex)
  22106. +{
  22107. + int rlen;
  22108. + struct inode *h_inode;
  22109. +
  22110. + h_inode = au_h_iptr(inode, bindex);
  22111. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  22112. + return rlen;
  22113. +}
  22114. +
  22115. +struct au_do_plink_lkup_args {
  22116. + struct dentry **errp;
  22117. + struct qstr *tgtname;
  22118. + struct dentry *h_parent;
  22119. + struct au_branch *br;
  22120. +};
  22121. +
  22122. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  22123. + struct dentry *h_parent,
  22124. + struct au_branch *br)
  22125. +{
  22126. + struct dentry *h_dentry;
  22127. + struct mutex *h_mtx;
  22128. +
  22129. + h_mtx = &h_parent->d_inode->i_mutex;
  22130. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  22131. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  22132. + mutex_unlock(h_mtx);
  22133. + return h_dentry;
  22134. +}
  22135. +
  22136. +static void au_call_do_plink_lkup(void *args)
  22137. +{
  22138. + struct au_do_plink_lkup_args *a = args;
  22139. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  22140. +}
  22141. +
  22142. +/* lookup the plink-ed @inode under the branch at @bindex */
  22143. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  22144. +{
  22145. + struct dentry *h_dentry, *h_parent;
  22146. + struct au_branch *br;
  22147. + struct inode *h_dir;
  22148. + int wkq_err;
  22149. + char a[PLINK_NAME_LEN];
  22150. + struct qstr tgtname = QSTR_INIT(a, 0);
  22151. +
  22152. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  22153. +
  22154. + br = au_sbr(inode->i_sb, bindex);
  22155. + h_parent = br->br_wbr->wbr_plink;
  22156. + h_dir = h_parent->d_inode;
  22157. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  22158. +
  22159. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  22160. + struct au_do_plink_lkup_args args = {
  22161. + .errp = &h_dentry,
  22162. + .tgtname = &tgtname,
  22163. + .h_parent = h_parent,
  22164. + .br = br
  22165. + };
  22166. +
  22167. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  22168. + if (unlikely(wkq_err))
  22169. + h_dentry = ERR_PTR(wkq_err);
  22170. + } else
  22171. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  22172. +
  22173. + return h_dentry;
  22174. +}
  22175. +
  22176. +/* create a pseudo-link */
  22177. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  22178. + struct dentry *h_dentry, struct au_branch *br)
  22179. +{
  22180. + int err;
  22181. + struct path h_path = {
  22182. + .mnt = au_br_mnt(br)
  22183. + };
  22184. + struct inode *h_dir, *delegated;
  22185. +
  22186. + h_dir = h_parent->d_inode;
  22187. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
  22188. +again:
  22189. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  22190. + err = PTR_ERR(h_path.dentry);
  22191. + if (IS_ERR(h_path.dentry))
  22192. + goto out;
  22193. +
  22194. + err = 0;
  22195. + /* wh.plink dir is not monitored */
  22196. + /* todo: is it really safe? */
  22197. + if (h_path.dentry->d_inode
  22198. + && h_path.dentry->d_inode != h_dentry->d_inode) {
  22199. + delegated = NULL;
  22200. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  22201. + if (unlikely(err == -EWOULDBLOCK)) {
  22202. + pr_warn("cannot retry for NFSv4 delegation"
  22203. + " for an internal unlink\n");
  22204. + iput(delegated);
  22205. + }
  22206. + dput(h_path.dentry);
  22207. + h_path.dentry = NULL;
  22208. + if (!err)
  22209. + goto again;
  22210. + }
  22211. + if (!err && !h_path.dentry->d_inode) {
  22212. + delegated = NULL;
  22213. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  22214. + if (unlikely(err == -EWOULDBLOCK)) {
  22215. + pr_warn("cannot retry for NFSv4 delegation"
  22216. + " for an internal link\n");
  22217. + iput(delegated);
  22218. + }
  22219. + }
  22220. + dput(h_path.dentry);
  22221. +
  22222. +out:
  22223. + mutex_unlock(&h_dir->i_mutex);
  22224. + return err;
  22225. +}
  22226. +
  22227. +struct do_whplink_args {
  22228. + int *errp;
  22229. + struct qstr *tgt;
  22230. + struct dentry *h_parent;
  22231. + struct dentry *h_dentry;
  22232. + struct au_branch *br;
  22233. +};
  22234. +
  22235. +static void call_do_whplink(void *args)
  22236. +{
  22237. + struct do_whplink_args *a = args;
  22238. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  22239. +}
  22240. +
  22241. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  22242. + aufs_bindex_t bindex, struct au_branch *br)
  22243. +{
  22244. + int err, wkq_err;
  22245. + struct au_wbr *wbr;
  22246. + struct dentry *h_parent;
  22247. + struct inode *h_dir;
  22248. + char a[PLINK_NAME_LEN];
  22249. + struct qstr tgtname = QSTR_INIT(a, 0);
  22250. +
  22251. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  22252. + h_parent = wbr->wbr_plink;
  22253. + h_dir = h_parent->d_inode;
  22254. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  22255. +
  22256. + /* always superio. */
  22257. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  22258. + struct do_whplink_args args = {
  22259. + .errp = &err,
  22260. + .tgt = &tgtname,
  22261. + .h_parent = h_parent,
  22262. + .h_dentry = h_dentry,
  22263. + .br = br
  22264. + };
  22265. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  22266. + if (unlikely(wkq_err))
  22267. + err = wkq_err;
  22268. + } else
  22269. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  22270. +
  22271. + return err;
  22272. +}
  22273. +
  22274. +/* free a single plink */
  22275. +static void do_put_plink(struct pseudo_link *plink, int do_del)
  22276. +{
  22277. + if (do_del)
  22278. + hlist_del(&plink->hlist);
  22279. + iput(plink->inode);
  22280. + kfree(plink);
  22281. +}
  22282. +
  22283. +static void do_put_plink_rcu(struct rcu_head *rcu)
  22284. +{
  22285. + struct pseudo_link *plink;
  22286. +
  22287. + plink = container_of(rcu, struct pseudo_link, rcu);
  22288. + iput(plink->inode);
  22289. + kfree(plink);
  22290. +}
  22291. +
  22292. +/*
  22293. + * create a new pseudo-link for @h_dentry on @bindex.
  22294. + * the linked inode is held in aufs @inode.
  22295. + */
  22296. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  22297. + struct dentry *h_dentry)
  22298. +{
  22299. + struct super_block *sb;
  22300. + struct au_sbinfo *sbinfo;
  22301. + struct hlist_head *plink_hlist;
  22302. + struct pseudo_link *plink, *tmp;
  22303. + struct au_sphlhead *sphl;
  22304. + int found, err, cnt, i;
  22305. +
  22306. + sb = inode->i_sb;
  22307. + sbinfo = au_sbi(sb);
  22308. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  22309. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  22310. +
  22311. + found = au_plink_test(inode);
  22312. + if (found)
  22313. + return;
  22314. +
  22315. + i = au_plink_hash(inode->i_ino);
  22316. + sphl = sbinfo->si_plink + i;
  22317. + plink_hlist = &sphl->head;
  22318. + tmp = kmalloc(sizeof(*plink), GFP_NOFS);
  22319. + if (tmp)
  22320. + tmp->inode = au_igrab(inode);
  22321. + else {
  22322. + err = -ENOMEM;
  22323. + goto out;
  22324. + }
  22325. +
  22326. + spin_lock(&sphl->spin);
  22327. + hlist_for_each_entry(plink, plink_hlist, hlist) {
  22328. + if (plink->inode == inode) {
  22329. + found = 1;
  22330. + break;
  22331. + }
  22332. + }
  22333. + if (!found)
  22334. + hlist_add_head_rcu(&tmp->hlist, plink_hlist);
  22335. + spin_unlock(&sphl->spin);
  22336. + if (!found) {
  22337. + cnt = au_sphl_count(sphl);
  22338. +#define msg "unexpectedly unblanced or too many pseudo-links"
  22339. + if (cnt > AUFS_PLINK_WARN)
  22340. + AuWarn1(msg ", %d\n", cnt);
  22341. +#undef msg
  22342. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  22343. + } else {
  22344. + do_put_plink(tmp, 0);
  22345. + return;
  22346. + }
  22347. +
  22348. +out:
  22349. + if (unlikely(err)) {
  22350. + pr_warn("err %d, damaged pseudo link.\n", err);
  22351. + if (tmp) {
  22352. + au_sphl_del_rcu(&tmp->hlist, sphl);
  22353. + call_rcu(&tmp->rcu, do_put_plink_rcu);
  22354. + }
  22355. + }
  22356. +}
  22357. +
  22358. +/* free all plinks */
  22359. +void au_plink_put(struct super_block *sb, int verbose)
  22360. +{
  22361. + int i, warned;
  22362. + struct au_sbinfo *sbinfo;
  22363. + struct hlist_head *plink_hlist;
  22364. + struct hlist_node *tmp;
  22365. + struct pseudo_link *plink;
  22366. +
  22367. + SiMustWriteLock(sb);
  22368. +
  22369. + sbinfo = au_sbi(sb);
  22370. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  22371. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  22372. +
  22373. + /* no spin_lock since sbinfo is write-locked */
  22374. + warned = 0;
  22375. + for (i = 0; i < AuPlink_NHASH; i++) {
  22376. + plink_hlist = &sbinfo->si_plink[i].head;
  22377. + if (!warned && verbose && !hlist_empty(plink_hlist)) {
  22378. + pr_warn("pseudo-link is not flushed");
  22379. + warned = 1;
  22380. + }
  22381. + hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
  22382. + do_put_plink(plink, 0);
  22383. + INIT_HLIST_HEAD(plink_hlist);
  22384. + }
  22385. +}
  22386. +
  22387. +void au_plink_clean(struct super_block *sb, int verbose)
  22388. +{
  22389. + struct dentry *root;
  22390. +
  22391. + root = sb->s_root;
  22392. + aufs_write_lock(root);
  22393. + if (au_opt_test(au_mntflags(sb), PLINK))
  22394. + au_plink_put(sb, verbose);
  22395. + aufs_write_unlock(root);
  22396. +}
  22397. +
  22398. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  22399. +{
  22400. + int do_put;
  22401. + aufs_bindex_t bstart, bend, bindex;
  22402. +
  22403. + do_put = 0;
  22404. + bstart = au_ibstart(inode);
  22405. + bend = au_ibend(inode);
  22406. + if (bstart >= 0) {
  22407. + for (bindex = bstart; bindex <= bend; bindex++) {
  22408. + if (!au_h_iptr(inode, bindex)
  22409. + || au_ii_br_id(inode, bindex) != br_id)
  22410. + continue;
  22411. + au_set_h_iptr(inode, bindex, NULL, 0);
  22412. + do_put = 1;
  22413. + break;
  22414. + }
  22415. + if (do_put)
  22416. + for (bindex = bstart; bindex <= bend; bindex++)
  22417. + if (au_h_iptr(inode, bindex)) {
  22418. + do_put = 0;
  22419. + break;
  22420. + }
  22421. + } else
  22422. + do_put = 1;
  22423. +
  22424. + return do_put;
  22425. +}
  22426. +
  22427. +/* free the plinks on a branch specified by @br_id */
  22428. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  22429. +{
  22430. + struct au_sbinfo *sbinfo;
  22431. + struct hlist_head *plink_hlist;
  22432. + struct hlist_node *tmp;
  22433. + struct pseudo_link *plink;
  22434. + struct inode *inode;
  22435. + int i, do_put;
  22436. +
  22437. + SiMustWriteLock(sb);
  22438. +
  22439. + sbinfo = au_sbi(sb);
  22440. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  22441. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  22442. +
  22443. + /* no spin_lock since sbinfo is write-locked */
  22444. + for (i = 0; i < AuPlink_NHASH; i++) {
  22445. + plink_hlist = &sbinfo->si_plink[i].head;
  22446. + hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
  22447. + inode = au_igrab(plink->inode);
  22448. + ii_write_lock_child(inode);
  22449. + do_put = au_plink_do_half_refresh(inode, br_id);
  22450. + if (do_put)
  22451. + do_put_plink(plink, 1);
  22452. + ii_write_unlock(inode);
  22453. + iput(inode);
  22454. + }
  22455. + }
  22456. +}
  22457. diff -Nur linux-4.0.5.orig/fs/aufs/poll.c linux-4.0.5/fs/aufs/poll.c
  22458. --- linux-4.0.5.orig/fs/aufs/poll.c 1969-12-31 18:00:00.000000000 -0600
  22459. +++ linux-4.0.5/fs/aufs/poll.c 2015-06-21 14:07:48.686432781 -0500
  22460. @@ -0,0 +1,52 @@
  22461. +/*
  22462. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  22463. + *
  22464. + * This program, aufs is free software; you can redistribute it and/or modify
  22465. + * it under the terms of the GNU General Public License as published by
  22466. + * the Free Software Foundation; either version 2 of the License, or
  22467. + * (at your option) any later version.
  22468. + *
  22469. + * This program is distributed in the hope that it will be useful,
  22470. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22471. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22472. + * GNU General Public License for more details.
  22473. + *
  22474. + * You should have received a copy of the GNU General Public License
  22475. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22476. + */
  22477. +
  22478. +/*
  22479. + * poll operation
  22480. + * There is only one filesystem which implements ->poll operation, currently.
  22481. + */
  22482. +
  22483. +#include "aufs.h"
  22484. +
  22485. +unsigned int aufs_poll(struct file *file, poll_table *wait)
  22486. +{
  22487. + unsigned int mask;
  22488. + int err;
  22489. + struct file *h_file;
  22490. + struct super_block *sb;
  22491. +
  22492. + /* We should pretend an error happened. */
  22493. + mask = POLLERR /* | POLLIN | POLLOUT */;
  22494. + sb = file->f_path.dentry->d_sb;
  22495. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  22496. +
  22497. + h_file = au_read_pre(file, /*keep_fi*/0);
  22498. + err = PTR_ERR(h_file);
  22499. + if (IS_ERR(h_file))
  22500. + goto out;
  22501. +
  22502. + /* it is not an error if h_file has no operation */
  22503. + mask = DEFAULT_POLLMASK;
  22504. + if (h_file->f_op->poll)
  22505. + mask = h_file->f_op->poll(h_file, wait);
  22506. + fput(h_file); /* instead of au_read_post() */
  22507. +
  22508. +out:
  22509. + si_read_unlock(sb);
  22510. + AuTraceErr((int)mask);
  22511. + return mask;
  22512. +}
  22513. diff -Nur linux-4.0.5.orig/fs/aufs/posix_acl.c linux-4.0.5/fs/aufs/posix_acl.c
  22514. --- linux-4.0.5.orig/fs/aufs/posix_acl.c 1969-12-31 18:00:00.000000000 -0600
  22515. +++ linux-4.0.5/fs/aufs/posix_acl.c 2015-06-21 14:07:48.686432781 -0500
  22516. @@ -0,0 +1,99 @@
  22517. +/*
  22518. + * Copyright (C) 2014-2015 Junjiro R. Okajima
  22519. + *
  22520. + * This program, aufs is free software; you can redistribute it and/or modify
  22521. + * it under the terms of the GNU General Public License as published by
  22522. + * the Free Software Foundation; either version 2 of the License, or
  22523. + * (at your option) any later version.
  22524. + *
  22525. + * This program is distributed in the hope that it will be useful,
  22526. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22527. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22528. + * GNU General Public License for more details.
  22529. + *
  22530. + * You should have received a copy of the GNU General Public License
  22531. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22532. + */
  22533. +
  22534. +/*
  22535. + * posix acl operations
  22536. + */
  22537. +
  22538. +#include <linux/fs.h>
  22539. +#include <linux/posix_acl.h>
  22540. +#include "aufs.h"
  22541. +
  22542. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  22543. +{
  22544. + struct posix_acl *acl;
  22545. + int err;
  22546. + aufs_bindex_t bindex;
  22547. + struct inode *h_inode;
  22548. + struct super_block *sb;
  22549. +
  22550. + acl = NULL;
  22551. + sb = inode->i_sb;
  22552. + si_read_lock(sb, AuLock_FLUSH);
  22553. + ii_read_lock_child(inode);
  22554. + if (!(sb->s_flags & MS_POSIXACL))
  22555. + goto out;
  22556. +
  22557. + bindex = au_ibstart(inode);
  22558. + h_inode = au_h_iptr(inode, bindex);
  22559. + if (unlikely(!h_inode
  22560. + || ((h_inode->i_mode & S_IFMT)
  22561. + != (inode->i_mode & S_IFMT)))) {
  22562. + err = au_busy_or_stale();
  22563. + acl = ERR_PTR(err);
  22564. + goto out;
  22565. + }
  22566. +
  22567. + /* always topmost only */
  22568. + acl = get_acl(h_inode, type);
  22569. +
  22570. +out:
  22571. + ii_read_unlock(inode);
  22572. + si_read_unlock(sb);
  22573. +
  22574. + AuTraceErrPtr(acl);
  22575. + return acl;
  22576. +}
  22577. +
  22578. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  22579. +{
  22580. + int err;
  22581. + ssize_t ssz;
  22582. + struct dentry *dentry;
  22583. + struct au_srxattr arg = {
  22584. + .type = AU_ACL_SET,
  22585. + .u.acl_set = {
  22586. + .acl = acl,
  22587. + .type = type
  22588. + },
  22589. + };
  22590. +
  22591. + mutex_lock(&inode->i_mutex);
  22592. + if (inode->i_ino == AUFS_ROOT_INO)
  22593. + dentry = dget(inode->i_sb->s_root);
  22594. + else {
  22595. + dentry = d_find_alias(inode);
  22596. + if (!dentry)
  22597. + dentry = d_find_any_alias(inode);
  22598. + if (!dentry) {
  22599. + pr_warn("cannot handle this inode, "
  22600. + "please report to aufs-users ML\n");
  22601. + err = -ENOENT;
  22602. + goto out;
  22603. + }
  22604. + }
  22605. +
  22606. + ssz = au_srxattr(dentry, &arg);
  22607. + dput(dentry);
  22608. + err = ssz;
  22609. + if (ssz >= 0)
  22610. + err = 0;
  22611. +
  22612. +out:
  22613. + mutex_unlock(&inode->i_mutex);
  22614. + return err;
  22615. +}
  22616. diff -Nur linux-4.0.5.orig/fs/aufs/procfs.c linux-4.0.5/fs/aufs/procfs.c
  22617. --- linux-4.0.5.orig/fs/aufs/procfs.c 1969-12-31 18:00:00.000000000 -0600
  22618. +++ linux-4.0.5/fs/aufs/procfs.c 2015-06-21 14:07:48.686432781 -0500
  22619. @@ -0,0 +1,169 @@
  22620. +/*
  22621. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  22622. + *
  22623. + * This program, aufs is free software; you can redistribute it and/or modify
  22624. + * it under the terms of the GNU General Public License as published by
  22625. + * the Free Software Foundation; either version 2 of the License, or
  22626. + * (at your option) any later version.
  22627. + *
  22628. + * This program is distributed in the hope that it will be useful,
  22629. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22630. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22631. + * GNU General Public License for more details.
  22632. + *
  22633. + * You should have received a copy of the GNU General Public License
  22634. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22635. + */
  22636. +
  22637. +/*
  22638. + * procfs interfaces
  22639. + */
  22640. +
  22641. +#include <linux/proc_fs.h>
  22642. +#include "aufs.h"
  22643. +
  22644. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  22645. +{
  22646. + struct au_sbinfo *sbinfo;
  22647. +
  22648. + sbinfo = file->private_data;
  22649. + if (sbinfo) {
  22650. + au_plink_maint_leave(sbinfo);
  22651. + kobject_put(&sbinfo->si_kobj);
  22652. + }
  22653. +
  22654. + return 0;
  22655. +}
  22656. +
  22657. +static void au_procfs_plm_write_clean(struct file *file)
  22658. +{
  22659. + struct au_sbinfo *sbinfo;
  22660. +
  22661. + sbinfo = file->private_data;
  22662. + if (sbinfo)
  22663. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  22664. +}
  22665. +
  22666. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  22667. +{
  22668. + int err;
  22669. + struct super_block *sb;
  22670. + struct au_sbinfo *sbinfo;
  22671. +
  22672. + err = -EBUSY;
  22673. + if (unlikely(file->private_data))
  22674. + goto out;
  22675. +
  22676. + sb = NULL;
  22677. + /* don't use au_sbilist_lock() here */
  22678. + spin_lock(&au_sbilist.spin);
  22679. + list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  22680. + if (id == sysaufs_si_id(sbinfo)) {
  22681. + kobject_get(&sbinfo->si_kobj);
  22682. + sb = sbinfo->si_sb;
  22683. + break;
  22684. + }
  22685. + spin_unlock(&au_sbilist.spin);
  22686. +
  22687. + err = -EINVAL;
  22688. + if (unlikely(!sb))
  22689. + goto out;
  22690. +
  22691. + err = au_plink_maint_enter(sb);
  22692. + if (!err)
  22693. + /* keep kobject_get() */
  22694. + file->private_data = sbinfo;
  22695. + else
  22696. + kobject_put(&sbinfo->si_kobj);
  22697. +out:
  22698. + return err;
  22699. +}
  22700. +
  22701. +/*
  22702. + * Accept a valid "si=xxxx" only.
  22703. + * Once it is accepted successfully, accept "clean" too.
  22704. + */
  22705. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  22706. + size_t count, loff_t *ppos)
  22707. +{
  22708. + ssize_t err;
  22709. + unsigned long id;
  22710. + /* last newline is allowed */
  22711. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  22712. +
  22713. + err = -EACCES;
  22714. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  22715. + goto out;
  22716. +
  22717. + err = -EINVAL;
  22718. + if (unlikely(count > sizeof(buf)))
  22719. + goto out;
  22720. +
  22721. + err = copy_from_user(buf, ubuf, count);
  22722. + if (unlikely(err)) {
  22723. + err = -EFAULT;
  22724. + goto out;
  22725. + }
  22726. + buf[count] = 0;
  22727. +
  22728. + err = -EINVAL;
  22729. + if (!strcmp("clean", buf)) {
  22730. + au_procfs_plm_write_clean(file);
  22731. + goto out_success;
  22732. + } else if (unlikely(strncmp("si=", buf, 3)))
  22733. + goto out;
  22734. +
  22735. + err = kstrtoul(buf + 3, 16, &id);
  22736. + if (unlikely(err))
  22737. + goto out;
  22738. +
  22739. + err = au_procfs_plm_write_si(file, id);
  22740. + if (unlikely(err))
  22741. + goto out;
  22742. +
  22743. +out_success:
  22744. + err = count; /* success */
  22745. +out:
  22746. + return err;
  22747. +}
  22748. +
  22749. +static const struct file_operations au_procfs_plm_fop = {
  22750. + .write = au_procfs_plm_write,
  22751. + .release = au_procfs_plm_release,
  22752. + .owner = THIS_MODULE
  22753. +};
  22754. +
  22755. +/* ---------------------------------------------------------------------- */
  22756. +
  22757. +static struct proc_dir_entry *au_procfs_dir;
  22758. +
  22759. +void au_procfs_fin(void)
  22760. +{
  22761. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  22762. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  22763. +}
  22764. +
  22765. +int __init au_procfs_init(void)
  22766. +{
  22767. + int err;
  22768. + struct proc_dir_entry *entry;
  22769. +
  22770. + err = -ENOMEM;
  22771. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  22772. + if (unlikely(!au_procfs_dir))
  22773. + goto out;
  22774. +
  22775. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
  22776. + au_procfs_dir, &au_procfs_plm_fop);
  22777. + if (unlikely(!entry))
  22778. + goto out_dir;
  22779. +
  22780. + err = 0;
  22781. + goto out; /* success */
  22782. +
  22783. +
  22784. +out_dir:
  22785. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  22786. +out:
  22787. + return err;
  22788. +}
  22789. diff -Nur linux-4.0.5.orig/fs/aufs/rdu.c linux-4.0.5/fs/aufs/rdu.c
  22790. --- linux-4.0.5.orig/fs/aufs/rdu.c 1969-12-31 18:00:00.000000000 -0600
  22791. +++ linux-4.0.5/fs/aufs/rdu.c 2015-06-21 14:07:48.686432781 -0500
  22792. @@ -0,0 +1,388 @@
  22793. +/*
  22794. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  22795. + *
  22796. + * This program, aufs is free software; you can redistribute it and/or modify
  22797. + * it under the terms of the GNU General Public License as published by
  22798. + * the Free Software Foundation; either version 2 of the License, or
  22799. + * (at your option) any later version.
  22800. + *
  22801. + * This program is distributed in the hope that it will be useful,
  22802. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22803. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22804. + * GNU General Public License for more details.
  22805. + *
  22806. + * You should have received a copy of the GNU General Public License
  22807. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22808. + */
  22809. +
  22810. +/*
  22811. + * readdir in userspace.
  22812. + */
  22813. +
  22814. +#include <linux/compat.h>
  22815. +#include <linux/fs_stack.h>
  22816. +#include <linux/security.h>
  22817. +#include "aufs.h"
  22818. +
  22819. +/* bits for struct aufs_rdu.flags */
  22820. +#define AuRdu_CALLED 1
  22821. +#define AuRdu_CONT (1 << 1)
  22822. +#define AuRdu_FULL (1 << 2)
  22823. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  22824. +#define au_fset_rdu(flags, name) \
  22825. + do { (flags) |= AuRdu_##name; } while (0)
  22826. +#define au_fclr_rdu(flags, name) \
  22827. + do { (flags) &= ~AuRdu_##name; } while (0)
  22828. +
  22829. +struct au_rdu_arg {
  22830. + struct dir_context ctx;
  22831. + struct aufs_rdu *rdu;
  22832. + union au_rdu_ent_ul ent;
  22833. + unsigned long end;
  22834. +
  22835. + struct super_block *sb;
  22836. + int err;
  22837. +};
  22838. +
  22839. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  22840. + loff_t offset, u64 h_ino, unsigned int d_type)
  22841. +{
  22842. + int err, len;
  22843. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  22844. + struct aufs_rdu *rdu = arg->rdu;
  22845. + struct au_rdu_ent ent;
  22846. +
  22847. + err = 0;
  22848. + arg->err = 0;
  22849. + au_fset_rdu(rdu->cookie.flags, CALLED);
  22850. + len = au_rdu_len(nlen);
  22851. + if (arg->ent.ul + len < arg->end) {
  22852. + ent.ino = h_ino;
  22853. + ent.bindex = rdu->cookie.bindex;
  22854. + ent.type = d_type;
  22855. + ent.nlen = nlen;
  22856. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  22857. + ent.type = DT_UNKNOWN;
  22858. +
  22859. + /* unnecessary to support mmap_sem since this is a dir */
  22860. + err = -EFAULT;
  22861. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  22862. + goto out;
  22863. + if (copy_to_user(arg->ent.e->name, name, nlen))
  22864. + goto out;
  22865. + /* the terminating NULL */
  22866. + if (__put_user(0, arg->ent.e->name + nlen))
  22867. + goto out;
  22868. + err = 0;
  22869. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  22870. + arg->ent.ul += len;
  22871. + rdu->rent++;
  22872. + } else {
  22873. + err = -EFAULT;
  22874. + au_fset_rdu(rdu->cookie.flags, FULL);
  22875. + rdu->full = 1;
  22876. + rdu->tail = arg->ent;
  22877. + }
  22878. +
  22879. +out:
  22880. + /* AuTraceErr(err); */
  22881. + return err;
  22882. +}
  22883. +
  22884. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  22885. +{
  22886. + int err;
  22887. + loff_t offset;
  22888. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  22889. +
  22890. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  22891. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  22892. + err = offset;
  22893. + if (unlikely(offset != cookie->h_pos))
  22894. + goto out;
  22895. +
  22896. + err = 0;
  22897. + do {
  22898. + arg->err = 0;
  22899. + au_fclr_rdu(cookie->flags, CALLED);
  22900. + /* smp_mb(); */
  22901. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  22902. + if (err >= 0)
  22903. + err = arg->err;
  22904. + } while (!err
  22905. + && au_ftest_rdu(cookie->flags, CALLED)
  22906. + && !au_ftest_rdu(cookie->flags, FULL));
  22907. + cookie->h_pos = h_file->f_pos;
  22908. +
  22909. +out:
  22910. + AuTraceErr(err);
  22911. + return err;
  22912. +}
  22913. +
  22914. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  22915. +{
  22916. + int err;
  22917. + aufs_bindex_t bend;
  22918. + struct au_rdu_arg arg = {
  22919. + .ctx = {
  22920. + .actor = au_rdu_fill
  22921. + }
  22922. + };
  22923. + struct dentry *dentry;
  22924. + struct inode *inode;
  22925. + struct file *h_file;
  22926. + struct au_rdu_cookie *cookie = &rdu->cookie;
  22927. +
  22928. + err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
  22929. + if (unlikely(err)) {
  22930. + err = -EFAULT;
  22931. + AuTraceErr(err);
  22932. + goto out;
  22933. + }
  22934. + rdu->rent = 0;
  22935. + rdu->tail = rdu->ent;
  22936. + rdu->full = 0;
  22937. + arg.rdu = rdu;
  22938. + arg.ent = rdu->ent;
  22939. + arg.end = arg.ent.ul;
  22940. + arg.end += rdu->sz;
  22941. +
  22942. + err = -ENOTDIR;
  22943. + if (unlikely(!file->f_op->iterate))
  22944. + goto out;
  22945. +
  22946. + err = security_file_permission(file, MAY_READ);
  22947. + AuTraceErr(err);
  22948. + if (unlikely(err))
  22949. + goto out;
  22950. +
  22951. + dentry = file->f_path.dentry;
  22952. + inode = dentry->d_inode;
  22953. +#if 1
  22954. + mutex_lock(&inode->i_mutex);
  22955. +#else
  22956. + err = mutex_lock_killable(&inode->i_mutex);
  22957. + AuTraceErr(err);
  22958. + if (unlikely(err))
  22959. + goto out;
  22960. +#endif
  22961. +
  22962. + arg.sb = inode->i_sb;
  22963. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  22964. + if (unlikely(err))
  22965. + goto out_mtx;
  22966. + err = au_alive_dir(dentry);
  22967. + if (unlikely(err))
  22968. + goto out_si;
  22969. + /* todo: reval? */
  22970. + fi_read_lock(file);
  22971. +
  22972. + err = -EAGAIN;
  22973. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  22974. + && cookie->generation != au_figen(file)))
  22975. + goto out_unlock;
  22976. +
  22977. + err = 0;
  22978. + if (!rdu->blk) {
  22979. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  22980. + if (!rdu->blk)
  22981. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  22982. + }
  22983. + bend = au_fbstart(file);
  22984. + if (cookie->bindex < bend)
  22985. + cookie->bindex = bend;
  22986. + bend = au_fbend_dir(file);
  22987. + /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
  22988. + for (; !err && cookie->bindex <= bend;
  22989. + cookie->bindex++, cookie->h_pos = 0) {
  22990. + h_file = au_hf_dir(file, cookie->bindex);
  22991. + if (!h_file)
  22992. + continue;
  22993. +
  22994. + au_fclr_rdu(cookie->flags, FULL);
  22995. + err = au_rdu_do(h_file, &arg);
  22996. + AuTraceErr(err);
  22997. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  22998. + break;
  22999. + }
  23000. + AuDbg("rent %llu\n", rdu->rent);
  23001. +
  23002. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  23003. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  23004. + au_fset_rdu(cookie->flags, CONT);
  23005. + cookie->generation = au_figen(file);
  23006. + }
  23007. +
  23008. + ii_read_lock_child(inode);
  23009. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
  23010. + ii_read_unlock(inode);
  23011. +
  23012. +out_unlock:
  23013. + fi_read_unlock(file);
  23014. +out_si:
  23015. + si_read_unlock(arg.sb);
  23016. +out_mtx:
  23017. + mutex_unlock(&inode->i_mutex);
  23018. +out:
  23019. + AuTraceErr(err);
  23020. + return err;
  23021. +}
  23022. +
  23023. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  23024. +{
  23025. + int err;
  23026. + ino_t ino;
  23027. + unsigned long long nent;
  23028. + union au_rdu_ent_ul *u;
  23029. + struct au_rdu_ent ent;
  23030. + struct super_block *sb;
  23031. +
  23032. + err = 0;
  23033. + nent = rdu->nent;
  23034. + u = &rdu->ent;
  23035. + sb = file->f_path.dentry->d_sb;
  23036. + si_read_lock(sb, AuLock_FLUSH);
  23037. + while (nent-- > 0) {
  23038. + /* unnecessary to support mmap_sem since this is a dir */
  23039. + err = copy_from_user(&ent, u->e, sizeof(ent));
  23040. + if (!err)
  23041. + err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
  23042. + if (unlikely(err)) {
  23043. + err = -EFAULT;
  23044. + AuTraceErr(err);
  23045. + break;
  23046. + }
  23047. +
  23048. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  23049. + if (!ent.wh)
  23050. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  23051. + else
  23052. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  23053. + &ino);
  23054. + if (unlikely(err)) {
  23055. + AuTraceErr(err);
  23056. + break;
  23057. + }
  23058. +
  23059. + err = __put_user(ino, &u->e->ino);
  23060. + if (unlikely(err)) {
  23061. + err = -EFAULT;
  23062. + AuTraceErr(err);
  23063. + break;
  23064. + }
  23065. + u->ul += au_rdu_len(ent.nlen);
  23066. + }
  23067. + si_read_unlock(sb);
  23068. +
  23069. + return err;
  23070. +}
  23071. +
  23072. +/* ---------------------------------------------------------------------- */
  23073. +
  23074. +static int au_rdu_verify(struct aufs_rdu *rdu)
  23075. +{
  23076. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  23077. + "%llu, b%d, 0x%x, g%u}\n",
  23078. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  23079. + rdu->blk,
  23080. + rdu->rent, rdu->shwh, rdu->full,
  23081. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  23082. + rdu->cookie.generation);
  23083. +
  23084. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  23085. + return 0;
  23086. +
  23087. + AuDbg("%u:%u\n",
  23088. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  23089. + return -EINVAL;
  23090. +}
  23091. +
  23092. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  23093. +{
  23094. + long err, e;
  23095. + struct aufs_rdu rdu;
  23096. + void __user *p = (void __user *)arg;
  23097. +
  23098. + err = copy_from_user(&rdu, p, sizeof(rdu));
  23099. + if (unlikely(err)) {
  23100. + err = -EFAULT;
  23101. + AuTraceErr(err);
  23102. + goto out;
  23103. + }
  23104. + err = au_rdu_verify(&rdu);
  23105. + if (unlikely(err))
  23106. + goto out;
  23107. +
  23108. + switch (cmd) {
  23109. + case AUFS_CTL_RDU:
  23110. + err = au_rdu(file, &rdu);
  23111. + if (unlikely(err))
  23112. + break;
  23113. +
  23114. + e = copy_to_user(p, &rdu, sizeof(rdu));
  23115. + if (unlikely(e)) {
  23116. + err = -EFAULT;
  23117. + AuTraceErr(err);
  23118. + }
  23119. + break;
  23120. + case AUFS_CTL_RDU_INO:
  23121. + err = au_rdu_ino(file, &rdu);
  23122. + break;
  23123. +
  23124. + default:
  23125. + /* err = -ENOTTY; */
  23126. + err = -EINVAL;
  23127. + }
  23128. +
  23129. +out:
  23130. + AuTraceErr(err);
  23131. + return err;
  23132. +}
  23133. +
  23134. +#ifdef CONFIG_COMPAT
  23135. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  23136. +{
  23137. + long err, e;
  23138. + struct aufs_rdu rdu;
  23139. + void __user *p = compat_ptr(arg);
  23140. +
  23141. + /* todo: get_user()? */
  23142. + err = copy_from_user(&rdu, p, sizeof(rdu));
  23143. + if (unlikely(err)) {
  23144. + err = -EFAULT;
  23145. + AuTraceErr(err);
  23146. + goto out;
  23147. + }
  23148. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  23149. + err = au_rdu_verify(&rdu);
  23150. + if (unlikely(err))
  23151. + goto out;
  23152. +
  23153. + switch (cmd) {
  23154. + case AUFS_CTL_RDU:
  23155. + err = au_rdu(file, &rdu);
  23156. + if (unlikely(err))
  23157. + break;
  23158. +
  23159. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  23160. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  23161. + e = copy_to_user(p, &rdu, sizeof(rdu));
  23162. + if (unlikely(e)) {
  23163. + err = -EFAULT;
  23164. + AuTraceErr(err);
  23165. + }
  23166. + break;
  23167. + case AUFS_CTL_RDU_INO:
  23168. + err = au_rdu_ino(file, &rdu);
  23169. + break;
  23170. +
  23171. + default:
  23172. + /* err = -ENOTTY; */
  23173. + err = -EINVAL;
  23174. + }
  23175. +
  23176. +out:
  23177. + AuTraceErr(err);
  23178. + return err;
  23179. +}
  23180. +#endif
  23181. diff -Nur linux-4.0.5.orig/fs/aufs/rwsem.h linux-4.0.5/fs/aufs/rwsem.h
  23182. --- linux-4.0.5.orig/fs/aufs/rwsem.h 1969-12-31 18:00:00.000000000 -0600
  23183. +++ linux-4.0.5/fs/aufs/rwsem.h 2015-06-21 14:07:48.686432781 -0500
  23184. @@ -0,0 +1,191 @@
  23185. +/*
  23186. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23187. + *
  23188. + * This program, aufs is free software; you can redistribute it and/or modify
  23189. + * it under the terms of the GNU General Public License as published by
  23190. + * the Free Software Foundation; either version 2 of the License, or
  23191. + * (at your option) any later version.
  23192. + *
  23193. + * This program is distributed in the hope that it will be useful,
  23194. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23195. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23196. + * GNU General Public License for more details.
  23197. + *
  23198. + * You should have received a copy of the GNU General Public License
  23199. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23200. + */
  23201. +
  23202. +/*
  23203. + * simple read-write semaphore wrappers
  23204. + */
  23205. +
  23206. +#ifndef __AUFS_RWSEM_H__
  23207. +#define __AUFS_RWSEM_H__
  23208. +
  23209. +#ifdef __KERNEL__
  23210. +
  23211. +#include "debug.h"
  23212. +
  23213. +struct au_rwsem {
  23214. + struct rw_semaphore rwsem;
  23215. +#ifdef CONFIG_AUFS_DEBUG
  23216. + /* just for debugging, not almighty counter */
  23217. + atomic_t rcnt, wcnt;
  23218. +#endif
  23219. +};
  23220. +
  23221. +#ifdef CONFIG_AUFS_DEBUG
  23222. +#define AuDbgCntInit(rw) do { \
  23223. + atomic_set(&(rw)->rcnt, 0); \
  23224. + atomic_set(&(rw)->wcnt, 0); \
  23225. + smp_mb(); /* atomic set */ \
  23226. +} while (0)
  23227. +
  23228. +#define AuDbgRcntInc(rw) atomic_inc(&(rw)->rcnt)
  23229. +#define AuDbgRcntDec(rw) WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
  23230. +#define AuDbgWcntInc(rw) atomic_inc(&(rw)->wcnt)
  23231. +#define AuDbgWcntDec(rw) WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
  23232. +#else
  23233. +#define AuDbgCntInit(rw) do {} while (0)
  23234. +#define AuDbgRcntInc(rw) do {} while (0)
  23235. +#define AuDbgRcntDec(rw) do {} while (0)
  23236. +#define AuDbgWcntInc(rw) do {} while (0)
  23237. +#define AuDbgWcntDec(rw) do {} while (0)
  23238. +#endif /* CONFIG_AUFS_DEBUG */
  23239. +
  23240. +/* to debug easier, do not make them inlined functions */
  23241. +#define AuRwMustNoWaiters(rw) AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
  23242. +/* rwsem_is_locked() is unusable */
  23243. +#define AuRwMustReadLock(rw) AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
  23244. +#define AuRwMustWriteLock(rw) AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
  23245. +#define AuRwMustAnyLock(rw) AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
  23246. + && atomic_read(&(rw)->wcnt) <= 0)
  23247. +#define AuRwDestroy(rw) AuDebugOn(atomic_read(&(rw)->rcnt) \
  23248. + || atomic_read(&(rw)->wcnt))
  23249. +
  23250. +#define au_rw_class(rw, key) lockdep_set_class(&(rw)->rwsem, key)
  23251. +
  23252. +static inline void au_rw_init(struct au_rwsem *rw)
  23253. +{
  23254. + AuDbgCntInit(rw);
  23255. + init_rwsem(&rw->rwsem);
  23256. +}
  23257. +
  23258. +static inline void au_rw_init_wlock(struct au_rwsem *rw)
  23259. +{
  23260. + au_rw_init(rw);
  23261. + down_write(&rw->rwsem);
  23262. + AuDbgWcntInc(rw);
  23263. +}
  23264. +
  23265. +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
  23266. + unsigned int lsc)
  23267. +{
  23268. + au_rw_init(rw);
  23269. + down_write_nested(&rw->rwsem, lsc);
  23270. + AuDbgWcntInc(rw);
  23271. +}
  23272. +
  23273. +static inline void au_rw_read_lock(struct au_rwsem *rw)
  23274. +{
  23275. + down_read(&rw->rwsem);
  23276. + AuDbgRcntInc(rw);
  23277. +}
  23278. +
  23279. +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
  23280. +{
  23281. + down_read_nested(&rw->rwsem, lsc);
  23282. + AuDbgRcntInc(rw);
  23283. +}
  23284. +
  23285. +static inline void au_rw_read_unlock(struct au_rwsem *rw)
  23286. +{
  23287. + AuRwMustReadLock(rw);
  23288. + AuDbgRcntDec(rw);
  23289. + up_read(&rw->rwsem);
  23290. +}
  23291. +
  23292. +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
  23293. +{
  23294. + AuRwMustWriteLock(rw);
  23295. + AuDbgRcntInc(rw);
  23296. + AuDbgWcntDec(rw);
  23297. + downgrade_write(&rw->rwsem);
  23298. +}
  23299. +
  23300. +static inline void au_rw_write_lock(struct au_rwsem *rw)
  23301. +{
  23302. + down_write(&rw->rwsem);
  23303. + AuDbgWcntInc(rw);
  23304. +}
  23305. +
  23306. +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
  23307. + unsigned int lsc)
  23308. +{
  23309. + down_write_nested(&rw->rwsem, lsc);
  23310. + AuDbgWcntInc(rw);
  23311. +}
  23312. +
  23313. +static inline void au_rw_write_unlock(struct au_rwsem *rw)
  23314. +{
  23315. + AuRwMustWriteLock(rw);
  23316. + AuDbgWcntDec(rw);
  23317. + up_write(&rw->rwsem);
  23318. +}
  23319. +
  23320. +/* why is not _nested version defined */
  23321. +static inline int au_rw_read_trylock(struct au_rwsem *rw)
  23322. +{
  23323. + int ret;
  23324. +
  23325. + ret = down_read_trylock(&rw->rwsem);
  23326. + if (ret)
  23327. + AuDbgRcntInc(rw);
  23328. + return ret;
  23329. +}
  23330. +
  23331. +static inline int au_rw_write_trylock(struct au_rwsem *rw)
  23332. +{
  23333. + int ret;
  23334. +
  23335. + ret = down_write_trylock(&rw->rwsem);
  23336. + if (ret)
  23337. + AuDbgWcntInc(rw);
  23338. + return ret;
  23339. +}
  23340. +
  23341. +#undef AuDbgCntInit
  23342. +#undef AuDbgRcntInc
  23343. +#undef AuDbgRcntDec
  23344. +#undef AuDbgWcntInc
  23345. +#undef AuDbgWcntDec
  23346. +
  23347. +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  23348. +static inline void prefix##_read_lock(param) \
  23349. +{ au_rw_read_lock(rwsem); } \
  23350. +static inline void prefix##_write_lock(param) \
  23351. +{ au_rw_write_lock(rwsem); } \
  23352. +static inline int prefix##_read_trylock(param) \
  23353. +{ return au_rw_read_trylock(rwsem); } \
  23354. +static inline int prefix##_write_trylock(param) \
  23355. +{ return au_rw_write_trylock(rwsem); }
  23356. +/* why is not _nested version defined */
  23357. +/* static inline void prefix##_read_trylock_nested(param, lsc)
  23358. +{ au_rw_read_trylock_nested(rwsem, lsc)); }
  23359. +static inline void prefix##_write_trylock_nestd(param, lsc)
  23360. +{ au_rw_write_trylock_nested(rwsem, lsc); } */
  23361. +
  23362. +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
  23363. +static inline void prefix##_read_unlock(param) \
  23364. +{ au_rw_read_unlock(rwsem); } \
  23365. +static inline void prefix##_write_unlock(param) \
  23366. +{ au_rw_write_unlock(rwsem); } \
  23367. +static inline void prefix##_downgrade_lock(param) \
  23368. +{ au_rw_dgrade_lock(rwsem); }
  23369. +
  23370. +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
  23371. + AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  23372. + AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
  23373. +
  23374. +#endif /* __KERNEL__ */
  23375. +#endif /* __AUFS_RWSEM_H__ */
  23376. diff -Nur linux-4.0.5.orig/fs/aufs/sbinfo.c linux-4.0.5/fs/aufs/sbinfo.c
  23377. --- linux-4.0.5.orig/fs/aufs/sbinfo.c 1969-12-31 18:00:00.000000000 -0600
  23378. +++ linux-4.0.5/fs/aufs/sbinfo.c 2015-06-21 14:07:48.686432781 -0500
  23379. @@ -0,0 +1,354 @@
  23380. +/*
  23381. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23382. + *
  23383. + * This program, aufs is free software; you can redistribute it and/or modify
  23384. + * it under the terms of the GNU General Public License as published by
  23385. + * the Free Software Foundation; either version 2 of the License, or
  23386. + * (at your option) any later version.
  23387. + *
  23388. + * This program is distributed in the hope that it will be useful,
  23389. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23390. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23391. + * GNU General Public License for more details.
  23392. + *
  23393. + * You should have received a copy of the GNU General Public License
  23394. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23395. + */
  23396. +
  23397. +/*
  23398. + * superblock private data
  23399. + */
  23400. +
  23401. +#include "aufs.h"
  23402. +
  23403. +/*
  23404. + * they are necessary regardless sysfs is disabled.
  23405. + */
  23406. +void au_si_free(struct kobject *kobj)
  23407. +{
  23408. + int i;
  23409. + struct au_sbinfo *sbinfo;
  23410. + char *locked __maybe_unused; /* debug only */
  23411. +
  23412. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  23413. + for (i = 0; i < AuPlink_NHASH; i++)
  23414. + AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
  23415. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  23416. +
  23417. + au_rw_write_lock(&sbinfo->si_rwsem);
  23418. + au_br_free(sbinfo);
  23419. + au_rw_write_unlock(&sbinfo->si_rwsem);
  23420. +
  23421. + AuDebugOn(radix_tree_gang_lookup
  23422. + (&sbinfo->au_si_pid.tree, (void **)&locked,
  23423. + /*first_index*/PID_MAX_DEFAULT - 1,
  23424. + /*max_items*/sizeof(locked)/sizeof(*locked)));
  23425. +
  23426. + kfree(sbinfo->si_branch);
  23427. + kfree(sbinfo->au_si_pid.bitmap);
  23428. + mutex_destroy(&sbinfo->si_xib_mtx);
  23429. + AuRwDestroy(&sbinfo->si_rwsem);
  23430. +
  23431. + kfree(sbinfo);
  23432. +}
  23433. +
  23434. +int au_si_alloc(struct super_block *sb)
  23435. +{
  23436. + int err, i;
  23437. + struct au_sbinfo *sbinfo;
  23438. + static struct lock_class_key aufs_si;
  23439. +
  23440. + err = -ENOMEM;
  23441. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  23442. + if (unlikely(!sbinfo))
  23443. + goto out;
  23444. +
  23445. + BUILD_BUG_ON(sizeof(unsigned long) !=
  23446. + sizeof(*sbinfo->au_si_pid.bitmap));
  23447. + sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
  23448. + sizeof(*sbinfo->au_si_pid.bitmap),
  23449. + GFP_NOFS);
  23450. + if (unlikely(!sbinfo->au_si_pid.bitmap))
  23451. + goto out_sbinfo;
  23452. +
  23453. + /* will be reallocated separately */
  23454. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  23455. + if (unlikely(!sbinfo->si_branch))
  23456. + goto out_pidmap;
  23457. +
  23458. + err = sysaufs_si_init(sbinfo);
  23459. + if (unlikely(err))
  23460. + goto out_br;
  23461. +
  23462. + au_nwt_init(&sbinfo->si_nowait);
  23463. + au_rw_init_wlock(&sbinfo->si_rwsem);
  23464. + au_rw_class(&sbinfo->si_rwsem, &aufs_si);
  23465. + spin_lock_init(&sbinfo->au_si_pid.tree_lock);
  23466. + INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
  23467. +
  23468. + atomic_long_set(&sbinfo->si_ninodes, 0);
  23469. + atomic_long_set(&sbinfo->si_nfiles, 0);
  23470. +
  23471. + sbinfo->si_bend = -1;
  23472. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  23473. +
  23474. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  23475. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  23476. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  23477. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  23478. +
  23479. + au_fhsm_init(sbinfo);
  23480. +
  23481. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  23482. +
  23483. + sbinfo->si_xino_jiffy = jiffies;
  23484. + sbinfo->si_xino_expire
  23485. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  23486. + mutex_init(&sbinfo->si_xib_mtx);
  23487. + sbinfo->si_xino_brid = -1;
  23488. + /* leave si_xib_last_pindex and si_xib_next_bit */
  23489. +
  23490. + au_sphl_init(&sbinfo->si_aopen);
  23491. +
  23492. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  23493. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  23494. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  23495. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  23496. +
  23497. + for (i = 0; i < AuPlink_NHASH; i++)
  23498. + au_sphl_init(sbinfo->si_plink + i);
  23499. + init_waitqueue_head(&sbinfo->si_plink_wq);
  23500. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  23501. +
  23502. + au_sphl_init(&sbinfo->si_files);
  23503. +
  23504. + /* leave other members for sysaufs and si_mnt. */
  23505. + sbinfo->si_sb = sb;
  23506. + sb->s_fs_info = sbinfo;
  23507. + si_pid_set(sb);
  23508. + return 0; /* success */
  23509. +
  23510. +out_br:
  23511. + kfree(sbinfo->si_branch);
  23512. +out_pidmap:
  23513. + kfree(sbinfo->au_si_pid.bitmap);
  23514. +out_sbinfo:
  23515. + kfree(sbinfo);
  23516. +out:
  23517. + return err;
  23518. +}
  23519. +
  23520. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
  23521. +{
  23522. + int err, sz;
  23523. + struct au_branch **brp;
  23524. +
  23525. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  23526. +
  23527. + err = -ENOMEM;
  23528. + sz = sizeof(*brp) * (sbinfo->si_bend + 1);
  23529. + if (unlikely(!sz))
  23530. + sz = sizeof(*brp);
  23531. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
  23532. + if (brp) {
  23533. + sbinfo->si_branch = brp;
  23534. + err = 0;
  23535. + }
  23536. +
  23537. + return err;
  23538. +}
  23539. +
  23540. +/* ---------------------------------------------------------------------- */
  23541. +
  23542. +unsigned int au_sigen_inc(struct super_block *sb)
  23543. +{
  23544. + unsigned int gen;
  23545. +
  23546. + SiMustWriteLock(sb);
  23547. +
  23548. + gen = ++au_sbi(sb)->si_generation;
  23549. + au_update_digen(sb->s_root);
  23550. + au_update_iigen(sb->s_root->d_inode, /*half*/0);
  23551. + sb->s_root->d_inode->i_version++;
  23552. + return gen;
  23553. +}
  23554. +
  23555. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  23556. +{
  23557. + aufs_bindex_t br_id;
  23558. + int i;
  23559. + struct au_sbinfo *sbinfo;
  23560. +
  23561. + SiMustWriteLock(sb);
  23562. +
  23563. + sbinfo = au_sbi(sb);
  23564. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  23565. + br_id = ++sbinfo->si_last_br_id;
  23566. + AuDebugOn(br_id < 0);
  23567. + if (br_id && au_br_index(sb, br_id) < 0)
  23568. + return br_id;
  23569. + }
  23570. +
  23571. + return -1;
  23572. +}
  23573. +
  23574. +/* ---------------------------------------------------------------------- */
  23575. +
  23576. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  23577. +int si_read_lock(struct super_block *sb, int flags)
  23578. +{
  23579. + int err;
  23580. +
  23581. + err = 0;
  23582. + if (au_ftest_lock(flags, FLUSH))
  23583. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  23584. +
  23585. + si_noflush_read_lock(sb);
  23586. + err = au_plink_maint(sb, flags);
  23587. + if (unlikely(err))
  23588. + si_read_unlock(sb);
  23589. +
  23590. + return err;
  23591. +}
  23592. +
  23593. +int si_write_lock(struct super_block *sb, int flags)
  23594. +{
  23595. + int err;
  23596. +
  23597. + if (au_ftest_lock(flags, FLUSH))
  23598. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  23599. +
  23600. + si_noflush_write_lock(sb);
  23601. + err = au_plink_maint(sb, flags);
  23602. + if (unlikely(err))
  23603. + si_write_unlock(sb);
  23604. +
  23605. + return err;
  23606. +}
  23607. +
  23608. +/* dentry and super_block lock. call at entry point */
  23609. +int aufs_read_lock(struct dentry *dentry, int flags)
  23610. +{
  23611. + int err;
  23612. + struct super_block *sb;
  23613. +
  23614. + sb = dentry->d_sb;
  23615. + err = si_read_lock(sb, flags);
  23616. + if (unlikely(err))
  23617. + goto out;
  23618. +
  23619. + if (au_ftest_lock(flags, DW))
  23620. + di_write_lock_child(dentry);
  23621. + else
  23622. + di_read_lock_child(dentry, flags);
  23623. +
  23624. + if (au_ftest_lock(flags, GEN)) {
  23625. + err = au_digen_test(dentry, au_sigen(sb));
  23626. + AuDebugOn(!err && au_dbrange_test(dentry));
  23627. + if (unlikely(err))
  23628. + aufs_read_unlock(dentry, flags);
  23629. + }
  23630. +
  23631. +out:
  23632. + return err;
  23633. +}
  23634. +
  23635. +void aufs_read_unlock(struct dentry *dentry, int flags)
  23636. +{
  23637. + if (au_ftest_lock(flags, DW))
  23638. + di_write_unlock(dentry);
  23639. + else
  23640. + di_read_unlock(dentry, flags);
  23641. + si_read_unlock(dentry->d_sb);
  23642. +}
  23643. +
  23644. +void aufs_write_lock(struct dentry *dentry)
  23645. +{
  23646. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  23647. + di_write_lock_child(dentry);
  23648. +}
  23649. +
  23650. +void aufs_write_unlock(struct dentry *dentry)
  23651. +{
  23652. + di_write_unlock(dentry);
  23653. + si_write_unlock(dentry->d_sb);
  23654. +}
  23655. +
  23656. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  23657. +{
  23658. + int err;
  23659. + unsigned int sigen;
  23660. + struct super_block *sb;
  23661. +
  23662. + sb = d1->d_sb;
  23663. + err = si_read_lock(sb, flags);
  23664. + if (unlikely(err))
  23665. + goto out;
  23666. +
  23667. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
  23668. +
  23669. + if (au_ftest_lock(flags, GEN)) {
  23670. + sigen = au_sigen(sb);
  23671. + err = au_digen_test(d1, sigen);
  23672. + AuDebugOn(!err && au_dbrange_test(d1));
  23673. + if (!err) {
  23674. + err = au_digen_test(d2, sigen);
  23675. + AuDebugOn(!err && au_dbrange_test(d2));
  23676. + }
  23677. + if (unlikely(err))
  23678. + aufs_read_and_write_unlock2(d1, d2);
  23679. + }
  23680. +
  23681. +out:
  23682. + return err;
  23683. +}
  23684. +
  23685. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  23686. +{
  23687. + di_write_unlock2(d1, d2);
  23688. + si_read_unlock(d1->d_sb);
  23689. +}
  23690. +
  23691. +/* ---------------------------------------------------------------------- */
  23692. +
  23693. +int si_pid_test_slow(struct super_block *sb)
  23694. +{
  23695. + void *p;
  23696. +
  23697. + rcu_read_lock();
  23698. + p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
  23699. + rcu_read_unlock();
  23700. +
  23701. + return (long)!!p;
  23702. +}
  23703. +
  23704. +void si_pid_set_slow(struct super_block *sb)
  23705. +{
  23706. + int err;
  23707. + struct au_sbinfo *sbinfo;
  23708. +
  23709. + AuDebugOn(si_pid_test_slow(sb));
  23710. +
  23711. + sbinfo = au_sbi(sb);
  23712. + err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  23713. + AuDebugOn(err);
  23714. + spin_lock(&sbinfo->au_si_pid.tree_lock);
  23715. + err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
  23716. + /*any valid ptr*/sb);
  23717. + spin_unlock(&sbinfo->au_si_pid.tree_lock);
  23718. + AuDebugOn(err);
  23719. + radix_tree_preload_end();
  23720. +}
  23721. +
  23722. +void si_pid_clr_slow(struct super_block *sb)
  23723. +{
  23724. + void *p;
  23725. + struct au_sbinfo *sbinfo;
  23726. +
  23727. + AuDebugOn(!si_pid_test_slow(sb));
  23728. +
  23729. + sbinfo = au_sbi(sb);
  23730. + spin_lock(&sbinfo->au_si_pid.tree_lock);
  23731. + p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
  23732. + spin_unlock(&sbinfo->au_si_pid.tree_lock);
  23733. +}
  23734. diff -Nur linux-4.0.5.orig/fs/aufs/spl.h linux-4.0.5/fs/aufs/spl.h
  23735. --- linux-4.0.5.orig/fs/aufs/spl.h 1969-12-31 18:00:00.000000000 -0600
  23736. +++ linux-4.0.5/fs/aufs/spl.h 2015-06-21 14:07:48.686432781 -0500
  23737. @@ -0,0 +1,111 @@
  23738. +/*
  23739. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23740. + *
  23741. + * This program, aufs is free software; you can redistribute it and/or modify
  23742. + * it under the terms of the GNU General Public License as published by
  23743. + * the Free Software Foundation; either version 2 of the License, or
  23744. + * (at your option) any later version.
  23745. + *
  23746. + * This program is distributed in the hope that it will be useful,
  23747. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23748. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23749. + * GNU General Public License for more details.
  23750. + *
  23751. + * You should have received a copy of the GNU General Public License
  23752. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23753. + */
  23754. +
  23755. +/*
  23756. + * simple list protected by a spinlock
  23757. + */
  23758. +
  23759. +#ifndef __AUFS_SPL_H__
  23760. +#define __AUFS_SPL_H__
  23761. +
  23762. +#ifdef __KERNEL__
  23763. +
  23764. +struct au_splhead {
  23765. + spinlock_t spin;
  23766. + struct list_head head;
  23767. +};
  23768. +
  23769. +static inline void au_spl_init(struct au_splhead *spl)
  23770. +{
  23771. + spin_lock_init(&spl->spin);
  23772. + INIT_LIST_HEAD(&spl->head);
  23773. +}
  23774. +
  23775. +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
  23776. +{
  23777. + spin_lock(&spl->spin);
  23778. + list_add(list, &spl->head);
  23779. + spin_unlock(&spl->spin);
  23780. +}
  23781. +
  23782. +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
  23783. +{
  23784. + spin_lock(&spl->spin);
  23785. + list_del(list);
  23786. + spin_unlock(&spl->spin);
  23787. +}
  23788. +
  23789. +static inline void au_spl_del_rcu(struct list_head *list,
  23790. + struct au_splhead *spl)
  23791. +{
  23792. + spin_lock(&spl->spin);
  23793. + list_del_rcu(list);
  23794. + spin_unlock(&spl->spin);
  23795. +}
  23796. +
  23797. +/* ---------------------------------------------------------------------- */
  23798. +
  23799. +struct au_sphlhead {
  23800. + spinlock_t spin;
  23801. + struct hlist_head head;
  23802. +};
  23803. +
  23804. +static inline void au_sphl_init(struct au_sphlhead *sphl)
  23805. +{
  23806. + spin_lock_init(&sphl->spin);
  23807. + INIT_HLIST_HEAD(&sphl->head);
  23808. +}
  23809. +
  23810. +static inline void au_sphl_add(struct hlist_node *hlist,
  23811. + struct au_sphlhead *sphl)
  23812. +{
  23813. + spin_lock(&sphl->spin);
  23814. + hlist_add_head(hlist, &sphl->head);
  23815. + spin_unlock(&sphl->spin);
  23816. +}
  23817. +
  23818. +static inline void au_sphl_del(struct hlist_node *hlist,
  23819. + struct au_sphlhead *sphl)
  23820. +{
  23821. + spin_lock(&sphl->spin);
  23822. + hlist_del(hlist);
  23823. + spin_unlock(&sphl->spin);
  23824. +}
  23825. +
  23826. +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
  23827. + struct au_sphlhead *sphl)
  23828. +{
  23829. + spin_lock(&sphl->spin);
  23830. + hlist_del_rcu(hlist);
  23831. + spin_unlock(&sphl->spin);
  23832. +}
  23833. +
  23834. +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
  23835. +{
  23836. + unsigned long cnt;
  23837. + struct hlist_node *pos;
  23838. +
  23839. + cnt = 0;
  23840. + spin_lock(&sphl->spin);
  23841. + hlist_for_each(pos, &sphl->head)
  23842. + cnt++;
  23843. + spin_unlock(&sphl->spin);
  23844. + return cnt;
  23845. +}
  23846. +
  23847. +#endif /* __KERNEL__ */
  23848. +#endif /* __AUFS_SPL_H__ */
  23849. diff -Nur linux-4.0.5.orig/fs/aufs/super.c linux-4.0.5/fs/aufs/super.c
  23850. --- linux-4.0.5.orig/fs/aufs/super.c 1969-12-31 18:00:00.000000000 -0600
  23851. +++ linux-4.0.5/fs/aufs/super.c 2015-06-21 14:07:48.686432781 -0500
  23852. @@ -0,0 +1,1006 @@
  23853. +/*
  23854. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23855. + *
  23856. + * This program, aufs is free software; you can redistribute it and/or modify
  23857. + * it under the terms of the GNU General Public License as published by
  23858. + * the Free Software Foundation; either version 2 of the License, or
  23859. + * (at your option) any later version.
  23860. + *
  23861. + * This program is distributed in the hope that it will be useful,
  23862. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23863. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23864. + * GNU General Public License for more details.
  23865. + *
  23866. + * You should have received a copy of the GNU General Public License
  23867. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23868. + */
  23869. +
  23870. +/*
  23871. + * mount and super_block operations
  23872. + */
  23873. +
  23874. +#include <linux/mm.h>
  23875. +#include <linux/seq_file.h>
  23876. +#include <linux/statfs.h>
  23877. +#include <linux/vmalloc.h>
  23878. +#include "aufs.h"
  23879. +
  23880. +/*
  23881. + * super_operations
  23882. + */
  23883. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  23884. +{
  23885. + struct au_icntnr *c;
  23886. +
  23887. + c = au_cache_alloc_icntnr();
  23888. + if (c) {
  23889. + au_icntnr_init(c);
  23890. + c->vfs_inode.i_version = 1; /* sigen(sb); */
  23891. + c->iinfo.ii_hinode = NULL;
  23892. + return &c->vfs_inode;
  23893. + }
  23894. + return NULL;
  23895. +}
  23896. +
  23897. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  23898. +{
  23899. + struct inode *inode = container_of(head, struct inode, i_rcu);
  23900. +
  23901. + INIT_HLIST_HEAD(&inode->i_dentry);
  23902. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  23903. +}
  23904. +
  23905. +static void aufs_destroy_inode(struct inode *inode)
  23906. +{
  23907. + au_iinfo_fin(inode);
  23908. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  23909. +}
  23910. +
  23911. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  23912. +{
  23913. + struct inode *inode;
  23914. + int err;
  23915. +
  23916. + inode = iget_locked(sb, ino);
  23917. + if (unlikely(!inode)) {
  23918. + inode = ERR_PTR(-ENOMEM);
  23919. + goto out;
  23920. + }
  23921. + if (!(inode->i_state & I_NEW))
  23922. + goto out;
  23923. +
  23924. + err = au_xigen_new(inode);
  23925. + if (!err)
  23926. + err = au_iinfo_init(inode);
  23927. + if (!err)
  23928. + inode->i_version++;
  23929. + else {
  23930. + iget_failed(inode);
  23931. + inode = ERR_PTR(err);
  23932. + }
  23933. +
  23934. +out:
  23935. + /* never return NULL */
  23936. + AuDebugOn(!inode);
  23937. + AuTraceErrPtr(inode);
  23938. + return inode;
  23939. +}
  23940. +
  23941. +/* lock free root dinfo */
  23942. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  23943. +{
  23944. + int err;
  23945. + aufs_bindex_t bindex, bend;
  23946. + struct path path;
  23947. + struct au_hdentry *hdp;
  23948. + struct au_branch *br;
  23949. + au_br_perm_str_t perm;
  23950. +
  23951. + err = 0;
  23952. + bend = au_sbend(sb);
  23953. + hdp = au_di(sb->s_root)->di_hdentry;
  23954. + for (bindex = 0; !err && bindex <= bend; bindex++) {
  23955. + br = au_sbr(sb, bindex);
  23956. + path.mnt = au_br_mnt(br);
  23957. + path.dentry = hdp[bindex].hd_dentry;
  23958. + err = au_seq_path(seq, &path);
  23959. + if (err > 0) {
  23960. + au_optstr_br_perm(&perm, br->br_perm);
  23961. + err = seq_printf(seq, "=%s", perm.a);
  23962. + if (err == -1)
  23963. + err = -E2BIG;
  23964. + }
  23965. + if (!err && bindex != bend)
  23966. + err = seq_putc(seq, ':');
  23967. + }
  23968. +
  23969. + return err;
  23970. +}
  23971. +
  23972. +static void au_show_wbr_create(struct seq_file *m, int v,
  23973. + struct au_sbinfo *sbinfo)
  23974. +{
  23975. + const char *pat;
  23976. +
  23977. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  23978. +
  23979. + seq_puts(m, ",create=");
  23980. + pat = au_optstr_wbr_create(v);
  23981. + switch (v) {
  23982. + case AuWbrCreate_TDP:
  23983. + case AuWbrCreate_RR:
  23984. + case AuWbrCreate_MFS:
  23985. + case AuWbrCreate_PMFS:
  23986. + seq_puts(m, pat);
  23987. + break;
  23988. + case AuWbrCreate_MFSV:
  23989. + seq_printf(m, /*pat*/"mfs:%lu",
  23990. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  23991. + / MSEC_PER_SEC);
  23992. + break;
  23993. + case AuWbrCreate_PMFSV:
  23994. + seq_printf(m, /*pat*/"pmfs:%lu",
  23995. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  23996. + / MSEC_PER_SEC);
  23997. + break;
  23998. + case AuWbrCreate_MFSRR:
  23999. + seq_printf(m, /*pat*/"mfsrr:%llu",
  24000. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  24001. + break;
  24002. + case AuWbrCreate_MFSRRV:
  24003. + seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
  24004. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  24005. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  24006. + / MSEC_PER_SEC);
  24007. + break;
  24008. + case AuWbrCreate_PMFSRR:
  24009. + seq_printf(m, /*pat*/"pmfsrr:%llu",
  24010. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  24011. + break;
  24012. + case AuWbrCreate_PMFSRRV:
  24013. + seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
  24014. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  24015. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  24016. + / MSEC_PER_SEC);
  24017. + break;
  24018. + }
  24019. +}
  24020. +
  24021. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  24022. +{
  24023. +#ifdef CONFIG_SYSFS
  24024. + return 0;
  24025. +#else
  24026. + int err;
  24027. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  24028. + aufs_bindex_t bindex, brid;
  24029. + struct qstr *name;
  24030. + struct file *f;
  24031. + struct dentry *d, *h_root;
  24032. + struct au_hdentry *hdp;
  24033. +
  24034. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  24035. +
  24036. + err = 0;
  24037. + f = au_sbi(sb)->si_xib;
  24038. + if (!f)
  24039. + goto out;
  24040. +
  24041. + /* stop printing the default xino path on the first writable branch */
  24042. + h_root = NULL;
  24043. + brid = au_xino_brid(sb);
  24044. + if (brid >= 0) {
  24045. + bindex = au_br_index(sb, brid);
  24046. + hdp = au_di(sb->s_root)->di_hdentry;
  24047. + h_root = hdp[0 + bindex].hd_dentry;
  24048. + }
  24049. + d = f->f_path.dentry;
  24050. + name = &d->d_name;
  24051. + /* safe ->d_parent because the file is unlinked */
  24052. + if (d->d_parent == h_root
  24053. + && name->len == len
  24054. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  24055. + goto out;
  24056. +
  24057. + seq_puts(seq, ",xino=");
  24058. + err = au_xino_path(seq, f);
  24059. +
  24060. +out:
  24061. + return err;
  24062. +#endif
  24063. +}
  24064. +
  24065. +/* seq_file will re-call me in case of too long string */
  24066. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  24067. +{
  24068. + int err;
  24069. + unsigned int mnt_flags, v;
  24070. + struct super_block *sb;
  24071. + struct au_sbinfo *sbinfo;
  24072. +
  24073. +#define AuBool(name, str) do { \
  24074. + v = au_opt_test(mnt_flags, name); \
  24075. + if (v != au_opt_test(AuOpt_Def, name)) \
  24076. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  24077. +} while (0)
  24078. +
  24079. +#define AuStr(name, str) do { \
  24080. + v = mnt_flags & AuOptMask_##name; \
  24081. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  24082. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  24083. +} while (0)
  24084. +
  24085. +#define AuUInt(name, str, val) do { \
  24086. + if (val != AUFS_##name##_DEF) \
  24087. + seq_printf(m, "," #str "=%u", val); \
  24088. +} while (0)
  24089. +
  24090. + sb = dentry->d_sb;
  24091. + if (sb->s_flags & MS_POSIXACL)
  24092. + seq_puts(m, ",acl");
  24093. +
  24094. + /* lock free root dinfo */
  24095. + si_noflush_read_lock(sb);
  24096. + sbinfo = au_sbi(sb);
  24097. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  24098. +
  24099. + mnt_flags = au_mntflags(sb);
  24100. + if (au_opt_test(mnt_flags, XINO)) {
  24101. + err = au_show_xino(m, sb);
  24102. + if (unlikely(err))
  24103. + goto out;
  24104. + } else
  24105. + seq_puts(m, ",noxino");
  24106. +
  24107. + AuBool(TRUNC_XINO, trunc_xino);
  24108. + AuStr(UDBA, udba);
  24109. + AuBool(SHWH, shwh);
  24110. + AuBool(PLINK, plink);
  24111. + AuBool(DIO, dio);
  24112. + AuBool(DIRPERM1, dirperm1);
  24113. +
  24114. + v = sbinfo->si_wbr_create;
  24115. + if (v != AuWbrCreate_Def)
  24116. + au_show_wbr_create(m, v, sbinfo);
  24117. +
  24118. + v = sbinfo->si_wbr_copyup;
  24119. + if (v != AuWbrCopyup_Def)
  24120. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  24121. +
  24122. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  24123. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  24124. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  24125. +
  24126. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  24127. +
  24128. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  24129. + AuUInt(RDCACHE, rdcache, v);
  24130. +
  24131. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  24132. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  24133. +
  24134. + au_fhsm_show(m, sbinfo);
  24135. +
  24136. + AuBool(SUM, sum);
  24137. + /* AuBool(SUM_W, wsum); */
  24138. + AuBool(WARN_PERM, warn_perm);
  24139. + AuBool(VERBOSE, verbose);
  24140. +
  24141. +out:
  24142. + /* be sure to print "br:" last */
  24143. + if (!sysaufs_brs) {
  24144. + seq_puts(m, ",br:");
  24145. + au_show_brs(m, sb);
  24146. + }
  24147. + si_read_unlock(sb);
  24148. + return 0;
  24149. +
  24150. +#undef AuBool
  24151. +#undef AuStr
  24152. +#undef AuUInt
  24153. +}
  24154. +
  24155. +/* ---------------------------------------------------------------------- */
  24156. +
  24157. +/* sum mode which returns the summation for statfs(2) */
  24158. +
  24159. +static u64 au_add_till_max(u64 a, u64 b)
  24160. +{
  24161. + u64 old;
  24162. +
  24163. + old = a;
  24164. + a += b;
  24165. + if (old <= a)
  24166. + return a;
  24167. + return ULLONG_MAX;
  24168. +}
  24169. +
  24170. +static u64 au_mul_till_max(u64 a, long mul)
  24171. +{
  24172. + u64 old;
  24173. +
  24174. + old = a;
  24175. + a *= mul;
  24176. + if (old <= a)
  24177. + return a;
  24178. + return ULLONG_MAX;
  24179. +}
  24180. +
  24181. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  24182. +{
  24183. + int err;
  24184. + long bsize, factor;
  24185. + u64 blocks, bfree, bavail, files, ffree;
  24186. + aufs_bindex_t bend, bindex, i;
  24187. + unsigned char shared;
  24188. + struct path h_path;
  24189. + struct super_block *h_sb;
  24190. +
  24191. + err = 0;
  24192. + bsize = LONG_MAX;
  24193. + files = 0;
  24194. + ffree = 0;
  24195. + blocks = 0;
  24196. + bfree = 0;
  24197. + bavail = 0;
  24198. + bend = au_sbend(sb);
  24199. + for (bindex = 0; bindex <= bend; bindex++) {
  24200. + h_path.mnt = au_sbr_mnt(sb, bindex);
  24201. + h_sb = h_path.mnt->mnt_sb;
  24202. + shared = 0;
  24203. + for (i = 0; !shared && i < bindex; i++)
  24204. + shared = (au_sbr_sb(sb, i) == h_sb);
  24205. + if (shared)
  24206. + continue;
  24207. +
  24208. + /* sb->s_root for NFS is unreliable */
  24209. + h_path.dentry = h_path.mnt->mnt_root;
  24210. + err = vfs_statfs(&h_path, buf);
  24211. + if (unlikely(err))
  24212. + goto out;
  24213. +
  24214. + if (bsize > buf->f_bsize) {
  24215. + /*
  24216. + * we will reduce bsize, so we have to expand blocks
  24217. + * etc. to match them again
  24218. + */
  24219. + factor = (bsize / buf->f_bsize);
  24220. + blocks = au_mul_till_max(blocks, factor);
  24221. + bfree = au_mul_till_max(bfree, factor);
  24222. + bavail = au_mul_till_max(bavail, factor);
  24223. + bsize = buf->f_bsize;
  24224. + }
  24225. +
  24226. + factor = (buf->f_bsize / bsize);
  24227. + blocks = au_add_till_max(blocks,
  24228. + au_mul_till_max(buf->f_blocks, factor));
  24229. + bfree = au_add_till_max(bfree,
  24230. + au_mul_till_max(buf->f_bfree, factor));
  24231. + bavail = au_add_till_max(bavail,
  24232. + au_mul_till_max(buf->f_bavail, factor));
  24233. + files = au_add_till_max(files, buf->f_files);
  24234. + ffree = au_add_till_max(ffree, buf->f_ffree);
  24235. + }
  24236. +
  24237. + buf->f_bsize = bsize;
  24238. + buf->f_blocks = blocks;
  24239. + buf->f_bfree = bfree;
  24240. + buf->f_bavail = bavail;
  24241. + buf->f_files = files;
  24242. + buf->f_ffree = ffree;
  24243. + buf->f_frsize = 0;
  24244. +
  24245. +out:
  24246. + return err;
  24247. +}
  24248. +
  24249. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  24250. +{
  24251. + int err;
  24252. + struct path h_path;
  24253. + struct super_block *sb;
  24254. +
  24255. + /* lock free root dinfo */
  24256. + sb = dentry->d_sb;
  24257. + si_noflush_read_lock(sb);
  24258. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  24259. + /* sb->s_root for NFS is unreliable */
  24260. + h_path.mnt = au_sbr_mnt(sb, 0);
  24261. + h_path.dentry = h_path.mnt->mnt_root;
  24262. + err = vfs_statfs(&h_path, buf);
  24263. + } else
  24264. + err = au_statfs_sum(sb, buf);
  24265. + si_read_unlock(sb);
  24266. +
  24267. + if (!err) {
  24268. + buf->f_type = AUFS_SUPER_MAGIC;
  24269. + buf->f_namelen = AUFS_MAX_NAMELEN;
  24270. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  24271. + }
  24272. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  24273. +
  24274. + return err;
  24275. +}
  24276. +
  24277. +/* ---------------------------------------------------------------------- */
  24278. +
  24279. +static int aufs_sync_fs(struct super_block *sb, int wait)
  24280. +{
  24281. + int err, e;
  24282. + aufs_bindex_t bend, bindex;
  24283. + struct au_branch *br;
  24284. + struct super_block *h_sb;
  24285. +
  24286. + err = 0;
  24287. + si_noflush_read_lock(sb);
  24288. + bend = au_sbend(sb);
  24289. + for (bindex = 0; bindex <= bend; bindex++) {
  24290. + br = au_sbr(sb, bindex);
  24291. + if (!au_br_writable(br->br_perm))
  24292. + continue;
  24293. +
  24294. + h_sb = au_sbr_sb(sb, bindex);
  24295. + if (h_sb->s_op->sync_fs) {
  24296. + e = h_sb->s_op->sync_fs(h_sb, wait);
  24297. + if (unlikely(e && !err))
  24298. + err = e;
  24299. + /* go on even if an error happens */
  24300. + }
  24301. + }
  24302. + si_read_unlock(sb);
  24303. +
  24304. + return err;
  24305. +}
  24306. +
  24307. +/* ---------------------------------------------------------------------- */
  24308. +
  24309. +/* final actions when unmounting a file system */
  24310. +static void aufs_put_super(struct super_block *sb)
  24311. +{
  24312. + struct au_sbinfo *sbinfo;
  24313. +
  24314. + sbinfo = au_sbi(sb);
  24315. + if (!sbinfo)
  24316. + return;
  24317. +
  24318. + dbgaufs_si_fin(sbinfo);
  24319. + kobject_put(&sbinfo->si_kobj);
  24320. +}
  24321. +
  24322. +/* ---------------------------------------------------------------------- */
  24323. +
  24324. +void au_array_free(void *array)
  24325. +{
  24326. + if (array) {
  24327. + if (!is_vmalloc_addr(array))
  24328. + kfree(array);
  24329. + else
  24330. + vfree(array);
  24331. + }
  24332. +}
  24333. +
  24334. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
  24335. +{
  24336. + void *array;
  24337. + unsigned long long n, sz;
  24338. +
  24339. + array = NULL;
  24340. + n = 0;
  24341. + if (!*hint)
  24342. + goto out;
  24343. +
  24344. + if (*hint > ULLONG_MAX / sizeof(array)) {
  24345. + array = ERR_PTR(-EMFILE);
  24346. + pr_err("hint %llu\n", *hint);
  24347. + goto out;
  24348. + }
  24349. +
  24350. + sz = sizeof(array) * *hint;
  24351. + array = kzalloc(sz, GFP_NOFS);
  24352. + if (unlikely(!array))
  24353. + array = vzalloc(sz);
  24354. + if (unlikely(!array)) {
  24355. + array = ERR_PTR(-ENOMEM);
  24356. + goto out;
  24357. + }
  24358. +
  24359. + n = cb(array, *hint, arg);
  24360. + AuDebugOn(n > *hint);
  24361. +
  24362. +out:
  24363. + *hint = n;
  24364. + return array;
  24365. +}
  24366. +
  24367. +static unsigned long long au_iarray_cb(void *a,
  24368. + unsigned long long max __maybe_unused,
  24369. + void *arg)
  24370. +{
  24371. + unsigned long long n;
  24372. + struct inode **p, *inode;
  24373. + struct list_head *head;
  24374. +
  24375. + n = 0;
  24376. + p = a;
  24377. + head = arg;
  24378. + spin_lock(&inode_sb_list_lock);
  24379. + list_for_each_entry(inode, head, i_sb_list) {
  24380. + if (!is_bad_inode(inode)
  24381. + && au_ii(inode)->ii_bstart >= 0) {
  24382. + spin_lock(&inode->i_lock);
  24383. + if (atomic_read(&inode->i_count)) {
  24384. + au_igrab(inode);
  24385. + *p++ = inode;
  24386. + n++;
  24387. + AuDebugOn(n > max);
  24388. + }
  24389. + spin_unlock(&inode->i_lock);
  24390. + }
  24391. + }
  24392. + spin_unlock(&inode_sb_list_lock);
  24393. +
  24394. + return n;
  24395. +}
  24396. +
  24397. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  24398. +{
  24399. + *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
  24400. + return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
  24401. +}
  24402. +
  24403. +void au_iarray_free(struct inode **a, unsigned long long max)
  24404. +{
  24405. + unsigned long long ull;
  24406. +
  24407. + for (ull = 0; ull < max; ull++)
  24408. + iput(a[ull]);
  24409. + au_array_free(a);
  24410. +}
  24411. +
  24412. +/* ---------------------------------------------------------------------- */
  24413. +
  24414. +/*
  24415. + * refresh dentry and inode at remount time.
  24416. + */
  24417. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  24418. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  24419. + struct dentry *parent)
  24420. +{
  24421. + int err;
  24422. +
  24423. + di_write_lock_child(dentry);
  24424. + di_read_lock_parent(parent, AuLock_IR);
  24425. + err = au_refresh_dentry(dentry, parent);
  24426. + if (!err && dir_flags)
  24427. + au_hn_reset(dentry->d_inode, dir_flags);
  24428. + di_read_unlock(parent, AuLock_IR);
  24429. + di_write_unlock(dentry);
  24430. +
  24431. + return err;
  24432. +}
  24433. +
  24434. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  24435. + struct au_sbinfo *sbinfo,
  24436. + const unsigned int dir_flags)
  24437. +{
  24438. + int err;
  24439. + struct dentry *parent;
  24440. + struct inode *inode;
  24441. +
  24442. + err = 0;
  24443. + parent = dget_parent(dentry);
  24444. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  24445. + inode = dentry->d_inode;
  24446. + if (inode) {
  24447. + if (!S_ISDIR(inode->i_mode))
  24448. + err = au_do_refresh(dentry, /*dir_flags*/0,
  24449. + parent);
  24450. + else {
  24451. + err = au_do_refresh(dentry, dir_flags, parent);
  24452. + if (unlikely(err))
  24453. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  24454. + }
  24455. + } else
  24456. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  24457. + AuDbgDentry(dentry);
  24458. + }
  24459. + dput(parent);
  24460. +
  24461. + AuTraceErr(err);
  24462. + return err;
  24463. +}
  24464. +
  24465. +static int au_refresh_d(struct super_block *sb)
  24466. +{
  24467. + int err, i, j, ndentry, e;
  24468. + unsigned int sigen;
  24469. + struct au_dcsub_pages dpages;
  24470. + struct au_dpage *dpage;
  24471. + struct dentry **dentries, *d;
  24472. + struct au_sbinfo *sbinfo;
  24473. + struct dentry *root = sb->s_root;
  24474. + const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
  24475. +
  24476. + err = au_dpages_init(&dpages, GFP_NOFS);
  24477. + if (unlikely(err))
  24478. + goto out;
  24479. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  24480. + if (unlikely(err))
  24481. + goto out_dpages;
  24482. +
  24483. + sigen = au_sigen(sb);
  24484. + sbinfo = au_sbi(sb);
  24485. + for (i = 0; i < dpages.ndpage; i++) {
  24486. + dpage = dpages.dpages + i;
  24487. + dentries = dpage->dentries;
  24488. + ndentry = dpage->ndentry;
  24489. + for (j = 0; j < ndentry; j++) {
  24490. + d = dentries[j];
  24491. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
  24492. + if (unlikely(e && !err))
  24493. + err = e;
  24494. + /* go on even err */
  24495. + }
  24496. + }
  24497. +
  24498. +out_dpages:
  24499. + au_dpages_free(&dpages);
  24500. +out:
  24501. + return err;
  24502. +}
  24503. +
  24504. +static int au_refresh_i(struct super_block *sb)
  24505. +{
  24506. + int err, e;
  24507. + unsigned int sigen;
  24508. + unsigned long long max, ull;
  24509. + struct inode *inode, **array;
  24510. +
  24511. + array = au_iarray_alloc(sb, &max);
  24512. + err = PTR_ERR(array);
  24513. + if (IS_ERR(array))
  24514. + goto out;
  24515. +
  24516. + err = 0;
  24517. + sigen = au_sigen(sb);
  24518. + for (ull = 0; ull < max; ull++) {
  24519. + inode = array[ull];
  24520. + if (unlikely(!inode))
  24521. + break;
  24522. + if (au_iigen(inode, NULL) != sigen) {
  24523. + ii_write_lock_child(inode);
  24524. + e = au_refresh_hinode_self(inode);
  24525. + ii_write_unlock(inode);
  24526. + if (unlikely(e)) {
  24527. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  24528. + if (!err)
  24529. + err = e;
  24530. + /* go on even if err */
  24531. + }
  24532. + }
  24533. + }
  24534. +
  24535. + au_iarray_free(array, max);
  24536. +
  24537. +out:
  24538. + return err;
  24539. +}
  24540. +
  24541. +static void au_remount_refresh(struct super_block *sb)
  24542. +{
  24543. + int err, e;
  24544. + unsigned int udba;
  24545. + aufs_bindex_t bindex, bend;
  24546. + struct dentry *root;
  24547. + struct inode *inode;
  24548. + struct au_branch *br;
  24549. +
  24550. + au_sigen_inc(sb);
  24551. + au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
  24552. +
  24553. + root = sb->s_root;
  24554. + DiMustNoWaiters(root);
  24555. + inode = root->d_inode;
  24556. + IiMustNoWaiters(inode);
  24557. +
  24558. + udba = au_opt_udba(sb);
  24559. + bend = au_sbend(sb);
  24560. + for (bindex = 0; bindex <= bend; bindex++) {
  24561. + br = au_sbr(sb, bindex);
  24562. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  24563. + if (unlikely(err))
  24564. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  24565. + bindex, err);
  24566. + /* go on even if err */
  24567. + }
  24568. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  24569. +
  24570. + di_write_unlock(root);
  24571. + err = au_refresh_d(sb);
  24572. + e = au_refresh_i(sb);
  24573. + if (unlikely(e && !err))
  24574. + err = e;
  24575. + /* aufs_write_lock() calls ..._child() */
  24576. + di_write_lock_child(root);
  24577. +
  24578. + au_cpup_attr_all(inode, /*force*/1);
  24579. +
  24580. + if (unlikely(err))
  24581. + AuIOErr("refresh failed, ignored, %d\n", err);
  24582. +}
  24583. +
  24584. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  24585. +static int cvt_err(int err)
  24586. +{
  24587. + AuTraceErr(err);
  24588. +
  24589. + switch (err) {
  24590. + case -ENOENT:
  24591. + case -ENOTDIR:
  24592. + case -EEXIST:
  24593. + case -EIO:
  24594. + err = -EINVAL;
  24595. + }
  24596. + return err;
  24597. +}
  24598. +
  24599. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  24600. +{
  24601. + int err, do_dx;
  24602. + unsigned int mntflags;
  24603. + struct au_opts opts;
  24604. + struct dentry *root;
  24605. + struct inode *inode;
  24606. + struct au_sbinfo *sbinfo;
  24607. +
  24608. + err = 0;
  24609. + root = sb->s_root;
  24610. + if (!data || !*data) {
  24611. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  24612. + if (!err) {
  24613. + di_write_lock_child(root);
  24614. + err = au_opts_verify(sb, *flags, /*pending*/0);
  24615. + aufs_write_unlock(root);
  24616. + }
  24617. + goto out;
  24618. + }
  24619. +
  24620. + err = -ENOMEM;
  24621. + memset(&opts, 0, sizeof(opts));
  24622. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  24623. + if (unlikely(!opts.opt))
  24624. + goto out;
  24625. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  24626. + opts.flags = AuOpts_REMOUNT;
  24627. + opts.sb_flags = *flags;
  24628. +
  24629. + /* parse it before aufs lock */
  24630. + err = au_opts_parse(sb, data, &opts);
  24631. + if (unlikely(err))
  24632. + goto out_opts;
  24633. +
  24634. + sbinfo = au_sbi(sb);
  24635. + inode = root->d_inode;
  24636. + mutex_lock(&inode->i_mutex);
  24637. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  24638. + if (unlikely(err))
  24639. + goto out_mtx;
  24640. + di_write_lock_child(root);
  24641. +
  24642. + /* au_opts_remount() may return an error */
  24643. + err = au_opts_remount(sb, &opts);
  24644. + au_opts_free(&opts);
  24645. +
  24646. + if (au_ftest_opts(opts.flags, REFRESH))
  24647. + au_remount_refresh(sb);
  24648. +
  24649. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  24650. + mntflags = au_mntflags(sb);
  24651. + do_dx = !!au_opt_test(mntflags, DIO);
  24652. + au_dy_arefresh(do_dx);
  24653. + }
  24654. +
  24655. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  24656. + aufs_write_unlock(root);
  24657. +
  24658. +out_mtx:
  24659. + mutex_unlock(&inode->i_mutex);
  24660. +out_opts:
  24661. + free_page((unsigned long)opts.opt);
  24662. +out:
  24663. + err = cvt_err(err);
  24664. + AuTraceErr(err);
  24665. + return err;
  24666. +}
  24667. +
  24668. +static const struct super_operations aufs_sop = {
  24669. + .alloc_inode = aufs_alloc_inode,
  24670. + .destroy_inode = aufs_destroy_inode,
  24671. + /* always deleting, no clearing */
  24672. + .drop_inode = generic_delete_inode,
  24673. + .show_options = aufs_show_options,
  24674. + .statfs = aufs_statfs,
  24675. + .put_super = aufs_put_super,
  24676. + .sync_fs = aufs_sync_fs,
  24677. + .remount_fs = aufs_remount_fs
  24678. +};
  24679. +
  24680. +/* ---------------------------------------------------------------------- */
  24681. +
  24682. +static int alloc_root(struct super_block *sb)
  24683. +{
  24684. + int err;
  24685. + struct inode *inode;
  24686. + struct dentry *root;
  24687. +
  24688. + err = -ENOMEM;
  24689. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  24690. + err = PTR_ERR(inode);
  24691. + if (IS_ERR(inode))
  24692. + goto out;
  24693. +
  24694. + inode->i_op = &aufs_dir_iop;
  24695. + inode->i_fop = &aufs_dir_fop;
  24696. + inode->i_mode = S_IFDIR;
  24697. + set_nlink(inode, 2);
  24698. + unlock_new_inode(inode);
  24699. +
  24700. + root = d_make_root(inode);
  24701. + if (unlikely(!root))
  24702. + goto out;
  24703. + err = PTR_ERR(root);
  24704. + if (IS_ERR(root))
  24705. + goto out;
  24706. +
  24707. + err = au_di_init(root);
  24708. + if (!err) {
  24709. + sb->s_root = root;
  24710. + return 0; /* success */
  24711. + }
  24712. + dput(root);
  24713. +
  24714. +out:
  24715. + return err;
  24716. +}
  24717. +
  24718. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  24719. + int silent __maybe_unused)
  24720. +{
  24721. + int err;
  24722. + struct au_opts opts;
  24723. + struct dentry *root;
  24724. + struct inode *inode;
  24725. + char *arg = raw_data;
  24726. +
  24727. + if (unlikely(!arg || !*arg)) {
  24728. + err = -EINVAL;
  24729. + pr_err("no arg\n");
  24730. + goto out;
  24731. + }
  24732. +
  24733. + err = -ENOMEM;
  24734. + memset(&opts, 0, sizeof(opts));
  24735. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  24736. + if (unlikely(!opts.opt))
  24737. + goto out;
  24738. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  24739. + opts.sb_flags = sb->s_flags;
  24740. +
  24741. + err = au_si_alloc(sb);
  24742. + if (unlikely(err))
  24743. + goto out_opts;
  24744. +
  24745. + /* all timestamps always follow the ones on the branch */
  24746. + sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
  24747. + sb->s_op = &aufs_sop;
  24748. + sb->s_d_op = &aufs_dop;
  24749. + sb->s_magic = AUFS_SUPER_MAGIC;
  24750. + sb->s_maxbytes = 0;
  24751. + sb->s_stack_depth = 1;
  24752. + au_export_init(sb);
  24753. + /* au_xattr_init(sb); */
  24754. +
  24755. + err = alloc_root(sb);
  24756. + if (unlikely(err)) {
  24757. + si_write_unlock(sb);
  24758. + goto out_info;
  24759. + }
  24760. + root = sb->s_root;
  24761. + inode = root->d_inode;
  24762. +
  24763. + /*
  24764. + * actually we can parse options regardless aufs lock here.
  24765. + * but at remount time, parsing must be done before aufs lock.
  24766. + * so we follow the same rule.
  24767. + */
  24768. + ii_write_lock_parent(inode);
  24769. + aufs_write_unlock(root);
  24770. + err = au_opts_parse(sb, arg, &opts);
  24771. + if (unlikely(err))
  24772. + goto out_root;
  24773. +
  24774. + /* lock vfs_inode first, then aufs. */
  24775. + mutex_lock(&inode->i_mutex);
  24776. + aufs_write_lock(root);
  24777. + err = au_opts_mount(sb, &opts);
  24778. + au_opts_free(&opts);
  24779. + aufs_write_unlock(root);
  24780. + mutex_unlock(&inode->i_mutex);
  24781. + if (!err)
  24782. + goto out_opts; /* success */
  24783. +
  24784. +out_root:
  24785. + dput(root);
  24786. + sb->s_root = NULL;
  24787. +out_info:
  24788. + dbgaufs_si_fin(au_sbi(sb));
  24789. + kobject_put(&au_sbi(sb)->si_kobj);
  24790. + sb->s_fs_info = NULL;
  24791. +out_opts:
  24792. + free_page((unsigned long)opts.opt);
  24793. +out:
  24794. + AuTraceErr(err);
  24795. + err = cvt_err(err);
  24796. + AuTraceErr(err);
  24797. + return err;
  24798. +}
  24799. +
  24800. +/* ---------------------------------------------------------------------- */
  24801. +
  24802. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  24803. + const char *dev_name __maybe_unused,
  24804. + void *raw_data)
  24805. +{
  24806. + struct dentry *root;
  24807. + struct super_block *sb;
  24808. +
  24809. + /* all timestamps always follow the ones on the branch */
  24810. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  24811. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  24812. + if (IS_ERR(root))
  24813. + goto out;
  24814. +
  24815. + sb = root->d_sb;
  24816. + si_write_lock(sb, !AuLock_FLUSH);
  24817. + sysaufs_brs_add(sb, 0);
  24818. + si_write_unlock(sb);
  24819. + au_sbilist_add(sb);
  24820. +
  24821. +out:
  24822. + return root;
  24823. +}
  24824. +
  24825. +static void aufs_kill_sb(struct super_block *sb)
  24826. +{
  24827. + struct au_sbinfo *sbinfo;
  24828. +
  24829. + sbinfo = au_sbi(sb);
  24830. + if (sbinfo) {
  24831. + au_sbilist_del(sb);
  24832. + aufs_write_lock(sb->s_root);
  24833. + au_fhsm_fin(sb);
  24834. + if (sbinfo->si_wbr_create_ops->fin)
  24835. + sbinfo->si_wbr_create_ops->fin(sb);
  24836. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  24837. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  24838. + au_remount_refresh(sb);
  24839. + }
  24840. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  24841. + au_plink_put(sb, /*verbose*/1);
  24842. + au_xino_clr(sb);
  24843. + sbinfo->si_sb = NULL;
  24844. + aufs_write_unlock(sb->s_root);
  24845. + au_nwt_flush(&sbinfo->si_nowait);
  24846. + }
  24847. + kill_anon_super(sb);
  24848. +}
  24849. +
  24850. +struct file_system_type aufs_fs_type = {
  24851. + .name = AUFS_FSTYPE,
  24852. + /* a race between rename and others */
  24853. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  24854. + .mount = aufs_mount,
  24855. + .kill_sb = aufs_kill_sb,
  24856. + /* no need to __module_get() and module_put(). */
  24857. + .owner = THIS_MODULE,
  24858. +};
  24859. diff -Nur linux-4.0.5.orig/fs/aufs/super.h linux-4.0.5/fs/aufs/super.h
  24860. --- linux-4.0.5.orig/fs/aufs/super.h 1969-12-31 18:00:00.000000000 -0600
  24861. +++ linux-4.0.5/fs/aufs/super.h 2015-06-21 14:07:48.686432781 -0500
  24862. @@ -0,0 +1,639 @@
  24863. +/*
  24864. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24865. + *
  24866. + * This program, aufs is free software; you can redistribute it and/or modify
  24867. + * it under the terms of the GNU General Public License as published by
  24868. + * the Free Software Foundation; either version 2 of the License, or
  24869. + * (at your option) any later version.
  24870. + *
  24871. + * This program is distributed in the hope that it will be useful,
  24872. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24873. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24874. + * GNU General Public License for more details.
  24875. + *
  24876. + * You should have received a copy of the GNU General Public License
  24877. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24878. + */
  24879. +
  24880. +/*
  24881. + * super_block operations
  24882. + */
  24883. +
  24884. +#ifndef __AUFS_SUPER_H__
  24885. +#define __AUFS_SUPER_H__
  24886. +
  24887. +#ifdef __KERNEL__
  24888. +
  24889. +#include <linux/fs.h>
  24890. +#include <linux/kobject.h>
  24891. +#include "rwsem.h"
  24892. +#include "spl.h"
  24893. +#include "wkq.h"
  24894. +
  24895. +typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
  24896. +typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
  24897. + loff_t *);
  24898. +
  24899. +/* policies to select one among multiple writable branches */
  24900. +struct au_wbr_copyup_operations {
  24901. + int (*copyup)(struct dentry *dentry);
  24902. +};
  24903. +
  24904. +#define AuWbr_DIR 1 /* target is a dir */
  24905. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  24906. +
  24907. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  24908. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  24909. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  24910. +
  24911. +struct au_wbr_create_operations {
  24912. + int (*create)(struct dentry *dentry, unsigned int flags);
  24913. + int (*init)(struct super_block *sb);
  24914. + int (*fin)(struct super_block *sb);
  24915. +};
  24916. +
  24917. +struct au_wbr_mfs {
  24918. + struct mutex mfs_lock; /* protect this structure */
  24919. + unsigned long mfs_jiffy;
  24920. + unsigned long mfs_expire;
  24921. + aufs_bindex_t mfs_bindex;
  24922. +
  24923. + unsigned long long mfsrr_bytes;
  24924. + unsigned long long mfsrr_watermark;
  24925. +};
  24926. +
  24927. +struct pseudo_link {
  24928. + union {
  24929. + struct hlist_node hlist;
  24930. + struct rcu_head rcu;
  24931. + };
  24932. + struct inode *inode;
  24933. +};
  24934. +
  24935. +#define AuPlink_NHASH 100
  24936. +static inline int au_plink_hash(ino_t ino)
  24937. +{
  24938. + return ino % AuPlink_NHASH;
  24939. +}
  24940. +
  24941. +/* File-based Hierarchical Storage Management */
  24942. +struct au_fhsm {
  24943. +#ifdef CONFIG_AUFS_FHSM
  24944. + /* allow only one process who can receive the notification */
  24945. + spinlock_t fhsm_spin;
  24946. + pid_t fhsm_pid;
  24947. + wait_queue_head_t fhsm_wqh;
  24948. + atomic_t fhsm_readable;
  24949. +
  24950. + /* these are protected by si_rwsem */
  24951. + unsigned long fhsm_expire;
  24952. + aufs_bindex_t fhsm_bottom;
  24953. +#endif
  24954. +};
  24955. +
  24956. +struct au_branch;
  24957. +struct au_sbinfo {
  24958. + /* nowait tasks in the system-wide workqueue */
  24959. + struct au_nowait_tasks si_nowait;
  24960. +
  24961. + /*
  24962. + * tried sb->s_umount, but failed due to the dependecy between i_mutex.
  24963. + * rwsem for au_sbinfo is necessary.
  24964. + */
  24965. + struct au_rwsem si_rwsem;
  24966. +
  24967. + /* prevent recursive locking in deleting inode */
  24968. + struct {
  24969. + unsigned long *bitmap;
  24970. + spinlock_t tree_lock;
  24971. + struct radix_tree_root tree;
  24972. + } au_si_pid;
  24973. +
  24974. + /*
  24975. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  24976. + * remount.
  24977. + */
  24978. + atomic_long_t si_ninodes, si_nfiles;
  24979. +
  24980. + /* branch management */
  24981. + unsigned int si_generation;
  24982. +
  24983. + /* see AuSi_ flags */
  24984. + unsigned char au_si_status;
  24985. +
  24986. + aufs_bindex_t si_bend;
  24987. +
  24988. + /* dirty trick to keep br_id plus */
  24989. + unsigned int si_last_br_id :
  24990. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  24991. + struct au_branch **si_branch;
  24992. +
  24993. + /* policy to select a writable branch */
  24994. + unsigned char si_wbr_copyup;
  24995. + unsigned char si_wbr_create;
  24996. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  24997. + struct au_wbr_create_operations *si_wbr_create_ops;
  24998. +
  24999. + /* round robin */
  25000. + atomic_t si_wbr_rr_next;
  25001. +
  25002. + /* most free space */
  25003. + struct au_wbr_mfs si_wbr_mfs;
  25004. +
  25005. + /* File-based Hierarchical Storage Management */
  25006. + struct au_fhsm si_fhsm;
  25007. +
  25008. + /* mount flags */
  25009. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  25010. + unsigned int si_mntflags;
  25011. +
  25012. + /* external inode number (bitmap and translation table) */
  25013. + au_readf_t si_xread;
  25014. + au_writef_t si_xwrite;
  25015. + struct file *si_xib;
  25016. + struct mutex si_xib_mtx; /* protect xib members */
  25017. + unsigned long *si_xib_buf;
  25018. + unsigned long si_xib_last_pindex;
  25019. + int si_xib_next_bit;
  25020. + aufs_bindex_t si_xino_brid;
  25021. + unsigned long si_xino_jiffy;
  25022. + unsigned long si_xino_expire;
  25023. + /* reserved for future use */
  25024. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  25025. +
  25026. +#ifdef CONFIG_AUFS_EXPORT
  25027. + /* i_generation */
  25028. + struct file *si_xigen;
  25029. + atomic_t si_xigen_next;
  25030. +#endif
  25031. +
  25032. + /* dirty trick to suppoer atomic_open */
  25033. + struct au_sphlhead si_aopen;
  25034. +
  25035. + /* vdir parameters */
  25036. + unsigned long si_rdcache; /* max cache time in jiffies */
  25037. + unsigned int si_rdblk; /* deblk size */
  25038. + unsigned int si_rdhash; /* hash size */
  25039. +
  25040. + /*
  25041. + * If the number of whiteouts are larger than si_dirwh, leave all of
  25042. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  25043. + * future fsck.aufs or kernel thread will remove them later.
  25044. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  25045. + */
  25046. + unsigned int si_dirwh;
  25047. +
  25048. + /* pseudo_link list */
  25049. + struct au_sphlhead si_plink[AuPlink_NHASH];
  25050. + wait_queue_head_t si_plink_wq;
  25051. + spinlock_t si_plink_maint_lock;
  25052. + pid_t si_plink_maint_pid;
  25053. +
  25054. + /* file list */
  25055. + struct au_sphlhead si_files;
  25056. +
  25057. + /*
  25058. + * sysfs and lifetime management.
  25059. + * this is not a small structure and it may be a waste of memory in case
  25060. + * of sysfs is disabled, particulary when many aufs-es are mounted.
  25061. + * but using sysfs is majority.
  25062. + */
  25063. + struct kobject si_kobj;
  25064. +#ifdef CONFIG_DEBUG_FS
  25065. + struct dentry *si_dbgaufs;
  25066. + struct dentry *si_dbgaufs_plink;
  25067. + struct dentry *si_dbgaufs_xib;
  25068. +#ifdef CONFIG_AUFS_EXPORT
  25069. + struct dentry *si_dbgaufs_xigen;
  25070. +#endif
  25071. +#endif
  25072. +
  25073. +#ifdef CONFIG_AUFS_SBILIST
  25074. + struct list_head si_list;
  25075. +#endif
  25076. +
  25077. + /* dirty, necessary for unmounting, sysfs and sysrq */
  25078. + struct super_block *si_sb;
  25079. +};
  25080. +
  25081. +/* sbinfo status flags */
  25082. +/*
  25083. + * set true when refresh_dirs() failed at remount time.
  25084. + * then try refreshing dirs at access time again.
  25085. + * if it is false, refreshing dirs at access time is unnecesary
  25086. + */
  25087. +#define AuSi_FAILED_REFRESH_DIR 1
  25088. +
  25089. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  25090. +
  25091. +#ifndef CONFIG_AUFS_FHSM
  25092. +#undef AuSi_FHSM
  25093. +#define AuSi_FHSM 0
  25094. +#endif
  25095. +
  25096. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  25097. + unsigned int flag)
  25098. +{
  25099. + AuRwMustAnyLock(&sbi->si_rwsem);
  25100. + return sbi->au_si_status & flag;
  25101. +}
  25102. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  25103. +#define au_fset_si(sbinfo, name) do { \
  25104. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  25105. + (sbinfo)->au_si_status |= AuSi_##name; \
  25106. +} while (0)
  25107. +#define au_fclr_si(sbinfo, name) do { \
  25108. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  25109. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  25110. +} while (0)
  25111. +
  25112. +/* ---------------------------------------------------------------------- */
  25113. +
  25114. +/* policy to select one among writable branches */
  25115. +#define AuWbrCopyup(sbinfo, ...) \
  25116. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  25117. +#define AuWbrCreate(sbinfo, ...) \
  25118. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  25119. +
  25120. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  25121. +#define AuLock_DW 1 /* write-lock dentry */
  25122. +#define AuLock_IR (1 << 1) /* read-lock inode */
  25123. +#define AuLock_IW (1 << 2) /* write-lock inode */
  25124. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  25125. +#define AuLock_DIR (1 << 4) /* target is a dir */
  25126. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  25127. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  25128. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  25129. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  25130. +#define au_fset_lock(flags, name) \
  25131. + do { (flags) |= AuLock_##name; } while (0)
  25132. +#define au_fclr_lock(flags, name) \
  25133. + do { (flags) &= ~AuLock_##name; } while (0)
  25134. +
  25135. +/* ---------------------------------------------------------------------- */
  25136. +
  25137. +/* super.c */
  25138. +extern struct file_system_type aufs_fs_type;
  25139. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  25140. +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
  25141. + void *arg);
  25142. +void au_array_free(void *array);
  25143. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
  25144. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  25145. +void au_iarray_free(struct inode **a, unsigned long long max);
  25146. +
  25147. +/* sbinfo.c */
  25148. +void au_si_free(struct kobject *kobj);
  25149. +int au_si_alloc(struct super_block *sb);
  25150. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
  25151. +
  25152. +unsigned int au_sigen_inc(struct super_block *sb);
  25153. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  25154. +
  25155. +int si_read_lock(struct super_block *sb, int flags);
  25156. +int si_write_lock(struct super_block *sb, int flags);
  25157. +int aufs_read_lock(struct dentry *dentry, int flags);
  25158. +void aufs_read_unlock(struct dentry *dentry, int flags);
  25159. +void aufs_write_lock(struct dentry *dentry);
  25160. +void aufs_write_unlock(struct dentry *dentry);
  25161. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  25162. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  25163. +
  25164. +int si_pid_test_slow(struct super_block *sb);
  25165. +void si_pid_set_slow(struct super_block *sb);
  25166. +void si_pid_clr_slow(struct super_block *sb);
  25167. +
  25168. +/* wbr_policy.c */
  25169. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  25170. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  25171. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  25172. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  25173. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
  25174. +
  25175. +/* mvdown.c */
  25176. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  25177. +
  25178. +#ifdef CONFIG_AUFS_FHSM
  25179. +/* fhsm.c */
  25180. +
  25181. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  25182. +{
  25183. + pid_t pid;
  25184. +
  25185. + spin_lock(&fhsm->fhsm_spin);
  25186. + pid = fhsm->fhsm_pid;
  25187. + spin_unlock(&fhsm->fhsm_spin);
  25188. +
  25189. + return pid;
  25190. +}
  25191. +
  25192. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  25193. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  25194. +int au_fhsm_fd(struct super_block *sb, int oflags);
  25195. +int au_fhsm_br_alloc(struct au_branch *br);
  25196. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  25197. +void au_fhsm_fin(struct super_block *sb);
  25198. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  25199. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  25200. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  25201. +#else
  25202. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  25203. + int force)
  25204. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  25205. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  25206. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  25207. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  25208. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  25209. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  25210. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  25211. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  25212. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  25213. +#endif
  25214. +
  25215. +/* ---------------------------------------------------------------------- */
  25216. +
  25217. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  25218. +{
  25219. + return sb->s_fs_info;
  25220. +}
  25221. +
  25222. +/* ---------------------------------------------------------------------- */
  25223. +
  25224. +#ifdef CONFIG_AUFS_EXPORT
  25225. +int au_test_nfsd(void);
  25226. +void au_export_init(struct super_block *sb);
  25227. +void au_xigen_inc(struct inode *inode);
  25228. +int au_xigen_new(struct inode *inode);
  25229. +int au_xigen_set(struct super_block *sb, struct file *base);
  25230. +void au_xigen_clr(struct super_block *sb);
  25231. +
  25232. +static inline int au_busy_or_stale(void)
  25233. +{
  25234. + if (!au_test_nfsd())
  25235. + return -EBUSY;
  25236. + return -ESTALE;
  25237. +}
  25238. +#else
  25239. +AuStubInt0(au_test_nfsd, void)
  25240. +AuStubVoid(au_export_init, struct super_block *sb)
  25241. +AuStubVoid(au_xigen_inc, struct inode *inode)
  25242. +AuStubInt0(au_xigen_new, struct inode *inode)
  25243. +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
  25244. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  25245. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  25246. +#endif /* CONFIG_AUFS_EXPORT */
  25247. +
  25248. +/* ---------------------------------------------------------------------- */
  25249. +
  25250. +#ifdef CONFIG_AUFS_SBILIST
  25251. +/* module.c */
  25252. +extern struct au_splhead au_sbilist;
  25253. +
  25254. +static inline void au_sbilist_init(void)
  25255. +{
  25256. + au_spl_init(&au_sbilist);
  25257. +}
  25258. +
  25259. +static inline void au_sbilist_add(struct super_block *sb)
  25260. +{
  25261. + au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
  25262. +}
  25263. +
  25264. +static inline void au_sbilist_del(struct super_block *sb)
  25265. +{
  25266. + au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
  25267. +}
  25268. +
  25269. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  25270. +static inline void au_sbilist_lock(void)
  25271. +{
  25272. + spin_lock(&au_sbilist.spin);
  25273. +}
  25274. +
  25275. +static inline void au_sbilist_unlock(void)
  25276. +{
  25277. + spin_unlock(&au_sbilist.spin);
  25278. +}
  25279. +#define AuGFP_SBILIST GFP_ATOMIC
  25280. +#else
  25281. +AuStubVoid(au_sbilist_lock, void)
  25282. +AuStubVoid(au_sbilist_unlock, void)
  25283. +#define AuGFP_SBILIST GFP_NOFS
  25284. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  25285. +#else
  25286. +AuStubVoid(au_sbilist_init, void)
  25287. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  25288. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  25289. +AuStubVoid(au_sbilist_lock, void)
  25290. +AuStubVoid(au_sbilist_unlock, void)
  25291. +#define AuGFP_SBILIST GFP_NOFS
  25292. +#endif
  25293. +
  25294. +/* ---------------------------------------------------------------------- */
  25295. +
  25296. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  25297. +{
  25298. + /*
  25299. + * This function is a dynamic '__init' function actually,
  25300. + * so the tiny check for si_rwsem is unnecessary.
  25301. + */
  25302. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  25303. +#ifdef CONFIG_DEBUG_FS
  25304. + sbinfo->si_dbgaufs = NULL;
  25305. + sbinfo->si_dbgaufs_plink = NULL;
  25306. + sbinfo->si_dbgaufs_xib = NULL;
  25307. +#ifdef CONFIG_AUFS_EXPORT
  25308. + sbinfo->si_dbgaufs_xigen = NULL;
  25309. +#endif
  25310. +#endif
  25311. +}
  25312. +
  25313. +/* ---------------------------------------------------------------------- */
  25314. +
  25315. +static inline pid_t si_pid_bit(void)
  25316. +{
  25317. + /* the origin of pid is 1, but the bitmap's is 0 */
  25318. + return current->pid - 1;
  25319. +}
  25320. +
  25321. +static inline int si_pid_test(struct super_block *sb)
  25322. +{
  25323. + pid_t bit;
  25324. +
  25325. + bit = si_pid_bit();
  25326. + if (bit < PID_MAX_DEFAULT)
  25327. + return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  25328. + return si_pid_test_slow(sb);
  25329. +}
  25330. +
  25331. +static inline void si_pid_set(struct super_block *sb)
  25332. +{
  25333. + pid_t bit;
  25334. +
  25335. + bit = si_pid_bit();
  25336. + if (bit < PID_MAX_DEFAULT) {
  25337. + AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
  25338. + set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  25339. + /* smp_mb(); */
  25340. + } else
  25341. + si_pid_set_slow(sb);
  25342. +}
  25343. +
  25344. +static inline void si_pid_clr(struct super_block *sb)
  25345. +{
  25346. + pid_t bit;
  25347. +
  25348. + bit = si_pid_bit();
  25349. + if (bit < PID_MAX_DEFAULT) {
  25350. + AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
  25351. + clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  25352. + /* smp_mb(); */
  25353. + } else
  25354. + si_pid_clr_slow(sb);
  25355. +}
  25356. +
  25357. +/* ---------------------------------------------------------------------- */
  25358. +
  25359. +/* lock superblock. mainly for entry point functions */
  25360. +/*
  25361. + * __si_read_lock, __si_write_lock,
  25362. + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
  25363. + */
  25364. +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
  25365. +
  25366. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  25367. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  25368. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  25369. +
  25370. +static inline void si_noflush_read_lock(struct super_block *sb)
  25371. +{
  25372. + __si_read_lock(sb);
  25373. + si_pid_set(sb);
  25374. +}
  25375. +
  25376. +static inline int si_noflush_read_trylock(struct super_block *sb)
  25377. +{
  25378. + int locked;
  25379. +
  25380. + locked = __si_read_trylock(sb);
  25381. + if (locked)
  25382. + si_pid_set(sb);
  25383. + return locked;
  25384. +}
  25385. +
  25386. +static inline void si_noflush_write_lock(struct super_block *sb)
  25387. +{
  25388. + __si_write_lock(sb);
  25389. + si_pid_set(sb);
  25390. +}
  25391. +
  25392. +static inline int si_noflush_write_trylock(struct super_block *sb)
  25393. +{
  25394. + int locked;
  25395. +
  25396. + locked = __si_write_trylock(sb);
  25397. + if (locked)
  25398. + si_pid_set(sb);
  25399. + return locked;
  25400. +}
  25401. +
  25402. +#if 0 /* reserved */
  25403. +static inline int si_read_trylock(struct super_block *sb, int flags)
  25404. +{
  25405. + if (au_ftest_lock(flags, FLUSH))
  25406. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25407. + return si_noflush_read_trylock(sb);
  25408. +}
  25409. +#endif
  25410. +
  25411. +static inline void si_read_unlock(struct super_block *sb)
  25412. +{
  25413. + si_pid_clr(sb);
  25414. + __si_read_unlock(sb);
  25415. +}
  25416. +
  25417. +#if 0 /* reserved */
  25418. +static inline int si_write_trylock(struct super_block *sb, int flags)
  25419. +{
  25420. + if (au_ftest_lock(flags, FLUSH))
  25421. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25422. + return si_noflush_write_trylock(sb);
  25423. +}
  25424. +#endif
  25425. +
  25426. +static inline void si_write_unlock(struct super_block *sb)
  25427. +{
  25428. + si_pid_clr(sb);
  25429. + __si_write_unlock(sb);
  25430. +}
  25431. +
  25432. +#if 0 /* reserved */
  25433. +static inline void si_downgrade_lock(struct super_block *sb)
  25434. +{
  25435. + __si_downgrade_lock(sb);
  25436. +}
  25437. +#endif
  25438. +
  25439. +/* ---------------------------------------------------------------------- */
  25440. +
  25441. +static inline aufs_bindex_t au_sbend(struct super_block *sb)
  25442. +{
  25443. + SiMustAnyLock(sb);
  25444. + return au_sbi(sb)->si_bend;
  25445. +}
  25446. +
  25447. +static inline unsigned int au_mntflags(struct super_block *sb)
  25448. +{
  25449. + SiMustAnyLock(sb);
  25450. + return au_sbi(sb)->si_mntflags;
  25451. +}
  25452. +
  25453. +static inline unsigned int au_sigen(struct super_block *sb)
  25454. +{
  25455. + SiMustAnyLock(sb);
  25456. + return au_sbi(sb)->si_generation;
  25457. +}
  25458. +
  25459. +static inline void au_ninodes_inc(struct super_block *sb)
  25460. +{
  25461. + atomic_long_inc(&au_sbi(sb)->si_ninodes);
  25462. +}
  25463. +
  25464. +static inline void au_ninodes_dec(struct super_block *sb)
  25465. +{
  25466. + AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
  25467. + atomic_long_dec(&au_sbi(sb)->si_ninodes);
  25468. +}
  25469. +
  25470. +static inline void au_nfiles_inc(struct super_block *sb)
  25471. +{
  25472. + atomic_long_inc(&au_sbi(sb)->si_nfiles);
  25473. +}
  25474. +
  25475. +static inline void au_nfiles_dec(struct super_block *sb)
  25476. +{
  25477. + AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
  25478. + atomic_long_dec(&au_sbi(sb)->si_nfiles);
  25479. +}
  25480. +
  25481. +static inline struct au_branch *au_sbr(struct super_block *sb,
  25482. + aufs_bindex_t bindex)
  25483. +{
  25484. + SiMustAnyLock(sb);
  25485. + return au_sbi(sb)->si_branch[0 + bindex];
  25486. +}
  25487. +
  25488. +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
  25489. +{
  25490. + SiMustWriteLock(sb);
  25491. + au_sbi(sb)->si_xino_brid = brid;
  25492. +}
  25493. +
  25494. +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
  25495. +{
  25496. + SiMustAnyLock(sb);
  25497. + return au_sbi(sb)->si_xino_brid;
  25498. +}
  25499. +
  25500. +#endif /* __KERNEL__ */
  25501. +#endif /* __AUFS_SUPER_H__ */
  25502. diff -Nur linux-4.0.5.orig/fs/aufs/sysaufs.c linux-4.0.5/fs/aufs/sysaufs.c
  25503. --- linux-4.0.5.orig/fs/aufs/sysaufs.c 1969-12-31 18:00:00.000000000 -0600
  25504. +++ linux-4.0.5/fs/aufs/sysaufs.c 2015-06-21 14:07:48.686432781 -0500
  25505. @@ -0,0 +1,104 @@
  25506. +/*
  25507. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25508. + *
  25509. + * This program, aufs is free software; you can redistribute it and/or modify
  25510. + * it under the terms of the GNU General Public License as published by
  25511. + * the Free Software Foundation; either version 2 of the License, or
  25512. + * (at your option) any later version.
  25513. + *
  25514. + * This program is distributed in the hope that it will be useful,
  25515. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25516. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25517. + * GNU General Public License for more details.
  25518. + *
  25519. + * You should have received a copy of the GNU General Public License
  25520. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25521. + */
  25522. +
  25523. +/*
  25524. + * sysfs interface and lifetime management
  25525. + * they are necessary regardless sysfs is disabled.
  25526. + */
  25527. +
  25528. +#include <linux/random.h>
  25529. +#include "aufs.h"
  25530. +
  25531. +unsigned long sysaufs_si_mask;
  25532. +struct kset *sysaufs_kset;
  25533. +
  25534. +#define AuSiAttr(_name) { \
  25535. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  25536. + .show = sysaufs_si_##_name, \
  25537. +}
  25538. +
  25539. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  25540. +struct attribute *sysaufs_si_attrs[] = {
  25541. + &sysaufs_si_attr_xi_path.attr,
  25542. + NULL,
  25543. +};
  25544. +
  25545. +static const struct sysfs_ops au_sbi_ops = {
  25546. + .show = sysaufs_si_show
  25547. +};
  25548. +
  25549. +static struct kobj_type au_sbi_ktype = {
  25550. + .release = au_si_free,
  25551. + .sysfs_ops = &au_sbi_ops,
  25552. + .default_attrs = sysaufs_si_attrs
  25553. +};
  25554. +
  25555. +/* ---------------------------------------------------------------------- */
  25556. +
  25557. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  25558. +{
  25559. + int err;
  25560. +
  25561. + sbinfo->si_kobj.kset = sysaufs_kset;
  25562. + /* cf. sysaufs_name() */
  25563. + err = kobject_init_and_add
  25564. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  25565. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  25566. +
  25567. + dbgaufs_si_null(sbinfo);
  25568. + if (!err) {
  25569. + err = dbgaufs_si_init(sbinfo);
  25570. + if (unlikely(err))
  25571. + kobject_put(&sbinfo->si_kobj);
  25572. + }
  25573. + return err;
  25574. +}
  25575. +
  25576. +void sysaufs_fin(void)
  25577. +{
  25578. + dbgaufs_fin();
  25579. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  25580. + kset_unregister(sysaufs_kset);
  25581. +}
  25582. +
  25583. +int __init sysaufs_init(void)
  25584. +{
  25585. + int err;
  25586. +
  25587. + do {
  25588. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  25589. + } while (!sysaufs_si_mask);
  25590. +
  25591. + err = -EINVAL;
  25592. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  25593. + if (unlikely(!sysaufs_kset))
  25594. + goto out;
  25595. + err = PTR_ERR(sysaufs_kset);
  25596. + if (IS_ERR(sysaufs_kset))
  25597. + goto out;
  25598. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  25599. + if (unlikely(err)) {
  25600. + kset_unregister(sysaufs_kset);
  25601. + goto out;
  25602. + }
  25603. +
  25604. + err = dbgaufs_init();
  25605. + if (unlikely(err))
  25606. + sysaufs_fin();
  25607. +out:
  25608. + return err;
  25609. +}
  25610. diff -Nur linux-4.0.5.orig/fs/aufs/sysaufs.h linux-4.0.5/fs/aufs/sysaufs.h
  25611. --- linux-4.0.5.orig/fs/aufs/sysaufs.h 1969-12-31 18:00:00.000000000 -0600
  25612. +++ linux-4.0.5/fs/aufs/sysaufs.h 2015-06-21 14:07:48.686432781 -0500
  25613. @@ -0,0 +1,101 @@
  25614. +/*
  25615. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25616. + *
  25617. + * This program, aufs is free software; you can redistribute it and/or modify
  25618. + * it under the terms of the GNU General Public License as published by
  25619. + * the Free Software Foundation; either version 2 of the License, or
  25620. + * (at your option) any later version.
  25621. + *
  25622. + * This program is distributed in the hope that it will be useful,
  25623. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25624. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25625. + * GNU General Public License for more details.
  25626. + *
  25627. + * You should have received a copy of the GNU General Public License
  25628. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25629. + */
  25630. +
  25631. +/*
  25632. + * sysfs interface and mount lifetime management
  25633. + */
  25634. +
  25635. +#ifndef __SYSAUFS_H__
  25636. +#define __SYSAUFS_H__
  25637. +
  25638. +#ifdef __KERNEL__
  25639. +
  25640. +#include <linux/sysfs.h>
  25641. +#include "module.h"
  25642. +
  25643. +struct super_block;
  25644. +struct au_sbinfo;
  25645. +
  25646. +struct sysaufs_si_attr {
  25647. + struct attribute attr;
  25648. + int (*show)(struct seq_file *seq, struct super_block *sb);
  25649. +};
  25650. +
  25651. +/* ---------------------------------------------------------------------- */
  25652. +
  25653. +/* sysaufs.c */
  25654. +extern unsigned long sysaufs_si_mask;
  25655. +extern struct kset *sysaufs_kset;
  25656. +extern struct attribute *sysaufs_si_attrs[];
  25657. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  25658. +int __init sysaufs_init(void);
  25659. +void sysaufs_fin(void);
  25660. +
  25661. +/* ---------------------------------------------------------------------- */
  25662. +
  25663. +/* some people doesn't like to show a pointer in kernel */
  25664. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  25665. +{
  25666. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  25667. +}
  25668. +
  25669. +#define SysaufsSiNamePrefix "si_"
  25670. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  25671. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  25672. +{
  25673. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  25674. + sysaufs_si_id(sbinfo));
  25675. +}
  25676. +
  25677. +struct au_branch;
  25678. +#ifdef CONFIG_SYSFS
  25679. +/* sysfs.c */
  25680. +extern struct attribute_group *sysaufs_attr_group;
  25681. +
  25682. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  25683. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  25684. + char *buf);
  25685. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  25686. +#ifdef CONFIG_COMPAT
  25687. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  25688. +#endif
  25689. +
  25690. +void sysaufs_br_init(struct au_branch *br);
  25691. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  25692. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  25693. +
  25694. +#define sysaufs_brs_init() do {} while (0)
  25695. +
  25696. +#else
  25697. +#define sysaufs_attr_group NULL
  25698. +
  25699. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  25700. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  25701. + struct attribute *attr, char *buf)
  25702. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  25703. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  25704. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  25705. +
  25706. +static inline void sysaufs_brs_init(void)
  25707. +{
  25708. + sysaufs_brs = 0;
  25709. +}
  25710. +
  25711. +#endif /* CONFIG_SYSFS */
  25712. +
  25713. +#endif /* __KERNEL__ */
  25714. +#endif /* __SYSAUFS_H__ */
  25715. diff -Nur linux-4.0.5.orig/fs/aufs/sysfs.c linux-4.0.5/fs/aufs/sysfs.c
  25716. --- linux-4.0.5.orig/fs/aufs/sysfs.c 1969-12-31 18:00:00.000000000 -0600
  25717. +++ linux-4.0.5/fs/aufs/sysfs.c 2015-06-21 14:07:48.686432781 -0500
  25718. @@ -0,0 +1,372 @@
  25719. +/*
  25720. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25721. + *
  25722. + * This program, aufs is free software; you can redistribute it and/or modify
  25723. + * it under the terms of the GNU General Public License as published by
  25724. + * the Free Software Foundation; either version 2 of the License, or
  25725. + * (at your option) any later version.
  25726. + *
  25727. + * This program is distributed in the hope that it will be useful,
  25728. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25729. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25730. + * GNU General Public License for more details.
  25731. + *
  25732. + * You should have received a copy of the GNU General Public License
  25733. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25734. + */
  25735. +
  25736. +/*
  25737. + * sysfs interface
  25738. + */
  25739. +
  25740. +#include <linux/compat.h>
  25741. +#include <linux/seq_file.h>
  25742. +#include "aufs.h"
  25743. +
  25744. +#ifdef CONFIG_AUFS_FS_MODULE
  25745. +/* this entry violates the "one line per file" policy of sysfs */
  25746. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  25747. + char *buf)
  25748. +{
  25749. + ssize_t err;
  25750. + static char *conf =
  25751. +/* this file is generated at compiling */
  25752. +#include "conf.str"
  25753. + ;
  25754. +
  25755. + err = snprintf(buf, PAGE_SIZE, conf);
  25756. + if (unlikely(err >= PAGE_SIZE))
  25757. + err = -EFBIG;
  25758. + return err;
  25759. +}
  25760. +
  25761. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  25762. +#endif
  25763. +
  25764. +static struct attribute *au_attr[] = {
  25765. +#ifdef CONFIG_AUFS_FS_MODULE
  25766. + &au_config_attr.attr,
  25767. +#endif
  25768. + NULL, /* need to NULL terminate the list of attributes */
  25769. +};
  25770. +
  25771. +static struct attribute_group sysaufs_attr_group_body = {
  25772. + .attrs = au_attr
  25773. +};
  25774. +
  25775. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  25776. +
  25777. +/* ---------------------------------------------------------------------- */
  25778. +
  25779. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  25780. +{
  25781. + int err;
  25782. +
  25783. + SiMustAnyLock(sb);
  25784. +
  25785. + err = 0;
  25786. + if (au_opt_test(au_mntflags(sb), XINO)) {
  25787. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  25788. + seq_putc(seq, '\n');
  25789. + }
  25790. + return err;
  25791. +}
  25792. +
  25793. +/*
  25794. + * the lifetime of branch is independent from the entry under sysfs.
  25795. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  25796. + * unlinked.
  25797. + */
  25798. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  25799. + aufs_bindex_t bindex, int idx)
  25800. +{
  25801. + int err;
  25802. + struct path path;
  25803. + struct dentry *root;
  25804. + struct au_branch *br;
  25805. + au_br_perm_str_t perm;
  25806. +
  25807. + AuDbg("b%d\n", bindex);
  25808. +
  25809. + err = 0;
  25810. + root = sb->s_root;
  25811. + di_read_lock_parent(root, !AuLock_IR);
  25812. + br = au_sbr(sb, bindex);
  25813. +
  25814. + switch (idx) {
  25815. + case AuBrSysfs_BR:
  25816. + path.mnt = au_br_mnt(br);
  25817. + path.dentry = au_h_dptr(root, bindex);
  25818. + au_seq_path(seq, &path);
  25819. + au_optstr_br_perm(&perm, br->br_perm);
  25820. + err = seq_printf(seq, "=%s\n", perm.a);
  25821. + break;
  25822. + case AuBrSysfs_BRID:
  25823. + err = seq_printf(seq, "%d\n", br->br_id);
  25824. + break;
  25825. + }
  25826. + di_read_unlock(root, !AuLock_IR);
  25827. + if (err == -1)
  25828. + err = -E2BIG;
  25829. +
  25830. + return err;
  25831. +}
  25832. +
  25833. +/* ---------------------------------------------------------------------- */
  25834. +
  25835. +static struct seq_file *au_seq(char *p, ssize_t len)
  25836. +{
  25837. + struct seq_file *seq;
  25838. +
  25839. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  25840. + if (seq) {
  25841. + /* mutex_init(&seq.lock); */
  25842. + seq->buf = p;
  25843. + seq->size = len;
  25844. + return seq; /* success */
  25845. + }
  25846. +
  25847. + seq = ERR_PTR(-ENOMEM);
  25848. + return seq;
  25849. +}
  25850. +
  25851. +#define SysaufsBr_PREFIX "br"
  25852. +#define SysaufsBrid_PREFIX "brid"
  25853. +
  25854. +/* todo: file size may exceed PAGE_SIZE */
  25855. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  25856. + char *buf)
  25857. +{
  25858. + ssize_t err;
  25859. + int idx;
  25860. + long l;
  25861. + aufs_bindex_t bend;
  25862. + struct au_sbinfo *sbinfo;
  25863. + struct super_block *sb;
  25864. + struct seq_file *seq;
  25865. + char *name;
  25866. + struct attribute **cattr;
  25867. +
  25868. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  25869. + sb = sbinfo->si_sb;
  25870. +
  25871. + /*
  25872. + * prevent a race condition between sysfs and aufs.
  25873. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  25874. + * prohibits maintaining the sysfs entries.
  25875. + * hew we acquire read lock after sysfs_get_active_two().
  25876. + * on the other hand, the remount process may maintain the sysfs/aufs
  25877. + * entries after acquiring write lock.
  25878. + * it can cause a deadlock.
  25879. + * simply we gave up processing read here.
  25880. + */
  25881. + err = -EBUSY;
  25882. + if (unlikely(!si_noflush_read_trylock(sb)))
  25883. + goto out;
  25884. +
  25885. + seq = au_seq(buf, PAGE_SIZE);
  25886. + err = PTR_ERR(seq);
  25887. + if (IS_ERR(seq))
  25888. + goto out_unlock;
  25889. +
  25890. + name = (void *)attr->name;
  25891. + cattr = sysaufs_si_attrs;
  25892. + while (*cattr) {
  25893. + if (!strcmp(name, (*cattr)->name)) {
  25894. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  25895. + ->show(seq, sb);
  25896. + goto out_seq;
  25897. + }
  25898. + cattr++;
  25899. + }
  25900. +
  25901. + if (!strncmp(name, SysaufsBrid_PREFIX,
  25902. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  25903. + idx = AuBrSysfs_BRID;
  25904. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  25905. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  25906. + sizeof(SysaufsBr_PREFIX) - 1)) {
  25907. + idx = AuBrSysfs_BR;
  25908. + name += sizeof(SysaufsBr_PREFIX) - 1;
  25909. + } else
  25910. + BUG();
  25911. +
  25912. + err = kstrtol(name, 10, &l);
  25913. + if (!err) {
  25914. + bend = au_sbend(sb);
  25915. + if (l <= bend)
  25916. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  25917. + else
  25918. + err = -ENOENT;
  25919. + }
  25920. +
  25921. +out_seq:
  25922. + if (!err) {
  25923. + err = seq->count;
  25924. + /* sysfs limit */
  25925. + if (unlikely(err == PAGE_SIZE))
  25926. + err = -EFBIG;
  25927. + }
  25928. + kfree(seq);
  25929. +out_unlock:
  25930. + si_read_unlock(sb);
  25931. +out:
  25932. + return err;
  25933. +}
  25934. +
  25935. +/* ---------------------------------------------------------------------- */
  25936. +
  25937. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  25938. +{
  25939. + int err;
  25940. + int16_t brid;
  25941. + aufs_bindex_t bindex, bend;
  25942. + size_t sz;
  25943. + char *buf;
  25944. + struct seq_file *seq;
  25945. + struct au_branch *br;
  25946. +
  25947. + si_read_lock(sb, AuLock_FLUSH);
  25948. + bend = au_sbend(sb);
  25949. + err = bend + 1;
  25950. + if (!arg)
  25951. + goto out;
  25952. +
  25953. + err = -ENOMEM;
  25954. + buf = (void *)__get_free_page(GFP_NOFS);
  25955. + if (unlikely(!buf))
  25956. + goto out;
  25957. +
  25958. + seq = au_seq(buf, PAGE_SIZE);
  25959. + err = PTR_ERR(seq);
  25960. + if (IS_ERR(seq))
  25961. + goto out_buf;
  25962. +
  25963. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  25964. + for (bindex = 0; bindex <= bend; bindex++, arg++) {
  25965. + err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
  25966. + if (unlikely(err))
  25967. + break;
  25968. +
  25969. + br = au_sbr(sb, bindex);
  25970. + brid = br->br_id;
  25971. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  25972. + err = __put_user(brid, &arg->id);
  25973. + if (unlikely(err))
  25974. + break;
  25975. +
  25976. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  25977. + err = __put_user(br->br_perm, &arg->perm);
  25978. + if (unlikely(err))
  25979. + break;
  25980. +
  25981. + au_seq_path(seq, &br->br_path);
  25982. + err = seq_putc(seq, '\0');
  25983. + if (!err && seq->count <= sz) {
  25984. + err = copy_to_user(arg->path, seq->buf, seq->count);
  25985. + seq->count = 0;
  25986. + if (unlikely(err))
  25987. + break;
  25988. + } else {
  25989. + err = -E2BIG;
  25990. + goto out_seq;
  25991. + }
  25992. + }
  25993. + if (unlikely(err))
  25994. + err = -EFAULT;
  25995. +
  25996. +out_seq:
  25997. + kfree(seq);
  25998. +out_buf:
  25999. + free_page((unsigned long)buf);
  26000. +out:
  26001. + si_read_unlock(sb);
  26002. + return err;
  26003. +}
  26004. +
  26005. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  26006. +{
  26007. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  26008. +}
  26009. +
  26010. +#ifdef CONFIG_COMPAT
  26011. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  26012. +{
  26013. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  26014. +}
  26015. +#endif
  26016. +
  26017. +/* ---------------------------------------------------------------------- */
  26018. +
  26019. +void sysaufs_br_init(struct au_branch *br)
  26020. +{
  26021. + int i;
  26022. + struct au_brsysfs *br_sysfs;
  26023. + struct attribute *attr;
  26024. +
  26025. + br_sysfs = br->br_sysfs;
  26026. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  26027. + attr = &br_sysfs->attr;
  26028. + sysfs_attr_init(attr);
  26029. + attr->name = br_sysfs->name;
  26030. + attr->mode = S_IRUGO;
  26031. + br_sysfs++;
  26032. + }
  26033. +}
  26034. +
  26035. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  26036. +{
  26037. + struct au_branch *br;
  26038. + struct kobject *kobj;
  26039. + struct au_brsysfs *br_sysfs;
  26040. + int i;
  26041. + aufs_bindex_t bend;
  26042. +
  26043. + dbgaufs_brs_del(sb, bindex);
  26044. +
  26045. + if (!sysaufs_brs)
  26046. + return;
  26047. +
  26048. + kobj = &au_sbi(sb)->si_kobj;
  26049. + bend = au_sbend(sb);
  26050. + for (; bindex <= bend; bindex++) {
  26051. + br = au_sbr(sb, bindex);
  26052. + br_sysfs = br->br_sysfs;
  26053. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  26054. + sysfs_remove_file(kobj, &br_sysfs->attr);
  26055. + br_sysfs++;
  26056. + }
  26057. + }
  26058. +}
  26059. +
  26060. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  26061. +{
  26062. + int err, i;
  26063. + aufs_bindex_t bend;
  26064. + struct kobject *kobj;
  26065. + struct au_branch *br;
  26066. + struct au_brsysfs *br_sysfs;
  26067. +
  26068. + dbgaufs_brs_add(sb, bindex);
  26069. +
  26070. + if (!sysaufs_brs)
  26071. + return;
  26072. +
  26073. + kobj = &au_sbi(sb)->si_kobj;
  26074. + bend = au_sbend(sb);
  26075. + for (; bindex <= bend; bindex++) {
  26076. + br = au_sbr(sb, bindex);
  26077. + br_sysfs = br->br_sysfs;
  26078. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  26079. + SysaufsBr_PREFIX "%d", bindex);
  26080. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  26081. + SysaufsBrid_PREFIX "%d", bindex);
  26082. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  26083. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  26084. + if (unlikely(err))
  26085. + pr_warn("failed %s under sysfs(%d)\n",
  26086. + br_sysfs->name, err);
  26087. + br_sysfs++;
  26088. + }
  26089. + }
  26090. +}
  26091. diff -Nur linux-4.0.5.orig/fs/aufs/sysrq.c linux-4.0.5/fs/aufs/sysrq.c
  26092. --- linux-4.0.5.orig/fs/aufs/sysrq.c 1969-12-31 18:00:00.000000000 -0600
  26093. +++ linux-4.0.5/fs/aufs/sysrq.c 2015-06-21 14:07:48.686432781 -0500
  26094. @@ -0,0 +1,157 @@
  26095. +/*
  26096. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26097. + *
  26098. + * This program, aufs is free software; you can redistribute it and/or modify
  26099. + * it under the terms of the GNU General Public License as published by
  26100. + * the Free Software Foundation; either version 2 of the License, or
  26101. + * (at your option) any later version.
  26102. + *
  26103. + * This program is distributed in the hope that it will be useful,
  26104. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26105. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26106. + * GNU General Public License for more details.
  26107. + *
  26108. + * You should have received a copy of the GNU General Public License
  26109. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26110. + */
  26111. +
  26112. +/*
  26113. + * magic sysrq hanlder
  26114. + */
  26115. +
  26116. +/* #include <linux/sysrq.h> */
  26117. +#include <linux/writeback.h>
  26118. +#include "aufs.h"
  26119. +
  26120. +/* ---------------------------------------------------------------------- */
  26121. +
  26122. +static void sysrq_sb(struct super_block *sb)
  26123. +{
  26124. + char *plevel;
  26125. + struct au_sbinfo *sbinfo;
  26126. + struct file *file;
  26127. + struct au_sphlhead *files;
  26128. + struct au_finfo *finfo;
  26129. +
  26130. + plevel = au_plevel;
  26131. + au_plevel = KERN_WARNING;
  26132. +
  26133. + /* since we define pr_fmt, call printk directly */
  26134. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  26135. +
  26136. + sbinfo = au_sbi(sb);
  26137. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  26138. + pr("superblock\n");
  26139. + au_dpri_sb(sb);
  26140. +
  26141. +#if 0
  26142. + pr("root dentry\n");
  26143. + au_dpri_dentry(sb->s_root);
  26144. + pr("root inode\n");
  26145. + au_dpri_inode(sb->s_root->d_inode);
  26146. +#endif
  26147. +
  26148. +#if 0
  26149. + do {
  26150. + int err, i, j, ndentry;
  26151. + struct au_dcsub_pages dpages;
  26152. + struct au_dpage *dpage;
  26153. +
  26154. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  26155. + if (unlikely(err))
  26156. + break;
  26157. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  26158. + if (!err)
  26159. + for (i = 0; i < dpages.ndpage; i++) {
  26160. + dpage = dpages.dpages + i;
  26161. + ndentry = dpage->ndentry;
  26162. + for (j = 0; j < ndentry; j++)
  26163. + au_dpri_dentry(dpage->dentries[j]);
  26164. + }
  26165. + au_dpages_free(&dpages);
  26166. + } while (0);
  26167. +#endif
  26168. +
  26169. +#if 1
  26170. + {
  26171. + struct inode *i;
  26172. +
  26173. + pr("isolated inode\n");
  26174. + spin_lock(&inode_sb_list_lock);
  26175. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  26176. + spin_lock(&i->i_lock);
  26177. + if (1 || hlist_empty(&i->i_dentry))
  26178. + au_dpri_inode(i);
  26179. + spin_unlock(&i->i_lock);
  26180. + }
  26181. + spin_unlock(&inode_sb_list_lock);
  26182. + }
  26183. +#endif
  26184. + pr("files\n");
  26185. + files = &au_sbi(sb)->si_files;
  26186. + spin_lock(&files->spin);
  26187. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  26188. + umode_t mode;
  26189. +
  26190. + file = finfo->fi_file;
  26191. + mode = file_inode(file)->i_mode;
  26192. + if (!special_file(mode))
  26193. + au_dpri_file(file);
  26194. + }
  26195. + spin_unlock(&files->spin);
  26196. + pr("done\n");
  26197. +
  26198. +#undef pr
  26199. + au_plevel = plevel;
  26200. +}
  26201. +
  26202. +/* ---------------------------------------------------------------------- */
  26203. +
  26204. +/* module parameter */
  26205. +static char *aufs_sysrq_key = "a";
  26206. +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
  26207. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  26208. +
  26209. +static void au_sysrq(int key __maybe_unused)
  26210. +{
  26211. + struct au_sbinfo *sbinfo;
  26212. +
  26213. + lockdep_off();
  26214. + au_sbilist_lock();
  26215. + list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  26216. + sysrq_sb(sbinfo->si_sb);
  26217. + au_sbilist_unlock();
  26218. + lockdep_on();
  26219. +}
  26220. +
  26221. +static struct sysrq_key_op au_sysrq_op = {
  26222. + .handler = au_sysrq,
  26223. + .help_msg = "Aufs",
  26224. + .action_msg = "Aufs",
  26225. + .enable_mask = SYSRQ_ENABLE_DUMP
  26226. +};
  26227. +
  26228. +/* ---------------------------------------------------------------------- */
  26229. +
  26230. +int __init au_sysrq_init(void)
  26231. +{
  26232. + int err;
  26233. + char key;
  26234. +
  26235. + err = -1;
  26236. + key = *aufs_sysrq_key;
  26237. + if ('a' <= key && key <= 'z')
  26238. + err = register_sysrq_key(key, &au_sysrq_op);
  26239. + if (unlikely(err))
  26240. + pr_err("err %d, sysrq=%c\n", err, key);
  26241. + return err;
  26242. +}
  26243. +
  26244. +void au_sysrq_fin(void)
  26245. +{
  26246. + int err;
  26247. +
  26248. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  26249. + if (unlikely(err))
  26250. + pr_err("err %d (ignored)\n", err);
  26251. +}
  26252. diff -Nur linux-4.0.5.orig/fs/aufs/vdir.c linux-4.0.5/fs/aufs/vdir.c
  26253. --- linux-4.0.5.orig/fs/aufs/vdir.c 1969-12-31 18:00:00.000000000 -0600
  26254. +++ linux-4.0.5/fs/aufs/vdir.c 2015-06-21 14:07:48.690432781 -0500
  26255. @@ -0,0 +1,888 @@
  26256. +/*
  26257. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26258. + *
  26259. + * This program, aufs is free software; you can redistribute it and/or modify
  26260. + * it under the terms of the GNU General Public License as published by
  26261. + * the Free Software Foundation; either version 2 of the License, or
  26262. + * (at your option) any later version.
  26263. + *
  26264. + * This program is distributed in the hope that it will be useful,
  26265. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26266. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26267. + * GNU General Public License for more details.
  26268. + *
  26269. + * You should have received a copy of the GNU General Public License
  26270. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26271. + */
  26272. +
  26273. +/*
  26274. + * virtual or vertical directory
  26275. + */
  26276. +
  26277. +#include "aufs.h"
  26278. +
  26279. +static unsigned int calc_size(int nlen)
  26280. +{
  26281. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  26282. +}
  26283. +
  26284. +static int set_deblk_end(union au_vdir_deblk_p *p,
  26285. + union au_vdir_deblk_p *deblk_end)
  26286. +{
  26287. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  26288. + p->de->de_str.len = 0;
  26289. + /* smp_mb(); */
  26290. + return 0;
  26291. + }
  26292. + return -1; /* error */
  26293. +}
  26294. +
  26295. +/* returns true or false */
  26296. +static int is_deblk_end(union au_vdir_deblk_p *p,
  26297. + union au_vdir_deblk_p *deblk_end)
  26298. +{
  26299. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  26300. + return !p->de->de_str.len;
  26301. + return 1;
  26302. +}
  26303. +
  26304. +static unsigned char *last_deblk(struct au_vdir *vdir)
  26305. +{
  26306. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  26307. +}
  26308. +
  26309. +/* ---------------------------------------------------------------------- */
  26310. +
  26311. +/* estimate the apropriate size for name hash table */
  26312. +unsigned int au_rdhash_est(loff_t sz)
  26313. +{
  26314. + unsigned int n;
  26315. +
  26316. + n = UINT_MAX;
  26317. + sz >>= 10;
  26318. + if (sz < n)
  26319. + n = sz;
  26320. + if (sz < AUFS_RDHASH_DEF)
  26321. + n = AUFS_RDHASH_DEF;
  26322. + /* pr_info("n %u\n", n); */
  26323. + return n;
  26324. +}
  26325. +
  26326. +/*
  26327. + * the allocated memory has to be freed by
  26328. + * au_nhash_wh_free() or au_nhash_de_free().
  26329. + */
  26330. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  26331. +{
  26332. + struct hlist_head *head;
  26333. + unsigned int u;
  26334. + size_t sz;
  26335. +
  26336. + sz = sizeof(*nhash->nh_head) * num_hash;
  26337. + head = kmalloc(sz, gfp);
  26338. + if (head) {
  26339. + nhash->nh_num = num_hash;
  26340. + nhash->nh_head = head;
  26341. + for (u = 0; u < num_hash; u++)
  26342. + INIT_HLIST_HEAD(head++);
  26343. + return 0; /* success */
  26344. + }
  26345. +
  26346. + return -ENOMEM;
  26347. +}
  26348. +
  26349. +static void nhash_count(struct hlist_head *head)
  26350. +{
  26351. +#if 0
  26352. + unsigned long n;
  26353. + struct hlist_node *pos;
  26354. +
  26355. + n = 0;
  26356. + hlist_for_each(pos, head)
  26357. + n++;
  26358. + pr_info("%lu\n", n);
  26359. +#endif
  26360. +}
  26361. +
  26362. +static void au_nhash_wh_do_free(struct hlist_head *head)
  26363. +{
  26364. + struct au_vdir_wh *pos;
  26365. + struct hlist_node *node;
  26366. +
  26367. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  26368. + kfree(pos);
  26369. +}
  26370. +
  26371. +static void au_nhash_de_do_free(struct hlist_head *head)
  26372. +{
  26373. + struct au_vdir_dehstr *pos;
  26374. + struct hlist_node *node;
  26375. +
  26376. + hlist_for_each_entry_safe(pos, node, head, hash)
  26377. + au_cache_free_vdir_dehstr(pos);
  26378. +}
  26379. +
  26380. +static void au_nhash_do_free(struct au_nhash *nhash,
  26381. + void (*free)(struct hlist_head *head))
  26382. +{
  26383. + unsigned int n;
  26384. + struct hlist_head *head;
  26385. +
  26386. + n = nhash->nh_num;
  26387. + if (!n)
  26388. + return;
  26389. +
  26390. + head = nhash->nh_head;
  26391. + while (n-- > 0) {
  26392. + nhash_count(head);
  26393. + free(head++);
  26394. + }
  26395. + kfree(nhash->nh_head);
  26396. +}
  26397. +
  26398. +void au_nhash_wh_free(struct au_nhash *whlist)
  26399. +{
  26400. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  26401. +}
  26402. +
  26403. +static void au_nhash_de_free(struct au_nhash *delist)
  26404. +{
  26405. + au_nhash_do_free(delist, au_nhash_de_do_free);
  26406. +}
  26407. +
  26408. +/* ---------------------------------------------------------------------- */
  26409. +
  26410. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  26411. + int limit)
  26412. +{
  26413. + int num;
  26414. + unsigned int u, n;
  26415. + struct hlist_head *head;
  26416. + struct au_vdir_wh *pos;
  26417. +
  26418. + num = 0;
  26419. + n = whlist->nh_num;
  26420. + head = whlist->nh_head;
  26421. + for (u = 0; u < n; u++, head++)
  26422. + hlist_for_each_entry(pos, head, wh_hash)
  26423. + if (pos->wh_bindex == btgt && ++num > limit)
  26424. + return 1;
  26425. + return 0;
  26426. +}
  26427. +
  26428. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  26429. + unsigned char *name,
  26430. + unsigned int len)
  26431. +{
  26432. + unsigned int v;
  26433. + /* const unsigned int magic_bit = 12; */
  26434. +
  26435. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  26436. +
  26437. + v = 0;
  26438. + while (len--)
  26439. + v += *name++;
  26440. + /* v = hash_long(v, magic_bit); */
  26441. + v %= nhash->nh_num;
  26442. + return nhash->nh_head + v;
  26443. +}
  26444. +
  26445. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  26446. + int nlen)
  26447. +{
  26448. + return str->len == nlen && !memcmp(str->name, name, nlen);
  26449. +}
  26450. +
  26451. +/* returns found or not */
  26452. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  26453. +{
  26454. + struct hlist_head *head;
  26455. + struct au_vdir_wh *pos;
  26456. + struct au_vdir_destr *str;
  26457. +
  26458. + head = au_name_hash(whlist, name, nlen);
  26459. + hlist_for_each_entry(pos, head, wh_hash) {
  26460. + str = &pos->wh_str;
  26461. + AuDbg("%.*s\n", str->len, str->name);
  26462. + if (au_nhash_test_name(str, name, nlen))
  26463. + return 1;
  26464. + }
  26465. + return 0;
  26466. +}
  26467. +
  26468. +/* returns found(true) or not */
  26469. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  26470. +{
  26471. + struct hlist_head *head;
  26472. + struct au_vdir_dehstr *pos;
  26473. + struct au_vdir_destr *str;
  26474. +
  26475. + head = au_name_hash(delist, name, nlen);
  26476. + hlist_for_each_entry(pos, head, hash) {
  26477. + str = pos->str;
  26478. + AuDbg("%.*s\n", str->len, str->name);
  26479. + if (au_nhash_test_name(str, name, nlen))
  26480. + return 1;
  26481. + }
  26482. + return 0;
  26483. +}
  26484. +
  26485. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  26486. + unsigned char d_type)
  26487. +{
  26488. +#ifdef CONFIG_AUFS_SHWH
  26489. + wh->wh_ino = ino;
  26490. + wh->wh_type = d_type;
  26491. +#endif
  26492. +}
  26493. +
  26494. +/* ---------------------------------------------------------------------- */
  26495. +
  26496. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  26497. + unsigned int d_type, aufs_bindex_t bindex,
  26498. + unsigned char shwh)
  26499. +{
  26500. + int err;
  26501. + struct au_vdir_destr *str;
  26502. + struct au_vdir_wh *wh;
  26503. +
  26504. + AuDbg("%.*s\n", nlen, name);
  26505. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  26506. +
  26507. + err = -ENOMEM;
  26508. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  26509. + if (unlikely(!wh))
  26510. + goto out;
  26511. +
  26512. + err = 0;
  26513. + wh->wh_bindex = bindex;
  26514. + if (shwh)
  26515. + au_shwh_init_wh(wh, ino, d_type);
  26516. + str = &wh->wh_str;
  26517. + str->len = nlen;
  26518. + memcpy(str->name, name, nlen);
  26519. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  26520. + /* smp_mb(); */
  26521. +
  26522. +out:
  26523. + return err;
  26524. +}
  26525. +
  26526. +static int append_deblk(struct au_vdir *vdir)
  26527. +{
  26528. + int err;
  26529. + unsigned long ul;
  26530. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  26531. + union au_vdir_deblk_p p, deblk_end;
  26532. + unsigned char **o;
  26533. +
  26534. + err = -ENOMEM;
  26535. + o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  26536. + GFP_NOFS);
  26537. + if (unlikely(!o))
  26538. + goto out;
  26539. +
  26540. + vdir->vd_deblk = o;
  26541. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  26542. + if (p.deblk) {
  26543. + ul = vdir->vd_nblk++;
  26544. + vdir->vd_deblk[ul] = p.deblk;
  26545. + vdir->vd_last.ul = ul;
  26546. + vdir->vd_last.p.deblk = p.deblk;
  26547. + deblk_end.deblk = p.deblk + deblk_sz;
  26548. + err = set_deblk_end(&p, &deblk_end);
  26549. + }
  26550. +
  26551. +out:
  26552. + return err;
  26553. +}
  26554. +
  26555. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  26556. + unsigned int d_type, struct au_nhash *delist)
  26557. +{
  26558. + int err;
  26559. + unsigned int sz;
  26560. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  26561. + union au_vdir_deblk_p p, *room, deblk_end;
  26562. + struct au_vdir_dehstr *dehstr;
  26563. +
  26564. + p.deblk = last_deblk(vdir);
  26565. + deblk_end.deblk = p.deblk + deblk_sz;
  26566. + room = &vdir->vd_last.p;
  26567. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  26568. + || !is_deblk_end(room, &deblk_end));
  26569. +
  26570. + sz = calc_size(nlen);
  26571. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  26572. + err = append_deblk(vdir);
  26573. + if (unlikely(err))
  26574. + goto out;
  26575. +
  26576. + p.deblk = last_deblk(vdir);
  26577. + deblk_end.deblk = p.deblk + deblk_sz;
  26578. + /* smp_mb(); */
  26579. + AuDebugOn(room->deblk != p.deblk);
  26580. + }
  26581. +
  26582. + err = -ENOMEM;
  26583. + dehstr = au_cache_alloc_vdir_dehstr();
  26584. + if (unlikely(!dehstr))
  26585. + goto out;
  26586. +
  26587. + dehstr->str = &room->de->de_str;
  26588. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  26589. + room->de->de_ino = ino;
  26590. + room->de->de_type = d_type;
  26591. + room->de->de_str.len = nlen;
  26592. + memcpy(room->de->de_str.name, name, nlen);
  26593. +
  26594. + err = 0;
  26595. + room->deblk += sz;
  26596. + if (unlikely(set_deblk_end(room, &deblk_end)))
  26597. + err = append_deblk(vdir);
  26598. + /* smp_mb(); */
  26599. +
  26600. +out:
  26601. + return err;
  26602. +}
  26603. +
  26604. +/* ---------------------------------------------------------------------- */
  26605. +
  26606. +void au_vdir_free(struct au_vdir *vdir)
  26607. +{
  26608. + unsigned char **deblk;
  26609. +
  26610. + deblk = vdir->vd_deblk;
  26611. + while (vdir->vd_nblk--)
  26612. + kfree(*deblk++);
  26613. + kfree(vdir->vd_deblk);
  26614. + au_cache_free_vdir(vdir);
  26615. +}
  26616. +
  26617. +static struct au_vdir *alloc_vdir(struct file *file)
  26618. +{
  26619. + struct au_vdir *vdir;
  26620. + struct super_block *sb;
  26621. + int err;
  26622. +
  26623. + sb = file->f_path.dentry->d_sb;
  26624. + SiMustAnyLock(sb);
  26625. +
  26626. + err = -ENOMEM;
  26627. + vdir = au_cache_alloc_vdir();
  26628. + if (unlikely(!vdir))
  26629. + goto out;
  26630. +
  26631. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  26632. + if (unlikely(!vdir->vd_deblk))
  26633. + goto out_free;
  26634. +
  26635. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  26636. + if (!vdir->vd_deblk_sz) {
  26637. + /* estimate the apropriate size for deblk */
  26638. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  26639. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  26640. + }
  26641. + vdir->vd_nblk = 0;
  26642. + vdir->vd_version = 0;
  26643. + vdir->vd_jiffy = 0;
  26644. + err = append_deblk(vdir);
  26645. + if (!err)
  26646. + return vdir; /* success */
  26647. +
  26648. + kfree(vdir->vd_deblk);
  26649. +
  26650. +out_free:
  26651. + au_cache_free_vdir(vdir);
  26652. +out:
  26653. + vdir = ERR_PTR(err);
  26654. + return vdir;
  26655. +}
  26656. +
  26657. +static int reinit_vdir(struct au_vdir *vdir)
  26658. +{
  26659. + int err;
  26660. + union au_vdir_deblk_p p, deblk_end;
  26661. +
  26662. + while (vdir->vd_nblk > 1) {
  26663. + kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
  26664. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  26665. + vdir->vd_nblk--;
  26666. + }
  26667. + p.deblk = vdir->vd_deblk[0];
  26668. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  26669. + err = set_deblk_end(&p, &deblk_end);
  26670. + /* keep vd_dblk_sz */
  26671. + vdir->vd_last.ul = 0;
  26672. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  26673. + vdir->vd_version = 0;
  26674. + vdir->vd_jiffy = 0;
  26675. + /* smp_mb(); */
  26676. + return err;
  26677. +}
  26678. +
  26679. +/* ---------------------------------------------------------------------- */
  26680. +
  26681. +#define AuFillVdir_CALLED 1
  26682. +#define AuFillVdir_WHABLE (1 << 1)
  26683. +#define AuFillVdir_SHWH (1 << 2)
  26684. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  26685. +#define au_fset_fillvdir(flags, name) \
  26686. + do { (flags) |= AuFillVdir_##name; } while (0)
  26687. +#define au_fclr_fillvdir(flags, name) \
  26688. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  26689. +
  26690. +#ifndef CONFIG_AUFS_SHWH
  26691. +#undef AuFillVdir_SHWH
  26692. +#define AuFillVdir_SHWH 0
  26693. +#endif
  26694. +
  26695. +struct fillvdir_arg {
  26696. + struct dir_context ctx;
  26697. + struct file *file;
  26698. + struct au_vdir *vdir;
  26699. + struct au_nhash delist;
  26700. + struct au_nhash whlist;
  26701. + aufs_bindex_t bindex;
  26702. + unsigned int flags;
  26703. + int err;
  26704. +};
  26705. +
  26706. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  26707. + loff_t offset __maybe_unused, u64 h_ino,
  26708. + unsigned int d_type)
  26709. +{
  26710. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  26711. + char *name = (void *)__name;
  26712. + struct super_block *sb;
  26713. + ino_t ino;
  26714. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  26715. +
  26716. + arg->err = 0;
  26717. + sb = arg->file->f_path.dentry->d_sb;
  26718. + au_fset_fillvdir(arg->flags, CALLED);
  26719. + /* smp_mb(); */
  26720. + if (nlen <= AUFS_WH_PFX_LEN
  26721. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  26722. + if (test_known(&arg->delist, name, nlen)
  26723. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  26724. + goto out; /* already exists or whiteouted */
  26725. +
  26726. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  26727. + if (!arg->err) {
  26728. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  26729. + d_type = DT_UNKNOWN;
  26730. + arg->err = append_de(arg->vdir, name, nlen, ino,
  26731. + d_type, &arg->delist);
  26732. + }
  26733. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  26734. + name += AUFS_WH_PFX_LEN;
  26735. + nlen -= AUFS_WH_PFX_LEN;
  26736. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  26737. + goto out; /* already whiteouted */
  26738. +
  26739. + if (shwh)
  26740. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  26741. + &ino);
  26742. + if (!arg->err) {
  26743. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  26744. + d_type = DT_UNKNOWN;
  26745. + arg->err = au_nhash_append_wh
  26746. + (&arg->whlist, name, nlen, ino, d_type,
  26747. + arg->bindex, shwh);
  26748. + }
  26749. + }
  26750. +
  26751. +out:
  26752. + if (!arg->err)
  26753. + arg->vdir->vd_jiffy = jiffies;
  26754. + /* smp_mb(); */
  26755. + AuTraceErr(arg->err);
  26756. + return arg->err;
  26757. +}
  26758. +
  26759. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  26760. + struct au_nhash *whlist, struct au_nhash *delist)
  26761. +{
  26762. +#ifdef CONFIG_AUFS_SHWH
  26763. + int err;
  26764. + unsigned int nh, u;
  26765. + struct hlist_head *head;
  26766. + struct au_vdir_wh *pos;
  26767. + struct hlist_node *n;
  26768. + char *p, *o;
  26769. + struct au_vdir_destr *destr;
  26770. +
  26771. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  26772. +
  26773. + err = -ENOMEM;
  26774. + o = p = (void *)__get_free_page(GFP_NOFS);
  26775. + if (unlikely(!p))
  26776. + goto out;
  26777. +
  26778. + err = 0;
  26779. + nh = whlist->nh_num;
  26780. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  26781. + p += AUFS_WH_PFX_LEN;
  26782. + for (u = 0; u < nh; u++) {
  26783. + head = whlist->nh_head + u;
  26784. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  26785. + destr = &pos->wh_str;
  26786. + memcpy(p, destr->name, destr->len);
  26787. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  26788. + pos->wh_ino, pos->wh_type, delist);
  26789. + if (unlikely(err))
  26790. + break;
  26791. + }
  26792. + }
  26793. +
  26794. + free_page((unsigned long)o);
  26795. +
  26796. +out:
  26797. + AuTraceErr(err);
  26798. + return err;
  26799. +#else
  26800. + return 0;
  26801. +#endif
  26802. +}
  26803. +
  26804. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  26805. +{
  26806. + int err;
  26807. + unsigned int rdhash;
  26808. + loff_t offset;
  26809. + aufs_bindex_t bend, bindex, bstart;
  26810. + unsigned char shwh;
  26811. + struct file *hf, *file;
  26812. + struct super_block *sb;
  26813. +
  26814. + file = arg->file;
  26815. + sb = file->f_path.dentry->d_sb;
  26816. + SiMustAnyLock(sb);
  26817. +
  26818. + rdhash = au_sbi(sb)->si_rdhash;
  26819. + if (!rdhash)
  26820. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  26821. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  26822. + if (unlikely(err))
  26823. + goto out;
  26824. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  26825. + if (unlikely(err))
  26826. + goto out_delist;
  26827. +
  26828. + err = 0;
  26829. + arg->flags = 0;
  26830. + shwh = 0;
  26831. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  26832. + shwh = 1;
  26833. + au_fset_fillvdir(arg->flags, SHWH);
  26834. + }
  26835. + bstart = au_fbstart(file);
  26836. + bend = au_fbend_dir(file);
  26837. + for (bindex = bstart; !err && bindex <= bend; bindex++) {
  26838. + hf = au_hf_dir(file, bindex);
  26839. + if (!hf)
  26840. + continue;
  26841. +
  26842. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  26843. + err = offset;
  26844. + if (unlikely(offset))
  26845. + break;
  26846. +
  26847. + arg->bindex = bindex;
  26848. + au_fclr_fillvdir(arg->flags, WHABLE);
  26849. + if (shwh
  26850. + || (bindex != bend
  26851. + && au_br_whable(au_sbr_perm(sb, bindex))))
  26852. + au_fset_fillvdir(arg->flags, WHABLE);
  26853. + do {
  26854. + arg->err = 0;
  26855. + au_fclr_fillvdir(arg->flags, CALLED);
  26856. + /* smp_mb(); */
  26857. + err = vfsub_iterate_dir(hf, &arg->ctx);
  26858. + if (err >= 0)
  26859. + err = arg->err;
  26860. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  26861. +
  26862. + /*
  26863. + * dir_relax() may be good for concurrency, but aufs should not
  26864. + * use it since it will cause a lockdep problem.
  26865. + */
  26866. + }
  26867. +
  26868. + if (!err && shwh)
  26869. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  26870. +
  26871. + au_nhash_wh_free(&arg->whlist);
  26872. +
  26873. +out_delist:
  26874. + au_nhash_de_free(&arg->delist);
  26875. +out:
  26876. + return err;
  26877. +}
  26878. +
  26879. +static int read_vdir(struct file *file, int may_read)
  26880. +{
  26881. + int err;
  26882. + unsigned long expire;
  26883. + unsigned char do_read;
  26884. + struct fillvdir_arg arg = {
  26885. + .ctx = {
  26886. + .actor = fillvdir
  26887. + }
  26888. + };
  26889. + struct inode *inode;
  26890. + struct au_vdir *vdir, *allocated;
  26891. +
  26892. + err = 0;
  26893. + inode = file_inode(file);
  26894. + IMustLock(inode);
  26895. + SiMustAnyLock(inode->i_sb);
  26896. +
  26897. + allocated = NULL;
  26898. + do_read = 0;
  26899. + expire = au_sbi(inode->i_sb)->si_rdcache;
  26900. + vdir = au_ivdir(inode);
  26901. + if (!vdir) {
  26902. + do_read = 1;
  26903. + vdir = alloc_vdir(file);
  26904. + err = PTR_ERR(vdir);
  26905. + if (IS_ERR(vdir))
  26906. + goto out;
  26907. + err = 0;
  26908. + allocated = vdir;
  26909. + } else if (may_read
  26910. + && (inode->i_version != vdir->vd_version
  26911. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  26912. + do_read = 1;
  26913. + err = reinit_vdir(vdir);
  26914. + if (unlikely(err))
  26915. + goto out;
  26916. + }
  26917. +
  26918. + if (!do_read)
  26919. + return 0; /* success */
  26920. +
  26921. + arg.file = file;
  26922. + arg.vdir = vdir;
  26923. + err = au_do_read_vdir(&arg);
  26924. + if (!err) {
  26925. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  26926. + vdir->vd_version = inode->i_version;
  26927. + vdir->vd_last.ul = 0;
  26928. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  26929. + if (allocated)
  26930. + au_set_ivdir(inode, allocated);
  26931. + } else if (allocated)
  26932. + au_vdir_free(allocated);
  26933. +
  26934. +out:
  26935. + return err;
  26936. +}
  26937. +
  26938. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  26939. +{
  26940. + int err, rerr;
  26941. + unsigned long ul, n;
  26942. + const unsigned int deblk_sz = src->vd_deblk_sz;
  26943. +
  26944. + AuDebugOn(tgt->vd_nblk != 1);
  26945. +
  26946. + err = -ENOMEM;
  26947. + if (tgt->vd_nblk < src->vd_nblk) {
  26948. + unsigned char **p;
  26949. +
  26950. + p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  26951. + GFP_NOFS);
  26952. + if (unlikely(!p))
  26953. + goto out;
  26954. + tgt->vd_deblk = p;
  26955. + }
  26956. +
  26957. + if (tgt->vd_deblk_sz != deblk_sz) {
  26958. + unsigned char *p;
  26959. +
  26960. + tgt->vd_deblk_sz = deblk_sz;
  26961. + p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
  26962. + if (unlikely(!p))
  26963. + goto out;
  26964. + tgt->vd_deblk[0] = p;
  26965. + }
  26966. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  26967. + tgt->vd_version = src->vd_version;
  26968. + tgt->vd_jiffy = src->vd_jiffy;
  26969. +
  26970. + n = src->vd_nblk;
  26971. + for (ul = 1; ul < n; ul++) {
  26972. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  26973. + GFP_NOFS);
  26974. + if (unlikely(!tgt->vd_deblk[ul]))
  26975. + goto out;
  26976. + tgt->vd_nblk++;
  26977. + }
  26978. + tgt->vd_nblk = n;
  26979. + tgt->vd_last.ul = tgt->vd_last.ul;
  26980. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  26981. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  26982. + - src->vd_deblk[src->vd_last.ul];
  26983. + /* smp_mb(); */
  26984. + return 0; /* success */
  26985. +
  26986. +out:
  26987. + rerr = reinit_vdir(tgt);
  26988. + BUG_ON(rerr);
  26989. + return err;
  26990. +}
  26991. +
  26992. +int au_vdir_init(struct file *file)
  26993. +{
  26994. + int err;
  26995. + struct inode *inode;
  26996. + struct au_vdir *vdir_cache, *allocated;
  26997. +
  26998. + /* test file->f_pos here instead of ctx->pos */
  26999. + err = read_vdir(file, !file->f_pos);
  27000. + if (unlikely(err))
  27001. + goto out;
  27002. +
  27003. + allocated = NULL;
  27004. + vdir_cache = au_fvdir_cache(file);
  27005. + if (!vdir_cache) {
  27006. + vdir_cache = alloc_vdir(file);
  27007. + err = PTR_ERR(vdir_cache);
  27008. + if (IS_ERR(vdir_cache))
  27009. + goto out;
  27010. + allocated = vdir_cache;
  27011. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  27012. + /* test file->f_pos here instead of ctx->pos */
  27013. + err = reinit_vdir(vdir_cache);
  27014. + if (unlikely(err))
  27015. + goto out;
  27016. + } else
  27017. + return 0; /* success */
  27018. +
  27019. + inode = file_inode(file);
  27020. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  27021. + if (!err) {
  27022. + file->f_version = inode->i_version;
  27023. + if (allocated)
  27024. + au_set_fvdir_cache(file, allocated);
  27025. + } else if (allocated)
  27026. + au_vdir_free(allocated);
  27027. +
  27028. +out:
  27029. + return err;
  27030. +}
  27031. +
  27032. +static loff_t calc_offset(struct au_vdir *vdir)
  27033. +{
  27034. + loff_t offset;
  27035. + union au_vdir_deblk_p p;
  27036. +
  27037. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  27038. + offset = vdir->vd_last.p.deblk - p.deblk;
  27039. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  27040. + return offset;
  27041. +}
  27042. +
  27043. +/* returns true or false */
  27044. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  27045. +{
  27046. + int valid;
  27047. + unsigned int deblk_sz;
  27048. + unsigned long ul, n;
  27049. + loff_t offset;
  27050. + union au_vdir_deblk_p p, deblk_end;
  27051. + struct au_vdir *vdir_cache;
  27052. +
  27053. + valid = 1;
  27054. + vdir_cache = au_fvdir_cache(file);
  27055. + offset = calc_offset(vdir_cache);
  27056. + AuDbg("offset %lld\n", offset);
  27057. + if (ctx->pos == offset)
  27058. + goto out;
  27059. +
  27060. + vdir_cache->vd_last.ul = 0;
  27061. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  27062. + if (!ctx->pos)
  27063. + goto out;
  27064. +
  27065. + valid = 0;
  27066. + deblk_sz = vdir_cache->vd_deblk_sz;
  27067. + ul = div64_u64(ctx->pos, deblk_sz);
  27068. + AuDbg("ul %lu\n", ul);
  27069. + if (ul >= vdir_cache->vd_nblk)
  27070. + goto out;
  27071. +
  27072. + n = vdir_cache->vd_nblk;
  27073. + for (; ul < n; ul++) {
  27074. + p.deblk = vdir_cache->vd_deblk[ul];
  27075. + deblk_end.deblk = p.deblk + deblk_sz;
  27076. + offset = ul;
  27077. + offset *= deblk_sz;
  27078. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  27079. + unsigned int l;
  27080. +
  27081. + l = calc_size(p.de->de_str.len);
  27082. + offset += l;
  27083. + p.deblk += l;
  27084. + }
  27085. + if (!is_deblk_end(&p, &deblk_end)) {
  27086. + valid = 1;
  27087. + vdir_cache->vd_last.ul = ul;
  27088. + vdir_cache->vd_last.p = p;
  27089. + break;
  27090. + }
  27091. + }
  27092. +
  27093. +out:
  27094. + /* smp_mb(); */
  27095. + AuTraceErr(!valid);
  27096. + return valid;
  27097. +}
  27098. +
  27099. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  27100. +{
  27101. + unsigned int l, deblk_sz;
  27102. + union au_vdir_deblk_p deblk_end;
  27103. + struct au_vdir *vdir_cache;
  27104. + struct au_vdir_de *de;
  27105. +
  27106. + vdir_cache = au_fvdir_cache(file);
  27107. + if (!seek_vdir(file, ctx))
  27108. + return 0;
  27109. +
  27110. + deblk_sz = vdir_cache->vd_deblk_sz;
  27111. + while (1) {
  27112. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  27113. + deblk_end.deblk += deblk_sz;
  27114. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  27115. + de = vdir_cache->vd_last.p.de;
  27116. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  27117. + de->de_str.len, de->de_str.name, ctx->pos,
  27118. + (unsigned long)de->de_ino, de->de_type);
  27119. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  27120. + de->de_str.len, de->de_ino,
  27121. + de->de_type))) {
  27122. + /* todo: ignore the error caused by udba? */
  27123. + /* return err; */
  27124. + return 0;
  27125. + }
  27126. +
  27127. + l = calc_size(de->de_str.len);
  27128. + vdir_cache->vd_last.p.deblk += l;
  27129. + ctx->pos += l;
  27130. + }
  27131. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  27132. + vdir_cache->vd_last.ul++;
  27133. + vdir_cache->vd_last.p.deblk
  27134. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  27135. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  27136. + continue;
  27137. + }
  27138. + break;
  27139. + }
  27140. +
  27141. + /* smp_mb(); */
  27142. + return 0;
  27143. +}
  27144. diff -Nur linux-4.0.5.orig/fs/aufs/vfsub.c linux-4.0.5/fs/aufs/vfsub.c
  27145. --- linux-4.0.5.orig/fs/aufs/vfsub.c 1969-12-31 18:00:00.000000000 -0600
  27146. +++ linux-4.0.5/fs/aufs/vfsub.c 2015-06-21 14:07:48.690432781 -0500
  27147. @@ -0,0 +1,846 @@
  27148. +/*
  27149. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27150. + *
  27151. + * This program, aufs is free software; you can redistribute it and/or modify
  27152. + * it under the terms of the GNU General Public License as published by
  27153. + * the Free Software Foundation; either version 2 of the License, or
  27154. + * (at your option) any later version.
  27155. + *
  27156. + * This program is distributed in the hope that it will be useful,
  27157. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27158. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27159. + * GNU General Public License for more details.
  27160. + *
  27161. + * You should have received a copy of the GNU General Public License
  27162. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27163. + */
  27164. +
  27165. +/*
  27166. + * sub-routines for VFS
  27167. + */
  27168. +
  27169. +#include <linux/namei.h>
  27170. +#include <linux/security.h>
  27171. +#include <linux/splice.h>
  27172. +#include "aufs.h"
  27173. +
  27174. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  27175. +{
  27176. + int err;
  27177. + struct kstat st;
  27178. + struct super_block *h_sb;
  27179. +
  27180. + /* for remote fs, leave work for its getattr or d_revalidate */
  27181. + /* for bad i_attr fs, handle them in aufs_getattr() */
  27182. + /* still some fs may acquire i_mutex. we need to skip them */
  27183. + err = 0;
  27184. + if (!did)
  27185. + did = &err;
  27186. + h_sb = h_path->dentry->d_sb;
  27187. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  27188. + if (*did)
  27189. + err = vfs_getattr(h_path, &st);
  27190. +
  27191. + return err;
  27192. +}
  27193. +
  27194. +/* ---------------------------------------------------------------------- */
  27195. +
  27196. +struct file *vfsub_dentry_open(struct path *path, int flags)
  27197. +{
  27198. + struct file *file;
  27199. +
  27200. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  27201. + current_cred());
  27202. + if (!IS_ERR_OR_NULL(file)
  27203. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  27204. + i_readcount_inc(path->dentry->d_inode);
  27205. +
  27206. + return file;
  27207. +}
  27208. +
  27209. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  27210. +{
  27211. + struct file *file;
  27212. +
  27213. + lockdep_off();
  27214. + file = filp_open(path,
  27215. + oflags /* | __FMODE_NONOTIFY */,
  27216. + mode);
  27217. + lockdep_on();
  27218. + if (IS_ERR(file))
  27219. + goto out;
  27220. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  27221. +
  27222. +out:
  27223. + return file;
  27224. +}
  27225. +
  27226. +/*
  27227. + * Ideally this function should call VFS:do_last() in order to keep all its
  27228. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  27229. + * structure such as nameidata. This is a second (or third) best approach.
  27230. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  27231. + */
  27232. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  27233. + struct vfsub_aopen_args *args, struct au_branch *br)
  27234. +{
  27235. + int err;
  27236. + struct file *file = args->file;
  27237. + /* copied from linux/fs/namei.c:atomic_open() */
  27238. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  27239. +
  27240. + IMustLock(dir);
  27241. + AuDebugOn(!dir->i_op->atomic_open);
  27242. +
  27243. + err = au_br_test_oflag(args->open_flag, br);
  27244. + if (unlikely(err))
  27245. + goto out;
  27246. +
  27247. + args->file->f_path.dentry = DENTRY_NOT_SET;
  27248. + args->file->f_path.mnt = au_br_mnt(br);
  27249. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  27250. + args->create_mode, args->opened);
  27251. + if (err >= 0) {
  27252. + /* some filesystems don't set FILE_CREATED while succeeded? */
  27253. + if (*args->opened & FILE_CREATED)
  27254. + fsnotify_create(dir, dentry);
  27255. + } else
  27256. + goto out;
  27257. +
  27258. +
  27259. + if (!err) {
  27260. + /* todo: call VFS:may_open() here */
  27261. + err = open_check_o_direct(file);
  27262. + /* todo: ima_file_check() too? */
  27263. + if (!err && (args->open_flag & __FMODE_EXEC))
  27264. + err = deny_write_access(file);
  27265. + if (unlikely(err))
  27266. + /* note that the file is created and still opened */
  27267. + goto out;
  27268. + }
  27269. +
  27270. + atomic_inc(&br->br_count);
  27271. + fsnotify_open(file);
  27272. +
  27273. +out:
  27274. + return err;
  27275. +}
  27276. +
  27277. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  27278. +{
  27279. + int err;
  27280. +
  27281. + err = kern_path(name, flags, path);
  27282. + if (!err && path->dentry->d_inode)
  27283. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  27284. + return err;
  27285. +}
  27286. +
  27287. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  27288. + int len)
  27289. +{
  27290. + struct path path = {
  27291. + .mnt = NULL
  27292. + };
  27293. +
  27294. + /* VFS checks it too, but by WARN_ON_ONCE() */
  27295. + IMustLock(parent->d_inode);
  27296. +
  27297. + path.dentry = lookup_one_len(name, parent, len);
  27298. + if (IS_ERR(path.dentry))
  27299. + goto out;
  27300. + if (path.dentry->d_inode)
  27301. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  27302. +
  27303. +out:
  27304. + AuTraceErrPtr(path.dentry);
  27305. + return path.dentry;
  27306. +}
  27307. +
  27308. +void vfsub_call_lkup_one(void *args)
  27309. +{
  27310. + struct vfsub_lkup_one_args *a = args;
  27311. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  27312. +}
  27313. +
  27314. +/* ---------------------------------------------------------------------- */
  27315. +
  27316. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  27317. + struct dentry *d2, struct au_hinode *hdir2)
  27318. +{
  27319. + struct dentry *d;
  27320. +
  27321. + lockdep_off();
  27322. + d = lock_rename(d1, d2);
  27323. + lockdep_on();
  27324. + au_hn_suspend(hdir1);
  27325. + if (hdir1 != hdir2)
  27326. + au_hn_suspend(hdir2);
  27327. +
  27328. + return d;
  27329. +}
  27330. +
  27331. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  27332. + struct dentry *d2, struct au_hinode *hdir2)
  27333. +{
  27334. + au_hn_resume(hdir1);
  27335. + if (hdir1 != hdir2)
  27336. + au_hn_resume(hdir2);
  27337. + lockdep_off();
  27338. + unlock_rename(d1, d2);
  27339. + lockdep_on();
  27340. +}
  27341. +
  27342. +/* ---------------------------------------------------------------------- */
  27343. +
  27344. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  27345. +{
  27346. + int err;
  27347. + struct dentry *d;
  27348. +
  27349. + IMustLock(dir);
  27350. +
  27351. + d = path->dentry;
  27352. + path->dentry = d->d_parent;
  27353. + err = security_path_mknod(path, d, mode, 0);
  27354. + path->dentry = d;
  27355. + if (unlikely(err))
  27356. + goto out;
  27357. +
  27358. + lockdep_off();
  27359. + err = vfs_create(dir, path->dentry, mode, want_excl);
  27360. + lockdep_on();
  27361. + if (!err) {
  27362. + struct path tmp = *path;
  27363. + int did;
  27364. +
  27365. + vfsub_update_h_iattr(&tmp, &did);
  27366. + if (did) {
  27367. + tmp.dentry = path->dentry->d_parent;
  27368. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27369. + }
  27370. + /*ignore*/
  27371. + }
  27372. +
  27373. +out:
  27374. + return err;
  27375. +}
  27376. +
  27377. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  27378. +{
  27379. + int err;
  27380. + struct dentry *d;
  27381. +
  27382. + IMustLock(dir);
  27383. +
  27384. + d = path->dentry;
  27385. + path->dentry = d->d_parent;
  27386. + err = security_path_symlink(path, d, symname);
  27387. + path->dentry = d;
  27388. + if (unlikely(err))
  27389. + goto out;
  27390. +
  27391. + lockdep_off();
  27392. + err = vfs_symlink(dir, path->dentry, symname);
  27393. + lockdep_on();
  27394. + if (!err) {
  27395. + struct path tmp = *path;
  27396. + int did;
  27397. +
  27398. + vfsub_update_h_iattr(&tmp, &did);
  27399. + if (did) {
  27400. + tmp.dentry = path->dentry->d_parent;
  27401. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27402. + }
  27403. + /*ignore*/
  27404. + }
  27405. +
  27406. +out:
  27407. + return err;
  27408. +}
  27409. +
  27410. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  27411. +{
  27412. + int err;
  27413. + struct dentry *d;
  27414. +
  27415. + IMustLock(dir);
  27416. +
  27417. + d = path->dentry;
  27418. + path->dentry = d->d_parent;
  27419. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  27420. + path->dentry = d;
  27421. + if (unlikely(err))
  27422. + goto out;
  27423. +
  27424. + lockdep_off();
  27425. + err = vfs_mknod(dir, path->dentry, mode, dev);
  27426. + lockdep_on();
  27427. + if (!err) {
  27428. + struct path tmp = *path;
  27429. + int did;
  27430. +
  27431. + vfsub_update_h_iattr(&tmp, &did);
  27432. + if (did) {
  27433. + tmp.dentry = path->dentry->d_parent;
  27434. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27435. + }
  27436. + /*ignore*/
  27437. + }
  27438. +
  27439. +out:
  27440. + return err;
  27441. +}
  27442. +
  27443. +static int au_test_nlink(struct inode *inode)
  27444. +{
  27445. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  27446. +
  27447. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  27448. + || inode->i_nlink < link_max)
  27449. + return 0;
  27450. + return -EMLINK;
  27451. +}
  27452. +
  27453. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  27454. + struct inode **delegated_inode)
  27455. +{
  27456. + int err;
  27457. + struct dentry *d;
  27458. +
  27459. + IMustLock(dir);
  27460. +
  27461. + err = au_test_nlink(src_dentry->d_inode);
  27462. + if (unlikely(err))
  27463. + return err;
  27464. +
  27465. + /* we don't call may_linkat() */
  27466. + d = path->dentry;
  27467. + path->dentry = d->d_parent;
  27468. + err = security_path_link(src_dentry, path, d);
  27469. + path->dentry = d;
  27470. + if (unlikely(err))
  27471. + goto out;
  27472. +
  27473. + lockdep_off();
  27474. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  27475. + lockdep_on();
  27476. + if (!err) {
  27477. + struct path tmp = *path;
  27478. + int did;
  27479. +
  27480. + /* fuse has different memory inode for the same inumber */
  27481. + vfsub_update_h_iattr(&tmp, &did);
  27482. + if (did) {
  27483. + tmp.dentry = path->dentry->d_parent;
  27484. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27485. + tmp.dentry = src_dentry;
  27486. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27487. + }
  27488. + /*ignore*/
  27489. + }
  27490. +
  27491. +out:
  27492. + return err;
  27493. +}
  27494. +
  27495. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  27496. + struct inode *dir, struct path *path,
  27497. + struct inode **delegated_inode)
  27498. +{
  27499. + int err;
  27500. + struct path tmp = {
  27501. + .mnt = path->mnt
  27502. + };
  27503. + struct dentry *d;
  27504. +
  27505. + IMustLock(dir);
  27506. + IMustLock(src_dir);
  27507. +
  27508. + d = path->dentry;
  27509. + path->dentry = d->d_parent;
  27510. + tmp.dentry = src_dentry->d_parent;
  27511. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  27512. + path->dentry = d;
  27513. + if (unlikely(err))
  27514. + goto out;
  27515. +
  27516. + lockdep_off();
  27517. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  27518. + delegated_inode, /*flags*/0);
  27519. + lockdep_on();
  27520. + if (!err) {
  27521. + int did;
  27522. +
  27523. + tmp.dentry = d->d_parent;
  27524. + vfsub_update_h_iattr(&tmp, &did);
  27525. + if (did) {
  27526. + tmp.dentry = src_dentry;
  27527. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27528. + tmp.dentry = src_dentry->d_parent;
  27529. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27530. + }
  27531. + /*ignore*/
  27532. + }
  27533. +
  27534. +out:
  27535. + return err;
  27536. +}
  27537. +
  27538. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  27539. +{
  27540. + int err;
  27541. + struct dentry *d;
  27542. +
  27543. + IMustLock(dir);
  27544. +
  27545. + d = path->dentry;
  27546. + path->dentry = d->d_parent;
  27547. + err = security_path_mkdir(path, d, mode);
  27548. + path->dentry = d;
  27549. + if (unlikely(err))
  27550. + goto out;
  27551. +
  27552. + lockdep_off();
  27553. + err = vfs_mkdir(dir, path->dentry, mode);
  27554. + lockdep_on();
  27555. + if (!err) {
  27556. + struct path tmp = *path;
  27557. + int did;
  27558. +
  27559. + vfsub_update_h_iattr(&tmp, &did);
  27560. + if (did) {
  27561. + tmp.dentry = path->dentry->d_parent;
  27562. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  27563. + }
  27564. + /*ignore*/
  27565. + }
  27566. +
  27567. +out:
  27568. + return err;
  27569. +}
  27570. +
  27571. +int vfsub_rmdir(struct inode *dir, struct path *path)
  27572. +{
  27573. + int err;
  27574. + struct dentry *d;
  27575. +
  27576. + IMustLock(dir);
  27577. +
  27578. + d = path->dentry;
  27579. + path->dentry = d->d_parent;
  27580. + err = security_path_rmdir(path, d);
  27581. + path->dentry = d;
  27582. + if (unlikely(err))
  27583. + goto out;
  27584. +
  27585. + lockdep_off();
  27586. + err = vfs_rmdir(dir, path->dentry);
  27587. + lockdep_on();
  27588. + if (!err) {
  27589. + struct path tmp = {
  27590. + .dentry = path->dentry->d_parent,
  27591. + .mnt = path->mnt
  27592. + };
  27593. +
  27594. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  27595. + }
  27596. +
  27597. +out:
  27598. + return err;
  27599. +}
  27600. +
  27601. +/* ---------------------------------------------------------------------- */
  27602. +
  27603. +/* todo: support mmap_sem? */
  27604. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  27605. + loff_t *ppos)
  27606. +{
  27607. + ssize_t err;
  27608. +
  27609. + lockdep_off();
  27610. + err = vfs_read(file, ubuf, count, ppos);
  27611. + lockdep_on();
  27612. + if (err >= 0)
  27613. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  27614. + return err;
  27615. +}
  27616. +
  27617. +/* todo: kernel_read()? */
  27618. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  27619. + loff_t *ppos)
  27620. +{
  27621. + ssize_t err;
  27622. + mm_segment_t oldfs;
  27623. + union {
  27624. + void *k;
  27625. + char __user *u;
  27626. + } buf;
  27627. +
  27628. + buf.k = kbuf;
  27629. + oldfs = get_fs();
  27630. + set_fs(KERNEL_DS);
  27631. + err = vfsub_read_u(file, buf.u, count, ppos);
  27632. + set_fs(oldfs);
  27633. + return err;
  27634. +}
  27635. +
  27636. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  27637. + loff_t *ppos)
  27638. +{
  27639. + ssize_t err;
  27640. +
  27641. + lockdep_off();
  27642. + err = vfs_write(file, ubuf, count, ppos);
  27643. + lockdep_on();
  27644. + if (err >= 0)
  27645. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  27646. + return err;
  27647. +}
  27648. +
  27649. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  27650. +{
  27651. + ssize_t err;
  27652. + mm_segment_t oldfs;
  27653. + union {
  27654. + void *k;
  27655. + const char __user *u;
  27656. + } buf;
  27657. +
  27658. + buf.k = kbuf;
  27659. + oldfs = get_fs();
  27660. + set_fs(KERNEL_DS);
  27661. + err = vfsub_write_u(file, buf.u, count, ppos);
  27662. + set_fs(oldfs);
  27663. + return err;
  27664. +}
  27665. +
  27666. +int vfsub_flush(struct file *file, fl_owner_t id)
  27667. +{
  27668. + int err;
  27669. +
  27670. + err = 0;
  27671. + if (file->f_op->flush) {
  27672. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  27673. + err = file->f_op->flush(file, id);
  27674. + else {
  27675. + lockdep_off();
  27676. + err = file->f_op->flush(file, id);
  27677. + lockdep_on();
  27678. + }
  27679. + if (!err)
  27680. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  27681. + /*ignore*/
  27682. + }
  27683. + return err;
  27684. +}
  27685. +
  27686. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  27687. +{
  27688. + int err;
  27689. +
  27690. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  27691. +
  27692. + lockdep_off();
  27693. + err = iterate_dir(file, ctx);
  27694. + lockdep_on();
  27695. + if (err >= 0)
  27696. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  27697. + return err;
  27698. +}
  27699. +
  27700. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  27701. + struct pipe_inode_info *pipe, size_t len,
  27702. + unsigned int flags)
  27703. +{
  27704. + long err;
  27705. +
  27706. + lockdep_off();
  27707. + err = do_splice_to(in, ppos, pipe, len, flags);
  27708. + lockdep_on();
  27709. + file_accessed(in);
  27710. + if (err >= 0)
  27711. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  27712. + return err;
  27713. +}
  27714. +
  27715. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  27716. + loff_t *ppos, size_t len, unsigned int flags)
  27717. +{
  27718. + long err;
  27719. +
  27720. + lockdep_off();
  27721. + err = do_splice_from(pipe, out, ppos, len, flags);
  27722. + lockdep_on();
  27723. + if (err >= 0)
  27724. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  27725. + return err;
  27726. +}
  27727. +
  27728. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  27729. +{
  27730. + int err;
  27731. +
  27732. + /* file can be NULL */
  27733. + lockdep_off();
  27734. + err = vfs_fsync(file, datasync);
  27735. + lockdep_on();
  27736. + if (!err) {
  27737. + if (!path) {
  27738. + AuDebugOn(!file);
  27739. + path = &file->f_path;
  27740. + }
  27741. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  27742. + }
  27743. + return err;
  27744. +}
  27745. +
  27746. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  27747. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  27748. + struct file *h_file)
  27749. +{
  27750. + int err;
  27751. + struct inode *h_inode;
  27752. + struct super_block *h_sb;
  27753. +
  27754. + if (!h_file) {
  27755. + err = vfsub_truncate(h_path, length);
  27756. + goto out;
  27757. + }
  27758. +
  27759. + h_inode = h_path->dentry->d_inode;
  27760. + h_sb = h_inode->i_sb;
  27761. + lockdep_off();
  27762. + sb_start_write(h_sb);
  27763. + lockdep_on();
  27764. + err = locks_verify_truncate(h_inode, h_file, length);
  27765. + if (!err)
  27766. + err = security_path_truncate(h_path);
  27767. + if (!err) {
  27768. + lockdep_off();
  27769. + err = do_truncate(h_path->dentry, length, attr, h_file);
  27770. + lockdep_on();
  27771. + }
  27772. + lockdep_off();
  27773. + sb_end_write(h_sb);
  27774. + lockdep_on();
  27775. +
  27776. +out:
  27777. + return err;
  27778. +}
  27779. +
  27780. +/* ---------------------------------------------------------------------- */
  27781. +
  27782. +struct au_vfsub_mkdir_args {
  27783. + int *errp;
  27784. + struct inode *dir;
  27785. + struct path *path;
  27786. + int mode;
  27787. +};
  27788. +
  27789. +static void au_call_vfsub_mkdir(void *args)
  27790. +{
  27791. + struct au_vfsub_mkdir_args *a = args;
  27792. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  27793. +}
  27794. +
  27795. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  27796. +{
  27797. + int err, do_sio, wkq_err;
  27798. +
  27799. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  27800. + if (!do_sio) {
  27801. + lockdep_off();
  27802. + err = vfsub_mkdir(dir, path, mode);
  27803. + lockdep_on();
  27804. + } else {
  27805. + struct au_vfsub_mkdir_args args = {
  27806. + .errp = &err,
  27807. + .dir = dir,
  27808. + .path = path,
  27809. + .mode = mode
  27810. + };
  27811. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  27812. + if (unlikely(wkq_err))
  27813. + err = wkq_err;
  27814. + }
  27815. +
  27816. + return err;
  27817. +}
  27818. +
  27819. +struct au_vfsub_rmdir_args {
  27820. + int *errp;
  27821. + struct inode *dir;
  27822. + struct path *path;
  27823. +};
  27824. +
  27825. +static void au_call_vfsub_rmdir(void *args)
  27826. +{
  27827. + struct au_vfsub_rmdir_args *a = args;
  27828. + *a->errp = vfsub_rmdir(a->dir, a->path);
  27829. +}
  27830. +
  27831. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  27832. +{
  27833. + int err, do_sio, wkq_err;
  27834. +
  27835. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  27836. + if (!do_sio) {
  27837. + lockdep_off();
  27838. + err = vfsub_rmdir(dir, path);
  27839. + lockdep_on();
  27840. + } else {
  27841. + struct au_vfsub_rmdir_args args = {
  27842. + .errp = &err,
  27843. + .dir = dir,
  27844. + .path = path
  27845. + };
  27846. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  27847. + if (unlikely(wkq_err))
  27848. + err = wkq_err;
  27849. + }
  27850. +
  27851. + return err;
  27852. +}
  27853. +
  27854. +/* ---------------------------------------------------------------------- */
  27855. +
  27856. +struct notify_change_args {
  27857. + int *errp;
  27858. + struct path *path;
  27859. + struct iattr *ia;
  27860. + struct inode **delegated_inode;
  27861. +};
  27862. +
  27863. +static void call_notify_change(void *args)
  27864. +{
  27865. + struct notify_change_args *a = args;
  27866. + struct inode *h_inode;
  27867. +
  27868. + h_inode = a->path->dentry->d_inode;
  27869. + IMustLock(h_inode);
  27870. +
  27871. + *a->errp = -EPERM;
  27872. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  27873. + lockdep_off();
  27874. + *a->errp = notify_change(a->path->dentry, a->ia,
  27875. + a->delegated_inode);
  27876. + lockdep_on();
  27877. + if (!*a->errp)
  27878. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  27879. + }
  27880. + AuTraceErr(*a->errp);
  27881. +}
  27882. +
  27883. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  27884. + struct inode **delegated_inode)
  27885. +{
  27886. + int err;
  27887. + struct notify_change_args args = {
  27888. + .errp = &err,
  27889. + .path = path,
  27890. + .ia = ia,
  27891. + .delegated_inode = delegated_inode
  27892. + };
  27893. +
  27894. + call_notify_change(&args);
  27895. +
  27896. + return err;
  27897. +}
  27898. +
  27899. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  27900. + struct inode **delegated_inode)
  27901. +{
  27902. + int err, wkq_err;
  27903. + struct notify_change_args args = {
  27904. + .errp = &err,
  27905. + .path = path,
  27906. + .ia = ia,
  27907. + .delegated_inode = delegated_inode
  27908. + };
  27909. +
  27910. + wkq_err = au_wkq_wait(call_notify_change, &args);
  27911. + if (unlikely(wkq_err))
  27912. + err = wkq_err;
  27913. +
  27914. + return err;
  27915. +}
  27916. +
  27917. +/* ---------------------------------------------------------------------- */
  27918. +
  27919. +struct unlink_args {
  27920. + int *errp;
  27921. + struct inode *dir;
  27922. + struct path *path;
  27923. + struct inode **delegated_inode;
  27924. +};
  27925. +
  27926. +static void call_unlink(void *args)
  27927. +{
  27928. + struct unlink_args *a = args;
  27929. + struct dentry *d = a->path->dentry;
  27930. + struct inode *h_inode;
  27931. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  27932. + && au_dcount(d) == 1);
  27933. +
  27934. + IMustLock(a->dir);
  27935. +
  27936. + a->path->dentry = d->d_parent;
  27937. + *a->errp = security_path_unlink(a->path, d);
  27938. + a->path->dentry = d;
  27939. + if (unlikely(*a->errp))
  27940. + return;
  27941. +
  27942. + if (!stop_sillyrename)
  27943. + dget(d);
  27944. + h_inode = d->d_inode;
  27945. + if (h_inode)
  27946. + ihold(h_inode);
  27947. +
  27948. + lockdep_off();
  27949. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  27950. + lockdep_on();
  27951. + if (!*a->errp) {
  27952. + struct path tmp = {
  27953. + .dentry = d->d_parent,
  27954. + .mnt = a->path->mnt
  27955. + };
  27956. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  27957. + }
  27958. +
  27959. + if (!stop_sillyrename)
  27960. + dput(d);
  27961. + if (h_inode)
  27962. + iput(h_inode);
  27963. +
  27964. + AuTraceErr(*a->errp);
  27965. +}
  27966. +
  27967. +/*
  27968. + * @dir: must be locked.
  27969. + * @dentry: target dentry.
  27970. + */
  27971. +int vfsub_unlink(struct inode *dir, struct path *path,
  27972. + struct inode **delegated_inode, int force)
  27973. +{
  27974. + int err;
  27975. + struct unlink_args args = {
  27976. + .errp = &err,
  27977. + .dir = dir,
  27978. + .path = path,
  27979. + .delegated_inode = delegated_inode
  27980. + };
  27981. +
  27982. + if (!force)
  27983. + call_unlink(&args);
  27984. + else {
  27985. + int wkq_err;
  27986. +
  27987. + wkq_err = au_wkq_wait(call_unlink, &args);
  27988. + if (unlikely(wkq_err))
  27989. + err = wkq_err;
  27990. + }
  27991. +
  27992. + return err;
  27993. +}
  27994. diff -Nur linux-4.0.5.orig/fs/aufs/vfsub.h linux-4.0.5/fs/aufs/vfsub.h
  27995. --- linux-4.0.5.orig/fs/aufs/vfsub.h 1969-12-31 18:00:00.000000000 -0600
  27996. +++ linux-4.0.5/fs/aufs/vfsub.h 2015-06-21 14:07:48.690432781 -0500
  27997. @@ -0,0 +1,286 @@
  27998. +/*
  27999. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  28000. + *
  28001. + * This program, aufs is free software; you can redistribute it and/or modify
  28002. + * it under the terms of the GNU General Public License as published by
  28003. + * the Free Software Foundation; either version 2 of the License, or
  28004. + * (at your option) any later version.
  28005. + *
  28006. + * This program is distributed in the hope that it will be useful,
  28007. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28008. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28009. + * GNU General Public License for more details.
  28010. + *
  28011. + * You should have received a copy of the GNU General Public License
  28012. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28013. + */
  28014. +
  28015. +/*
  28016. + * sub-routines for VFS
  28017. + */
  28018. +
  28019. +#ifndef __AUFS_VFSUB_H__
  28020. +#define __AUFS_VFSUB_H__
  28021. +
  28022. +#ifdef __KERNEL__
  28023. +
  28024. +#include <linux/fs.h>
  28025. +#include <linux/mount.h>
  28026. +#include <linux/xattr.h>
  28027. +#include "debug.h"
  28028. +
  28029. +/* copied from linux/fs/internal.h */
  28030. +/* todo: BAD approach!! */
  28031. +extern void __mnt_drop_write(struct vfsmount *);
  28032. +extern spinlock_t inode_sb_list_lock;
  28033. +extern int open_check_o_direct(struct file *f);
  28034. +
  28035. +/* ---------------------------------------------------------------------- */
  28036. +
  28037. +/* lock subclass for lower inode */
  28038. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  28039. +/* reduce? gave up. */
  28040. +enum {
  28041. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  28042. + AuLsc_I_PARENT, /* lower inode, parent first */
  28043. + AuLsc_I_PARENT2, /* copyup dirs */
  28044. + AuLsc_I_PARENT3, /* copyup wh */
  28045. + AuLsc_I_CHILD,
  28046. + AuLsc_I_CHILD2,
  28047. + AuLsc_I_End
  28048. +};
  28049. +
  28050. +/* to debug easier, do not make them inlined functions */
  28051. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  28052. +#define IMustLock(i) MtxMustLock(&(i)->i_mutex)
  28053. +
  28054. +/* ---------------------------------------------------------------------- */
  28055. +
  28056. +static inline void vfsub_drop_nlink(struct inode *inode)
  28057. +{
  28058. + AuDebugOn(!inode->i_nlink);
  28059. + drop_nlink(inode);
  28060. +}
  28061. +
  28062. +static inline void vfsub_dead_dir(struct inode *inode)
  28063. +{
  28064. + AuDebugOn(!S_ISDIR(inode->i_mode));
  28065. + inode->i_flags |= S_DEAD;
  28066. + clear_nlink(inode);
  28067. +}
  28068. +
  28069. +static inline int vfsub_native_ro(struct inode *inode)
  28070. +{
  28071. + return (inode->i_sb->s_flags & MS_RDONLY)
  28072. + || IS_RDONLY(inode)
  28073. + /* || IS_APPEND(inode) */
  28074. + || IS_IMMUTABLE(inode);
  28075. +}
  28076. +
  28077. +/* ---------------------------------------------------------------------- */
  28078. +
  28079. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  28080. +struct file *vfsub_dentry_open(struct path *path, int flags);
  28081. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  28082. +struct vfsub_aopen_args {
  28083. + struct file *file;
  28084. + unsigned int open_flag;
  28085. + umode_t create_mode;
  28086. + int *opened;
  28087. +};
  28088. +struct au_branch;
  28089. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  28090. + struct vfsub_aopen_args *args, struct au_branch *br);
  28091. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  28092. +
  28093. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  28094. + int len);
  28095. +
  28096. +struct vfsub_lkup_one_args {
  28097. + struct dentry **errp;
  28098. + struct qstr *name;
  28099. + struct dentry *parent;
  28100. +};
  28101. +
  28102. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  28103. + struct dentry *parent)
  28104. +{
  28105. + return vfsub_lookup_one_len(name->name, parent, name->len);
  28106. +}
  28107. +
  28108. +void vfsub_call_lkup_one(void *args);
  28109. +
  28110. +/* ---------------------------------------------------------------------- */
  28111. +
  28112. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  28113. +{
  28114. + int err;
  28115. +
  28116. + lockdep_off();
  28117. + err = mnt_want_write(mnt);
  28118. + lockdep_on();
  28119. + return err;
  28120. +}
  28121. +
  28122. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  28123. +{
  28124. + lockdep_off();
  28125. + mnt_drop_write(mnt);
  28126. + lockdep_on();
  28127. +}
  28128. +
  28129. +#if 0 /* reserved */
  28130. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  28131. +{
  28132. + lockdep_off();
  28133. + mnt_drop_write_file(file);
  28134. + lockdep_on();
  28135. +}
  28136. +#endif
  28137. +
  28138. +/* ---------------------------------------------------------------------- */
  28139. +
  28140. +struct au_hinode;
  28141. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  28142. + struct dentry *d2, struct au_hinode *hdir2);
  28143. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  28144. + struct dentry *d2, struct au_hinode *hdir2);
  28145. +
  28146. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  28147. + bool want_excl);
  28148. +int vfsub_symlink(struct inode *dir, struct path *path,
  28149. + const char *symname);
  28150. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  28151. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  28152. + struct path *path, struct inode **delegated_inode);
  28153. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  28154. + struct inode *hdir, struct path *path,
  28155. + struct inode **delegated_inode);
  28156. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  28157. +int vfsub_rmdir(struct inode *dir, struct path *path);
  28158. +
  28159. +/* ---------------------------------------------------------------------- */
  28160. +
  28161. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  28162. + loff_t *ppos);
  28163. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  28164. + loff_t *ppos);
  28165. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  28166. + loff_t *ppos);
  28167. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  28168. + loff_t *ppos);
  28169. +int vfsub_flush(struct file *file, fl_owner_t id);
  28170. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  28171. +
  28172. +static inline loff_t vfsub_f_size_read(struct file *file)
  28173. +{
  28174. + return i_size_read(file_inode(file));
  28175. +}
  28176. +
  28177. +static inline unsigned int vfsub_file_flags(struct file *file)
  28178. +{
  28179. + unsigned int flags;
  28180. +
  28181. + spin_lock(&file->f_lock);
  28182. + flags = file->f_flags;
  28183. + spin_unlock(&file->f_lock);
  28184. +
  28185. + return flags;
  28186. +}
  28187. +
  28188. +#if 0 /* reserved */
  28189. +static inline void vfsub_file_accessed(struct file *h_file)
  28190. +{
  28191. + file_accessed(h_file);
  28192. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  28193. +}
  28194. +#endif
  28195. +
  28196. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  28197. + struct dentry *h_dentry)
  28198. +{
  28199. + struct path h_path = {
  28200. + .dentry = h_dentry,
  28201. + .mnt = h_mnt
  28202. + };
  28203. + touch_atime(&h_path);
  28204. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  28205. +}
  28206. +
  28207. +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
  28208. + int flags)
  28209. +{
  28210. + return generic_update_time(h_inode, ts, flags);
  28211. + /* no vfsub_update_h_iattr() since we don't have struct path */
  28212. +}
  28213. +
  28214. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  28215. + struct pipe_inode_info *pipe, size_t len,
  28216. + unsigned int flags);
  28217. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  28218. + loff_t *ppos, size_t len, unsigned int flags);
  28219. +
  28220. +static inline long vfsub_truncate(struct path *path, loff_t length)
  28221. +{
  28222. + long err;
  28223. +
  28224. + lockdep_off();
  28225. + err = vfs_truncate(path, length);
  28226. + lockdep_on();
  28227. + return err;
  28228. +}
  28229. +
  28230. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  28231. + struct file *h_file);
  28232. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  28233. +
  28234. +/* ---------------------------------------------------------------------- */
  28235. +
  28236. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  28237. +{
  28238. + loff_t err;
  28239. +
  28240. + lockdep_off();
  28241. + err = vfs_llseek(file, offset, origin);
  28242. + lockdep_on();
  28243. + return err;
  28244. +}
  28245. +
  28246. +/* ---------------------------------------------------------------------- */
  28247. +
  28248. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  28249. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  28250. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  28251. + struct inode **delegated_inode);
  28252. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  28253. + struct inode **delegated_inode);
  28254. +int vfsub_unlink(struct inode *dir, struct path *path,
  28255. + struct inode **delegated_inode, int force);
  28256. +
  28257. +/* ---------------------------------------------------------------------- */
  28258. +
  28259. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  28260. + const void *value, size_t size, int flags)
  28261. +{
  28262. + int err;
  28263. +
  28264. + lockdep_off();
  28265. + err = vfs_setxattr(dentry, name, value, size, flags);
  28266. + lockdep_on();
  28267. +
  28268. + return err;
  28269. +}
  28270. +
  28271. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  28272. +{
  28273. + int err;
  28274. +
  28275. + lockdep_off();
  28276. + err = vfs_removexattr(dentry, name);
  28277. + lockdep_on();
  28278. +
  28279. + return err;
  28280. +}
  28281. +
  28282. +#endif /* __KERNEL__ */
  28283. +#endif /* __AUFS_VFSUB_H__ */
  28284. diff -Nur linux-4.0.5.orig/fs/aufs/wbr_policy.c linux-4.0.5/fs/aufs/wbr_policy.c
  28285. --- linux-4.0.5.orig/fs/aufs/wbr_policy.c 1969-12-31 18:00:00.000000000 -0600
  28286. +++ linux-4.0.5/fs/aufs/wbr_policy.c 2015-06-21 14:07:48.690432781 -0500
  28287. @@ -0,0 +1,765 @@
  28288. +/*
  28289. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  28290. + *
  28291. + * This program, aufs is free software; you can redistribute it and/or modify
  28292. + * it under the terms of the GNU General Public License as published by
  28293. + * the Free Software Foundation; either version 2 of the License, or
  28294. + * (at your option) any later version.
  28295. + *
  28296. + * This program is distributed in the hope that it will be useful,
  28297. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28298. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28299. + * GNU General Public License for more details.
  28300. + *
  28301. + * You should have received a copy of the GNU General Public License
  28302. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28303. + */
  28304. +
  28305. +/*
  28306. + * policies for selecting one among multiple writable branches
  28307. + */
  28308. +
  28309. +#include <linux/statfs.h>
  28310. +#include "aufs.h"
  28311. +
  28312. +/* subset of cpup_attr() */
  28313. +static noinline_for_stack
  28314. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  28315. +{
  28316. + int err, sbits;
  28317. + struct iattr ia;
  28318. + struct inode *h_isrc;
  28319. +
  28320. + h_isrc = h_src->d_inode;
  28321. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  28322. + ia.ia_mode = h_isrc->i_mode;
  28323. + ia.ia_uid = h_isrc->i_uid;
  28324. + ia.ia_gid = h_isrc->i_gid;
  28325. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  28326. + au_cpup_attr_flags(h_path->dentry->d_inode, h_isrc->i_flags);
  28327. + /* no delegation since it is just created */
  28328. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  28329. +
  28330. + /* is this nfs only? */
  28331. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  28332. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  28333. + ia.ia_mode = h_isrc->i_mode;
  28334. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  28335. + }
  28336. +
  28337. + return err;
  28338. +}
  28339. +
  28340. +#define AuCpdown_PARENT_OPQ 1
  28341. +#define AuCpdown_WHED (1 << 1)
  28342. +#define AuCpdown_MADE_DIR (1 << 2)
  28343. +#define AuCpdown_DIROPQ (1 << 3)
  28344. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  28345. +#define au_fset_cpdown(flags, name) \
  28346. + do { (flags) |= AuCpdown_##name; } while (0)
  28347. +#define au_fclr_cpdown(flags, name) \
  28348. + do { (flags) &= ~AuCpdown_##name; } while (0)
  28349. +
  28350. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  28351. + unsigned int *flags)
  28352. +{
  28353. + int err;
  28354. + struct dentry *opq_dentry;
  28355. +
  28356. + opq_dentry = au_diropq_create(dentry, bdst);
  28357. + err = PTR_ERR(opq_dentry);
  28358. + if (IS_ERR(opq_dentry))
  28359. + goto out;
  28360. + dput(opq_dentry);
  28361. + au_fset_cpdown(*flags, DIROPQ);
  28362. +
  28363. +out:
  28364. + return err;
  28365. +}
  28366. +
  28367. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  28368. + struct inode *dir, aufs_bindex_t bdst)
  28369. +{
  28370. + int err;
  28371. + struct path h_path;
  28372. + struct au_branch *br;
  28373. +
  28374. + br = au_sbr(dentry->d_sb, bdst);
  28375. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  28376. + err = PTR_ERR(h_path.dentry);
  28377. + if (IS_ERR(h_path.dentry))
  28378. + goto out;
  28379. +
  28380. + err = 0;
  28381. + if (h_path.dentry->d_inode) {
  28382. + h_path.mnt = au_br_mnt(br);
  28383. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  28384. + dentry);
  28385. + }
  28386. + dput(h_path.dentry);
  28387. +
  28388. +out:
  28389. + return err;
  28390. +}
  28391. +
  28392. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  28393. + struct au_pin *pin,
  28394. + struct dentry *h_parent, void *arg)
  28395. +{
  28396. + int err, rerr;
  28397. + aufs_bindex_t bopq, bstart;
  28398. + struct path h_path;
  28399. + struct dentry *parent;
  28400. + struct inode *h_dir, *h_inode, *inode, *dir;
  28401. + unsigned int *flags = arg;
  28402. +
  28403. + bstart = au_dbstart(dentry);
  28404. + /* dentry is di-locked */
  28405. + parent = dget_parent(dentry);
  28406. + dir = parent->d_inode;
  28407. + h_dir = h_parent->d_inode;
  28408. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  28409. + IMustLock(h_dir);
  28410. +
  28411. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  28412. + if (unlikely(err < 0))
  28413. + goto out;
  28414. + h_path.dentry = au_h_dptr(dentry, bdst);
  28415. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  28416. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
  28417. + S_IRWXU | S_IRUGO | S_IXUGO);
  28418. + if (unlikely(err))
  28419. + goto out_put;
  28420. + au_fset_cpdown(*flags, MADE_DIR);
  28421. +
  28422. + bopq = au_dbdiropq(dentry);
  28423. + au_fclr_cpdown(*flags, WHED);
  28424. + au_fclr_cpdown(*flags, DIROPQ);
  28425. + if (au_dbwh(dentry) == bdst)
  28426. + au_fset_cpdown(*flags, WHED);
  28427. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  28428. + au_fset_cpdown(*flags, PARENT_OPQ);
  28429. + h_inode = h_path.dentry->d_inode;
  28430. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  28431. + if (au_ftest_cpdown(*flags, WHED)) {
  28432. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  28433. + if (unlikely(err)) {
  28434. + mutex_unlock(&h_inode->i_mutex);
  28435. + goto out_dir;
  28436. + }
  28437. + }
  28438. +
  28439. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
  28440. + mutex_unlock(&h_inode->i_mutex);
  28441. + if (unlikely(err))
  28442. + goto out_opq;
  28443. +
  28444. + if (au_ftest_cpdown(*flags, WHED)) {
  28445. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  28446. + if (unlikely(err))
  28447. + goto out_opq;
  28448. + }
  28449. +
  28450. + inode = dentry->d_inode;
  28451. + if (au_ibend(inode) < bdst)
  28452. + au_set_ibend(inode, bdst);
  28453. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  28454. + au_hi_flags(inode, /*isdir*/1));
  28455. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  28456. + goto out; /* success */
  28457. +
  28458. + /* revert */
  28459. +out_opq:
  28460. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  28461. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  28462. + rerr = au_diropq_remove(dentry, bdst);
  28463. + mutex_unlock(&h_inode->i_mutex);
  28464. + if (unlikely(rerr)) {
  28465. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  28466. + dentry, bdst, rerr);
  28467. + err = -EIO;
  28468. + goto out;
  28469. + }
  28470. + }
  28471. +out_dir:
  28472. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  28473. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  28474. + if (unlikely(rerr)) {
  28475. + AuIOErr("failed removing %pd b%d (%d)\n",
  28476. + dentry, bdst, rerr);
  28477. + err = -EIO;
  28478. + }
  28479. + }
  28480. +out_put:
  28481. + au_set_h_dptr(dentry, bdst, NULL);
  28482. + if (au_dbend(dentry) == bdst)
  28483. + au_update_dbend(dentry);
  28484. +out:
  28485. + dput(parent);
  28486. + return err;
  28487. +}
  28488. +
  28489. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  28490. +{
  28491. + int err;
  28492. + unsigned int flags;
  28493. +
  28494. + flags = 0;
  28495. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  28496. +
  28497. + return err;
  28498. +}
  28499. +
  28500. +/* ---------------------------------------------------------------------- */
  28501. +
  28502. +/* policies for create */
  28503. +
  28504. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  28505. +{
  28506. + int err, i, j, ndentry;
  28507. + aufs_bindex_t bopq;
  28508. + struct au_dcsub_pages dpages;
  28509. + struct au_dpage *dpage;
  28510. + struct dentry **dentries, *parent, *d;
  28511. +
  28512. + err = au_dpages_init(&dpages, GFP_NOFS);
  28513. + if (unlikely(err))
  28514. + goto out;
  28515. + parent = dget_parent(dentry);
  28516. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  28517. + if (unlikely(err))
  28518. + goto out_free;
  28519. +
  28520. + err = bindex;
  28521. + for (i = 0; i < dpages.ndpage; i++) {
  28522. + dpage = dpages.dpages + i;
  28523. + dentries = dpage->dentries;
  28524. + ndentry = dpage->ndentry;
  28525. + for (j = 0; j < ndentry; j++) {
  28526. + d = dentries[j];
  28527. + di_read_lock_parent2(d, !AuLock_IR);
  28528. + bopq = au_dbdiropq(d);
  28529. + di_read_unlock(d, !AuLock_IR);
  28530. + if (bopq >= 0 && bopq < err)
  28531. + err = bopq;
  28532. + }
  28533. + }
  28534. +
  28535. +out_free:
  28536. + dput(parent);
  28537. + au_dpages_free(&dpages);
  28538. +out:
  28539. + return err;
  28540. +}
  28541. +
  28542. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  28543. +{
  28544. + for (; bindex >= 0; bindex--)
  28545. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  28546. + return bindex;
  28547. + return -EROFS;
  28548. +}
  28549. +
  28550. +/* top down parent */
  28551. +static int au_wbr_create_tdp(struct dentry *dentry,
  28552. + unsigned int flags __maybe_unused)
  28553. +{
  28554. + int err;
  28555. + aufs_bindex_t bstart, bindex;
  28556. + struct super_block *sb;
  28557. + struct dentry *parent, *h_parent;
  28558. +
  28559. + sb = dentry->d_sb;
  28560. + bstart = au_dbstart(dentry);
  28561. + err = bstart;
  28562. + if (!au_br_rdonly(au_sbr(sb, bstart)))
  28563. + goto out;
  28564. +
  28565. + err = -EROFS;
  28566. + parent = dget_parent(dentry);
  28567. + for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
  28568. + h_parent = au_h_dptr(parent, bindex);
  28569. + if (!h_parent || !h_parent->d_inode)
  28570. + continue;
  28571. +
  28572. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  28573. + err = bindex;
  28574. + break;
  28575. + }
  28576. + }
  28577. + dput(parent);
  28578. +
  28579. + /* bottom up here */
  28580. + if (unlikely(err < 0)) {
  28581. + err = au_wbr_bu(sb, bstart - 1);
  28582. + if (err >= 0)
  28583. + err = au_wbr_nonopq(dentry, err);
  28584. + }
  28585. +
  28586. +out:
  28587. + AuDbg("b%d\n", err);
  28588. + return err;
  28589. +}
  28590. +
  28591. +/* ---------------------------------------------------------------------- */
  28592. +
  28593. +/* an exception for the policy other than tdp */
  28594. +static int au_wbr_create_exp(struct dentry *dentry)
  28595. +{
  28596. + int err;
  28597. + aufs_bindex_t bwh, bdiropq;
  28598. + struct dentry *parent;
  28599. +
  28600. + err = -1;
  28601. + bwh = au_dbwh(dentry);
  28602. + parent = dget_parent(dentry);
  28603. + bdiropq = au_dbdiropq(parent);
  28604. + if (bwh >= 0) {
  28605. + if (bdiropq >= 0)
  28606. + err = min(bdiropq, bwh);
  28607. + else
  28608. + err = bwh;
  28609. + AuDbg("%d\n", err);
  28610. + } else if (bdiropq >= 0) {
  28611. + err = bdiropq;
  28612. + AuDbg("%d\n", err);
  28613. + }
  28614. + dput(parent);
  28615. +
  28616. + if (err >= 0)
  28617. + err = au_wbr_nonopq(dentry, err);
  28618. +
  28619. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  28620. + err = -1;
  28621. +
  28622. + AuDbg("%d\n", err);
  28623. + return err;
  28624. +}
  28625. +
  28626. +/* ---------------------------------------------------------------------- */
  28627. +
  28628. +/* round robin */
  28629. +static int au_wbr_create_init_rr(struct super_block *sb)
  28630. +{
  28631. + int err;
  28632. +
  28633. + err = au_wbr_bu(sb, au_sbend(sb));
  28634. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  28635. + /* smp_mb(); */
  28636. +
  28637. + AuDbg("b%d\n", err);
  28638. + return err;
  28639. +}
  28640. +
  28641. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  28642. +{
  28643. + int err, nbr;
  28644. + unsigned int u;
  28645. + aufs_bindex_t bindex, bend;
  28646. + struct super_block *sb;
  28647. + atomic_t *next;
  28648. +
  28649. + err = au_wbr_create_exp(dentry);
  28650. + if (err >= 0)
  28651. + goto out;
  28652. +
  28653. + sb = dentry->d_sb;
  28654. + next = &au_sbi(sb)->si_wbr_rr_next;
  28655. + bend = au_sbend(sb);
  28656. + nbr = bend + 1;
  28657. + for (bindex = 0; bindex <= bend; bindex++) {
  28658. + if (!au_ftest_wbr(flags, DIR)) {
  28659. + err = atomic_dec_return(next) + 1;
  28660. + /* modulo for 0 is meaningless */
  28661. + if (unlikely(!err))
  28662. + err = atomic_dec_return(next) + 1;
  28663. + } else
  28664. + err = atomic_read(next);
  28665. + AuDbg("%d\n", err);
  28666. + u = err;
  28667. + err = u % nbr;
  28668. + AuDbg("%d\n", err);
  28669. + if (!au_br_rdonly(au_sbr(sb, err)))
  28670. + break;
  28671. + err = -EROFS;
  28672. + }
  28673. +
  28674. + if (err >= 0)
  28675. + err = au_wbr_nonopq(dentry, err);
  28676. +
  28677. +out:
  28678. + AuDbg("%d\n", err);
  28679. + return err;
  28680. +}
  28681. +
  28682. +/* ---------------------------------------------------------------------- */
  28683. +
  28684. +/* most free space */
  28685. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  28686. +{
  28687. + struct super_block *sb;
  28688. + struct au_branch *br;
  28689. + struct au_wbr_mfs *mfs;
  28690. + struct dentry *h_parent;
  28691. + aufs_bindex_t bindex, bend;
  28692. + int err;
  28693. + unsigned long long b, bavail;
  28694. + struct path h_path;
  28695. + /* reduce the stack usage */
  28696. + struct kstatfs *st;
  28697. +
  28698. + st = kmalloc(sizeof(*st), GFP_NOFS);
  28699. + if (unlikely(!st)) {
  28700. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  28701. + return;
  28702. + }
  28703. +
  28704. + bavail = 0;
  28705. + sb = dentry->d_sb;
  28706. + mfs = &au_sbi(sb)->si_wbr_mfs;
  28707. + MtxMustLock(&mfs->mfs_lock);
  28708. + mfs->mfs_bindex = -EROFS;
  28709. + mfs->mfsrr_bytes = 0;
  28710. + if (!parent) {
  28711. + bindex = 0;
  28712. + bend = au_sbend(sb);
  28713. + } else {
  28714. + bindex = au_dbstart(parent);
  28715. + bend = au_dbtaildir(parent);
  28716. + }
  28717. +
  28718. + for (; bindex <= bend; bindex++) {
  28719. + if (parent) {
  28720. + h_parent = au_h_dptr(parent, bindex);
  28721. + if (!h_parent || !h_parent->d_inode)
  28722. + continue;
  28723. + }
  28724. + br = au_sbr(sb, bindex);
  28725. + if (au_br_rdonly(br))
  28726. + continue;
  28727. +
  28728. + /* sb->s_root for NFS is unreliable */
  28729. + h_path.mnt = au_br_mnt(br);
  28730. + h_path.dentry = h_path.mnt->mnt_root;
  28731. + err = vfs_statfs(&h_path, st);
  28732. + if (unlikely(err)) {
  28733. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  28734. + continue;
  28735. + }
  28736. +
  28737. + /* when the available size is equal, select the lower one */
  28738. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  28739. + || sizeof(b) < sizeof(st->f_bsize));
  28740. + b = st->f_bavail * st->f_bsize;
  28741. + br->br_wbr->wbr_bytes = b;
  28742. + if (b >= bavail) {
  28743. + bavail = b;
  28744. + mfs->mfs_bindex = bindex;
  28745. + mfs->mfs_jiffy = jiffies;
  28746. + }
  28747. + }
  28748. +
  28749. + mfs->mfsrr_bytes = bavail;
  28750. + AuDbg("b%d\n", mfs->mfs_bindex);
  28751. + kfree(st);
  28752. +}
  28753. +
  28754. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  28755. +{
  28756. + int err;
  28757. + struct dentry *parent;
  28758. + struct super_block *sb;
  28759. + struct au_wbr_mfs *mfs;
  28760. +
  28761. + err = au_wbr_create_exp(dentry);
  28762. + if (err >= 0)
  28763. + goto out;
  28764. +
  28765. + sb = dentry->d_sb;
  28766. + parent = NULL;
  28767. + if (au_ftest_wbr(flags, PARENT))
  28768. + parent = dget_parent(dentry);
  28769. + mfs = &au_sbi(sb)->si_wbr_mfs;
  28770. + mutex_lock(&mfs->mfs_lock);
  28771. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  28772. + || mfs->mfs_bindex < 0
  28773. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  28774. + au_mfs(dentry, parent);
  28775. + mutex_unlock(&mfs->mfs_lock);
  28776. + err = mfs->mfs_bindex;
  28777. + dput(parent);
  28778. +
  28779. + if (err >= 0)
  28780. + err = au_wbr_nonopq(dentry, err);
  28781. +
  28782. +out:
  28783. + AuDbg("b%d\n", err);
  28784. + return err;
  28785. +}
  28786. +
  28787. +static int au_wbr_create_init_mfs(struct super_block *sb)
  28788. +{
  28789. + struct au_wbr_mfs *mfs;
  28790. +
  28791. + mfs = &au_sbi(sb)->si_wbr_mfs;
  28792. + mutex_init(&mfs->mfs_lock);
  28793. + mfs->mfs_jiffy = 0;
  28794. + mfs->mfs_bindex = -EROFS;
  28795. +
  28796. + return 0;
  28797. +}
  28798. +
  28799. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  28800. +{
  28801. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  28802. + return 0;
  28803. +}
  28804. +
  28805. +/* ---------------------------------------------------------------------- */
  28806. +
  28807. +/* most free space and then round robin */
  28808. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  28809. +{
  28810. + int err;
  28811. + struct au_wbr_mfs *mfs;
  28812. +
  28813. + err = au_wbr_create_mfs(dentry, flags);
  28814. + if (err >= 0) {
  28815. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  28816. + mutex_lock(&mfs->mfs_lock);
  28817. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  28818. + err = au_wbr_create_rr(dentry, flags);
  28819. + mutex_unlock(&mfs->mfs_lock);
  28820. + }
  28821. +
  28822. + AuDbg("b%d\n", err);
  28823. + return err;
  28824. +}
  28825. +
  28826. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  28827. +{
  28828. + int err;
  28829. +
  28830. + au_wbr_create_init_mfs(sb); /* ignore */
  28831. + err = au_wbr_create_init_rr(sb);
  28832. +
  28833. + return err;
  28834. +}
  28835. +
  28836. +/* ---------------------------------------------------------------------- */
  28837. +
  28838. +/* top down parent and most free space */
  28839. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  28840. +{
  28841. + int err, e2;
  28842. + unsigned long long b;
  28843. + aufs_bindex_t bindex, bstart, bend;
  28844. + struct super_block *sb;
  28845. + struct dentry *parent, *h_parent;
  28846. + struct au_branch *br;
  28847. +
  28848. + err = au_wbr_create_tdp(dentry, flags);
  28849. + if (unlikely(err < 0))
  28850. + goto out;
  28851. + parent = dget_parent(dentry);
  28852. + bstart = au_dbstart(parent);
  28853. + bend = au_dbtaildir(parent);
  28854. + if (bstart == bend)
  28855. + goto out_parent; /* success */
  28856. +
  28857. + e2 = au_wbr_create_mfs(dentry, flags);
  28858. + if (e2 < 0)
  28859. + goto out_parent; /* success */
  28860. +
  28861. + /* when the available size is equal, select upper one */
  28862. + sb = dentry->d_sb;
  28863. + br = au_sbr(sb, err);
  28864. + b = br->br_wbr->wbr_bytes;
  28865. + AuDbg("b%d, %llu\n", err, b);
  28866. +
  28867. + for (bindex = bstart; bindex <= bend; bindex++) {
  28868. + h_parent = au_h_dptr(parent, bindex);
  28869. + if (!h_parent || !h_parent->d_inode)
  28870. + continue;
  28871. +
  28872. + br = au_sbr(sb, bindex);
  28873. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  28874. + b = br->br_wbr->wbr_bytes;
  28875. + err = bindex;
  28876. + AuDbg("b%d, %llu\n", err, b);
  28877. + }
  28878. + }
  28879. +
  28880. + if (err >= 0)
  28881. + err = au_wbr_nonopq(dentry, err);
  28882. +
  28883. +out_parent:
  28884. + dput(parent);
  28885. +out:
  28886. + AuDbg("b%d\n", err);
  28887. + return err;
  28888. +}
  28889. +
  28890. +/* ---------------------------------------------------------------------- */
  28891. +
  28892. +/*
  28893. + * - top down parent
  28894. + * - most free space with parent
  28895. + * - most free space round-robin regardless parent
  28896. + */
  28897. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  28898. +{
  28899. + int err;
  28900. + unsigned long long watermark;
  28901. + struct super_block *sb;
  28902. + struct au_branch *br;
  28903. + struct au_wbr_mfs *mfs;
  28904. +
  28905. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  28906. + if (unlikely(err < 0))
  28907. + goto out;
  28908. +
  28909. + sb = dentry->d_sb;
  28910. + br = au_sbr(sb, err);
  28911. + mfs = &au_sbi(sb)->si_wbr_mfs;
  28912. + mutex_lock(&mfs->mfs_lock);
  28913. + watermark = mfs->mfsrr_watermark;
  28914. + mutex_unlock(&mfs->mfs_lock);
  28915. + if (br->br_wbr->wbr_bytes < watermark)
  28916. + /* regardless the parent dir */
  28917. + err = au_wbr_create_mfsrr(dentry, flags);
  28918. +
  28919. +out:
  28920. + AuDbg("b%d\n", err);
  28921. + return err;
  28922. +}
  28923. +
  28924. +/* ---------------------------------------------------------------------- */
  28925. +
  28926. +/* policies for copyup */
  28927. +
  28928. +/* top down parent */
  28929. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  28930. +{
  28931. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  28932. +}
  28933. +
  28934. +/* bottom up parent */
  28935. +static int au_wbr_copyup_bup(struct dentry *dentry)
  28936. +{
  28937. + int err;
  28938. + aufs_bindex_t bindex, bstart;
  28939. + struct dentry *parent, *h_parent;
  28940. + struct super_block *sb;
  28941. +
  28942. + err = -EROFS;
  28943. + sb = dentry->d_sb;
  28944. + parent = dget_parent(dentry);
  28945. + bstart = au_dbstart(parent);
  28946. + for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
  28947. + h_parent = au_h_dptr(parent, bindex);
  28948. + if (!h_parent || !h_parent->d_inode)
  28949. + continue;
  28950. +
  28951. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  28952. + err = bindex;
  28953. + break;
  28954. + }
  28955. + }
  28956. + dput(parent);
  28957. +
  28958. + /* bottom up here */
  28959. + if (unlikely(err < 0))
  28960. + err = au_wbr_bu(sb, bstart - 1);
  28961. +
  28962. + AuDbg("b%d\n", err);
  28963. + return err;
  28964. +}
  28965. +
  28966. +/* bottom up */
  28967. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
  28968. +{
  28969. + int err;
  28970. +
  28971. + err = au_wbr_bu(dentry->d_sb, bstart);
  28972. + AuDbg("b%d\n", err);
  28973. + if (err > bstart)
  28974. + err = au_wbr_nonopq(dentry, err);
  28975. +
  28976. + AuDbg("b%d\n", err);
  28977. + return err;
  28978. +}
  28979. +
  28980. +static int au_wbr_copyup_bu(struct dentry *dentry)
  28981. +{
  28982. + int err;
  28983. + aufs_bindex_t bstart;
  28984. +
  28985. + bstart = au_dbstart(dentry);
  28986. + err = au_wbr_do_copyup_bu(dentry, bstart);
  28987. + return err;
  28988. +}
  28989. +
  28990. +/* ---------------------------------------------------------------------- */
  28991. +
  28992. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  28993. + [AuWbrCopyup_TDP] = {
  28994. + .copyup = au_wbr_copyup_tdp
  28995. + },
  28996. + [AuWbrCopyup_BUP] = {
  28997. + .copyup = au_wbr_copyup_bup
  28998. + },
  28999. + [AuWbrCopyup_BU] = {
  29000. + .copyup = au_wbr_copyup_bu
  29001. + }
  29002. +};
  29003. +
  29004. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  29005. + [AuWbrCreate_TDP] = {
  29006. + .create = au_wbr_create_tdp
  29007. + },
  29008. + [AuWbrCreate_RR] = {
  29009. + .create = au_wbr_create_rr,
  29010. + .init = au_wbr_create_init_rr
  29011. + },
  29012. + [AuWbrCreate_MFS] = {
  29013. + .create = au_wbr_create_mfs,
  29014. + .init = au_wbr_create_init_mfs,
  29015. + .fin = au_wbr_create_fin_mfs
  29016. + },
  29017. + [AuWbrCreate_MFSV] = {
  29018. + .create = au_wbr_create_mfs,
  29019. + .init = au_wbr_create_init_mfs,
  29020. + .fin = au_wbr_create_fin_mfs
  29021. + },
  29022. + [AuWbrCreate_MFSRR] = {
  29023. + .create = au_wbr_create_mfsrr,
  29024. + .init = au_wbr_create_init_mfsrr,
  29025. + .fin = au_wbr_create_fin_mfs
  29026. + },
  29027. + [AuWbrCreate_MFSRRV] = {
  29028. + .create = au_wbr_create_mfsrr,
  29029. + .init = au_wbr_create_init_mfsrr,
  29030. + .fin = au_wbr_create_fin_mfs
  29031. + },
  29032. + [AuWbrCreate_PMFS] = {
  29033. + .create = au_wbr_create_pmfs,
  29034. + .init = au_wbr_create_init_mfs,
  29035. + .fin = au_wbr_create_fin_mfs
  29036. + },
  29037. + [AuWbrCreate_PMFSV] = {
  29038. + .create = au_wbr_create_pmfs,
  29039. + .init = au_wbr_create_init_mfs,
  29040. + .fin = au_wbr_create_fin_mfs
  29041. + },
  29042. + [AuWbrCreate_PMFSRR] = {
  29043. + .create = au_wbr_create_pmfsrr,
  29044. + .init = au_wbr_create_init_mfsrr,
  29045. + .fin = au_wbr_create_fin_mfs
  29046. + },
  29047. + [AuWbrCreate_PMFSRRV] = {
  29048. + .create = au_wbr_create_pmfsrr,
  29049. + .init = au_wbr_create_init_mfsrr,
  29050. + .fin = au_wbr_create_fin_mfs
  29051. + }
  29052. +};
  29053. diff -Nur linux-4.0.5.orig/fs/aufs/whout.c linux-4.0.5/fs/aufs/whout.c
  29054. --- linux-4.0.5.orig/fs/aufs/whout.c 1969-12-31 18:00:00.000000000 -0600
  29055. +++ linux-4.0.5/fs/aufs/whout.c 2015-06-21 14:07:48.690432781 -0500
  29056. @@ -0,0 +1,1064 @@
  29057. +/*
  29058. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  29059. + *
  29060. + * This program, aufs is free software; you can redistribute it and/or modify
  29061. + * it under the terms of the GNU General Public License as published by
  29062. + * the Free Software Foundation; either version 2 of the License, or
  29063. + * (at your option) any later version.
  29064. + *
  29065. + * This program is distributed in the hope that it will be useful,
  29066. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29067. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29068. + * GNU General Public License for more details.
  29069. + *
  29070. + * You should have received a copy of the GNU General Public License
  29071. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29072. + */
  29073. +
  29074. +/*
  29075. + * whiteout for logical deletion and opaque directory
  29076. + */
  29077. +
  29078. +#include "aufs.h"
  29079. +
  29080. +#define WH_MASK S_IRUGO
  29081. +
  29082. +/*
  29083. + * If a directory contains this file, then it is opaque. We start with the
  29084. + * .wh. flag so that it is blocked by lookup.
  29085. + */
  29086. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  29087. + sizeof(AUFS_WH_DIROPQ) - 1);
  29088. +
  29089. +/*
  29090. + * generate whiteout name, which is NOT terminated by NULL.
  29091. + * @name: original d_name.name
  29092. + * @len: original d_name.len
  29093. + * @wh: whiteout qstr
  29094. + * returns zero when succeeds, otherwise error.
  29095. + * succeeded value as wh->name should be freed by kfree().
  29096. + */
  29097. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  29098. +{
  29099. + char *p;
  29100. +
  29101. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  29102. + return -ENAMETOOLONG;
  29103. +
  29104. + wh->len = name->len + AUFS_WH_PFX_LEN;
  29105. + p = kmalloc(wh->len, GFP_NOFS);
  29106. + wh->name = p;
  29107. + if (p) {
  29108. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  29109. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  29110. + /* smp_mb(); */
  29111. + return 0;
  29112. + }
  29113. + return -ENOMEM;
  29114. +}
  29115. +
  29116. +/* ---------------------------------------------------------------------- */
  29117. +
  29118. +/*
  29119. + * test if the @wh_name exists under @h_parent.
  29120. + * @try_sio specifies the necessary of super-io.
  29121. + */
  29122. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  29123. +{
  29124. + int err;
  29125. + struct dentry *wh_dentry;
  29126. +
  29127. + if (!try_sio)
  29128. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  29129. + else
  29130. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  29131. + err = PTR_ERR(wh_dentry);
  29132. + if (IS_ERR(wh_dentry)) {
  29133. + if (err == -ENAMETOOLONG)
  29134. + err = 0;
  29135. + goto out;
  29136. + }
  29137. +
  29138. + err = 0;
  29139. + if (!wh_dentry->d_inode)
  29140. + goto out_wh; /* success */
  29141. +
  29142. + err = 1;
  29143. + if (d_is_reg(wh_dentry))
  29144. + goto out_wh; /* success */
  29145. +
  29146. + err = -EIO;
  29147. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  29148. + wh_dentry, wh_dentry->d_inode->i_mode);
  29149. +
  29150. +out_wh:
  29151. + dput(wh_dentry);
  29152. +out:
  29153. + return err;
  29154. +}
  29155. +
  29156. +/*
  29157. + * test if the @h_dentry sets opaque or not.
  29158. + */
  29159. +int au_diropq_test(struct dentry *h_dentry)
  29160. +{
  29161. + int err;
  29162. + struct inode *h_dir;
  29163. +
  29164. + h_dir = h_dentry->d_inode;
  29165. + err = au_wh_test(h_dentry, &diropq_name,
  29166. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  29167. + return err;
  29168. +}
  29169. +
  29170. +/*
  29171. + * returns a negative dentry whose name is unique and temporary.
  29172. + */
  29173. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  29174. + struct qstr *prefix)
  29175. +{
  29176. + struct dentry *dentry;
  29177. + int i;
  29178. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  29179. + *name, *p;
  29180. + /* strict atomic_t is unnecessary here */
  29181. + static unsigned short cnt;
  29182. + struct qstr qs;
  29183. +
  29184. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  29185. +
  29186. + name = defname;
  29187. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  29188. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  29189. + dentry = ERR_PTR(-ENAMETOOLONG);
  29190. + if (unlikely(qs.len > NAME_MAX))
  29191. + goto out;
  29192. + dentry = ERR_PTR(-ENOMEM);
  29193. + name = kmalloc(qs.len + 1, GFP_NOFS);
  29194. + if (unlikely(!name))
  29195. + goto out;
  29196. + }
  29197. +
  29198. + /* doubly whiteout-ed */
  29199. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  29200. + p = name + AUFS_WH_PFX_LEN * 2;
  29201. + memcpy(p, prefix->name, prefix->len);
  29202. + p += prefix->len;
  29203. + *p++ = '.';
  29204. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  29205. +
  29206. + qs.name = name;
  29207. + for (i = 0; i < 3; i++) {
  29208. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  29209. + dentry = au_sio_lkup_one(&qs, h_parent);
  29210. + if (IS_ERR(dentry) || !dentry->d_inode)
  29211. + goto out_name;
  29212. + dput(dentry);
  29213. + }
  29214. + /* pr_warn("could not get random name\n"); */
  29215. + dentry = ERR_PTR(-EEXIST);
  29216. + AuDbg("%.*s\n", AuLNPair(&qs));
  29217. + BUG();
  29218. +
  29219. +out_name:
  29220. + if (name != defname)
  29221. + kfree(name);
  29222. +out:
  29223. + AuTraceErrPtr(dentry);
  29224. + return dentry;
  29225. +}
  29226. +
  29227. +/*
  29228. + * rename the @h_dentry on @br to the whiteouted temporary name.
  29229. + */
  29230. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  29231. +{
  29232. + int err;
  29233. + struct path h_path = {
  29234. + .mnt = au_br_mnt(br)
  29235. + };
  29236. + struct inode *h_dir, *delegated;
  29237. + struct dentry *h_parent;
  29238. +
  29239. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  29240. + h_dir = h_parent->d_inode;
  29241. + IMustLock(h_dir);
  29242. +
  29243. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  29244. + err = PTR_ERR(h_path.dentry);
  29245. + if (IS_ERR(h_path.dentry))
  29246. + goto out;
  29247. +
  29248. + /* under the same dir, no need to lock_rename() */
  29249. + delegated = NULL;
  29250. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
  29251. + AuTraceErr(err);
  29252. + if (unlikely(err == -EWOULDBLOCK)) {
  29253. + pr_warn("cannot retry for NFSv4 delegation"
  29254. + " for an internal rename\n");
  29255. + iput(delegated);
  29256. + }
  29257. + dput(h_path.dentry);
  29258. +
  29259. +out:
  29260. + AuTraceErr(err);
  29261. + return err;
  29262. +}
  29263. +
  29264. +/* ---------------------------------------------------------------------- */
  29265. +/*
  29266. + * functions for removing a whiteout
  29267. + */
  29268. +
  29269. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  29270. +{
  29271. + int err, force;
  29272. + struct inode *delegated;
  29273. +
  29274. + /*
  29275. + * forces superio when the dir has a sticky bit.
  29276. + * this may be a violation of unix fs semantics.
  29277. + */
  29278. + force = (h_dir->i_mode & S_ISVTX)
  29279. + && !uid_eq(current_fsuid(), h_path->dentry->d_inode->i_uid);
  29280. + delegated = NULL;
  29281. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  29282. + if (unlikely(err == -EWOULDBLOCK)) {
  29283. + pr_warn("cannot retry for NFSv4 delegation"
  29284. + " for an internal unlink\n");
  29285. + iput(delegated);
  29286. + }
  29287. + return err;
  29288. +}
  29289. +
  29290. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  29291. + struct dentry *dentry)
  29292. +{
  29293. + int err;
  29294. +
  29295. + err = do_unlink_wh(h_dir, h_path);
  29296. + if (!err && dentry)
  29297. + au_set_dbwh(dentry, -1);
  29298. +
  29299. + return err;
  29300. +}
  29301. +
  29302. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  29303. + struct au_branch *br)
  29304. +{
  29305. + int err;
  29306. + struct path h_path = {
  29307. + .mnt = au_br_mnt(br)
  29308. + };
  29309. +
  29310. + err = 0;
  29311. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  29312. + if (IS_ERR(h_path.dentry))
  29313. + err = PTR_ERR(h_path.dentry);
  29314. + else {
  29315. + if (h_path.dentry->d_inode
  29316. + && d_is_reg(h_path.dentry))
  29317. + err = do_unlink_wh(h_parent->d_inode, &h_path);
  29318. + dput(h_path.dentry);
  29319. + }
  29320. +
  29321. + return err;
  29322. +}
  29323. +
  29324. +/* ---------------------------------------------------------------------- */
  29325. +/*
  29326. + * initialize/clean whiteout for a branch
  29327. + */
  29328. +
  29329. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  29330. + const int isdir)
  29331. +{
  29332. + int err;
  29333. + struct inode *delegated;
  29334. +
  29335. + if (!whpath->dentry->d_inode)
  29336. + return;
  29337. +
  29338. + if (isdir)
  29339. + err = vfsub_rmdir(h_dir, whpath);
  29340. + else {
  29341. + delegated = NULL;
  29342. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  29343. + if (unlikely(err == -EWOULDBLOCK)) {
  29344. + pr_warn("cannot retry for NFSv4 delegation"
  29345. + " for an internal unlink\n");
  29346. + iput(delegated);
  29347. + }
  29348. + }
  29349. + if (unlikely(err))
  29350. + pr_warn("failed removing %pd (%d), ignored.\n",
  29351. + whpath->dentry, err);
  29352. +}
  29353. +
  29354. +static int test_linkable(struct dentry *h_root)
  29355. +{
  29356. + struct inode *h_dir = h_root->d_inode;
  29357. +
  29358. + if (h_dir->i_op->link)
  29359. + return 0;
  29360. +
  29361. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  29362. + h_root, au_sbtype(h_root->d_sb));
  29363. + return -ENOSYS;
  29364. +}
  29365. +
  29366. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  29367. +static int au_whdir(struct inode *h_dir, struct path *path)
  29368. +{
  29369. + int err;
  29370. +
  29371. + err = -EEXIST;
  29372. + if (!path->dentry->d_inode) {
  29373. + int mode = S_IRWXU;
  29374. +
  29375. + if (au_test_nfs(path->dentry->d_sb))
  29376. + mode |= S_IXUGO;
  29377. + err = vfsub_mkdir(h_dir, path, mode);
  29378. + } else if (d_is_dir(path->dentry))
  29379. + err = 0;
  29380. + else
  29381. + pr_err("unknown %pd exists\n", path->dentry);
  29382. +
  29383. + return err;
  29384. +}
  29385. +
  29386. +struct au_wh_base {
  29387. + const struct qstr *name;
  29388. + struct dentry *dentry;
  29389. +};
  29390. +
  29391. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  29392. + struct path *h_path)
  29393. +{
  29394. + h_path->dentry = base[AuBrWh_BASE].dentry;
  29395. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  29396. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  29397. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  29398. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  29399. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  29400. +}
  29401. +
  29402. +/*
  29403. + * returns tri-state,
  29404. + * minus: error, caller should print the message
  29405. + * zero: succuess
  29406. + * plus: error, caller should NOT print the message
  29407. + */
  29408. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  29409. + int do_plink, struct au_wh_base base[],
  29410. + struct path *h_path)
  29411. +{
  29412. + int err;
  29413. + struct inode *h_dir;
  29414. +
  29415. + h_dir = h_root->d_inode;
  29416. + h_path->dentry = base[AuBrWh_BASE].dentry;
  29417. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  29418. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  29419. + if (do_plink) {
  29420. + err = test_linkable(h_root);
  29421. + if (unlikely(err)) {
  29422. + err = 1;
  29423. + goto out;
  29424. + }
  29425. +
  29426. + err = au_whdir(h_dir, h_path);
  29427. + if (unlikely(err))
  29428. + goto out;
  29429. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  29430. + } else
  29431. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  29432. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  29433. + err = au_whdir(h_dir, h_path);
  29434. + if (unlikely(err))
  29435. + goto out;
  29436. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  29437. +
  29438. +out:
  29439. + return err;
  29440. +}
  29441. +
  29442. +/*
  29443. + * for the moment, aufs supports the branch filesystem which does not support
  29444. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  29445. + * copyup failed. finally, such filesystem will not be used as the writable
  29446. + * branch.
  29447. + *
  29448. + * returns tri-state, see above.
  29449. + */
  29450. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  29451. + int do_plink, struct au_wh_base base[],
  29452. + struct path *h_path)
  29453. +{
  29454. + int err;
  29455. + struct inode *h_dir;
  29456. +
  29457. + WbrWhMustWriteLock(wbr);
  29458. +
  29459. + err = test_linkable(h_root);
  29460. + if (unlikely(err)) {
  29461. + err = 1;
  29462. + goto out;
  29463. + }
  29464. +
  29465. + /*
  29466. + * todo: should this create be done in /sbin/mount.aufs helper?
  29467. + */
  29468. + err = -EEXIST;
  29469. + h_dir = h_root->d_inode;
  29470. + if (!base[AuBrWh_BASE].dentry->d_inode) {
  29471. + h_path->dentry = base[AuBrWh_BASE].dentry;
  29472. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  29473. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  29474. + err = 0;
  29475. + else
  29476. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  29477. + if (unlikely(err))
  29478. + goto out;
  29479. +
  29480. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  29481. + if (do_plink) {
  29482. + err = au_whdir(h_dir, h_path);
  29483. + if (unlikely(err))
  29484. + goto out;
  29485. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  29486. + } else
  29487. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  29488. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  29489. +
  29490. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  29491. + err = au_whdir(h_dir, h_path);
  29492. + if (unlikely(err))
  29493. + goto out;
  29494. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  29495. +
  29496. +out:
  29497. + return err;
  29498. +}
  29499. +
  29500. +/*
  29501. + * initialize the whiteout base file/dir for @br.
  29502. + */
  29503. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  29504. +{
  29505. + int err, i;
  29506. + const unsigned char do_plink
  29507. + = !!au_opt_test(au_mntflags(sb), PLINK);
  29508. + struct inode *h_dir;
  29509. + struct path path = br->br_path;
  29510. + struct dentry *h_root = path.dentry;
  29511. + struct au_wbr *wbr = br->br_wbr;
  29512. + static const struct qstr base_name[] = {
  29513. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  29514. + sizeof(AUFS_BASE_NAME) - 1),
  29515. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  29516. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  29517. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  29518. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  29519. + };
  29520. + struct au_wh_base base[] = {
  29521. + [AuBrWh_BASE] = {
  29522. + .name = base_name + AuBrWh_BASE,
  29523. + .dentry = NULL
  29524. + },
  29525. + [AuBrWh_PLINK] = {
  29526. + .name = base_name + AuBrWh_PLINK,
  29527. + .dentry = NULL
  29528. + },
  29529. + [AuBrWh_ORPH] = {
  29530. + .name = base_name + AuBrWh_ORPH,
  29531. + .dentry = NULL
  29532. + }
  29533. + };
  29534. +
  29535. + if (wbr)
  29536. + WbrWhMustWriteLock(wbr);
  29537. +
  29538. + for (i = 0; i < AuBrWh_Last; i++) {
  29539. + /* doubly whiteouted */
  29540. + struct dentry *d;
  29541. +
  29542. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  29543. + err = PTR_ERR(d);
  29544. + if (IS_ERR(d))
  29545. + goto out;
  29546. +
  29547. + base[i].dentry = d;
  29548. + AuDebugOn(wbr
  29549. + && wbr->wbr_wh[i]
  29550. + && wbr->wbr_wh[i] != base[i].dentry);
  29551. + }
  29552. +
  29553. + if (wbr)
  29554. + for (i = 0; i < AuBrWh_Last; i++) {
  29555. + dput(wbr->wbr_wh[i]);
  29556. + wbr->wbr_wh[i] = NULL;
  29557. + }
  29558. +
  29559. + err = 0;
  29560. + if (!au_br_writable(br->br_perm)) {
  29561. + h_dir = h_root->d_inode;
  29562. + au_wh_init_ro(h_dir, base, &path);
  29563. + } else if (!au_br_wh_linkable(br->br_perm)) {
  29564. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  29565. + if (err > 0)
  29566. + goto out;
  29567. + else if (err)
  29568. + goto out_err;
  29569. + } else {
  29570. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  29571. + if (err > 0)
  29572. + goto out;
  29573. + else if (err)
  29574. + goto out_err;
  29575. + }
  29576. + goto out; /* success */
  29577. +
  29578. +out_err:
  29579. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  29580. + err, h_root, au_sbtype(h_root->d_sb));
  29581. +out:
  29582. + for (i = 0; i < AuBrWh_Last; i++)
  29583. + dput(base[i].dentry);
  29584. + return err;
  29585. +}
  29586. +
  29587. +/* ---------------------------------------------------------------------- */
  29588. +/*
  29589. + * whiteouts are all hard-linked usually.
  29590. + * when its link count reaches a ceiling, we create a new whiteout base
  29591. + * asynchronously.
  29592. + */
  29593. +
  29594. +struct reinit_br_wh {
  29595. + struct super_block *sb;
  29596. + struct au_branch *br;
  29597. +};
  29598. +
  29599. +static void reinit_br_wh(void *arg)
  29600. +{
  29601. + int err;
  29602. + aufs_bindex_t bindex;
  29603. + struct path h_path;
  29604. + struct reinit_br_wh *a = arg;
  29605. + struct au_wbr *wbr;
  29606. + struct inode *dir, *delegated;
  29607. + struct dentry *h_root;
  29608. + struct au_hinode *hdir;
  29609. +
  29610. + err = 0;
  29611. + wbr = a->br->br_wbr;
  29612. + /* big aufs lock */
  29613. + si_noflush_write_lock(a->sb);
  29614. + if (!au_br_writable(a->br->br_perm))
  29615. + goto out;
  29616. + bindex = au_br_index(a->sb, a->br->br_id);
  29617. + if (unlikely(bindex < 0))
  29618. + goto out;
  29619. +
  29620. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  29621. + dir = a->sb->s_root->d_inode;
  29622. + hdir = au_hi(dir, bindex);
  29623. + h_root = au_h_dptr(a->sb->s_root, bindex);
  29624. + AuDebugOn(h_root != au_br_dentry(a->br));
  29625. +
  29626. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  29627. + wbr_wh_write_lock(wbr);
  29628. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  29629. + h_root, a->br);
  29630. + if (!err) {
  29631. + h_path.dentry = wbr->wbr_whbase;
  29632. + h_path.mnt = au_br_mnt(a->br);
  29633. + delegated = NULL;
  29634. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  29635. + /*force*/0);
  29636. + if (unlikely(err == -EWOULDBLOCK)) {
  29637. + pr_warn("cannot retry for NFSv4 delegation"
  29638. + " for an internal unlink\n");
  29639. + iput(delegated);
  29640. + }
  29641. + } else {
  29642. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  29643. + err = 0;
  29644. + }
  29645. + dput(wbr->wbr_whbase);
  29646. + wbr->wbr_whbase = NULL;
  29647. + if (!err)
  29648. + err = au_wh_init(a->br, a->sb);
  29649. + wbr_wh_write_unlock(wbr);
  29650. + au_hn_imtx_unlock(hdir);
  29651. + di_read_unlock(a->sb->s_root, AuLock_IR);
  29652. + if (!err)
  29653. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  29654. +
  29655. +out:
  29656. + if (wbr)
  29657. + atomic_dec(&wbr->wbr_wh_running);
  29658. + atomic_dec(&a->br->br_count);
  29659. + si_write_unlock(a->sb);
  29660. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  29661. + kfree(arg);
  29662. + if (unlikely(err))
  29663. + AuIOErr("err %d\n", err);
  29664. +}
  29665. +
  29666. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  29667. +{
  29668. + int do_dec, wkq_err;
  29669. + struct reinit_br_wh *arg;
  29670. +
  29671. + do_dec = 1;
  29672. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  29673. + goto out;
  29674. +
  29675. + /* ignore ENOMEM */
  29676. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  29677. + if (arg) {
  29678. + /*
  29679. + * dec(wh_running), kfree(arg) and dec(br_count)
  29680. + * in reinit function
  29681. + */
  29682. + arg->sb = sb;
  29683. + arg->br = br;
  29684. + atomic_inc(&br->br_count);
  29685. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  29686. + if (unlikely(wkq_err)) {
  29687. + atomic_dec(&br->br_wbr->wbr_wh_running);
  29688. + atomic_dec(&br->br_count);
  29689. + kfree(arg);
  29690. + }
  29691. + do_dec = 0;
  29692. + }
  29693. +
  29694. +out:
  29695. + if (do_dec)
  29696. + atomic_dec(&br->br_wbr->wbr_wh_running);
  29697. +}
  29698. +
  29699. +/* ---------------------------------------------------------------------- */
  29700. +
  29701. +/*
  29702. + * create the whiteout @wh.
  29703. + */
  29704. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  29705. + struct dentry *wh)
  29706. +{
  29707. + int err;
  29708. + struct path h_path = {
  29709. + .dentry = wh
  29710. + };
  29711. + struct au_branch *br;
  29712. + struct au_wbr *wbr;
  29713. + struct dentry *h_parent;
  29714. + struct inode *h_dir, *delegated;
  29715. +
  29716. + h_parent = wh->d_parent; /* dir inode is locked */
  29717. + h_dir = h_parent->d_inode;
  29718. + IMustLock(h_dir);
  29719. +
  29720. + br = au_sbr(sb, bindex);
  29721. + h_path.mnt = au_br_mnt(br);
  29722. + wbr = br->br_wbr;
  29723. + wbr_wh_read_lock(wbr);
  29724. + if (wbr->wbr_whbase) {
  29725. + delegated = NULL;
  29726. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  29727. + if (unlikely(err == -EWOULDBLOCK)) {
  29728. + pr_warn("cannot retry for NFSv4 delegation"
  29729. + " for an internal link\n");
  29730. + iput(delegated);
  29731. + }
  29732. + if (!err || err != -EMLINK)
  29733. + goto out;
  29734. +
  29735. + /* link count full. re-initialize br_whbase. */
  29736. + kick_reinit_br_wh(sb, br);
  29737. + }
  29738. +
  29739. + /* return this error in this context */
  29740. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  29741. + if (!err)
  29742. + au_fhsm_wrote(sb, bindex, /*force*/0);
  29743. +
  29744. +out:
  29745. + wbr_wh_read_unlock(wbr);
  29746. + return err;
  29747. +}
  29748. +
  29749. +/* ---------------------------------------------------------------------- */
  29750. +
  29751. +/*
  29752. + * create or remove the diropq.
  29753. + */
  29754. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  29755. + unsigned int flags)
  29756. +{
  29757. + struct dentry *opq_dentry, *h_dentry;
  29758. + struct super_block *sb;
  29759. + struct au_branch *br;
  29760. + int err;
  29761. +
  29762. + sb = dentry->d_sb;
  29763. + br = au_sbr(sb, bindex);
  29764. + h_dentry = au_h_dptr(dentry, bindex);
  29765. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  29766. + if (IS_ERR(opq_dentry))
  29767. + goto out;
  29768. +
  29769. + if (au_ftest_diropq(flags, CREATE)) {
  29770. + err = link_or_create_wh(sb, bindex, opq_dentry);
  29771. + if (!err) {
  29772. + au_set_dbdiropq(dentry, bindex);
  29773. + goto out; /* success */
  29774. + }
  29775. + } else {
  29776. + struct path tmp = {
  29777. + .dentry = opq_dentry,
  29778. + .mnt = au_br_mnt(br)
  29779. + };
  29780. + err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
  29781. + if (!err)
  29782. + au_set_dbdiropq(dentry, -1);
  29783. + }
  29784. + dput(opq_dentry);
  29785. + opq_dentry = ERR_PTR(err);
  29786. +
  29787. +out:
  29788. + return opq_dentry;
  29789. +}
  29790. +
  29791. +struct do_diropq_args {
  29792. + struct dentry **errp;
  29793. + struct dentry *dentry;
  29794. + aufs_bindex_t bindex;
  29795. + unsigned int flags;
  29796. +};
  29797. +
  29798. +static void call_do_diropq(void *args)
  29799. +{
  29800. + struct do_diropq_args *a = args;
  29801. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  29802. +}
  29803. +
  29804. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  29805. + unsigned int flags)
  29806. +{
  29807. + struct dentry *diropq, *h_dentry;
  29808. +
  29809. + h_dentry = au_h_dptr(dentry, bindex);
  29810. + if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
  29811. + diropq = do_diropq(dentry, bindex, flags);
  29812. + else {
  29813. + int wkq_err;
  29814. + struct do_diropq_args args = {
  29815. + .errp = &diropq,
  29816. + .dentry = dentry,
  29817. + .bindex = bindex,
  29818. + .flags = flags
  29819. + };
  29820. +
  29821. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  29822. + if (unlikely(wkq_err))
  29823. + diropq = ERR_PTR(wkq_err);
  29824. + }
  29825. +
  29826. + return diropq;
  29827. +}
  29828. +
  29829. +/* ---------------------------------------------------------------------- */
  29830. +
  29831. +/*
  29832. + * lookup whiteout dentry.
  29833. + * @h_parent: lower parent dentry which must exist and be locked
  29834. + * @base_name: name of dentry which will be whiteouted
  29835. + * returns dentry for whiteout.
  29836. + */
  29837. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  29838. + struct au_branch *br)
  29839. +{
  29840. + int err;
  29841. + struct qstr wh_name;
  29842. + struct dentry *wh_dentry;
  29843. +
  29844. + err = au_wh_name_alloc(&wh_name, base_name);
  29845. + wh_dentry = ERR_PTR(err);
  29846. + if (!err) {
  29847. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  29848. + kfree(wh_name.name);
  29849. + }
  29850. + return wh_dentry;
  29851. +}
  29852. +
  29853. +/*
  29854. + * link/create a whiteout for @dentry on @bindex.
  29855. + */
  29856. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  29857. + struct dentry *h_parent)
  29858. +{
  29859. + struct dentry *wh_dentry;
  29860. + struct super_block *sb;
  29861. + int err;
  29862. +
  29863. + sb = dentry->d_sb;
  29864. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  29865. + if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
  29866. + err = link_or_create_wh(sb, bindex, wh_dentry);
  29867. + if (!err) {
  29868. + au_set_dbwh(dentry, bindex);
  29869. + au_fhsm_wrote(sb, bindex, /*force*/0);
  29870. + } else {
  29871. + dput(wh_dentry);
  29872. + wh_dentry = ERR_PTR(err);
  29873. + }
  29874. + }
  29875. +
  29876. + return wh_dentry;
  29877. +}
  29878. +
  29879. +/* ---------------------------------------------------------------------- */
  29880. +
  29881. +/* Delete all whiteouts in this directory on branch bindex. */
  29882. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  29883. + aufs_bindex_t bindex, struct au_branch *br)
  29884. +{
  29885. + int err;
  29886. + unsigned long ul, n;
  29887. + struct qstr wh_name;
  29888. + char *p;
  29889. + struct hlist_head *head;
  29890. + struct au_vdir_wh *pos;
  29891. + struct au_vdir_destr *str;
  29892. +
  29893. + err = -ENOMEM;
  29894. + p = (void *)__get_free_page(GFP_NOFS);
  29895. + wh_name.name = p;
  29896. + if (unlikely(!wh_name.name))
  29897. + goto out;
  29898. +
  29899. + err = 0;
  29900. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  29901. + p += AUFS_WH_PFX_LEN;
  29902. + n = whlist->nh_num;
  29903. + head = whlist->nh_head;
  29904. + for (ul = 0; !err && ul < n; ul++, head++) {
  29905. + hlist_for_each_entry(pos, head, wh_hash) {
  29906. + if (pos->wh_bindex != bindex)
  29907. + continue;
  29908. +
  29909. + str = &pos->wh_str;
  29910. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  29911. + memcpy(p, str->name, str->len);
  29912. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  29913. + err = unlink_wh_name(h_dentry, &wh_name, br);
  29914. + if (!err)
  29915. + continue;
  29916. + break;
  29917. + }
  29918. + AuIOErr("whiteout name too long %.*s\n",
  29919. + str->len, str->name);
  29920. + err = -EIO;
  29921. + break;
  29922. + }
  29923. + }
  29924. + free_page((unsigned long)wh_name.name);
  29925. +
  29926. +out:
  29927. + return err;
  29928. +}
  29929. +
  29930. +struct del_wh_children_args {
  29931. + int *errp;
  29932. + struct dentry *h_dentry;
  29933. + struct au_nhash *whlist;
  29934. + aufs_bindex_t bindex;
  29935. + struct au_branch *br;
  29936. +};
  29937. +
  29938. +static void call_del_wh_children(void *args)
  29939. +{
  29940. + struct del_wh_children_args *a = args;
  29941. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  29942. +}
  29943. +
  29944. +/* ---------------------------------------------------------------------- */
  29945. +
  29946. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  29947. +{
  29948. + struct au_whtmp_rmdir *whtmp;
  29949. + int err;
  29950. + unsigned int rdhash;
  29951. +
  29952. + SiMustAnyLock(sb);
  29953. +
  29954. + whtmp = kmalloc(sizeof(*whtmp), gfp);
  29955. + if (unlikely(!whtmp)) {
  29956. + whtmp = ERR_PTR(-ENOMEM);
  29957. + goto out;
  29958. + }
  29959. +
  29960. + whtmp->dir = NULL;
  29961. + whtmp->br = NULL;
  29962. + whtmp->wh_dentry = NULL;
  29963. + /* no estimation for dir size */
  29964. + rdhash = au_sbi(sb)->si_rdhash;
  29965. + if (!rdhash)
  29966. + rdhash = AUFS_RDHASH_DEF;
  29967. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  29968. + if (unlikely(err)) {
  29969. + kfree(whtmp);
  29970. + whtmp = ERR_PTR(err);
  29971. + }
  29972. +
  29973. +out:
  29974. + return whtmp;
  29975. +}
  29976. +
  29977. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  29978. +{
  29979. + if (whtmp->br)
  29980. + atomic_dec(&whtmp->br->br_count);
  29981. + dput(whtmp->wh_dentry);
  29982. + iput(whtmp->dir);
  29983. + au_nhash_wh_free(&whtmp->whlist);
  29984. + kfree(whtmp);
  29985. +}
  29986. +
  29987. +/*
  29988. + * rmdir the whiteouted temporary named dir @h_dentry.
  29989. + * @whlist: whiteouted children.
  29990. + */
  29991. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  29992. + struct dentry *wh_dentry, struct au_nhash *whlist)
  29993. +{
  29994. + int err;
  29995. + unsigned int h_nlink;
  29996. + struct path h_tmp;
  29997. + struct inode *wh_inode, *h_dir;
  29998. + struct au_branch *br;
  29999. +
  30000. + h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
  30001. + IMustLock(h_dir);
  30002. +
  30003. + br = au_sbr(dir->i_sb, bindex);
  30004. + wh_inode = wh_dentry->d_inode;
  30005. + mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
  30006. +
  30007. + /*
  30008. + * someone else might change some whiteouts while we were sleeping.
  30009. + * it means this whlist may have an obsoleted entry.
  30010. + */
  30011. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  30012. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  30013. + else {
  30014. + int wkq_err;
  30015. + struct del_wh_children_args args = {
  30016. + .errp = &err,
  30017. + .h_dentry = wh_dentry,
  30018. + .whlist = whlist,
  30019. + .bindex = bindex,
  30020. + .br = br
  30021. + };
  30022. +
  30023. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  30024. + if (unlikely(wkq_err))
  30025. + err = wkq_err;
  30026. + }
  30027. + mutex_unlock(&wh_inode->i_mutex);
  30028. +
  30029. + if (!err) {
  30030. + h_tmp.dentry = wh_dentry;
  30031. + h_tmp.mnt = au_br_mnt(br);
  30032. + h_nlink = h_dir->i_nlink;
  30033. + err = vfsub_rmdir(h_dir, &h_tmp);
  30034. + /* some fs doesn't change the parent nlink in some cases */
  30035. + h_nlink -= h_dir->i_nlink;
  30036. + }
  30037. +
  30038. + if (!err) {
  30039. + if (au_ibstart(dir) == bindex) {
  30040. + /* todo: dir->i_mutex is necessary */
  30041. + au_cpup_attr_timesizes(dir);
  30042. + if (h_nlink)
  30043. + vfsub_drop_nlink(dir);
  30044. + }
  30045. + return 0; /* success */
  30046. + }
  30047. +
  30048. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  30049. + return err;
  30050. +}
  30051. +
  30052. +static void call_rmdir_whtmp(void *args)
  30053. +{
  30054. + int err;
  30055. + aufs_bindex_t bindex;
  30056. + struct au_whtmp_rmdir *a = args;
  30057. + struct super_block *sb;
  30058. + struct dentry *h_parent;
  30059. + struct inode *h_dir;
  30060. + struct au_hinode *hdir;
  30061. +
  30062. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  30063. + /* mutex_lock(&a->dir->i_mutex); */
  30064. + err = -EROFS;
  30065. + sb = a->dir->i_sb;
  30066. + si_read_lock(sb, !AuLock_FLUSH);
  30067. + if (!au_br_writable(a->br->br_perm))
  30068. + goto out;
  30069. + bindex = au_br_index(sb, a->br->br_id);
  30070. + if (unlikely(bindex < 0))
  30071. + goto out;
  30072. +
  30073. + err = -EIO;
  30074. + ii_write_lock_parent(a->dir);
  30075. + h_parent = dget_parent(a->wh_dentry);
  30076. + h_dir = h_parent->d_inode;
  30077. + hdir = au_hi(a->dir, bindex);
  30078. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  30079. + if (unlikely(err))
  30080. + goto out_mnt;
  30081. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  30082. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  30083. + a->br);
  30084. + if (!err)
  30085. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  30086. + au_hn_imtx_unlock(hdir);
  30087. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  30088. +
  30089. +out_mnt:
  30090. + dput(h_parent);
  30091. + ii_write_unlock(a->dir);
  30092. +out:
  30093. + /* mutex_unlock(&a->dir->i_mutex); */
  30094. + au_whtmp_rmdir_free(a);
  30095. + si_read_unlock(sb);
  30096. + au_nwt_done(&au_sbi(sb)->si_nowait);
  30097. + if (unlikely(err))
  30098. + AuIOErr("err %d\n", err);
  30099. +}
  30100. +
  30101. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30102. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  30103. +{
  30104. + int wkq_err;
  30105. + struct super_block *sb;
  30106. +
  30107. + IMustLock(dir);
  30108. +
  30109. + /* all post-process will be done in do_rmdir_whtmp(). */
  30110. + sb = dir->i_sb;
  30111. + args->dir = au_igrab(dir);
  30112. + args->br = au_sbr(sb, bindex);
  30113. + atomic_inc(&args->br->br_count);
  30114. + args->wh_dentry = dget(wh_dentry);
  30115. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  30116. + if (unlikely(wkq_err)) {
  30117. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  30118. + au_whtmp_rmdir_free(args);
  30119. + }
  30120. +}
  30121. diff -Nur linux-4.0.5.orig/fs/aufs/whout.h linux-4.0.5/fs/aufs/whout.h
  30122. --- linux-4.0.5.orig/fs/aufs/whout.h 1969-12-31 18:00:00.000000000 -0600
  30123. +++ linux-4.0.5/fs/aufs/whout.h 2015-06-21 14:07:48.690432781 -0500
  30124. @@ -0,0 +1,85 @@
  30125. +/*
  30126. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30127. + *
  30128. + * This program, aufs is free software; you can redistribute it and/or modify
  30129. + * it under the terms of the GNU General Public License as published by
  30130. + * the Free Software Foundation; either version 2 of the License, or
  30131. + * (at your option) any later version.
  30132. + *
  30133. + * This program is distributed in the hope that it will be useful,
  30134. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30135. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30136. + * GNU General Public License for more details.
  30137. + *
  30138. + * You should have received a copy of the GNU General Public License
  30139. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30140. + */
  30141. +
  30142. +/*
  30143. + * whiteout for logical deletion and opaque directory
  30144. + */
  30145. +
  30146. +#ifndef __AUFS_WHOUT_H__
  30147. +#define __AUFS_WHOUT_H__
  30148. +
  30149. +#ifdef __KERNEL__
  30150. +
  30151. +#include "dir.h"
  30152. +
  30153. +/* whout.c */
  30154. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  30155. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  30156. +int au_diropq_test(struct dentry *h_dentry);
  30157. +struct au_branch;
  30158. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  30159. + struct qstr *prefix);
  30160. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  30161. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  30162. + struct dentry *dentry);
  30163. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  30164. +
  30165. +/* diropq flags */
  30166. +#define AuDiropq_CREATE 1
  30167. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  30168. +#define au_fset_diropq(flags, name) \
  30169. + do { (flags) |= AuDiropq_##name; } while (0)
  30170. +#define au_fclr_diropq(flags, name) \
  30171. + do { (flags) &= ~AuDiropq_##name; } while (0)
  30172. +
  30173. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  30174. + unsigned int flags);
  30175. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  30176. + struct au_branch *br);
  30177. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  30178. + struct dentry *h_parent);
  30179. +
  30180. +/* real rmdir for the whiteout-ed dir */
  30181. +struct au_whtmp_rmdir {
  30182. + struct inode *dir;
  30183. + struct au_branch *br;
  30184. + struct dentry *wh_dentry;
  30185. + struct au_nhash whlist;
  30186. +};
  30187. +
  30188. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  30189. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  30190. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30191. + struct dentry *wh_dentry, struct au_nhash *whlist);
  30192. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  30193. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  30194. +
  30195. +/* ---------------------------------------------------------------------- */
  30196. +
  30197. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  30198. + aufs_bindex_t bindex)
  30199. +{
  30200. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  30201. +}
  30202. +
  30203. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  30204. +{
  30205. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  30206. +}
  30207. +
  30208. +#endif /* __KERNEL__ */
  30209. +#endif /* __AUFS_WHOUT_H__ */
  30210. diff -Nur linux-4.0.5.orig/fs/aufs/wkq.c linux-4.0.5/fs/aufs/wkq.c
  30211. --- linux-4.0.5.orig/fs/aufs/wkq.c 1969-12-31 18:00:00.000000000 -0600
  30212. +++ linux-4.0.5/fs/aufs/wkq.c 2015-06-21 14:07:48.690432781 -0500
  30213. @@ -0,0 +1,213 @@
  30214. +/*
  30215. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30216. + *
  30217. + * This program, aufs is free software; you can redistribute it and/or modify
  30218. + * it under the terms of the GNU General Public License as published by
  30219. + * the Free Software Foundation; either version 2 of the License, or
  30220. + * (at your option) any later version.
  30221. + *
  30222. + * This program is distributed in the hope that it will be useful,
  30223. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30224. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30225. + * GNU General Public License for more details.
  30226. + *
  30227. + * You should have received a copy of the GNU General Public License
  30228. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30229. + */
  30230. +
  30231. +/*
  30232. + * workqueue for asynchronous/super-io operations
  30233. + * todo: try new dredential scheme
  30234. + */
  30235. +
  30236. +#include <linux/module.h>
  30237. +#include "aufs.h"
  30238. +
  30239. +/* internal workqueue named AUFS_WKQ_NAME */
  30240. +
  30241. +static struct workqueue_struct *au_wkq;
  30242. +
  30243. +struct au_wkinfo {
  30244. + struct work_struct wk;
  30245. + struct kobject *kobj;
  30246. +
  30247. + unsigned int flags; /* see wkq.h */
  30248. +
  30249. + au_wkq_func_t func;
  30250. + void *args;
  30251. +
  30252. + struct completion *comp;
  30253. +};
  30254. +
  30255. +/* ---------------------------------------------------------------------- */
  30256. +
  30257. +static void wkq_func(struct work_struct *wk)
  30258. +{
  30259. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  30260. +
  30261. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  30262. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  30263. +
  30264. + wkinfo->func(wkinfo->args);
  30265. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  30266. + complete(wkinfo->comp);
  30267. + else {
  30268. + kobject_put(wkinfo->kobj);
  30269. + module_put(THIS_MODULE); /* todo: ?? */
  30270. + kfree(wkinfo);
  30271. + }
  30272. +}
  30273. +
  30274. +/*
  30275. + * Since struct completion is large, try allocating it dynamically.
  30276. + */
  30277. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  30278. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  30279. +
  30280. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  30281. +{
  30282. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  30283. + if (*comp) {
  30284. + init_completion(*comp);
  30285. + wkinfo->comp = *comp;
  30286. + return 0;
  30287. + }
  30288. + return -ENOMEM;
  30289. +}
  30290. +
  30291. +static void au_wkq_comp_free(struct completion *comp)
  30292. +{
  30293. + kfree(comp);
  30294. +}
  30295. +
  30296. +#else
  30297. +
  30298. +/* no braces */
  30299. +#define AuWkqCompDeclare(name) \
  30300. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  30301. + struct completion *comp = &_ ## name
  30302. +
  30303. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  30304. +{
  30305. + wkinfo->comp = *comp;
  30306. + return 0;
  30307. +}
  30308. +
  30309. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  30310. +{
  30311. + /* empty */
  30312. +}
  30313. +#endif /* 4KSTACKS */
  30314. +
  30315. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  30316. +{
  30317. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  30318. + if (au_wkq_test()) {
  30319. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  30320. + " due to a dead dir by UDBA?\n");
  30321. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  30322. + }
  30323. + } else
  30324. + au_dbg_verify_kthread();
  30325. +
  30326. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  30327. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  30328. + queue_work(au_wkq, &wkinfo->wk);
  30329. + } else {
  30330. + INIT_WORK(&wkinfo->wk, wkq_func);
  30331. + schedule_work(&wkinfo->wk);
  30332. + }
  30333. +}
  30334. +
  30335. +/*
  30336. + * Be careful. It is easy to make deadlock happen.
  30337. + * processA: lock, wkq and wait
  30338. + * processB: wkq and wait, lock in wkq
  30339. + * --> deadlock
  30340. + */
  30341. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  30342. +{
  30343. + int err;
  30344. + AuWkqCompDeclare(comp);
  30345. + struct au_wkinfo wkinfo = {
  30346. + .flags = flags,
  30347. + .func = func,
  30348. + .args = args
  30349. + };
  30350. +
  30351. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  30352. + if (!err) {
  30353. + au_wkq_run(&wkinfo);
  30354. + /* no timeout, no interrupt */
  30355. + wait_for_completion(wkinfo.comp);
  30356. + au_wkq_comp_free(comp);
  30357. + destroy_work_on_stack(&wkinfo.wk);
  30358. + }
  30359. +
  30360. + return err;
  30361. +
  30362. +}
  30363. +
  30364. +/*
  30365. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  30366. + * problem in a concurrent umounting.
  30367. + */
  30368. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  30369. + unsigned int flags)
  30370. +{
  30371. + int err;
  30372. + struct au_wkinfo *wkinfo;
  30373. +
  30374. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  30375. +
  30376. + /*
  30377. + * wkq_func() must free this wkinfo.
  30378. + * it highly depends upon the implementation of workqueue.
  30379. + */
  30380. + err = 0;
  30381. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  30382. + if (wkinfo) {
  30383. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  30384. + wkinfo->flags = flags & ~AuWkq_WAIT;
  30385. + wkinfo->func = func;
  30386. + wkinfo->args = args;
  30387. + wkinfo->comp = NULL;
  30388. + kobject_get(wkinfo->kobj);
  30389. + __module_get(THIS_MODULE); /* todo: ?? */
  30390. +
  30391. + au_wkq_run(wkinfo);
  30392. + } else {
  30393. + err = -ENOMEM;
  30394. + au_nwt_done(&au_sbi(sb)->si_nowait);
  30395. + }
  30396. +
  30397. + return err;
  30398. +}
  30399. +
  30400. +/* ---------------------------------------------------------------------- */
  30401. +
  30402. +void au_nwt_init(struct au_nowait_tasks *nwt)
  30403. +{
  30404. + atomic_set(&nwt->nw_len, 0);
  30405. + /* smp_mb(); */ /* atomic_set */
  30406. + init_waitqueue_head(&nwt->nw_wq);
  30407. +}
  30408. +
  30409. +void au_wkq_fin(void)
  30410. +{
  30411. + destroy_workqueue(au_wkq);
  30412. +}
  30413. +
  30414. +int __init au_wkq_init(void)
  30415. +{
  30416. + int err;
  30417. +
  30418. + err = 0;
  30419. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  30420. + if (IS_ERR(au_wkq))
  30421. + err = PTR_ERR(au_wkq);
  30422. + else if (!au_wkq)
  30423. + err = -ENOMEM;
  30424. +
  30425. + return err;
  30426. +}
  30427. diff -Nur linux-4.0.5.orig/fs/aufs/wkq.h linux-4.0.5/fs/aufs/wkq.h
  30428. --- linux-4.0.5.orig/fs/aufs/wkq.h 1969-12-31 18:00:00.000000000 -0600
  30429. +++ linux-4.0.5/fs/aufs/wkq.h 2015-06-21 14:07:48.690432781 -0500
  30430. @@ -0,0 +1,91 @@
  30431. +/*
  30432. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30433. + *
  30434. + * This program, aufs is free software; you can redistribute it and/or modify
  30435. + * it under the terms of the GNU General Public License as published by
  30436. + * the Free Software Foundation; either version 2 of the License, or
  30437. + * (at your option) any later version.
  30438. + *
  30439. + * This program is distributed in the hope that it will be useful,
  30440. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30441. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30442. + * GNU General Public License for more details.
  30443. + *
  30444. + * You should have received a copy of the GNU General Public License
  30445. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30446. + */
  30447. +
  30448. +/*
  30449. + * workqueue for asynchronous/super-io operations
  30450. + * todo: try new credentials management scheme
  30451. + */
  30452. +
  30453. +#ifndef __AUFS_WKQ_H__
  30454. +#define __AUFS_WKQ_H__
  30455. +
  30456. +#ifdef __KERNEL__
  30457. +
  30458. +struct super_block;
  30459. +
  30460. +/* ---------------------------------------------------------------------- */
  30461. +
  30462. +/*
  30463. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  30464. + */
  30465. +struct au_nowait_tasks {
  30466. + atomic_t nw_len;
  30467. + wait_queue_head_t nw_wq;
  30468. +};
  30469. +
  30470. +/* ---------------------------------------------------------------------- */
  30471. +
  30472. +typedef void (*au_wkq_func_t)(void *args);
  30473. +
  30474. +/* wkq flags */
  30475. +#define AuWkq_WAIT 1
  30476. +#define AuWkq_NEST (1 << 1)
  30477. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  30478. +#define au_fset_wkq(flags, name) \
  30479. + do { (flags) |= AuWkq_##name; } while (0)
  30480. +#define au_fclr_wkq(flags, name) \
  30481. + do { (flags) &= ~AuWkq_##name; } while (0)
  30482. +
  30483. +#ifndef CONFIG_AUFS_HNOTIFY
  30484. +#undef AuWkq_NEST
  30485. +#define AuWkq_NEST 0
  30486. +#endif
  30487. +
  30488. +/* wkq.c */
  30489. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  30490. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  30491. + unsigned int flags);
  30492. +void au_nwt_init(struct au_nowait_tasks *nwt);
  30493. +int __init au_wkq_init(void);
  30494. +void au_wkq_fin(void);
  30495. +
  30496. +/* ---------------------------------------------------------------------- */
  30497. +
  30498. +static inline int au_wkq_test(void)
  30499. +{
  30500. + return current->flags & PF_WQ_WORKER;
  30501. +}
  30502. +
  30503. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  30504. +{
  30505. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  30506. +}
  30507. +
  30508. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  30509. +{
  30510. + if (atomic_dec_and_test(&nwt->nw_len))
  30511. + wake_up_all(&nwt->nw_wq);
  30512. +}
  30513. +
  30514. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  30515. +{
  30516. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  30517. + return 0;
  30518. +}
  30519. +
  30520. +#endif /* __KERNEL__ */
  30521. +#endif /* __AUFS_WKQ_H__ */
  30522. diff -Nur linux-4.0.5.orig/fs/aufs/xattr.c linux-4.0.5/fs/aufs/xattr.c
  30523. --- linux-4.0.5.orig/fs/aufs/xattr.c 1969-12-31 18:00:00.000000000 -0600
  30524. +++ linux-4.0.5/fs/aufs/xattr.c 2015-06-21 14:07:48.690432781 -0500
  30525. @@ -0,0 +1,344 @@
  30526. +/*
  30527. + * Copyright (C) 2014-2015 Junjiro R. Okajima
  30528. + *
  30529. + * This program, aufs is free software; you can redistribute it and/or modify
  30530. + * it under the terms of the GNU General Public License as published by
  30531. + * the Free Software Foundation; either version 2 of the License, or
  30532. + * (at your option) any later version.
  30533. + *
  30534. + * This program is distributed in the hope that it will be useful,
  30535. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30536. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30537. + * GNU General Public License for more details.
  30538. + *
  30539. + * You should have received a copy of the GNU General Public License
  30540. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30541. + */
  30542. +
  30543. +/*
  30544. + * handling xattr functions
  30545. + */
  30546. +
  30547. +#include <linux/xattr.h>
  30548. +#include "aufs.h"
  30549. +
  30550. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  30551. +{
  30552. + if (!ignore_flags)
  30553. + goto out;
  30554. + switch (err) {
  30555. + case -ENOMEM:
  30556. + case -EDQUOT:
  30557. + goto out;
  30558. + }
  30559. +
  30560. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  30561. + err = 0;
  30562. + goto out;
  30563. + }
  30564. +
  30565. +#define cmp(brattr, prefix) do { \
  30566. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  30567. + XATTR_##prefix##_PREFIX_LEN)) { \
  30568. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  30569. + err = 0; \
  30570. + goto out; \
  30571. + } \
  30572. + } while (0)
  30573. +
  30574. + cmp(SEC, SECURITY);
  30575. + cmp(SYS, SYSTEM);
  30576. + cmp(TR, TRUSTED);
  30577. + cmp(USR, USER);
  30578. +#undef cmp
  30579. +
  30580. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  30581. + err = 0;
  30582. +
  30583. +out:
  30584. + return err;
  30585. +}
  30586. +
  30587. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  30588. +
  30589. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  30590. + char *name, char **buf, unsigned int ignore_flags,
  30591. + unsigned int verbose)
  30592. +{
  30593. + int err;
  30594. + ssize_t ssz;
  30595. + struct inode *h_idst;
  30596. +
  30597. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  30598. + err = ssz;
  30599. + if (unlikely(err <= 0)) {
  30600. + if (err == -ENODATA
  30601. + || (err == -EOPNOTSUPP
  30602. + && ((ignore_flags & au_xattr_out_of_list)
  30603. + || (au_test_nfs_noacl(h_src->d_inode)
  30604. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  30605. + || !strcmp(name,
  30606. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  30607. + ))
  30608. + err = 0;
  30609. + if (err && (verbose || au_debug_test()))
  30610. + pr_err("%s, err %d\n", name, err);
  30611. + goto out;
  30612. + }
  30613. +
  30614. + /* unlock it temporary */
  30615. + h_idst = h_dst->d_inode;
  30616. + mutex_unlock(&h_idst->i_mutex);
  30617. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  30618. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  30619. + if (unlikely(err)) {
  30620. + if (verbose || au_debug_test())
  30621. + pr_err("%s, err %d\n", name, err);
  30622. + err = au_xattr_ignore(err, name, ignore_flags);
  30623. + }
  30624. +
  30625. +out:
  30626. + return err;
  30627. +}
  30628. +
  30629. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  30630. + unsigned int verbose)
  30631. +{
  30632. + int err, unlocked, acl_access, acl_default;
  30633. + ssize_t ssz;
  30634. + struct inode *h_isrc, *h_idst;
  30635. + char *value, *p, *o, *e;
  30636. +
  30637. + /* try stopping to update the source inode while we are referencing */
  30638. + /* there should not be the parent-child relationship between them */
  30639. + h_isrc = h_src->d_inode;
  30640. + h_idst = h_dst->d_inode;
  30641. + mutex_unlock(&h_idst->i_mutex);
  30642. + mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
  30643. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  30644. + unlocked = 0;
  30645. +
  30646. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  30647. + ssz = vfs_listxattr(h_src, NULL, 0);
  30648. + err = ssz;
  30649. + if (unlikely(err < 0)) {
  30650. + AuTraceErr(err);
  30651. + if (err == -ENODATA
  30652. + || err == -EOPNOTSUPP)
  30653. + err = 0; /* ignore */
  30654. + goto out;
  30655. + }
  30656. +
  30657. + err = 0;
  30658. + p = NULL;
  30659. + o = NULL;
  30660. + if (ssz) {
  30661. + err = -ENOMEM;
  30662. + p = kmalloc(ssz, GFP_NOFS);
  30663. + o = p;
  30664. + if (unlikely(!p))
  30665. + goto out;
  30666. + err = vfs_listxattr(h_src, p, ssz);
  30667. + }
  30668. + mutex_unlock(&h_isrc->i_mutex);
  30669. + unlocked = 1;
  30670. + AuDbg("err %d, ssz %zd\n", err, ssz);
  30671. + if (unlikely(err < 0))
  30672. + goto out_free;
  30673. +
  30674. + err = 0;
  30675. + e = p + ssz;
  30676. + value = NULL;
  30677. + acl_access = 0;
  30678. + acl_default = 0;
  30679. + while (!err && p < e) {
  30680. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  30681. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  30682. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  30683. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  30684. + - 1);
  30685. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  30686. + verbose);
  30687. + p += strlen(p) + 1;
  30688. + }
  30689. + AuTraceErr(err);
  30690. + ignore_flags |= au_xattr_out_of_list;
  30691. + if (!err && !acl_access) {
  30692. + err = au_do_cpup_xattr(h_dst, h_src,
  30693. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  30694. + ignore_flags, verbose);
  30695. + AuTraceErr(err);
  30696. + }
  30697. + if (!err && !acl_default) {
  30698. + err = au_do_cpup_xattr(h_dst, h_src,
  30699. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  30700. + ignore_flags, verbose);
  30701. + AuTraceErr(err);
  30702. + }
  30703. +
  30704. + kfree(value);
  30705. +
  30706. +out_free:
  30707. + kfree(o);
  30708. +out:
  30709. + if (!unlocked)
  30710. + mutex_unlock(&h_isrc->i_mutex);
  30711. + AuTraceErr(err);
  30712. + return err;
  30713. +}
  30714. +
  30715. +/* ---------------------------------------------------------------------- */
  30716. +
  30717. +enum {
  30718. + AU_XATTR_LIST,
  30719. + AU_XATTR_GET
  30720. +};
  30721. +
  30722. +struct au_lgxattr {
  30723. + int type;
  30724. + union {
  30725. + struct {
  30726. + char *list;
  30727. + size_t size;
  30728. + } list;
  30729. + struct {
  30730. + const char *name;
  30731. + void *value;
  30732. + size_t size;
  30733. + } get;
  30734. + } u;
  30735. +};
  30736. +
  30737. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  30738. +{
  30739. + ssize_t err;
  30740. + struct path h_path;
  30741. + struct super_block *sb;
  30742. +
  30743. + sb = dentry->d_sb;
  30744. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  30745. + if (unlikely(err))
  30746. + goto out;
  30747. + err = au_h_path_getattr(dentry, /*force*/1, &h_path);
  30748. + if (unlikely(err))
  30749. + goto out_si;
  30750. + if (unlikely(!h_path.dentry))
  30751. + /* illegally overlapped or something */
  30752. + goto out_di; /* pretending success */
  30753. +
  30754. + /* always topmost entry only */
  30755. + switch (arg->type) {
  30756. + case AU_XATTR_LIST:
  30757. + err = vfs_listxattr(h_path.dentry,
  30758. + arg->u.list.list, arg->u.list.size);
  30759. + break;
  30760. + case AU_XATTR_GET:
  30761. + err = vfs_getxattr(h_path.dentry,
  30762. + arg->u.get.name, arg->u.get.value,
  30763. + arg->u.get.size);
  30764. + break;
  30765. + }
  30766. +
  30767. +out_di:
  30768. + di_read_unlock(dentry, AuLock_IR);
  30769. +out_si:
  30770. + si_read_unlock(sb);
  30771. +out:
  30772. + AuTraceErr(err);
  30773. + return err;
  30774. +}
  30775. +
  30776. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  30777. +{
  30778. + struct au_lgxattr arg = {
  30779. + .type = AU_XATTR_LIST,
  30780. + .u.list = {
  30781. + .list = list,
  30782. + .size = size
  30783. + },
  30784. + };
  30785. +
  30786. + return au_lgxattr(dentry, &arg);
  30787. +}
  30788. +
  30789. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  30790. + size_t size)
  30791. +{
  30792. + struct au_lgxattr arg = {
  30793. + .type = AU_XATTR_GET,
  30794. + .u.get = {
  30795. + .name = name,
  30796. + .value = value,
  30797. + .size = size
  30798. + },
  30799. + };
  30800. +
  30801. + return au_lgxattr(dentry, &arg);
  30802. +}
  30803. +
  30804. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  30805. + size_t size, int flags)
  30806. +{
  30807. + struct au_srxattr arg = {
  30808. + .type = AU_XATTR_SET,
  30809. + .u.set = {
  30810. + .name = name,
  30811. + .value = value,
  30812. + .size = size,
  30813. + .flags = flags
  30814. + },
  30815. + };
  30816. +
  30817. + return au_srxattr(dentry, &arg);
  30818. +}
  30819. +
  30820. +int aufs_removexattr(struct dentry *dentry, const char *name)
  30821. +{
  30822. + struct au_srxattr arg = {
  30823. + .type = AU_XATTR_REMOVE,
  30824. + .u.remove = {
  30825. + .name = name
  30826. + },
  30827. + };
  30828. +
  30829. + return au_srxattr(dentry, &arg);
  30830. +}
  30831. +
  30832. +/* ---------------------------------------------------------------------- */
  30833. +
  30834. +#if 0
  30835. +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  30836. + const char *name, size_t name_len, int type)
  30837. +{
  30838. + return aufs_listxattr(dentry, list, list_size);
  30839. +}
  30840. +
  30841. +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
  30842. + size_t size, int type)
  30843. +{
  30844. + return aufs_getxattr(dentry, name, buffer, size);
  30845. +}
  30846. +
  30847. +static int au_xattr_set(struct dentry *dentry, const char *name,
  30848. + const void *value, size_t size, int flags, int type)
  30849. +{
  30850. + return aufs_setxattr(dentry, name, value, size, flags);
  30851. +}
  30852. +
  30853. +static const struct xattr_handler au_xattr_handler = {
  30854. + /* no prefix, no flags */
  30855. + .list = au_xattr_list,
  30856. + .get = au_xattr_get,
  30857. + .set = au_xattr_set
  30858. + /* why no remove? */
  30859. +};
  30860. +
  30861. +static const struct xattr_handler *au_xattr_handlers[] = {
  30862. + &au_xattr_handler
  30863. +};
  30864. +
  30865. +void au_xattr_init(struct super_block *sb)
  30866. +{
  30867. + /* sb->s_xattr = au_xattr_handlers; */
  30868. +}
  30869. +#endif
  30870. diff -Nur linux-4.0.5.orig/fs/aufs/xino.c linux-4.0.5/fs/aufs/xino.c
  30871. --- linux-4.0.5.orig/fs/aufs/xino.c 1969-12-31 18:00:00.000000000 -0600
  30872. +++ linux-4.0.5/fs/aufs/xino.c 2015-06-21 14:07:48.690432781 -0500
  30873. @@ -0,0 +1,1322 @@
  30874. +/*
  30875. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30876. + *
  30877. + * This program, aufs is free software; you can redistribute it and/or modify
  30878. + * it under the terms of the GNU General Public License as published by
  30879. + * the Free Software Foundation; either version 2 of the License, or
  30880. + * (at your option) any later version.
  30881. + *
  30882. + * This program is distributed in the hope that it will be useful,
  30883. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30884. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30885. + * GNU General Public License for more details.
  30886. + *
  30887. + * You should have received a copy of the GNU General Public License
  30888. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30889. + */
  30890. +
  30891. +/*
  30892. + * external inode number translation table and bitmap
  30893. + */
  30894. +
  30895. +#include <linux/seq_file.h>
  30896. +#include <linux/statfs.h>
  30897. +#include "aufs.h"
  30898. +
  30899. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  30900. +ssize_t xino_fread(au_readf_t func, struct file *file, void *kbuf, size_t size,
  30901. + loff_t *pos)
  30902. +{
  30903. + ssize_t err;
  30904. + mm_segment_t oldfs;
  30905. + union {
  30906. + void *k;
  30907. + char __user *u;
  30908. + } buf;
  30909. +
  30910. + buf.k = kbuf;
  30911. + oldfs = get_fs();
  30912. + set_fs(KERNEL_DS);
  30913. + do {
  30914. + /* todo: signal_pending? */
  30915. + err = func(file, buf.u, size, pos);
  30916. + } while (err == -EAGAIN || err == -EINTR);
  30917. + set_fs(oldfs);
  30918. +
  30919. +#if 0 /* reserved for future use */
  30920. + if (err > 0)
  30921. + fsnotify_access(file->f_path.dentry);
  30922. +#endif
  30923. +
  30924. + return err;
  30925. +}
  30926. +
  30927. +/* ---------------------------------------------------------------------- */
  30928. +
  30929. +static ssize_t do_xino_fwrite(au_writef_t func, struct file *file, void *kbuf,
  30930. + size_t size, loff_t *pos)
  30931. +{
  30932. + ssize_t err;
  30933. + mm_segment_t oldfs;
  30934. + union {
  30935. + void *k;
  30936. + const char __user *u;
  30937. + } buf;
  30938. +
  30939. + buf.k = kbuf;
  30940. + oldfs = get_fs();
  30941. + set_fs(KERNEL_DS);
  30942. + do {
  30943. + /* todo: signal_pending? */
  30944. + err = func(file, buf.u, size, pos);
  30945. + } while (err == -EAGAIN || err == -EINTR);
  30946. + set_fs(oldfs);
  30947. +
  30948. +#if 0 /* reserved for future use */
  30949. + if (err > 0)
  30950. + fsnotify_modify(file->f_path.dentry);
  30951. +#endif
  30952. +
  30953. + return err;
  30954. +}
  30955. +
  30956. +struct do_xino_fwrite_args {
  30957. + ssize_t *errp;
  30958. + au_writef_t func;
  30959. + struct file *file;
  30960. + void *buf;
  30961. + size_t size;
  30962. + loff_t *pos;
  30963. +};
  30964. +
  30965. +static void call_do_xino_fwrite(void *args)
  30966. +{
  30967. + struct do_xino_fwrite_args *a = args;
  30968. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  30969. +}
  30970. +
  30971. +ssize_t xino_fwrite(au_writef_t func, struct file *file, void *buf, size_t size,
  30972. + loff_t *pos)
  30973. +{
  30974. + ssize_t err;
  30975. +
  30976. + /* todo: signal block and no wkq? */
  30977. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  30978. + lockdep_off();
  30979. + err = do_xino_fwrite(func, file, buf, size, pos);
  30980. + lockdep_on();
  30981. + } else {
  30982. + /*
  30983. + * it breaks RLIMIT_FSIZE and normal user's limit,
  30984. + * users should care about quota and real 'filesystem full.'
  30985. + */
  30986. + int wkq_err;
  30987. + struct do_xino_fwrite_args args = {
  30988. + .errp = &err,
  30989. + .func = func,
  30990. + .file = file,
  30991. + .buf = buf,
  30992. + .size = size,
  30993. + .pos = pos
  30994. + };
  30995. +
  30996. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  30997. + if (unlikely(wkq_err))
  30998. + err = wkq_err;
  30999. + }
  31000. +
  31001. + return err;
  31002. +}
  31003. +
  31004. +/* ---------------------------------------------------------------------- */
  31005. +
  31006. +/*
  31007. + * create a new xinofile at the same place/path as @base_file.
  31008. + */
  31009. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
  31010. +{
  31011. + struct file *file;
  31012. + struct dentry *base, *parent;
  31013. + struct inode *dir, *delegated;
  31014. + struct qstr *name;
  31015. + struct path path;
  31016. + int err;
  31017. +
  31018. + base = base_file->f_path.dentry;
  31019. + parent = base->d_parent; /* dir inode is locked */
  31020. + dir = parent->d_inode;
  31021. + IMustLock(dir);
  31022. +
  31023. + file = ERR_PTR(-EINVAL);
  31024. + name = &base->d_name;
  31025. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  31026. + if (IS_ERR(path.dentry)) {
  31027. + file = (void *)path.dentry;
  31028. + pr_err("%pd lookup err %ld\n",
  31029. + base, PTR_ERR(path.dentry));
  31030. + goto out;
  31031. + }
  31032. +
  31033. + /* no need to mnt_want_write() since we call dentry_open() later */
  31034. + err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
  31035. + if (unlikely(err)) {
  31036. + file = ERR_PTR(err);
  31037. + pr_err("%pd create err %d\n", base, err);
  31038. + goto out_dput;
  31039. + }
  31040. +
  31041. + path.mnt = base_file->f_path.mnt;
  31042. + file = vfsub_dentry_open(&path,
  31043. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  31044. + /* | __FMODE_NONOTIFY */);
  31045. + if (IS_ERR(file)) {
  31046. + pr_err("%pd open err %ld\n", base, PTR_ERR(file));
  31047. + goto out_dput;
  31048. + }
  31049. +
  31050. + delegated = NULL;
  31051. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  31052. + if (unlikely(err == -EWOULDBLOCK)) {
  31053. + pr_warn("cannot retry for NFSv4 delegation"
  31054. + " for an internal unlink\n");
  31055. + iput(delegated);
  31056. + }
  31057. + if (unlikely(err)) {
  31058. + pr_err("%pd unlink err %d\n", base, err);
  31059. + goto out_fput;
  31060. + }
  31061. +
  31062. + if (copy_src) {
  31063. + /* no one can touch copy_src xino */
  31064. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  31065. + if (unlikely(err)) {
  31066. + pr_err("%pd copy err %d\n", base, err);
  31067. + goto out_fput;
  31068. + }
  31069. + }
  31070. + goto out_dput; /* success */
  31071. +
  31072. +out_fput:
  31073. + fput(file);
  31074. + file = ERR_PTR(err);
  31075. +out_dput:
  31076. + dput(path.dentry);
  31077. +out:
  31078. + return file;
  31079. +}
  31080. +
  31081. +struct au_xino_lock_dir {
  31082. + struct au_hinode *hdir;
  31083. + struct dentry *parent;
  31084. + struct mutex *mtx;
  31085. +};
  31086. +
  31087. +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
  31088. + struct au_xino_lock_dir *ldir)
  31089. +{
  31090. + aufs_bindex_t brid, bindex;
  31091. +
  31092. + ldir->hdir = NULL;
  31093. + bindex = -1;
  31094. + brid = au_xino_brid(sb);
  31095. + if (brid >= 0)
  31096. + bindex = au_br_index(sb, brid);
  31097. + if (bindex >= 0) {
  31098. + ldir->hdir = au_hi(sb->s_root->d_inode, bindex);
  31099. + au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  31100. + } else {
  31101. + ldir->parent = dget_parent(xino->f_path.dentry);
  31102. + ldir->mtx = &ldir->parent->d_inode->i_mutex;
  31103. + mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
  31104. + }
  31105. +}
  31106. +
  31107. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  31108. +{
  31109. + if (ldir->hdir)
  31110. + au_hn_imtx_unlock(ldir->hdir);
  31111. + else {
  31112. + mutex_unlock(ldir->mtx);
  31113. + dput(ldir->parent);
  31114. + }
  31115. +}
  31116. +
  31117. +/* ---------------------------------------------------------------------- */
  31118. +
  31119. +/* trucate xino files asynchronously */
  31120. +
  31121. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
  31122. +{
  31123. + int err;
  31124. + unsigned long jiffy;
  31125. + blkcnt_t blocks;
  31126. + aufs_bindex_t bi, bend;
  31127. + struct kstatfs *st;
  31128. + struct au_branch *br;
  31129. + struct file *new_xino, *file;
  31130. + struct super_block *h_sb;
  31131. + struct au_xino_lock_dir ldir;
  31132. +
  31133. + err = -ENOMEM;
  31134. + st = kzalloc(sizeof(*st), GFP_NOFS);
  31135. + if (unlikely(!st))
  31136. + goto out;
  31137. +
  31138. + err = -EINVAL;
  31139. + bend = au_sbend(sb);
  31140. + if (unlikely(bindex < 0 || bend < bindex))
  31141. + goto out_st;
  31142. + br = au_sbr(sb, bindex);
  31143. + file = br->br_xino.xi_file;
  31144. + if (!file)
  31145. + goto out_st;
  31146. +
  31147. + err = vfs_statfs(&file->f_path, st);
  31148. + if (unlikely(err))
  31149. + AuErr1("statfs err %d, ignored\n", err);
  31150. + jiffy = jiffies;
  31151. + blocks = file_inode(file)->i_blocks;
  31152. + pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  31153. + bindex, (u64)blocks, st->f_bfree, st->f_blocks);
  31154. +
  31155. + au_xino_lock_dir(sb, file, &ldir);
  31156. + /* mnt_want_write() is unnecessary here */
  31157. + new_xino = au_xino_create2(file, file);
  31158. + au_xino_unlock_dir(&ldir);
  31159. + err = PTR_ERR(new_xino);
  31160. + if (IS_ERR(new_xino)) {
  31161. + pr_err("err %d, ignored\n", err);
  31162. + goto out_st;
  31163. + }
  31164. + err = 0;
  31165. + fput(file);
  31166. + br->br_xino.xi_file = new_xino;
  31167. +
  31168. + h_sb = au_br_sb(br);
  31169. + for (bi = 0; bi <= bend; bi++) {
  31170. + if (unlikely(bi == bindex))
  31171. + continue;
  31172. + br = au_sbr(sb, bi);
  31173. + if (au_br_sb(br) != h_sb)
  31174. + continue;
  31175. +
  31176. + fput(br->br_xino.xi_file);
  31177. + br->br_xino.xi_file = new_xino;
  31178. + get_file(new_xino);
  31179. + }
  31180. +
  31181. + err = vfs_statfs(&new_xino->f_path, st);
  31182. + if (!err) {
  31183. + pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  31184. + bindex, (u64)file_inode(new_xino)->i_blocks,
  31185. + st->f_bfree, st->f_blocks);
  31186. + if (file_inode(new_xino)->i_blocks < blocks)
  31187. + au_sbi(sb)->si_xino_jiffy = jiffy;
  31188. + } else
  31189. + AuErr1("statfs err %d, ignored\n", err);
  31190. +
  31191. +out_st:
  31192. + kfree(st);
  31193. +out:
  31194. + return err;
  31195. +}
  31196. +
  31197. +struct xino_do_trunc_args {
  31198. + struct super_block *sb;
  31199. + struct au_branch *br;
  31200. +};
  31201. +
  31202. +static void xino_do_trunc(void *_args)
  31203. +{
  31204. + struct xino_do_trunc_args *args = _args;
  31205. + struct super_block *sb;
  31206. + struct au_branch *br;
  31207. + struct inode *dir;
  31208. + int err;
  31209. + aufs_bindex_t bindex;
  31210. +
  31211. + err = 0;
  31212. + sb = args->sb;
  31213. + dir = sb->s_root->d_inode;
  31214. + br = args->br;
  31215. +
  31216. + si_noflush_write_lock(sb);
  31217. + ii_read_lock_parent(dir);
  31218. + bindex = au_br_index(sb, br->br_id);
  31219. + err = au_xino_trunc(sb, bindex);
  31220. + ii_read_unlock(dir);
  31221. + if (unlikely(err))
  31222. + pr_warn("err b%d, (%d)\n", bindex, err);
  31223. + atomic_dec(&br->br_xino_running);
  31224. + atomic_dec(&br->br_count);
  31225. + si_write_unlock(sb);
  31226. + au_nwt_done(&au_sbi(sb)->si_nowait);
  31227. + kfree(args);
  31228. +}
  31229. +
  31230. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  31231. +{
  31232. + int err;
  31233. + struct kstatfs st;
  31234. + struct au_sbinfo *sbinfo;
  31235. +
  31236. + /* todo: si_xino_expire and the ratio should be customizable */
  31237. + sbinfo = au_sbi(sb);
  31238. + if (time_before(jiffies,
  31239. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  31240. + return 0;
  31241. +
  31242. + /* truncation border */
  31243. + err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
  31244. + if (unlikely(err)) {
  31245. + AuErr1("statfs err %d, ignored\n", err);
  31246. + return 0;
  31247. + }
  31248. + if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
  31249. + return 0;
  31250. +
  31251. + return 1;
  31252. +}
  31253. +
  31254. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  31255. +{
  31256. + struct xino_do_trunc_args *args;
  31257. + int wkq_err;
  31258. +
  31259. + if (!xino_trunc_test(sb, br))
  31260. + return;
  31261. +
  31262. + if (atomic_inc_return(&br->br_xino_running) > 1)
  31263. + goto out;
  31264. +
  31265. + /* lock and kfree() will be called in trunc_xino() */
  31266. + args = kmalloc(sizeof(*args), GFP_NOFS);
  31267. + if (unlikely(!args)) {
  31268. + AuErr1("no memory\n");
  31269. + goto out_args;
  31270. + }
  31271. +
  31272. + atomic_inc(&br->br_count);
  31273. + args->sb = sb;
  31274. + args->br = br;
  31275. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  31276. + if (!wkq_err)
  31277. + return; /* success */
  31278. +
  31279. + pr_err("wkq %d\n", wkq_err);
  31280. + atomic_dec(&br->br_count);
  31281. +
  31282. +out_args:
  31283. + kfree(args);
  31284. +out:
  31285. + atomic_dec(&br->br_xino_running);
  31286. +}
  31287. +
  31288. +/* ---------------------------------------------------------------------- */
  31289. +
  31290. +static int au_xino_do_write(au_writef_t write, struct file *file,
  31291. + ino_t h_ino, ino_t ino)
  31292. +{
  31293. + loff_t pos;
  31294. + ssize_t sz;
  31295. +
  31296. + pos = h_ino;
  31297. + if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
  31298. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  31299. + return -EFBIG;
  31300. + }
  31301. + pos *= sizeof(ino);
  31302. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
  31303. + if (sz == sizeof(ino))
  31304. + return 0; /* success */
  31305. +
  31306. + AuIOErr("write failed (%zd)\n", sz);
  31307. + return -EIO;
  31308. +}
  31309. +
  31310. +/*
  31311. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  31312. + * at the position of @h_ino.
  31313. + * even if @ino is zero, it is written to the xinofile and means no entry.
  31314. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  31315. + * try truncating it.
  31316. + */
  31317. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  31318. + ino_t ino)
  31319. +{
  31320. + int err;
  31321. + unsigned int mnt_flags;
  31322. + struct au_branch *br;
  31323. +
  31324. + BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
  31325. + || ((loff_t)-1) > 0);
  31326. + SiMustAnyLock(sb);
  31327. +
  31328. + mnt_flags = au_mntflags(sb);
  31329. + if (!au_opt_test(mnt_flags, XINO))
  31330. + return 0;
  31331. +
  31332. + br = au_sbr(sb, bindex);
  31333. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  31334. + h_ino, ino);
  31335. + if (!err) {
  31336. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  31337. + && au_test_fs_trunc_xino(au_br_sb(br)))
  31338. + xino_try_trunc(sb, br);
  31339. + return 0; /* success */
  31340. + }
  31341. +
  31342. + AuIOErr("write failed (%d)\n", err);
  31343. + return -EIO;
  31344. +}
  31345. +
  31346. +/* ---------------------------------------------------------------------- */
  31347. +
  31348. +/* aufs inode number bitmap */
  31349. +
  31350. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  31351. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  31352. +{
  31353. + ino_t ino;
  31354. +
  31355. + AuDebugOn(bit < 0 || page_bits <= bit);
  31356. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  31357. + return ino;
  31358. +}
  31359. +
  31360. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  31361. +{
  31362. + AuDebugOn(ino < AUFS_FIRST_INO);
  31363. + ino -= AUFS_FIRST_INO;
  31364. + *pindex = ino / page_bits;
  31365. + *bit = ino % page_bits;
  31366. +}
  31367. +
  31368. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  31369. +{
  31370. + int err;
  31371. + loff_t pos;
  31372. + ssize_t sz;
  31373. + struct au_sbinfo *sbinfo;
  31374. + struct file *xib;
  31375. + unsigned long *p;
  31376. +
  31377. + sbinfo = au_sbi(sb);
  31378. + MtxMustLock(&sbinfo->si_xib_mtx);
  31379. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  31380. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  31381. +
  31382. + if (pindex == sbinfo->si_xib_last_pindex)
  31383. + return 0;
  31384. +
  31385. + xib = sbinfo->si_xib;
  31386. + p = sbinfo->si_xib_buf;
  31387. + pos = sbinfo->si_xib_last_pindex;
  31388. + pos *= PAGE_SIZE;
  31389. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  31390. + if (unlikely(sz != PAGE_SIZE))
  31391. + goto out;
  31392. +
  31393. + pos = pindex;
  31394. + pos *= PAGE_SIZE;
  31395. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  31396. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  31397. + else {
  31398. + memset(p, 0, PAGE_SIZE);
  31399. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  31400. + }
  31401. + if (sz == PAGE_SIZE) {
  31402. + sbinfo->si_xib_last_pindex = pindex;
  31403. + return 0; /* success */
  31404. + }
  31405. +
  31406. +out:
  31407. + AuIOErr1("write failed (%zd)\n", sz);
  31408. + err = sz;
  31409. + if (sz >= 0)
  31410. + err = -EIO;
  31411. + return err;
  31412. +}
  31413. +
  31414. +/* ---------------------------------------------------------------------- */
  31415. +
  31416. +static void au_xib_clear_bit(struct inode *inode)
  31417. +{
  31418. + int err, bit;
  31419. + unsigned long pindex;
  31420. + struct super_block *sb;
  31421. + struct au_sbinfo *sbinfo;
  31422. +
  31423. + AuDebugOn(inode->i_nlink);
  31424. +
  31425. + sb = inode->i_sb;
  31426. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  31427. + AuDebugOn(page_bits <= bit);
  31428. + sbinfo = au_sbi(sb);
  31429. + mutex_lock(&sbinfo->si_xib_mtx);
  31430. + err = xib_pindex(sb, pindex);
  31431. + if (!err) {
  31432. + clear_bit(bit, sbinfo->si_xib_buf);
  31433. + sbinfo->si_xib_next_bit = bit;
  31434. + }
  31435. + mutex_unlock(&sbinfo->si_xib_mtx);
  31436. +}
  31437. +
  31438. +/* for s_op->delete_inode() */
  31439. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  31440. +{
  31441. + int err;
  31442. + unsigned int mnt_flags;
  31443. + aufs_bindex_t bindex, bend, bi;
  31444. + unsigned char try_trunc;
  31445. + struct au_iinfo *iinfo;
  31446. + struct super_block *sb;
  31447. + struct au_hinode *hi;
  31448. + struct inode *h_inode;
  31449. + struct au_branch *br;
  31450. + au_writef_t xwrite;
  31451. +
  31452. + sb = inode->i_sb;
  31453. + mnt_flags = au_mntflags(sb);
  31454. + if (!au_opt_test(mnt_flags, XINO)
  31455. + || inode->i_ino == AUFS_ROOT_INO)
  31456. + return;
  31457. +
  31458. + if (unlinked) {
  31459. + au_xigen_inc(inode);
  31460. + au_xib_clear_bit(inode);
  31461. + }
  31462. +
  31463. + iinfo = au_ii(inode);
  31464. + if (!iinfo)
  31465. + return;
  31466. +
  31467. + bindex = iinfo->ii_bstart;
  31468. + if (bindex < 0)
  31469. + return;
  31470. +
  31471. + xwrite = au_sbi(sb)->si_xwrite;
  31472. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  31473. + hi = iinfo->ii_hinode + bindex;
  31474. + bend = iinfo->ii_bend;
  31475. + for (; bindex <= bend; bindex++, hi++) {
  31476. + h_inode = hi->hi_inode;
  31477. + if (!h_inode
  31478. + || (!unlinked && h_inode->i_nlink))
  31479. + continue;
  31480. +
  31481. + /* inode may not be revalidated */
  31482. + bi = au_br_index(sb, hi->hi_id);
  31483. + if (bi < 0)
  31484. + continue;
  31485. +
  31486. + br = au_sbr(sb, bi);
  31487. + err = au_xino_do_write(xwrite, br->br_xino.xi_file,
  31488. + h_inode->i_ino, /*ino*/0);
  31489. + if (!err && try_trunc
  31490. + && au_test_fs_trunc_xino(au_br_sb(br)))
  31491. + xino_try_trunc(sb, br);
  31492. + }
  31493. +}
  31494. +
  31495. +/* get an unused inode number from bitmap */
  31496. +ino_t au_xino_new_ino(struct super_block *sb)
  31497. +{
  31498. + ino_t ino;
  31499. + unsigned long *p, pindex, ul, pend;
  31500. + struct au_sbinfo *sbinfo;
  31501. + struct file *file;
  31502. + int free_bit, err;
  31503. +
  31504. + if (!au_opt_test(au_mntflags(sb), XINO))
  31505. + return iunique(sb, AUFS_FIRST_INO);
  31506. +
  31507. + sbinfo = au_sbi(sb);
  31508. + mutex_lock(&sbinfo->si_xib_mtx);
  31509. + p = sbinfo->si_xib_buf;
  31510. + free_bit = sbinfo->si_xib_next_bit;
  31511. + if (free_bit < page_bits && !test_bit(free_bit, p))
  31512. + goto out; /* success */
  31513. + free_bit = find_first_zero_bit(p, page_bits);
  31514. + if (free_bit < page_bits)
  31515. + goto out; /* success */
  31516. +
  31517. + pindex = sbinfo->si_xib_last_pindex;
  31518. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  31519. + err = xib_pindex(sb, ul);
  31520. + if (unlikely(err))
  31521. + goto out_err;
  31522. + free_bit = find_first_zero_bit(p, page_bits);
  31523. + if (free_bit < page_bits)
  31524. + goto out; /* success */
  31525. + }
  31526. +
  31527. + file = sbinfo->si_xib;
  31528. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  31529. + for (ul = pindex + 1; ul <= pend; ul++) {
  31530. + err = xib_pindex(sb, ul);
  31531. + if (unlikely(err))
  31532. + goto out_err;
  31533. + free_bit = find_first_zero_bit(p, page_bits);
  31534. + if (free_bit < page_bits)
  31535. + goto out; /* success */
  31536. + }
  31537. + BUG();
  31538. +
  31539. +out:
  31540. + set_bit(free_bit, p);
  31541. + sbinfo->si_xib_next_bit = free_bit + 1;
  31542. + pindex = sbinfo->si_xib_last_pindex;
  31543. + mutex_unlock(&sbinfo->si_xib_mtx);
  31544. + ino = xib_calc_ino(pindex, free_bit);
  31545. + AuDbg("i%lu\n", (unsigned long)ino);
  31546. + return ino;
  31547. +out_err:
  31548. + mutex_unlock(&sbinfo->si_xib_mtx);
  31549. + AuDbg("i0\n");
  31550. + return 0;
  31551. +}
  31552. +
  31553. +/*
  31554. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  31555. + * at the position of @h_ino.
  31556. + * if @ino does not exist and @do_new is true, get new one.
  31557. + */
  31558. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  31559. + ino_t *ino)
  31560. +{
  31561. + int err;
  31562. + ssize_t sz;
  31563. + loff_t pos;
  31564. + struct file *file;
  31565. + struct au_sbinfo *sbinfo;
  31566. +
  31567. + *ino = 0;
  31568. + if (!au_opt_test(au_mntflags(sb), XINO))
  31569. + return 0; /* no xino */
  31570. +
  31571. + err = 0;
  31572. + sbinfo = au_sbi(sb);
  31573. + pos = h_ino;
  31574. + if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
  31575. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  31576. + return -EFBIG;
  31577. + }
  31578. + pos *= sizeof(*ino);
  31579. +
  31580. + file = au_sbr(sb, bindex)->br_xino.xi_file;
  31581. + if (vfsub_f_size_read(file) < pos + sizeof(*ino))
  31582. + return 0; /* no ino */
  31583. +
  31584. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
  31585. + if (sz == sizeof(*ino))
  31586. + return 0; /* success */
  31587. +
  31588. + err = sz;
  31589. + if (unlikely(sz >= 0)) {
  31590. + err = -EIO;
  31591. + AuIOErr("xino read error (%zd)\n", sz);
  31592. + }
  31593. +
  31594. + return err;
  31595. +}
  31596. +
  31597. +/* ---------------------------------------------------------------------- */
  31598. +
  31599. +/* create and set a new xino file */
  31600. +
  31601. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
  31602. +{
  31603. + struct file *file;
  31604. + struct dentry *h_parent, *d;
  31605. + struct inode *h_dir, *inode;
  31606. + int err;
  31607. +
  31608. + /*
  31609. + * at mount-time, and the xino file is the default path,
  31610. + * hnotify is disabled so we have no notify events to ignore.
  31611. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  31612. + */
  31613. + file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  31614. + /* | __FMODE_NONOTIFY */,
  31615. + S_IRUGO | S_IWUGO);
  31616. + if (IS_ERR(file)) {
  31617. + if (!silent)
  31618. + pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
  31619. + return file;
  31620. + }
  31621. +
  31622. + /* keep file count */
  31623. + err = 0;
  31624. + inode = file_inode(file);
  31625. + h_parent = dget_parent(file->f_path.dentry);
  31626. + h_dir = h_parent->d_inode;
  31627. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  31628. + /* mnt_want_write() is unnecessary here */
  31629. + /* no delegation since it is just created */
  31630. + if (inode->i_nlink)
  31631. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  31632. + /*force*/0);
  31633. + mutex_unlock(&h_dir->i_mutex);
  31634. + dput(h_parent);
  31635. + if (unlikely(err)) {
  31636. + if (!silent)
  31637. + pr_err("unlink %s(%d)\n", fname, err);
  31638. + goto out;
  31639. + }
  31640. +
  31641. + err = -EINVAL;
  31642. + d = file->f_path.dentry;
  31643. + if (unlikely(sb == d->d_sb)) {
  31644. + if (!silent)
  31645. + pr_err("%s must be outside\n", fname);
  31646. + goto out;
  31647. + }
  31648. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  31649. + if (!silent)
  31650. + pr_err("xino doesn't support %s(%s)\n",
  31651. + fname, au_sbtype(d->d_sb));
  31652. + goto out;
  31653. + }
  31654. + return file; /* success */
  31655. +
  31656. +out:
  31657. + fput(file);
  31658. + file = ERR_PTR(err);
  31659. + return file;
  31660. +}
  31661. +
  31662. +/*
  31663. + * find another branch who is on the same filesystem of the specified
  31664. + * branch{@btgt}. search until @bend.
  31665. + */
  31666. +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  31667. + aufs_bindex_t bend)
  31668. +{
  31669. + aufs_bindex_t bindex;
  31670. + struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
  31671. +
  31672. + for (bindex = 0; bindex < btgt; bindex++)
  31673. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  31674. + return bindex;
  31675. + for (bindex++; bindex <= bend; bindex++)
  31676. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  31677. + return bindex;
  31678. + return -1;
  31679. +}
  31680. +
  31681. +/* ---------------------------------------------------------------------- */
  31682. +
  31683. +/*
  31684. + * initialize the xinofile for the specified branch @br
  31685. + * at the place/path where @base_file indicates.
  31686. + * test whether another branch is on the same filesystem or not,
  31687. + * if @do_test is true.
  31688. + */
  31689. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  31690. + struct file *base_file, int do_test)
  31691. +{
  31692. + int err;
  31693. + ino_t ino;
  31694. + aufs_bindex_t bend, bindex;
  31695. + struct au_branch *shared_br, *b;
  31696. + struct file *file;
  31697. + struct super_block *tgt_sb;
  31698. +
  31699. + shared_br = NULL;
  31700. + bend = au_sbend(sb);
  31701. + if (do_test) {
  31702. + tgt_sb = au_br_sb(br);
  31703. + for (bindex = 0; bindex <= bend; bindex++) {
  31704. + b = au_sbr(sb, bindex);
  31705. + if (tgt_sb == au_br_sb(b)) {
  31706. + shared_br = b;
  31707. + break;
  31708. + }
  31709. + }
  31710. + }
  31711. +
  31712. + if (!shared_br || !shared_br->br_xino.xi_file) {
  31713. + struct au_xino_lock_dir ldir;
  31714. +
  31715. + au_xino_lock_dir(sb, base_file, &ldir);
  31716. + /* mnt_want_write() is unnecessary here */
  31717. + file = au_xino_create2(base_file, NULL);
  31718. + au_xino_unlock_dir(&ldir);
  31719. + err = PTR_ERR(file);
  31720. + if (IS_ERR(file))
  31721. + goto out;
  31722. + br->br_xino.xi_file = file;
  31723. + } else {
  31724. + br->br_xino.xi_file = shared_br->br_xino.xi_file;
  31725. + get_file(br->br_xino.xi_file);
  31726. + }
  31727. +
  31728. + ino = AUFS_ROOT_INO;
  31729. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  31730. + h_ino, ino);
  31731. + if (unlikely(err)) {
  31732. + fput(br->br_xino.xi_file);
  31733. + br->br_xino.xi_file = NULL;
  31734. + }
  31735. +
  31736. +out:
  31737. + return err;
  31738. +}
  31739. +
  31740. +/* ---------------------------------------------------------------------- */
  31741. +
  31742. +/* trucate a xino bitmap file */
  31743. +
  31744. +/* todo: slow */
  31745. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  31746. +{
  31747. + int err, bit;
  31748. + ssize_t sz;
  31749. + unsigned long pindex;
  31750. + loff_t pos, pend;
  31751. + struct au_sbinfo *sbinfo;
  31752. + au_readf_t func;
  31753. + ino_t *ino;
  31754. + unsigned long *p;
  31755. +
  31756. + err = 0;
  31757. + sbinfo = au_sbi(sb);
  31758. + MtxMustLock(&sbinfo->si_xib_mtx);
  31759. + p = sbinfo->si_xib_buf;
  31760. + func = sbinfo->si_xread;
  31761. + pend = vfsub_f_size_read(file);
  31762. + pos = 0;
  31763. + while (pos < pend) {
  31764. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  31765. + err = sz;
  31766. + if (unlikely(sz <= 0))
  31767. + goto out;
  31768. +
  31769. + err = 0;
  31770. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  31771. + if (unlikely(*ino < AUFS_FIRST_INO))
  31772. + continue;
  31773. +
  31774. + xib_calc_bit(*ino, &pindex, &bit);
  31775. + AuDebugOn(page_bits <= bit);
  31776. + err = xib_pindex(sb, pindex);
  31777. + if (!err)
  31778. + set_bit(bit, p);
  31779. + else
  31780. + goto out;
  31781. + }
  31782. + }
  31783. +
  31784. +out:
  31785. + return err;
  31786. +}
  31787. +
  31788. +static int xib_restore(struct super_block *sb)
  31789. +{
  31790. + int err;
  31791. + aufs_bindex_t bindex, bend;
  31792. + void *page;
  31793. +
  31794. + err = -ENOMEM;
  31795. + page = (void *)__get_free_page(GFP_NOFS);
  31796. + if (unlikely(!page))
  31797. + goto out;
  31798. +
  31799. + err = 0;
  31800. + bend = au_sbend(sb);
  31801. + for (bindex = 0; !err && bindex <= bend; bindex++)
  31802. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
  31803. + err = do_xib_restore
  31804. + (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
  31805. + else
  31806. + AuDbg("b%d\n", bindex);
  31807. + free_page((unsigned long)page);
  31808. +
  31809. +out:
  31810. + return err;
  31811. +}
  31812. +
  31813. +int au_xib_trunc(struct super_block *sb)
  31814. +{
  31815. + int err;
  31816. + ssize_t sz;
  31817. + loff_t pos;
  31818. + struct au_xino_lock_dir ldir;
  31819. + struct au_sbinfo *sbinfo;
  31820. + unsigned long *p;
  31821. + struct file *file;
  31822. +
  31823. + SiMustWriteLock(sb);
  31824. +
  31825. + err = 0;
  31826. + sbinfo = au_sbi(sb);
  31827. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  31828. + goto out;
  31829. +
  31830. + file = sbinfo->si_xib;
  31831. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  31832. + goto out;
  31833. +
  31834. + au_xino_lock_dir(sb, file, &ldir);
  31835. + /* mnt_want_write() is unnecessary here */
  31836. + file = au_xino_create2(sbinfo->si_xib, NULL);
  31837. + au_xino_unlock_dir(&ldir);
  31838. + err = PTR_ERR(file);
  31839. + if (IS_ERR(file))
  31840. + goto out;
  31841. + fput(sbinfo->si_xib);
  31842. + sbinfo->si_xib = file;
  31843. +
  31844. + p = sbinfo->si_xib_buf;
  31845. + memset(p, 0, PAGE_SIZE);
  31846. + pos = 0;
  31847. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  31848. + if (unlikely(sz != PAGE_SIZE)) {
  31849. + err = sz;
  31850. + AuIOErr("err %d\n", err);
  31851. + if (sz >= 0)
  31852. + err = -EIO;
  31853. + goto out;
  31854. + }
  31855. +
  31856. + mutex_lock(&sbinfo->si_xib_mtx);
  31857. + /* mnt_want_write() is unnecessary here */
  31858. + err = xib_restore(sb);
  31859. + mutex_unlock(&sbinfo->si_xib_mtx);
  31860. +
  31861. +out:
  31862. + return err;
  31863. +}
  31864. +
  31865. +/* ---------------------------------------------------------------------- */
  31866. +
  31867. +/*
  31868. + * xino mount option handlers
  31869. + */
  31870. +static au_readf_t find_readf(struct file *h_file)
  31871. +{
  31872. + const struct file_operations *fop = h_file->f_op;
  31873. +
  31874. + if (fop->read)
  31875. + return fop->read;
  31876. + if (fop->aio_read)
  31877. + return do_sync_read;
  31878. + if (fop->read_iter)
  31879. + return new_sync_read;
  31880. + return ERR_PTR(-ENOSYS);
  31881. +}
  31882. +
  31883. +static au_writef_t find_writef(struct file *h_file)
  31884. +{
  31885. + const struct file_operations *fop = h_file->f_op;
  31886. +
  31887. + if (fop->write)
  31888. + return fop->write;
  31889. + if (fop->aio_write)
  31890. + return do_sync_write;
  31891. + if (fop->write_iter)
  31892. + return new_sync_write;
  31893. + return ERR_PTR(-ENOSYS);
  31894. +}
  31895. +
  31896. +/* xino bitmap */
  31897. +static void xino_clear_xib(struct super_block *sb)
  31898. +{
  31899. + struct au_sbinfo *sbinfo;
  31900. +
  31901. + SiMustWriteLock(sb);
  31902. +
  31903. + sbinfo = au_sbi(sb);
  31904. + sbinfo->si_xread = NULL;
  31905. + sbinfo->si_xwrite = NULL;
  31906. + if (sbinfo->si_xib)
  31907. + fput(sbinfo->si_xib);
  31908. + sbinfo->si_xib = NULL;
  31909. + free_page((unsigned long)sbinfo->si_xib_buf);
  31910. + sbinfo->si_xib_buf = NULL;
  31911. +}
  31912. +
  31913. +static int au_xino_set_xib(struct super_block *sb, struct file *base)
  31914. +{
  31915. + int err;
  31916. + loff_t pos;
  31917. + struct au_sbinfo *sbinfo;
  31918. + struct file *file;
  31919. +
  31920. + SiMustWriteLock(sb);
  31921. +
  31922. + sbinfo = au_sbi(sb);
  31923. + file = au_xino_create2(base, sbinfo->si_xib);
  31924. + err = PTR_ERR(file);
  31925. + if (IS_ERR(file))
  31926. + goto out;
  31927. + if (sbinfo->si_xib)
  31928. + fput(sbinfo->si_xib);
  31929. + sbinfo->si_xib = file;
  31930. + sbinfo->si_xread = find_readf(file);
  31931. + sbinfo->si_xwrite = find_writef(file);
  31932. +
  31933. + err = -ENOMEM;
  31934. + if (!sbinfo->si_xib_buf)
  31935. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  31936. + if (unlikely(!sbinfo->si_xib_buf))
  31937. + goto out_unset;
  31938. +
  31939. + sbinfo->si_xib_last_pindex = 0;
  31940. + sbinfo->si_xib_next_bit = 0;
  31941. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  31942. + pos = 0;
  31943. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  31944. + PAGE_SIZE, &pos);
  31945. + if (unlikely(err != PAGE_SIZE))
  31946. + goto out_free;
  31947. + }
  31948. + err = 0;
  31949. + goto out; /* success */
  31950. +
  31951. +out_free:
  31952. + free_page((unsigned long)sbinfo->si_xib_buf);
  31953. + sbinfo->si_xib_buf = NULL;
  31954. + if (err >= 0)
  31955. + err = -EIO;
  31956. +out_unset:
  31957. + fput(sbinfo->si_xib);
  31958. + sbinfo->si_xib = NULL;
  31959. + sbinfo->si_xread = NULL;
  31960. + sbinfo->si_xwrite = NULL;
  31961. +out:
  31962. + return err;
  31963. +}
  31964. +
  31965. +/* xino for each branch */
  31966. +static void xino_clear_br(struct super_block *sb)
  31967. +{
  31968. + aufs_bindex_t bindex, bend;
  31969. + struct au_branch *br;
  31970. +
  31971. + bend = au_sbend(sb);
  31972. + for (bindex = 0; bindex <= bend; bindex++) {
  31973. + br = au_sbr(sb, bindex);
  31974. + if (!br || !br->br_xino.xi_file)
  31975. + continue;
  31976. +
  31977. + fput(br->br_xino.xi_file);
  31978. + br->br_xino.xi_file = NULL;
  31979. + }
  31980. +}
  31981. +
  31982. +static int au_xino_set_br(struct super_block *sb, struct file *base)
  31983. +{
  31984. + int err;
  31985. + ino_t ino;
  31986. + aufs_bindex_t bindex, bend, bshared;
  31987. + struct {
  31988. + struct file *old, *new;
  31989. + } *fpair, *p;
  31990. + struct au_branch *br;
  31991. + struct inode *inode;
  31992. + au_writef_t writef;
  31993. +
  31994. + SiMustWriteLock(sb);
  31995. +
  31996. + err = -ENOMEM;
  31997. + bend = au_sbend(sb);
  31998. + fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
  31999. + if (unlikely(!fpair))
  32000. + goto out;
  32001. +
  32002. + inode = sb->s_root->d_inode;
  32003. + ino = AUFS_ROOT_INO;
  32004. + writef = au_sbi(sb)->si_xwrite;
  32005. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
  32006. + br = au_sbr(sb, bindex);
  32007. + bshared = is_sb_shared(sb, bindex, bindex - 1);
  32008. + if (bshared >= 0) {
  32009. + /* shared xino */
  32010. + *p = fpair[bshared];
  32011. + get_file(p->new);
  32012. + }
  32013. +
  32014. + if (!p->new) {
  32015. + /* new xino */
  32016. + p->old = br->br_xino.xi_file;
  32017. + p->new = au_xino_create2(base, br->br_xino.xi_file);
  32018. + err = PTR_ERR(p->new);
  32019. + if (IS_ERR(p->new)) {
  32020. + p->new = NULL;
  32021. + goto out_pair;
  32022. + }
  32023. + }
  32024. +
  32025. + err = au_xino_do_write(writef, p->new,
  32026. + au_h_iptr(inode, bindex)->i_ino, ino);
  32027. + if (unlikely(err))
  32028. + goto out_pair;
  32029. + }
  32030. +
  32031. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
  32032. + br = au_sbr(sb, bindex);
  32033. + if (br->br_xino.xi_file)
  32034. + fput(br->br_xino.xi_file);
  32035. + get_file(p->new);
  32036. + br->br_xino.xi_file = p->new;
  32037. + }
  32038. +
  32039. +out_pair:
  32040. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
  32041. + if (p->new)
  32042. + fput(p->new);
  32043. + else
  32044. + break;
  32045. + kfree(fpair);
  32046. +out:
  32047. + return err;
  32048. +}
  32049. +
  32050. +void au_xino_clr(struct super_block *sb)
  32051. +{
  32052. + struct au_sbinfo *sbinfo;
  32053. +
  32054. + au_xigen_clr(sb);
  32055. + xino_clear_xib(sb);
  32056. + xino_clear_br(sb);
  32057. + sbinfo = au_sbi(sb);
  32058. + /* lvalue, do not call au_mntflags() */
  32059. + au_opt_clr(sbinfo->si_mntflags, XINO);
  32060. +}
  32061. +
  32062. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
  32063. +{
  32064. + int err, skip;
  32065. + struct dentry *parent, *cur_parent;
  32066. + struct qstr *dname, *cur_name;
  32067. + struct file *cur_xino;
  32068. + struct inode *dir;
  32069. + struct au_sbinfo *sbinfo;
  32070. +
  32071. + SiMustWriteLock(sb);
  32072. +
  32073. + err = 0;
  32074. + sbinfo = au_sbi(sb);
  32075. + parent = dget_parent(xino->file->f_path.dentry);
  32076. + if (remount) {
  32077. + skip = 0;
  32078. + dname = &xino->file->f_path.dentry->d_name;
  32079. + cur_xino = sbinfo->si_xib;
  32080. + if (cur_xino) {
  32081. + cur_parent = dget_parent(cur_xino->f_path.dentry);
  32082. + cur_name = &cur_xino->f_path.dentry->d_name;
  32083. + skip = (cur_parent == parent
  32084. + && au_qstreq(dname, cur_name));
  32085. + dput(cur_parent);
  32086. + }
  32087. + if (skip)
  32088. + goto out;
  32089. + }
  32090. +
  32091. + au_opt_set(sbinfo->si_mntflags, XINO);
  32092. + dir = parent->d_inode;
  32093. + mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
  32094. + /* mnt_want_write() is unnecessary here */
  32095. + err = au_xino_set_xib(sb, xino->file);
  32096. + if (!err)
  32097. + err = au_xigen_set(sb, xino->file);
  32098. + if (!err)
  32099. + err = au_xino_set_br(sb, xino->file);
  32100. + mutex_unlock(&dir->i_mutex);
  32101. + if (!err)
  32102. + goto out; /* success */
  32103. +
  32104. + /* reset all */
  32105. + AuIOErr("failed creating xino(%d).\n", err);
  32106. + au_xigen_clr(sb);
  32107. + xino_clear_xib(sb);
  32108. +
  32109. +out:
  32110. + dput(parent);
  32111. + return err;
  32112. +}
  32113. +
  32114. +/* ---------------------------------------------------------------------- */
  32115. +
  32116. +/*
  32117. + * create a xinofile at the default place/path.
  32118. + */
  32119. +struct file *au_xino_def(struct super_block *sb)
  32120. +{
  32121. + struct file *file;
  32122. + char *page, *p;
  32123. + struct au_branch *br;
  32124. + struct super_block *h_sb;
  32125. + struct path path;
  32126. + aufs_bindex_t bend, bindex, bwr;
  32127. +
  32128. + br = NULL;
  32129. + bend = au_sbend(sb);
  32130. + bwr = -1;
  32131. + for (bindex = 0; bindex <= bend; bindex++) {
  32132. + br = au_sbr(sb, bindex);
  32133. + if (au_br_writable(br->br_perm)
  32134. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  32135. + bwr = bindex;
  32136. + break;
  32137. + }
  32138. + }
  32139. +
  32140. + if (bwr >= 0) {
  32141. + file = ERR_PTR(-ENOMEM);
  32142. + page = (void *)__get_free_page(GFP_NOFS);
  32143. + if (unlikely(!page))
  32144. + goto out;
  32145. + path.mnt = au_br_mnt(br);
  32146. + path.dentry = au_h_dptr(sb->s_root, bwr);
  32147. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  32148. + file = (void *)p;
  32149. + if (!IS_ERR(p)) {
  32150. + strcat(p, "/" AUFS_XINO_FNAME);
  32151. + AuDbg("%s\n", p);
  32152. + file = au_xino_create(sb, p, /*silent*/0);
  32153. + if (!IS_ERR(file))
  32154. + au_xino_brid_set(sb, br->br_id);
  32155. + }
  32156. + free_page((unsigned long)page);
  32157. + } else {
  32158. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  32159. + if (IS_ERR(file))
  32160. + goto out;
  32161. + h_sb = file->f_path.dentry->d_sb;
  32162. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  32163. + pr_err("xino doesn't support %s(%s)\n",
  32164. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  32165. + fput(file);
  32166. + file = ERR_PTR(-EINVAL);
  32167. + }
  32168. + if (!IS_ERR(file))
  32169. + au_xino_brid_set(sb, -1);
  32170. + }
  32171. +
  32172. +out:
  32173. + return file;
  32174. +}
  32175. +
  32176. +/* ---------------------------------------------------------------------- */
  32177. +
  32178. +int au_xino_path(struct seq_file *seq, struct file *file)
  32179. +{
  32180. + int err;
  32181. +
  32182. + err = au_seq_path(seq, &file->f_path);
  32183. + if (unlikely(err < 0))
  32184. + goto out;
  32185. +
  32186. + err = 0;
  32187. +#define Deleted "\\040(deleted)"
  32188. + seq->count -= sizeof(Deleted) - 1;
  32189. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  32190. + sizeof(Deleted) - 1));
  32191. +#undef Deleted
  32192. +
  32193. +out:
  32194. + return err;
  32195. +}
  32196. diff -Nur linux-4.0.5.orig/fs/buffer.c linux-4.0.5/fs/buffer.c
  32197. --- linux-4.0.5.orig/fs/buffer.c 2015-06-06 10:21:22.000000000 -0500
  32198. +++ linux-4.0.5/fs/buffer.c 2015-06-21 14:08:25.130432449 -0500
  32199. @@ -2450,7 +2450,7 @@
  32200. * Update file times before taking page lock. We may end up failing the
  32201. * fault so this update may be superfluous but who really cares...
  32202. */
  32203. - file_update_time(vma->vm_file);
  32204. + vma_file_update_time(vma);
  32205. ret = __block_page_mkwrite(vma, vmf, get_block);
  32206. sb_end_pagefault(sb);
  32207. diff -Nur linux-4.0.5.orig/fs/dcache.c linux-4.0.5/fs/dcache.c
  32208. --- linux-4.0.5.orig/fs/dcache.c 2015-06-06 10:21:22.000000000 -0500
  32209. +++ linux-4.0.5/fs/dcache.c 2015-06-21 14:08:28.354432418 -0500
  32210. @@ -1130,7 +1130,7 @@
  32211. *
  32212. * The @enter() and @finish() callbacks are called with d_lock held.
  32213. */
  32214. -static void d_walk(struct dentry *parent, void *data,
  32215. +void d_walk(struct dentry *parent, void *data,
  32216. enum d_walk_ret (*enter)(void *, struct dentry *),
  32217. void (*finish)(void *))
  32218. {
  32219. @@ -1235,6 +1235,7 @@
  32220. seq = 1;
  32221. goto again;
  32222. }
  32223. +EXPORT_SYMBOL(d_walk);
  32224. /*
  32225. * Search for at least 1 mount point in the dentry's subdirs.
  32226. diff -Nur linux-4.0.5.orig/fs/file_table.c linux-4.0.5/fs/file_table.c
  32227. --- linux-4.0.5.orig/fs/file_table.c 2015-06-06 10:21:22.000000000 -0500
  32228. +++ linux-4.0.5/fs/file_table.c 2015-06-21 14:08:28.358432418 -0500
  32229. @@ -147,6 +147,7 @@
  32230. }
  32231. return ERR_PTR(-ENFILE);
  32232. }
  32233. +EXPORT_SYMBOL(get_empty_filp);
  32234. /**
  32235. * alloc_file - allocate and initialize a 'struct file'
  32236. @@ -308,6 +309,7 @@
  32237. file_free(file);
  32238. }
  32239. }
  32240. +EXPORT_SYMBOL(put_filp);
  32241. void __init files_init(unsigned long mempages)
  32242. {
  32243. diff -Nur linux-4.0.5.orig/fs/inode.c linux-4.0.5/fs/inode.c
  32244. --- linux-4.0.5.orig/fs/inode.c 2015-06-06 10:21:22.000000000 -0500
  32245. +++ linux-4.0.5/fs/inode.c 2015-06-21 14:08:28.358432418 -0500
  32246. @@ -58,6 +58,7 @@
  32247. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
  32248. __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
  32249. +EXPORT_SYMBOL(inode_sb_list_lock);
  32250. /*
  32251. * Empty aops. Can be used for the cases where the user does not
  32252. diff -Nur linux-4.0.5.orig/fs/Kconfig linux-4.0.5/fs/Kconfig
  32253. --- linux-4.0.5.orig/fs/Kconfig 2015-06-06 10:21:22.000000000 -0500
  32254. +++ linux-4.0.5/fs/Kconfig 2015-06-21 14:08:20.746432489 -0500
  32255. @@ -218,6 +218,7 @@
  32256. source "fs/ufs/Kconfig"
  32257. source "fs/exofs/Kconfig"
  32258. source "fs/f2fs/Kconfig"
  32259. +source "fs/aufs/Kconfig"
  32260. endif # MISC_FILESYSTEMS
  32261. diff -Nur linux-4.0.5.orig/fs/Makefile linux-4.0.5/fs/Makefile
  32262. --- linux-4.0.5.orig/fs/Makefile 2015-06-06 10:21:22.000000000 -0500
  32263. +++ linux-4.0.5/fs/Makefile 2015-06-21 14:08:20.746432489 -0500
  32264. @@ -126,3 +126,4 @@
  32265. obj-$(CONFIG_CEPH_FS) += ceph/
  32266. obj-$(CONFIG_PSTORE) += pstore/
  32267. obj-$(CONFIG_EFIVAR_FS) += efivarfs/
  32268. +obj-$(CONFIG_AUFS_FS) += aufs/
  32269. diff -Nur linux-4.0.5.orig/fs/namespace.c linux-4.0.5/fs/namespace.c
  32270. --- linux-4.0.5.orig/fs/namespace.c 2015-06-06 10:21:22.000000000 -0500
  32271. +++ linux-4.0.5/fs/namespace.c 2015-06-21 14:08:28.358432418 -0500
  32272. @@ -463,6 +463,7 @@
  32273. mnt_dec_writers(real_mount(mnt));
  32274. preempt_enable();
  32275. }
  32276. +EXPORT_SYMBOL_GPL(__mnt_drop_write);
  32277. /**
  32278. * mnt_drop_write - give up write access to a mount
  32279. @@ -1770,6 +1771,7 @@
  32280. }
  32281. return 0;
  32282. }
  32283. +EXPORT_SYMBOL(iterate_mounts);
  32284. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  32285. {
  32286. diff -Nur linux-4.0.5.orig/fs/notify/group.c linux-4.0.5/fs/notify/group.c
  32287. --- linux-4.0.5.orig/fs/notify/group.c 2015-06-06 10:21:22.000000000 -0500
  32288. +++ linux-4.0.5/fs/notify/group.c 2015-06-21 14:08:28.362432418 -0500
  32289. @@ -22,6 +22,7 @@
  32290. #include <linux/srcu.h>
  32291. #include <linux/rculist.h>
  32292. #include <linux/wait.h>
  32293. +#include <linux/module.h>
  32294. #include <linux/fsnotify_backend.h>
  32295. #include "fsnotify.h"
  32296. @@ -72,6 +73,7 @@
  32297. {
  32298. atomic_inc(&group->refcnt);
  32299. }
  32300. +EXPORT_SYMBOL(fsnotify_get_group);
  32301. /*
  32302. * Drop a reference to a group. Free it if it's through.
  32303. @@ -81,6 +83,7 @@
  32304. if (atomic_dec_and_test(&group->refcnt))
  32305. fsnotify_final_destroy_group(group);
  32306. }
  32307. +EXPORT_SYMBOL(fsnotify_put_group);
  32308. /*
  32309. * Create a new fsnotify_group and hold a reference for the group returned.
  32310. @@ -109,6 +112,7 @@
  32311. return group;
  32312. }
  32313. +EXPORT_SYMBOL(fsnotify_alloc_group);
  32314. int fsnotify_fasync(int fd, struct file *file, int on)
  32315. {
  32316. diff -Nur linux-4.0.5.orig/fs/notify/mark.c linux-4.0.5/fs/notify/mark.c
  32317. --- linux-4.0.5.orig/fs/notify/mark.c 2015-06-06 10:21:22.000000000 -0500
  32318. +++ linux-4.0.5/fs/notify/mark.c 2015-06-21 14:08:28.362432418 -0500
  32319. @@ -109,6 +109,7 @@
  32320. mark->free_mark(mark);
  32321. }
  32322. }
  32323. +EXPORT_SYMBOL(fsnotify_put_mark);
  32324. /* Calculate mask of events for a list of marks */
  32325. u32 fsnotify_recalc_mask(struct hlist_head *head)
  32326. @@ -202,6 +203,7 @@
  32327. fsnotify_destroy_mark_locked(mark, group);
  32328. mutex_unlock(&group->mark_mutex);
  32329. }
  32330. +EXPORT_SYMBOL(fsnotify_destroy_mark);
  32331. /*
  32332. * Destroy all marks in the given list. The marks must be already detached from
  32333. @@ -376,6 +378,7 @@
  32334. return ret;
  32335. }
  32336. +EXPORT_SYMBOL(fsnotify_add_mark);
  32337. int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
  32338. struct inode *inode, struct vfsmount *mnt, int allow_dups)
  32339. @@ -455,6 +458,7 @@
  32340. atomic_set(&mark->refcnt, 1);
  32341. mark->free_mark = free_mark;
  32342. }
  32343. +EXPORT_SYMBOL(fsnotify_init_mark);
  32344. static int fsnotify_mark_destroy(void *ignored)
  32345. {
  32346. diff -Nur linux-4.0.5.orig/fs/open.c linux-4.0.5/fs/open.c
  32347. --- linux-4.0.5.orig/fs/open.c 2015-06-06 10:21:22.000000000 -0500
  32348. +++ linux-4.0.5/fs/open.c 2015-06-21 14:08:28.362432418 -0500
  32349. @@ -62,6 +62,7 @@
  32350. mutex_unlock(&dentry->d_inode->i_mutex);
  32351. return ret;
  32352. }
  32353. +EXPORT_SYMBOL(do_truncate);
  32354. long vfs_truncate(struct path *path, loff_t length)
  32355. {
  32356. @@ -672,6 +673,7 @@
  32357. }
  32358. return 0;
  32359. }
  32360. +EXPORT_SYMBOL(open_check_o_direct);
  32361. static int do_dentry_open(struct file *f,
  32362. int (*open)(struct inode *, struct file *),
  32363. diff -Nur linux-4.0.5.orig/fs/proc/base.c linux-4.0.5/fs/proc/base.c
  32364. --- linux-4.0.5.orig/fs/proc/base.c 2015-06-06 10:21:22.000000000 -0500
  32365. +++ linux-4.0.5/fs/proc/base.c 2015-06-21 14:08:25.134432449 -0500
  32366. @@ -1735,7 +1735,7 @@
  32367. down_read(&mm->mmap_sem);
  32368. vma = find_exact_vma(mm, vm_start, vm_end);
  32369. if (vma && vma->vm_file) {
  32370. - *path = vma->vm_file->f_path;
  32371. + *path = vma_pr_or_file(vma)->f_path;
  32372. path_get(path);
  32373. rc = 0;
  32374. }
  32375. diff -Nur linux-4.0.5.orig/fs/proc/nommu.c linux-4.0.5/fs/proc/nommu.c
  32376. --- linux-4.0.5.orig/fs/proc/nommu.c 2015-06-06 10:21:22.000000000 -0500
  32377. +++ linux-4.0.5/fs/proc/nommu.c 2015-06-21 14:08:25.134432449 -0500
  32378. @@ -45,7 +45,10 @@
  32379. file = region->vm_file;
  32380. if (file) {
  32381. - struct inode *inode = file_inode(region->vm_file);
  32382. + struct inode *inode;
  32383. +
  32384. + file = vmr_pr_or_file(region);
  32385. + inode = file_inode(file);
  32386. dev = inode->i_sb->s_dev;
  32387. ino = inode->i_ino;
  32388. }
  32389. diff -Nur linux-4.0.5.orig/fs/proc/task_mmu.c linux-4.0.5/fs/proc/task_mmu.c
  32390. --- linux-4.0.5.orig/fs/proc/task_mmu.c 2015-06-06 10:21:22.000000000 -0500
  32391. +++ linux-4.0.5/fs/proc/task_mmu.c 2015-06-21 14:08:25.134432449 -0500
  32392. @@ -279,7 +279,10 @@
  32393. const char *name = NULL;
  32394. if (file) {
  32395. - struct inode *inode = file_inode(vma->vm_file);
  32396. + struct inode *inode;
  32397. +
  32398. + file = vma_pr_or_file(vma);
  32399. + inode = file_inode(file);
  32400. dev = inode->i_sb->s_dev;
  32401. ino = inode->i_ino;
  32402. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  32403. @@ -1479,7 +1482,7 @@
  32404. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  32405. struct vm_area_struct *vma = v;
  32406. struct numa_maps *md = &numa_priv->md;
  32407. - struct file *file = vma->vm_file;
  32408. + struct file *file = vma_pr_or_file(vma);
  32409. struct mm_struct *mm = vma->vm_mm;
  32410. struct mm_walk walk = {
  32411. .hugetlb_entry = gather_hugetlb_stats,
  32412. diff -Nur linux-4.0.5.orig/fs/proc/task_nommu.c linux-4.0.5/fs/proc/task_nommu.c
  32413. --- linux-4.0.5.orig/fs/proc/task_nommu.c 2015-06-06 10:21:22.000000000 -0500
  32414. +++ linux-4.0.5/fs/proc/task_nommu.c 2015-06-21 14:08:25.134432449 -0500
  32415. @@ -160,7 +160,10 @@
  32416. file = vma->vm_file;
  32417. if (file) {
  32418. - struct inode *inode = file_inode(vma->vm_file);
  32419. + struct inode *inode;
  32420. +
  32421. + file = vma_pr_or_file(vma);
  32422. + inode = file_inode(file);
  32423. dev = inode->i_sb->s_dev;
  32424. ino = inode->i_ino;
  32425. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  32426. diff -Nur linux-4.0.5.orig/fs/splice.c linux-4.0.5/fs/splice.c
  32427. --- linux-4.0.5.orig/fs/splice.c 2015-06-06 10:21:22.000000000 -0500
  32428. +++ linux-4.0.5/fs/splice.c 2015-06-21 14:08:28.362432418 -0500
  32429. @@ -1099,8 +1099,8 @@
  32430. /*
  32431. * Attempt to initiate a splice from pipe to file.
  32432. */
  32433. -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32434. - loff_t *ppos, size_t len, unsigned int flags)
  32435. +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32436. + loff_t *ppos, size_t len, unsigned int flags)
  32437. {
  32438. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  32439. loff_t *, size_t, unsigned int);
  32440. @@ -1112,13 +1112,14 @@
  32441. return splice_write(pipe, out, ppos, len, flags);
  32442. }
  32443. +EXPORT_SYMBOL(do_splice_from);
  32444. /*
  32445. * Attempt to initiate a splice from a file to a pipe.
  32446. */
  32447. -static long do_splice_to(struct file *in, loff_t *ppos,
  32448. - struct pipe_inode_info *pipe, size_t len,
  32449. - unsigned int flags)
  32450. +long do_splice_to(struct file *in, loff_t *ppos,
  32451. + struct pipe_inode_info *pipe, size_t len,
  32452. + unsigned int flags)
  32453. {
  32454. ssize_t (*splice_read)(struct file *, loff_t *,
  32455. struct pipe_inode_info *, size_t, unsigned int);
  32456. @@ -1138,6 +1139,7 @@
  32457. return splice_read(in, ppos, pipe, len, flags);
  32458. }
  32459. +EXPORT_SYMBOL(do_splice_to);
  32460. /**
  32461. * splice_direct_to_actor - splices data directly between two non-pipes
  32462. diff -Nur linux-4.0.5.orig/fs/xattr.c linux-4.0.5/fs/xattr.c
  32463. --- linux-4.0.5.orig/fs/xattr.c 2015-06-06 10:21:22.000000000 -0500
  32464. +++ linux-4.0.5/fs/xattr.c 2015-06-21 14:08:28.362432418 -0500
  32465. @@ -207,6 +207,7 @@
  32466. *xattr_value = value;
  32467. return error;
  32468. }
  32469. +EXPORT_SYMBOL(vfs_getxattr_alloc);
  32470. /* Compare an extended attribute value with the given value */
  32471. int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
  32472. diff -Nur linux-4.0.5.orig/include/linux/file.h linux-4.0.5/include/linux/file.h
  32473. --- linux-4.0.5.orig/include/linux/file.h 2015-06-06 10:21:22.000000000 -0500
  32474. +++ linux-4.0.5/include/linux/file.h 2015-06-21 14:08:17.150432522 -0500
  32475. @@ -19,6 +19,7 @@
  32476. struct path;
  32477. extern struct file *alloc_file(struct path *, fmode_t mode,
  32478. const struct file_operations *fop);
  32479. +extern struct file *get_empty_filp(void);
  32480. static inline void fput_light(struct file *file, int fput_needed)
  32481. {
  32482. diff -Nur linux-4.0.5.orig/include/linux/mm.h linux-4.0.5/include/linux/mm.h
  32483. --- linux-4.0.5.orig/include/linux/mm.h 2015-06-06 10:21:22.000000000 -0500
  32484. +++ linux-4.0.5/include/linux/mm.h 2015-06-21 14:08:25.138432449 -0500
  32485. @@ -1254,6 +1254,28 @@
  32486. }
  32487. #endif
  32488. +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
  32489. +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
  32490. + int);
  32491. +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
  32492. +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
  32493. +
  32494. +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
  32495. + __LINE__)
  32496. +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
  32497. + __LINE__)
  32498. +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
  32499. +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
  32500. +
  32501. +#ifndef CONFIG_MMU
  32502. +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
  32503. +extern void vmr_do_fput(struct vm_region *, const char[], int);
  32504. +
  32505. +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
  32506. + __LINE__)
  32507. +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
  32508. +#endif /* !CONFIG_MMU */
  32509. +
  32510. extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
  32511. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  32512. void *buf, int len, int write);
  32513. diff -Nur linux-4.0.5.orig/include/linux/mm_types.h linux-4.0.5/include/linux/mm_types.h
  32514. --- linux-4.0.5.orig/include/linux/mm_types.h 2015-06-06 10:21:22.000000000 -0500
  32515. +++ linux-4.0.5/include/linux/mm_types.h 2015-06-21 14:08:25.138432449 -0500
  32516. @@ -241,6 +241,7 @@
  32517. unsigned long vm_top; /* region allocated to here */
  32518. unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
  32519. struct file *vm_file; /* the backing file or NULL */
  32520. + struct file *vm_prfile; /* the virtual backing file or NULL */
  32521. int vm_usage; /* region usage count (access under nommu_region_sem) */
  32522. bool vm_icache_flushed : 1; /* true if the icache has been flushed for
  32523. @@ -305,6 +306,7 @@
  32524. unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
  32525. units, *not* PAGE_CACHE_SIZE */
  32526. struct file * vm_file; /* File we map to (can be NULL). */
  32527. + struct file *vm_prfile; /* shadow of vm_file */
  32528. void * vm_private_data; /* was vm_pte (shared mem) */
  32529. #ifndef CONFIG_MMU
  32530. diff -Nur linux-4.0.5.orig/include/linux/splice.h linux-4.0.5/include/linux/splice.h
  32531. --- linux-4.0.5.orig/include/linux/splice.h 2015-06-06 10:21:22.000000000 -0500
  32532. +++ linux-4.0.5/include/linux/splice.h 2015-06-21 14:08:17.150432522 -0500
  32533. @@ -83,4 +83,10 @@
  32534. extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
  32535. extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
  32536. +
  32537. +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32538. + loff_t *ppos, size_t len, unsigned int flags);
  32539. +extern long do_splice_to(struct file *in, loff_t *ppos,
  32540. + struct pipe_inode_info *pipe, size_t len,
  32541. + unsigned int flags);
  32542. #endif
  32543. diff -Nur linux-4.0.5.orig/include/uapi/linux/aufs_type.h linux-4.0.5/include/uapi/linux/aufs_type.h
  32544. --- linux-4.0.5.orig/include/uapi/linux/aufs_type.h 1969-12-31 18:00:00.000000000 -0600
  32545. +++ linux-4.0.5/include/uapi/linux/aufs_type.h 2015-06-21 14:09:29.158432082 -0500
  32546. @@ -0,0 +1,419 @@
  32547. +/*
  32548. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  32549. + *
  32550. + * This program, aufs is free software; you can redistribute it and/or modify
  32551. + * it under the terms of the GNU General Public License as published by
  32552. + * the Free Software Foundation; either version 2 of the License, or
  32553. + * (at your option) any later version.
  32554. + *
  32555. + * This program is distributed in the hope that it will be useful,
  32556. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32557. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32558. + * GNU General Public License for more details.
  32559. + *
  32560. + * You should have received a copy of the GNU General Public License
  32561. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32562. + */
  32563. +
  32564. +#ifndef __AUFS_TYPE_H__
  32565. +#define __AUFS_TYPE_H__
  32566. +
  32567. +#define AUFS_NAME "aufs"
  32568. +
  32569. +#ifdef __KERNEL__
  32570. +/*
  32571. + * define it before including all other headers.
  32572. + * sched.h may use pr_* macros before defining "current", so define the
  32573. + * no-current version first, and re-define later.
  32574. + */
  32575. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  32576. +#include <linux/sched.h>
  32577. +#undef pr_fmt
  32578. +#define pr_fmt(fmt) \
  32579. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  32580. + (int)sizeof(current->comm), current->comm, current->pid
  32581. +#else
  32582. +#include <stdint.h>
  32583. +#include <sys/types.h>
  32584. +#endif /* __KERNEL__ */
  32585. +
  32586. +#include <linux/limits.h>
  32587. +
  32588. +#define AUFS_VERSION "4.0-20150622"
  32589. +
  32590. +/* todo? move this to linux-2.6.19/include/magic.h */
  32591. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  32592. +
  32593. +/* ---------------------------------------------------------------------- */
  32594. +
  32595. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  32596. +typedef int8_t aufs_bindex_t;
  32597. +#define AUFS_BRANCH_MAX 127
  32598. +#else
  32599. +typedef int16_t aufs_bindex_t;
  32600. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  32601. +#define AUFS_BRANCH_MAX 511
  32602. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  32603. +#define AUFS_BRANCH_MAX 1023
  32604. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  32605. +#define AUFS_BRANCH_MAX 32767
  32606. +#endif
  32607. +#endif
  32608. +
  32609. +#ifdef __KERNEL__
  32610. +#ifndef AUFS_BRANCH_MAX
  32611. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  32612. +#endif
  32613. +#endif /* __KERNEL__ */
  32614. +
  32615. +/* ---------------------------------------------------------------------- */
  32616. +
  32617. +#define AUFS_FSTYPE AUFS_NAME
  32618. +
  32619. +#define AUFS_ROOT_INO 2
  32620. +#define AUFS_FIRST_INO 11
  32621. +
  32622. +#define AUFS_WH_PFX ".wh."
  32623. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  32624. +#define AUFS_WH_TMP_LEN 4
  32625. +/* a limit for rmdir/rename a dir and copyup */
  32626. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  32627. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  32628. + - 1 /* dot */\
  32629. + - AUFS_WH_TMP_LEN) /* hex */
  32630. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  32631. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  32632. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  32633. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  32634. +#define AUFS_DIRWH_DEF 3
  32635. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  32636. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  32637. +#define AUFS_RDBLK_DEF 512 /* bytes */
  32638. +#define AUFS_RDHASH_DEF 32
  32639. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  32640. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  32641. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  32642. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  32643. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  32644. +
  32645. +/* pseudo-link maintenace under /proc */
  32646. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  32647. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  32648. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  32649. +
  32650. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  32651. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  32652. +
  32653. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  32654. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  32655. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  32656. +
  32657. +/* doubly whiteouted */
  32658. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  32659. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  32660. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  32661. +
  32662. +/* branch permissions and attributes */
  32663. +#define AUFS_BRPERM_RW "rw"
  32664. +#define AUFS_BRPERM_RO "ro"
  32665. +#define AUFS_BRPERM_RR "rr"
  32666. +#define AUFS_BRATTR_COO_REG "coo_reg"
  32667. +#define AUFS_BRATTR_COO_ALL "coo_all"
  32668. +#define AUFS_BRATTR_FHSM "fhsm"
  32669. +#define AUFS_BRATTR_UNPIN "unpin"
  32670. +#define AUFS_BRATTR_ICEX "icex"
  32671. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  32672. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  32673. +#define AUFS_BRATTR_ICEX_TR "icextr"
  32674. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  32675. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  32676. +#define AUFS_BRRATTR_WH "wh"
  32677. +#define AUFS_BRWATTR_NLWH "nolwh"
  32678. +#define AUFS_BRWATTR_MOO "moo"
  32679. +
  32680. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  32681. +#define AuBrPerm_RO (1 << 1) /* readonly */
  32682. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  32683. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  32684. +
  32685. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  32686. +#define AuBrAttr_COO_ALL (1 << 4)
  32687. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  32688. +
  32689. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  32690. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  32691. + branch. meaningless since
  32692. + linux-3.18-rc1 */
  32693. +
  32694. +/* ignore error in copying XATTR */
  32695. +#define AuBrAttr_ICEX_SEC (1 << 7)
  32696. +#define AuBrAttr_ICEX_SYS (1 << 8)
  32697. +#define AuBrAttr_ICEX_TR (1 << 9)
  32698. +#define AuBrAttr_ICEX_USR (1 << 10)
  32699. +#define AuBrAttr_ICEX_OTH (1 << 11)
  32700. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  32701. + | AuBrAttr_ICEX_SYS \
  32702. + | AuBrAttr_ICEX_TR \
  32703. + | AuBrAttr_ICEX_USR \
  32704. + | AuBrAttr_ICEX_OTH)
  32705. +
  32706. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  32707. +#define AuBrRAttr_Mask AuBrRAttr_WH
  32708. +
  32709. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  32710. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  32711. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  32712. +
  32713. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  32714. +
  32715. +/* #warning test userspace */
  32716. +#ifdef __KERNEL__
  32717. +#ifndef CONFIG_AUFS_FHSM
  32718. +#undef AuBrAttr_FHSM
  32719. +#define AuBrAttr_FHSM 0
  32720. +#endif
  32721. +#ifndef CONFIG_AUFS_XATTR
  32722. +#undef AuBrAttr_ICEX
  32723. +#define AuBrAttr_ICEX 0
  32724. +#undef AuBrAttr_ICEX_SEC
  32725. +#define AuBrAttr_ICEX_SEC 0
  32726. +#undef AuBrAttr_ICEX_SYS
  32727. +#define AuBrAttr_ICEX_SYS 0
  32728. +#undef AuBrAttr_ICEX_TR
  32729. +#define AuBrAttr_ICEX_TR 0
  32730. +#undef AuBrAttr_ICEX_USR
  32731. +#define AuBrAttr_ICEX_USR 0
  32732. +#undef AuBrAttr_ICEX_OTH
  32733. +#define AuBrAttr_ICEX_OTH 0
  32734. +#endif
  32735. +#endif
  32736. +
  32737. +/* the longest combination */
  32738. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  32739. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  32740. + "+" AUFS_BRATTR_COO_REG \
  32741. + "+" AUFS_BRATTR_FHSM \
  32742. + "+" AUFS_BRATTR_UNPIN \
  32743. + "+" AUFS_BRATTR_ICEX_SEC \
  32744. + "+" AUFS_BRATTR_ICEX_SYS \
  32745. + "+" AUFS_BRATTR_ICEX_USR \
  32746. + "+" AUFS_BRATTR_ICEX_OTH \
  32747. + "+" AUFS_BRWATTR_NLWH)
  32748. +
  32749. +typedef struct {
  32750. + char a[AuBrPermStrSz];
  32751. +} au_br_perm_str_t;
  32752. +
  32753. +static inline int au_br_writable(int brperm)
  32754. +{
  32755. + return brperm & AuBrPerm_RW;
  32756. +}
  32757. +
  32758. +static inline int au_br_whable(int brperm)
  32759. +{
  32760. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  32761. +}
  32762. +
  32763. +static inline int au_br_wh_linkable(int brperm)
  32764. +{
  32765. + return !(brperm & AuBrWAttr_NoLinkWH);
  32766. +}
  32767. +
  32768. +static inline int au_br_cmoo(int brperm)
  32769. +{
  32770. + return brperm & AuBrAttr_CMOO_Mask;
  32771. +}
  32772. +
  32773. +static inline int au_br_fhsm(int brperm)
  32774. +{
  32775. + return brperm & AuBrAttr_FHSM;
  32776. +}
  32777. +
  32778. +/* ---------------------------------------------------------------------- */
  32779. +
  32780. +/* ioctl */
  32781. +enum {
  32782. + /* readdir in userspace */
  32783. + AuCtl_RDU,
  32784. + AuCtl_RDU_INO,
  32785. +
  32786. + AuCtl_WBR_FD, /* pathconf wrapper */
  32787. + AuCtl_IBUSY, /* busy inode */
  32788. + AuCtl_MVDOWN, /* move-down */
  32789. + AuCtl_BR, /* info about branches */
  32790. + AuCtl_FHSM_FD /* connection for fhsm */
  32791. +};
  32792. +
  32793. +/* borrowed from linux/include/linux/kernel.h */
  32794. +#ifndef ALIGN
  32795. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  32796. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  32797. +#endif
  32798. +
  32799. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  32800. +#ifndef __aligned
  32801. +#define __aligned(x) __attribute__((aligned(x)))
  32802. +#endif
  32803. +
  32804. +#ifdef __KERNEL__
  32805. +#ifndef __packed
  32806. +#define __packed __attribute__((packed))
  32807. +#endif
  32808. +#endif
  32809. +
  32810. +struct au_rdu_cookie {
  32811. + uint64_t h_pos;
  32812. + int16_t bindex;
  32813. + uint8_t flags;
  32814. + uint8_t pad;
  32815. + uint32_t generation;
  32816. +} __aligned(8);
  32817. +
  32818. +struct au_rdu_ent {
  32819. + uint64_t ino;
  32820. + int16_t bindex;
  32821. + uint8_t type;
  32822. + uint8_t nlen;
  32823. + uint8_t wh;
  32824. + char name[0];
  32825. +} __aligned(8);
  32826. +
  32827. +static inline int au_rdu_len(int nlen)
  32828. +{
  32829. + /* include the terminating NULL */
  32830. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  32831. + sizeof(uint64_t));
  32832. +}
  32833. +
  32834. +union au_rdu_ent_ul {
  32835. + struct au_rdu_ent __user *e;
  32836. + uint64_t ul;
  32837. +};
  32838. +
  32839. +enum {
  32840. + AufsCtlRduV_SZ,
  32841. + AufsCtlRduV_End
  32842. +};
  32843. +
  32844. +struct aufs_rdu {
  32845. + /* input */
  32846. + union {
  32847. + uint64_t sz; /* AuCtl_RDU */
  32848. + uint64_t nent; /* AuCtl_RDU_INO */
  32849. + };
  32850. + union au_rdu_ent_ul ent;
  32851. + uint16_t verify[AufsCtlRduV_End];
  32852. +
  32853. + /* input/output */
  32854. + uint32_t blk;
  32855. +
  32856. + /* output */
  32857. + union au_rdu_ent_ul tail;
  32858. + /* number of entries which were added in a single call */
  32859. + uint64_t rent;
  32860. + uint8_t full;
  32861. + uint8_t shwh;
  32862. +
  32863. + struct au_rdu_cookie cookie;
  32864. +} __aligned(8);
  32865. +
  32866. +/* ---------------------------------------------------------------------- */
  32867. +
  32868. +struct aufs_wbr_fd {
  32869. + uint32_t oflags;
  32870. + int16_t brid;
  32871. +} __aligned(8);
  32872. +
  32873. +/* ---------------------------------------------------------------------- */
  32874. +
  32875. +struct aufs_ibusy {
  32876. + uint64_t ino, h_ino;
  32877. + int16_t bindex;
  32878. +} __aligned(8);
  32879. +
  32880. +/* ---------------------------------------------------------------------- */
  32881. +
  32882. +/* error code for move-down */
  32883. +/* the actual message strings are implemented in aufs-util.git */
  32884. +enum {
  32885. + EAU_MVDOWN_OPAQUE = 1,
  32886. + EAU_MVDOWN_WHITEOUT,
  32887. + EAU_MVDOWN_UPPER,
  32888. + EAU_MVDOWN_BOTTOM,
  32889. + EAU_MVDOWN_NOUPPER,
  32890. + EAU_MVDOWN_NOLOWERBR,
  32891. + EAU_Last
  32892. +};
  32893. +
  32894. +/* flags for move-down */
  32895. +#define AUFS_MVDOWN_DMSG 1
  32896. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  32897. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  32898. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  32899. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  32900. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  32901. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  32902. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  32903. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  32904. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  32905. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  32906. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  32907. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  32908. +
  32909. +/* index for move-down */
  32910. +enum {
  32911. + AUFS_MVDOWN_UPPER,
  32912. + AUFS_MVDOWN_LOWER,
  32913. + AUFS_MVDOWN_NARRAY
  32914. +};
  32915. +
  32916. +/*
  32917. + * additional info of move-down
  32918. + * number of free blocks and inodes.
  32919. + * subset of struct kstatfs, but smaller and always 64bit.
  32920. + */
  32921. +struct aufs_stfs {
  32922. + uint64_t f_blocks;
  32923. + uint64_t f_bavail;
  32924. + uint64_t f_files;
  32925. + uint64_t f_ffree;
  32926. +};
  32927. +
  32928. +struct aufs_stbr {
  32929. + int16_t brid; /* optional input */
  32930. + int16_t bindex; /* output */
  32931. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  32932. +} __aligned(8);
  32933. +
  32934. +struct aufs_mvdown {
  32935. + uint32_t flags; /* input/output */
  32936. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  32937. + int8_t au_errno; /* output */
  32938. +} __aligned(8);
  32939. +
  32940. +/* ---------------------------------------------------------------------- */
  32941. +
  32942. +union aufs_brinfo {
  32943. + /* PATH_MAX may differ between kernel-space and user-space */
  32944. + char _spacer[4096];
  32945. + struct {
  32946. + int16_t id;
  32947. + int perm;
  32948. + char path[0];
  32949. + };
  32950. +} __aligned(8);
  32951. +
  32952. +/* ---------------------------------------------------------------------- */
  32953. +
  32954. +#define AuCtlType 'A'
  32955. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  32956. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  32957. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  32958. + struct aufs_wbr_fd)
  32959. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  32960. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  32961. + struct aufs_mvdown)
  32962. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  32963. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  32964. +
  32965. +#endif /* __AUFS_TYPE_H__ */
  32966. diff -Nur linux-4.0.5.orig/include/uapi/linux/Kbuild linux-4.0.5/include/uapi/linux/Kbuild
  32967. --- linux-4.0.5.orig/include/uapi/linux/Kbuild 2015-06-06 10:21:22.000000000 -0500
  32968. +++ linux-4.0.5/include/uapi/linux/Kbuild 2015-06-21 14:08:20.746432489 -0500
  32969. @@ -58,6 +58,7 @@
  32970. header-y += atm_tcp.h
  32971. header-y += atm_zatm.h
  32972. header-y += audit.h
  32973. +header-y += aufs_type.h
  32974. header-y += auto_fs4.h
  32975. header-y += auto_fs.h
  32976. header-y += auxvec.h
  32977. diff -Nur linux-4.0.5.orig/kernel/fork.c linux-4.0.5/kernel/fork.c
  32978. --- linux-4.0.5.orig/kernel/fork.c 2015-06-06 10:21:22.000000000 -0500
  32979. +++ linux-4.0.5/kernel/fork.c 2015-06-21 14:08:25.138432449 -0500
  32980. @@ -430,7 +430,7 @@
  32981. struct inode *inode = file_inode(file);
  32982. struct address_space *mapping = file->f_mapping;
  32983. - get_file(file);
  32984. + vma_get_file(tmp);
  32985. if (tmp->vm_flags & VM_DENYWRITE)
  32986. atomic_dec(&inode->i_writecount);
  32987. i_mmap_lock_write(mapping);
  32988. diff -Nur linux-4.0.5.orig/MAINTAINERS linux-4.0.5/MAINTAINERS
  32989. --- linux-4.0.5.orig/MAINTAINERS 2015-06-06 10:21:22.000000000 -0500
  32990. +++ linux-4.0.5/MAINTAINERS 2015-06-21 14:08:17.142432521 -0500
  32991. @@ -1864,6 +1864,19 @@
  32992. F: include/uapi/linux/audit.h
  32993. F: kernel/audit*
  32994. +AUFS (advanced multi layered unification filesystem) FILESYSTEM
  32995. +M: "J. R. Okajima" <hooanon05g@gmail.com>
  32996. +L: linux-unionfs@vger.kernel.org
  32997. +L: aufs-users@lists.sourceforge.net (members only)
  32998. +W: http://aufs.sourceforge.net
  32999. +T: git://github.com/sfjro/aufs4-linux.git
  33000. +S: Supported
  33001. +F: Documentation/filesystems/aufs/
  33002. +F: Documentation/ABI/testing/debugfs-aufs
  33003. +F: Documentation/ABI/testing/sysfs-aufs
  33004. +F: fs/aufs/
  33005. +F: include/uapi/linux/aufs_type.h
  33006. +
  33007. AUXILIARY DISPLAY DRIVERS
  33008. M: Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
  33009. W: http://miguelojeda.es/auxdisplay.htm
  33010. diff -Nur linux-4.0.5.orig/mm/filemap.c linux-4.0.5/mm/filemap.c
  33011. --- linux-4.0.5.orig/mm/filemap.c 2015-06-06 10:21:22.000000000 -0500
  33012. +++ linux-4.0.5/mm/filemap.c 2015-06-21 14:08:25.142432449 -0500
  33013. @@ -2064,7 +2064,7 @@
  33014. int ret = VM_FAULT_LOCKED;
  33015. sb_start_pagefault(inode->i_sb);
  33016. - file_update_time(vma->vm_file);
  33017. + vma_file_update_time(vma);
  33018. lock_page(page);
  33019. if (page->mapping != inode->i_mapping) {
  33020. unlock_page(page);
  33021. diff -Nur linux-4.0.5.orig/mm/madvise.c linux-4.0.5/mm/madvise.c
  33022. --- linux-4.0.5.orig/mm/madvise.c 2015-06-06 10:21:22.000000000 -0500
  33023. +++ linux-4.0.5/mm/madvise.c 2015-06-21 14:08:25.142432449 -0500
  33024. @@ -320,12 +320,12 @@
  33025. * vma's reference to the file) can go away as soon as we drop
  33026. * mmap_sem.
  33027. */
  33028. - get_file(f);
  33029. + vma_get_file(vma);
  33030. up_read(&current->mm->mmap_sem);
  33031. error = vfs_fallocate(f,
  33032. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  33033. offset, end - start);
  33034. - fput(f);
  33035. + vma_fput(vma);
  33036. down_read(&current->mm->mmap_sem);
  33037. return error;
  33038. }
  33039. diff -Nur linux-4.0.5.orig/mm/Makefile linux-4.0.5/mm/Makefile
  33040. --- linux-4.0.5.orig/mm/Makefile 2015-06-06 10:21:22.000000000 -0500
  33041. +++ linux-4.0.5/mm/Makefile 2015-06-21 14:08:25.142432449 -0500
  33042. @@ -21,7 +21,7 @@
  33043. mm_init.o mmu_context.o percpu.o slab_common.o \
  33044. compaction.o vmacache.o \
  33045. interval_tree.o list_lru.o workingset.o \
  33046. - debug.o $(mmu-y)
  33047. + prfile.o debug.o $(mmu-y)
  33048. obj-y += init-mm.o
  33049. diff -Nur linux-4.0.5.orig/mm/memory.c linux-4.0.5/mm/memory.c
  33050. --- linux-4.0.5.orig/mm/memory.c 2015-06-06 10:21:22.000000000 -0500
  33051. +++ linux-4.0.5/mm/memory.c 2015-06-21 14:08:25.142432449 -0500
  33052. @@ -2132,7 +2132,7 @@
  33053. }
  33054. if (!page_mkwrite)
  33055. - file_update_time(vma->vm_file);
  33056. + vma_file_update_time(vma);
  33057. }
  33058. return ret;
  33059. diff -Nur linux-4.0.5.orig/mm/mmap.c linux-4.0.5/mm/mmap.c
  33060. --- linux-4.0.5.orig/mm/mmap.c 2015-06-06 10:21:22.000000000 -0500
  33061. +++ linux-4.0.5/mm/mmap.c 2015-06-21 14:08:25.142432449 -0500
  33062. @@ -274,7 +274,7 @@
  33063. if (vma->vm_ops && vma->vm_ops->close)
  33064. vma->vm_ops->close(vma);
  33065. if (vma->vm_file)
  33066. - fput(vma->vm_file);
  33067. + vma_fput(vma);
  33068. mpol_put(vma_policy(vma));
  33069. kmem_cache_free(vm_area_cachep, vma);
  33070. return next;
  33071. @@ -886,7 +886,7 @@
  33072. if (remove_next) {
  33073. if (file) {
  33074. uprobe_munmap(next, next->vm_start, next->vm_end);
  33075. - fput(file);
  33076. + vma_fput(vma);
  33077. }
  33078. if (next->anon_vma)
  33079. anon_vma_merge(vma, next);
  33080. @@ -1671,8 +1671,8 @@
  33081. return addr;
  33082. unmap_and_free_vma:
  33083. + vma_fput(vma);
  33084. vma->vm_file = NULL;
  33085. - fput(file);
  33086. /* Undo any partial mapping done by a device driver. */
  33087. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  33088. @@ -2473,7 +2473,7 @@
  33089. goto out_free_mpol;
  33090. if (new->vm_file)
  33091. - get_file(new->vm_file);
  33092. + vma_get_file(new);
  33093. if (new->vm_ops && new->vm_ops->open)
  33094. new->vm_ops->open(new);
  33095. @@ -2492,7 +2492,7 @@
  33096. if (new->vm_ops && new->vm_ops->close)
  33097. new->vm_ops->close(new);
  33098. if (new->vm_file)
  33099. - fput(new->vm_file);
  33100. + vma_fput(new);
  33101. unlink_anon_vmas(new);
  33102. out_free_mpol:
  33103. mpol_put(vma_policy(new));
  33104. @@ -2635,7 +2635,6 @@
  33105. struct vm_area_struct *vma;
  33106. unsigned long populate = 0;
  33107. unsigned long ret = -EINVAL;
  33108. - struct file *file;
  33109. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
  33110. "See Documentation/vm/remap_file_pages.txt.\n",
  33111. @@ -2679,10 +2678,10 @@
  33112. munlock_vma_pages_range(vma, start, start + size);
  33113. }
  33114. - file = get_file(vma->vm_file);
  33115. + vma_get_file(vma);
  33116. ret = do_mmap_pgoff(vma->vm_file, start, size,
  33117. prot, flags, pgoff, &populate);
  33118. - fput(file);
  33119. + vma_fput(vma);
  33120. out:
  33121. up_write(&mm->mmap_sem);
  33122. if (populate)
  33123. @@ -2950,7 +2949,7 @@
  33124. if (anon_vma_clone(new_vma, vma))
  33125. goto out_free_mempol;
  33126. if (new_vma->vm_file)
  33127. - get_file(new_vma->vm_file);
  33128. + vma_get_file(new_vma);
  33129. if (new_vma->vm_ops && new_vma->vm_ops->open)
  33130. new_vma->vm_ops->open(new_vma);
  33131. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  33132. diff -Nur linux-4.0.5.orig/mm/msync.c linux-4.0.5/mm/msync.c
  33133. --- linux-4.0.5.orig/mm/msync.c 2015-06-06 10:21:22.000000000 -0500
  33134. +++ linux-4.0.5/mm/msync.c 2015-06-21 14:08:25.142432449 -0500
  33135. @@ -84,10 +84,10 @@
  33136. start = vma->vm_end;
  33137. if ((flags & MS_SYNC) && file &&
  33138. (vma->vm_flags & VM_SHARED)) {
  33139. - get_file(file);
  33140. + vma_get_file(vma);
  33141. up_read(&mm->mmap_sem);
  33142. error = vfs_fsync_range(file, fstart, fend, 1);
  33143. - fput(file);
  33144. + vma_fput(vma);
  33145. if (error || start >= end)
  33146. goto out;
  33147. down_read(&mm->mmap_sem);
  33148. diff -Nur linux-4.0.5.orig/mm/nommu.c linux-4.0.5/mm/nommu.c
  33149. --- linux-4.0.5.orig/mm/nommu.c 2015-06-06 10:21:22.000000000 -0500
  33150. +++ linux-4.0.5/mm/nommu.c 2015-06-21 14:08:25.142432449 -0500
  33151. @@ -693,7 +693,7 @@
  33152. up_write(&nommu_region_sem);
  33153. if (region->vm_file)
  33154. - fput(region->vm_file);
  33155. + vmr_fput(region);
  33156. /* IO memory and memory shared directly out of the pagecache
  33157. * from ramfs/tmpfs mustn't be released here */
  33158. @@ -858,7 +858,7 @@
  33159. if (vma->vm_ops && vma->vm_ops->close)
  33160. vma->vm_ops->close(vma);
  33161. if (vma->vm_file)
  33162. - fput(vma->vm_file);
  33163. + vma_fput(vma);
  33164. put_nommu_region(vma->vm_region);
  33165. kmem_cache_free(vm_area_cachep, vma);
  33166. }
  33167. @@ -1398,7 +1398,7 @@
  33168. goto error_just_free;
  33169. }
  33170. }
  33171. - fput(region->vm_file);
  33172. + vmr_fput(region);
  33173. kmem_cache_free(vm_region_jar, region);
  33174. region = pregion;
  33175. result = start;
  33176. @@ -1474,10 +1474,10 @@
  33177. up_write(&nommu_region_sem);
  33178. error:
  33179. if (region->vm_file)
  33180. - fput(region->vm_file);
  33181. + vmr_fput(region);
  33182. kmem_cache_free(vm_region_jar, region);
  33183. if (vma->vm_file)
  33184. - fput(vma->vm_file);
  33185. + vma_fput(vma);
  33186. kmem_cache_free(vm_area_cachep, vma);
  33187. kleave(" = %d", ret);
  33188. return ret;
  33189. diff -Nur linux-4.0.5.orig/mm/prfile.c linux-4.0.5/mm/prfile.c
  33190. --- linux-4.0.5.orig/mm/prfile.c 1969-12-31 18:00:00.000000000 -0600
  33191. +++ linux-4.0.5/mm/prfile.c 2015-06-21 14:08:25.142432449 -0500
  33192. @@ -0,0 +1,86 @@
  33193. +/*
  33194. + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
  33195. + * in /proc/PID/maps.
  33196. + * Call these functions via macros defined in linux/mm.h.
  33197. + *
  33198. + * See Documentation/filesystems/aufs/design/06mmap.txt
  33199. + *
  33200. + * Copyright (c) 2014 Junjro R. Okajima
  33201. + * Copyright (c) 2014 Ian Campbell
  33202. + */
  33203. +
  33204. +#include <linux/mm.h>
  33205. +#include <linux/file.h>
  33206. +#include <linux/fs.h>
  33207. +
  33208. +/* #define PRFILE_TRACE */
  33209. +static inline void prfile_trace(struct file *f, struct file *pr,
  33210. + const char func[], int line, const char func2[])
  33211. +{
  33212. +#ifdef PRFILE_TRACE
  33213. + if (pr)
  33214. + pr_info("%s:%d: %s, %p\n", func, line, func2,
  33215. + f ? (char *)f->f_path.dentry->d_name.name : "(null)");
  33216. +#endif
  33217. +}
  33218. +
  33219. +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
  33220. + int line)
  33221. +{
  33222. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33223. +
  33224. + prfile_trace(f, pr, func, line, __func__);
  33225. + file_update_time(f);
  33226. + if (f && pr)
  33227. + file_update_time(pr);
  33228. +}
  33229. +
  33230. +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
  33231. + int line)
  33232. +{
  33233. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33234. +
  33235. + prfile_trace(f, pr, func, line, __func__);
  33236. + return (f && pr) ? pr : f;
  33237. +}
  33238. +
  33239. +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
  33240. +{
  33241. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33242. +
  33243. + prfile_trace(f, pr, func, line, __func__);
  33244. + get_file(f);
  33245. + if (f && pr)
  33246. + get_file(pr);
  33247. +}
  33248. +
  33249. +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
  33250. +{
  33251. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  33252. +
  33253. + prfile_trace(f, pr, func, line, __func__);
  33254. + fput(f);
  33255. + if (f && pr)
  33256. + fput(pr);
  33257. +}
  33258. +
  33259. +#ifndef CONFIG_MMU
  33260. +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
  33261. + int line)
  33262. +{
  33263. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  33264. +
  33265. + prfile_trace(f, pr, func, line, __func__);
  33266. + return (f && pr) ? pr : f;
  33267. +}
  33268. +
  33269. +void vmr_do_fput(struct vm_region *region, const char func[], int line)
  33270. +{
  33271. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  33272. +
  33273. + prfile_trace(f, pr, func, line, __func__);
  33274. + fput(f);
  33275. + if (f && pr)
  33276. + fput(pr);
  33277. +}
  33278. +#endif /* !CONFIG_MMU */
  33279. diff -Nur linux-4.0.5.orig/security/commoncap.c linux-4.0.5/security/commoncap.c
  33280. --- linux-4.0.5.orig/security/commoncap.c 2015-06-06 10:21:22.000000000 -0500
  33281. +++ linux-4.0.5/security/commoncap.c 2015-06-21 14:08:28.366432418 -0500
  33282. @@ -975,9 +975,11 @@
  33283. }
  33284. return ret;
  33285. }
  33286. +EXPORT_SYMBOL(cap_mmap_addr);
  33287. int cap_mmap_file(struct file *file, unsigned long reqprot,
  33288. unsigned long prot, unsigned long flags)
  33289. {
  33290. return 0;
  33291. }
  33292. +EXPORT_SYMBOL(cap_mmap_file);
  33293. diff -Nur linux-4.0.5.orig/security/device_cgroup.c linux-4.0.5/security/device_cgroup.c
  33294. --- linux-4.0.5.orig/security/device_cgroup.c 2015-06-06 10:21:22.000000000 -0500
  33295. +++ linux-4.0.5/security/device_cgroup.c 2015-06-21 14:08:28.366432418 -0500
  33296. @@ -7,6 +7,7 @@
  33297. #include <linux/device_cgroup.h>
  33298. #include <linux/cgroup.h>
  33299. #include <linux/ctype.h>
  33300. +#include <linux/export.h>
  33301. #include <linux/list.h>
  33302. #include <linux/uaccess.h>
  33303. #include <linux/seq_file.h>
  33304. @@ -849,6 +850,7 @@
  33305. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  33306. access);
  33307. }
  33308. +EXPORT_SYMBOL(__devcgroup_inode_permission);
  33309. int devcgroup_inode_mknod(int mode, dev_t dev)
  33310. {
  33311. diff -Nur linux-4.0.5.orig/security/security.c linux-4.0.5/security/security.c
  33312. --- linux-4.0.5.orig/security/security.c 2015-06-06 10:21:22.000000000 -0500
  33313. +++ linux-4.0.5/security/security.c 2015-06-21 14:08:28.366432418 -0500
  33314. @@ -430,6 +430,7 @@
  33315. return 0;
  33316. return security_ops->path_rmdir(dir, dentry);
  33317. }
  33318. +EXPORT_SYMBOL(security_path_rmdir);
  33319. int security_path_unlink(struct path *dir, struct dentry *dentry)
  33320. {
  33321. @@ -446,6 +447,7 @@
  33322. return 0;
  33323. return security_ops->path_symlink(dir, dentry, old_name);
  33324. }
  33325. +EXPORT_SYMBOL(security_path_symlink);
  33326. int security_path_link(struct dentry *old_dentry, struct path *new_dir,
  33327. struct dentry *new_dentry)
  33328. @@ -454,6 +456,7 @@
  33329. return 0;
  33330. return security_ops->path_link(old_dentry, new_dir, new_dentry);
  33331. }
  33332. +EXPORT_SYMBOL(security_path_link);
  33333. int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
  33334. struct path *new_dir, struct dentry *new_dentry,
  33335. @@ -481,6 +484,7 @@
  33336. return 0;
  33337. return security_ops->path_truncate(path);
  33338. }
  33339. +EXPORT_SYMBOL(security_path_truncate);
  33340. int security_path_chmod(struct path *path, umode_t mode)
  33341. {
  33342. @@ -488,6 +492,7 @@
  33343. return 0;
  33344. return security_ops->path_chmod(path, mode);
  33345. }
  33346. +EXPORT_SYMBOL(security_path_chmod);
  33347. int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
  33348. {
  33349. @@ -495,6 +500,7 @@
  33350. return 0;
  33351. return security_ops->path_chown(path, uid, gid);
  33352. }
  33353. +EXPORT_SYMBOL(security_path_chown);
  33354. int security_path_chroot(struct path *path)
  33355. {
  33356. @@ -580,6 +586,7 @@
  33357. return 0;
  33358. return security_ops->inode_readlink(dentry);
  33359. }
  33360. +EXPORT_SYMBOL(security_inode_readlink);
  33361. int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
  33362. {
  33363. @@ -594,6 +601,7 @@
  33364. return 0;
  33365. return security_ops->inode_permission(inode, mask);
  33366. }
  33367. +EXPORT_SYMBOL(security_inode_permission);
  33368. int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
  33369. {
  33370. @@ -716,6 +724,7 @@
  33371. return fsnotify_perm(file, mask);
  33372. }
  33373. +EXPORT_SYMBOL(security_file_permission);
  33374. int security_file_alloc(struct file *file)
  33375. {
  33376. @@ -775,6 +784,7 @@
  33377. return ret;
  33378. return ima_file_mmap(file, prot);
  33379. }
  33380. +EXPORT_SYMBOL(security_mmap_file);
  33381. int security_mmap_addr(unsigned long addr)
  33382. {