aufs4.4-20160912.patch 886 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931
  1. --- /dev/null
  2. +++ linux-4.4/Documentation/ABI/testing/debugfs-aufs 2016-10-23 11:20:57.612159953 +0300
  3. @@ -0,0 +1,50 @@
  4. +What: /debug/aufs/si_<id>/
  5. +Date: March 2009
  6. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  7. +Description:
  8. + Under /debug/aufs, a directory named si_<id> is created
  9. + per aufs mount, where <id> is a unique id generated
  10. + internally.
  11. +
  12. +What: /debug/aufs/si_<id>/plink
  13. +Date: Apr 2013
  14. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  15. +Description:
  16. + It has three lines and shows the information about the
  17. + pseudo-link. The first line is a single number
  18. + representing a number of buckets. The second line is a
  19. + number of pseudo-links per buckets (separated by a
  20. + blank). The last line is a single number representing a
  21. + total number of psedo-links.
  22. + When the aufs mount option 'noplink' is specified, it
  23. + will show "1\n0\n0\n".
  24. +
  25. +What: /debug/aufs/si_<id>/xib
  26. +Date: March 2009
  27. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  28. +Description:
  29. + It shows the consumed blocks by xib (External Inode Number
  30. + Bitmap), its block size and file size.
  31. + When the aufs mount option 'noxino' is specified, it
  32. + will be empty. About XINO files, see the aufs manual.
  33. +
  34. +What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
  35. +Date: March 2009
  36. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  37. +Description:
  38. + It shows the consumed blocks by xino (External Inode Number
  39. + Translation Table), its link count, block size and file
  40. + size.
  41. + When the aufs mount option 'noxino' is specified, it
  42. + will be empty. About XINO files, see the aufs manual.
  43. +
  44. +What: /debug/aufs/si_<id>/xigen
  45. +Date: March 2009
  46. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  47. +Description:
  48. + It shows the consumed blocks by xigen (External Inode
  49. + Generation Table), its block size and file size.
  50. + If CONFIG_AUFS_EXPORT is disabled, this entry will not
  51. + be created.
  52. + When the aufs mount option 'noxino' is specified, it
  53. + will be empty. About XINO files, see the aufs manual.
  54. --- /dev/null
  55. +++ linux-4.4/Documentation/ABI/testing/sysfs-aufs 2016-10-23 11:20:57.612159953 +0300
  56. @@ -0,0 +1,31 @@
  57. +What: /sys/fs/aufs/si_<id>/
  58. +Date: March 2009
  59. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  60. +Description:
  61. + Under /sys/fs/aufs, a directory named si_<id> is created
  62. + per aufs mount, where <id> is a unique id generated
  63. + internally.
  64. +
  65. +What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
  66. +Date: March 2009
  67. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  68. +Description:
  69. + It shows the abolute path of a member directory (which
  70. + is called branch) in aufs, and its permission.
  71. +
  72. +What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
  73. +Date: July 2013
  74. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  75. +Description:
  76. + It shows the id of a member directory (which is called
  77. + branch) in aufs.
  78. +
  79. +What: /sys/fs/aufs/si_<id>/xi_path
  80. +Date: March 2009
  81. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  82. +Description:
  83. + It shows the abolute path of XINO (External Inode Number
  84. + Bitmap, Translation Table and Generation Table) file
  85. + even if it is the default path.
  86. + When the aufs mount option 'noxino' is specified, it
  87. + will be empty. About XINO files, see the aufs manual.
  88. --- /dev/null
  89. +++ linux-4.4/Documentation/filesystems/aufs/design/01intro.txt 2016-10-23 11:20:57.615493286 +0300
  90. @@ -0,0 +1,170 @@
  91. +
  92. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  93. +#
  94. +# This program is free software; you can redistribute it and/or modify
  95. +# it under the terms of the GNU General Public License as published by
  96. +# the Free Software Foundation; either version 2 of the License, or
  97. +# (at your option) any later version.
  98. +#
  99. +# This program is distributed in the hope that it will be useful,
  100. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  101. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  102. +# GNU General Public License for more details.
  103. +#
  104. +# You should have received a copy of the GNU General Public License
  105. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  106. +
  107. +Introduction
  108. +----------------------------------------
  109. +
  110. +aufs [ei ju: ef es] | [a u f s]
  111. +1. abbrev. for "advanced multi-layered unification filesystem".
  112. +2. abbrev. for "another unionfs".
  113. +3. abbrev. for "auf das" in German which means "on the" in English.
  114. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  115. + But "Filesystem aufs Filesystem" is hard to understand.
  116. +
  117. +AUFS is a filesystem with features:
  118. +- multi layered stackable unification filesystem, the member directory
  119. + is called as a branch.
  120. +- branch permission and attribute, 'readonly', 'real-readonly',
  121. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  122. + combination.
  123. +- internal "file copy-on-write".
  124. +- logical deletion, whiteout.
  125. +- dynamic branch manipulation, adding, deleting and changing permission.
  126. +- allow bypassing aufs, user's direct branch access.
  127. +- external inode number translation table and bitmap which maintains the
  128. + persistent aufs inode number.
  129. +- seekable directory, including NFS readdir.
  130. +- file mapping, mmap and sharing pages.
  131. +- pseudo-link, hardlink over branches.
  132. +- loopback mounted filesystem as a branch.
  133. +- several policies to select one among multiple writable branches.
  134. +- revert a single systemcall when an error occurs in aufs.
  135. +- and more...
  136. +
  137. +
  138. +Multi Layered Stackable Unification Filesystem
  139. +----------------------------------------------------------------------
  140. +Most people already knows what it is.
  141. +It is a filesystem which unifies several directories and provides a
  142. +merged single directory. When users access a file, the access will be
  143. +passed/re-directed/converted (sorry, I am not sure which English word is
  144. +correct) to the real file on the member filesystem. The member
  145. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  146. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  147. +readonly branch is handled by creating 'whiteout' on the upper writable
  148. +branch.
  149. +
  150. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  151. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  152. +different approaches to implement the merged-view.
  153. +The former tries putting it into VFS, and the latter implements as a
  154. +separate filesystem.
  155. +(If I misunderstand about these implementations, please let me know and
  156. +I shall correct it. Because it is a long time ago when I read their
  157. +source files last time).
  158. +
  159. +UnionMount's approach will be able to small, but may be hard to share
  160. +branches between several UnionMount since the whiteout in it is
  161. +implemented in the inode on branch filesystem and always
  162. +shared. According to Bharata's post, readdir does not seems to be
  163. +finished yet.
  164. +There are several missing features known in this implementations such as
  165. +- for users, the inode number may change silently. eg. copy-up.
  166. +- link(2) may break by copy-up.
  167. +- read(2) may get an obsoleted filedata (fstat(2) too).
  168. +- fcntl(F_SETLK) may be broken by copy-up.
  169. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  170. + open(O_RDWR).
  171. +
  172. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  173. +merged into mainline. This is another implementation of UnionMount as a
  174. +separated filesystem. All the limitations and known problems which
  175. +UnionMount are equally inherited to "overlay" filesystem.
  176. +
  177. +Unionfs has a longer history. When I started implementing a stackable
  178. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  179. +inode, dentry and file objects and they have an array pointing lower
  180. +same kind objects. After contributing many patches for Unionfs, I
  181. +re-started my project AUFS (Jun 2006).
  182. +
  183. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  184. +implemented my own ideas, approaches and enhancements and it became
  185. +totally different one.
  186. +
  187. +Comparing DM snapshot and fs based implementation
  188. +- the number of bytes to be copied between devices is much smaller.
  189. +- the type of filesystem must be one and only.
  190. +- the fs must be writable, no readonly fs, even for the lower original
  191. + device. so the compression fs will not be usable. but if we use
  192. + loopback mount, we may address this issue.
  193. + for instance,
  194. + mount /cdrom/squashfs.img /sq
  195. + losetup /sq/ext2.img
  196. + losetup /somewhere/cow
  197. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  198. +- it will be difficult (or needs more operations) to extract the
  199. + difference between the original device and COW.
  200. +- DM snapshot-merge may help a lot when users try merging. in the
  201. + fs-layer union, users will use rsync(1).
  202. +
  203. +You may want to read my old paper "Filesystems in LiveCD"
  204. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  205. +
  206. +
  207. +Several characters/aspects/persona of aufs
  208. +----------------------------------------------------------------------
  209. +
  210. +Aufs has several characters, aspects or persona.
  211. +1. a filesystem, callee of VFS helper
  212. +2. sub-VFS, caller of VFS helper for branches
  213. +3. a virtual filesystem which maintains persistent inode number
  214. +4. reader/writer of files on branches such like an application
  215. +
  216. +1. Callee of VFS Helper
  217. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  218. +unlink(2) from an application reaches sys_unlink() kernel function and
  219. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  220. +calls filesystem specific unlink operation. Actually aufs implements the
  221. +unlink operation but it behaves like a redirector.
  222. +
  223. +2. Caller of VFS Helper for Branches
  224. +aufs_unlink() passes the unlink request to the branch filesystem as if
  225. +it were called from VFS. So the called unlink operation of the branch
  226. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  227. +every necessary pre/post operation for the branch filesystem.
  228. +- acquire the lock for the parent dir on a branch
  229. +- lookup in a branch
  230. +- revalidate dentry on a branch
  231. +- mnt_want_write() for a branch
  232. +- vfs_unlink() for a branch
  233. +- mnt_drop_write() for a branch
  234. +- release the lock on a branch
  235. +
  236. +3. Persistent Inode Number
  237. +One of the most important issue for a filesystem is to maintain inode
  238. +numbers. This is particularly important to support exporting a
  239. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  240. +backend block device for its own. But some storage is necessary to
  241. +keep and maintain the inode numbers. It may be a large space and may not
  242. +suit to keep in memory. Aufs rents some space from its first writable
  243. +branch filesystem (by default) and creates file(s) on it. These files
  244. +are created by aufs internally and removed soon (currently) keeping
  245. +opened.
  246. +Note: Because these files are removed, they are totally gone after
  247. + unmounting aufs. It means the inode numbers are not persistent
  248. + across unmount or reboot. I have a plan to make them really
  249. + persistent which will be important for aufs on NFS server.
  250. +
  251. +4. Read/Write Files Internally (copy-on-write)
  252. +Because a branch can be readonly, when you write a file on it, aufs will
  253. +"copy-up" it to the upper writable branch internally. And then write the
  254. +originally requested thing to the file. Generally kernel doesn't
  255. +open/read/write file actively. In aufs, even a single write may cause a
  256. +internal "file copy". This behaviour is very similar to cp(1) command.
  257. +
  258. +Some people may think it is better to pass such work to user space
  259. +helper, instead of doing in kernel space. Actually I am still thinking
  260. +about it. But currently I have implemented it in kernel space.
  261. --- /dev/null
  262. +++ linux-4.4/Documentation/filesystems/aufs/design/02struct.txt 2016-10-23 11:20:57.615493286 +0300
  263. @@ -0,0 +1,258 @@
  264. +
  265. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  266. +#
  267. +# This program is free software; you can redistribute it and/or modify
  268. +# it under the terms of the GNU General Public License as published by
  269. +# the Free Software Foundation; either version 2 of the License, or
  270. +# (at your option) any later version.
  271. +#
  272. +# This program is distributed in the hope that it will be useful,
  273. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  274. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  275. +# GNU General Public License for more details.
  276. +#
  277. +# You should have received a copy of the GNU General Public License
  278. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  279. +
  280. +Basic Aufs Internal Structure
  281. +
  282. +Superblock/Inode/Dentry/File Objects
  283. +----------------------------------------------------------------------
  284. +As like an ordinary filesystem, aufs has its own
  285. +superblock/inode/dentry/file objects. All these objects have a
  286. +dynamically allocated array and store the same kind of pointers to the
  287. +lower filesystem, branch.
  288. +For example, when you build a union with one readwrite branch and one
  289. +readonly, mounted /au, /rw and /ro respectively.
  290. +- /au = /rw + /ro
  291. +- /ro/fileA exists but /rw/fileA
  292. +
  293. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  294. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  295. +- [0] = NULL (because /rw/fileA doesn't exist)
  296. +- [1] = /ro/fileA
  297. +
  298. +This style of an array is essentially same to the aufs
  299. +superblock/inode/dentry/file objects.
  300. +
  301. +Because aufs supports manipulating branches, ie. add/delete/change
  302. +branches dynamically, these objects has its own generation. When
  303. +branches are changed, the generation in aufs superblock is
  304. +incremented. And a generation in other object are compared when it is
  305. +accessed. When a generation in other objects are obsoleted, aufs
  306. +refreshes the internal array.
  307. +
  308. +
  309. +Superblock
  310. +----------------------------------------------------------------------
  311. +Additionally aufs superblock has some data for policies to select one
  312. +among multiple writable branches, XIB files, pseudo-links and kobject.
  313. +See below in detail.
  314. +About the policies which supports copy-down a directory, see
  315. +wbr_policy.txt too.
  316. +
  317. +
  318. +Branch and XINO(External Inode Number Translation Table)
  319. +----------------------------------------------------------------------
  320. +Every branch has its own xino (external inode number translation table)
  321. +file. The xino file is created and unlinked by aufs internally. When two
  322. +members of a union exist on the same filesystem, they share the single
  323. +xino file.
  324. +The struct of a xino file is simple, just a sequence of aufs inode
  325. +numbers which is indexed by the lower inode number.
  326. +In the above sample, assume the inode number of /ro/fileA is i111 and
  327. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  328. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  329. +
  330. +When the inode numbers are not contiguous, the xino file will be sparse
  331. +which has a hole in it and doesn't consume as much disk space as it
  332. +might appear. If your branch filesystem consumes disk space for such
  333. +holes, then you should specify 'xino=' option at mounting aufs.
  334. +
  335. +Aufs has a mount option to free the disk blocks for such holes in XINO
  336. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  337. +meet a problem of disk shortage due to XINO files, then you should try
  338. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  339. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  340. +the holes in XINO files.
  341. +
  342. +Also a writable branch has three kinds of "whiteout bases". All these
  343. +are existed when the branch is joined to aufs, and their names are
  344. +whiteout-ed doubly, so that users will never see their names in aufs
  345. +hierarchy.
  346. +1. a regular file which will be hardlinked to all whiteouts.
  347. +2. a directory to store a pseudo-link.
  348. +3. a directory to store an "orphan"-ed file temporary.
  349. +
  350. +1. Whiteout Base
  351. + When you remove a file on a readonly branch, aufs handles it as a
  352. + logical deletion and creates a whiteout on the upper writable branch
  353. + as a hardlink of this file in order not to consume inode on the
  354. + writable branch.
  355. +2. Pseudo-link Dir
  356. + See below, Pseudo-link.
  357. +3. Step-Parent Dir
  358. + When "fileC" exists on the lower readonly branch only and it is
  359. + opened and removed with its parent dir, and then user writes
  360. + something into it, then aufs copies-up fileC to this
  361. + directory. Because there is no other dir to store fileC. After
  362. + creating a file under this dir, the file is unlinked.
  363. +
  364. +Because aufs supports manipulating branches, ie. add/delete/change
  365. +dynamically, a branch has its own id. When the branch order changes,
  366. +aufs finds the new index by searching the branch id.
  367. +
  368. +
  369. +Pseudo-link
  370. +----------------------------------------------------------------------
  371. +Assume "fileA" exists on the lower readonly branch only and it is
  372. +hardlinked to "fileB" on the branch. When you write something to fileA,
  373. +aufs copies-up it to the upper writable branch. Additionally aufs
  374. +creates a hardlink under the Pseudo-link Directory of the writable
  375. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  376. +simple list. If fileB is read after unlinking fileA, aufs returns
  377. +filedata from the pseudo-link instead of the lower readonly
  378. +branch. Because the pseudo-link is based upon the inode, to keep the
  379. +inode number by xino (see above) is essentially necessary.
  380. +
  381. +All the hardlinks under the Pseudo-link Directory of the writable branch
  382. +should be restored in a proper location later. Aufs provides a utility
  383. +to do this. The userspace helpers executed at remounting and unmounting
  384. +aufs by default.
  385. +During this utility is running, it puts aufs into the pseudo-link
  386. +maintenance mode. In this mode, only the process which began the
  387. +maintenance mode (and its child processes) is allowed to operate in
  388. +aufs. Some other processes which are not related to the pseudo-link will
  389. +be allowed to run too, but the rest have to return an error or wait
  390. +until the maintenance mode ends. If a process already acquires an inode
  391. +mutex (in VFS), it has to return an error.
  392. +
  393. +
  394. +XIB(external inode number bitmap)
  395. +----------------------------------------------------------------------
  396. +Addition to the xino file per a branch, aufs has an external inode number
  397. +bitmap in a superblock object. It is also an internal file such like a
  398. +xino file.
  399. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  400. +not.
  401. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  402. +
  403. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  404. +reduce the number of consumed disk blocks for these files.
  405. +
  406. +
  407. +Virtual or Vertical Dir, and Readdir in Userspace
  408. +----------------------------------------------------------------------
  409. +In order to support multiple layers (branches), aufs readdir operation
  410. +constructs a virtual dir block on memory. For readdir, aufs calls
  411. +vfs_readdir() internally for each dir on branches, merges their entries
  412. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  413. +object. So the file object has its entry list until it is closed. The
  414. +entry list will be updated when the file position is zero and becomes
  415. +obsoleted. This decision is made in aufs automatically.
  416. +
  417. +The dynamically allocated memory block for the name of entries has a
  418. +unit of 512 bytes (by default) and stores the names contiguously (no
  419. +padding). Another block for each entry is handled by kmem_cache too.
  420. +During building dir blocks, aufs creates hash list and judging whether
  421. +the entry is whiteouted by its upper branch or already listed.
  422. +The merged result is cached in the corresponding inode object and
  423. +maintained by a customizable life-time option.
  424. +
  425. +Some people may call it can be a security hole or invite DoS attack
  426. +since the opened and once readdir-ed dir (file object) holds its entry
  427. +list and becomes a pressure for system memory. But I'd say it is similar
  428. +to files under /proc or /sys. The virtual files in them also holds a
  429. +memory page (generally) while they are opened. When an idea to reduce
  430. +memory for them is introduced, it will be applied to aufs too.
  431. +For those who really hate this situation, I've developed readdir(3)
  432. +library which operates this merging in userspace. You just need to set
  433. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  434. +kernel space for readdir(3).
  435. +
  436. +
  437. +Workqueue
  438. +----------------------------------------------------------------------
  439. +Aufs sometimes requires privilege access to a branch. For instance,
  440. +in copy-up/down operation. When a user process is going to make changes
  441. +to a file which exists in the lower readonly branch only, and the mode
  442. +of one of ancestor directories may not be writable by a user
  443. +process. Here aufs copy-up the file with its ancestors and they may
  444. +require privilege to set its owner/group/mode/etc.
  445. +This is a typical case of a application character of aufs (see
  446. +Introduction).
  447. +
  448. +Aufs uses workqueue synchronously for this case. It creates its own
  449. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  450. +passes the request to call mkdir or write (for example), and wait for
  451. +its completion. This approach solves a problem of a signal handler
  452. +simply.
  453. +If aufs didn't adopt the workqueue and changed the privilege of the
  454. +process, then the process may receive the unexpected SIGXFSZ or other
  455. +signals.
  456. +
  457. +Also aufs uses the system global workqueue ("events" kernel thread) too
  458. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  459. +whiteout base and etc. This is unrelated to a privilege.
  460. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  461. +superblock at the beginning, at the same time waits for the completion
  462. +of all queued asynchronous tasks.
  463. +
  464. +
  465. +Whiteout
  466. +----------------------------------------------------------------------
  467. +The whiteout in aufs is very similar to Unionfs's. That is represented
  468. +by its filename. UnionMount takes an approach of a file mode, but I am
  469. +afraid several utilities (find(1) or something) will have to support it.
  470. +
  471. +Basically the whiteout represents "logical deletion" which stops aufs to
  472. +lookup further, but also it represents "dir is opaque" which also stop
  473. +further lookup.
  474. +
  475. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  476. +In order to make several functions in a single systemcall to be
  477. +revertible, aufs adopts an approach to rename a directory to a temporary
  478. +unique whiteouted name.
  479. +For example, in rename(2) dir where the target dir already existed, aufs
  480. +renames the target dir to a temporary unique whiteouted name before the
  481. +actual rename on a branch, and then handles other actions (make it opaque,
  482. +update the attributes, etc). If an error happens in these actions, aufs
  483. +simply renames the whiteouted name back and returns an error. If all are
  484. +succeeded, aufs registers a function to remove the whiteouted unique
  485. +temporary name completely and asynchronously to the system global
  486. +workqueue.
  487. +
  488. +
  489. +Copy-up
  490. +----------------------------------------------------------------------
  491. +It is a well-known feature or concept.
  492. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  493. +internally and makes change to the new file on the upper writable branch.
  494. +When the trigger systemcall does not update the timestamps of the parent
  495. +dir, aufs reverts it after copy-up.
  496. +
  497. +
  498. +Move-down (aufs3.9 and later)
  499. +----------------------------------------------------------------------
  500. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  501. +the lower readonly branch to the upper writable branch when a user
  502. +changes something about the file.
  503. +"Move-down" is an opposite action of copy-up. Basically this action is
  504. +ran manually instead of automatically and internally.
  505. +For desgin and implementation, aufs has to consider these issues.
  506. +- whiteout for the file may exist on the lower branch.
  507. +- ancestor directories may not exist on the lower branch.
  508. +- diropq for the ancestor directories may exist on the upper branch.
  509. +- free space on the lower branch will reduce.
  510. +- another access to the file may happen during moving-down, including
  511. + UDBA (see "Revalidate Dentry and UDBA").
  512. +- the file should not be hard-linked nor pseudo-linked. they should be
  513. + handled by auplink utility later.
  514. +
  515. +Sometimes users want to move-down a file from the upper writable branch
  516. +to the lower readonly or writable branch. For instance,
  517. +- the free space of the upper writable branch is going to run out.
  518. +- create a new intermediate branch between the upper and lower branch.
  519. +- etc.
  520. +
  521. +For this purpose, use "aumvdown" command in aufs-util.git.
  522. --- /dev/null
  523. +++ linux-4.4/Documentation/filesystems/aufs/design/03atomic_open.txt 2016-10-23 11:20:57.615493286 +0300
  524. @@ -0,0 +1,85 @@
  525. +
  526. +# Copyright (C) 2015-2016 Junjiro R. Okajima
  527. +#
  528. +# This program is free software; you can redistribute it and/or modify
  529. +# it under the terms of the GNU General Public License as published by
  530. +# the Free Software Foundation; either version 2 of the License, or
  531. +# (at your option) any later version.
  532. +#
  533. +# This program is distributed in the hope that it will be useful,
  534. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  535. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  536. +# GNU General Public License for more details.
  537. +#
  538. +# You should have received a copy of the GNU General Public License
  539. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  540. +
  541. +Support for a branch who has its ->atomic_open()
  542. +----------------------------------------------------------------------
  543. +The filesystems who implement its ->atomic_open() are not majority. For
  544. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  545. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  546. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  547. +sure whether all filesystems who have ->atomic_open() behave like this,
  548. +but NFSv4 surely returns the error.
  549. +
  550. +In order to support ->atomic_open() for aufs, there are a few
  551. +approaches.
  552. +
  553. +A. Introduce aufs_atomic_open()
  554. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  555. + branch fs.
  556. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  557. + an aufs user Pip Cet's approach
  558. + - calls aufs_create(), VFS finish_open() and notify_change().
  559. + - pass fake-mode to finish_open(), and then correct the mode by
  560. + notify_change().
  561. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  562. + - no aufs_atomic_open().
  563. + - aufs_lookup() registers the TID to an aufs internal object.
  564. + - aufs_create() does nothing when the matching TID is registered, but
  565. + registers the mode.
  566. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  567. + TID is registered.
  568. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  569. + credential
  570. + - no aufs_atomic_open().
  571. + - aufs_create() registers the TID to an internal object. this info
  572. + represents "this process created this file just now."
  573. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  574. + registered TID and re-try open() with superuser's credential.
  575. +
  576. +Pros and cons for each approach.
  577. +
  578. +A.
  579. + - straightforward but highly depends upon VFS internal.
  580. + - the atomic behavaiour is kept.
  581. + - some of parameters such as nameidata are hard to reproduce for
  582. + branch fs.
  583. + - large overhead.
  584. +B.
  585. + - easy to implement.
  586. + - the atomic behavaiour is lost.
  587. +C.
  588. + - the atomic behavaiour is kept.
  589. + - dirty and tricky.
  590. + - VFS checks whether the file is created correctly after calling
  591. + ->create(), which means this approach doesn't work.
  592. +D.
  593. + - easy to implement.
  594. + - the atomic behavaiour is lost.
  595. + - to open a file with superuser's credential and give it to a user
  596. + process is a bad idea, since the file object keeps the credential
  597. + in it. It may affect LSM or something. This approach doesn't work
  598. + either.
  599. +
  600. +The approach A is ideal, but it hard to implement. So here is a
  601. +variation of A, which is to be implemented.
  602. +
  603. +A-1. Introduce aufs_atomic_open()
  604. + - calls branch fs ->atomic_open() if exists. otherwise calls
  605. + vfs_create() and finish_open().
  606. + - the demerit is that the several checks after branch fs
  607. + ->atomic_open() are lost. in the ordinary case, the checks are
  608. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  609. + be implemented in aufs, but not all I am afraid.
  610. --- /dev/null
  611. +++ linux-4.4/Documentation/filesystems/aufs/design/03lookup.txt 2016-10-23 11:20:57.615493286 +0300
  612. @@ -0,0 +1,113 @@
  613. +
  614. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  615. +#
  616. +# This program is free software; you can redistribute it and/or modify
  617. +# it under the terms of the GNU General Public License as published by
  618. +# the Free Software Foundation; either version 2 of the License, or
  619. +# (at your option) any later version.
  620. +#
  621. +# This program is distributed in the hope that it will be useful,
  622. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  623. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  624. +# GNU General Public License for more details.
  625. +#
  626. +# You should have received a copy of the GNU General Public License
  627. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  628. +
  629. +Lookup in a Branch
  630. +----------------------------------------------------------------------
  631. +Since aufs has a character of sub-VFS (see Introduction), it operates
  632. +lookup for branches as VFS does. It may be a heavy work. But almost all
  633. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  634. +directly connected to its parent. Digging down the directory hierarchy
  635. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  636. +aufs calls it.
  637. +
  638. +When a branch is a remote filesystem, aufs basically relies upon its
  639. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  640. +them.
  641. +For d_revalidate, aufs implements three levels of revalidate tests. See
  642. +"Revalidate Dentry and UDBA" in detail.
  643. +
  644. +
  645. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  646. +----------------------------------------------------------------------
  647. +Let's try case study.
  648. +- aufs has two branches, upper readwrite and lower readonly.
  649. + /au = /rw + /ro
  650. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  651. +- user invoked "chmod a+rx /au/dirA"
  652. +- the internal copy-up is activated and "/rw/dirA" is created and its
  653. + permission bits are set to world readable.
  654. +- then "/au/dirA" becomes world readable?
  655. +
  656. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  657. +or it may be a natively readonly filesystem. If aufs respects the lower
  658. +branch, it should not respond readdir request from other users. But user
  659. +allowed it by chmod. Should really aufs rejects showing the entries
  660. +under /ro/dirA?
  661. +
  662. +To be honest, I don't have a good solution for this case. So aufs
  663. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  664. +users.
  665. +When dirperm1 is specified, aufs checks only the highest one for the
  666. +directory permission, and shows the entries. Otherwise, as usual, checks
  667. +every dir existing on all branches and rejects the request.
  668. +
  669. +As a side effect, dirperm1 option improves the performance of aufs
  670. +because the number of permission check is reduced when the number of
  671. +branch is many.
  672. +
  673. +
  674. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  675. +----------------------------------------------------------------------
  676. +Generally VFS helpers re-validate a dentry as a part of lookup.
  677. +0. digging down the directory hierarchy.
  678. +1. lock the parent dir by its i_mutex.
  679. +2. lookup the final (child) entry.
  680. +3. revalidate it.
  681. +4. call the actual operation (create, unlink, etc.)
  682. +5. unlock the parent dir
  683. +
  684. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  685. +called. Actually aufs implements it and checks the dentry on a branch is
  686. +still valid.
  687. +But it is not enough. Because aufs has to release the lock for the
  688. +parent dir on a branch at the end of ->lookup() (step 2) and
  689. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  690. +held by VFS.
  691. +If the file on a branch is changed directly, eg. bypassing aufs, after
  692. +aufs released the lock, then the subsequent operation may cause
  693. +something unpleasant result.
  694. +
  695. +This situation is a result of VFS architecture, ->lookup() and
  696. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  697. +design from VFS's point of view. It is just not suitable for sub-VFS
  698. +character in aufs.
  699. +
  700. +Aufs supports such case by three level of revalidation which is
  701. +selectable by user.
  702. +1. Simple Revalidate
  703. + Addition to the native flow in VFS's, confirm the child-parent
  704. + relationship on the branch just after locking the parent dir on the
  705. + branch in the "actual operation" (step 4). When this validation
  706. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  707. + checks the validation of the dentry on branches.
  708. +2. Monitor Changes Internally by Inotify/Fsnotify
  709. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  710. + the dentry on the branch, and returns EBUSY if it finds different
  711. + dentry.
  712. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  713. + during it is in cache. When the event is notified, aufs registers a
  714. + function to kernel 'events' thread by schedule_work(). And the
  715. + function sets some special status to the cached aufs dentry and inode
  716. + private data. If they are not cached, then aufs has nothing to
  717. + do. When the same file is accessed through aufs (step 0-3) later,
  718. + aufs will detect the status and refresh all necessary data.
  719. + In this mode, aufs has to ignore the event which is fired by aufs
  720. + itself.
  721. +3. No Extra Validation
  722. + This is the simplest test and doesn't add any additional revalidation
  723. + test, and skip the revalidation in step 4. It is useful and improves
  724. + aufs performance when system surely hide the aufs branches from user,
  725. + by over-mounting something (or another method).
  726. --- /dev/null
  727. +++ linux-4.4/Documentation/filesystems/aufs/design/04branch.txt 2016-10-23 11:20:57.615493286 +0300
  728. @@ -0,0 +1,74 @@
  729. +
  730. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  731. +#
  732. +# This program is free software; you can redistribute it and/or modify
  733. +# it under the terms of the GNU General Public License as published by
  734. +# the Free Software Foundation; either version 2 of the License, or
  735. +# (at your option) any later version.
  736. +#
  737. +# This program is distributed in the hope that it will be useful,
  738. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  739. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  740. +# GNU General Public License for more details.
  741. +#
  742. +# You should have received a copy of the GNU General Public License
  743. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  744. +
  745. +Branch Manipulation
  746. +
  747. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  748. +and changing its permission/attribute, there are a lot of works to do.
  749. +
  750. +
  751. +Add a Branch
  752. +----------------------------------------------------------------------
  753. +o Confirm the adding dir exists outside of aufs, including loopback
  754. + mount, and its various attributes.
  755. +o Initialize the xino file and whiteout bases if necessary.
  756. + See struct.txt.
  757. +
  758. +o Check the owner/group/mode of the directory
  759. + When the owner/group/mode of the adding directory differs from the
  760. + existing branch, aufs issues a warning because it may impose a
  761. + security risk.
  762. + For example, when a upper writable branch has a world writable empty
  763. + top directory, a malicious user can create any files on the writable
  764. + branch directly, like copy-up and modify manually. If something like
  765. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  766. + writable branch, and the writable branch is world-writable, then a
  767. + malicious guy may create /etc/passwd on the writable branch directly
  768. + and the infected file will be valid in aufs.
  769. + I am afraid it can be a security issue, but aufs can do nothing except
  770. + producing a warning.
  771. +
  772. +
  773. +Delete a Branch
  774. +----------------------------------------------------------------------
  775. +o Confirm the deleting branch is not busy
  776. + To be general, there is one merit to adopt "remount" interface to
  777. + manipulate branches. It is to discard caches. At deleting a branch,
  778. + aufs checks the still cached (and connected) dentries and inodes. If
  779. + there are any, then they are all in-use. An inode without its
  780. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  781. +
  782. + For the cached one, aufs checks whether the same named entry exists on
  783. + other branches.
  784. + If the cached one is a directory, because aufs provides a merged view
  785. + to users, as long as one dir is left on any branch aufs can show the
  786. + dir to users. In this case, the branch can be removed from aufs.
  787. + Otherwise aufs rejects deleting the branch.
  788. +
  789. + If any file on the deleting branch is opened by aufs, then aufs
  790. + rejects deleting.
  791. +
  792. +
  793. +Modify the Permission of a Branch
  794. +----------------------------------------------------------------------
  795. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  796. + See struct.txt.
  797. +
  798. +o rw --> ro: Confirm the modifying branch is not busy
  799. + Aufs rejects the request if any of these conditions are true.
  800. + - a file on the branch is mmap-ed.
  801. + - a regular file on the branch is opened for write and there is no
  802. + same named entry on the upper branch.
  803. --- /dev/null
  804. +++ linux-4.4/Documentation/filesystems/aufs/design/05wbr_policy.txt 2016-10-23 11:20:57.615493286 +0300
  805. @@ -0,0 +1,64 @@
  806. +
  807. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  808. +#
  809. +# This program is free software; you can redistribute it and/or modify
  810. +# it under the terms of the GNU General Public License as published by
  811. +# the Free Software Foundation; either version 2 of the License, or
  812. +# (at your option) any later version.
  813. +#
  814. +# This program is distributed in the hope that it will be useful,
  815. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  816. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  817. +# GNU General Public License for more details.
  818. +#
  819. +# You should have received a copy of the GNU General Public License
  820. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  821. +
  822. +Policies to Select One among Multiple Writable Branches
  823. +----------------------------------------------------------------------
  824. +When the number of writable branch is more than one, aufs has to decide
  825. +the target branch for file creation or copy-up. By default, the highest
  826. +writable branch which has the parent (or ancestor) dir of the target
  827. +file is chosen (top-down-parent policy).
  828. +By user's request, aufs implements some other policies to select the
  829. +writable branch, for file creation several policies, round-robin,
  830. +most-free-space, and other policies. For copy-up, top-down-parent,
  831. +bottom-up-parent, bottom-up and others.
  832. +
  833. +As expected, the round-robin policy selects the branch in circular. When
  834. +you have two writable branches and creates 10 new files, 5 files will be
  835. +created for each branch. mkdir(2) systemcall is an exception. When you
  836. +create 10 new directories, all will be created on the same branch.
  837. +And the most-free-space policy selects the one which has most free
  838. +space among the writable branches. The amount of free space will be
  839. +checked by aufs internally, and users can specify its time interval.
  840. +
  841. +The policies for copy-up is more simple,
  842. +top-down-parent is equivalent to the same named on in create policy,
  843. +bottom-up-parent selects the writable branch where the parent dir
  844. +exists and the nearest upper one from the copyup-source,
  845. +bottom-up selects the nearest upper writable branch from the
  846. +copyup-source, regardless the existence of the parent dir.
  847. +
  848. +There are some rules or exceptions to apply these policies.
  849. +- If there is a readonly branch above the policy-selected branch and
  850. + the parent dir is marked as opaque (a variation of whiteout), or the
  851. + target (creating) file is whiteout-ed on the upper readonly branch,
  852. + then the result of the policy is ignored and the target file will be
  853. + created on the nearest upper writable branch than the readonly branch.
  854. +- If there is a writable branch above the policy-selected branch and
  855. + the parent dir is marked as opaque or the target file is whiteouted
  856. + on the branch, then the result of the policy is ignored and the target
  857. + file will be created on the highest one among the upper writable
  858. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  859. + it as usual.
  860. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  861. + They try selecting the branch where the source exists as possible
  862. + since copyup a large file will take long time. If it can't be,
  863. + ie. the branch where the source exists is readonly, then they will
  864. + follow the copyup policy.
  865. +- There is an exception for rename(2) when the target exists.
  866. + If the rename target exists, aufs compares the index of the branches
  867. + where the source and the target exists and selects the higher
  868. + one. If the selected branch is readonly, then aufs follows the
  869. + copyup policy.
  870. --- /dev/null
  871. +++ linux-4.4/Documentation/filesystems/aufs/design/06fhsm.txt 2016-10-23 11:20:57.615493286 +0300
  872. @@ -0,0 +1,120 @@
  873. +
  874. +# Copyright (C) 2011-2016 Junjiro R. Okajima
  875. +#
  876. +# This program is free software; you can redistribute it and/or modify
  877. +# it under the terms of the GNU General Public License as published by
  878. +# the Free Software Foundation; either version 2 of the License, or
  879. +# (at your option) any later version.
  880. +#
  881. +# This program is distributed in the hope that it will be useful,
  882. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  883. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  884. +# GNU General Public License for more details.
  885. +#
  886. +# You should have received a copy of the GNU General Public License
  887. +# along with this program; if not, write to the Free Software
  888. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  889. +
  890. +
  891. +File-based Hierarchical Storage Management (FHSM)
  892. +----------------------------------------------------------------------
  893. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  894. +storage world. Aufs provides this feature as file-based with multiple
  895. +writable branches, based upon the principle of "Colder, the Lower".
  896. +Here the word "colder" means that the less used files, and "lower" means
  897. +that the position in the order of the stacked branches vertically.
  898. +These multiple writable branches are prioritized, ie. the topmost one
  899. +should be the fastest drive and be used heavily.
  900. +
  901. +o Characters in aufs FHSM story
  902. +- aufs itself and a new branch attribute.
  903. +- a new ioctl interface to move-down and to establish a connection with
  904. + the daemon ("move-down" is a converse of "copy-up").
  905. +- userspace tool and daemon.
  906. +
  907. +The userspace daemon establishes a connection with aufs and waits for
  908. +the notification. The notified information is very similar to struct
  909. +statfs containing the number of consumed blocks and inodes.
  910. +When the consumed blocks/inodes of a branch exceeds the user-specified
  911. +upper watermark, the daemon activates its move-down process until the
  912. +consumed blocks/inodes reaches the user-specified lower watermark.
  913. +
  914. +The actual move-down is done by aufs based upon the request from
  915. +user-space since we need to maintain the inode number and the internal
  916. +pointer arrays in aufs.
  917. +
  918. +Currently aufs FHSM handles the regular files only. Additionally they
  919. +must not be hard-linked nor pseudo-linked.
  920. +
  921. +
  922. +o Cowork of aufs and the user-space daemon
  923. + During the userspace daemon established the connection, aufs sends a
  924. + small notification to it whenever aufs writes something into the
  925. + writable branch. But it may cost high since aufs issues statfs(2)
  926. + internally. So user can specify a new option to cache the
  927. + info. Actually the notification is controlled by these factors.
  928. + + the specified cache time.
  929. + + classified as "force" by aufs internally.
  930. + Until the specified time expires, aufs doesn't send the info
  931. + except the forced cases. When aufs decide forcing, the info is always
  932. + notified to userspace.
  933. + For example, the number of free inodes is generally large enough and
  934. + the shortage of it happens rarely. So aufs doesn't force the
  935. + notification when creating a new file, directory and others. This is
  936. + the typical case which aufs doesn't force.
  937. + When aufs writes the actual filedata and the files consumes any of new
  938. + blocks, the aufs forces notifying.
  939. +
  940. +
  941. +o Interfaces in aufs
  942. +- New branch attribute.
  943. + + fhsm
  944. + Specifies that the branch is managed by FHSM feature. In other word,
  945. + participant in the FHSM.
  946. + When nofhsm is set to the branch, it will not be the source/target
  947. + branch of the move-down operation. This attribute is set
  948. + independently from coo and moo attributes, and if you want full
  949. + FHSM, you should specify them as well.
  950. +- New mount option.
  951. + + fhsm_sec
  952. + Specifies a second to suppress many less important info to be
  953. + notified.
  954. +- New ioctl.
  955. + + AUFS_CTL_FHSM_FD
  956. + create a new file descriptor which userspace can read the notification
  957. + (a subset of struct statfs) from aufs.
  958. +- Module parameter 'brs'
  959. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  960. + be set.
  961. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  962. + When there are two or more branches with fhsm attributes,
  963. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  964. + terminates it. As a result of remounting and branch-manipulation, the
  965. + number of branches with fhsm attribute can be one. In this case,
  966. + /sbin/mount.aufs will terminate the user-space daemon.
  967. +
  968. +
  969. +Finally the operation is done as these steps in kernel-space.
  970. +- make sure that,
  971. + + no one else is using the file.
  972. + + the file is not hard-linked.
  973. + + the file is not pseudo-linked.
  974. + + the file is a regular file.
  975. + + the parent dir is not opaqued.
  976. +- find the target writable branch.
  977. +- make sure the file is not whiteout-ed by the upper (than the target)
  978. + branch.
  979. +- make the parent dir on the target branch.
  980. +- mutex lock the inode on the branch.
  981. +- unlink the whiteout on the target branch (if exists).
  982. +- lookup and create the whiteout-ed temporary name on the target branch.
  983. +- copy the file as the whiteout-ed temporary name on the target branch.
  984. +- rename the whiteout-ed temporary name to the original name.
  985. +- unlink the file on the source branch.
  986. +- maintain the internal pointer array and the external inode number
  987. + table (XINO).
  988. +- maintain the timestamps and other attributes of the parent dir and the
  989. + file.
  990. +
  991. +And of course, in every step, an error may happen. So the operation
  992. +should restore the original file state after an error happens.
  993. --- /dev/null
  994. +++ linux-4.4/Documentation/filesystems/aufs/design/06mmap.txt 2016-10-23 11:20:57.618826619 +0300
  995. @@ -0,0 +1,72 @@
  996. +
  997. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  998. +#
  999. +# This program is free software; you can redistribute it and/or modify
  1000. +# it under the terms of the GNU General Public License as published by
  1001. +# the Free Software Foundation; either version 2 of the License, or
  1002. +# (at your option) any later version.
  1003. +#
  1004. +# This program is distributed in the hope that it will be useful,
  1005. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1006. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1007. +# GNU General Public License for more details.
  1008. +#
  1009. +# You should have received a copy of the GNU General Public License
  1010. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1011. +
  1012. +mmap(2) -- File Memory Mapping
  1013. +----------------------------------------------------------------------
  1014. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1015. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1016. +->mmap().
  1017. +This approach is simple and good, but there is one problem.
  1018. +Under /proc, several entries show the mmapped files by its path (with
  1019. +device and inode number), and the printed path will be the path on the
  1020. +branch fs's instead of virtual aufs's.
  1021. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1022. +user) may expect the path on aufs.
  1023. +
  1024. +To address this issue, aufs adds a new member called vm_prfile in struct
  1025. +vm_area_struct (and struct vm_region). The original vm_file points to
  1026. +the file on the branch fs in order to handle everything correctly as
  1027. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1028. +show-functions in procfs refers to vm_prfile if it is set.
  1029. +Also we need to maintain several other places where touching vm_file
  1030. +such like
  1031. +- fork()/clone() copies vma and the reference count of vm_file is
  1032. + incremented.
  1033. +- merging vma maintains the ref count too.
  1034. +
  1035. +This is not a good approach. It just fakes the printed path. But it
  1036. +leaves all behaviour around f_mapping unchanged. This is surely an
  1037. +advantage.
  1038. +Actually aufs had adopted another complicated approach which calls
  1039. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1040. +approach, aufs met a hard problem and I could not solve it without
  1041. +switching the approach.
  1042. +
  1043. +There may be one more another approach which is
  1044. +- bind-mount the branch-root onto the aufs-root internally
  1045. +- grab the new vfsmount (ie. struct mount)
  1046. +- lazy-umount the branch-root internally
  1047. +- in open(2) the aufs-file, open the branch-file with the hidden
  1048. + vfsmount (instead of the original branch's vfsmount)
  1049. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1050. + but it may be possible from userspace by the mount helper.
  1051. +
  1052. +Adding the internal hidden vfsmount and using it in opening a file, the
  1053. +file path under /proc will be printed correctly. This approach looks
  1054. +smarter, but is not possible I am afraid.
  1055. +- aufs-root may be bind-mount later. when it happens, another hidden
  1056. + vfsmount will be required.
  1057. +- it is hard to get the chance to bind-mount and lazy-umount
  1058. + + in kernel-space, FS can have vfsmount in open(2) via
  1059. + file->f_path, and aufs can know its vfsmount. But several locks are
  1060. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1061. + here, then it may cause a deadlock.
  1062. + + in user-space, bind-mount doesn't invoke the mount helper.
  1063. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1064. + means a new member vm_prinode will be necessary. this is essentially
  1065. + equivalent to vm_prfile described above.
  1066. +
  1067. +I have to give up this "looks-smater" approach.
  1068. --- /dev/null
  1069. +++ linux-4.4/Documentation/filesystems/aufs/design/06xattr.txt 2016-10-23 11:20:57.618826619 +0300
  1070. @@ -0,0 +1,96 @@
  1071. +
  1072. +# Copyright (C) 2014-2016 Junjiro R. Okajima
  1073. +#
  1074. +# This program is free software; you can redistribute it and/or modify
  1075. +# it under the terms of the GNU General Public License as published by
  1076. +# the Free Software Foundation; either version 2 of the License, or
  1077. +# (at your option) any later version.
  1078. +#
  1079. +# This program is distributed in the hope that it will be useful,
  1080. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1081. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1082. +# GNU General Public License for more details.
  1083. +#
  1084. +# You should have received a copy of the GNU General Public License
  1085. +# along with this program; if not, write to the Free Software
  1086. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1087. +
  1088. +
  1089. +Listing XATTR/EA and getting the value
  1090. +----------------------------------------------------------------------
  1091. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1092. +shows the values from the topmost existing file. This behaviour is good
  1093. +for the non-dir entries since the bahaviour exactly matches the shown
  1094. +information. But for the directories, aufs considers all the same named
  1095. +entries on the lower branches. Which means, if one of the lower entry
  1096. +rejects readdir call, then aufs returns an error even if the topmost
  1097. +entry allows it. This behaviour is necessary to respect the branch fs's
  1098. +security, but can make users confused since the user-visible standard
  1099. +attributes don't match the behaviour.
  1100. +To address this issue, aufs has a mount option called dirperm1 which
  1101. +checks the permission for the topmost entry only, and ignores the lower
  1102. +entry's permission.
  1103. +
  1104. +A similar issue can happen around XATTR.
  1105. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1106. +always set. Otherwise these very unpleasant situation would happen.
  1107. +- listxattr(2) may return the duplicated entries.
  1108. +- users may not be able to remove or reset the XATTR forever,
  1109. +
  1110. +
  1111. +XATTR/EA support in the internal (copy,move)-(up,down)
  1112. +----------------------------------------------------------------------
  1113. +Generally the extended attributes of inode are categorized as these.
  1114. +- "security" for LSM and capability.
  1115. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1116. + fs generally.
  1117. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1118. +- "user" for userspace, 'user_xattr' mount option is required for the
  1119. + branch fs generally.
  1120. +
  1121. +Moreover there are some other categories. Aufs handles these rather
  1122. +unpopular categories as the ordinary ones, ie. there is no special
  1123. +condition nor exception.
  1124. +
  1125. +In copy-up, the support for XATTR on the dst branch may differ from the
  1126. +src branch. In this case, the copy-up operation will get an error and
  1127. +the original user operation which triggered the copy-up will fail. It
  1128. +can happen that even all copy-up will fail.
  1129. +When both of src and dst branches support XATTR and if an error occurs
  1130. +during copying XATTR, then the copy-up should fail obviously. That is a
  1131. +good reason and aufs should return an error to userspace. But when only
  1132. +the src branch support that XATTR, aufs should not return an error.
  1133. +For example, the src branch supports ACL but the dst branch doesn't
  1134. +because the dst branch may natively un-support it or temporary
  1135. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1136. +may NOT return an error even if the XATTR is not supported. It is
  1137. +totally up to the branch fs.
  1138. +
  1139. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1140. +fs, then aufs tries removing the just copied entry and returns the error
  1141. +to the userspace. The worst case of this situation will be all copy-up
  1142. +will fail.
  1143. +
  1144. +For the copy-up operation, there two basic approaches.
  1145. +- copy the specified XATTR only (by category above), and return the
  1146. + error unconditionally if it happens.
  1147. +- copy all XATTR, and ignore the error on the specified category only.
  1148. +
  1149. +In order to support XATTR and to implement the correct behaviour, aufs
  1150. +chooses the latter approach and introduces some new branch attributes,
  1151. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1152. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1153. +convenient, "icex" is also provided which means all "icex*" attributes
  1154. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1155. +
  1156. +The meaning of these attributes is to ignore the error from setting
  1157. +XATTR on that branch.
  1158. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1159. +error from the dst branch according to the specified attributes.
  1160. +
  1161. +Some XATTR may have its default value. The default value may come from
  1162. +the parent dir or the environment. If the default value is set at the
  1163. +file creating-time, it will be overwritten by copy-up.
  1164. +Some contradiction may happen I am afraid.
  1165. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1166. +now, aufs implements the branch attributes to ignore the error.
  1167. --- /dev/null
  1168. +++ linux-4.4/Documentation/filesystems/aufs/design/07export.txt 2016-10-23 11:20:57.618826619 +0300
  1169. @@ -0,0 +1,58 @@
  1170. +
  1171. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  1172. +#
  1173. +# This program is free software; you can redistribute it and/or modify
  1174. +# it under the terms of the GNU General Public License as published by
  1175. +# the Free Software Foundation; either version 2 of the License, or
  1176. +# (at your option) any later version.
  1177. +#
  1178. +# This program is distributed in the hope that it will be useful,
  1179. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1180. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1181. +# GNU General Public License for more details.
  1182. +#
  1183. +# You should have received a copy of the GNU General Public License
  1184. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1185. +
  1186. +Export Aufs via NFS
  1187. +----------------------------------------------------------------------
  1188. +Here is an approach.
  1189. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1190. + generation.
  1191. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1192. + store it in xigen file.
  1193. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1194. + file. it is necessary even if it is not unlinked, because any data of
  1195. + inode may be changed by UDBA.
  1196. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1197. + build file handle by
  1198. + + branch id (4 bytes)
  1199. + + superblock generation (4 bytes)
  1200. + + inode number (4 or 8 bytes)
  1201. + + parent dir inode number (4 or 8 bytes)
  1202. + + inode generation (4 bytes))
  1203. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1204. + bytes)
  1205. + + file handle for a branch (by exportfs_encode_fh())
  1206. +- fh_to_dentry():
  1207. + + find the index of a branch from its id in handle, and check it is
  1208. + still exist in aufs.
  1209. + + 1st level: get the inode number from handle and search it in cache.
  1210. + + 2nd level: if not found in cache, get the parent inode number from
  1211. + the handle and search it in cache. and then open the found parent
  1212. + dir, find the matching inode number by vfs_readdir() and get its
  1213. + name, and call lookup_one_len() for the target dentry.
  1214. + + 3rd level: if the parent dir is not cached, call
  1215. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1216. + build a pathname of it, convert it a pathname in aufs, call
  1217. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1218. + the 2nd level.
  1219. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1220. + for every branch, but not itself. to get this, (currently) aufs
  1221. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1222. + idea, but I didn't get other approach.
  1223. + + test the generation of the gotten inode.
  1224. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1225. + convert it into ESTALE for NFSD.
  1226. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1227. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1228. --- /dev/null
  1229. +++ linux-4.4/Documentation/filesystems/aufs/design/08shwh.txt 2016-10-23 11:20:57.618826619 +0300
  1230. @@ -0,0 +1,52 @@
  1231. +
  1232. +# Copyright (C) 2005-2016 Junjiro R. Okajima
  1233. +#
  1234. +# This program is free software; you can redistribute it and/or modify
  1235. +# it under the terms of the GNU General Public License as published by
  1236. +# the Free Software Foundation; either version 2 of the License, or
  1237. +# (at your option) any later version.
  1238. +#
  1239. +# This program is distributed in the hope that it will be useful,
  1240. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1241. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1242. +# GNU General Public License for more details.
  1243. +#
  1244. +# You should have received a copy of the GNU General Public License
  1245. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1246. +
  1247. +Show Whiteout Mode (shwh)
  1248. +----------------------------------------------------------------------
  1249. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1250. +them is very useful for users. For instance, creating a new middle layer
  1251. +(branch) by merging existing layers.
  1252. +
  1253. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1254. +When you have three branches,
  1255. +- Bottom: 'system', squashfs (underlying base system), read-only
  1256. +- Middle: 'mods', squashfs, read-only
  1257. +- Top: 'overlay', ram (tmpfs), read-write
  1258. +
  1259. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1260. +the changes made to the system during the session.
  1261. +When larger changes have been made, or smaller changes have accumulated,
  1262. +the size of the saved top layer data grows. At this point, it would be
  1263. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1264. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1265. +restoring save and load speed.
  1266. +
  1267. +This merging is simplified by the use of another aufs mount, of just the
  1268. +two overlay branches using the 'shwh' option.
  1269. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1270. + aufs /livesys/merge_union
  1271. +
  1272. +A merged view of these two branches is then available at
  1273. +/livesys/merge_union, and the new feature is that the whiteouts are
  1274. +visible!
  1275. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1276. +writing to all branches. Also the default mode for all branches is 'ro'.
  1277. +It is now possible to save the combined contents of the two overlay
  1278. +branches to a new squashfs, e.g.:
  1279. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1280. +
  1281. +This new squashfs archive can be stored on the boot device and the
  1282. +initramfs will use it to replace the old one at the next boot.
  1283. --- /dev/null
  1284. +++ linux-4.4/Documentation/filesystems/aufs/design/10dynop.txt 2016-10-23 11:20:57.618826619 +0300
  1285. @@ -0,0 +1,47 @@
  1286. +
  1287. +# Copyright (C) 2010-2016 Junjiro R. Okajima
  1288. +#
  1289. +# This program is free software; you can redistribute it and/or modify
  1290. +# it under the terms of the GNU General Public License as published by
  1291. +# the Free Software Foundation; either version 2 of the License, or
  1292. +# (at your option) any later version.
  1293. +#
  1294. +# This program is distributed in the hope that it will be useful,
  1295. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1296. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1297. +# GNU General Public License for more details.
  1298. +#
  1299. +# You should have received a copy of the GNU General Public License
  1300. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1301. +
  1302. +Dynamically customizable FS operations
  1303. +----------------------------------------------------------------------
  1304. +Generally FS operations (struct inode_operations, struct
  1305. +address_space_operations, struct file_operations, etc.) are defined as
  1306. +"static const", but it never means that FS have only one set of
  1307. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1308. +three sets, one for XIP, for NOBH, and for normal.
  1309. +Since aufs overrides and redirects these operations, sometimes aufs has
  1310. +to change its behaviour according to the branch FS type. More importantly
  1311. +VFS acts differently if a function (member in the struct) is set or
  1312. +not. It means aufs should have several sets of operations and select one
  1313. +among them according to the branch FS definition.
  1314. +
  1315. +In order to solve this problem and not to affect the behaviour of VFS,
  1316. +aufs defines these operations dynamically. For instance, aufs defines
  1317. +dummy direct_IO function for struct address_space_operations, but it may
  1318. +not be set to the address_space_operations actually. When the branch FS
  1319. +doesn't have it, aufs doesn't set it to its address_space_operations
  1320. +while the function definition itself is still alive. So the behaviour
  1321. +itself will not change, and it will return an error when direct_IO is
  1322. +not set.
  1323. +
  1324. +The lifetime of these dynamically generated operation object is
  1325. +maintained by aufs branch object. When the branch is removed from aufs,
  1326. +the reference counter of the object is decremented. When it reaches
  1327. +zero, the dynamically generated operation object will be freed.
  1328. +
  1329. +This approach is designed to support AIO (io_submit), Direct I/O and
  1330. +XIP (DAX) mainly.
  1331. +Currently this approach is applied to address_space_operations for
  1332. +regular files only.
  1333. --- /dev/null
  1334. +++ linux-4.4/Documentation/filesystems/aufs/README 2016-10-23 11:20:57.612159953 +0300
  1335. @@ -0,0 +1,392 @@
  1336. +
  1337. +Aufs4 -- advanced multi layered unification filesystem version 4.x
  1338. +http://aufs.sf.net
  1339. +Junjiro R. Okajima
  1340. +
  1341. +
  1342. +0. Introduction
  1343. +----------------------------------------
  1344. +In the early days, aufs was entirely re-designed and re-implemented
  1345. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1346. +improvements and implementations, it becomes totally different from
  1347. +Unionfs while keeping the basic features.
  1348. +Recently, Unionfs Version 2.x series begin taking some of the same
  1349. +approaches to aufs1's.
  1350. +Unionfs is being developed by Professor Erez Zadok at Stony Brook
  1351. +University and his team.
  1352. +
  1353. +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
  1354. +If you want older kernel version support, try aufs2-2.6.git or
  1355. +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
  1356. +
  1357. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1358. + According to Christoph Hellwig, linux rejects all union-type
  1359. + filesystems but UnionMount.
  1360. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1361. +
  1362. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1363. + UnionMount, and he pointed out an issue around a directory mutex
  1364. + lock and aufs addressed it. But it is still unsure whether aufs will
  1365. + be merged (or any other union solution).
  1366. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1367. +
  1368. +
  1369. +1. Features
  1370. +----------------------------------------
  1371. +- unite several directories into a single virtual filesystem. The member
  1372. + directory is called as a branch.
  1373. +- you can specify the permission flags to the branch, which are 'readonly',
  1374. + 'readwrite' and 'whiteout-able.'
  1375. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1376. + readonly branch are modifiable logically.
  1377. +- dynamic branch manipulation, add, del.
  1378. +- etc...
  1379. +
  1380. +Also there are many enhancements in aufs, such as:
  1381. +- test only the highest one for the directory permission (dirperm1)
  1382. +- copyup on open (coo=)
  1383. +- 'move' policy for copy-up between two writable branches, after
  1384. + checking free space.
  1385. +- xattr, acl
  1386. +- readdir(3) in userspace.
  1387. +- keep inode number by external inode number table
  1388. +- keep the timestamps of file/dir in internal copyup operation
  1389. +- seekable directory, supporting NFS readdir.
  1390. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1391. + on branch
  1392. +- do not copyup, nor create a whiteout when it is unnecessary
  1393. +- revert a single systemcall when an error occurs in aufs
  1394. +- remount interface instead of ioctl
  1395. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1396. +- loopback mounted filesystem as a branch
  1397. +- kernel thread for removing the dir who has a plenty of whiteouts
  1398. +- support copyup sparse file (a file which has a 'hole' in it)
  1399. +- default permission flags for branches
  1400. +- selectable permission flags for ro branch, whether whiteout can
  1401. + exist or not
  1402. +- export via NFS.
  1403. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1404. +- support multiple writable branches, some policies to select one
  1405. + among multiple writable branches.
  1406. +- a new semantics for link(2) and rename(2) to support multiple
  1407. + writable branches.
  1408. +- no glibc changes are required.
  1409. +- pseudo hardlink (hardlink over branches)
  1410. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1411. + including NFS or remote filesystem branch.
  1412. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1413. +- and more...
  1414. +
  1415. +Currently these features are dropped temporary from aufs4.
  1416. +See design/08plan.txt in detail.
  1417. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1418. + (robr)
  1419. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1420. +
  1421. +Features or just an idea in the future (see also design/*.txt),
  1422. +- reorder the branch index without del/re-add.
  1423. +- permanent xino files for NFSD
  1424. +- an option for refreshing the opened files after add/del branches
  1425. +- light version, without branch manipulation. (unnecessary?)
  1426. +- copyup in userspace
  1427. +- inotify in userspace
  1428. +- readv/writev
  1429. +
  1430. +
  1431. +2. Download
  1432. +----------------------------------------
  1433. +There are three GIT trees for aufs4, aufs4-linux.git,
  1434. +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
  1435. +"aufs-util.git."
  1436. +While the aufs-util is always necessary, you need either of aufs4-linux
  1437. +or aufs4-standalone.
  1438. +
  1439. +The aufs4-linux tree includes the whole linux mainline GIT tree,
  1440. +git://git.kernel.org/.../torvalds/linux.git.
  1441. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1442. +build aufs4 as an external kernel module.
  1443. +Several extra patches are not included in this tree. Only
  1444. +aufs4-standalone tree contains them. They are described in the later
  1445. +section "Configuration and Compilation."
  1446. +
  1447. +On the other hand, the aufs4-standalone tree has only aufs source files
  1448. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1449. +But you need to apply all aufs patches manually.
  1450. +
  1451. +You will find GIT branches whose name is in form of "aufs4.x" where "x"
  1452. +represents the linux kernel version, "linux-4.x". For instance,
  1453. +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
  1454. +"aufs4.x-rcN" branch.
  1455. +
  1456. +o aufs4-linux tree
  1457. +$ git clone --reference /your/linux/git/tree \
  1458. + git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
  1459. +- if you don't have linux GIT tree, then remove "--reference ..."
  1460. +$ cd aufs4-linux.git
  1461. +$ git checkout origin/aufs4.0
  1462. +
  1463. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1464. +leave the patch-work to GIT.
  1465. +$ cd /your/linux/git/tree
  1466. +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
  1467. +$ git fetch aufs4
  1468. +$ git checkout -b my4.0 v4.0
  1469. +$ (add your local change...)
  1470. +$ git pull aufs4 aufs4.0
  1471. +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
  1472. +- you may need to solve some conflicts between your_changes and
  1473. + aufs4.0. in this case, git-rerere is recommended so that you can
  1474. + solve the similar conflicts automatically when you upgrade to 4.1 or
  1475. + later in the future.
  1476. +
  1477. +o aufs4-standalone tree
  1478. +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
  1479. +$ cd aufs4-standalone.git
  1480. +$ git checkout origin/aufs4.0
  1481. +
  1482. +o aufs-util tree
  1483. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1484. +- note that the public aufs-util.git is on SourceForge instead of
  1485. + GitHUB.
  1486. +$ cd aufs-util.git
  1487. +$ git checkout origin/aufs4.0
  1488. +
  1489. +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1490. +The minor version number, 'x' in '4.x', of aufs may not always
  1491. +follow the minor version number of the kernel.
  1492. +Because changes in the kernel that cause the use of a new
  1493. +minor version number do not always require changes to aufs-util.
  1494. +
  1495. +Since aufs-util has its own minor version number, you may not be
  1496. +able to find a GIT branch in aufs-util for your kernel's
  1497. +exact minor version number.
  1498. +In this case, you should git-checkout the branch for the
  1499. +nearest lower number.
  1500. +
  1501. +For (an unreleased) example:
  1502. +If you are using "linux-4.10" and the "aufs4.10" branch
  1503. +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
  1504. +or something numerically smaller is the branch for your kernel.
  1505. +
  1506. +Also you can view all branches by
  1507. + $ git branch -a
  1508. +
  1509. +
  1510. +3. Configuration and Compilation
  1511. +----------------------------------------
  1512. +Make sure you have git-checkout'ed the correct branch.
  1513. +
  1514. +For aufs4-linux tree,
  1515. +- enable CONFIG_AUFS_FS.
  1516. +- set other aufs configurations if necessary.
  1517. +
  1518. +For aufs4-standalone tree,
  1519. +There are several ways to build.
  1520. +
  1521. +1.
  1522. +- apply ./aufs4-kbuild.patch to your kernel source files.
  1523. +- apply ./aufs4-base.patch too.
  1524. +- apply ./aufs4-mmap.patch too.
  1525. +- apply ./aufs4-standalone.patch too, if you have a plan to set
  1526. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
  1527. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1528. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1529. +- enable CONFIG_AUFS_FS, you can select either
  1530. + =m or =y.
  1531. +- and build your kernel as usual.
  1532. +- install the built kernel.
  1533. + Note: Since linux-3.9, every filesystem module requires an alias
  1534. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1535. + modules.aliases file if you set CONFIG_AUFS_FS=m.
  1536. +- install the header files too by "make headers_install" to the
  1537. + directory where you specify. By default, it is $PWD/usr.
  1538. + "make help" shows a brief note for headers_install.
  1539. +- and reboot your system.
  1540. +
  1541. +2.
  1542. +- module only (CONFIG_AUFS_FS=m).
  1543. +- apply ./aufs4-base.patch to your kernel source files.
  1544. +- apply ./aufs4-mmap.patch too.
  1545. +- apply ./aufs4-standalone.patch too.
  1546. +- build your kernel, don't forget "make headers_install", and reboot.
  1547. +- edit ./config.mk and set other aufs configurations if necessary.
  1548. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1549. + every aufs configurations.
  1550. +- build the module by simple "make".
  1551. + Note: Since linux-3.9, every filesystem module requires an alias
  1552. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1553. + modules.aliases file.
  1554. +- you can specify ${KDIR} make variable which points to your kernel
  1555. + source tree.
  1556. +- install the files
  1557. + + run "make install" to install the aufs module, or copy the built
  1558. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1559. + + run "make install_headers" (instead of headers_install) to install
  1560. + the modified aufs header file (you can specify DESTDIR which is
  1561. + available in aufs standalone version's Makefile only), or copy
  1562. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1563. + you like manually. By default, the target directory is $PWD/usr.
  1564. +- no need to apply aufs4-kbuild.patch, nor copying source files to your
  1565. + kernel source tree.
  1566. +
  1567. +Note: The header file aufs_type.h is necessary to build aufs-util
  1568. + as well as "make headers_install" in the kernel source tree.
  1569. + headers_install is subject to be forgotten, but it is essentially
  1570. + necessary, not only for building aufs-util.
  1571. + You may not meet problems without headers_install in some older
  1572. + version though.
  1573. +
  1574. +And then,
  1575. +- read README in aufs-util, build and install it
  1576. +- note that your distribution may contain an obsoleted version of
  1577. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1578. + utilities, make sure that your compiler refers the correct aufs header
  1579. + file which is built by "make headers_install."
  1580. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1581. + then run "make install_ulib" too. And refer to the aufs manual in
  1582. + detail.
  1583. +
  1584. +There several other patches in aufs4-standalone.git. They are all
  1585. +optional. When you meet some problems, they will help you.
  1586. +- aufs4-loopback.patch
  1587. + Supports a nested loopback mount in a branch-fs. This patch is
  1588. + unnecessary until aufs produces a message like "you may want to try
  1589. + another patch for loopback file".
  1590. +- vfs-ino.patch
  1591. + Modifies a system global kernel internal function get_next_ino() in
  1592. + order to stop assigning 0 for an inode-number. Not directly related to
  1593. + aufs, but recommended generally.
  1594. +- tmpfs-idr.patch
  1595. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1596. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1597. + duplication of inode number, which is important for backup tools and
  1598. + other utilities. When you find aufs XINO files for tmpfs branch
  1599. + growing too much, try this patch.
  1600. +- lockdep-debug.patch
  1601. + Because aufs is not only an ordinary filesystem (callee of VFS), but
  1602. + also a caller of VFS functions for branch filesystems, subclassing of
  1603. + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
  1604. + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
  1605. + need to apply this debug patch to expand several constant values.
  1606. + If don't know what LOCKDEP, then you don't have apply this patch.
  1607. +
  1608. +
  1609. +4. Usage
  1610. +----------------------------------------
  1611. +At first, make sure aufs-util are installed, and please read the aufs
  1612. +manual, aufs.5 in aufs-util.git tree.
  1613. +$ man -l aufs.5
  1614. +
  1615. +And then,
  1616. +$ mkdir /tmp/rw /tmp/aufs
  1617. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1618. +
  1619. +Here is another example. The result is equivalent.
  1620. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1621. + Or
  1622. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1623. +# mount -o remount,append:${HOME} /tmp/aufs
  1624. +
  1625. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1626. +you modify a file under /tmp/aufs, the one on your home directory is
  1627. +not affected, instead the same named file will be newly created under
  1628. +/tmp/rw. And all of your modification to a file will be applied to
  1629. +the one under /tmp/rw. This is called the file based Copy on Write
  1630. +(COW) method.
  1631. +Aufs mount options are described in aufs.5.
  1632. +If you run chroot or something and make your aufs as a root directory,
  1633. +then you need to customize the shutdown script. See the aufs manual in
  1634. +detail.
  1635. +
  1636. +Additionally, there are some sample usages of aufs which are a
  1637. +diskless system with network booting, and LiveCD over NFS.
  1638. +See sample dir in CVS tree on SourceForge.
  1639. +
  1640. +
  1641. +5. Contact
  1642. +----------------------------------------
  1643. +When you have any problems or strange behaviour in aufs, please let me
  1644. +know with:
  1645. +- /proc/mounts (instead of the output of mount(8))
  1646. +- /sys/module/aufs/*
  1647. +- /sys/fs/aufs/* (if you have them)
  1648. +- /debug/aufs/* (if you have them)
  1649. +- linux kernel version
  1650. + if your kernel is not plain, for example modified by distributor,
  1651. + the url where i can download its source is necessary too.
  1652. +- aufs version which was printed at loading the module or booting the
  1653. + system, instead of the date you downloaded.
  1654. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1655. +- kernel configuration or /proc/config.gz (if you have it)
  1656. +- behaviour which you think to be incorrect
  1657. +- actual operation, reproducible one is better
  1658. +- mailto: aufs-users at lists.sourceforge.net
  1659. +
  1660. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1661. +and Feature Requests) on SourceForge. Please join and write to
  1662. +aufs-users ML.
  1663. +
  1664. +
  1665. +6. Acknowledgements
  1666. +----------------------------------------
  1667. +Thanks to everyone who have tried and are using aufs, whoever
  1668. +have reported a bug or any feedback.
  1669. +
  1670. +Especially donators:
  1671. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1672. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1673. + scripts) is making "doubling" donations.
  1674. + Unfortunately I cannot list all of the donators, but I really
  1675. + appreciate.
  1676. + It ends Aug 2010, but the ordinary donation URL is still available.
  1677. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1678. +Dai Itasaka made a donation (2007/8).
  1679. +Chuck Smith made a donation (2008/4, 10 and 12).
  1680. +Henk Schoneveld made a donation (2008/9).
  1681. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1682. +Francois Dupoux made a donation (2008/11).
  1683. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1684. + aufs2 GIT tree (2009/2).
  1685. +William Grant made a donation (2009/3).
  1686. +Patrick Lane made a donation (2009/4).
  1687. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1688. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1689. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1690. +Pavel Pronskiy made a donation (2011/2).
  1691. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1692. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1693. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1694. +11).
  1695. +Sam Liddicott made a donation (2011/9).
  1696. +Era Scarecrow made a donation (2013/4).
  1697. +Bor Ratajc made a donation (2013/4).
  1698. +Alessandro Gorreta made a donation (2013/4).
  1699. +POIRETTE Marc made a donation (2013/4).
  1700. +Alessandro Gorreta made a donation (2013/4).
  1701. +lauri kasvandik made a donation (2013/5).
  1702. +"pemasu from Finland" made a donation (2013/7).
  1703. +The Parted Magic Project made a donation (2013/9 and 11).
  1704. +Pavel Barta made a donation (2013/10).
  1705. +Nikolay Pertsev made a donation (2014/5).
  1706. +James B made a donation (2014/7 and 2015/7).
  1707. +Stefano Di Biase made a donation (2014/8).
  1708. +Daniel Epellei made a donation (2015/1).
  1709. +OmegaPhil made a donation (2016/1).
  1710. +Tomasz Szewczyk made a donation (2016/4).
  1711. +
  1712. +Thank you very much.
  1713. +Donations are always, including future donations, very important and
  1714. +helpful for me to keep on developing aufs.
  1715. +
  1716. +
  1717. +7.
  1718. +----------------------------------------
  1719. +If you are an experienced user, no explanation is needed. Aufs is
  1720. +just a linux filesystem.
  1721. +
  1722. +
  1723. +Enjoy!
  1724. +
  1725. +# Local variables: ;
  1726. +# mode: text;
  1727. +# End: ;
  1728. --- /dev/null
  1729. +++ linux-4.4/fs/aufs/aufs.h 2016-10-23 11:20:57.622159952 +0300
  1730. @@ -0,0 +1,59 @@
  1731. +/*
  1732. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  1733. + *
  1734. + * This program, aufs is free software; you can redistribute it and/or modify
  1735. + * it under the terms of the GNU General Public License as published by
  1736. + * the Free Software Foundation; either version 2 of the License, or
  1737. + * (at your option) any later version.
  1738. + *
  1739. + * This program is distributed in the hope that it will be useful,
  1740. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1741. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1742. + * GNU General Public License for more details.
  1743. + *
  1744. + * You should have received a copy of the GNU General Public License
  1745. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1746. + */
  1747. +
  1748. +/*
  1749. + * all header files
  1750. + */
  1751. +
  1752. +#ifndef __AUFS_H__
  1753. +#define __AUFS_H__
  1754. +
  1755. +#ifdef __KERNEL__
  1756. +
  1757. +#define AuStub(type, name, body, ...) \
  1758. + static inline type name(__VA_ARGS__) { body; }
  1759. +
  1760. +#define AuStubVoid(name, ...) \
  1761. + AuStub(void, name, , __VA_ARGS__)
  1762. +#define AuStubInt0(name, ...) \
  1763. + AuStub(int, name, return 0, __VA_ARGS__)
  1764. +
  1765. +#include "debug.h"
  1766. +
  1767. +#include "branch.h"
  1768. +#include "cpup.h"
  1769. +#include "dcsub.h"
  1770. +#include "dbgaufs.h"
  1771. +#include "dentry.h"
  1772. +#include "dir.h"
  1773. +#include "dynop.h"
  1774. +#include "file.h"
  1775. +#include "fstype.h"
  1776. +#include "inode.h"
  1777. +#include "loop.h"
  1778. +#include "module.h"
  1779. +#include "opts.h"
  1780. +#include "rwsem.h"
  1781. +#include "spl.h"
  1782. +#include "super.h"
  1783. +#include "sysaufs.h"
  1784. +#include "vfsub.h"
  1785. +#include "whout.h"
  1786. +#include "wkq.h"
  1787. +
  1788. +#endif /* __KERNEL__ */
  1789. +#endif /* __AUFS_H__ */
  1790. --- /dev/null
  1791. +++ linux-4.4/fs/aufs/branch.c 2016-10-23 11:20:57.622159952 +0300
  1792. @@ -0,0 +1,1411 @@
  1793. +/*
  1794. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  1795. + *
  1796. + * This program, aufs is free software; you can redistribute it and/or modify
  1797. + * it under the terms of the GNU General Public License as published by
  1798. + * the Free Software Foundation; either version 2 of the License, or
  1799. + * (at your option) any later version.
  1800. + *
  1801. + * This program is distributed in the hope that it will be useful,
  1802. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1803. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1804. + * GNU General Public License for more details.
  1805. + *
  1806. + * You should have received a copy of the GNU General Public License
  1807. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1808. + */
  1809. +
  1810. +/*
  1811. + * branch management
  1812. + */
  1813. +
  1814. +#include <linux/compat.h>
  1815. +#include <linux/statfs.h>
  1816. +#include "aufs.h"
  1817. +
  1818. +/*
  1819. + * free a single branch
  1820. + */
  1821. +static void au_br_do_free(struct au_branch *br)
  1822. +{
  1823. + int i;
  1824. + struct au_wbr *wbr;
  1825. + struct au_dykey **key;
  1826. +
  1827. + au_hnotify_fin_br(br);
  1828. +
  1829. + if (br->br_xino.xi_file)
  1830. + fput(br->br_xino.xi_file);
  1831. + mutex_destroy(&br->br_xino.xi_nondir_mtx);
  1832. +
  1833. + AuDebugOn(au_br_count(br));
  1834. + au_br_count_fin(br);
  1835. +
  1836. + wbr = br->br_wbr;
  1837. + if (wbr) {
  1838. + for (i = 0; i < AuBrWh_Last; i++)
  1839. + dput(wbr->wbr_wh[i]);
  1840. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  1841. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  1842. + }
  1843. +
  1844. + if (br->br_fhsm) {
  1845. + au_br_fhsm_fin(br->br_fhsm);
  1846. + au_delayed_kfree(br->br_fhsm);
  1847. + }
  1848. +
  1849. + key = br->br_dykey;
  1850. + for (i = 0; i < AuBrDynOp; i++, key++)
  1851. + if (*key)
  1852. + au_dy_put(*key);
  1853. + else
  1854. + break;
  1855. +
  1856. + /* recursive lock, s_umount of branch's */
  1857. + lockdep_off();
  1858. + path_put(&br->br_path);
  1859. + lockdep_on();
  1860. + if (wbr)
  1861. + au_delayed_kfree(wbr);
  1862. + au_delayed_kfree(br);
  1863. +}
  1864. +
  1865. +/*
  1866. + * frees all branches
  1867. + */
  1868. +void au_br_free(struct au_sbinfo *sbinfo)
  1869. +{
  1870. + aufs_bindex_t bmax;
  1871. + struct au_branch **br;
  1872. +
  1873. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  1874. +
  1875. + bmax = sbinfo->si_bbot + 1;
  1876. + br = sbinfo->si_branch;
  1877. + while (bmax--)
  1878. + au_br_do_free(*br++);
  1879. +}
  1880. +
  1881. +/*
  1882. + * find the index of a branch which is specified by @br_id.
  1883. + */
  1884. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  1885. +{
  1886. + aufs_bindex_t bindex, bbot;
  1887. +
  1888. + bbot = au_sbbot(sb);
  1889. + for (bindex = 0; bindex <= bbot; bindex++)
  1890. + if (au_sbr_id(sb, bindex) == br_id)
  1891. + return bindex;
  1892. + return -1;
  1893. +}
  1894. +
  1895. +/* ---------------------------------------------------------------------- */
  1896. +
  1897. +/*
  1898. + * add a branch
  1899. + */
  1900. +
  1901. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  1902. + struct dentry *h_root)
  1903. +{
  1904. + if (unlikely(h_adding == h_root
  1905. + || au_test_loopback_overlap(sb, h_adding)))
  1906. + return 1;
  1907. + if (h_adding->d_sb != h_root->d_sb)
  1908. + return 0;
  1909. + return au_test_subdir(h_adding, h_root)
  1910. + || au_test_subdir(h_root, h_adding);
  1911. +}
  1912. +
  1913. +/*
  1914. + * returns a newly allocated branch. @new_nbranch is a number of branches
  1915. + * after adding a branch.
  1916. + */
  1917. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  1918. + int perm)
  1919. +{
  1920. + struct au_branch *add_branch;
  1921. + struct dentry *root;
  1922. + struct inode *inode;
  1923. + int err;
  1924. +
  1925. + err = -ENOMEM;
  1926. + root = sb->s_root;
  1927. + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
  1928. + if (unlikely(!add_branch))
  1929. + goto out;
  1930. +
  1931. + err = au_hnotify_init_br(add_branch, perm);
  1932. + if (unlikely(err))
  1933. + goto out_br;
  1934. +
  1935. + if (au_br_writable(perm)) {
  1936. + /* may be freed separately at changing the branch permission */
  1937. + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
  1938. + GFP_NOFS);
  1939. + if (unlikely(!add_branch->br_wbr))
  1940. + goto out_hnotify;
  1941. + }
  1942. +
  1943. + if (au_br_fhsm(perm)) {
  1944. + err = au_fhsm_br_alloc(add_branch);
  1945. + if (unlikely(err))
  1946. + goto out_wbr;
  1947. + }
  1948. +
  1949. + err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
  1950. + if (!err)
  1951. + err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
  1952. + if (!err) {
  1953. + inode = d_inode(root);
  1954. + err = au_hinode_realloc(au_ii(inode), new_nbranch, /*may_shrink*/0);
  1955. + }
  1956. + if (!err)
  1957. + return add_branch; /* success */
  1958. +
  1959. +out_wbr:
  1960. + if (add_branch->br_wbr)
  1961. + au_delayed_kfree(add_branch->br_wbr);
  1962. +out_hnotify:
  1963. + au_hnotify_fin_br(add_branch);
  1964. +out_br:
  1965. + au_delayed_kfree(add_branch);
  1966. +out:
  1967. + return ERR_PTR(err);
  1968. +}
  1969. +
  1970. +/*
  1971. + * test if the branch permission is legal or not.
  1972. + */
  1973. +static int test_br(struct inode *inode, int brperm, char *path)
  1974. +{
  1975. + int err;
  1976. +
  1977. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  1978. + if (!err)
  1979. + goto out;
  1980. +
  1981. + err = -EINVAL;
  1982. + pr_err("write permission for readonly mount or inode, %s\n", path);
  1983. +
  1984. +out:
  1985. + return err;
  1986. +}
  1987. +
  1988. +/*
  1989. + * returns:
  1990. + * 0: success, the caller will add it
  1991. + * plus: success, it is already unified, the caller should ignore it
  1992. + * minus: error
  1993. + */
  1994. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  1995. +{
  1996. + int err;
  1997. + aufs_bindex_t bbot, bindex;
  1998. + struct dentry *root, *h_dentry;
  1999. + struct inode *inode, *h_inode;
  2000. +
  2001. + root = sb->s_root;
  2002. + bbot = au_sbbot(sb);
  2003. + if (unlikely(bbot >= 0
  2004. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2005. + err = 1;
  2006. + if (!remount) {
  2007. + err = -EINVAL;
  2008. + pr_err("%s duplicated\n", add->pathname);
  2009. + }
  2010. + goto out;
  2011. + }
  2012. +
  2013. + err = -ENOSPC; /* -E2BIG; */
  2014. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2015. + || AUFS_BRANCH_MAX - 1 <= bbot)) {
  2016. + pr_err("number of branches exceeded %s\n", add->pathname);
  2017. + goto out;
  2018. + }
  2019. +
  2020. + err = -EDOM;
  2021. + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
  2022. + pr_err("bad index %d\n", add->bindex);
  2023. + goto out;
  2024. + }
  2025. +
  2026. + inode = d_inode(add->path.dentry);
  2027. + err = -ENOENT;
  2028. + if (unlikely(!inode->i_nlink)) {
  2029. + pr_err("no existence %s\n", add->pathname);
  2030. + goto out;
  2031. + }
  2032. +
  2033. + err = -EINVAL;
  2034. + if (unlikely(inode->i_sb == sb)) {
  2035. + pr_err("%s must be outside\n", add->pathname);
  2036. + goto out;
  2037. + }
  2038. +
  2039. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2040. + pr_err("unsupported filesystem, %s (%s)\n",
  2041. + add->pathname, au_sbtype(inode->i_sb));
  2042. + goto out;
  2043. + }
  2044. +
  2045. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2046. + pr_err("already stacked, %s (%s)\n",
  2047. + add->pathname, au_sbtype(inode->i_sb));
  2048. + goto out;
  2049. + }
  2050. +
  2051. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2052. + if (unlikely(err))
  2053. + goto out;
  2054. +
  2055. + if (bbot < 0)
  2056. + return 0; /* success */
  2057. +
  2058. + err = -EINVAL;
  2059. + for (bindex = 0; bindex <= bbot; bindex++)
  2060. + if (unlikely(test_overlap(sb, add->path.dentry,
  2061. + au_h_dptr(root, bindex)))) {
  2062. + pr_err("%s is overlapped\n", add->pathname);
  2063. + goto out;
  2064. + }
  2065. +
  2066. + err = 0;
  2067. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2068. + h_dentry = au_h_dptr(root, 0);
  2069. + h_inode = d_inode(h_dentry);
  2070. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2071. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2072. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2073. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2074. + add->pathname,
  2075. + i_uid_read(inode), i_gid_read(inode),
  2076. + (inode->i_mode & S_IALLUGO),
  2077. + i_uid_read(h_inode), i_gid_read(h_inode),
  2078. + (h_inode->i_mode & S_IALLUGO));
  2079. + }
  2080. +
  2081. +out:
  2082. + return err;
  2083. +}
  2084. +
  2085. +/*
  2086. + * initialize or clean the whiteouts for an adding branch
  2087. + */
  2088. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2089. + int new_perm)
  2090. +{
  2091. + int err, old_perm;
  2092. + aufs_bindex_t bindex;
  2093. + struct mutex *h_mtx;
  2094. + struct au_wbr *wbr;
  2095. + struct au_hinode *hdir;
  2096. + struct dentry *h_dentry;
  2097. +
  2098. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2099. + if (unlikely(err))
  2100. + goto out;
  2101. +
  2102. + wbr = br->br_wbr;
  2103. + old_perm = br->br_perm;
  2104. + br->br_perm = new_perm;
  2105. + hdir = NULL;
  2106. + h_mtx = NULL;
  2107. + bindex = au_br_index(sb, br->br_id);
  2108. + if (0 <= bindex) {
  2109. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2110. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  2111. + } else {
  2112. + h_dentry = au_br_dentry(br);
  2113. + h_mtx = &d_inode(h_dentry)->i_mutex;
  2114. + mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
  2115. + }
  2116. + if (!wbr)
  2117. + err = au_wh_init(br, sb);
  2118. + else {
  2119. + wbr_wh_write_lock(wbr);
  2120. + err = au_wh_init(br, sb);
  2121. + wbr_wh_write_unlock(wbr);
  2122. + }
  2123. + if (hdir)
  2124. + au_hn_imtx_unlock(hdir);
  2125. + else
  2126. + mutex_unlock(h_mtx);
  2127. + vfsub_mnt_drop_write(au_br_mnt(br));
  2128. + br->br_perm = old_perm;
  2129. +
  2130. + if (!err && wbr && !au_br_writable(new_perm)) {
  2131. + au_delayed_kfree(wbr);
  2132. + br->br_wbr = NULL;
  2133. + }
  2134. +
  2135. +out:
  2136. + return err;
  2137. +}
  2138. +
  2139. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2140. + int perm)
  2141. +{
  2142. + int err;
  2143. + struct kstatfs kst;
  2144. + struct au_wbr *wbr;
  2145. +
  2146. + wbr = br->br_wbr;
  2147. + au_rw_init(&wbr->wbr_wh_rwsem);
  2148. + atomic_set(&wbr->wbr_wh_running, 0);
  2149. +
  2150. + /*
  2151. + * a limit for rmdir/rename a dir
  2152. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2153. + */
  2154. + err = vfs_statfs(&br->br_path, &kst);
  2155. + if (unlikely(err))
  2156. + goto out;
  2157. + err = -EINVAL;
  2158. + if (kst.f_namelen >= NAME_MAX)
  2159. + err = au_br_init_wh(sb, br, perm);
  2160. + else
  2161. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2162. + au_br_dentry(br),
  2163. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2164. +
  2165. +out:
  2166. + return err;
  2167. +}
  2168. +
  2169. +/* initialize a new branch */
  2170. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2171. + struct au_opt_add *add)
  2172. +{
  2173. + int err;
  2174. + struct inode *h_inode;
  2175. +
  2176. + err = 0;
  2177. + mutex_init(&br->br_xino.xi_nondir_mtx);
  2178. + br->br_perm = add->perm;
  2179. + br->br_path = add->path; /* set first, path_get() later */
  2180. + spin_lock_init(&br->br_dykey_lock);
  2181. + au_br_count_init(br);
  2182. + atomic_set(&br->br_xino_running, 0);
  2183. + br->br_id = au_new_br_id(sb);
  2184. + AuDebugOn(br->br_id < 0);
  2185. +
  2186. + if (au_br_writable(add->perm)) {
  2187. + err = au_wbr_init(br, sb, add->perm);
  2188. + if (unlikely(err))
  2189. + goto out_err;
  2190. + }
  2191. +
  2192. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2193. + h_inode = d_inode(add->path.dentry);
  2194. + err = au_xino_br(sb, br, h_inode->i_ino,
  2195. + au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
  2196. + if (unlikely(err)) {
  2197. + AuDebugOn(br->br_xino.xi_file);
  2198. + goto out_err;
  2199. + }
  2200. + }
  2201. +
  2202. + sysaufs_br_init(br);
  2203. + path_get(&br->br_path);
  2204. + goto out; /* success */
  2205. +
  2206. +out_err:
  2207. + memset(&br->br_path, 0, sizeof(br->br_path));
  2208. +out:
  2209. + return err;
  2210. +}
  2211. +
  2212. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2213. + struct au_branch *br, aufs_bindex_t bbot,
  2214. + aufs_bindex_t amount)
  2215. +{
  2216. + struct au_branch **brp;
  2217. +
  2218. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2219. +
  2220. + brp = sbinfo->si_branch + bindex;
  2221. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2222. + *brp = br;
  2223. + sbinfo->si_bbot++;
  2224. + if (unlikely(bbot < 0))
  2225. + sbinfo->si_bbot = 0;
  2226. +}
  2227. +
  2228. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2229. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2230. +{
  2231. + struct au_hdentry *hdp;
  2232. +
  2233. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2234. +
  2235. + hdp = au_hdentry(dinfo, bindex);
  2236. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2237. + au_h_dentry_init(hdp);
  2238. + dinfo->di_bbot++;
  2239. + if (unlikely(bbot < 0))
  2240. + dinfo->di_btop = 0;
  2241. +}
  2242. +
  2243. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2244. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2245. +{
  2246. + struct au_hinode *hip;
  2247. +
  2248. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2249. +
  2250. + hip = au_hinode(iinfo, bindex);
  2251. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2252. + au_hinode_init(hip);
  2253. + iinfo->ii_bbot++;
  2254. + if (unlikely(bbot < 0))
  2255. + iinfo->ii_btop = 0;
  2256. +}
  2257. +
  2258. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2259. + aufs_bindex_t bindex)
  2260. +{
  2261. + struct dentry *root, *h_dentry;
  2262. + struct inode *root_inode, *h_inode;
  2263. + aufs_bindex_t bbot, amount;
  2264. +
  2265. + root = sb->s_root;
  2266. + root_inode = d_inode(root);
  2267. + bbot = au_sbbot(sb);
  2268. + amount = bbot + 1 - bindex;
  2269. + h_dentry = au_br_dentry(br);
  2270. + au_sbilist_lock();
  2271. + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
  2272. + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
  2273. + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
  2274. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2275. + h_inode = d_inode(h_dentry);
  2276. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2277. + au_sbilist_unlock();
  2278. +}
  2279. +
  2280. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2281. +{
  2282. + int err;
  2283. + aufs_bindex_t bbot, add_bindex;
  2284. + struct dentry *root, *h_dentry;
  2285. + struct inode *root_inode;
  2286. + struct au_branch *add_branch;
  2287. +
  2288. + root = sb->s_root;
  2289. + root_inode = d_inode(root);
  2290. + IMustLock(root_inode);
  2291. + err = test_add(sb, add, remount);
  2292. + if (unlikely(err < 0))
  2293. + goto out;
  2294. + if (err) {
  2295. + err = 0;
  2296. + goto out; /* success */
  2297. + }
  2298. +
  2299. + bbot = au_sbbot(sb);
  2300. + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
  2301. + err = PTR_ERR(add_branch);
  2302. + if (IS_ERR(add_branch))
  2303. + goto out;
  2304. +
  2305. + err = au_br_init(add_branch, sb, add);
  2306. + if (unlikely(err)) {
  2307. + au_br_do_free(add_branch);
  2308. + goto out;
  2309. + }
  2310. +
  2311. + add_bindex = add->bindex;
  2312. + if (!remount)
  2313. + au_br_do_add(sb, add_branch, add_bindex);
  2314. + else {
  2315. + sysaufs_brs_del(sb, add_bindex);
  2316. + au_br_do_add(sb, add_branch, add_bindex);
  2317. + sysaufs_brs_add(sb, add_bindex);
  2318. + }
  2319. +
  2320. + h_dentry = add->path.dentry;
  2321. + if (!add_bindex) {
  2322. + au_cpup_attr_all(root_inode, /*force*/1);
  2323. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2324. + } else
  2325. + au_add_nlink(root_inode, d_inode(h_dentry));
  2326. +
  2327. + /*
  2328. + * this test/set prevents aufs from handling unnecesary notify events
  2329. + * of xino files, in case of re-adding a writable branch which was
  2330. + * once detached from aufs.
  2331. + */
  2332. + if (au_xino_brid(sb) < 0
  2333. + && au_br_writable(add_branch->br_perm)
  2334. + && !au_test_fs_bad_xino(h_dentry->d_sb)
  2335. + && add_branch->br_xino.xi_file
  2336. + && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
  2337. + au_xino_brid_set(sb, add_branch->br_id);
  2338. +
  2339. +out:
  2340. + return err;
  2341. +}
  2342. +
  2343. +/* ---------------------------------------------------------------------- */
  2344. +
  2345. +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
  2346. + unsigned long long max __maybe_unused,
  2347. + void *arg)
  2348. +{
  2349. + unsigned long long n;
  2350. + struct file **p, *f;
  2351. + struct au_sphlhead *files;
  2352. + struct au_finfo *finfo;
  2353. +
  2354. + n = 0;
  2355. + p = a;
  2356. + files = &au_sbi(sb)->si_files;
  2357. + spin_lock(&files->spin);
  2358. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  2359. + f = finfo->fi_file;
  2360. + if (file_count(f)
  2361. + && !special_file(file_inode(f)->i_mode)) {
  2362. + get_file(f);
  2363. + *p++ = f;
  2364. + n++;
  2365. + AuDebugOn(n > max);
  2366. + }
  2367. + }
  2368. + spin_unlock(&files->spin);
  2369. +
  2370. + return n;
  2371. +}
  2372. +
  2373. +static struct file **au_farray_alloc(struct super_block *sb,
  2374. + unsigned long long *max)
  2375. +{
  2376. + *max = au_nfiles(sb);
  2377. + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
  2378. +}
  2379. +
  2380. +static void au_farray_free(struct file **a, unsigned long long max)
  2381. +{
  2382. + unsigned long long ull;
  2383. +
  2384. + for (ull = 0; ull < max; ull++)
  2385. + if (a[ull])
  2386. + fput(a[ull]);
  2387. + kvfree(a);
  2388. +}
  2389. +
  2390. +/* ---------------------------------------------------------------------- */
  2391. +
  2392. +/*
  2393. + * delete a branch
  2394. + */
  2395. +
  2396. +/* to show the line number, do not make it inlined function */
  2397. +#define AuVerbose(do_info, fmt, ...) do { \
  2398. + if (do_info) \
  2399. + pr_info(fmt, ##__VA_ARGS__); \
  2400. +} while (0)
  2401. +
  2402. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
  2403. + aufs_bindex_t bbot)
  2404. +{
  2405. + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
  2406. +}
  2407. +
  2408. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
  2409. + aufs_bindex_t bbot)
  2410. +{
  2411. + return au_test_ibusy(d_inode(dentry), btop, bbot);
  2412. +}
  2413. +
  2414. +/*
  2415. + * test if the branch is deletable or not.
  2416. + */
  2417. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2418. + unsigned int sigen, const unsigned int verbose)
  2419. +{
  2420. + int err, i, j, ndentry;
  2421. + aufs_bindex_t btop, bbot;
  2422. + struct au_dcsub_pages dpages;
  2423. + struct au_dpage *dpage;
  2424. + struct dentry *d;
  2425. +
  2426. + err = au_dpages_init(&dpages, GFP_NOFS);
  2427. + if (unlikely(err))
  2428. + goto out;
  2429. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2430. + if (unlikely(err))
  2431. + goto out_dpages;
  2432. +
  2433. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2434. + dpage = dpages.dpages + i;
  2435. + ndentry = dpage->ndentry;
  2436. + for (j = 0; !err && j < ndentry; j++) {
  2437. + d = dpage->dentries[j];
  2438. + AuDebugOn(au_dcount(d) <= 0);
  2439. + if (!au_digen_test(d, sigen)) {
  2440. + di_read_lock_child(d, AuLock_IR);
  2441. + if (unlikely(au_dbrange_test(d))) {
  2442. + di_read_unlock(d, AuLock_IR);
  2443. + continue;
  2444. + }
  2445. + } else {
  2446. + di_write_lock_child(d);
  2447. + if (unlikely(au_dbrange_test(d))) {
  2448. + di_write_unlock(d);
  2449. + continue;
  2450. + }
  2451. + err = au_reval_dpath(d, sigen);
  2452. + if (!err)
  2453. + di_downgrade_lock(d, AuLock_IR);
  2454. + else {
  2455. + di_write_unlock(d);
  2456. + break;
  2457. + }
  2458. + }
  2459. +
  2460. + /* AuDbgDentry(d); */
  2461. + btop = au_dbtop(d);
  2462. + bbot = au_dbbot(d);
  2463. + if (btop <= bindex
  2464. + && bindex <= bbot
  2465. + && au_h_dptr(d, bindex)
  2466. + && au_test_dbusy(d, btop, bbot)) {
  2467. + err = -EBUSY;
  2468. + AuVerbose(verbose, "busy %pd\n", d);
  2469. + AuDbgDentry(d);
  2470. + }
  2471. + di_read_unlock(d, AuLock_IR);
  2472. + }
  2473. + }
  2474. +
  2475. +out_dpages:
  2476. + au_dpages_free(&dpages);
  2477. +out:
  2478. + return err;
  2479. +}
  2480. +
  2481. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2482. + unsigned int sigen, const unsigned int verbose)
  2483. +{
  2484. + int err;
  2485. + unsigned long long max, ull;
  2486. + struct inode *i, **array;
  2487. + aufs_bindex_t btop, bbot;
  2488. +
  2489. + array = au_iarray_alloc(sb, &max);
  2490. + err = PTR_ERR(array);
  2491. + if (IS_ERR(array))
  2492. + goto out;
  2493. +
  2494. + err = 0;
  2495. + AuDbg("b%d\n", bindex);
  2496. + for (ull = 0; !err && ull < max; ull++) {
  2497. + i = array[ull];
  2498. + if (unlikely(!i))
  2499. + break;
  2500. + if (i->i_ino == AUFS_ROOT_INO)
  2501. + continue;
  2502. +
  2503. + /* AuDbgInode(i); */
  2504. + if (au_iigen(i, NULL) == sigen)
  2505. + ii_read_lock_child(i);
  2506. + else {
  2507. + ii_write_lock_child(i);
  2508. + err = au_refresh_hinode_self(i);
  2509. + au_iigen_dec(i);
  2510. + if (!err)
  2511. + ii_downgrade_lock(i);
  2512. + else {
  2513. + ii_write_unlock(i);
  2514. + break;
  2515. + }
  2516. + }
  2517. +
  2518. + btop = au_ibtop(i);
  2519. + bbot = au_ibbot(i);
  2520. + if (btop <= bindex
  2521. + && bindex <= bbot
  2522. + && au_h_iptr(i, bindex)
  2523. + && au_test_ibusy(i, btop, bbot)) {
  2524. + err = -EBUSY;
  2525. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2526. + AuDbgInode(i);
  2527. + }
  2528. + ii_read_unlock(i);
  2529. + }
  2530. + au_iarray_free(array, max);
  2531. +
  2532. +out:
  2533. + return err;
  2534. +}
  2535. +
  2536. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2537. + const unsigned int verbose)
  2538. +{
  2539. + int err;
  2540. + unsigned int sigen;
  2541. +
  2542. + sigen = au_sigen(root->d_sb);
  2543. + DiMustNoWaiters(root);
  2544. + IiMustNoWaiters(d_inode(root));
  2545. + di_write_unlock(root);
  2546. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2547. + if (!err)
  2548. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2549. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2550. +
  2551. + return err;
  2552. +}
  2553. +
  2554. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2555. + struct file **to_free, int *idx)
  2556. +{
  2557. + int err;
  2558. + unsigned char matched, root;
  2559. + aufs_bindex_t bindex, bbot;
  2560. + struct au_fidir *fidir;
  2561. + struct au_hfile *hfile;
  2562. +
  2563. + err = 0;
  2564. + root = IS_ROOT(file->f_path.dentry);
  2565. + if (root) {
  2566. + get_file(file);
  2567. + to_free[*idx] = file;
  2568. + (*idx)++;
  2569. + goto out;
  2570. + }
  2571. +
  2572. + matched = 0;
  2573. + fidir = au_fi(file)->fi_hdir;
  2574. + AuDebugOn(!fidir);
  2575. + bbot = au_fbbot_dir(file);
  2576. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
  2577. + hfile = fidir->fd_hfile + bindex;
  2578. + if (!hfile->hf_file)
  2579. + continue;
  2580. +
  2581. + if (hfile->hf_br->br_id == br_id) {
  2582. + matched = 1;
  2583. + break;
  2584. + }
  2585. + }
  2586. + if (matched)
  2587. + err = -EBUSY;
  2588. +
  2589. +out:
  2590. + return err;
  2591. +}
  2592. +
  2593. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2594. + struct file **to_free, int opened)
  2595. +{
  2596. + int err, idx;
  2597. + unsigned long long ull, max;
  2598. + aufs_bindex_t btop;
  2599. + struct file *file, **array;
  2600. + struct dentry *root;
  2601. + struct au_hfile *hfile;
  2602. +
  2603. + array = au_farray_alloc(sb, &max);
  2604. + err = PTR_ERR(array);
  2605. + if (IS_ERR(array))
  2606. + goto out;
  2607. +
  2608. + err = 0;
  2609. + idx = 0;
  2610. + root = sb->s_root;
  2611. + di_write_unlock(root);
  2612. + for (ull = 0; ull < max; ull++) {
  2613. + file = array[ull];
  2614. + if (unlikely(!file))
  2615. + break;
  2616. +
  2617. + /* AuDbg("%pD\n", file); */
  2618. + fi_read_lock(file);
  2619. + btop = au_fbtop(file);
  2620. + if (!d_is_dir(file->f_path.dentry)) {
  2621. + hfile = &au_fi(file)->fi_htop;
  2622. + if (hfile->hf_br->br_id == br_id)
  2623. + err = -EBUSY;
  2624. + } else
  2625. + err = test_dir_busy(file, br_id, to_free, &idx);
  2626. + fi_read_unlock(file);
  2627. + if (unlikely(err))
  2628. + break;
  2629. + }
  2630. + di_write_lock_child(root);
  2631. + au_farray_free(array, max);
  2632. + AuDebugOn(idx > opened);
  2633. +
  2634. +out:
  2635. + return err;
  2636. +}
  2637. +
  2638. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2639. + aufs_bindex_t br_id)
  2640. +{
  2641. + unsigned long long ull;
  2642. + aufs_bindex_t bindex, btop, bbot, bfound;
  2643. + struct file *file;
  2644. + struct au_fidir *fidir;
  2645. + struct au_hfile *hfile;
  2646. +
  2647. + for (ull = 0; ull < opened; ull++) {
  2648. + file = to_free[ull];
  2649. + if (unlikely(!file))
  2650. + break;
  2651. +
  2652. + /* AuDbg("%pD\n", file); */
  2653. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2654. + bfound = -1;
  2655. + fidir = au_fi(file)->fi_hdir;
  2656. + AuDebugOn(!fidir);
  2657. + fi_write_lock(file);
  2658. + btop = au_fbtop(file);
  2659. + bbot = au_fbbot_dir(file);
  2660. + for (bindex = btop; bindex <= bbot; bindex++) {
  2661. + hfile = fidir->fd_hfile + bindex;
  2662. + if (!hfile->hf_file)
  2663. + continue;
  2664. +
  2665. + if (hfile->hf_br->br_id == br_id) {
  2666. + bfound = bindex;
  2667. + break;
  2668. + }
  2669. + }
  2670. + AuDebugOn(bfound < 0);
  2671. + au_set_h_fptr(file, bfound, NULL);
  2672. + if (bfound == btop) {
  2673. + for (btop++; btop <= bbot; btop++)
  2674. + if (au_hf_dir(file, btop)) {
  2675. + au_set_fbtop(file, btop);
  2676. + break;
  2677. + }
  2678. + }
  2679. + fi_write_unlock(file);
  2680. + }
  2681. +}
  2682. +
  2683. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2684. + const aufs_bindex_t bindex,
  2685. + const aufs_bindex_t bbot)
  2686. +{
  2687. + struct au_branch **brp, **p;
  2688. +
  2689. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2690. +
  2691. + brp = sbinfo->si_branch + bindex;
  2692. + if (bindex < bbot)
  2693. + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
  2694. + sbinfo->si_branch[0 + bbot] = NULL;
  2695. + sbinfo->si_bbot--;
  2696. +
  2697. + p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
  2698. + /*may_shrink*/1);
  2699. + if (p)
  2700. + sbinfo->si_branch = p;
  2701. + /* harmless error */
  2702. +}
  2703. +
  2704. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2705. + const aufs_bindex_t bbot)
  2706. +{
  2707. + struct au_hdentry *hdp, *p;
  2708. +
  2709. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2710. +
  2711. + hdp = au_hdentry(dinfo, bindex);
  2712. + if (bindex < bbot)
  2713. + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
  2714. + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
  2715. + dinfo->di_bbot--;
  2716. +
  2717. + p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
  2718. + /*may_shrink*/1);
  2719. + if (p)
  2720. + dinfo->di_hdentry = p;
  2721. + /* harmless error */
  2722. +}
  2723. +
  2724. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2725. + const aufs_bindex_t bbot)
  2726. +{
  2727. + struct au_hinode *hip, *p;
  2728. +
  2729. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2730. +
  2731. + hip = au_hinode(iinfo, bindex);
  2732. + if (bindex < bbot)
  2733. + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
  2734. + /* au_hinode_init(au_hinode(iinfo, bbot)); */
  2735. + iinfo->ii_bbot--;
  2736. +
  2737. + p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
  2738. + /*may_shrink*/1);
  2739. + if (p)
  2740. + iinfo->ii_hinode = p;
  2741. + /* harmless error */
  2742. +}
  2743. +
  2744. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2745. + struct au_branch *br)
  2746. +{
  2747. + aufs_bindex_t bbot;
  2748. + struct au_sbinfo *sbinfo;
  2749. + struct dentry *root, *h_root;
  2750. + struct inode *inode, *h_inode;
  2751. + struct au_hinode *hinode;
  2752. +
  2753. + SiMustWriteLock(sb);
  2754. +
  2755. + root = sb->s_root;
  2756. + inode = d_inode(root);
  2757. + sbinfo = au_sbi(sb);
  2758. + bbot = sbinfo->si_bbot;
  2759. +
  2760. + h_root = au_h_dptr(root, bindex);
  2761. + hinode = au_hi(inode, bindex);
  2762. + h_inode = au_igrab(hinode->hi_inode);
  2763. + au_hiput(hinode);
  2764. +
  2765. + au_sbilist_lock();
  2766. + au_br_do_del_brp(sbinfo, bindex, bbot);
  2767. + au_br_do_del_hdp(au_di(root), bindex, bbot);
  2768. + au_br_do_del_hip(au_ii(inode), bindex, bbot);
  2769. + au_sbilist_unlock();
  2770. +
  2771. + dput(h_root);
  2772. + iput(h_inode);
  2773. + au_br_do_free(br);
  2774. +}
  2775. +
  2776. +static unsigned long long empty_cb(struct super_block *sb, void *array,
  2777. + unsigned long long max, void *arg)
  2778. +{
  2779. + return max;
  2780. +}
  2781. +
  2782. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2783. +{
  2784. + int err, rerr, i;
  2785. + unsigned long long opened;
  2786. + unsigned int mnt_flags;
  2787. + aufs_bindex_t bindex, bbot, br_id;
  2788. + unsigned char do_wh, verbose;
  2789. + struct au_branch *br;
  2790. + struct au_wbr *wbr;
  2791. + struct dentry *root;
  2792. + struct file **to_free;
  2793. +
  2794. + err = 0;
  2795. + opened = 0;
  2796. + to_free = NULL;
  2797. + root = sb->s_root;
  2798. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2799. + if (bindex < 0) {
  2800. + if (remount)
  2801. + goto out; /* success */
  2802. + err = -ENOENT;
  2803. + pr_err("%s no such branch\n", del->pathname);
  2804. + goto out;
  2805. + }
  2806. + AuDbg("bindex b%d\n", bindex);
  2807. +
  2808. + err = -EBUSY;
  2809. + mnt_flags = au_mntflags(sb);
  2810. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2811. + bbot = au_sbbot(sb);
  2812. + if (unlikely(!bbot)) {
  2813. + AuVerbose(verbose, "no more branches left\n");
  2814. + goto out;
  2815. + }
  2816. + br = au_sbr(sb, bindex);
  2817. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  2818. +
  2819. + br_id = br->br_id;
  2820. + opened = au_br_count(br);
  2821. + if (unlikely(opened)) {
  2822. + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
  2823. + err = PTR_ERR(to_free);
  2824. + if (IS_ERR(to_free))
  2825. + goto out;
  2826. +
  2827. + err = test_file_busy(sb, br_id, to_free, opened);
  2828. + if (unlikely(err)) {
  2829. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  2830. + goto out;
  2831. + }
  2832. + }
  2833. +
  2834. + wbr = br->br_wbr;
  2835. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  2836. + if (do_wh) {
  2837. + /* instead of WbrWhMustWriteLock(wbr) */
  2838. + SiMustWriteLock(sb);
  2839. + for (i = 0; i < AuBrWh_Last; i++) {
  2840. + dput(wbr->wbr_wh[i]);
  2841. + wbr->wbr_wh[i] = NULL;
  2842. + }
  2843. + }
  2844. +
  2845. + err = test_children_busy(root, bindex, verbose);
  2846. + if (unlikely(err)) {
  2847. + if (do_wh)
  2848. + goto out_wh;
  2849. + goto out;
  2850. + }
  2851. +
  2852. + err = 0;
  2853. + if (to_free) {
  2854. + /*
  2855. + * now we confirmed the branch is deletable.
  2856. + * let's free the remaining opened dirs on the branch.
  2857. + */
  2858. + di_write_unlock(root);
  2859. + br_del_file(to_free, opened, br_id);
  2860. + di_write_lock_child(root);
  2861. + }
  2862. +
  2863. + if (!remount)
  2864. + au_br_do_del(sb, bindex, br);
  2865. + else {
  2866. + sysaufs_brs_del(sb, bindex);
  2867. + au_br_do_del(sb, bindex, br);
  2868. + sysaufs_brs_add(sb, bindex);
  2869. + }
  2870. +
  2871. + if (!bindex) {
  2872. + au_cpup_attr_all(d_inode(root), /*force*/1);
  2873. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  2874. + } else
  2875. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  2876. + if (au_opt_test(mnt_flags, PLINK))
  2877. + au_plink_half_refresh(sb, br_id);
  2878. +
  2879. + if (au_xino_brid(sb) == br_id)
  2880. + au_xino_brid_set(sb, -1);
  2881. + goto out; /* success */
  2882. +
  2883. +out_wh:
  2884. + /* revert */
  2885. + rerr = au_br_init_wh(sb, br, br->br_perm);
  2886. + if (rerr)
  2887. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  2888. + del->pathname, rerr);
  2889. +out:
  2890. + if (to_free)
  2891. + au_farray_free(to_free, opened);
  2892. + return err;
  2893. +}
  2894. +
  2895. +/* ---------------------------------------------------------------------- */
  2896. +
  2897. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  2898. +{
  2899. + int err;
  2900. + aufs_bindex_t btop, bbot;
  2901. + struct aufs_ibusy ibusy;
  2902. + struct inode *inode, *h_inode;
  2903. +
  2904. + err = -EPERM;
  2905. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  2906. + goto out;
  2907. +
  2908. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  2909. + if (!err)
  2910. + err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
  2911. + if (unlikely(err)) {
  2912. + err = -EFAULT;
  2913. + AuTraceErr(err);
  2914. + goto out;
  2915. + }
  2916. +
  2917. + err = -EINVAL;
  2918. + si_read_lock(sb, AuLock_FLUSH);
  2919. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
  2920. + goto out_unlock;
  2921. +
  2922. + err = 0;
  2923. + ibusy.h_ino = 0; /* invalid */
  2924. + inode = ilookup(sb, ibusy.ino);
  2925. + if (!inode
  2926. + || inode->i_ino == AUFS_ROOT_INO
  2927. + || au_is_bad_inode(inode))
  2928. + goto out_unlock;
  2929. +
  2930. + ii_read_lock_child(inode);
  2931. + btop = au_ibtop(inode);
  2932. + bbot = au_ibbot(inode);
  2933. + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
  2934. + h_inode = au_h_iptr(inode, ibusy.bindex);
  2935. + if (h_inode && au_test_ibusy(inode, btop, bbot))
  2936. + ibusy.h_ino = h_inode->i_ino;
  2937. + }
  2938. + ii_read_unlock(inode);
  2939. + iput(inode);
  2940. +
  2941. +out_unlock:
  2942. + si_read_unlock(sb);
  2943. + if (!err) {
  2944. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  2945. + if (unlikely(err)) {
  2946. + err = -EFAULT;
  2947. + AuTraceErr(err);
  2948. + }
  2949. + }
  2950. +out:
  2951. + return err;
  2952. +}
  2953. +
  2954. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  2955. +{
  2956. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  2957. +}
  2958. +
  2959. +#ifdef CONFIG_COMPAT
  2960. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  2961. +{
  2962. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  2963. +}
  2964. +#endif
  2965. +
  2966. +/* ---------------------------------------------------------------------- */
  2967. +
  2968. +/*
  2969. + * change a branch permission
  2970. + */
  2971. +
  2972. +static void au_warn_ima(void)
  2973. +{
  2974. +#ifdef CONFIG_IMA
  2975. + /* since it doesn't support mark_files_ro() */
  2976. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  2977. +#endif
  2978. +}
  2979. +
  2980. +static int do_need_sigen_inc(int a, int b)
  2981. +{
  2982. + return au_br_whable(a) && !au_br_whable(b);
  2983. +}
  2984. +
  2985. +static int need_sigen_inc(int old, int new)
  2986. +{
  2987. + return do_need_sigen_inc(old, new)
  2988. + || do_need_sigen_inc(new, old);
  2989. +}
  2990. +
  2991. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  2992. +{
  2993. + int err, do_warn;
  2994. + unsigned int mnt_flags;
  2995. + unsigned long long ull, max;
  2996. + aufs_bindex_t br_id;
  2997. + unsigned char verbose, writer;
  2998. + struct file *file, *hf, **array;
  2999. + struct au_hfile *hfile;
  3000. +
  3001. + mnt_flags = au_mntflags(sb);
  3002. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3003. +
  3004. + array = au_farray_alloc(sb, &max);
  3005. + err = PTR_ERR(array);
  3006. + if (IS_ERR(array))
  3007. + goto out;
  3008. +
  3009. + do_warn = 0;
  3010. + br_id = au_sbr_id(sb, bindex);
  3011. + for (ull = 0; ull < max; ull++) {
  3012. + file = array[ull];
  3013. + if (unlikely(!file))
  3014. + break;
  3015. +
  3016. + /* AuDbg("%pD\n", file); */
  3017. + fi_read_lock(file);
  3018. + if (unlikely(au_test_mmapped(file))) {
  3019. + err = -EBUSY;
  3020. + AuVerbose(verbose, "mmapped %pD\n", file);
  3021. + AuDbgFile(file);
  3022. + FiMustNoWaiters(file);
  3023. + fi_read_unlock(file);
  3024. + goto out_array;
  3025. + }
  3026. +
  3027. + hfile = &au_fi(file)->fi_htop;
  3028. + hf = hfile->hf_file;
  3029. + if (!d_is_reg(file->f_path.dentry)
  3030. + || !(file->f_mode & FMODE_WRITE)
  3031. + || hfile->hf_br->br_id != br_id
  3032. + || !(hf->f_mode & FMODE_WRITE))
  3033. + array[ull] = NULL;
  3034. + else {
  3035. + do_warn = 1;
  3036. + get_file(file);
  3037. + }
  3038. +
  3039. + FiMustNoWaiters(file);
  3040. + fi_read_unlock(file);
  3041. + fput(file);
  3042. + }
  3043. +
  3044. + err = 0;
  3045. + if (do_warn)
  3046. + au_warn_ima();
  3047. +
  3048. + for (ull = 0; ull < max; ull++) {
  3049. + file = array[ull];
  3050. + if (!file)
  3051. + continue;
  3052. +
  3053. + /* todo: already flushed? */
  3054. + /*
  3055. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3056. + * approach which resets f_mode and calls mnt_drop_write() and
  3057. + * file_release_write() for each file, because the branch
  3058. + * attribute in aufs world is totally different from the native
  3059. + * fs rw/ro mode.
  3060. + */
  3061. + /* fi_read_lock(file); */
  3062. + hfile = &au_fi(file)->fi_htop;
  3063. + hf = hfile->hf_file;
  3064. + /* fi_read_unlock(file); */
  3065. + spin_lock(&hf->f_lock);
  3066. + writer = !!(hf->f_mode & FMODE_WRITER);
  3067. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3068. + spin_unlock(&hf->f_lock);
  3069. + if (writer) {
  3070. + put_write_access(file_inode(hf));
  3071. + __mnt_drop_write(hf->f_path.mnt);
  3072. + }
  3073. + }
  3074. +
  3075. +out_array:
  3076. + au_farray_free(array, max);
  3077. +out:
  3078. + AuTraceErr(err);
  3079. + return err;
  3080. +}
  3081. +
  3082. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3083. + int *do_refresh)
  3084. +{
  3085. + int err, rerr;
  3086. + aufs_bindex_t bindex;
  3087. + struct dentry *root;
  3088. + struct au_branch *br;
  3089. + struct au_br_fhsm *bf;
  3090. +
  3091. + root = sb->s_root;
  3092. + bindex = au_find_dbindex(root, mod->h_root);
  3093. + if (bindex < 0) {
  3094. + if (remount)
  3095. + return 0; /* success */
  3096. + err = -ENOENT;
  3097. + pr_err("%s no such branch\n", mod->path);
  3098. + goto out;
  3099. + }
  3100. + AuDbg("bindex b%d\n", bindex);
  3101. +
  3102. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3103. + if (unlikely(err))
  3104. + goto out;
  3105. +
  3106. + br = au_sbr(sb, bindex);
  3107. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3108. + if (br->br_perm == mod->perm)
  3109. + return 0; /* success */
  3110. +
  3111. + /* pre-allocate for non-fhsm --> fhsm */
  3112. + bf = NULL;
  3113. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3114. + err = au_fhsm_br_alloc(br);
  3115. + if (unlikely(err))
  3116. + goto out;
  3117. + bf = br->br_fhsm;
  3118. + br->br_fhsm = NULL;
  3119. + }
  3120. +
  3121. + if (au_br_writable(br->br_perm)) {
  3122. + /* remove whiteout base */
  3123. + err = au_br_init_wh(sb, br, mod->perm);
  3124. + if (unlikely(err))
  3125. + goto out_bf;
  3126. +
  3127. + if (!au_br_writable(mod->perm)) {
  3128. + /* rw --> ro, file might be mmapped */
  3129. + DiMustNoWaiters(root);
  3130. + IiMustNoWaiters(d_inode(root));
  3131. + di_write_unlock(root);
  3132. + err = au_br_mod_files_ro(sb, bindex);
  3133. + /* aufs_write_lock() calls ..._child() */
  3134. + di_write_lock_child(root);
  3135. +
  3136. + if (unlikely(err)) {
  3137. + rerr = -ENOMEM;
  3138. + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
  3139. + GFP_NOFS);
  3140. + if (br->br_wbr)
  3141. + rerr = au_wbr_init(br, sb, br->br_perm);
  3142. + if (unlikely(rerr)) {
  3143. + AuIOErr("nested error %d (%d)\n",
  3144. + rerr, err);
  3145. + br->br_perm = mod->perm;
  3146. + }
  3147. + }
  3148. + }
  3149. + } else if (au_br_writable(mod->perm)) {
  3150. + /* ro --> rw */
  3151. + err = -ENOMEM;
  3152. + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3153. + if (br->br_wbr) {
  3154. + err = au_wbr_init(br, sb, mod->perm);
  3155. + if (unlikely(err)) {
  3156. + au_delayed_kfree(br->br_wbr);
  3157. + br->br_wbr = NULL;
  3158. + }
  3159. + }
  3160. + }
  3161. + if (unlikely(err))
  3162. + goto out_bf;
  3163. +
  3164. + if (au_br_fhsm(br->br_perm)) {
  3165. + if (!au_br_fhsm(mod->perm)) {
  3166. + /* fhsm --> non-fhsm */
  3167. + au_br_fhsm_fin(br->br_fhsm);
  3168. + au_delayed_kfree(br->br_fhsm);
  3169. + br->br_fhsm = NULL;
  3170. + }
  3171. + } else if (au_br_fhsm(mod->perm))
  3172. + /* non-fhsm --> fhsm */
  3173. + br->br_fhsm = bf;
  3174. +
  3175. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3176. + br->br_perm = mod->perm;
  3177. + goto out; /* success */
  3178. +
  3179. +out_bf:
  3180. + if (bf)
  3181. + au_delayed_kfree(bf);
  3182. +out:
  3183. + AuTraceErr(err);
  3184. + return err;
  3185. +}
  3186. +
  3187. +/* ---------------------------------------------------------------------- */
  3188. +
  3189. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3190. +{
  3191. + int err;
  3192. + struct kstatfs kstfs;
  3193. +
  3194. + err = vfs_statfs(&br->br_path, &kstfs);
  3195. + if (!err) {
  3196. + stfs->f_blocks = kstfs.f_blocks;
  3197. + stfs->f_bavail = kstfs.f_bavail;
  3198. + stfs->f_files = kstfs.f_files;
  3199. + stfs->f_ffree = kstfs.f_ffree;
  3200. + }
  3201. +
  3202. + return err;
  3203. +}
  3204. --- /dev/null
  3205. +++ linux-4.4/fs/aufs/branch.h 2016-10-23 11:20:57.622159952 +0300
  3206. @@ -0,0 +1,309 @@
  3207. +/*
  3208. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  3209. + *
  3210. + * This program, aufs is free software; you can redistribute it and/or modify
  3211. + * it under the terms of the GNU General Public License as published by
  3212. + * the Free Software Foundation; either version 2 of the License, or
  3213. + * (at your option) any later version.
  3214. + *
  3215. + * This program is distributed in the hope that it will be useful,
  3216. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3217. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3218. + * GNU General Public License for more details.
  3219. + *
  3220. + * You should have received a copy of the GNU General Public License
  3221. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3222. + */
  3223. +
  3224. +/*
  3225. + * branch filesystems and xino for them
  3226. + */
  3227. +
  3228. +#ifndef __AUFS_BRANCH_H__
  3229. +#define __AUFS_BRANCH_H__
  3230. +
  3231. +#ifdef __KERNEL__
  3232. +
  3233. +#include <linux/mount.h>
  3234. +#include "dynop.h"
  3235. +#include "rwsem.h"
  3236. +#include "super.h"
  3237. +
  3238. +/* ---------------------------------------------------------------------- */
  3239. +
  3240. +/* a xino file */
  3241. +struct au_xino_file {
  3242. + struct file *xi_file;
  3243. + struct mutex xi_nondir_mtx;
  3244. +
  3245. + /* todo: make xino files an array to support huge inode number */
  3246. +
  3247. +#ifdef CONFIG_DEBUG_FS
  3248. + struct dentry *xi_dbgaufs;
  3249. +#endif
  3250. +};
  3251. +
  3252. +/* File-based Hierarchical Storage Management */
  3253. +struct au_br_fhsm {
  3254. +#ifdef CONFIG_AUFS_FHSM
  3255. + struct mutex bf_lock;
  3256. + unsigned long bf_jiffy;
  3257. + struct aufs_stfs bf_stfs;
  3258. + int bf_readable;
  3259. +#endif
  3260. +};
  3261. +
  3262. +/* members for writable branch only */
  3263. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3264. +struct au_wbr {
  3265. + struct au_rwsem wbr_wh_rwsem;
  3266. + struct dentry *wbr_wh[AuBrWh_Last];
  3267. + atomic_t wbr_wh_running;
  3268. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3269. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3270. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3271. +
  3272. + /* mfs mode */
  3273. + unsigned long long wbr_bytes;
  3274. +};
  3275. +
  3276. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3277. +#define AuBrDynOp (AuDyLast * 4)
  3278. +
  3279. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3280. +/* support for asynchronous destruction */
  3281. +struct au_br_hfsnotify {
  3282. + struct fsnotify_group *hfsn_group;
  3283. +};
  3284. +#endif
  3285. +
  3286. +/* sysfs entries */
  3287. +struct au_brsysfs {
  3288. + char name[16];
  3289. + struct attribute attr;
  3290. +};
  3291. +
  3292. +enum {
  3293. + AuBrSysfs_BR,
  3294. + AuBrSysfs_BRID,
  3295. + AuBrSysfs_Last
  3296. +};
  3297. +
  3298. +/* protected by superblock rwsem */
  3299. +struct au_branch {
  3300. + struct au_xino_file br_xino;
  3301. +
  3302. + aufs_bindex_t br_id;
  3303. +
  3304. + int br_perm;
  3305. + struct path br_path;
  3306. + spinlock_t br_dykey_lock;
  3307. + struct au_dykey *br_dykey[AuBrDynOp];
  3308. + struct percpu_counter br_count;
  3309. +
  3310. + struct au_wbr *br_wbr;
  3311. + struct au_br_fhsm *br_fhsm;
  3312. +
  3313. + /* xino truncation */
  3314. + atomic_t br_xino_running;
  3315. +
  3316. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3317. + struct au_br_hfsnotify *br_hfsn;
  3318. +#endif
  3319. +
  3320. +#ifdef CONFIG_SYSFS
  3321. + /* entries under sysfs per mount-point */
  3322. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3323. +#endif
  3324. +};
  3325. +
  3326. +/* ---------------------------------------------------------------------- */
  3327. +
  3328. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3329. +{
  3330. + return br->br_path.mnt;
  3331. +}
  3332. +
  3333. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3334. +{
  3335. + return br->br_path.dentry;
  3336. +}
  3337. +
  3338. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3339. +{
  3340. + return au_br_mnt(br)->mnt_sb;
  3341. +}
  3342. +
  3343. +static inline void au_br_get(struct au_branch *br)
  3344. +{
  3345. + percpu_counter_inc(&br->br_count);
  3346. +}
  3347. +
  3348. +static inline void au_br_put(struct au_branch *br)
  3349. +{
  3350. + percpu_counter_dec(&br->br_count);
  3351. +}
  3352. +
  3353. +static inline s64 au_br_count(struct au_branch *br)
  3354. +{
  3355. + return percpu_counter_sum(&br->br_count);
  3356. +}
  3357. +
  3358. +static inline void au_br_count_init(struct au_branch *br)
  3359. +{
  3360. + percpu_counter_init(&br->br_count, 0, GFP_NOFS);
  3361. +}
  3362. +
  3363. +static inline void au_br_count_fin(struct au_branch *br)
  3364. +{
  3365. + percpu_counter_destroy(&br->br_count);
  3366. +}
  3367. +
  3368. +static inline int au_br_rdonly(struct au_branch *br)
  3369. +{
  3370. + return ((au_br_sb(br)->s_flags & MS_RDONLY)
  3371. + || !au_br_writable(br->br_perm))
  3372. + ? -EROFS : 0;
  3373. +}
  3374. +
  3375. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3376. +{
  3377. +#ifdef CONFIG_AUFS_HNOTIFY
  3378. + return !(brperm & AuBrPerm_RR);
  3379. +#else
  3380. + return 0;
  3381. +#endif
  3382. +}
  3383. +
  3384. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3385. +{
  3386. + int err, exec_flag;
  3387. +
  3388. + err = 0;
  3389. + exec_flag = oflag & __FMODE_EXEC;
  3390. + if (unlikely(exec_flag && path_noexec(&br->br_path)))
  3391. + err = -EACCES;
  3392. +
  3393. + return err;
  3394. +}
  3395. +
  3396. +/* ---------------------------------------------------------------------- */
  3397. +
  3398. +/* branch.c */
  3399. +struct au_sbinfo;
  3400. +void au_br_free(struct au_sbinfo *sinfo);
  3401. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3402. +struct au_opt_add;
  3403. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3404. +struct au_opt_del;
  3405. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3406. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3407. +#ifdef CONFIG_COMPAT
  3408. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3409. +#endif
  3410. +struct au_opt_mod;
  3411. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3412. + int *do_refresh);
  3413. +struct aufs_stfs;
  3414. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3415. +
  3416. +/* xino.c */
  3417. +static const loff_t au_loff_max = LLONG_MAX;
  3418. +
  3419. +int au_xib_trunc(struct super_block *sb);
  3420. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
  3421. + loff_t *pos);
  3422. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  3423. + size_t size, loff_t *pos);
  3424. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
  3425. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
  3426. +ino_t au_xino_new_ino(struct super_block *sb);
  3427. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3428. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3429. + ino_t ino);
  3430. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3431. + ino_t *ino);
  3432. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3433. + struct file *base_file, int do_test);
  3434. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
  3435. +
  3436. +struct au_opt_xino;
  3437. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
  3438. +void au_xino_clr(struct super_block *sb);
  3439. +struct file *au_xino_def(struct super_block *sb);
  3440. +int au_xino_path(struct seq_file *seq, struct file *file);
  3441. +
  3442. +/* ---------------------------------------------------------------------- */
  3443. +
  3444. +/* Superblock to branch */
  3445. +static inline
  3446. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3447. +{
  3448. + return au_sbr(sb, bindex)->br_id;
  3449. +}
  3450. +
  3451. +static inline
  3452. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3453. +{
  3454. + return au_br_mnt(au_sbr(sb, bindex));
  3455. +}
  3456. +
  3457. +static inline
  3458. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3459. +{
  3460. + return au_br_sb(au_sbr(sb, bindex));
  3461. +}
  3462. +
  3463. +static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
  3464. +{
  3465. + au_br_get(au_sbr(sb, bindex));
  3466. +}
  3467. +
  3468. +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
  3469. +{
  3470. + au_br_put(au_sbr(sb, bindex));
  3471. +}
  3472. +
  3473. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3474. +{
  3475. + return au_sbr(sb, bindex)->br_perm;
  3476. +}
  3477. +
  3478. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3479. +{
  3480. + return au_br_whable(au_sbr_perm(sb, bindex));
  3481. +}
  3482. +
  3483. +/* ---------------------------------------------------------------------- */
  3484. +
  3485. +/*
  3486. + * wbr_wh_read_lock, wbr_wh_write_lock
  3487. + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
  3488. + */
  3489. +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
  3490. +
  3491. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
  3492. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
  3493. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
  3494. +
  3495. +/* ---------------------------------------------------------------------- */
  3496. +
  3497. +#ifdef CONFIG_AUFS_FHSM
  3498. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3499. +{
  3500. + mutex_init(&brfhsm->bf_lock);
  3501. + brfhsm->bf_jiffy = 0;
  3502. + brfhsm->bf_readable = 0;
  3503. +}
  3504. +
  3505. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3506. +{
  3507. + mutex_destroy(&brfhsm->bf_lock);
  3508. +}
  3509. +#else
  3510. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3511. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3512. +#endif
  3513. +
  3514. +#endif /* __KERNEL__ */
  3515. +#endif /* __AUFS_BRANCH_H__ */
  3516. --- /dev/null
  3517. +++ linux-4.4/fs/aufs/conf.mk 2016-10-23 11:20:57.625493286 +0300
  3518. @@ -0,0 +1,38 @@
  3519. +
  3520. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3521. +
  3522. +define AuConf
  3523. +ifdef ${1}
  3524. +AuConfStr += ${1}=${${1}}
  3525. +endif
  3526. +endef
  3527. +
  3528. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3529. + SBILIST \
  3530. + HNOTIFY HFSNOTIFY \
  3531. + EXPORT INO_T_64 \
  3532. + XATTR \
  3533. + FHSM \
  3534. + RDU \
  3535. + SHWH \
  3536. + BR_RAMFS \
  3537. + BR_FUSE POLL \
  3538. + BR_HFSPLUS \
  3539. + BDEV_LOOP \
  3540. + DEBUG MAGIC_SYSRQ
  3541. +$(foreach i, ${AuConfAll}, \
  3542. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3543. +
  3544. +AuConfName = ${obj}/conf.str
  3545. +${AuConfName}.tmp: FORCE
  3546. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3547. +${AuConfName}: ${AuConfName}.tmp
  3548. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3549. + echo ' GEN ' $@; \
  3550. + cp -p $< $@; \
  3551. + }
  3552. +FORCE:
  3553. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3554. +${obj}/sysfs.o: ${AuConfName}
  3555. +
  3556. +-include ${srctree}/${src}/conf_priv.mk
  3557. --- /dev/null
  3558. +++ linux-4.4/fs/aufs/cpup.c 2016-10-23 11:20:57.625493286 +0300
  3559. @@ -0,0 +1,1383 @@
  3560. +/*
  3561. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  3562. + *
  3563. + * This program, aufs is free software; you can redistribute it and/or modify
  3564. + * it under the terms of the GNU General Public License as published by
  3565. + * the Free Software Foundation; either version 2 of the License, or
  3566. + * (at your option) any later version.
  3567. + *
  3568. + * This program is distributed in the hope that it will be useful,
  3569. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3570. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3571. + * GNU General Public License for more details.
  3572. + *
  3573. + * You should have received a copy of the GNU General Public License
  3574. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3575. + */
  3576. +
  3577. +/*
  3578. + * copy-up functions, see wbr_policy.c for copy-down
  3579. + */
  3580. +
  3581. +#include <linux/fs_stack.h>
  3582. +#include <linux/mm.h>
  3583. +#include <linux/task_work.h>
  3584. +#include "aufs.h"
  3585. +
  3586. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3587. +{
  3588. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3589. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3590. +
  3591. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3592. +
  3593. + dst->i_flags |= iflags & ~mask;
  3594. + if (au_test_fs_notime(dst->i_sb))
  3595. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3596. +}
  3597. +
  3598. +void au_cpup_attr_timesizes(struct inode *inode)
  3599. +{
  3600. + struct inode *h_inode;
  3601. +
  3602. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3603. + fsstack_copy_attr_times(inode, h_inode);
  3604. + fsstack_copy_inode_size(inode, h_inode);
  3605. +}
  3606. +
  3607. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3608. +{
  3609. + struct inode *h_inode;
  3610. + struct super_block *sb;
  3611. + aufs_bindex_t bindex, bbot;
  3612. +
  3613. + sb = inode->i_sb;
  3614. + bindex = au_ibtop(inode);
  3615. + h_inode = au_h_iptr(inode, bindex);
  3616. + if (!force
  3617. + && !S_ISDIR(h_inode->i_mode)
  3618. + && au_opt_test(au_mntflags(sb), PLINK)
  3619. + && au_plink_test(inode))
  3620. + return;
  3621. +
  3622. + /*
  3623. + * 0 can happen in revalidating.
  3624. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3625. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3626. + * case.
  3627. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3628. + * the incorrect link count.
  3629. + */
  3630. + set_nlink(inode, h_inode->i_nlink);
  3631. +
  3632. + /*
  3633. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3634. + * it may includes whplink directory.
  3635. + */
  3636. + if (S_ISDIR(h_inode->i_mode)) {
  3637. + bbot = au_ibbot(inode);
  3638. + for (bindex++; bindex <= bbot; bindex++) {
  3639. + h_inode = au_h_iptr(inode, bindex);
  3640. + if (h_inode)
  3641. + au_add_nlink(inode, h_inode);
  3642. + }
  3643. + }
  3644. +}
  3645. +
  3646. +void au_cpup_attr_changeable(struct inode *inode)
  3647. +{
  3648. + struct inode *h_inode;
  3649. +
  3650. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3651. + inode->i_mode = h_inode->i_mode;
  3652. + inode->i_uid = h_inode->i_uid;
  3653. + inode->i_gid = h_inode->i_gid;
  3654. + au_cpup_attr_timesizes(inode);
  3655. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3656. +}
  3657. +
  3658. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3659. +{
  3660. + struct au_iinfo *iinfo = au_ii(inode);
  3661. +
  3662. + IiMustWriteLock(inode);
  3663. +
  3664. + iinfo->ii_higen = h_inode->i_generation;
  3665. + iinfo->ii_hsb1 = h_inode->i_sb;
  3666. +}
  3667. +
  3668. +void au_cpup_attr_all(struct inode *inode, int force)
  3669. +{
  3670. + struct inode *h_inode;
  3671. +
  3672. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3673. + au_cpup_attr_changeable(inode);
  3674. + if (inode->i_nlink > 0)
  3675. + au_cpup_attr_nlink(inode, force);
  3676. + inode->i_rdev = h_inode->i_rdev;
  3677. + inode->i_blkbits = h_inode->i_blkbits;
  3678. + au_cpup_igen(inode, h_inode);
  3679. +}
  3680. +
  3681. +/* ---------------------------------------------------------------------- */
  3682. +
  3683. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3684. +
  3685. +/* keep the timestamps of the parent dir when cpup */
  3686. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3687. + struct path *h_path)
  3688. +{
  3689. + struct inode *h_inode;
  3690. +
  3691. + dt->dt_dentry = dentry;
  3692. + dt->dt_h_path = *h_path;
  3693. + h_inode = d_inode(h_path->dentry);
  3694. + dt->dt_atime = h_inode->i_atime;
  3695. + dt->dt_mtime = h_inode->i_mtime;
  3696. + /* smp_mb(); */
  3697. +}
  3698. +
  3699. +void au_dtime_revert(struct au_dtime *dt)
  3700. +{
  3701. + struct iattr attr;
  3702. + int err;
  3703. +
  3704. + attr.ia_atime = dt->dt_atime;
  3705. + attr.ia_mtime = dt->dt_mtime;
  3706. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3707. + | ATTR_ATIME | ATTR_ATIME_SET;
  3708. +
  3709. + /* no delegation since this is a directory */
  3710. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3711. + if (unlikely(err))
  3712. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3713. +}
  3714. +
  3715. +/* ---------------------------------------------------------------------- */
  3716. +
  3717. +/* internal use only */
  3718. +struct au_cpup_reg_attr {
  3719. + int valid;
  3720. + struct kstat st;
  3721. + unsigned int iflags; /* inode->i_flags */
  3722. +};
  3723. +
  3724. +static noinline_for_stack
  3725. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  3726. + struct au_cpup_reg_attr *h_src_attr)
  3727. +{
  3728. + int err, sbits, icex;
  3729. + unsigned int mnt_flags;
  3730. + unsigned char verbose;
  3731. + struct iattr ia;
  3732. + struct path h_path;
  3733. + struct inode *h_isrc, *h_idst;
  3734. + struct kstat *h_st;
  3735. + struct au_branch *br;
  3736. +
  3737. + h_path.dentry = au_h_dptr(dst, bindex);
  3738. + h_idst = d_inode(h_path.dentry);
  3739. + br = au_sbr(dst->d_sb, bindex);
  3740. + h_path.mnt = au_br_mnt(br);
  3741. + h_isrc = d_inode(h_src);
  3742. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  3743. + | ATTR_ATIME | ATTR_MTIME
  3744. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  3745. + if (h_src_attr && h_src_attr->valid) {
  3746. + h_st = &h_src_attr->st;
  3747. + ia.ia_uid = h_st->uid;
  3748. + ia.ia_gid = h_st->gid;
  3749. + ia.ia_atime = h_st->atime;
  3750. + ia.ia_mtime = h_st->mtime;
  3751. + if (h_idst->i_mode != h_st->mode
  3752. + && !S_ISLNK(h_idst->i_mode)) {
  3753. + ia.ia_valid |= ATTR_MODE;
  3754. + ia.ia_mode = h_st->mode;
  3755. + }
  3756. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  3757. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  3758. + } else {
  3759. + ia.ia_uid = h_isrc->i_uid;
  3760. + ia.ia_gid = h_isrc->i_gid;
  3761. + ia.ia_atime = h_isrc->i_atime;
  3762. + ia.ia_mtime = h_isrc->i_mtime;
  3763. + if (h_idst->i_mode != h_isrc->i_mode
  3764. + && !S_ISLNK(h_idst->i_mode)) {
  3765. + ia.ia_valid |= ATTR_MODE;
  3766. + ia.ia_mode = h_isrc->i_mode;
  3767. + }
  3768. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  3769. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  3770. + }
  3771. + /* no delegation since it is just created */
  3772. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3773. +
  3774. + /* is this nfs only? */
  3775. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  3776. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  3777. + ia.ia_mode = h_isrc->i_mode;
  3778. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3779. + }
  3780. +
  3781. + icex = br->br_perm & AuBrAttr_ICEX;
  3782. + if (!err) {
  3783. + mnt_flags = au_mntflags(dst->d_sb);
  3784. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3785. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  3786. + }
  3787. +
  3788. + return err;
  3789. +}
  3790. +
  3791. +/* ---------------------------------------------------------------------- */
  3792. +
  3793. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  3794. + char *buf, unsigned long blksize)
  3795. +{
  3796. + int err;
  3797. + size_t sz, rbytes, wbytes;
  3798. + unsigned char all_zero;
  3799. + char *p, *zp;
  3800. + struct mutex *h_mtx;
  3801. + /* reduce stack usage */
  3802. + struct iattr *ia;
  3803. +
  3804. + zp = page_address(ZERO_PAGE(0));
  3805. + if (unlikely(!zp))
  3806. + return -ENOMEM; /* possible? */
  3807. +
  3808. + err = 0;
  3809. + all_zero = 0;
  3810. + while (len) {
  3811. + AuDbg("len %lld\n", len);
  3812. + sz = blksize;
  3813. + if (len < blksize)
  3814. + sz = len;
  3815. +
  3816. + rbytes = 0;
  3817. + /* todo: signal_pending? */
  3818. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  3819. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  3820. + err = rbytes;
  3821. + }
  3822. + if (unlikely(err < 0))
  3823. + break;
  3824. +
  3825. + all_zero = 0;
  3826. + if (len >= rbytes && rbytes == blksize)
  3827. + all_zero = !memcmp(buf, zp, rbytes);
  3828. + if (!all_zero) {
  3829. + wbytes = rbytes;
  3830. + p = buf;
  3831. + while (wbytes) {
  3832. + size_t b;
  3833. +
  3834. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  3835. + err = b;
  3836. + /* todo: signal_pending? */
  3837. + if (unlikely(err == -EAGAIN || err == -EINTR))
  3838. + continue;
  3839. + if (unlikely(err < 0))
  3840. + break;
  3841. + wbytes -= b;
  3842. + p += b;
  3843. + }
  3844. + if (unlikely(err < 0))
  3845. + break;
  3846. + } else {
  3847. + loff_t res;
  3848. +
  3849. + AuLabel(hole);
  3850. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  3851. + err = res;
  3852. + if (unlikely(res < 0))
  3853. + break;
  3854. + }
  3855. + len -= rbytes;
  3856. + err = 0;
  3857. + }
  3858. +
  3859. + /* the last block may be a hole */
  3860. + if (!err && all_zero) {
  3861. + AuLabel(last hole);
  3862. +
  3863. + err = 1;
  3864. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  3865. + /* nfs requires this step to make last hole */
  3866. + /* is this only nfs? */
  3867. + do {
  3868. + /* todo: signal_pending? */
  3869. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  3870. + } while (err == -EAGAIN || err == -EINTR);
  3871. + if (err == 1)
  3872. + dst->f_pos--;
  3873. + }
  3874. +
  3875. + if (err == 1) {
  3876. + ia = (void *)buf;
  3877. + ia->ia_size = dst->f_pos;
  3878. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  3879. + ia->ia_file = dst;
  3880. + h_mtx = &file_inode(dst)->i_mutex;
  3881. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  3882. + /* no delegation since it is just created */
  3883. + err = vfsub_notify_change(&dst->f_path, ia,
  3884. + /*delegated*/NULL);
  3885. + mutex_unlock(h_mtx);
  3886. + }
  3887. + }
  3888. +
  3889. + return err;
  3890. +}
  3891. +
  3892. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  3893. +{
  3894. + int err;
  3895. + unsigned long blksize;
  3896. + unsigned char do_kfree;
  3897. + char *buf;
  3898. +
  3899. + err = -ENOMEM;
  3900. + blksize = dst->f_path.dentry->d_sb->s_blocksize;
  3901. + if (!blksize || PAGE_SIZE < blksize)
  3902. + blksize = PAGE_SIZE;
  3903. + AuDbg("blksize %lu\n", blksize);
  3904. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  3905. + if (do_kfree)
  3906. + buf = kmalloc(blksize, GFP_NOFS);
  3907. + else
  3908. + buf = (void *)__get_free_page(GFP_NOFS);
  3909. + if (unlikely(!buf))
  3910. + goto out;
  3911. +
  3912. + if (len > (1 << 22))
  3913. + AuDbg("copying a large file %lld\n", (long long)len);
  3914. +
  3915. + src->f_pos = 0;
  3916. + dst->f_pos = 0;
  3917. + err = au_do_copy_file(dst, src, len, buf, blksize);
  3918. + if (do_kfree)
  3919. + au_delayed_kfree(buf);
  3920. + else
  3921. + au_delayed_free_page((unsigned long)buf);
  3922. +
  3923. +out:
  3924. + return err;
  3925. +}
  3926. +
  3927. +/*
  3928. + * to support a sparse file which is opened with O_APPEND,
  3929. + * we need to close the file.
  3930. + */
  3931. +static int au_cp_regular(struct au_cp_generic *cpg)
  3932. +{
  3933. + int err, i;
  3934. + enum { SRC, DST };
  3935. + struct {
  3936. + aufs_bindex_t bindex;
  3937. + unsigned int flags;
  3938. + struct dentry *dentry;
  3939. + int force_wr;
  3940. + struct file *file;
  3941. + void *label;
  3942. + } *f, file[] = {
  3943. + {
  3944. + .bindex = cpg->bsrc,
  3945. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  3946. + .label = &&out
  3947. + },
  3948. + {
  3949. + .bindex = cpg->bdst,
  3950. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  3951. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  3952. + .label = &&out_src
  3953. + }
  3954. + };
  3955. + struct super_block *sb;
  3956. + struct task_struct *tsk = current;
  3957. +
  3958. + /* bsrc branch can be ro/rw. */
  3959. + sb = cpg->dentry->d_sb;
  3960. + f = file;
  3961. + for (i = 0; i < 2; i++, f++) {
  3962. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  3963. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  3964. + /*file*/NULL, f->force_wr);
  3965. + err = PTR_ERR(f->file);
  3966. + if (IS_ERR(f->file))
  3967. + goto *f->label;
  3968. + }
  3969. +
  3970. + /* try stopping to update while we copyup */
  3971. + IMustLock(d_inode(file[SRC].dentry));
  3972. + err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
  3973. +
  3974. + /* i wonder if we had O_NO_DELAY_FPUT flag */
  3975. + if (tsk->flags & PF_KTHREAD)
  3976. + __fput_sync(file[DST].file);
  3977. + else {
  3978. + WARN(1, "%pD\nPlease report this warning to aufs-users ML",
  3979. + file[DST].file);
  3980. + fput(file[DST].file);
  3981. + /*
  3982. + * too bad.
  3983. + * we have to call both since we don't know which place the file
  3984. + * was added to.
  3985. + */
  3986. + task_work_run();
  3987. + flush_delayed_fput();
  3988. + }
  3989. + au_sbr_put(sb, file[DST].bindex);
  3990. +
  3991. +out_src:
  3992. + fput(file[SRC].file);
  3993. + au_sbr_put(sb, file[SRC].bindex);
  3994. +out:
  3995. + return err;
  3996. +}
  3997. +
  3998. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  3999. + struct au_cpup_reg_attr *h_src_attr)
  4000. +{
  4001. + int err, rerr;
  4002. + loff_t l;
  4003. + struct path h_path;
  4004. + struct inode *h_src_inode, *h_dst_inode;
  4005. +
  4006. + err = 0;
  4007. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4008. + l = i_size_read(h_src_inode);
  4009. + if (cpg->len == -1 || l < cpg->len)
  4010. + cpg->len = l;
  4011. + if (cpg->len) {
  4012. + /* try stopping to update while we are referencing */
  4013. + mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
  4014. + au_pin_hdir_unlock(cpg->pin);
  4015. +
  4016. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4017. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4018. + h_src_attr->iflags = h_src_inode->i_flags;
  4019. + if (!au_test_nfs(h_src_inode->i_sb))
  4020. + err = vfs_getattr(&h_path, &h_src_attr->st);
  4021. + else {
  4022. + mutex_unlock(&h_src_inode->i_mutex);
  4023. + err = vfs_getattr(&h_path, &h_src_attr->st);
  4024. + mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
  4025. + }
  4026. + if (unlikely(err)) {
  4027. + mutex_unlock(&h_src_inode->i_mutex);
  4028. + goto out;
  4029. + }
  4030. + h_src_attr->valid = 1;
  4031. + err = au_cp_regular(cpg);
  4032. + mutex_unlock(&h_src_inode->i_mutex);
  4033. + rerr = au_pin_hdir_relock(cpg->pin);
  4034. + if (!err && rerr)
  4035. + err = rerr;
  4036. + }
  4037. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4038. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4039. + h_dst_inode = d_inode(h_path.dentry);
  4040. + spin_lock(&h_dst_inode->i_lock);
  4041. + h_dst_inode->i_state |= I_LINKABLE;
  4042. + spin_unlock(&h_dst_inode->i_lock);
  4043. + }
  4044. +
  4045. +out:
  4046. + return err;
  4047. +}
  4048. +
  4049. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4050. + struct inode *h_dir)
  4051. +{
  4052. + int err, symlen;
  4053. + mm_segment_t old_fs;
  4054. + union {
  4055. + char *k;
  4056. + char __user *u;
  4057. + } sym;
  4058. + struct inode *h_inode = d_inode(h_src);
  4059. + const struct inode_operations *h_iop = h_inode->i_op;
  4060. +
  4061. + err = -ENOSYS;
  4062. + if (unlikely(!h_iop->readlink))
  4063. + goto out;
  4064. +
  4065. + err = -ENOMEM;
  4066. + sym.k = (void *)__get_free_page(GFP_NOFS);
  4067. + if (unlikely(!sym.k))
  4068. + goto out;
  4069. +
  4070. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  4071. + old_fs = get_fs();
  4072. + set_fs(KERNEL_DS);
  4073. + symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
  4074. + err = symlen;
  4075. + set_fs(old_fs);
  4076. +
  4077. + if (symlen > 0) {
  4078. + sym.k[symlen] = 0;
  4079. + err = vfsub_symlink(h_dir, h_path, sym.k);
  4080. + }
  4081. + au_delayed_free_page((unsigned long)sym.k);
  4082. +
  4083. +out:
  4084. + return err;
  4085. +}
  4086. +
  4087. +/*
  4088. + * regardless 'acl' option, reset all ACL.
  4089. + * All ACL will be copied up later from the original entry on the lower branch.
  4090. + */
  4091. +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
  4092. +{
  4093. + int err;
  4094. + struct dentry *h_dentry;
  4095. + struct inode *h_inode;
  4096. +
  4097. + h_dentry = h_path->dentry;
  4098. + h_inode = d_inode(h_dentry);
  4099. + /* forget_all_cached_acls(h_inode)); */
  4100. + err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
  4101. + AuTraceErr(err);
  4102. + if (err == -EOPNOTSUPP)
  4103. + err = 0;
  4104. + if (!err)
  4105. + err = vfsub_acl_chmod(h_inode, mode);
  4106. +
  4107. + AuTraceErr(err);
  4108. + return err;
  4109. +}
  4110. +
  4111. +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4112. + struct inode *h_dir, struct path *h_path)
  4113. +{
  4114. + int err;
  4115. + struct inode *dir, *inode;
  4116. +
  4117. + err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
  4118. + AuTraceErr(err);
  4119. + if (err == -EOPNOTSUPP)
  4120. + err = 0;
  4121. + if (unlikely(err))
  4122. + goto out;
  4123. +
  4124. + /*
  4125. + * strange behaviour from the users view,
  4126. + * particularry setattr case
  4127. + */
  4128. + dir = d_inode(dst_parent);
  4129. + if (au_ibtop(dir) == cpg->bdst)
  4130. + au_cpup_attr_nlink(dir, /*force*/1);
  4131. + inode = d_inode(cpg->dentry);
  4132. + au_cpup_attr_nlink(inode, /*force*/1);
  4133. +
  4134. +out:
  4135. + return err;
  4136. +}
  4137. +
  4138. +static noinline_for_stack
  4139. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4140. + struct au_cpup_reg_attr *h_src_attr)
  4141. +{
  4142. + int err;
  4143. + umode_t mode;
  4144. + unsigned int mnt_flags;
  4145. + unsigned char isdir, isreg, force;
  4146. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4147. + struct au_dtime dt;
  4148. + struct path h_path;
  4149. + struct dentry *h_src, *h_dst, *h_parent;
  4150. + struct inode *h_inode, *h_dir;
  4151. + struct super_block *sb;
  4152. +
  4153. + /* bsrc branch can be ro/rw. */
  4154. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4155. + h_inode = d_inode(h_src);
  4156. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4157. +
  4158. + /* try stopping to be referenced while we are creating */
  4159. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4160. + if (au_ftest_cpup(cpg->flags, RENAME))
  4161. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4162. + AUFS_WH_PFX_LEN));
  4163. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4164. + h_dir = d_inode(h_parent);
  4165. + IMustLock(h_dir);
  4166. + AuDebugOn(h_parent != h_dst->d_parent);
  4167. +
  4168. + sb = cpg->dentry->d_sb;
  4169. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4170. + if (do_dt) {
  4171. + h_path.dentry = h_parent;
  4172. + au_dtime_store(&dt, dst_parent, &h_path);
  4173. + }
  4174. + h_path.dentry = h_dst;
  4175. +
  4176. + isreg = 0;
  4177. + isdir = 0;
  4178. + mode = h_inode->i_mode;
  4179. + switch (mode & S_IFMT) {
  4180. + case S_IFREG:
  4181. + isreg = 1;
  4182. + err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
  4183. + /*want_excl*/true);
  4184. + if (!err)
  4185. + err = au_do_cpup_regular(cpg, h_src_attr);
  4186. + break;
  4187. + case S_IFDIR:
  4188. + isdir = 1;
  4189. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4190. + if (!err)
  4191. + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
  4192. + break;
  4193. + case S_IFLNK:
  4194. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4195. + break;
  4196. + case S_IFCHR:
  4197. + case S_IFBLK:
  4198. + AuDebugOn(!capable(CAP_MKNOD));
  4199. + /*FALLTHROUGH*/
  4200. + case S_IFIFO:
  4201. + case S_IFSOCK:
  4202. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4203. + break;
  4204. + default:
  4205. + AuIOErr("Unknown inode type 0%o\n", mode);
  4206. + err = -EIO;
  4207. + }
  4208. + if (!err)
  4209. + err = au_reset_acl(h_dir, &h_path, mode);
  4210. +
  4211. + mnt_flags = au_mntflags(sb);
  4212. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4213. + && !isdir
  4214. + && au_opt_test(mnt_flags, XINO)
  4215. + && (h_inode->i_nlink == 1
  4216. + || (h_inode->i_state & I_LINKABLE))
  4217. + /* todo: unnecessary? */
  4218. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4219. + && cpg->bdst < cpg->bsrc
  4220. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4221. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4222. + /* ignore this error */
  4223. +
  4224. + if (!err) {
  4225. + force = 0;
  4226. + if (isreg) {
  4227. + force = !!cpg->len;
  4228. + if (cpg->len == -1)
  4229. + force = !!i_size_read(h_inode);
  4230. + }
  4231. + au_fhsm_wrote(sb, cpg->bdst, force);
  4232. + }
  4233. +
  4234. + if (do_dt)
  4235. + au_dtime_revert(&dt);
  4236. + return err;
  4237. +}
  4238. +
  4239. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4240. +{
  4241. + int err;
  4242. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4243. + struct inode *h_dir;
  4244. + aufs_bindex_t bdst;
  4245. +
  4246. + dentry = cpg->dentry;
  4247. + bdst = cpg->bdst;
  4248. + h_dentry = au_h_dptr(dentry, bdst);
  4249. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4250. + dget(h_dentry);
  4251. + au_set_h_dptr(dentry, bdst, NULL);
  4252. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4253. + if (!err)
  4254. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4255. + au_set_h_dptr(dentry, bdst, h_dentry);
  4256. + } else {
  4257. + err = 0;
  4258. + parent = dget_parent(dentry);
  4259. + h_parent = au_h_dptr(parent, bdst);
  4260. + dput(parent);
  4261. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  4262. + if (IS_ERR(h_path->dentry))
  4263. + err = PTR_ERR(h_path->dentry);
  4264. + }
  4265. + if (unlikely(err))
  4266. + goto out;
  4267. +
  4268. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4269. + h_dir = d_inode(h_parent);
  4270. + IMustLock(h_dir);
  4271. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4272. + /* no delegation since it is just created */
  4273. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
  4274. + dput(h_path->dentry);
  4275. +
  4276. +out:
  4277. + return err;
  4278. +}
  4279. +
  4280. +/*
  4281. + * copyup the @dentry from @bsrc to @bdst.
  4282. + * the caller must set the both of lower dentries.
  4283. + * @len is for truncating when it is -1 copyup the entire file.
  4284. + * in link/rename cases, @dst_parent may be different from the real one.
  4285. + * basic->bsrc can be larger than basic->bdst.
  4286. + */
  4287. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4288. +{
  4289. + int err, rerr;
  4290. + aufs_bindex_t old_ibtop;
  4291. + unsigned char isdir, plink;
  4292. + struct dentry *h_src, *h_dst, *h_parent;
  4293. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4294. + struct super_block *sb;
  4295. + struct au_branch *br;
  4296. + /* to reuduce stack size */
  4297. + struct {
  4298. + struct au_dtime dt;
  4299. + struct path h_path;
  4300. + struct au_cpup_reg_attr h_src_attr;
  4301. + } *a;
  4302. +
  4303. + err = -ENOMEM;
  4304. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4305. + if (unlikely(!a))
  4306. + goto out;
  4307. + a->h_src_attr.valid = 0;
  4308. +
  4309. + sb = cpg->dentry->d_sb;
  4310. + br = au_sbr(sb, cpg->bdst);
  4311. + a->h_path.mnt = au_br_mnt(br);
  4312. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4313. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4314. + h_dir = d_inode(h_parent);
  4315. + IMustLock(h_dir);
  4316. +
  4317. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4318. + inode = d_inode(cpg->dentry);
  4319. +
  4320. + if (!dst_parent)
  4321. + dst_parent = dget_parent(cpg->dentry);
  4322. + else
  4323. + dget(dst_parent);
  4324. +
  4325. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4326. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4327. + if (dst_inode) {
  4328. + if (unlikely(!plink)) {
  4329. + err = -EIO;
  4330. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4331. + "but plink is disabled\n",
  4332. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4333. + goto out_parent;
  4334. + }
  4335. +
  4336. + if (dst_inode->i_nlink) {
  4337. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4338. +
  4339. + h_src = au_plink_lkup(inode, cpg->bdst);
  4340. + err = PTR_ERR(h_src);
  4341. + if (IS_ERR(h_src))
  4342. + goto out_parent;
  4343. + if (unlikely(d_is_negative(h_src))) {
  4344. + err = -EIO;
  4345. + AuIOErr("i%lu exists on b%d "
  4346. + "but not pseudo-linked\n",
  4347. + inode->i_ino, cpg->bdst);
  4348. + dput(h_src);
  4349. + goto out_parent;
  4350. + }
  4351. +
  4352. + if (do_dt) {
  4353. + a->h_path.dentry = h_parent;
  4354. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4355. + }
  4356. +
  4357. + a->h_path.dentry = h_dst;
  4358. + delegated = NULL;
  4359. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4360. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4361. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4362. + if (do_dt)
  4363. + au_dtime_revert(&a->dt);
  4364. + if (unlikely(err == -EWOULDBLOCK)) {
  4365. + pr_warn("cannot retry for NFSv4 delegation"
  4366. + " for an internal link\n");
  4367. + iput(delegated);
  4368. + }
  4369. + dput(h_src);
  4370. + goto out_parent;
  4371. + } else
  4372. + /* todo: cpup_wh_file? */
  4373. + /* udba work */
  4374. + au_update_ibrange(inode, /*do_put_zero*/1);
  4375. + }
  4376. +
  4377. + isdir = S_ISDIR(inode->i_mode);
  4378. + old_ibtop = au_ibtop(inode);
  4379. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4380. + if (unlikely(err))
  4381. + goto out_rev;
  4382. + dst_inode = d_inode(h_dst);
  4383. + mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
  4384. + /* todo: necessary? */
  4385. + /* au_pin_hdir_unlock(cpg->pin); */
  4386. +
  4387. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  4388. + if (unlikely(err)) {
  4389. + /* todo: necessary? */
  4390. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4391. + mutex_unlock(&dst_inode->i_mutex);
  4392. + goto out_rev;
  4393. + }
  4394. +
  4395. + if (cpg->bdst < old_ibtop) {
  4396. + if (S_ISREG(inode->i_mode)) {
  4397. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4398. + if (unlikely(err)) {
  4399. + /* ignore an error */
  4400. + /* au_pin_hdir_relock(cpg->pin); */
  4401. + mutex_unlock(&dst_inode->i_mutex);
  4402. + goto out_rev;
  4403. + }
  4404. + }
  4405. + au_set_ibtop(inode, cpg->bdst);
  4406. + } else
  4407. + au_set_ibbot(inode, cpg->bdst);
  4408. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4409. + au_hi_flags(inode, isdir));
  4410. +
  4411. + /* todo: necessary? */
  4412. + /* err = au_pin_hdir_relock(cpg->pin); */
  4413. + mutex_unlock(&dst_inode->i_mutex);
  4414. + if (unlikely(err))
  4415. + goto out_rev;
  4416. +
  4417. + src_inode = d_inode(h_src);
  4418. + if (!isdir
  4419. + && (src_inode->i_nlink > 1
  4420. + || src_inode->i_state & I_LINKABLE)
  4421. + && plink)
  4422. + au_plink_append(inode, cpg->bdst, h_dst);
  4423. +
  4424. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4425. + a->h_path.dentry = h_dst;
  4426. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4427. + }
  4428. + if (!err)
  4429. + goto out_parent; /* success */
  4430. +
  4431. + /* revert */
  4432. +out_rev:
  4433. + a->h_path.dentry = h_parent;
  4434. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4435. + a->h_path.dentry = h_dst;
  4436. + rerr = 0;
  4437. + if (d_is_positive(h_dst)) {
  4438. + if (!isdir) {
  4439. + /* no delegation since it is just created */
  4440. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4441. + /*delegated*/NULL, /*force*/0);
  4442. + } else
  4443. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4444. + }
  4445. + au_dtime_revert(&a->dt);
  4446. + if (rerr) {
  4447. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4448. + err = -EIO;
  4449. + }
  4450. +out_parent:
  4451. + dput(dst_parent);
  4452. + au_delayed_kfree(a);
  4453. +out:
  4454. + return err;
  4455. +}
  4456. +
  4457. +#if 0 /* reserved */
  4458. +struct au_cpup_single_args {
  4459. + int *errp;
  4460. + struct au_cp_generic *cpg;
  4461. + struct dentry *dst_parent;
  4462. +};
  4463. +
  4464. +static void au_call_cpup_single(void *args)
  4465. +{
  4466. + struct au_cpup_single_args *a = args;
  4467. +
  4468. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4469. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4470. + au_pin_hdir_release(a->cpg->pin);
  4471. +}
  4472. +#endif
  4473. +
  4474. +/*
  4475. + * prevent SIGXFSZ in copy-up.
  4476. + * testing CAP_MKNOD is for generic fs,
  4477. + * but CAP_FSETID is for xfs only, currently.
  4478. + */
  4479. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4480. +{
  4481. + int do_sio;
  4482. + struct super_block *sb;
  4483. + struct inode *h_dir;
  4484. +
  4485. + do_sio = 0;
  4486. + sb = au_pinned_parent(pin)->d_sb;
  4487. + if (!au_wkq_test()
  4488. + && (!au_sbi(sb)->si_plink_maint_pid
  4489. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4490. + switch (mode & S_IFMT) {
  4491. + case S_IFREG:
  4492. + /* no condition about RLIMIT_FSIZE and the file size */
  4493. + do_sio = 1;
  4494. + break;
  4495. + case S_IFCHR:
  4496. + case S_IFBLK:
  4497. + do_sio = !capable(CAP_MKNOD);
  4498. + break;
  4499. + }
  4500. + if (!do_sio)
  4501. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4502. + && !capable(CAP_FSETID));
  4503. + /* this workaround may be removed in the future */
  4504. + if (!do_sio) {
  4505. + h_dir = au_pinned_h_dir(pin);
  4506. + do_sio = h_dir->i_mode & S_ISVTX;
  4507. + }
  4508. + }
  4509. +
  4510. + return do_sio;
  4511. +}
  4512. +
  4513. +#if 0 /* reserved */
  4514. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4515. +{
  4516. + int err, wkq_err;
  4517. + struct dentry *h_dentry;
  4518. +
  4519. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4520. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4521. + err = au_cpup_single(cpg, dst_parent);
  4522. + else {
  4523. + struct au_cpup_single_args args = {
  4524. + .errp = &err,
  4525. + .cpg = cpg,
  4526. + .dst_parent = dst_parent
  4527. + };
  4528. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4529. + if (unlikely(wkq_err))
  4530. + err = wkq_err;
  4531. + }
  4532. +
  4533. + return err;
  4534. +}
  4535. +#endif
  4536. +
  4537. +/*
  4538. + * copyup the @dentry from the first active lower branch to @bdst,
  4539. + * using au_cpup_single().
  4540. + */
  4541. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4542. +{
  4543. + int err;
  4544. + unsigned int flags_orig;
  4545. + struct dentry *dentry;
  4546. +
  4547. + AuDebugOn(cpg->bsrc < 0);
  4548. +
  4549. + dentry = cpg->dentry;
  4550. + DiMustWriteLock(dentry);
  4551. +
  4552. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4553. + if (!err) {
  4554. + flags_orig = cpg->flags;
  4555. + au_fset_cpup(cpg->flags, RENAME);
  4556. + err = au_cpup_single(cpg, NULL);
  4557. + cpg->flags = flags_orig;
  4558. + if (!err)
  4559. + return 0; /* success */
  4560. +
  4561. + /* revert */
  4562. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4563. + au_set_dbtop(dentry, cpg->bsrc);
  4564. + }
  4565. +
  4566. + return err;
  4567. +}
  4568. +
  4569. +struct au_cpup_simple_args {
  4570. + int *errp;
  4571. + struct au_cp_generic *cpg;
  4572. +};
  4573. +
  4574. +static void au_call_cpup_simple(void *args)
  4575. +{
  4576. + struct au_cpup_simple_args *a = args;
  4577. +
  4578. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4579. + *a->errp = au_cpup_simple(a->cpg);
  4580. + au_pin_hdir_release(a->cpg->pin);
  4581. +}
  4582. +
  4583. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4584. +{
  4585. + int err, wkq_err;
  4586. + struct dentry *dentry, *parent;
  4587. + struct file *h_file;
  4588. + struct inode *h_dir;
  4589. +
  4590. + dentry = cpg->dentry;
  4591. + h_file = NULL;
  4592. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4593. + AuDebugOn(cpg->bsrc < 0);
  4594. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4595. + err = PTR_ERR(h_file);
  4596. + if (IS_ERR(h_file))
  4597. + goto out;
  4598. + }
  4599. +
  4600. + parent = dget_parent(dentry);
  4601. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4602. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  4603. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4604. + err = au_cpup_simple(cpg);
  4605. + else {
  4606. + struct au_cpup_simple_args args = {
  4607. + .errp = &err,
  4608. + .cpg = cpg
  4609. + };
  4610. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4611. + if (unlikely(wkq_err))
  4612. + err = wkq_err;
  4613. + }
  4614. +
  4615. + dput(parent);
  4616. + if (h_file)
  4617. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4618. +
  4619. +out:
  4620. + return err;
  4621. +}
  4622. +
  4623. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4624. +{
  4625. + aufs_bindex_t bsrc, bbot;
  4626. + struct dentry *dentry, *h_dentry;
  4627. +
  4628. + if (cpg->bsrc < 0) {
  4629. + dentry = cpg->dentry;
  4630. + bbot = au_dbbot(dentry);
  4631. + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
  4632. + h_dentry = au_h_dptr(dentry, bsrc);
  4633. + if (h_dentry) {
  4634. + AuDebugOn(d_is_negative(h_dentry));
  4635. + break;
  4636. + }
  4637. + }
  4638. + AuDebugOn(bsrc > bbot);
  4639. + cpg->bsrc = bsrc;
  4640. + }
  4641. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4642. + return au_do_sio_cpup_simple(cpg);
  4643. +}
  4644. +
  4645. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4646. +{
  4647. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4648. + return au_do_sio_cpup_simple(cpg);
  4649. +}
  4650. +
  4651. +/* ---------------------------------------------------------------------- */
  4652. +
  4653. +/*
  4654. + * copyup the deleted file for writing.
  4655. + */
  4656. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  4657. + struct file *file)
  4658. +{
  4659. + int err;
  4660. + unsigned int flags_orig;
  4661. + aufs_bindex_t bsrc_orig;
  4662. + struct au_dinfo *dinfo;
  4663. + struct {
  4664. + struct au_hdentry *hd;
  4665. + struct dentry *h_dentry;
  4666. + } hdst, hsrc;
  4667. +
  4668. + dinfo = au_di(cpg->dentry);
  4669. + AuRwMustWriteLock(&dinfo->di_rwsem);
  4670. +
  4671. + bsrc_orig = cpg->bsrc;
  4672. + cpg->bsrc = dinfo->di_btop;
  4673. + hdst.hd = au_hdentry(dinfo, cpg->bdst);
  4674. + hdst.h_dentry = hdst.hd->hd_dentry;
  4675. + hdst.hd->hd_dentry = wh_dentry;
  4676. + dinfo->di_btop = cpg->bdst;
  4677. +
  4678. + hsrc.h_dentry = NULL;
  4679. + if (file) {
  4680. + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
  4681. + hsrc.h_dentry = hsrc.hd->hd_dentry;
  4682. + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
  4683. + }
  4684. + flags_orig = cpg->flags;
  4685. + cpg->flags = !AuCpup_DTIME;
  4686. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  4687. + cpg->flags = flags_orig;
  4688. + if (file) {
  4689. + if (!err)
  4690. + err = au_reopen_nondir(file);
  4691. + hsrc.hd->hd_dentry = hsrc.h_dentry;
  4692. + }
  4693. + hdst.hd->hd_dentry = hdst.h_dentry;
  4694. + dinfo->di_btop = cpg->bsrc;
  4695. + cpg->bsrc = bsrc_orig;
  4696. +
  4697. + return err;
  4698. +}
  4699. +
  4700. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4701. +{
  4702. + int err;
  4703. + aufs_bindex_t bdst;
  4704. + struct au_dtime dt;
  4705. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  4706. + struct au_branch *br;
  4707. + struct path h_path;
  4708. +
  4709. + dentry = cpg->dentry;
  4710. + bdst = cpg->bdst;
  4711. + br = au_sbr(dentry->d_sb, bdst);
  4712. + parent = dget_parent(dentry);
  4713. + h_parent = au_h_dptr(parent, bdst);
  4714. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  4715. + err = PTR_ERR(wh_dentry);
  4716. + if (IS_ERR(wh_dentry))
  4717. + goto out;
  4718. +
  4719. + h_path.dentry = h_parent;
  4720. + h_path.mnt = au_br_mnt(br);
  4721. + au_dtime_store(&dt, parent, &h_path);
  4722. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  4723. + if (unlikely(err))
  4724. + goto out_wh;
  4725. +
  4726. + dget(wh_dentry);
  4727. + h_path.dentry = wh_dentry;
  4728. + if (!d_is_dir(wh_dentry)) {
  4729. + /* no delegation since it is just created */
  4730. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  4731. + /*delegated*/NULL, /*force*/0);
  4732. + } else
  4733. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  4734. + if (unlikely(err)) {
  4735. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  4736. + wh_dentry, err);
  4737. + err = -EIO;
  4738. + }
  4739. + au_dtime_revert(&dt);
  4740. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  4741. +
  4742. +out_wh:
  4743. + dput(wh_dentry);
  4744. +out:
  4745. + dput(parent);
  4746. + return err;
  4747. +}
  4748. +
  4749. +struct au_cpup_wh_args {
  4750. + int *errp;
  4751. + struct au_cp_generic *cpg;
  4752. + struct file *file;
  4753. +};
  4754. +
  4755. +static void au_call_cpup_wh(void *args)
  4756. +{
  4757. + struct au_cpup_wh_args *a = args;
  4758. +
  4759. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4760. + *a->errp = au_cpup_wh(a->cpg, a->file);
  4761. + au_pin_hdir_release(a->cpg->pin);
  4762. +}
  4763. +
  4764. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4765. +{
  4766. + int err, wkq_err;
  4767. + aufs_bindex_t bdst;
  4768. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  4769. + struct inode *dir, *h_dir, *h_tmpdir;
  4770. + struct au_wbr *wbr;
  4771. + struct au_pin wh_pin, *pin_orig;
  4772. +
  4773. + dentry = cpg->dentry;
  4774. + bdst = cpg->bdst;
  4775. + parent = dget_parent(dentry);
  4776. + dir = d_inode(parent);
  4777. + h_orph = NULL;
  4778. + h_parent = NULL;
  4779. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  4780. + h_tmpdir = h_dir;
  4781. + pin_orig = NULL;
  4782. + if (!h_dir->i_nlink) {
  4783. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  4784. + h_orph = wbr->wbr_orph;
  4785. +
  4786. + h_parent = dget(au_h_dptr(parent, bdst));
  4787. + au_set_h_dptr(parent, bdst, dget(h_orph));
  4788. + h_tmpdir = d_inode(h_orph);
  4789. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  4790. +
  4791. + mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
  4792. + /* todo: au_h_open_pre()? */
  4793. +
  4794. + pin_orig = cpg->pin;
  4795. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  4796. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  4797. + cpg->pin = &wh_pin;
  4798. + }
  4799. +
  4800. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  4801. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4802. + err = au_cpup_wh(cpg, file);
  4803. + else {
  4804. + struct au_cpup_wh_args args = {
  4805. + .errp = &err,
  4806. + .cpg = cpg,
  4807. + .file = file
  4808. + };
  4809. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  4810. + if (unlikely(wkq_err))
  4811. + err = wkq_err;
  4812. + }
  4813. +
  4814. + if (h_orph) {
  4815. + mutex_unlock(&h_tmpdir->i_mutex);
  4816. + /* todo: au_h_open_post()? */
  4817. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  4818. + au_set_h_dptr(parent, bdst, h_parent);
  4819. + AuDebugOn(!pin_orig);
  4820. + cpg->pin = pin_orig;
  4821. + }
  4822. + iput(h_dir);
  4823. + dput(parent);
  4824. +
  4825. + return err;
  4826. +}
  4827. +
  4828. +/* ---------------------------------------------------------------------- */
  4829. +
  4830. +/*
  4831. + * generic routine for both of copy-up and copy-down.
  4832. + */
  4833. +/* cf. revalidate function in file.c */
  4834. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  4835. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  4836. + struct au_pin *pin,
  4837. + struct dentry *h_parent, void *arg),
  4838. + void *arg)
  4839. +{
  4840. + int err;
  4841. + struct au_pin pin;
  4842. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  4843. +
  4844. + err = 0;
  4845. + parent = dget_parent(dentry);
  4846. + if (IS_ROOT(parent))
  4847. + goto out;
  4848. +
  4849. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  4850. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  4851. +
  4852. + /* do not use au_dpage */
  4853. + real_parent = parent;
  4854. + while (1) {
  4855. + dput(parent);
  4856. + parent = dget_parent(dentry);
  4857. + h_parent = au_h_dptr(parent, bdst);
  4858. + if (h_parent)
  4859. + goto out; /* success */
  4860. +
  4861. + /* find top dir which is necessary to cpup */
  4862. + do {
  4863. + d = parent;
  4864. + dput(parent);
  4865. + parent = dget_parent(d);
  4866. + di_read_lock_parent3(parent, !AuLock_IR);
  4867. + h_parent = au_h_dptr(parent, bdst);
  4868. + di_read_unlock(parent, !AuLock_IR);
  4869. + } while (!h_parent);
  4870. +
  4871. + if (d != real_parent)
  4872. + di_write_lock_child3(d);
  4873. +
  4874. + /* somebody else might create while we were sleeping */
  4875. + h_dentry = au_h_dptr(d, bdst);
  4876. + if (!h_dentry || d_is_negative(h_dentry)) {
  4877. + if (h_dentry)
  4878. + au_update_dbtop(d);
  4879. +
  4880. + au_pin_set_dentry(&pin, d);
  4881. + err = au_do_pin(&pin);
  4882. + if (!err) {
  4883. + err = cp(d, bdst, &pin, h_parent, arg);
  4884. + au_unpin(&pin);
  4885. + }
  4886. + }
  4887. +
  4888. + if (d != real_parent)
  4889. + di_write_unlock(d);
  4890. + if (unlikely(err))
  4891. + break;
  4892. + }
  4893. +
  4894. +out:
  4895. + dput(parent);
  4896. + return err;
  4897. +}
  4898. +
  4899. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  4900. + struct au_pin *pin,
  4901. + struct dentry *h_parent __maybe_unused,
  4902. + void *arg __maybe_unused)
  4903. +{
  4904. + struct au_cp_generic cpg = {
  4905. + .dentry = dentry,
  4906. + .bdst = bdst,
  4907. + .bsrc = -1,
  4908. + .len = 0,
  4909. + .pin = pin,
  4910. + .flags = AuCpup_DTIME
  4911. + };
  4912. + return au_sio_cpup_simple(&cpg);
  4913. +}
  4914. +
  4915. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4916. +{
  4917. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  4918. +}
  4919. +
  4920. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4921. +{
  4922. + int err;
  4923. + struct dentry *parent;
  4924. + struct inode *dir;
  4925. +
  4926. + parent = dget_parent(dentry);
  4927. + dir = d_inode(parent);
  4928. + err = 0;
  4929. + if (au_h_iptr(dir, bdst))
  4930. + goto out;
  4931. +
  4932. + di_read_unlock(parent, AuLock_IR);
  4933. + di_write_lock_parent(parent);
  4934. + /* someone else might change our inode while we were sleeping */
  4935. + if (!au_h_iptr(dir, bdst))
  4936. + err = au_cpup_dirs(dentry, bdst);
  4937. + di_downgrade_lock(parent, AuLock_IR);
  4938. +
  4939. +out:
  4940. + dput(parent);
  4941. + return err;
  4942. +}
  4943. --- /dev/null
  4944. +++ linux-4.4/fs/aufs/cpup.h 2016-10-23 11:20:57.625493286 +0300
  4945. @@ -0,0 +1,94 @@
  4946. +/*
  4947. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  4948. + *
  4949. + * This program, aufs is free software; you can redistribute it and/or modify
  4950. + * it under the terms of the GNU General Public License as published by
  4951. + * the Free Software Foundation; either version 2 of the License, or
  4952. + * (at your option) any later version.
  4953. + *
  4954. + * This program is distributed in the hope that it will be useful,
  4955. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4956. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4957. + * GNU General Public License for more details.
  4958. + *
  4959. + * You should have received a copy of the GNU General Public License
  4960. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  4961. + */
  4962. +
  4963. +/*
  4964. + * copy-up/down functions
  4965. + */
  4966. +
  4967. +#ifndef __AUFS_CPUP_H__
  4968. +#define __AUFS_CPUP_H__
  4969. +
  4970. +#ifdef __KERNEL__
  4971. +
  4972. +#include <linux/path.h>
  4973. +
  4974. +struct inode;
  4975. +struct file;
  4976. +struct au_pin;
  4977. +
  4978. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  4979. +void au_cpup_attr_timesizes(struct inode *inode);
  4980. +void au_cpup_attr_nlink(struct inode *inode, int force);
  4981. +void au_cpup_attr_changeable(struct inode *inode);
  4982. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  4983. +void au_cpup_attr_all(struct inode *inode, int force);
  4984. +
  4985. +/* ---------------------------------------------------------------------- */
  4986. +
  4987. +struct au_cp_generic {
  4988. + struct dentry *dentry;
  4989. + aufs_bindex_t bdst, bsrc;
  4990. + loff_t len;
  4991. + struct au_pin *pin;
  4992. + unsigned int flags;
  4993. +};
  4994. +
  4995. +/* cpup flags */
  4996. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  4997. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  4998. + for link(2) */
  4999. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  5000. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  5001. + cpup */
  5002. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  5003. + existing entry */
  5004. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  5005. + the branch is marked as RO */
  5006. +
  5007. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  5008. +#define au_fset_cpup(flags, name) \
  5009. + do { (flags) |= AuCpup_##name; } while (0)
  5010. +#define au_fclr_cpup(flags, name) \
  5011. + do { (flags) &= ~AuCpup_##name; } while (0)
  5012. +
  5013. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  5014. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  5015. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  5016. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  5017. +
  5018. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5019. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5020. + struct au_pin *pin,
  5021. + struct dentry *h_parent, void *arg),
  5022. + void *arg);
  5023. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5024. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5025. +
  5026. +/* ---------------------------------------------------------------------- */
  5027. +
  5028. +/* keep timestamps when copyup */
  5029. +struct au_dtime {
  5030. + struct dentry *dt_dentry;
  5031. + struct path dt_h_path;
  5032. + struct timespec dt_atime, dt_mtime;
  5033. +};
  5034. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  5035. + struct path *h_path);
  5036. +void au_dtime_revert(struct au_dtime *dt);
  5037. +
  5038. +#endif /* __KERNEL__ */
  5039. +#endif /* __AUFS_CPUP_H__ */
  5040. --- /dev/null
  5041. +++ linux-4.4/fs/aufs/dbgaufs.c 2016-10-23 11:20:57.625493286 +0300
  5042. @@ -0,0 +1,438 @@
  5043. +/*
  5044. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5045. + *
  5046. + * This program, aufs is free software; you can redistribute it and/or modify
  5047. + * it under the terms of the GNU General Public License as published by
  5048. + * the Free Software Foundation; either version 2 of the License, or
  5049. + * (at your option) any later version.
  5050. + *
  5051. + * This program is distributed in the hope that it will be useful,
  5052. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5053. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5054. + * GNU General Public License for more details.
  5055. + *
  5056. + * You should have received a copy of the GNU General Public License
  5057. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5058. + */
  5059. +
  5060. +/*
  5061. + * debugfs interface
  5062. + */
  5063. +
  5064. +#include <linux/debugfs.h>
  5065. +#include "aufs.h"
  5066. +
  5067. +#ifndef CONFIG_SYSFS
  5068. +#error DEBUG_FS depends upon SYSFS
  5069. +#endif
  5070. +
  5071. +static struct dentry *dbgaufs;
  5072. +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
  5073. +
  5074. +/* 20 is max digits length of ulong 64 */
  5075. +struct dbgaufs_arg {
  5076. + int n;
  5077. + char a[20 * 4];
  5078. +};
  5079. +
  5080. +/*
  5081. + * common function for all XINO files
  5082. + */
  5083. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5084. + struct file *file)
  5085. +{
  5086. + au_delayed_kfree(file->private_data);
  5087. + return 0;
  5088. +}
  5089. +
  5090. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
  5091. +{
  5092. + int err;
  5093. + struct kstat st;
  5094. + struct dbgaufs_arg *p;
  5095. +
  5096. + err = -ENOMEM;
  5097. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5098. + if (unlikely(!p))
  5099. + goto out;
  5100. +
  5101. + err = 0;
  5102. + p->n = 0;
  5103. + file->private_data = p;
  5104. + if (!xf)
  5105. + goto out;
  5106. +
  5107. + err = vfs_getattr(&xf->f_path, &st);
  5108. + if (!err) {
  5109. + if (do_fcnt)
  5110. + p->n = snprintf
  5111. + (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
  5112. + (long)file_count(xf), st.blocks, st.blksize,
  5113. + (long long)st.size);
  5114. + else
  5115. + p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
  5116. + st.blocks, st.blksize,
  5117. + (long long)st.size);
  5118. + AuDebugOn(p->n >= sizeof(p->a));
  5119. + } else {
  5120. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5121. + err = 0;
  5122. + }
  5123. +
  5124. +out:
  5125. + return err;
  5126. +
  5127. +}
  5128. +
  5129. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5130. + size_t count, loff_t *ppos)
  5131. +{
  5132. + struct dbgaufs_arg *p;
  5133. +
  5134. + p = file->private_data;
  5135. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5136. +}
  5137. +
  5138. +/* ---------------------------------------------------------------------- */
  5139. +
  5140. +struct dbgaufs_plink_arg {
  5141. + int n;
  5142. + char a[];
  5143. +};
  5144. +
  5145. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5146. + struct file *file)
  5147. +{
  5148. + au_delayed_free_page((unsigned long)file->private_data);
  5149. + return 0;
  5150. +}
  5151. +
  5152. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5153. +{
  5154. + int err, i, limit;
  5155. + unsigned long n, sum;
  5156. + struct dbgaufs_plink_arg *p;
  5157. + struct au_sbinfo *sbinfo;
  5158. + struct super_block *sb;
  5159. + struct au_sphlhead *sphl;
  5160. +
  5161. + err = -ENOMEM;
  5162. + p = (void *)get_zeroed_page(GFP_NOFS);
  5163. + if (unlikely(!p))
  5164. + goto out;
  5165. +
  5166. + err = -EFBIG;
  5167. + sbinfo = inode->i_private;
  5168. + sb = sbinfo->si_sb;
  5169. + si_noflush_read_lock(sb);
  5170. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5171. + limit = PAGE_SIZE - sizeof(p->n);
  5172. +
  5173. + /* the number of buckets */
  5174. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5175. + p->n += n;
  5176. + limit -= n;
  5177. +
  5178. + sum = 0;
  5179. + for (i = 0, sphl = sbinfo->si_plink;
  5180. + i < AuPlink_NHASH;
  5181. + i++, sphl++) {
  5182. + n = au_sphl_count(sphl);
  5183. + sum += n;
  5184. +
  5185. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5186. + p->n += n;
  5187. + limit -= n;
  5188. + if (unlikely(limit <= 0))
  5189. + goto out_free;
  5190. + }
  5191. + p->a[p->n - 1] = '\n';
  5192. +
  5193. + /* the sum of plinks */
  5194. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5195. + p->n += n;
  5196. + limit -= n;
  5197. + if (unlikely(limit <= 0))
  5198. + goto out_free;
  5199. + } else {
  5200. +#define str "1\n0\n0\n"
  5201. + p->n = sizeof(str) - 1;
  5202. + strcpy(p->a, str);
  5203. +#undef str
  5204. + }
  5205. + si_read_unlock(sb);
  5206. +
  5207. + err = 0;
  5208. + file->private_data = p;
  5209. + goto out; /* success */
  5210. +
  5211. +out_free:
  5212. + au_delayed_free_page((unsigned long)p);
  5213. +out:
  5214. + return err;
  5215. +}
  5216. +
  5217. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5218. + size_t count, loff_t *ppos)
  5219. +{
  5220. + struct dbgaufs_plink_arg *p;
  5221. +
  5222. + p = file->private_data;
  5223. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5224. +}
  5225. +
  5226. +static const struct file_operations dbgaufs_plink_fop = {
  5227. + .owner = THIS_MODULE,
  5228. + .open = dbgaufs_plink_open,
  5229. + .release = dbgaufs_plink_release,
  5230. + .read = dbgaufs_plink_read
  5231. +};
  5232. +
  5233. +/* ---------------------------------------------------------------------- */
  5234. +
  5235. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5236. +{
  5237. + int err;
  5238. + struct au_sbinfo *sbinfo;
  5239. + struct super_block *sb;
  5240. +
  5241. + sbinfo = inode->i_private;
  5242. + sb = sbinfo->si_sb;
  5243. + si_noflush_read_lock(sb);
  5244. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
  5245. + si_read_unlock(sb);
  5246. + return err;
  5247. +}
  5248. +
  5249. +static const struct file_operations dbgaufs_xib_fop = {
  5250. + .owner = THIS_MODULE,
  5251. + .open = dbgaufs_xib_open,
  5252. + .release = dbgaufs_xi_release,
  5253. + .read = dbgaufs_xi_read
  5254. +};
  5255. +
  5256. +/* ---------------------------------------------------------------------- */
  5257. +
  5258. +#define DbgaufsXi_PREFIX "xi"
  5259. +
  5260. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5261. +{
  5262. + int err;
  5263. + long l;
  5264. + struct au_sbinfo *sbinfo;
  5265. + struct super_block *sb;
  5266. + struct file *xf;
  5267. + struct qstr *name;
  5268. +
  5269. + err = -ENOENT;
  5270. + xf = NULL;
  5271. + name = &file->f_path.dentry->d_name;
  5272. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5273. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5274. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5275. + goto out;
  5276. + err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5277. + if (unlikely(err))
  5278. + goto out;
  5279. +
  5280. + sbinfo = inode->i_private;
  5281. + sb = sbinfo->si_sb;
  5282. + si_noflush_read_lock(sb);
  5283. + if (l <= au_sbbot(sb)) {
  5284. + xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
  5285. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
  5286. + } else
  5287. + err = -ENOENT;
  5288. + si_read_unlock(sb);
  5289. +
  5290. +out:
  5291. + return err;
  5292. +}
  5293. +
  5294. +static const struct file_operations dbgaufs_xino_fop = {
  5295. + .owner = THIS_MODULE,
  5296. + .open = dbgaufs_xino_open,
  5297. + .release = dbgaufs_xi_release,
  5298. + .read = dbgaufs_xi_read
  5299. +};
  5300. +
  5301. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5302. +{
  5303. + aufs_bindex_t bbot;
  5304. + struct au_branch *br;
  5305. + struct au_xino_file *xi;
  5306. +
  5307. + if (!au_sbi(sb)->si_dbgaufs)
  5308. + return;
  5309. +
  5310. + bbot = au_sbbot(sb);
  5311. + for (; bindex <= bbot; bindex++) {
  5312. + br = au_sbr(sb, bindex);
  5313. + xi = &br->br_xino;
  5314. + /* debugfs acquires the parent i_mutex */
  5315. + lockdep_off();
  5316. + debugfs_remove(xi->xi_dbgaufs);
  5317. + lockdep_on();
  5318. + xi->xi_dbgaufs = NULL;
  5319. + }
  5320. +}
  5321. +
  5322. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  5323. +{
  5324. + struct au_sbinfo *sbinfo;
  5325. + struct dentry *parent;
  5326. + struct au_branch *br;
  5327. + struct au_xino_file *xi;
  5328. + aufs_bindex_t bbot;
  5329. + char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
  5330. +
  5331. + sbinfo = au_sbi(sb);
  5332. + parent = sbinfo->si_dbgaufs;
  5333. + if (!parent)
  5334. + return;
  5335. +
  5336. + bbot = au_sbbot(sb);
  5337. + for (; bindex <= bbot; bindex++) {
  5338. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5339. + br = au_sbr(sb, bindex);
  5340. + xi = &br->br_xino;
  5341. + AuDebugOn(xi->xi_dbgaufs);
  5342. + /* debugfs acquires the parent i_mutex */
  5343. + lockdep_off();
  5344. + xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5345. + sbinfo, &dbgaufs_xino_fop);
  5346. + lockdep_on();
  5347. + /* ignore an error */
  5348. + if (unlikely(!xi->xi_dbgaufs))
  5349. + AuWarn1("failed %s under debugfs\n", name);
  5350. + }
  5351. +}
  5352. +
  5353. +/* ---------------------------------------------------------------------- */
  5354. +
  5355. +#ifdef CONFIG_AUFS_EXPORT
  5356. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5357. +{
  5358. + int err;
  5359. + struct au_sbinfo *sbinfo;
  5360. + struct super_block *sb;
  5361. +
  5362. + sbinfo = inode->i_private;
  5363. + sb = sbinfo->si_sb;
  5364. + si_noflush_read_lock(sb);
  5365. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
  5366. + si_read_unlock(sb);
  5367. + return err;
  5368. +}
  5369. +
  5370. +static const struct file_operations dbgaufs_xigen_fop = {
  5371. + .owner = THIS_MODULE,
  5372. + .open = dbgaufs_xigen_open,
  5373. + .release = dbgaufs_xi_release,
  5374. + .read = dbgaufs_xi_read
  5375. +};
  5376. +
  5377. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5378. +{
  5379. + int err;
  5380. +
  5381. + /*
  5382. + * This function is a dynamic '__init' function actually,
  5383. + * so the tiny check for si_rwsem is unnecessary.
  5384. + */
  5385. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5386. +
  5387. + err = -EIO;
  5388. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5389. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5390. + &dbgaufs_xigen_fop);
  5391. + if (sbinfo->si_dbgaufs_xigen)
  5392. + err = 0;
  5393. +
  5394. + return err;
  5395. +}
  5396. +#else
  5397. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5398. +{
  5399. + return 0;
  5400. +}
  5401. +#endif /* CONFIG_AUFS_EXPORT */
  5402. +
  5403. +/* ---------------------------------------------------------------------- */
  5404. +
  5405. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5406. +{
  5407. + /*
  5408. + * This function is a dynamic '__fin' function actually,
  5409. + * so the tiny check for si_rwsem is unnecessary.
  5410. + */
  5411. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5412. +
  5413. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5414. + sbinfo->si_dbgaufs = NULL;
  5415. + kobject_put(&sbinfo->si_kobj);
  5416. +}
  5417. +
  5418. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5419. +{
  5420. + int err;
  5421. + char name[SysaufsSiNameLen];
  5422. +
  5423. + /*
  5424. + * This function is a dynamic '__init' function actually,
  5425. + * so the tiny check for si_rwsem is unnecessary.
  5426. + */
  5427. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5428. +
  5429. + err = -ENOENT;
  5430. + if (!dbgaufs) {
  5431. + AuErr1("/debug/aufs is uninitialized\n");
  5432. + goto out;
  5433. + }
  5434. +
  5435. + err = -EIO;
  5436. + sysaufs_name(sbinfo, name);
  5437. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5438. + if (unlikely(!sbinfo->si_dbgaufs))
  5439. + goto out;
  5440. + kobject_get(&sbinfo->si_kobj);
  5441. +
  5442. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5443. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5444. + &dbgaufs_xib_fop);
  5445. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5446. + goto out_dir;
  5447. +
  5448. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5449. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5450. + &dbgaufs_plink_fop);
  5451. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5452. + goto out_dir;
  5453. +
  5454. + err = dbgaufs_xigen_init(sbinfo);
  5455. + if (!err)
  5456. + goto out; /* success */
  5457. +
  5458. +out_dir:
  5459. + dbgaufs_si_fin(sbinfo);
  5460. +out:
  5461. + return err;
  5462. +}
  5463. +
  5464. +/* ---------------------------------------------------------------------- */
  5465. +
  5466. +void dbgaufs_fin(void)
  5467. +{
  5468. + debugfs_remove(dbgaufs);
  5469. +}
  5470. +
  5471. +int __init dbgaufs_init(void)
  5472. +{
  5473. + int err;
  5474. +
  5475. + err = -EIO;
  5476. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5477. + if (dbgaufs)
  5478. + err = 0;
  5479. + return err;
  5480. +}
  5481. --- /dev/null
  5482. +++ linux-4.4/fs/aufs/dbgaufs.h 2016-10-23 11:20:57.625493286 +0300
  5483. @@ -0,0 +1,48 @@
  5484. +/*
  5485. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5486. + *
  5487. + * This program, aufs is free software; you can redistribute it and/or modify
  5488. + * it under the terms of the GNU General Public License as published by
  5489. + * the Free Software Foundation; either version 2 of the License, or
  5490. + * (at your option) any later version.
  5491. + *
  5492. + * This program is distributed in the hope that it will be useful,
  5493. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5494. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5495. + * GNU General Public License for more details.
  5496. + *
  5497. + * You should have received a copy of the GNU General Public License
  5498. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5499. + */
  5500. +
  5501. +/*
  5502. + * debugfs interface
  5503. + */
  5504. +
  5505. +#ifndef __DBGAUFS_H__
  5506. +#define __DBGAUFS_H__
  5507. +
  5508. +#ifdef __KERNEL__
  5509. +
  5510. +struct super_block;
  5511. +struct au_sbinfo;
  5512. +
  5513. +#ifdef CONFIG_DEBUG_FS
  5514. +/* dbgaufs.c */
  5515. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5516. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  5517. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5518. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5519. +void dbgaufs_fin(void);
  5520. +int __init dbgaufs_init(void);
  5521. +#else
  5522. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5523. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  5524. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5525. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5526. +AuStubVoid(dbgaufs_fin, void)
  5527. +AuStubInt0(__init dbgaufs_init, void)
  5528. +#endif /* CONFIG_DEBUG_FS */
  5529. +
  5530. +#endif /* __KERNEL__ */
  5531. +#endif /* __DBGAUFS_H__ */
  5532. --- /dev/null
  5533. +++ linux-4.4/fs/aufs/dcsub.c 2016-10-23 11:20:57.625493286 +0300
  5534. @@ -0,0 +1,225 @@
  5535. +/*
  5536. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5537. + *
  5538. + * This program, aufs is free software; you can redistribute it and/or modify
  5539. + * it under the terms of the GNU General Public License as published by
  5540. + * the Free Software Foundation; either version 2 of the License, or
  5541. + * (at your option) any later version.
  5542. + *
  5543. + * This program is distributed in the hope that it will be useful,
  5544. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5545. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5546. + * GNU General Public License for more details.
  5547. + *
  5548. + * You should have received a copy of the GNU General Public License
  5549. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5550. + */
  5551. +
  5552. +/*
  5553. + * sub-routines for dentry cache
  5554. + */
  5555. +
  5556. +#include "aufs.h"
  5557. +
  5558. +static void au_dpage_free(struct au_dpage *dpage)
  5559. +{
  5560. + int i;
  5561. + struct dentry **p;
  5562. +
  5563. + p = dpage->dentries;
  5564. + for (i = 0; i < dpage->ndentry; i++)
  5565. + dput(*p++);
  5566. + au_delayed_free_page((unsigned long)dpage->dentries);
  5567. +}
  5568. +
  5569. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  5570. +{
  5571. + int err;
  5572. + void *p;
  5573. +
  5574. + err = -ENOMEM;
  5575. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  5576. + if (unlikely(!dpages->dpages))
  5577. + goto out;
  5578. +
  5579. + p = (void *)__get_free_page(gfp);
  5580. + if (unlikely(!p))
  5581. + goto out_dpages;
  5582. +
  5583. + dpages->dpages[0].ndentry = 0;
  5584. + dpages->dpages[0].dentries = p;
  5585. + dpages->ndpage = 1;
  5586. + return 0; /* success */
  5587. +
  5588. +out_dpages:
  5589. + au_delayed_kfree(dpages->dpages);
  5590. +out:
  5591. + return err;
  5592. +}
  5593. +
  5594. +void au_dpages_free(struct au_dcsub_pages *dpages)
  5595. +{
  5596. + int i;
  5597. + struct au_dpage *p;
  5598. +
  5599. + p = dpages->dpages;
  5600. + for (i = 0; i < dpages->ndpage; i++)
  5601. + au_dpage_free(p++);
  5602. + au_delayed_kfree(dpages->dpages);
  5603. +}
  5604. +
  5605. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  5606. + struct dentry *dentry, gfp_t gfp)
  5607. +{
  5608. + int err, sz;
  5609. + struct au_dpage *dpage;
  5610. + void *p;
  5611. +
  5612. + dpage = dpages->dpages + dpages->ndpage - 1;
  5613. + sz = PAGE_SIZE / sizeof(dentry);
  5614. + if (unlikely(dpage->ndentry >= sz)) {
  5615. + AuLabel(new dpage);
  5616. + err = -ENOMEM;
  5617. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  5618. + p = au_kzrealloc(dpages->dpages, sz,
  5619. + sz + sizeof(*dpages->dpages), gfp,
  5620. + /*may_shrink*/0);
  5621. + if (unlikely(!p))
  5622. + goto out;
  5623. +
  5624. + dpages->dpages = p;
  5625. + dpage = dpages->dpages + dpages->ndpage;
  5626. + p = (void *)__get_free_page(gfp);
  5627. + if (unlikely(!p))
  5628. + goto out;
  5629. +
  5630. + dpage->ndentry = 0;
  5631. + dpage->dentries = p;
  5632. + dpages->ndpage++;
  5633. + }
  5634. +
  5635. + AuDebugOn(au_dcount(dentry) <= 0);
  5636. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  5637. + return 0; /* success */
  5638. +
  5639. +out:
  5640. + return err;
  5641. +}
  5642. +
  5643. +/* todo: BAD approach */
  5644. +/* copied from linux/fs/dcache.c */
  5645. +enum d_walk_ret {
  5646. + D_WALK_CONTINUE,
  5647. + D_WALK_QUIT,
  5648. + D_WALK_NORETRY,
  5649. + D_WALK_SKIP,
  5650. +};
  5651. +
  5652. +extern void d_walk(struct dentry *parent, void *data,
  5653. + enum d_walk_ret (*enter)(void *, struct dentry *),
  5654. + void (*finish)(void *));
  5655. +
  5656. +struct ac_dpages_arg {
  5657. + int err;
  5658. + struct au_dcsub_pages *dpages;
  5659. + struct super_block *sb;
  5660. + au_dpages_test test;
  5661. + void *arg;
  5662. +};
  5663. +
  5664. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  5665. +{
  5666. + enum d_walk_ret ret;
  5667. + struct ac_dpages_arg *arg = _arg;
  5668. +
  5669. + ret = D_WALK_CONTINUE;
  5670. + if (dentry->d_sb == arg->sb
  5671. + && !IS_ROOT(dentry)
  5672. + && au_dcount(dentry) > 0
  5673. + && au_di(dentry)
  5674. + && (!arg->test || arg->test(dentry, arg->arg))) {
  5675. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  5676. + if (unlikely(arg->err))
  5677. + ret = D_WALK_QUIT;
  5678. + }
  5679. +
  5680. + return ret;
  5681. +}
  5682. +
  5683. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5684. + au_dpages_test test, void *arg)
  5685. +{
  5686. + struct ac_dpages_arg args = {
  5687. + .err = 0,
  5688. + .dpages = dpages,
  5689. + .sb = root->d_sb,
  5690. + .test = test,
  5691. + .arg = arg
  5692. + };
  5693. +
  5694. + d_walk(root, &args, au_call_dpages_append, NULL);
  5695. +
  5696. + return args.err;
  5697. +}
  5698. +
  5699. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5700. + int do_include, au_dpages_test test, void *arg)
  5701. +{
  5702. + int err;
  5703. +
  5704. + err = 0;
  5705. + write_seqlock(&rename_lock);
  5706. + spin_lock(&dentry->d_lock);
  5707. + if (do_include
  5708. + && au_dcount(dentry) > 0
  5709. + && (!test || test(dentry, arg)))
  5710. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5711. + spin_unlock(&dentry->d_lock);
  5712. + if (unlikely(err))
  5713. + goto out;
  5714. +
  5715. + /*
  5716. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  5717. + * mount
  5718. + */
  5719. + while (!IS_ROOT(dentry)) {
  5720. + dentry = dentry->d_parent; /* rename_lock is locked */
  5721. + spin_lock(&dentry->d_lock);
  5722. + if (au_dcount(dentry) > 0
  5723. + && (!test || test(dentry, arg)))
  5724. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5725. + spin_unlock(&dentry->d_lock);
  5726. + if (unlikely(err))
  5727. + break;
  5728. + }
  5729. +
  5730. +out:
  5731. + write_sequnlock(&rename_lock);
  5732. + return err;
  5733. +}
  5734. +
  5735. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  5736. +{
  5737. + return au_di(dentry) && dentry->d_sb == arg;
  5738. +}
  5739. +
  5740. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5741. + struct dentry *dentry, int do_include)
  5742. +{
  5743. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  5744. + au_dcsub_dpages_aufs, dentry->d_sb);
  5745. +}
  5746. +
  5747. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  5748. +{
  5749. + struct path path[2] = {
  5750. + {
  5751. + .dentry = d1
  5752. + },
  5753. + {
  5754. + .dentry = d2
  5755. + }
  5756. + };
  5757. +
  5758. + return path_is_under(path + 0, path + 1);
  5759. +}
  5760. --- /dev/null
  5761. +++ linux-4.4/fs/aufs/dcsub.h 2016-10-23 11:20:57.625493286 +0300
  5762. @@ -0,0 +1,136 @@
  5763. +/*
  5764. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5765. + *
  5766. + * This program, aufs is free software; you can redistribute it and/or modify
  5767. + * it under the terms of the GNU General Public License as published by
  5768. + * the Free Software Foundation; either version 2 of the License, or
  5769. + * (at your option) any later version.
  5770. + *
  5771. + * This program is distributed in the hope that it will be useful,
  5772. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5773. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5774. + * GNU General Public License for more details.
  5775. + *
  5776. + * You should have received a copy of the GNU General Public License
  5777. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5778. + */
  5779. +
  5780. +/*
  5781. + * sub-routines for dentry cache
  5782. + */
  5783. +
  5784. +#ifndef __AUFS_DCSUB_H__
  5785. +#define __AUFS_DCSUB_H__
  5786. +
  5787. +#ifdef __KERNEL__
  5788. +
  5789. +#include <linux/dcache.h>
  5790. +#include <linux/fs.h>
  5791. +
  5792. +struct au_dpage {
  5793. + int ndentry;
  5794. + struct dentry **dentries;
  5795. +};
  5796. +
  5797. +struct au_dcsub_pages {
  5798. + int ndpage;
  5799. + struct au_dpage *dpages;
  5800. +};
  5801. +
  5802. +/* ---------------------------------------------------------------------- */
  5803. +
  5804. +/* dcsub.c */
  5805. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  5806. +void au_dpages_free(struct au_dcsub_pages *dpages);
  5807. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  5808. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5809. + au_dpages_test test, void *arg);
  5810. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5811. + int do_include, au_dpages_test test, void *arg);
  5812. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5813. + struct dentry *dentry, int do_include);
  5814. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  5815. +
  5816. +/* ---------------------------------------------------------------------- */
  5817. +
  5818. +/*
  5819. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  5820. + * include/linux/dcache.h. Try them (in the future).
  5821. + */
  5822. +
  5823. +static inline int au_d_hashed_positive(struct dentry *d)
  5824. +{
  5825. + int err;
  5826. + struct inode *inode = d_inode(d);
  5827. +
  5828. + err = 0;
  5829. + if (unlikely(d_unhashed(d)
  5830. + || d_is_negative(d)
  5831. + || !inode->i_nlink))
  5832. + err = -ENOENT;
  5833. + return err;
  5834. +}
  5835. +
  5836. +static inline int au_d_linkable(struct dentry *d)
  5837. +{
  5838. + int err;
  5839. + struct inode *inode = d_inode(d);
  5840. +
  5841. + err = au_d_hashed_positive(d);
  5842. + if (err
  5843. + && d_is_positive(d)
  5844. + && (inode->i_state & I_LINKABLE))
  5845. + err = 0;
  5846. + return err;
  5847. +}
  5848. +
  5849. +static inline int au_d_alive(struct dentry *d)
  5850. +{
  5851. + int err;
  5852. + struct inode *inode;
  5853. +
  5854. + err = 0;
  5855. + if (!IS_ROOT(d))
  5856. + err = au_d_hashed_positive(d);
  5857. + else {
  5858. + inode = d_inode(d);
  5859. + if (unlikely(d_unlinked(d)
  5860. + || d_is_negative(d)
  5861. + || !inode->i_nlink))
  5862. + err = -ENOENT;
  5863. + }
  5864. + return err;
  5865. +}
  5866. +
  5867. +static inline int au_alive_dir(struct dentry *d)
  5868. +{
  5869. + int err;
  5870. +
  5871. + err = au_d_alive(d);
  5872. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  5873. + err = -ENOENT;
  5874. + return err;
  5875. +}
  5876. +
  5877. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  5878. +{
  5879. + return a->len == b->len
  5880. + && !memcmp(a->name, b->name, a->len);
  5881. +}
  5882. +
  5883. +/*
  5884. + * by the commit
  5885. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  5886. + * taking d_lock
  5887. + * the type of d_lockref.count became int, but the inlined function d_count()
  5888. + * still returns unsigned int.
  5889. + * I don't know why. Maybe it is for every d_count() users?
  5890. + * Anyway au_dcount() lives on.
  5891. + */
  5892. +static inline int au_dcount(struct dentry *d)
  5893. +{
  5894. + return (int)d_count(d);
  5895. +}
  5896. +
  5897. +#endif /* __KERNEL__ */
  5898. +#endif /* __AUFS_DCSUB_H__ */
  5899. --- /dev/null
  5900. +++ linux-4.4/fs/aufs/debug.c 2016-10-23 11:20:57.625493286 +0300
  5901. @@ -0,0 +1,440 @@
  5902. +/*
  5903. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  5904. + *
  5905. + * This program, aufs is free software; you can redistribute it and/or modify
  5906. + * it under the terms of the GNU General Public License as published by
  5907. + * the Free Software Foundation; either version 2 of the License, or
  5908. + * (at your option) any later version.
  5909. + *
  5910. + * This program is distributed in the hope that it will be useful,
  5911. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5912. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5913. + * GNU General Public License for more details.
  5914. + *
  5915. + * You should have received a copy of the GNU General Public License
  5916. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5917. + */
  5918. +
  5919. +/*
  5920. + * debug print functions
  5921. + */
  5922. +
  5923. +#include "aufs.h"
  5924. +
  5925. +/* Returns 0, or -errno. arg is in kp->arg. */
  5926. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  5927. +{
  5928. + int err, n;
  5929. +
  5930. + err = kstrtoint(val, 0, &n);
  5931. + if (!err) {
  5932. + if (n > 0)
  5933. + au_debug_on();
  5934. + else
  5935. + au_debug_off();
  5936. + }
  5937. + return err;
  5938. +}
  5939. +
  5940. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  5941. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  5942. +{
  5943. + atomic_t *a;
  5944. +
  5945. + a = kp->arg;
  5946. + return sprintf(buffer, "%d", atomic_read(a));
  5947. +}
  5948. +
  5949. +static struct kernel_param_ops param_ops_atomic_t = {
  5950. + .set = param_atomic_t_set,
  5951. + .get = param_atomic_t_get
  5952. + /* void (*free)(void *arg) */
  5953. +};
  5954. +
  5955. +atomic_t aufs_debug = ATOMIC_INIT(0);
  5956. +MODULE_PARM_DESC(debug, "debug print");
  5957. +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
  5958. +
  5959. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  5960. +char *au_plevel = KERN_DEBUG;
  5961. +#define dpri(fmt, ...) do { \
  5962. + if ((au_plevel \
  5963. + && strcmp(au_plevel, KERN_DEBUG)) \
  5964. + || au_debug_test()) \
  5965. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  5966. +} while (0)
  5967. +
  5968. +/* ---------------------------------------------------------------------- */
  5969. +
  5970. +void au_dpri_whlist(struct au_nhash *whlist)
  5971. +{
  5972. + unsigned long ul, n;
  5973. + struct hlist_head *head;
  5974. + struct au_vdir_wh *pos;
  5975. +
  5976. + n = whlist->nh_num;
  5977. + head = whlist->nh_head;
  5978. + for (ul = 0; ul < n; ul++) {
  5979. + hlist_for_each_entry(pos, head, wh_hash)
  5980. + dpri("b%d, %.*s, %d\n",
  5981. + pos->wh_bindex,
  5982. + pos->wh_str.len, pos->wh_str.name,
  5983. + pos->wh_str.len);
  5984. + head++;
  5985. + }
  5986. +}
  5987. +
  5988. +void au_dpri_vdir(struct au_vdir *vdir)
  5989. +{
  5990. + unsigned long ul;
  5991. + union au_vdir_deblk_p p;
  5992. + unsigned char *o;
  5993. +
  5994. + if (!vdir || IS_ERR(vdir)) {
  5995. + dpri("err %ld\n", PTR_ERR(vdir));
  5996. + return;
  5997. + }
  5998. +
  5999. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
  6000. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  6001. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  6002. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  6003. + p.deblk = vdir->vd_deblk[ul];
  6004. + o = p.deblk;
  6005. + dpri("[%lu]: %p\n", ul, o);
  6006. + }
  6007. +}
  6008. +
  6009. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  6010. + struct dentry *wh)
  6011. +{
  6012. + char *n = NULL;
  6013. + int l = 0;
  6014. +
  6015. + if (!inode || IS_ERR(inode)) {
  6016. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  6017. + return -1;
  6018. + }
  6019. +
  6020. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  6021. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  6022. + && sizeof(inode->i_blocks) != sizeof(u64));
  6023. + if (wh) {
  6024. + n = (void *)wh->d_name.name;
  6025. + l = wh->d_name.len;
  6026. + }
  6027. +
  6028. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  6029. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  6030. + bindex, inode,
  6031. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  6032. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  6033. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  6034. + hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
  6035. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  6036. + inode->i_state, inode->i_flags, inode->i_version,
  6037. + inode->i_generation,
  6038. + l ? ", wh " : "", l, n);
  6039. + return 0;
  6040. +}
  6041. +
  6042. +void au_dpri_inode(struct inode *inode)
  6043. +{
  6044. + struct au_iinfo *iinfo;
  6045. + struct au_hinode *hi;
  6046. + aufs_bindex_t bindex;
  6047. + int err, hn;
  6048. +
  6049. + err = do_pri_inode(-1, inode, -1, NULL);
  6050. + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
  6051. + return;
  6052. +
  6053. + iinfo = au_ii(inode);
  6054. + dpri("i-1: btop %d, bbot %d, gen %d\n",
  6055. + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
  6056. + if (iinfo->ii_btop < 0)
  6057. + return;
  6058. + hn = 0;
  6059. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
  6060. + hi = au_hinode(iinfo, bindex);
  6061. + hn = !!au_hn(hi);
  6062. + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
  6063. + }
  6064. +}
  6065. +
  6066. +void au_dpri_dalias(struct inode *inode)
  6067. +{
  6068. + struct dentry *d;
  6069. +
  6070. + spin_lock(&inode->i_lock);
  6071. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6072. + au_dpri_dentry(d);
  6073. + spin_unlock(&inode->i_lock);
  6074. +}
  6075. +
  6076. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6077. +{
  6078. + struct dentry *wh = NULL;
  6079. + int hn;
  6080. + struct inode *inode;
  6081. + struct au_iinfo *iinfo;
  6082. + struct au_hinode *hi;
  6083. +
  6084. + if (!dentry || IS_ERR(dentry)) {
  6085. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6086. + return -1;
  6087. + }
  6088. + /* do not call dget_parent() here */
  6089. + /* note: access d_xxx without d_lock */
  6090. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6091. + bindex, dentry, dentry,
  6092. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6093. + au_dcount(dentry), dentry->d_flags,
  6094. + d_unhashed(dentry) ? "un" : "");
  6095. + hn = -1;
  6096. + inode = NULL;
  6097. + if (d_is_positive(dentry))
  6098. + inode = d_inode(dentry);
  6099. + if (inode
  6100. + && au_test_aufs(dentry->d_sb)
  6101. + && bindex >= 0
  6102. + && !au_is_bad_inode(inode)) {
  6103. + iinfo = au_ii(inode);
  6104. + hi = au_hinode(iinfo, bindex);
  6105. + hn = !!au_hn(hi);
  6106. + wh = hi->hi_whdentry;
  6107. + }
  6108. + do_pri_inode(bindex, inode, hn, wh);
  6109. + return 0;
  6110. +}
  6111. +
  6112. +void au_dpri_dentry(struct dentry *dentry)
  6113. +{
  6114. + struct au_dinfo *dinfo;
  6115. + aufs_bindex_t bindex;
  6116. + int err;
  6117. +
  6118. + err = do_pri_dentry(-1, dentry);
  6119. + if (err || !au_test_aufs(dentry->d_sb))
  6120. + return;
  6121. +
  6122. + dinfo = au_di(dentry);
  6123. + if (!dinfo)
  6124. + return;
  6125. + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6126. + dinfo->di_btop, dinfo->di_bbot,
  6127. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6128. + dinfo->di_tmpfile);
  6129. + if (dinfo->di_btop < 0)
  6130. + return;
  6131. + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
  6132. + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
  6133. +}
  6134. +
  6135. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6136. +{
  6137. + char a[32];
  6138. +
  6139. + if (!file || IS_ERR(file)) {
  6140. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6141. + return -1;
  6142. + }
  6143. + a[0] = 0;
  6144. + if (bindex < 0
  6145. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6146. + && au_test_aufs(file->f_path.dentry->d_sb)
  6147. + && au_fi(file))
  6148. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6149. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6150. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6151. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6152. + file->f_version, file->f_pos, a);
  6153. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6154. + do_pri_dentry(bindex, file->f_path.dentry);
  6155. + return 0;
  6156. +}
  6157. +
  6158. +void au_dpri_file(struct file *file)
  6159. +{
  6160. + struct au_finfo *finfo;
  6161. + struct au_fidir *fidir;
  6162. + struct au_hfile *hfile;
  6163. + aufs_bindex_t bindex;
  6164. + int err;
  6165. +
  6166. + err = do_pri_file(-1, file);
  6167. + if (err
  6168. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6169. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6170. + return;
  6171. +
  6172. + finfo = au_fi(file);
  6173. + if (!finfo)
  6174. + return;
  6175. + if (finfo->fi_btop < 0)
  6176. + return;
  6177. + fidir = finfo->fi_hdir;
  6178. + if (!fidir)
  6179. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6180. + else
  6181. + for (bindex = finfo->fi_btop;
  6182. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6183. + bindex++) {
  6184. + hfile = fidir->fd_hfile + bindex;
  6185. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6186. + }
  6187. +}
  6188. +
  6189. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6190. +{
  6191. + struct vfsmount *mnt;
  6192. + struct super_block *sb;
  6193. +
  6194. + if (!br || IS_ERR(br))
  6195. + goto out;
  6196. + mnt = au_br_mnt(br);
  6197. + if (!mnt || IS_ERR(mnt))
  6198. + goto out;
  6199. + sb = mnt->mnt_sb;
  6200. + if (!sb || IS_ERR(sb))
  6201. + goto out;
  6202. +
  6203. + dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
  6204. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6205. + "xino %d\n",
  6206. + bindex, br->br_perm, br->br_id, au_br_count(br),
  6207. + br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6208. + sb->s_flags, sb->s_count,
  6209. + atomic_read(&sb->s_active), !!br->br_xino.xi_file);
  6210. + return 0;
  6211. +
  6212. +out:
  6213. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6214. + return -1;
  6215. +}
  6216. +
  6217. +void au_dpri_sb(struct super_block *sb)
  6218. +{
  6219. + struct au_sbinfo *sbinfo;
  6220. + aufs_bindex_t bindex;
  6221. + int err;
  6222. + /* to reuduce stack size */
  6223. + struct {
  6224. + struct vfsmount mnt;
  6225. + struct au_branch fake;
  6226. + } *a;
  6227. +
  6228. + /* this function can be called from magic sysrq */
  6229. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6230. + if (unlikely(!a)) {
  6231. + dpri("no memory\n");
  6232. + return;
  6233. + }
  6234. +
  6235. + a->mnt.mnt_sb = sb;
  6236. + a->fake.br_path.mnt = &a->mnt;
  6237. + au_br_count_init(&a->fake);
  6238. + err = do_pri_br(-1, &a->fake);
  6239. + au_br_count_fin(&a->fake);
  6240. + au_delayed_kfree(a);
  6241. + dpri("dev 0x%x\n", sb->s_dev);
  6242. + if (err || !au_test_aufs(sb))
  6243. + return;
  6244. +
  6245. + sbinfo = au_sbi(sb);
  6246. + if (!sbinfo)
  6247. + return;
  6248. + dpri("nw %d, gen %u, kobj %d\n",
  6249. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6250. + atomic_read(&sbinfo->si_kobj.kref.refcount));
  6251. + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
  6252. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6253. +}
  6254. +
  6255. +/* ---------------------------------------------------------------------- */
  6256. +
  6257. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6258. +{
  6259. + struct inode *h_inode, *inode = d_inode(dentry);
  6260. + struct dentry *h_dentry;
  6261. + aufs_bindex_t bindex, bbot, bi;
  6262. +
  6263. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6264. + return;
  6265. +
  6266. + bbot = au_dbbot(dentry);
  6267. + bi = au_ibbot(inode);
  6268. + if (bi < bbot)
  6269. + bbot = bi;
  6270. + bindex = au_dbtop(dentry);
  6271. + bi = au_ibtop(inode);
  6272. + if (bi > bindex)
  6273. + bindex = bi;
  6274. +
  6275. + for (; bindex <= bbot; bindex++) {
  6276. + h_dentry = au_h_dptr(dentry, bindex);
  6277. + if (!h_dentry)
  6278. + continue;
  6279. + h_inode = au_h_iptr(inode, bindex);
  6280. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6281. + au_debug_on();
  6282. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6283. + AuDbgDentry(dentry);
  6284. + AuDbgInode(inode);
  6285. + au_debug_off();
  6286. + BUG();
  6287. + }
  6288. + }
  6289. +}
  6290. +
  6291. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6292. +{
  6293. + int err, i, j;
  6294. + struct au_dcsub_pages dpages;
  6295. + struct au_dpage *dpage;
  6296. + struct dentry **dentries;
  6297. +
  6298. + err = au_dpages_init(&dpages, GFP_NOFS);
  6299. + AuDebugOn(err);
  6300. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6301. + AuDebugOn(err);
  6302. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6303. + dpage = dpages.dpages + i;
  6304. + dentries = dpage->dentries;
  6305. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6306. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6307. + }
  6308. + au_dpages_free(&dpages);
  6309. +}
  6310. +
  6311. +void au_dbg_verify_kthread(void)
  6312. +{
  6313. + if (au_wkq_test()) {
  6314. + au_dbg_blocked();
  6315. + /*
  6316. + * It may be recursive, but udba=notify between two aufs mounts,
  6317. + * where a single ro branch is shared, is not a problem.
  6318. + */
  6319. + /* WARN_ON(1); */
  6320. + }
  6321. +}
  6322. +
  6323. +/* ---------------------------------------------------------------------- */
  6324. +
  6325. +int __init au_debug_init(void)
  6326. +{
  6327. + aufs_bindex_t bindex;
  6328. + struct au_vdir_destr destr;
  6329. +
  6330. + bindex = -1;
  6331. + AuDebugOn(bindex >= 0);
  6332. +
  6333. + destr.len = -1;
  6334. + AuDebugOn(destr.len < NAME_MAX);
  6335. +
  6336. +#ifdef CONFIG_4KSTACKS
  6337. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6338. +#endif
  6339. +
  6340. + return 0;
  6341. +}
  6342. --- /dev/null
  6343. +++ linux-4.4/fs/aufs/debug.h 2016-10-23 11:20:57.628826619 +0300
  6344. @@ -0,0 +1,225 @@
  6345. +/*
  6346. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  6347. + *
  6348. + * This program, aufs is free software; you can redistribute it and/or modify
  6349. + * it under the terms of the GNU General Public License as published by
  6350. + * the Free Software Foundation; either version 2 of the License, or
  6351. + * (at your option) any later version.
  6352. + *
  6353. + * This program is distributed in the hope that it will be useful,
  6354. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6355. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6356. + * GNU General Public License for more details.
  6357. + *
  6358. + * You should have received a copy of the GNU General Public License
  6359. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6360. + */
  6361. +
  6362. +/*
  6363. + * debug print functions
  6364. + */
  6365. +
  6366. +#ifndef __AUFS_DEBUG_H__
  6367. +#define __AUFS_DEBUG_H__
  6368. +
  6369. +#ifdef __KERNEL__
  6370. +
  6371. +#include <linux/atomic.h>
  6372. +#include <linux/module.h>
  6373. +#include <linux/kallsyms.h>
  6374. +#include <linux/sysrq.h>
  6375. +
  6376. +#ifdef CONFIG_AUFS_DEBUG
  6377. +#define AuDebugOn(a) BUG_ON(a)
  6378. +
  6379. +/* module parameter */
  6380. +extern atomic_t aufs_debug;
  6381. +static inline void au_debug_on(void)
  6382. +{
  6383. + atomic_inc(&aufs_debug);
  6384. +}
  6385. +static inline void au_debug_off(void)
  6386. +{
  6387. + atomic_dec_if_positive(&aufs_debug);
  6388. +}
  6389. +
  6390. +static inline int au_debug_test(void)
  6391. +{
  6392. + return atomic_read(&aufs_debug) > 0;
  6393. +}
  6394. +#else
  6395. +#define AuDebugOn(a) do {} while (0)
  6396. +AuStubVoid(au_debug_on, void)
  6397. +AuStubVoid(au_debug_off, void)
  6398. +AuStubInt0(au_debug_test, void)
  6399. +#endif /* CONFIG_AUFS_DEBUG */
  6400. +
  6401. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6402. +
  6403. +/* ---------------------------------------------------------------------- */
  6404. +
  6405. +/* debug print */
  6406. +
  6407. +#define AuDbg(fmt, ...) do { \
  6408. + if (au_debug_test()) \
  6409. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6410. +} while (0)
  6411. +#define AuLabel(l) AuDbg(#l "\n")
  6412. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6413. +#define AuWarn1(fmt, ...) do { \
  6414. + static unsigned char _c; \
  6415. + if (!_c++) \
  6416. + pr_warn(fmt, ##__VA_ARGS__); \
  6417. +} while (0)
  6418. +
  6419. +#define AuErr1(fmt, ...) do { \
  6420. + static unsigned char _c; \
  6421. + if (!_c++) \
  6422. + pr_err(fmt, ##__VA_ARGS__); \
  6423. +} while (0)
  6424. +
  6425. +#define AuIOErr1(fmt, ...) do { \
  6426. + static unsigned char _c; \
  6427. + if (!_c++) \
  6428. + AuIOErr(fmt, ##__VA_ARGS__); \
  6429. +} while (0)
  6430. +
  6431. +#define AuUnsupportMsg "This operation is not supported." \
  6432. + " Please report this application to aufs-users ML."
  6433. +#define AuUnsupport(fmt, ...) do { \
  6434. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6435. + dump_stack(); \
  6436. +} while (0)
  6437. +
  6438. +#define AuTraceErr(e) do { \
  6439. + if (unlikely((e) < 0)) \
  6440. + AuDbg("err %d\n", (int)(e)); \
  6441. +} while (0)
  6442. +
  6443. +#define AuTraceErrPtr(p) do { \
  6444. + if (IS_ERR(p)) \
  6445. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6446. +} while (0)
  6447. +
  6448. +/* dirty macros for debug print, use with "%.*s" and caution */
  6449. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6450. +
  6451. +/* ---------------------------------------------------------------------- */
  6452. +
  6453. +struct dentry;
  6454. +#ifdef CONFIG_AUFS_DEBUG
  6455. +extern struct mutex au_dbg_mtx;
  6456. +extern char *au_plevel;
  6457. +struct au_nhash;
  6458. +void au_dpri_whlist(struct au_nhash *whlist);
  6459. +struct au_vdir;
  6460. +void au_dpri_vdir(struct au_vdir *vdir);
  6461. +struct inode;
  6462. +void au_dpri_inode(struct inode *inode);
  6463. +void au_dpri_dalias(struct inode *inode);
  6464. +void au_dpri_dentry(struct dentry *dentry);
  6465. +struct file;
  6466. +void au_dpri_file(struct file *filp);
  6467. +struct super_block;
  6468. +void au_dpri_sb(struct super_block *sb);
  6469. +
  6470. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6471. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6472. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6473. +void au_dbg_verify_kthread(void);
  6474. +
  6475. +int __init au_debug_init(void);
  6476. +
  6477. +#define AuDbgWhlist(w) do { \
  6478. + mutex_lock(&au_dbg_mtx); \
  6479. + AuDbg(#w "\n"); \
  6480. + au_dpri_whlist(w); \
  6481. + mutex_unlock(&au_dbg_mtx); \
  6482. +} while (0)
  6483. +
  6484. +#define AuDbgVdir(v) do { \
  6485. + mutex_lock(&au_dbg_mtx); \
  6486. + AuDbg(#v "\n"); \
  6487. + au_dpri_vdir(v); \
  6488. + mutex_unlock(&au_dbg_mtx); \
  6489. +} while (0)
  6490. +
  6491. +#define AuDbgInode(i) do { \
  6492. + mutex_lock(&au_dbg_mtx); \
  6493. + AuDbg(#i "\n"); \
  6494. + au_dpri_inode(i); \
  6495. + mutex_unlock(&au_dbg_mtx); \
  6496. +} while (0)
  6497. +
  6498. +#define AuDbgDAlias(i) do { \
  6499. + mutex_lock(&au_dbg_mtx); \
  6500. + AuDbg(#i "\n"); \
  6501. + au_dpri_dalias(i); \
  6502. + mutex_unlock(&au_dbg_mtx); \
  6503. +} while (0)
  6504. +
  6505. +#define AuDbgDentry(d) do { \
  6506. + mutex_lock(&au_dbg_mtx); \
  6507. + AuDbg(#d "\n"); \
  6508. + au_dpri_dentry(d); \
  6509. + mutex_unlock(&au_dbg_mtx); \
  6510. +} while (0)
  6511. +
  6512. +#define AuDbgFile(f) do { \
  6513. + mutex_lock(&au_dbg_mtx); \
  6514. + AuDbg(#f "\n"); \
  6515. + au_dpri_file(f); \
  6516. + mutex_unlock(&au_dbg_mtx); \
  6517. +} while (0)
  6518. +
  6519. +#define AuDbgSb(sb) do { \
  6520. + mutex_lock(&au_dbg_mtx); \
  6521. + AuDbg(#sb "\n"); \
  6522. + au_dpri_sb(sb); \
  6523. + mutex_unlock(&au_dbg_mtx); \
  6524. +} while (0)
  6525. +
  6526. +#define AuDbgSym(addr) do { \
  6527. + char sym[KSYM_SYMBOL_LEN]; \
  6528. + sprint_symbol(sym, (unsigned long)addr); \
  6529. + AuDbg("%s\n", sym); \
  6530. +} while (0)
  6531. +#else
  6532. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6533. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6534. +AuStubVoid(au_dbg_verify_kthread, void)
  6535. +AuStubInt0(__init au_debug_init, void)
  6536. +
  6537. +#define AuDbgWhlist(w) do {} while (0)
  6538. +#define AuDbgVdir(v) do {} while (0)
  6539. +#define AuDbgInode(i) do {} while (0)
  6540. +#define AuDbgDAlias(i) do {} while (0)
  6541. +#define AuDbgDentry(d) do {} while (0)
  6542. +#define AuDbgFile(f) do {} while (0)
  6543. +#define AuDbgSb(sb) do {} while (0)
  6544. +#define AuDbgSym(addr) do {} while (0)
  6545. +#endif /* CONFIG_AUFS_DEBUG */
  6546. +
  6547. +/* ---------------------------------------------------------------------- */
  6548. +
  6549. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  6550. +int __init au_sysrq_init(void);
  6551. +void au_sysrq_fin(void);
  6552. +
  6553. +#ifdef CONFIG_HW_CONSOLE
  6554. +#define au_dbg_blocked() do { \
  6555. + WARN_ON(1); \
  6556. + handle_sysrq('w'); \
  6557. +} while (0)
  6558. +#else
  6559. +AuStubVoid(au_dbg_blocked, void)
  6560. +#endif
  6561. +
  6562. +#else
  6563. +AuStubInt0(__init au_sysrq_init, void)
  6564. +AuStubVoid(au_sysrq_fin, void)
  6565. +AuStubVoid(au_dbg_blocked, void)
  6566. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  6567. +
  6568. +#endif /* __KERNEL__ */
  6569. +#endif /* __AUFS_DEBUG_H__ */
  6570. --- /dev/null
  6571. +++ linux-4.4/fs/aufs/dentry.c 2016-10-23 11:20:57.628826619 +0300
  6572. @@ -0,0 +1,1130 @@
  6573. +/*
  6574. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  6575. + *
  6576. + * This program, aufs is free software; you can redistribute it and/or modify
  6577. + * it under the terms of the GNU General Public License as published by
  6578. + * the Free Software Foundation; either version 2 of the License, or
  6579. + * (at your option) any later version.
  6580. + *
  6581. + * This program is distributed in the hope that it will be useful,
  6582. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6583. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6584. + * GNU General Public License for more details.
  6585. + *
  6586. + * You should have received a copy of the GNU General Public License
  6587. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6588. + */
  6589. +
  6590. +/*
  6591. + * lookup and dentry operations
  6592. + */
  6593. +
  6594. +#include <linux/namei.h>
  6595. +#include "aufs.h"
  6596. +
  6597. +struct au_do_lookup_args {
  6598. + unsigned int flags;
  6599. + mode_t type;
  6600. +};
  6601. +
  6602. +/*
  6603. + * returns positive/negative dentry, NULL or an error.
  6604. + * NULL means whiteout-ed or not-found.
  6605. + */
  6606. +static struct dentry*
  6607. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  6608. + aufs_bindex_t bindex, struct qstr *wh_name,
  6609. + struct au_do_lookup_args *args)
  6610. +{
  6611. + struct dentry *h_dentry;
  6612. + struct inode *h_inode;
  6613. + struct au_branch *br;
  6614. + int wh_found, opq;
  6615. + unsigned char wh_able;
  6616. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  6617. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  6618. + IGNORE_PERM);
  6619. +
  6620. + wh_found = 0;
  6621. + br = au_sbr(dentry->d_sb, bindex);
  6622. + wh_able = !!au_br_whable(br->br_perm);
  6623. + if (wh_able)
  6624. + wh_found = au_wh_test(h_parent, wh_name, ignore_perm);
  6625. + h_dentry = ERR_PTR(wh_found);
  6626. + if (!wh_found)
  6627. + goto real_lookup;
  6628. + if (unlikely(wh_found < 0))
  6629. + goto out;
  6630. +
  6631. + /* We found a whiteout */
  6632. + /* au_set_dbbot(dentry, bindex); */
  6633. + au_set_dbwh(dentry, bindex);
  6634. + if (!allow_neg)
  6635. + return NULL; /* success */
  6636. +
  6637. +real_lookup:
  6638. + if (!ignore_perm)
  6639. + h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  6640. + else
  6641. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6642. + if (IS_ERR(h_dentry)) {
  6643. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  6644. + && !allow_neg)
  6645. + h_dentry = NULL;
  6646. + goto out;
  6647. + }
  6648. +
  6649. + h_inode = d_inode(h_dentry);
  6650. + if (d_is_negative(h_dentry)) {
  6651. + if (!allow_neg)
  6652. + goto out_neg;
  6653. + } else if (wh_found
  6654. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  6655. + goto out_neg;
  6656. +
  6657. + if (au_dbbot(dentry) <= bindex)
  6658. + au_set_dbbot(dentry, bindex);
  6659. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  6660. + au_set_dbtop(dentry, bindex);
  6661. + au_set_h_dptr(dentry, bindex, h_dentry);
  6662. +
  6663. + if (!d_is_dir(h_dentry)
  6664. + || !wh_able
  6665. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  6666. + goto out; /* success */
  6667. +
  6668. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  6669. + opq = au_diropq_test(h_dentry);
  6670. + mutex_unlock(&h_inode->i_mutex);
  6671. + if (opq > 0)
  6672. + au_set_dbdiropq(dentry, bindex);
  6673. + else if (unlikely(opq < 0)) {
  6674. + au_set_h_dptr(dentry, bindex, NULL);
  6675. + h_dentry = ERR_PTR(opq);
  6676. + }
  6677. + goto out;
  6678. +
  6679. +out_neg:
  6680. + dput(h_dentry);
  6681. + h_dentry = NULL;
  6682. +out:
  6683. + return h_dentry;
  6684. +}
  6685. +
  6686. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  6687. +{
  6688. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  6689. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  6690. + return -EPERM;
  6691. + return 0;
  6692. +}
  6693. +
  6694. +/*
  6695. + * returns the number of lower positive dentries,
  6696. + * otherwise an error.
  6697. + * can be called at unlinking with @type is zero.
  6698. + */
  6699. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  6700. + unsigned int flags)
  6701. +{
  6702. + int npositive, err;
  6703. + aufs_bindex_t bindex, btail, bdiropq;
  6704. + unsigned char isdir, dirperm1;
  6705. + struct qstr whname;
  6706. + struct au_do_lookup_args args = {
  6707. + .flags = flags
  6708. + };
  6709. + const struct qstr *name = &dentry->d_name;
  6710. + struct dentry *parent;
  6711. + struct super_block *sb;
  6712. +
  6713. + sb = dentry->d_sb;
  6714. + err = au_test_shwh(sb, name);
  6715. + if (unlikely(err))
  6716. + goto out;
  6717. +
  6718. + err = au_wh_name_alloc(&whname, name);
  6719. + if (unlikely(err))
  6720. + goto out;
  6721. +
  6722. + isdir = !!d_is_dir(dentry);
  6723. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  6724. +
  6725. + npositive = 0;
  6726. + parent = dget_parent(dentry);
  6727. + btail = au_dbtaildir(parent);
  6728. + for (bindex = btop; bindex <= btail; bindex++) {
  6729. + struct dentry *h_parent, *h_dentry;
  6730. + struct inode *h_inode, *h_dir;
  6731. +
  6732. + h_dentry = au_h_dptr(dentry, bindex);
  6733. + if (h_dentry) {
  6734. + if (d_is_positive(h_dentry))
  6735. + npositive++;
  6736. + break;
  6737. + }
  6738. + h_parent = au_h_dptr(parent, bindex);
  6739. + if (!h_parent || !d_is_dir(h_parent))
  6740. + continue;
  6741. +
  6742. + h_dir = d_inode(h_parent);
  6743. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  6744. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
  6745. + &args);
  6746. + mutex_unlock(&h_dir->i_mutex);
  6747. + err = PTR_ERR(h_dentry);
  6748. + if (IS_ERR(h_dentry))
  6749. + goto out_parent;
  6750. + if (h_dentry)
  6751. + au_fclr_lkup(args.flags, ALLOW_NEG);
  6752. + if (dirperm1)
  6753. + au_fset_lkup(args.flags, IGNORE_PERM);
  6754. +
  6755. + if (au_dbwh(dentry) == bindex)
  6756. + break;
  6757. + if (!h_dentry)
  6758. + continue;
  6759. + if (d_is_negative(h_dentry))
  6760. + continue;
  6761. + h_inode = d_inode(h_dentry);
  6762. + npositive++;
  6763. + if (!args.type)
  6764. + args.type = h_inode->i_mode & S_IFMT;
  6765. + if (args.type != S_IFDIR)
  6766. + break;
  6767. + else if (isdir) {
  6768. + /* the type of lower may be different */
  6769. + bdiropq = au_dbdiropq(dentry);
  6770. + if (bdiropq >= 0 && bdiropq <= bindex)
  6771. + break;
  6772. + }
  6773. + }
  6774. +
  6775. + if (npositive) {
  6776. + AuLabel(positive);
  6777. + au_update_dbtop(dentry);
  6778. + }
  6779. + err = npositive;
  6780. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  6781. + && au_dbtop(dentry) < 0)) {
  6782. + err = -EIO;
  6783. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  6784. + dentry, err);
  6785. + }
  6786. +
  6787. +out_parent:
  6788. + dput(parent);
  6789. + au_delayed_kfree(whname.name);
  6790. +out:
  6791. + return err;
  6792. +}
  6793. +
  6794. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  6795. +{
  6796. + struct dentry *dentry;
  6797. + int wkq_err;
  6798. +
  6799. + if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
  6800. + dentry = vfsub_lkup_one(name, parent);
  6801. + else {
  6802. + struct vfsub_lkup_one_args args = {
  6803. + .errp = &dentry,
  6804. + .name = name,
  6805. + .parent = parent
  6806. + };
  6807. +
  6808. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  6809. + if (unlikely(wkq_err))
  6810. + dentry = ERR_PTR(wkq_err);
  6811. + }
  6812. +
  6813. + return dentry;
  6814. +}
  6815. +
  6816. +/*
  6817. + * lookup @dentry on @bindex which should be negative.
  6818. + */
  6819. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  6820. +{
  6821. + int err;
  6822. + struct dentry *parent, *h_parent, *h_dentry;
  6823. + struct au_branch *br;
  6824. +
  6825. + parent = dget_parent(dentry);
  6826. + h_parent = au_h_dptr(parent, bindex);
  6827. + br = au_sbr(dentry->d_sb, bindex);
  6828. + if (wh)
  6829. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  6830. + else
  6831. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6832. + err = PTR_ERR(h_dentry);
  6833. + if (IS_ERR(h_dentry))
  6834. + goto out;
  6835. + if (unlikely(d_is_positive(h_dentry))) {
  6836. + err = -EIO;
  6837. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  6838. + dput(h_dentry);
  6839. + goto out;
  6840. + }
  6841. +
  6842. + err = 0;
  6843. + if (bindex < au_dbtop(dentry))
  6844. + au_set_dbtop(dentry, bindex);
  6845. + if (au_dbbot(dentry) < bindex)
  6846. + au_set_dbbot(dentry, bindex);
  6847. + au_set_h_dptr(dentry, bindex, h_dentry);
  6848. +
  6849. +out:
  6850. + dput(parent);
  6851. + return err;
  6852. +}
  6853. +
  6854. +/* ---------------------------------------------------------------------- */
  6855. +
  6856. +/* subset of struct inode */
  6857. +struct au_iattr {
  6858. + unsigned long i_ino;
  6859. + /* unsigned int i_nlink; */
  6860. + kuid_t i_uid;
  6861. + kgid_t i_gid;
  6862. + u64 i_version;
  6863. +/*
  6864. + loff_t i_size;
  6865. + blkcnt_t i_blocks;
  6866. +*/
  6867. + umode_t i_mode;
  6868. +};
  6869. +
  6870. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  6871. +{
  6872. + ia->i_ino = h_inode->i_ino;
  6873. + /* ia->i_nlink = h_inode->i_nlink; */
  6874. + ia->i_uid = h_inode->i_uid;
  6875. + ia->i_gid = h_inode->i_gid;
  6876. + ia->i_version = h_inode->i_version;
  6877. +/*
  6878. + ia->i_size = h_inode->i_size;
  6879. + ia->i_blocks = h_inode->i_blocks;
  6880. +*/
  6881. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  6882. +}
  6883. +
  6884. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  6885. +{
  6886. + return ia->i_ino != h_inode->i_ino
  6887. + /* || ia->i_nlink != h_inode->i_nlink */
  6888. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  6889. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  6890. + || ia->i_version != h_inode->i_version
  6891. +/*
  6892. + || ia->i_size != h_inode->i_size
  6893. + || ia->i_blocks != h_inode->i_blocks
  6894. +*/
  6895. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  6896. +}
  6897. +
  6898. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  6899. + struct au_branch *br)
  6900. +{
  6901. + int err;
  6902. + struct au_iattr ia;
  6903. + struct inode *h_inode;
  6904. + struct dentry *h_d;
  6905. + struct super_block *h_sb;
  6906. +
  6907. + err = 0;
  6908. + memset(&ia, -1, sizeof(ia));
  6909. + h_sb = h_dentry->d_sb;
  6910. + h_inode = NULL;
  6911. + if (d_is_positive(h_dentry)) {
  6912. + h_inode = d_inode(h_dentry);
  6913. + au_iattr_save(&ia, h_inode);
  6914. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  6915. + /* nfs d_revalidate may return 0 for negative dentry */
  6916. + /* fuse d_revalidate always return 0 for negative dentry */
  6917. + goto out;
  6918. +
  6919. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  6920. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  6921. + err = PTR_ERR(h_d);
  6922. + if (IS_ERR(h_d))
  6923. + goto out;
  6924. +
  6925. + err = 0;
  6926. + if (unlikely(h_d != h_dentry
  6927. + || d_inode(h_d) != h_inode
  6928. + || (h_inode && au_iattr_test(&ia, h_inode))))
  6929. + err = au_busy_or_stale();
  6930. + dput(h_d);
  6931. +
  6932. +out:
  6933. + AuTraceErr(err);
  6934. + return err;
  6935. +}
  6936. +
  6937. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  6938. + struct dentry *h_parent, struct au_branch *br)
  6939. +{
  6940. + int err;
  6941. +
  6942. + err = 0;
  6943. + if (udba == AuOpt_UDBA_REVAL
  6944. + && !au_test_fs_remote(h_dentry->d_sb)) {
  6945. + IMustLock(h_dir);
  6946. + err = (d_inode(h_dentry->d_parent) != h_dir);
  6947. + } else if (udba != AuOpt_UDBA_NONE)
  6948. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  6949. +
  6950. + return err;
  6951. +}
  6952. +
  6953. +/* ---------------------------------------------------------------------- */
  6954. +
  6955. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  6956. +{
  6957. + int err;
  6958. + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
  6959. + struct au_hdentry tmp, *p, *q;
  6960. + struct au_dinfo *dinfo;
  6961. + struct super_block *sb;
  6962. +
  6963. + DiMustWriteLock(dentry);
  6964. +
  6965. + sb = dentry->d_sb;
  6966. + dinfo = au_di(dentry);
  6967. + bbot = dinfo->di_bbot;
  6968. + bwh = dinfo->di_bwh;
  6969. + bdiropq = dinfo->di_bdiropq;
  6970. + bindex = dinfo->di_btop;
  6971. + p = au_hdentry(dinfo, bindex);
  6972. + for (; bindex <= bbot; bindex++, p++) {
  6973. + if (!p->hd_dentry)
  6974. + continue;
  6975. +
  6976. + new_bindex = au_br_index(sb, p->hd_id);
  6977. + if (new_bindex == bindex)
  6978. + continue;
  6979. +
  6980. + if (dinfo->di_bwh == bindex)
  6981. + bwh = new_bindex;
  6982. + if (dinfo->di_bdiropq == bindex)
  6983. + bdiropq = new_bindex;
  6984. + if (new_bindex < 0) {
  6985. + au_hdput(p);
  6986. + p->hd_dentry = NULL;
  6987. + continue;
  6988. + }
  6989. +
  6990. + /* swap two lower dentries, and loop again */
  6991. + q = au_hdentry(dinfo, new_bindex);
  6992. + tmp = *q;
  6993. + *q = *p;
  6994. + *p = tmp;
  6995. + if (tmp.hd_dentry) {
  6996. + bindex--;
  6997. + p--;
  6998. + }
  6999. + }
  7000. +
  7001. + dinfo->di_bwh = -1;
  7002. + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
  7003. + dinfo->di_bwh = bwh;
  7004. +
  7005. + dinfo->di_bdiropq = -1;
  7006. + if (bdiropq >= 0
  7007. + && bdiropq <= au_sbbot(sb)
  7008. + && au_sbr_whable(sb, bdiropq))
  7009. + dinfo->di_bdiropq = bdiropq;
  7010. +
  7011. + err = -EIO;
  7012. + dinfo->di_btop = -1;
  7013. + dinfo->di_bbot = -1;
  7014. + bbot = au_dbbot(parent);
  7015. + bindex = 0;
  7016. + p = au_hdentry(dinfo, bindex);
  7017. + for (; bindex <= bbot; bindex++, p++)
  7018. + if (p->hd_dentry) {
  7019. + dinfo->di_btop = bindex;
  7020. + break;
  7021. + }
  7022. +
  7023. + if (dinfo->di_btop >= 0) {
  7024. + bindex = bbot;
  7025. + p = au_hdentry(dinfo, bindex);
  7026. + for (; bindex >= 0; bindex--, p--)
  7027. + if (p->hd_dentry) {
  7028. + dinfo->di_bbot = bindex;
  7029. + err = 0;
  7030. + break;
  7031. + }
  7032. + }
  7033. +
  7034. + return err;
  7035. +}
  7036. +
  7037. +static void au_do_hide(struct dentry *dentry)
  7038. +{
  7039. + struct inode *inode;
  7040. +
  7041. + if (d_really_is_positive(dentry)) {
  7042. + inode = d_inode(dentry);
  7043. + if (!d_is_dir(dentry)) {
  7044. + if (inode->i_nlink && !d_unhashed(dentry))
  7045. + drop_nlink(inode);
  7046. + } else {
  7047. + clear_nlink(inode);
  7048. + /* stop next lookup */
  7049. + inode->i_flags |= S_DEAD;
  7050. + }
  7051. + smp_mb(); /* necessary? */
  7052. + }
  7053. + d_drop(dentry);
  7054. +}
  7055. +
  7056. +static int au_hide_children(struct dentry *parent)
  7057. +{
  7058. + int err, i, j, ndentry;
  7059. + struct au_dcsub_pages dpages;
  7060. + struct au_dpage *dpage;
  7061. + struct dentry *dentry;
  7062. +
  7063. + err = au_dpages_init(&dpages, GFP_NOFS);
  7064. + if (unlikely(err))
  7065. + goto out;
  7066. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7067. + if (unlikely(err))
  7068. + goto out_dpages;
  7069. +
  7070. + /* in reverse order */
  7071. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7072. + dpage = dpages.dpages + i;
  7073. + ndentry = dpage->ndentry;
  7074. + for (j = ndentry - 1; j >= 0; j--) {
  7075. + dentry = dpage->dentries[j];
  7076. + if (dentry != parent)
  7077. + au_do_hide(dentry);
  7078. + }
  7079. + }
  7080. +
  7081. +out_dpages:
  7082. + au_dpages_free(&dpages);
  7083. +out:
  7084. + return err;
  7085. +}
  7086. +
  7087. +static void au_hide(struct dentry *dentry)
  7088. +{
  7089. + int err;
  7090. +
  7091. + AuDbgDentry(dentry);
  7092. + if (d_is_dir(dentry)) {
  7093. + /* shrink_dcache_parent(dentry); */
  7094. + err = au_hide_children(dentry);
  7095. + if (unlikely(err))
  7096. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7097. + dentry, err);
  7098. + }
  7099. + au_do_hide(dentry);
  7100. +}
  7101. +
  7102. +/*
  7103. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7104. + *
  7105. + * a dirty branch is added
  7106. + * - on the top of layers
  7107. + * - in the middle of layers
  7108. + * - to the bottom of layers
  7109. + *
  7110. + * on the added branch there exists
  7111. + * - a whiteout
  7112. + * - a diropq
  7113. + * - a same named entry
  7114. + * + exist
  7115. + * * negative --> positive
  7116. + * * positive --> positive
  7117. + * - type is unchanged
  7118. + * - type is changed
  7119. + * + doesn't exist
  7120. + * * negative --> negative
  7121. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7122. + * - none
  7123. + */
  7124. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7125. + struct au_dinfo *tmp)
  7126. +{
  7127. + int err;
  7128. + aufs_bindex_t bindex, bbot;
  7129. + struct {
  7130. + struct dentry *dentry;
  7131. + struct inode *inode;
  7132. + mode_t mode;
  7133. + } orig_h, tmp_h = {
  7134. + .dentry = NULL
  7135. + };
  7136. + struct au_hdentry *hd;
  7137. + struct inode *inode, *h_inode;
  7138. + struct dentry *h_dentry;
  7139. +
  7140. + err = 0;
  7141. + AuDebugOn(dinfo->di_btop < 0);
  7142. + orig_h.mode = 0;
  7143. + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
  7144. + orig_h.inode = NULL;
  7145. + if (d_is_positive(orig_h.dentry)) {
  7146. + orig_h.inode = d_inode(orig_h.dentry);
  7147. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7148. + }
  7149. + if (tmp->di_btop >= 0) {
  7150. + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
  7151. + if (d_is_positive(tmp_h.dentry)) {
  7152. + tmp_h.inode = d_inode(tmp_h.dentry);
  7153. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7154. + }
  7155. + }
  7156. +
  7157. + inode = NULL;
  7158. + if (d_really_is_positive(dentry))
  7159. + inode = d_inode(dentry);
  7160. + if (!orig_h.inode) {
  7161. + AuDbg("nagative originally\n");
  7162. + if (inode) {
  7163. + au_hide(dentry);
  7164. + goto out;
  7165. + }
  7166. + AuDebugOn(inode);
  7167. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7168. + AuDebugOn(dinfo->di_bdiropq != -1);
  7169. +
  7170. + if (!tmp_h.inode) {
  7171. + AuDbg("negative --> negative\n");
  7172. + /* should have only one negative lower */
  7173. + if (tmp->di_btop >= 0
  7174. + && tmp->di_btop < dinfo->di_btop) {
  7175. + AuDebugOn(tmp->di_btop != tmp->di_bbot);
  7176. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7177. + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
  7178. + au_di_cp(dinfo, tmp);
  7179. + hd = au_hdentry(tmp, tmp->di_btop);
  7180. + au_set_h_dptr(dentry, tmp->di_btop,
  7181. + dget(hd->hd_dentry));
  7182. + }
  7183. + au_dbg_verify_dinode(dentry);
  7184. + } else {
  7185. + AuDbg("negative --> positive\n");
  7186. + /*
  7187. + * similar to the behaviour of creating with bypassing
  7188. + * aufs.
  7189. + * unhash it in order to force an error in the
  7190. + * succeeding create operation.
  7191. + * we should not set S_DEAD here.
  7192. + */
  7193. + d_drop(dentry);
  7194. + /* au_di_swap(tmp, dinfo); */
  7195. + au_dbg_verify_dinode(dentry);
  7196. + }
  7197. + } else {
  7198. + AuDbg("positive originally\n");
  7199. + /* inode may be NULL */
  7200. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7201. + if (!tmp_h.inode) {
  7202. + AuDbg("positive --> negative\n");
  7203. + /* or bypassing aufs */
  7204. + au_hide(dentry);
  7205. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
  7206. + dinfo->di_bwh = tmp->di_bwh;
  7207. + if (inode)
  7208. + err = au_refresh_hinode_self(inode);
  7209. + au_dbg_verify_dinode(dentry);
  7210. + } else if (orig_h.mode == tmp_h.mode) {
  7211. + AuDbg("positive --> positive, same type\n");
  7212. + if (!S_ISDIR(orig_h.mode)
  7213. + && dinfo->di_btop > tmp->di_btop) {
  7214. + /*
  7215. + * similar to the behaviour of removing and
  7216. + * creating.
  7217. + */
  7218. + au_hide(dentry);
  7219. + if (inode)
  7220. + err = au_refresh_hinode_self(inode);
  7221. + au_dbg_verify_dinode(dentry);
  7222. + } else {
  7223. + /* fill empty slots */
  7224. + if (dinfo->di_btop > tmp->di_btop)
  7225. + dinfo->di_btop = tmp->di_btop;
  7226. + if (dinfo->di_bbot < tmp->di_bbot)
  7227. + dinfo->di_bbot = tmp->di_bbot;
  7228. + dinfo->di_bwh = tmp->di_bwh;
  7229. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7230. + bbot = dinfo->di_bbot;
  7231. + bindex = tmp->di_btop;
  7232. + hd = au_hdentry(tmp, bindex);
  7233. + for (; bindex <= bbot; bindex++, hd++) {
  7234. + if (au_h_dptr(dentry, bindex))
  7235. + continue;
  7236. + h_dentry = hd->hd_dentry;
  7237. + if (!h_dentry)
  7238. + continue;
  7239. + AuDebugOn(d_is_negative(h_dentry));
  7240. + h_inode = d_inode(h_dentry);
  7241. + AuDebugOn(orig_h.mode
  7242. + != (h_inode->i_mode
  7243. + & S_IFMT));
  7244. + au_set_h_dptr(dentry, bindex,
  7245. + dget(h_dentry));
  7246. + }
  7247. + if (inode)
  7248. + err = au_refresh_hinode(inode, dentry);
  7249. + au_dbg_verify_dinode(dentry);
  7250. + }
  7251. + } else {
  7252. + AuDbg("positive --> positive, different type\n");
  7253. + /* similar to the behaviour of removing and creating */
  7254. + au_hide(dentry);
  7255. + if (inode)
  7256. + err = au_refresh_hinode_self(inode);
  7257. + au_dbg_verify_dinode(dentry);
  7258. + }
  7259. + }
  7260. +
  7261. +out:
  7262. + return err;
  7263. +}
  7264. +
  7265. +void au_refresh_dop(struct dentry *dentry, int force_reval)
  7266. +{
  7267. + const struct dentry_operations *dop
  7268. + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
  7269. + static const unsigned int mask
  7270. + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
  7271. +
  7272. + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
  7273. +
  7274. + if (dentry->d_op == dop)
  7275. + return;
  7276. +
  7277. + AuDbg("%pd\n", dentry);
  7278. + spin_lock(&dentry->d_lock);
  7279. + if (dop == &aufs_dop)
  7280. + dentry->d_flags |= mask;
  7281. + else
  7282. + dentry->d_flags &= ~mask;
  7283. + dentry->d_op = dop;
  7284. + spin_unlock(&dentry->d_lock);
  7285. +}
  7286. +
  7287. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7288. +{
  7289. + int err, ebrange, nbr;
  7290. + unsigned int sigen;
  7291. + struct au_dinfo *dinfo, *tmp;
  7292. + struct super_block *sb;
  7293. + struct inode *inode;
  7294. +
  7295. + DiMustWriteLock(dentry);
  7296. + AuDebugOn(IS_ROOT(dentry));
  7297. + AuDebugOn(d_really_is_negative(parent));
  7298. +
  7299. + sb = dentry->d_sb;
  7300. + sigen = au_sigen(sb);
  7301. + err = au_digen_test(parent, sigen);
  7302. + if (unlikely(err))
  7303. + goto out;
  7304. +
  7305. + nbr = au_sbbot(sb) + 1;
  7306. + dinfo = au_di(dentry);
  7307. + err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
  7308. + if (unlikely(err))
  7309. + goto out;
  7310. + ebrange = au_dbrange_test(dentry);
  7311. + if (!ebrange)
  7312. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7313. +
  7314. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7315. + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
  7316. + if (d_really_is_positive(dentry)) {
  7317. + inode = d_inode(dentry);
  7318. + err = au_refresh_hinode_self(inode);
  7319. + }
  7320. + au_dbg_verify_dinode(dentry);
  7321. + if (!err)
  7322. + goto out_dgen; /* success */
  7323. + goto out;
  7324. + }
  7325. +
  7326. + /* temporary dinfo */
  7327. + AuDbgDentry(dentry);
  7328. + err = -ENOMEM;
  7329. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7330. + if (unlikely(!tmp))
  7331. + goto out;
  7332. + au_di_swap(tmp, dinfo);
  7333. + /* returns the number of positive dentries */
  7334. + /*
  7335. + * if current working dir is removed, it returns an error.
  7336. + * but the dentry is legal.
  7337. + */
  7338. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  7339. + AuDbgDentry(dentry);
  7340. + au_di_swap(tmp, dinfo);
  7341. + if (err == -ENOENT)
  7342. + err = 0;
  7343. + if (err >= 0) {
  7344. + /* compare/refresh by dinfo */
  7345. + AuDbgDentry(dentry);
  7346. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7347. + au_dbg_verify_dinode(dentry);
  7348. + AuTraceErr(err);
  7349. + }
  7350. + au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
  7351. + au_rw_write_unlock(&tmp->di_rwsem);
  7352. + au_di_free(tmp);
  7353. + if (unlikely(err))
  7354. + goto out;
  7355. +
  7356. +out_dgen:
  7357. + au_update_digen(dentry);
  7358. +out:
  7359. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7360. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7361. + AuDbgDentry(dentry);
  7362. + }
  7363. + AuTraceErr(err);
  7364. + return err;
  7365. +}
  7366. +
  7367. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7368. + struct dentry *dentry, aufs_bindex_t bindex)
  7369. +{
  7370. + int err, valid;
  7371. +
  7372. + err = 0;
  7373. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7374. + goto out;
  7375. +
  7376. + AuDbg("b%d\n", bindex);
  7377. + /*
  7378. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7379. + * due to whiteout and branch permission.
  7380. + */
  7381. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7382. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7383. + /* it may return tri-state */
  7384. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7385. +
  7386. + if (unlikely(valid < 0))
  7387. + err = valid;
  7388. + else if (!valid)
  7389. + err = -EINVAL;
  7390. +
  7391. +out:
  7392. + AuTraceErr(err);
  7393. + return err;
  7394. +}
  7395. +
  7396. +/* todo: remove this */
  7397. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7398. + unsigned int flags, int do_udba)
  7399. +{
  7400. + int err;
  7401. + umode_t mode, h_mode;
  7402. + aufs_bindex_t bindex, btail, btop, ibs, ibe;
  7403. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7404. + struct inode *h_inode, *h_cached_inode;
  7405. + struct dentry *h_dentry;
  7406. + struct qstr *name, *h_name;
  7407. +
  7408. + err = 0;
  7409. + plus = 0;
  7410. + mode = 0;
  7411. + ibs = -1;
  7412. + ibe = -1;
  7413. + unhashed = !!d_unhashed(dentry);
  7414. + is_root = !!IS_ROOT(dentry);
  7415. + name = &dentry->d_name;
  7416. + tmpfile = au_di(dentry)->di_tmpfile;
  7417. +
  7418. + /*
  7419. + * Theoretically, REVAL test should be unnecessary in case of
  7420. + * {FS,I}NOTIFY.
  7421. + * But {fs,i}notify doesn't fire some necessary events,
  7422. + * IN_ATTRIB for atime/nlink/pageio
  7423. + * Let's do REVAL test too.
  7424. + */
  7425. + if (do_udba && inode) {
  7426. + mode = (inode->i_mode & S_IFMT);
  7427. + plus = (inode->i_nlink > 0);
  7428. + ibs = au_ibtop(inode);
  7429. + ibe = au_ibbot(inode);
  7430. + }
  7431. +
  7432. + btop = au_dbtop(dentry);
  7433. + btail = btop;
  7434. + if (inode && S_ISDIR(inode->i_mode))
  7435. + btail = au_dbtaildir(dentry);
  7436. + for (bindex = btop; bindex <= btail; bindex++) {
  7437. + h_dentry = au_h_dptr(dentry, bindex);
  7438. + if (!h_dentry)
  7439. + continue;
  7440. +
  7441. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7442. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7443. + spin_lock(&h_dentry->d_lock);
  7444. + h_name = &h_dentry->d_name;
  7445. + if (unlikely(do_udba
  7446. + && !is_root
  7447. + && ((!h_nfs
  7448. + && (unhashed != !!d_unhashed(h_dentry)
  7449. + || (!tmpfile
  7450. + && !au_qstreq(name, h_name))
  7451. + ))
  7452. + || (h_nfs
  7453. + && !(flags & LOOKUP_OPEN)
  7454. + && (h_dentry->d_flags
  7455. + & DCACHE_NFSFS_RENAMED)))
  7456. + )) {
  7457. + int h_unhashed;
  7458. +
  7459. + h_unhashed = d_unhashed(h_dentry);
  7460. + spin_unlock(&h_dentry->d_lock);
  7461. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7462. + unhashed, h_unhashed, dentry, h_dentry);
  7463. + goto err;
  7464. + }
  7465. + spin_unlock(&h_dentry->d_lock);
  7466. +
  7467. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7468. + if (unlikely(err))
  7469. + /* do not goto err, to keep the errno */
  7470. + break;
  7471. +
  7472. + /* todo: plink too? */
  7473. + if (!do_udba)
  7474. + continue;
  7475. +
  7476. + /* UDBA tests */
  7477. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7478. + goto err;
  7479. +
  7480. + h_inode = NULL;
  7481. + if (d_is_positive(h_dentry))
  7482. + h_inode = d_inode(h_dentry);
  7483. + h_plus = plus;
  7484. + h_mode = mode;
  7485. + h_cached_inode = h_inode;
  7486. + if (h_inode) {
  7487. + h_mode = (h_inode->i_mode & S_IFMT);
  7488. + h_plus = (h_inode->i_nlink > 0);
  7489. + }
  7490. + if (inode && ibs <= bindex && bindex <= ibe)
  7491. + h_cached_inode = au_h_iptr(inode, bindex);
  7492. +
  7493. + if (!h_nfs) {
  7494. + if (unlikely(plus != h_plus && !tmpfile))
  7495. + goto err;
  7496. + } else {
  7497. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7498. + && !is_root
  7499. + && !IS_ROOT(h_dentry)
  7500. + && unhashed != d_unhashed(h_dentry)))
  7501. + goto err;
  7502. + }
  7503. + if (unlikely(mode != h_mode
  7504. + || h_cached_inode != h_inode))
  7505. + goto err;
  7506. + continue;
  7507. +
  7508. +err:
  7509. + err = -EINVAL;
  7510. + break;
  7511. + }
  7512. +
  7513. + AuTraceErr(err);
  7514. + return err;
  7515. +}
  7516. +
  7517. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  7518. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7519. +{
  7520. + int err;
  7521. + struct dentry *parent;
  7522. +
  7523. + if (!au_digen_test(dentry, sigen))
  7524. + return 0;
  7525. +
  7526. + parent = dget_parent(dentry);
  7527. + di_read_lock_parent(parent, AuLock_IR);
  7528. + AuDebugOn(au_digen_test(parent, sigen));
  7529. + au_dbg_verify_gen(parent, sigen);
  7530. + err = au_refresh_dentry(dentry, parent);
  7531. + di_read_unlock(parent, AuLock_IR);
  7532. + dput(parent);
  7533. + AuTraceErr(err);
  7534. + return err;
  7535. +}
  7536. +
  7537. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7538. +{
  7539. + int err;
  7540. + struct dentry *d, *parent;
  7541. +
  7542. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  7543. + return simple_reval_dpath(dentry, sigen);
  7544. +
  7545. + /* slow loop, keep it simple and stupid */
  7546. + /* cf: au_cpup_dirs() */
  7547. + err = 0;
  7548. + parent = NULL;
  7549. + while (au_digen_test(dentry, sigen)) {
  7550. + d = dentry;
  7551. + while (1) {
  7552. + dput(parent);
  7553. + parent = dget_parent(d);
  7554. + if (!au_digen_test(parent, sigen))
  7555. + break;
  7556. + d = parent;
  7557. + }
  7558. +
  7559. + if (d != dentry)
  7560. + di_write_lock_child2(d);
  7561. +
  7562. + /* someone might update our dentry while we were sleeping */
  7563. + if (au_digen_test(d, sigen)) {
  7564. + /*
  7565. + * todo: consolidate with simple_reval_dpath(),
  7566. + * do_refresh() and au_reval_for_attr().
  7567. + */
  7568. + di_read_lock_parent(parent, AuLock_IR);
  7569. + err = au_refresh_dentry(d, parent);
  7570. + di_read_unlock(parent, AuLock_IR);
  7571. + }
  7572. +
  7573. + if (d != dentry)
  7574. + di_write_unlock(d);
  7575. + dput(parent);
  7576. + if (unlikely(err))
  7577. + break;
  7578. + }
  7579. +
  7580. + return err;
  7581. +}
  7582. +
  7583. +/*
  7584. + * if valid returns 1, otherwise 0.
  7585. + */
  7586. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  7587. +{
  7588. + int valid, err;
  7589. + unsigned int sigen;
  7590. + unsigned char do_udba;
  7591. + struct super_block *sb;
  7592. + struct inode *inode;
  7593. +
  7594. + /* todo: support rcu-walk? */
  7595. + if (flags & LOOKUP_RCU)
  7596. + return -ECHILD;
  7597. +
  7598. + valid = 0;
  7599. + if (unlikely(!au_di(dentry)))
  7600. + goto out;
  7601. +
  7602. + valid = 1;
  7603. + sb = dentry->d_sb;
  7604. + /*
  7605. + * todo: very ugly
  7606. + * i_mutex of parent dir may be held,
  7607. + * but we should not return 'invalid' due to busy.
  7608. + */
  7609. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  7610. + if (unlikely(err)) {
  7611. + valid = err;
  7612. + AuTraceErr(err);
  7613. + goto out;
  7614. + }
  7615. + inode = NULL;
  7616. + if (d_really_is_positive(dentry))
  7617. + inode = d_inode(dentry);
  7618. + if (unlikely(inode && au_is_bad_inode(inode))) {
  7619. + err = -EINVAL;
  7620. + AuTraceErr(err);
  7621. + goto out_dgrade;
  7622. + }
  7623. + if (unlikely(au_dbrange_test(dentry))) {
  7624. + err = -EINVAL;
  7625. + AuTraceErr(err);
  7626. + goto out_dgrade;
  7627. + }
  7628. +
  7629. + sigen = au_sigen(sb);
  7630. + if (au_digen_test(dentry, sigen)) {
  7631. + AuDebugOn(IS_ROOT(dentry));
  7632. + err = au_reval_dpath(dentry, sigen);
  7633. + if (unlikely(err)) {
  7634. + AuTraceErr(err);
  7635. + goto out_dgrade;
  7636. + }
  7637. + }
  7638. + di_downgrade_lock(dentry, AuLock_IR);
  7639. +
  7640. + err = -EINVAL;
  7641. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  7642. + && inode
  7643. + && !(inode->i_state && I_LINKABLE)
  7644. + && (IS_DEADDIR(inode) || !inode->i_nlink)) {
  7645. + AuTraceErr(err);
  7646. + goto out_inval;
  7647. + }
  7648. +
  7649. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  7650. + if (do_udba && inode) {
  7651. + aufs_bindex_t btop = au_ibtop(inode);
  7652. + struct inode *h_inode;
  7653. +
  7654. + if (btop >= 0) {
  7655. + h_inode = au_h_iptr(inode, btop);
  7656. + if (h_inode && au_test_higen(inode, h_inode)) {
  7657. + AuTraceErr(err);
  7658. + goto out_inval;
  7659. + }
  7660. + }
  7661. + }
  7662. +
  7663. + err = h_d_revalidate(dentry, inode, flags, do_udba);
  7664. + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
  7665. + err = -EIO;
  7666. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  7667. + dentry, err);
  7668. + }
  7669. + goto out_inval;
  7670. +
  7671. +out_dgrade:
  7672. + di_downgrade_lock(dentry, AuLock_IR);
  7673. +out_inval:
  7674. + aufs_read_unlock(dentry, AuLock_IR);
  7675. + AuTraceErr(err);
  7676. + valid = !err;
  7677. +out:
  7678. + if (!valid) {
  7679. + AuDbg("%pd invalid, %d\n", dentry, valid);
  7680. + d_drop(dentry);
  7681. + }
  7682. + return valid;
  7683. +}
  7684. +
  7685. +static void aufs_d_release(struct dentry *dentry)
  7686. +{
  7687. + if (au_di(dentry)) {
  7688. + au_di_fin(dentry);
  7689. + au_hn_di_reinit(dentry);
  7690. + }
  7691. +}
  7692. +
  7693. +const struct dentry_operations aufs_dop = {
  7694. + .d_revalidate = aufs_d_revalidate,
  7695. + .d_weak_revalidate = aufs_d_revalidate,
  7696. + .d_release = aufs_d_release
  7697. +};
  7698. +
  7699. +/* aufs_dop without d_revalidate */
  7700. +const struct dentry_operations aufs_dop_noreval = {
  7701. + .d_release = aufs_d_release
  7702. +};
  7703. --- /dev/null
  7704. +++ linux-4.4/fs/aufs/dentry.h 2016-10-23 11:20:57.628826619 +0300
  7705. @@ -0,0 +1,255 @@
  7706. +/*
  7707. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  7708. + *
  7709. + * This program, aufs is free software; you can redistribute it and/or modify
  7710. + * it under the terms of the GNU General Public License as published by
  7711. + * the Free Software Foundation; either version 2 of the License, or
  7712. + * (at your option) any later version.
  7713. + *
  7714. + * This program is distributed in the hope that it will be useful,
  7715. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7716. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7717. + * GNU General Public License for more details.
  7718. + *
  7719. + * You should have received a copy of the GNU General Public License
  7720. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7721. + */
  7722. +
  7723. +/*
  7724. + * lookup and dentry operations
  7725. + */
  7726. +
  7727. +#ifndef __AUFS_DENTRY_H__
  7728. +#define __AUFS_DENTRY_H__
  7729. +
  7730. +#ifdef __KERNEL__
  7731. +
  7732. +#include <linux/dcache.h>
  7733. +#include "rwsem.h"
  7734. +
  7735. +struct au_hdentry {
  7736. + struct dentry *hd_dentry;
  7737. + aufs_bindex_t hd_id;
  7738. +};
  7739. +
  7740. +struct au_dinfo {
  7741. + atomic_t di_generation;
  7742. +
  7743. + struct au_rwsem di_rwsem;
  7744. + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
  7745. + unsigned char di_tmpfile; /* to allow the different name */
  7746. + union {
  7747. + struct au_hdentry *di_hdentry;
  7748. + struct llist_node di_lnode; /* delayed free */
  7749. + };
  7750. +} ____cacheline_aligned_in_smp;
  7751. +
  7752. +/* ---------------------------------------------------------------------- */
  7753. +
  7754. +/* flags for au_lkup_dentry() */
  7755. +#define AuLkup_ALLOW_NEG 1
  7756. +#define AuLkup_IGNORE_PERM (1 << 1)
  7757. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  7758. +#define au_fset_lkup(flags, name) \
  7759. + do { (flags) |= AuLkup_##name; } while (0)
  7760. +#define au_fclr_lkup(flags, name) \
  7761. + do { (flags) &= ~AuLkup_##name; } while (0)
  7762. +
  7763. +/* ---------------------------------------------------------------------- */
  7764. +
  7765. +/* dentry.c */
  7766. +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
  7767. +struct au_branch;
  7768. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  7769. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7770. + struct dentry *h_parent, struct au_branch *br);
  7771. +
  7772. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  7773. + unsigned int flags);
  7774. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  7775. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  7776. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  7777. +void au_refresh_dop(struct dentry *dentry, int force_reval);
  7778. +
  7779. +/* dinfo.c */
  7780. +void au_di_init_once(void *_di);
  7781. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  7782. +void au_di_free(struct au_dinfo *dinfo);
  7783. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  7784. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  7785. +int au_di_init(struct dentry *dentry);
  7786. +void au_di_fin(struct dentry *dentry);
  7787. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
  7788. +
  7789. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  7790. +void di_read_unlock(struct dentry *d, int flags);
  7791. +void di_downgrade_lock(struct dentry *d, int flags);
  7792. +void di_write_lock(struct dentry *d, unsigned int lsc);
  7793. +void di_write_unlock(struct dentry *d);
  7794. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  7795. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  7796. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  7797. +
  7798. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  7799. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  7800. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  7801. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  7802. +
  7803. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  7804. + struct dentry *h_dentry);
  7805. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  7806. +int au_dbrange_test(struct dentry *dentry);
  7807. +void au_update_digen(struct dentry *dentry);
  7808. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  7809. +void au_update_dbtop(struct dentry *dentry);
  7810. +void au_update_dbbot(struct dentry *dentry);
  7811. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  7812. +
  7813. +/* ---------------------------------------------------------------------- */
  7814. +
  7815. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  7816. +{
  7817. + return dentry->d_fsdata;
  7818. +}
  7819. +
  7820. +/* ---------------------------------------------------------------------- */
  7821. +
  7822. +/* lock subclass for dinfo */
  7823. +enum {
  7824. + AuLsc_DI_CHILD, /* child first */
  7825. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  7826. + AuLsc_DI_CHILD3, /* copyup dirs */
  7827. + AuLsc_DI_PARENT,
  7828. + AuLsc_DI_PARENT2,
  7829. + AuLsc_DI_PARENT3,
  7830. + AuLsc_DI_TMP /* temp for replacing dinfo */
  7831. +};
  7832. +
  7833. +/*
  7834. + * di_read_lock_child, di_write_lock_child,
  7835. + * di_read_lock_child2, di_write_lock_child2,
  7836. + * di_read_lock_child3, di_write_lock_child3,
  7837. + * di_read_lock_parent, di_write_lock_parent,
  7838. + * di_read_lock_parent2, di_write_lock_parent2,
  7839. + * di_read_lock_parent3, di_write_lock_parent3,
  7840. + */
  7841. +#define AuReadLockFunc(name, lsc) \
  7842. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  7843. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  7844. +
  7845. +#define AuWriteLockFunc(name, lsc) \
  7846. +static inline void di_write_lock_##name(struct dentry *d) \
  7847. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  7848. +
  7849. +#define AuRWLockFuncs(name, lsc) \
  7850. + AuReadLockFunc(name, lsc) \
  7851. + AuWriteLockFunc(name, lsc)
  7852. +
  7853. +AuRWLockFuncs(child, CHILD);
  7854. +AuRWLockFuncs(child2, CHILD2);
  7855. +AuRWLockFuncs(child3, CHILD3);
  7856. +AuRWLockFuncs(parent, PARENT);
  7857. +AuRWLockFuncs(parent2, PARENT2);
  7858. +AuRWLockFuncs(parent3, PARENT3);
  7859. +
  7860. +#undef AuReadLockFunc
  7861. +#undef AuWriteLockFunc
  7862. +#undef AuRWLockFuncs
  7863. +
  7864. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  7865. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  7866. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  7867. +
  7868. +/* ---------------------------------------------------------------------- */
  7869. +
  7870. +/* todo: memory barrier? */
  7871. +static inline unsigned int au_digen(struct dentry *d)
  7872. +{
  7873. + return atomic_read(&au_di(d)->di_generation);
  7874. +}
  7875. +
  7876. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  7877. +{
  7878. + hdentry->hd_dentry = NULL;
  7879. +}
  7880. +
  7881. +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
  7882. + aufs_bindex_t bindex)
  7883. +{
  7884. + return di->di_hdentry + bindex;
  7885. +}
  7886. +
  7887. +static inline void au_hdput(struct au_hdentry *hd)
  7888. +{
  7889. + if (hd)
  7890. + dput(hd->hd_dentry);
  7891. +}
  7892. +
  7893. +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
  7894. +{
  7895. + DiMustAnyLock(dentry);
  7896. + return au_di(dentry)->di_btop;
  7897. +}
  7898. +
  7899. +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
  7900. +{
  7901. + DiMustAnyLock(dentry);
  7902. + return au_di(dentry)->di_bbot;
  7903. +}
  7904. +
  7905. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  7906. +{
  7907. + DiMustAnyLock(dentry);
  7908. + return au_di(dentry)->di_bwh;
  7909. +}
  7910. +
  7911. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  7912. +{
  7913. + DiMustAnyLock(dentry);
  7914. + return au_di(dentry)->di_bdiropq;
  7915. +}
  7916. +
  7917. +/* todo: hard/soft set? */
  7918. +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
  7919. +{
  7920. + DiMustWriteLock(dentry);
  7921. + au_di(dentry)->di_btop = bindex;
  7922. +}
  7923. +
  7924. +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
  7925. +{
  7926. + DiMustWriteLock(dentry);
  7927. + au_di(dentry)->di_bbot = bindex;
  7928. +}
  7929. +
  7930. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  7931. +{
  7932. + DiMustWriteLock(dentry);
  7933. + /* dbwh can be outside of btop - bbot range */
  7934. + au_di(dentry)->di_bwh = bindex;
  7935. +}
  7936. +
  7937. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  7938. +{
  7939. + DiMustWriteLock(dentry);
  7940. + au_di(dentry)->di_bdiropq = bindex;
  7941. +}
  7942. +
  7943. +/* ---------------------------------------------------------------------- */
  7944. +
  7945. +#ifdef CONFIG_AUFS_HNOTIFY
  7946. +static inline void au_digen_dec(struct dentry *d)
  7947. +{
  7948. + atomic_dec(&au_di(d)->di_generation);
  7949. +}
  7950. +
  7951. +static inline void au_hn_di_reinit(struct dentry *dentry)
  7952. +{
  7953. + dentry->d_fsdata = NULL;
  7954. +}
  7955. +#else
  7956. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  7957. +#endif /* CONFIG_AUFS_HNOTIFY */
  7958. +
  7959. +#endif /* __KERNEL__ */
  7960. +#endif /* __AUFS_DENTRY_H__ */
  7961. --- /dev/null
  7962. +++ linux-4.4/fs/aufs/dinfo.c 2016-10-23 11:20:57.628826619 +0300
  7963. @@ -0,0 +1,553 @@
  7964. +/*
  7965. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  7966. + *
  7967. + * This program, aufs is free software; you can redistribute it and/or modify
  7968. + * it under the terms of the GNU General Public License as published by
  7969. + * the Free Software Foundation; either version 2 of the License, or
  7970. + * (at your option) any later version.
  7971. + *
  7972. + * This program is distributed in the hope that it will be useful,
  7973. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7974. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7975. + * GNU General Public License for more details.
  7976. + *
  7977. + * You should have received a copy of the GNU General Public License
  7978. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7979. + */
  7980. +
  7981. +/*
  7982. + * dentry private data
  7983. + */
  7984. +
  7985. +#include "aufs.h"
  7986. +
  7987. +void au_di_init_once(void *_dinfo)
  7988. +{
  7989. + struct au_dinfo *dinfo = _dinfo;
  7990. +
  7991. + au_rw_init(&dinfo->di_rwsem);
  7992. +}
  7993. +
  7994. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  7995. +{
  7996. + struct au_dinfo *dinfo;
  7997. + int nbr, i;
  7998. +
  7999. + dinfo = au_cache_alloc_dinfo();
  8000. + if (unlikely(!dinfo))
  8001. + goto out;
  8002. +
  8003. + nbr = au_sbbot(sb) + 1;
  8004. + if (nbr <= 0)
  8005. + nbr = 1;
  8006. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  8007. + if (dinfo->di_hdentry) {
  8008. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  8009. + dinfo->di_btop = -1;
  8010. + dinfo->di_bbot = -1;
  8011. + dinfo->di_bwh = -1;
  8012. + dinfo->di_bdiropq = -1;
  8013. + dinfo->di_tmpfile = 0;
  8014. + for (i = 0; i < nbr; i++)
  8015. + dinfo->di_hdentry[i].hd_id = -1;
  8016. + goto out;
  8017. + }
  8018. +
  8019. + au_cache_dfree_dinfo(dinfo);
  8020. + dinfo = NULL;
  8021. +
  8022. +out:
  8023. + return dinfo;
  8024. +}
  8025. +
  8026. +void au_di_free(struct au_dinfo *dinfo)
  8027. +{
  8028. + struct au_hdentry *p;
  8029. + aufs_bindex_t bbot, bindex;
  8030. +
  8031. + /* dentry may not be revalidated */
  8032. + bindex = dinfo->di_btop;
  8033. + if (bindex >= 0) {
  8034. + bbot = dinfo->di_bbot;
  8035. + p = au_hdentry(dinfo, bindex);
  8036. + while (bindex++ <= bbot)
  8037. + au_hdput(p++);
  8038. + }
  8039. + au_delayed_kfree(dinfo->di_hdentry);
  8040. + au_cache_dfree_dinfo(dinfo);
  8041. +}
  8042. +
  8043. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  8044. +{
  8045. + struct au_hdentry *p;
  8046. + aufs_bindex_t bi;
  8047. +
  8048. + AuRwMustWriteLock(&a->di_rwsem);
  8049. + AuRwMustWriteLock(&b->di_rwsem);
  8050. +
  8051. +#define DiSwap(v, name) \
  8052. + do { \
  8053. + v = a->di_##name; \
  8054. + a->di_##name = b->di_##name; \
  8055. + b->di_##name = v; \
  8056. + } while (0)
  8057. +
  8058. + DiSwap(p, hdentry);
  8059. + DiSwap(bi, btop);
  8060. + DiSwap(bi, bbot);
  8061. + DiSwap(bi, bwh);
  8062. + DiSwap(bi, bdiropq);
  8063. + /* smp_mb(); */
  8064. +
  8065. +#undef DiSwap
  8066. +}
  8067. +
  8068. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  8069. +{
  8070. + AuRwMustWriteLock(&dst->di_rwsem);
  8071. + AuRwMustWriteLock(&src->di_rwsem);
  8072. +
  8073. + dst->di_btop = src->di_btop;
  8074. + dst->di_bbot = src->di_bbot;
  8075. + dst->di_bwh = src->di_bwh;
  8076. + dst->di_bdiropq = src->di_bdiropq;
  8077. + /* smp_mb(); */
  8078. +}
  8079. +
  8080. +int au_di_init(struct dentry *dentry)
  8081. +{
  8082. + int err;
  8083. + struct super_block *sb;
  8084. + struct au_dinfo *dinfo;
  8085. +
  8086. + err = 0;
  8087. + sb = dentry->d_sb;
  8088. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  8089. + if (dinfo) {
  8090. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  8091. + /* smp_mb(); */ /* atomic_set */
  8092. + dentry->d_fsdata = dinfo;
  8093. + } else
  8094. + err = -ENOMEM;
  8095. +
  8096. + return err;
  8097. +}
  8098. +
  8099. +void au_di_fin(struct dentry *dentry)
  8100. +{
  8101. + struct au_dinfo *dinfo;
  8102. +
  8103. + dinfo = au_di(dentry);
  8104. + AuRwDestroy(&dinfo->di_rwsem);
  8105. + au_di_free(dinfo);
  8106. +}
  8107. +
  8108. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
  8109. +{
  8110. + int err, sz;
  8111. + struct au_hdentry *hdp;
  8112. +
  8113. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8114. +
  8115. + err = -ENOMEM;
  8116. + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
  8117. + if (!sz)
  8118. + sz = sizeof(*hdp);
  8119. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
  8120. + may_shrink);
  8121. + if (hdp) {
  8122. + dinfo->di_hdentry = hdp;
  8123. + err = 0;
  8124. + }
  8125. +
  8126. + return err;
  8127. +}
  8128. +
  8129. +/* ---------------------------------------------------------------------- */
  8130. +
  8131. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8132. +{
  8133. + switch (lsc) {
  8134. + case AuLsc_DI_CHILD:
  8135. + ii_write_lock_child(inode);
  8136. + break;
  8137. + case AuLsc_DI_CHILD2:
  8138. + ii_write_lock_child2(inode);
  8139. + break;
  8140. + case AuLsc_DI_CHILD3:
  8141. + ii_write_lock_child3(inode);
  8142. + break;
  8143. + case AuLsc_DI_PARENT:
  8144. + ii_write_lock_parent(inode);
  8145. + break;
  8146. + case AuLsc_DI_PARENT2:
  8147. + ii_write_lock_parent2(inode);
  8148. + break;
  8149. + case AuLsc_DI_PARENT3:
  8150. + ii_write_lock_parent3(inode);
  8151. + break;
  8152. + default:
  8153. + BUG();
  8154. + }
  8155. +}
  8156. +
  8157. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8158. +{
  8159. + switch (lsc) {
  8160. + case AuLsc_DI_CHILD:
  8161. + ii_read_lock_child(inode);
  8162. + break;
  8163. + case AuLsc_DI_CHILD2:
  8164. + ii_read_lock_child2(inode);
  8165. + break;
  8166. + case AuLsc_DI_CHILD3:
  8167. + ii_read_lock_child3(inode);
  8168. + break;
  8169. + case AuLsc_DI_PARENT:
  8170. + ii_read_lock_parent(inode);
  8171. + break;
  8172. + case AuLsc_DI_PARENT2:
  8173. + ii_read_lock_parent2(inode);
  8174. + break;
  8175. + case AuLsc_DI_PARENT3:
  8176. + ii_read_lock_parent3(inode);
  8177. + break;
  8178. + default:
  8179. + BUG();
  8180. + }
  8181. +}
  8182. +
  8183. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8184. +{
  8185. + struct inode *inode;
  8186. +
  8187. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8188. + if (d_really_is_positive(d)) {
  8189. + inode = d_inode(d);
  8190. + if (au_ftest_lock(flags, IW))
  8191. + do_ii_write_lock(inode, lsc);
  8192. + else if (au_ftest_lock(flags, IR))
  8193. + do_ii_read_lock(inode, lsc);
  8194. + }
  8195. +}
  8196. +
  8197. +void di_read_unlock(struct dentry *d, int flags)
  8198. +{
  8199. + struct inode *inode;
  8200. +
  8201. + if (d_really_is_positive(d)) {
  8202. + inode = d_inode(d);
  8203. + if (au_ftest_lock(flags, IW)) {
  8204. + au_dbg_verify_dinode(d);
  8205. + ii_write_unlock(inode);
  8206. + } else if (au_ftest_lock(flags, IR)) {
  8207. + au_dbg_verify_dinode(d);
  8208. + ii_read_unlock(inode);
  8209. + }
  8210. + }
  8211. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8212. +}
  8213. +
  8214. +void di_downgrade_lock(struct dentry *d, int flags)
  8215. +{
  8216. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8217. + ii_downgrade_lock(d_inode(d));
  8218. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8219. +}
  8220. +
  8221. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8222. +{
  8223. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8224. + if (d_really_is_positive(d))
  8225. + do_ii_write_lock(d_inode(d), lsc);
  8226. +}
  8227. +
  8228. +void di_write_unlock(struct dentry *d)
  8229. +{
  8230. + au_dbg_verify_dinode(d);
  8231. + if (d_really_is_positive(d))
  8232. + ii_write_unlock(d_inode(d));
  8233. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8234. +}
  8235. +
  8236. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8237. +{
  8238. + AuDebugOn(d1 == d2
  8239. + || d_inode(d1) == d_inode(d2)
  8240. + || d1->d_sb != d2->d_sb);
  8241. +
  8242. + if (isdir && au_test_subdir(d1, d2)) {
  8243. + di_write_lock_child(d1);
  8244. + di_write_lock_child2(d2);
  8245. + } else {
  8246. + /* there should be no races */
  8247. + di_write_lock_child(d2);
  8248. + di_write_lock_child2(d1);
  8249. + }
  8250. +}
  8251. +
  8252. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8253. +{
  8254. + AuDebugOn(d1 == d2
  8255. + || d_inode(d1) == d_inode(d2)
  8256. + || d1->d_sb != d2->d_sb);
  8257. +
  8258. + if (isdir && au_test_subdir(d1, d2)) {
  8259. + di_write_lock_parent(d1);
  8260. + di_write_lock_parent2(d2);
  8261. + } else {
  8262. + /* there should be no races */
  8263. + di_write_lock_parent(d2);
  8264. + di_write_lock_parent2(d1);
  8265. + }
  8266. +}
  8267. +
  8268. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8269. +{
  8270. + di_write_unlock(d1);
  8271. + if (d_inode(d1) == d_inode(d2))
  8272. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8273. + else
  8274. + di_write_unlock(d2);
  8275. +}
  8276. +
  8277. +/* ---------------------------------------------------------------------- */
  8278. +
  8279. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8280. +{
  8281. + struct dentry *d;
  8282. +
  8283. + DiMustAnyLock(dentry);
  8284. +
  8285. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  8286. + return NULL;
  8287. + AuDebugOn(bindex < 0);
  8288. + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
  8289. + AuDebugOn(d && au_dcount(d) <= 0);
  8290. + return d;
  8291. +}
  8292. +
  8293. +/*
  8294. + * extended version of au_h_dptr().
  8295. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8296. + * error.
  8297. + */
  8298. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8299. +{
  8300. + struct dentry *h_dentry;
  8301. + struct inode *inode, *h_inode;
  8302. +
  8303. + AuDebugOn(d_really_is_negative(dentry));
  8304. +
  8305. + h_dentry = NULL;
  8306. + if (au_dbtop(dentry) <= bindex
  8307. + && bindex <= au_dbbot(dentry))
  8308. + h_dentry = au_h_dptr(dentry, bindex);
  8309. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8310. + dget(h_dentry);
  8311. + goto out; /* success */
  8312. + }
  8313. +
  8314. + inode = d_inode(dentry);
  8315. + AuDebugOn(bindex < au_ibtop(inode));
  8316. + AuDebugOn(au_ibbot(inode) < bindex);
  8317. + h_inode = au_h_iptr(inode, bindex);
  8318. + h_dentry = d_find_alias(h_inode);
  8319. + if (h_dentry) {
  8320. + if (!IS_ERR(h_dentry)) {
  8321. + if (!au_d_linkable(h_dentry))
  8322. + goto out; /* success */
  8323. + dput(h_dentry);
  8324. + } else
  8325. + goto out;
  8326. + }
  8327. +
  8328. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8329. + h_dentry = au_plink_lkup(inode, bindex);
  8330. + AuDebugOn(!h_dentry);
  8331. + if (!IS_ERR(h_dentry)) {
  8332. + if (!au_d_hashed_positive(h_dentry))
  8333. + goto out; /* success */
  8334. + dput(h_dentry);
  8335. + h_dentry = NULL;
  8336. + }
  8337. + }
  8338. +
  8339. +out:
  8340. + AuDbgDentry(h_dentry);
  8341. + return h_dentry;
  8342. +}
  8343. +
  8344. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8345. +{
  8346. + aufs_bindex_t bbot, bwh;
  8347. +
  8348. + bbot = au_dbbot(dentry);
  8349. + if (0 <= bbot) {
  8350. + bwh = au_dbwh(dentry);
  8351. + if (!bwh)
  8352. + return bwh;
  8353. + if (0 < bwh && bwh < bbot)
  8354. + return bwh - 1;
  8355. + }
  8356. + return bbot;
  8357. +}
  8358. +
  8359. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8360. +{
  8361. + aufs_bindex_t bbot, bopq;
  8362. +
  8363. + bbot = au_dbtail(dentry);
  8364. + if (0 <= bbot) {
  8365. + bopq = au_dbdiropq(dentry);
  8366. + if (0 <= bopq && bopq < bbot)
  8367. + bbot = bopq;
  8368. + }
  8369. + return bbot;
  8370. +}
  8371. +
  8372. +/* ---------------------------------------------------------------------- */
  8373. +
  8374. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8375. + struct dentry *h_dentry)
  8376. +{
  8377. + struct au_dinfo *dinfo;
  8378. + struct au_hdentry *hd;
  8379. + struct au_branch *br;
  8380. +
  8381. + DiMustWriteLock(dentry);
  8382. +
  8383. + dinfo = au_di(dentry);
  8384. + hd = au_hdentry(dinfo, bindex);
  8385. + au_hdput(hd);
  8386. + hd->hd_dentry = h_dentry;
  8387. + if (h_dentry) {
  8388. + br = au_sbr(dentry->d_sb, bindex);
  8389. + hd->hd_id = br->br_id;
  8390. + }
  8391. +}
  8392. +
  8393. +int au_dbrange_test(struct dentry *dentry)
  8394. +{
  8395. + int err;
  8396. + aufs_bindex_t btop, bbot;
  8397. +
  8398. + err = 0;
  8399. + btop = au_dbtop(dentry);
  8400. + bbot = au_dbbot(dentry);
  8401. + if (btop >= 0)
  8402. + AuDebugOn(bbot < 0 && btop > bbot);
  8403. + else {
  8404. + err = -EIO;
  8405. + AuDebugOn(bbot >= 0);
  8406. + }
  8407. +
  8408. + return err;
  8409. +}
  8410. +
  8411. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8412. +{
  8413. + int err;
  8414. +
  8415. + err = 0;
  8416. + if (unlikely(au_digen(dentry) != sigen
  8417. + || au_iigen_test(d_inode(dentry), sigen)))
  8418. + err = -EIO;
  8419. +
  8420. + return err;
  8421. +}
  8422. +
  8423. +void au_update_digen(struct dentry *dentry)
  8424. +{
  8425. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8426. + /* smp_mb(); */ /* atomic_set */
  8427. +}
  8428. +
  8429. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8430. +{
  8431. + struct au_dinfo *dinfo;
  8432. + struct dentry *h_d;
  8433. + struct au_hdentry *hdp;
  8434. + aufs_bindex_t bindex, bbot;
  8435. +
  8436. + DiMustWriteLock(dentry);
  8437. +
  8438. + dinfo = au_di(dentry);
  8439. + if (!dinfo || dinfo->di_btop < 0)
  8440. + return;
  8441. +
  8442. + if (do_put_zero) {
  8443. + bbot = dinfo->di_bbot;
  8444. + bindex = dinfo->di_btop;
  8445. + hdp = au_hdentry(dinfo, bindex);
  8446. + for (; bindex <= bbot; bindex++, hdp++) {
  8447. + h_d = hdp->hd_dentry;
  8448. + if (h_d && d_is_negative(h_d))
  8449. + au_set_h_dptr(dentry, bindex, NULL);
  8450. + }
  8451. + }
  8452. +
  8453. + dinfo->di_btop = 0;
  8454. + hdp = au_hdentry(dinfo, dinfo->di_btop);
  8455. + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
  8456. + if (hdp->hd_dentry)
  8457. + break;
  8458. + if (dinfo->di_btop > dinfo->di_bbot) {
  8459. + dinfo->di_btop = -1;
  8460. + dinfo->di_bbot = -1;
  8461. + return;
  8462. + }
  8463. +
  8464. + hdp = au_hdentry(dinfo, dinfo->di_bbot);
  8465. + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
  8466. + if (hdp->hd_dentry)
  8467. + break;
  8468. + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
  8469. +}
  8470. +
  8471. +void au_update_dbtop(struct dentry *dentry)
  8472. +{
  8473. + aufs_bindex_t bindex, bbot;
  8474. + struct dentry *h_dentry;
  8475. +
  8476. + bbot = au_dbbot(dentry);
  8477. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  8478. + h_dentry = au_h_dptr(dentry, bindex);
  8479. + if (!h_dentry)
  8480. + continue;
  8481. + if (d_is_positive(h_dentry)) {
  8482. + au_set_dbtop(dentry, bindex);
  8483. + return;
  8484. + }
  8485. + au_set_h_dptr(dentry, bindex, NULL);
  8486. + }
  8487. +}
  8488. +
  8489. +void au_update_dbbot(struct dentry *dentry)
  8490. +{
  8491. + aufs_bindex_t bindex, btop;
  8492. + struct dentry *h_dentry;
  8493. +
  8494. + btop = au_dbtop(dentry);
  8495. + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
  8496. + h_dentry = au_h_dptr(dentry, bindex);
  8497. + if (!h_dentry)
  8498. + continue;
  8499. + if (d_is_positive(h_dentry)) {
  8500. + au_set_dbbot(dentry, bindex);
  8501. + return;
  8502. + }
  8503. + au_set_h_dptr(dentry, bindex, NULL);
  8504. + }
  8505. +}
  8506. +
  8507. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  8508. +{
  8509. + aufs_bindex_t bindex, bbot;
  8510. +
  8511. + bbot = au_dbbot(dentry);
  8512. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
  8513. + if (au_h_dptr(dentry, bindex) == h_dentry)
  8514. + return bindex;
  8515. + return -1;
  8516. +}
  8517. --- /dev/null
  8518. +++ linux-4.4/fs/aufs/dir.c 2016-10-23 11:20:57.632159952 +0300
  8519. @@ -0,0 +1,764 @@
  8520. +/*
  8521. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  8522. + *
  8523. + * This program, aufs is free software; you can redistribute it and/or modify
  8524. + * it under the terms of the GNU General Public License as published by
  8525. + * the Free Software Foundation; either version 2 of the License, or
  8526. + * (at your option) any later version.
  8527. + *
  8528. + * This program is distributed in the hope that it will be useful,
  8529. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8530. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8531. + * GNU General Public License for more details.
  8532. + *
  8533. + * You should have received a copy of the GNU General Public License
  8534. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8535. + */
  8536. +
  8537. +/*
  8538. + * directory operations
  8539. + */
  8540. +
  8541. +#include <linux/fs_stack.h>
  8542. +#include "aufs.h"
  8543. +
  8544. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  8545. +{
  8546. + unsigned int nlink;
  8547. +
  8548. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8549. +
  8550. + nlink = dir->i_nlink;
  8551. + nlink += h_dir->i_nlink - 2;
  8552. + if (h_dir->i_nlink < 2)
  8553. + nlink += 2;
  8554. + smp_mb(); /* for i_nlink */
  8555. + /* 0 can happen in revaliding */
  8556. + set_nlink(dir, nlink);
  8557. +}
  8558. +
  8559. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  8560. +{
  8561. + unsigned int nlink;
  8562. +
  8563. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8564. +
  8565. + nlink = dir->i_nlink;
  8566. + nlink -= h_dir->i_nlink - 2;
  8567. + if (h_dir->i_nlink < 2)
  8568. + nlink -= 2;
  8569. + smp_mb(); /* for i_nlink */
  8570. + /* nlink == 0 means the branch-fs is broken */
  8571. + set_nlink(dir, nlink);
  8572. +}
  8573. +
  8574. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  8575. +{
  8576. + loff_t sz;
  8577. + aufs_bindex_t bindex, bbot;
  8578. + struct file *h_file;
  8579. + struct dentry *h_dentry;
  8580. +
  8581. + sz = 0;
  8582. + if (file) {
  8583. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  8584. +
  8585. + bbot = au_fbbot_dir(file);
  8586. + for (bindex = au_fbtop(file);
  8587. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  8588. + bindex++) {
  8589. + h_file = au_hf_dir(file, bindex);
  8590. + if (h_file && file_inode(h_file))
  8591. + sz += vfsub_f_size_read(h_file);
  8592. + }
  8593. + } else {
  8594. + AuDebugOn(!dentry);
  8595. + AuDebugOn(!d_is_dir(dentry));
  8596. +
  8597. + bbot = au_dbtaildir(dentry);
  8598. + for (bindex = au_dbtop(dentry);
  8599. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  8600. + bindex++) {
  8601. + h_dentry = au_h_dptr(dentry, bindex);
  8602. + if (h_dentry && d_is_positive(h_dentry))
  8603. + sz += i_size_read(d_inode(h_dentry));
  8604. + }
  8605. + }
  8606. + if (sz < KMALLOC_MAX_SIZE)
  8607. + sz = roundup_pow_of_two(sz);
  8608. + if (sz > KMALLOC_MAX_SIZE)
  8609. + sz = KMALLOC_MAX_SIZE;
  8610. + else if (sz < NAME_MAX) {
  8611. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  8612. + sz = AUFS_RDBLK_DEF;
  8613. + }
  8614. + return sz;
  8615. +}
  8616. +
  8617. +struct au_dir_ts_arg {
  8618. + struct dentry *dentry;
  8619. + aufs_bindex_t brid;
  8620. +};
  8621. +
  8622. +static void au_do_dir_ts(void *arg)
  8623. +{
  8624. + struct au_dir_ts_arg *a = arg;
  8625. + struct au_dtime dt;
  8626. + struct path h_path;
  8627. + struct inode *dir, *h_dir;
  8628. + struct super_block *sb;
  8629. + struct au_branch *br;
  8630. + struct au_hinode *hdir;
  8631. + int err;
  8632. + aufs_bindex_t btop, bindex;
  8633. +
  8634. + sb = a->dentry->d_sb;
  8635. + if (d_really_is_negative(a->dentry))
  8636. + goto out;
  8637. + /* no dir->i_mutex lock */
  8638. + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
  8639. +
  8640. + dir = d_inode(a->dentry);
  8641. + btop = au_ibtop(dir);
  8642. + bindex = au_br_index(sb, a->brid);
  8643. + if (bindex < btop)
  8644. + goto out_unlock;
  8645. +
  8646. + br = au_sbr(sb, bindex);
  8647. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  8648. + if (!h_path.dentry)
  8649. + goto out_unlock;
  8650. + h_path.mnt = au_br_mnt(br);
  8651. + au_dtime_store(&dt, a->dentry, &h_path);
  8652. +
  8653. + br = au_sbr(sb, btop);
  8654. + if (!au_br_writable(br->br_perm))
  8655. + goto out_unlock;
  8656. + h_path.dentry = au_h_dptr(a->dentry, btop);
  8657. + h_path.mnt = au_br_mnt(br);
  8658. + err = vfsub_mnt_want_write(h_path.mnt);
  8659. + if (err)
  8660. + goto out_unlock;
  8661. + hdir = au_hi(dir, btop);
  8662. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  8663. + h_dir = au_h_iptr(dir, btop);
  8664. + if (h_dir->i_nlink
  8665. + && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  8666. + dt.dt_h_path = h_path;
  8667. + au_dtime_revert(&dt);
  8668. + }
  8669. + au_hn_imtx_unlock(hdir);
  8670. + vfsub_mnt_drop_write(h_path.mnt);
  8671. + au_cpup_attr_timesizes(dir);
  8672. +
  8673. +out_unlock:
  8674. + aufs_read_unlock(a->dentry, AuLock_DW);
  8675. +out:
  8676. + dput(a->dentry);
  8677. + au_nwt_done(&au_sbi(sb)->si_nowait);
  8678. + au_delayed_kfree(arg);
  8679. +}
  8680. +
  8681. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  8682. +{
  8683. + int perm, wkq_err;
  8684. + aufs_bindex_t btop;
  8685. + struct au_dir_ts_arg *arg;
  8686. + struct dentry *dentry;
  8687. + struct super_block *sb;
  8688. +
  8689. + IMustLock(dir);
  8690. +
  8691. + dentry = d_find_any_alias(dir);
  8692. + AuDebugOn(!dentry);
  8693. + sb = dentry->d_sb;
  8694. + btop = au_ibtop(dir);
  8695. + if (btop == bindex) {
  8696. + au_cpup_attr_timesizes(dir);
  8697. + goto out;
  8698. + }
  8699. +
  8700. + perm = au_sbr_perm(sb, btop);
  8701. + if (!au_br_writable(perm))
  8702. + goto out;
  8703. +
  8704. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  8705. + if (!arg)
  8706. + goto out;
  8707. +
  8708. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  8709. + arg->brid = au_sbr_id(sb, bindex);
  8710. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  8711. + if (unlikely(wkq_err)) {
  8712. + pr_err("wkq %d\n", wkq_err);
  8713. + dput(dentry);
  8714. + au_delayed_kfree(arg);
  8715. + }
  8716. +
  8717. +out:
  8718. + dput(dentry);
  8719. +}
  8720. +
  8721. +/* ---------------------------------------------------------------------- */
  8722. +
  8723. +static int reopen_dir(struct file *file)
  8724. +{
  8725. + int err;
  8726. + unsigned int flags;
  8727. + aufs_bindex_t bindex, btail, btop;
  8728. + struct dentry *dentry, *h_dentry;
  8729. + struct file *h_file;
  8730. +
  8731. + /* open all lower dirs */
  8732. + dentry = file->f_path.dentry;
  8733. + btop = au_dbtop(dentry);
  8734. + for (bindex = au_fbtop(file); bindex < btop; bindex++)
  8735. + au_set_h_fptr(file, bindex, NULL);
  8736. + au_set_fbtop(file, btop);
  8737. +
  8738. + btail = au_dbtaildir(dentry);
  8739. + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
  8740. + au_set_h_fptr(file, bindex, NULL);
  8741. + au_set_fbbot_dir(file, btail);
  8742. +
  8743. + flags = vfsub_file_flags(file);
  8744. + for (bindex = btop; bindex <= btail; bindex++) {
  8745. + h_dentry = au_h_dptr(dentry, bindex);
  8746. + if (!h_dentry)
  8747. + continue;
  8748. + h_file = au_hf_dir(file, bindex);
  8749. + if (h_file)
  8750. + continue;
  8751. +
  8752. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8753. + err = PTR_ERR(h_file);
  8754. + if (IS_ERR(h_file))
  8755. + goto out; /* close all? */
  8756. + au_set_h_fptr(file, bindex, h_file);
  8757. + }
  8758. + au_update_figen(file);
  8759. + /* todo: necessary? */
  8760. + /* file->f_ra = h_file->f_ra; */
  8761. + err = 0;
  8762. +
  8763. +out:
  8764. + return err;
  8765. +}
  8766. +
  8767. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  8768. +{
  8769. + int err;
  8770. + aufs_bindex_t bindex, btail;
  8771. + struct dentry *dentry, *h_dentry;
  8772. + struct vfsmount *mnt;
  8773. +
  8774. + FiMustWriteLock(file);
  8775. + AuDebugOn(h_file);
  8776. +
  8777. + err = 0;
  8778. + mnt = file->f_path.mnt;
  8779. + dentry = file->f_path.dentry;
  8780. + file->f_version = d_inode(dentry)->i_version;
  8781. + bindex = au_dbtop(dentry);
  8782. + au_set_fbtop(file, bindex);
  8783. + btail = au_dbtaildir(dentry);
  8784. + au_set_fbbot_dir(file, btail);
  8785. + for (; !err && bindex <= btail; bindex++) {
  8786. + h_dentry = au_h_dptr(dentry, bindex);
  8787. + if (!h_dentry)
  8788. + continue;
  8789. +
  8790. + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
  8791. + if (unlikely(err))
  8792. + break;
  8793. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8794. + if (IS_ERR(h_file)) {
  8795. + err = PTR_ERR(h_file);
  8796. + break;
  8797. + }
  8798. + au_set_h_fptr(file, bindex, h_file);
  8799. + }
  8800. + au_update_figen(file);
  8801. + /* todo: necessary? */
  8802. + /* file->f_ra = h_file->f_ra; */
  8803. + if (!err)
  8804. + return 0; /* success */
  8805. +
  8806. + /* close all */
  8807. + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
  8808. + au_set_h_fptr(file, bindex, NULL);
  8809. + au_set_fbtop(file, -1);
  8810. + au_set_fbbot_dir(file, -1);
  8811. +
  8812. + return err;
  8813. +}
  8814. +
  8815. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  8816. + struct file *file)
  8817. +{
  8818. + int err;
  8819. + struct super_block *sb;
  8820. + struct au_fidir *fidir;
  8821. +
  8822. + err = -ENOMEM;
  8823. + sb = file->f_path.dentry->d_sb;
  8824. + si_read_lock(sb, AuLock_FLUSH);
  8825. + fidir = au_fidir_alloc(sb);
  8826. + if (fidir) {
  8827. + struct au_do_open_args args = {
  8828. + .open = do_open_dir,
  8829. + .fidir = fidir
  8830. + };
  8831. + err = au_do_open(file, &args);
  8832. + if (unlikely(err))
  8833. + au_delayed_kfree(fidir);
  8834. + }
  8835. + si_read_unlock(sb);
  8836. + return err;
  8837. +}
  8838. +
  8839. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  8840. + struct file *file)
  8841. +{
  8842. + struct au_vdir *vdir_cache;
  8843. + struct au_finfo *finfo;
  8844. + struct au_fidir *fidir;
  8845. + struct au_hfile *hf;
  8846. + aufs_bindex_t bindex, bbot;
  8847. + int execed, delayed;
  8848. +
  8849. + delayed = (current->flags & PF_KTHREAD) || in_interrupt();
  8850. + finfo = au_fi(file);
  8851. + fidir = finfo->fi_hdir;
  8852. + if (fidir) {
  8853. + au_sphl_del(&finfo->fi_hlist,
  8854. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  8855. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  8856. + if (vdir_cache)
  8857. + au_vdir_free(vdir_cache, delayed);
  8858. +
  8859. + bindex = finfo->fi_btop;
  8860. + if (bindex >= 0) {
  8861. + execed = vfsub_file_execed(file);
  8862. + hf = fidir->fd_hfile + bindex;
  8863. + /*
  8864. + * calls fput() instead of filp_close(),
  8865. + * since no dnotify or lock for the lower file.
  8866. + */
  8867. + bbot = fidir->fd_bbot;
  8868. + for (; bindex <= bbot; bindex++, hf++)
  8869. + if (hf->hf_file)
  8870. + au_hfput(hf, execed);
  8871. + }
  8872. + au_delayed_kfree(fidir);
  8873. + finfo->fi_hdir = NULL;
  8874. + }
  8875. + au_finfo_fin(file, delayed);
  8876. + return 0;
  8877. +}
  8878. +
  8879. +/* ---------------------------------------------------------------------- */
  8880. +
  8881. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  8882. +{
  8883. + int err;
  8884. + aufs_bindex_t bindex, bbot;
  8885. + struct file *h_file;
  8886. +
  8887. + err = 0;
  8888. + bbot = au_fbbot_dir(file);
  8889. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  8890. + h_file = au_hf_dir(file, bindex);
  8891. + if (h_file)
  8892. + err = vfsub_flush(h_file, id);
  8893. + }
  8894. + return err;
  8895. +}
  8896. +
  8897. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  8898. +{
  8899. + return au_do_flush(file, id, au_do_flush_dir);
  8900. +}
  8901. +
  8902. +/* ---------------------------------------------------------------------- */
  8903. +
  8904. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  8905. +{
  8906. + int err;
  8907. + aufs_bindex_t bbot, bindex;
  8908. + struct inode *inode;
  8909. + struct super_block *sb;
  8910. +
  8911. + err = 0;
  8912. + sb = dentry->d_sb;
  8913. + inode = d_inode(dentry);
  8914. + IMustLock(inode);
  8915. + bbot = au_dbbot(dentry);
  8916. + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
  8917. + struct path h_path;
  8918. +
  8919. + if (au_test_ro(sb, bindex, inode))
  8920. + continue;
  8921. + h_path.dentry = au_h_dptr(dentry, bindex);
  8922. + if (!h_path.dentry)
  8923. + continue;
  8924. +
  8925. + h_path.mnt = au_sbr_mnt(sb, bindex);
  8926. + err = vfsub_fsync(NULL, &h_path, datasync);
  8927. + }
  8928. +
  8929. + return err;
  8930. +}
  8931. +
  8932. +static int au_do_fsync_dir(struct file *file, int datasync)
  8933. +{
  8934. + int err;
  8935. + aufs_bindex_t bbot, bindex;
  8936. + struct file *h_file;
  8937. + struct super_block *sb;
  8938. + struct inode *inode;
  8939. +
  8940. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  8941. + if (unlikely(err))
  8942. + goto out;
  8943. +
  8944. + inode = file_inode(file);
  8945. + sb = inode->i_sb;
  8946. + bbot = au_fbbot_dir(file);
  8947. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  8948. + h_file = au_hf_dir(file, bindex);
  8949. + if (!h_file || au_test_ro(sb, bindex, inode))
  8950. + continue;
  8951. +
  8952. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  8953. + }
  8954. +
  8955. +out:
  8956. + return err;
  8957. +}
  8958. +
  8959. +/*
  8960. + * @file may be NULL
  8961. + */
  8962. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  8963. + int datasync)
  8964. +{
  8965. + int err;
  8966. + struct dentry *dentry;
  8967. + struct inode *inode;
  8968. + struct super_block *sb;
  8969. + struct mutex *mtx;
  8970. +
  8971. + err = 0;
  8972. + dentry = file->f_path.dentry;
  8973. + inode = d_inode(dentry);
  8974. + mtx = &inode->i_mutex;
  8975. + mutex_lock(mtx);
  8976. + sb = dentry->d_sb;
  8977. + si_noflush_read_lock(sb);
  8978. + if (file)
  8979. + err = au_do_fsync_dir(file, datasync);
  8980. + else {
  8981. + di_write_lock_child(dentry);
  8982. + err = au_do_fsync_dir_no_file(dentry, datasync);
  8983. + }
  8984. + au_cpup_attr_timesizes(inode);
  8985. + di_write_unlock(dentry);
  8986. + if (file)
  8987. + fi_write_unlock(file);
  8988. +
  8989. + si_read_unlock(sb);
  8990. + mutex_unlock(mtx);
  8991. + return err;
  8992. +}
  8993. +
  8994. +/* ---------------------------------------------------------------------- */
  8995. +
  8996. +static int aufs_iterate(struct file *file, struct dir_context *ctx)
  8997. +{
  8998. + int err;
  8999. + struct dentry *dentry;
  9000. + struct inode *inode, *h_inode;
  9001. + struct super_block *sb;
  9002. +
  9003. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  9004. +
  9005. + dentry = file->f_path.dentry;
  9006. + inode = d_inode(dentry);
  9007. + IMustLock(inode);
  9008. +
  9009. + sb = dentry->d_sb;
  9010. + si_read_lock(sb, AuLock_FLUSH);
  9011. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  9012. + if (unlikely(err))
  9013. + goto out;
  9014. + err = au_alive_dir(dentry);
  9015. + if (!err)
  9016. + err = au_vdir_init(file);
  9017. + di_downgrade_lock(dentry, AuLock_IR);
  9018. + if (unlikely(err))
  9019. + goto out_unlock;
  9020. +
  9021. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  9022. + if (!au_test_nfsd()) {
  9023. + err = au_vdir_fill_de(file, ctx);
  9024. + fsstack_copy_attr_atime(inode, h_inode);
  9025. + } else {
  9026. + /*
  9027. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  9028. + * encode_fh() and others.
  9029. + */
  9030. + atomic_inc(&h_inode->i_count);
  9031. + di_read_unlock(dentry, AuLock_IR);
  9032. + si_read_unlock(sb);
  9033. + err = au_vdir_fill_de(file, ctx);
  9034. + fsstack_copy_attr_atime(inode, h_inode);
  9035. + fi_write_unlock(file);
  9036. + iput(h_inode);
  9037. +
  9038. + AuTraceErr(err);
  9039. + return err;
  9040. + }
  9041. +
  9042. +out_unlock:
  9043. + di_read_unlock(dentry, AuLock_IR);
  9044. + fi_write_unlock(file);
  9045. +out:
  9046. + si_read_unlock(sb);
  9047. + return err;
  9048. +}
  9049. +
  9050. +/* ---------------------------------------------------------------------- */
  9051. +
  9052. +#define AuTestEmpty_WHONLY 1
  9053. +#define AuTestEmpty_CALLED (1 << 1)
  9054. +#define AuTestEmpty_SHWH (1 << 2)
  9055. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  9056. +#define au_fset_testempty(flags, name) \
  9057. + do { (flags) |= AuTestEmpty_##name; } while (0)
  9058. +#define au_fclr_testempty(flags, name) \
  9059. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  9060. +
  9061. +#ifndef CONFIG_AUFS_SHWH
  9062. +#undef AuTestEmpty_SHWH
  9063. +#define AuTestEmpty_SHWH 0
  9064. +#endif
  9065. +
  9066. +struct test_empty_arg {
  9067. + struct dir_context ctx;
  9068. + struct au_nhash *whlist;
  9069. + unsigned int flags;
  9070. + int err;
  9071. + aufs_bindex_t bindex;
  9072. +};
  9073. +
  9074. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  9075. + int namelen, loff_t offset __maybe_unused, u64 ino,
  9076. + unsigned int d_type)
  9077. +{
  9078. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  9079. + ctx);
  9080. + char *name = (void *)__name;
  9081. +
  9082. + arg->err = 0;
  9083. + au_fset_testempty(arg->flags, CALLED);
  9084. + /* smp_mb(); */
  9085. + if (name[0] == '.'
  9086. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  9087. + goto out; /* success */
  9088. +
  9089. + if (namelen <= AUFS_WH_PFX_LEN
  9090. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  9091. + if (au_ftest_testempty(arg->flags, WHONLY)
  9092. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  9093. + arg->err = -ENOTEMPTY;
  9094. + goto out;
  9095. + }
  9096. +
  9097. + name += AUFS_WH_PFX_LEN;
  9098. + namelen -= AUFS_WH_PFX_LEN;
  9099. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  9100. + arg->err = au_nhash_append_wh
  9101. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  9102. + au_ftest_testempty(arg->flags, SHWH));
  9103. +
  9104. +out:
  9105. + /* smp_mb(); */
  9106. + AuTraceErr(arg->err);
  9107. + return arg->err;
  9108. +}
  9109. +
  9110. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9111. +{
  9112. + int err;
  9113. + struct file *h_file;
  9114. +
  9115. + h_file = au_h_open(dentry, arg->bindex,
  9116. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9117. + /*file*/NULL, /*force_wr*/0);
  9118. + err = PTR_ERR(h_file);
  9119. + if (IS_ERR(h_file))
  9120. + goto out;
  9121. +
  9122. + err = 0;
  9123. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9124. + && !file_inode(h_file)->i_nlink)
  9125. + goto out_put;
  9126. +
  9127. + do {
  9128. + arg->err = 0;
  9129. + au_fclr_testempty(arg->flags, CALLED);
  9130. + /* smp_mb(); */
  9131. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9132. + if (err >= 0)
  9133. + err = arg->err;
  9134. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9135. +
  9136. +out_put:
  9137. + fput(h_file);
  9138. + au_sbr_put(dentry->d_sb, arg->bindex);
  9139. +out:
  9140. + return err;
  9141. +}
  9142. +
  9143. +struct do_test_empty_args {
  9144. + int *errp;
  9145. + struct dentry *dentry;
  9146. + struct test_empty_arg *arg;
  9147. +};
  9148. +
  9149. +static void call_do_test_empty(void *args)
  9150. +{
  9151. + struct do_test_empty_args *a = args;
  9152. + *a->errp = do_test_empty(a->dentry, a->arg);
  9153. +}
  9154. +
  9155. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9156. +{
  9157. + int err, wkq_err;
  9158. + struct dentry *h_dentry;
  9159. + struct inode *h_inode;
  9160. +
  9161. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9162. + h_inode = d_inode(h_dentry);
  9163. + /* todo: i_mode changes anytime? */
  9164. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  9165. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  9166. + mutex_unlock(&h_inode->i_mutex);
  9167. + if (!err)
  9168. + err = do_test_empty(dentry, arg);
  9169. + else {
  9170. + struct do_test_empty_args args = {
  9171. + .errp = &err,
  9172. + .dentry = dentry,
  9173. + .arg = arg
  9174. + };
  9175. + unsigned int flags = arg->flags;
  9176. +
  9177. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9178. + if (unlikely(wkq_err))
  9179. + err = wkq_err;
  9180. + arg->flags = flags;
  9181. + }
  9182. +
  9183. + return err;
  9184. +}
  9185. +
  9186. +int au_test_empty_lower(struct dentry *dentry)
  9187. +{
  9188. + int err;
  9189. + unsigned int rdhash;
  9190. + aufs_bindex_t bindex, btop, btail;
  9191. + struct au_nhash whlist;
  9192. + struct test_empty_arg arg = {
  9193. + .ctx = {
  9194. + .actor = test_empty_cb
  9195. + }
  9196. + };
  9197. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9198. +
  9199. + SiMustAnyLock(dentry->d_sb);
  9200. +
  9201. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9202. + if (!rdhash)
  9203. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9204. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9205. + if (unlikely(err))
  9206. + goto out;
  9207. +
  9208. + arg.flags = 0;
  9209. + arg.whlist = &whlist;
  9210. + btop = au_dbtop(dentry);
  9211. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9212. + au_fset_testempty(arg.flags, SHWH);
  9213. + test_empty = do_test_empty;
  9214. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9215. + test_empty = sio_test_empty;
  9216. + arg.bindex = btop;
  9217. + err = test_empty(dentry, &arg);
  9218. + if (unlikely(err))
  9219. + goto out_whlist;
  9220. +
  9221. + au_fset_testempty(arg.flags, WHONLY);
  9222. + btail = au_dbtaildir(dentry);
  9223. + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
  9224. + struct dentry *h_dentry;
  9225. +
  9226. + h_dentry = au_h_dptr(dentry, bindex);
  9227. + if (h_dentry && d_is_positive(h_dentry)) {
  9228. + arg.bindex = bindex;
  9229. + err = test_empty(dentry, &arg);
  9230. + }
  9231. + }
  9232. +
  9233. +out_whlist:
  9234. + au_nhash_wh_free(&whlist);
  9235. +out:
  9236. + return err;
  9237. +}
  9238. +
  9239. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9240. +{
  9241. + int err;
  9242. + struct test_empty_arg arg = {
  9243. + .ctx = {
  9244. + .actor = test_empty_cb
  9245. + }
  9246. + };
  9247. + aufs_bindex_t bindex, btail;
  9248. +
  9249. + err = 0;
  9250. + arg.whlist = whlist;
  9251. + arg.flags = AuTestEmpty_WHONLY;
  9252. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9253. + au_fset_testempty(arg.flags, SHWH);
  9254. + btail = au_dbtaildir(dentry);
  9255. + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
  9256. + struct dentry *h_dentry;
  9257. +
  9258. + h_dentry = au_h_dptr(dentry, bindex);
  9259. + if (h_dentry && d_is_positive(h_dentry)) {
  9260. + arg.bindex = bindex;
  9261. + err = sio_test_empty(dentry, &arg);
  9262. + }
  9263. + }
  9264. +
  9265. + return err;
  9266. +}
  9267. +
  9268. +/* ---------------------------------------------------------------------- */
  9269. +
  9270. +const struct file_operations aufs_dir_fop = {
  9271. + .owner = THIS_MODULE,
  9272. + .llseek = default_llseek,
  9273. + .read = generic_read_dir,
  9274. + .iterate = aufs_iterate,
  9275. + .unlocked_ioctl = aufs_ioctl_dir,
  9276. +#ifdef CONFIG_COMPAT
  9277. + .compat_ioctl = aufs_compat_ioctl_dir,
  9278. +#endif
  9279. + .open = aufs_open_dir,
  9280. + .release = aufs_release_dir,
  9281. + .flush = aufs_flush_dir,
  9282. + .fsync = aufs_fsync_dir
  9283. +};
  9284. --- /dev/null
  9285. +++ linux-4.4/fs/aufs/dir.h 2016-10-23 11:20:57.632159952 +0300
  9286. @@ -0,0 +1,137 @@
  9287. +/*
  9288. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  9289. + *
  9290. + * This program, aufs is free software; you can redistribute it and/or modify
  9291. + * it under the terms of the GNU General Public License as published by
  9292. + * the Free Software Foundation; either version 2 of the License, or
  9293. + * (at your option) any later version.
  9294. + *
  9295. + * This program is distributed in the hope that it will be useful,
  9296. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9297. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9298. + * GNU General Public License for more details.
  9299. + *
  9300. + * You should have received a copy of the GNU General Public License
  9301. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9302. + */
  9303. +
  9304. +/*
  9305. + * directory operations
  9306. + */
  9307. +
  9308. +#ifndef __AUFS_DIR_H__
  9309. +#define __AUFS_DIR_H__
  9310. +
  9311. +#ifdef __KERNEL__
  9312. +
  9313. +#include <linux/fs.h>
  9314. +
  9315. +/* ---------------------------------------------------------------------- */
  9316. +
  9317. +/* need to be faster and smaller */
  9318. +
  9319. +struct au_nhash {
  9320. + unsigned int nh_num;
  9321. + struct hlist_head *nh_head;
  9322. +};
  9323. +
  9324. +struct au_vdir_destr {
  9325. + unsigned char len;
  9326. + unsigned char name[0];
  9327. +} __packed;
  9328. +
  9329. +struct au_vdir_dehstr {
  9330. + struct hlist_node hash;
  9331. + union {
  9332. + struct au_vdir_destr *str;
  9333. + struct llist_node lnode; /* delayed free */
  9334. + };
  9335. +} ____cacheline_aligned_in_smp;
  9336. +
  9337. +struct au_vdir_de {
  9338. + ino_t de_ino;
  9339. + unsigned char de_type;
  9340. + /* caution: packed */
  9341. + struct au_vdir_destr de_str;
  9342. +} __packed;
  9343. +
  9344. +struct au_vdir_wh {
  9345. + struct hlist_node wh_hash;
  9346. +#ifdef CONFIG_AUFS_SHWH
  9347. + ino_t wh_ino;
  9348. + aufs_bindex_t wh_bindex;
  9349. + unsigned char wh_type;
  9350. +#else
  9351. + aufs_bindex_t wh_bindex;
  9352. +#endif
  9353. + /* caution: packed */
  9354. + struct au_vdir_destr wh_str;
  9355. +} __packed;
  9356. +
  9357. +union au_vdir_deblk_p {
  9358. + unsigned char *deblk;
  9359. + struct au_vdir_de *de;
  9360. +};
  9361. +
  9362. +struct au_vdir {
  9363. + unsigned char **vd_deblk;
  9364. + unsigned long vd_nblk;
  9365. + struct {
  9366. + unsigned long ul;
  9367. + union au_vdir_deblk_p p;
  9368. + } vd_last;
  9369. +
  9370. + unsigned long vd_version;
  9371. + unsigned int vd_deblk_sz;
  9372. + union {
  9373. + unsigned long vd_jiffy;
  9374. + struct llist_node vd_lnode; /* delayed free */
  9375. + };
  9376. +} ____cacheline_aligned_in_smp;
  9377. +
  9378. +/* ---------------------------------------------------------------------- */
  9379. +
  9380. +/* dir.c */
  9381. +extern const struct file_operations aufs_dir_fop;
  9382. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9383. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9384. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9385. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9386. +int au_test_empty_lower(struct dentry *dentry);
  9387. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9388. +
  9389. +/* vdir.c */
  9390. +unsigned int au_rdhash_est(loff_t sz);
  9391. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9392. +void au_nhash_wh_free(struct au_nhash *whlist);
  9393. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9394. + int limit);
  9395. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9396. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9397. + unsigned int d_type, aufs_bindex_t bindex,
  9398. + unsigned char shwh);
  9399. +void au_vdir_free(struct au_vdir *vdir, int atonce);
  9400. +int au_vdir_init(struct file *file);
  9401. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9402. +
  9403. +/* ioctl.c */
  9404. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9405. +
  9406. +#ifdef CONFIG_AUFS_RDU
  9407. +/* rdu.c */
  9408. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9409. +#ifdef CONFIG_COMPAT
  9410. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9411. + unsigned long arg);
  9412. +#endif
  9413. +#else
  9414. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9415. + unsigned int cmd, unsigned long arg)
  9416. +#ifdef CONFIG_COMPAT
  9417. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9418. + unsigned int cmd, unsigned long arg)
  9419. +#endif
  9420. +#endif
  9421. +
  9422. +#endif /* __KERNEL__ */
  9423. +#endif /* __AUFS_DIR_H__ */
  9424. --- /dev/null
  9425. +++ linux-4.4/fs/aufs/dynop.c 2016-10-23 11:20:57.632159952 +0300
  9426. @@ -0,0 +1,369 @@
  9427. +/*
  9428. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  9429. + *
  9430. + * This program, aufs is free software; you can redistribute it and/or modify
  9431. + * it under the terms of the GNU General Public License as published by
  9432. + * the Free Software Foundation; either version 2 of the License, or
  9433. + * (at your option) any later version.
  9434. + *
  9435. + * This program is distributed in the hope that it will be useful,
  9436. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9437. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9438. + * GNU General Public License for more details.
  9439. + *
  9440. + * You should have received a copy of the GNU General Public License
  9441. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9442. + */
  9443. +
  9444. +/*
  9445. + * dynamically customizable operations for regular files
  9446. + */
  9447. +
  9448. +#include "aufs.h"
  9449. +
  9450. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  9451. +
  9452. +/*
  9453. + * How large will these lists be?
  9454. + * Usually just a few elements, 20-30 at most for each, I guess.
  9455. + */
  9456. +static struct au_sphlhead dynop[AuDyLast];
  9457. +
  9458. +static struct au_dykey *dy_gfind_get(struct au_sphlhead *sphl, const void *h_op)
  9459. +{
  9460. + struct au_dykey *key, *tmp;
  9461. + struct hlist_head *head;
  9462. +
  9463. + key = NULL;
  9464. + head = &sphl->head;
  9465. + rcu_read_lock();
  9466. + hlist_for_each_entry_rcu(tmp, head, dk_hnode)
  9467. + if (tmp->dk_op.dy_hop == h_op) {
  9468. + key = tmp;
  9469. + kref_get(&key->dk_kref);
  9470. + break;
  9471. + }
  9472. + rcu_read_unlock();
  9473. +
  9474. + return key;
  9475. +}
  9476. +
  9477. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  9478. +{
  9479. + struct au_dykey **k, *found;
  9480. + const void *h_op = key->dk_op.dy_hop;
  9481. + int i;
  9482. +
  9483. + found = NULL;
  9484. + k = br->br_dykey;
  9485. + for (i = 0; i < AuBrDynOp; i++)
  9486. + if (k[i]) {
  9487. + if (k[i]->dk_op.dy_hop == h_op) {
  9488. + found = k[i];
  9489. + break;
  9490. + }
  9491. + } else
  9492. + break;
  9493. + if (!found) {
  9494. + spin_lock(&br->br_dykey_lock);
  9495. + for (; i < AuBrDynOp; i++)
  9496. + if (k[i]) {
  9497. + if (k[i]->dk_op.dy_hop == h_op) {
  9498. + found = k[i];
  9499. + break;
  9500. + }
  9501. + } else {
  9502. + k[i] = key;
  9503. + break;
  9504. + }
  9505. + spin_unlock(&br->br_dykey_lock);
  9506. + BUG_ON(i == AuBrDynOp); /* expand the array */
  9507. + }
  9508. +
  9509. + return found;
  9510. +}
  9511. +
  9512. +/* kref_get() if @key is already added */
  9513. +static struct au_dykey *dy_gadd(struct au_sphlhead *sphl, struct au_dykey *key)
  9514. +{
  9515. + struct au_dykey *tmp, *found;
  9516. + struct hlist_head *head;
  9517. + const void *h_op = key->dk_op.dy_hop;
  9518. +
  9519. + found = NULL;
  9520. + head = &sphl->head;
  9521. + spin_lock(&sphl->spin);
  9522. + hlist_for_each_entry(tmp, head, dk_hnode)
  9523. + if (tmp->dk_op.dy_hop == h_op) {
  9524. + kref_get(&tmp->dk_kref);
  9525. + found = tmp;
  9526. + break;
  9527. + }
  9528. + if (!found)
  9529. + hlist_add_head_rcu(&key->dk_hnode, head);
  9530. + spin_unlock(&sphl->spin);
  9531. +
  9532. + if (!found)
  9533. + DyPrSym(key);
  9534. + return found;
  9535. +}
  9536. +
  9537. +static void dy_free_rcu(struct rcu_head *rcu)
  9538. +{
  9539. + struct au_dykey *key;
  9540. +
  9541. + key = container_of(rcu, struct au_dykey, dk_rcu);
  9542. + DyPrSym(key);
  9543. + kfree(key); /* not delayed */
  9544. +}
  9545. +
  9546. +static void dy_free(struct kref *kref)
  9547. +{
  9548. + struct au_dykey *key;
  9549. + struct au_sphlhead *sphl;
  9550. +
  9551. + key = container_of(kref, struct au_dykey, dk_kref);
  9552. + sphl = dynop + key->dk_op.dy_type;
  9553. + au_sphl_del_rcu(&key->dk_hnode, sphl);
  9554. + call_rcu(&key->dk_rcu, dy_free_rcu);
  9555. +}
  9556. +
  9557. +void au_dy_put(struct au_dykey *key)
  9558. +{
  9559. + kref_put(&key->dk_kref, dy_free);
  9560. +}
  9561. +
  9562. +/* ---------------------------------------------------------------------- */
  9563. +
  9564. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  9565. +
  9566. +#ifdef CONFIG_AUFS_DEBUG
  9567. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  9568. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  9569. +#else
  9570. +#define DyDbgDeclare(cnt) do {} while (0)
  9571. +#define DyDbgInc(cnt) do {} while (0)
  9572. +#endif
  9573. +
  9574. +#define DySet(func, dst, src, h_op, h_sb) do { \
  9575. + DyDbgInc(cnt); \
  9576. + if (h_op->func) { \
  9577. + if (src.func) \
  9578. + dst.func = src.func; \
  9579. + else \
  9580. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  9581. + } \
  9582. +} while (0)
  9583. +
  9584. +#define DySetForce(func, dst, src) do { \
  9585. + AuDebugOn(!src.func); \
  9586. + DyDbgInc(cnt); \
  9587. + dst.func = src.func; \
  9588. +} while (0)
  9589. +
  9590. +#define DySetAop(func) \
  9591. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  9592. +#define DySetAopForce(func) \
  9593. + DySetForce(func, dyaop->da_op, aufs_aop)
  9594. +
  9595. +static void dy_aop(struct au_dykey *key, const void *h_op,
  9596. + struct super_block *h_sb __maybe_unused)
  9597. +{
  9598. + struct au_dyaop *dyaop = (void *)key;
  9599. + const struct address_space_operations *h_aop = h_op;
  9600. + DyDbgDeclare(cnt);
  9601. +
  9602. + AuDbg("%s\n", au_sbtype(h_sb));
  9603. +
  9604. + DySetAop(writepage);
  9605. + DySetAopForce(readpage); /* force */
  9606. + DySetAop(writepages);
  9607. + DySetAop(set_page_dirty);
  9608. + DySetAop(readpages);
  9609. + DySetAop(write_begin);
  9610. + DySetAop(write_end);
  9611. + DySetAop(bmap);
  9612. + DySetAop(invalidatepage);
  9613. + DySetAop(releasepage);
  9614. + DySetAop(freepage);
  9615. + /* this one will be changed according to an aufs mount option */
  9616. + DySetAop(direct_IO);
  9617. + DySetAop(migratepage);
  9618. + DySetAop(launder_page);
  9619. + DySetAop(is_partially_uptodate);
  9620. + DySetAop(is_dirty_writeback);
  9621. + DySetAop(error_remove_page);
  9622. + DySetAop(swap_activate);
  9623. + DySetAop(swap_deactivate);
  9624. +
  9625. + DyDbgSize(cnt, *h_aop);
  9626. +}
  9627. +
  9628. +/* ---------------------------------------------------------------------- */
  9629. +
  9630. +static void dy_bug(struct kref *kref)
  9631. +{
  9632. + BUG();
  9633. +}
  9634. +
  9635. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  9636. +{
  9637. + struct au_dykey *key, *old;
  9638. + struct au_sphlhead *sphl;
  9639. + struct op {
  9640. + unsigned int sz;
  9641. + void (*set)(struct au_dykey *key, const void *h_op,
  9642. + struct super_block *h_sb __maybe_unused);
  9643. + };
  9644. + static const struct op a[] = {
  9645. + [AuDy_AOP] = {
  9646. + .sz = sizeof(struct au_dyaop),
  9647. + .set = dy_aop
  9648. + }
  9649. + };
  9650. + const struct op *p;
  9651. +
  9652. + sphl = dynop + op->dy_type;
  9653. + key = dy_gfind_get(sphl, op->dy_hop);
  9654. + if (key)
  9655. + goto out_add; /* success */
  9656. +
  9657. + p = a + op->dy_type;
  9658. + key = kzalloc(p->sz, GFP_NOFS);
  9659. + if (unlikely(!key)) {
  9660. + key = ERR_PTR(-ENOMEM);
  9661. + goto out;
  9662. + }
  9663. +
  9664. + key->dk_op.dy_hop = op->dy_hop;
  9665. + kref_init(&key->dk_kref);
  9666. + p->set(key, op->dy_hop, au_br_sb(br));
  9667. + old = dy_gadd(sphl, key);
  9668. + if (old) {
  9669. + au_delayed_kfree(key);
  9670. + key = old;
  9671. + }
  9672. +
  9673. +out_add:
  9674. + old = dy_bradd(br, key);
  9675. + if (old)
  9676. + /* its ref-count should never be zero here */
  9677. + kref_put(&key->dk_kref, dy_bug);
  9678. +out:
  9679. + return key;
  9680. +}
  9681. +
  9682. +/* ---------------------------------------------------------------------- */
  9683. +/*
  9684. + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
  9685. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  9686. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  9687. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  9688. + * See the aufs manual in detail.
  9689. + */
  9690. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  9691. +{
  9692. + if (!do_dx)
  9693. + dyaop->da_op.direct_IO = NULL;
  9694. + else
  9695. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  9696. +}
  9697. +
  9698. +static struct au_dyaop *dy_aget(struct au_branch *br,
  9699. + const struct address_space_operations *h_aop,
  9700. + int do_dx)
  9701. +{
  9702. + struct au_dyaop *dyaop;
  9703. + struct au_dynop op;
  9704. +
  9705. + op.dy_type = AuDy_AOP;
  9706. + op.dy_haop = h_aop;
  9707. + dyaop = (void *)dy_get(&op, br);
  9708. + if (IS_ERR(dyaop))
  9709. + goto out;
  9710. + dy_adx(dyaop, do_dx);
  9711. +
  9712. +out:
  9713. + return dyaop;
  9714. +}
  9715. +
  9716. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9717. + struct inode *h_inode)
  9718. +{
  9719. + int err, do_dx;
  9720. + struct super_block *sb;
  9721. + struct au_branch *br;
  9722. + struct au_dyaop *dyaop;
  9723. +
  9724. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  9725. + IiMustWriteLock(inode);
  9726. +
  9727. + sb = inode->i_sb;
  9728. + br = au_sbr(sb, bindex);
  9729. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  9730. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  9731. + err = PTR_ERR(dyaop);
  9732. + if (IS_ERR(dyaop))
  9733. + /* unnecessary to call dy_fput() */
  9734. + goto out;
  9735. +
  9736. + err = 0;
  9737. + inode->i_mapping->a_ops = &dyaop->da_op;
  9738. +
  9739. +out:
  9740. + return err;
  9741. +}
  9742. +
  9743. +/*
  9744. + * Is it safe to replace a_ops during the inode/file is in operation?
  9745. + * Yes, I hope so.
  9746. + */
  9747. +int au_dy_irefresh(struct inode *inode)
  9748. +{
  9749. + int err;
  9750. + aufs_bindex_t btop;
  9751. + struct inode *h_inode;
  9752. +
  9753. + err = 0;
  9754. + if (S_ISREG(inode->i_mode)) {
  9755. + btop = au_ibtop(inode);
  9756. + h_inode = au_h_iptr(inode, btop);
  9757. + err = au_dy_iaop(inode, btop, h_inode);
  9758. + }
  9759. + return err;
  9760. +}
  9761. +
  9762. +void au_dy_arefresh(int do_dx)
  9763. +{
  9764. + struct au_sphlhead *sphl;
  9765. + struct hlist_head *head;
  9766. + struct au_dykey *key;
  9767. +
  9768. + sphl = dynop + AuDy_AOP;
  9769. + head = &sphl->head;
  9770. + spin_lock(&sphl->spin);
  9771. + hlist_for_each_entry(key, head, dk_hnode)
  9772. + dy_adx((void *)key, do_dx);
  9773. + spin_unlock(&sphl->spin);
  9774. +}
  9775. +
  9776. +/* ---------------------------------------------------------------------- */
  9777. +
  9778. +void __init au_dy_init(void)
  9779. +{
  9780. + int i;
  9781. +
  9782. + /* make sure that 'struct au_dykey *' can be any type */
  9783. + BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
  9784. +
  9785. + for (i = 0; i < AuDyLast; i++)
  9786. + au_sphl_init(dynop + i);
  9787. +}
  9788. +
  9789. +void au_dy_fin(void)
  9790. +{
  9791. + int i;
  9792. +
  9793. + for (i = 0; i < AuDyLast; i++)
  9794. + WARN_ON(!hlist_empty(&dynop[i].head));
  9795. +}
  9796. --- /dev/null
  9797. +++ linux-4.4/fs/aufs/dynop.h 2016-10-23 11:20:57.632159952 +0300
  9798. @@ -0,0 +1,74 @@
  9799. +/*
  9800. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  9801. + *
  9802. + * This program, aufs is free software; you can redistribute it and/or modify
  9803. + * it under the terms of the GNU General Public License as published by
  9804. + * the Free Software Foundation; either version 2 of the License, or
  9805. + * (at your option) any later version.
  9806. + *
  9807. + * This program is distributed in the hope that it will be useful,
  9808. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9809. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9810. + * GNU General Public License for more details.
  9811. + *
  9812. + * You should have received a copy of the GNU General Public License
  9813. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9814. + */
  9815. +
  9816. +/*
  9817. + * dynamically customizable operations (for regular files only)
  9818. + */
  9819. +
  9820. +#ifndef __AUFS_DYNOP_H__
  9821. +#define __AUFS_DYNOP_H__
  9822. +
  9823. +#ifdef __KERNEL__
  9824. +
  9825. +#include <linux/fs.h>
  9826. +#include <linux/kref.h>
  9827. +
  9828. +enum {AuDy_AOP, AuDyLast};
  9829. +
  9830. +struct au_dynop {
  9831. + int dy_type;
  9832. + union {
  9833. + const void *dy_hop;
  9834. + const struct address_space_operations *dy_haop;
  9835. + };
  9836. +};
  9837. +
  9838. +struct au_dykey {
  9839. + union {
  9840. + struct hlist_node dk_hnode;
  9841. + struct rcu_head dk_rcu;
  9842. + };
  9843. + struct au_dynop dk_op;
  9844. +
  9845. + /*
  9846. + * during I am in the branch local array, kref is gotten. when the
  9847. + * branch is removed, kref is put.
  9848. + */
  9849. + struct kref dk_kref;
  9850. +};
  9851. +
  9852. +/* stop unioning since their sizes are very different from each other */
  9853. +struct au_dyaop {
  9854. + struct au_dykey da_key;
  9855. + struct address_space_operations da_op; /* not const */
  9856. +};
  9857. +
  9858. +/* ---------------------------------------------------------------------- */
  9859. +
  9860. +/* dynop.c */
  9861. +struct au_branch;
  9862. +void au_dy_put(struct au_dykey *key);
  9863. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9864. + struct inode *h_inode);
  9865. +int au_dy_irefresh(struct inode *inode);
  9866. +void au_dy_arefresh(int do_dio);
  9867. +
  9868. +void __init au_dy_init(void);
  9869. +void au_dy_fin(void);
  9870. +
  9871. +#endif /* __KERNEL__ */
  9872. +#endif /* __AUFS_DYNOP_H__ */
  9873. --- /dev/null
  9874. +++ linux-4.4/fs/aufs/export.c 2016-10-23 11:20:57.632159952 +0300
  9875. @@ -0,0 +1,839 @@
  9876. +/*
  9877. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  9878. + *
  9879. + * This program, aufs is free software; you can redistribute it and/or modify
  9880. + * it under the terms of the GNU General Public License as published by
  9881. + * the Free Software Foundation; either version 2 of the License, or
  9882. + * (at your option) any later version.
  9883. + *
  9884. + * This program is distributed in the hope that it will be useful,
  9885. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9886. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9887. + * GNU General Public License for more details.
  9888. + *
  9889. + * You should have received a copy of the GNU General Public License
  9890. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9891. + */
  9892. +
  9893. +/*
  9894. + * export via nfs
  9895. + */
  9896. +
  9897. +#include <linux/exportfs.h>
  9898. +#include <linux/fs_struct.h>
  9899. +#include <linux/namei.h>
  9900. +#include <linux/nsproxy.h>
  9901. +#include <linux/random.h>
  9902. +#include <linux/writeback.h>
  9903. +#include "../fs/mount.h"
  9904. +#include "aufs.h"
  9905. +
  9906. +union conv {
  9907. +#ifdef CONFIG_AUFS_INO_T_64
  9908. + __u32 a[2];
  9909. +#else
  9910. + __u32 a[1];
  9911. +#endif
  9912. + ino_t ino;
  9913. +};
  9914. +
  9915. +static ino_t decode_ino(__u32 *a)
  9916. +{
  9917. + union conv u;
  9918. +
  9919. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  9920. + u.a[0] = a[0];
  9921. +#ifdef CONFIG_AUFS_INO_T_64
  9922. + u.a[1] = a[1];
  9923. +#endif
  9924. + return u.ino;
  9925. +}
  9926. +
  9927. +static void encode_ino(__u32 *a, ino_t ino)
  9928. +{
  9929. + union conv u;
  9930. +
  9931. + u.ino = ino;
  9932. + a[0] = u.a[0];
  9933. +#ifdef CONFIG_AUFS_INO_T_64
  9934. + a[1] = u.a[1];
  9935. +#endif
  9936. +}
  9937. +
  9938. +/* NFS file handle */
  9939. +enum {
  9940. + Fh_br_id,
  9941. + Fh_sigen,
  9942. +#ifdef CONFIG_AUFS_INO_T_64
  9943. + /* support 64bit inode number */
  9944. + Fh_ino1,
  9945. + Fh_ino2,
  9946. + Fh_dir_ino1,
  9947. + Fh_dir_ino2,
  9948. +#else
  9949. + Fh_ino1,
  9950. + Fh_dir_ino1,
  9951. +#endif
  9952. + Fh_igen,
  9953. + Fh_h_type,
  9954. + Fh_tail,
  9955. +
  9956. + Fh_ino = Fh_ino1,
  9957. + Fh_dir_ino = Fh_dir_ino1
  9958. +};
  9959. +
  9960. +static int au_test_anon(struct dentry *dentry)
  9961. +{
  9962. + /* note: read d_flags without d_lock */
  9963. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  9964. +}
  9965. +
  9966. +int au_test_nfsd(void)
  9967. +{
  9968. + int ret;
  9969. + struct task_struct *tsk = current;
  9970. + char comm[sizeof(tsk->comm)];
  9971. +
  9972. + ret = 0;
  9973. + if (tsk->flags & PF_KTHREAD) {
  9974. + get_task_comm(comm, tsk);
  9975. + ret = !strcmp(comm, "nfsd");
  9976. + }
  9977. +
  9978. + return ret;
  9979. +}
  9980. +
  9981. +/* ---------------------------------------------------------------------- */
  9982. +/* inode generation external table */
  9983. +
  9984. +void au_xigen_inc(struct inode *inode)
  9985. +{
  9986. + loff_t pos;
  9987. + ssize_t sz;
  9988. + __u32 igen;
  9989. + struct super_block *sb;
  9990. + struct au_sbinfo *sbinfo;
  9991. +
  9992. + sb = inode->i_sb;
  9993. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  9994. +
  9995. + sbinfo = au_sbi(sb);
  9996. + pos = inode->i_ino;
  9997. + pos *= sizeof(igen);
  9998. + igen = inode->i_generation + 1;
  9999. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  10000. + sizeof(igen), &pos);
  10001. + if (sz == sizeof(igen))
  10002. + return; /* success */
  10003. +
  10004. + if (unlikely(sz >= 0))
  10005. + AuIOErr("xigen error (%zd)\n", sz);
  10006. +}
  10007. +
  10008. +int au_xigen_new(struct inode *inode)
  10009. +{
  10010. + int err;
  10011. + loff_t pos;
  10012. + ssize_t sz;
  10013. + struct super_block *sb;
  10014. + struct au_sbinfo *sbinfo;
  10015. + struct file *file;
  10016. +
  10017. + err = 0;
  10018. + /* todo: dirty, at mount time */
  10019. + if (inode->i_ino == AUFS_ROOT_INO)
  10020. + goto out;
  10021. + sb = inode->i_sb;
  10022. + SiMustAnyLock(sb);
  10023. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  10024. + goto out;
  10025. +
  10026. + err = -EFBIG;
  10027. + pos = inode->i_ino;
  10028. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  10029. + AuIOErr1("too large i%lld\n", pos);
  10030. + goto out;
  10031. + }
  10032. + pos *= sizeof(inode->i_generation);
  10033. +
  10034. + err = 0;
  10035. + sbinfo = au_sbi(sb);
  10036. + file = sbinfo->si_xigen;
  10037. + BUG_ON(!file);
  10038. +
  10039. + if (vfsub_f_size_read(file)
  10040. + < pos + sizeof(inode->i_generation)) {
  10041. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  10042. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  10043. + sizeof(inode->i_generation), &pos);
  10044. + } else
  10045. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  10046. + sizeof(inode->i_generation), &pos);
  10047. + if (sz == sizeof(inode->i_generation))
  10048. + goto out; /* success */
  10049. +
  10050. + err = sz;
  10051. + if (unlikely(sz >= 0)) {
  10052. + err = -EIO;
  10053. + AuIOErr("xigen error (%zd)\n", sz);
  10054. + }
  10055. +
  10056. +out:
  10057. + return err;
  10058. +}
  10059. +
  10060. +int au_xigen_set(struct super_block *sb, struct file *base)
  10061. +{
  10062. + int err;
  10063. + struct au_sbinfo *sbinfo;
  10064. + struct file *file;
  10065. +
  10066. + SiMustWriteLock(sb);
  10067. +
  10068. + sbinfo = au_sbi(sb);
  10069. + file = au_xino_create2(base, sbinfo->si_xigen);
  10070. + err = PTR_ERR(file);
  10071. + if (IS_ERR(file))
  10072. + goto out;
  10073. + err = 0;
  10074. + if (sbinfo->si_xigen)
  10075. + fput(sbinfo->si_xigen);
  10076. + sbinfo->si_xigen = file;
  10077. +
  10078. +out:
  10079. + return err;
  10080. +}
  10081. +
  10082. +void au_xigen_clr(struct super_block *sb)
  10083. +{
  10084. + struct au_sbinfo *sbinfo;
  10085. +
  10086. + SiMustWriteLock(sb);
  10087. +
  10088. + sbinfo = au_sbi(sb);
  10089. + if (sbinfo->si_xigen) {
  10090. + fput(sbinfo->si_xigen);
  10091. + sbinfo->si_xigen = NULL;
  10092. + }
  10093. +}
  10094. +
  10095. +/* ---------------------------------------------------------------------- */
  10096. +
  10097. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  10098. + ino_t dir_ino)
  10099. +{
  10100. + struct dentry *dentry, *d;
  10101. + struct inode *inode;
  10102. + unsigned int sigen;
  10103. +
  10104. + dentry = NULL;
  10105. + inode = ilookup(sb, ino);
  10106. + if (!inode)
  10107. + goto out;
  10108. +
  10109. + dentry = ERR_PTR(-ESTALE);
  10110. + sigen = au_sigen(sb);
  10111. + if (unlikely(au_is_bad_inode(inode)
  10112. + || IS_DEADDIR(inode)
  10113. + || sigen != au_iigen(inode, NULL)))
  10114. + goto out_iput;
  10115. +
  10116. + dentry = NULL;
  10117. + if (!dir_ino || S_ISDIR(inode->i_mode))
  10118. + dentry = d_find_alias(inode);
  10119. + else {
  10120. + spin_lock(&inode->i_lock);
  10121. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  10122. + spin_lock(&d->d_lock);
  10123. + if (!au_test_anon(d)
  10124. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  10125. + dentry = dget_dlock(d);
  10126. + spin_unlock(&d->d_lock);
  10127. + break;
  10128. + }
  10129. + spin_unlock(&d->d_lock);
  10130. + }
  10131. + spin_unlock(&inode->i_lock);
  10132. + }
  10133. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  10134. + /* need to refresh */
  10135. + dput(dentry);
  10136. + dentry = NULL;
  10137. + }
  10138. +
  10139. +out_iput:
  10140. + iput(inode);
  10141. +out:
  10142. + AuTraceErrPtr(dentry);
  10143. + return dentry;
  10144. +}
  10145. +
  10146. +/* ---------------------------------------------------------------------- */
  10147. +
  10148. +/* todo: dirty? */
  10149. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  10150. +
  10151. +struct au_compare_mnt_args {
  10152. + /* input */
  10153. + struct super_block *sb;
  10154. +
  10155. + /* output */
  10156. + struct vfsmount *mnt;
  10157. +};
  10158. +
  10159. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  10160. +{
  10161. + struct au_compare_mnt_args *a = arg;
  10162. +
  10163. + if (mnt->mnt_sb != a->sb)
  10164. + return 0;
  10165. + a->mnt = mntget(mnt);
  10166. + return 1;
  10167. +}
  10168. +
  10169. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  10170. +{
  10171. + int err;
  10172. + struct path root;
  10173. + struct au_compare_mnt_args args = {
  10174. + .sb = sb
  10175. + };
  10176. +
  10177. + get_fs_root(current->fs, &root);
  10178. + rcu_read_lock();
  10179. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  10180. + rcu_read_unlock();
  10181. + path_put(&root);
  10182. + AuDebugOn(!err);
  10183. + AuDebugOn(!args.mnt);
  10184. + return args.mnt;
  10185. +}
  10186. +
  10187. +struct au_nfsd_si_lock {
  10188. + unsigned int sigen;
  10189. + aufs_bindex_t bindex, br_id;
  10190. + unsigned char force_lock;
  10191. +};
  10192. +
  10193. +static int si_nfsd_read_lock(struct super_block *sb,
  10194. + struct au_nfsd_si_lock *nsi_lock)
  10195. +{
  10196. + int err;
  10197. + aufs_bindex_t bindex;
  10198. +
  10199. + si_read_lock(sb, AuLock_FLUSH);
  10200. +
  10201. + /* branch id may be wrapped around */
  10202. + err = 0;
  10203. + bindex = au_br_index(sb, nsi_lock->br_id);
  10204. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  10205. + goto out; /* success */
  10206. +
  10207. + err = -ESTALE;
  10208. + bindex = -1;
  10209. + if (!nsi_lock->force_lock)
  10210. + si_read_unlock(sb);
  10211. +
  10212. +out:
  10213. + nsi_lock->bindex = bindex;
  10214. + return err;
  10215. +}
  10216. +
  10217. +struct find_name_by_ino {
  10218. + struct dir_context ctx;
  10219. + int called, found;
  10220. + ino_t ino;
  10221. + char *name;
  10222. + int namelen;
  10223. +};
  10224. +
  10225. +static int
  10226. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  10227. + loff_t offset, u64 ino, unsigned int d_type)
  10228. +{
  10229. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  10230. + ctx);
  10231. +
  10232. + a->called++;
  10233. + if (a->ino != ino)
  10234. + return 0;
  10235. +
  10236. + memcpy(a->name, name, namelen);
  10237. + a->namelen = namelen;
  10238. + a->found = 1;
  10239. + return 1;
  10240. +}
  10241. +
  10242. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  10243. + struct au_nfsd_si_lock *nsi_lock)
  10244. +{
  10245. + struct dentry *dentry, *parent;
  10246. + struct file *file;
  10247. + struct inode *dir;
  10248. + struct find_name_by_ino arg = {
  10249. + .ctx = {
  10250. + .actor = find_name_by_ino
  10251. + }
  10252. + };
  10253. + int err;
  10254. +
  10255. + parent = path->dentry;
  10256. + if (nsi_lock)
  10257. + si_read_unlock(parent->d_sb);
  10258. + file = vfsub_dentry_open(path, au_dir_roflags);
  10259. + dentry = (void *)file;
  10260. + if (IS_ERR(file))
  10261. + goto out;
  10262. +
  10263. + dentry = ERR_PTR(-ENOMEM);
  10264. + arg.name = (void *)__get_free_page(GFP_NOFS);
  10265. + if (unlikely(!arg.name))
  10266. + goto out_file;
  10267. + arg.ino = ino;
  10268. + arg.found = 0;
  10269. + do {
  10270. + arg.called = 0;
  10271. + /* smp_mb(); */
  10272. + err = vfsub_iterate_dir(file, &arg.ctx);
  10273. + } while (!err && !arg.found && arg.called);
  10274. + dentry = ERR_PTR(err);
  10275. + if (unlikely(err))
  10276. + goto out_name;
  10277. + /* instead of ENOENT */
  10278. + dentry = ERR_PTR(-ESTALE);
  10279. + if (!arg.found)
  10280. + goto out_name;
  10281. +
  10282. + /* do not call vfsub_lkup_one() */
  10283. + dir = d_inode(parent);
  10284. + mutex_lock(&dir->i_mutex);
  10285. + dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
  10286. + mutex_unlock(&dir->i_mutex);
  10287. + AuTraceErrPtr(dentry);
  10288. + if (IS_ERR(dentry))
  10289. + goto out_name;
  10290. + AuDebugOn(au_test_anon(dentry));
  10291. + if (unlikely(d_really_is_negative(dentry))) {
  10292. + dput(dentry);
  10293. + dentry = ERR_PTR(-ENOENT);
  10294. + }
  10295. +
  10296. +out_name:
  10297. + au_delayed_free_page((unsigned long)arg.name);
  10298. +out_file:
  10299. + fput(file);
  10300. +out:
  10301. + if (unlikely(nsi_lock
  10302. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  10303. + if (!IS_ERR(dentry)) {
  10304. + dput(dentry);
  10305. + dentry = ERR_PTR(-ESTALE);
  10306. + }
  10307. + AuTraceErrPtr(dentry);
  10308. + return dentry;
  10309. +}
  10310. +
  10311. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  10312. + ino_t dir_ino,
  10313. + struct au_nfsd_si_lock *nsi_lock)
  10314. +{
  10315. + struct dentry *dentry;
  10316. + struct path path;
  10317. +
  10318. + if (dir_ino != AUFS_ROOT_INO) {
  10319. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  10320. + dentry = path.dentry;
  10321. + if (!path.dentry || IS_ERR(path.dentry))
  10322. + goto out;
  10323. + AuDebugOn(au_test_anon(path.dentry));
  10324. + } else
  10325. + path.dentry = dget(sb->s_root);
  10326. +
  10327. + path.mnt = au_mnt_get(sb);
  10328. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  10329. + path_put(&path);
  10330. +
  10331. +out:
  10332. + AuTraceErrPtr(dentry);
  10333. + return dentry;
  10334. +}
  10335. +
  10336. +/* ---------------------------------------------------------------------- */
  10337. +
  10338. +static int h_acceptable(void *expv, struct dentry *dentry)
  10339. +{
  10340. + return 1;
  10341. +}
  10342. +
  10343. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  10344. + char *buf, int len, struct super_block *sb)
  10345. +{
  10346. + char *p;
  10347. + int n;
  10348. + struct path path;
  10349. +
  10350. + p = d_path(h_rootpath, buf, len);
  10351. + if (IS_ERR(p))
  10352. + goto out;
  10353. + n = strlen(p);
  10354. +
  10355. + path.mnt = h_rootpath->mnt;
  10356. + path.dentry = h_parent;
  10357. + p = d_path(&path, buf, len);
  10358. + if (IS_ERR(p))
  10359. + goto out;
  10360. + if (n != 1)
  10361. + p += n;
  10362. +
  10363. + path.mnt = au_mnt_get(sb);
  10364. + path.dentry = sb->s_root;
  10365. + p = d_path(&path, buf, len - strlen(p));
  10366. + mntput(path.mnt);
  10367. + if (IS_ERR(p))
  10368. + goto out;
  10369. + if (n != 1)
  10370. + p[strlen(p)] = '/';
  10371. +
  10372. +out:
  10373. + AuTraceErrPtr(p);
  10374. + return p;
  10375. +}
  10376. +
  10377. +static
  10378. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  10379. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  10380. +{
  10381. + struct dentry *dentry, *h_parent, *root;
  10382. + struct super_block *h_sb;
  10383. + char *pathname, *p;
  10384. + struct vfsmount *h_mnt;
  10385. + struct au_branch *br;
  10386. + int err;
  10387. + struct path path;
  10388. +
  10389. + br = au_sbr(sb, nsi_lock->bindex);
  10390. + h_mnt = au_br_mnt(br);
  10391. + h_sb = h_mnt->mnt_sb;
  10392. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  10393. + lockdep_off();
  10394. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  10395. + fh_len - Fh_tail, fh[Fh_h_type],
  10396. + h_acceptable, /*context*/NULL);
  10397. + lockdep_on();
  10398. + dentry = h_parent;
  10399. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  10400. + AuWarn1("%s decode_fh failed, %ld\n",
  10401. + au_sbtype(h_sb), PTR_ERR(h_parent));
  10402. + goto out;
  10403. + }
  10404. + dentry = NULL;
  10405. + if (unlikely(au_test_anon(h_parent))) {
  10406. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  10407. + au_sbtype(h_sb));
  10408. + goto out_h_parent;
  10409. + }
  10410. +
  10411. + dentry = ERR_PTR(-ENOMEM);
  10412. + pathname = (void *)__get_free_page(GFP_NOFS);
  10413. + if (unlikely(!pathname))
  10414. + goto out_h_parent;
  10415. +
  10416. + root = sb->s_root;
  10417. + path.mnt = h_mnt;
  10418. + di_read_lock_parent(root, !AuLock_IR);
  10419. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  10420. + di_read_unlock(root, !AuLock_IR);
  10421. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  10422. + dentry = (void *)p;
  10423. + if (IS_ERR(p))
  10424. + goto out_pathname;
  10425. +
  10426. + si_read_unlock(sb);
  10427. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  10428. + dentry = ERR_PTR(err);
  10429. + if (unlikely(err))
  10430. + goto out_relock;
  10431. +
  10432. + dentry = ERR_PTR(-ENOENT);
  10433. + AuDebugOn(au_test_anon(path.dentry));
  10434. + if (unlikely(d_really_is_negative(path.dentry)))
  10435. + goto out_path;
  10436. +
  10437. + if (ino != d_inode(path.dentry)->i_ino)
  10438. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  10439. + else
  10440. + dentry = dget(path.dentry);
  10441. +
  10442. +out_path:
  10443. + path_put(&path);
  10444. +out_relock:
  10445. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  10446. + if (!IS_ERR(dentry)) {
  10447. + dput(dentry);
  10448. + dentry = ERR_PTR(-ESTALE);
  10449. + }
  10450. +out_pathname:
  10451. + au_delayed_free_page((unsigned long)pathname);
  10452. +out_h_parent:
  10453. + dput(h_parent);
  10454. +out:
  10455. + AuTraceErrPtr(dentry);
  10456. + return dentry;
  10457. +}
  10458. +
  10459. +/* ---------------------------------------------------------------------- */
  10460. +
  10461. +static struct dentry *
  10462. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  10463. + int fh_type)
  10464. +{
  10465. + struct dentry *dentry;
  10466. + __u32 *fh = fid->raw;
  10467. + struct au_branch *br;
  10468. + ino_t ino, dir_ino;
  10469. + struct au_nfsd_si_lock nsi_lock = {
  10470. + .force_lock = 0
  10471. + };
  10472. +
  10473. + dentry = ERR_PTR(-ESTALE);
  10474. + /* it should never happen, but the file handle is unreliable */
  10475. + if (unlikely(fh_len < Fh_tail))
  10476. + goto out;
  10477. + nsi_lock.sigen = fh[Fh_sigen];
  10478. + nsi_lock.br_id = fh[Fh_br_id];
  10479. +
  10480. + /* branch id may be wrapped around */
  10481. + br = NULL;
  10482. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  10483. + goto out;
  10484. + nsi_lock.force_lock = 1;
  10485. +
  10486. + /* is this inode still cached? */
  10487. + ino = decode_ino(fh + Fh_ino);
  10488. + /* it should never happen */
  10489. + if (unlikely(ino == AUFS_ROOT_INO))
  10490. + goto out_unlock;
  10491. +
  10492. + dir_ino = decode_ino(fh + Fh_dir_ino);
  10493. + dentry = decode_by_ino(sb, ino, dir_ino);
  10494. + if (IS_ERR(dentry))
  10495. + goto out_unlock;
  10496. + if (dentry)
  10497. + goto accept;
  10498. +
  10499. + /* is the parent dir cached? */
  10500. + br = au_sbr(sb, nsi_lock.bindex);
  10501. + au_br_get(br);
  10502. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  10503. + if (IS_ERR(dentry))
  10504. + goto out_unlock;
  10505. + if (dentry)
  10506. + goto accept;
  10507. +
  10508. + /* lookup path */
  10509. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  10510. + if (IS_ERR(dentry))
  10511. + goto out_unlock;
  10512. + if (unlikely(!dentry))
  10513. + /* todo?: make it ESTALE */
  10514. + goto out_unlock;
  10515. +
  10516. +accept:
  10517. + if (!au_digen_test(dentry, au_sigen(sb))
  10518. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  10519. + goto out_unlock; /* success */
  10520. +
  10521. + dput(dentry);
  10522. + dentry = ERR_PTR(-ESTALE);
  10523. +out_unlock:
  10524. + if (br)
  10525. + au_br_put(br);
  10526. + si_read_unlock(sb);
  10527. +out:
  10528. + AuTraceErrPtr(dentry);
  10529. + return dentry;
  10530. +}
  10531. +
  10532. +#if 0 /* reserved for future use */
  10533. +/* support subtreecheck option */
  10534. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  10535. + int fh_len, int fh_type)
  10536. +{
  10537. + struct dentry *parent;
  10538. + __u32 *fh = fid->raw;
  10539. + ino_t dir_ino;
  10540. +
  10541. + dir_ino = decode_ino(fh + Fh_dir_ino);
  10542. + parent = decode_by_ino(sb, dir_ino, 0);
  10543. + if (IS_ERR(parent))
  10544. + goto out;
  10545. + if (!parent)
  10546. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  10547. + dir_ino, fh, fh_len);
  10548. +
  10549. +out:
  10550. + AuTraceErrPtr(parent);
  10551. + return parent;
  10552. +}
  10553. +#endif
  10554. +
  10555. +/* ---------------------------------------------------------------------- */
  10556. +
  10557. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  10558. + struct inode *dir)
  10559. +{
  10560. + int err;
  10561. + aufs_bindex_t bindex;
  10562. + struct super_block *sb, *h_sb;
  10563. + struct dentry *dentry, *parent, *h_parent;
  10564. + struct inode *h_dir;
  10565. + struct au_branch *br;
  10566. +
  10567. + err = -ENOSPC;
  10568. + if (unlikely(*max_len <= Fh_tail)) {
  10569. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  10570. + goto out;
  10571. + }
  10572. +
  10573. + err = FILEID_ROOT;
  10574. + if (inode->i_ino == AUFS_ROOT_INO) {
  10575. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  10576. + goto out;
  10577. + }
  10578. +
  10579. + h_parent = NULL;
  10580. + sb = inode->i_sb;
  10581. + err = si_read_lock(sb, AuLock_FLUSH);
  10582. + if (unlikely(err))
  10583. + goto out;
  10584. +
  10585. +#ifdef CONFIG_AUFS_DEBUG
  10586. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  10587. + AuWarn1("NFS-exporting requires xino\n");
  10588. +#endif
  10589. + err = -EIO;
  10590. + parent = NULL;
  10591. + ii_read_lock_child(inode);
  10592. + bindex = au_ibtop(inode);
  10593. + if (!dir) {
  10594. + dentry = d_find_any_alias(inode);
  10595. + if (unlikely(!dentry))
  10596. + goto out_unlock;
  10597. + AuDebugOn(au_test_anon(dentry));
  10598. + parent = dget_parent(dentry);
  10599. + dput(dentry);
  10600. + if (unlikely(!parent))
  10601. + goto out_unlock;
  10602. + if (d_really_is_positive(parent))
  10603. + dir = d_inode(parent);
  10604. + }
  10605. +
  10606. + ii_read_lock_parent(dir);
  10607. + h_dir = au_h_iptr(dir, bindex);
  10608. + ii_read_unlock(dir);
  10609. + if (unlikely(!h_dir))
  10610. + goto out_parent;
  10611. + h_parent = d_find_any_alias(h_dir);
  10612. + if (unlikely(!h_parent))
  10613. + goto out_hparent;
  10614. +
  10615. + err = -EPERM;
  10616. + br = au_sbr(sb, bindex);
  10617. + h_sb = au_br_sb(br);
  10618. + if (unlikely(!h_sb->s_export_op)) {
  10619. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  10620. + goto out_hparent;
  10621. + }
  10622. +
  10623. + fh[Fh_br_id] = br->br_id;
  10624. + fh[Fh_sigen] = au_sigen(sb);
  10625. + encode_ino(fh + Fh_ino, inode->i_ino);
  10626. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  10627. + fh[Fh_igen] = inode->i_generation;
  10628. +
  10629. + *max_len -= Fh_tail;
  10630. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  10631. + max_len,
  10632. + /*connectable or subtreecheck*/0);
  10633. + err = fh[Fh_h_type];
  10634. + *max_len += Fh_tail;
  10635. + /* todo: macros? */
  10636. + if (err != FILEID_INVALID)
  10637. + err = 99;
  10638. + else
  10639. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  10640. +
  10641. +out_hparent:
  10642. + dput(h_parent);
  10643. +out_parent:
  10644. + dput(parent);
  10645. +out_unlock:
  10646. + ii_read_unlock(inode);
  10647. + si_read_unlock(sb);
  10648. +out:
  10649. + if (unlikely(err < 0))
  10650. + err = FILEID_INVALID;
  10651. + return err;
  10652. +}
  10653. +
  10654. +/* ---------------------------------------------------------------------- */
  10655. +
  10656. +static int aufs_commit_metadata(struct inode *inode)
  10657. +{
  10658. + int err;
  10659. + aufs_bindex_t bindex;
  10660. + struct super_block *sb;
  10661. + struct inode *h_inode;
  10662. + int (*f)(struct inode *inode);
  10663. +
  10664. + sb = inode->i_sb;
  10665. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10666. + ii_write_lock_child(inode);
  10667. + bindex = au_ibtop(inode);
  10668. + AuDebugOn(bindex < 0);
  10669. + h_inode = au_h_iptr(inode, bindex);
  10670. +
  10671. + f = h_inode->i_sb->s_export_op->commit_metadata;
  10672. + if (f)
  10673. + err = f(h_inode);
  10674. + else {
  10675. + struct writeback_control wbc = {
  10676. + .sync_mode = WB_SYNC_ALL,
  10677. + .nr_to_write = 0 /* metadata only */
  10678. + };
  10679. +
  10680. + err = sync_inode(h_inode, &wbc);
  10681. + }
  10682. +
  10683. + au_cpup_attr_timesizes(inode);
  10684. + ii_write_unlock(inode);
  10685. + si_read_unlock(sb);
  10686. + return err;
  10687. +}
  10688. +
  10689. +/* ---------------------------------------------------------------------- */
  10690. +
  10691. +static struct export_operations aufs_export_op = {
  10692. + .fh_to_dentry = aufs_fh_to_dentry,
  10693. + /* .fh_to_parent = aufs_fh_to_parent, */
  10694. + .encode_fh = aufs_encode_fh,
  10695. + .commit_metadata = aufs_commit_metadata
  10696. +};
  10697. +
  10698. +void au_export_init(struct super_block *sb)
  10699. +{
  10700. + struct au_sbinfo *sbinfo;
  10701. + __u32 u;
  10702. +
  10703. + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
  10704. + && IS_MODULE(CONFIG_EXPORTFS),
  10705. + AUFS_NAME ": unsupported configuration "
  10706. + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
  10707. +
  10708. + sb->s_export_op = &aufs_export_op;
  10709. + sbinfo = au_sbi(sb);
  10710. + sbinfo->si_xigen = NULL;
  10711. + get_random_bytes(&u, sizeof(u));
  10712. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  10713. + atomic_set(&sbinfo->si_xigen_next, u);
  10714. +}
  10715. --- /dev/null
  10716. +++ linux-4.4/fs/aufs/fhsm.c 2016-10-23 11:20:57.632159952 +0300
  10717. @@ -0,0 +1,426 @@
  10718. +/*
  10719. + * Copyright (C) 2011-2016 Junjiro R. Okajima
  10720. + *
  10721. + * This program, aufs is free software; you can redistribute it and/or modify
  10722. + * it under the terms of the GNU General Public License as published by
  10723. + * the Free Software Foundation; either version 2 of the License, or
  10724. + * (at your option) any later version.
  10725. + *
  10726. + * This program is distributed in the hope that it will be useful,
  10727. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10728. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10729. + * GNU General Public License for more details.
  10730. + *
  10731. + * You should have received a copy of the GNU General Public License
  10732. + * along with this program; if not, write to the Free Software
  10733. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  10734. + */
  10735. +
  10736. +/*
  10737. + * File-based Hierarchy Storage Management
  10738. + */
  10739. +
  10740. +#include <linux/anon_inodes.h>
  10741. +#include <linux/poll.h>
  10742. +#include <linux/seq_file.h>
  10743. +#include <linux/statfs.h>
  10744. +#include "aufs.h"
  10745. +
  10746. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  10747. +{
  10748. + struct au_sbinfo *sbinfo;
  10749. + struct au_fhsm *fhsm;
  10750. +
  10751. + SiMustAnyLock(sb);
  10752. +
  10753. + sbinfo = au_sbi(sb);
  10754. + fhsm = &sbinfo->si_fhsm;
  10755. + AuDebugOn(!fhsm);
  10756. + return fhsm->fhsm_bottom;
  10757. +}
  10758. +
  10759. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  10760. +{
  10761. + struct au_sbinfo *sbinfo;
  10762. + struct au_fhsm *fhsm;
  10763. +
  10764. + SiMustWriteLock(sb);
  10765. +
  10766. + sbinfo = au_sbi(sb);
  10767. + fhsm = &sbinfo->si_fhsm;
  10768. + AuDebugOn(!fhsm);
  10769. + fhsm->fhsm_bottom = bindex;
  10770. +}
  10771. +
  10772. +/* ---------------------------------------------------------------------- */
  10773. +
  10774. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  10775. +{
  10776. + struct au_br_fhsm *bf;
  10777. +
  10778. + bf = br->br_fhsm;
  10779. + MtxMustLock(&bf->bf_lock);
  10780. +
  10781. + return !bf->bf_readable
  10782. + || time_after(jiffies,
  10783. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  10784. +}
  10785. +
  10786. +/* ---------------------------------------------------------------------- */
  10787. +
  10788. +static void au_fhsm_notify(struct super_block *sb, int val)
  10789. +{
  10790. + struct au_sbinfo *sbinfo;
  10791. + struct au_fhsm *fhsm;
  10792. +
  10793. + SiMustAnyLock(sb);
  10794. +
  10795. + sbinfo = au_sbi(sb);
  10796. + fhsm = &sbinfo->si_fhsm;
  10797. + if (au_fhsm_pid(fhsm)
  10798. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  10799. + atomic_set(&fhsm->fhsm_readable, val);
  10800. + if (val)
  10801. + wake_up(&fhsm->fhsm_wqh);
  10802. + }
  10803. +}
  10804. +
  10805. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  10806. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  10807. +{
  10808. + int err;
  10809. + struct au_branch *br;
  10810. + struct au_br_fhsm *bf;
  10811. +
  10812. + br = au_sbr(sb, bindex);
  10813. + AuDebugOn(au_br_rdonly(br));
  10814. + bf = br->br_fhsm;
  10815. + AuDebugOn(!bf);
  10816. +
  10817. + if (do_lock)
  10818. + mutex_lock(&bf->bf_lock);
  10819. + else
  10820. + MtxMustLock(&bf->bf_lock);
  10821. +
  10822. + /* sb->s_root for NFS is unreliable */
  10823. + err = au_br_stfs(br, &bf->bf_stfs);
  10824. + if (unlikely(err)) {
  10825. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  10826. + goto out;
  10827. + }
  10828. +
  10829. + bf->bf_jiffy = jiffies;
  10830. + bf->bf_readable = 1;
  10831. + if (do_notify)
  10832. + au_fhsm_notify(sb, /*val*/1);
  10833. + if (rstfs)
  10834. + *rstfs = bf->bf_stfs;
  10835. +
  10836. +out:
  10837. + if (do_lock)
  10838. + mutex_unlock(&bf->bf_lock);
  10839. + au_fhsm_notify(sb, /*val*/1);
  10840. +
  10841. + return err;
  10842. +}
  10843. +
  10844. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  10845. +{
  10846. + int err;
  10847. + struct au_sbinfo *sbinfo;
  10848. + struct au_fhsm *fhsm;
  10849. + struct au_branch *br;
  10850. + struct au_br_fhsm *bf;
  10851. +
  10852. + AuDbg("b%d, force %d\n", bindex, force);
  10853. + SiMustAnyLock(sb);
  10854. +
  10855. + sbinfo = au_sbi(sb);
  10856. + fhsm = &sbinfo->si_fhsm;
  10857. + if (!au_ftest_si(sbinfo, FHSM)
  10858. + || fhsm->fhsm_bottom == bindex)
  10859. + return;
  10860. +
  10861. + br = au_sbr(sb, bindex);
  10862. + bf = br->br_fhsm;
  10863. + AuDebugOn(!bf);
  10864. + mutex_lock(&bf->bf_lock);
  10865. + if (force
  10866. + || au_fhsm_pid(fhsm)
  10867. + || au_fhsm_test_jiffy(sbinfo, br))
  10868. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  10869. + /*do_notify*/1);
  10870. + mutex_unlock(&bf->bf_lock);
  10871. +}
  10872. +
  10873. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  10874. +{
  10875. + aufs_bindex_t bindex, bbot;
  10876. + struct au_branch *br;
  10877. +
  10878. + /* exclude the bottom */
  10879. + bbot = au_fhsm_bottom(sb);
  10880. + for (bindex = 0; bindex < bbot; bindex++) {
  10881. + br = au_sbr(sb, bindex);
  10882. + if (au_br_fhsm(br->br_perm))
  10883. + au_fhsm_wrote(sb, bindex, force);
  10884. + }
  10885. +}
  10886. +
  10887. +/* ---------------------------------------------------------------------- */
  10888. +
  10889. +static unsigned int au_fhsm_poll(struct file *file,
  10890. + struct poll_table_struct *wait)
  10891. +{
  10892. + unsigned int mask;
  10893. + struct au_sbinfo *sbinfo;
  10894. + struct au_fhsm *fhsm;
  10895. +
  10896. + mask = 0;
  10897. + sbinfo = file->private_data;
  10898. + fhsm = &sbinfo->si_fhsm;
  10899. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  10900. + if (atomic_read(&fhsm->fhsm_readable))
  10901. + mask = POLLIN /* | POLLRDNORM */;
  10902. +
  10903. + AuTraceErr((int)mask);
  10904. + return mask;
  10905. +}
  10906. +
  10907. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  10908. + struct aufs_stfs *stfs, __s16 brid)
  10909. +{
  10910. + int err;
  10911. +
  10912. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  10913. + if (!err)
  10914. + err = __put_user(brid, &stbr->brid);
  10915. + if (unlikely(err))
  10916. + err = -EFAULT;
  10917. +
  10918. + return err;
  10919. +}
  10920. +
  10921. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  10922. + struct aufs_stbr __user *stbr, size_t count)
  10923. +{
  10924. + ssize_t err;
  10925. + int nstbr;
  10926. + aufs_bindex_t bindex, bbot;
  10927. + struct au_branch *br;
  10928. + struct au_br_fhsm *bf;
  10929. +
  10930. + /* except the bottom branch */
  10931. + err = 0;
  10932. + nstbr = 0;
  10933. + bbot = au_fhsm_bottom(sb);
  10934. + for (bindex = 0; !err && bindex < bbot; bindex++) {
  10935. + br = au_sbr(sb, bindex);
  10936. + if (!au_br_fhsm(br->br_perm))
  10937. + continue;
  10938. +
  10939. + bf = br->br_fhsm;
  10940. + mutex_lock(&bf->bf_lock);
  10941. + if (bf->bf_readable) {
  10942. + err = -EFAULT;
  10943. + if (count >= sizeof(*stbr))
  10944. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  10945. + br->br_id);
  10946. + if (!err) {
  10947. + bf->bf_readable = 0;
  10948. + count -= sizeof(*stbr);
  10949. + nstbr++;
  10950. + }
  10951. + }
  10952. + mutex_unlock(&bf->bf_lock);
  10953. + }
  10954. + if (!err)
  10955. + err = sizeof(*stbr) * nstbr;
  10956. +
  10957. + return err;
  10958. +}
  10959. +
  10960. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  10961. + loff_t *pos)
  10962. +{
  10963. + ssize_t err;
  10964. + int readable;
  10965. + aufs_bindex_t nfhsm, bindex, bbot;
  10966. + struct au_sbinfo *sbinfo;
  10967. + struct au_fhsm *fhsm;
  10968. + struct au_branch *br;
  10969. + struct super_block *sb;
  10970. +
  10971. + err = 0;
  10972. + sbinfo = file->private_data;
  10973. + fhsm = &sbinfo->si_fhsm;
  10974. +need_data:
  10975. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  10976. + if (!atomic_read(&fhsm->fhsm_readable)) {
  10977. + if (vfsub_file_flags(file) & O_NONBLOCK)
  10978. + err = -EAGAIN;
  10979. + else
  10980. + err = wait_event_interruptible_locked_irq
  10981. + (fhsm->fhsm_wqh,
  10982. + atomic_read(&fhsm->fhsm_readable));
  10983. + }
  10984. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  10985. + if (unlikely(err))
  10986. + goto out;
  10987. +
  10988. + /* sb may already be dead */
  10989. + au_rw_read_lock(&sbinfo->si_rwsem);
  10990. + readable = atomic_read(&fhsm->fhsm_readable);
  10991. + if (readable > 0) {
  10992. + sb = sbinfo->si_sb;
  10993. + AuDebugOn(!sb);
  10994. + /* exclude the bottom branch */
  10995. + nfhsm = 0;
  10996. + bbot = au_fhsm_bottom(sb);
  10997. + for (bindex = 0; bindex < bbot; bindex++) {
  10998. + br = au_sbr(sb, bindex);
  10999. + if (au_br_fhsm(br->br_perm))
  11000. + nfhsm++;
  11001. + }
  11002. + err = -EMSGSIZE;
  11003. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  11004. + atomic_set(&fhsm->fhsm_readable, 0);
  11005. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  11006. + count);
  11007. + }
  11008. + }
  11009. + au_rw_read_unlock(&sbinfo->si_rwsem);
  11010. + if (!readable)
  11011. + goto need_data;
  11012. +
  11013. +out:
  11014. + return err;
  11015. +}
  11016. +
  11017. +static int au_fhsm_release(struct inode *inode, struct file *file)
  11018. +{
  11019. + struct au_sbinfo *sbinfo;
  11020. + struct au_fhsm *fhsm;
  11021. +
  11022. + /* sb may already be dead */
  11023. + sbinfo = file->private_data;
  11024. + fhsm = &sbinfo->si_fhsm;
  11025. + spin_lock(&fhsm->fhsm_spin);
  11026. + fhsm->fhsm_pid = 0;
  11027. + spin_unlock(&fhsm->fhsm_spin);
  11028. + kobject_put(&sbinfo->si_kobj);
  11029. +
  11030. + return 0;
  11031. +}
  11032. +
  11033. +static const struct file_operations au_fhsm_fops = {
  11034. + .owner = THIS_MODULE,
  11035. + .llseek = noop_llseek,
  11036. + .read = au_fhsm_read,
  11037. + .poll = au_fhsm_poll,
  11038. + .release = au_fhsm_release
  11039. +};
  11040. +
  11041. +int au_fhsm_fd(struct super_block *sb, int oflags)
  11042. +{
  11043. + int err, fd;
  11044. + struct au_sbinfo *sbinfo;
  11045. + struct au_fhsm *fhsm;
  11046. +
  11047. + err = -EPERM;
  11048. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  11049. + goto out;
  11050. +
  11051. + err = -EINVAL;
  11052. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  11053. + goto out;
  11054. +
  11055. + err = 0;
  11056. + sbinfo = au_sbi(sb);
  11057. + fhsm = &sbinfo->si_fhsm;
  11058. + spin_lock(&fhsm->fhsm_spin);
  11059. + if (!fhsm->fhsm_pid)
  11060. + fhsm->fhsm_pid = current->pid;
  11061. + else
  11062. + err = -EBUSY;
  11063. + spin_unlock(&fhsm->fhsm_spin);
  11064. + if (unlikely(err))
  11065. + goto out;
  11066. +
  11067. + oflags |= O_RDONLY;
  11068. + /* oflags |= FMODE_NONOTIFY; */
  11069. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  11070. + err = fd;
  11071. + if (unlikely(fd < 0))
  11072. + goto out_pid;
  11073. +
  11074. + /* succeed reglardless 'fhsm' status */
  11075. + kobject_get(&sbinfo->si_kobj);
  11076. + si_noflush_read_lock(sb);
  11077. + if (au_ftest_si(sbinfo, FHSM))
  11078. + au_fhsm_wrote_all(sb, /*force*/0);
  11079. + si_read_unlock(sb);
  11080. + goto out; /* success */
  11081. +
  11082. +out_pid:
  11083. + spin_lock(&fhsm->fhsm_spin);
  11084. + fhsm->fhsm_pid = 0;
  11085. + spin_unlock(&fhsm->fhsm_spin);
  11086. +out:
  11087. + AuTraceErr(err);
  11088. + return err;
  11089. +}
  11090. +
  11091. +/* ---------------------------------------------------------------------- */
  11092. +
  11093. +int au_fhsm_br_alloc(struct au_branch *br)
  11094. +{
  11095. + int err;
  11096. +
  11097. + err = 0;
  11098. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  11099. + if (br->br_fhsm)
  11100. + au_br_fhsm_init(br->br_fhsm);
  11101. + else
  11102. + err = -ENOMEM;
  11103. +
  11104. + return err;
  11105. +}
  11106. +
  11107. +/* ---------------------------------------------------------------------- */
  11108. +
  11109. +void au_fhsm_fin(struct super_block *sb)
  11110. +{
  11111. + au_fhsm_notify(sb, /*val*/-1);
  11112. +}
  11113. +
  11114. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  11115. +{
  11116. + struct au_fhsm *fhsm;
  11117. +
  11118. + fhsm = &sbinfo->si_fhsm;
  11119. + spin_lock_init(&fhsm->fhsm_spin);
  11120. + init_waitqueue_head(&fhsm->fhsm_wqh);
  11121. + atomic_set(&fhsm->fhsm_readable, 0);
  11122. + fhsm->fhsm_expire
  11123. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  11124. + fhsm->fhsm_bottom = -1;
  11125. +}
  11126. +
  11127. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  11128. +{
  11129. + sbinfo->si_fhsm.fhsm_expire
  11130. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  11131. +}
  11132. +
  11133. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  11134. +{
  11135. + unsigned int u;
  11136. +
  11137. + if (!au_ftest_si(sbinfo, FHSM))
  11138. + return;
  11139. +
  11140. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  11141. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  11142. + seq_printf(seq, ",fhsm_sec=%u", u);
  11143. +}
  11144. --- /dev/null
  11145. +++ linux-4.4/fs/aufs/file.c 2016-10-23 11:20:57.635493285 +0300
  11146. @@ -0,0 +1,852 @@
  11147. +/*
  11148. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  11149. + *
  11150. + * This program, aufs is free software; you can redistribute it and/or modify
  11151. + * it under the terms of the GNU General Public License as published by
  11152. + * the Free Software Foundation; either version 2 of the License, or
  11153. + * (at your option) any later version.
  11154. + *
  11155. + * This program is distributed in the hope that it will be useful,
  11156. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11157. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11158. + * GNU General Public License for more details.
  11159. + *
  11160. + * You should have received a copy of the GNU General Public License
  11161. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11162. + */
  11163. +
  11164. +/*
  11165. + * handling file/dir, and address_space operation
  11166. + */
  11167. +
  11168. +#ifdef CONFIG_AUFS_DEBUG
  11169. +#include <linux/migrate.h>
  11170. +#endif
  11171. +#include <linux/pagemap.h>
  11172. +#include "aufs.h"
  11173. +
  11174. +/* drop flags for writing */
  11175. +unsigned int au_file_roflags(unsigned int flags)
  11176. +{
  11177. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  11178. + flags |= O_RDONLY | O_NOATIME;
  11179. + return flags;
  11180. +}
  11181. +
  11182. +/* common functions to regular file and dir */
  11183. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  11184. + struct file *file, int force_wr)
  11185. +{
  11186. + struct file *h_file;
  11187. + struct dentry *h_dentry;
  11188. + struct inode *h_inode;
  11189. + struct super_block *sb;
  11190. + struct au_branch *br;
  11191. + struct path h_path;
  11192. + int err;
  11193. +
  11194. + /* a race condition can happen between open and unlink/rmdir */
  11195. + h_file = ERR_PTR(-ENOENT);
  11196. + h_dentry = au_h_dptr(dentry, bindex);
  11197. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  11198. + goto out;
  11199. + h_inode = d_inode(h_dentry);
  11200. + spin_lock(&h_dentry->d_lock);
  11201. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  11202. + /* || !d_inode(dentry)->i_nlink */
  11203. + ;
  11204. + spin_unlock(&h_dentry->d_lock);
  11205. + if (unlikely(err))
  11206. + goto out;
  11207. +
  11208. + sb = dentry->d_sb;
  11209. + br = au_sbr(sb, bindex);
  11210. + err = au_br_test_oflag(flags, br);
  11211. + h_file = ERR_PTR(err);
  11212. + if (unlikely(err))
  11213. + goto out;
  11214. +
  11215. + /* drop flags for writing */
  11216. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  11217. + if (force_wr && !(flags & O_WRONLY))
  11218. + force_wr = 0;
  11219. + flags = au_file_roflags(flags);
  11220. + if (force_wr) {
  11221. + h_file = ERR_PTR(-EROFS);
  11222. + flags = au_file_roflags(flags);
  11223. + if (unlikely(vfsub_native_ro(h_inode)
  11224. + || IS_APPEND(h_inode)))
  11225. + goto out;
  11226. + flags &= ~O_ACCMODE;
  11227. + flags |= O_WRONLY;
  11228. + }
  11229. + }
  11230. + flags &= ~O_CREAT;
  11231. + au_br_get(br);
  11232. + h_path.dentry = h_dentry;
  11233. + h_path.mnt = au_br_mnt(br);
  11234. + h_file = vfsub_dentry_open(&h_path, flags);
  11235. + if (IS_ERR(h_file))
  11236. + goto out_br;
  11237. +
  11238. + if (flags & __FMODE_EXEC) {
  11239. + err = deny_write_access(h_file);
  11240. + if (unlikely(err)) {
  11241. + fput(h_file);
  11242. + h_file = ERR_PTR(err);
  11243. + goto out_br;
  11244. + }
  11245. + }
  11246. + fsnotify_open(h_file);
  11247. + goto out; /* success */
  11248. +
  11249. +out_br:
  11250. + au_br_put(br);
  11251. +out:
  11252. + return h_file;
  11253. +}
  11254. +
  11255. +static int au_cmoo(struct dentry *dentry)
  11256. +{
  11257. + int err, cmoo;
  11258. + unsigned int udba;
  11259. + struct path h_path;
  11260. + struct au_pin pin;
  11261. + struct au_cp_generic cpg = {
  11262. + .dentry = dentry,
  11263. + .bdst = -1,
  11264. + .bsrc = -1,
  11265. + .len = -1,
  11266. + .pin = &pin,
  11267. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  11268. + };
  11269. + struct inode *delegated;
  11270. + struct super_block *sb;
  11271. + struct au_sbinfo *sbinfo;
  11272. + struct au_fhsm *fhsm;
  11273. + pid_t pid;
  11274. + struct au_branch *br;
  11275. + struct dentry *parent;
  11276. + struct au_hinode *hdir;
  11277. +
  11278. + DiMustWriteLock(dentry);
  11279. + IiMustWriteLock(d_inode(dentry));
  11280. +
  11281. + err = 0;
  11282. + if (IS_ROOT(dentry))
  11283. + goto out;
  11284. + cpg.bsrc = au_dbtop(dentry);
  11285. + if (!cpg.bsrc)
  11286. + goto out;
  11287. +
  11288. + sb = dentry->d_sb;
  11289. + sbinfo = au_sbi(sb);
  11290. + fhsm = &sbinfo->si_fhsm;
  11291. + pid = au_fhsm_pid(fhsm);
  11292. + if (pid
  11293. + && (current->pid == pid
  11294. + || current->real_parent->pid == pid))
  11295. + goto out;
  11296. +
  11297. + br = au_sbr(sb, cpg.bsrc);
  11298. + cmoo = au_br_cmoo(br->br_perm);
  11299. + if (!cmoo)
  11300. + goto out;
  11301. + if (!d_is_reg(dentry))
  11302. + cmoo &= AuBrAttr_COO_ALL;
  11303. + if (!cmoo)
  11304. + goto out;
  11305. +
  11306. + parent = dget_parent(dentry);
  11307. + di_write_lock_parent(parent);
  11308. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  11309. + cpg.bdst = err;
  11310. + if (unlikely(err < 0)) {
  11311. + err = 0; /* there is no upper writable branch */
  11312. + goto out_dgrade;
  11313. + }
  11314. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  11315. +
  11316. + /* do not respect the coo attrib for the target branch */
  11317. + err = au_cpup_dirs(dentry, cpg.bdst);
  11318. + if (unlikely(err))
  11319. + goto out_dgrade;
  11320. +
  11321. + di_downgrade_lock(parent, AuLock_IR);
  11322. + udba = au_opt_udba(sb);
  11323. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  11324. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11325. + if (unlikely(err))
  11326. + goto out_parent;
  11327. +
  11328. + err = au_sio_cpup_simple(&cpg);
  11329. + au_unpin(&pin);
  11330. + if (unlikely(err))
  11331. + goto out_parent;
  11332. + if (!(cmoo & AuBrWAttr_MOO))
  11333. + goto out_parent; /* success */
  11334. +
  11335. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  11336. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11337. + if (unlikely(err))
  11338. + goto out_parent;
  11339. +
  11340. + h_path.mnt = au_br_mnt(br);
  11341. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  11342. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  11343. + delegated = NULL;
  11344. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  11345. + au_unpin(&pin);
  11346. + /* todo: keep h_dentry or not? */
  11347. + if (unlikely(err == -EWOULDBLOCK)) {
  11348. + pr_warn("cannot retry for NFSv4 delegation"
  11349. + " for an internal unlink\n");
  11350. + iput(delegated);
  11351. + }
  11352. + if (unlikely(err)) {
  11353. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  11354. + dentry, err);
  11355. + err = 0;
  11356. + }
  11357. + goto out_parent; /* success */
  11358. +
  11359. +out_dgrade:
  11360. + di_downgrade_lock(parent, AuLock_IR);
  11361. +out_parent:
  11362. + di_read_unlock(parent, AuLock_IR);
  11363. + dput(parent);
  11364. +out:
  11365. + AuTraceErr(err);
  11366. + return err;
  11367. +}
  11368. +
  11369. +int au_do_open(struct file *file, struct au_do_open_args *args)
  11370. +{
  11371. + int err, no_lock = args->no_lock;
  11372. + struct dentry *dentry;
  11373. + struct au_finfo *finfo;
  11374. +
  11375. + if (!no_lock)
  11376. + err = au_finfo_init(file, args->fidir);
  11377. + else {
  11378. + lockdep_off();
  11379. + err = au_finfo_init(file, args->fidir);
  11380. + lockdep_on();
  11381. + }
  11382. + if (unlikely(err))
  11383. + goto out;
  11384. +
  11385. + dentry = file->f_path.dentry;
  11386. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  11387. + if (!no_lock) {
  11388. + di_write_lock_child(dentry);
  11389. + err = au_cmoo(dentry);
  11390. + di_downgrade_lock(dentry, AuLock_IR);
  11391. + if (!err)
  11392. + err = args->open(file, vfsub_file_flags(file), NULL);
  11393. + di_read_unlock(dentry, AuLock_IR);
  11394. + } else {
  11395. + err = au_cmoo(dentry);
  11396. + if (!err)
  11397. + err = args->open(file, vfsub_file_flags(file),
  11398. + args->h_file);
  11399. + if (!err && au_fbtop(file) != au_dbtop(dentry))
  11400. + /*
  11401. + * cmoo happens after h_file was opened.
  11402. + * need to refresh file later.
  11403. + */
  11404. + atomic_dec(&au_fi(file)->fi_generation);
  11405. + }
  11406. +
  11407. + finfo = au_fi(file);
  11408. + if (!err) {
  11409. + finfo->fi_file = file;
  11410. + au_sphl_add(&finfo->fi_hlist,
  11411. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  11412. + }
  11413. + if (!no_lock)
  11414. + fi_write_unlock(file);
  11415. + else {
  11416. + lockdep_off();
  11417. + fi_write_unlock(file);
  11418. + lockdep_on();
  11419. + }
  11420. + if (unlikely(err)) {
  11421. + finfo->fi_hdir = NULL;
  11422. + au_finfo_fin(file, /*atonce*/0);
  11423. + }
  11424. +
  11425. +out:
  11426. + return err;
  11427. +}
  11428. +
  11429. +int au_reopen_nondir(struct file *file)
  11430. +{
  11431. + int err;
  11432. + aufs_bindex_t btop;
  11433. + struct dentry *dentry;
  11434. + struct file *h_file, *h_file_tmp;
  11435. +
  11436. + dentry = file->f_path.dentry;
  11437. + btop = au_dbtop(dentry);
  11438. + h_file_tmp = NULL;
  11439. + if (au_fbtop(file) == btop) {
  11440. + h_file = au_hf_top(file);
  11441. + if (file->f_mode == h_file->f_mode)
  11442. + return 0; /* success */
  11443. + h_file_tmp = h_file;
  11444. + get_file(h_file_tmp);
  11445. + au_set_h_fptr(file, btop, NULL);
  11446. + }
  11447. + AuDebugOn(au_fi(file)->fi_hdir);
  11448. + /*
  11449. + * it can happen
  11450. + * file exists on both of rw and ro
  11451. + * open --> dbtop and fbtop are both 0
  11452. + * prepend a branch as rw, "rw" become ro
  11453. + * remove rw/file
  11454. + * delete the top branch, "rw" becomes rw again
  11455. + * --> dbtop is 1, fbtop is still 0
  11456. + * write --> fbtop is 0 but dbtop is 1
  11457. + */
  11458. + /* AuDebugOn(au_fbtop(file) < btop); */
  11459. +
  11460. + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
  11461. + file, /*force_wr*/0);
  11462. + err = PTR_ERR(h_file);
  11463. + if (IS_ERR(h_file)) {
  11464. + if (h_file_tmp) {
  11465. + au_sbr_get(dentry->d_sb, btop);
  11466. + au_set_h_fptr(file, btop, h_file_tmp);
  11467. + h_file_tmp = NULL;
  11468. + }
  11469. + goto out; /* todo: close all? */
  11470. + }
  11471. +
  11472. + err = 0;
  11473. + au_set_fbtop(file, btop);
  11474. + au_set_h_fptr(file, btop, h_file);
  11475. + au_update_figen(file);
  11476. + /* todo: necessary? */
  11477. + /* file->f_ra = h_file->f_ra; */
  11478. +
  11479. +out:
  11480. + if (h_file_tmp)
  11481. + fput(h_file_tmp);
  11482. + return err;
  11483. +}
  11484. +
  11485. +/* ---------------------------------------------------------------------- */
  11486. +
  11487. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  11488. + struct dentry *hi_wh)
  11489. +{
  11490. + int err;
  11491. + aufs_bindex_t btop;
  11492. + struct au_dinfo *dinfo;
  11493. + struct dentry *h_dentry;
  11494. + struct au_hdentry *hdp;
  11495. +
  11496. + dinfo = au_di(file->f_path.dentry);
  11497. + AuRwMustWriteLock(&dinfo->di_rwsem);
  11498. +
  11499. + btop = dinfo->di_btop;
  11500. + dinfo->di_btop = btgt;
  11501. + hdp = au_hdentry(dinfo, btgt);
  11502. + h_dentry = hdp->hd_dentry;
  11503. + hdp->hd_dentry = hi_wh;
  11504. + err = au_reopen_nondir(file);
  11505. + hdp->hd_dentry = h_dentry;
  11506. + dinfo->di_btop = btop;
  11507. +
  11508. + return err;
  11509. +}
  11510. +
  11511. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  11512. + aufs_bindex_t bcpup, struct au_pin *pin)
  11513. +{
  11514. + int err;
  11515. + struct inode *inode, *h_inode;
  11516. + struct dentry *h_dentry, *hi_wh;
  11517. + struct au_cp_generic cpg = {
  11518. + .dentry = file->f_path.dentry,
  11519. + .bdst = bcpup,
  11520. + .bsrc = -1,
  11521. + .len = len,
  11522. + .pin = pin
  11523. + };
  11524. +
  11525. + au_update_dbtop(cpg.dentry);
  11526. + inode = d_inode(cpg.dentry);
  11527. + h_inode = NULL;
  11528. + if (au_dbtop(cpg.dentry) <= bcpup
  11529. + && au_dbbot(cpg.dentry) >= bcpup) {
  11530. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  11531. + if (h_dentry && d_is_positive(h_dentry))
  11532. + h_inode = d_inode(h_dentry);
  11533. + }
  11534. + hi_wh = au_hi_wh(inode, bcpup);
  11535. + if (!hi_wh && !h_inode)
  11536. + err = au_sio_cpup_wh(&cpg, file);
  11537. + else
  11538. + /* already copied-up after unlink */
  11539. + err = au_reopen_wh(file, bcpup, hi_wh);
  11540. +
  11541. + if (!err
  11542. + && (inode->i_nlink > 1
  11543. + || (inode->i_state & I_LINKABLE))
  11544. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  11545. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  11546. +
  11547. + return err;
  11548. +}
  11549. +
  11550. +/*
  11551. + * prepare the @file for writing.
  11552. + */
  11553. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  11554. +{
  11555. + int err;
  11556. + aufs_bindex_t dbtop;
  11557. + struct dentry *parent;
  11558. + struct inode *inode;
  11559. + struct super_block *sb;
  11560. + struct file *h_file;
  11561. + struct au_cp_generic cpg = {
  11562. + .dentry = file->f_path.dentry,
  11563. + .bdst = -1,
  11564. + .bsrc = -1,
  11565. + .len = len,
  11566. + .pin = pin,
  11567. + .flags = AuCpup_DTIME
  11568. + };
  11569. +
  11570. + sb = cpg.dentry->d_sb;
  11571. + inode = d_inode(cpg.dentry);
  11572. + cpg.bsrc = au_fbtop(file);
  11573. + err = au_test_ro(sb, cpg.bsrc, inode);
  11574. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  11575. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  11576. + /*flags*/0);
  11577. + goto out;
  11578. + }
  11579. +
  11580. + /* need to cpup or reopen */
  11581. + parent = dget_parent(cpg.dentry);
  11582. + di_write_lock_parent(parent);
  11583. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11584. + cpg.bdst = err;
  11585. + if (unlikely(err < 0))
  11586. + goto out_dgrade;
  11587. + err = 0;
  11588. +
  11589. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  11590. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  11591. + if (unlikely(err))
  11592. + goto out_dgrade;
  11593. + }
  11594. +
  11595. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11596. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11597. + if (unlikely(err))
  11598. + goto out_dgrade;
  11599. +
  11600. + dbtop = au_dbtop(cpg.dentry);
  11601. + if (dbtop <= cpg.bdst)
  11602. + cpg.bsrc = cpg.bdst;
  11603. +
  11604. + if (dbtop <= cpg.bdst /* just reopen */
  11605. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  11606. + ) {
  11607. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  11608. + if (IS_ERR(h_file))
  11609. + err = PTR_ERR(h_file);
  11610. + else {
  11611. + di_downgrade_lock(parent, AuLock_IR);
  11612. + if (dbtop > cpg.bdst)
  11613. + err = au_sio_cpup_simple(&cpg);
  11614. + if (!err)
  11615. + err = au_reopen_nondir(file);
  11616. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  11617. + }
  11618. + } else { /* copyup as wh and reopen */
  11619. + /*
  11620. + * since writable hfsplus branch is not supported,
  11621. + * h_open_pre/post() are unnecessary.
  11622. + */
  11623. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  11624. + di_downgrade_lock(parent, AuLock_IR);
  11625. + }
  11626. +
  11627. + if (!err) {
  11628. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  11629. + goto out_dput; /* success */
  11630. + }
  11631. + au_unpin(pin);
  11632. + goto out_unlock;
  11633. +
  11634. +out_dgrade:
  11635. + di_downgrade_lock(parent, AuLock_IR);
  11636. +out_unlock:
  11637. + di_read_unlock(parent, AuLock_IR);
  11638. +out_dput:
  11639. + dput(parent);
  11640. +out:
  11641. + return err;
  11642. +}
  11643. +
  11644. +/* ---------------------------------------------------------------------- */
  11645. +
  11646. +int au_do_flush(struct file *file, fl_owner_t id,
  11647. + int (*flush)(struct file *file, fl_owner_t id))
  11648. +{
  11649. + int err;
  11650. + struct super_block *sb;
  11651. + struct inode *inode;
  11652. +
  11653. + inode = file_inode(file);
  11654. + sb = inode->i_sb;
  11655. + si_noflush_read_lock(sb);
  11656. + fi_read_lock(file);
  11657. + ii_read_lock_child(inode);
  11658. +
  11659. + err = flush(file, id);
  11660. + au_cpup_attr_timesizes(inode);
  11661. +
  11662. + ii_read_unlock(inode);
  11663. + fi_read_unlock(file);
  11664. + si_read_unlock(sb);
  11665. + return err;
  11666. +}
  11667. +
  11668. +/* ---------------------------------------------------------------------- */
  11669. +
  11670. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  11671. +{
  11672. + int err;
  11673. + struct au_pin pin;
  11674. + struct au_finfo *finfo;
  11675. + struct dentry *parent, *hi_wh;
  11676. + struct inode *inode;
  11677. + struct super_block *sb;
  11678. + struct au_cp_generic cpg = {
  11679. + .dentry = file->f_path.dentry,
  11680. + .bdst = -1,
  11681. + .bsrc = -1,
  11682. + .len = -1,
  11683. + .pin = &pin,
  11684. + .flags = AuCpup_DTIME
  11685. + };
  11686. +
  11687. + FiMustWriteLock(file);
  11688. +
  11689. + err = 0;
  11690. + finfo = au_fi(file);
  11691. + sb = cpg.dentry->d_sb;
  11692. + inode = d_inode(cpg.dentry);
  11693. + cpg.bdst = au_ibtop(inode);
  11694. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  11695. + goto out;
  11696. +
  11697. + parent = dget_parent(cpg.dentry);
  11698. + if (au_test_ro(sb, cpg.bdst, inode)) {
  11699. + di_read_lock_parent(parent, !AuLock_IR);
  11700. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11701. + cpg.bdst = err;
  11702. + di_read_unlock(parent, !AuLock_IR);
  11703. + if (unlikely(err < 0))
  11704. + goto out_parent;
  11705. + err = 0;
  11706. + }
  11707. +
  11708. + di_read_lock_parent(parent, AuLock_IR);
  11709. + hi_wh = au_hi_wh(inode, cpg.bdst);
  11710. + if (!S_ISDIR(inode->i_mode)
  11711. + && au_opt_test(au_mntflags(sb), PLINK)
  11712. + && au_plink_test(inode)
  11713. + && !d_unhashed(cpg.dentry)
  11714. + && cpg.bdst < au_dbtop(cpg.dentry)) {
  11715. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  11716. + if (unlikely(err))
  11717. + goto out_unlock;
  11718. +
  11719. + /* always superio. */
  11720. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11721. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11722. + if (!err) {
  11723. + err = au_sio_cpup_simple(&cpg);
  11724. + au_unpin(&pin);
  11725. + }
  11726. + } else if (hi_wh) {
  11727. + /* already copied-up after unlink */
  11728. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  11729. + *need_reopen = 0;
  11730. + }
  11731. +
  11732. +out_unlock:
  11733. + di_read_unlock(parent, AuLock_IR);
  11734. +out_parent:
  11735. + dput(parent);
  11736. +out:
  11737. + return err;
  11738. +}
  11739. +
  11740. +static void au_do_refresh_dir(struct file *file)
  11741. +{
  11742. + int execed;
  11743. + aufs_bindex_t bindex, bbot, new_bindex, brid;
  11744. + struct au_hfile *p, tmp, *q;
  11745. + struct au_finfo *finfo;
  11746. + struct super_block *sb;
  11747. + struct au_fidir *fidir;
  11748. +
  11749. + FiMustWriteLock(file);
  11750. +
  11751. + sb = file->f_path.dentry->d_sb;
  11752. + finfo = au_fi(file);
  11753. + fidir = finfo->fi_hdir;
  11754. + AuDebugOn(!fidir);
  11755. + p = fidir->fd_hfile + finfo->fi_btop;
  11756. + brid = p->hf_br->br_id;
  11757. + bbot = fidir->fd_bbot;
  11758. + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
  11759. + if (!p->hf_file)
  11760. + continue;
  11761. +
  11762. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  11763. + if (new_bindex == bindex)
  11764. + continue;
  11765. + if (new_bindex < 0) {
  11766. + au_set_h_fptr(file, bindex, NULL);
  11767. + continue;
  11768. + }
  11769. +
  11770. + /* swap two lower inode, and loop again */
  11771. + q = fidir->fd_hfile + new_bindex;
  11772. + tmp = *q;
  11773. + *q = *p;
  11774. + *p = tmp;
  11775. + if (tmp.hf_file) {
  11776. + bindex--;
  11777. + p--;
  11778. + }
  11779. + }
  11780. +
  11781. + execed = vfsub_file_execed(file);
  11782. + p = fidir->fd_hfile;
  11783. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  11784. + bbot = au_sbbot(sb);
  11785. + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
  11786. + finfo->fi_btop++, p++)
  11787. + if (p->hf_file) {
  11788. + if (file_inode(p->hf_file))
  11789. + break;
  11790. + au_hfput(p, execed);
  11791. + }
  11792. + } else {
  11793. + bbot = au_br_index(sb, brid);
  11794. + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
  11795. + finfo->fi_btop++, p++)
  11796. + if (p->hf_file)
  11797. + au_hfput(p, execed);
  11798. + bbot = au_sbbot(sb);
  11799. + }
  11800. +
  11801. + p = fidir->fd_hfile + bbot;
  11802. + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
  11803. + fidir->fd_bbot--, p--)
  11804. + if (p->hf_file) {
  11805. + if (file_inode(p->hf_file))
  11806. + break;
  11807. + au_hfput(p, execed);
  11808. + }
  11809. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  11810. +}
  11811. +
  11812. +/*
  11813. + * after branch manipulating, refresh the file.
  11814. + */
  11815. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  11816. +{
  11817. + int err, need_reopen, nbr;
  11818. + aufs_bindex_t bbot, bindex;
  11819. + struct dentry *dentry;
  11820. + struct super_block *sb;
  11821. + struct au_finfo *finfo;
  11822. + struct au_hfile *hfile;
  11823. +
  11824. + dentry = file->f_path.dentry;
  11825. + sb = dentry->d_sb;
  11826. + nbr = au_sbbot(sb) + 1;
  11827. + finfo = au_fi(file);
  11828. + if (!finfo->fi_hdir) {
  11829. + hfile = &finfo->fi_htop;
  11830. + AuDebugOn(!hfile->hf_file);
  11831. + bindex = au_br_index(sb, hfile->hf_br->br_id);
  11832. + AuDebugOn(bindex < 0);
  11833. + if (bindex != finfo->fi_btop)
  11834. + au_set_fbtop(file, bindex);
  11835. + } else {
  11836. + err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
  11837. + if (unlikely(err))
  11838. + goto out;
  11839. + au_do_refresh_dir(file);
  11840. + }
  11841. +
  11842. + err = 0;
  11843. + need_reopen = 1;
  11844. + if (!au_test_mmapped(file))
  11845. + err = au_file_refresh_by_inode(file, &need_reopen);
  11846. + if (finfo->fi_hdir)
  11847. + /* harmless if err */
  11848. + au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
  11849. + if (!err && need_reopen && !d_unlinked(dentry))
  11850. + err = reopen(file);
  11851. + if (!err) {
  11852. + au_update_figen(file);
  11853. + goto out; /* success */
  11854. + }
  11855. +
  11856. + /* error, close all lower files */
  11857. + if (finfo->fi_hdir) {
  11858. + bbot = au_fbbot_dir(file);
  11859. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
  11860. + au_set_h_fptr(file, bindex, NULL);
  11861. + }
  11862. +
  11863. +out:
  11864. + return err;
  11865. +}
  11866. +
  11867. +/* common function to regular file and dir */
  11868. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  11869. + int wlock)
  11870. +{
  11871. + int err;
  11872. + unsigned int sigen, figen;
  11873. + aufs_bindex_t btop;
  11874. + unsigned char pseudo_link;
  11875. + struct dentry *dentry;
  11876. + struct inode *inode;
  11877. +
  11878. + err = 0;
  11879. + dentry = file->f_path.dentry;
  11880. + inode = d_inode(dentry);
  11881. + sigen = au_sigen(dentry->d_sb);
  11882. + fi_write_lock(file);
  11883. + figen = au_figen(file);
  11884. + di_write_lock_child(dentry);
  11885. + btop = au_dbtop(dentry);
  11886. + pseudo_link = (btop != au_ibtop(inode));
  11887. + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
  11888. + if (!wlock) {
  11889. + di_downgrade_lock(dentry, AuLock_IR);
  11890. + fi_downgrade_lock(file);
  11891. + }
  11892. + goto out; /* success */
  11893. + }
  11894. +
  11895. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  11896. + if (au_digen_test(dentry, sigen)) {
  11897. + err = au_reval_dpath(dentry, sigen);
  11898. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  11899. + }
  11900. +
  11901. + if (!err)
  11902. + err = refresh_file(file, reopen);
  11903. + if (!err) {
  11904. + if (!wlock) {
  11905. + di_downgrade_lock(dentry, AuLock_IR);
  11906. + fi_downgrade_lock(file);
  11907. + }
  11908. + } else {
  11909. + di_write_unlock(dentry);
  11910. + fi_write_unlock(file);
  11911. + }
  11912. +
  11913. +out:
  11914. + return err;
  11915. +}
  11916. +
  11917. +/* ---------------------------------------------------------------------- */
  11918. +
  11919. +/* cf. aufs_nopage() */
  11920. +/* for madvise(2) */
  11921. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  11922. +{
  11923. + unlock_page(page);
  11924. + return 0;
  11925. +}
  11926. +
  11927. +/* it will never be called, but necessary to support O_DIRECT */
  11928. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  11929. + loff_t offset)
  11930. +{ BUG(); return 0; }
  11931. +
  11932. +/* they will never be called. */
  11933. +#ifdef CONFIG_AUFS_DEBUG
  11934. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  11935. + loff_t pos, unsigned len, unsigned flags,
  11936. + struct page **pagep, void **fsdata)
  11937. +{ AuUnsupport(); return 0; }
  11938. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  11939. + loff_t pos, unsigned len, unsigned copied,
  11940. + struct page *page, void *fsdata)
  11941. +{ AuUnsupport(); return 0; }
  11942. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  11943. +{ AuUnsupport(); return 0; }
  11944. +
  11945. +static int aufs_set_page_dirty(struct page *page)
  11946. +{ AuUnsupport(); return 0; }
  11947. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  11948. + unsigned int length)
  11949. +{ AuUnsupport(); }
  11950. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  11951. +{ AuUnsupport(); return 0; }
  11952. +#if 0 /* called by memory compaction regardless file */
  11953. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  11954. + struct page *page, enum migrate_mode mode)
  11955. +{ AuUnsupport(); return 0; }
  11956. +#endif
  11957. +static int aufs_launder_page(struct page *page)
  11958. +{ AuUnsupport(); return 0; }
  11959. +static int aufs_is_partially_uptodate(struct page *page,
  11960. + unsigned long from,
  11961. + unsigned long count)
  11962. +{ AuUnsupport(); return 0; }
  11963. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  11964. + bool *writeback)
  11965. +{ AuUnsupport(); }
  11966. +static int aufs_error_remove_page(struct address_space *mapping,
  11967. + struct page *page)
  11968. +{ AuUnsupport(); return 0; }
  11969. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  11970. + sector_t *span)
  11971. +{ AuUnsupport(); return 0; }
  11972. +static void aufs_swap_deactivate(struct file *file)
  11973. +{ AuUnsupport(); }
  11974. +#endif /* CONFIG_AUFS_DEBUG */
  11975. +
  11976. +const struct address_space_operations aufs_aop = {
  11977. + .readpage = aufs_readpage,
  11978. + .direct_IO = aufs_direct_IO,
  11979. +#ifdef CONFIG_AUFS_DEBUG
  11980. + .writepage = aufs_writepage,
  11981. + /* no writepages, because of writepage */
  11982. + .set_page_dirty = aufs_set_page_dirty,
  11983. + /* no readpages, because of readpage */
  11984. + .write_begin = aufs_write_begin,
  11985. + .write_end = aufs_write_end,
  11986. + /* no bmap, no block device */
  11987. + .invalidatepage = aufs_invalidatepage,
  11988. + .releasepage = aufs_releasepage,
  11989. + /* is fallback_migrate_page ok? */
  11990. + /* .migratepage = aufs_migratepage, */
  11991. + .launder_page = aufs_launder_page,
  11992. + .is_partially_uptodate = aufs_is_partially_uptodate,
  11993. + .is_dirty_writeback = aufs_is_dirty_writeback,
  11994. + .error_remove_page = aufs_error_remove_page,
  11995. + .swap_activate = aufs_swap_activate,
  11996. + .swap_deactivate = aufs_swap_deactivate
  11997. +#endif /* CONFIG_AUFS_DEBUG */
  11998. +};
  11999. --- /dev/null
  12000. +++ linux-4.4/fs/aufs/file.h 2016-10-23 11:20:57.635493285 +0300
  12001. @@ -0,0 +1,294 @@
  12002. +/*
  12003. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  12004. + *
  12005. + * This program, aufs is free software; you can redistribute it and/or modify
  12006. + * it under the terms of the GNU General Public License as published by
  12007. + * the Free Software Foundation; either version 2 of the License, or
  12008. + * (at your option) any later version.
  12009. + *
  12010. + * This program is distributed in the hope that it will be useful,
  12011. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12012. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12013. + * GNU General Public License for more details.
  12014. + *
  12015. + * You should have received a copy of the GNU General Public License
  12016. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12017. + */
  12018. +
  12019. +/*
  12020. + * file operations
  12021. + */
  12022. +
  12023. +#ifndef __AUFS_FILE_H__
  12024. +#define __AUFS_FILE_H__
  12025. +
  12026. +#ifdef __KERNEL__
  12027. +
  12028. +#include <linux/file.h>
  12029. +#include <linux/fs.h>
  12030. +#include <linux/poll.h>
  12031. +#include "rwsem.h"
  12032. +
  12033. +struct au_branch;
  12034. +struct au_hfile {
  12035. + struct file *hf_file;
  12036. + struct au_branch *hf_br;
  12037. +};
  12038. +
  12039. +struct au_vdir;
  12040. +struct au_fidir {
  12041. + aufs_bindex_t fd_bbot;
  12042. + aufs_bindex_t fd_nent;
  12043. + struct au_vdir *fd_vdir_cache;
  12044. + struct au_hfile fd_hfile[];
  12045. +};
  12046. +
  12047. +static inline int au_fidir_sz(int nent)
  12048. +{
  12049. + AuDebugOn(nent < 0);
  12050. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  12051. +}
  12052. +
  12053. +struct au_finfo {
  12054. + atomic_t fi_generation;
  12055. +
  12056. + struct au_rwsem fi_rwsem;
  12057. + aufs_bindex_t fi_btop;
  12058. +
  12059. + /* do not union them */
  12060. + struct { /* for non-dir */
  12061. + struct au_hfile fi_htop;
  12062. + atomic_t fi_mmapped;
  12063. + };
  12064. + struct au_fidir *fi_hdir; /* for dir only */
  12065. +
  12066. + struct hlist_node fi_hlist;
  12067. + union {
  12068. + struct file *fi_file; /* very ugly */
  12069. + struct llist_node fi_lnode; /* delayed free */
  12070. + };
  12071. +} ____cacheline_aligned_in_smp;
  12072. +
  12073. +/* ---------------------------------------------------------------------- */
  12074. +
  12075. +/* file.c */
  12076. +extern const struct address_space_operations aufs_aop;
  12077. +unsigned int au_file_roflags(unsigned int flags);
  12078. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  12079. + struct file *file, int force_wr);
  12080. +struct au_do_open_args {
  12081. + int no_lock;
  12082. + int (*open)(struct file *file, int flags,
  12083. + struct file *h_file);
  12084. + struct au_fidir *fidir;
  12085. + struct file *h_file;
  12086. +};
  12087. +int au_do_open(struct file *file, struct au_do_open_args *args);
  12088. +int au_reopen_nondir(struct file *file);
  12089. +struct au_pin;
  12090. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  12091. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  12092. + int wlock);
  12093. +int au_do_flush(struct file *file, fl_owner_t id,
  12094. + int (*flush)(struct file *file, fl_owner_t id));
  12095. +
  12096. +/* poll.c */
  12097. +#ifdef CONFIG_AUFS_POLL
  12098. +unsigned int aufs_poll(struct file *file, poll_table *wait);
  12099. +#endif
  12100. +
  12101. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  12102. +/* hfsplus.c */
  12103. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  12104. + int force_wr);
  12105. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  12106. + struct file *h_file);
  12107. +#else
  12108. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  12109. + aufs_bindex_t bindex, int force_wr)
  12110. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  12111. + struct file *h_file);
  12112. +#endif
  12113. +
  12114. +/* f_op.c */
  12115. +extern const struct file_operations aufs_file_fop;
  12116. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  12117. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  12118. +struct file *au_read_pre(struct file *file, int keep_fi);
  12119. +
  12120. +/* finfo.c */
  12121. +void au_hfput(struct au_hfile *hf, int execed);
  12122. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  12123. + struct file *h_file);
  12124. +
  12125. +void au_update_figen(struct file *file);
  12126. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  12127. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
  12128. +
  12129. +void au_fi_init_once(void *_fi);
  12130. +void au_finfo_fin(struct file *file, int atonce);
  12131. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  12132. +
  12133. +/* ioctl.c */
  12134. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  12135. +#ifdef CONFIG_COMPAT
  12136. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  12137. + unsigned long arg);
  12138. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  12139. + unsigned long arg);
  12140. +#endif
  12141. +
  12142. +/* ---------------------------------------------------------------------- */
  12143. +
  12144. +static inline struct au_finfo *au_fi(struct file *file)
  12145. +{
  12146. + return file->private_data;
  12147. +}
  12148. +
  12149. +/* ---------------------------------------------------------------------- */
  12150. +
  12151. +/*
  12152. + * fi_read_lock, fi_write_lock,
  12153. + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
  12154. + */
  12155. +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
  12156. +
  12157. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  12158. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  12159. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  12160. +
  12161. +/* ---------------------------------------------------------------------- */
  12162. +
  12163. +/* todo: hard/soft set? */
  12164. +static inline aufs_bindex_t au_fbtop(struct file *file)
  12165. +{
  12166. + FiMustAnyLock(file);
  12167. + return au_fi(file)->fi_btop;
  12168. +}
  12169. +
  12170. +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
  12171. +{
  12172. + FiMustAnyLock(file);
  12173. + AuDebugOn(!au_fi(file)->fi_hdir);
  12174. + return au_fi(file)->fi_hdir->fd_bbot;
  12175. +}
  12176. +
  12177. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  12178. +{
  12179. + FiMustAnyLock(file);
  12180. + AuDebugOn(!au_fi(file)->fi_hdir);
  12181. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  12182. +}
  12183. +
  12184. +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
  12185. +{
  12186. + FiMustWriteLock(file);
  12187. + au_fi(file)->fi_btop = bindex;
  12188. +}
  12189. +
  12190. +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
  12191. +{
  12192. + FiMustWriteLock(file);
  12193. + AuDebugOn(!au_fi(file)->fi_hdir);
  12194. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  12195. +}
  12196. +
  12197. +static inline void au_set_fvdir_cache(struct file *file,
  12198. + struct au_vdir *vdir_cache)
  12199. +{
  12200. + FiMustWriteLock(file);
  12201. + AuDebugOn(!au_fi(file)->fi_hdir);
  12202. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  12203. +}
  12204. +
  12205. +static inline struct file *au_hf_top(struct file *file)
  12206. +{
  12207. + FiMustAnyLock(file);
  12208. + AuDebugOn(au_fi(file)->fi_hdir);
  12209. + return au_fi(file)->fi_htop.hf_file;
  12210. +}
  12211. +
  12212. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  12213. +{
  12214. + FiMustAnyLock(file);
  12215. + AuDebugOn(!au_fi(file)->fi_hdir);
  12216. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  12217. +}
  12218. +
  12219. +/* todo: memory barrier? */
  12220. +static inline unsigned int au_figen(struct file *f)
  12221. +{
  12222. + return atomic_read(&au_fi(f)->fi_generation);
  12223. +}
  12224. +
  12225. +static inline void au_set_mmapped(struct file *f)
  12226. +{
  12227. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  12228. + return;
  12229. + pr_warn("fi_mmapped wrapped around\n");
  12230. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  12231. + ;
  12232. +}
  12233. +
  12234. +static inline void au_unset_mmapped(struct file *f)
  12235. +{
  12236. + atomic_dec(&au_fi(f)->fi_mmapped);
  12237. +}
  12238. +
  12239. +static inline int au_test_mmapped(struct file *f)
  12240. +{
  12241. + return atomic_read(&au_fi(f)->fi_mmapped);
  12242. +}
  12243. +
  12244. +/* customize vma->vm_file */
  12245. +
  12246. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  12247. + struct file *file)
  12248. +{
  12249. + struct file *f;
  12250. +
  12251. + f = vma->vm_file;
  12252. + get_file(file);
  12253. + vma->vm_file = file;
  12254. + fput(f);
  12255. +}
  12256. +
  12257. +#ifdef CONFIG_MMU
  12258. +#define AuDbgVmRegion(file, vma) do {} while (0)
  12259. +
  12260. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  12261. + struct file *file)
  12262. +{
  12263. + au_do_vm_file_reset(vma, file);
  12264. +}
  12265. +#else
  12266. +#define AuDbgVmRegion(file, vma) \
  12267. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  12268. +
  12269. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  12270. + struct file *file)
  12271. +{
  12272. + struct file *f;
  12273. +
  12274. + au_do_vm_file_reset(vma, file);
  12275. + f = vma->vm_region->vm_file;
  12276. + get_file(file);
  12277. + vma->vm_region->vm_file = file;
  12278. + fput(f);
  12279. +}
  12280. +#endif /* CONFIG_MMU */
  12281. +
  12282. +/* handle vma->vm_prfile */
  12283. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  12284. + struct file *file)
  12285. +{
  12286. + get_file(file);
  12287. + vma->vm_prfile = file;
  12288. +#ifndef CONFIG_MMU
  12289. + get_file(file);
  12290. + vma->vm_region->vm_prfile = file;
  12291. +#endif
  12292. +}
  12293. +
  12294. +#endif /* __KERNEL__ */
  12295. +#endif /* __AUFS_FILE_H__ */
  12296. --- /dev/null
  12297. +++ linux-4.4/fs/aufs/finfo.c 2016-10-23 11:20:57.635493285 +0300
  12298. @@ -0,0 +1,151 @@
  12299. +/*
  12300. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  12301. + *
  12302. + * This program, aufs is free software; you can redistribute it and/or modify
  12303. + * it under the terms of the GNU General Public License as published by
  12304. + * the Free Software Foundation; either version 2 of the License, or
  12305. + * (at your option) any later version.
  12306. + *
  12307. + * This program is distributed in the hope that it will be useful,
  12308. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12309. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12310. + * GNU General Public License for more details.
  12311. + *
  12312. + * You should have received a copy of the GNU General Public License
  12313. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12314. + */
  12315. +
  12316. +/*
  12317. + * file private data
  12318. + */
  12319. +
  12320. +#include "aufs.h"
  12321. +
  12322. +void au_hfput(struct au_hfile *hf, int execed)
  12323. +{
  12324. + if (execed)
  12325. + allow_write_access(hf->hf_file);
  12326. + fput(hf->hf_file);
  12327. + hf->hf_file = NULL;
  12328. + au_br_put(hf->hf_br);
  12329. + hf->hf_br = NULL;
  12330. +}
  12331. +
  12332. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  12333. +{
  12334. + struct au_finfo *finfo = au_fi(file);
  12335. + struct au_hfile *hf;
  12336. + struct au_fidir *fidir;
  12337. +
  12338. + fidir = finfo->fi_hdir;
  12339. + if (!fidir) {
  12340. + AuDebugOn(finfo->fi_btop != bindex);
  12341. + hf = &finfo->fi_htop;
  12342. + } else
  12343. + hf = fidir->fd_hfile + bindex;
  12344. +
  12345. + if (hf && hf->hf_file)
  12346. + au_hfput(hf, vfsub_file_execed(file));
  12347. + if (val) {
  12348. + FiMustWriteLock(file);
  12349. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  12350. + hf->hf_file = val;
  12351. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  12352. + }
  12353. +}
  12354. +
  12355. +void au_update_figen(struct file *file)
  12356. +{
  12357. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  12358. + /* smp_mb(); */ /* atomic_set */
  12359. +}
  12360. +
  12361. +/* ---------------------------------------------------------------------- */
  12362. +
  12363. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  12364. +{
  12365. + struct au_fidir *fidir;
  12366. + int nbr;
  12367. +
  12368. + nbr = au_sbbot(sb) + 1;
  12369. + if (nbr < 2)
  12370. + nbr = 2; /* initial allocate for 2 branches */
  12371. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  12372. + if (fidir) {
  12373. + fidir->fd_bbot = -1;
  12374. + fidir->fd_nent = nbr;
  12375. + }
  12376. +
  12377. + return fidir;
  12378. +}
  12379. +
  12380. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
  12381. +{
  12382. + int err;
  12383. + struct au_fidir *fidir, *p;
  12384. +
  12385. + AuRwMustWriteLock(&finfo->fi_rwsem);
  12386. + fidir = finfo->fi_hdir;
  12387. + AuDebugOn(!fidir);
  12388. +
  12389. + err = -ENOMEM;
  12390. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  12391. + GFP_NOFS, may_shrink);
  12392. + if (p) {
  12393. + p->fd_nent = nbr;
  12394. + finfo->fi_hdir = p;
  12395. + err = 0;
  12396. + }
  12397. +
  12398. + return err;
  12399. +}
  12400. +
  12401. +/* ---------------------------------------------------------------------- */
  12402. +
  12403. +void au_finfo_fin(struct file *file, int atonce)
  12404. +{
  12405. + struct au_finfo *finfo;
  12406. +
  12407. + au_nfiles_dec(file->f_path.dentry->d_sb);
  12408. +
  12409. + finfo = au_fi(file);
  12410. + AuDebugOn(finfo->fi_hdir);
  12411. + AuRwDestroy(&finfo->fi_rwsem);
  12412. + if (!atonce)
  12413. + au_cache_dfree_finfo(finfo);
  12414. + else
  12415. + au_cache_free_finfo(finfo);
  12416. +}
  12417. +
  12418. +void au_fi_init_once(void *_finfo)
  12419. +{
  12420. + struct au_finfo *finfo = _finfo;
  12421. +
  12422. + au_rw_init(&finfo->fi_rwsem);
  12423. +}
  12424. +
  12425. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  12426. +{
  12427. + int err;
  12428. + struct au_finfo *finfo;
  12429. + struct dentry *dentry;
  12430. +
  12431. + err = -ENOMEM;
  12432. + dentry = file->f_path.dentry;
  12433. + finfo = au_cache_alloc_finfo();
  12434. + if (unlikely(!finfo))
  12435. + goto out;
  12436. +
  12437. + err = 0;
  12438. + au_nfiles_inc(dentry->d_sb);
  12439. + au_rw_write_lock(&finfo->fi_rwsem);
  12440. + finfo->fi_btop = -1;
  12441. + finfo->fi_hdir = fidir;
  12442. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  12443. + /* smp_mb(); */ /* atomic_set */
  12444. +
  12445. + file->private_data = finfo;
  12446. +
  12447. +out:
  12448. + return err;
  12449. +}
  12450. --- /dev/null
  12451. +++ linux-4.4/fs/aufs/f_op.c 2016-10-23 11:20:57.632159952 +0300
  12452. @@ -0,0 +1,774 @@
  12453. +/*
  12454. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  12455. + *
  12456. + * This program, aufs is free software; you can redistribute it and/or modify
  12457. + * it under the terms of the GNU General Public License as published by
  12458. + * the Free Software Foundation; either version 2 of the License, or
  12459. + * (at your option) any later version.
  12460. + *
  12461. + * This program is distributed in the hope that it will be useful,
  12462. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12463. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12464. + * GNU General Public License for more details.
  12465. + *
  12466. + * You should have received a copy of the GNU General Public License
  12467. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12468. + */
  12469. +
  12470. +/*
  12471. + * file and vm operations
  12472. + */
  12473. +
  12474. +#include <linux/aio.h>
  12475. +#include <linux/fs_stack.h>
  12476. +#include <linux/mman.h>
  12477. +#include <linux/security.h>
  12478. +#include "aufs.h"
  12479. +
  12480. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  12481. +{
  12482. + int err;
  12483. + aufs_bindex_t bindex;
  12484. + struct dentry *dentry, *h_dentry;
  12485. + struct au_finfo *finfo;
  12486. + struct inode *h_inode;
  12487. +
  12488. + FiMustWriteLock(file);
  12489. +
  12490. + err = 0;
  12491. + dentry = file->f_path.dentry;
  12492. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  12493. + finfo = au_fi(file);
  12494. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  12495. + atomic_set(&finfo->fi_mmapped, 0);
  12496. + bindex = au_dbtop(dentry);
  12497. + if (!h_file) {
  12498. + h_dentry = au_h_dptr(dentry, bindex);
  12499. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  12500. + if (unlikely(err))
  12501. + goto out;
  12502. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  12503. + } else {
  12504. + h_dentry = h_file->f_path.dentry;
  12505. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  12506. + if (unlikely(err))
  12507. + goto out;
  12508. + get_file(h_file);
  12509. + }
  12510. + if (IS_ERR(h_file))
  12511. + err = PTR_ERR(h_file);
  12512. + else {
  12513. + if ((flags & __O_TMPFILE)
  12514. + && !(flags & O_EXCL)) {
  12515. + h_inode = file_inode(h_file);
  12516. + spin_lock(&h_inode->i_lock);
  12517. + h_inode->i_state |= I_LINKABLE;
  12518. + spin_unlock(&h_inode->i_lock);
  12519. + }
  12520. + au_set_fbtop(file, bindex);
  12521. + au_set_h_fptr(file, bindex, h_file);
  12522. + au_update_figen(file);
  12523. + /* todo: necessary? */
  12524. + /* file->f_ra = h_file->f_ra; */
  12525. + }
  12526. +
  12527. +out:
  12528. + return err;
  12529. +}
  12530. +
  12531. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  12532. + struct file *file)
  12533. +{
  12534. + int err;
  12535. + struct super_block *sb;
  12536. + struct au_do_open_args args = {
  12537. + .open = au_do_open_nondir
  12538. + };
  12539. +
  12540. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  12541. + file, vfsub_file_flags(file), file->f_mode);
  12542. +
  12543. + sb = file->f_path.dentry->d_sb;
  12544. + si_read_lock(sb, AuLock_FLUSH);
  12545. + err = au_do_open(file, &args);
  12546. + si_read_unlock(sb);
  12547. + return err;
  12548. +}
  12549. +
  12550. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  12551. +{
  12552. + struct au_finfo *finfo;
  12553. + aufs_bindex_t bindex;
  12554. + int delayed;
  12555. +
  12556. + finfo = au_fi(file);
  12557. + au_sphl_del(&finfo->fi_hlist,
  12558. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  12559. + bindex = finfo->fi_btop;
  12560. + if (bindex >= 0)
  12561. + au_set_h_fptr(file, bindex, NULL);
  12562. +
  12563. + delayed = (current->flags & PF_KTHREAD) || in_interrupt();
  12564. + au_finfo_fin(file, delayed);
  12565. + return 0;
  12566. +}
  12567. +
  12568. +/* ---------------------------------------------------------------------- */
  12569. +
  12570. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  12571. +{
  12572. + int err;
  12573. + struct file *h_file;
  12574. +
  12575. + err = 0;
  12576. + h_file = au_hf_top(file);
  12577. + if (h_file)
  12578. + err = vfsub_flush(h_file, id);
  12579. + return err;
  12580. +}
  12581. +
  12582. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  12583. +{
  12584. + return au_do_flush(file, id, au_do_flush_nondir);
  12585. +}
  12586. +
  12587. +/* ---------------------------------------------------------------------- */
  12588. +/*
  12589. + * read and write functions acquire [fdi]_rwsem once, but release before
  12590. + * mmap_sem. This is because to stop a race condition between mmap(2).
  12591. + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
  12592. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  12593. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  12594. + */
  12595. +
  12596. +/* Callers should call au_read_post() or fput() in the end */
  12597. +struct file *au_read_pre(struct file *file, int keep_fi)
  12598. +{
  12599. + struct file *h_file;
  12600. + int err;
  12601. +
  12602. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  12603. + if (!err) {
  12604. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  12605. + h_file = au_hf_top(file);
  12606. + get_file(h_file);
  12607. + if (!keep_fi)
  12608. + fi_read_unlock(file);
  12609. + } else
  12610. + h_file = ERR_PTR(err);
  12611. +
  12612. + return h_file;
  12613. +}
  12614. +
  12615. +static void au_read_post(struct inode *inode, struct file *h_file)
  12616. +{
  12617. + /* update without lock, I don't think it a problem */
  12618. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  12619. + fput(h_file);
  12620. +}
  12621. +
  12622. +struct au_write_pre {
  12623. + blkcnt_t blks;
  12624. + aufs_bindex_t btop;
  12625. +};
  12626. +
  12627. +/*
  12628. + * return with iinfo is write-locked
  12629. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  12630. + * end
  12631. + */
  12632. +static struct file *au_write_pre(struct file *file, int do_ready,
  12633. + struct au_write_pre *wpre)
  12634. +{
  12635. + struct file *h_file;
  12636. + struct dentry *dentry;
  12637. + int err;
  12638. + struct au_pin pin;
  12639. +
  12640. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12641. + h_file = ERR_PTR(err);
  12642. + if (unlikely(err))
  12643. + goto out;
  12644. +
  12645. + dentry = file->f_path.dentry;
  12646. + if (do_ready) {
  12647. + err = au_ready_to_write(file, -1, &pin);
  12648. + if (unlikely(err)) {
  12649. + h_file = ERR_PTR(err);
  12650. + di_write_unlock(dentry);
  12651. + goto out_fi;
  12652. + }
  12653. + }
  12654. +
  12655. + di_downgrade_lock(dentry, /*flags*/0);
  12656. + if (wpre)
  12657. + wpre->btop = au_fbtop(file);
  12658. + h_file = au_hf_top(file);
  12659. + get_file(h_file);
  12660. + if (wpre)
  12661. + wpre->blks = file_inode(h_file)->i_blocks;
  12662. + if (do_ready)
  12663. + au_unpin(&pin);
  12664. + di_read_unlock(dentry, /*flags*/0);
  12665. +
  12666. +out_fi:
  12667. + fi_write_unlock(file);
  12668. +out:
  12669. + return h_file;
  12670. +}
  12671. +
  12672. +static void au_write_post(struct inode *inode, struct file *h_file,
  12673. + struct au_write_pre *wpre, ssize_t written)
  12674. +{
  12675. + struct inode *h_inode;
  12676. +
  12677. + au_cpup_attr_timesizes(inode);
  12678. + AuDebugOn(au_ibtop(inode) != wpre->btop);
  12679. + h_inode = file_inode(h_file);
  12680. + inode->i_mode = h_inode->i_mode;
  12681. + ii_write_unlock(inode);
  12682. + fput(h_file);
  12683. +
  12684. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  12685. + if (written > 0)
  12686. + au_fhsm_wrote(inode->i_sb, wpre->btop,
  12687. + /*force*/h_inode->i_blocks > wpre->blks);
  12688. +}
  12689. +
  12690. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  12691. + loff_t *ppos)
  12692. +{
  12693. + ssize_t err;
  12694. + struct inode *inode;
  12695. + struct file *h_file;
  12696. + struct super_block *sb;
  12697. +
  12698. + inode = file_inode(file);
  12699. + sb = inode->i_sb;
  12700. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12701. +
  12702. + h_file = au_read_pre(file, /*keep_fi*/0);
  12703. + err = PTR_ERR(h_file);
  12704. + if (IS_ERR(h_file))
  12705. + goto out;
  12706. +
  12707. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  12708. + err = vfsub_read_u(h_file, buf, count, ppos);
  12709. + /* todo: necessary? */
  12710. + /* file->f_ra = h_file->f_ra; */
  12711. + au_read_post(inode, h_file);
  12712. +
  12713. +out:
  12714. + si_read_unlock(sb);
  12715. + return err;
  12716. +}
  12717. +
  12718. +/*
  12719. + * todo: very ugly
  12720. + * it locks both of i_mutex and si_rwsem for read in safe.
  12721. + * if the plink maintenance mode continues forever (that is the problem),
  12722. + * may loop forever.
  12723. + */
  12724. +static void au_mtx_and_read_lock(struct inode *inode)
  12725. +{
  12726. + int err;
  12727. + struct super_block *sb = inode->i_sb;
  12728. +
  12729. + while (1) {
  12730. + mutex_lock(&inode->i_mutex);
  12731. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  12732. + if (!err)
  12733. + break;
  12734. + mutex_unlock(&inode->i_mutex);
  12735. + si_read_lock(sb, AuLock_NOPLMW);
  12736. + si_read_unlock(sb);
  12737. + }
  12738. +}
  12739. +
  12740. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  12741. + size_t count, loff_t *ppos)
  12742. +{
  12743. + ssize_t err;
  12744. + struct au_write_pre wpre;
  12745. + struct inode *inode;
  12746. + struct file *h_file;
  12747. + char __user *buf = (char __user *)ubuf;
  12748. +
  12749. + inode = file_inode(file);
  12750. + au_mtx_and_read_lock(inode);
  12751. +
  12752. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12753. + err = PTR_ERR(h_file);
  12754. + if (IS_ERR(h_file))
  12755. + goto out;
  12756. +
  12757. + err = vfsub_write_u(h_file, buf, count, ppos);
  12758. + au_write_post(inode, h_file, &wpre, err);
  12759. +
  12760. +out:
  12761. + si_read_unlock(inode->i_sb);
  12762. + mutex_unlock(&inode->i_mutex);
  12763. + return err;
  12764. +}
  12765. +
  12766. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  12767. + struct iov_iter *iov_iter)
  12768. +{
  12769. + ssize_t err;
  12770. + struct file *file;
  12771. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  12772. +
  12773. + err = security_file_permission(h_file, rw);
  12774. + if (unlikely(err))
  12775. + goto out;
  12776. +
  12777. + err = -ENOSYS;
  12778. + iter = NULL;
  12779. + if (rw == MAY_READ)
  12780. + iter = h_file->f_op->read_iter;
  12781. + else if (rw == MAY_WRITE)
  12782. + iter = h_file->f_op->write_iter;
  12783. +
  12784. + file = kio->ki_filp;
  12785. + kio->ki_filp = h_file;
  12786. + if (iter) {
  12787. + lockdep_off();
  12788. + err = iter(kio, iov_iter);
  12789. + lockdep_on();
  12790. + } else
  12791. + /* currently there is no such fs */
  12792. + WARN_ON_ONCE(1);
  12793. + kio->ki_filp = file;
  12794. +
  12795. +out:
  12796. + return err;
  12797. +}
  12798. +
  12799. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12800. +{
  12801. + ssize_t err;
  12802. + struct file *file, *h_file;
  12803. + struct inode *inode;
  12804. + struct super_block *sb;
  12805. +
  12806. + file = kio->ki_filp;
  12807. + inode = file_inode(file);
  12808. + sb = inode->i_sb;
  12809. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12810. +
  12811. + h_file = au_read_pre(file, /*keep_fi*/1);
  12812. + err = PTR_ERR(h_file);
  12813. + if (IS_ERR(h_file))
  12814. + goto out;
  12815. +
  12816. + if (au_test_loopback_kthread()) {
  12817. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  12818. + if (file->f_mapping != h_file->f_mapping) {
  12819. + file->f_mapping = h_file->f_mapping;
  12820. + smp_mb(); /* unnecessary? */
  12821. + }
  12822. + }
  12823. + fi_read_unlock(file);
  12824. +
  12825. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  12826. + /* todo: necessary? */
  12827. + /* file->f_ra = h_file->f_ra; */
  12828. + au_read_post(inode, h_file);
  12829. +
  12830. +out:
  12831. + si_read_unlock(sb);
  12832. + return err;
  12833. +}
  12834. +
  12835. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12836. +{
  12837. + ssize_t err;
  12838. + struct au_write_pre wpre;
  12839. + struct inode *inode;
  12840. + struct file *file, *h_file;
  12841. +
  12842. + file = kio->ki_filp;
  12843. + inode = file_inode(file);
  12844. + au_mtx_and_read_lock(inode);
  12845. +
  12846. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12847. + err = PTR_ERR(h_file);
  12848. + if (IS_ERR(h_file))
  12849. + goto out;
  12850. +
  12851. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  12852. + au_write_post(inode, h_file, &wpre, err);
  12853. +
  12854. +out:
  12855. + si_read_unlock(inode->i_sb);
  12856. + mutex_unlock(&inode->i_mutex);
  12857. + return err;
  12858. +}
  12859. +
  12860. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  12861. + struct pipe_inode_info *pipe, size_t len,
  12862. + unsigned int flags)
  12863. +{
  12864. + ssize_t err;
  12865. + struct file *h_file;
  12866. + struct inode *inode;
  12867. + struct super_block *sb;
  12868. +
  12869. + inode = file_inode(file);
  12870. + sb = inode->i_sb;
  12871. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12872. +
  12873. + h_file = au_read_pre(file, /*keep_fi*/0);
  12874. + err = PTR_ERR(h_file);
  12875. + if (IS_ERR(h_file))
  12876. + goto out;
  12877. +
  12878. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  12879. + /* todo: necessasry? */
  12880. + /* file->f_ra = h_file->f_ra; */
  12881. + au_read_post(inode, h_file);
  12882. +
  12883. +out:
  12884. + si_read_unlock(sb);
  12885. + return err;
  12886. +}
  12887. +
  12888. +static ssize_t
  12889. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  12890. + size_t len, unsigned int flags)
  12891. +{
  12892. + ssize_t err;
  12893. + struct au_write_pre wpre;
  12894. + struct inode *inode;
  12895. + struct file *h_file;
  12896. +
  12897. + inode = file_inode(file);
  12898. + au_mtx_and_read_lock(inode);
  12899. +
  12900. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12901. + err = PTR_ERR(h_file);
  12902. + if (IS_ERR(h_file))
  12903. + goto out;
  12904. +
  12905. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  12906. + au_write_post(inode, h_file, &wpre, err);
  12907. +
  12908. +out:
  12909. + si_read_unlock(inode->i_sb);
  12910. + mutex_unlock(&inode->i_mutex);
  12911. + return err;
  12912. +}
  12913. +
  12914. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  12915. + loff_t len)
  12916. +{
  12917. + long err;
  12918. + struct au_write_pre wpre;
  12919. + struct inode *inode;
  12920. + struct file *h_file;
  12921. +
  12922. + inode = file_inode(file);
  12923. + au_mtx_and_read_lock(inode);
  12924. +
  12925. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12926. + err = PTR_ERR(h_file);
  12927. + if (IS_ERR(h_file))
  12928. + goto out;
  12929. +
  12930. + lockdep_off();
  12931. + err = vfs_fallocate(h_file, mode, offset, len);
  12932. + lockdep_on();
  12933. + au_write_post(inode, h_file, &wpre, /*written*/1);
  12934. +
  12935. +out:
  12936. + si_read_unlock(inode->i_sb);
  12937. + mutex_unlock(&inode->i_mutex);
  12938. + return err;
  12939. +}
  12940. +
  12941. +/* ---------------------------------------------------------------------- */
  12942. +
  12943. +/*
  12944. + * The locking order around current->mmap_sem.
  12945. + * - in most and regular cases
  12946. + * file I/O syscall -- aufs_read() or something
  12947. + * -- si_rwsem for read -- mmap_sem
  12948. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  12949. + * - in mmap case
  12950. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  12951. + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
  12952. + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
  12953. + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  12954. + * It means that when aufs acquires si_rwsem for write, the process should never
  12955. + * acquire mmap_sem.
  12956. + *
  12957. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  12958. + * problem either since any directory is not able to be mmap-ed.
  12959. + * The similar scenario is applied to aufs_readlink() too.
  12960. + */
  12961. +
  12962. +#if 0 /* stop calling security_file_mmap() */
  12963. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  12964. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  12965. +
  12966. +static unsigned long au_arch_prot_conv(unsigned long flags)
  12967. +{
  12968. + /* currently ppc64 only */
  12969. +#ifdef CONFIG_PPC64
  12970. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  12971. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  12972. + return AuConv_VM_PROT(flags, SAO);
  12973. +#else
  12974. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  12975. + return 0;
  12976. +#endif
  12977. +}
  12978. +
  12979. +static unsigned long au_prot_conv(unsigned long flags)
  12980. +{
  12981. + return AuConv_VM_PROT(flags, READ)
  12982. + | AuConv_VM_PROT(flags, WRITE)
  12983. + | AuConv_VM_PROT(flags, EXEC)
  12984. + | au_arch_prot_conv(flags);
  12985. +}
  12986. +
  12987. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  12988. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  12989. +
  12990. +static unsigned long au_flag_conv(unsigned long flags)
  12991. +{
  12992. + return AuConv_VM_MAP(flags, GROWSDOWN)
  12993. + | AuConv_VM_MAP(flags, DENYWRITE)
  12994. + | AuConv_VM_MAP(flags, LOCKED);
  12995. +}
  12996. +#endif
  12997. +
  12998. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  12999. +{
  13000. + int err;
  13001. + const unsigned char wlock
  13002. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  13003. + struct super_block *sb;
  13004. + struct file *h_file;
  13005. + struct inode *inode;
  13006. +
  13007. + AuDbgVmRegion(file, vma);
  13008. +
  13009. + inode = file_inode(file);
  13010. + sb = inode->i_sb;
  13011. + lockdep_off();
  13012. + si_read_lock(sb, AuLock_NOPLMW);
  13013. +
  13014. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  13015. + lockdep_on();
  13016. + err = PTR_ERR(h_file);
  13017. + if (IS_ERR(h_file))
  13018. + goto out;
  13019. +
  13020. + err = 0;
  13021. + au_set_mmapped(file);
  13022. + au_vm_file_reset(vma, h_file);
  13023. + /*
  13024. + * we cannot call security_mmap_file() here since it may acquire
  13025. + * mmap_sem or i_mutex.
  13026. + *
  13027. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  13028. + * au_flag_conv(vma->vm_flags));
  13029. + */
  13030. + if (!err)
  13031. + err = h_file->f_op->mmap(h_file, vma);
  13032. + if (!err) {
  13033. + au_vm_prfile_set(vma, file);
  13034. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  13035. + goto out_fput; /* success */
  13036. + }
  13037. + au_unset_mmapped(file);
  13038. + au_vm_file_reset(vma, file);
  13039. +
  13040. +out_fput:
  13041. + lockdep_off();
  13042. + ii_write_unlock(inode);
  13043. + lockdep_on();
  13044. + fput(h_file);
  13045. +out:
  13046. + lockdep_off();
  13047. + si_read_unlock(sb);
  13048. + lockdep_on();
  13049. + AuTraceErr(err);
  13050. + return err;
  13051. +}
  13052. +
  13053. +/* ---------------------------------------------------------------------- */
  13054. +
  13055. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  13056. + int datasync)
  13057. +{
  13058. + int err;
  13059. + struct au_write_pre wpre;
  13060. + struct inode *inode;
  13061. + struct file *h_file;
  13062. +
  13063. + err = 0; /* -EBADF; */ /* posix? */
  13064. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  13065. + goto out;
  13066. +
  13067. + inode = file_inode(file);
  13068. + au_mtx_and_read_lock(inode);
  13069. +
  13070. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  13071. + err = PTR_ERR(h_file);
  13072. + if (IS_ERR(h_file))
  13073. + goto out_unlock;
  13074. +
  13075. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  13076. + au_write_post(inode, h_file, &wpre, /*written*/0);
  13077. +
  13078. +out_unlock:
  13079. + si_read_unlock(inode->i_sb);
  13080. + mutex_unlock(&inode->i_mutex);
  13081. +out:
  13082. + return err;
  13083. +}
  13084. +
  13085. +/* no one supports this operation, currently */
  13086. +#if 0
  13087. +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
  13088. +{
  13089. + int err;
  13090. + struct au_write_pre wpre;
  13091. + struct inode *inode;
  13092. + struct file *file, *h_file;
  13093. +
  13094. + err = 0; /* -EBADF; */ /* posix? */
  13095. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  13096. + goto out;
  13097. +
  13098. + file = kio->ki_filp;
  13099. + inode = file_inode(file);
  13100. + au_mtx_and_read_lock(inode);
  13101. +
  13102. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  13103. + err = PTR_ERR(h_file);
  13104. + if (IS_ERR(h_file))
  13105. + goto out_unlock;
  13106. +
  13107. + err = -ENOSYS;
  13108. + h_file = au_hf_top(file);
  13109. + if (h_file->f_op->aio_fsync) {
  13110. + struct mutex *h_mtx;
  13111. +
  13112. + h_mtx = &file_inode(h_file)->i_mutex;
  13113. + if (!is_sync_kiocb(kio)) {
  13114. + get_file(h_file);
  13115. + fput(file);
  13116. + }
  13117. + kio->ki_filp = h_file;
  13118. + err = h_file->f_op->aio_fsync(kio, datasync);
  13119. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  13120. + if (!err)
  13121. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
  13122. + /*ignore*/
  13123. + mutex_unlock(h_mtx);
  13124. + }
  13125. + au_write_post(inode, h_file, &wpre, /*written*/0);
  13126. +
  13127. +out_unlock:
  13128. + si_read_unlock(inode->sb);
  13129. + mutex_unlock(&inode->i_mutex);
  13130. +out:
  13131. + return err;
  13132. +}
  13133. +#endif
  13134. +
  13135. +static int aufs_fasync(int fd, struct file *file, int flag)
  13136. +{
  13137. + int err;
  13138. + struct file *h_file;
  13139. + struct super_block *sb;
  13140. +
  13141. + sb = file->f_path.dentry->d_sb;
  13142. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  13143. +
  13144. + h_file = au_read_pre(file, /*keep_fi*/0);
  13145. + err = PTR_ERR(h_file);
  13146. + if (IS_ERR(h_file))
  13147. + goto out;
  13148. +
  13149. + if (h_file->f_op->fasync)
  13150. + err = h_file->f_op->fasync(fd, h_file, flag);
  13151. + fput(h_file); /* instead of au_read_post() */
  13152. +
  13153. +out:
  13154. + si_read_unlock(sb);
  13155. + return err;
  13156. +}
  13157. +
  13158. +static int aufs_setfl(struct file *file, unsigned long arg)
  13159. +{
  13160. + int err;
  13161. + struct file *h_file;
  13162. + struct super_block *sb;
  13163. +
  13164. + sb = file->f_path.dentry->d_sb;
  13165. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  13166. +
  13167. + h_file = au_read_pre(file, /*keep_fi*/0);
  13168. + err = PTR_ERR(h_file);
  13169. + if (IS_ERR(h_file))
  13170. + goto out;
  13171. +
  13172. + arg |= vfsub_file_flags(file) & FASYNC; /* stop calling h_file->fasync */
  13173. + err = setfl(/*unused fd*/-1, h_file, arg);
  13174. + fput(h_file); /* instead of au_read_post() */
  13175. +
  13176. +out:
  13177. + si_read_unlock(sb);
  13178. + return err;
  13179. +}
  13180. +
  13181. +/* ---------------------------------------------------------------------- */
  13182. +
  13183. +/* no one supports this operation, currently */
  13184. +#if 0
  13185. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  13186. + size_t len, loff_t *pos, int more)
  13187. +{
  13188. +}
  13189. +#endif
  13190. +
  13191. +/* ---------------------------------------------------------------------- */
  13192. +
  13193. +const struct file_operations aufs_file_fop = {
  13194. + .owner = THIS_MODULE,
  13195. +
  13196. + .llseek = default_llseek,
  13197. +
  13198. + .read = aufs_read,
  13199. + .write = aufs_write,
  13200. + .read_iter = aufs_read_iter,
  13201. + .write_iter = aufs_write_iter,
  13202. +
  13203. +#ifdef CONFIG_AUFS_POLL
  13204. + .poll = aufs_poll,
  13205. +#endif
  13206. + .unlocked_ioctl = aufs_ioctl_nondir,
  13207. +#ifdef CONFIG_COMPAT
  13208. + .compat_ioctl = aufs_compat_ioctl_nondir,
  13209. +#endif
  13210. + .mmap = aufs_mmap,
  13211. + .open = aufs_open_nondir,
  13212. + .flush = aufs_flush_nondir,
  13213. + .release = aufs_release_nondir,
  13214. + .fsync = aufs_fsync_nondir,
  13215. + /* .aio_fsync = aufs_aio_fsync_nondir, */
  13216. + .fasync = aufs_fasync,
  13217. + /* .sendpage = aufs_sendpage, */
  13218. + .setfl = aufs_setfl,
  13219. + .splice_write = aufs_splice_write,
  13220. + .splice_read = aufs_splice_read,
  13221. +#if 0
  13222. + .aio_splice_write = aufs_aio_splice_write,
  13223. + .aio_splice_read = aufs_aio_splice_read,
  13224. +#endif
  13225. + .fallocate = aufs_fallocate
  13226. +};
  13227. --- /dev/null
  13228. +++ linux-4.4/fs/aufs/fstype.h 2016-10-23 11:20:57.635493285 +0300
  13229. @@ -0,0 +1,400 @@
  13230. +/*
  13231. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  13232. + *
  13233. + * This program, aufs is free software; you can redistribute it and/or modify
  13234. + * it under the terms of the GNU General Public License as published by
  13235. + * the Free Software Foundation; either version 2 of the License, or
  13236. + * (at your option) any later version.
  13237. + *
  13238. + * This program is distributed in the hope that it will be useful,
  13239. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13240. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13241. + * GNU General Public License for more details.
  13242. + *
  13243. + * You should have received a copy of the GNU General Public License
  13244. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13245. + */
  13246. +
  13247. +/*
  13248. + * judging filesystem type
  13249. + */
  13250. +
  13251. +#ifndef __AUFS_FSTYPE_H__
  13252. +#define __AUFS_FSTYPE_H__
  13253. +
  13254. +#ifdef __KERNEL__
  13255. +
  13256. +#include <linux/fs.h>
  13257. +#include <linux/magic.h>
  13258. +#include <linux/nfs_fs.h>
  13259. +#include <linux/romfs_fs.h>
  13260. +
  13261. +static inline int au_test_aufs(struct super_block *sb)
  13262. +{
  13263. + return sb->s_magic == AUFS_SUPER_MAGIC;
  13264. +}
  13265. +
  13266. +static inline const char *au_sbtype(struct super_block *sb)
  13267. +{
  13268. + return sb->s_type->name;
  13269. +}
  13270. +
  13271. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  13272. +{
  13273. +#if IS_ENABLED(CONFIG_ISO9660_FS)
  13274. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  13275. +#else
  13276. + return 0;
  13277. +#endif
  13278. +}
  13279. +
  13280. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  13281. +{
  13282. +#if IS_ENABLED(CONFIG_ROMFS_FS)
  13283. + return sb->s_magic == ROMFS_MAGIC;
  13284. +#else
  13285. + return 0;
  13286. +#endif
  13287. +}
  13288. +
  13289. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  13290. +{
  13291. +#if IS_ENABLED(CONFIG_CRAMFS)
  13292. + return sb->s_magic == CRAMFS_MAGIC;
  13293. +#endif
  13294. + return 0;
  13295. +}
  13296. +
  13297. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  13298. +{
  13299. +#if IS_ENABLED(CONFIG_NFS_FS)
  13300. + return sb->s_magic == NFS_SUPER_MAGIC;
  13301. +#else
  13302. + return 0;
  13303. +#endif
  13304. +}
  13305. +
  13306. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  13307. +{
  13308. +#if IS_ENABLED(CONFIG_FUSE_FS)
  13309. + return sb->s_magic == FUSE_SUPER_MAGIC;
  13310. +#else
  13311. + return 0;
  13312. +#endif
  13313. +}
  13314. +
  13315. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  13316. +{
  13317. +#if IS_ENABLED(CONFIG_XFS_FS)
  13318. + return sb->s_magic == XFS_SB_MAGIC;
  13319. +#else
  13320. + return 0;
  13321. +#endif
  13322. +}
  13323. +
  13324. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  13325. +{
  13326. +#ifdef CONFIG_TMPFS
  13327. + return sb->s_magic == TMPFS_MAGIC;
  13328. +#else
  13329. + return 0;
  13330. +#endif
  13331. +}
  13332. +
  13333. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  13334. +{
  13335. +#if IS_ENABLED(CONFIG_ECRYPT_FS)
  13336. + return !strcmp(au_sbtype(sb), "ecryptfs");
  13337. +#else
  13338. + return 0;
  13339. +#endif
  13340. +}
  13341. +
  13342. +static inline int au_test_ramfs(struct super_block *sb)
  13343. +{
  13344. + return sb->s_magic == RAMFS_MAGIC;
  13345. +}
  13346. +
  13347. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  13348. +{
  13349. +#if IS_ENABLED(CONFIG_UBIFS_FS)
  13350. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  13351. +#else
  13352. + return 0;
  13353. +#endif
  13354. +}
  13355. +
  13356. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  13357. +{
  13358. +#ifdef CONFIG_PROC_FS
  13359. + return sb->s_magic == PROC_SUPER_MAGIC;
  13360. +#else
  13361. + return 0;
  13362. +#endif
  13363. +}
  13364. +
  13365. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  13366. +{
  13367. +#ifdef CONFIG_SYSFS
  13368. + return sb->s_magic == SYSFS_MAGIC;
  13369. +#else
  13370. + return 0;
  13371. +#endif
  13372. +}
  13373. +
  13374. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  13375. +{
  13376. +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
  13377. + return sb->s_magic == CONFIGFS_MAGIC;
  13378. +#else
  13379. + return 0;
  13380. +#endif
  13381. +}
  13382. +
  13383. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  13384. +{
  13385. +#if IS_ENABLED(CONFIG_MINIX_FS)
  13386. + return sb->s_magic == MINIX3_SUPER_MAGIC
  13387. + || sb->s_magic == MINIX2_SUPER_MAGIC
  13388. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  13389. + || sb->s_magic == MINIX_SUPER_MAGIC
  13390. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  13391. +#else
  13392. + return 0;
  13393. +#endif
  13394. +}
  13395. +
  13396. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  13397. +{
  13398. +#if IS_ENABLED(CONFIG_FAT_FS)
  13399. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  13400. +#else
  13401. + return 0;
  13402. +#endif
  13403. +}
  13404. +
  13405. +static inline int au_test_msdos(struct super_block *sb)
  13406. +{
  13407. + return au_test_fat(sb);
  13408. +}
  13409. +
  13410. +static inline int au_test_vfat(struct super_block *sb)
  13411. +{
  13412. + return au_test_fat(sb);
  13413. +}
  13414. +
  13415. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  13416. +{
  13417. +#ifdef CONFIG_SECURITYFS
  13418. + return sb->s_magic == SECURITYFS_MAGIC;
  13419. +#else
  13420. + return 0;
  13421. +#endif
  13422. +}
  13423. +
  13424. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  13425. +{
  13426. +#if IS_ENABLED(CONFIG_SQUASHFS)
  13427. + return sb->s_magic == SQUASHFS_MAGIC;
  13428. +#else
  13429. + return 0;
  13430. +#endif
  13431. +}
  13432. +
  13433. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  13434. +{
  13435. +#if IS_ENABLED(CONFIG_BTRFS_FS)
  13436. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  13437. +#else
  13438. + return 0;
  13439. +#endif
  13440. +}
  13441. +
  13442. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  13443. +{
  13444. +#if IS_ENABLED(CONFIG_XENFS)
  13445. + return sb->s_magic == XENFS_SUPER_MAGIC;
  13446. +#else
  13447. + return 0;
  13448. +#endif
  13449. +}
  13450. +
  13451. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  13452. +{
  13453. +#ifdef CONFIG_DEBUG_FS
  13454. + return sb->s_magic == DEBUGFS_MAGIC;
  13455. +#else
  13456. + return 0;
  13457. +#endif
  13458. +}
  13459. +
  13460. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  13461. +{
  13462. +#if IS_ENABLED(CONFIG_NILFS)
  13463. + return sb->s_magic == NILFS_SUPER_MAGIC;
  13464. +#else
  13465. + return 0;
  13466. +#endif
  13467. +}
  13468. +
  13469. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  13470. +{
  13471. +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
  13472. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  13473. +#else
  13474. + return 0;
  13475. +#endif
  13476. +}
  13477. +
  13478. +/* ---------------------------------------------------------------------- */
  13479. +/*
  13480. + * they can't be an aufs branch.
  13481. + */
  13482. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  13483. +{
  13484. + return
  13485. +#ifndef CONFIG_AUFS_BR_RAMFS
  13486. + au_test_ramfs(sb) ||
  13487. +#endif
  13488. + au_test_procfs(sb)
  13489. + || au_test_sysfs(sb)
  13490. + || au_test_configfs(sb)
  13491. + || au_test_debugfs(sb)
  13492. + || au_test_securityfs(sb)
  13493. + || au_test_xenfs(sb)
  13494. + || au_test_ecryptfs(sb)
  13495. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  13496. + || au_test_aufs(sb); /* will be supported in next version */
  13497. +}
  13498. +
  13499. +static inline int au_test_fs_remote(struct super_block *sb)
  13500. +{
  13501. + return !au_test_tmpfs(sb)
  13502. +#ifdef CONFIG_AUFS_BR_RAMFS
  13503. + && !au_test_ramfs(sb)
  13504. +#endif
  13505. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  13506. +}
  13507. +
  13508. +/* ---------------------------------------------------------------------- */
  13509. +
  13510. +/*
  13511. + * Note: these functions (below) are created after reading ->getattr() in all
  13512. + * filesystems under linux/fs. it means we have to do so in every update...
  13513. + */
  13514. +
  13515. +/*
  13516. + * some filesystems require getattr to refresh the inode attributes before
  13517. + * referencing.
  13518. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  13519. + * and leave the work for d_revalidate()
  13520. + */
  13521. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  13522. +{
  13523. + return au_test_nfs(sb)
  13524. + || au_test_fuse(sb)
  13525. + /* || au_test_btrfs(sb) */ /* untested */
  13526. + ;
  13527. +}
  13528. +
  13529. +/*
  13530. + * filesystems which don't maintain i_size or i_blocks.
  13531. + */
  13532. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  13533. +{
  13534. + return au_test_xfs(sb)
  13535. + || au_test_btrfs(sb)
  13536. + || au_test_ubifs(sb)
  13537. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  13538. + /* || au_test_minix(sb) */ /* untested */
  13539. + ;
  13540. +}
  13541. +
  13542. +/*
  13543. + * filesystems which don't store the correct value in some of their inode
  13544. + * attributes.
  13545. + */
  13546. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  13547. +{
  13548. + return au_test_fs_bad_iattr_size(sb)
  13549. + || au_test_fat(sb)
  13550. + || au_test_msdos(sb)
  13551. + || au_test_vfat(sb);
  13552. +}
  13553. +
  13554. +/* they don't check i_nlink in link(2) */
  13555. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  13556. +{
  13557. + return au_test_tmpfs(sb)
  13558. +#ifdef CONFIG_AUFS_BR_RAMFS
  13559. + || au_test_ramfs(sb)
  13560. +#endif
  13561. + || au_test_ubifs(sb)
  13562. + || au_test_hfsplus(sb);
  13563. +}
  13564. +
  13565. +/*
  13566. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  13567. + */
  13568. +static inline int au_test_fs_notime(struct super_block *sb)
  13569. +{
  13570. + return au_test_nfs(sb)
  13571. + || au_test_fuse(sb)
  13572. + || au_test_ubifs(sb)
  13573. + ;
  13574. +}
  13575. +
  13576. +/* temporary support for i#1 in cramfs */
  13577. +static inline int au_test_fs_unique_ino(struct inode *inode)
  13578. +{
  13579. + if (au_test_cramfs(inode->i_sb))
  13580. + return inode->i_ino != 1;
  13581. + return 1;
  13582. +}
  13583. +
  13584. +/* ---------------------------------------------------------------------- */
  13585. +
  13586. +/*
  13587. + * the filesystem where the xino files placed must support i/o after unlink and
  13588. + * maintain i_size and i_blocks.
  13589. + */
  13590. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  13591. +{
  13592. + return au_test_fs_remote(sb)
  13593. + || au_test_fs_bad_iattr_size(sb)
  13594. + /* don't want unnecessary work for xino */
  13595. + || au_test_aufs(sb)
  13596. + || au_test_ecryptfs(sb)
  13597. + || au_test_nilfs(sb);
  13598. +}
  13599. +
  13600. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  13601. +{
  13602. + return au_test_tmpfs(sb)
  13603. + || au_test_ramfs(sb);
  13604. +}
  13605. +
  13606. +/*
  13607. + * test if the @sb is real-readonly.
  13608. + */
  13609. +static inline int au_test_fs_rr(struct super_block *sb)
  13610. +{
  13611. + return au_test_squashfs(sb)
  13612. + || au_test_iso9660(sb)
  13613. + || au_test_cramfs(sb)
  13614. + || au_test_romfs(sb);
  13615. +}
  13616. +
  13617. +/*
  13618. + * test if the @inode is nfs with 'noacl' option
  13619. + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
  13620. + */
  13621. +static inline int au_test_nfs_noacl(struct inode *inode)
  13622. +{
  13623. + return au_test_nfs(inode->i_sb)
  13624. + /* && IS_POSIXACL(inode) */
  13625. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  13626. +}
  13627. +
  13628. +#endif /* __KERNEL__ */
  13629. +#endif /* __AUFS_FSTYPE_H__ */
  13630. --- /dev/null
  13631. +++ linux-4.4/fs/aufs/hfsnotify.c 2016-10-23 11:20:57.635493285 +0300
  13632. @@ -0,0 +1,287 @@
  13633. +/*
  13634. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  13635. + *
  13636. + * This program, aufs is free software; you can redistribute it and/or modify
  13637. + * it under the terms of the GNU General Public License as published by
  13638. + * the Free Software Foundation; either version 2 of the License, or
  13639. + * (at your option) any later version.
  13640. + *
  13641. + * This program is distributed in the hope that it will be useful,
  13642. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13643. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13644. + * GNU General Public License for more details.
  13645. + *
  13646. + * You should have received a copy of the GNU General Public License
  13647. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13648. + */
  13649. +
  13650. +/*
  13651. + * fsnotify for the lower directories
  13652. + */
  13653. +
  13654. +#include "aufs.h"
  13655. +
  13656. +/* FS_IN_IGNORED is unnecessary */
  13657. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  13658. + | FS_CREATE | FS_EVENT_ON_CHILD);
  13659. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  13660. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  13661. +
  13662. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  13663. +{
  13664. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  13665. + hn_mark);
  13666. + /* AuDbg("here\n"); */
  13667. + au_cache_dfree_hnotify(hn);
  13668. + smp_mb__before_atomic();
  13669. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  13670. + wake_up(&au_hfsn_wq);
  13671. +}
  13672. +
  13673. +static int au_hfsn_alloc(struct au_hinode *hinode)
  13674. +{
  13675. + int err;
  13676. + struct au_hnotify *hn;
  13677. + struct super_block *sb;
  13678. + struct au_branch *br;
  13679. + struct fsnotify_mark *mark;
  13680. + aufs_bindex_t bindex;
  13681. +
  13682. + hn = hinode->hi_notify;
  13683. + sb = hn->hn_aufs_inode->i_sb;
  13684. + bindex = au_br_index(sb, hinode->hi_id);
  13685. + br = au_sbr(sb, bindex);
  13686. + AuDebugOn(!br->br_hfsn);
  13687. +
  13688. + mark = &hn->hn_mark;
  13689. + fsnotify_init_mark(mark, au_hfsn_free_mark);
  13690. + mark->mask = AuHfsnMask;
  13691. + /*
  13692. + * by udba rename or rmdir, aufs assign a new inode to the known
  13693. + * h_inode, so specify 1 to allow dups.
  13694. + */
  13695. + lockdep_off();
  13696. + err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
  13697. + /*mnt*/NULL, /*allow_dups*/1);
  13698. + lockdep_on();
  13699. +
  13700. + return err;
  13701. +}
  13702. +
  13703. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  13704. +{
  13705. + struct fsnotify_mark *mark;
  13706. + unsigned long long ull;
  13707. + struct fsnotify_group *group;
  13708. +
  13709. + ull = atomic64_inc_return(&au_hfsn_ifree);
  13710. + BUG_ON(!ull);
  13711. +
  13712. + mark = &hn->hn_mark;
  13713. + spin_lock(&mark->lock);
  13714. + group = mark->group;
  13715. + fsnotify_get_group(group);
  13716. + spin_unlock(&mark->lock);
  13717. + lockdep_off();
  13718. + fsnotify_destroy_mark(mark, group);
  13719. + fsnotify_put_mark(mark);
  13720. + fsnotify_put_group(group);
  13721. + lockdep_on();
  13722. +
  13723. + /* free hn by myself */
  13724. + return 0;
  13725. +}
  13726. +
  13727. +/* ---------------------------------------------------------------------- */
  13728. +
  13729. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  13730. +{
  13731. + struct fsnotify_mark *mark;
  13732. +
  13733. + mark = &hinode->hi_notify->hn_mark;
  13734. + spin_lock(&mark->lock);
  13735. + if (do_set) {
  13736. + AuDebugOn(mark->mask & AuHfsnMask);
  13737. + mark->mask |= AuHfsnMask;
  13738. + } else {
  13739. + AuDebugOn(!(mark->mask & AuHfsnMask));
  13740. + mark->mask &= ~AuHfsnMask;
  13741. + }
  13742. + spin_unlock(&mark->lock);
  13743. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  13744. +}
  13745. +
  13746. +/* ---------------------------------------------------------------------- */
  13747. +
  13748. +/* #define AuDbgHnotify */
  13749. +#ifdef AuDbgHnotify
  13750. +static char *au_hfsn_name(u32 mask)
  13751. +{
  13752. +#ifdef CONFIG_AUFS_DEBUG
  13753. +#define test_ret(flag) \
  13754. + do { \
  13755. + if (mask & flag) \
  13756. + return #flag; \
  13757. + } while (0)
  13758. + test_ret(FS_ACCESS);
  13759. + test_ret(FS_MODIFY);
  13760. + test_ret(FS_ATTRIB);
  13761. + test_ret(FS_CLOSE_WRITE);
  13762. + test_ret(FS_CLOSE_NOWRITE);
  13763. + test_ret(FS_OPEN);
  13764. + test_ret(FS_MOVED_FROM);
  13765. + test_ret(FS_MOVED_TO);
  13766. + test_ret(FS_CREATE);
  13767. + test_ret(FS_DELETE);
  13768. + test_ret(FS_DELETE_SELF);
  13769. + test_ret(FS_MOVE_SELF);
  13770. + test_ret(FS_UNMOUNT);
  13771. + test_ret(FS_Q_OVERFLOW);
  13772. + test_ret(FS_IN_IGNORED);
  13773. + test_ret(FS_ISDIR);
  13774. + test_ret(FS_IN_ONESHOT);
  13775. + test_ret(FS_EVENT_ON_CHILD);
  13776. + return "";
  13777. +#undef test_ret
  13778. +#else
  13779. + return "??";
  13780. +#endif
  13781. +}
  13782. +#endif
  13783. +
  13784. +/* ---------------------------------------------------------------------- */
  13785. +
  13786. +static void au_hfsn_free_group(struct fsnotify_group *group)
  13787. +{
  13788. + struct au_br_hfsnotify *hfsn = group->private;
  13789. +
  13790. + /* AuDbg("here\n"); */
  13791. + au_delayed_kfree(hfsn);
  13792. +}
  13793. +
  13794. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  13795. + struct inode *inode,
  13796. + struct fsnotify_mark *inode_mark,
  13797. + struct fsnotify_mark *vfsmount_mark,
  13798. + u32 mask, void *data, int data_type,
  13799. + const unsigned char *file_name, u32 cookie)
  13800. +{
  13801. + int err;
  13802. + struct au_hnotify *hnotify;
  13803. + struct inode *h_dir, *h_inode;
  13804. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  13805. +
  13806. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  13807. +
  13808. + err = 0;
  13809. + /* if FS_UNMOUNT happens, there must be another bug */
  13810. + AuDebugOn(mask & FS_UNMOUNT);
  13811. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  13812. + goto out;
  13813. +
  13814. + h_dir = inode;
  13815. + h_inode = NULL;
  13816. +#ifdef AuDbgHnotify
  13817. + au_debug_on();
  13818. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  13819. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  13820. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  13821. + h_dir->i_ino, mask, au_hfsn_name(mask),
  13822. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  13823. + /* WARN_ON(1); */
  13824. + }
  13825. + au_debug_off();
  13826. +#endif
  13827. +
  13828. + AuDebugOn(!inode_mark);
  13829. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  13830. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  13831. +
  13832. +out:
  13833. + return err;
  13834. +}
  13835. +
  13836. +static struct fsnotify_ops au_hfsn_ops = {
  13837. + .handle_event = au_hfsn_handle_event,
  13838. + .free_group_priv = au_hfsn_free_group
  13839. +};
  13840. +
  13841. +/* ---------------------------------------------------------------------- */
  13842. +
  13843. +static void au_hfsn_fin_br(struct au_branch *br)
  13844. +{
  13845. + struct au_br_hfsnotify *hfsn;
  13846. +
  13847. + hfsn = br->br_hfsn;
  13848. + if (hfsn) {
  13849. + lockdep_off();
  13850. + fsnotify_put_group(hfsn->hfsn_group);
  13851. + lockdep_on();
  13852. + }
  13853. +}
  13854. +
  13855. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  13856. +{
  13857. + int err;
  13858. + struct fsnotify_group *group;
  13859. + struct au_br_hfsnotify *hfsn;
  13860. +
  13861. + err = 0;
  13862. + br->br_hfsn = NULL;
  13863. + if (!au_br_hnotifyable(perm))
  13864. + goto out;
  13865. +
  13866. + err = -ENOMEM;
  13867. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  13868. + if (unlikely(!hfsn))
  13869. + goto out;
  13870. +
  13871. + err = 0;
  13872. + group = fsnotify_alloc_group(&au_hfsn_ops);
  13873. + if (IS_ERR(group)) {
  13874. + err = PTR_ERR(group);
  13875. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  13876. + goto out_hfsn;
  13877. + }
  13878. +
  13879. + group->private = hfsn;
  13880. + hfsn->hfsn_group = group;
  13881. + br->br_hfsn = hfsn;
  13882. + goto out; /* success */
  13883. +
  13884. +out_hfsn:
  13885. + au_delayed_kfree(hfsn);
  13886. +out:
  13887. + return err;
  13888. +}
  13889. +
  13890. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  13891. +{
  13892. + int err;
  13893. +
  13894. + err = 0;
  13895. + if (!br->br_hfsn)
  13896. + err = au_hfsn_init_br(br, perm);
  13897. +
  13898. + return err;
  13899. +}
  13900. +
  13901. +/* ---------------------------------------------------------------------- */
  13902. +
  13903. +static void au_hfsn_fin(void)
  13904. +{
  13905. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  13906. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  13907. +}
  13908. +
  13909. +const struct au_hnotify_op au_hnotify_op = {
  13910. + .ctl = au_hfsn_ctl,
  13911. + .alloc = au_hfsn_alloc,
  13912. + .free = au_hfsn_free,
  13913. +
  13914. + .fin = au_hfsn_fin,
  13915. +
  13916. + .reset_br = au_hfsn_reset_br,
  13917. + .fin_br = au_hfsn_fin_br,
  13918. + .init_br = au_hfsn_init_br
  13919. +};
  13920. --- /dev/null
  13921. +++ linux-4.4/fs/aufs/hfsplus.c 2016-10-23 11:20:57.635493285 +0300
  13922. @@ -0,0 +1,56 @@
  13923. +/*
  13924. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  13925. + *
  13926. + * This program, aufs is free software; you can redistribute it and/or modify
  13927. + * it under the terms of the GNU General Public License as published by
  13928. + * the Free Software Foundation; either version 2 of the License, or
  13929. + * (at your option) any later version.
  13930. + *
  13931. + * This program is distributed in the hope that it will be useful,
  13932. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13933. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13934. + * GNU General Public License for more details.
  13935. + *
  13936. + * You should have received a copy of the GNU General Public License
  13937. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13938. + */
  13939. +
  13940. +/*
  13941. + * special support for filesystems which aqucires an inode mutex
  13942. + * at final closing a file, eg, hfsplus.
  13943. + *
  13944. + * This trick is very simple and stupid, just to open the file before really
  13945. + * neceeary open to tell hfsplus that this is not the final closing.
  13946. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  13947. + * and au_h_open_post() after releasing it.
  13948. + */
  13949. +
  13950. +#include "aufs.h"
  13951. +
  13952. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  13953. + int force_wr)
  13954. +{
  13955. + struct file *h_file;
  13956. + struct dentry *h_dentry;
  13957. +
  13958. + h_dentry = au_h_dptr(dentry, bindex);
  13959. + AuDebugOn(!h_dentry);
  13960. + AuDebugOn(d_is_negative(h_dentry));
  13961. +
  13962. + h_file = NULL;
  13963. + if (au_test_hfsplus(h_dentry->d_sb)
  13964. + && d_is_reg(h_dentry))
  13965. + h_file = au_h_open(dentry, bindex,
  13966. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  13967. + /*file*/NULL, force_wr);
  13968. + return h_file;
  13969. +}
  13970. +
  13971. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  13972. + struct file *h_file)
  13973. +{
  13974. + if (h_file) {
  13975. + fput(h_file);
  13976. + au_sbr_put(dentry->d_sb, bindex);
  13977. + }
  13978. +}
  13979. --- /dev/null
  13980. +++ linux-4.4/fs/aufs/hnotify.c 2016-10-23 11:20:57.635493285 +0300
  13981. @@ -0,0 +1,723 @@
  13982. +/*
  13983. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  13984. + *
  13985. + * This program, aufs is free software; you can redistribute it and/or modify
  13986. + * it under the terms of the GNU General Public License as published by
  13987. + * the Free Software Foundation; either version 2 of the License, or
  13988. + * (at your option) any later version.
  13989. + *
  13990. + * This program is distributed in the hope that it will be useful,
  13991. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13992. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13993. + * GNU General Public License for more details.
  13994. + *
  13995. + * You should have received a copy of the GNU General Public License
  13996. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13997. + */
  13998. +
  13999. +/*
  14000. + * abstraction to notify the direct changes on lower directories
  14001. + */
  14002. +
  14003. +#include "aufs.h"
  14004. +
  14005. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  14006. +{
  14007. + int err;
  14008. + struct au_hnotify *hn;
  14009. +
  14010. + err = -ENOMEM;
  14011. + hn = au_cache_alloc_hnotify();
  14012. + if (hn) {
  14013. + hn->hn_aufs_inode = inode;
  14014. + hinode->hi_notify = hn;
  14015. + err = au_hnotify_op.alloc(hinode);
  14016. + AuTraceErr(err);
  14017. + if (unlikely(err)) {
  14018. + hinode->hi_notify = NULL;
  14019. + au_cache_dfree_hnotify(hn);
  14020. + /*
  14021. + * The upper dir was removed by udba, but the same named
  14022. + * dir left. In this case, aufs assignes a new inode
  14023. + * number and set the monitor again.
  14024. + * For the lower dir, the old monitnor is still left.
  14025. + */
  14026. + if (err == -EEXIST)
  14027. + err = 0;
  14028. + }
  14029. + }
  14030. +
  14031. + AuTraceErr(err);
  14032. + return err;
  14033. +}
  14034. +
  14035. +void au_hn_free(struct au_hinode *hinode)
  14036. +{
  14037. + struct au_hnotify *hn;
  14038. +
  14039. + hn = hinode->hi_notify;
  14040. + if (hn) {
  14041. + hinode->hi_notify = NULL;
  14042. + if (au_hnotify_op.free(hinode, hn))
  14043. + au_cache_dfree_hnotify(hn);
  14044. + }
  14045. +}
  14046. +
  14047. +/* ---------------------------------------------------------------------- */
  14048. +
  14049. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  14050. +{
  14051. + if (hinode->hi_notify)
  14052. + au_hnotify_op.ctl(hinode, do_set);
  14053. +}
  14054. +
  14055. +void au_hn_reset(struct inode *inode, unsigned int flags)
  14056. +{
  14057. + aufs_bindex_t bindex, bbot;
  14058. + struct inode *hi;
  14059. + struct dentry *iwhdentry;
  14060. +
  14061. + bbot = au_ibbot(inode);
  14062. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  14063. + hi = au_h_iptr(inode, bindex);
  14064. + if (!hi)
  14065. + continue;
  14066. +
  14067. + /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
  14068. + iwhdentry = au_hi_wh(inode, bindex);
  14069. + if (iwhdentry)
  14070. + dget(iwhdentry);
  14071. + au_igrab(hi);
  14072. + au_set_h_iptr(inode, bindex, NULL, 0);
  14073. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  14074. + flags & ~AuHi_XINO);
  14075. + iput(hi);
  14076. + dput(iwhdentry);
  14077. + /* mutex_unlock(&hi->i_mutex); */
  14078. + }
  14079. +}
  14080. +
  14081. +/* ---------------------------------------------------------------------- */
  14082. +
  14083. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  14084. +{
  14085. + int err;
  14086. + aufs_bindex_t bindex, bbot, bfound, btop;
  14087. + struct inode *h_i;
  14088. +
  14089. + err = 0;
  14090. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14091. + pr_warn("branch root dir was changed\n");
  14092. + goto out;
  14093. + }
  14094. +
  14095. + bfound = -1;
  14096. + bbot = au_ibbot(inode);
  14097. + btop = au_ibtop(inode);
  14098. +#if 0 /* reserved for future use */
  14099. + if (bindex == bbot) {
  14100. + /* keep this ino in rename case */
  14101. + goto out;
  14102. + }
  14103. +#endif
  14104. + for (bindex = btop; bindex <= bbot; bindex++)
  14105. + if (au_h_iptr(inode, bindex) == h_inode) {
  14106. + bfound = bindex;
  14107. + break;
  14108. + }
  14109. + if (bfound < 0)
  14110. + goto out;
  14111. +
  14112. + for (bindex = btop; bindex <= bbot; bindex++) {
  14113. + h_i = au_h_iptr(inode, bindex);
  14114. + if (!h_i)
  14115. + continue;
  14116. +
  14117. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  14118. + /* ignore this error */
  14119. + /* bad action? */
  14120. + }
  14121. +
  14122. + /* children inode number will be broken */
  14123. +
  14124. +out:
  14125. + AuTraceErr(err);
  14126. + return err;
  14127. +}
  14128. +
  14129. +static int hn_gen_tree(struct dentry *dentry)
  14130. +{
  14131. + int err, i, j, ndentry;
  14132. + struct au_dcsub_pages dpages;
  14133. + struct au_dpage *dpage;
  14134. + struct dentry **dentries;
  14135. +
  14136. + err = au_dpages_init(&dpages, GFP_NOFS);
  14137. + if (unlikely(err))
  14138. + goto out;
  14139. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  14140. + if (unlikely(err))
  14141. + goto out_dpages;
  14142. +
  14143. + for (i = 0; i < dpages.ndpage; i++) {
  14144. + dpage = dpages.dpages + i;
  14145. + dentries = dpage->dentries;
  14146. + ndentry = dpage->ndentry;
  14147. + for (j = 0; j < ndentry; j++) {
  14148. + struct dentry *d;
  14149. +
  14150. + d = dentries[j];
  14151. + if (IS_ROOT(d))
  14152. + continue;
  14153. +
  14154. + au_digen_dec(d);
  14155. + if (d_really_is_positive(d))
  14156. + /* todo: reset children xino?
  14157. + cached children only? */
  14158. + au_iigen_dec(d_inode(d));
  14159. + }
  14160. + }
  14161. +
  14162. +out_dpages:
  14163. + au_dpages_free(&dpages);
  14164. +
  14165. +#if 0
  14166. + /* discard children */
  14167. + dentry_unhash(dentry);
  14168. + dput(dentry);
  14169. +#endif
  14170. +out:
  14171. + return err;
  14172. +}
  14173. +
  14174. +/*
  14175. + * return 0 if processed.
  14176. + */
  14177. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  14178. + const unsigned int isdir)
  14179. +{
  14180. + int err;
  14181. + struct dentry *d;
  14182. + struct qstr *dname;
  14183. +
  14184. + err = 1;
  14185. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14186. + pr_warn("branch root dir was changed\n");
  14187. + err = 0;
  14188. + goto out;
  14189. + }
  14190. +
  14191. + if (!isdir) {
  14192. + AuDebugOn(!name);
  14193. + au_iigen_dec(inode);
  14194. + spin_lock(&inode->i_lock);
  14195. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  14196. + spin_lock(&d->d_lock);
  14197. + dname = &d->d_name;
  14198. + if (dname->len != nlen
  14199. + && memcmp(dname->name, name, nlen)) {
  14200. + spin_unlock(&d->d_lock);
  14201. + continue;
  14202. + }
  14203. + err = 0;
  14204. + au_digen_dec(d);
  14205. + spin_unlock(&d->d_lock);
  14206. + break;
  14207. + }
  14208. + spin_unlock(&inode->i_lock);
  14209. + } else {
  14210. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  14211. + d = d_find_any_alias(inode);
  14212. + if (!d) {
  14213. + au_iigen_dec(inode);
  14214. + goto out;
  14215. + }
  14216. +
  14217. + spin_lock(&d->d_lock);
  14218. + dname = &d->d_name;
  14219. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  14220. + spin_unlock(&d->d_lock);
  14221. + err = hn_gen_tree(d);
  14222. + spin_lock(&d->d_lock);
  14223. + }
  14224. + spin_unlock(&d->d_lock);
  14225. + dput(d);
  14226. + }
  14227. +
  14228. +out:
  14229. + AuTraceErr(err);
  14230. + return err;
  14231. +}
  14232. +
  14233. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  14234. +{
  14235. + int err;
  14236. +
  14237. + if (IS_ROOT(dentry)) {
  14238. + pr_warn("branch root dir was changed\n");
  14239. + return 0;
  14240. + }
  14241. +
  14242. + err = 0;
  14243. + if (!isdir) {
  14244. + au_digen_dec(dentry);
  14245. + if (d_really_is_positive(dentry))
  14246. + au_iigen_dec(d_inode(dentry));
  14247. + } else {
  14248. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  14249. + if (d_really_is_positive(dentry))
  14250. + err = hn_gen_tree(dentry);
  14251. + }
  14252. +
  14253. + AuTraceErr(err);
  14254. + return err;
  14255. +}
  14256. +
  14257. +/* ---------------------------------------------------------------------- */
  14258. +
  14259. +/* hnotify job flags */
  14260. +#define AuHnJob_XINO0 1
  14261. +#define AuHnJob_GEN (1 << 1)
  14262. +#define AuHnJob_DIRENT (1 << 2)
  14263. +#define AuHnJob_ISDIR (1 << 3)
  14264. +#define AuHnJob_TRYXINO0 (1 << 4)
  14265. +#define AuHnJob_MNTPNT (1 << 5)
  14266. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  14267. +#define au_fset_hnjob(flags, name) \
  14268. + do { (flags) |= AuHnJob_##name; } while (0)
  14269. +#define au_fclr_hnjob(flags, name) \
  14270. + do { (flags) &= ~AuHnJob_##name; } while (0)
  14271. +
  14272. +enum {
  14273. + AuHn_CHILD,
  14274. + AuHn_PARENT,
  14275. + AuHnLast
  14276. +};
  14277. +
  14278. +struct au_hnotify_args {
  14279. + struct inode *h_dir, *dir, *h_child_inode;
  14280. + u32 mask;
  14281. + unsigned int flags[AuHnLast];
  14282. + unsigned int h_child_nlen;
  14283. + char h_child_name[];
  14284. +};
  14285. +
  14286. +struct hn_job_args {
  14287. + unsigned int flags;
  14288. + struct inode *inode, *h_inode, *dir, *h_dir;
  14289. + struct dentry *dentry;
  14290. + char *h_name;
  14291. + int h_nlen;
  14292. +};
  14293. +
  14294. +static int hn_job(struct hn_job_args *a)
  14295. +{
  14296. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  14297. + int e;
  14298. +
  14299. + /* reset xino */
  14300. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  14301. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  14302. +
  14303. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  14304. + && a->inode
  14305. + && a->h_inode) {
  14306. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  14307. + if (!a->h_inode->i_nlink
  14308. + && !(a->h_inode->i_state & I_LINKABLE))
  14309. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  14310. + mutex_unlock(&a->h_inode->i_mutex);
  14311. + }
  14312. +
  14313. + /* make the generation obsolete */
  14314. + if (au_ftest_hnjob(a->flags, GEN)) {
  14315. + e = -1;
  14316. + if (a->inode)
  14317. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  14318. + isdir);
  14319. + if (e && a->dentry)
  14320. + hn_gen_by_name(a->dentry, isdir);
  14321. + /* ignore this error */
  14322. + }
  14323. +
  14324. + /* make dir entries obsolete */
  14325. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  14326. + struct au_vdir *vdir;
  14327. +
  14328. + vdir = au_ivdir(a->inode);
  14329. + if (vdir)
  14330. + vdir->vd_jiffy = 0;
  14331. + /* IMustLock(a->inode); */
  14332. + /* a->inode->i_version++; */
  14333. + }
  14334. +
  14335. + /* can do nothing but warn */
  14336. + if (au_ftest_hnjob(a->flags, MNTPNT)
  14337. + && a->dentry
  14338. + && d_mountpoint(a->dentry))
  14339. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  14340. +
  14341. + return 0;
  14342. +}
  14343. +
  14344. +/* ---------------------------------------------------------------------- */
  14345. +
  14346. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  14347. + struct inode *dir)
  14348. +{
  14349. + struct dentry *dentry, *d, *parent;
  14350. + struct qstr *dname;
  14351. +
  14352. + parent = d_find_any_alias(dir);
  14353. + if (!parent)
  14354. + return NULL;
  14355. +
  14356. + dentry = NULL;
  14357. + spin_lock(&parent->d_lock);
  14358. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  14359. + /* AuDbg("%pd\n", d); */
  14360. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  14361. + dname = &d->d_name;
  14362. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  14363. + goto cont_unlock;
  14364. + if (au_di(d))
  14365. + au_digen_dec(d);
  14366. + else
  14367. + goto cont_unlock;
  14368. + if (au_dcount(d) > 0) {
  14369. + dentry = dget_dlock(d);
  14370. + spin_unlock(&d->d_lock);
  14371. + break;
  14372. + }
  14373. +
  14374. +cont_unlock:
  14375. + spin_unlock(&d->d_lock);
  14376. + }
  14377. + spin_unlock(&parent->d_lock);
  14378. + dput(parent);
  14379. +
  14380. + if (dentry)
  14381. + di_write_lock_child(dentry);
  14382. +
  14383. + return dentry;
  14384. +}
  14385. +
  14386. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  14387. + aufs_bindex_t bindex, ino_t h_ino)
  14388. +{
  14389. + struct inode *inode;
  14390. + ino_t ino;
  14391. + int err;
  14392. +
  14393. + inode = NULL;
  14394. + err = au_xino_read(sb, bindex, h_ino, &ino);
  14395. + if (!err && ino)
  14396. + inode = ilookup(sb, ino);
  14397. + if (!inode)
  14398. + goto out;
  14399. +
  14400. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14401. + pr_warn("wrong root branch\n");
  14402. + iput(inode);
  14403. + inode = NULL;
  14404. + goto out;
  14405. + }
  14406. +
  14407. + ii_write_lock_child(inode);
  14408. +
  14409. +out:
  14410. + return inode;
  14411. +}
  14412. +
  14413. +static void au_hn_bh(void *_args)
  14414. +{
  14415. + struct au_hnotify_args *a = _args;
  14416. + struct super_block *sb;
  14417. + aufs_bindex_t bindex, bbot, bfound;
  14418. + unsigned char xino, try_iput;
  14419. + int err;
  14420. + struct inode *inode;
  14421. + ino_t h_ino;
  14422. + struct hn_job_args args;
  14423. + struct dentry *dentry;
  14424. + struct au_sbinfo *sbinfo;
  14425. +
  14426. + AuDebugOn(!_args);
  14427. + AuDebugOn(!a->h_dir);
  14428. + AuDebugOn(!a->dir);
  14429. + AuDebugOn(!a->mask);
  14430. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  14431. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  14432. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  14433. +
  14434. + inode = NULL;
  14435. + dentry = NULL;
  14436. + /*
  14437. + * do not lock a->dir->i_mutex here
  14438. + * because of d_revalidate() may cause a deadlock.
  14439. + */
  14440. + sb = a->dir->i_sb;
  14441. + AuDebugOn(!sb);
  14442. + sbinfo = au_sbi(sb);
  14443. + AuDebugOn(!sbinfo);
  14444. + si_write_lock(sb, AuLock_NOPLMW);
  14445. +
  14446. + ii_read_lock_parent(a->dir);
  14447. + bfound = -1;
  14448. + bbot = au_ibbot(a->dir);
  14449. + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
  14450. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  14451. + bfound = bindex;
  14452. + break;
  14453. + }
  14454. + ii_read_unlock(a->dir);
  14455. + if (unlikely(bfound < 0))
  14456. + goto out;
  14457. +
  14458. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  14459. + h_ino = 0;
  14460. + if (a->h_child_inode)
  14461. + h_ino = a->h_child_inode->i_ino;
  14462. +
  14463. + if (a->h_child_nlen
  14464. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  14465. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  14466. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  14467. + a->dir);
  14468. + try_iput = 0;
  14469. + if (dentry && d_really_is_positive(dentry))
  14470. + inode = d_inode(dentry);
  14471. + if (xino && !inode && h_ino
  14472. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  14473. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  14474. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  14475. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  14476. + try_iput = 1;
  14477. + }
  14478. +
  14479. + args.flags = a->flags[AuHn_CHILD];
  14480. + args.dentry = dentry;
  14481. + args.inode = inode;
  14482. + args.h_inode = a->h_child_inode;
  14483. + args.dir = a->dir;
  14484. + args.h_dir = a->h_dir;
  14485. + args.h_name = a->h_child_name;
  14486. + args.h_nlen = a->h_child_nlen;
  14487. + err = hn_job(&args);
  14488. + if (dentry) {
  14489. + if (au_di(dentry))
  14490. + di_write_unlock(dentry);
  14491. + dput(dentry);
  14492. + }
  14493. + if (inode && try_iput) {
  14494. + ii_write_unlock(inode);
  14495. + iput(inode);
  14496. + }
  14497. +
  14498. + ii_write_lock_parent(a->dir);
  14499. + args.flags = a->flags[AuHn_PARENT];
  14500. + args.dentry = NULL;
  14501. + args.inode = a->dir;
  14502. + args.h_inode = a->h_dir;
  14503. + args.dir = NULL;
  14504. + args.h_dir = NULL;
  14505. + args.h_name = NULL;
  14506. + args.h_nlen = 0;
  14507. + err = hn_job(&args);
  14508. + ii_write_unlock(a->dir);
  14509. +
  14510. +out:
  14511. + iput(a->h_child_inode);
  14512. + iput(a->h_dir);
  14513. + iput(a->dir);
  14514. + si_write_unlock(sb);
  14515. + au_nwt_done(&sbinfo->si_nowait);
  14516. + au_delayed_kfree(a);
  14517. +}
  14518. +
  14519. +/* ---------------------------------------------------------------------- */
  14520. +
  14521. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  14522. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  14523. +{
  14524. + int err, len;
  14525. + unsigned int flags[AuHnLast], f;
  14526. + unsigned char isdir, isroot, wh;
  14527. + struct inode *dir;
  14528. + struct au_hnotify_args *args;
  14529. + char *p, *h_child_name;
  14530. +
  14531. + err = 0;
  14532. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  14533. + dir = igrab(hnotify->hn_aufs_inode);
  14534. + if (!dir)
  14535. + goto out;
  14536. +
  14537. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  14538. + wh = 0;
  14539. + h_child_name = (void *)h_child_qstr->name;
  14540. + len = h_child_qstr->len;
  14541. + if (h_child_name) {
  14542. + if (len > AUFS_WH_PFX_LEN
  14543. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  14544. + h_child_name += AUFS_WH_PFX_LEN;
  14545. + len -= AUFS_WH_PFX_LEN;
  14546. + wh = 1;
  14547. + }
  14548. + }
  14549. +
  14550. + isdir = 0;
  14551. + if (h_child_inode)
  14552. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  14553. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  14554. + flags[AuHn_CHILD] = 0;
  14555. + if (isdir)
  14556. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  14557. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  14558. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  14559. + switch (mask & FS_EVENTS_POSS_ON_CHILD) {
  14560. + case FS_MOVED_FROM:
  14561. + case FS_MOVED_TO:
  14562. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  14563. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  14564. + /*FALLTHROUGH*/
  14565. + case FS_CREATE:
  14566. + AuDebugOn(!h_child_name);
  14567. + break;
  14568. +
  14569. + case FS_DELETE:
  14570. + /*
  14571. + * aufs never be able to get this child inode.
  14572. + * revalidation should be in d_revalidate()
  14573. + * by checking i_nlink, i_generation or d_unhashed().
  14574. + */
  14575. + AuDebugOn(!h_child_name);
  14576. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  14577. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  14578. + break;
  14579. +
  14580. + default:
  14581. + AuDebugOn(1);
  14582. + }
  14583. +
  14584. + if (wh)
  14585. + h_child_inode = NULL;
  14586. +
  14587. + err = -ENOMEM;
  14588. + /* iput() and kfree() will be called in au_hnotify() */
  14589. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  14590. + if (unlikely(!args)) {
  14591. + AuErr1("no memory\n");
  14592. + iput(dir);
  14593. + goto out;
  14594. + }
  14595. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  14596. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  14597. + args->mask = mask;
  14598. + args->dir = dir;
  14599. + args->h_dir = igrab(h_dir);
  14600. + if (h_child_inode)
  14601. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  14602. + args->h_child_inode = h_child_inode;
  14603. + args->h_child_nlen = len;
  14604. + if (len) {
  14605. + p = (void *)args;
  14606. + p += sizeof(*args);
  14607. + memcpy(p, h_child_name, len);
  14608. + p[len] = 0;
  14609. + }
  14610. +
  14611. + /* NFS fires the event for silly-renamed one from kworker */
  14612. + f = 0;
  14613. + if (!dir->i_nlink
  14614. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  14615. + f = AuWkq_NEST;
  14616. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  14617. + if (unlikely(err)) {
  14618. + pr_err("wkq %d\n", err);
  14619. + iput(args->h_child_inode);
  14620. + iput(args->h_dir);
  14621. + iput(args->dir);
  14622. + au_delayed_kfree(args);
  14623. + }
  14624. +
  14625. +out:
  14626. + return err;
  14627. +}
  14628. +
  14629. +/* ---------------------------------------------------------------------- */
  14630. +
  14631. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  14632. +{
  14633. + int err;
  14634. +
  14635. + AuDebugOn(!(udba & AuOptMask_UDBA));
  14636. +
  14637. + err = 0;
  14638. + if (au_hnotify_op.reset_br)
  14639. + err = au_hnotify_op.reset_br(udba, br, perm);
  14640. +
  14641. + return err;
  14642. +}
  14643. +
  14644. +int au_hnotify_init_br(struct au_branch *br, int perm)
  14645. +{
  14646. + int err;
  14647. +
  14648. + err = 0;
  14649. + if (au_hnotify_op.init_br)
  14650. + err = au_hnotify_op.init_br(br, perm);
  14651. +
  14652. + return err;
  14653. +}
  14654. +
  14655. +void au_hnotify_fin_br(struct au_branch *br)
  14656. +{
  14657. + if (au_hnotify_op.fin_br)
  14658. + au_hnotify_op.fin_br(br);
  14659. +}
  14660. +
  14661. +static void au_hn_destroy_cache(void)
  14662. +{
  14663. + struct au_cache *cp;
  14664. +
  14665. + flush_delayed_work(&au_dfree.dwork);
  14666. + cp = au_dfree.cache + AuCache_HNOTIFY;
  14667. + AuDebugOn(!llist_empty(&cp->llist));
  14668. + kmem_cache_destroy(cp->cache);
  14669. + cp->cache = NULL;
  14670. +}
  14671. +
  14672. +AU_CACHE_DFREE_FUNC(hnotify, HNOTIFY, hn_lnode);
  14673. +
  14674. +int __init au_hnotify_init(void)
  14675. +{
  14676. + int err;
  14677. + struct au_cache *cp;
  14678. +
  14679. + err = -ENOMEM;
  14680. + cp = au_dfree.cache + AuCache_HNOTIFY;
  14681. + cp->cache = AuCache(au_hnotify);
  14682. + if (cp->cache) {
  14683. + err = 0;
  14684. + if (au_hnotify_op.init)
  14685. + err = au_hnotify_op.init();
  14686. + if (unlikely(err))
  14687. + au_hn_destroy_cache();
  14688. + }
  14689. + AuTraceErr(err);
  14690. + return err;
  14691. +}
  14692. +
  14693. +void au_hnotify_fin(void)
  14694. +{
  14695. + struct au_cache *cp;
  14696. +
  14697. + if (au_hnotify_op.fin)
  14698. + au_hnotify_op.fin();
  14699. +
  14700. + /* cf. au_cache_fin() */
  14701. + cp = au_dfree.cache + AuCache_HNOTIFY;
  14702. + if (cp->cache)
  14703. + au_hn_destroy_cache();
  14704. +}
  14705. --- /dev/null
  14706. +++ linux-4.4/fs/aufs/iinfo.c 2016-10-23 11:20:57.635493285 +0300
  14707. @@ -0,0 +1,285 @@
  14708. +/*
  14709. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  14710. + *
  14711. + * This program, aufs is free software; you can redistribute it and/or modify
  14712. + * it under the terms of the GNU General Public License as published by
  14713. + * the Free Software Foundation; either version 2 of the License, or
  14714. + * (at your option) any later version.
  14715. + *
  14716. + * This program is distributed in the hope that it will be useful,
  14717. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14718. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14719. + * GNU General Public License for more details.
  14720. + *
  14721. + * You should have received a copy of the GNU General Public License
  14722. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14723. + */
  14724. +
  14725. +/*
  14726. + * inode private data
  14727. + */
  14728. +
  14729. +#include "aufs.h"
  14730. +
  14731. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  14732. +{
  14733. + struct inode *h_inode;
  14734. + struct au_hinode *hinode;
  14735. +
  14736. + IiMustAnyLock(inode);
  14737. +
  14738. + hinode = au_hinode(au_ii(inode), bindex);
  14739. + h_inode = hinode->hi_inode;
  14740. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14741. + return h_inode;
  14742. +}
  14743. +
  14744. +/* todo: hard/soft set? */
  14745. +void au_hiput(struct au_hinode *hinode)
  14746. +{
  14747. + au_hn_free(hinode);
  14748. + dput(hinode->hi_whdentry);
  14749. + iput(hinode->hi_inode);
  14750. +}
  14751. +
  14752. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  14753. +{
  14754. + unsigned int flags;
  14755. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  14756. +
  14757. + flags = 0;
  14758. + if (au_opt_test(mnt_flags, XINO))
  14759. + au_fset_hi(flags, XINO);
  14760. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  14761. + au_fset_hi(flags, HNOTIFY);
  14762. + return flags;
  14763. +}
  14764. +
  14765. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  14766. + struct inode *h_inode, unsigned int flags)
  14767. +{
  14768. + struct au_hinode *hinode;
  14769. + struct inode *hi;
  14770. + struct au_iinfo *iinfo = au_ii(inode);
  14771. +
  14772. + IiMustWriteLock(inode);
  14773. +
  14774. + hinode = au_hinode(iinfo, bindex);
  14775. + hi = hinode->hi_inode;
  14776. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14777. +
  14778. + if (hi)
  14779. + au_hiput(hinode);
  14780. + hinode->hi_inode = h_inode;
  14781. + if (h_inode) {
  14782. + int err;
  14783. + struct super_block *sb = inode->i_sb;
  14784. + struct au_branch *br;
  14785. +
  14786. + AuDebugOn(inode->i_mode
  14787. + && (h_inode->i_mode & S_IFMT)
  14788. + != (inode->i_mode & S_IFMT));
  14789. + if (bindex == iinfo->ii_btop)
  14790. + au_cpup_igen(inode, h_inode);
  14791. + br = au_sbr(sb, bindex);
  14792. + hinode->hi_id = br->br_id;
  14793. + if (au_ftest_hi(flags, XINO)) {
  14794. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  14795. + inode->i_ino);
  14796. + if (unlikely(err))
  14797. + AuIOErr1("failed au_xino_write() %d\n", err);
  14798. + }
  14799. +
  14800. + if (au_ftest_hi(flags, HNOTIFY)
  14801. + && au_br_hnotifyable(br->br_perm)) {
  14802. + err = au_hn_alloc(hinode, inode);
  14803. + if (unlikely(err))
  14804. + AuIOErr1("au_hn_alloc() %d\n", err);
  14805. + }
  14806. + }
  14807. +}
  14808. +
  14809. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  14810. + struct dentry *h_wh)
  14811. +{
  14812. + struct au_hinode *hinode;
  14813. +
  14814. + IiMustWriteLock(inode);
  14815. +
  14816. + hinode = au_hinode(au_ii(inode), bindex);
  14817. + AuDebugOn(hinode->hi_whdentry);
  14818. + hinode->hi_whdentry = h_wh;
  14819. +}
  14820. +
  14821. +void au_update_iigen(struct inode *inode, int half)
  14822. +{
  14823. + struct au_iinfo *iinfo;
  14824. + struct au_iigen *iigen;
  14825. + unsigned int sigen;
  14826. +
  14827. + sigen = au_sigen(inode->i_sb);
  14828. + iinfo = au_ii(inode);
  14829. + iigen = &iinfo->ii_generation;
  14830. + spin_lock(&iigen->ig_spin);
  14831. + iigen->ig_generation = sigen;
  14832. + if (half)
  14833. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  14834. + else
  14835. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  14836. + spin_unlock(&iigen->ig_spin);
  14837. +}
  14838. +
  14839. +/* it may be called at remount time, too */
  14840. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  14841. +{
  14842. + struct au_iinfo *iinfo;
  14843. + aufs_bindex_t bindex, bbot;
  14844. +
  14845. + AuDebugOn(au_is_bad_inode(inode));
  14846. + IiMustWriteLock(inode);
  14847. +
  14848. + iinfo = au_ii(inode);
  14849. + if (do_put_zero && iinfo->ii_btop >= 0) {
  14850. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  14851. + bindex++) {
  14852. + struct inode *h_i;
  14853. +
  14854. + h_i = au_hinode(iinfo, bindex)->hi_inode;
  14855. + if (h_i
  14856. + && !h_i->i_nlink
  14857. + && !(h_i->i_state & I_LINKABLE))
  14858. + au_set_h_iptr(inode, bindex, NULL, 0);
  14859. + }
  14860. + }
  14861. +
  14862. + iinfo->ii_btop = -1;
  14863. + iinfo->ii_bbot = -1;
  14864. + bbot = au_sbbot(inode->i_sb);
  14865. + for (bindex = 0; bindex <= bbot; bindex++)
  14866. + if (au_hinode(iinfo, bindex)->hi_inode) {
  14867. + iinfo->ii_btop = bindex;
  14868. + break;
  14869. + }
  14870. + if (iinfo->ii_btop >= 0)
  14871. + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
  14872. + if (au_hinode(iinfo, bindex)->hi_inode) {
  14873. + iinfo->ii_bbot = bindex;
  14874. + break;
  14875. + }
  14876. + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
  14877. +}
  14878. +
  14879. +/* ---------------------------------------------------------------------- */
  14880. +
  14881. +void au_icntnr_init_once(void *_c)
  14882. +{
  14883. + struct au_icntnr *c = _c;
  14884. + struct au_iinfo *iinfo = &c->iinfo;
  14885. +
  14886. + spin_lock_init(&iinfo->ii_generation.ig_spin);
  14887. + au_rw_init(&iinfo->ii_rwsem);
  14888. + inode_init_once(&c->vfs_inode);
  14889. +}
  14890. +
  14891. +void au_hinode_init(struct au_hinode *hinode)
  14892. +{
  14893. + hinode->hi_inode = NULL;
  14894. + hinode->hi_id = -1;
  14895. + au_hn_init(hinode);
  14896. + hinode->hi_whdentry = NULL;
  14897. +}
  14898. +
  14899. +int au_iinfo_init(struct inode *inode)
  14900. +{
  14901. + struct au_iinfo *iinfo;
  14902. + struct super_block *sb;
  14903. + struct au_hinode *hi;
  14904. + int nbr, i;
  14905. +
  14906. + sb = inode->i_sb;
  14907. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  14908. + nbr = au_sbbot(sb) + 1;
  14909. + if (unlikely(nbr <= 0))
  14910. + nbr = 1;
  14911. + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  14912. + if (hi) {
  14913. + au_ninodes_inc(sb);
  14914. +
  14915. + iinfo->ii_hinode = hi;
  14916. + for (i = 0; i < nbr; i++, hi++)
  14917. + au_hinode_init(hi);
  14918. +
  14919. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  14920. + iinfo->ii_btop = -1;
  14921. + iinfo->ii_bbot = -1;
  14922. + iinfo->ii_vdir = NULL;
  14923. + return 0;
  14924. + }
  14925. + return -ENOMEM;
  14926. +}
  14927. +
  14928. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
  14929. +{
  14930. + int err, i;
  14931. + struct au_hinode *hip;
  14932. +
  14933. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  14934. +
  14935. + err = -ENOMEM;
  14936. + hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
  14937. + may_shrink);
  14938. + if (hip) {
  14939. + iinfo->ii_hinode = hip;
  14940. + i = iinfo->ii_bbot + 1;
  14941. + hip += i;
  14942. + for (; i < nbr; i++, hip++)
  14943. + au_hinode_init(hip);
  14944. + err = 0;
  14945. + }
  14946. +
  14947. + return err;
  14948. +}
  14949. +
  14950. +void au_iinfo_fin(struct inode *inode)
  14951. +{
  14952. + struct au_iinfo *iinfo;
  14953. + struct au_hinode *hi;
  14954. + struct super_block *sb;
  14955. + aufs_bindex_t bindex, bbot;
  14956. + const unsigned char unlinked = !inode->i_nlink;
  14957. +
  14958. + AuDebugOn(au_is_bad_inode(inode));
  14959. +
  14960. + sb = inode->i_sb;
  14961. + au_ninodes_dec(sb);
  14962. + if (si_pid_test(sb))
  14963. + au_xino_delete_inode(inode, unlinked);
  14964. + else {
  14965. + /*
  14966. + * it is safe to hide the dependency between sbinfo and
  14967. + * sb->s_umount.
  14968. + */
  14969. + lockdep_off();
  14970. + si_noflush_read_lock(sb);
  14971. + au_xino_delete_inode(inode, unlinked);
  14972. + si_read_unlock(sb);
  14973. + lockdep_on();
  14974. + }
  14975. +
  14976. + iinfo = au_ii(inode);
  14977. + if (iinfo->ii_vdir)
  14978. + au_vdir_free(iinfo->ii_vdir, /*atonce*/0);
  14979. +
  14980. + bindex = iinfo->ii_btop;
  14981. + if (bindex >= 0) {
  14982. + hi = au_hinode(iinfo, bindex);
  14983. + bbot = iinfo->ii_bbot;
  14984. + while (bindex++ <= bbot) {
  14985. + if (hi->hi_inode)
  14986. + au_hiput(hi);
  14987. + hi++;
  14988. + }
  14989. + }
  14990. + au_delayed_kfree(iinfo->ii_hinode);
  14991. + AuRwDestroy(&iinfo->ii_rwsem);
  14992. +}
  14993. --- /dev/null
  14994. +++ linux-4.4/fs/aufs/inode.c 2016-10-23 11:20:57.638826618 +0300
  14995. @@ -0,0 +1,519 @@
  14996. +/*
  14997. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  14998. + *
  14999. + * This program, aufs is free software; you can redistribute it and/or modify
  15000. + * it under the terms of the GNU General Public License as published by
  15001. + * the Free Software Foundation; either version 2 of the License, or
  15002. + * (at your option) any later version.
  15003. + *
  15004. + * This program is distributed in the hope that it will be useful,
  15005. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15006. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15007. + * GNU General Public License for more details.
  15008. + *
  15009. + * You should have received a copy of the GNU General Public License
  15010. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15011. + */
  15012. +
  15013. +/*
  15014. + * inode functions
  15015. + */
  15016. +
  15017. +#include "aufs.h"
  15018. +
  15019. +struct inode *au_igrab(struct inode *inode)
  15020. +{
  15021. + if (inode) {
  15022. + AuDebugOn(!atomic_read(&inode->i_count));
  15023. + ihold(inode);
  15024. + }
  15025. + return inode;
  15026. +}
  15027. +
  15028. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  15029. +{
  15030. + au_cpup_attr_all(inode, /*force*/0);
  15031. + au_update_iigen(inode, /*half*/1);
  15032. + if (do_version)
  15033. + inode->i_version++;
  15034. +}
  15035. +
  15036. +static int au_ii_refresh(struct inode *inode, int *update)
  15037. +{
  15038. + int err, e, nbr;
  15039. + umode_t type;
  15040. + aufs_bindex_t bindex, new_bindex;
  15041. + struct super_block *sb;
  15042. + struct au_iinfo *iinfo;
  15043. + struct au_hinode *p, *q, tmp;
  15044. +
  15045. + AuDebugOn(au_is_bad_inode(inode));
  15046. + IiMustWriteLock(inode);
  15047. +
  15048. + *update = 0;
  15049. + sb = inode->i_sb;
  15050. + nbr = au_sbbot(sb) + 1;
  15051. + type = inode->i_mode & S_IFMT;
  15052. + iinfo = au_ii(inode);
  15053. + err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
  15054. + if (unlikely(err))
  15055. + goto out;
  15056. +
  15057. + AuDebugOn(iinfo->ii_btop < 0);
  15058. + p = au_hinode(iinfo, iinfo->ii_btop);
  15059. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  15060. + bindex++, p++) {
  15061. + if (!p->hi_inode)
  15062. + continue;
  15063. +
  15064. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  15065. + new_bindex = au_br_index(sb, p->hi_id);
  15066. + if (new_bindex == bindex)
  15067. + continue;
  15068. +
  15069. + if (new_bindex < 0) {
  15070. + *update = 1;
  15071. + au_hiput(p);
  15072. + p->hi_inode = NULL;
  15073. + continue;
  15074. + }
  15075. +
  15076. + if (new_bindex < iinfo->ii_btop)
  15077. + iinfo->ii_btop = new_bindex;
  15078. + if (iinfo->ii_bbot < new_bindex)
  15079. + iinfo->ii_bbot = new_bindex;
  15080. + /* swap two lower inode, and loop again */
  15081. + q = au_hinode(iinfo, new_bindex);
  15082. + tmp = *q;
  15083. + *q = *p;
  15084. + *p = tmp;
  15085. + if (tmp.hi_inode) {
  15086. + bindex--;
  15087. + p--;
  15088. + }
  15089. + }
  15090. + au_update_ibrange(inode, /*do_put_zero*/0);
  15091. + au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
  15092. + e = au_dy_irefresh(inode);
  15093. + if (unlikely(e && !err))
  15094. + err = e;
  15095. +
  15096. +out:
  15097. + AuTraceErr(err);
  15098. + return err;
  15099. +}
  15100. +
  15101. +void au_refresh_iop(struct inode *inode, int force_getattr)
  15102. +{
  15103. + int type;
  15104. + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
  15105. + const struct inode_operations *iop
  15106. + = force_getattr ? aufs_iop : sbi->si_iop_array;
  15107. +
  15108. + if (inode->i_op == iop)
  15109. + return;
  15110. +
  15111. + switch (inode->i_mode & S_IFMT) {
  15112. + case S_IFDIR:
  15113. + type = AuIop_DIR;
  15114. + break;
  15115. + case S_IFLNK:
  15116. + type = AuIop_SYMLINK;
  15117. + break;
  15118. + default:
  15119. + type = AuIop_OTHER;
  15120. + break;
  15121. + }
  15122. +
  15123. + inode->i_op = iop + type;
  15124. + /* unnecessary smp_wmb() */
  15125. +}
  15126. +
  15127. +int au_refresh_hinode_self(struct inode *inode)
  15128. +{
  15129. + int err, update;
  15130. +
  15131. + err = au_ii_refresh(inode, &update);
  15132. + if (!err)
  15133. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  15134. +
  15135. + AuTraceErr(err);
  15136. + return err;
  15137. +}
  15138. +
  15139. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  15140. +{
  15141. + int err, e, update;
  15142. + unsigned int flags;
  15143. + umode_t mode;
  15144. + aufs_bindex_t bindex, bbot;
  15145. + unsigned char isdir;
  15146. + struct au_hinode *p;
  15147. + struct au_iinfo *iinfo;
  15148. +
  15149. + err = au_ii_refresh(inode, &update);
  15150. + if (unlikely(err))
  15151. + goto out;
  15152. +
  15153. + update = 0;
  15154. + iinfo = au_ii(inode);
  15155. + p = au_hinode(iinfo, iinfo->ii_btop);
  15156. + mode = (inode->i_mode & S_IFMT);
  15157. + isdir = S_ISDIR(mode);
  15158. + flags = au_hi_flags(inode, isdir);
  15159. + bbot = au_dbbot(dentry);
  15160. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  15161. + struct inode *h_i, *h_inode;
  15162. + struct dentry *h_d;
  15163. +
  15164. + h_d = au_h_dptr(dentry, bindex);
  15165. + if (!h_d || d_is_negative(h_d))
  15166. + continue;
  15167. +
  15168. + h_inode = d_inode(h_d);
  15169. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  15170. + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
  15171. + h_i = au_h_iptr(inode, bindex);
  15172. + if (h_i) {
  15173. + if (h_i == h_inode)
  15174. + continue;
  15175. + err = -EIO;
  15176. + break;
  15177. + }
  15178. + }
  15179. + if (bindex < iinfo->ii_btop)
  15180. + iinfo->ii_btop = bindex;
  15181. + if (iinfo->ii_bbot < bindex)
  15182. + iinfo->ii_bbot = bindex;
  15183. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  15184. + update = 1;
  15185. + }
  15186. + au_update_ibrange(inode, /*do_put_zero*/0);
  15187. + e = au_dy_irefresh(inode);
  15188. + if (unlikely(e && !err))
  15189. + err = e;
  15190. + if (!err)
  15191. + au_refresh_hinode_attr(inode, update && isdir);
  15192. +
  15193. +out:
  15194. + AuTraceErr(err);
  15195. + return err;
  15196. +}
  15197. +
  15198. +static int set_inode(struct inode *inode, struct dentry *dentry)
  15199. +{
  15200. + int err;
  15201. + unsigned int flags;
  15202. + umode_t mode;
  15203. + aufs_bindex_t bindex, btop, btail;
  15204. + unsigned char isdir;
  15205. + struct dentry *h_dentry;
  15206. + struct inode *h_inode;
  15207. + struct au_iinfo *iinfo;
  15208. + struct inode_operations *iop;
  15209. +
  15210. + IiMustWriteLock(inode);
  15211. +
  15212. + err = 0;
  15213. + isdir = 0;
  15214. + iop = au_sbi(inode->i_sb)->si_iop_array;
  15215. + btop = au_dbtop(dentry);
  15216. + h_dentry = au_h_dptr(dentry, btop);
  15217. + h_inode = d_inode(h_dentry);
  15218. + mode = h_inode->i_mode;
  15219. + switch (mode & S_IFMT) {
  15220. + case S_IFREG:
  15221. + btail = au_dbtail(dentry);
  15222. + inode->i_op = iop + AuIop_OTHER;
  15223. + inode->i_fop = &aufs_file_fop;
  15224. + err = au_dy_iaop(inode, btop, h_inode);
  15225. + if (unlikely(err))
  15226. + goto out;
  15227. + break;
  15228. + case S_IFDIR:
  15229. + isdir = 1;
  15230. + btail = au_dbtaildir(dentry);
  15231. + inode->i_op = iop + AuIop_DIR;
  15232. + inode->i_fop = &aufs_dir_fop;
  15233. + break;
  15234. + case S_IFLNK:
  15235. + btail = au_dbtail(dentry);
  15236. + inode->i_op = iop + AuIop_SYMLINK;
  15237. + break;
  15238. + case S_IFBLK:
  15239. + case S_IFCHR:
  15240. + case S_IFIFO:
  15241. + case S_IFSOCK:
  15242. + btail = au_dbtail(dentry);
  15243. + inode->i_op = iop + AuIop_OTHER;
  15244. + init_special_inode(inode, mode, h_inode->i_rdev);
  15245. + break;
  15246. + default:
  15247. + AuIOErr("Unknown file type 0%o\n", mode);
  15248. + err = -EIO;
  15249. + goto out;
  15250. + }
  15251. +
  15252. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  15253. + flags = au_hi_flags(inode, isdir);
  15254. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  15255. + && au_ftest_hi(flags, HNOTIFY)
  15256. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  15257. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  15258. + au_fclr_hi(flags, HNOTIFY);
  15259. + iinfo = au_ii(inode);
  15260. + iinfo->ii_btop = btop;
  15261. + iinfo->ii_bbot = btail;
  15262. + for (bindex = btop; bindex <= btail; bindex++) {
  15263. + h_dentry = au_h_dptr(dentry, bindex);
  15264. + if (h_dentry)
  15265. + au_set_h_iptr(inode, bindex,
  15266. + au_igrab(d_inode(h_dentry)), flags);
  15267. + }
  15268. + au_cpup_attr_all(inode, /*force*/1);
  15269. + /*
  15270. + * to force calling aufs_get_acl() every time,
  15271. + * do not call cache_no_acl() for aufs inode.
  15272. + */
  15273. +
  15274. +out:
  15275. + return err;
  15276. +}
  15277. +
  15278. +/*
  15279. + * successful returns with iinfo write_locked
  15280. + * minus: errno
  15281. + * zero: success, matched
  15282. + * plus: no error, but unmatched
  15283. + */
  15284. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  15285. +{
  15286. + int err;
  15287. + unsigned int gen, igflags;
  15288. + aufs_bindex_t bindex, bbot;
  15289. + struct inode *h_inode, *h_dinode;
  15290. + struct dentry *h_dentry;
  15291. +
  15292. + /*
  15293. + * before this function, if aufs got any iinfo lock, it must be only
  15294. + * one, the parent dir.
  15295. + * it can happen by UDBA and the obsoleted inode number.
  15296. + */
  15297. + err = -EIO;
  15298. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  15299. + goto out;
  15300. +
  15301. + err = 1;
  15302. + ii_write_lock_new_child(inode);
  15303. + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
  15304. + h_dinode = d_inode(h_dentry);
  15305. + bbot = au_ibbot(inode);
  15306. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  15307. + h_inode = au_h_iptr(inode, bindex);
  15308. + if (!h_inode || h_inode != h_dinode)
  15309. + continue;
  15310. +
  15311. + err = 0;
  15312. + gen = au_iigen(inode, &igflags);
  15313. + if (gen == au_digen(dentry)
  15314. + && !au_ig_ftest(igflags, HALF_REFRESHED))
  15315. + break;
  15316. +
  15317. + /* fully refresh inode using dentry */
  15318. + err = au_refresh_hinode(inode, dentry);
  15319. + if (!err)
  15320. + au_update_iigen(inode, /*half*/0);
  15321. + break;
  15322. + }
  15323. +
  15324. + if (unlikely(err))
  15325. + ii_write_unlock(inode);
  15326. +out:
  15327. + return err;
  15328. +}
  15329. +
  15330. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  15331. + unsigned int d_type, ino_t *ino)
  15332. +{
  15333. + int err;
  15334. + struct mutex *mtx;
  15335. +
  15336. + /* prevent hardlinked inode number from race condition */
  15337. + mtx = NULL;
  15338. + if (d_type != DT_DIR) {
  15339. + mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
  15340. + mutex_lock(mtx);
  15341. + }
  15342. + err = au_xino_read(sb, bindex, h_ino, ino);
  15343. + if (unlikely(err))
  15344. + goto out;
  15345. +
  15346. + if (!*ino) {
  15347. + err = -EIO;
  15348. + *ino = au_xino_new_ino(sb);
  15349. + if (unlikely(!*ino))
  15350. + goto out;
  15351. + err = au_xino_write(sb, bindex, h_ino, *ino);
  15352. + if (unlikely(err))
  15353. + goto out;
  15354. + }
  15355. +
  15356. +out:
  15357. + if (mtx)
  15358. + mutex_unlock(mtx);
  15359. + return err;
  15360. +}
  15361. +
  15362. +/* successful returns with iinfo write_locked */
  15363. +/* todo: return with unlocked? */
  15364. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  15365. +{
  15366. + struct inode *inode, *h_inode;
  15367. + struct dentry *h_dentry;
  15368. + struct super_block *sb;
  15369. + struct mutex *mtx;
  15370. + ino_t h_ino, ino;
  15371. + int err;
  15372. + aufs_bindex_t btop;
  15373. +
  15374. + sb = dentry->d_sb;
  15375. + btop = au_dbtop(dentry);
  15376. + h_dentry = au_h_dptr(dentry, btop);
  15377. + h_inode = d_inode(h_dentry);
  15378. + h_ino = h_inode->i_ino;
  15379. +
  15380. + /*
  15381. + * stop 'race'-ing between hardlinks under different
  15382. + * parents.
  15383. + */
  15384. + mtx = NULL;
  15385. + if (!d_is_dir(h_dentry))
  15386. + mtx = &au_sbr(sb, btop)->br_xino.xi_nondir_mtx;
  15387. +
  15388. +new_ino:
  15389. + if (mtx)
  15390. + mutex_lock(mtx);
  15391. + err = au_xino_read(sb, btop, h_ino, &ino);
  15392. + inode = ERR_PTR(err);
  15393. + if (unlikely(err))
  15394. + goto out;
  15395. +
  15396. + if (!ino) {
  15397. + ino = au_xino_new_ino(sb);
  15398. + if (unlikely(!ino)) {
  15399. + inode = ERR_PTR(-EIO);
  15400. + goto out;
  15401. + }
  15402. + }
  15403. +
  15404. + AuDbg("i%lu\n", (unsigned long)ino);
  15405. + inode = au_iget_locked(sb, ino);
  15406. + err = PTR_ERR(inode);
  15407. + if (IS_ERR(inode))
  15408. + goto out;
  15409. +
  15410. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  15411. + if (inode->i_state & I_NEW) {
  15412. + ii_write_lock_new_child(inode);
  15413. + err = set_inode(inode, dentry);
  15414. + if (!err) {
  15415. + unlock_new_inode(inode);
  15416. + goto out; /* success */
  15417. + }
  15418. +
  15419. + /*
  15420. + * iget_failed() calls iput(), but we need to call
  15421. + * ii_write_unlock() after iget_failed(). so dirty hack for
  15422. + * i_count.
  15423. + */
  15424. + atomic_inc(&inode->i_count);
  15425. + iget_failed(inode);
  15426. + ii_write_unlock(inode);
  15427. + au_xino_write(sb, btop, h_ino, /*ino*/0);
  15428. + /* ignore this error */
  15429. + goto out_iput;
  15430. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  15431. + /*
  15432. + * horrible race condition between lookup, readdir and copyup
  15433. + * (or something).
  15434. + */
  15435. + if (mtx)
  15436. + mutex_unlock(mtx);
  15437. + err = reval_inode(inode, dentry);
  15438. + if (unlikely(err < 0)) {
  15439. + mtx = NULL;
  15440. + goto out_iput;
  15441. + }
  15442. +
  15443. + if (!err) {
  15444. + mtx = NULL;
  15445. + goto out; /* success */
  15446. + } else if (mtx)
  15447. + mutex_lock(mtx);
  15448. + }
  15449. +
  15450. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  15451. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  15452. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  15453. + btop, au_sbtype(h_dentry->d_sb), dentry,
  15454. + (unsigned long)h_ino, (unsigned long)ino);
  15455. + ino = 0;
  15456. + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
  15457. + if (!err) {
  15458. + iput(inode);
  15459. + if (mtx)
  15460. + mutex_unlock(mtx);
  15461. + goto new_ino;
  15462. + }
  15463. +
  15464. +out_iput:
  15465. + iput(inode);
  15466. + inode = ERR_PTR(err);
  15467. +out:
  15468. + if (mtx)
  15469. + mutex_unlock(mtx);
  15470. + return inode;
  15471. +}
  15472. +
  15473. +/* ---------------------------------------------------------------------- */
  15474. +
  15475. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  15476. + struct inode *inode)
  15477. +{
  15478. + int err;
  15479. + struct inode *hi;
  15480. +
  15481. + err = au_br_rdonly(au_sbr(sb, bindex));
  15482. +
  15483. + /* pseudo-link after flushed may happen out of bounds */
  15484. + if (!err
  15485. + && inode
  15486. + && au_ibtop(inode) <= bindex
  15487. + && bindex <= au_ibbot(inode)) {
  15488. + /*
  15489. + * permission check is unnecessary since vfsub routine
  15490. + * will be called later
  15491. + */
  15492. + hi = au_h_iptr(inode, bindex);
  15493. + if (hi)
  15494. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  15495. + }
  15496. +
  15497. + return err;
  15498. +}
  15499. +
  15500. +int au_test_h_perm(struct inode *h_inode, int mask)
  15501. +{
  15502. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  15503. + return 0;
  15504. + return inode_permission(h_inode, mask);
  15505. +}
  15506. +
  15507. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  15508. +{
  15509. + if (au_test_nfs(h_inode->i_sb)
  15510. + && (mask & MAY_WRITE)
  15511. + && S_ISDIR(h_inode->i_mode))
  15512. + mask |= MAY_READ; /* force permission check */
  15513. + return au_test_h_perm(h_inode, mask);
  15514. +}
  15515. --- /dev/null
  15516. +++ linux-4.4/fs/aufs/inode.h 2016-10-23 11:20:57.638826618 +0300
  15517. @@ -0,0 +1,700 @@
  15518. +/*
  15519. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  15520. + *
  15521. + * This program, aufs is free software; you can redistribute it and/or modify
  15522. + * it under the terms of the GNU General Public License as published by
  15523. + * the Free Software Foundation; either version 2 of the License, or
  15524. + * (at your option) any later version.
  15525. + *
  15526. + * This program is distributed in the hope that it will be useful,
  15527. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15528. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15529. + * GNU General Public License for more details.
  15530. + *
  15531. + * You should have received a copy of the GNU General Public License
  15532. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15533. + */
  15534. +
  15535. +/*
  15536. + * inode operations
  15537. + */
  15538. +
  15539. +#ifndef __AUFS_INODE_H__
  15540. +#define __AUFS_INODE_H__
  15541. +
  15542. +#ifdef __KERNEL__
  15543. +
  15544. +#include <linux/fsnotify.h>
  15545. +#include "rwsem.h"
  15546. +
  15547. +struct vfsmount;
  15548. +
  15549. +struct au_hnotify {
  15550. +#ifdef CONFIG_AUFS_HNOTIFY
  15551. +#ifdef CONFIG_AUFS_HFSNOTIFY
  15552. + /* never use fsnotify_add_vfsmount_mark() */
  15553. + struct fsnotify_mark hn_mark;
  15554. +#endif
  15555. + union {
  15556. + struct inode *hn_aufs_inode; /* no get/put */
  15557. + struct llist_node hn_lnode; /* delayed free */
  15558. + };
  15559. +#endif
  15560. +} ____cacheline_aligned_in_smp;
  15561. +
  15562. +struct au_hinode {
  15563. + struct inode *hi_inode;
  15564. + aufs_bindex_t hi_id;
  15565. +#ifdef CONFIG_AUFS_HNOTIFY
  15566. + struct au_hnotify *hi_notify;
  15567. +#endif
  15568. +
  15569. + /* reference to the copied-up whiteout with get/put */
  15570. + struct dentry *hi_whdentry;
  15571. +};
  15572. +
  15573. +/* ig_flags */
  15574. +#define AuIG_HALF_REFRESHED 1
  15575. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  15576. +#define au_ig_fset(flags, name) \
  15577. + do { (flags) |= AuIG_##name; } while (0)
  15578. +#define au_ig_fclr(flags, name) \
  15579. + do { (flags) &= ~AuIG_##name; } while (0)
  15580. +
  15581. +struct au_iigen {
  15582. + spinlock_t ig_spin;
  15583. + __u32 ig_generation, ig_flags;
  15584. +};
  15585. +
  15586. +struct au_vdir;
  15587. +struct au_iinfo {
  15588. + struct au_iigen ii_generation;
  15589. + struct super_block *ii_hsb1; /* no get/put */
  15590. +
  15591. + struct au_rwsem ii_rwsem;
  15592. + aufs_bindex_t ii_btop, ii_bbot;
  15593. + __u32 ii_higen;
  15594. + struct au_hinode *ii_hinode;
  15595. + struct au_vdir *ii_vdir;
  15596. +};
  15597. +
  15598. +struct au_icntnr {
  15599. + struct au_iinfo iinfo;
  15600. + struct inode vfs_inode;
  15601. + union {
  15602. + struct hlist_node plink;
  15603. + struct llist_node lnode; /* delayed free */
  15604. + };
  15605. +} ____cacheline_aligned_in_smp;
  15606. +
  15607. +/* au_pin flags */
  15608. +#define AuPin_DI_LOCKED 1
  15609. +#define AuPin_MNT_WRITE (1 << 1)
  15610. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  15611. +#define au_fset_pin(flags, name) \
  15612. + do { (flags) |= AuPin_##name; } while (0)
  15613. +#define au_fclr_pin(flags, name) \
  15614. + do { (flags) &= ~AuPin_##name; } while (0)
  15615. +
  15616. +struct au_pin {
  15617. + /* input */
  15618. + struct dentry *dentry;
  15619. + unsigned int udba;
  15620. + unsigned char lsc_di, lsc_hi, flags;
  15621. + aufs_bindex_t bindex;
  15622. +
  15623. + /* output */
  15624. + struct dentry *parent;
  15625. + struct au_hinode *hdir;
  15626. + struct vfsmount *h_mnt;
  15627. +
  15628. + /* temporary unlock/relock for copyup */
  15629. + struct dentry *h_dentry, *h_parent;
  15630. + struct au_branch *br;
  15631. + struct task_struct *task;
  15632. +};
  15633. +
  15634. +void au_pin_hdir_unlock(struct au_pin *p);
  15635. +int au_pin_hdir_lock(struct au_pin *p);
  15636. +int au_pin_hdir_relock(struct au_pin *p);
  15637. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
  15638. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  15639. +void au_pin_hdir_release(struct au_pin *p);
  15640. +
  15641. +/* ---------------------------------------------------------------------- */
  15642. +
  15643. +static inline struct au_iinfo *au_ii(struct inode *inode)
  15644. +{
  15645. + BUG_ON(is_bad_inode(inode));
  15646. + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  15647. +}
  15648. +
  15649. +/* ---------------------------------------------------------------------- */
  15650. +
  15651. +/* inode.c */
  15652. +struct inode *au_igrab(struct inode *inode);
  15653. +void au_refresh_iop(struct inode *inode, int force_getattr);
  15654. +int au_refresh_hinode_self(struct inode *inode);
  15655. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  15656. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  15657. + unsigned int d_type, ino_t *ino);
  15658. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  15659. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  15660. + struct inode *inode);
  15661. +int au_test_h_perm(struct inode *h_inode, int mask);
  15662. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  15663. +
  15664. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  15665. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  15666. +{
  15667. +#ifdef CONFIG_AUFS_SHWH
  15668. + return au_ino(sb, bindex, h_ino, d_type, ino);
  15669. +#else
  15670. + return 0;
  15671. +#endif
  15672. +}
  15673. +
  15674. +/* i_op.c */
  15675. +enum {
  15676. + AuIop_SYMLINK,
  15677. + AuIop_DIR,
  15678. + AuIop_OTHER,
  15679. + AuIop_Last
  15680. +};
  15681. +extern struct inode_operations aufs_iop[AuIop_Last],
  15682. + aufs_iop_nogetattr[AuIop_Last];
  15683. +
  15684. +/* au_wr_dir flags */
  15685. +#define AuWrDir_ADD_ENTRY 1
  15686. +#define AuWrDir_ISDIR (1 << 1)
  15687. +#define AuWrDir_TMPFILE (1 << 2)
  15688. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  15689. +#define au_fset_wrdir(flags, name) \
  15690. + do { (flags) |= AuWrDir_##name; } while (0)
  15691. +#define au_fclr_wrdir(flags, name) \
  15692. + do { (flags) &= ~AuWrDir_##name; } while (0)
  15693. +
  15694. +struct au_wr_dir_args {
  15695. + aufs_bindex_t force_btgt;
  15696. + unsigned char flags;
  15697. +};
  15698. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  15699. + struct au_wr_dir_args *args);
  15700. +
  15701. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  15702. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  15703. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  15704. + unsigned int udba, unsigned char flags);
  15705. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  15706. + unsigned int udba, unsigned char flags) __must_check;
  15707. +int au_do_pin(struct au_pin *pin) __must_check;
  15708. +void au_unpin(struct au_pin *pin);
  15709. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  15710. +
  15711. +#define AuIcpup_DID_CPUP 1
  15712. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  15713. +#define au_fset_icpup(flags, name) \
  15714. + do { (flags) |= AuIcpup_##name; } while (0)
  15715. +#define au_fclr_icpup(flags, name) \
  15716. + do { (flags) &= ~AuIcpup_##name; } while (0)
  15717. +
  15718. +struct au_icpup_args {
  15719. + unsigned char flags;
  15720. + unsigned char pin_flags;
  15721. + aufs_bindex_t btgt;
  15722. + unsigned int udba;
  15723. + struct au_pin pin;
  15724. + struct path h_path;
  15725. + struct inode *h_inode;
  15726. +};
  15727. +
  15728. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  15729. + struct au_icpup_args *a);
  15730. +
  15731. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
  15732. +
  15733. +/* i_op_add.c */
  15734. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  15735. + struct dentry *h_parent, int isdir);
  15736. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  15737. + dev_t dev);
  15738. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  15739. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  15740. + bool want_excl);
  15741. +struct vfsub_aopen_args;
  15742. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  15743. + struct vfsub_aopen_args *args);
  15744. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  15745. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  15746. + struct dentry *dentry);
  15747. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  15748. +
  15749. +/* i_op_del.c */
  15750. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  15751. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  15752. + struct dentry *h_parent, int isdir);
  15753. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  15754. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  15755. +
  15756. +/* i_op_ren.c */
  15757. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  15758. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  15759. + struct inode *dir, struct dentry *dentry);
  15760. +
  15761. +/* iinfo.c */
  15762. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  15763. +void au_hiput(struct au_hinode *hinode);
  15764. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  15765. + struct dentry *h_wh);
  15766. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  15767. +
  15768. +/* hinode flags */
  15769. +#define AuHi_XINO 1
  15770. +#define AuHi_HNOTIFY (1 << 1)
  15771. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  15772. +#define au_fset_hi(flags, name) \
  15773. + do { (flags) |= AuHi_##name; } while (0)
  15774. +#define au_fclr_hi(flags, name) \
  15775. + do { (flags) &= ~AuHi_##name; } while (0)
  15776. +
  15777. +#ifndef CONFIG_AUFS_HNOTIFY
  15778. +#undef AuHi_HNOTIFY
  15779. +#define AuHi_HNOTIFY 0
  15780. +#endif
  15781. +
  15782. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  15783. + struct inode *h_inode, unsigned int flags);
  15784. +
  15785. +void au_update_iigen(struct inode *inode, int half);
  15786. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  15787. +
  15788. +void au_icntnr_init_once(void *_c);
  15789. +void au_hinode_init(struct au_hinode *hinode);
  15790. +int au_iinfo_init(struct inode *inode);
  15791. +void au_iinfo_fin(struct inode *inode);
  15792. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
  15793. +
  15794. +#ifdef CONFIG_PROC_FS
  15795. +/* plink.c */
  15796. +int au_plink_maint(struct super_block *sb, int flags);
  15797. +struct au_sbinfo;
  15798. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  15799. +int au_plink_maint_enter(struct super_block *sb);
  15800. +#ifdef CONFIG_AUFS_DEBUG
  15801. +void au_plink_list(struct super_block *sb);
  15802. +#else
  15803. +AuStubVoid(au_plink_list, struct super_block *sb)
  15804. +#endif
  15805. +int au_plink_test(struct inode *inode);
  15806. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  15807. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  15808. + struct dentry *h_dentry);
  15809. +void au_plink_put(struct super_block *sb, int verbose);
  15810. +void au_plink_clean(struct super_block *sb, int verbose);
  15811. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  15812. +#else
  15813. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  15814. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  15815. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  15816. +AuStubVoid(au_plink_list, struct super_block *sb);
  15817. +AuStubInt0(au_plink_test, struct inode *inode);
  15818. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  15819. + struct inode *inode, aufs_bindex_t bindex);
  15820. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  15821. + struct dentry *h_dentry);
  15822. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  15823. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  15824. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  15825. +#endif /* CONFIG_PROC_FS */
  15826. +
  15827. +#ifdef CONFIG_AUFS_XATTR
  15828. +/* xattr.c */
  15829. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  15830. + unsigned int verbose);
  15831. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  15832. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  15833. + size_t size);
  15834. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  15835. + size_t size, int flags);
  15836. +int aufs_removexattr(struct dentry *dentry, const char *name);
  15837. +
  15838. +/* void au_xattr_init(struct super_block *sb); */
  15839. +#else
  15840. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  15841. + int ignore_flags, unsigned int verbose);
  15842. +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
  15843. +#endif
  15844. +
  15845. +#ifdef CONFIG_FS_POSIX_ACL
  15846. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  15847. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  15848. +#endif
  15849. +
  15850. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  15851. +enum {
  15852. + AU_XATTR_SET,
  15853. + AU_XATTR_REMOVE,
  15854. + AU_ACL_SET
  15855. +};
  15856. +
  15857. +struct au_srxattr {
  15858. + int type;
  15859. + union {
  15860. + struct {
  15861. + const char *name;
  15862. + const void *value;
  15863. + size_t size;
  15864. + int flags;
  15865. + } set;
  15866. + struct {
  15867. + const char *name;
  15868. + } remove;
  15869. + struct {
  15870. + struct posix_acl *acl;
  15871. + int type;
  15872. + } acl_set;
  15873. + } u;
  15874. +};
  15875. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
  15876. +#endif
  15877. +
  15878. +/* ---------------------------------------------------------------------- */
  15879. +
  15880. +/* lock subclass for iinfo */
  15881. +enum {
  15882. + AuLsc_II_CHILD, /* child first */
  15883. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  15884. + AuLsc_II_CHILD3, /* copyup dirs */
  15885. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  15886. + AuLsc_II_PARENT2,
  15887. + AuLsc_II_PARENT3, /* copyup dirs */
  15888. + AuLsc_II_NEW_CHILD
  15889. +};
  15890. +
  15891. +/*
  15892. + * ii_read_lock_child, ii_write_lock_child,
  15893. + * ii_read_lock_child2, ii_write_lock_child2,
  15894. + * ii_read_lock_child3, ii_write_lock_child3,
  15895. + * ii_read_lock_parent, ii_write_lock_parent,
  15896. + * ii_read_lock_parent2, ii_write_lock_parent2,
  15897. + * ii_read_lock_parent3, ii_write_lock_parent3,
  15898. + * ii_read_lock_new_child, ii_write_lock_new_child,
  15899. + */
  15900. +#define AuReadLockFunc(name, lsc) \
  15901. +static inline void ii_read_lock_##name(struct inode *i) \
  15902. +{ \
  15903. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15904. +}
  15905. +
  15906. +#define AuWriteLockFunc(name, lsc) \
  15907. +static inline void ii_write_lock_##name(struct inode *i) \
  15908. +{ \
  15909. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15910. +}
  15911. +
  15912. +#define AuRWLockFuncs(name, lsc) \
  15913. + AuReadLockFunc(name, lsc) \
  15914. + AuWriteLockFunc(name, lsc)
  15915. +
  15916. +AuRWLockFuncs(child, CHILD);
  15917. +AuRWLockFuncs(child2, CHILD2);
  15918. +AuRWLockFuncs(child3, CHILD3);
  15919. +AuRWLockFuncs(parent, PARENT);
  15920. +AuRWLockFuncs(parent2, PARENT2);
  15921. +AuRWLockFuncs(parent3, PARENT3);
  15922. +AuRWLockFuncs(new_child, NEW_CHILD);
  15923. +
  15924. +#undef AuReadLockFunc
  15925. +#undef AuWriteLockFunc
  15926. +#undef AuRWLockFuncs
  15927. +
  15928. +/*
  15929. + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
  15930. + */
  15931. +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
  15932. +
  15933. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  15934. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  15935. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  15936. +
  15937. +/* ---------------------------------------------------------------------- */
  15938. +
  15939. +static inline void au_icntnr_init(struct au_icntnr *c)
  15940. +{
  15941. +#ifdef CONFIG_AUFS_DEBUG
  15942. + c->vfs_inode.i_mode = 0;
  15943. +#endif
  15944. +}
  15945. +
  15946. +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
  15947. +{
  15948. + unsigned int gen;
  15949. + struct au_iinfo *iinfo;
  15950. + struct au_iigen *iigen;
  15951. +
  15952. + iinfo = au_ii(inode);
  15953. + iigen = &iinfo->ii_generation;
  15954. + spin_lock(&iigen->ig_spin);
  15955. + if (igflags)
  15956. + *igflags = iigen->ig_flags;
  15957. + gen = iigen->ig_generation;
  15958. + spin_unlock(&iigen->ig_spin);
  15959. +
  15960. + return gen;
  15961. +}
  15962. +
  15963. +/* tiny test for inode number */
  15964. +/* tmpfs generation is too rough */
  15965. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  15966. +{
  15967. + struct au_iinfo *iinfo;
  15968. +
  15969. + iinfo = au_ii(inode);
  15970. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  15971. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  15972. + && iinfo->ii_higen == h_inode->i_generation);
  15973. +}
  15974. +
  15975. +static inline void au_iigen_dec(struct inode *inode)
  15976. +{
  15977. + struct au_iinfo *iinfo;
  15978. + struct au_iigen *iigen;
  15979. +
  15980. + iinfo = au_ii(inode);
  15981. + iigen = &iinfo->ii_generation;
  15982. + spin_lock(&iigen->ig_spin);
  15983. + iigen->ig_generation--;
  15984. + spin_unlock(&iigen->ig_spin);
  15985. +}
  15986. +
  15987. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  15988. +{
  15989. + int err;
  15990. +
  15991. + err = 0;
  15992. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  15993. + err = -EIO;
  15994. +
  15995. + return err;
  15996. +}
  15997. +
  15998. +/* ---------------------------------------------------------------------- */
  15999. +
  16000. +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
  16001. + aufs_bindex_t bindex)
  16002. +{
  16003. + return iinfo->ii_hinode + bindex;
  16004. +}
  16005. +
  16006. +static inline int au_is_bad_inode(struct inode *inode)
  16007. +{
  16008. + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
  16009. +}
  16010. +
  16011. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  16012. + aufs_bindex_t bindex)
  16013. +{
  16014. + IiMustAnyLock(inode);
  16015. + return au_hinode(au_ii(inode), bindex)->hi_id;
  16016. +}
  16017. +
  16018. +static inline aufs_bindex_t au_ibtop(struct inode *inode)
  16019. +{
  16020. + IiMustAnyLock(inode);
  16021. + return au_ii(inode)->ii_btop;
  16022. +}
  16023. +
  16024. +static inline aufs_bindex_t au_ibbot(struct inode *inode)
  16025. +{
  16026. + IiMustAnyLock(inode);
  16027. + return au_ii(inode)->ii_bbot;
  16028. +}
  16029. +
  16030. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  16031. +{
  16032. + IiMustAnyLock(inode);
  16033. + return au_ii(inode)->ii_vdir;
  16034. +}
  16035. +
  16036. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  16037. +{
  16038. + IiMustAnyLock(inode);
  16039. + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
  16040. +}
  16041. +
  16042. +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
  16043. +{
  16044. + IiMustWriteLock(inode);
  16045. + au_ii(inode)->ii_btop = bindex;
  16046. +}
  16047. +
  16048. +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
  16049. +{
  16050. + IiMustWriteLock(inode);
  16051. + au_ii(inode)->ii_bbot = bindex;
  16052. +}
  16053. +
  16054. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  16055. +{
  16056. + IiMustWriteLock(inode);
  16057. + au_ii(inode)->ii_vdir = vdir;
  16058. +}
  16059. +
  16060. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  16061. +{
  16062. + IiMustAnyLock(inode);
  16063. + return au_hinode(au_ii(inode), bindex);
  16064. +}
  16065. +
  16066. +/* ---------------------------------------------------------------------- */
  16067. +
  16068. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  16069. +{
  16070. + if (pin)
  16071. + return pin->parent;
  16072. + return NULL;
  16073. +}
  16074. +
  16075. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  16076. +{
  16077. + if (pin && pin->hdir)
  16078. + return pin->hdir->hi_inode;
  16079. + return NULL;
  16080. +}
  16081. +
  16082. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  16083. +{
  16084. + if (pin)
  16085. + return pin->hdir;
  16086. + return NULL;
  16087. +}
  16088. +
  16089. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  16090. +{
  16091. + if (pin)
  16092. + pin->dentry = dentry;
  16093. +}
  16094. +
  16095. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  16096. + unsigned char lflag)
  16097. +{
  16098. + if (pin) {
  16099. + if (lflag)
  16100. + au_fset_pin(pin->flags, DI_LOCKED);
  16101. + else
  16102. + au_fclr_pin(pin->flags, DI_LOCKED);
  16103. + }
  16104. +}
  16105. +
  16106. +#if 0 /* reserved */
  16107. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  16108. +{
  16109. + if (pin) {
  16110. + dput(pin->parent);
  16111. + pin->parent = dget(parent);
  16112. + }
  16113. +}
  16114. +#endif
  16115. +
  16116. +/* ---------------------------------------------------------------------- */
  16117. +
  16118. +struct au_branch;
  16119. +#ifdef CONFIG_AUFS_HNOTIFY
  16120. +struct au_hnotify_op {
  16121. + void (*ctl)(struct au_hinode *hinode, int do_set);
  16122. + int (*alloc)(struct au_hinode *hinode);
  16123. +
  16124. + /*
  16125. + * if it returns true, the the caller should free hinode->hi_notify,
  16126. + * otherwise ->free() frees it.
  16127. + */
  16128. + int (*free)(struct au_hinode *hinode,
  16129. + struct au_hnotify *hn) __must_check;
  16130. +
  16131. + void (*fin)(void);
  16132. + int (*init)(void);
  16133. +
  16134. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  16135. + void (*fin_br)(struct au_branch *br);
  16136. + int (*init_br)(struct au_branch *br, int perm);
  16137. +};
  16138. +
  16139. +/* hnotify.c */
  16140. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  16141. +void au_hn_free(struct au_hinode *hinode);
  16142. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  16143. +void au_hn_reset(struct inode *inode, unsigned int flags);
  16144. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  16145. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  16146. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  16147. +int au_hnotify_init_br(struct au_branch *br, int perm);
  16148. +void au_hnotify_fin_br(struct au_branch *br);
  16149. +int __init au_hnotify_init(void);
  16150. +void au_hnotify_fin(void);
  16151. +
  16152. +/* hfsnotify.c */
  16153. +extern const struct au_hnotify_op au_hnotify_op;
  16154. +
  16155. +static inline
  16156. +void au_hn_init(struct au_hinode *hinode)
  16157. +{
  16158. + hinode->hi_notify = NULL;
  16159. +}
  16160. +
  16161. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  16162. +{
  16163. + return hinode->hi_notify;
  16164. +}
  16165. +
  16166. +#else
  16167. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  16168. + struct au_hinode *hinode __maybe_unused,
  16169. + struct inode *inode __maybe_unused)
  16170. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  16171. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  16172. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  16173. + int do_set __maybe_unused)
  16174. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  16175. + unsigned int flags __maybe_unused)
  16176. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  16177. + struct au_branch *br __maybe_unused,
  16178. + int perm __maybe_unused)
  16179. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  16180. + int perm __maybe_unused)
  16181. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  16182. +AuStubInt0(__init au_hnotify_init, void)
  16183. +AuStubVoid(au_hnotify_fin, void)
  16184. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  16185. +#endif /* CONFIG_AUFS_HNOTIFY */
  16186. +
  16187. +static inline void au_hn_suspend(struct au_hinode *hdir)
  16188. +{
  16189. + au_hn_ctl(hdir, /*do_set*/0);
  16190. +}
  16191. +
  16192. +static inline void au_hn_resume(struct au_hinode *hdir)
  16193. +{
  16194. + au_hn_ctl(hdir, /*do_set*/1);
  16195. +}
  16196. +
  16197. +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
  16198. +{
  16199. + mutex_lock(&hdir->hi_inode->i_mutex);
  16200. + au_hn_suspend(hdir);
  16201. +}
  16202. +
  16203. +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
  16204. + unsigned int sc __maybe_unused)
  16205. +{
  16206. + mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
  16207. + au_hn_suspend(hdir);
  16208. +}
  16209. +
  16210. +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
  16211. +{
  16212. + au_hn_resume(hdir);
  16213. + mutex_unlock(&hdir->hi_inode->i_mutex);
  16214. +}
  16215. +
  16216. +#endif /* __KERNEL__ */
  16217. +#endif /* __AUFS_INODE_H__ */
  16218. --- /dev/null
  16219. +++ linux-4.4/fs/aufs/ioctl.c 2016-10-23 11:20:57.638826618 +0300
  16220. @@ -0,0 +1,219 @@
  16221. +/*
  16222. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  16223. + *
  16224. + * This program, aufs is free software; you can redistribute it and/or modify
  16225. + * it under the terms of the GNU General Public License as published by
  16226. + * the Free Software Foundation; either version 2 of the License, or
  16227. + * (at your option) any later version.
  16228. + *
  16229. + * This program is distributed in the hope that it will be useful,
  16230. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16231. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16232. + * GNU General Public License for more details.
  16233. + *
  16234. + * You should have received a copy of the GNU General Public License
  16235. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16236. + */
  16237. +
  16238. +/*
  16239. + * ioctl
  16240. + * plink-management and readdir in userspace.
  16241. + * assist the pathconf(3) wrapper library.
  16242. + * move-down
  16243. + * File-based Hierarchical Storage Management.
  16244. + */
  16245. +
  16246. +#include <linux/compat.h>
  16247. +#include <linux/file.h>
  16248. +#include "aufs.h"
  16249. +
  16250. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  16251. +{
  16252. + int err, fd;
  16253. + aufs_bindex_t wbi, bindex, bbot;
  16254. + struct file *h_file;
  16255. + struct super_block *sb;
  16256. + struct dentry *root;
  16257. + struct au_branch *br;
  16258. + struct aufs_wbr_fd wbrfd = {
  16259. + .oflags = au_dir_roflags,
  16260. + .brid = -1
  16261. + };
  16262. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  16263. + | O_NOATIME | O_CLOEXEC;
  16264. +
  16265. + AuDebugOn(wbrfd.oflags & ~valid);
  16266. +
  16267. + if (arg) {
  16268. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  16269. + if (unlikely(err)) {
  16270. + err = -EFAULT;
  16271. + goto out;
  16272. + }
  16273. +
  16274. + err = -EINVAL;
  16275. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  16276. + wbrfd.oflags |= au_dir_roflags;
  16277. + AuDbg("0%o\n", wbrfd.oflags);
  16278. + if (unlikely(wbrfd.oflags & ~valid))
  16279. + goto out;
  16280. + }
  16281. +
  16282. + fd = get_unused_fd_flags(0);
  16283. + err = fd;
  16284. + if (unlikely(fd < 0))
  16285. + goto out;
  16286. +
  16287. + h_file = ERR_PTR(-EINVAL);
  16288. + wbi = 0;
  16289. + br = NULL;
  16290. + sb = path->dentry->d_sb;
  16291. + root = sb->s_root;
  16292. + aufs_read_lock(root, AuLock_IR);
  16293. + bbot = au_sbbot(sb);
  16294. + if (wbrfd.brid >= 0) {
  16295. + wbi = au_br_index(sb, wbrfd.brid);
  16296. + if (unlikely(wbi < 0 || wbi > bbot))
  16297. + goto out_unlock;
  16298. + }
  16299. +
  16300. + h_file = ERR_PTR(-ENOENT);
  16301. + br = au_sbr(sb, wbi);
  16302. + if (!au_br_writable(br->br_perm)) {
  16303. + if (arg)
  16304. + goto out_unlock;
  16305. +
  16306. + bindex = wbi + 1;
  16307. + wbi = -1;
  16308. + for (; bindex <= bbot; bindex++) {
  16309. + br = au_sbr(sb, bindex);
  16310. + if (au_br_writable(br->br_perm)) {
  16311. + wbi = bindex;
  16312. + br = au_sbr(sb, wbi);
  16313. + break;
  16314. + }
  16315. + }
  16316. + }
  16317. + AuDbg("wbi %d\n", wbi);
  16318. + if (wbi >= 0)
  16319. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  16320. + /*force_wr*/0);
  16321. +
  16322. +out_unlock:
  16323. + aufs_read_unlock(root, AuLock_IR);
  16324. + err = PTR_ERR(h_file);
  16325. + if (IS_ERR(h_file))
  16326. + goto out_fd;
  16327. +
  16328. + au_br_put(br); /* cf. au_h_open() */
  16329. + fd_install(fd, h_file);
  16330. + err = fd;
  16331. + goto out; /* success */
  16332. +
  16333. +out_fd:
  16334. + put_unused_fd(fd);
  16335. +out:
  16336. + AuTraceErr(err);
  16337. + return err;
  16338. +}
  16339. +
  16340. +/* ---------------------------------------------------------------------- */
  16341. +
  16342. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  16343. +{
  16344. + long err;
  16345. + struct dentry *dentry;
  16346. +
  16347. + switch (cmd) {
  16348. + case AUFS_CTL_RDU:
  16349. + case AUFS_CTL_RDU_INO:
  16350. + err = au_rdu_ioctl(file, cmd, arg);
  16351. + break;
  16352. +
  16353. + case AUFS_CTL_WBR_FD:
  16354. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  16355. + break;
  16356. +
  16357. + case AUFS_CTL_IBUSY:
  16358. + err = au_ibusy_ioctl(file, arg);
  16359. + break;
  16360. +
  16361. + case AUFS_CTL_BRINFO:
  16362. + err = au_brinfo_ioctl(file, arg);
  16363. + break;
  16364. +
  16365. + case AUFS_CTL_FHSM_FD:
  16366. + dentry = file->f_path.dentry;
  16367. + if (IS_ROOT(dentry))
  16368. + err = au_fhsm_fd(dentry->d_sb, arg);
  16369. + else
  16370. + err = -ENOTTY;
  16371. + break;
  16372. +
  16373. + default:
  16374. + /* do not call the lower */
  16375. + AuDbg("0x%x\n", cmd);
  16376. + err = -ENOTTY;
  16377. + }
  16378. +
  16379. + AuTraceErr(err);
  16380. + return err;
  16381. +}
  16382. +
  16383. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  16384. +{
  16385. + long err;
  16386. +
  16387. + switch (cmd) {
  16388. + case AUFS_CTL_MVDOWN:
  16389. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  16390. + break;
  16391. +
  16392. + case AUFS_CTL_WBR_FD:
  16393. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  16394. + break;
  16395. +
  16396. + default:
  16397. + /* do not call the lower */
  16398. + AuDbg("0x%x\n", cmd);
  16399. + err = -ENOTTY;
  16400. + }
  16401. +
  16402. + AuTraceErr(err);
  16403. + return err;
  16404. +}
  16405. +
  16406. +#ifdef CONFIG_COMPAT
  16407. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  16408. + unsigned long arg)
  16409. +{
  16410. + long err;
  16411. +
  16412. + switch (cmd) {
  16413. + case AUFS_CTL_RDU:
  16414. + case AUFS_CTL_RDU_INO:
  16415. + err = au_rdu_compat_ioctl(file, cmd, arg);
  16416. + break;
  16417. +
  16418. + case AUFS_CTL_IBUSY:
  16419. + err = au_ibusy_compat_ioctl(file, arg);
  16420. + break;
  16421. +
  16422. + case AUFS_CTL_BRINFO:
  16423. + err = au_brinfo_compat_ioctl(file, arg);
  16424. + break;
  16425. +
  16426. + default:
  16427. + err = aufs_ioctl_dir(file, cmd, arg);
  16428. + }
  16429. +
  16430. + AuTraceErr(err);
  16431. + return err;
  16432. +}
  16433. +
  16434. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  16435. + unsigned long arg)
  16436. +{
  16437. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  16438. +}
  16439. +#endif
  16440. --- /dev/null
  16441. +++ linux-4.4/fs/aufs/i_op_add.c 2016-10-23 11:20:57.635493285 +0300
  16442. @@ -0,0 +1,924 @@
  16443. +/*
  16444. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  16445. + *
  16446. + * This program, aufs is free software; you can redistribute it and/or modify
  16447. + * it under the terms of the GNU General Public License as published by
  16448. + * the Free Software Foundation; either version 2 of the License, or
  16449. + * (at your option) any later version.
  16450. + *
  16451. + * This program is distributed in the hope that it will be useful,
  16452. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16453. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16454. + * GNU General Public License for more details.
  16455. + *
  16456. + * You should have received a copy of the GNU General Public License
  16457. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16458. + */
  16459. +
  16460. +/*
  16461. + * inode operations (add entry)
  16462. + */
  16463. +
  16464. +#include "aufs.h"
  16465. +
  16466. +/*
  16467. + * final procedure of adding a new entry, except link(2).
  16468. + * remove whiteout, instantiate, copyup the parent dir's times and size
  16469. + * and update version.
  16470. + * if it failed, re-create the removed whiteout.
  16471. + */
  16472. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  16473. + struct dentry *wh_dentry, struct dentry *dentry)
  16474. +{
  16475. + int err, rerr;
  16476. + aufs_bindex_t bwh;
  16477. + struct path h_path;
  16478. + struct super_block *sb;
  16479. + struct inode *inode, *h_dir;
  16480. + struct dentry *wh;
  16481. +
  16482. + bwh = -1;
  16483. + sb = dir->i_sb;
  16484. + if (wh_dentry) {
  16485. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  16486. + IMustLock(h_dir);
  16487. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  16488. + bwh = au_dbwh(dentry);
  16489. + h_path.dentry = wh_dentry;
  16490. + h_path.mnt = au_sbr_mnt(sb, bindex);
  16491. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  16492. + dentry);
  16493. + if (unlikely(err))
  16494. + goto out;
  16495. + }
  16496. +
  16497. + inode = au_new_inode(dentry, /*must_new*/1);
  16498. + if (!IS_ERR(inode)) {
  16499. + d_instantiate(dentry, inode);
  16500. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  16501. + IMustLock(dir);
  16502. + au_dir_ts(dir, bindex);
  16503. + dir->i_version++;
  16504. + au_fhsm_wrote(sb, bindex, /*force*/0);
  16505. + return 0; /* success */
  16506. + }
  16507. +
  16508. + err = PTR_ERR(inode);
  16509. + if (!wh_dentry)
  16510. + goto out;
  16511. +
  16512. + /* revert */
  16513. + /* dir inode is locked */
  16514. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  16515. + rerr = PTR_ERR(wh);
  16516. + if (IS_ERR(wh)) {
  16517. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  16518. + dentry, err, rerr);
  16519. + err = -EIO;
  16520. + } else
  16521. + dput(wh);
  16522. +
  16523. +out:
  16524. + return err;
  16525. +}
  16526. +
  16527. +static int au_d_may_add(struct dentry *dentry)
  16528. +{
  16529. + int err;
  16530. +
  16531. + err = 0;
  16532. + if (unlikely(d_unhashed(dentry)))
  16533. + err = -ENOENT;
  16534. + if (unlikely(d_really_is_positive(dentry)))
  16535. + err = -EEXIST;
  16536. + return err;
  16537. +}
  16538. +
  16539. +/*
  16540. + * simple tests for the adding inode operations.
  16541. + * following the checks in vfs, plus the parent-child relationship.
  16542. + */
  16543. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  16544. + struct dentry *h_parent, int isdir)
  16545. +{
  16546. + int err;
  16547. + umode_t h_mode;
  16548. + struct dentry *h_dentry;
  16549. + struct inode *h_inode;
  16550. +
  16551. + err = -ENAMETOOLONG;
  16552. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16553. + goto out;
  16554. +
  16555. + h_dentry = au_h_dptr(dentry, bindex);
  16556. + if (d_really_is_negative(dentry)) {
  16557. + err = -EEXIST;
  16558. + if (unlikely(d_is_positive(h_dentry)))
  16559. + goto out;
  16560. + } else {
  16561. + /* rename(2) case */
  16562. + err = -EIO;
  16563. + if (unlikely(d_is_negative(h_dentry)))
  16564. + goto out;
  16565. + h_inode = d_inode(h_dentry);
  16566. + if (unlikely(!h_inode->i_nlink))
  16567. + goto out;
  16568. +
  16569. + h_mode = h_inode->i_mode;
  16570. + if (!isdir) {
  16571. + err = -EISDIR;
  16572. + if (unlikely(S_ISDIR(h_mode)))
  16573. + goto out;
  16574. + } else if (unlikely(!S_ISDIR(h_mode))) {
  16575. + err = -ENOTDIR;
  16576. + goto out;
  16577. + }
  16578. + }
  16579. +
  16580. + err = 0;
  16581. + /* expected parent dir is locked */
  16582. + if (unlikely(h_parent != h_dentry->d_parent))
  16583. + err = -EIO;
  16584. +
  16585. +out:
  16586. + AuTraceErr(err);
  16587. + return err;
  16588. +}
  16589. +
  16590. +/*
  16591. + * initial procedure of adding a new entry.
  16592. + * prepare writable branch and the parent dir, lock it,
  16593. + * and lookup whiteout for the new entry.
  16594. + */
  16595. +static struct dentry*
  16596. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  16597. + struct dentry *src_dentry, struct au_pin *pin,
  16598. + struct au_wr_dir_args *wr_dir_args)
  16599. +{
  16600. + struct dentry *wh_dentry, *h_parent;
  16601. + struct super_block *sb;
  16602. + struct au_branch *br;
  16603. + int err;
  16604. + unsigned int udba;
  16605. + aufs_bindex_t bcpup;
  16606. +
  16607. + AuDbg("%pd\n", dentry);
  16608. +
  16609. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  16610. + bcpup = err;
  16611. + wh_dentry = ERR_PTR(err);
  16612. + if (unlikely(err < 0))
  16613. + goto out;
  16614. +
  16615. + sb = dentry->d_sb;
  16616. + udba = au_opt_udba(sb);
  16617. + err = au_pin(pin, dentry, bcpup, udba,
  16618. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16619. + wh_dentry = ERR_PTR(err);
  16620. + if (unlikely(err))
  16621. + goto out;
  16622. +
  16623. + h_parent = au_pinned_h_parent(pin);
  16624. + if (udba != AuOpt_UDBA_NONE
  16625. + && au_dbtop(dentry) == bcpup)
  16626. + err = au_may_add(dentry, bcpup, h_parent,
  16627. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  16628. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16629. + err = -ENAMETOOLONG;
  16630. + wh_dentry = ERR_PTR(err);
  16631. + if (unlikely(err))
  16632. + goto out_unpin;
  16633. +
  16634. + br = au_sbr(sb, bcpup);
  16635. + if (dt) {
  16636. + struct path tmp = {
  16637. + .dentry = h_parent,
  16638. + .mnt = au_br_mnt(br)
  16639. + };
  16640. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  16641. + }
  16642. +
  16643. + wh_dentry = NULL;
  16644. + if (bcpup != au_dbwh(dentry))
  16645. + goto out; /* success */
  16646. +
  16647. + /*
  16648. + * ENAMETOOLONG here means that if we allowed create such name, then it
  16649. + * would not be able to removed in the future. So we don't allow such
  16650. + * name here and we don't handle ENAMETOOLONG differently here.
  16651. + */
  16652. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  16653. +
  16654. +out_unpin:
  16655. + if (IS_ERR(wh_dentry))
  16656. + au_unpin(pin);
  16657. +out:
  16658. + return wh_dentry;
  16659. +}
  16660. +
  16661. +/* ---------------------------------------------------------------------- */
  16662. +
  16663. +enum { Mknod, Symlink, Creat };
  16664. +struct simple_arg {
  16665. + int type;
  16666. + union {
  16667. + struct {
  16668. + umode_t mode;
  16669. + bool want_excl;
  16670. + bool try_aopen;
  16671. + struct vfsub_aopen_args *aopen;
  16672. + } c;
  16673. + struct {
  16674. + const char *symname;
  16675. + } s;
  16676. + struct {
  16677. + umode_t mode;
  16678. + dev_t dev;
  16679. + } m;
  16680. + } u;
  16681. +};
  16682. +
  16683. +static int add_simple(struct inode *dir, struct dentry *dentry,
  16684. + struct simple_arg *arg)
  16685. +{
  16686. + int err, rerr;
  16687. + aufs_bindex_t btop;
  16688. + unsigned char created;
  16689. + const unsigned char try_aopen
  16690. + = (arg->type == Creat && arg->u.c.try_aopen);
  16691. + struct dentry *wh_dentry, *parent;
  16692. + struct inode *h_dir;
  16693. + struct super_block *sb;
  16694. + struct au_branch *br;
  16695. + /* to reuduce stack size */
  16696. + struct {
  16697. + struct au_dtime dt;
  16698. + struct au_pin pin;
  16699. + struct path h_path;
  16700. + struct au_wr_dir_args wr_dir_args;
  16701. + } *a;
  16702. +
  16703. + AuDbg("%pd\n", dentry);
  16704. + IMustLock(dir);
  16705. +
  16706. + err = -ENOMEM;
  16707. + a = kmalloc(sizeof(*a), GFP_NOFS);
  16708. + if (unlikely(!a))
  16709. + goto out;
  16710. + a->wr_dir_args.force_btgt = -1;
  16711. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  16712. +
  16713. + parent = dentry->d_parent; /* dir inode is locked */
  16714. + if (!try_aopen) {
  16715. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  16716. + if (unlikely(err))
  16717. + goto out_free;
  16718. + }
  16719. + err = au_d_may_add(dentry);
  16720. + if (unlikely(err))
  16721. + goto out_unlock;
  16722. + if (!try_aopen)
  16723. + di_write_lock_parent(parent);
  16724. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  16725. + &a->pin, &a->wr_dir_args);
  16726. + err = PTR_ERR(wh_dentry);
  16727. + if (IS_ERR(wh_dentry))
  16728. + goto out_parent;
  16729. +
  16730. + btop = au_dbtop(dentry);
  16731. + sb = dentry->d_sb;
  16732. + br = au_sbr(sb, btop);
  16733. + a->h_path.dentry = au_h_dptr(dentry, btop);
  16734. + a->h_path.mnt = au_br_mnt(br);
  16735. + h_dir = au_pinned_h_dir(&a->pin);
  16736. + switch (arg->type) {
  16737. + case Creat:
  16738. + err = 0;
  16739. + if (!try_aopen || !h_dir->i_op->atomic_open)
  16740. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  16741. + arg->u.c.want_excl);
  16742. + else
  16743. + err = vfsub_atomic_open(h_dir, a->h_path.dentry,
  16744. + arg->u.c.aopen, br);
  16745. + break;
  16746. + case Symlink:
  16747. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  16748. + break;
  16749. + case Mknod:
  16750. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  16751. + arg->u.m.dev);
  16752. + break;
  16753. + default:
  16754. + BUG();
  16755. + }
  16756. + created = !err;
  16757. + if (!err)
  16758. + err = epilog(dir, btop, wh_dentry, dentry);
  16759. +
  16760. + /* revert */
  16761. + if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
  16762. + /* no delegation since it is just created */
  16763. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  16764. + /*force*/0);
  16765. + if (rerr) {
  16766. + AuIOErr("%pd revert failure(%d, %d)\n",
  16767. + dentry, err, rerr);
  16768. + err = -EIO;
  16769. + }
  16770. + au_dtime_revert(&a->dt);
  16771. + }
  16772. +
  16773. + if (!err && try_aopen && !h_dir->i_op->atomic_open)
  16774. + *arg->u.c.aopen->opened |= FILE_CREATED;
  16775. +
  16776. + au_unpin(&a->pin);
  16777. + dput(wh_dentry);
  16778. +
  16779. +out_parent:
  16780. + if (!try_aopen)
  16781. + di_write_unlock(parent);
  16782. +out_unlock:
  16783. + if (unlikely(err)) {
  16784. + au_update_dbtop(dentry);
  16785. + d_drop(dentry);
  16786. + }
  16787. + if (!try_aopen)
  16788. + aufs_read_unlock(dentry, AuLock_DW);
  16789. +out_free:
  16790. + au_delayed_kfree(a);
  16791. +out:
  16792. + return err;
  16793. +}
  16794. +
  16795. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  16796. + dev_t dev)
  16797. +{
  16798. + struct simple_arg arg = {
  16799. + .type = Mknod,
  16800. + .u.m = {
  16801. + .mode = mode,
  16802. + .dev = dev
  16803. + }
  16804. + };
  16805. + return add_simple(dir, dentry, &arg);
  16806. +}
  16807. +
  16808. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  16809. +{
  16810. + struct simple_arg arg = {
  16811. + .type = Symlink,
  16812. + .u.s.symname = symname
  16813. + };
  16814. + return add_simple(dir, dentry, &arg);
  16815. +}
  16816. +
  16817. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  16818. + bool want_excl)
  16819. +{
  16820. + struct simple_arg arg = {
  16821. + .type = Creat,
  16822. + .u.c = {
  16823. + .mode = mode,
  16824. + .want_excl = want_excl
  16825. + }
  16826. + };
  16827. + return add_simple(dir, dentry, &arg);
  16828. +}
  16829. +
  16830. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  16831. + struct vfsub_aopen_args *aopen_args)
  16832. +{
  16833. + struct simple_arg arg = {
  16834. + .type = Creat,
  16835. + .u.c = {
  16836. + .mode = aopen_args->create_mode,
  16837. + .want_excl = aopen_args->open_flag & O_EXCL,
  16838. + .try_aopen = true,
  16839. + .aopen = aopen_args
  16840. + }
  16841. + };
  16842. + return add_simple(dir, dentry, &arg);
  16843. +}
  16844. +
  16845. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  16846. +{
  16847. + int err;
  16848. + aufs_bindex_t bindex;
  16849. + struct super_block *sb;
  16850. + struct dentry *parent, *h_parent, *h_dentry;
  16851. + struct inode *h_dir, *inode;
  16852. + struct vfsmount *h_mnt;
  16853. + struct au_wr_dir_args wr_dir_args = {
  16854. + .force_btgt = -1,
  16855. + .flags = AuWrDir_TMPFILE
  16856. + };
  16857. +
  16858. + /* copy-up may happen */
  16859. + mutex_lock(&dir->i_mutex);
  16860. +
  16861. + sb = dir->i_sb;
  16862. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16863. + if (unlikely(err))
  16864. + goto out;
  16865. +
  16866. + err = au_di_init(dentry);
  16867. + if (unlikely(err))
  16868. + goto out_si;
  16869. +
  16870. + err = -EBUSY;
  16871. + parent = d_find_any_alias(dir);
  16872. + AuDebugOn(!parent);
  16873. + di_write_lock_parent(parent);
  16874. + if (unlikely(d_inode(parent) != dir))
  16875. + goto out_parent;
  16876. +
  16877. + err = au_digen_test(parent, au_sigen(sb));
  16878. + if (unlikely(err))
  16879. + goto out_parent;
  16880. +
  16881. + bindex = au_dbtop(parent);
  16882. + au_set_dbtop(dentry, bindex);
  16883. + au_set_dbbot(dentry, bindex);
  16884. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  16885. + bindex = err;
  16886. + if (unlikely(err < 0))
  16887. + goto out_parent;
  16888. +
  16889. + err = -EOPNOTSUPP;
  16890. + h_dir = au_h_iptr(dir, bindex);
  16891. + if (unlikely(!h_dir->i_op->tmpfile))
  16892. + goto out_parent;
  16893. +
  16894. + h_mnt = au_sbr_mnt(sb, bindex);
  16895. + err = vfsub_mnt_want_write(h_mnt);
  16896. + if (unlikely(err))
  16897. + goto out_parent;
  16898. +
  16899. + h_parent = au_h_dptr(parent, bindex);
  16900. + err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
  16901. + if (unlikely(err))
  16902. + goto out_mnt;
  16903. +
  16904. + err = -ENOMEM;
  16905. + h_dentry = d_alloc(h_parent, &dentry->d_name);
  16906. + if (unlikely(!h_dentry))
  16907. + goto out_mnt;
  16908. +
  16909. + err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
  16910. + if (unlikely(err))
  16911. + goto out_dentry;
  16912. +
  16913. + au_set_dbtop(dentry, bindex);
  16914. + au_set_dbbot(dentry, bindex);
  16915. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  16916. + inode = au_new_inode(dentry, /*must_new*/1);
  16917. + if (IS_ERR(inode)) {
  16918. + err = PTR_ERR(inode);
  16919. + au_set_h_dptr(dentry, bindex, NULL);
  16920. + au_set_dbtop(dentry, -1);
  16921. + au_set_dbbot(dentry, -1);
  16922. + } else {
  16923. + if (!inode->i_nlink)
  16924. + set_nlink(inode, 1);
  16925. + d_tmpfile(dentry, inode);
  16926. + au_di(dentry)->di_tmpfile = 1;
  16927. +
  16928. + /* update without i_mutex */
  16929. + if (au_ibtop(dir) == au_dbtop(dentry))
  16930. + au_cpup_attr_timesizes(dir);
  16931. + }
  16932. +
  16933. +out_dentry:
  16934. + dput(h_dentry);
  16935. +out_mnt:
  16936. + vfsub_mnt_drop_write(h_mnt);
  16937. +out_parent:
  16938. + di_write_unlock(parent);
  16939. + dput(parent);
  16940. + di_write_unlock(dentry);
  16941. + if (unlikely(err)) {
  16942. + au_di_fin(dentry);
  16943. + dentry->d_fsdata = NULL;
  16944. + }
  16945. +out_si:
  16946. + si_read_unlock(sb);
  16947. +out:
  16948. + mutex_unlock(&dir->i_mutex);
  16949. + return err;
  16950. +}
  16951. +
  16952. +/* ---------------------------------------------------------------------- */
  16953. +
  16954. +struct au_link_args {
  16955. + aufs_bindex_t bdst, bsrc;
  16956. + struct au_pin pin;
  16957. + struct path h_path;
  16958. + struct dentry *src_parent, *parent;
  16959. +};
  16960. +
  16961. +static int au_cpup_before_link(struct dentry *src_dentry,
  16962. + struct au_link_args *a)
  16963. +{
  16964. + int err;
  16965. + struct dentry *h_src_dentry;
  16966. + struct au_cp_generic cpg = {
  16967. + .dentry = src_dentry,
  16968. + .bdst = a->bdst,
  16969. + .bsrc = a->bsrc,
  16970. + .len = -1,
  16971. + .pin = &a->pin,
  16972. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  16973. + };
  16974. +
  16975. + di_read_lock_parent(a->src_parent, AuLock_IR);
  16976. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  16977. + if (unlikely(err))
  16978. + goto out;
  16979. +
  16980. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  16981. + err = au_pin(&a->pin, src_dentry, a->bdst,
  16982. + au_opt_udba(src_dentry->d_sb),
  16983. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16984. + if (unlikely(err))
  16985. + goto out;
  16986. +
  16987. + err = au_sio_cpup_simple(&cpg);
  16988. + au_unpin(&a->pin);
  16989. +
  16990. +out:
  16991. + di_read_unlock(a->src_parent, AuLock_IR);
  16992. + return err;
  16993. +}
  16994. +
  16995. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  16996. + struct au_link_args *a)
  16997. +{
  16998. + int err;
  16999. + unsigned char plink;
  17000. + aufs_bindex_t bbot;
  17001. + struct dentry *h_src_dentry;
  17002. + struct inode *h_inode, *inode, *delegated;
  17003. + struct super_block *sb;
  17004. + struct file *h_file;
  17005. +
  17006. + plink = 0;
  17007. + h_inode = NULL;
  17008. + sb = src_dentry->d_sb;
  17009. + inode = d_inode(src_dentry);
  17010. + if (au_ibtop(inode) <= a->bdst)
  17011. + h_inode = au_h_iptr(inode, a->bdst);
  17012. + if (!h_inode || !h_inode->i_nlink) {
  17013. + /* copyup src_dentry as the name of dentry. */
  17014. + bbot = au_dbbot(dentry);
  17015. + if (bbot < a->bsrc)
  17016. + au_set_dbbot(dentry, a->bsrc);
  17017. + au_set_h_dptr(dentry, a->bsrc,
  17018. + dget(au_h_dptr(src_dentry, a->bsrc)));
  17019. + dget(a->h_path.dentry);
  17020. + au_set_h_dptr(dentry, a->bdst, NULL);
  17021. + AuDbg("temporary d_inode...\n");
  17022. + spin_lock(&dentry->d_lock);
  17023. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  17024. + spin_unlock(&dentry->d_lock);
  17025. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  17026. + if (IS_ERR(h_file))
  17027. + err = PTR_ERR(h_file);
  17028. + else {
  17029. + struct au_cp_generic cpg = {
  17030. + .dentry = dentry,
  17031. + .bdst = a->bdst,
  17032. + .bsrc = -1,
  17033. + .len = -1,
  17034. + .pin = &a->pin,
  17035. + .flags = AuCpup_KEEPLINO
  17036. + };
  17037. + err = au_sio_cpup_simple(&cpg);
  17038. + au_h_open_post(dentry, a->bsrc, h_file);
  17039. + if (!err) {
  17040. + dput(a->h_path.dentry);
  17041. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  17042. + } else
  17043. + au_set_h_dptr(dentry, a->bdst,
  17044. + a->h_path.dentry);
  17045. + }
  17046. + spin_lock(&dentry->d_lock);
  17047. + dentry->d_inode = NULL; /* restore */
  17048. + spin_unlock(&dentry->d_lock);
  17049. + AuDbg("temporary d_inode...done\n");
  17050. + au_set_h_dptr(dentry, a->bsrc, NULL);
  17051. + au_set_dbbot(dentry, bbot);
  17052. + } else {
  17053. + /* the inode of src_dentry already exists on a.bdst branch */
  17054. + h_src_dentry = d_find_alias(h_inode);
  17055. + if (!h_src_dentry && au_plink_test(inode)) {
  17056. + plink = 1;
  17057. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  17058. + err = PTR_ERR(h_src_dentry);
  17059. + if (IS_ERR(h_src_dentry))
  17060. + goto out;
  17061. +
  17062. + if (unlikely(d_is_negative(h_src_dentry))) {
  17063. + dput(h_src_dentry);
  17064. + h_src_dentry = NULL;
  17065. + }
  17066. +
  17067. + }
  17068. + if (h_src_dentry) {
  17069. + delegated = NULL;
  17070. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  17071. + &a->h_path, &delegated);
  17072. + if (unlikely(err == -EWOULDBLOCK)) {
  17073. + pr_warn("cannot retry for NFSv4 delegation"
  17074. + " for an internal link\n");
  17075. + iput(delegated);
  17076. + }
  17077. + dput(h_src_dentry);
  17078. + } else {
  17079. + AuIOErr("no dentry found for hi%lu on b%d\n",
  17080. + h_inode->i_ino, a->bdst);
  17081. + err = -EIO;
  17082. + }
  17083. + }
  17084. +
  17085. + if (!err && !plink)
  17086. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  17087. +
  17088. +out:
  17089. + AuTraceErr(err);
  17090. + return err;
  17091. +}
  17092. +
  17093. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  17094. + struct dentry *dentry)
  17095. +{
  17096. + int err, rerr;
  17097. + struct au_dtime dt;
  17098. + struct au_link_args *a;
  17099. + struct dentry *wh_dentry, *h_src_dentry;
  17100. + struct inode *inode, *delegated;
  17101. + struct super_block *sb;
  17102. + struct au_wr_dir_args wr_dir_args = {
  17103. + /* .force_btgt = -1, */
  17104. + .flags = AuWrDir_ADD_ENTRY
  17105. + };
  17106. +
  17107. + IMustLock(dir);
  17108. + inode = d_inode(src_dentry);
  17109. + IMustLock(inode);
  17110. +
  17111. + err = -ENOMEM;
  17112. + a = kzalloc(sizeof(*a), GFP_NOFS);
  17113. + if (unlikely(!a))
  17114. + goto out;
  17115. +
  17116. + a->parent = dentry->d_parent; /* dir inode is locked */
  17117. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  17118. + AuLock_NOPLM | AuLock_GEN);
  17119. + if (unlikely(err))
  17120. + goto out_kfree;
  17121. + err = au_d_linkable(src_dentry);
  17122. + if (unlikely(err))
  17123. + goto out_unlock;
  17124. + err = au_d_may_add(dentry);
  17125. + if (unlikely(err))
  17126. + goto out_unlock;
  17127. +
  17128. + a->src_parent = dget_parent(src_dentry);
  17129. + wr_dir_args.force_btgt = au_ibtop(inode);
  17130. +
  17131. + di_write_lock_parent(a->parent);
  17132. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  17133. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  17134. + &wr_dir_args);
  17135. + err = PTR_ERR(wh_dentry);
  17136. + if (IS_ERR(wh_dentry))
  17137. + goto out_parent;
  17138. +
  17139. + err = 0;
  17140. + sb = dentry->d_sb;
  17141. + a->bdst = au_dbtop(dentry);
  17142. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  17143. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  17144. + a->bsrc = au_ibtop(inode);
  17145. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  17146. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  17147. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  17148. + if (!h_src_dentry) {
  17149. + a->bsrc = au_dbtop(src_dentry);
  17150. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  17151. + AuDebugOn(!h_src_dentry);
  17152. + } else if (IS_ERR(h_src_dentry)) {
  17153. + err = PTR_ERR(h_src_dentry);
  17154. + goto out_parent;
  17155. + }
  17156. +
  17157. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  17158. + if (a->bdst < a->bsrc
  17159. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  17160. + err = au_cpup_or_link(src_dentry, dentry, a);
  17161. + else {
  17162. + delegated = NULL;
  17163. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  17164. + &a->h_path, &delegated);
  17165. + if (unlikely(err == -EWOULDBLOCK)) {
  17166. + pr_warn("cannot retry for NFSv4 delegation"
  17167. + " for an internal link\n");
  17168. + iput(delegated);
  17169. + }
  17170. + }
  17171. + dput(h_src_dentry);
  17172. + } else {
  17173. + /*
  17174. + * copyup src_dentry to the branch we process,
  17175. + * and then link(2) to it.
  17176. + */
  17177. + dput(h_src_dentry);
  17178. + if (a->bdst < a->bsrc
  17179. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  17180. + au_unpin(&a->pin);
  17181. + di_write_unlock(a->parent);
  17182. + err = au_cpup_before_link(src_dentry, a);
  17183. + di_write_lock_parent(a->parent);
  17184. + if (!err)
  17185. + err = au_pin(&a->pin, dentry, a->bdst,
  17186. + au_opt_udba(sb),
  17187. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  17188. + if (unlikely(err))
  17189. + goto out_wh;
  17190. + }
  17191. + if (!err) {
  17192. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  17193. + err = -ENOENT;
  17194. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  17195. + delegated = NULL;
  17196. + err = vfsub_link(h_src_dentry,
  17197. + au_pinned_h_dir(&a->pin),
  17198. + &a->h_path, &delegated);
  17199. + if (unlikely(err == -EWOULDBLOCK)) {
  17200. + pr_warn("cannot retry"
  17201. + " for NFSv4 delegation"
  17202. + " for an internal link\n");
  17203. + iput(delegated);
  17204. + }
  17205. + }
  17206. + }
  17207. + }
  17208. + if (unlikely(err))
  17209. + goto out_unpin;
  17210. +
  17211. + if (wh_dentry) {
  17212. + a->h_path.dentry = wh_dentry;
  17213. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  17214. + dentry);
  17215. + if (unlikely(err))
  17216. + goto out_revert;
  17217. + }
  17218. +
  17219. + au_dir_ts(dir, a->bdst);
  17220. + dir->i_version++;
  17221. + inc_nlink(inode);
  17222. + inode->i_ctime = dir->i_ctime;
  17223. + d_instantiate(dentry, au_igrab(inode));
  17224. + if (d_unhashed(a->h_path.dentry))
  17225. + /* some filesystem calls d_drop() */
  17226. + d_drop(dentry);
  17227. + /* some filesystems consume an inode even hardlink */
  17228. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  17229. + goto out_unpin; /* success */
  17230. +
  17231. +out_revert:
  17232. + /* no delegation since it is just created */
  17233. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  17234. + /*delegated*/NULL, /*force*/0);
  17235. + if (unlikely(rerr)) {
  17236. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  17237. + err = -EIO;
  17238. + }
  17239. + au_dtime_revert(&dt);
  17240. +out_unpin:
  17241. + au_unpin(&a->pin);
  17242. +out_wh:
  17243. + dput(wh_dentry);
  17244. +out_parent:
  17245. + di_write_unlock(a->parent);
  17246. + dput(a->src_parent);
  17247. +out_unlock:
  17248. + if (unlikely(err)) {
  17249. + au_update_dbtop(dentry);
  17250. + d_drop(dentry);
  17251. + }
  17252. + aufs_read_and_write_unlock2(dentry, src_dentry);
  17253. +out_kfree:
  17254. + au_delayed_kfree(a);
  17255. +out:
  17256. + AuTraceErr(err);
  17257. + return err;
  17258. +}
  17259. +
  17260. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  17261. +{
  17262. + int err, rerr;
  17263. + aufs_bindex_t bindex;
  17264. + unsigned char diropq;
  17265. + struct path h_path;
  17266. + struct dentry *wh_dentry, *parent, *opq_dentry;
  17267. + struct mutex *h_mtx;
  17268. + struct super_block *sb;
  17269. + struct {
  17270. + struct au_pin pin;
  17271. + struct au_dtime dt;
  17272. + } *a; /* reduce the stack usage */
  17273. + struct au_wr_dir_args wr_dir_args = {
  17274. + .force_btgt = -1,
  17275. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  17276. + };
  17277. +
  17278. + IMustLock(dir);
  17279. +
  17280. + err = -ENOMEM;
  17281. + a = kmalloc(sizeof(*a), GFP_NOFS);
  17282. + if (unlikely(!a))
  17283. + goto out;
  17284. +
  17285. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  17286. + if (unlikely(err))
  17287. + goto out_free;
  17288. + err = au_d_may_add(dentry);
  17289. + if (unlikely(err))
  17290. + goto out_unlock;
  17291. +
  17292. + parent = dentry->d_parent; /* dir inode is locked */
  17293. + di_write_lock_parent(parent);
  17294. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  17295. + &a->pin, &wr_dir_args);
  17296. + err = PTR_ERR(wh_dentry);
  17297. + if (IS_ERR(wh_dentry))
  17298. + goto out_parent;
  17299. +
  17300. + sb = dentry->d_sb;
  17301. + bindex = au_dbtop(dentry);
  17302. + h_path.dentry = au_h_dptr(dentry, bindex);
  17303. + h_path.mnt = au_sbr_mnt(sb, bindex);
  17304. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  17305. + if (unlikely(err))
  17306. + goto out_unpin;
  17307. +
  17308. + /* make the dir opaque */
  17309. + diropq = 0;
  17310. + h_mtx = &d_inode(h_path.dentry)->i_mutex;
  17311. + if (wh_dentry
  17312. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  17313. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  17314. + opq_dentry = au_diropq_create(dentry, bindex);
  17315. + mutex_unlock(h_mtx);
  17316. + err = PTR_ERR(opq_dentry);
  17317. + if (IS_ERR(opq_dentry))
  17318. + goto out_dir;
  17319. + dput(opq_dentry);
  17320. + diropq = 1;
  17321. + }
  17322. +
  17323. + err = epilog(dir, bindex, wh_dentry, dentry);
  17324. + if (!err) {
  17325. + inc_nlink(dir);
  17326. + goto out_unpin; /* success */
  17327. + }
  17328. +
  17329. + /* revert */
  17330. + if (diropq) {
  17331. + AuLabel(revert opq);
  17332. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  17333. + rerr = au_diropq_remove(dentry, bindex);
  17334. + mutex_unlock(h_mtx);
  17335. + if (rerr) {
  17336. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  17337. + dentry, err, rerr);
  17338. + err = -EIO;
  17339. + }
  17340. + }
  17341. +
  17342. +out_dir:
  17343. + AuLabel(revert dir);
  17344. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  17345. + if (rerr) {
  17346. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  17347. + dentry, err, rerr);
  17348. + err = -EIO;
  17349. + }
  17350. + au_dtime_revert(&a->dt);
  17351. +out_unpin:
  17352. + au_unpin(&a->pin);
  17353. + dput(wh_dentry);
  17354. +out_parent:
  17355. + di_write_unlock(parent);
  17356. +out_unlock:
  17357. + if (unlikely(err)) {
  17358. + au_update_dbtop(dentry);
  17359. + d_drop(dentry);
  17360. + }
  17361. + aufs_read_unlock(dentry, AuLock_DW);
  17362. +out_free:
  17363. + au_delayed_kfree(a);
  17364. +out:
  17365. + return err;
  17366. +}
  17367. --- /dev/null
  17368. +++ linux-4.4/fs/aufs/i_op.c 2016-10-23 11:20:57.635493285 +0300
  17369. @@ -0,0 +1,1518 @@
  17370. +/*
  17371. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  17372. + *
  17373. + * This program, aufs is free software; you can redistribute it and/or modify
  17374. + * it under the terms of the GNU General Public License as published by
  17375. + * the Free Software Foundation; either version 2 of the License, or
  17376. + * (at your option) any later version.
  17377. + *
  17378. + * This program is distributed in the hope that it will be useful,
  17379. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17380. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17381. + * GNU General Public License for more details.
  17382. + *
  17383. + * You should have received a copy of the GNU General Public License
  17384. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17385. + */
  17386. +
  17387. +/*
  17388. + * inode operations (except add/del/rename)
  17389. + */
  17390. +
  17391. +#include <linux/device_cgroup.h>
  17392. +#include <linux/fs_stack.h>
  17393. +#include <linux/namei.h>
  17394. +#include <linux/security.h>
  17395. +#include "aufs.h"
  17396. +
  17397. +static int h_permission(struct inode *h_inode, int mask,
  17398. + struct path *h_path, int brperm)
  17399. +{
  17400. + int err;
  17401. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  17402. +
  17403. + err = -EACCES;
  17404. + if ((write_mask && IS_IMMUTABLE(h_inode))
  17405. + || ((mask & MAY_EXEC)
  17406. + && S_ISREG(h_inode->i_mode)
  17407. + && (path_noexec(h_path)
  17408. + || !(h_inode->i_mode & S_IXUGO))))
  17409. + goto out;
  17410. +
  17411. + /*
  17412. + * - skip the lower fs test in the case of write to ro branch.
  17413. + * - nfs dir permission write check is optimized, but a policy for
  17414. + * link/rename requires a real check.
  17415. + * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
  17416. + * in this case, generic_permission() returns -EOPNOTSUPP.
  17417. + */
  17418. + if ((write_mask && !au_br_writable(brperm))
  17419. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  17420. + && write_mask && !(mask & MAY_READ))
  17421. + || !h_inode->i_op->permission) {
  17422. + /* AuLabel(generic_permission); */
  17423. + /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
  17424. + err = generic_permission(h_inode, mask);
  17425. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  17426. + err = h_inode->i_op->permission(h_inode, mask);
  17427. + AuTraceErr(err);
  17428. + } else {
  17429. + /* AuLabel(h_inode->permission); */
  17430. + err = h_inode->i_op->permission(h_inode, mask);
  17431. + AuTraceErr(err);
  17432. + }
  17433. +
  17434. + if (!err)
  17435. + err = devcgroup_inode_permission(h_inode, mask);
  17436. + if (!err)
  17437. + err = security_inode_permission(h_inode, mask);
  17438. +
  17439. +#if 0
  17440. + if (!err) {
  17441. + /* todo: do we need to call ima_path_check()? */
  17442. + struct path h_path = {
  17443. + .dentry =
  17444. + .mnt = h_mnt
  17445. + };
  17446. + err = ima_path_check(&h_path,
  17447. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  17448. + IMA_COUNT_LEAVE);
  17449. + }
  17450. +#endif
  17451. +
  17452. +out:
  17453. + return err;
  17454. +}
  17455. +
  17456. +static int aufs_permission(struct inode *inode, int mask)
  17457. +{
  17458. + int err;
  17459. + aufs_bindex_t bindex, bbot;
  17460. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  17461. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  17462. + struct inode *h_inode;
  17463. + struct super_block *sb;
  17464. + struct au_branch *br;
  17465. +
  17466. + /* todo: support rcu-walk? */
  17467. + if (mask & MAY_NOT_BLOCK)
  17468. + return -ECHILD;
  17469. +
  17470. + sb = inode->i_sb;
  17471. + si_read_lock(sb, AuLock_FLUSH);
  17472. + ii_read_lock_child(inode);
  17473. +#if 0
  17474. + err = au_iigen_test(inode, au_sigen(sb));
  17475. + if (unlikely(err))
  17476. + goto out;
  17477. +#endif
  17478. +
  17479. + if (!isdir
  17480. + || write_mask
  17481. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  17482. + err = au_busy_or_stale();
  17483. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  17484. + if (unlikely(!h_inode
  17485. + || (h_inode->i_mode & S_IFMT)
  17486. + != (inode->i_mode & S_IFMT)))
  17487. + goto out;
  17488. +
  17489. + err = 0;
  17490. + bindex = au_ibtop(inode);
  17491. + br = au_sbr(sb, bindex);
  17492. + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
  17493. + if (write_mask
  17494. + && !err
  17495. + && !special_file(h_inode->i_mode)) {
  17496. + /* test whether the upper writable branch exists */
  17497. + err = -EROFS;
  17498. + for (; bindex >= 0; bindex--)
  17499. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  17500. + err = 0;
  17501. + break;
  17502. + }
  17503. + }
  17504. + goto out;
  17505. + }
  17506. +
  17507. + /* non-write to dir */
  17508. + err = 0;
  17509. + bbot = au_ibbot(inode);
  17510. + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
  17511. + h_inode = au_h_iptr(inode, bindex);
  17512. + if (h_inode) {
  17513. + err = au_busy_or_stale();
  17514. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  17515. + break;
  17516. +
  17517. + br = au_sbr(sb, bindex);
  17518. + err = h_permission(h_inode, mask, &br->br_path,
  17519. + br->br_perm);
  17520. + }
  17521. + }
  17522. +
  17523. +out:
  17524. + ii_read_unlock(inode);
  17525. + si_read_unlock(sb);
  17526. + return err;
  17527. +}
  17528. +
  17529. +/* ---------------------------------------------------------------------- */
  17530. +
  17531. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  17532. + unsigned int flags)
  17533. +{
  17534. + struct dentry *ret, *parent;
  17535. + struct inode *inode;
  17536. + struct super_block *sb;
  17537. + int err, npositive;
  17538. +
  17539. + IMustLock(dir);
  17540. +
  17541. + /* todo: support rcu-walk? */
  17542. + ret = ERR_PTR(-ECHILD);
  17543. + if (flags & LOOKUP_RCU)
  17544. + goto out;
  17545. +
  17546. + ret = ERR_PTR(-ENAMETOOLONG);
  17547. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  17548. + goto out;
  17549. +
  17550. + sb = dir->i_sb;
  17551. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  17552. + ret = ERR_PTR(err);
  17553. + if (unlikely(err))
  17554. + goto out;
  17555. +
  17556. + err = au_di_init(dentry);
  17557. + ret = ERR_PTR(err);
  17558. + if (unlikely(err))
  17559. + goto out_si;
  17560. +
  17561. + inode = NULL;
  17562. + npositive = 0; /* suppress a warning */
  17563. + parent = dentry->d_parent; /* dir inode is locked */
  17564. + di_read_lock_parent(parent, AuLock_IR);
  17565. + err = au_alive_dir(parent);
  17566. + if (!err)
  17567. + err = au_digen_test(parent, au_sigen(sb));
  17568. + if (!err) {
  17569. + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
  17570. + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
  17571. + AuLkup_ALLOW_NEG);
  17572. + err = npositive;
  17573. + }
  17574. + di_read_unlock(parent, AuLock_IR);
  17575. + ret = ERR_PTR(err);
  17576. + if (unlikely(err < 0))
  17577. + goto out_unlock;
  17578. +
  17579. + if (npositive) {
  17580. + inode = au_new_inode(dentry, /*must_new*/0);
  17581. + if (IS_ERR(inode)) {
  17582. + ret = (void *)inode;
  17583. + inode = NULL;
  17584. + goto out_unlock;
  17585. + }
  17586. + }
  17587. +
  17588. + if (inode)
  17589. + atomic_inc(&inode->i_count);
  17590. + ret = d_splice_alias(inode, dentry);
  17591. +#if 0
  17592. + if (unlikely(d_need_lookup(dentry))) {
  17593. + spin_lock(&dentry->d_lock);
  17594. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  17595. + spin_unlock(&dentry->d_lock);
  17596. + } else
  17597. +#endif
  17598. + if (inode) {
  17599. + if (!IS_ERR(ret)) {
  17600. + iput(inode);
  17601. + if (ret && ret != dentry)
  17602. + ii_write_unlock(inode);
  17603. + } else {
  17604. + ii_write_unlock(inode);
  17605. + iput(inode);
  17606. + inode = NULL;
  17607. + }
  17608. + }
  17609. +
  17610. +out_unlock:
  17611. + di_write_unlock(dentry);
  17612. +out_si:
  17613. + si_read_unlock(sb);
  17614. +out:
  17615. + return ret;
  17616. +}
  17617. +
  17618. +/* ---------------------------------------------------------------------- */
  17619. +
  17620. +struct aopen_node {
  17621. + struct hlist_node hlist;
  17622. + struct file *file, *h_file;
  17623. +};
  17624. +
  17625. +static int au_do_aopen(struct inode *inode, struct file *file)
  17626. +{
  17627. + struct au_sphlhead *aopen;
  17628. + struct aopen_node *node;
  17629. + struct au_do_open_args args = {
  17630. + .no_lock = 1,
  17631. + .open = au_do_open_nondir
  17632. + };
  17633. +
  17634. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  17635. + spin_lock(&aopen->spin);
  17636. + hlist_for_each_entry(node, &aopen->head, hlist)
  17637. + if (node->file == file) {
  17638. + args.h_file = node->h_file;
  17639. + break;
  17640. + }
  17641. + spin_unlock(&aopen->spin);
  17642. + /* AuDebugOn(!args.h_file); */
  17643. +
  17644. + return au_do_open(file, &args);
  17645. +}
  17646. +
  17647. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  17648. + struct file *file, unsigned int open_flag,
  17649. + umode_t create_mode, int *opened)
  17650. +{
  17651. + int err, h_opened = *opened;
  17652. + unsigned int lkup_flags;
  17653. + struct dentry *parent, *d;
  17654. + struct au_sphlhead *aopen;
  17655. + struct vfsub_aopen_args args = {
  17656. + .open_flag = open_flag,
  17657. + .create_mode = create_mode,
  17658. + .opened = &h_opened
  17659. + };
  17660. + struct aopen_node aopen_node = {
  17661. + .file = file
  17662. + };
  17663. +
  17664. + IMustLock(dir);
  17665. + AuDbg("open_flag 0%o\n", open_flag);
  17666. + AuDbgDentry(dentry);
  17667. +
  17668. + err = 0;
  17669. + if (!au_di(dentry)) {
  17670. + lkup_flags = LOOKUP_OPEN;
  17671. + if (open_flag & O_CREAT)
  17672. + lkup_flags |= LOOKUP_CREATE;
  17673. + d = aufs_lookup(dir, dentry, lkup_flags);
  17674. + if (IS_ERR(d)) {
  17675. + err = PTR_ERR(d);
  17676. + AuTraceErr(err);
  17677. + goto out;
  17678. + } else if (d) {
  17679. + /*
  17680. + * obsoleted dentry found.
  17681. + * another error will be returned later.
  17682. + */
  17683. + d_drop(d);
  17684. + AuDbgDentry(d);
  17685. + dput(d);
  17686. + }
  17687. + AuDbgDentry(dentry);
  17688. + }
  17689. +
  17690. + if (d_is_positive(dentry)
  17691. + || d_unhashed(dentry)
  17692. + || d_unlinked(dentry)
  17693. + || !(open_flag & O_CREAT))
  17694. + goto out_no_open;
  17695. +
  17696. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  17697. + if (unlikely(err))
  17698. + goto out;
  17699. +
  17700. + parent = dentry->d_parent; /* dir is locked */
  17701. + di_write_lock_parent(parent);
  17702. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  17703. + if (unlikely(err))
  17704. + goto out_unlock;
  17705. +
  17706. + AuDbgDentry(dentry);
  17707. + if (d_is_positive(dentry))
  17708. + goto out_unlock;
  17709. +
  17710. + args.file = get_empty_filp();
  17711. + err = PTR_ERR(args.file);
  17712. + if (IS_ERR(args.file))
  17713. + goto out_unlock;
  17714. +
  17715. + args.file->f_flags = file->f_flags;
  17716. + err = au_aopen_or_create(dir, dentry, &args);
  17717. + AuTraceErr(err);
  17718. + AuDbgFile(args.file);
  17719. + if (unlikely(err < 0)) {
  17720. + if (h_opened & FILE_OPENED)
  17721. + fput(args.file);
  17722. + else
  17723. + put_filp(args.file);
  17724. + goto out_unlock;
  17725. + }
  17726. +
  17727. + /* some filesystems don't set FILE_CREATED while succeeded? */
  17728. + *opened |= FILE_CREATED;
  17729. + if (h_opened & FILE_OPENED)
  17730. + aopen_node.h_file = args.file;
  17731. + else {
  17732. + put_filp(args.file);
  17733. + args.file = NULL;
  17734. + }
  17735. + aopen = &au_sbi(dir->i_sb)->si_aopen;
  17736. + au_sphl_add(&aopen_node.hlist, aopen);
  17737. + err = finish_open(file, dentry, au_do_aopen, opened);
  17738. + au_sphl_del(&aopen_node.hlist, aopen);
  17739. + AuTraceErr(err);
  17740. + AuDbgFile(file);
  17741. + if (aopen_node.h_file)
  17742. + fput(aopen_node.h_file);
  17743. +
  17744. +out_unlock:
  17745. + di_write_unlock(parent);
  17746. + aufs_read_unlock(dentry, AuLock_DW);
  17747. + AuDbgDentry(dentry);
  17748. + if (unlikely(err < 0))
  17749. + goto out;
  17750. +out_no_open:
  17751. + if (err >= 0 && !(*opened & FILE_CREATED)) {
  17752. + AuLabel(out_no_open);
  17753. + dget(dentry);
  17754. + err = finish_no_open(file, dentry);
  17755. + }
  17756. +out:
  17757. + AuDbg("%pd%s%s\n", dentry,
  17758. + (*opened & FILE_CREATED) ? " created" : "",
  17759. + (*opened & FILE_OPENED) ? " opened" : "");
  17760. + AuTraceErr(err);
  17761. + return err;
  17762. +}
  17763. +
  17764. +
  17765. +/* ---------------------------------------------------------------------- */
  17766. +
  17767. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  17768. + const unsigned char add_entry, aufs_bindex_t bcpup,
  17769. + aufs_bindex_t btop)
  17770. +{
  17771. + int err;
  17772. + struct dentry *h_parent;
  17773. + struct inode *h_dir;
  17774. +
  17775. + if (add_entry)
  17776. + IMustLock(d_inode(parent));
  17777. + else
  17778. + di_write_lock_parent(parent);
  17779. +
  17780. + err = 0;
  17781. + if (!au_h_dptr(parent, bcpup)) {
  17782. + if (btop > bcpup)
  17783. + err = au_cpup_dirs(dentry, bcpup);
  17784. + else if (btop < bcpup)
  17785. + err = au_cpdown_dirs(dentry, bcpup);
  17786. + else
  17787. + BUG();
  17788. + }
  17789. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  17790. + h_parent = au_h_dptr(parent, bcpup);
  17791. + h_dir = d_inode(h_parent);
  17792. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  17793. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  17794. + /* todo: no unlock here */
  17795. + mutex_unlock(&h_dir->i_mutex);
  17796. +
  17797. + AuDbg("bcpup %d\n", bcpup);
  17798. + if (!err) {
  17799. + if (d_really_is_negative(dentry))
  17800. + au_set_h_dptr(dentry, btop, NULL);
  17801. + au_update_dbrange(dentry, /*do_put_zero*/0);
  17802. + }
  17803. + }
  17804. +
  17805. + if (!add_entry)
  17806. + di_write_unlock(parent);
  17807. + if (!err)
  17808. + err = bcpup; /* success */
  17809. +
  17810. + AuTraceErr(err);
  17811. + return err;
  17812. +}
  17813. +
  17814. +/*
  17815. + * decide the branch and the parent dir where we will create a new entry.
  17816. + * returns new bindex or an error.
  17817. + * copyup the parent dir if needed.
  17818. + */
  17819. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  17820. + struct au_wr_dir_args *args)
  17821. +{
  17822. + int err;
  17823. + unsigned int flags;
  17824. + aufs_bindex_t bcpup, btop, src_btop;
  17825. + const unsigned char add_entry
  17826. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  17827. + | au_ftest_wrdir(args->flags, TMPFILE);
  17828. + struct super_block *sb;
  17829. + struct dentry *parent;
  17830. + struct au_sbinfo *sbinfo;
  17831. +
  17832. + sb = dentry->d_sb;
  17833. + sbinfo = au_sbi(sb);
  17834. + parent = dget_parent(dentry);
  17835. + btop = au_dbtop(dentry);
  17836. + bcpup = btop;
  17837. + if (args->force_btgt < 0) {
  17838. + if (src_dentry) {
  17839. + src_btop = au_dbtop(src_dentry);
  17840. + if (src_btop < btop)
  17841. + bcpup = src_btop;
  17842. + } else if (add_entry) {
  17843. + flags = 0;
  17844. + if (au_ftest_wrdir(args->flags, ISDIR))
  17845. + au_fset_wbr(flags, DIR);
  17846. + err = AuWbrCreate(sbinfo, dentry, flags);
  17847. + bcpup = err;
  17848. + }
  17849. +
  17850. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  17851. + if (add_entry)
  17852. + err = AuWbrCopyup(sbinfo, dentry);
  17853. + else {
  17854. + if (!IS_ROOT(dentry)) {
  17855. + di_read_lock_parent(parent, !AuLock_IR);
  17856. + err = AuWbrCopyup(sbinfo, dentry);
  17857. + di_read_unlock(parent, !AuLock_IR);
  17858. + } else
  17859. + err = AuWbrCopyup(sbinfo, dentry);
  17860. + }
  17861. + bcpup = err;
  17862. + if (unlikely(err < 0))
  17863. + goto out;
  17864. + }
  17865. + } else {
  17866. + bcpup = args->force_btgt;
  17867. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  17868. + }
  17869. +
  17870. + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
  17871. + err = bcpup;
  17872. + if (bcpup == btop)
  17873. + goto out; /* success */
  17874. +
  17875. + /* copyup the new parent into the branch we process */
  17876. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
  17877. + if (err >= 0) {
  17878. + if (d_really_is_negative(dentry)) {
  17879. + au_set_h_dptr(dentry, btop, NULL);
  17880. + au_set_dbtop(dentry, bcpup);
  17881. + au_set_dbbot(dentry, bcpup);
  17882. + }
  17883. + AuDebugOn(add_entry
  17884. + && !au_ftest_wrdir(args->flags, TMPFILE)
  17885. + && !au_h_dptr(dentry, bcpup));
  17886. + }
  17887. +
  17888. +out:
  17889. + dput(parent);
  17890. + return err;
  17891. +}
  17892. +
  17893. +/* ---------------------------------------------------------------------- */
  17894. +
  17895. +void au_pin_hdir_unlock(struct au_pin *p)
  17896. +{
  17897. + if (p->hdir)
  17898. + au_hn_imtx_unlock(p->hdir);
  17899. +}
  17900. +
  17901. +int au_pin_hdir_lock(struct au_pin *p)
  17902. +{
  17903. + int err;
  17904. +
  17905. + err = 0;
  17906. + if (!p->hdir)
  17907. + goto out;
  17908. +
  17909. + /* even if an error happens later, keep this lock */
  17910. + au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
  17911. +
  17912. + err = -EBUSY;
  17913. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  17914. + goto out;
  17915. +
  17916. + err = 0;
  17917. + if (p->h_dentry)
  17918. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  17919. + p->h_parent, p->br);
  17920. +
  17921. +out:
  17922. + return err;
  17923. +}
  17924. +
  17925. +int au_pin_hdir_relock(struct au_pin *p)
  17926. +{
  17927. + int err, i;
  17928. + struct inode *h_i;
  17929. + struct dentry *h_d[] = {
  17930. + p->h_dentry,
  17931. + p->h_parent
  17932. + };
  17933. +
  17934. + err = au_pin_hdir_lock(p);
  17935. + if (unlikely(err))
  17936. + goto out;
  17937. +
  17938. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  17939. + if (!h_d[i])
  17940. + continue;
  17941. + if (d_is_positive(h_d[i])) {
  17942. + h_i = d_inode(h_d[i]);
  17943. + err = !h_i->i_nlink;
  17944. + }
  17945. + }
  17946. +
  17947. +out:
  17948. + return err;
  17949. +}
  17950. +
  17951. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  17952. +{
  17953. +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
  17954. + p->hdir->hi_inode->i_mutex.owner = task;
  17955. +#endif
  17956. +}
  17957. +
  17958. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  17959. +{
  17960. + if (p->hdir) {
  17961. + mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
  17962. + p->lsc_hi, 0, NULL, _RET_IP_);
  17963. + au_pin_hdir_set_owner(p, current);
  17964. + }
  17965. +}
  17966. +
  17967. +void au_pin_hdir_release(struct au_pin *p)
  17968. +{
  17969. + if (p->hdir) {
  17970. + au_pin_hdir_set_owner(p, p->task);
  17971. + mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
  17972. + }
  17973. +}
  17974. +
  17975. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  17976. +{
  17977. + if (pin && pin->parent)
  17978. + return au_h_dptr(pin->parent, pin->bindex);
  17979. + return NULL;
  17980. +}
  17981. +
  17982. +void au_unpin(struct au_pin *p)
  17983. +{
  17984. + if (p->hdir)
  17985. + au_pin_hdir_unlock(p);
  17986. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  17987. + vfsub_mnt_drop_write(p->h_mnt);
  17988. + if (!p->hdir)
  17989. + return;
  17990. +
  17991. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17992. + di_read_unlock(p->parent, AuLock_IR);
  17993. + iput(p->hdir->hi_inode);
  17994. + dput(p->parent);
  17995. + p->parent = NULL;
  17996. + p->hdir = NULL;
  17997. + p->h_mnt = NULL;
  17998. + /* do not clear p->task */
  17999. +}
  18000. +
  18001. +int au_do_pin(struct au_pin *p)
  18002. +{
  18003. + int err;
  18004. + struct super_block *sb;
  18005. + struct inode *h_dir;
  18006. +
  18007. + err = 0;
  18008. + sb = p->dentry->d_sb;
  18009. + p->br = au_sbr(sb, p->bindex);
  18010. + if (IS_ROOT(p->dentry)) {
  18011. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  18012. + p->h_mnt = au_br_mnt(p->br);
  18013. + err = vfsub_mnt_want_write(p->h_mnt);
  18014. + if (unlikely(err)) {
  18015. + au_fclr_pin(p->flags, MNT_WRITE);
  18016. + goto out_err;
  18017. + }
  18018. + }
  18019. + goto out;
  18020. + }
  18021. +
  18022. + p->h_dentry = NULL;
  18023. + if (p->bindex <= au_dbbot(p->dentry))
  18024. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  18025. +
  18026. + p->parent = dget_parent(p->dentry);
  18027. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18028. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  18029. +
  18030. + h_dir = NULL;
  18031. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  18032. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  18033. + if (p->hdir)
  18034. + h_dir = p->hdir->hi_inode;
  18035. +
  18036. + /*
  18037. + * udba case, or
  18038. + * if DI_LOCKED is not set, then p->parent may be different
  18039. + * and h_parent can be NULL.
  18040. + */
  18041. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  18042. + err = -EBUSY;
  18043. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18044. + di_read_unlock(p->parent, AuLock_IR);
  18045. + dput(p->parent);
  18046. + p->parent = NULL;
  18047. + goto out_err;
  18048. + }
  18049. +
  18050. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  18051. + p->h_mnt = au_br_mnt(p->br);
  18052. + err = vfsub_mnt_want_write(p->h_mnt);
  18053. + if (unlikely(err)) {
  18054. + au_fclr_pin(p->flags, MNT_WRITE);
  18055. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  18056. + di_read_unlock(p->parent, AuLock_IR);
  18057. + dput(p->parent);
  18058. + p->parent = NULL;
  18059. + goto out_err;
  18060. + }
  18061. + }
  18062. +
  18063. + au_igrab(h_dir);
  18064. + err = au_pin_hdir_lock(p);
  18065. + if (!err)
  18066. + goto out; /* success */
  18067. +
  18068. + au_unpin(p);
  18069. +
  18070. +out_err:
  18071. + pr_err("err %d\n", err);
  18072. + err = au_busy_or_stale();
  18073. +out:
  18074. + return err;
  18075. +}
  18076. +
  18077. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  18078. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  18079. + unsigned int udba, unsigned char flags)
  18080. +{
  18081. + p->dentry = dentry;
  18082. + p->udba = udba;
  18083. + p->lsc_di = lsc_di;
  18084. + p->lsc_hi = lsc_hi;
  18085. + p->flags = flags;
  18086. + p->bindex = bindex;
  18087. +
  18088. + p->parent = NULL;
  18089. + p->hdir = NULL;
  18090. + p->h_mnt = NULL;
  18091. +
  18092. + p->h_dentry = NULL;
  18093. + p->h_parent = NULL;
  18094. + p->br = NULL;
  18095. + p->task = current;
  18096. +}
  18097. +
  18098. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  18099. + unsigned int udba, unsigned char flags)
  18100. +{
  18101. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  18102. + udba, flags);
  18103. + return au_do_pin(pin);
  18104. +}
  18105. +
  18106. +/* ---------------------------------------------------------------------- */
  18107. +
  18108. +/*
  18109. + * ->setattr() and ->getattr() are called in various cases.
  18110. + * chmod, stat: dentry is revalidated.
  18111. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  18112. + * unhashed.
  18113. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  18114. + */
  18115. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  18116. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  18117. +{
  18118. + int err;
  18119. + struct dentry *parent;
  18120. +
  18121. + err = 0;
  18122. + if (au_digen_test(dentry, sigen)) {
  18123. + parent = dget_parent(dentry);
  18124. + di_read_lock_parent(parent, AuLock_IR);
  18125. + err = au_refresh_dentry(dentry, parent);
  18126. + di_read_unlock(parent, AuLock_IR);
  18127. + dput(parent);
  18128. + }
  18129. +
  18130. + AuTraceErr(err);
  18131. + return err;
  18132. +}
  18133. +
  18134. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  18135. + struct au_icpup_args *a)
  18136. +{
  18137. + int err;
  18138. + loff_t sz;
  18139. + aufs_bindex_t btop, ibtop;
  18140. + struct dentry *hi_wh, *parent;
  18141. + struct inode *inode;
  18142. + struct au_wr_dir_args wr_dir_args = {
  18143. + .force_btgt = -1,
  18144. + .flags = 0
  18145. + };
  18146. +
  18147. + if (d_is_dir(dentry))
  18148. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  18149. + /* plink or hi_wh() case */
  18150. + btop = au_dbtop(dentry);
  18151. + inode = d_inode(dentry);
  18152. + ibtop = au_ibtop(inode);
  18153. + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
  18154. + wr_dir_args.force_btgt = ibtop;
  18155. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  18156. + if (unlikely(err < 0))
  18157. + goto out;
  18158. + a->btgt = err;
  18159. + if (err != btop)
  18160. + au_fset_icpup(a->flags, DID_CPUP);
  18161. +
  18162. + err = 0;
  18163. + a->pin_flags = AuPin_MNT_WRITE;
  18164. + parent = NULL;
  18165. + if (!IS_ROOT(dentry)) {
  18166. + au_fset_pin(a->pin_flags, DI_LOCKED);
  18167. + parent = dget_parent(dentry);
  18168. + di_write_lock_parent(parent);
  18169. + }
  18170. +
  18171. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  18172. + if (unlikely(err))
  18173. + goto out_parent;
  18174. +
  18175. + sz = -1;
  18176. + a->h_path.dentry = au_h_dptr(dentry, btop);
  18177. + a->h_inode = d_inode(a->h_path.dentry);
  18178. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  18179. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  18180. + if (ia->ia_size < i_size_read(a->h_inode))
  18181. + sz = ia->ia_size;
  18182. + mutex_unlock(&a->h_inode->i_mutex);
  18183. + }
  18184. +
  18185. + hi_wh = NULL;
  18186. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  18187. + hi_wh = au_hi_wh(inode, a->btgt);
  18188. + if (!hi_wh) {
  18189. + struct au_cp_generic cpg = {
  18190. + .dentry = dentry,
  18191. + .bdst = a->btgt,
  18192. + .bsrc = -1,
  18193. + .len = sz,
  18194. + .pin = &a->pin
  18195. + };
  18196. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  18197. + if (unlikely(err))
  18198. + goto out_unlock;
  18199. + hi_wh = au_hi_wh(inode, a->btgt);
  18200. + /* todo: revalidate hi_wh? */
  18201. + }
  18202. + }
  18203. +
  18204. + if (parent) {
  18205. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  18206. + di_downgrade_lock(parent, AuLock_IR);
  18207. + dput(parent);
  18208. + parent = NULL;
  18209. + }
  18210. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  18211. + goto out; /* success */
  18212. +
  18213. + if (!d_unhashed(dentry)) {
  18214. + struct au_cp_generic cpg = {
  18215. + .dentry = dentry,
  18216. + .bdst = a->btgt,
  18217. + .bsrc = btop,
  18218. + .len = sz,
  18219. + .pin = &a->pin,
  18220. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  18221. + };
  18222. + err = au_sio_cpup_simple(&cpg);
  18223. + if (!err)
  18224. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  18225. + } else if (!hi_wh)
  18226. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  18227. + else
  18228. + a->h_path.dentry = hi_wh; /* do not dget here */
  18229. +
  18230. +out_unlock:
  18231. + a->h_inode = d_inode(a->h_path.dentry);
  18232. + if (!err)
  18233. + goto out; /* success */
  18234. + au_unpin(&a->pin);
  18235. +out_parent:
  18236. + if (parent) {
  18237. + di_write_unlock(parent);
  18238. + dput(parent);
  18239. + }
  18240. +out:
  18241. + if (!err)
  18242. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  18243. + return err;
  18244. +}
  18245. +
  18246. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  18247. +{
  18248. + int err;
  18249. + struct inode *inode, *delegated;
  18250. + struct super_block *sb;
  18251. + struct file *file;
  18252. + struct au_icpup_args *a;
  18253. +
  18254. + inode = d_inode(dentry);
  18255. + IMustLock(inode);
  18256. +
  18257. + err = -ENOMEM;
  18258. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18259. + if (unlikely(!a))
  18260. + goto out;
  18261. +
  18262. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  18263. + ia->ia_valid &= ~ATTR_MODE;
  18264. +
  18265. + file = NULL;
  18266. + sb = dentry->d_sb;
  18267. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18268. + if (unlikely(err))
  18269. + goto out_kfree;
  18270. +
  18271. + if (ia->ia_valid & ATTR_FILE) {
  18272. + /* currently ftruncate(2) only */
  18273. + AuDebugOn(!d_is_reg(dentry));
  18274. + file = ia->ia_file;
  18275. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  18276. + if (unlikely(err))
  18277. + goto out_si;
  18278. + ia->ia_file = au_hf_top(file);
  18279. + a->udba = AuOpt_UDBA_NONE;
  18280. + } else {
  18281. + /* fchmod() doesn't pass ia_file */
  18282. + a->udba = au_opt_udba(sb);
  18283. + di_write_lock_child(dentry);
  18284. + /* no d_unlinked(), to set UDBA_NONE for root */
  18285. + if (d_unhashed(dentry))
  18286. + a->udba = AuOpt_UDBA_NONE;
  18287. + if (a->udba != AuOpt_UDBA_NONE) {
  18288. + AuDebugOn(IS_ROOT(dentry));
  18289. + err = au_reval_for_attr(dentry, au_sigen(sb));
  18290. + if (unlikely(err))
  18291. + goto out_dentry;
  18292. + }
  18293. + }
  18294. +
  18295. + err = au_pin_and_icpup(dentry, ia, a);
  18296. + if (unlikely(err < 0))
  18297. + goto out_dentry;
  18298. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  18299. + ia->ia_file = NULL;
  18300. + ia->ia_valid &= ~ATTR_FILE;
  18301. + }
  18302. +
  18303. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  18304. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  18305. + == (ATTR_MODE | ATTR_CTIME)) {
  18306. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  18307. + if (unlikely(err))
  18308. + goto out_unlock;
  18309. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  18310. + && (ia->ia_valid & ATTR_CTIME)) {
  18311. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  18312. + if (unlikely(err))
  18313. + goto out_unlock;
  18314. + }
  18315. +
  18316. + if (ia->ia_valid & ATTR_SIZE) {
  18317. + struct file *f;
  18318. +
  18319. + if (ia->ia_size < i_size_read(inode))
  18320. + /* unmap only */
  18321. + truncate_setsize(inode, ia->ia_size);
  18322. +
  18323. + f = NULL;
  18324. + if (ia->ia_valid & ATTR_FILE)
  18325. + f = ia->ia_file;
  18326. + mutex_unlock(&a->h_inode->i_mutex);
  18327. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  18328. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  18329. + } else {
  18330. + delegated = NULL;
  18331. + while (1) {
  18332. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  18333. + if (delegated) {
  18334. + err = break_deleg_wait(&delegated);
  18335. + if (!err)
  18336. + continue;
  18337. + }
  18338. + break;
  18339. + }
  18340. + }
  18341. + /*
  18342. + * regardless aufs 'acl' option setting.
  18343. + * why don't all acl-aware fs call this func from their ->setattr()?
  18344. + */
  18345. + if (!err && (ia->ia_valid & ATTR_MODE))
  18346. + err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
  18347. + if (!err)
  18348. + au_cpup_attr_changeable(inode);
  18349. +
  18350. +out_unlock:
  18351. + mutex_unlock(&a->h_inode->i_mutex);
  18352. + au_unpin(&a->pin);
  18353. + if (unlikely(err))
  18354. + au_update_dbtop(dentry);
  18355. +out_dentry:
  18356. + di_write_unlock(dentry);
  18357. + if (file) {
  18358. + fi_write_unlock(file);
  18359. + ia->ia_file = file;
  18360. + ia->ia_valid |= ATTR_FILE;
  18361. + }
  18362. +out_si:
  18363. + si_read_unlock(sb);
  18364. +out_kfree:
  18365. + au_delayed_kfree(a);
  18366. +out:
  18367. + AuTraceErr(err);
  18368. + return err;
  18369. +}
  18370. +
  18371. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  18372. +static int au_h_path_to_set_attr(struct dentry *dentry,
  18373. + struct au_icpup_args *a, struct path *h_path)
  18374. +{
  18375. + int err;
  18376. + struct super_block *sb;
  18377. +
  18378. + sb = dentry->d_sb;
  18379. + a->udba = au_opt_udba(sb);
  18380. + /* no d_unlinked(), to set UDBA_NONE for root */
  18381. + if (d_unhashed(dentry))
  18382. + a->udba = AuOpt_UDBA_NONE;
  18383. + if (a->udba != AuOpt_UDBA_NONE) {
  18384. + AuDebugOn(IS_ROOT(dentry));
  18385. + err = au_reval_for_attr(dentry, au_sigen(sb));
  18386. + if (unlikely(err))
  18387. + goto out;
  18388. + }
  18389. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  18390. + if (unlikely(err < 0))
  18391. + goto out;
  18392. +
  18393. + h_path->dentry = a->h_path.dentry;
  18394. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  18395. +
  18396. +out:
  18397. + return err;
  18398. +}
  18399. +
  18400. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
  18401. +{
  18402. + int err;
  18403. + struct path h_path;
  18404. + struct super_block *sb;
  18405. + struct au_icpup_args *a;
  18406. + struct inode *inode, *h_inode;
  18407. +
  18408. + inode = d_inode(dentry);
  18409. + IMustLock(inode);
  18410. +
  18411. + err = -ENOMEM;
  18412. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18413. + if (unlikely(!a))
  18414. + goto out;
  18415. +
  18416. + sb = dentry->d_sb;
  18417. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18418. + if (unlikely(err))
  18419. + goto out_kfree;
  18420. +
  18421. + h_path.dentry = NULL; /* silence gcc */
  18422. + di_write_lock_child(dentry);
  18423. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  18424. + if (unlikely(err))
  18425. + goto out_di;
  18426. +
  18427. + mutex_unlock(&a->h_inode->i_mutex);
  18428. + switch (arg->type) {
  18429. + case AU_XATTR_SET:
  18430. + err = vfsub_setxattr(h_path.dentry,
  18431. + arg->u.set.name, arg->u.set.value,
  18432. + arg->u.set.size, arg->u.set.flags);
  18433. + break;
  18434. + case AU_XATTR_REMOVE:
  18435. + err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
  18436. + break;
  18437. + case AU_ACL_SET:
  18438. + err = -EOPNOTSUPP;
  18439. + h_inode = d_inode(h_path.dentry);
  18440. + if (h_inode->i_op->set_acl)
  18441. + err = h_inode->i_op->set_acl(h_inode,
  18442. + arg->u.acl_set.acl,
  18443. + arg->u.acl_set.type);
  18444. + break;
  18445. + }
  18446. + if (!err)
  18447. + au_cpup_attr_timesizes(inode);
  18448. +
  18449. + au_unpin(&a->pin);
  18450. + if (unlikely(err))
  18451. + au_update_dbtop(dentry);
  18452. +
  18453. +out_di:
  18454. + di_write_unlock(dentry);
  18455. + si_read_unlock(sb);
  18456. +out_kfree:
  18457. + au_delayed_kfree(a);
  18458. +out:
  18459. + AuTraceErr(err);
  18460. + return err;
  18461. +}
  18462. +#endif
  18463. +
  18464. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  18465. + unsigned int nlink)
  18466. +{
  18467. + unsigned int n;
  18468. +
  18469. + inode->i_mode = st->mode;
  18470. + /* don't i_[ug]id_write() here */
  18471. + inode->i_uid = st->uid;
  18472. + inode->i_gid = st->gid;
  18473. + inode->i_atime = st->atime;
  18474. + inode->i_mtime = st->mtime;
  18475. + inode->i_ctime = st->ctime;
  18476. +
  18477. + au_cpup_attr_nlink(inode, /*force*/0);
  18478. + if (S_ISDIR(inode->i_mode)) {
  18479. + n = inode->i_nlink;
  18480. + n -= nlink;
  18481. + n += st->nlink;
  18482. + smp_mb(); /* for i_nlink */
  18483. + /* 0 can happen */
  18484. + set_nlink(inode, n);
  18485. + }
  18486. +
  18487. + spin_lock(&inode->i_lock);
  18488. + inode->i_blocks = st->blocks;
  18489. + i_size_write(inode, st->size);
  18490. + spin_unlock(&inode->i_lock);
  18491. +}
  18492. +
  18493. +/*
  18494. + * common routine for aufs_getattr() and aufs_getxattr().
  18495. + * returns zero or negative (an error).
  18496. + * @dentry will be read-locked in success.
  18497. + */
  18498. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
  18499. +{
  18500. + int err;
  18501. + unsigned int mnt_flags, sigen;
  18502. + unsigned char udba_none;
  18503. + aufs_bindex_t bindex;
  18504. + struct super_block *sb, *h_sb;
  18505. + struct inode *inode;
  18506. +
  18507. + h_path->mnt = NULL;
  18508. + h_path->dentry = NULL;
  18509. +
  18510. + err = 0;
  18511. + sb = dentry->d_sb;
  18512. + mnt_flags = au_mntflags(sb);
  18513. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  18514. +
  18515. + /* support fstat(2) */
  18516. + if (!d_unlinked(dentry) && !udba_none) {
  18517. + sigen = au_sigen(sb);
  18518. + err = au_digen_test(dentry, sigen);
  18519. + if (!err) {
  18520. + di_read_lock_child(dentry, AuLock_IR);
  18521. + err = au_dbrange_test(dentry);
  18522. + if (unlikely(err)) {
  18523. + di_read_unlock(dentry, AuLock_IR);
  18524. + goto out;
  18525. + }
  18526. + } else {
  18527. + AuDebugOn(IS_ROOT(dentry));
  18528. + di_write_lock_child(dentry);
  18529. + err = au_dbrange_test(dentry);
  18530. + if (!err)
  18531. + err = au_reval_for_attr(dentry, sigen);
  18532. + if (!err)
  18533. + di_downgrade_lock(dentry, AuLock_IR);
  18534. + else {
  18535. + di_write_unlock(dentry);
  18536. + goto out;
  18537. + }
  18538. + }
  18539. + } else
  18540. + di_read_lock_child(dentry, AuLock_IR);
  18541. +
  18542. + inode = d_inode(dentry);
  18543. + bindex = au_ibtop(inode);
  18544. + h_path->mnt = au_sbr_mnt(sb, bindex);
  18545. + h_sb = h_path->mnt->mnt_sb;
  18546. + if (!force
  18547. + && !au_test_fs_bad_iattr(h_sb)
  18548. + && udba_none)
  18549. + goto out; /* success */
  18550. +
  18551. + if (au_dbtop(dentry) == bindex)
  18552. + h_path->dentry = au_h_dptr(dentry, bindex);
  18553. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  18554. + h_path->dentry = au_plink_lkup(inode, bindex);
  18555. + if (IS_ERR(h_path->dentry))
  18556. + /* pretending success */
  18557. + h_path->dentry = NULL;
  18558. + else
  18559. + dput(h_path->dentry);
  18560. + }
  18561. +
  18562. +out:
  18563. + return err;
  18564. +}
  18565. +
  18566. +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
  18567. + struct dentry *dentry, struct kstat *st)
  18568. +{
  18569. + int err;
  18570. + unsigned char positive;
  18571. + struct path h_path;
  18572. + struct inode *inode;
  18573. + struct super_block *sb;
  18574. +
  18575. + inode = d_inode(dentry);
  18576. + sb = dentry->d_sb;
  18577. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18578. + if (unlikely(err))
  18579. + goto out;
  18580. + err = au_h_path_getattr(dentry, /*force*/0, &h_path);
  18581. + if (unlikely(err))
  18582. + goto out_si;
  18583. + if (unlikely(!h_path.dentry))
  18584. + /* illegally overlapped or something */
  18585. + goto out_fill; /* pretending success */
  18586. +
  18587. + positive = d_is_positive(h_path.dentry);
  18588. + if (positive)
  18589. + err = vfs_getattr(&h_path, st);
  18590. + if (!err) {
  18591. + if (positive)
  18592. + au_refresh_iattr(inode, st,
  18593. + d_inode(h_path.dentry)->i_nlink);
  18594. + goto out_fill; /* success */
  18595. + }
  18596. + AuTraceErr(err);
  18597. + goto out_di;
  18598. +
  18599. +out_fill:
  18600. + generic_fillattr(inode, st);
  18601. +out_di:
  18602. + di_read_unlock(dentry, AuLock_IR);
  18603. +out_si:
  18604. + si_read_unlock(sb);
  18605. +out:
  18606. + AuTraceErr(err);
  18607. + return err;
  18608. +}
  18609. +
  18610. +/* ---------------------------------------------------------------------- */
  18611. +
  18612. +/*
  18613. + * Assumption:
  18614. + * - the number of symlinks is not so many.
  18615. + *
  18616. + * Structure:
  18617. + * - sbinfo (instead of iinfo) contains an hlist of struct au_symlink.
  18618. + * If iinfo contained the hlist, then it would be rather large waste of memory
  18619. + * I am afraid.
  18620. + * - struct au_symlink contains the necessary info for h_inode follow_link() and
  18621. + * put_link().
  18622. + */
  18623. +
  18624. +struct au_symlink {
  18625. + union {
  18626. + struct hlist_node hlist;
  18627. + struct rcu_head rcu;
  18628. + };
  18629. +
  18630. + struct inode *h_inode;
  18631. + void *h_cookie;
  18632. +};
  18633. +
  18634. +static void au_symlink_add(struct super_block *sb, struct au_symlink *slink,
  18635. + struct inode *h_inode, void *cookie)
  18636. +{
  18637. + struct au_sbinfo *sbinfo;
  18638. +
  18639. + ihold(h_inode);
  18640. + slink->h_inode = h_inode;
  18641. + slink->h_cookie = cookie;
  18642. + sbinfo = au_sbi(sb);
  18643. + au_sphl_add(&slink->hlist, &sbinfo->si_symlink);
  18644. +}
  18645. +
  18646. +static void au_symlink_del(struct super_block *sb, struct au_symlink *slink)
  18647. +{
  18648. + struct au_sbinfo *sbinfo;
  18649. +
  18650. + /* do not iput() within rcu */
  18651. + iput(slink->h_inode);
  18652. + slink->h_inode = NULL;
  18653. + sbinfo = au_sbi(sb);
  18654. + au_sphl_del_rcu(&slink->hlist, &sbinfo->si_symlink);
  18655. + kfree_rcu(slink, rcu);
  18656. +}
  18657. +
  18658. +static const char *aufs_follow_link(struct dentry *dentry, void **cookie)
  18659. +{
  18660. + const char *ret;
  18661. + struct inode *inode, *h_inode;
  18662. + struct dentry *h_dentry;
  18663. + struct au_symlink *slink;
  18664. + int err;
  18665. + aufs_bindex_t bindex;
  18666. +
  18667. + ret = NULL; /* suppress a warning */
  18668. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  18669. + if (unlikely(err))
  18670. + goto out;
  18671. +
  18672. + err = au_d_hashed_positive(dentry);
  18673. + if (unlikely(err))
  18674. + goto out_unlock;
  18675. +
  18676. + err = -EINVAL;
  18677. + inode = d_inode(dentry);
  18678. + bindex = au_ibtop(inode);
  18679. + h_inode = au_h_iptr(inode, bindex);
  18680. + if (unlikely(!h_inode->i_op->follow_link))
  18681. + goto out_unlock;
  18682. +
  18683. + err = -ENOMEM;
  18684. + slink = kmalloc(sizeof(*slink), GFP_NOFS);
  18685. + if (unlikely(!slink))
  18686. + goto out_unlock;
  18687. +
  18688. + err = -EBUSY;
  18689. + h_dentry = NULL;
  18690. + if (au_dbtop(dentry) <= bindex) {
  18691. + h_dentry = au_h_dptr(dentry, bindex);
  18692. + if (h_dentry)
  18693. + dget(h_dentry);
  18694. + }
  18695. + if (!h_dentry) {
  18696. + h_dentry = d_find_any_alias(h_inode);
  18697. + if (IS_ERR(h_dentry)) {
  18698. + err = PTR_ERR(h_dentry);
  18699. + goto out_free;
  18700. + }
  18701. + }
  18702. + if (unlikely(!h_dentry))
  18703. + goto out_free;
  18704. +
  18705. + err = 0;
  18706. + AuDbg("%pf\n", h_inode->i_op->follow_link);
  18707. + AuDbgDentry(h_dentry);
  18708. + ret = h_inode->i_op->follow_link(h_dentry, cookie);
  18709. + dput(h_dentry);
  18710. +
  18711. + if (!IS_ERR_OR_NULL(ret)) {
  18712. + au_symlink_add(inode->i_sb, slink, h_inode, *cookie);
  18713. + *cookie = slink;
  18714. + AuDbg("slink %p\n", slink);
  18715. + goto out_unlock; /* success */
  18716. + }
  18717. +
  18718. +out_free:
  18719. + slink->h_inode = NULL;
  18720. + kfree_rcu(slink, rcu);
  18721. +out_unlock:
  18722. + aufs_read_unlock(dentry, AuLock_IR);
  18723. +out:
  18724. + if (unlikely(err))
  18725. + ret = ERR_PTR(err);
  18726. + AuTraceErrPtr(ret);
  18727. + return ret;
  18728. +}
  18729. +
  18730. +static void aufs_put_link(struct inode *inode, void *cookie)
  18731. +{
  18732. + struct au_symlink *slink;
  18733. + struct inode *h_inode;
  18734. +
  18735. + slink = cookie;
  18736. + AuDbg("slink %p\n", slink);
  18737. + h_inode = slink->h_inode;
  18738. + AuDbg("%pf\n", h_inode->i_op->put_link);
  18739. + AuDbgInode(h_inode);
  18740. + if (h_inode->i_op->put_link)
  18741. + h_inode->i_op->put_link(h_inode, slink->h_cookie);
  18742. + au_symlink_del(inode->i_sb, slink);
  18743. +}
  18744. +
  18745. +/* ---------------------------------------------------------------------- */
  18746. +
  18747. +static int au_is_special(struct inode *inode)
  18748. +{
  18749. + return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
  18750. +}
  18751. +
  18752. +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
  18753. +{
  18754. + int err;
  18755. + aufs_bindex_t bindex;
  18756. + struct super_block *sb;
  18757. + struct inode *h_inode;
  18758. + struct vfsmount *h_mnt;
  18759. +
  18760. + sb = inode->i_sb;
  18761. + WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
  18762. + "unexpected s_flags 0x%lx", sb->s_flags);
  18763. +
  18764. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  18765. + lockdep_off();
  18766. + si_read_lock(sb, AuLock_FLUSH);
  18767. + ii_write_lock_child(inode);
  18768. + lockdep_on();
  18769. +
  18770. + err = 0;
  18771. + bindex = au_ibtop(inode);
  18772. + h_inode = au_h_iptr(inode, bindex);
  18773. + if (!au_test_ro(sb, bindex, inode)) {
  18774. + h_mnt = au_sbr_mnt(sb, bindex);
  18775. + err = vfsub_mnt_want_write(h_mnt);
  18776. + if (!err) {
  18777. + err = vfsub_update_time(h_inode, ts, flags);
  18778. + vfsub_mnt_drop_write(h_mnt);
  18779. + }
  18780. + } else if (au_is_special(h_inode)) {
  18781. + /*
  18782. + * Never copy-up here.
  18783. + * These special files may already be opened and used for
  18784. + * communicating. If we copied it up, then the communication
  18785. + * would be corrupted.
  18786. + */
  18787. + AuWarn1("timestamps for i%lu are ignored "
  18788. + "since it is on readonly branch (hi%lu).\n",
  18789. + inode->i_ino, h_inode->i_ino);
  18790. + } else if (flags & ~S_ATIME) {
  18791. + err = -EIO;
  18792. + AuIOErr1("unexpected flags 0x%x\n", flags);
  18793. + AuDebugOn(1);
  18794. + }
  18795. +
  18796. + lockdep_off();
  18797. + if (!err)
  18798. + au_cpup_attr_timesizes(inode);
  18799. + ii_write_unlock(inode);
  18800. + si_read_unlock(sb);
  18801. + lockdep_on();
  18802. +
  18803. + if (!err && (flags & S_VERSION))
  18804. + inode_inc_iversion(inode);
  18805. +
  18806. + return err;
  18807. +}
  18808. +
  18809. +/* ---------------------------------------------------------------------- */
  18810. +
  18811. +/* no getattr version will be set by module.c:aufs_init() */
  18812. +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
  18813. + aufs_iop[] = {
  18814. + [AuIop_SYMLINK] = {
  18815. + .permission = aufs_permission,
  18816. +#ifdef CONFIG_FS_POSIX_ACL
  18817. + .get_acl = aufs_get_acl,
  18818. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  18819. +#endif
  18820. +
  18821. + .setattr = aufs_setattr,
  18822. + .getattr = aufs_getattr,
  18823. +
  18824. +#ifdef CONFIG_AUFS_XATTR
  18825. + .setxattr = aufs_setxattr,
  18826. + .getxattr = aufs_getxattr,
  18827. + .listxattr = aufs_listxattr,
  18828. + .removexattr = aufs_removexattr,
  18829. +#endif
  18830. +
  18831. + .readlink = generic_readlink,
  18832. + .follow_link = aufs_follow_link,
  18833. + .put_link = aufs_put_link,
  18834. +
  18835. + /* .update_time = aufs_update_time */
  18836. + },
  18837. + [AuIop_DIR] = {
  18838. + .create = aufs_create,
  18839. + .lookup = aufs_lookup,
  18840. + .link = aufs_link,
  18841. + .unlink = aufs_unlink,
  18842. + .symlink = aufs_symlink,
  18843. + .mkdir = aufs_mkdir,
  18844. + .rmdir = aufs_rmdir,
  18845. + .mknod = aufs_mknod,
  18846. + .rename = aufs_rename,
  18847. +
  18848. + .permission = aufs_permission,
  18849. +#ifdef CONFIG_FS_POSIX_ACL
  18850. + .get_acl = aufs_get_acl,
  18851. + .set_acl = aufs_set_acl,
  18852. +#endif
  18853. +
  18854. + .setattr = aufs_setattr,
  18855. + .getattr = aufs_getattr,
  18856. +
  18857. +#ifdef CONFIG_AUFS_XATTR
  18858. + .setxattr = aufs_setxattr,
  18859. + .getxattr = aufs_getxattr,
  18860. + .listxattr = aufs_listxattr,
  18861. + .removexattr = aufs_removexattr,
  18862. +#endif
  18863. +
  18864. + .update_time = aufs_update_time,
  18865. + .atomic_open = aufs_atomic_open,
  18866. + .tmpfile = aufs_tmpfile
  18867. + },
  18868. + [AuIop_OTHER] = {
  18869. + .permission = aufs_permission,
  18870. +#ifdef CONFIG_FS_POSIX_ACL
  18871. + .get_acl = aufs_get_acl,
  18872. + .set_acl = aufs_set_acl,
  18873. +#endif
  18874. +
  18875. + .setattr = aufs_setattr,
  18876. + .getattr = aufs_getattr,
  18877. +
  18878. +#ifdef CONFIG_AUFS_XATTR
  18879. + .setxattr = aufs_setxattr,
  18880. + .getxattr = aufs_getxattr,
  18881. + .listxattr = aufs_listxattr,
  18882. + .removexattr = aufs_removexattr,
  18883. +#endif
  18884. +
  18885. + .update_time = aufs_update_time
  18886. + }
  18887. +};
  18888. --- /dev/null
  18889. +++ linux-4.4/fs/aufs/i_op_del.c 2016-10-23 11:20:57.635493285 +0300
  18890. @@ -0,0 +1,511 @@
  18891. +/*
  18892. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  18893. + *
  18894. + * This program, aufs is free software; you can redistribute it and/or modify
  18895. + * it under the terms of the GNU General Public License as published by
  18896. + * the Free Software Foundation; either version 2 of the License, or
  18897. + * (at your option) any later version.
  18898. + *
  18899. + * This program is distributed in the hope that it will be useful,
  18900. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18901. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18902. + * GNU General Public License for more details.
  18903. + *
  18904. + * You should have received a copy of the GNU General Public License
  18905. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18906. + */
  18907. +
  18908. +/*
  18909. + * inode operations (del entry)
  18910. + */
  18911. +
  18912. +#include "aufs.h"
  18913. +
  18914. +/*
  18915. + * decide if a new whiteout for @dentry is necessary or not.
  18916. + * when it is necessary, prepare the parent dir for the upper branch whose
  18917. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  18918. + * be done by caller.
  18919. + * return value:
  18920. + * 0: wh is unnecessary
  18921. + * plus: wh is necessary
  18922. + * minus: error
  18923. + */
  18924. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  18925. +{
  18926. + int need_wh, err;
  18927. + aufs_bindex_t btop;
  18928. + struct super_block *sb;
  18929. +
  18930. + sb = dentry->d_sb;
  18931. + btop = au_dbtop(dentry);
  18932. + if (*bcpup < 0) {
  18933. + *bcpup = btop;
  18934. + if (au_test_ro(sb, btop, d_inode(dentry))) {
  18935. + err = AuWbrCopyup(au_sbi(sb), dentry);
  18936. + *bcpup = err;
  18937. + if (unlikely(err < 0))
  18938. + goto out;
  18939. + }
  18940. + } else
  18941. + AuDebugOn(btop < *bcpup
  18942. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  18943. + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
  18944. +
  18945. + if (*bcpup != btop) {
  18946. + err = au_cpup_dirs(dentry, *bcpup);
  18947. + if (unlikely(err))
  18948. + goto out;
  18949. + need_wh = 1;
  18950. + } else {
  18951. + struct au_dinfo *dinfo, *tmp;
  18952. +
  18953. + need_wh = -ENOMEM;
  18954. + dinfo = au_di(dentry);
  18955. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  18956. + if (tmp) {
  18957. + au_di_cp(tmp, dinfo);
  18958. + au_di_swap(tmp, dinfo);
  18959. + /* returns the number of positive dentries */
  18960. + need_wh = au_lkup_dentry(dentry, btop + 1,
  18961. + /* AuLkup_IGNORE_PERM */ 0);
  18962. + au_di_swap(tmp, dinfo);
  18963. + au_rw_write_unlock(&tmp->di_rwsem);
  18964. + au_di_free(tmp);
  18965. + }
  18966. + }
  18967. + AuDbg("need_wh %d\n", need_wh);
  18968. + err = need_wh;
  18969. +
  18970. +out:
  18971. + return err;
  18972. +}
  18973. +
  18974. +/*
  18975. + * simple tests for the del-entry operations.
  18976. + * following the checks in vfs, plus the parent-child relationship.
  18977. + */
  18978. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  18979. + struct dentry *h_parent, int isdir)
  18980. +{
  18981. + int err;
  18982. + umode_t h_mode;
  18983. + struct dentry *h_dentry, *h_latest;
  18984. + struct inode *h_inode;
  18985. +
  18986. + h_dentry = au_h_dptr(dentry, bindex);
  18987. + if (d_really_is_positive(dentry)) {
  18988. + err = -ENOENT;
  18989. + if (unlikely(d_is_negative(h_dentry)))
  18990. + goto out;
  18991. + h_inode = d_inode(h_dentry);
  18992. + if (unlikely(!h_inode->i_nlink))
  18993. + goto out;
  18994. +
  18995. + h_mode = h_inode->i_mode;
  18996. + if (!isdir) {
  18997. + err = -EISDIR;
  18998. + if (unlikely(S_ISDIR(h_mode)))
  18999. + goto out;
  19000. + } else if (unlikely(!S_ISDIR(h_mode))) {
  19001. + err = -ENOTDIR;
  19002. + goto out;
  19003. + }
  19004. + } else {
  19005. + /* rename(2) case */
  19006. + err = -EIO;
  19007. + if (unlikely(d_is_positive(h_dentry)))
  19008. + goto out;
  19009. + }
  19010. +
  19011. + err = -ENOENT;
  19012. + /* expected parent dir is locked */
  19013. + if (unlikely(h_parent != h_dentry->d_parent))
  19014. + goto out;
  19015. + err = 0;
  19016. +
  19017. + /*
  19018. + * rmdir a dir may break the consistency on some filesystem.
  19019. + * let's try heavy test.
  19020. + */
  19021. + err = -EACCES;
  19022. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  19023. + && au_test_h_perm(d_inode(h_parent),
  19024. + MAY_EXEC | MAY_WRITE)))
  19025. + goto out;
  19026. +
  19027. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  19028. + err = -EIO;
  19029. + if (IS_ERR(h_latest))
  19030. + goto out;
  19031. + if (h_latest == h_dentry)
  19032. + err = 0;
  19033. + dput(h_latest);
  19034. +
  19035. +out:
  19036. + return err;
  19037. +}
  19038. +
  19039. +/*
  19040. + * decide the branch where we operate for @dentry. the branch index will be set
  19041. + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
  19042. + * dir for reverting.
  19043. + * when a new whiteout is necessary, create it.
  19044. + */
  19045. +static struct dentry*
  19046. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  19047. + struct au_dtime *dt, struct au_pin *pin)
  19048. +{
  19049. + struct dentry *wh_dentry;
  19050. + struct super_block *sb;
  19051. + struct path h_path;
  19052. + int err, need_wh;
  19053. + unsigned int udba;
  19054. + aufs_bindex_t bcpup;
  19055. +
  19056. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  19057. + wh_dentry = ERR_PTR(need_wh);
  19058. + if (unlikely(need_wh < 0))
  19059. + goto out;
  19060. +
  19061. + sb = dentry->d_sb;
  19062. + udba = au_opt_udba(sb);
  19063. + bcpup = *rbcpup;
  19064. + err = au_pin(pin, dentry, bcpup, udba,
  19065. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19066. + wh_dentry = ERR_PTR(err);
  19067. + if (unlikely(err))
  19068. + goto out;
  19069. +
  19070. + h_path.dentry = au_pinned_h_parent(pin);
  19071. + if (udba != AuOpt_UDBA_NONE
  19072. + && au_dbtop(dentry) == bcpup) {
  19073. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  19074. + wh_dentry = ERR_PTR(err);
  19075. + if (unlikely(err))
  19076. + goto out_unpin;
  19077. + }
  19078. +
  19079. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  19080. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  19081. + wh_dentry = NULL;
  19082. + if (!need_wh)
  19083. + goto out; /* success, no need to create whiteout */
  19084. +
  19085. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  19086. + if (IS_ERR(wh_dentry))
  19087. + goto out_unpin;
  19088. +
  19089. + /* returns with the parent is locked and wh_dentry is dget-ed */
  19090. + goto out; /* success */
  19091. +
  19092. +out_unpin:
  19093. + au_unpin(pin);
  19094. +out:
  19095. + return wh_dentry;
  19096. +}
  19097. +
  19098. +/*
  19099. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  19100. + * in order to be revertible and save time for removing many child whiteouts
  19101. + * under the dir.
  19102. + * returns 1 when there are too many child whiteout and caller should remove
  19103. + * them asynchronously. returns 0 when the number of children is enough small to
  19104. + * remove now or the branch fs is a remote fs.
  19105. + * otherwise return an error.
  19106. + */
  19107. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  19108. + struct au_nhash *whlist, struct inode *dir)
  19109. +{
  19110. + int rmdir_later, err, dirwh;
  19111. + struct dentry *h_dentry;
  19112. + struct super_block *sb;
  19113. + struct inode *inode;
  19114. +
  19115. + sb = dentry->d_sb;
  19116. + SiMustAnyLock(sb);
  19117. + h_dentry = au_h_dptr(dentry, bindex);
  19118. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  19119. + if (unlikely(err))
  19120. + goto out;
  19121. +
  19122. + /* stop monitoring */
  19123. + inode = d_inode(dentry);
  19124. + au_hn_free(au_hi(inode, bindex));
  19125. +
  19126. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  19127. + dirwh = au_sbi(sb)->si_dirwh;
  19128. + rmdir_later = (dirwh <= 1);
  19129. + if (!rmdir_later)
  19130. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  19131. + dirwh);
  19132. + if (rmdir_later)
  19133. + return rmdir_later;
  19134. + }
  19135. +
  19136. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  19137. + if (unlikely(err)) {
  19138. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  19139. + h_dentry, bindex, err);
  19140. + err = 0;
  19141. + }
  19142. +
  19143. +out:
  19144. + AuTraceErr(err);
  19145. + return err;
  19146. +}
  19147. +
  19148. +/*
  19149. + * final procedure for deleting a entry.
  19150. + * maintain dentry and iattr.
  19151. + */
  19152. +static void epilog(struct inode *dir, struct dentry *dentry,
  19153. + aufs_bindex_t bindex)
  19154. +{
  19155. + struct inode *inode;
  19156. +
  19157. + inode = d_inode(dentry);
  19158. + d_drop(dentry);
  19159. + inode->i_ctime = dir->i_ctime;
  19160. +
  19161. + au_dir_ts(dir, bindex);
  19162. + dir->i_version++;
  19163. +}
  19164. +
  19165. +/*
  19166. + * when an error happened, remove the created whiteout and revert everything.
  19167. + */
  19168. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  19169. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  19170. + struct dentry *dentry, struct au_dtime *dt)
  19171. +{
  19172. + int rerr;
  19173. + struct path h_path = {
  19174. + .dentry = wh_dentry,
  19175. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  19176. + };
  19177. +
  19178. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  19179. + if (!rerr) {
  19180. + au_set_dbwh(dentry, bwh);
  19181. + au_dtime_revert(dt);
  19182. + return 0;
  19183. + }
  19184. +
  19185. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  19186. + return -EIO;
  19187. +}
  19188. +
  19189. +/* ---------------------------------------------------------------------- */
  19190. +
  19191. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  19192. +{
  19193. + int err;
  19194. + aufs_bindex_t bwh, bindex, btop;
  19195. + struct inode *inode, *h_dir, *delegated;
  19196. + struct dentry *parent, *wh_dentry;
  19197. + /* to reuduce stack size */
  19198. + struct {
  19199. + struct au_dtime dt;
  19200. + struct au_pin pin;
  19201. + struct path h_path;
  19202. + } *a;
  19203. +
  19204. + IMustLock(dir);
  19205. +
  19206. + err = -ENOMEM;
  19207. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19208. + if (unlikely(!a))
  19209. + goto out;
  19210. +
  19211. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  19212. + if (unlikely(err))
  19213. + goto out_free;
  19214. + err = au_d_hashed_positive(dentry);
  19215. + if (unlikely(err))
  19216. + goto out_unlock;
  19217. + inode = d_inode(dentry);
  19218. + IMustLock(inode);
  19219. + err = -EISDIR;
  19220. + if (unlikely(d_is_dir(dentry)))
  19221. + goto out_unlock; /* possible? */
  19222. +
  19223. + btop = au_dbtop(dentry);
  19224. + bwh = au_dbwh(dentry);
  19225. + bindex = -1;
  19226. + parent = dentry->d_parent; /* dir inode is locked */
  19227. + di_write_lock_parent(parent);
  19228. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  19229. + &a->pin);
  19230. + err = PTR_ERR(wh_dentry);
  19231. + if (IS_ERR(wh_dentry))
  19232. + goto out_parent;
  19233. +
  19234. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
  19235. + a->h_path.dentry = au_h_dptr(dentry, btop);
  19236. + dget(a->h_path.dentry);
  19237. + if (bindex == btop) {
  19238. + h_dir = au_pinned_h_dir(&a->pin);
  19239. + delegated = NULL;
  19240. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  19241. + if (unlikely(err == -EWOULDBLOCK)) {
  19242. + pr_warn("cannot retry for NFSv4 delegation"
  19243. + " for an internal unlink\n");
  19244. + iput(delegated);
  19245. + }
  19246. + } else {
  19247. + /* dir inode is locked */
  19248. + h_dir = d_inode(wh_dentry->d_parent);
  19249. + IMustLock(h_dir);
  19250. + err = 0;
  19251. + }
  19252. +
  19253. + if (!err) {
  19254. + vfsub_drop_nlink(inode);
  19255. + epilog(dir, dentry, bindex);
  19256. +
  19257. + /* update target timestamps */
  19258. + if (bindex == btop) {
  19259. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  19260. + /*ignore*/
  19261. + inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  19262. + } else
  19263. + /* todo: this timestamp may be reverted later */
  19264. + inode->i_ctime = h_dir->i_ctime;
  19265. + goto out_unpin; /* success */
  19266. + }
  19267. +
  19268. + /* revert */
  19269. + if (wh_dentry) {
  19270. + int rerr;
  19271. +
  19272. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  19273. + &a->dt);
  19274. + if (rerr)
  19275. + err = rerr;
  19276. + }
  19277. +
  19278. +out_unpin:
  19279. + au_unpin(&a->pin);
  19280. + dput(wh_dentry);
  19281. + dput(a->h_path.dentry);
  19282. +out_parent:
  19283. + di_write_unlock(parent);
  19284. +out_unlock:
  19285. + aufs_read_unlock(dentry, AuLock_DW);
  19286. +out_free:
  19287. + au_delayed_kfree(a);
  19288. +out:
  19289. + return err;
  19290. +}
  19291. +
  19292. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  19293. +{
  19294. + int err, rmdir_later;
  19295. + aufs_bindex_t bwh, bindex, btop;
  19296. + struct inode *inode;
  19297. + struct dentry *parent, *wh_dentry, *h_dentry;
  19298. + struct au_whtmp_rmdir *args;
  19299. + /* to reuduce stack size */
  19300. + struct {
  19301. + struct au_dtime dt;
  19302. + struct au_pin pin;
  19303. + } *a;
  19304. +
  19305. + IMustLock(dir);
  19306. +
  19307. + err = -ENOMEM;
  19308. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19309. + if (unlikely(!a))
  19310. + goto out;
  19311. +
  19312. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  19313. + if (unlikely(err))
  19314. + goto out_free;
  19315. + err = au_alive_dir(dentry);
  19316. + if (unlikely(err))
  19317. + goto out_unlock;
  19318. + inode = d_inode(dentry);
  19319. + IMustLock(inode);
  19320. + err = -ENOTDIR;
  19321. + if (unlikely(!d_is_dir(dentry)))
  19322. + goto out_unlock; /* possible? */
  19323. +
  19324. + err = -ENOMEM;
  19325. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  19326. + if (unlikely(!args))
  19327. + goto out_unlock;
  19328. +
  19329. + parent = dentry->d_parent; /* dir inode is locked */
  19330. + di_write_lock_parent(parent);
  19331. + err = au_test_empty(dentry, &args->whlist);
  19332. + if (unlikely(err))
  19333. + goto out_parent;
  19334. +
  19335. + btop = au_dbtop(dentry);
  19336. + bwh = au_dbwh(dentry);
  19337. + bindex = -1;
  19338. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  19339. + &a->pin);
  19340. + err = PTR_ERR(wh_dentry);
  19341. + if (IS_ERR(wh_dentry))
  19342. + goto out_parent;
  19343. +
  19344. + h_dentry = au_h_dptr(dentry, btop);
  19345. + dget(h_dentry);
  19346. + rmdir_later = 0;
  19347. + if (bindex == btop) {
  19348. + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
  19349. + if (err > 0) {
  19350. + rmdir_later = err;
  19351. + err = 0;
  19352. + }
  19353. + } else {
  19354. + /* stop monitoring */
  19355. + au_hn_free(au_hi(inode, btop));
  19356. +
  19357. + /* dir inode is locked */
  19358. + IMustLock(d_inode(wh_dentry->d_parent));
  19359. + err = 0;
  19360. + }
  19361. +
  19362. + if (!err) {
  19363. + vfsub_dead_dir(inode);
  19364. + au_set_dbdiropq(dentry, -1);
  19365. + epilog(dir, dentry, bindex);
  19366. +
  19367. + if (rmdir_later) {
  19368. + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
  19369. + args = NULL;
  19370. + }
  19371. +
  19372. + goto out_unpin; /* success */
  19373. + }
  19374. +
  19375. + /* revert */
  19376. + AuLabel(revert);
  19377. + if (wh_dentry) {
  19378. + int rerr;
  19379. +
  19380. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  19381. + &a->dt);
  19382. + if (rerr)
  19383. + err = rerr;
  19384. + }
  19385. +
  19386. +out_unpin:
  19387. + au_unpin(&a->pin);
  19388. + dput(wh_dentry);
  19389. + dput(h_dentry);
  19390. +out_parent:
  19391. + di_write_unlock(parent);
  19392. + if (args)
  19393. + au_whtmp_rmdir_free(args);
  19394. +out_unlock:
  19395. + aufs_read_unlock(dentry, AuLock_DW);
  19396. +out_free:
  19397. + au_delayed_kfree(a);
  19398. +out:
  19399. + AuTraceErr(err);
  19400. + return err;
  19401. +}
  19402. --- /dev/null
  19403. +++ linux-4.4/fs/aufs/i_op_ren.c 2016-10-23 11:20:57.635493285 +0300
  19404. @@ -0,0 +1,1015 @@
  19405. +/*
  19406. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  19407. + *
  19408. + * This program, aufs is free software; you can redistribute it and/or modify
  19409. + * it under the terms of the GNU General Public License as published by
  19410. + * the Free Software Foundation; either version 2 of the License, or
  19411. + * (at your option) any later version.
  19412. + *
  19413. + * This program is distributed in the hope that it will be useful,
  19414. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19415. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19416. + * GNU General Public License for more details.
  19417. + *
  19418. + * You should have received a copy of the GNU General Public License
  19419. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19420. + */
  19421. +
  19422. +/*
  19423. + * inode operation (rename entry)
  19424. + * todo: this is crazy monster
  19425. + */
  19426. +
  19427. +#include "aufs.h"
  19428. +
  19429. +enum { AuSRC, AuDST, AuSrcDst };
  19430. +enum { AuPARENT, AuCHILD, AuParentChild };
  19431. +
  19432. +#define AuRen_ISDIR 1
  19433. +#define AuRen_ISSAMEDIR (1 << 1)
  19434. +#define AuRen_WHSRC (1 << 2)
  19435. +#define AuRen_WHDST (1 << 3)
  19436. +#define AuRen_MNT_WRITE (1 << 4)
  19437. +#define AuRen_DT_DSTDIR (1 << 5)
  19438. +#define AuRen_DIROPQ (1 << 6)
  19439. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  19440. +#define au_fset_ren(flags, name) \
  19441. + do { (flags) |= AuRen_##name; } while (0)
  19442. +#define au_fclr_ren(flags, name) \
  19443. + do { (flags) &= ~AuRen_##name; } while (0)
  19444. +
  19445. +struct au_ren_args {
  19446. + struct {
  19447. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  19448. + *wh_dentry;
  19449. + struct inode *dir, *inode;
  19450. + struct au_hinode *hdir;
  19451. + struct au_dtime dt[AuParentChild];
  19452. + aufs_bindex_t btop;
  19453. + } sd[AuSrcDst];
  19454. +
  19455. +#define src_dentry sd[AuSRC].dentry
  19456. +#define src_dir sd[AuSRC].dir
  19457. +#define src_inode sd[AuSRC].inode
  19458. +#define src_h_dentry sd[AuSRC].h_dentry
  19459. +#define src_parent sd[AuSRC].parent
  19460. +#define src_h_parent sd[AuSRC].h_parent
  19461. +#define src_wh_dentry sd[AuSRC].wh_dentry
  19462. +#define src_hdir sd[AuSRC].hdir
  19463. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  19464. +#define src_dt sd[AuSRC].dt
  19465. +#define src_btop sd[AuSRC].btop
  19466. +
  19467. +#define dst_dentry sd[AuDST].dentry
  19468. +#define dst_dir sd[AuDST].dir
  19469. +#define dst_inode sd[AuDST].inode
  19470. +#define dst_h_dentry sd[AuDST].h_dentry
  19471. +#define dst_parent sd[AuDST].parent
  19472. +#define dst_h_parent sd[AuDST].h_parent
  19473. +#define dst_wh_dentry sd[AuDST].wh_dentry
  19474. +#define dst_hdir sd[AuDST].hdir
  19475. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  19476. +#define dst_dt sd[AuDST].dt
  19477. +#define dst_btop sd[AuDST].btop
  19478. +
  19479. + struct dentry *h_trap;
  19480. + struct au_branch *br;
  19481. + struct au_hinode *src_hinode;
  19482. + struct path h_path;
  19483. + struct au_nhash whlist;
  19484. + aufs_bindex_t btgt, src_bwh, src_bdiropq;
  19485. +
  19486. + unsigned int flags;
  19487. +
  19488. + struct au_whtmp_rmdir *thargs;
  19489. + struct dentry *h_dst;
  19490. +};
  19491. +
  19492. +/* ---------------------------------------------------------------------- */
  19493. +
  19494. +/*
  19495. + * functions for reverting.
  19496. + * when an error happened in a single rename systemcall, we should revert
  19497. + * everything as if nothing happened.
  19498. + * we don't need to revert the copied-up/down the parent dir since they are
  19499. + * harmless.
  19500. + */
  19501. +
  19502. +#define RevertFailure(fmt, ...) do { \
  19503. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  19504. + ##__VA_ARGS__, err, rerr); \
  19505. + err = -EIO; \
  19506. +} while (0)
  19507. +
  19508. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  19509. +{
  19510. + int rerr;
  19511. +
  19512. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  19513. + rerr = au_diropq_remove(a->src_dentry, a->btgt);
  19514. + au_hn_imtx_unlock(a->src_hinode);
  19515. + au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
  19516. + if (rerr)
  19517. + RevertFailure("remove diropq %pd", a->src_dentry);
  19518. +}
  19519. +
  19520. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  19521. +{
  19522. + int rerr;
  19523. + struct inode *delegated;
  19524. +
  19525. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  19526. + a->src_h_parent);
  19527. + rerr = PTR_ERR(a->h_path.dentry);
  19528. + if (IS_ERR(a->h_path.dentry)) {
  19529. + RevertFailure("lkup one %pd", a->src_dentry);
  19530. + return;
  19531. + }
  19532. +
  19533. + delegated = NULL;
  19534. + rerr = vfsub_rename(a->dst_h_dir,
  19535. + au_h_dptr(a->src_dentry, a->btgt),
  19536. + a->src_h_dir, &a->h_path, &delegated);
  19537. + if (unlikely(rerr == -EWOULDBLOCK)) {
  19538. + pr_warn("cannot retry for NFSv4 delegation"
  19539. + " for an internal rename\n");
  19540. + iput(delegated);
  19541. + }
  19542. + d_drop(a->h_path.dentry);
  19543. + dput(a->h_path.dentry);
  19544. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  19545. + if (rerr)
  19546. + RevertFailure("rename %pd", a->src_dentry);
  19547. +}
  19548. +
  19549. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  19550. +{
  19551. + int rerr;
  19552. + struct inode *delegated;
  19553. +
  19554. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  19555. + a->dst_h_parent);
  19556. + rerr = PTR_ERR(a->h_path.dentry);
  19557. + if (IS_ERR(a->h_path.dentry)) {
  19558. + RevertFailure("lkup one %pd", a->dst_dentry);
  19559. + return;
  19560. + }
  19561. + if (d_is_positive(a->h_path.dentry)) {
  19562. + d_drop(a->h_path.dentry);
  19563. + dput(a->h_path.dentry);
  19564. + return;
  19565. + }
  19566. +
  19567. + delegated = NULL;
  19568. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  19569. + &delegated);
  19570. + if (unlikely(rerr == -EWOULDBLOCK)) {
  19571. + pr_warn("cannot retry for NFSv4 delegation"
  19572. + " for an internal rename\n");
  19573. + iput(delegated);
  19574. + }
  19575. + d_drop(a->h_path.dentry);
  19576. + dput(a->h_path.dentry);
  19577. + if (!rerr)
  19578. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  19579. + else
  19580. + RevertFailure("rename %pd", a->h_dst);
  19581. +}
  19582. +
  19583. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  19584. +{
  19585. + int rerr;
  19586. +
  19587. + a->h_path.dentry = a->src_wh_dentry;
  19588. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  19589. + au_set_dbwh(a->src_dentry, a->src_bwh);
  19590. + if (rerr)
  19591. + RevertFailure("unlink %pd", a->src_wh_dentry);
  19592. +}
  19593. +#undef RevertFailure
  19594. +
  19595. +/* ---------------------------------------------------------------------- */
  19596. +
  19597. +/*
  19598. + * when we have to copyup the renaming entry, do it with the rename-target name
  19599. + * in order to minimize the cost (the later actual rename is unnecessary).
  19600. + * otherwise rename it on the target branch.
  19601. + */
  19602. +static int au_ren_or_cpup(struct au_ren_args *a)
  19603. +{
  19604. + int err;
  19605. + struct dentry *d;
  19606. + struct inode *delegated;
  19607. +
  19608. + d = a->src_dentry;
  19609. + if (au_dbtop(d) == a->btgt) {
  19610. + a->h_path.dentry = a->dst_h_dentry;
  19611. + if (au_ftest_ren(a->flags, DIROPQ)
  19612. + && au_dbdiropq(d) == a->btgt)
  19613. + au_fclr_ren(a->flags, DIROPQ);
  19614. + AuDebugOn(au_dbtop(d) != a->btgt);
  19615. + delegated = NULL;
  19616. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  19617. + a->dst_h_dir, &a->h_path, &delegated);
  19618. + if (unlikely(err == -EWOULDBLOCK)) {
  19619. + pr_warn("cannot retry for NFSv4 delegation"
  19620. + " for an internal rename\n");
  19621. + iput(delegated);
  19622. + }
  19623. + } else
  19624. + BUG();
  19625. +
  19626. + if (!err && a->h_dst)
  19627. + /* it will be set to dinfo later */
  19628. + dget(a->h_dst);
  19629. +
  19630. + return err;
  19631. +}
  19632. +
  19633. +/* cf. aufs_rmdir() */
  19634. +static int au_ren_del_whtmp(struct au_ren_args *a)
  19635. +{
  19636. + int err;
  19637. + struct inode *dir;
  19638. +
  19639. + dir = a->dst_dir;
  19640. + SiMustAnyLock(dir->i_sb);
  19641. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  19642. + au_sbi(dir->i_sb)->si_dirwh)
  19643. + || au_test_fs_remote(a->h_dst->d_sb)) {
  19644. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  19645. + if (unlikely(err))
  19646. + pr_warn("failed removing whtmp dir %pd (%d), "
  19647. + "ignored.\n", a->h_dst, err);
  19648. + } else {
  19649. + au_nhash_wh_free(&a->thargs->whlist);
  19650. + a->thargs->whlist = a->whlist;
  19651. + a->whlist.nh_num = 0;
  19652. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  19653. + dput(a->h_dst);
  19654. + a->thargs = NULL;
  19655. + }
  19656. +
  19657. + return 0;
  19658. +}
  19659. +
  19660. +/* make it 'opaque' dir. */
  19661. +static int au_ren_diropq(struct au_ren_args *a)
  19662. +{
  19663. + int err;
  19664. + struct dentry *diropq;
  19665. +
  19666. + err = 0;
  19667. + a->src_bdiropq = au_dbdiropq(a->src_dentry);
  19668. + a->src_hinode = au_hi(a->src_inode, a->btgt);
  19669. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  19670. + diropq = au_diropq_create(a->src_dentry, a->btgt);
  19671. + au_hn_imtx_unlock(a->src_hinode);
  19672. + if (IS_ERR(diropq))
  19673. + err = PTR_ERR(diropq);
  19674. + else
  19675. + dput(diropq);
  19676. +
  19677. + return err;
  19678. +}
  19679. +
  19680. +static int do_rename(struct au_ren_args *a)
  19681. +{
  19682. + int err;
  19683. + struct dentry *d, *h_d;
  19684. +
  19685. + /* prepare workqueue args for asynchronous rmdir */
  19686. + h_d = a->dst_h_dentry;
  19687. + if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
  19688. + err = -ENOMEM;
  19689. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
  19690. + if (unlikely(!a->thargs))
  19691. + goto out;
  19692. + a->h_dst = dget(h_d);
  19693. + }
  19694. +
  19695. + /* create whiteout for src_dentry */
  19696. + if (au_ftest_ren(a->flags, WHSRC)) {
  19697. + a->src_bwh = au_dbwh(a->src_dentry);
  19698. + AuDebugOn(a->src_bwh >= 0);
  19699. + a->src_wh_dentry
  19700. + = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
  19701. + err = PTR_ERR(a->src_wh_dentry);
  19702. + if (IS_ERR(a->src_wh_dentry))
  19703. + goto out_thargs;
  19704. + }
  19705. +
  19706. + /* lookup whiteout for dentry */
  19707. + if (au_ftest_ren(a->flags, WHDST)) {
  19708. + h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
  19709. + a->br);
  19710. + err = PTR_ERR(h_d);
  19711. + if (IS_ERR(h_d))
  19712. + goto out_whsrc;
  19713. + if (d_is_negative(h_d))
  19714. + dput(h_d);
  19715. + else
  19716. + a->dst_wh_dentry = h_d;
  19717. + }
  19718. +
  19719. + /* rename dentry to tmpwh */
  19720. + if (a->thargs) {
  19721. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  19722. + if (unlikely(err))
  19723. + goto out_whdst;
  19724. +
  19725. + d = a->dst_dentry;
  19726. + au_set_h_dptr(d, a->btgt, NULL);
  19727. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  19728. + if (unlikely(err))
  19729. + goto out_whtmp;
  19730. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  19731. + }
  19732. +
  19733. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
  19734. +
  19735. + /* rename by vfs_rename or cpup */
  19736. + d = a->dst_dentry;
  19737. + if (au_ftest_ren(a->flags, ISDIR)
  19738. + && (a->dst_wh_dentry
  19739. + || au_dbdiropq(d) == a->btgt
  19740. + /* hide the lower to keep xino */
  19741. + || a->btgt < au_dbbot(d)
  19742. + || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
  19743. + au_fset_ren(a->flags, DIROPQ);
  19744. + err = au_ren_or_cpup(a);
  19745. + if (unlikely(err))
  19746. + /* leave the copied-up one */
  19747. + goto out_whtmp;
  19748. +
  19749. + /* make dir opaque */
  19750. + if (au_ftest_ren(a->flags, DIROPQ)) {
  19751. + err = au_ren_diropq(a);
  19752. + if (unlikely(err))
  19753. + goto out_rename;
  19754. + }
  19755. +
  19756. + /* update target timestamps */
  19757. + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
  19758. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  19759. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  19760. + a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  19761. +
  19762. + /* remove whiteout for dentry */
  19763. + if (a->dst_wh_dentry) {
  19764. + a->h_path.dentry = a->dst_wh_dentry;
  19765. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  19766. + a->dst_dentry);
  19767. + if (unlikely(err))
  19768. + goto out_diropq;
  19769. + }
  19770. +
  19771. + /* remove whtmp */
  19772. + if (a->thargs)
  19773. + au_ren_del_whtmp(a); /* ignore this error */
  19774. +
  19775. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  19776. + err = 0;
  19777. + goto out_success;
  19778. +
  19779. +out_diropq:
  19780. + if (au_ftest_ren(a->flags, DIROPQ))
  19781. + au_ren_rev_diropq(err, a);
  19782. +out_rename:
  19783. + au_ren_rev_rename(err, a);
  19784. + dput(a->h_dst);
  19785. +out_whtmp:
  19786. + if (a->thargs)
  19787. + au_ren_rev_whtmp(err, a);
  19788. +out_whdst:
  19789. + dput(a->dst_wh_dentry);
  19790. + a->dst_wh_dentry = NULL;
  19791. +out_whsrc:
  19792. + if (a->src_wh_dentry)
  19793. + au_ren_rev_whsrc(err, a);
  19794. +out_success:
  19795. + dput(a->src_wh_dentry);
  19796. + dput(a->dst_wh_dentry);
  19797. +out_thargs:
  19798. + if (a->thargs) {
  19799. + dput(a->h_dst);
  19800. + au_whtmp_rmdir_free(a->thargs);
  19801. + a->thargs = NULL;
  19802. + }
  19803. +out:
  19804. + return err;
  19805. +}
  19806. +
  19807. +/* ---------------------------------------------------------------------- */
  19808. +
  19809. +/*
  19810. + * test if @dentry dir can be rename destination or not.
  19811. + * success means, it is a logically empty dir.
  19812. + */
  19813. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  19814. +{
  19815. + return au_test_empty(dentry, whlist);
  19816. +}
  19817. +
  19818. +/*
  19819. + * test if @dentry dir can be rename source or not.
  19820. + * if it can, return 0 and @children is filled.
  19821. + * success means,
  19822. + * - it is a logically empty dir.
  19823. + * - or, it exists on writable branch and has no children including whiteouts
  19824. + * on the lower branch.
  19825. + */
  19826. +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
  19827. +{
  19828. + int err;
  19829. + unsigned int rdhash;
  19830. + aufs_bindex_t btop;
  19831. +
  19832. + btop = au_dbtop(dentry);
  19833. + if (btop != btgt) {
  19834. + struct au_nhash whlist;
  19835. +
  19836. + SiMustAnyLock(dentry->d_sb);
  19837. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  19838. + if (!rdhash)
  19839. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  19840. + dentry));
  19841. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  19842. + if (unlikely(err))
  19843. + goto out;
  19844. + err = au_test_empty(dentry, &whlist);
  19845. + au_nhash_wh_free(&whlist);
  19846. + goto out;
  19847. + }
  19848. +
  19849. + if (btop == au_dbtaildir(dentry))
  19850. + return 0; /* success */
  19851. +
  19852. + err = au_test_empty_lower(dentry);
  19853. +
  19854. +out:
  19855. + if (err == -ENOTEMPTY) {
  19856. + AuWarn1("renaming dir who has child(ren) on multiple branches,"
  19857. + " is not supported\n");
  19858. + err = -EXDEV;
  19859. + }
  19860. + return err;
  19861. +}
  19862. +
  19863. +/* side effect: sets whlist and h_dentry */
  19864. +static int au_ren_may_dir(struct au_ren_args *a)
  19865. +{
  19866. + int err;
  19867. + unsigned int rdhash;
  19868. + struct dentry *d;
  19869. +
  19870. + d = a->dst_dentry;
  19871. + SiMustAnyLock(d->d_sb);
  19872. +
  19873. + err = 0;
  19874. + if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
  19875. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  19876. + if (!rdhash)
  19877. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  19878. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  19879. + if (unlikely(err))
  19880. + goto out;
  19881. +
  19882. + au_set_dbtop(d, a->dst_btop);
  19883. + err = may_rename_dstdir(d, &a->whlist);
  19884. + au_set_dbtop(d, a->btgt);
  19885. + }
  19886. + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
  19887. + if (unlikely(err))
  19888. + goto out;
  19889. +
  19890. + d = a->src_dentry;
  19891. + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
  19892. + if (au_ftest_ren(a->flags, ISDIR)) {
  19893. + err = may_rename_srcdir(d, a->btgt);
  19894. + if (unlikely(err)) {
  19895. + au_nhash_wh_free(&a->whlist);
  19896. + a->whlist.nh_num = 0;
  19897. + }
  19898. + }
  19899. +out:
  19900. + return err;
  19901. +}
  19902. +
  19903. +/* ---------------------------------------------------------------------- */
  19904. +
  19905. +/*
  19906. + * simple tests for rename.
  19907. + * following the checks in vfs, plus the parent-child relationship.
  19908. + */
  19909. +static int au_may_ren(struct au_ren_args *a)
  19910. +{
  19911. + int err, isdir;
  19912. + struct inode *h_inode;
  19913. +
  19914. + if (a->src_btop == a->btgt) {
  19915. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  19916. + au_ftest_ren(a->flags, ISDIR));
  19917. + if (unlikely(err))
  19918. + goto out;
  19919. + err = -EINVAL;
  19920. + if (unlikely(a->src_h_dentry == a->h_trap))
  19921. + goto out;
  19922. + }
  19923. +
  19924. + err = 0;
  19925. + if (a->dst_btop != a->btgt)
  19926. + goto out;
  19927. +
  19928. + err = -ENOTEMPTY;
  19929. + if (unlikely(a->dst_h_dentry == a->h_trap))
  19930. + goto out;
  19931. +
  19932. + err = -EIO;
  19933. + isdir = !!au_ftest_ren(a->flags, ISDIR);
  19934. + if (d_really_is_negative(a->dst_dentry)) {
  19935. + if (d_is_negative(a->dst_h_dentry))
  19936. + err = au_may_add(a->dst_dentry, a->btgt,
  19937. + a->dst_h_parent, isdir);
  19938. + } else {
  19939. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  19940. + goto out;
  19941. + h_inode = d_inode(a->dst_h_dentry);
  19942. + if (h_inode->i_nlink)
  19943. + err = au_may_del(a->dst_dentry, a->btgt,
  19944. + a->dst_h_parent, isdir);
  19945. + }
  19946. +
  19947. +out:
  19948. + if (unlikely(err == -ENOENT || err == -EEXIST))
  19949. + err = -EIO;
  19950. + AuTraceErr(err);
  19951. + return err;
  19952. +}
  19953. +
  19954. +/* ---------------------------------------------------------------------- */
  19955. +
  19956. +/*
  19957. + * locking order
  19958. + * (VFS)
  19959. + * - src_dir and dir by lock_rename()
  19960. + * - inode if exitsts
  19961. + * (aufs)
  19962. + * - lock all
  19963. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  19964. + * + si_read_lock
  19965. + * + di_write_lock2_child()
  19966. + * + di_write_lock_child()
  19967. + * + ii_write_lock_child()
  19968. + * + di_write_lock_child2()
  19969. + * + ii_write_lock_child2()
  19970. + * + src_parent and parent
  19971. + * + di_write_lock_parent()
  19972. + * + ii_write_lock_parent()
  19973. + * + di_write_lock_parent2()
  19974. + * + ii_write_lock_parent2()
  19975. + * + lower src_dir and dir by vfsub_lock_rename()
  19976. + * + verify the every relationships between child and parent. if any
  19977. + * of them failed, unlock all and return -EBUSY.
  19978. + */
  19979. +static void au_ren_unlock(struct au_ren_args *a)
  19980. +{
  19981. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  19982. + a->dst_h_parent, a->dst_hdir);
  19983. + if (au_ftest_ren(a->flags, MNT_WRITE))
  19984. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  19985. +}
  19986. +
  19987. +static int au_ren_lock(struct au_ren_args *a)
  19988. +{
  19989. + int err;
  19990. + unsigned int udba;
  19991. +
  19992. + err = 0;
  19993. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  19994. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  19995. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  19996. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  19997. +
  19998. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  19999. + if (unlikely(err))
  20000. + goto out;
  20001. + au_fset_ren(a->flags, MNT_WRITE);
  20002. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  20003. + a->dst_h_parent, a->dst_hdir);
  20004. + udba = au_opt_udba(a->src_dentry->d_sb);
  20005. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  20006. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  20007. + err = au_busy_or_stale();
  20008. + if (!err && au_dbtop(a->src_dentry) == a->btgt)
  20009. + err = au_h_verify(a->src_h_dentry, udba,
  20010. + d_inode(a->src_h_parent), a->src_h_parent,
  20011. + a->br);
  20012. + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
  20013. + err = au_h_verify(a->dst_h_dentry, udba,
  20014. + d_inode(a->dst_h_parent), a->dst_h_parent,
  20015. + a->br);
  20016. + if (!err)
  20017. + goto out; /* success */
  20018. +
  20019. + err = au_busy_or_stale();
  20020. + au_ren_unlock(a);
  20021. +
  20022. +out:
  20023. + return err;
  20024. +}
  20025. +
  20026. +/* ---------------------------------------------------------------------- */
  20027. +
  20028. +static void au_ren_refresh_dir(struct au_ren_args *a)
  20029. +{
  20030. + struct inode *dir;
  20031. +
  20032. + dir = a->dst_dir;
  20033. + dir->i_version++;
  20034. + if (au_ftest_ren(a->flags, ISDIR)) {
  20035. + /* is this updating defined in POSIX? */
  20036. + au_cpup_attr_timesizes(a->src_inode);
  20037. + au_cpup_attr_nlink(dir, /*force*/1);
  20038. + }
  20039. +
  20040. + au_dir_ts(dir, a->btgt);
  20041. +
  20042. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  20043. + return;
  20044. +
  20045. + dir = a->src_dir;
  20046. + dir->i_version++;
  20047. + if (au_ftest_ren(a->flags, ISDIR))
  20048. + au_cpup_attr_nlink(dir, /*force*/1);
  20049. + au_dir_ts(dir, a->btgt);
  20050. +}
  20051. +
  20052. +static void au_ren_refresh(struct au_ren_args *a)
  20053. +{
  20054. + aufs_bindex_t bbot, bindex;
  20055. + struct dentry *d, *h_d;
  20056. + struct inode *i, *h_i;
  20057. + struct super_block *sb;
  20058. +
  20059. + d = a->dst_dentry;
  20060. + d_drop(d);
  20061. + if (a->h_dst)
  20062. + /* already dget-ed by au_ren_or_cpup() */
  20063. + au_set_h_dptr(d, a->btgt, a->h_dst);
  20064. +
  20065. + i = a->dst_inode;
  20066. + if (i) {
  20067. + if (!au_ftest_ren(a->flags, ISDIR))
  20068. + vfsub_drop_nlink(i);
  20069. + else {
  20070. + vfsub_dead_dir(i);
  20071. + au_cpup_attr_timesizes(i);
  20072. + }
  20073. + au_update_dbrange(d, /*do_put_zero*/1);
  20074. + } else {
  20075. + bbot = a->btgt;
  20076. + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
  20077. + au_set_h_dptr(d, bindex, NULL);
  20078. + bbot = au_dbbot(d);
  20079. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
  20080. + au_set_h_dptr(d, bindex, NULL);
  20081. + au_update_dbrange(d, /*do_put_zero*/0);
  20082. + }
  20083. +
  20084. + d = a->src_dentry;
  20085. + au_set_dbwh(d, -1);
  20086. + bbot = au_dbbot(d);
  20087. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  20088. + h_d = au_h_dptr(d, bindex);
  20089. + if (h_d)
  20090. + au_set_h_dptr(d, bindex, NULL);
  20091. + }
  20092. + au_set_dbbot(d, a->btgt);
  20093. +
  20094. + sb = d->d_sb;
  20095. + i = a->src_inode;
  20096. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  20097. + return; /* success */
  20098. +
  20099. + bbot = au_ibbot(i);
  20100. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  20101. + h_i = au_h_iptr(i, bindex);
  20102. + if (h_i) {
  20103. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  20104. + /* ignore this error */
  20105. + au_set_h_iptr(i, bindex, NULL, 0);
  20106. + }
  20107. + }
  20108. + au_set_ibbot(i, a->btgt);
  20109. +}
  20110. +
  20111. +/* ---------------------------------------------------------------------- */
  20112. +
  20113. +/* mainly for link(2) and rename(2) */
  20114. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  20115. +{
  20116. + aufs_bindex_t bdiropq, bwh;
  20117. + struct dentry *parent;
  20118. + struct au_branch *br;
  20119. +
  20120. + parent = dentry->d_parent;
  20121. + IMustLock(d_inode(parent)); /* dir is locked */
  20122. +
  20123. + bdiropq = au_dbdiropq(parent);
  20124. + bwh = au_dbwh(dentry);
  20125. + br = au_sbr(dentry->d_sb, btgt);
  20126. + if (au_br_rdonly(br)
  20127. + || (0 <= bdiropq && bdiropq < btgt)
  20128. + || (0 <= bwh && bwh < btgt))
  20129. + btgt = -1;
  20130. +
  20131. + AuDbg("btgt %d\n", btgt);
  20132. + return btgt;
  20133. +}
  20134. +
  20135. +/* sets src_btop, dst_btop and btgt */
  20136. +static int au_ren_wbr(struct au_ren_args *a)
  20137. +{
  20138. + int err;
  20139. + struct au_wr_dir_args wr_dir_args = {
  20140. + /* .force_btgt = -1, */
  20141. + .flags = AuWrDir_ADD_ENTRY
  20142. + };
  20143. +
  20144. + a->src_btop = au_dbtop(a->src_dentry);
  20145. + a->dst_btop = au_dbtop(a->dst_dentry);
  20146. + if (au_ftest_ren(a->flags, ISDIR))
  20147. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  20148. + wr_dir_args.force_btgt = a->src_btop;
  20149. + if (a->dst_inode && a->dst_btop < a->src_btop)
  20150. + wr_dir_args.force_btgt = a->dst_btop;
  20151. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  20152. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  20153. + a->btgt = err;
  20154. +
  20155. + return err;
  20156. +}
  20157. +
  20158. +static void au_ren_dt(struct au_ren_args *a)
  20159. +{
  20160. + a->h_path.dentry = a->src_h_parent;
  20161. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  20162. + if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
  20163. + a->h_path.dentry = a->dst_h_parent;
  20164. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  20165. + }
  20166. +
  20167. + au_fclr_ren(a->flags, DT_DSTDIR);
  20168. + if (!au_ftest_ren(a->flags, ISDIR))
  20169. + return;
  20170. +
  20171. + a->h_path.dentry = a->src_h_dentry;
  20172. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  20173. + if (d_is_positive(a->dst_h_dentry)) {
  20174. + au_fset_ren(a->flags, DT_DSTDIR);
  20175. + a->h_path.dentry = a->dst_h_dentry;
  20176. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  20177. + }
  20178. +}
  20179. +
  20180. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  20181. +{
  20182. + struct dentry *h_d;
  20183. + struct mutex *h_mtx;
  20184. +
  20185. + au_dtime_revert(a->src_dt + AuPARENT);
  20186. + if (!au_ftest_ren(a->flags, ISSAMEDIR))
  20187. + au_dtime_revert(a->dst_dt + AuPARENT);
  20188. +
  20189. + if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
  20190. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  20191. + h_mtx = &d_inode(h_d)->i_mutex;
  20192. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  20193. + au_dtime_revert(a->src_dt + AuCHILD);
  20194. + mutex_unlock(h_mtx);
  20195. +
  20196. + if (au_ftest_ren(a->flags, DT_DSTDIR)) {
  20197. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  20198. + h_mtx = &d_inode(h_d)->i_mutex;
  20199. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  20200. + au_dtime_revert(a->dst_dt + AuCHILD);
  20201. + mutex_unlock(h_mtx);
  20202. + }
  20203. + }
  20204. +}
  20205. +
  20206. +/* ---------------------------------------------------------------------- */
  20207. +
  20208. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  20209. + struct inode *_dst_dir, struct dentry *_dst_dentry)
  20210. +{
  20211. + int err, flags;
  20212. + /* reduce stack space */
  20213. + struct au_ren_args *a;
  20214. +
  20215. + AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
  20216. + IMustLock(_src_dir);
  20217. + IMustLock(_dst_dir);
  20218. +
  20219. + err = -ENOMEM;
  20220. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  20221. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20222. + if (unlikely(!a))
  20223. + goto out;
  20224. +
  20225. + a->src_dir = _src_dir;
  20226. + a->src_dentry = _src_dentry;
  20227. + a->src_inode = NULL;
  20228. + if (d_really_is_positive(a->src_dentry))
  20229. + a->src_inode = d_inode(a->src_dentry);
  20230. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  20231. + a->dst_dir = _dst_dir;
  20232. + a->dst_dentry = _dst_dentry;
  20233. + a->dst_inode = NULL;
  20234. + if (d_really_is_positive(a->dst_dentry))
  20235. + a->dst_inode = d_inode(a->dst_dentry);
  20236. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  20237. + if (a->dst_inode) {
  20238. + IMustLock(a->dst_inode);
  20239. + au_igrab(a->dst_inode);
  20240. + }
  20241. +
  20242. + err = -ENOTDIR;
  20243. + flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  20244. + if (d_is_dir(a->src_dentry)) {
  20245. + au_fset_ren(a->flags, ISDIR);
  20246. + if (unlikely(d_really_is_positive(a->dst_dentry)
  20247. + && !d_is_dir(a->dst_dentry)))
  20248. + goto out_free;
  20249. + flags |= AuLock_DIRS;
  20250. + }
  20251. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, flags);
  20252. + if (unlikely(err))
  20253. + goto out_free;
  20254. +
  20255. + err = au_d_hashed_positive(a->src_dentry);
  20256. + if (unlikely(err))
  20257. + goto out_unlock;
  20258. + err = -ENOENT;
  20259. + if (a->dst_inode) {
  20260. + /*
  20261. + * If it is a dir, VFS unhash dst_dentry before this
  20262. + * function. It means we cannot rely upon d_unhashed().
  20263. + */
  20264. + if (unlikely(!a->dst_inode->i_nlink))
  20265. + goto out_unlock;
  20266. + if (!S_ISDIR(a->dst_inode->i_mode)) {
  20267. + err = au_d_hashed_positive(a->dst_dentry);
  20268. + if (unlikely(err))
  20269. + goto out_unlock;
  20270. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  20271. + goto out_unlock;
  20272. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  20273. + goto out_unlock;
  20274. +
  20275. + /*
  20276. + * is it possible?
  20277. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  20278. + * there may exist a problem somewhere else.
  20279. + */
  20280. + err = -EINVAL;
  20281. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  20282. + goto out_unlock;
  20283. +
  20284. + au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
  20285. + di_write_lock_parent(a->dst_parent);
  20286. +
  20287. + /* which branch we process */
  20288. + err = au_ren_wbr(a);
  20289. + if (unlikely(err < 0))
  20290. + goto out_parent;
  20291. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  20292. + a->h_path.mnt = au_br_mnt(a->br);
  20293. +
  20294. + /* are they available to be renamed */
  20295. + err = au_ren_may_dir(a);
  20296. + if (unlikely(err))
  20297. + goto out_children;
  20298. +
  20299. + /* prepare the writable parent dir on the same branch */
  20300. + if (a->dst_btop == a->btgt) {
  20301. + au_fset_ren(a->flags, WHDST);
  20302. + } else {
  20303. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  20304. + if (unlikely(err))
  20305. + goto out_children;
  20306. + }
  20307. +
  20308. + if (a->src_dir != a->dst_dir) {
  20309. + /*
  20310. + * this temporary unlock is safe,
  20311. + * because both dir->i_mutex are locked.
  20312. + */
  20313. + di_write_unlock(a->dst_parent);
  20314. + di_write_lock_parent(a->src_parent);
  20315. + err = au_wr_dir_need_wh(a->src_dentry,
  20316. + au_ftest_ren(a->flags, ISDIR),
  20317. + &a->btgt);
  20318. + di_write_unlock(a->src_parent);
  20319. + di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
  20320. + au_fclr_ren(a->flags, ISSAMEDIR);
  20321. + } else
  20322. + err = au_wr_dir_need_wh(a->src_dentry,
  20323. + au_ftest_ren(a->flags, ISDIR),
  20324. + &a->btgt);
  20325. + if (unlikely(err < 0))
  20326. + goto out_children;
  20327. + if (err)
  20328. + au_fset_ren(a->flags, WHSRC);
  20329. +
  20330. + /* cpup src */
  20331. + if (a->src_btop != a->btgt) {
  20332. + struct au_pin pin;
  20333. +
  20334. + err = au_pin(&pin, a->src_dentry, a->btgt,
  20335. + au_opt_udba(a->src_dentry->d_sb),
  20336. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20337. + if (!err) {
  20338. + struct au_cp_generic cpg = {
  20339. + .dentry = a->src_dentry,
  20340. + .bdst = a->btgt,
  20341. + .bsrc = a->src_btop,
  20342. + .len = -1,
  20343. + .pin = &pin,
  20344. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  20345. + };
  20346. + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
  20347. + err = au_sio_cpup_simple(&cpg);
  20348. + au_unpin(&pin);
  20349. + }
  20350. + if (unlikely(err))
  20351. + goto out_children;
  20352. + a->src_btop = a->btgt;
  20353. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  20354. + au_fset_ren(a->flags, WHSRC);
  20355. + }
  20356. +
  20357. + /* lock them all */
  20358. + err = au_ren_lock(a);
  20359. + if (unlikely(err))
  20360. + /* leave the copied-up one */
  20361. + goto out_children;
  20362. +
  20363. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  20364. + err = au_may_ren(a);
  20365. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  20366. + err = -ENAMETOOLONG;
  20367. + if (unlikely(err))
  20368. + goto out_hdir;
  20369. +
  20370. + /* store timestamps to be revertible */
  20371. + au_ren_dt(a);
  20372. +
  20373. + /* here we go */
  20374. + err = do_rename(a);
  20375. + if (unlikely(err))
  20376. + goto out_dt;
  20377. +
  20378. + /* update dir attributes */
  20379. + au_ren_refresh_dir(a);
  20380. +
  20381. + /* dput/iput all lower dentries */
  20382. + au_ren_refresh(a);
  20383. +
  20384. + goto out_hdir; /* success */
  20385. +
  20386. +out_dt:
  20387. + au_ren_rev_dt(err, a);
  20388. +out_hdir:
  20389. + au_ren_unlock(a);
  20390. +out_children:
  20391. + au_nhash_wh_free(&a->whlist);
  20392. + if (err && a->dst_inode && a->dst_btop != a->btgt) {
  20393. + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
  20394. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  20395. + au_set_dbtop(a->dst_dentry, a->dst_btop);
  20396. + }
  20397. +out_parent:
  20398. + if (!err)
  20399. + d_move(a->src_dentry, a->dst_dentry);
  20400. + else {
  20401. + au_update_dbtop(a->dst_dentry);
  20402. + if (!a->dst_inode)
  20403. + d_drop(a->dst_dentry);
  20404. + }
  20405. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  20406. + di_write_unlock(a->dst_parent);
  20407. + else
  20408. + di_write_unlock2(a->src_parent, a->dst_parent);
  20409. +out_unlock:
  20410. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  20411. +out_free:
  20412. + iput(a->dst_inode);
  20413. + if (a->thargs)
  20414. + au_whtmp_rmdir_free(a->thargs);
  20415. + au_delayed_kfree(a);
  20416. +out:
  20417. + AuTraceErr(err);
  20418. + return err;
  20419. +}
  20420. --- /dev/null
  20421. +++ linux-4.4/fs/aufs/Kconfig 2016-10-23 11:20:57.622159952 +0300
  20422. @@ -0,0 +1,185 @@
  20423. +config AUFS_FS
  20424. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  20425. + help
  20426. + Aufs is a stackable unification filesystem such as Unionfs,
  20427. + which unifies several directories and provides a merged single
  20428. + directory.
  20429. + In the early days, aufs was entirely re-designed and
  20430. + re-implemented Unionfs Version 1.x series. Introducing many
  20431. + original ideas, approaches and improvements, it becomes totally
  20432. + different from Unionfs while keeping the basic features.
  20433. +
  20434. +if AUFS_FS
  20435. +choice
  20436. + prompt "Maximum number of branches"
  20437. + default AUFS_BRANCH_MAX_127
  20438. + help
  20439. + Specifies the maximum number of branches (or member directories)
  20440. + in a single aufs. The larger value consumes more system
  20441. + resources and has a minor impact to performance.
  20442. +config AUFS_BRANCH_MAX_127
  20443. + bool "127"
  20444. + help
  20445. + Specifies the maximum number of branches (or member directories)
  20446. + in a single aufs. The larger value consumes more system
  20447. + resources and has a minor impact to performance.
  20448. +config AUFS_BRANCH_MAX_511
  20449. + bool "511"
  20450. + help
  20451. + Specifies the maximum number of branches (or member directories)
  20452. + in a single aufs. The larger value consumes more system
  20453. + resources and has a minor impact to performance.
  20454. +config AUFS_BRANCH_MAX_1023
  20455. + bool "1023"
  20456. + help
  20457. + Specifies the maximum number of branches (or member directories)
  20458. + in a single aufs. The larger value consumes more system
  20459. + resources and has a minor impact to performance.
  20460. +config AUFS_BRANCH_MAX_32767
  20461. + bool "32767"
  20462. + help
  20463. + Specifies the maximum number of branches (or member directories)
  20464. + in a single aufs. The larger value consumes more system
  20465. + resources and has a minor impact to performance.
  20466. +endchoice
  20467. +
  20468. +config AUFS_SBILIST
  20469. + bool
  20470. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  20471. + default y
  20472. + help
  20473. + Automatic configuration for internal use.
  20474. + When aufs supports Magic SysRq or /proc, enabled automatically.
  20475. +
  20476. +config AUFS_HNOTIFY
  20477. + bool "Detect direct branch access (bypassing aufs)"
  20478. + help
  20479. + If you want to modify files on branches directly, eg. bypassing aufs,
  20480. + and want aufs to detect the changes of them fully, then enable this
  20481. + option and use 'udba=notify' mount option.
  20482. + Currently there is only one available configuration, "fsnotify".
  20483. + It will have a negative impact to the performance.
  20484. + See detail in aufs.5.
  20485. +
  20486. +choice
  20487. + prompt "method" if AUFS_HNOTIFY
  20488. + default AUFS_HFSNOTIFY
  20489. +config AUFS_HFSNOTIFY
  20490. + bool "fsnotify"
  20491. + select FSNOTIFY
  20492. +endchoice
  20493. +
  20494. +config AUFS_EXPORT
  20495. + bool "NFS-exportable aufs"
  20496. + depends on EXPORTFS
  20497. + help
  20498. + If you want to export your mounted aufs via NFS, then enable this
  20499. + option. There are several requirements for this configuration.
  20500. + See detail in aufs.5.
  20501. +
  20502. +config AUFS_INO_T_64
  20503. + bool
  20504. + depends on AUFS_EXPORT
  20505. + depends on 64BIT && !(ALPHA || S390)
  20506. + default y
  20507. + help
  20508. + Automatic configuration for internal use.
  20509. + /* typedef unsigned long/int __kernel_ino_t */
  20510. + /* alpha and s390x are int */
  20511. +
  20512. +config AUFS_XATTR
  20513. + bool "support for XATTR/EA (including Security Labels)"
  20514. + help
  20515. + If your branch fs supports XATTR/EA and you want to make them
  20516. + available in aufs too, then enable this opsion and specify the
  20517. + branch attributes for EA.
  20518. + See detail in aufs.5.
  20519. +
  20520. +config AUFS_FHSM
  20521. + bool "File-based Hierarchical Storage Management"
  20522. + help
  20523. + Hierarchical Storage Management (or HSM) is a well-known feature
  20524. + in the storage world. Aufs provides this feature as file-based.
  20525. + with multiple branches.
  20526. + These multiple branches are prioritized, ie. the topmost one
  20527. + should be the fastest drive and be used heavily.
  20528. +
  20529. +config AUFS_RDU
  20530. + bool "Readdir in userspace"
  20531. + help
  20532. + Aufs has two methods to provide a merged view for a directory,
  20533. + by a user-space library and by kernel-space natively. The latter
  20534. + is always enabled but sometimes large and slow.
  20535. + If you enable this option, install the library in aufs2-util
  20536. + package, and set some environment variables for your readdir(3),
  20537. + then the work will be handled in user-space which generally
  20538. + shows better performance in most cases.
  20539. + See detail in aufs.5.
  20540. +
  20541. +config AUFS_SHWH
  20542. + bool "Show whiteouts"
  20543. + help
  20544. + If you want to make the whiteouts in aufs visible, then enable
  20545. + this option and specify 'shwh' mount option. Although it may
  20546. + sounds like philosophy or something, but in technically it
  20547. + simply shows the name of whiteout with keeping its behaviour.
  20548. +
  20549. +config AUFS_BR_RAMFS
  20550. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  20551. + help
  20552. + If you want to use ramfs as an aufs branch fs, then enable this
  20553. + option. Generally tmpfs is recommended.
  20554. + Aufs prohibited them to be a branch fs by default, because
  20555. + initramfs becomes unusable after switch_root or something
  20556. + generally. If you sets initramfs as an aufs branch and boot your
  20557. + system by switch_root, you will meet a problem easily since the
  20558. + files in initramfs may be inaccessible.
  20559. + Unless you are going to use ramfs as an aufs branch fs without
  20560. + switch_root or something, leave it N.
  20561. +
  20562. +config AUFS_BR_FUSE
  20563. + bool "Fuse fs as an aufs branch"
  20564. + depends on FUSE_FS
  20565. + select AUFS_POLL
  20566. + help
  20567. + If you want to use fuse-based userspace filesystem as an aufs
  20568. + branch fs, then enable this option.
  20569. + It implements the internal poll(2) operation which is
  20570. + implemented by fuse only (curretnly).
  20571. +
  20572. +config AUFS_POLL
  20573. + bool
  20574. + help
  20575. + Automatic configuration for internal use.
  20576. +
  20577. +config AUFS_BR_HFSPLUS
  20578. + bool "Hfsplus as an aufs branch"
  20579. + depends on HFSPLUS_FS
  20580. + default y
  20581. + help
  20582. + If you want to use hfsplus fs as an aufs branch fs, then enable
  20583. + this option. This option introduces a small overhead at
  20584. + copying-up a file on hfsplus.
  20585. +
  20586. +config AUFS_BDEV_LOOP
  20587. + bool
  20588. + depends on BLK_DEV_LOOP
  20589. + default y
  20590. + help
  20591. + Automatic configuration for internal use.
  20592. + Convert =[ym] into =y.
  20593. +
  20594. +config AUFS_DEBUG
  20595. + bool "Debug aufs"
  20596. + help
  20597. + Enable this to compile aufs internal debug code.
  20598. + It will have a negative impact to the performance.
  20599. +
  20600. +config AUFS_MAGIC_SYSRQ
  20601. + bool
  20602. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  20603. + default y
  20604. + help
  20605. + Automatic configuration for internal use.
  20606. + When aufs supports Magic SysRq, enabled automatically.
  20607. +endif
  20608. --- /dev/null
  20609. +++ linux-4.4/fs/aufs/loop.c 2016-10-23 11:20:57.638826618 +0300
  20610. @@ -0,0 +1,147 @@
  20611. +/*
  20612. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  20613. + *
  20614. + * This program, aufs is free software; you can redistribute it and/or modify
  20615. + * it under the terms of the GNU General Public License as published by
  20616. + * the Free Software Foundation; either version 2 of the License, or
  20617. + * (at your option) any later version.
  20618. + *
  20619. + * This program is distributed in the hope that it will be useful,
  20620. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20621. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20622. + * GNU General Public License for more details.
  20623. + *
  20624. + * You should have received a copy of the GNU General Public License
  20625. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20626. + */
  20627. +
  20628. +/*
  20629. + * support for loopback block device as a branch
  20630. + */
  20631. +
  20632. +#include "aufs.h"
  20633. +
  20634. +/* added into drivers/block/loop.c */
  20635. +static struct file *(*backing_file_func)(struct super_block *sb);
  20636. +
  20637. +/*
  20638. + * test if two lower dentries have overlapping branches.
  20639. + */
  20640. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  20641. +{
  20642. + struct super_block *h_sb;
  20643. + struct file *backing_file;
  20644. +
  20645. + if (unlikely(!backing_file_func)) {
  20646. + /* don't load "loop" module here */
  20647. + backing_file_func = symbol_get(loop_backing_file);
  20648. + if (unlikely(!backing_file_func))
  20649. + /* "loop" module is not loaded */
  20650. + return 0;
  20651. + }
  20652. +
  20653. + h_sb = h_adding->d_sb;
  20654. + backing_file = backing_file_func(h_sb);
  20655. + if (!backing_file)
  20656. + return 0;
  20657. +
  20658. + h_adding = backing_file->f_path.dentry;
  20659. + /*
  20660. + * h_adding can be local NFS.
  20661. + * in this case aufs cannot detect the loop.
  20662. + */
  20663. + if (unlikely(h_adding->d_sb == sb))
  20664. + return 1;
  20665. + return !!au_test_subdir(h_adding, sb->s_root);
  20666. +}
  20667. +
  20668. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  20669. +int au_test_loopback_kthread(void)
  20670. +{
  20671. + int ret;
  20672. + struct task_struct *tsk = current;
  20673. + char c, comm[sizeof(tsk->comm)];
  20674. +
  20675. + ret = 0;
  20676. + if (tsk->flags & PF_KTHREAD) {
  20677. + get_task_comm(comm, tsk);
  20678. + c = comm[4];
  20679. + ret = ('0' <= c && c <= '9'
  20680. + && !strncmp(comm, "loop", 4));
  20681. + }
  20682. +
  20683. + return ret;
  20684. +}
  20685. +
  20686. +/* ---------------------------------------------------------------------- */
  20687. +
  20688. +#define au_warn_loopback_step 16
  20689. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  20690. +static unsigned long *au_warn_loopback_array;
  20691. +
  20692. +void au_warn_loopback(struct super_block *h_sb)
  20693. +{
  20694. + int i, new_nelem;
  20695. + unsigned long *a, magic;
  20696. + static DEFINE_SPINLOCK(spin);
  20697. +
  20698. + magic = h_sb->s_magic;
  20699. + spin_lock(&spin);
  20700. + a = au_warn_loopback_array;
  20701. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  20702. + if (a[i] == magic) {
  20703. + spin_unlock(&spin);
  20704. + return;
  20705. + }
  20706. +
  20707. + /* h_sb is new to us, print it */
  20708. + if (i < au_warn_loopback_nelem) {
  20709. + a[i] = magic;
  20710. + goto pr;
  20711. + }
  20712. +
  20713. + /* expand the array */
  20714. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  20715. + a = au_kzrealloc(au_warn_loopback_array,
  20716. + au_warn_loopback_nelem * sizeof(unsigned long),
  20717. + new_nelem * sizeof(unsigned long), GFP_ATOMIC,
  20718. + /*may_shrink*/0);
  20719. + if (a) {
  20720. + au_warn_loopback_nelem = new_nelem;
  20721. + au_warn_loopback_array = a;
  20722. + a[i] = magic;
  20723. + goto pr;
  20724. + }
  20725. +
  20726. + spin_unlock(&spin);
  20727. + AuWarn1("realloc failed, ignored\n");
  20728. + return;
  20729. +
  20730. +pr:
  20731. + spin_unlock(&spin);
  20732. + pr_warn("you may want to try another patch for loopback file "
  20733. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  20734. +}
  20735. +
  20736. +int au_loopback_init(void)
  20737. +{
  20738. + int err;
  20739. + struct super_block *sb __maybe_unused;
  20740. +
  20741. + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
  20742. +
  20743. + err = 0;
  20744. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  20745. + sizeof(unsigned long), GFP_NOFS);
  20746. + if (unlikely(!au_warn_loopback_array))
  20747. + err = -ENOMEM;
  20748. +
  20749. + return err;
  20750. +}
  20751. +
  20752. +void au_loopback_fin(void)
  20753. +{
  20754. + if (backing_file_func)
  20755. + symbol_put(loop_backing_file);
  20756. + au_delayed_kfree(au_warn_loopback_array);
  20757. +}
  20758. --- /dev/null
  20759. +++ linux-4.4/fs/aufs/loop.h 2016-10-23 11:20:57.638826618 +0300
  20760. @@ -0,0 +1,52 @@
  20761. +/*
  20762. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  20763. + *
  20764. + * This program, aufs is free software; you can redistribute it and/or modify
  20765. + * it under the terms of the GNU General Public License as published by
  20766. + * the Free Software Foundation; either version 2 of the License, or
  20767. + * (at your option) any later version.
  20768. + *
  20769. + * This program is distributed in the hope that it will be useful,
  20770. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20771. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20772. + * GNU General Public License for more details.
  20773. + *
  20774. + * You should have received a copy of the GNU General Public License
  20775. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20776. + */
  20777. +
  20778. +/*
  20779. + * support for loopback mount as a branch
  20780. + */
  20781. +
  20782. +#ifndef __AUFS_LOOP_H__
  20783. +#define __AUFS_LOOP_H__
  20784. +
  20785. +#ifdef __KERNEL__
  20786. +
  20787. +struct dentry;
  20788. +struct super_block;
  20789. +
  20790. +#ifdef CONFIG_AUFS_BDEV_LOOP
  20791. +/* drivers/block/loop.c */
  20792. +struct file *loop_backing_file(struct super_block *sb);
  20793. +
  20794. +/* loop.c */
  20795. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  20796. +int au_test_loopback_kthread(void);
  20797. +void au_warn_loopback(struct super_block *h_sb);
  20798. +
  20799. +int au_loopback_init(void);
  20800. +void au_loopback_fin(void);
  20801. +#else
  20802. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  20803. + struct dentry *h_adding)
  20804. +AuStubInt0(au_test_loopback_kthread, void)
  20805. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  20806. +
  20807. +AuStubInt0(au_loopback_init, void)
  20808. +AuStubVoid(au_loopback_fin, void)
  20809. +#endif /* BLK_DEV_LOOP */
  20810. +
  20811. +#endif /* __KERNEL__ */
  20812. +#endif /* __AUFS_LOOP_H__ */
  20813. --- /dev/null
  20814. +++ linux-4.4/fs/aufs/magic.mk 2016-10-23 11:20:57.638826618 +0300
  20815. @@ -0,0 +1,30 @@
  20816. +
  20817. +# defined in ${srctree}/fs/fuse/inode.c
  20818. +# tristate
  20819. +ifdef CONFIG_FUSE_FS
  20820. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  20821. +endif
  20822. +
  20823. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  20824. +# tristate
  20825. +ifdef CONFIG_XFS_FS
  20826. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  20827. +endif
  20828. +
  20829. +# defined in ${srctree}/fs/configfs/mount.c
  20830. +# tristate
  20831. +ifdef CONFIG_CONFIGFS_FS
  20832. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  20833. +endif
  20834. +
  20835. +# defined in ${srctree}/fs/ubifs/ubifs.h
  20836. +# tristate
  20837. +ifdef CONFIG_UBIFS_FS
  20838. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  20839. +endif
  20840. +
  20841. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  20842. +# tristate
  20843. +ifdef CONFIG_HFSPLUS_FS
  20844. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  20845. +endif
  20846. --- /dev/null
  20847. +++ linux-4.4/fs/aufs/Makefile 2016-10-23 11:20:57.622159952 +0300
  20848. @@ -0,0 +1,44 @@
  20849. +
  20850. +include ${src}/magic.mk
  20851. +ifeq (${CONFIG_AUFS_FS},m)
  20852. +include ${src}/conf.mk
  20853. +endif
  20854. +-include ${src}/priv_def.mk
  20855. +
  20856. +# cf. include/linux/kernel.h
  20857. +# enable pr_debug
  20858. +ccflags-y += -DDEBUG
  20859. +# sparse requires the full pathname
  20860. +ifdef M
  20861. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  20862. +else
  20863. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  20864. +endif
  20865. +
  20866. +obj-$(CONFIG_AUFS_FS) += aufs.o
  20867. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  20868. + wkq.o vfsub.o dcsub.o \
  20869. + cpup.o whout.o wbr_policy.o \
  20870. + dinfo.o dentry.o \
  20871. + dynop.o \
  20872. + finfo.o file.o f_op.o \
  20873. + dir.o vdir.o \
  20874. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  20875. + mvdown.o ioctl.o
  20876. +
  20877. +# all are boolean
  20878. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  20879. +aufs-$(CONFIG_SYSFS) += sysfs.o
  20880. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  20881. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  20882. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  20883. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  20884. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  20885. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  20886. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  20887. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  20888. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  20889. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  20890. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  20891. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  20892. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  20893. --- /dev/null
  20894. +++ linux-4.4/fs/aufs/module.c 2016-10-23 11:20:57.638826618 +0300
  20895. @@ -0,0 +1,333 @@
  20896. +/*
  20897. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  20898. + *
  20899. + * This program, aufs is free software; you can redistribute it and/or modify
  20900. + * it under the terms of the GNU General Public License as published by
  20901. + * the Free Software Foundation; either version 2 of the License, or
  20902. + * (at your option) any later version.
  20903. + *
  20904. + * This program is distributed in the hope that it will be useful,
  20905. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20906. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20907. + * GNU General Public License for more details.
  20908. + *
  20909. + * You should have received a copy of the GNU General Public License
  20910. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20911. + */
  20912. +
  20913. +/*
  20914. + * module global variables and operations
  20915. + */
  20916. +
  20917. +#include <linux/module.h>
  20918. +#include <linux/seq_file.h>
  20919. +#include "aufs.h"
  20920. +
  20921. +/* shrinkable realloc */
  20922. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
  20923. +{
  20924. + size_t sz;
  20925. + int diff;
  20926. +
  20927. + sz = 0;
  20928. + diff = -1;
  20929. + if (p) {
  20930. +#if 0 /* unused */
  20931. + if (!new_sz) {
  20932. + au_delayed_kfree(p);
  20933. + p = NULL;
  20934. + goto out;
  20935. + }
  20936. +#else
  20937. + AuDebugOn(!new_sz);
  20938. +#endif
  20939. + sz = ksize(p);
  20940. + diff = au_kmidx_sub(sz, new_sz);
  20941. + }
  20942. + if (sz && !diff)
  20943. + goto out;
  20944. +
  20945. + if (sz < new_sz)
  20946. + /* expand or SLOB */
  20947. + p = krealloc(p, new_sz, gfp);
  20948. + else if (new_sz < sz && may_shrink) {
  20949. + /* shrink */
  20950. + void *q;
  20951. +
  20952. + q = kmalloc(new_sz, gfp);
  20953. + if (q) {
  20954. + if (p) {
  20955. + memcpy(q, p, new_sz);
  20956. + au_delayed_kfree(p);
  20957. + }
  20958. + p = q;
  20959. + } else
  20960. + p = NULL;
  20961. + }
  20962. +
  20963. +out:
  20964. + return p;
  20965. +}
  20966. +
  20967. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  20968. + int may_shrink)
  20969. +{
  20970. + p = au_krealloc(p, new_sz, gfp, may_shrink);
  20971. + if (p && new_sz > nused)
  20972. + memset(p + nused, 0, new_sz - nused);
  20973. + return p;
  20974. +}
  20975. +
  20976. +/* ---------------------------------------------------------------------- */
  20977. +/*
  20978. + * aufs caches
  20979. + */
  20980. +
  20981. +struct au_dfree au_dfree;
  20982. +
  20983. +/* delayed free */
  20984. +static void au_do_dfree(struct work_struct *work __maybe_unused)
  20985. +{
  20986. + struct llist_head *head;
  20987. + struct llist_node *node, *next;
  20988. +
  20989. +#define AU_CACHE_DFREE_DO_BODY(name, idx, lnode) do { \
  20990. + head = &au_dfree.cache[AuCache_##idx].llist; \
  20991. + node = llist_del_all(head); \
  20992. + for (; node; node = next) { \
  20993. + struct au_##name *p \
  20994. + = llist_entry(node, struct au_##name, \
  20995. + lnode); \
  20996. + next = llist_next(node); \
  20997. + au_cache_free_##name(p); \
  20998. + } \
  20999. + } while (0)
  21000. +
  21001. + AU_CACHE_DFREE_DO_BODY(dinfo, DINFO, di_lnode);
  21002. + AU_CACHE_DFREE_DO_BODY(icntnr, ICNTNR, lnode);
  21003. + AU_CACHE_DFREE_DO_BODY(finfo, FINFO, fi_lnode);
  21004. + AU_CACHE_DFREE_DO_BODY(vdir, VDIR, vd_lnode);
  21005. + AU_CACHE_DFREE_DO_BODY(vdir_dehstr, DEHSTR, lnode);
  21006. +#ifdef CONFIG_AUFS_HNOTIFY
  21007. + AU_CACHE_DFREE_DO_BODY(hnotify, HNOTIFY, hn_lnode);
  21008. +#endif
  21009. +
  21010. +#define AU_DFREE_DO_BODY(llist, func) do { \
  21011. + node = llist_del_all(llist); \
  21012. + for (; node; node = next) { \
  21013. + next = llist_next(node); \
  21014. + func(node); \
  21015. + } \
  21016. + } while (0)
  21017. +
  21018. + AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_KFREE, kfree);
  21019. + AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_FREE_PAGE, au_free_page);
  21020. +
  21021. +#undef AU_CACHE_DFREE_DO_BODY
  21022. +#undef AU_DFREE_DO_BODY
  21023. +}
  21024. +
  21025. +AU_CACHE_DFREE_FUNC(dinfo, DINFO, di_lnode);
  21026. +AU_CACHE_DFREE_FUNC(icntnr, ICNTNR, lnode);
  21027. +AU_CACHE_DFREE_FUNC(finfo, FINFO, fi_lnode);
  21028. +AU_CACHE_DFREE_FUNC(vdir, VDIR, vd_lnode);
  21029. +AU_CACHE_DFREE_FUNC(vdir_dehstr, DEHSTR, lnode);
  21030. +
  21031. +static void au_cache_fin(void)
  21032. +{
  21033. + int i;
  21034. + struct au_cache *cp;
  21035. +
  21036. + /*
  21037. + * Make sure all delayed rcu free inodes are flushed before we
  21038. + * destroy cache.
  21039. + */
  21040. + rcu_barrier();
  21041. +
  21042. + /* excluding AuCache_HNOTIFY */
  21043. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  21044. + flush_delayed_work(&au_dfree.dwork);
  21045. + for (i = 0; i < AuCache_HNOTIFY; i++) {
  21046. + cp = au_dfree.cache + i;
  21047. + AuDebugOn(!llist_empty(&cp->llist));
  21048. + kmem_cache_destroy(cp->cache);
  21049. + cp->cache = NULL;
  21050. + }
  21051. +}
  21052. +
  21053. +static int __init au_cache_init(void)
  21054. +{
  21055. + struct au_cache *cp;
  21056. +
  21057. + cp = au_dfree.cache;
  21058. + cp[AuCache_DINFO].cache = AuCacheCtor(au_dinfo, au_di_init_once);
  21059. + if (cp[AuCache_DINFO].cache)
  21060. + /* SLAB_DESTROY_BY_RCU */
  21061. + cp[AuCache_ICNTNR].cache = AuCacheCtor(au_icntnr,
  21062. + au_icntnr_init_once);
  21063. + if (cp[AuCache_ICNTNR].cache)
  21064. + cp[AuCache_FINFO].cache = AuCacheCtor(au_finfo,
  21065. + au_fi_init_once);
  21066. + if (cp[AuCache_FINFO].cache)
  21067. + cp[AuCache_VDIR].cache = AuCache(au_vdir);
  21068. + if (cp[AuCache_VDIR].cache)
  21069. + cp[AuCache_DEHSTR].cache = AuCache(au_vdir_dehstr);
  21070. + if (cp[AuCache_DEHSTR].cache)
  21071. + return 0;
  21072. +
  21073. + au_cache_fin();
  21074. + return -ENOMEM;
  21075. +}
  21076. +
  21077. +/* ---------------------------------------------------------------------- */
  21078. +
  21079. +int au_dir_roflags;
  21080. +
  21081. +#ifdef CONFIG_AUFS_SBILIST
  21082. +/*
  21083. + * iterate_supers_type() doesn't protect us from
  21084. + * remounting (branch management)
  21085. + */
  21086. +struct au_sphlhead au_sbilist;
  21087. +#endif
  21088. +
  21089. +/*
  21090. + * functions for module interface.
  21091. + */
  21092. +MODULE_LICENSE("GPL");
  21093. +/* MODULE_LICENSE("GPL v2"); */
  21094. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  21095. +MODULE_DESCRIPTION(AUFS_NAME
  21096. + " -- Advanced multi layered unification filesystem");
  21097. +MODULE_VERSION(AUFS_VERSION);
  21098. +MODULE_ALIAS_FS(AUFS_NAME);
  21099. +
  21100. +/* this module parameter has no meaning when SYSFS is disabled */
  21101. +int sysaufs_brs = 1;
  21102. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  21103. +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
  21104. +
  21105. +/* this module parameter has no meaning when USER_NS is disabled */
  21106. +bool au_userns;
  21107. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  21108. +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
  21109. +
  21110. +/* ---------------------------------------------------------------------- */
  21111. +
  21112. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  21113. +
  21114. +int au_seq_path(struct seq_file *seq, struct path *path)
  21115. +{
  21116. + int err;
  21117. +
  21118. + err = seq_path(seq, path, au_esc_chars);
  21119. + if (err > 0)
  21120. + err = 0;
  21121. + else if (err < 0)
  21122. + err = -ENOMEM;
  21123. +
  21124. + return err;
  21125. +}
  21126. +
  21127. +/* ---------------------------------------------------------------------- */
  21128. +
  21129. +static int __init aufs_init(void)
  21130. +{
  21131. + int err, i;
  21132. + char *p;
  21133. + struct au_cache *cp;
  21134. +
  21135. + p = au_esc_chars;
  21136. + for (i = 1; i <= ' '; i++)
  21137. + *p++ = i;
  21138. + *p++ = '\\';
  21139. + *p++ = '\x7f';
  21140. + *p = 0;
  21141. +
  21142. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  21143. +
  21144. + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
  21145. + for (i = 0; i < AuIop_Last; i++)
  21146. + aufs_iop_nogetattr[i].getattr = NULL;
  21147. +
  21148. + /* First, initialize au_dfree */
  21149. + for (i = 0; i < AuCache_Last; i++) { /* including hnotify */
  21150. + cp = au_dfree.cache + i;
  21151. + cp->cache = NULL;
  21152. + init_llist_head(&cp->llist);
  21153. + }
  21154. + for (i = 0; i < AU_DFREE_Last; i++)
  21155. + init_llist_head(au_dfree.llist + i);
  21156. + INIT_DELAYED_WORK(&au_dfree.dwork, au_do_dfree);
  21157. +
  21158. + au_sbilist_init();
  21159. + sysaufs_brs_init();
  21160. + au_debug_init();
  21161. + au_dy_init();
  21162. + err = sysaufs_init();
  21163. + if (unlikely(err))
  21164. + goto out;
  21165. + err = au_procfs_init();
  21166. + if (unlikely(err))
  21167. + goto out_sysaufs;
  21168. + err = au_wkq_init();
  21169. + if (unlikely(err))
  21170. + goto out_procfs;
  21171. + err = au_loopback_init();
  21172. + if (unlikely(err))
  21173. + goto out_wkq;
  21174. + err = au_hnotify_init();
  21175. + if (unlikely(err))
  21176. + goto out_loopback;
  21177. + err = au_sysrq_init();
  21178. + if (unlikely(err))
  21179. + goto out_hin;
  21180. + err = au_cache_init();
  21181. + if (unlikely(err))
  21182. + goto out_sysrq;
  21183. +
  21184. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  21185. + err = register_filesystem(&aufs_fs_type);
  21186. + if (unlikely(err))
  21187. + goto out_cache;
  21188. +
  21189. + /* since we define pr_fmt, call printk directly */
  21190. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  21191. + goto out; /* success */
  21192. +
  21193. +out_cache:
  21194. + au_cache_fin();
  21195. +out_sysrq:
  21196. + au_sysrq_fin();
  21197. +out_hin:
  21198. + au_hnotify_fin();
  21199. +out_loopback:
  21200. + au_loopback_fin();
  21201. +out_wkq:
  21202. + au_wkq_fin();
  21203. +out_procfs:
  21204. + au_procfs_fin();
  21205. +out_sysaufs:
  21206. + sysaufs_fin();
  21207. + au_dy_fin();
  21208. + flush_delayed_work(&au_dfree.dwork);
  21209. +out:
  21210. + return err;
  21211. +}
  21212. +
  21213. +static void __exit aufs_exit(void)
  21214. +{
  21215. + unregister_filesystem(&aufs_fs_type);
  21216. + au_cache_fin();
  21217. + au_sysrq_fin();
  21218. + au_hnotify_fin();
  21219. + au_loopback_fin();
  21220. + au_wkq_fin();
  21221. + au_procfs_fin();
  21222. + sysaufs_fin();
  21223. + au_dy_fin();
  21224. + flush_delayed_work(&au_dfree.dwork);
  21225. +}
  21226. +
  21227. +module_init(aufs_init);
  21228. +module_exit(aufs_exit);
  21229. --- /dev/null
  21230. +++ linux-4.4/fs/aufs/module.h 2016-10-23 11:20:57.638826618 +0300
  21231. @@ -0,0 +1,156 @@
  21232. +/*
  21233. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  21234. + *
  21235. + * This program, aufs is free software; you can redistribute it and/or modify
  21236. + * it under the terms of the GNU General Public License as published by
  21237. + * the Free Software Foundation; either version 2 of the License, or
  21238. + * (at your option) any later version.
  21239. + *
  21240. + * This program is distributed in the hope that it will be useful,
  21241. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21242. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21243. + * GNU General Public License for more details.
  21244. + *
  21245. + * You should have received a copy of the GNU General Public License
  21246. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21247. + */
  21248. +
  21249. +/*
  21250. + * module initialization and module-global
  21251. + */
  21252. +
  21253. +#ifndef __AUFS_MODULE_H__
  21254. +#define __AUFS_MODULE_H__
  21255. +
  21256. +#ifdef __KERNEL__
  21257. +
  21258. +#include <linux/slab.h>
  21259. +#include "debug.h"
  21260. +
  21261. +struct path;
  21262. +struct seq_file;
  21263. +
  21264. +/* module parameters */
  21265. +extern int sysaufs_brs;
  21266. +extern bool au_userns;
  21267. +
  21268. +/* ---------------------------------------------------------------------- */
  21269. +
  21270. +extern int au_dir_roflags;
  21271. +
  21272. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
  21273. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  21274. + int may_shrink);
  21275. +
  21276. +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
  21277. +{
  21278. +#ifndef CONFIG_SLOB
  21279. + return kmalloc_index(sz) - kmalloc_index(new_sz);
  21280. +#else
  21281. + return -1; /* SLOB is untested */
  21282. +#endif
  21283. +}
  21284. +
  21285. +int au_seq_path(struct seq_file *seq, struct path *path);
  21286. +
  21287. +#ifdef CONFIG_PROC_FS
  21288. +/* procfs.c */
  21289. +int __init au_procfs_init(void);
  21290. +void au_procfs_fin(void);
  21291. +#else
  21292. +AuStubInt0(au_procfs_init, void);
  21293. +AuStubVoid(au_procfs_fin, void);
  21294. +#endif
  21295. +
  21296. +/* ---------------------------------------------------------------------- */
  21297. +
  21298. +/* kmem cache and delayed free */
  21299. +enum {
  21300. + AuCache_DINFO,
  21301. + AuCache_ICNTNR,
  21302. + AuCache_FINFO,
  21303. + AuCache_VDIR,
  21304. + AuCache_DEHSTR,
  21305. + AuCache_HNOTIFY, /* must be last */
  21306. + AuCache_Last
  21307. +};
  21308. +
  21309. +enum {
  21310. + AU_DFREE_KFREE,
  21311. + AU_DFREE_FREE_PAGE,
  21312. + AU_DFREE_Last
  21313. +};
  21314. +
  21315. +struct au_cache {
  21316. + struct kmem_cache *cache;
  21317. + struct llist_head llist; /* delayed free */
  21318. +};
  21319. +
  21320. +/*
  21321. + * in order to reduce the cost of the internal timer, consolidate all the
  21322. + * delayed free works into a single delayed_work.
  21323. + */
  21324. +struct au_dfree {
  21325. + struct au_cache cache[AuCache_Last];
  21326. + struct llist_head llist[AU_DFREE_Last];
  21327. + struct delayed_work dwork;
  21328. +};
  21329. +
  21330. +extern struct au_dfree au_dfree;
  21331. +
  21332. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  21333. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  21334. +#define AuCacheCtor(type, ctor) \
  21335. + kmem_cache_create(#type, sizeof(struct type), \
  21336. + __alignof__(struct type), AuCacheFlags, ctor)
  21337. +
  21338. +#define AU_DFREE_DELAY msecs_to_jiffies(10)
  21339. +#define AU_DFREE_BODY(lnode, llist) do { \
  21340. + if (llist_add(lnode, llist)) \
  21341. + schedule_delayed_work(&au_dfree.dwork, \
  21342. + AU_DFREE_DELAY); \
  21343. + } while (0)
  21344. +#define AU_CACHE_DFREE_FUNC(name, idx, lnode) \
  21345. + void au_cache_dfree_##name(struct au_##name *p) \
  21346. + { \
  21347. + struct au_cache *cp = au_dfree.cache + AuCache_##idx; \
  21348. + AU_DFREE_BODY(&p->lnode, &cp->llist); \
  21349. + }
  21350. +
  21351. +#define AuCacheFuncs(name, index) \
  21352. +static inline struct au_##name *au_cache_alloc_##name(void) \
  21353. +{ return kmem_cache_alloc(au_dfree.cache[AuCache_##index].cache, GFP_NOFS); } \
  21354. +static inline void au_cache_free_##name(struct au_##name *p) \
  21355. +{ kmem_cache_free(au_dfree.cache[AuCache_##index].cache, p); } \
  21356. +void au_cache_dfree_##name(struct au_##name *p)
  21357. +
  21358. +AuCacheFuncs(dinfo, DINFO);
  21359. +AuCacheFuncs(icntnr, ICNTNR);
  21360. +AuCacheFuncs(finfo, FINFO);
  21361. +AuCacheFuncs(vdir, VDIR);
  21362. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  21363. +#ifdef CONFIG_AUFS_HNOTIFY
  21364. +AuCacheFuncs(hnotify, HNOTIFY);
  21365. +#endif
  21366. +
  21367. +static inline void au_delayed_kfree(const void *p)
  21368. +{
  21369. + AuDebugOn(!p);
  21370. + AuDebugOn(ksize(p) < sizeof(struct llist_node));
  21371. +
  21372. + AU_DFREE_BODY((void *)p, au_dfree.llist + AU_DFREE_KFREE);
  21373. +}
  21374. +
  21375. +/* cast only */
  21376. +static inline void au_free_page(void *p)
  21377. +{
  21378. + free_page((unsigned long)p);
  21379. +}
  21380. +
  21381. +static inline void au_delayed_free_page(unsigned long addr)
  21382. +{
  21383. + AU_DFREE_BODY((void *)addr, au_dfree.llist + AU_DFREE_FREE_PAGE);
  21384. +}
  21385. +
  21386. +#endif /* __KERNEL__ */
  21387. +#endif /* __AUFS_MODULE_H__ */
  21388. --- /dev/null
  21389. +++ linux-4.4/fs/aufs/mvdown.c 2016-10-23 11:20:57.638826618 +0300
  21390. @@ -0,0 +1,704 @@
  21391. +/*
  21392. + * Copyright (C) 2011-2016 Junjiro R. Okajima
  21393. + *
  21394. + * This program, aufs is free software; you can redistribute it and/or modify
  21395. + * it under the terms of the GNU General Public License as published by
  21396. + * the Free Software Foundation; either version 2 of the License, or
  21397. + * (at your option) any later version.
  21398. + *
  21399. + * This program is distributed in the hope that it will be useful,
  21400. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21401. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21402. + * GNU General Public License for more details.
  21403. + *
  21404. + * You should have received a copy of the GNU General Public License
  21405. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21406. + */
  21407. +
  21408. +/*
  21409. + * move-down, opposite of copy-up
  21410. + */
  21411. +
  21412. +#include "aufs.h"
  21413. +
  21414. +struct au_mvd_args {
  21415. + struct {
  21416. + struct super_block *h_sb;
  21417. + struct dentry *h_parent;
  21418. + struct au_hinode *hdir;
  21419. + struct inode *h_dir, *h_inode;
  21420. + struct au_pin pin;
  21421. + } info[AUFS_MVDOWN_NARRAY];
  21422. +
  21423. + struct aufs_mvdown mvdown;
  21424. + struct dentry *dentry, *parent;
  21425. + struct inode *inode, *dir;
  21426. + struct super_block *sb;
  21427. + aufs_bindex_t bopq, bwh, bfound;
  21428. + unsigned char rename_lock;
  21429. +};
  21430. +
  21431. +#define mvd_errno mvdown.au_errno
  21432. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  21433. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  21434. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  21435. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  21436. +
  21437. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  21438. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  21439. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  21440. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  21441. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  21442. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  21443. +
  21444. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  21445. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  21446. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  21447. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  21448. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  21449. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  21450. +
  21451. +#define AU_MVD_PR(flag, ...) do { \
  21452. + if (flag) \
  21453. + pr_err(__VA_ARGS__); \
  21454. + } while (0)
  21455. +
  21456. +static int find_lower_writable(struct au_mvd_args *a)
  21457. +{
  21458. + struct super_block *sb;
  21459. + aufs_bindex_t bindex, bbot;
  21460. + struct au_branch *br;
  21461. +
  21462. + sb = a->sb;
  21463. + bindex = a->mvd_bsrc;
  21464. + bbot = au_sbbot(sb);
  21465. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  21466. + for (bindex++; bindex <= bbot; bindex++) {
  21467. + br = au_sbr(sb, bindex);
  21468. + if (au_br_fhsm(br->br_perm)
  21469. + && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
  21470. + return bindex;
  21471. + }
  21472. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  21473. + for (bindex++; bindex <= bbot; bindex++) {
  21474. + br = au_sbr(sb, bindex);
  21475. + if (!au_br_rdonly(br))
  21476. + return bindex;
  21477. + }
  21478. + else
  21479. + for (bindex++; bindex <= bbot; bindex++) {
  21480. + br = au_sbr(sb, bindex);
  21481. + if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
  21482. + if (au_br_rdonly(br))
  21483. + a->mvdown.flags
  21484. + |= AUFS_MVDOWN_ROLOWER_R;
  21485. + return bindex;
  21486. + }
  21487. + }
  21488. +
  21489. + return -1;
  21490. +}
  21491. +
  21492. +/* make the parent dir on bdst */
  21493. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  21494. +{
  21495. + int err;
  21496. +
  21497. + err = 0;
  21498. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  21499. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  21500. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  21501. + a->mvd_h_dst_parent = NULL;
  21502. + if (au_dbbot(a->parent) >= a->mvd_bdst)
  21503. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  21504. + if (!a->mvd_h_dst_parent) {
  21505. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  21506. + if (unlikely(err)) {
  21507. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  21508. + goto out;
  21509. + }
  21510. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  21511. + }
  21512. +
  21513. +out:
  21514. + AuTraceErr(err);
  21515. + return err;
  21516. +}
  21517. +
  21518. +/* lock them all */
  21519. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  21520. +{
  21521. + int err;
  21522. + struct dentry *h_trap;
  21523. +
  21524. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  21525. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  21526. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  21527. + au_opt_udba(a->sb),
  21528. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21529. + AuTraceErr(err);
  21530. + if (unlikely(err)) {
  21531. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  21532. + goto out;
  21533. + }
  21534. +
  21535. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  21536. + a->rename_lock = 0;
  21537. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  21538. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  21539. + au_opt_udba(a->sb),
  21540. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21541. + err = au_do_pin(&a->mvd_pin_src);
  21542. + AuTraceErr(err);
  21543. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  21544. + if (unlikely(err)) {
  21545. + AU_MVD_PR(dmsg, "pin_src failed\n");
  21546. + goto out_dst;
  21547. + }
  21548. + goto out; /* success */
  21549. + }
  21550. +
  21551. + a->rename_lock = 1;
  21552. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  21553. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  21554. + au_opt_udba(a->sb),
  21555. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21556. + AuTraceErr(err);
  21557. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  21558. + if (unlikely(err)) {
  21559. + AU_MVD_PR(dmsg, "pin_src failed\n");
  21560. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21561. + goto out_dst;
  21562. + }
  21563. + au_pin_hdir_unlock(&a->mvd_pin_src);
  21564. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21565. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21566. + if (h_trap) {
  21567. + err = (h_trap != a->mvd_h_src_parent);
  21568. + if (err)
  21569. + err = (h_trap != a->mvd_h_dst_parent);
  21570. + }
  21571. + BUG_ON(err); /* it should never happen */
  21572. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  21573. + err = -EBUSY;
  21574. + AuTraceErr(err);
  21575. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21576. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21577. + au_pin_hdir_lock(&a->mvd_pin_src);
  21578. + au_unpin(&a->mvd_pin_src);
  21579. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21580. + goto out_dst;
  21581. + }
  21582. + goto out; /* success */
  21583. +
  21584. +out_dst:
  21585. + au_unpin(&a->mvd_pin_dst);
  21586. +out:
  21587. + AuTraceErr(err);
  21588. + return err;
  21589. +}
  21590. +
  21591. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  21592. +{
  21593. + if (!a->rename_lock)
  21594. + au_unpin(&a->mvd_pin_src);
  21595. + else {
  21596. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21597. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21598. + au_pin_hdir_lock(&a->mvd_pin_src);
  21599. + au_unpin(&a->mvd_pin_src);
  21600. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21601. + }
  21602. + au_unpin(&a->mvd_pin_dst);
  21603. +}
  21604. +
  21605. +/* copy-down the file */
  21606. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  21607. +{
  21608. + int err;
  21609. + struct au_cp_generic cpg = {
  21610. + .dentry = a->dentry,
  21611. + .bdst = a->mvd_bdst,
  21612. + .bsrc = a->mvd_bsrc,
  21613. + .len = -1,
  21614. + .pin = &a->mvd_pin_dst,
  21615. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  21616. + };
  21617. +
  21618. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  21619. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  21620. + au_fset_cpup(cpg.flags, OVERWRITE);
  21621. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  21622. + au_fset_cpup(cpg.flags, RWDST);
  21623. + err = au_sio_cpdown_simple(&cpg);
  21624. + if (unlikely(err))
  21625. + AU_MVD_PR(dmsg, "cpdown failed\n");
  21626. +
  21627. + AuTraceErr(err);
  21628. + return err;
  21629. +}
  21630. +
  21631. +/*
  21632. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  21633. + * were sleeping
  21634. + */
  21635. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  21636. +{
  21637. + int err;
  21638. + struct path h_path;
  21639. + struct au_branch *br;
  21640. + struct inode *delegated;
  21641. +
  21642. + br = au_sbr(a->sb, a->mvd_bdst);
  21643. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  21644. + err = PTR_ERR(h_path.dentry);
  21645. + if (IS_ERR(h_path.dentry)) {
  21646. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  21647. + goto out;
  21648. + }
  21649. +
  21650. + err = 0;
  21651. + if (d_is_positive(h_path.dentry)) {
  21652. + h_path.mnt = au_br_mnt(br);
  21653. + delegated = NULL;
  21654. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  21655. + &delegated, /*force*/0);
  21656. + if (unlikely(err == -EWOULDBLOCK)) {
  21657. + pr_warn("cannot retry for NFSv4 delegation"
  21658. + " for an internal unlink\n");
  21659. + iput(delegated);
  21660. + }
  21661. + if (unlikely(err))
  21662. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  21663. + }
  21664. + dput(h_path.dentry);
  21665. +
  21666. +out:
  21667. + AuTraceErr(err);
  21668. + return err;
  21669. +}
  21670. +
  21671. +/*
  21672. + * unlink the topmost h_dentry
  21673. + */
  21674. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  21675. +{
  21676. + int err;
  21677. + struct path h_path;
  21678. + struct inode *delegated;
  21679. +
  21680. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  21681. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  21682. + delegated = NULL;
  21683. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  21684. + if (unlikely(err == -EWOULDBLOCK)) {
  21685. + pr_warn("cannot retry for NFSv4 delegation"
  21686. + " for an internal unlink\n");
  21687. + iput(delegated);
  21688. + }
  21689. + if (unlikely(err))
  21690. + AU_MVD_PR(dmsg, "unlink failed\n");
  21691. +
  21692. + AuTraceErr(err);
  21693. + return err;
  21694. +}
  21695. +
  21696. +/* Since mvdown succeeded, we ignore an error of this function */
  21697. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  21698. +{
  21699. + int err;
  21700. + struct au_branch *br;
  21701. +
  21702. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  21703. + br = au_sbr(a->sb, a->mvd_bsrc);
  21704. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  21705. + if (!err) {
  21706. + br = au_sbr(a->sb, a->mvd_bdst);
  21707. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  21708. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  21709. + }
  21710. + if (!err)
  21711. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  21712. + else
  21713. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  21714. +}
  21715. +
  21716. +/*
  21717. + * copy-down the file and unlink the bsrc file.
  21718. + * - unlink the bdst whout if exist
  21719. + * - copy-down the file (with whtmp name and rename)
  21720. + * - unlink the bsrc file
  21721. + */
  21722. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  21723. +{
  21724. + int err;
  21725. +
  21726. + err = au_do_mkdir(dmsg, a);
  21727. + if (!err)
  21728. + err = au_do_lock(dmsg, a);
  21729. + if (unlikely(err))
  21730. + goto out;
  21731. +
  21732. + /*
  21733. + * do not revert the activities we made on bdst since they should be
  21734. + * harmless in aufs.
  21735. + */
  21736. +
  21737. + err = au_do_cpdown(dmsg, a);
  21738. + if (!err)
  21739. + err = au_do_unlink_wh(dmsg, a);
  21740. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  21741. + err = au_do_unlink(dmsg, a);
  21742. + if (unlikely(err))
  21743. + goto out_unlock;
  21744. +
  21745. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  21746. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  21747. + if (find_lower_writable(a) < 0)
  21748. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  21749. +
  21750. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  21751. + au_do_stfs(dmsg, a);
  21752. +
  21753. + /* maintain internal array */
  21754. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  21755. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  21756. + au_set_dbtop(a->dentry, a->mvd_bdst);
  21757. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  21758. + au_set_ibtop(a->inode, a->mvd_bdst);
  21759. + } else {
  21760. + /* hide the lower */
  21761. + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
  21762. + au_set_dbbot(a->dentry, a->mvd_bsrc);
  21763. + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
  21764. + au_set_ibbot(a->inode, a->mvd_bsrc);
  21765. + }
  21766. + if (au_dbbot(a->dentry) < a->mvd_bdst)
  21767. + au_set_dbbot(a->dentry, a->mvd_bdst);
  21768. + if (au_ibbot(a->inode) < a->mvd_bdst)
  21769. + au_set_ibbot(a->inode, a->mvd_bdst);
  21770. +
  21771. +out_unlock:
  21772. + au_do_unlock(dmsg, a);
  21773. +out:
  21774. + AuTraceErr(err);
  21775. + return err;
  21776. +}
  21777. +
  21778. +/* ---------------------------------------------------------------------- */
  21779. +
  21780. +/* make sure the file is idle */
  21781. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  21782. +{
  21783. + int err, plinked;
  21784. +
  21785. + err = 0;
  21786. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  21787. + if (au_dbtop(a->dentry) == a->mvd_bsrc
  21788. + && au_dcount(a->dentry) == 1
  21789. + && atomic_read(&a->inode->i_count) == 1
  21790. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  21791. + && (!plinked || !au_plink_test(a->inode))
  21792. + && a->inode->i_nlink == 1)
  21793. + goto out;
  21794. +
  21795. + err = -EBUSY;
  21796. + AU_MVD_PR(dmsg,
  21797. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  21798. + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
  21799. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  21800. + a->mvd_h_src_inode->i_nlink,
  21801. + plinked, plinked ? au_plink_test(a->inode) : 0);
  21802. +
  21803. +out:
  21804. + AuTraceErr(err);
  21805. + return err;
  21806. +}
  21807. +
  21808. +/* make sure the parent dir is fine */
  21809. +static int au_mvd_args_parent(const unsigned char dmsg,
  21810. + struct au_mvd_args *a)
  21811. +{
  21812. + int err;
  21813. + aufs_bindex_t bindex;
  21814. +
  21815. + err = 0;
  21816. + if (unlikely(au_alive_dir(a->parent))) {
  21817. + err = -ENOENT;
  21818. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  21819. + goto out;
  21820. + }
  21821. +
  21822. + a->bopq = au_dbdiropq(a->parent);
  21823. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  21824. + AuDbg("b%d\n", bindex);
  21825. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  21826. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  21827. + err = -EINVAL;
  21828. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  21829. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  21830. + a->bopq, a->mvd_bdst);
  21831. + }
  21832. +
  21833. +out:
  21834. + AuTraceErr(err);
  21835. + return err;
  21836. +}
  21837. +
  21838. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  21839. + struct au_mvd_args *a)
  21840. +{
  21841. + int err;
  21842. + struct au_dinfo *dinfo, *tmp;
  21843. +
  21844. + /* lookup the next lower positive entry */
  21845. + err = -ENOMEM;
  21846. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  21847. + if (unlikely(!tmp))
  21848. + goto out;
  21849. +
  21850. + a->bfound = -1;
  21851. + a->bwh = -1;
  21852. + dinfo = au_di(a->dentry);
  21853. + au_di_cp(tmp, dinfo);
  21854. + au_di_swap(tmp, dinfo);
  21855. +
  21856. + /* returns the number of positive dentries */
  21857. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
  21858. + /* AuLkup_IGNORE_PERM */ 0);
  21859. + if (!err)
  21860. + a->bwh = au_dbwh(a->dentry);
  21861. + else if (err > 0)
  21862. + a->bfound = au_dbtop(a->dentry);
  21863. +
  21864. + au_di_swap(tmp, dinfo);
  21865. + au_rw_write_unlock(&tmp->di_rwsem);
  21866. + au_di_free(tmp);
  21867. + if (unlikely(err < 0))
  21868. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  21869. +
  21870. + /*
  21871. + * here, we have these cases.
  21872. + * bfound == -1
  21873. + * no positive dentry under bsrc. there are more sub-cases.
  21874. + * bwh < 0
  21875. + * there no whiteout, we can safely move-down.
  21876. + * bwh <= bsrc
  21877. + * impossible
  21878. + * bsrc < bwh && bwh < bdst
  21879. + * there is a whiteout on RO branch. cannot proceed.
  21880. + * bwh == bdst
  21881. + * there is a whiteout on the RW target branch. it should
  21882. + * be removed.
  21883. + * bdst < bwh
  21884. + * there is a whiteout somewhere unrelated branch.
  21885. + * -1 < bfound && bfound <= bsrc
  21886. + * impossible.
  21887. + * bfound < bdst
  21888. + * found, but it is on RO branch between bsrc and bdst. cannot
  21889. + * proceed.
  21890. + * bfound == bdst
  21891. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  21892. + * error.
  21893. + * bdst < bfound
  21894. + * found, after we create the file on bdst, it will be hidden.
  21895. + */
  21896. +
  21897. + AuDebugOn(a->bfound == -1
  21898. + && a->bwh != -1
  21899. + && a->bwh <= a->mvd_bsrc);
  21900. + AuDebugOn(-1 < a->bfound
  21901. + && a->bfound <= a->mvd_bsrc);
  21902. +
  21903. + err = -EINVAL;
  21904. + if (a->bfound == -1
  21905. + && a->mvd_bsrc < a->bwh
  21906. + && a->bwh != -1
  21907. + && a->bwh < a->mvd_bdst) {
  21908. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  21909. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  21910. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  21911. + goto out;
  21912. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  21913. + a->mvd_errno = EAU_MVDOWN_UPPER;
  21914. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  21915. + a->mvd_bdst, a->bfound);
  21916. + goto out;
  21917. + }
  21918. +
  21919. + err = 0; /* success */
  21920. +
  21921. +out:
  21922. + AuTraceErr(err);
  21923. + return err;
  21924. +}
  21925. +
  21926. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  21927. +{
  21928. + int err;
  21929. +
  21930. + err = 0;
  21931. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  21932. + && a->bfound == a->mvd_bdst)
  21933. + err = -EEXIST;
  21934. + AuTraceErr(err);
  21935. + return err;
  21936. +}
  21937. +
  21938. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  21939. +{
  21940. + int err;
  21941. + struct au_branch *br;
  21942. +
  21943. + err = -EISDIR;
  21944. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  21945. + goto out;
  21946. +
  21947. + err = -EINVAL;
  21948. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  21949. + a->mvd_bsrc = au_ibtop(a->inode);
  21950. + else {
  21951. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  21952. + if (unlikely(a->mvd_bsrc < 0
  21953. + || (a->mvd_bsrc < au_dbtop(a->dentry)
  21954. + || au_dbbot(a->dentry) < a->mvd_bsrc
  21955. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  21956. + || (a->mvd_bsrc < au_ibtop(a->inode)
  21957. + || au_ibbot(a->inode) < a->mvd_bsrc
  21958. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  21959. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  21960. + AU_MVD_PR(dmsg, "no upper\n");
  21961. + goto out;
  21962. + }
  21963. + }
  21964. + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
  21965. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  21966. + AU_MVD_PR(dmsg, "on the bottom\n");
  21967. + goto out;
  21968. + }
  21969. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  21970. + br = au_sbr(a->sb, a->mvd_bsrc);
  21971. + err = au_br_rdonly(br);
  21972. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  21973. + if (unlikely(err))
  21974. + goto out;
  21975. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  21976. + || IS_APPEND(a->mvd_h_src_inode))) {
  21977. + if (err)
  21978. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  21979. + /* go on */
  21980. + } else
  21981. + goto out;
  21982. +
  21983. + err = -EINVAL;
  21984. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  21985. + a->mvd_bdst = find_lower_writable(a);
  21986. + if (unlikely(a->mvd_bdst < 0)) {
  21987. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  21988. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  21989. + goto out;
  21990. + }
  21991. + } else {
  21992. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  21993. + if (unlikely(a->mvd_bdst < 0
  21994. + || au_sbbot(a->sb) < a->mvd_bdst)) {
  21995. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  21996. + AU_MVD_PR(dmsg, "no lower brid\n");
  21997. + goto out;
  21998. + }
  21999. + }
  22000. +
  22001. + err = au_mvd_args_busy(dmsg, a);
  22002. + if (!err)
  22003. + err = au_mvd_args_parent(dmsg, a);
  22004. + if (!err)
  22005. + err = au_mvd_args_intermediate(dmsg, a);
  22006. + if (!err)
  22007. + err = au_mvd_args_exist(dmsg, a);
  22008. + if (!err)
  22009. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  22010. +
  22011. +out:
  22012. + AuTraceErr(err);
  22013. + return err;
  22014. +}
  22015. +
  22016. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  22017. +{
  22018. + int err, e;
  22019. + unsigned char dmsg;
  22020. + struct au_mvd_args *args;
  22021. + struct inode *inode;
  22022. +
  22023. + inode = d_inode(dentry);
  22024. + err = -EPERM;
  22025. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  22026. + goto out;
  22027. +
  22028. + err = -ENOMEM;
  22029. + args = kmalloc(sizeof(*args), GFP_NOFS);
  22030. + if (unlikely(!args))
  22031. + goto out;
  22032. +
  22033. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  22034. + if (!err)
  22035. + err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
  22036. + if (unlikely(err)) {
  22037. + err = -EFAULT;
  22038. + AuTraceErr(err);
  22039. + goto out_free;
  22040. + }
  22041. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  22042. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  22043. + args->mvdown.au_errno = 0;
  22044. + args->dentry = dentry;
  22045. + args->inode = inode;
  22046. + args->sb = dentry->d_sb;
  22047. +
  22048. + err = -ENOENT;
  22049. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  22050. + args->parent = dget_parent(dentry);
  22051. + args->dir = d_inode(args->parent);
  22052. + mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
  22053. + dput(args->parent);
  22054. + if (unlikely(args->parent != dentry->d_parent)) {
  22055. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  22056. + goto out_dir;
  22057. + }
  22058. +
  22059. + mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  22060. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
  22061. + if (unlikely(err))
  22062. + goto out_inode;
  22063. +
  22064. + di_write_lock_parent(args->parent);
  22065. + err = au_mvd_args(dmsg, args);
  22066. + if (unlikely(err))
  22067. + goto out_parent;
  22068. +
  22069. + err = au_do_mvdown(dmsg, args);
  22070. + if (unlikely(err))
  22071. + goto out_parent;
  22072. +
  22073. + au_cpup_attr_timesizes(args->dir);
  22074. + au_cpup_attr_timesizes(inode);
  22075. + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
  22076. + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
  22077. + /* au_digen_dec(dentry); */
  22078. +
  22079. +out_parent:
  22080. + di_write_unlock(args->parent);
  22081. + aufs_read_unlock(dentry, AuLock_DW);
  22082. +out_inode:
  22083. + mutex_unlock(&inode->i_mutex);
  22084. +out_dir:
  22085. + mutex_unlock(&args->dir->i_mutex);
  22086. +out_free:
  22087. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  22088. + if (unlikely(e))
  22089. + err = -EFAULT;
  22090. + au_delayed_kfree(args);
  22091. +out:
  22092. + AuTraceErr(err);
  22093. + return err;
  22094. +}
  22095. --- /dev/null
  22096. +++ linux-4.4/fs/aufs/opts.c 2016-10-23 11:20:57.638826618 +0300
  22097. @@ -0,0 +1,1860 @@
  22098. +/*
  22099. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  22100. + *
  22101. + * This program, aufs is free software; you can redistribute it and/or modify
  22102. + * it under the terms of the GNU General Public License as published by
  22103. + * the Free Software Foundation; either version 2 of the License, or
  22104. + * (at your option) any later version.
  22105. + *
  22106. + * This program is distributed in the hope that it will be useful,
  22107. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22108. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22109. + * GNU General Public License for more details.
  22110. + *
  22111. + * You should have received a copy of the GNU General Public License
  22112. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22113. + */
  22114. +
  22115. +/*
  22116. + * mount options/flags
  22117. + */
  22118. +
  22119. +#include <linux/namei.h>
  22120. +#include <linux/types.h> /* a distribution requires */
  22121. +#include <linux/parser.h>
  22122. +#include "aufs.h"
  22123. +
  22124. +/* ---------------------------------------------------------------------- */
  22125. +
  22126. +enum {
  22127. + Opt_br,
  22128. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  22129. + Opt_idel, Opt_imod,
  22130. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  22131. + Opt_rdblk_def, Opt_rdhash_def,
  22132. + Opt_xino, Opt_noxino,
  22133. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  22134. + Opt_trunc_xino_path, Opt_itrunc_xino,
  22135. + Opt_trunc_xib, Opt_notrunc_xib,
  22136. + Opt_shwh, Opt_noshwh,
  22137. + Opt_plink, Opt_noplink, Opt_list_plink,
  22138. + Opt_udba,
  22139. + Opt_dio, Opt_nodio,
  22140. + Opt_diropq_a, Opt_diropq_w,
  22141. + Opt_warn_perm, Opt_nowarn_perm,
  22142. + Opt_wbr_copyup, Opt_wbr_create,
  22143. + Opt_fhsm_sec,
  22144. + Opt_verbose, Opt_noverbose,
  22145. + Opt_sum, Opt_nosum, Opt_wsum,
  22146. + Opt_dirperm1, Opt_nodirperm1,
  22147. + Opt_acl, Opt_noacl,
  22148. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  22149. +};
  22150. +
  22151. +static match_table_t options = {
  22152. + {Opt_br, "br=%s"},
  22153. + {Opt_br, "br:%s"},
  22154. +
  22155. + {Opt_add, "add=%d:%s"},
  22156. + {Opt_add, "add:%d:%s"},
  22157. + {Opt_add, "ins=%d:%s"},
  22158. + {Opt_add, "ins:%d:%s"},
  22159. + {Opt_append, "append=%s"},
  22160. + {Opt_append, "append:%s"},
  22161. + {Opt_prepend, "prepend=%s"},
  22162. + {Opt_prepend, "prepend:%s"},
  22163. +
  22164. + {Opt_del, "del=%s"},
  22165. + {Opt_del, "del:%s"},
  22166. + /* {Opt_idel, "idel:%d"}, */
  22167. + {Opt_mod, "mod=%s"},
  22168. + {Opt_mod, "mod:%s"},
  22169. + /* {Opt_imod, "imod:%d:%s"}, */
  22170. +
  22171. + {Opt_dirwh, "dirwh=%d"},
  22172. +
  22173. + {Opt_xino, "xino=%s"},
  22174. + {Opt_noxino, "noxino"},
  22175. + {Opt_trunc_xino, "trunc_xino"},
  22176. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  22177. + {Opt_notrunc_xino, "notrunc_xino"},
  22178. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  22179. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  22180. + /* {Opt_zxino, "zxino=%s"}, */
  22181. + {Opt_trunc_xib, "trunc_xib"},
  22182. + {Opt_notrunc_xib, "notrunc_xib"},
  22183. +
  22184. +#ifdef CONFIG_PROC_FS
  22185. + {Opt_plink, "plink"},
  22186. +#else
  22187. + {Opt_ignore_silent, "plink"},
  22188. +#endif
  22189. +
  22190. + {Opt_noplink, "noplink"},
  22191. +
  22192. +#ifdef CONFIG_AUFS_DEBUG
  22193. + {Opt_list_plink, "list_plink"},
  22194. +#endif
  22195. +
  22196. + {Opt_udba, "udba=%s"},
  22197. +
  22198. + {Opt_dio, "dio"},
  22199. + {Opt_nodio, "nodio"},
  22200. +
  22201. +#ifdef CONFIG_AUFS_FHSM
  22202. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  22203. +#else
  22204. + {Opt_ignore_silent, "fhsm_sec=%d"},
  22205. +#endif
  22206. +
  22207. + {Opt_diropq_a, "diropq=always"},
  22208. + {Opt_diropq_a, "diropq=a"},
  22209. + {Opt_diropq_w, "diropq=whiteouted"},
  22210. + {Opt_diropq_w, "diropq=w"},
  22211. +
  22212. + {Opt_warn_perm, "warn_perm"},
  22213. + {Opt_nowarn_perm, "nowarn_perm"},
  22214. +
  22215. + /* keep them temporary */
  22216. + {Opt_ignore_silent, "nodlgt"},
  22217. + {Opt_ignore_silent, "clean_plink"},
  22218. +
  22219. +#ifdef CONFIG_AUFS_SHWH
  22220. + {Opt_shwh, "shwh"},
  22221. +#endif
  22222. + {Opt_noshwh, "noshwh"},
  22223. +
  22224. + {Opt_dirperm1, "dirperm1"},
  22225. + {Opt_nodirperm1, "nodirperm1"},
  22226. +
  22227. + {Opt_verbose, "verbose"},
  22228. + {Opt_verbose, "v"},
  22229. + {Opt_noverbose, "noverbose"},
  22230. + {Opt_noverbose, "quiet"},
  22231. + {Opt_noverbose, "q"},
  22232. + {Opt_noverbose, "silent"},
  22233. +
  22234. + {Opt_sum, "sum"},
  22235. + {Opt_nosum, "nosum"},
  22236. + {Opt_wsum, "wsum"},
  22237. +
  22238. + {Opt_rdcache, "rdcache=%d"},
  22239. + {Opt_rdblk, "rdblk=%d"},
  22240. + {Opt_rdblk_def, "rdblk=def"},
  22241. + {Opt_rdhash, "rdhash=%d"},
  22242. + {Opt_rdhash_def, "rdhash=def"},
  22243. +
  22244. + {Opt_wbr_create, "create=%s"},
  22245. + {Opt_wbr_create, "create_policy=%s"},
  22246. + {Opt_wbr_copyup, "cpup=%s"},
  22247. + {Opt_wbr_copyup, "copyup=%s"},
  22248. + {Opt_wbr_copyup, "copyup_policy=%s"},
  22249. +
  22250. + /* generic VFS flag */
  22251. +#ifdef CONFIG_FS_POSIX_ACL
  22252. + {Opt_acl, "acl"},
  22253. + {Opt_noacl, "noacl"},
  22254. +#else
  22255. + {Opt_ignore_silent, "acl"},
  22256. + {Opt_ignore_silent, "noacl"},
  22257. +#endif
  22258. +
  22259. + /* internal use for the scripts */
  22260. + {Opt_ignore_silent, "si=%s"},
  22261. +
  22262. + {Opt_br, "dirs=%s"},
  22263. + {Opt_ignore, "debug=%d"},
  22264. + {Opt_ignore, "delete=whiteout"},
  22265. + {Opt_ignore, "delete=all"},
  22266. + {Opt_ignore, "imap=%s"},
  22267. +
  22268. + /* temporary workaround, due to old mount(8)? */
  22269. + {Opt_ignore_silent, "relatime"},
  22270. +
  22271. + {Opt_err, NULL}
  22272. +};
  22273. +
  22274. +/* ---------------------------------------------------------------------- */
  22275. +
  22276. +static const char *au_parser_pattern(int val, match_table_t tbl)
  22277. +{
  22278. + struct match_token *p;
  22279. +
  22280. + p = tbl;
  22281. + while (p->pattern) {
  22282. + if (p->token == val)
  22283. + return p->pattern;
  22284. + p++;
  22285. + }
  22286. + BUG();
  22287. + return "??";
  22288. +}
  22289. +
  22290. +static const char *au_optstr(int *val, match_table_t tbl)
  22291. +{
  22292. + struct match_token *p;
  22293. + int v;
  22294. +
  22295. + v = *val;
  22296. + if (!v)
  22297. + goto out;
  22298. + p = tbl;
  22299. + while (p->pattern) {
  22300. + if (p->token
  22301. + && (v & p->token) == p->token) {
  22302. + *val &= ~p->token;
  22303. + return p->pattern;
  22304. + }
  22305. + p++;
  22306. + }
  22307. +
  22308. +out:
  22309. + return NULL;
  22310. +}
  22311. +
  22312. +/* ---------------------------------------------------------------------- */
  22313. +
  22314. +static match_table_t brperm = {
  22315. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  22316. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  22317. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  22318. + {0, NULL}
  22319. +};
  22320. +
  22321. +static match_table_t brattr = {
  22322. + /* general */
  22323. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  22324. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  22325. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  22326. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  22327. +#ifdef CONFIG_AUFS_FHSM
  22328. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  22329. +#endif
  22330. +#ifdef CONFIG_AUFS_XATTR
  22331. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  22332. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  22333. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  22334. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  22335. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  22336. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  22337. +#endif
  22338. +
  22339. + /* ro/rr branch */
  22340. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  22341. +
  22342. + /* rw branch */
  22343. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  22344. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  22345. +
  22346. + {0, NULL}
  22347. +};
  22348. +
  22349. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  22350. +{
  22351. + int attr, v;
  22352. + char *p;
  22353. +
  22354. + attr = 0;
  22355. + do {
  22356. + p = strchr(str, '+');
  22357. + if (p)
  22358. + *p = 0;
  22359. + v = match_token(str, table, args);
  22360. + if (v) {
  22361. + if (v & AuBrAttr_CMOO_Mask)
  22362. + attr &= ~AuBrAttr_CMOO_Mask;
  22363. + attr |= v;
  22364. + } else {
  22365. + if (p)
  22366. + *p = '+';
  22367. + pr_warn("ignored branch attribute %s\n", str);
  22368. + break;
  22369. + }
  22370. + if (p)
  22371. + str = p + 1;
  22372. + } while (p);
  22373. +
  22374. + return attr;
  22375. +}
  22376. +
  22377. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  22378. +{
  22379. + int sz;
  22380. + const char *p;
  22381. + char *q;
  22382. +
  22383. + q = str->a;
  22384. + *q = 0;
  22385. + p = au_optstr(&perm, brattr);
  22386. + if (p) {
  22387. + sz = strlen(p);
  22388. + memcpy(q, p, sz + 1);
  22389. + q += sz;
  22390. + } else
  22391. + goto out;
  22392. +
  22393. + do {
  22394. + p = au_optstr(&perm, brattr);
  22395. + if (p) {
  22396. + *q++ = '+';
  22397. + sz = strlen(p);
  22398. + memcpy(q, p, sz + 1);
  22399. + q += sz;
  22400. + }
  22401. + } while (p);
  22402. +
  22403. +out:
  22404. + return q - str->a;
  22405. +}
  22406. +
  22407. +static int noinline_for_stack br_perm_val(char *perm)
  22408. +{
  22409. + int val, bad, sz;
  22410. + char *p;
  22411. + substring_t args[MAX_OPT_ARGS];
  22412. + au_br_perm_str_t attr;
  22413. +
  22414. + p = strchr(perm, '+');
  22415. + if (p)
  22416. + *p = 0;
  22417. + val = match_token(perm, brperm, args);
  22418. + if (!val) {
  22419. + if (p)
  22420. + *p = '+';
  22421. + pr_warn("ignored branch permission %s\n", perm);
  22422. + val = AuBrPerm_RO;
  22423. + goto out;
  22424. + }
  22425. + if (!p)
  22426. + goto out;
  22427. +
  22428. + val |= br_attr_val(p + 1, brattr, args);
  22429. +
  22430. + bad = 0;
  22431. + switch (val & AuBrPerm_Mask) {
  22432. + case AuBrPerm_RO:
  22433. + case AuBrPerm_RR:
  22434. + bad = val & AuBrWAttr_Mask;
  22435. + val &= ~AuBrWAttr_Mask;
  22436. + break;
  22437. + case AuBrPerm_RW:
  22438. + bad = val & AuBrRAttr_Mask;
  22439. + val &= ~AuBrRAttr_Mask;
  22440. + break;
  22441. + }
  22442. +
  22443. + /*
  22444. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  22445. + * does not treat it as an error, just warning.
  22446. + * this is a tiny guard for the user operation.
  22447. + */
  22448. + if (val & AuBrAttr_UNPIN) {
  22449. + bad |= AuBrAttr_UNPIN;
  22450. + val &= ~AuBrAttr_UNPIN;
  22451. + }
  22452. +
  22453. + if (unlikely(bad)) {
  22454. + sz = au_do_optstr_br_attr(&attr, bad);
  22455. + AuDebugOn(!sz);
  22456. + pr_warn("ignored branch attribute %s\n", attr.a);
  22457. + }
  22458. +
  22459. +out:
  22460. + return val;
  22461. +}
  22462. +
  22463. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  22464. +{
  22465. + au_br_perm_str_t attr;
  22466. + const char *p;
  22467. + char *q;
  22468. + int sz;
  22469. +
  22470. + q = str->a;
  22471. + p = au_optstr(&perm, brperm);
  22472. + AuDebugOn(!p || !*p);
  22473. + sz = strlen(p);
  22474. + memcpy(q, p, sz + 1);
  22475. + q += sz;
  22476. +
  22477. + sz = au_do_optstr_br_attr(&attr, perm);
  22478. + if (sz) {
  22479. + *q++ = '+';
  22480. + memcpy(q, attr.a, sz + 1);
  22481. + }
  22482. +
  22483. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  22484. +}
  22485. +
  22486. +/* ---------------------------------------------------------------------- */
  22487. +
  22488. +static match_table_t udbalevel = {
  22489. + {AuOpt_UDBA_REVAL, "reval"},
  22490. + {AuOpt_UDBA_NONE, "none"},
  22491. +#ifdef CONFIG_AUFS_HNOTIFY
  22492. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  22493. +#ifdef CONFIG_AUFS_HFSNOTIFY
  22494. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  22495. +#endif
  22496. +#endif
  22497. + {-1, NULL}
  22498. +};
  22499. +
  22500. +static int noinline_for_stack udba_val(char *str)
  22501. +{
  22502. + substring_t args[MAX_OPT_ARGS];
  22503. +
  22504. + return match_token(str, udbalevel, args);
  22505. +}
  22506. +
  22507. +const char *au_optstr_udba(int udba)
  22508. +{
  22509. + return au_parser_pattern(udba, udbalevel);
  22510. +}
  22511. +
  22512. +/* ---------------------------------------------------------------------- */
  22513. +
  22514. +static match_table_t au_wbr_create_policy = {
  22515. + {AuWbrCreate_TDP, "tdp"},
  22516. + {AuWbrCreate_TDP, "top-down-parent"},
  22517. + {AuWbrCreate_RR, "rr"},
  22518. + {AuWbrCreate_RR, "round-robin"},
  22519. + {AuWbrCreate_MFS, "mfs"},
  22520. + {AuWbrCreate_MFS, "most-free-space"},
  22521. + {AuWbrCreate_MFSV, "mfs:%d"},
  22522. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  22523. +
  22524. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  22525. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  22526. + {AuWbrCreate_PMFS, "pmfs"},
  22527. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  22528. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  22529. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  22530. +
  22531. + {-1, NULL}
  22532. +};
  22533. +
  22534. +/*
  22535. + * cf. linux/lib/parser.c and cmdline.c
  22536. + * gave up calling memparse() since it uses simple_strtoull() instead of
  22537. + * kstrto...().
  22538. + */
  22539. +static int noinline_for_stack
  22540. +au_match_ull(substring_t *s, unsigned long long *result)
  22541. +{
  22542. + int err;
  22543. + unsigned int len;
  22544. + char a[32];
  22545. +
  22546. + err = -ERANGE;
  22547. + len = s->to - s->from;
  22548. + if (len + 1 <= sizeof(a)) {
  22549. + memcpy(a, s->from, len);
  22550. + a[len] = '\0';
  22551. + err = kstrtoull(a, 0, result);
  22552. + }
  22553. + return err;
  22554. +}
  22555. +
  22556. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  22557. + struct au_opt_wbr_create *create)
  22558. +{
  22559. + int err;
  22560. + unsigned long long ull;
  22561. +
  22562. + err = 0;
  22563. + if (!au_match_ull(arg, &ull))
  22564. + create->mfsrr_watermark = ull;
  22565. + else {
  22566. + pr_err("bad integer in %s\n", str);
  22567. + err = -EINVAL;
  22568. + }
  22569. +
  22570. + return err;
  22571. +}
  22572. +
  22573. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  22574. + struct au_opt_wbr_create *create)
  22575. +{
  22576. + int n, err;
  22577. +
  22578. + err = 0;
  22579. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  22580. + create->mfs_second = n;
  22581. + else {
  22582. + pr_err("bad integer in %s\n", str);
  22583. + err = -EINVAL;
  22584. + }
  22585. +
  22586. + return err;
  22587. +}
  22588. +
  22589. +static int noinline_for_stack
  22590. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  22591. +{
  22592. + int err, e;
  22593. + substring_t args[MAX_OPT_ARGS];
  22594. +
  22595. + err = match_token(str, au_wbr_create_policy, args);
  22596. + create->wbr_create = err;
  22597. + switch (err) {
  22598. + case AuWbrCreate_MFSRRV:
  22599. + case AuWbrCreate_PMFSRRV:
  22600. + e = au_wbr_mfs_wmark(&args[0], str, create);
  22601. + if (!e)
  22602. + e = au_wbr_mfs_sec(&args[1], str, create);
  22603. + if (unlikely(e))
  22604. + err = e;
  22605. + break;
  22606. + case AuWbrCreate_MFSRR:
  22607. + case AuWbrCreate_PMFSRR:
  22608. + e = au_wbr_mfs_wmark(&args[0], str, create);
  22609. + if (unlikely(e)) {
  22610. + err = e;
  22611. + break;
  22612. + }
  22613. + /*FALLTHROUGH*/
  22614. + case AuWbrCreate_MFS:
  22615. + case AuWbrCreate_PMFS:
  22616. + create->mfs_second = AUFS_MFS_DEF_SEC;
  22617. + break;
  22618. + case AuWbrCreate_MFSV:
  22619. + case AuWbrCreate_PMFSV:
  22620. + e = au_wbr_mfs_sec(&args[0], str, create);
  22621. + if (unlikely(e))
  22622. + err = e;
  22623. + break;
  22624. + }
  22625. +
  22626. + return err;
  22627. +}
  22628. +
  22629. +const char *au_optstr_wbr_create(int wbr_create)
  22630. +{
  22631. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  22632. +}
  22633. +
  22634. +static match_table_t au_wbr_copyup_policy = {
  22635. + {AuWbrCopyup_TDP, "tdp"},
  22636. + {AuWbrCopyup_TDP, "top-down-parent"},
  22637. + {AuWbrCopyup_BUP, "bup"},
  22638. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  22639. + {AuWbrCopyup_BU, "bu"},
  22640. + {AuWbrCopyup_BU, "bottom-up"},
  22641. + {-1, NULL}
  22642. +};
  22643. +
  22644. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  22645. +{
  22646. + substring_t args[MAX_OPT_ARGS];
  22647. +
  22648. + return match_token(str, au_wbr_copyup_policy, args);
  22649. +}
  22650. +
  22651. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  22652. +{
  22653. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  22654. +}
  22655. +
  22656. +/* ---------------------------------------------------------------------- */
  22657. +
  22658. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  22659. +
  22660. +static void dump_opts(struct au_opts *opts)
  22661. +{
  22662. +#ifdef CONFIG_AUFS_DEBUG
  22663. + /* reduce stack space */
  22664. + union {
  22665. + struct au_opt_add *add;
  22666. + struct au_opt_del *del;
  22667. + struct au_opt_mod *mod;
  22668. + struct au_opt_xino *xino;
  22669. + struct au_opt_xino_itrunc *xino_itrunc;
  22670. + struct au_opt_wbr_create *create;
  22671. + } u;
  22672. + struct au_opt *opt;
  22673. +
  22674. + opt = opts->opt;
  22675. + while (opt->type != Opt_tail) {
  22676. + switch (opt->type) {
  22677. + case Opt_add:
  22678. + u.add = &opt->add;
  22679. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  22680. + u.add->bindex, u.add->pathname, u.add->perm,
  22681. + u.add->path.dentry);
  22682. + break;
  22683. + case Opt_del:
  22684. + case Opt_idel:
  22685. + u.del = &opt->del;
  22686. + AuDbg("del {%s, %p}\n",
  22687. + u.del->pathname, u.del->h_path.dentry);
  22688. + break;
  22689. + case Opt_mod:
  22690. + case Opt_imod:
  22691. + u.mod = &opt->mod;
  22692. + AuDbg("mod {%s, 0x%x, %p}\n",
  22693. + u.mod->path, u.mod->perm, u.mod->h_root);
  22694. + break;
  22695. + case Opt_append:
  22696. + u.add = &opt->add;
  22697. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  22698. + u.add->bindex, u.add->pathname, u.add->perm,
  22699. + u.add->path.dentry);
  22700. + break;
  22701. + case Opt_prepend:
  22702. + u.add = &opt->add;
  22703. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  22704. + u.add->bindex, u.add->pathname, u.add->perm,
  22705. + u.add->path.dentry);
  22706. + break;
  22707. + case Opt_dirwh:
  22708. + AuDbg("dirwh %d\n", opt->dirwh);
  22709. + break;
  22710. + case Opt_rdcache:
  22711. + AuDbg("rdcache %d\n", opt->rdcache);
  22712. + break;
  22713. + case Opt_rdblk:
  22714. + AuDbg("rdblk %u\n", opt->rdblk);
  22715. + break;
  22716. + case Opt_rdblk_def:
  22717. + AuDbg("rdblk_def\n");
  22718. + break;
  22719. + case Opt_rdhash:
  22720. + AuDbg("rdhash %u\n", opt->rdhash);
  22721. + break;
  22722. + case Opt_rdhash_def:
  22723. + AuDbg("rdhash_def\n");
  22724. + break;
  22725. + case Opt_xino:
  22726. + u.xino = &opt->xino;
  22727. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  22728. + break;
  22729. + case Opt_trunc_xino:
  22730. + AuLabel(trunc_xino);
  22731. + break;
  22732. + case Opt_notrunc_xino:
  22733. + AuLabel(notrunc_xino);
  22734. + break;
  22735. + case Opt_trunc_xino_path:
  22736. + case Opt_itrunc_xino:
  22737. + u.xino_itrunc = &opt->xino_itrunc;
  22738. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  22739. + break;
  22740. + case Opt_noxino:
  22741. + AuLabel(noxino);
  22742. + break;
  22743. + case Opt_trunc_xib:
  22744. + AuLabel(trunc_xib);
  22745. + break;
  22746. + case Opt_notrunc_xib:
  22747. + AuLabel(notrunc_xib);
  22748. + break;
  22749. + case Opt_shwh:
  22750. + AuLabel(shwh);
  22751. + break;
  22752. + case Opt_noshwh:
  22753. + AuLabel(noshwh);
  22754. + break;
  22755. + case Opt_dirperm1:
  22756. + AuLabel(dirperm1);
  22757. + break;
  22758. + case Opt_nodirperm1:
  22759. + AuLabel(nodirperm1);
  22760. + break;
  22761. + case Opt_plink:
  22762. + AuLabel(plink);
  22763. + break;
  22764. + case Opt_noplink:
  22765. + AuLabel(noplink);
  22766. + break;
  22767. + case Opt_list_plink:
  22768. + AuLabel(list_plink);
  22769. + break;
  22770. + case Opt_udba:
  22771. + AuDbg("udba %d, %s\n",
  22772. + opt->udba, au_optstr_udba(opt->udba));
  22773. + break;
  22774. + case Opt_dio:
  22775. + AuLabel(dio);
  22776. + break;
  22777. + case Opt_nodio:
  22778. + AuLabel(nodio);
  22779. + break;
  22780. + case Opt_diropq_a:
  22781. + AuLabel(diropq_a);
  22782. + break;
  22783. + case Opt_diropq_w:
  22784. + AuLabel(diropq_w);
  22785. + break;
  22786. + case Opt_warn_perm:
  22787. + AuLabel(warn_perm);
  22788. + break;
  22789. + case Opt_nowarn_perm:
  22790. + AuLabel(nowarn_perm);
  22791. + break;
  22792. + case Opt_verbose:
  22793. + AuLabel(verbose);
  22794. + break;
  22795. + case Opt_noverbose:
  22796. + AuLabel(noverbose);
  22797. + break;
  22798. + case Opt_sum:
  22799. + AuLabel(sum);
  22800. + break;
  22801. + case Opt_nosum:
  22802. + AuLabel(nosum);
  22803. + break;
  22804. + case Opt_wsum:
  22805. + AuLabel(wsum);
  22806. + break;
  22807. + case Opt_wbr_create:
  22808. + u.create = &opt->wbr_create;
  22809. + AuDbg("create %d, %s\n", u.create->wbr_create,
  22810. + au_optstr_wbr_create(u.create->wbr_create));
  22811. + switch (u.create->wbr_create) {
  22812. + case AuWbrCreate_MFSV:
  22813. + case AuWbrCreate_PMFSV:
  22814. + AuDbg("%d sec\n", u.create->mfs_second);
  22815. + break;
  22816. + case AuWbrCreate_MFSRR:
  22817. + AuDbg("%llu watermark\n",
  22818. + u.create->mfsrr_watermark);
  22819. + break;
  22820. + case AuWbrCreate_MFSRRV:
  22821. + case AuWbrCreate_PMFSRRV:
  22822. + AuDbg("%llu watermark, %d sec\n",
  22823. + u.create->mfsrr_watermark,
  22824. + u.create->mfs_second);
  22825. + break;
  22826. + }
  22827. + break;
  22828. + case Opt_wbr_copyup:
  22829. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  22830. + au_optstr_wbr_copyup(opt->wbr_copyup));
  22831. + break;
  22832. + case Opt_fhsm_sec:
  22833. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  22834. + break;
  22835. + case Opt_acl:
  22836. + AuLabel(acl);
  22837. + break;
  22838. + case Opt_noacl:
  22839. + AuLabel(noacl);
  22840. + break;
  22841. + default:
  22842. + BUG();
  22843. + }
  22844. + opt++;
  22845. + }
  22846. +#endif
  22847. +}
  22848. +
  22849. +void au_opts_free(struct au_opts *opts)
  22850. +{
  22851. + struct au_opt *opt;
  22852. +
  22853. + opt = opts->opt;
  22854. + while (opt->type != Opt_tail) {
  22855. + switch (opt->type) {
  22856. + case Opt_add:
  22857. + case Opt_append:
  22858. + case Opt_prepend:
  22859. + path_put(&opt->add.path);
  22860. + break;
  22861. + case Opt_del:
  22862. + case Opt_idel:
  22863. + path_put(&opt->del.h_path);
  22864. + break;
  22865. + case Opt_mod:
  22866. + case Opt_imod:
  22867. + dput(opt->mod.h_root);
  22868. + break;
  22869. + case Opt_xino:
  22870. + fput(opt->xino.file);
  22871. + break;
  22872. + }
  22873. + opt++;
  22874. + }
  22875. +}
  22876. +
  22877. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  22878. + aufs_bindex_t bindex)
  22879. +{
  22880. + int err;
  22881. + struct au_opt_add *add = &opt->add;
  22882. + char *p;
  22883. +
  22884. + add->bindex = bindex;
  22885. + add->perm = AuBrPerm_RO;
  22886. + add->pathname = opt_str;
  22887. + p = strchr(opt_str, '=');
  22888. + if (p) {
  22889. + *p++ = 0;
  22890. + if (*p)
  22891. + add->perm = br_perm_val(p);
  22892. + }
  22893. +
  22894. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  22895. + if (!err) {
  22896. + if (!p) {
  22897. + add->perm = AuBrPerm_RO;
  22898. + if (au_test_fs_rr(add->path.dentry->d_sb))
  22899. + add->perm = AuBrPerm_RR;
  22900. + else if (!bindex && !(sb_flags & MS_RDONLY))
  22901. + add->perm = AuBrPerm_RW;
  22902. + }
  22903. + opt->type = Opt_add;
  22904. + goto out;
  22905. + }
  22906. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  22907. + err = -EINVAL;
  22908. +
  22909. +out:
  22910. + return err;
  22911. +}
  22912. +
  22913. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  22914. +{
  22915. + int err;
  22916. +
  22917. + del->pathname = args[0].from;
  22918. + AuDbg("del path %s\n", del->pathname);
  22919. +
  22920. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  22921. + if (unlikely(err))
  22922. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  22923. +
  22924. + return err;
  22925. +}
  22926. +
  22927. +#if 0 /* reserved for future use */
  22928. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  22929. + struct au_opt_del *del, substring_t args[])
  22930. +{
  22931. + int err;
  22932. + struct dentry *root;
  22933. +
  22934. + err = -EINVAL;
  22935. + root = sb->s_root;
  22936. + aufs_read_lock(root, AuLock_FLUSH);
  22937. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  22938. + pr_err("out of bounds, %d\n", bindex);
  22939. + goto out;
  22940. + }
  22941. +
  22942. + err = 0;
  22943. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  22944. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  22945. +
  22946. +out:
  22947. + aufs_read_unlock(root, !AuLock_IR);
  22948. + return err;
  22949. +}
  22950. +#endif
  22951. +
  22952. +static int noinline_for_stack
  22953. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  22954. +{
  22955. + int err;
  22956. + struct path path;
  22957. + char *p;
  22958. +
  22959. + err = -EINVAL;
  22960. + mod->path = args[0].from;
  22961. + p = strchr(mod->path, '=');
  22962. + if (unlikely(!p)) {
  22963. + pr_err("no permssion %s\n", args[0].from);
  22964. + goto out;
  22965. + }
  22966. +
  22967. + *p++ = 0;
  22968. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  22969. + if (unlikely(err)) {
  22970. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  22971. + goto out;
  22972. + }
  22973. +
  22974. + mod->perm = br_perm_val(p);
  22975. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  22976. + mod->h_root = dget(path.dentry);
  22977. + path_put(&path);
  22978. +
  22979. +out:
  22980. + return err;
  22981. +}
  22982. +
  22983. +#if 0 /* reserved for future use */
  22984. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  22985. + struct au_opt_mod *mod, substring_t args[])
  22986. +{
  22987. + int err;
  22988. + struct dentry *root;
  22989. +
  22990. + err = -EINVAL;
  22991. + root = sb->s_root;
  22992. + aufs_read_lock(root, AuLock_FLUSH);
  22993. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  22994. + pr_err("out of bounds, %d\n", bindex);
  22995. + goto out;
  22996. + }
  22997. +
  22998. + err = 0;
  22999. + mod->perm = br_perm_val(args[1].from);
  23000. + AuDbg("mod path %s, perm 0x%x, %s\n",
  23001. + mod->path, mod->perm, args[1].from);
  23002. + mod->h_root = dget(au_h_dptr(root, bindex));
  23003. +
  23004. +out:
  23005. + aufs_read_unlock(root, !AuLock_IR);
  23006. + return err;
  23007. +}
  23008. +#endif
  23009. +
  23010. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  23011. + substring_t args[])
  23012. +{
  23013. + int err;
  23014. + struct file *file;
  23015. +
  23016. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  23017. + err = PTR_ERR(file);
  23018. + if (IS_ERR(file))
  23019. + goto out;
  23020. +
  23021. + err = -EINVAL;
  23022. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  23023. + fput(file);
  23024. + pr_err("%s must be outside\n", args[0].from);
  23025. + goto out;
  23026. + }
  23027. +
  23028. + err = 0;
  23029. + xino->file = file;
  23030. + xino->path = args[0].from;
  23031. +
  23032. +out:
  23033. + return err;
  23034. +}
  23035. +
  23036. +static int noinline_for_stack
  23037. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  23038. + struct au_opt_xino_itrunc *xino_itrunc,
  23039. + substring_t args[])
  23040. +{
  23041. + int err;
  23042. + aufs_bindex_t bbot, bindex;
  23043. + struct path path;
  23044. + struct dentry *root;
  23045. +
  23046. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  23047. + if (unlikely(err)) {
  23048. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  23049. + goto out;
  23050. + }
  23051. +
  23052. + xino_itrunc->bindex = -1;
  23053. + root = sb->s_root;
  23054. + aufs_read_lock(root, AuLock_FLUSH);
  23055. + bbot = au_sbbot(sb);
  23056. + for (bindex = 0; bindex <= bbot; bindex++) {
  23057. + if (au_h_dptr(root, bindex) == path.dentry) {
  23058. + xino_itrunc->bindex = bindex;
  23059. + break;
  23060. + }
  23061. + }
  23062. + aufs_read_unlock(root, !AuLock_IR);
  23063. + path_put(&path);
  23064. +
  23065. + if (unlikely(xino_itrunc->bindex < 0)) {
  23066. + pr_err("no such branch %s\n", args[0].from);
  23067. + err = -EINVAL;
  23068. + }
  23069. +
  23070. +out:
  23071. + return err;
  23072. +}
  23073. +
  23074. +/* called without aufs lock */
  23075. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  23076. +{
  23077. + int err, n, token;
  23078. + aufs_bindex_t bindex;
  23079. + unsigned char skipped;
  23080. + struct dentry *root;
  23081. + struct au_opt *opt, *opt_tail;
  23082. + char *opt_str;
  23083. + /* reduce the stack space */
  23084. + union {
  23085. + struct au_opt_xino_itrunc *xino_itrunc;
  23086. + struct au_opt_wbr_create *create;
  23087. + } u;
  23088. + struct {
  23089. + substring_t args[MAX_OPT_ARGS];
  23090. + } *a;
  23091. +
  23092. + err = -ENOMEM;
  23093. + a = kmalloc(sizeof(*a), GFP_NOFS);
  23094. + if (unlikely(!a))
  23095. + goto out;
  23096. +
  23097. + root = sb->s_root;
  23098. + err = 0;
  23099. + bindex = 0;
  23100. + opt = opts->opt;
  23101. + opt_tail = opt + opts->max_opt - 1;
  23102. + opt->type = Opt_tail;
  23103. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  23104. + err = -EINVAL;
  23105. + skipped = 0;
  23106. + token = match_token(opt_str, options, a->args);
  23107. + switch (token) {
  23108. + case Opt_br:
  23109. + err = 0;
  23110. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  23111. + && *opt_str) {
  23112. + err = opt_add(opt, opt_str, opts->sb_flags,
  23113. + bindex++);
  23114. + if (unlikely(!err && ++opt > opt_tail)) {
  23115. + err = -E2BIG;
  23116. + break;
  23117. + }
  23118. + opt->type = Opt_tail;
  23119. + skipped = 1;
  23120. + }
  23121. + break;
  23122. + case Opt_add:
  23123. + if (unlikely(match_int(&a->args[0], &n))) {
  23124. + pr_err("bad integer in %s\n", opt_str);
  23125. + break;
  23126. + }
  23127. + bindex = n;
  23128. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  23129. + bindex);
  23130. + if (!err)
  23131. + opt->type = token;
  23132. + break;
  23133. + case Opt_append:
  23134. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  23135. + /*dummy bindex*/1);
  23136. + if (!err)
  23137. + opt->type = token;
  23138. + break;
  23139. + case Opt_prepend:
  23140. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  23141. + /*bindex*/0);
  23142. + if (!err)
  23143. + opt->type = token;
  23144. + break;
  23145. + case Opt_del:
  23146. + err = au_opts_parse_del(&opt->del, a->args);
  23147. + if (!err)
  23148. + opt->type = token;
  23149. + break;
  23150. +#if 0 /* reserved for future use */
  23151. + case Opt_idel:
  23152. + del->pathname = "(indexed)";
  23153. + if (unlikely(match_int(&args[0], &n))) {
  23154. + pr_err("bad integer in %s\n", opt_str);
  23155. + break;
  23156. + }
  23157. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  23158. + if (!err)
  23159. + opt->type = token;
  23160. + break;
  23161. +#endif
  23162. + case Opt_mod:
  23163. + err = au_opts_parse_mod(&opt->mod, a->args);
  23164. + if (!err)
  23165. + opt->type = token;
  23166. + break;
  23167. +#ifdef IMOD /* reserved for future use */
  23168. + case Opt_imod:
  23169. + u.mod->path = "(indexed)";
  23170. + if (unlikely(match_int(&a->args[0], &n))) {
  23171. + pr_err("bad integer in %s\n", opt_str);
  23172. + break;
  23173. + }
  23174. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  23175. + if (!err)
  23176. + opt->type = token;
  23177. + break;
  23178. +#endif
  23179. + case Opt_xino:
  23180. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  23181. + if (!err)
  23182. + opt->type = token;
  23183. + break;
  23184. +
  23185. + case Opt_trunc_xino_path:
  23186. + err = au_opts_parse_xino_itrunc_path
  23187. + (sb, &opt->xino_itrunc, a->args);
  23188. + if (!err)
  23189. + opt->type = token;
  23190. + break;
  23191. +
  23192. + case Opt_itrunc_xino:
  23193. + u.xino_itrunc = &opt->xino_itrunc;
  23194. + if (unlikely(match_int(&a->args[0], &n))) {
  23195. + pr_err("bad integer in %s\n", opt_str);
  23196. + break;
  23197. + }
  23198. + u.xino_itrunc->bindex = n;
  23199. + aufs_read_lock(root, AuLock_FLUSH);
  23200. + if (n < 0 || au_sbbot(sb) < n) {
  23201. + pr_err("out of bounds, %d\n", n);
  23202. + aufs_read_unlock(root, !AuLock_IR);
  23203. + break;
  23204. + }
  23205. + aufs_read_unlock(root, !AuLock_IR);
  23206. + err = 0;
  23207. + opt->type = token;
  23208. + break;
  23209. +
  23210. + case Opt_dirwh:
  23211. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  23212. + break;
  23213. + err = 0;
  23214. + opt->type = token;
  23215. + break;
  23216. +
  23217. + case Opt_rdcache:
  23218. + if (unlikely(match_int(&a->args[0], &n))) {
  23219. + pr_err("bad integer in %s\n", opt_str);
  23220. + break;
  23221. + }
  23222. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  23223. + pr_err("rdcache must be smaller than %d\n",
  23224. + AUFS_RDCACHE_MAX);
  23225. + break;
  23226. + }
  23227. + opt->rdcache = n;
  23228. + err = 0;
  23229. + opt->type = token;
  23230. + break;
  23231. + case Opt_rdblk:
  23232. + if (unlikely(match_int(&a->args[0], &n)
  23233. + || n < 0
  23234. + || n > KMALLOC_MAX_SIZE)) {
  23235. + pr_err("bad integer in %s\n", opt_str);
  23236. + break;
  23237. + }
  23238. + if (unlikely(n && n < NAME_MAX)) {
  23239. + pr_err("rdblk must be larger than %d\n",
  23240. + NAME_MAX);
  23241. + break;
  23242. + }
  23243. + opt->rdblk = n;
  23244. + err = 0;
  23245. + opt->type = token;
  23246. + break;
  23247. + case Opt_rdhash:
  23248. + if (unlikely(match_int(&a->args[0], &n)
  23249. + || n < 0
  23250. + || n * sizeof(struct hlist_head)
  23251. + > KMALLOC_MAX_SIZE)) {
  23252. + pr_err("bad integer in %s\n", opt_str);
  23253. + break;
  23254. + }
  23255. + opt->rdhash = n;
  23256. + err = 0;
  23257. + opt->type = token;
  23258. + break;
  23259. +
  23260. + case Opt_trunc_xino:
  23261. + case Opt_notrunc_xino:
  23262. + case Opt_noxino:
  23263. + case Opt_trunc_xib:
  23264. + case Opt_notrunc_xib:
  23265. + case Opt_shwh:
  23266. + case Opt_noshwh:
  23267. + case Opt_dirperm1:
  23268. + case Opt_nodirperm1:
  23269. + case Opt_plink:
  23270. + case Opt_noplink:
  23271. + case Opt_list_plink:
  23272. + case Opt_dio:
  23273. + case Opt_nodio:
  23274. + case Opt_diropq_a:
  23275. + case Opt_diropq_w:
  23276. + case Opt_warn_perm:
  23277. + case Opt_nowarn_perm:
  23278. + case Opt_verbose:
  23279. + case Opt_noverbose:
  23280. + case Opt_sum:
  23281. + case Opt_nosum:
  23282. + case Opt_wsum:
  23283. + case Opt_rdblk_def:
  23284. + case Opt_rdhash_def:
  23285. + case Opt_acl:
  23286. + case Opt_noacl:
  23287. + err = 0;
  23288. + opt->type = token;
  23289. + break;
  23290. +
  23291. + case Opt_udba:
  23292. + opt->udba = udba_val(a->args[0].from);
  23293. + if (opt->udba >= 0) {
  23294. + err = 0;
  23295. + opt->type = token;
  23296. + } else
  23297. + pr_err("wrong value, %s\n", opt_str);
  23298. + break;
  23299. +
  23300. + case Opt_wbr_create:
  23301. + u.create = &opt->wbr_create;
  23302. + u.create->wbr_create
  23303. + = au_wbr_create_val(a->args[0].from, u.create);
  23304. + if (u.create->wbr_create >= 0) {
  23305. + err = 0;
  23306. + opt->type = token;
  23307. + } else
  23308. + pr_err("wrong value, %s\n", opt_str);
  23309. + break;
  23310. + case Opt_wbr_copyup:
  23311. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  23312. + if (opt->wbr_copyup >= 0) {
  23313. + err = 0;
  23314. + opt->type = token;
  23315. + } else
  23316. + pr_err("wrong value, %s\n", opt_str);
  23317. + break;
  23318. +
  23319. + case Opt_fhsm_sec:
  23320. + if (unlikely(match_int(&a->args[0], &n)
  23321. + || n < 0)) {
  23322. + pr_err("bad integer in %s\n", opt_str);
  23323. + break;
  23324. + }
  23325. + if (sysaufs_brs) {
  23326. + opt->fhsm_second = n;
  23327. + opt->type = token;
  23328. + } else
  23329. + pr_warn("ignored %s\n", opt_str);
  23330. + err = 0;
  23331. + break;
  23332. +
  23333. + case Opt_ignore:
  23334. + pr_warn("ignored %s\n", opt_str);
  23335. + /*FALLTHROUGH*/
  23336. + case Opt_ignore_silent:
  23337. + skipped = 1;
  23338. + err = 0;
  23339. + break;
  23340. + case Opt_err:
  23341. + pr_err("unknown option %s\n", opt_str);
  23342. + break;
  23343. + }
  23344. +
  23345. + if (!err && !skipped) {
  23346. + if (unlikely(++opt > opt_tail)) {
  23347. + err = -E2BIG;
  23348. + opt--;
  23349. + opt->type = Opt_tail;
  23350. + break;
  23351. + }
  23352. + opt->type = Opt_tail;
  23353. + }
  23354. + }
  23355. +
  23356. + au_delayed_kfree(a);
  23357. + dump_opts(opts);
  23358. + if (unlikely(err))
  23359. + au_opts_free(opts);
  23360. +
  23361. +out:
  23362. + return err;
  23363. +}
  23364. +
  23365. +static int au_opt_wbr_create(struct super_block *sb,
  23366. + struct au_opt_wbr_create *create)
  23367. +{
  23368. + int err;
  23369. + struct au_sbinfo *sbinfo;
  23370. +
  23371. + SiMustWriteLock(sb);
  23372. +
  23373. + err = 1; /* handled */
  23374. + sbinfo = au_sbi(sb);
  23375. + if (sbinfo->si_wbr_create_ops->fin) {
  23376. + err = sbinfo->si_wbr_create_ops->fin(sb);
  23377. + if (!err)
  23378. + err = 1;
  23379. + }
  23380. +
  23381. + sbinfo->si_wbr_create = create->wbr_create;
  23382. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  23383. + switch (create->wbr_create) {
  23384. + case AuWbrCreate_MFSRRV:
  23385. + case AuWbrCreate_MFSRR:
  23386. + case AuWbrCreate_PMFSRR:
  23387. + case AuWbrCreate_PMFSRRV:
  23388. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  23389. + /*FALLTHROUGH*/
  23390. + case AuWbrCreate_MFS:
  23391. + case AuWbrCreate_MFSV:
  23392. + case AuWbrCreate_PMFS:
  23393. + case AuWbrCreate_PMFSV:
  23394. + sbinfo->si_wbr_mfs.mfs_expire
  23395. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  23396. + break;
  23397. + }
  23398. +
  23399. + if (sbinfo->si_wbr_create_ops->init)
  23400. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  23401. +
  23402. + return err;
  23403. +}
  23404. +
  23405. +/*
  23406. + * returns,
  23407. + * plus: processed without an error
  23408. + * zero: unprocessed
  23409. + */
  23410. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  23411. + struct au_opts *opts)
  23412. +{
  23413. + int err;
  23414. + struct au_sbinfo *sbinfo;
  23415. +
  23416. + SiMustWriteLock(sb);
  23417. +
  23418. + err = 1; /* handled */
  23419. + sbinfo = au_sbi(sb);
  23420. + switch (opt->type) {
  23421. + case Opt_udba:
  23422. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  23423. + sbinfo->si_mntflags |= opt->udba;
  23424. + opts->given_udba |= opt->udba;
  23425. + break;
  23426. +
  23427. + case Opt_plink:
  23428. + au_opt_set(sbinfo->si_mntflags, PLINK);
  23429. + break;
  23430. + case Opt_noplink:
  23431. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  23432. + au_plink_put(sb, /*verbose*/1);
  23433. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  23434. + break;
  23435. + case Opt_list_plink:
  23436. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  23437. + au_plink_list(sb);
  23438. + break;
  23439. +
  23440. + case Opt_dio:
  23441. + au_opt_set(sbinfo->si_mntflags, DIO);
  23442. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  23443. + break;
  23444. + case Opt_nodio:
  23445. + au_opt_clr(sbinfo->si_mntflags, DIO);
  23446. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  23447. + break;
  23448. +
  23449. + case Opt_fhsm_sec:
  23450. + au_fhsm_set(sbinfo, opt->fhsm_second);
  23451. + break;
  23452. +
  23453. + case Opt_diropq_a:
  23454. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  23455. + break;
  23456. + case Opt_diropq_w:
  23457. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  23458. + break;
  23459. +
  23460. + case Opt_warn_perm:
  23461. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  23462. + break;
  23463. + case Opt_nowarn_perm:
  23464. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  23465. + break;
  23466. +
  23467. + case Opt_verbose:
  23468. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  23469. + break;
  23470. + case Opt_noverbose:
  23471. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  23472. + break;
  23473. +
  23474. + case Opt_sum:
  23475. + au_opt_set(sbinfo->si_mntflags, SUM);
  23476. + break;
  23477. + case Opt_wsum:
  23478. + au_opt_clr(sbinfo->si_mntflags, SUM);
  23479. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  23480. + case Opt_nosum:
  23481. + au_opt_clr(sbinfo->si_mntflags, SUM);
  23482. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  23483. + break;
  23484. +
  23485. + case Opt_wbr_create:
  23486. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  23487. + break;
  23488. + case Opt_wbr_copyup:
  23489. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  23490. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  23491. + break;
  23492. +
  23493. + case Opt_dirwh:
  23494. + sbinfo->si_dirwh = opt->dirwh;
  23495. + break;
  23496. +
  23497. + case Opt_rdcache:
  23498. + sbinfo->si_rdcache
  23499. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  23500. + break;
  23501. + case Opt_rdblk:
  23502. + sbinfo->si_rdblk = opt->rdblk;
  23503. + break;
  23504. + case Opt_rdblk_def:
  23505. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  23506. + break;
  23507. + case Opt_rdhash:
  23508. + sbinfo->si_rdhash = opt->rdhash;
  23509. + break;
  23510. + case Opt_rdhash_def:
  23511. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  23512. + break;
  23513. +
  23514. + case Opt_shwh:
  23515. + au_opt_set(sbinfo->si_mntflags, SHWH);
  23516. + break;
  23517. + case Opt_noshwh:
  23518. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  23519. + break;
  23520. +
  23521. + case Opt_dirperm1:
  23522. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  23523. + break;
  23524. + case Opt_nodirperm1:
  23525. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  23526. + break;
  23527. +
  23528. + case Opt_trunc_xino:
  23529. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  23530. + break;
  23531. + case Opt_notrunc_xino:
  23532. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  23533. + break;
  23534. +
  23535. + case Opt_trunc_xino_path:
  23536. + case Opt_itrunc_xino:
  23537. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
  23538. + if (!err)
  23539. + err = 1;
  23540. + break;
  23541. +
  23542. + case Opt_trunc_xib:
  23543. + au_fset_opts(opts->flags, TRUNC_XIB);
  23544. + break;
  23545. + case Opt_notrunc_xib:
  23546. + au_fclr_opts(opts->flags, TRUNC_XIB);
  23547. + break;
  23548. +
  23549. + case Opt_acl:
  23550. + sb->s_flags |= MS_POSIXACL;
  23551. + break;
  23552. + case Opt_noacl:
  23553. + sb->s_flags &= ~MS_POSIXACL;
  23554. + break;
  23555. +
  23556. + default:
  23557. + err = 0;
  23558. + break;
  23559. + }
  23560. +
  23561. + return err;
  23562. +}
  23563. +
  23564. +/*
  23565. + * returns tri-state.
  23566. + * plus: processed without an error
  23567. + * zero: unprocessed
  23568. + * minus: error
  23569. + */
  23570. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  23571. + struct au_opts *opts)
  23572. +{
  23573. + int err, do_refresh;
  23574. +
  23575. + err = 0;
  23576. + switch (opt->type) {
  23577. + case Opt_append:
  23578. + opt->add.bindex = au_sbbot(sb) + 1;
  23579. + if (opt->add.bindex < 0)
  23580. + opt->add.bindex = 0;
  23581. + goto add;
  23582. + case Opt_prepend:
  23583. + opt->add.bindex = 0;
  23584. + add: /* indented label */
  23585. + case Opt_add:
  23586. + err = au_br_add(sb, &opt->add,
  23587. + au_ftest_opts(opts->flags, REMOUNT));
  23588. + if (!err) {
  23589. + err = 1;
  23590. + au_fset_opts(opts->flags, REFRESH);
  23591. + }
  23592. + break;
  23593. +
  23594. + case Opt_del:
  23595. + case Opt_idel:
  23596. + err = au_br_del(sb, &opt->del,
  23597. + au_ftest_opts(opts->flags, REMOUNT));
  23598. + if (!err) {
  23599. + err = 1;
  23600. + au_fset_opts(opts->flags, TRUNC_XIB);
  23601. + au_fset_opts(opts->flags, REFRESH);
  23602. + }
  23603. + break;
  23604. +
  23605. + case Opt_mod:
  23606. + case Opt_imod:
  23607. + err = au_br_mod(sb, &opt->mod,
  23608. + au_ftest_opts(opts->flags, REMOUNT),
  23609. + &do_refresh);
  23610. + if (!err) {
  23611. + err = 1;
  23612. + if (do_refresh)
  23613. + au_fset_opts(opts->flags, REFRESH);
  23614. + }
  23615. + break;
  23616. + }
  23617. +
  23618. + return err;
  23619. +}
  23620. +
  23621. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  23622. + struct au_opt_xino **opt_xino,
  23623. + struct au_opts *opts)
  23624. +{
  23625. + int err;
  23626. + aufs_bindex_t bbot, bindex;
  23627. + struct dentry *root, *parent, *h_root;
  23628. +
  23629. + err = 0;
  23630. + switch (opt->type) {
  23631. + case Opt_xino:
  23632. + err = au_xino_set(sb, &opt->xino,
  23633. + !!au_ftest_opts(opts->flags, REMOUNT));
  23634. + if (unlikely(err))
  23635. + break;
  23636. +
  23637. + *opt_xino = &opt->xino;
  23638. + au_xino_brid_set(sb, -1);
  23639. +
  23640. + /* safe d_parent access */
  23641. + parent = opt->xino.file->f_path.dentry->d_parent;
  23642. + root = sb->s_root;
  23643. + bbot = au_sbbot(sb);
  23644. + for (bindex = 0; bindex <= bbot; bindex++) {
  23645. + h_root = au_h_dptr(root, bindex);
  23646. + if (h_root == parent) {
  23647. + au_xino_brid_set(sb, au_sbr_id(sb, bindex));
  23648. + break;
  23649. + }
  23650. + }
  23651. + break;
  23652. +
  23653. + case Opt_noxino:
  23654. + au_xino_clr(sb);
  23655. + au_xino_brid_set(sb, -1);
  23656. + *opt_xino = (void *)-1;
  23657. + break;
  23658. + }
  23659. +
  23660. + return err;
  23661. +}
  23662. +
  23663. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  23664. + unsigned int pending)
  23665. +{
  23666. + int err, fhsm;
  23667. + aufs_bindex_t bindex, bbot;
  23668. + unsigned char do_plink, skip, do_free, can_no_dreval;
  23669. + struct au_branch *br;
  23670. + struct au_wbr *wbr;
  23671. + struct dentry *root, *dentry;
  23672. + struct inode *dir, *h_dir;
  23673. + struct au_sbinfo *sbinfo;
  23674. + struct au_hinode *hdir;
  23675. +
  23676. + SiMustAnyLock(sb);
  23677. +
  23678. + sbinfo = au_sbi(sb);
  23679. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  23680. +
  23681. + if (!(sb_flags & MS_RDONLY)) {
  23682. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  23683. + pr_warn("first branch should be rw\n");
  23684. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  23685. + pr_warn_once("shwh should be used with ro\n");
  23686. + }
  23687. +
  23688. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  23689. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  23690. + pr_warn_once("udba=*notify requires xino\n");
  23691. +
  23692. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  23693. + pr_warn_once("dirperm1 breaks the protection"
  23694. + " by the permission bits on the lower branch\n");
  23695. +
  23696. + err = 0;
  23697. + fhsm = 0;
  23698. + root = sb->s_root;
  23699. + dir = d_inode(root);
  23700. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  23701. + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
  23702. + UDBA_NONE);
  23703. + bbot = au_sbbot(sb);
  23704. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  23705. + skip = 0;
  23706. + h_dir = au_h_iptr(dir, bindex);
  23707. + br = au_sbr(sb, bindex);
  23708. +
  23709. + if ((br->br_perm & AuBrAttr_ICEX)
  23710. + && !h_dir->i_op->listxattr)
  23711. + br->br_perm &= ~AuBrAttr_ICEX;
  23712. +#if 0
  23713. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  23714. + && (au_br_sb(br)->s_flags & MS_NOSEC))
  23715. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  23716. +#endif
  23717. +
  23718. + do_free = 0;
  23719. + wbr = br->br_wbr;
  23720. + if (wbr)
  23721. + wbr_wh_read_lock(wbr);
  23722. +
  23723. + if (!au_br_writable(br->br_perm)) {
  23724. + do_free = !!wbr;
  23725. + skip = (!wbr
  23726. + || (!wbr->wbr_whbase
  23727. + && !wbr->wbr_plink
  23728. + && !wbr->wbr_orph));
  23729. + } else if (!au_br_wh_linkable(br->br_perm)) {
  23730. + /* skip = (!br->br_whbase && !br->br_orph); */
  23731. + skip = (!wbr || !wbr->wbr_whbase);
  23732. + if (skip && wbr) {
  23733. + if (do_plink)
  23734. + skip = !!wbr->wbr_plink;
  23735. + else
  23736. + skip = !wbr->wbr_plink;
  23737. + }
  23738. + } else {
  23739. + /* skip = (br->br_whbase && br->br_ohph); */
  23740. + skip = (wbr && wbr->wbr_whbase);
  23741. + if (skip) {
  23742. + if (do_plink)
  23743. + skip = !!wbr->wbr_plink;
  23744. + else
  23745. + skip = !wbr->wbr_plink;
  23746. + }
  23747. + }
  23748. + if (wbr)
  23749. + wbr_wh_read_unlock(wbr);
  23750. +
  23751. + if (can_no_dreval) {
  23752. + dentry = br->br_path.dentry;
  23753. + spin_lock(&dentry->d_lock);
  23754. + if (dentry->d_flags &
  23755. + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
  23756. + can_no_dreval = 0;
  23757. + spin_unlock(&dentry->d_lock);
  23758. + }
  23759. +
  23760. + if (au_br_fhsm(br->br_perm)) {
  23761. + fhsm++;
  23762. + AuDebugOn(!br->br_fhsm);
  23763. + }
  23764. +
  23765. + if (skip)
  23766. + continue;
  23767. +
  23768. + hdir = au_hi(dir, bindex);
  23769. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  23770. + if (wbr)
  23771. + wbr_wh_write_lock(wbr);
  23772. + err = au_wh_init(br, sb);
  23773. + if (wbr)
  23774. + wbr_wh_write_unlock(wbr);
  23775. + au_hn_imtx_unlock(hdir);
  23776. +
  23777. + if (!err && do_free) {
  23778. + if (wbr)
  23779. + au_delayed_kfree(wbr);
  23780. + br->br_wbr = NULL;
  23781. + }
  23782. + }
  23783. +
  23784. + if (can_no_dreval)
  23785. + au_fset_si(sbinfo, NO_DREVAL);
  23786. + else
  23787. + au_fclr_si(sbinfo, NO_DREVAL);
  23788. +
  23789. + if (fhsm >= 2) {
  23790. + au_fset_si(sbinfo, FHSM);
  23791. + for (bindex = bbot; bindex >= 0; bindex--) {
  23792. + br = au_sbr(sb, bindex);
  23793. + if (au_br_fhsm(br->br_perm)) {
  23794. + au_fhsm_set_bottom(sb, bindex);
  23795. + break;
  23796. + }
  23797. + }
  23798. + } else {
  23799. + au_fclr_si(sbinfo, FHSM);
  23800. + au_fhsm_set_bottom(sb, -1);
  23801. + }
  23802. +
  23803. + return err;
  23804. +}
  23805. +
  23806. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  23807. +{
  23808. + int err;
  23809. + unsigned int tmp;
  23810. + aufs_bindex_t bindex, bbot;
  23811. + struct au_opt *opt;
  23812. + struct au_opt_xino *opt_xino, xino;
  23813. + struct au_sbinfo *sbinfo;
  23814. + struct au_branch *br;
  23815. + struct inode *dir;
  23816. +
  23817. + SiMustWriteLock(sb);
  23818. +
  23819. + err = 0;
  23820. + opt_xino = NULL;
  23821. + opt = opts->opt;
  23822. + while (err >= 0 && opt->type != Opt_tail)
  23823. + err = au_opt_simple(sb, opt++, opts);
  23824. + if (err > 0)
  23825. + err = 0;
  23826. + else if (unlikely(err < 0))
  23827. + goto out;
  23828. +
  23829. + /* disable xino and udba temporary */
  23830. + sbinfo = au_sbi(sb);
  23831. + tmp = sbinfo->si_mntflags;
  23832. + au_opt_clr(sbinfo->si_mntflags, XINO);
  23833. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  23834. +
  23835. + opt = opts->opt;
  23836. + while (err >= 0 && opt->type != Opt_tail)
  23837. + err = au_opt_br(sb, opt++, opts);
  23838. + if (err > 0)
  23839. + err = 0;
  23840. + else if (unlikely(err < 0))
  23841. + goto out;
  23842. +
  23843. + bbot = au_sbbot(sb);
  23844. + if (unlikely(bbot < 0)) {
  23845. + err = -EINVAL;
  23846. + pr_err("no branches\n");
  23847. + goto out;
  23848. + }
  23849. +
  23850. + if (au_opt_test(tmp, XINO))
  23851. + au_opt_set(sbinfo->si_mntflags, XINO);
  23852. + opt = opts->opt;
  23853. + while (!err && opt->type != Opt_tail)
  23854. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  23855. + if (unlikely(err))
  23856. + goto out;
  23857. +
  23858. + err = au_opts_verify(sb, sb->s_flags, tmp);
  23859. + if (unlikely(err))
  23860. + goto out;
  23861. +
  23862. + /* restore xino */
  23863. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  23864. + xino.file = au_xino_def(sb);
  23865. + err = PTR_ERR(xino.file);
  23866. + if (IS_ERR(xino.file))
  23867. + goto out;
  23868. +
  23869. + err = au_xino_set(sb, &xino, /*remount*/0);
  23870. + fput(xino.file);
  23871. + if (unlikely(err))
  23872. + goto out;
  23873. + }
  23874. +
  23875. + /* restore udba */
  23876. + tmp &= AuOptMask_UDBA;
  23877. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  23878. + sbinfo->si_mntflags |= tmp;
  23879. + bbot = au_sbbot(sb);
  23880. + for (bindex = 0; bindex <= bbot; bindex++) {
  23881. + br = au_sbr(sb, bindex);
  23882. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  23883. + if (unlikely(err))
  23884. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  23885. + bindex, err);
  23886. + /* go on even if err */
  23887. + }
  23888. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  23889. + dir = d_inode(sb->s_root);
  23890. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  23891. + }
  23892. +
  23893. +out:
  23894. + return err;
  23895. +}
  23896. +
  23897. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  23898. +{
  23899. + int err, rerr;
  23900. + unsigned char no_dreval;
  23901. + struct inode *dir;
  23902. + struct au_opt_xino *opt_xino;
  23903. + struct au_opt *opt;
  23904. + struct au_sbinfo *sbinfo;
  23905. +
  23906. + SiMustWriteLock(sb);
  23907. +
  23908. + err = 0;
  23909. + dir = d_inode(sb->s_root);
  23910. + sbinfo = au_sbi(sb);
  23911. + opt_xino = NULL;
  23912. + opt = opts->opt;
  23913. + while (err >= 0 && opt->type != Opt_tail) {
  23914. + err = au_opt_simple(sb, opt, opts);
  23915. + if (!err)
  23916. + err = au_opt_br(sb, opt, opts);
  23917. + if (!err)
  23918. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  23919. + opt++;
  23920. + }
  23921. + if (err > 0)
  23922. + err = 0;
  23923. + AuTraceErr(err);
  23924. + /* go on even err */
  23925. +
  23926. + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
  23927. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  23928. + if (unlikely(rerr && !err))
  23929. + err = rerr;
  23930. +
  23931. + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
  23932. + au_fset_opts(opts->flags, REFRESH_IDOP);
  23933. +
  23934. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  23935. + rerr = au_xib_trunc(sb);
  23936. + if (unlikely(rerr && !err))
  23937. + err = rerr;
  23938. + }
  23939. +
  23940. + /* will be handled by the caller */
  23941. + if (!au_ftest_opts(opts->flags, REFRESH)
  23942. + && (opts->given_udba
  23943. + || au_opt_test(sbinfo->si_mntflags, XINO)
  23944. + || au_ftest_opts(opts->flags, REFRESH_IDOP)
  23945. + ))
  23946. + au_fset_opts(opts->flags, REFRESH);
  23947. +
  23948. + AuDbg("status 0x%x\n", opts->flags);
  23949. + return err;
  23950. +}
  23951. +
  23952. +/* ---------------------------------------------------------------------- */
  23953. +
  23954. +unsigned int au_opt_udba(struct super_block *sb)
  23955. +{
  23956. + return au_mntflags(sb) & AuOptMask_UDBA;
  23957. +}
  23958. --- /dev/null
  23959. +++ linux-4.4/fs/aufs/opts.h 2016-10-23 11:20:57.638826618 +0300
  23960. @@ -0,0 +1,211 @@
  23961. +/*
  23962. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  23963. + *
  23964. + * This program, aufs is free software; you can redistribute it and/or modify
  23965. + * it under the terms of the GNU General Public License as published by
  23966. + * the Free Software Foundation; either version 2 of the License, or
  23967. + * (at your option) any later version.
  23968. + *
  23969. + * This program is distributed in the hope that it will be useful,
  23970. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23971. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23972. + * GNU General Public License for more details.
  23973. + *
  23974. + * You should have received a copy of the GNU General Public License
  23975. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23976. + */
  23977. +
  23978. +/*
  23979. + * mount options/flags
  23980. + */
  23981. +
  23982. +#ifndef __AUFS_OPTS_H__
  23983. +#define __AUFS_OPTS_H__
  23984. +
  23985. +#ifdef __KERNEL__
  23986. +
  23987. +#include <linux/path.h>
  23988. +
  23989. +struct file;
  23990. +struct super_block;
  23991. +
  23992. +/* ---------------------------------------------------------------------- */
  23993. +
  23994. +/* mount flags */
  23995. +#define AuOpt_XINO 1 /* external inode number bitmap
  23996. + and translation table */
  23997. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  23998. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  23999. +#define AuOpt_UDBA_REVAL (1 << 3)
  24000. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  24001. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  24002. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  24003. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  24004. + bits */
  24005. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  24006. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  24007. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  24008. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  24009. +#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
  24010. +#define AuOpt_DIO (1 << 14) /* direct io */
  24011. +
  24012. +#ifndef CONFIG_AUFS_HNOTIFY
  24013. +#undef AuOpt_UDBA_HNOTIFY
  24014. +#define AuOpt_UDBA_HNOTIFY 0
  24015. +#endif
  24016. +#ifndef CONFIG_AUFS_SHWH
  24017. +#undef AuOpt_SHWH
  24018. +#define AuOpt_SHWH 0
  24019. +#endif
  24020. +
  24021. +#define AuOpt_Def (AuOpt_XINO \
  24022. + | AuOpt_UDBA_REVAL \
  24023. + | AuOpt_PLINK \
  24024. + /* | AuOpt_DIRPERM1 */ \
  24025. + | AuOpt_WARN_PERM)
  24026. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  24027. + | AuOpt_UDBA_REVAL \
  24028. + | AuOpt_UDBA_HNOTIFY)
  24029. +
  24030. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  24031. +#define au_opt_set(flags, name) do { \
  24032. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  24033. + ((flags) |= AuOpt_##name); \
  24034. +} while (0)
  24035. +#define au_opt_set_udba(flags, name) do { \
  24036. + (flags) &= ~AuOptMask_UDBA; \
  24037. + ((flags) |= AuOpt_##name); \
  24038. +} while (0)
  24039. +#define au_opt_clr(flags, name) do { \
  24040. + ((flags) &= ~AuOpt_##name); \
  24041. +} while (0)
  24042. +
  24043. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  24044. +{
  24045. +#ifdef CONFIG_PROC_FS
  24046. + return mntflags;
  24047. +#else
  24048. + return mntflags & ~AuOpt_PLINK;
  24049. +#endif
  24050. +}
  24051. +
  24052. +/* ---------------------------------------------------------------------- */
  24053. +
  24054. +/* policies to select one among multiple writable branches */
  24055. +enum {
  24056. + AuWbrCreate_TDP, /* top down parent */
  24057. + AuWbrCreate_RR, /* round robin */
  24058. + AuWbrCreate_MFS, /* most free space */
  24059. + AuWbrCreate_MFSV, /* mfs with seconds */
  24060. + AuWbrCreate_MFSRR, /* mfs then rr */
  24061. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  24062. + AuWbrCreate_PMFS, /* parent and mfs */
  24063. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  24064. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  24065. + AuWbrCreate_PMFSRRV, /* plus seconds */
  24066. +
  24067. + AuWbrCreate_Def = AuWbrCreate_TDP
  24068. +};
  24069. +
  24070. +enum {
  24071. + AuWbrCopyup_TDP, /* top down parent */
  24072. + AuWbrCopyup_BUP, /* bottom up parent */
  24073. + AuWbrCopyup_BU, /* bottom up */
  24074. +
  24075. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  24076. +};
  24077. +
  24078. +/* ---------------------------------------------------------------------- */
  24079. +
  24080. +struct au_opt_add {
  24081. + aufs_bindex_t bindex;
  24082. + char *pathname;
  24083. + int perm;
  24084. + struct path path;
  24085. +};
  24086. +
  24087. +struct au_opt_del {
  24088. + char *pathname;
  24089. + struct path h_path;
  24090. +};
  24091. +
  24092. +struct au_opt_mod {
  24093. + char *path;
  24094. + int perm;
  24095. + struct dentry *h_root;
  24096. +};
  24097. +
  24098. +struct au_opt_xino {
  24099. + char *path;
  24100. + struct file *file;
  24101. +};
  24102. +
  24103. +struct au_opt_xino_itrunc {
  24104. + aufs_bindex_t bindex;
  24105. +};
  24106. +
  24107. +struct au_opt_wbr_create {
  24108. + int wbr_create;
  24109. + int mfs_second;
  24110. + unsigned long long mfsrr_watermark;
  24111. +};
  24112. +
  24113. +struct au_opt {
  24114. + int type;
  24115. + union {
  24116. + struct au_opt_xino xino;
  24117. + struct au_opt_xino_itrunc xino_itrunc;
  24118. + struct au_opt_add add;
  24119. + struct au_opt_del del;
  24120. + struct au_opt_mod mod;
  24121. + int dirwh;
  24122. + int rdcache;
  24123. + unsigned int rdblk;
  24124. + unsigned int rdhash;
  24125. + int udba;
  24126. + struct au_opt_wbr_create wbr_create;
  24127. + int wbr_copyup;
  24128. + unsigned int fhsm_second;
  24129. + };
  24130. +};
  24131. +
  24132. +/* opts flags */
  24133. +#define AuOpts_REMOUNT 1
  24134. +#define AuOpts_REFRESH (1 << 1)
  24135. +#define AuOpts_TRUNC_XIB (1 << 2)
  24136. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  24137. +#define AuOpts_REFRESH_IDOP (1 << 4)
  24138. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  24139. +#define au_fset_opts(flags, name) \
  24140. + do { (flags) |= AuOpts_##name; } while (0)
  24141. +#define au_fclr_opts(flags, name) \
  24142. + do { (flags) &= ~AuOpts_##name; } while (0)
  24143. +
  24144. +struct au_opts {
  24145. + struct au_opt *opt;
  24146. + int max_opt;
  24147. +
  24148. + unsigned int given_udba;
  24149. + unsigned int flags;
  24150. + unsigned long sb_flags;
  24151. +};
  24152. +
  24153. +/* ---------------------------------------------------------------------- */
  24154. +
  24155. +/* opts.c */
  24156. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  24157. +const char *au_optstr_udba(int udba);
  24158. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  24159. +const char *au_optstr_wbr_create(int wbr_create);
  24160. +
  24161. +void au_opts_free(struct au_opts *opts);
  24162. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  24163. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  24164. + unsigned int pending);
  24165. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  24166. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  24167. +
  24168. +unsigned int au_opt_udba(struct super_block *sb);
  24169. +
  24170. +#endif /* __KERNEL__ */
  24171. +#endif /* __AUFS_OPTS_H__ */
  24172. --- /dev/null
  24173. +++ linux-4.4/fs/aufs/plink.c 2016-10-23 11:20:57.638826618 +0300
  24174. @@ -0,0 +1,514 @@
  24175. +/*
  24176. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  24177. + *
  24178. + * This program, aufs is free software; you can redistribute it and/or modify
  24179. + * it under the terms of the GNU General Public License as published by
  24180. + * the Free Software Foundation; either version 2 of the License, or
  24181. + * (at your option) any later version.
  24182. + *
  24183. + * This program is distributed in the hope that it will be useful,
  24184. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24185. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24186. + * GNU General Public License for more details.
  24187. + *
  24188. + * You should have received a copy of the GNU General Public License
  24189. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24190. + */
  24191. +
  24192. +/*
  24193. + * pseudo-link
  24194. + */
  24195. +
  24196. +#include "aufs.h"
  24197. +
  24198. +/*
  24199. + * the pseudo-link maintenance mode.
  24200. + * during a user process maintains the pseudo-links,
  24201. + * prohibit adding a new plink and branch manipulation.
  24202. + *
  24203. + * Flags
  24204. + * NOPLM:
  24205. + * For entry functions which will handle plink, and i_mutex is already held
  24206. + * in VFS.
  24207. + * They cannot wait and should return an error at once.
  24208. + * Callers has to check the error.
  24209. + * NOPLMW:
  24210. + * For entry functions which will handle plink, but i_mutex is not held
  24211. + * in VFS.
  24212. + * They can wait the plink maintenance mode to finish.
  24213. + *
  24214. + * They behave like F_SETLK and F_SETLKW.
  24215. + * If the caller never handle plink, then both flags are unnecessary.
  24216. + */
  24217. +
  24218. +int au_plink_maint(struct super_block *sb, int flags)
  24219. +{
  24220. + int err;
  24221. + pid_t pid, ppid;
  24222. + struct task_struct *parent, *prev;
  24223. + struct au_sbinfo *sbi;
  24224. +
  24225. + SiMustAnyLock(sb);
  24226. +
  24227. + err = 0;
  24228. + if (!au_opt_test(au_mntflags(sb), PLINK))
  24229. + goto out;
  24230. +
  24231. + sbi = au_sbi(sb);
  24232. + pid = sbi->si_plink_maint_pid;
  24233. + if (!pid || pid == current->pid)
  24234. + goto out;
  24235. +
  24236. + /* todo: it highly depends upon /sbin/mount.aufs */
  24237. + prev = NULL;
  24238. + parent = current;
  24239. + ppid = 0;
  24240. + rcu_read_lock();
  24241. + while (1) {
  24242. + parent = rcu_dereference(parent->real_parent);
  24243. + if (parent == prev)
  24244. + break;
  24245. + ppid = task_pid_vnr(parent);
  24246. + if (pid == ppid) {
  24247. + rcu_read_unlock();
  24248. + goto out;
  24249. + }
  24250. + prev = parent;
  24251. + }
  24252. + rcu_read_unlock();
  24253. +
  24254. + if (au_ftest_lock(flags, NOPLMW)) {
  24255. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  24256. + /* AuDebugOn(!lockdep_depth(current)); */
  24257. + while (sbi->si_plink_maint_pid) {
  24258. + si_read_unlock(sb);
  24259. + /* gave up wake_up_bit() */
  24260. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  24261. +
  24262. + if (au_ftest_lock(flags, FLUSH))
  24263. + au_nwt_flush(&sbi->si_nowait);
  24264. + si_noflush_read_lock(sb);
  24265. + }
  24266. + } else if (au_ftest_lock(flags, NOPLM)) {
  24267. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  24268. + err = -EAGAIN;
  24269. + }
  24270. +
  24271. +out:
  24272. + return err;
  24273. +}
  24274. +
  24275. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  24276. +{
  24277. + spin_lock(&sbinfo->si_plink_maint_lock);
  24278. + sbinfo->si_plink_maint_pid = 0;
  24279. + spin_unlock(&sbinfo->si_plink_maint_lock);
  24280. + wake_up_all(&sbinfo->si_plink_wq);
  24281. +}
  24282. +
  24283. +int au_plink_maint_enter(struct super_block *sb)
  24284. +{
  24285. + int err;
  24286. + struct au_sbinfo *sbinfo;
  24287. +
  24288. + err = 0;
  24289. + sbinfo = au_sbi(sb);
  24290. + /* make sure i am the only one in this fs */
  24291. + si_write_lock(sb, AuLock_FLUSH);
  24292. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  24293. + spin_lock(&sbinfo->si_plink_maint_lock);
  24294. + if (!sbinfo->si_plink_maint_pid)
  24295. + sbinfo->si_plink_maint_pid = current->pid;
  24296. + else
  24297. + err = -EBUSY;
  24298. + spin_unlock(&sbinfo->si_plink_maint_lock);
  24299. + }
  24300. + si_write_unlock(sb);
  24301. +
  24302. + return err;
  24303. +}
  24304. +
  24305. +/* ---------------------------------------------------------------------- */
  24306. +
  24307. +#ifdef CONFIG_AUFS_DEBUG
  24308. +void au_plink_list(struct super_block *sb)
  24309. +{
  24310. + int i;
  24311. + struct au_sbinfo *sbinfo;
  24312. + struct hlist_head *plink_hlist;
  24313. + struct au_icntnr *icntnr;
  24314. +
  24315. + SiMustAnyLock(sb);
  24316. +
  24317. + sbinfo = au_sbi(sb);
  24318. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24319. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24320. +
  24321. + for (i = 0; i < AuPlink_NHASH; i++) {
  24322. + plink_hlist = &sbinfo->si_plink[i].head;
  24323. + rcu_read_lock();
  24324. + hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
  24325. + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
  24326. + rcu_read_unlock();
  24327. + }
  24328. +}
  24329. +#endif
  24330. +
  24331. +/* is the inode pseudo-linked? */
  24332. +int au_plink_test(struct inode *inode)
  24333. +{
  24334. + int found, i;
  24335. + struct au_sbinfo *sbinfo;
  24336. + struct hlist_head *plink_hlist;
  24337. + struct au_icntnr *icntnr;
  24338. +
  24339. + sbinfo = au_sbi(inode->i_sb);
  24340. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  24341. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  24342. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  24343. +
  24344. + found = 0;
  24345. + i = au_plink_hash(inode->i_ino);
  24346. + plink_hlist = &sbinfo->si_plink[i].head;
  24347. + rcu_read_lock();
  24348. + hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
  24349. + if (&icntnr->vfs_inode == inode) {
  24350. + found = 1;
  24351. + break;
  24352. + }
  24353. + rcu_read_unlock();
  24354. + return found;
  24355. +}
  24356. +
  24357. +/* ---------------------------------------------------------------------- */
  24358. +
  24359. +/*
  24360. + * generate a name for plink.
  24361. + * the file will be stored under AUFS_WH_PLINKDIR.
  24362. + */
  24363. +/* 20 is max digits length of ulong 64 */
  24364. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  24365. +
  24366. +static int plink_name(char *name, int len, struct inode *inode,
  24367. + aufs_bindex_t bindex)
  24368. +{
  24369. + int rlen;
  24370. + struct inode *h_inode;
  24371. +
  24372. + h_inode = au_h_iptr(inode, bindex);
  24373. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  24374. + return rlen;
  24375. +}
  24376. +
  24377. +struct au_do_plink_lkup_args {
  24378. + struct dentry **errp;
  24379. + struct qstr *tgtname;
  24380. + struct dentry *h_parent;
  24381. + struct au_branch *br;
  24382. +};
  24383. +
  24384. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  24385. + struct dentry *h_parent,
  24386. + struct au_branch *br)
  24387. +{
  24388. + struct dentry *h_dentry;
  24389. + struct mutex *h_mtx;
  24390. +
  24391. + h_mtx = &d_inode(h_parent)->i_mutex;
  24392. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  24393. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  24394. + mutex_unlock(h_mtx);
  24395. + return h_dentry;
  24396. +}
  24397. +
  24398. +static void au_call_do_plink_lkup(void *args)
  24399. +{
  24400. + struct au_do_plink_lkup_args *a = args;
  24401. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  24402. +}
  24403. +
  24404. +/* lookup the plink-ed @inode under the branch at @bindex */
  24405. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  24406. +{
  24407. + struct dentry *h_dentry, *h_parent;
  24408. + struct au_branch *br;
  24409. + int wkq_err;
  24410. + char a[PLINK_NAME_LEN];
  24411. + struct qstr tgtname = QSTR_INIT(a, 0);
  24412. +
  24413. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  24414. +
  24415. + br = au_sbr(inode->i_sb, bindex);
  24416. + h_parent = br->br_wbr->wbr_plink;
  24417. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  24418. +
  24419. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  24420. + struct au_do_plink_lkup_args args = {
  24421. + .errp = &h_dentry,
  24422. + .tgtname = &tgtname,
  24423. + .h_parent = h_parent,
  24424. + .br = br
  24425. + };
  24426. +
  24427. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  24428. + if (unlikely(wkq_err))
  24429. + h_dentry = ERR_PTR(wkq_err);
  24430. + } else
  24431. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  24432. +
  24433. + return h_dentry;
  24434. +}
  24435. +
  24436. +/* create a pseudo-link */
  24437. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  24438. + struct dentry *h_dentry, struct au_branch *br)
  24439. +{
  24440. + int err;
  24441. + struct path h_path = {
  24442. + .mnt = au_br_mnt(br)
  24443. + };
  24444. + struct inode *h_dir, *delegated;
  24445. +
  24446. + h_dir = d_inode(h_parent);
  24447. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
  24448. +again:
  24449. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  24450. + err = PTR_ERR(h_path.dentry);
  24451. + if (IS_ERR(h_path.dentry))
  24452. + goto out;
  24453. +
  24454. + err = 0;
  24455. + /* wh.plink dir is not monitored */
  24456. + /* todo: is it really safe? */
  24457. + if (d_is_positive(h_path.dentry)
  24458. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  24459. + delegated = NULL;
  24460. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  24461. + if (unlikely(err == -EWOULDBLOCK)) {
  24462. + pr_warn("cannot retry for NFSv4 delegation"
  24463. + " for an internal unlink\n");
  24464. + iput(delegated);
  24465. + }
  24466. + dput(h_path.dentry);
  24467. + h_path.dentry = NULL;
  24468. + if (!err)
  24469. + goto again;
  24470. + }
  24471. + if (!err && d_is_negative(h_path.dentry)) {
  24472. + delegated = NULL;
  24473. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  24474. + if (unlikely(err == -EWOULDBLOCK)) {
  24475. + pr_warn("cannot retry for NFSv4 delegation"
  24476. + " for an internal link\n");
  24477. + iput(delegated);
  24478. + }
  24479. + }
  24480. + dput(h_path.dentry);
  24481. +
  24482. +out:
  24483. + mutex_unlock(&h_dir->i_mutex);
  24484. + return err;
  24485. +}
  24486. +
  24487. +struct do_whplink_args {
  24488. + int *errp;
  24489. + struct qstr *tgt;
  24490. + struct dentry *h_parent;
  24491. + struct dentry *h_dentry;
  24492. + struct au_branch *br;
  24493. +};
  24494. +
  24495. +static void call_do_whplink(void *args)
  24496. +{
  24497. + struct do_whplink_args *a = args;
  24498. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  24499. +}
  24500. +
  24501. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  24502. + aufs_bindex_t bindex, struct au_branch *br)
  24503. +{
  24504. + int err, wkq_err;
  24505. + struct au_wbr *wbr;
  24506. + struct dentry *h_parent;
  24507. + char a[PLINK_NAME_LEN];
  24508. + struct qstr tgtname = QSTR_INIT(a, 0);
  24509. +
  24510. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  24511. + h_parent = wbr->wbr_plink;
  24512. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  24513. +
  24514. + /* always superio. */
  24515. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  24516. + struct do_whplink_args args = {
  24517. + .errp = &err,
  24518. + .tgt = &tgtname,
  24519. + .h_parent = h_parent,
  24520. + .h_dentry = h_dentry,
  24521. + .br = br
  24522. + };
  24523. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  24524. + if (unlikely(wkq_err))
  24525. + err = wkq_err;
  24526. + } else
  24527. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  24528. +
  24529. + return err;
  24530. +}
  24531. +
  24532. +/*
  24533. + * create a new pseudo-link for @h_dentry on @bindex.
  24534. + * the linked inode is held in aufs @inode.
  24535. + */
  24536. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  24537. + struct dentry *h_dentry)
  24538. +{
  24539. + struct super_block *sb;
  24540. + struct au_sbinfo *sbinfo;
  24541. + struct hlist_head *plink_hlist;
  24542. + struct au_icntnr *icntnr;
  24543. + struct au_sphlhead *sphl;
  24544. + int found, err, cnt, i;
  24545. +
  24546. + sb = inode->i_sb;
  24547. + sbinfo = au_sbi(sb);
  24548. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24549. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24550. +
  24551. + found = au_plink_test(inode);
  24552. + if (found)
  24553. + return;
  24554. +
  24555. + i = au_plink_hash(inode->i_ino);
  24556. + sphl = sbinfo->si_plink + i;
  24557. + plink_hlist = &sphl->head;
  24558. + au_igrab(inode);
  24559. +
  24560. + spin_lock(&sphl->spin);
  24561. + hlist_for_each_entry(icntnr, plink_hlist, plink) {
  24562. + if (&icntnr->vfs_inode == inode) {
  24563. + found = 1;
  24564. + break;
  24565. + }
  24566. + }
  24567. + if (!found) {
  24568. + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
  24569. + hlist_add_head_rcu(&icntnr->plink, plink_hlist);
  24570. + }
  24571. + spin_unlock(&sphl->spin);
  24572. + if (!found) {
  24573. + cnt = au_sphl_count(sphl);
  24574. +#define msg "unexpectedly unblanced or too many pseudo-links"
  24575. + if (cnt > AUFS_PLINK_WARN)
  24576. + AuWarn1(msg ", %d\n", cnt);
  24577. +#undef msg
  24578. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  24579. + if (unlikely(err)) {
  24580. + pr_warn("err %d, damaged pseudo link.\n", err);
  24581. + au_sphl_del_rcu(&icntnr->plink, sphl);
  24582. + iput(&icntnr->vfs_inode);
  24583. + }
  24584. + } else
  24585. + iput(&icntnr->vfs_inode);
  24586. +}
  24587. +
  24588. +/* free all plinks */
  24589. +void au_plink_put(struct super_block *sb, int verbose)
  24590. +{
  24591. + int i, warned;
  24592. + struct au_sbinfo *sbinfo;
  24593. + struct hlist_head *plink_hlist;
  24594. + struct hlist_node *tmp;
  24595. + struct au_icntnr *icntnr;
  24596. +
  24597. + SiMustWriteLock(sb);
  24598. +
  24599. + sbinfo = au_sbi(sb);
  24600. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24601. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24602. +
  24603. + /* no spin_lock since sbinfo is write-locked */
  24604. + warned = 0;
  24605. + for (i = 0; i < AuPlink_NHASH; i++) {
  24606. + plink_hlist = &sbinfo->si_plink[i].head;
  24607. + if (!warned && verbose && !hlist_empty(plink_hlist)) {
  24608. + pr_warn("pseudo-link is not flushed");
  24609. + warned = 1;
  24610. + }
  24611. + hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink)
  24612. + iput(&icntnr->vfs_inode);
  24613. + INIT_HLIST_HEAD(plink_hlist);
  24614. + }
  24615. +}
  24616. +
  24617. +void au_plink_clean(struct super_block *sb, int verbose)
  24618. +{
  24619. + struct dentry *root;
  24620. +
  24621. + root = sb->s_root;
  24622. + aufs_write_lock(root);
  24623. + if (au_opt_test(au_mntflags(sb), PLINK))
  24624. + au_plink_put(sb, verbose);
  24625. + aufs_write_unlock(root);
  24626. +}
  24627. +
  24628. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  24629. +{
  24630. + int do_put;
  24631. + aufs_bindex_t btop, bbot, bindex;
  24632. +
  24633. + do_put = 0;
  24634. + btop = au_ibtop(inode);
  24635. + bbot = au_ibbot(inode);
  24636. + if (btop >= 0) {
  24637. + for (bindex = btop; bindex <= bbot; bindex++) {
  24638. + if (!au_h_iptr(inode, bindex)
  24639. + || au_ii_br_id(inode, bindex) != br_id)
  24640. + continue;
  24641. + au_set_h_iptr(inode, bindex, NULL, 0);
  24642. + do_put = 1;
  24643. + break;
  24644. + }
  24645. + if (do_put)
  24646. + for (bindex = btop; bindex <= bbot; bindex++)
  24647. + if (au_h_iptr(inode, bindex)) {
  24648. + do_put = 0;
  24649. + break;
  24650. + }
  24651. + } else
  24652. + do_put = 1;
  24653. +
  24654. + return do_put;
  24655. +}
  24656. +
  24657. +/* free the plinks on a branch specified by @br_id */
  24658. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  24659. +{
  24660. + struct au_sbinfo *sbinfo;
  24661. + struct hlist_head *plink_hlist;
  24662. + struct hlist_node *tmp;
  24663. + struct au_icntnr *icntnr;
  24664. + struct inode *inode;
  24665. + int i, do_put;
  24666. +
  24667. + SiMustWriteLock(sb);
  24668. +
  24669. + sbinfo = au_sbi(sb);
  24670. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24671. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24672. +
  24673. + /* no spin_lock since sbinfo is write-locked */
  24674. + for (i = 0; i < AuPlink_NHASH; i++) {
  24675. + plink_hlist = &sbinfo->si_plink[i].head;
  24676. + hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink) {
  24677. + inode = au_igrab(&icntnr->vfs_inode);
  24678. + ii_write_lock_child(inode);
  24679. + do_put = au_plink_do_half_refresh(inode, br_id);
  24680. + if (do_put) {
  24681. + hlist_del(&icntnr->plink);
  24682. + iput(inode);
  24683. + }
  24684. + ii_write_unlock(inode);
  24685. + iput(inode);
  24686. + }
  24687. + }
  24688. +}
  24689. --- /dev/null
  24690. +++ linux-4.4/fs/aufs/poll.c 2016-10-23 11:20:57.638826618 +0300
  24691. @@ -0,0 +1,52 @@
  24692. +/*
  24693. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  24694. + *
  24695. + * This program, aufs is free software; you can redistribute it and/or modify
  24696. + * it under the terms of the GNU General Public License as published by
  24697. + * the Free Software Foundation; either version 2 of the License, or
  24698. + * (at your option) any later version.
  24699. + *
  24700. + * This program is distributed in the hope that it will be useful,
  24701. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24702. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24703. + * GNU General Public License for more details.
  24704. + *
  24705. + * You should have received a copy of the GNU General Public License
  24706. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24707. + */
  24708. +
  24709. +/*
  24710. + * poll operation
  24711. + * There is only one filesystem which implements ->poll operation, currently.
  24712. + */
  24713. +
  24714. +#include "aufs.h"
  24715. +
  24716. +unsigned int aufs_poll(struct file *file, poll_table *wait)
  24717. +{
  24718. + unsigned int mask;
  24719. + int err;
  24720. + struct file *h_file;
  24721. + struct super_block *sb;
  24722. +
  24723. + /* We should pretend an error happened. */
  24724. + mask = POLLERR /* | POLLIN | POLLOUT */;
  24725. + sb = file->f_path.dentry->d_sb;
  24726. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  24727. +
  24728. + h_file = au_read_pre(file, /*keep_fi*/0);
  24729. + err = PTR_ERR(h_file);
  24730. + if (IS_ERR(h_file))
  24731. + goto out;
  24732. +
  24733. + /* it is not an error if h_file has no operation */
  24734. + mask = DEFAULT_POLLMASK;
  24735. + if (h_file->f_op->poll)
  24736. + mask = h_file->f_op->poll(h_file, wait);
  24737. + fput(h_file); /* instead of au_read_post() */
  24738. +
  24739. +out:
  24740. + si_read_unlock(sb);
  24741. + AuTraceErr((int)mask);
  24742. + return mask;
  24743. +}
  24744. --- /dev/null
  24745. +++ linux-4.4/fs/aufs/posix_acl.c 2016-10-23 11:20:57.638826618 +0300
  24746. @@ -0,0 +1,98 @@
  24747. +/*
  24748. + * Copyright (C) 2014-2016 Junjiro R. Okajima
  24749. + *
  24750. + * This program, aufs is free software; you can redistribute it and/or modify
  24751. + * it under the terms of the GNU General Public License as published by
  24752. + * the Free Software Foundation; either version 2 of the License, or
  24753. + * (at your option) any later version.
  24754. + *
  24755. + * This program is distributed in the hope that it will be useful,
  24756. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24757. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24758. + * GNU General Public License for more details.
  24759. + *
  24760. + * You should have received a copy of the GNU General Public License
  24761. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24762. + */
  24763. +
  24764. +/*
  24765. + * posix acl operations
  24766. + */
  24767. +
  24768. +#include <linux/fs.h>
  24769. +#include "aufs.h"
  24770. +
  24771. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  24772. +{
  24773. + struct posix_acl *acl;
  24774. + int err;
  24775. + aufs_bindex_t bindex;
  24776. + struct inode *h_inode;
  24777. + struct super_block *sb;
  24778. +
  24779. + acl = NULL;
  24780. + sb = inode->i_sb;
  24781. + si_read_lock(sb, AuLock_FLUSH);
  24782. + ii_read_lock_child(inode);
  24783. + if (!(sb->s_flags & MS_POSIXACL))
  24784. + goto out;
  24785. +
  24786. + bindex = au_ibtop(inode);
  24787. + h_inode = au_h_iptr(inode, bindex);
  24788. + if (unlikely(!h_inode
  24789. + || ((h_inode->i_mode & S_IFMT)
  24790. + != (inode->i_mode & S_IFMT)))) {
  24791. + err = au_busy_or_stale();
  24792. + acl = ERR_PTR(err);
  24793. + goto out;
  24794. + }
  24795. +
  24796. + /* always topmost only */
  24797. + acl = get_acl(h_inode, type);
  24798. +
  24799. +out:
  24800. + ii_read_unlock(inode);
  24801. + si_read_unlock(sb);
  24802. +
  24803. + AuTraceErrPtr(acl);
  24804. + return acl;
  24805. +}
  24806. +
  24807. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  24808. +{
  24809. + int err;
  24810. + ssize_t ssz;
  24811. + struct dentry *dentry;
  24812. + struct au_srxattr arg = {
  24813. + .type = AU_ACL_SET,
  24814. + .u.acl_set = {
  24815. + .acl = acl,
  24816. + .type = type
  24817. + },
  24818. + };
  24819. +
  24820. + mutex_lock(&inode->i_mutex);
  24821. + if (inode->i_ino == AUFS_ROOT_INO)
  24822. + dentry = dget(inode->i_sb->s_root);
  24823. + else {
  24824. + dentry = d_find_alias(inode);
  24825. + if (!dentry)
  24826. + dentry = d_find_any_alias(inode);
  24827. + if (!dentry) {
  24828. + pr_warn("cannot handle this inode, "
  24829. + "please report to aufs-users ML\n");
  24830. + err = -ENOENT;
  24831. + goto out;
  24832. + }
  24833. + }
  24834. +
  24835. + ssz = au_srxattr(dentry, &arg);
  24836. + dput(dentry);
  24837. + err = ssz;
  24838. + if (ssz >= 0)
  24839. + err = 0;
  24840. +
  24841. +out:
  24842. + mutex_unlock(&inode->i_mutex);
  24843. + return err;
  24844. +}
  24845. --- /dev/null
  24846. +++ linux-4.4/fs/aufs/procfs.c 2016-10-23 11:20:57.638826618 +0300
  24847. @@ -0,0 +1,169 @@
  24848. +/*
  24849. + * Copyright (C) 2010-2016 Junjiro R. Okajima
  24850. + *
  24851. + * This program, aufs is free software; you can redistribute it and/or modify
  24852. + * it under the terms of the GNU General Public License as published by
  24853. + * the Free Software Foundation; either version 2 of the License, or
  24854. + * (at your option) any later version.
  24855. + *
  24856. + * This program is distributed in the hope that it will be useful,
  24857. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24858. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24859. + * GNU General Public License for more details.
  24860. + *
  24861. + * You should have received a copy of the GNU General Public License
  24862. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24863. + */
  24864. +
  24865. +/*
  24866. + * procfs interfaces
  24867. + */
  24868. +
  24869. +#include <linux/proc_fs.h>
  24870. +#include "aufs.h"
  24871. +
  24872. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  24873. +{
  24874. + struct au_sbinfo *sbinfo;
  24875. +
  24876. + sbinfo = file->private_data;
  24877. + if (sbinfo) {
  24878. + au_plink_maint_leave(sbinfo);
  24879. + kobject_put(&sbinfo->si_kobj);
  24880. + }
  24881. +
  24882. + return 0;
  24883. +}
  24884. +
  24885. +static void au_procfs_plm_write_clean(struct file *file)
  24886. +{
  24887. + struct au_sbinfo *sbinfo;
  24888. +
  24889. + sbinfo = file->private_data;
  24890. + if (sbinfo)
  24891. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  24892. +}
  24893. +
  24894. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  24895. +{
  24896. + int err;
  24897. + struct super_block *sb;
  24898. + struct au_sbinfo *sbinfo;
  24899. +
  24900. + err = -EBUSY;
  24901. + if (unlikely(file->private_data))
  24902. + goto out;
  24903. +
  24904. + sb = NULL;
  24905. + /* don't use au_sbilist_lock() here */
  24906. + spin_lock(&au_sbilist.spin);
  24907. + hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  24908. + if (id == sysaufs_si_id(sbinfo)) {
  24909. + kobject_get(&sbinfo->si_kobj);
  24910. + sb = sbinfo->si_sb;
  24911. + break;
  24912. + }
  24913. + spin_unlock(&au_sbilist.spin);
  24914. +
  24915. + err = -EINVAL;
  24916. + if (unlikely(!sb))
  24917. + goto out;
  24918. +
  24919. + err = au_plink_maint_enter(sb);
  24920. + if (!err)
  24921. + /* keep kobject_get() */
  24922. + file->private_data = sbinfo;
  24923. + else
  24924. + kobject_put(&sbinfo->si_kobj);
  24925. +out:
  24926. + return err;
  24927. +}
  24928. +
  24929. +/*
  24930. + * Accept a valid "si=xxxx" only.
  24931. + * Once it is accepted successfully, accept "clean" too.
  24932. + */
  24933. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  24934. + size_t count, loff_t *ppos)
  24935. +{
  24936. + ssize_t err;
  24937. + unsigned long id;
  24938. + /* last newline is allowed */
  24939. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  24940. +
  24941. + err = -EACCES;
  24942. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  24943. + goto out;
  24944. +
  24945. + err = -EINVAL;
  24946. + if (unlikely(count > sizeof(buf)))
  24947. + goto out;
  24948. +
  24949. + err = copy_from_user(buf, ubuf, count);
  24950. + if (unlikely(err)) {
  24951. + err = -EFAULT;
  24952. + goto out;
  24953. + }
  24954. + buf[count] = 0;
  24955. +
  24956. + err = -EINVAL;
  24957. + if (!strcmp("clean", buf)) {
  24958. + au_procfs_plm_write_clean(file);
  24959. + goto out_success;
  24960. + } else if (unlikely(strncmp("si=", buf, 3)))
  24961. + goto out;
  24962. +
  24963. + err = kstrtoul(buf + 3, 16, &id);
  24964. + if (unlikely(err))
  24965. + goto out;
  24966. +
  24967. + err = au_procfs_plm_write_si(file, id);
  24968. + if (unlikely(err))
  24969. + goto out;
  24970. +
  24971. +out_success:
  24972. + err = count; /* success */
  24973. +out:
  24974. + return err;
  24975. +}
  24976. +
  24977. +static const struct file_operations au_procfs_plm_fop = {
  24978. + .write = au_procfs_plm_write,
  24979. + .release = au_procfs_plm_release,
  24980. + .owner = THIS_MODULE
  24981. +};
  24982. +
  24983. +/* ---------------------------------------------------------------------- */
  24984. +
  24985. +static struct proc_dir_entry *au_procfs_dir;
  24986. +
  24987. +void au_procfs_fin(void)
  24988. +{
  24989. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  24990. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  24991. +}
  24992. +
  24993. +int __init au_procfs_init(void)
  24994. +{
  24995. + int err;
  24996. + struct proc_dir_entry *entry;
  24997. +
  24998. + err = -ENOMEM;
  24999. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  25000. + if (unlikely(!au_procfs_dir))
  25001. + goto out;
  25002. +
  25003. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
  25004. + au_procfs_dir, &au_procfs_plm_fop);
  25005. + if (unlikely(!entry))
  25006. + goto out_dir;
  25007. +
  25008. + err = 0;
  25009. + goto out; /* success */
  25010. +
  25011. +
  25012. +out_dir:
  25013. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  25014. +out:
  25015. + return err;
  25016. +}
  25017. --- /dev/null
  25018. +++ linux-4.4/fs/aufs/rdu.c 2016-10-23 11:20:57.638826618 +0300
  25019. @@ -0,0 +1,388 @@
  25020. +/*
  25021. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25022. + *
  25023. + * This program, aufs is free software; you can redistribute it and/or modify
  25024. + * it under the terms of the GNU General Public License as published by
  25025. + * the Free Software Foundation; either version 2 of the License, or
  25026. + * (at your option) any later version.
  25027. + *
  25028. + * This program is distributed in the hope that it will be useful,
  25029. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25030. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25031. + * GNU General Public License for more details.
  25032. + *
  25033. + * You should have received a copy of the GNU General Public License
  25034. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25035. + */
  25036. +
  25037. +/*
  25038. + * readdir in userspace.
  25039. + */
  25040. +
  25041. +#include <linux/compat.h>
  25042. +#include <linux/fs_stack.h>
  25043. +#include <linux/security.h>
  25044. +#include "aufs.h"
  25045. +
  25046. +/* bits for struct aufs_rdu.flags */
  25047. +#define AuRdu_CALLED 1
  25048. +#define AuRdu_CONT (1 << 1)
  25049. +#define AuRdu_FULL (1 << 2)
  25050. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  25051. +#define au_fset_rdu(flags, name) \
  25052. + do { (flags) |= AuRdu_##name; } while (0)
  25053. +#define au_fclr_rdu(flags, name) \
  25054. + do { (flags) &= ~AuRdu_##name; } while (0)
  25055. +
  25056. +struct au_rdu_arg {
  25057. + struct dir_context ctx;
  25058. + struct aufs_rdu *rdu;
  25059. + union au_rdu_ent_ul ent;
  25060. + unsigned long end;
  25061. +
  25062. + struct super_block *sb;
  25063. + int err;
  25064. +};
  25065. +
  25066. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  25067. + loff_t offset, u64 h_ino, unsigned int d_type)
  25068. +{
  25069. + int err, len;
  25070. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  25071. + struct aufs_rdu *rdu = arg->rdu;
  25072. + struct au_rdu_ent ent;
  25073. +
  25074. + err = 0;
  25075. + arg->err = 0;
  25076. + au_fset_rdu(rdu->cookie.flags, CALLED);
  25077. + len = au_rdu_len(nlen);
  25078. + if (arg->ent.ul + len < arg->end) {
  25079. + ent.ino = h_ino;
  25080. + ent.bindex = rdu->cookie.bindex;
  25081. + ent.type = d_type;
  25082. + ent.nlen = nlen;
  25083. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  25084. + ent.type = DT_UNKNOWN;
  25085. +
  25086. + /* unnecessary to support mmap_sem since this is a dir */
  25087. + err = -EFAULT;
  25088. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  25089. + goto out;
  25090. + if (copy_to_user(arg->ent.e->name, name, nlen))
  25091. + goto out;
  25092. + /* the terminating NULL */
  25093. + if (__put_user(0, arg->ent.e->name + nlen))
  25094. + goto out;
  25095. + err = 0;
  25096. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  25097. + arg->ent.ul += len;
  25098. + rdu->rent++;
  25099. + } else {
  25100. + err = -EFAULT;
  25101. + au_fset_rdu(rdu->cookie.flags, FULL);
  25102. + rdu->full = 1;
  25103. + rdu->tail = arg->ent;
  25104. + }
  25105. +
  25106. +out:
  25107. + /* AuTraceErr(err); */
  25108. + return err;
  25109. +}
  25110. +
  25111. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  25112. +{
  25113. + int err;
  25114. + loff_t offset;
  25115. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  25116. +
  25117. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  25118. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  25119. + err = offset;
  25120. + if (unlikely(offset != cookie->h_pos))
  25121. + goto out;
  25122. +
  25123. + err = 0;
  25124. + do {
  25125. + arg->err = 0;
  25126. + au_fclr_rdu(cookie->flags, CALLED);
  25127. + /* smp_mb(); */
  25128. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  25129. + if (err >= 0)
  25130. + err = arg->err;
  25131. + } while (!err
  25132. + && au_ftest_rdu(cookie->flags, CALLED)
  25133. + && !au_ftest_rdu(cookie->flags, FULL));
  25134. + cookie->h_pos = h_file->f_pos;
  25135. +
  25136. +out:
  25137. + AuTraceErr(err);
  25138. + return err;
  25139. +}
  25140. +
  25141. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  25142. +{
  25143. + int err;
  25144. + aufs_bindex_t bbot;
  25145. + struct au_rdu_arg arg = {
  25146. + .ctx = {
  25147. + .actor = au_rdu_fill
  25148. + }
  25149. + };
  25150. + struct dentry *dentry;
  25151. + struct inode *inode;
  25152. + struct file *h_file;
  25153. + struct au_rdu_cookie *cookie = &rdu->cookie;
  25154. +
  25155. + err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
  25156. + if (unlikely(err)) {
  25157. + err = -EFAULT;
  25158. + AuTraceErr(err);
  25159. + goto out;
  25160. + }
  25161. + rdu->rent = 0;
  25162. + rdu->tail = rdu->ent;
  25163. + rdu->full = 0;
  25164. + arg.rdu = rdu;
  25165. + arg.ent = rdu->ent;
  25166. + arg.end = arg.ent.ul;
  25167. + arg.end += rdu->sz;
  25168. +
  25169. + err = -ENOTDIR;
  25170. + if (unlikely(!file->f_op->iterate))
  25171. + goto out;
  25172. +
  25173. + err = security_file_permission(file, MAY_READ);
  25174. + AuTraceErr(err);
  25175. + if (unlikely(err))
  25176. + goto out;
  25177. +
  25178. + dentry = file->f_path.dentry;
  25179. + inode = d_inode(dentry);
  25180. +#if 1
  25181. + mutex_lock(&inode->i_mutex);
  25182. +#else
  25183. + err = mutex_lock_killable(&inode->i_mutex);
  25184. + AuTraceErr(err);
  25185. + if (unlikely(err))
  25186. + goto out;
  25187. +#endif
  25188. +
  25189. + arg.sb = inode->i_sb;
  25190. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  25191. + if (unlikely(err))
  25192. + goto out_mtx;
  25193. + err = au_alive_dir(dentry);
  25194. + if (unlikely(err))
  25195. + goto out_si;
  25196. + /* todo: reval? */
  25197. + fi_read_lock(file);
  25198. +
  25199. + err = -EAGAIN;
  25200. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  25201. + && cookie->generation != au_figen(file)))
  25202. + goto out_unlock;
  25203. +
  25204. + err = 0;
  25205. + if (!rdu->blk) {
  25206. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  25207. + if (!rdu->blk)
  25208. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  25209. + }
  25210. + bbot = au_fbtop(file);
  25211. + if (cookie->bindex < bbot)
  25212. + cookie->bindex = bbot;
  25213. + bbot = au_fbbot_dir(file);
  25214. + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
  25215. + for (; !err && cookie->bindex <= bbot;
  25216. + cookie->bindex++, cookie->h_pos = 0) {
  25217. + h_file = au_hf_dir(file, cookie->bindex);
  25218. + if (!h_file)
  25219. + continue;
  25220. +
  25221. + au_fclr_rdu(cookie->flags, FULL);
  25222. + err = au_rdu_do(h_file, &arg);
  25223. + AuTraceErr(err);
  25224. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  25225. + break;
  25226. + }
  25227. + AuDbg("rent %llu\n", rdu->rent);
  25228. +
  25229. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  25230. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  25231. + au_fset_rdu(cookie->flags, CONT);
  25232. + cookie->generation = au_figen(file);
  25233. + }
  25234. +
  25235. + ii_read_lock_child(inode);
  25236. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
  25237. + ii_read_unlock(inode);
  25238. +
  25239. +out_unlock:
  25240. + fi_read_unlock(file);
  25241. +out_si:
  25242. + si_read_unlock(arg.sb);
  25243. +out_mtx:
  25244. + mutex_unlock(&inode->i_mutex);
  25245. +out:
  25246. + AuTraceErr(err);
  25247. + return err;
  25248. +}
  25249. +
  25250. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  25251. +{
  25252. + int err;
  25253. + ino_t ino;
  25254. + unsigned long long nent;
  25255. + union au_rdu_ent_ul *u;
  25256. + struct au_rdu_ent ent;
  25257. + struct super_block *sb;
  25258. +
  25259. + err = 0;
  25260. + nent = rdu->nent;
  25261. + u = &rdu->ent;
  25262. + sb = file->f_path.dentry->d_sb;
  25263. + si_read_lock(sb, AuLock_FLUSH);
  25264. + while (nent-- > 0) {
  25265. + /* unnecessary to support mmap_sem since this is a dir */
  25266. + err = copy_from_user(&ent, u->e, sizeof(ent));
  25267. + if (!err)
  25268. + err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
  25269. + if (unlikely(err)) {
  25270. + err = -EFAULT;
  25271. + AuTraceErr(err);
  25272. + break;
  25273. + }
  25274. +
  25275. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  25276. + if (!ent.wh)
  25277. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  25278. + else
  25279. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  25280. + &ino);
  25281. + if (unlikely(err)) {
  25282. + AuTraceErr(err);
  25283. + break;
  25284. + }
  25285. +
  25286. + err = __put_user(ino, &u->e->ino);
  25287. + if (unlikely(err)) {
  25288. + err = -EFAULT;
  25289. + AuTraceErr(err);
  25290. + break;
  25291. + }
  25292. + u->ul += au_rdu_len(ent.nlen);
  25293. + }
  25294. + si_read_unlock(sb);
  25295. +
  25296. + return err;
  25297. +}
  25298. +
  25299. +/* ---------------------------------------------------------------------- */
  25300. +
  25301. +static int au_rdu_verify(struct aufs_rdu *rdu)
  25302. +{
  25303. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  25304. + "%llu, b%d, 0x%x, g%u}\n",
  25305. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  25306. + rdu->blk,
  25307. + rdu->rent, rdu->shwh, rdu->full,
  25308. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  25309. + rdu->cookie.generation);
  25310. +
  25311. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  25312. + return 0;
  25313. +
  25314. + AuDbg("%u:%u\n",
  25315. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  25316. + return -EINVAL;
  25317. +}
  25318. +
  25319. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  25320. +{
  25321. + long err, e;
  25322. + struct aufs_rdu rdu;
  25323. + void __user *p = (void __user *)arg;
  25324. +
  25325. + err = copy_from_user(&rdu, p, sizeof(rdu));
  25326. + if (unlikely(err)) {
  25327. + err = -EFAULT;
  25328. + AuTraceErr(err);
  25329. + goto out;
  25330. + }
  25331. + err = au_rdu_verify(&rdu);
  25332. + if (unlikely(err))
  25333. + goto out;
  25334. +
  25335. + switch (cmd) {
  25336. + case AUFS_CTL_RDU:
  25337. + err = au_rdu(file, &rdu);
  25338. + if (unlikely(err))
  25339. + break;
  25340. +
  25341. + e = copy_to_user(p, &rdu, sizeof(rdu));
  25342. + if (unlikely(e)) {
  25343. + err = -EFAULT;
  25344. + AuTraceErr(err);
  25345. + }
  25346. + break;
  25347. + case AUFS_CTL_RDU_INO:
  25348. + err = au_rdu_ino(file, &rdu);
  25349. + break;
  25350. +
  25351. + default:
  25352. + /* err = -ENOTTY; */
  25353. + err = -EINVAL;
  25354. + }
  25355. +
  25356. +out:
  25357. + AuTraceErr(err);
  25358. + return err;
  25359. +}
  25360. +
  25361. +#ifdef CONFIG_COMPAT
  25362. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  25363. +{
  25364. + long err, e;
  25365. + struct aufs_rdu rdu;
  25366. + void __user *p = compat_ptr(arg);
  25367. +
  25368. + /* todo: get_user()? */
  25369. + err = copy_from_user(&rdu, p, sizeof(rdu));
  25370. + if (unlikely(err)) {
  25371. + err = -EFAULT;
  25372. + AuTraceErr(err);
  25373. + goto out;
  25374. + }
  25375. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  25376. + err = au_rdu_verify(&rdu);
  25377. + if (unlikely(err))
  25378. + goto out;
  25379. +
  25380. + switch (cmd) {
  25381. + case AUFS_CTL_RDU:
  25382. + err = au_rdu(file, &rdu);
  25383. + if (unlikely(err))
  25384. + break;
  25385. +
  25386. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  25387. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  25388. + e = copy_to_user(p, &rdu, sizeof(rdu));
  25389. + if (unlikely(e)) {
  25390. + err = -EFAULT;
  25391. + AuTraceErr(err);
  25392. + }
  25393. + break;
  25394. + case AUFS_CTL_RDU_INO:
  25395. + err = au_rdu_ino(file, &rdu);
  25396. + break;
  25397. +
  25398. + default:
  25399. + /* err = -ENOTTY; */
  25400. + err = -EINVAL;
  25401. + }
  25402. +
  25403. +out:
  25404. + AuTraceErr(err);
  25405. + return err;
  25406. +}
  25407. +#endif
  25408. --- /dev/null
  25409. +++ linux-4.4/fs/aufs/rwsem.h 2016-10-23 11:20:57.638826618 +0300
  25410. @@ -0,0 +1,198 @@
  25411. +/*
  25412. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25413. + *
  25414. + * This program, aufs is free software; you can redistribute it and/or modify
  25415. + * it under the terms of the GNU General Public License as published by
  25416. + * the Free Software Foundation; either version 2 of the License, or
  25417. + * (at your option) any later version.
  25418. + *
  25419. + * This program is distributed in the hope that it will be useful,
  25420. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25421. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25422. + * GNU General Public License for more details.
  25423. + *
  25424. + * You should have received a copy of the GNU General Public License
  25425. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25426. + */
  25427. +
  25428. +/*
  25429. + * simple read-write semaphore wrappers
  25430. + */
  25431. +
  25432. +#ifndef __AUFS_RWSEM_H__
  25433. +#define __AUFS_RWSEM_H__
  25434. +
  25435. +#ifdef __KERNEL__
  25436. +
  25437. +#include "debug.h"
  25438. +
  25439. +struct au_rwsem {
  25440. + struct rw_semaphore rwsem;
  25441. +#ifdef CONFIG_AUFS_DEBUG
  25442. + /* just for debugging, not almighty counter */
  25443. + atomic_t rcnt, wcnt;
  25444. +#endif
  25445. +};
  25446. +
  25447. +#ifdef CONFIG_LOCKDEP
  25448. +#define au_lockdep_set_name(rw) \
  25449. + lockdep_set_class_and_name(&(rw)->rwsem, \
  25450. + /*original key*/(rw)->rwsem.dep_map.key, \
  25451. + /*name*/#rw)
  25452. +#else
  25453. +#define au_lockdep_set_name(rw) do {} while (0)
  25454. +#endif
  25455. +
  25456. +#ifdef CONFIG_AUFS_DEBUG
  25457. +#define AuDbgCntInit(rw) do { \
  25458. + atomic_set(&(rw)->rcnt, 0); \
  25459. + atomic_set(&(rw)->wcnt, 0); \
  25460. + smp_mb(); /* atomic set */ \
  25461. +} while (0)
  25462. +
  25463. +#define AuDbgCnt(rw, cnt) atomic_read(&(rw)->cnt)
  25464. +#define AuDbgCntInc(rw, cnt) atomic_inc(&(rw)->cnt)
  25465. +#define AuDbgCntDec(rw, cnt) WARN_ON(atomic_dec_return(&(rw)->cnt) < 0)
  25466. +#define AuDbgRcntInc(rw) AuDbgCntInc(rw, rcnt)
  25467. +#define AuDbgRcntDec(rw) AuDbgCntDec(rw, rcnt)
  25468. +#define AuDbgWcntInc(rw) AuDbgCntInc(rw, wcnt)
  25469. +#define AuDbgWcntDec(rw) AuDbgCntDec(rw, wcnt)
  25470. +#else
  25471. +#define AuDbgCnt(rw, cnt) 0
  25472. +#define AuDbgCntInit(rw) do {} while (0)
  25473. +#define AuDbgRcntInc(rw) do {} while (0)
  25474. +#define AuDbgRcntDec(rw) do {} while (0)
  25475. +#define AuDbgWcntInc(rw) do {} while (0)
  25476. +#define AuDbgWcntDec(rw) do {} while (0)
  25477. +#endif /* CONFIG_AUFS_DEBUG */
  25478. +
  25479. +/* to debug easier, do not make them inlined functions */
  25480. +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(&(rw)->rwsem))
  25481. +/* rwsem_is_locked() is unusable */
  25482. +#define AuRwMustReadLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0)
  25483. +#define AuRwMustWriteLock(rw) AuDebugOn(AuDbgCnt(rw, wcnt) <= 0)
  25484. +#define AuRwMustAnyLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0 \
  25485. + && AuDbgCnt(rw, wcnt) <= 0)
  25486. +#define AuRwDestroy(rw) AuDebugOn(AuDbgCnt(rw, rcnt) \
  25487. + || AuDbgCnt(rw, wcnt))
  25488. +
  25489. +#define au_rw_init(rw) do { \
  25490. + AuDbgCntInit(rw); \
  25491. + init_rwsem(&(rw)->rwsem); \
  25492. + au_lockdep_set_name(rw); \
  25493. + } while (0)
  25494. +
  25495. +#define au_rw_init_wlock(rw) do { \
  25496. + au_rw_init(rw); \
  25497. + down_write(&(rw)->rwsem); \
  25498. + AuDbgWcntInc(rw); \
  25499. + } while (0)
  25500. +
  25501. +#define au_rw_init_wlock_nested(rw, lsc) do { \
  25502. + au_rw_init(rw); \
  25503. + down_write_nested(&(rw)->rwsem, lsc); \
  25504. + AuDbgWcntInc(rw); \
  25505. + } while (0)
  25506. +
  25507. +static inline void au_rw_read_lock(struct au_rwsem *rw)
  25508. +{
  25509. + down_read(&rw->rwsem);
  25510. + AuDbgRcntInc(rw);
  25511. +}
  25512. +
  25513. +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
  25514. +{
  25515. + down_read_nested(&rw->rwsem, lsc);
  25516. + AuDbgRcntInc(rw);
  25517. +}
  25518. +
  25519. +static inline void au_rw_read_unlock(struct au_rwsem *rw)
  25520. +{
  25521. + AuRwMustReadLock(rw);
  25522. + AuDbgRcntDec(rw);
  25523. + up_read(&rw->rwsem);
  25524. +}
  25525. +
  25526. +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
  25527. +{
  25528. + AuRwMustWriteLock(rw);
  25529. + AuDbgRcntInc(rw);
  25530. + AuDbgWcntDec(rw);
  25531. + downgrade_write(&rw->rwsem);
  25532. +}
  25533. +
  25534. +static inline void au_rw_write_lock(struct au_rwsem *rw)
  25535. +{
  25536. + down_write(&rw->rwsem);
  25537. + AuDbgWcntInc(rw);
  25538. +}
  25539. +
  25540. +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
  25541. + unsigned int lsc)
  25542. +{
  25543. + down_write_nested(&rw->rwsem, lsc);
  25544. + AuDbgWcntInc(rw);
  25545. +}
  25546. +
  25547. +static inline void au_rw_write_unlock(struct au_rwsem *rw)
  25548. +{
  25549. + AuRwMustWriteLock(rw);
  25550. + AuDbgWcntDec(rw);
  25551. + up_write(&rw->rwsem);
  25552. +}
  25553. +
  25554. +/* why is not _nested version defined */
  25555. +static inline int au_rw_read_trylock(struct au_rwsem *rw)
  25556. +{
  25557. + int ret;
  25558. +
  25559. + ret = down_read_trylock(&rw->rwsem);
  25560. + if (ret)
  25561. + AuDbgRcntInc(rw);
  25562. + return ret;
  25563. +}
  25564. +
  25565. +static inline int au_rw_write_trylock(struct au_rwsem *rw)
  25566. +{
  25567. + int ret;
  25568. +
  25569. + ret = down_write_trylock(&rw->rwsem);
  25570. + if (ret)
  25571. + AuDbgWcntInc(rw);
  25572. + return ret;
  25573. +}
  25574. +
  25575. +#undef AuDbgCntDec
  25576. +#undef AuDbgRcntInc
  25577. +#undef AuDbgRcntDec
  25578. +#undef AuDbgWcntDec
  25579. +
  25580. +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  25581. +static inline void prefix##_read_lock(param) \
  25582. +{ au_rw_read_lock(rwsem); } \
  25583. +static inline void prefix##_write_lock(param) \
  25584. +{ au_rw_write_lock(rwsem); } \
  25585. +static inline int prefix##_read_trylock(param) \
  25586. +{ return au_rw_read_trylock(rwsem); } \
  25587. +static inline int prefix##_write_trylock(param) \
  25588. +{ return au_rw_write_trylock(rwsem); }
  25589. +/* why is not _nested version defined */
  25590. +/* static inline void prefix##_read_trylock_nested(param, lsc)
  25591. +{ au_rw_read_trylock_nested(rwsem, lsc)); }
  25592. +static inline void prefix##_write_trylock_nestd(param, lsc)
  25593. +{ au_rw_write_trylock_nested(rwsem, lsc); } */
  25594. +
  25595. +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
  25596. +static inline void prefix##_read_unlock(param) \
  25597. +{ au_rw_read_unlock(rwsem); } \
  25598. +static inline void prefix##_write_unlock(param) \
  25599. +{ au_rw_write_unlock(rwsem); } \
  25600. +static inline void prefix##_downgrade_lock(param) \
  25601. +{ au_rw_dgrade_lock(rwsem); }
  25602. +
  25603. +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
  25604. + AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  25605. + AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
  25606. +
  25607. +#endif /* __KERNEL__ */
  25608. +#endif /* __AUFS_RWSEM_H__ */
  25609. --- /dev/null
  25610. +++ linux-4.4/fs/aufs/sbinfo.c 2016-10-23 11:20:57.638826618 +0300
  25611. @@ -0,0 +1,359 @@
  25612. +/*
  25613. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25614. + *
  25615. + * This program, aufs is free software; you can redistribute it and/or modify
  25616. + * it under the terms of the GNU General Public License as published by
  25617. + * the Free Software Foundation; either version 2 of the License, or
  25618. + * (at your option) any later version.
  25619. + *
  25620. + * This program is distributed in the hope that it will be useful,
  25621. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25622. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25623. + * GNU General Public License for more details.
  25624. + *
  25625. + * You should have received a copy of the GNU General Public License
  25626. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25627. + */
  25628. +
  25629. +/*
  25630. + * superblock private data
  25631. + */
  25632. +
  25633. +#include "aufs.h"
  25634. +
  25635. +/*
  25636. + * they are necessary regardless sysfs is disabled.
  25637. + */
  25638. +void au_si_free(struct kobject *kobj)
  25639. +{
  25640. + int i;
  25641. + struct au_sbinfo *sbinfo;
  25642. + char *locked __maybe_unused; /* debug only */
  25643. +
  25644. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  25645. + for (i = 0; i < AuPlink_NHASH; i++)
  25646. + AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
  25647. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  25648. +
  25649. + AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
  25650. + percpu_counter_destroy(&sbinfo->si_ninodes);
  25651. + AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
  25652. + percpu_counter_destroy(&sbinfo->si_nfiles);
  25653. +
  25654. + AuDebugOn(!hlist_empty(&sbinfo->si_symlink.head));
  25655. +
  25656. + au_rw_write_lock(&sbinfo->si_rwsem);
  25657. + au_br_free(sbinfo);
  25658. + au_rw_write_unlock(&sbinfo->si_rwsem);
  25659. +
  25660. + au_delayed_kfree(sbinfo->si_branch);
  25661. + for (i = 0; i < AU_NPIDMAP; i++)
  25662. + if (sbinfo->au_si_pid.pid_bitmap[i])
  25663. + au_delayed_kfree(sbinfo->au_si_pid.pid_bitmap[i]);
  25664. + mutex_destroy(&sbinfo->au_si_pid.pid_mtx);
  25665. + mutex_destroy(&sbinfo->si_xib_mtx);
  25666. + AuRwDestroy(&sbinfo->si_rwsem);
  25667. +
  25668. + au_delayed_kfree(sbinfo);
  25669. +}
  25670. +
  25671. +int au_si_alloc(struct super_block *sb)
  25672. +{
  25673. + int err, i;
  25674. + struct au_sbinfo *sbinfo;
  25675. +
  25676. + err = -ENOMEM;
  25677. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  25678. + if (unlikely(!sbinfo))
  25679. + goto out;
  25680. +
  25681. + /* will be reallocated separately */
  25682. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  25683. + if (unlikely(!sbinfo->si_branch))
  25684. + goto out_sbinfo;
  25685. +
  25686. + err = sysaufs_si_init(sbinfo);
  25687. + if (unlikely(err))
  25688. + goto out_br;
  25689. +
  25690. + au_nwt_init(&sbinfo->si_nowait);
  25691. + au_rw_init_wlock(&sbinfo->si_rwsem);
  25692. + mutex_init(&sbinfo->au_si_pid.pid_mtx);
  25693. +
  25694. + percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
  25695. + percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
  25696. +
  25697. + sbinfo->si_bbot = -1;
  25698. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  25699. +
  25700. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  25701. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  25702. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  25703. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  25704. +
  25705. + au_fhsm_init(sbinfo);
  25706. +
  25707. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  25708. +
  25709. + au_sphl_init(&sbinfo->si_symlink);
  25710. +
  25711. + sbinfo->si_xino_jiffy = jiffies;
  25712. + sbinfo->si_xino_expire
  25713. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  25714. + mutex_init(&sbinfo->si_xib_mtx);
  25715. + sbinfo->si_xino_brid = -1;
  25716. + /* leave si_xib_last_pindex and si_xib_next_bit */
  25717. +
  25718. + au_sphl_init(&sbinfo->si_aopen);
  25719. +
  25720. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  25721. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  25722. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  25723. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  25724. +
  25725. + for (i = 0; i < AuPlink_NHASH; i++)
  25726. + au_sphl_init(sbinfo->si_plink + i);
  25727. + init_waitqueue_head(&sbinfo->si_plink_wq);
  25728. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  25729. +
  25730. + au_sphl_init(&sbinfo->si_files);
  25731. +
  25732. + /* with getattr by default */
  25733. + sbinfo->si_iop_array = aufs_iop;
  25734. +
  25735. + /* leave other members for sysaufs and si_mnt. */
  25736. + sbinfo->si_sb = sb;
  25737. + sb->s_fs_info = sbinfo;
  25738. + si_pid_set(sb);
  25739. + return 0; /* success */
  25740. +
  25741. +out_br:
  25742. + au_delayed_kfree(sbinfo->si_branch);
  25743. +out_sbinfo:
  25744. + au_delayed_kfree(sbinfo);
  25745. +out:
  25746. + return err;
  25747. +}
  25748. +
  25749. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
  25750. +{
  25751. + int err, sz;
  25752. + struct au_branch **brp;
  25753. +
  25754. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  25755. +
  25756. + err = -ENOMEM;
  25757. + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
  25758. + if (unlikely(!sz))
  25759. + sz = sizeof(*brp);
  25760. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
  25761. + may_shrink);
  25762. + if (brp) {
  25763. + sbinfo->si_branch = brp;
  25764. + err = 0;
  25765. + }
  25766. +
  25767. + return err;
  25768. +}
  25769. +
  25770. +/* ---------------------------------------------------------------------- */
  25771. +
  25772. +unsigned int au_sigen_inc(struct super_block *sb)
  25773. +{
  25774. + unsigned int gen;
  25775. + struct inode *inode;
  25776. +
  25777. + SiMustWriteLock(sb);
  25778. +
  25779. + gen = ++au_sbi(sb)->si_generation;
  25780. + au_update_digen(sb->s_root);
  25781. + inode = d_inode(sb->s_root);
  25782. + au_update_iigen(inode, /*half*/0);
  25783. + inode->i_version++;
  25784. + return gen;
  25785. +}
  25786. +
  25787. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  25788. +{
  25789. + aufs_bindex_t br_id;
  25790. + int i;
  25791. + struct au_sbinfo *sbinfo;
  25792. +
  25793. + SiMustWriteLock(sb);
  25794. +
  25795. + sbinfo = au_sbi(sb);
  25796. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  25797. + br_id = ++sbinfo->si_last_br_id;
  25798. + AuDebugOn(br_id < 0);
  25799. + if (br_id && au_br_index(sb, br_id) < 0)
  25800. + return br_id;
  25801. + }
  25802. +
  25803. + return -1;
  25804. +}
  25805. +
  25806. +/* ---------------------------------------------------------------------- */
  25807. +
  25808. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  25809. +int si_read_lock(struct super_block *sb, int flags)
  25810. +{
  25811. + int err;
  25812. +
  25813. + err = 0;
  25814. + if (au_ftest_lock(flags, FLUSH))
  25815. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25816. +
  25817. + si_noflush_read_lock(sb);
  25818. + err = au_plink_maint(sb, flags);
  25819. + if (unlikely(err))
  25820. + si_read_unlock(sb);
  25821. +
  25822. + return err;
  25823. +}
  25824. +
  25825. +int si_write_lock(struct super_block *sb, int flags)
  25826. +{
  25827. + int err;
  25828. +
  25829. + if (au_ftest_lock(flags, FLUSH))
  25830. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25831. +
  25832. + si_noflush_write_lock(sb);
  25833. + err = au_plink_maint(sb, flags);
  25834. + if (unlikely(err))
  25835. + si_write_unlock(sb);
  25836. +
  25837. + return err;
  25838. +}
  25839. +
  25840. +/* dentry and super_block lock. call at entry point */
  25841. +int aufs_read_lock(struct dentry *dentry, int flags)
  25842. +{
  25843. + int err;
  25844. + struct super_block *sb;
  25845. +
  25846. + sb = dentry->d_sb;
  25847. + err = si_read_lock(sb, flags);
  25848. + if (unlikely(err))
  25849. + goto out;
  25850. +
  25851. + if (au_ftest_lock(flags, DW))
  25852. + di_write_lock_child(dentry);
  25853. + else
  25854. + di_read_lock_child(dentry, flags);
  25855. +
  25856. + if (au_ftest_lock(flags, GEN)) {
  25857. + err = au_digen_test(dentry, au_sigen(sb));
  25858. + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
  25859. + AuDebugOn(!err && au_dbrange_test(dentry));
  25860. + else if (!err)
  25861. + err = au_dbrange_test(dentry);
  25862. + if (unlikely(err))
  25863. + aufs_read_unlock(dentry, flags);
  25864. + }
  25865. +
  25866. +out:
  25867. + return err;
  25868. +}
  25869. +
  25870. +void aufs_read_unlock(struct dentry *dentry, int flags)
  25871. +{
  25872. + if (au_ftest_lock(flags, DW))
  25873. + di_write_unlock(dentry);
  25874. + else
  25875. + di_read_unlock(dentry, flags);
  25876. + si_read_unlock(dentry->d_sb);
  25877. +}
  25878. +
  25879. +void aufs_write_lock(struct dentry *dentry)
  25880. +{
  25881. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  25882. + di_write_lock_child(dentry);
  25883. +}
  25884. +
  25885. +void aufs_write_unlock(struct dentry *dentry)
  25886. +{
  25887. + di_write_unlock(dentry);
  25888. + si_write_unlock(dentry->d_sb);
  25889. +}
  25890. +
  25891. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  25892. +{
  25893. + int err;
  25894. + unsigned int sigen;
  25895. + struct super_block *sb;
  25896. +
  25897. + sb = d1->d_sb;
  25898. + err = si_read_lock(sb, flags);
  25899. + if (unlikely(err))
  25900. + goto out;
  25901. +
  25902. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
  25903. +
  25904. + if (au_ftest_lock(flags, GEN)) {
  25905. + sigen = au_sigen(sb);
  25906. + err = au_digen_test(d1, sigen);
  25907. + AuDebugOn(!err && au_dbrange_test(d1));
  25908. + if (!err) {
  25909. + err = au_digen_test(d2, sigen);
  25910. + AuDebugOn(!err && au_dbrange_test(d2));
  25911. + }
  25912. + if (unlikely(err))
  25913. + aufs_read_and_write_unlock2(d1, d2);
  25914. + }
  25915. +
  25916. +out:
  25917. + return err;
  25918. +}
  25919. +
  25920. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  25921. +{
  25922. + di_write_unlock2(d1, d2);
  25923. + si_read_unlock(d1->d_sb);
  25924. +}
  25925. +
  25926. +/* ---------------------------------------------------------------------- */
  25927. +
  25928. +static void si_pid_alloc(struct au_si_pid *au_si_pid, int idx)
  25929. +{
  25930. + unsigned long *p;
  25931. +
  25932. + BUILD_BUG_ON(sizeof(unsigned long) !=
  25933. + sizeof(*au_si_pid->pid_bitmap));
  25934. +
  25935. + mutex_lock(&au_si_pid->pid_mtx);
  25936. + p = au_si_pid->pid_bitmap[idx];
  25937. + while (!p) {
  25938. + /*
  25939. + * bad approach.
  25940. + * but keeping 'si_pid_set()' void is more important.
  25941. + */
  25942. + p = kcalloc(BITS_TO_LONGS(AU_PIDSTEP),
  25943. + sizeof(*au_si_pid->pid_bitmap),
  25944. + GFP_NOFS);
  25945. + if (p)
  25946. + break;
  25947. + cond_resched();
  25948. + }
  25949. + au_si_pid->pid_bitmap[idx] = p;
  25950. + mutex_unlock(&au_si_pid->pid_mtx);
  25951. +}
  25952. +
  25953. +void si_pid_set(struct super_block *sb)
  25954. +{
  25955. + pid_t bit;
  25956. + int idx;
  25957. + unsigned long *bitmap;
  25958. + struct au_si_pid *au_si_pid;
  25959. +
  25960. + si_pid_idx_bit(&idx, &bit);
  25961. + au_si_pid = &au_sbi(sb)->au_si_pid;
  25962. + bitmap = au_si_pid->pid_bitmap[idx];
  25963. + if (!bitmap) {
  25964. + si_pid_alloc(au_si_pid, idx);
  25965. + bitmap = au_si_pid->pid_bitmap[idx];
  25966. + }
  25967. + AuDebugOn(test_bit(bit, bitmap));
  25968. + set_bit(bit, bitmap);
  25969. + /* smp_mb(); */
  25970. +}
  25971. --- /dev/null
  25972. +++ linux-4.4/fs/aufs/spl.h 2016-10-23 11:20:57.638826618 +0300
  25973. @@ -0,0 +1,113 @@
  25974. +/*
  25975. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  25976. + *
  25977. + * This program, aufs is free software; you can redistribute it and/or modify
  25978. + * it under the terms of the GNU General Public License as published by
  25979. + * the Free Software Foundation; either version 2 of the License, or
  25980. + * (at your option) any later version.
  25981. + *
  25982. + * This program is distributed in the hope that it will be useful,
  25983. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25984. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25985. + * GNU General Public License for more details.
  25986. + *
  25987. + * You should have received a copy of the GNU General Public License
  25988. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25989. + */
  25990. +
  25991. +/*
  25992. + * simple list protected by a spinlock
  25993. + */
  25994. +
  25995. +#ifndef __AUFS_SPL_H__
  25996. +#define __AUFS_SPL_H__
  25997. +
  25998. +#ifdef __KERNEL__
  25999. +
  26000. +#if 0
  26001. +struct au_splhead {
  26002. + spinlock_t spin;
  26003. + struct list_head head;
  26004. +};
  26005. +
  26006. +static inline void au_spl_init(struct au_splhead *spl)
  26007. +{
  26008. + spin_lock_init(&spl->spin);
  26009. + INIT_LIST_HEAD(&spl->head);
  26010. +}
  26011. +
  26012. +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
  26013. +{
  26014. + spin_lock(&spl->spin);
  26015. + list_add(list, &spl->head);
  26016. + spin_unlock(&spl->spin);
  26017. +}
  26018. +
  26019. +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
  26020. +{
  26021. + spin_lock(&spl->spin);
  26022. + list_del(list);
  26023. + spin_unlock(&spl->spin);
  26024. +}
  26025. +
  26026. +static inline void au_spl_del_rcu(struct list_head *list,
  26027. + struct au_splhead *spl)
  26028. +{
  26029. + spin_lock(&spl->spin);
  26030. + list_del_rcu(list);
  26031. + spin_unlock(&spl->spin);
  26032. +}
  26033. +#endif
  26034. +
  26035. +/* ---------------------------------------------------------------------- */
  26036. +
  26037. +struct au_sphlhead {
  26038. + spinlock_t spin;
  26039. + struct hlist_head head;
  26040. +};
  26041. +
  26042. +static inline void au_sphl_init(struct au_sphlhead *sphl)
  26043. +{
  26044. + spin_lock_init(&sphl->spin);
  26045. + INIT_HLIST_HEAD(&sphl->head);
  26046. +}
  26047. +
  26048. +static inline void au_sphl_add(struct hlist_node *hlist,
  26049. + struct au_sphlhead *sphl)
  26050. +{
  26051. + spin_lock(&sphl->spin);
  26052. + hlist_add_head(hlist, &sphl->head);
  26053. + spin_unlock(&sphl->spin);
  26054. +}
  26055. +
  26056. +static inline void au_sphl_del(struct hlist_node *hlist,
  26057. + struct au_sphlhead *sphl)
  26058. +{
  26059. + spin_lock(&sphl->spin);
  26060. + hlist_del(hlist);
  26061. + spin_unlock(&sphl->spin);
  26062. +}
  26063. +
  26064. +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
  26065. + struct au_sphlhead *sphl)
  26066. +{
  26067. + spin_lock(&sphl->spin);
  26068. + hlist_del_rcu(hlist);
  26069. + spin_unlock(&sphl->spin);
  26070. +}
  26071. +
  26072. +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
  26073. +{
  26074. + unsigned long cnt;
  26075. + struct hlist_node *pos;
  26076. +
  26077. + cnt = 0;
  26078. + spin_lock(&sphl->spin);
  26079. + hlist_for_each(pos, &sphl->head)
  26080. + cnt++;
  26081. + spin_unlock(&sphl->spin);
  26082. + return cnt;
  26083. +}
  26084. +
  26085. +#endif /* __KERNEL__ */
  26086. +#endif /* __AUFS_SPL_H__ */
  26087. --- /dev/null
  26088. +++ linux-4.4/fs/aufs/super.c 2016-10-23 11:20:57.638826618 +0300
  26089. @@ -0,0 +1,1038 @@
  26090. +/*
  26091. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  26092. + *
  26093. + * This program, aufs is free software; you can redistribute it and/or modify
  26094. + * it under the terms of the GNU General Public License as published by
  26095. + * the Free Software Foundation; either version 2 of the License, or
  26096. + * (at your option) any later version.
  26097. + *
  26098. + * This program is distributed in the hope that it will be useful,
  26099. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26100. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26101. + * GNU General Public License for more details.
  26102. + *
  26103. + * You should have received a copy of the GNU General Public License
  26104. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26105. + */
  26106. +
  26107. +/*
  26108. + * mount and super_block operations
  26109. + */
  26110. +
  26111. +#include <linux/mm.h>
  26112. +#include <linux/seq_file.h>
  26113. +#include <linux/statfs.h>
  26114. +#include <linux/vmalloc.h>
  26115. +#include "aufs.h"
  26116. +
  26117. +/*
  26118. + * super_operations
  26119. + */
  26120. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  26121. +{
  26122. + struct au_icntnr *c;
  26123. +
  26124. + c = au_cache_alloc_icntnr();
  26125. + if (c) {
  26126. + au_icntnr_init(c);
  26127. + c->vfs_inode.i_version = 1; /* sigen(sb); */
  26128. + c->iinfo.ii_hinode = NULL;
  26129. + return &c->vfs_inode;
  26130. + }
  26131. + return NULL;
  26132. +}
  26133. +
  26134. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  26135. +{
  26136. + struct inode *inode = container_of(head, struct inode, i_rcu);
  26137. +
  26138. + au_cache_dfree_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  26139. +}
  26140. +
  26141. +static void aufs_destroy_inode(struct inode *inode)
  26142. +{
  26143. + if (!au_is_bad_inode(inode))
  26144. + au_iinfo_fin(inode);
  26145. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  26146. +}
  26147. +
  26148. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  26149. +{
  26150. + struct inode *inode;
  26151. + int err;
  26152. +
  26153. + inode = iget_locked(sb, ino);
  26154. + if (unlikely(!inode)) {
  26155. + inode = ERR_PTR(-ENOMEM);
  26156. + goto out;
  26157. + }
  26158. + if (!(inode->i_state & I_NEW))
  26159. + goto out;
  26160. +
  26161. + err = au_xigen_new(inode);
  26162. + if (!err)
  26163. + err = au_iinfo_init(inode);
  26164. + if (!err)
  26165. + inode->i_version++;
  26166. + else {
  26167. + iget_failed(inode);
  26168. + inode = ERR_PTR(err);
  26169. + }
  26170. +
  26171. +out:
  26172. + /* never return NULL */
  26173. + AuDebugOn(!inode);
  26174. + AuTraceErrPtr(inode);
  26175. + return inode;
  26176. +}
  26177. +
  26178. +/* lock free root dinfo */
  26179. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  26180. +{
  26181. + int err;
  26182. + aufs_bindex_t bindex, bbot;
  26183. + struct path path;
  26184. + struct au_hdentry *hdp;
  26185. + struct au_branch *br;
  26186. + au_br_perm_str_t perm;
  26187. +
  26188. + err = 0;
  26189. + bbot = au_sbbot(sb);
  26190. + bindex = 0;
  26191. + hdp = au_hdentry(au_di(sb->s_root), bindex);
  26192. + for (; !err && bindex <= bbot; bindex++, hdp++) {
  26193. + br = au_sbr(sb, bindex);
  26194. + path.mnt = au_br_mnt(br);
  26195. + path.dentry = hdp->hd_dentry;
  26196. + err = au_seq_path(seq, &path);
  26197. + if (!err) {
  26198. + au_optstr_br_perm(&perm, br->br_perm);
  26199. + seq_printf(seq, "=%s", perm.a);
  26200. + if (bindex != bbot)
  26201. + seq_putc(seq, ':');
  26202. + }
  26203. + }
  26204. + if (unlikely(err || seq_has_overflowed(seq)))
  26205. + err = -E2BIG;
  26206. +
  26207. + return err;
  26208. +}
  26209. +
  26210. +static void au_show_wbr_create(struct seq_file *m, int v,
  26211. + struct au_sbinfo *sbinfo)
  26212. +{
  26213. + const char *pat;
  26214. +
  26215. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  26216. +
  26217. + seq_puts(m, ",create=");
  26218. + pat = au_optstr_wbr_create(v);
  26219. + switch (v) {
  26220. + case AuWbrCreate_TDP:
  26221. + case AuWbrCreate_RR:
  26222. + case AuWbrCreate_MFS:
  26223. + case AuWbrCreate_PMFS:
  26224. + seq_puts(m, pat);
  26225. + break;
  26226. + case AuWbrCreate_MFSV:
  26227. + seq_printf(m, /*pat*/"mfs:%lu",
  26228. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26229. + / MSEC_PER_SEC);
  26230. + break;
  26231. + case AuWbrCreate_PMFSV:
  26232. + seq_printf(m, /*pat*/"pmfs:%lu",
  26233. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26234. + / MSEC_PER_SEC);
  26235. + break;
  26236. + case AuWbrCreate_MFSRR:
  26237. + seq_printf(m, /*pat*/"mfsrr:%llu",
  26238. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  26239. + break;
  26240. + case AuWbrCreate_MFSRRV:
  26241. + seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
  26242. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  26243. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26244. + / MSEC_PER_SEC);
  26245. + break;
  26246. + case AuWbrCreate_PMFSRR:
  26247. + seq_printf(m, /*pat*/"pmfsrr:%llu",
  26248. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  26249. + break;
  26250. + case AuWbrCreate_PMFSRRV:
  26251. + seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
  26252. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  26253. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  26254. + / MSEC_PER_SEC);
  26255. + break;
  26256. + }
  26257. +}
  26258. +
  26259. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  26260. +{
  26261. +#ifdef CONFIG_SYSFS
  26262. + return 0;
  26263. +#else
  26264. + int err;
  26265. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  26266. + aufs_bindex_t bindex, brid;
  26267. + struct qstr *name;
  26268. + struct file *f;
  26269. + struct dentry *d, *h_root;
  26270. +
  26271. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  26272. +
  26273. + err = 0;
  26274. + f = au_sbi(sb)->si_xib;
  26275. + if (!f)
  26276. + goto out;
  26277. +
  26278. + /* stop printing the default xino path on the first writable branch */
  26279. + h_root = NULL;
  26280. + brid = au_xino_brid(sb);
  26281. + if (brid >= 0) {
  26282. + bindex = au_br_index(sb, brid);
  26283. + h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
  26284. + }
  26285. + d = f->f_path.dentry;
  26286. + name = &d->d_name;
  26287. + /* safe ->d_parent because the file is unlinked */
  26288. + if (d->d_parent == h_root
  26289. + && name->len == len
  26290. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  26291. + goto out;
  26292. +
  26293. + seq_puts(seq, ",xino=");
  26294. + err = au_xino_path(seq, f);
  26295. +
  26296. +out:
  26297. + return err;
  26298. +#endif
  26299. +}
  26300. +
  26301. +/* seq_file will re-call me in case of too long string */
  26302. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  26303. +{
  26304. + int err;
  26305. + unsigned int mnt_flags, v;
  26306. + struct super_block *sb;
  26307. + struct au_sbinfo *sbinfo;
  26308. +
  26309. +#define AuBool(name, str) do { \
  26310. + v = au_opt_test(mnt_flags, name); \
  26311. + if (v != au_opt_test(AuOpt_Def, name)) \
  26312. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  26313. +} while (0)
  26314. +
  26315. +#define AuStr(name, str) do { \
  26316. + v = mnt_flags & AuOptMask_##name; \
  26317. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  26318. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  26319. +} while (0)
  26320. +
  26321. +#define AuUInt(name, str, val) do { \
  26322. + if (val != AUFS_##name##_DEF) \
  26323. + seq_printf(m, "," #str "=%u", val); \
  26324. +} while (0)
  26325. +
  26326. + sb = dentry->d_sb;
  26327. + if (sb->s_flags & MS_POSIXACL)
  26328. + seq_puts(m, ",acl");
  26329. +
  26330. + /* lock free root dinfo */
  26331. + si_noflush_read_lock(sb);
  26332. + sbinfo = au_sbi(sb);
  26333. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  26334. +
  26335. + mnt_flags = au_mntflags(sb);
  26336. + if (au_opt_test(mnt_flags, XINO)) {
  26337. + err = au_show_xino(m, sb);
  26338. + if (unlikely(err))
  26339. + goto out;
  26340. + } else
  26341. + seq_puts(m, ",noxino");
  26342. +
  26343. + AuBool(TRUNC_XINO, trunc_xino);
  26344. + AuStr(UDBA, udba);
  26345. + AuBool(SHWH, shwh);
  26346. + AuBool(PLINK, plink);
  26347. + AuBool(DIO, dio);
  26348. + AuBool(DIRPERM1, dirperm1);
  26349. +
  26350. + v = sbinfo->si_wbr_create;
  26351. + if (v != AuWbrCreate_Def)
  26352. + au_show_wbr_create(m, v, sbinfo);
  26353. +
  26354. + v = sbinfo->si_wbr_copyup;
  26355. + if (v != AuWbrCopyup_Def)
  26356. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  26357. +
  26358. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  26359. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  26360. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  26361. +
  26362. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  26363. +
  26364. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  26365. + AuUInt(RDCACHE, rdcache, v);
  26366. +
  26367. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  26368. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  26369. +
  26370. + au_fhsm_show(m, sbinfo);
  26371. +
  26372. + AuBool(SUM, sum);
  26373. + /* AuBool(SUM_W, wsum); */
  26374. + AuBool(WARN_PERM, warn_perm);
  26375. + AuBool(VERBOSE, verbose);
  26376. +
  26377. +out:
  26378. + /* be sure to print "br:" last */
  26379. + if (!sysaufs_brs) {
  26380. + seq_puts(m, ",br:");
  26381. + au_show_brs(m, sb);
  26382. + }
  26383. + si_read_unlock(sb);
  26384. + return 0;
  26385. +
  26386. +#undef AuBool
  26387. +#undef AuStr
  26388. +#undef AuUInt
  26389. +}
  26390. +
  26391. +/* ---------------------------------------------------------------------- */
  26392. +
  26393. +/* sum mode which returns the summation for statfs(2) */
  26394. +
  26395. +static u64 au_add_till_max(u64 a, u64 b)
  26396. +{
  26397. + u64 old;
  26398. +
  26399. + old = a;
  26400. + a += b;
  26401. + if (old <= a)
  26402. + return a;
  26403. + return ULLONG_MAX;
  26404. +}
  26405. +
  26406. +static u64 au_mul_till_max(u64 a, long mul)
  26407. +{
  26408. + u64 old;
  26409. +
  26410. + old = a;
  26411. + a *= mul;
  26412. + if (old <= a)
  26413. + return a;
  26414. + return ULLONG_MAX;
  26415. +}
  26416. +
  26417. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  26418. +{
  26419. + int err;
  26420. + long bsize, factor;
  26421. + u64 blocks, bfree, bavail, files, ffree;
  26422. + aufs_bindex_t bbot, bindex, i;
  26423. + unsigned char shared;
  26424. + struct path h_path;
  26425. + struct super_block *h_sb;
  26426. +
  26427. + err = 0;
  26428. + bsize = LONG_MAX;
  26429. + files = 0;
  26430. + ffree = 0;
  26431. + blocks = 0;
  26432. + bfree = 0;
  26433. + bavail = 0;
  26434. + bbot = au_sbbot(sb);
  26435. + for (bindex = 0; bindex <= bbot; bindex++) {
  26436. + h_path.mnt = au_sbr_mnt(sb, bindex);
  26437. + h_sb = h_path.mnt->mnt_sb;
  26438. + shared = 0;
  26439. + for (i = 0; !shared && i < bindex; i++)
  26440. + shared = (au_sbr_sb(sb, i) == h_sb);
  26441. + if (shared)
  26442. + continue;
  26443. +
  26444. + /* sb->s_root for NFS is unreliable */
  26445. + h_path.dentry = h_path.mnt->mnt_root;
  26446. + err = vfs_statfs(&h_path, buf);
  26447. + if (unlikely(err))
  26448. + goto out;
  26449. +
  26450. + if (bsize > buf->f_bsize) {
  26451. + /*
  26452. + * we will reduce bsize, so we have to expand blocks
  26453. + * etc. to match them again
  26454. + */
  26455. + factor = (bsize / buf->f_bsize);
  26456. + blocks = au_mul_till_max(blocks, factor);
  26457. + bfree = au_mul_till_max(bfree, factor);
  26458. + bavail = au_mul_till_max(bavail, factor);
  26459. + bsize = buf->f_bsize;
  26460. + }
  26461. +
  26462. + factor = (buf->f_bsize / bsize);
  26463. + blocks = au_add_till_max(blocks,
  26464. + au_mul_till_max(buf->f_blocks, factor));
  26465. + bfree = au_add_till_max(bfree,
  26466. + au_mul_till_max(buf->f_bfree, factor));
  26467. + bavail = au_add_till_max(bavail,
  26468. + au_mul_till_max(buf->f_bavail, factor));
  26469. + files = au_add_till_max(files, buf->f_files);
  26470. + ffree = au_add_till_max(ffree, buf->f_ffree);
  26471. + }
  26472. +
  26473. + buf->f_bsize = bsize;
  26474. + buf->f_blocks = blocks;
  26475. + buf->f_bfree = bfree;
  26476. + buf->f_bavail = bavail;
  26477. + buf->f_files = files;
  26478. + buf->f_ffree = ffree;
  26479. + buf->f_frsize = 0;
  26480. +
  26481. +out:
  26482. + return err;
  26483. +}
  26484. +
  26485. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  26486. +{
  26487. + int err;
  26488. + struct path h_path;
  26489. + struct super_block *sb;
  26490. +
  26491. + /* lock free root dinfo */
  26492. + sb = dentry->d_sb;
  26493. + si_noflush_read_lock(sb);
  26494. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  26495. + /* sb->s_root for NFS is unreliable */
  26496. + h_path.mnt = au_sbr_mnt(sb, 0);
  26497. + h_path.dentry = h_path.mnt->mnt_root;
  26498. + err = vfs_statfs(&h_path, buf);
  26499. + } else
  26500. + err = au_statfs_sum(sb, buf);
  26501. + si_read_unlock(sb);
  26502. +
  26503. + if (!err) {
  26504. + buf->f_type = AUFS_SUPER_MAGIC;
  26505. + buf->f_namelen = AUFS_MAX_NAMELEN;
  26506. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  26507. + }
  26508. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  26509. +
  26510. + return err;
  26511. +}
  26512. +
  26513. +/* ---------------------------------------------------------------------- */
  26514. +
  26515. +static int aufs_sync_fs(struct super_block *sb, int wait)
  26516. +{
  26517. + int err, e;
  26518. + aufs_bindex_t bbot, bindex;
  26519. + struct au_branch *br;
  26520. + struct super_block *h_sb;
  26521. +
  26522. + err = 0;
  26523. + si_noflush_read_lock(sb);
  26524. + bbot = au_sbbot(sb);
  26525. + for (bindex = 0; bindex <= bbot; bindex++) {
  26526. + br = au_sbr(sb, bindex);
  26527. + if (!au_br_writable(br->br_perm))
  26528. + continue;
  26529. +
  26530. + h_sb = au_sbr_sb(sb, bindex);
  26531. + if (h_sb->s_op->sync_fs) {
  26532. + e = h_sb->s_op->sync_fs(h_sb, wait);
  26533. + if (unlikely(e && !err))
  26534. + err = e;
  26535. + /* go on even if an error happens */
  26536. + }
  26537. + }
  26538. + si_read_unlock(sb);
  26539. +
  26540. + return err;
  26541. +}
  26542. +
  26543. +/* ---------------------------------------------------------------------- */
  26544. +
  26545. +/* final actions when unmounting a file system */
  26546. +static void aufs_put_super(struct super_block *sb)
  26547. +{
  26548. + struct au_sbinfo *sbinfo;
  26549. +
  26550. + sbinfo = au_sbi(sb);
  26551. + if (!sbinfo)
  26552. + return;
  26553. +
  26554. + dbgaufs_si_fin(sbinfo);
  26555. + kobject_put(&sbinfo->si_kobj);
  26556. +}
  26557. +
  26558. +/* ---------------------------------------------------------------------- */
  26559. +
  26560. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  26561. + struct super_block *sb, void *arg)
  26562. +{
  26563. + void *array;
  26564. + unsigned long long n, sz;
  26565. +
  26566. + array = NULL;
  26567. + n = 0;
  26568. + if (!*hint)
  26569. + goto out;
  26570. +
  26571. + if (*hint > ULLONG_MAX / sizeof(array)) {
  26572. + array = ERR_PTR(-EMFILE);
  26573. + pr_err("hint %llu\n", *hint);
  26574. + goto out;
  26575. + }
  26576. +
  26577. + sz = sizeof(array) * *hint;
  26578. + array = kzalloc(sz, GFP_NOFS);
  26579. + if (unlikely(!array))
  26580. + array = vzalloc(sz);
  26581. + if (unlikely(!array)) {
  26582. + array = ERR_PTR(-ENOMEM);
  26583. + goto out;
  26584. + }
  26585. +
  26586. + n = cb(sb, array, *hint, arg);
  26587. + AuDebugOn(n > *hint);
  26588. +
  26589. +out:
  26590. + *hint = n;
  26591. + return array;
  26592. +}
  26593. +
  26594. +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
  26595. + unsigned long long max __maybe_unused,
  26596. + void *arg)
  26597. +{
  26598. + unsigned long long n;
  26599. + struct inode **p, *inode;
  26600. + struct list_head *head;
  26601. +
  26602. + n = 0;
  26603. + p = a;
  26604. + head = arg;
  26605. + spin_lock(&sb->s_inode_list_lock);
  26606. + list_for_each_entry(inode, head, i_sb_list) {
  26607. + if (!au_is_bad_inode(inode)
  26608. + && au_ii(inode)->ii_btop >= 0) {
  26609. + spin_lock(&inode->i_lock);
  26610. + if (atomic_read(&inode->i_count)) {
  26611. + au_igrab(inode);
  26612. + *p++ = inode;
  26613. + n++;
  26614. + AuDebugOn(n > max);
  26615. + }
  26616. + spin_unlock(&inode->i_lock);
  26617. + }
  26618. + }
  26619. + spin_unlock(&sb->s_inode_list_lock);
  26620. +
  26621. + return n;
  26622. +}
  26623. +
  26624. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  26625. +{
  26626. + *max = au_ninodes(sb);
  26627. + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
  26628. +}
  26629. +
  26630. +void au_iarray_free(struct inode **a, unsigned long long max)
  26631. +{
  26632. + unsigned long long ull;
  26633. +
  26634. + for (ull = 0; ull < max; ull++)
  26635. + iput(a[ull]);
  26636. + kvfree(a);
  26637. +}
  26638. +
  26639. +/* ---------------------------------------------------------------------- */
  26640. +
  26641. +/*
  26642. + * refresh dentry and inode at remount time.
  26643. + */
  26644. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  26645. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  26646. + struct dentry *parent)
  26647. +{
  26648. + int err;
  26649. +
  26650. + di_write_lock_child(dentry);
  26651. + di_read_lock_parent(parent, AuLock_IR);
  26652. + err = au_refresh_dentry(dentry, parent);
  26653. + if (!err && dir_flags)
  26654. + au_hn_reset(d_inode(dentry), dir_flags);
  26655. + di_read_unlock(parent, AuLock_IR);
  26656. + di_write_unlock(dentry);
  26657. +
  26658. + return err;
  26659. +}
  26660. +
  26661. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  26662. + struct au_sbinfo *sbinfo,
  26663. + const unsigned int dir_flags, unsigned int do_idop)
  26664. +{
  26665. + int err;
  26666. + struct dentry *parent;
  26667. +
  26668. + err = 0;
  26669. + parent = dget_parent(dentry);
  26670. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  26671. + if (d_really_is_positive(dentry)) {
  26672. + if (!d_is_dir(dentry))
  26673. + err = au_do_refresh(dentry, /*dir_flags*/0,
  26674. + parent);
  26675. + else {
  26676. + err = au_do_refresh(dentry, dir_flags, parent);
  26677. + if (unlikely(err))
  26678. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  26679. + }
  26680. + } else
  26681. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  26682. + AuDbgDentry(dentry);
  26683. + }
  26684. + dput(parent);
  26685. +
  26686. + if (!err) {
  26687. + if (do_idop)
  26688. + au_refresh_dop(dentry, /*force_reval*/0);
  26689. + } else
  26690. + au_refresh_dop(dentry, /*force_reval*/1);
  26691. +
  26692. + AuTraceErr(err);
  26693. + return err;
  26694. +}
  26695. +
  26696. +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
  26697. +{
  26698. + int err, i, j, ndentry, e;
  26699. + unsigned int sigen;
  26700. + struct au_dcsub_pages dpages;
  26701. + struct au_dpage *dpage;
  26702. + struct dentry **dentries, *d;
  26703. + struct au_sbinfo *sbinfo;
  26704. + struct dentry *root = sb->s_root;
  26705. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  26706. +
  26707. + if (do_idop)
  26708. + au_refresh_dop(root, /*force_reval*/0);
  26709. +
  26710. + err = au_dpages_init(&dpages, GFP_NOFS);
  26711. + if (unlikely(err))
  26712. + goto out;
  26713. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  26714. + if (unlikely(err))
  26715. + goto out_dpages;
  26716. +
  26717. + sigen = au_sigen(sb);
  26718. + sbinfo = au_sbi(sb);
  26719. + for (i = 0; i < dpages.ndpage; i++) {
  26720. + dpage = dpages.dpages + i;
  26721. + dentries = dpage->dentries;
  26722. + ndentry = dpage->ndentry;
  26723. + for (j = 0; j < ndentry; j++) {
  26724. + d = dentries[j];
  26725. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
  26726. + do_idop);
  26727. + if (unlikely(e && !err))
  26728. + err = e;
  26729. + /* go on even err */
  26730. + }
  26731. + }
  26732. +
  26733. +out_dpages:
  26734. + au_dpages_free(&dpages);
  26735. +out:
  26736. + return err;
  26737. +}
  26738. +
  26739. +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
  26740. +{
  26741. + int err, e;
  26742. + unsigned int sigen;
  26743. + unsigned long long max, ull;
  26744. + struct inode *inode, **array;
  26745. +
  26746. + array = au_iarray_alloc(sb, &max);
  26747. + err = PTR_ERR(array);
  26748. + if (IS_ERR(array))
  26749. + goto out;
  26750. +
  26751. + err = 0;
  26752. + sigen = au_sigen(sb);
  26753. + for (ull = 0; ull < max; ull++) {
  26754. + inode = array[ull];
  26755. + if (unlikely(!inode))
  26756. + break;
  26757. +
  26758. + e = 0;
  26759. + ii_write_lock_child(inode);
  26760. + if (au_iigen(inode, NULL) != sigen) {
  26761. + e = au_refresh_hinode_self(inode);
  26762. + if (unlikely(e)) {
  26763. + au_refresh_iop(inode, /*force_getattr*/1);
  26764. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  26765. + if (!err)
  26766. + err = e;
  26767. + /* go on even if err */
  26768. + }
  26769. + }
  26770. + if (!e && do_idop)
  26771. + au_refresh_iop(inode, /*force_getattr*/0);
  26772. + ii_write_unlock(inode);
  26773. + }
  26774. +
  26775. + au_iarray_free(array, max);
  26776. +
  26777. +out:
  26778. + return err;
  26779. +}
  26780. +
  26781. +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
  26782. +{
  26783. + int err, e;
  26784. + unsigned int udba;
  26785. + aufs_bindex_t bindex, bbot;
  26786. + struct dentry *root;
  26787. + struct inode *inode;
  26788. + struct au_branch *br;
  26789. + struct au_sbinfo *sbi;
  26790. +
  26791. + au_sigen_inc(sb);
  26792. + sbi = au_sbi(sb);
  26793. + au_fclr_si(sbi, FAILED_REFRESH_DIR);
  26794. +
  26795. + root = sb->s_root;
  26796. + DiMustNoWaiters(root);
  26797. + inode = d_inode(root);
  26798. + IiMustNoWaiters(inode);
  26799. +
  26800. + udba = au_opt_udba(sb);
  26801. + bbot = au_sbbot(sb);
  26802. + for (bindex = 0; bindex <= bbot; bindex++) {
  26803. + br = au_sbr(sb, bindex);
  26804. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  26805. + if (unlikely(err))
  26806. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  26807. + bindex, err);
  26808. + /* go on even if err */
  26809. + }
  26810. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  26811. +
  26812. + if (do_idop) {
  26813. + if (au_ftest_si(sbi, NO_DREVAL)) {
  26814. + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
  26815. + sb->s_d_op = &aufs_dop_noreval;
  26816. + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
  26817. + sbi->si_iop_array = aufs_iop_nogetattr;
  26818. + } else {
  26819. + AuDebugOn(sb->s_d_op == &aufs_dop);
  26820. + sb->s_d_op = &aufs_dop;
  26821. + AuDebugOn(sbi->si_iop_array == aufs_iop);
  26822. + sbi->si_iop_array = aufs_iop;
  26823. + }
  26824. + pr_info("reset to %pf and %pf\n",
  26825. + sb->s_d_op, sbi->si_iop_array);
  26826. + }
  26827. +
  26828. + di_write_unlock(root);
  26829. + err = au_refresh_d(sb, do_idop);
  26830. + e = au_refresh_i(sb, do_idop);
  26831. + if (unlikely(e && !err))
  26832. + err = e;
  26833. + /* aufs_write_lock() calls ..._child() */
  26834. + di_write_lock_child(root);
  26835. +
  26836. + au_cpup_attr_all(inode, /*force*/1);
  26837. +
  26838. + if (unlikely(err))
  26839. + AuIOErr("refresh failed, ignored, %d\n", err);
  26840. +}
  26841. +
  26842. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  26843. +static int cvt_err(int err)
  26844. +{
  26845. + AuTraceErr(err);
  26846. +
  26847. + switch (err) {
  26848. + case -ENOENT:
  26849. + case -ENOTDIR:
  26850. + case -EEXIST:
  26851. + case -EIO:
  26852. + err = -EINVAL;
  26853. + }
  26854. + return err;
  26855. +}
  26856. +
  26857. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  26858. +{
  26859. + int err, do_dx;
  26860. + unsigned int mntflags;
  26861. + struct au_opts opts = {
  26862. + .opt = NULL
  26863. + };
  26864. + struct dentry *root;
  26865. + struct inode *inode;
  26866. + struct au_sbinfo *sbinfo;
  26867. +
  26868. + err = 0;
  26869. + root = sb->s_root;
  26870. + if (!data || !*data) {
  26871. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  26872. + if (!err) {
  26873. + di_write_lock_child(root);
  26874. + err = au_opts_verify(sb, *flags, /*pending*/0);
  26875. + aufs_write_unlock(root);
  26876. + }
  26877. + goto out;
  26878. + }
  26879. +
  26880. + err = -ENOMEM;
  26881. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  26882. + if (unlikely(!opts.opt))
  26883. + goto out;
  26884. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  26885. + opts.flags = AuOpts_REMOUNT;
  26886. + opts.sb_flags = *flags;
  26887. +
  26888. + /* parse it before aufs lock */
  26889. + err = au_opts_parse(sb, data, &opts);
  26890. + if (unlikely(err))
  26891. + goto out_opts;
  26892. +
  26893. + sbinfo = au_sbi(sb);
  26894. + inode = d_inode(root);
  26895. + mutex_lock(&inode->i_mutex);
  26896. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  26897. + if (unlikely(err))
  26898. + goto out_mtx;
  26899. + di_write_lock_child(root);
  26900. +
  26901. + /* au_opts_remount() may return an error */
  26902. + err = au_opts_remount(sb, &opts);
  26903. + au_opts_free(&opts);
  26904. +
  26905. + if (au_ftest_opts(opts.flags, REFRESH))
  26906. + au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
  26907. +
  26908. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  26909. + mntflags = au_mntflags(sb);
  26910. + do_dx = !!au_opt_test(mntflags, DIO);
  26911. + au_dy_arefresh(do_dx);
  26912. + }
  26913. +
  26914. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  26915. + aufs_write_unlock(root);
  26916. +
  26917. +out_mtx:
  26918. + mutex_unlock(&inode->i_mutex);
  26919. +out_opts:
  26920. + au_delayed_free_page((unsigned long)opts.opt);
  26921. +out:
  26922. + err = cvt_err(err);
  26923. + AuTraceErr(err);
  26924. + return err;
  26925. +}
  26926. +
  26927. +static const struct super_operations aufs_sop = {
  26928. + .alloc_inode = aufs_alloc_inode,
  26929. + .destroy_inode = aufs_destroy_inode,
  26930. + /* always deleting, no clearing */
  26931. + .drop_inode = generic_delete_inode,
  26932. + .show_options = aufs_show_options,
  26933. + .statfs = aufs_statfs,
  26934. + .put_super = aufs_put_super,
  26935. + .sync_fs = aufs_sync_fs,
  26936. + .remount_fs = aufs_remount_fs
  26937. +};
  26938. +
  26939. +/* ---------------------------------------------------------------------- */
  26940. +
  26941. +static int alloc_root(struct super_block *sb)
  26942. +{
  26943. + int err;
  26944. + struct inode *inode;
  26945. + struct dentry *root;
  26946. +
  26947. + err = -ENOMEM;
  26948. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  26949. + err = PTR_ERR(inode);
  26950. + if (IS_ERR(inode))
  26951. + goto out;
  26952. +
  26953. + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
  26954. + inode->i_fop = &aufs_dir_fop;
  26955. + inode->i_mode = S_IFDIR;
  26956. + set_nlink(inode, 2);
  26957. + unlock_new_inode(inode);
  26958. +
  26959. + root = d_make_root(inode);
  26960. + if (unlikely(!root))
  26961. + goto out;
  26962. + err = PTR_ERR(root);
  26963. + if (IS_ERR(root))
  26964. + goto out;
  26965. +
  26966. + err = au_di_init(root);
  26967. + if (!err) {
  26968. + sb->s_root = root;
  26969. + return 0; /* success */
  26970. + }
  26971. + dput(root);
  26972. +
  26973. +out:
  26974. + return err;
  26975. +}
  26976. +
  26977. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  26978. + int silent __maybe_unused)
  26979. +{
  26980. + int err;
  26981. + struct au_opts opts = {
  26982. + .opt = NULL
  26983. + };
  26984. + struct au_sbinfo *sbinfo;
  26985. + struct dentry *root;
  26986. + struct inode *inode;
  26987. + char *arg = raw_data;
  26988. +
  26989. + if (unlikely(!arg || !*arg)) {
  26990. + err = -EINVAL;
  26991. + pr_err("no arg\n");
  26992. + goto out;
  26993. + }
  26994. +
  26995. + err = -ENOMEM;
  26996. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  26997. + if (unlikely(!opts.opt))
  26998. + goto out;
  26999. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  27000. + opts.sb_flags = sb->s_flags;
  27001. +
  27002. + err = au_si_alloc(sb);
  27003. + if (unlikely(err))
  27004. + goto out_opts;
  27005. + sbinfo = au_sbi(sb);
  27006. +
  27007. + /* all timestamps always follow the ones on the branch */
  27008. + sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
  27009. + sb->s_op = &aufs_sop;
  27010. + sb->s_d_op = &aufs_dop;
  27011. + sb->s_magic = AUFS_SUPER_MAGIC;
  27012. + sb->s_maxbytes = 0;
  27013. + sb->s_stack_depth = 1;
  27014. + au_export_init(sb);
  27015. + /* au_xattr_init(sb); */
  27016. +
  27017. + err = alloc_root(sb);
  27018. + if (unlikely(err)) {
  27019. + si_write_unlock(sb);
  27020. + goto out_info;
  27021. + }
  27022. + root = sb->s_root;
  27023. + inode = d_inode(root);
  27024. +
  27025. + /*
  27026. + * actually we can parse options regardless aufs lock here.
  27027. + * but at remount time, parsing must be done before aufs lock.
  27028. + * so we follow the same rule.
  27029. + */
  27030. + ii_write_lock_parent(inode);
  27031. + aufs_write_unlock(root);
  27032. + err = au_opts_parse(sb, arg, &opts);
  27033. + if (unlikely(err))
  27034. + goto out_root;
  27035. +
  27036. + /* lock vfs_inode first, then aufs. */
  27037. + mutex_lock(&inode->i_mutex);
  27038. + aufs_write_lock(root);
  27039. + err = au_opts_mount(sb, &opts);
  27040. + au_opts_free(&opts);
  27041. + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
  27042. + sb->s_d_op = &aufs_dop_noreval;
  27043. + pr_info("%pf\n", sb->s_d_op);
  27044. + au_refresh_dop(root, /*force_reval*/0);
  27045. + sbinfo->si_iop_array = aufs_iop_nogetattr;
  27046. + au_refresh_iop(inode, /*force_getattr*/0);
  27047. + }
  27048. + aufs_write_unlock(root);
  27049. + mutex_unlock(&inode->i_mutex);
  27050. + if (!err)
  27051. + goto out_opts; /* success */
  27052. +
  27053. +out_root:
  27054. + dput(root);
  27055. + sb->s_root = NULL;
  27056. +out_info:
  27057. + dbgaufs_si_fin(sbinfo);
  27058. + kobject_put(&sbinfo->si_kobj);
  27059. + sb->s_fs_info = NULL;
  27060. +out_opts:
  27061. + au_delayed_free_page((unsigned long)opts.opt);
  27062. +out:
  27063. + AuTraceErr(err);
  27064. + err = cvt_err(err);
  27065. + AuTraceErr(err);
  27066. + return err;
  27067. +}
  27068. +
  27069. +/* ---------------------------------------------------------------------- */
  27070. +
  27071. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  27072. + const char *dev_name __maybe_unused,
  27073. + void *raw_data)
  27074. +{
  27075. + struct dentry *root;
  27076. + struct super_block *sb;
  27077. +
  27078. + /* all timestamps always follow the ones on the branch */
  27079. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  27080. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  27081. + if (IS_ERR(root))
  27082. + goto out;
  27083. +
  27084. + sb = root->d_sb;
  27085. + si_write_lock(sb, !AuLock_FLUSH);
  27086. + sysaufs_brs_add(sb, 0);
  27087. + si_write_unlock(sb);
  27088. + au_sbilist_add(sb);
  27089. +
  27090. +out:
  27091. + return root;
  27092. +}
  27093. +
  27094. +static void aufs_kill_sb(struct super_block *sb)
  27095. +{
  27096. + struct au_sbinfo *sbinfo;
  27097. +
  27098. + sbinfo = au_sbi(sb);
  27099. + if (sbinfo) {
  27100. + au_sbilist_del(sb);
  27101. + aufs_write_lock(sb->s_root);
  27102. + au_fhsm_fin(sb);
  27103. + if (sbinfo->si_wbr_create_ops->fin)
  27104. + sbinfo->si_wbr_create_ops->fin(sb);
  27105. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  27106. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  27107. + au_remount_refresh(sb, /*do_idop*/0);
  27108. + }
  27109. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  27110. + au_plink_put(sb, /*verbose*/1);
  27111. + au_xino_clr(sb);
  27112. + sbinfo->si_sb = NULL;
  27113. + aufs_write_unlock(sb->s_root);
  27114. + au_nwt_flush(&sbinfo->si_nowait);
  27115. + }
  27116. + kill_anon_super(sb);
  27117. +}
  27118. +
  27119. +struct file_system_type aufs_fs_type = {
  27120. + .name = AUFS_FSTYPE,
  27121. + /* a race between rename and others */
  27122. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  27123. + .mount = aufs_mount,
  27124. + .kill_sb = aufs_kill_sb,
  27125. + /* no need to __module_get() and module_put(). */
  27126. + .owner = THIS_MODULE,
  27127. +};
  27128. --- /dev/null
  27129. +++ linux-4.4/fs/aufs/super.h 2016-10-23 11:20:57.642159951 +0300
  27130. @@ -0,0 +1,641 @@
  27131. +/*
  27132. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27133. + *
  27134. + * This program, aufs is free software; you can redistribute it and/or modify
  27135. + * it under the terms of the GNU General Public License as published by
  27136. + * the Free Software Foundation; either version 2 of the License, or
  27137. + * (at your option) any later version.
  27138. + *
  27139. + * This program is distributed in the hope that it will be useful,
  27140. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27141. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27142. + * GNU General Public License for more details.
  27143. + *
  27144. + * You should have received a copy of the GNU General Public License
  27145. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27146. + */
  27147. +
  27148. +/*
  27149. + * super_block operations
  27150. + */
  27151. +
  27152. +#ifndef __AUFS_SUPER_H__
  27153. +#define __AUFS_SUPER_H__
  27154. +
  27155. +#ifdef __KERNEL__
  27156. +
  27157. +#include <linux/fs.h>
  27158. +#include <linux/kobject.h>
  27159. +#include "rwsem.h"
  27160. +#include "spl.h"
  27161. +#include "wkq.h"
  27162. +
  27163. +/* policies to select one among multiple writable branches */
  27164. +struct au_wbr_copyup_operations {
  27165. + int (*copyup)(struct dentry *dentry);
  27166. +};
  27167. +
  27168. +#define AuWbr_DIR 1 /* target is a dir */
  27169. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  27170. +
  27171. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  27172. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  27173. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  27174. +
  27175. +struct au_wbr_create_operations {
  27176. + int (*create)(struct dentry *dentry, unsigned int flags);
  27177. + int (*init)(struct super_block *sb);
  27178. + int (*fin)(struct super_block *sb);
  27179. +};
  27180. +
  27181. +struct au_wbr_mfs {
  27182. + struct mutex mfs_lock; /* protect this structure */
  27183. + unsigned long mfs_jiffy;
  27184. + unsigned long mfs_expire;
  27185. + aufs_bindex_t mfs_bindex;
  27186. +
  27187. + unsigned long long mfsrr_bytes;
  27188. + unsigned long long mfsrr_watermark;
  27189. +};
  27190. +
  27191. +#define AuPlink_NHASH 100
  27192. +static inline int au_plink_hash(ino_t ino)
  27193. +{
  27194. + return ino % AuPlink_NHASH;
  27195. +}
  27196. +
  27197. +/* File-based Hierarchical Storage Management */
  27198. +struct au_fhsm {
  27199. +#ifdef CONFIG_AUFS_FHSM
  27200. + /* allow only one process who can receive the notification */
  27201. + spinlock_t fhsm_spin;
  27202. + pid_t fhsm_pid;
  27203. + wait_queue_head_t fhsm_wqh;
  27204. + atomic_t fhsm_readable;
  27205. +
  27206. + /* these are protected by si_rwsem */
  27207. + unsigned long fhsm_expire;
  27208. + aufs_bindex_t fhsm_bottom;
  27209. +#endif
  27210. +};
  27211. +
  27212. +#define AU_PIDSTEP (int)(BITS_TO_LONGS(PID_MAX_DEFAULT) * BITS_PER_LONG)
  27213. +#define AU_NPIDMAP (int)DIV_ROUND_UP(PID_MAX_LIMIT, AU_PIDSTEP)
  27214. +struct au_si_pid {
  27215. + unsigned long *pid_bitmap[AU_NPIDMAP];
  27216. + struct mutex pid_mtx;
  27217. +};
  27218. +
  27219. +struct au_branch;
  27220. +struct au_sbinfo {
  27221. + /* nowait tasks in the system-wide workqueue */
  27222. + struct au_nowait_tasks si_nowait;
  27223. +
  27224. + /*
  27225. + * tried sb->s_umount, but failed due to the dependecy between i_mutex.
  27226. + * rwsem for au_sbinfo is necessary.
  27227. + */
  27228. + struct au_rwsem si_rwsem;
  27229. +
  27230. + /* prevent recursive locking in deleting inode */
  27231. + struct au_si_pid au_si_pid;
  27232. +
  27233. + /*
  27234. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  27235. + * remount.
  27236. + */
  27237. + struct percpu_counter si_ninodes, si_nfiles;
  27238. +
  27239. + /* branch management */
  27240. + unsigned int si_generation;
  27241. +
  27242. + /* see AuSi_ flags */
  27243. + unsigned char au_si_status;
  27244. +
  27245. + aufs_bindex_t si_bbot;
  27246. +
  27247. + /* dirty trick to keep br_id plus */
  27248. + unsigned int si_last_br_id :
  27249. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  27250. + struct au_branch **si_branch;
  27251. +
  27252. + /* policy to select a writable branch */
  27253. + unsigned char si_wbr_copyup;
  27254. + unsigned char si_wbr_create;
  27255. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  27256. + struct au_wbr_create_operations *si_wbr_create_ops;
  27257. +
  27258. + /* round robin */
  27259. + atomic_t si_wbr_rr_next;
  27260. +
  27261. + /* most free space */
  27262. + struct au_wbr_mfs si_wbr_mfs;
  27263. +
  27264. + /* File-based Hierarchical Storage Management */
  27265. + struct au_fhsm si_fhsm;
  27266. +
  27267. + /* mount flags */
  27268. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  27269. + unsigned int si_mntflags;
  27270. +
  27271. + /* symlink to follow_link() and put_link() */
  27272. + struct au_sphlhead si_symlink;
  27273. +
  27274. + /* external inode number (bitmap and translation table) */
  27275. + vfs_readf_t si_xread;
  27276. + vfs_writef_t si_xwrite;
  27277. + struct file *si_xib;
  27278. + struct mutex si_xib_mtx; /* protect xib members */
  27279. + unsigned long *si_xib_buf;
  27280. + unsigned long si_xib_last_pindex;
  27281. + int si_xib_next_bit;
  27282. + aufs_bindex_t si_xino_brid;
  27283. + unsigned long si_xino_jiffy;
  27284. + unsigned long si_xino_expire;
  27285. + /* reserved for future use */
  27286. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  27287. +
  27288. +#ifdef CONFIG_AUFS_EXPORT
  27289. + /* i_generation */
  27290. + struct file *si_xigen;
  27291. + atomic_t si_xigen_next;
  27292. +#endif
  27293. +
  27294. + /* dirty trick to suppoer atomic_open */
  27295. + struct au_sphlhead si_aopen;
  27296. +
  27297. + /* vdir parameters */
  27298. + unsigned long si_rdcache; /* max cache time in jiffies */
  27299. + unsigned int si_rdblk; /* deblk size */
  27300. + unsigned int si_rdhash; /* hash size */
  27301. +
  27302. + /*
  27303. + * If the number of whiteouts are larger than si_dirwh, leave all of
  27304. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  27305. + * future fsck.aufs or kernel thread will remove them later.
  27306. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  27307. + */
  27308. + unsigned int si_dirwh;
  27309. +
  27310. + /* pseudo_link list */
  27311. + struct au_sphlhead si_plink[AuPlink_NHASH];
  27312. + wait_queue_head_t si_plink_wq;
  27313. + spinlock_t si_plink_maint_lock;
  27314. + pid_t si_plink_maint_pid;
  27315. +
  27316. + /* file list */
  27317. + struct au_sphlhead si_files;
  27318. +
  27319. + /* with/without getattr, brother of sb->s_d_op */
  27320. + struct inode_operations *si_iop_array;
  27321. +
  27322. + /*
  27323. + * sysfs and lifetime management.
  27324. + * this is not a small structure and it may be a waste of memory in case
  27325. + * of sysfs is disabled, particulary when many aufs-es are mounted.
  27326. + * but using sysfs is majority.
  27327. + */
  27328. + struct kobject si_kobj;
  27329. +#ifdef CONFIG_DEBUG_FS
  27330. + struct dentry *si_dbgaufs;
  27331. + struct dentry *si_dbgaufs_plink;
  27332. + struct dentry *si_dbgaufs_xib;
  27333. +#ifdef CONFIG_AUFS_EXPORT
  27334. + struct dentry *si_dbgaufs_xigen;
  27335. +#endif
  27336. +#endif
  27337. +
  27338. +#ifdef CONFIG_AUFS_SBILIST
  27339. + struct hlist_node si_list;
  27340. +#endif
  27341. +
  27342. + /* dirty, necessary for unmounting, sysfs and sysrq */
  27343. + struct super_block *si_sb;
  27344. +};
  27345. +
  27346. +/* sbinfo status flags */
  27347. +/*
  27348. + * set true when refresh_dirs() failed at remount time.
  27349. + * then try refreshing dirs at access time again.
  27350. + * if it is false, refreshing dirs at access time is unnecesary
  27351. + */
  27352. +#define AuSi_FAILED_REFRESH_DIR 1
  27353. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  27354. +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
  27355. +
  27356. +#ifndef CONFIG_AUFS_FHSM
  27357. +#undef AuSi_FHSM
  27358. +#define AuSi_FHSM 0
  27359. +#endif
  27360. +
  27361. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  27362. + unsigned int flag)
  27363. +{
  27364. + AuRwMustAnyLock(&sbi->si_rwsem);
  27365. + return sbi->au_si_status & flag;
  27366. +}
  27367. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  27368. +#define au_fset_si(sbinfo, name) do { \
  27369. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  27370. + (sbinfo)->au_si_status |= AuSi_##name; \
  27371. +} while (0)
  27372. +#define au_fclr_si(sbinfo, name) do { \
  27373. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  27374. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  27375. +} while (0)
  27376. +
  27377. +/* ---------------------------------------------------------------------- */
  27378. +
  27379. +/* policy to select one among writable branches */
  27380. +#define AuWbrCopyup(sbinfo, ...) \
  27381. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  27382. +#define AuWbrCreate(sbinfo, ...) \
  27383. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  27384. +
  27385. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  27386. +#define AuLock_DW 1 /* write-lock dentry */
  27387. +#define AuLock_IR (1 << 1) /* read-lock inode */
  27388. +#define AuLock_IW (1 << 2) /* write-lock inode */
  27389. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  27390. +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
  27391. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  27392. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  27393. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  27394. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  27395. +#define au_fset_lock(flags, name) \
  27396. + do { (flags) |= AuLock_##name; } while (0)
  27397. +#define au_fclr_lock(flags, name) \
  27398. + do { (flags) &= ~AuLock_##name; } while (0)
  27399. +
  27400. +/* ---------------------------------------------------------------------- */
  27401. +
  27402. +/* super.c */
  27403. +extern struct file_system_type aufs_fs_type;
  27404. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  27405. +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
  27406. + unsigned long long max, void *arg);
  27407. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  27408. + struct super_block *sb, void *arg);
  27409. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  27410. +void au_iarray_free(struct inode **a, unsigned long long max);
  27411. +
  27412. +/* sbinfo.c */
  27413. +void au_si_free(struct kobject *kobj);
  27414. +int au_si_alloc(struct super_block *sb);
  27415. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
  27416. +
  27417. +unsigned int au_sigen_inc(struct super_block *sb);
  27418. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  27419. +
  27420. +int si_read_lock(struct super_block *sb, int flags);
  27421. +int si_write_lock(struct super_block *sb, int flags);
  27422. +int aufs_read_lock(struct dentry *dentry, int flags);
  27423. +void aufs_read_unlock(struct dentry *dentry, int flags);
  27424. +void aufs_write_lock(struct dentry *dentry);
  27425. +void aufs_write_unlock(struct dentry *dentry);
  27426. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  27427. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  27428. +
  27429. +/* wbr_policy.c */
  27430. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  27431. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  27432. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  27433. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  27434. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
  27435. +
  27436. +/* mvdown.c */
  27437. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  27438. +
  27439. +#ifdef CONFIG_AUFS_FHSM
  27440. +/* fhsm.c */
  27441. +
  27442. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  27443. +{
  27444. + pid_t pid;
  27445. +
  27446. + spin_lock(&fhsm->fhsm_spin);
  27447. + pid = fhsm->fhsm_pid;
  27448. + spin_unlock(&fhsm->fhsm_spin);
  27449. +
  27450. + return pid;
  27451. +}
  27452. +
  27453. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  27454. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  27455. +int au_fhsm_fd(struct super_block *sb, int oflags);
  27456. +int au_fhsm_br_alloc(struct au_branch *br);
  27457. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  27458. +void au_fhsm_fin(struct super_block *sb);
  27459. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  27460. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  27461. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  27462. +#else
  27463. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  27464. + int force)
  27465. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  27466. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  27467. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  27468. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  27469. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  27470. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  27471. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  27472. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  27473. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  27474. +#endif
  27475. +
  27476. +/* ---------------------------------------------------------------------- */
  27477. +
  27478. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  27479. +{
  27480. + return sb->s_fs_info;
  27481. +}
  27482. +
  27483. +/* ---------------------------------------------------------------------- */
  27484. +
  27485. +#ifdef CONFIG_AUFS_EXPORT
  27486. +int au_test_nfsd(void);
  27487. +void au_export_init(struct super_block *sb);
  27488. +void au_xigen_inc(struct inode *inode);
  27489. +int au_xigen_new(struct inode *inode);
  27490. +int au_xigen_set(struct super_block *sb, struct file *base);
  27491. +void au_xigen_clr(struct super_block *sb);
  27492. +
  27493. +static inline int au_busy_or_stale(void)
  27494. +{
  27495. + if (!au_test_nfsd())
  27496. + return -EBUSY;
  27497. + return -ESTALE;
  27498. +}
  27499. +#else
  27500. +AuStubInt0(au_test_nfsd, void)
  27501. +AuStubVoid(au_export_init, struct super_block *sb)
  27502. +AuStubVoid(au_xigen_inc, struct inode *inode)
  27503. +AuStubInt0(au_xigen_new, struct inode *inode)
  27504. +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
  27505. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  27506. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  27507. +#endif /* CONFIG_AUFS_EXPORT */
  27508. +
  27509. +/* ---------------------------------------------------------------------- */
  27510. +
  27511. +#ifdef CONFIG_AUFS_SBILIST
  27512. +/* module.c */
  27513. +extern struct au_sphlhead au_sbilist;
  27514. +
  27515. +static inline void au_sbilist_init(void)
  27516. +{
  27517. + au_sphl_init(&au_sbilist);
  27518. +}
  27519. +
  27520. +static inline void au_sbilist_add(struct super_block *sb)
  27521. +{
  27522. + au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
  27523. +}
  27524. +
  27525. +static inline void au_sbilist_del(struct super_block *sb)
  27526. +{
  27527. + au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
  27528. +}
  27529. +
  27530. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  27531. +static inline void au_sbilist_lock(void)
  27532. +{
  27533. + spin_lock(&au_sbilist.spin);
  27534. +}
  27535. +
  27536. +static inline void au_sbilist_unlock(void)
  27537. +{
  27538. + spin_unlock(&au_sbilist.spin);
  27539. +}
  27540. +#define AuGFP_SBILIST GFP_ATOMIC
  27541. +#else
  27542. +AuStubVoid(au_sbilist_lock, void)
  27543. +AuStubVoid(au_sbilist_unlock, void)
  27544. +#define AuGFP_SBILIST GFP_NOFS
  27545. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  27546. +#else
  27547. +AuStubVoid(au_sbilist_init, void)
  27548. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  27549. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  27550. +AuStubVoid(au_sbilist_lock, void)
  27551. +AuStubVoid(au_sbilist_unlock, void)
  27552. +#define AuGFP_SBILIST GFP_NOFS
  27553. +#endif
  27554. +
  27555. +/* ---------------------------------------------------------------------- */
  27556. +
  27557. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  27558. +{
  27559. + /*
  27560. + * This function is a dynamic '__init' function actually,
  27561. + * so the tiny check for si_rwsem is unnecessary.
  27562. + */
  27563. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  27564. +#ifdef CONFIG_DEBUG_FS
  27565. + sbinfo->si_dbgaufs = NULL;
  27566. + sbinfo->si_dbgaufs_plink = NULL;
  27567. + sbinfo->si_dbgaufs_xib = NULL;
  27568. +#ifdef CONFIG_AUFS_EXPORT
  27569. + sbinfo->si_dbgaufs_xigen = NULL;
  27570. +#endif
  27571. +#endif
  27572. +}
  27573. +
  27574. +/* ---------------------------------------------------------------------- */
  27575. +
  27576. +static inline void si_pid_idx_bit(int *idx, pid_t *bit)
  27577. +{
  27578. + /* the origin of pid is 1, but the bitmap's is 0 */
  27579. + *bit = current->pid - 1;
  27580. + *idx = *bit / AU_PIDSTEP;
  27581. + *bit %= AU_PIDSTEP;
  27582. +}
  27583. +
  27584. +static inline int si_pid_test(struct super_block *sb)
  27585. +{
  27586. + pid_t bit;
  27587. + int idx;
  27588. + unsigned long *bitmap;
  27589. +
  27590. + si_pid_idx_bit(&idx, &bit);
  27591. + bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
  27592. + if (bitmap)
  27593. + return test_bit(bit, bitmap);
  27594. + return 0;
  27595. +}
  27596. +
  27597. +static inline void si_pid_clr(struct super_block *sb)
  27598. +{
  27599. + pid_t bit;
  27600. + int idx;
  27601. + unsigned long *bitmap;
  27602. +
  27603. + si_pid_idx_bit(&idx, &bit);
  27604. + bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
  27605. + BUG_ON(!bitmap);
  27606. + AuDebugOn(!test_bit(bit, bitmap));
  27607. + clear_bit(bit, bitmap);
  27608. + /* smp_mb(); */
  27609. +}
  27610. +
  27611. +void si_pid_set(struct super_block *sb);
  27612. +
  27613. +/* ---------------------------------------------------------------------- */
  27614. +
  27615. +/* lock superblock. mainly for entry point functions */
  27616. +/*
  27617. + * __si_read_lock, __si_write_lock,
  27618. + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
  27619. + */
  27620. +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
  27621. +
  27622. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  27623. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  27624. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  27625. +
  27626. +static inline void si_noflush_read_lock(struct super_block *sb)
  27627. +{
  27628. + __si_read_lock(sb);
  27629. + si_pid_set(sb);
  27630. +}
  27631. +
  27632. +static inline int si_noflush_read_trylock(struct super_block *sb)
  27633. +{
  27634. + int locked;
  27635. +
  27636. + locked = __si_read_trylock(sb);
  27637. + if (locked)
  27638. + si_pid_set(sb);
  27639. + return locked;
  27640. +}
  27641. +
  27642. +static inline void si_noflush_write_lock(struct super_block *sb)
  27643. +{
  27644. + __si_write_lock(sb);
  27645. + si_pid_set(sb);
  27646. +}
  27647. +
  27648. +static inline int si_noflush_write_trylock(struct super_block *sb)
  27649. +{
  27650. + int locked;
  27651. +
  27652. + locked = __si_write_trylock(sb);
  27653. + if (locked)
  27654. + si_pid_set(sb);
  27655. + return locked;
  27656. +}
  27657. +
  27658. +#if 0 /* reserved */
  27659. +static inline int si_read_trylock(struct super_block *sb, int flags)
  27660. +{
  27661. + if (au_ftest_lock(flags, FLUSH))
  27662. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  27663. + return si_noflush_read_trylock(sb);
  27664. +}
  27665. +#endif
  27666. +
  27667. +static inline void si_read_unlock(struct super_block *sb)
  27668. +{
  27669. + si_pid_clr(sb);
  27670. + __si_read_unlock(sb);
  27671. +}
  27672. +
  27673. +#if 0 /* reserved */
  27674. +static inline int si_write_trylock(struct super_block *sb, int flags)
  27675. +{
  27676. + if (au_ftest_lock(flags, FLUSH))
  27677. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  27678. + return si_noflush_write_trylock(sb);
  27679. +}
  27680. +#endif
  27681. +
  27682. +static inline void si_write_unlock(struct super_block *sb)
  27683. +{
  27684. + si_pid_clr(sb);
  27685. + __si_write_unlock(sb);
  27686. +}
  27687. +
  27688. +#if 0 /* reserved */
  27689. +static inline void si_downgrade_lock(struct super_block *sb)
  27690. +{
  27691. + __si_downgrade_lock(sb);
  27692. +}
  27693. +#endif
  27694. +
  27695. +/* ---------------------------------------------------------------------- */
  27696. +
  27697. +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
  27698. +{
  27699. + SiMustAnyLock(sb);
  27700. + return au_sbi(sb)->si_bbot;
  27701. +}
  27702. +
  27703. +static inline unsigned int au_mntflags(struct super_block *sb)
  27704. +{
  27705. + SiMustAnyLock(sb);
  27706. + return au_sbi(sb)->si_mntflags;
  27707. +}
  27708. +
  27709. +static inline unsigned int au_sigen(struct super_block *sb)
  27710. +{
  27711. + SiMustAnyLock(sb);
  27712. + return au_sbi(sb)->si_generation;
  27713. +}
  27714. +
  27715. +static inline unsigned long long au_ninodes(struct super_block *sb)
  27716. +{
  27717. + s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
  27718. +
  27719. + BUG_ON(n < 0);
  27720. + return n;
  27721. +}
  27722. +
  27723. +static inline void au_ninodes_inc(struct super_block *sb)
  27724. +{
  27725. + percpu_counter_inc(&au_sbi(sb)->si_ninodes);
  27726. +}
  27727. +
  27728. +static inline void au_ninodes_dec(struct super_block *sb)
  27729. +{
  27730. + percpu_counter_dec(&au_sbi(sb)->si_ninodes);
  27731. +}
  27732. +
  27733. +static inline unsigned long long au_nfiles(struct super_block *sb)
  27734. +{
  27735. + s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
  27736. +
  27737. + BUG_ON(n < 0);
  27738. + return n;
  27739. +}
  27740. +
  27741. +static inline void au_nfiles_inc(struct super_block *sb)
  27742. +{
  27743. + percpu_counter_inc(&au_sbi(sb)->si_nfiles);
  27744. +}
  27745. +
  27746. +static inline void au_nfiles_dec(struct super_block *sb)
  27747. +{
  27748. + percpu_counter_dec(&au_sbi(sb)->si_nfiles);
  27749. +}
  27750. +
  27751. +static inline struct au_branch *au_sbr(struct super_block *sb,
  27752. + aufs_bindex_t bindex)
  27753. +{
  27754. + SiMustAnyLock(sb);
  27755. + return au_sbi(sb)->si_branch[0 + bindex];
  27756. +}
  27757. +
  27758. +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
  27759. +{
  27760. + SiMustWriteLock(sb);
  27761. + au_sbi(sb)->si_xino_brid = brid;
  27762. +}
  27763. +
  27764. +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
  27765. +{
  27766. + SiMustAnyLock(sb);
  27767. + return au_sbi(sb)->si_xino_brid;
  27768. +}
  27769. +
  27770. +#endif /* __KERNEL__ */
  27771. +#endif /* __AUFS_SUPER_H__ */
  27772. --- /dev/null
  27773. +++ linux-4.4/fs/aufs/sysaufs.c 2016-10-23 11:20:57.642159951 +0300
  27774. @@ -0,0 +1,104 @@
  27775. +/*
  27776. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27777. + *
  27778. + * This program, aufs is free software; you can redistribute it and/or modify
  27779. + * it under the terms of the GNU General Public License as published by
  27780. + * the Free Software Foundation; either version 2 of the License, or
  27781. + * (at your option) any later version.
  27782. + *
  27783. + * This program is distributed in the hope that it will be useful,
  27784. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27785. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27786. + * GNU General Public License for more details.
  27787. + *
  27788. + * You should have received a copy of the GNU General Public License
  27789. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27790. + */
  27791. +
  27792. +/*
  27793. + * sysfs interface and lifetime management
  27794. + * they are necessary regardless sysfs is disabled.
  27795. + */
  27796. +
  27797. +#include <linux/random.h>
  27798. +#include "aufs.h"
  27799. +
  27800. +unsigned long sysaufs_si_mask;
  27801. +struct kset *sysaufs_kset;
  27802. +
  27803. +#define AuSiAttr(_name) { \
  27804. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  27805. + .show = sysaufs_si_##_name, \
  27806. +}
  27807. +
  27808. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  27809. +struct attribute *sysaufs_si_attrs[] = {
  27810. + &sysaufs_si_attr_xi_path.attr,
  27811. + NULL,
  27812. +};
  27813. +
  27814. +static const struct sysfs_ops au_sbi_ops = {
  27815. + .show = sysaufs_si_show
  27816. +};
  27817. +
  27818. +static struct kobj_type au_sbi_ktype = {
  27819. + .release = au_si_free,
  27820. + .sysfs_ops = &au_sbi_ops,
  27821. + .default_attrs = sysaufs_si_attrs
  27822. +};
  27823. +
  27824. +/* ---------------------------------------------------------------------- */
  27825. +
  27826. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  27827. +{
  27828. + int err;
  27829. +
  27830. + sbinfo->si_kobj.kset = sysaufs_kset;
  27831. + /* cf. sysaufs_name() */
  27832. + err = kobject_init_and_add
  27833. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  27834. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  27835. +
  27836. + dbgaufs_si_null(sbinfo);
  27837. + if (!err) {
  27838. + err = dbgaufs_si_init(sbinfo);
  27839. + if (unlikely(err))
  27840. + kobject_put(&sbinfo->si_kobj);
  27841. + }
  27842. + return err;
  27843. +}
  27844. +
  27845. +void sysaufs_fin(void)
  27846. +{
  27847. + dbgaufs_fin();
  27848. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  27849. + kset_unregister(sysaufs_kset);
  27850. +}
  27851. +
  27852. +int __init sysaufs_init(void)
  27853. +{
  27854. + int err;
  27855. +
  27856. + do {
  27857. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  27858. + } while (!sysaufs_si_mask);
  27859. +
  27860. + err = -EINVAL;
  27861. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  27862. + if (unlikely(!sysaufs_kset))
  27863. + goto out;
  27864. + err = PTR_ERR(sysaufs_kset);
  27865. + if (IS_ERR(sysaufs_kset))
  27866. + goto out;
  27867. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  27868. + if (unlikely(err)) {
  27869. + kset_unregister(sysaufs_kset);
  27870. + goto out;
  27871. + }
  27872. +
  27873. + err = dbgaufs_init();
  27874. + if (unlikely(err))
  27875. + sysaufs_fin();
  27876. +out:
  27877. + return err;
  27878. +}
  27879. --- /dev/null
  27880. +++ linux-4.4/fs/aufs/sysaufs.h 2016-10-23 11:20:57.642159951 +0300
  27881. @@ -0,0 +1,101 @@
  27882. +/*
  27883. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27884. + *
  27885. + * This program, aufs is free software; you can redistribute it and/or modify
  27886. + * it under the terms of the GNU General Public License as published by
  27887. + * the Free Software Foundation; either version 2 of the License, or
  27888. + * (at your option) any later version.
  27889. + *
  27890. + * This program is distributed in the hope that it will be useful,
  27891. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27892. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27893. + * GNU General Public License for more details.
  27894. + *
  27895. + * You should have received a copy of the GNU General Public License
  27896. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27897. + */
  27898. +
  27899. +/*
  27900. + * sysfs interface and mount lifetime management
  27901. + */
  27902. +
  27903. +#ifndef __SYSAUFS_H__
  27904. +#define __SYSAUFS_H__
  27905. +
  27906. +#ifdef __KERNEL__
  27907. +
  27908. +#include <linux/sysfs.h>
  27909. +#include "module.h"
  27910. +
  27911. +struct super_block;
  27912. +struct au_sbinfo;
  27913. +
  27914. +struct sysaufs_si_attr {
  27915. + struct attribute attr;
  27916. + int (*show)(struct seq_file *seq, struct super_block *sb);
  27917. +};
  27918. +
  27919. +/* ---------------------------------------------------------------------- */
  27920. +
  27921. +/* sysaufs.c */
  27922. +extern unsigned long sysaufs_si_mask;
  27923. +extern struct kset *sysaufs_kset;
  27924. +extern struct attribute *sysaufs_si_attrs[];
  27925. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  27926. +int __init sysaufs_init(void);
  27927. +void sysaufs_fin(void);
  27928. +
  27929. +/* ---------------------------------------------------------------------- */
  27930. +
  27931. +/* some people doesn't like to show a pointer in kernel */
  27932. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  27933. +{
  27934. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  27935. +}
  27936. +
  27937. +#define SysaufsSiNamePrefix "si_"
  27938. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  27939. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  27940. +{
  27941. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  27942. + sysaufs_si_id(sbinfo));
  27943. +}
  27944. +
  27945. +struct au_branch;
  27946. +#ifdef CONFIG_SYSFS
  27947. +/* sysfs.c */
  27948. +extern struct attribute_group *sysaufs_attr_group;
  27949. +
  27950. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  27951. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  27952. + char *buf);
  27953. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  27954. +#ifdef CONFIG_COMPAT
  27955. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  27956. +#endif
  27957. +
  27958. +void sysaufs_br_init(struct au_branch *br);
  27959. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  27960. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  27961. +
  27962. +#define sysaufs_brs_init() do {} while (0)
  27963. +
  27964. +#else
  27965. +#define sysaufs_attr_group NULL
  27966. +
  27967. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  27968. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  27969. + struct attribute *attr, char *buf)
  27970. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  27971. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  27972. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  27973. +
  27974. +static inline void sysaufs_brs_init(void)
  27975. +{
  27976. + sysaufs_brs = 0;
  27977. +}
  27978. +
  27979. +#endif /* CONFIG_SYSFS */
  27980. +
  27981. +#endif /* __KERNEL__ */
  27982. +#endif /* __SYSAUFS_H__ */
  27983. --- /dev/null
  27984. +++ linux-4.4/fs/aufs/sysfs.c 2016-10-23 11:20:57.642159951 +0300
  27985. @@ -0,0 +1,376 @@
  27986. +/*
  27987. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  27988. + *
  27989. + * This program, aufs is free software; you can redistribute it and/or modify
  27990. + * it under the terms of the GNU General Public License as published by
  27991. + * the Free Software Foundation; either version 2 of the License, or
  27992. + * (at your option) any later version.
  27993. + *
  27994. + * This program is distributed in the hope that it will be useful,
  27995. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27996. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27997. + * GNU General Public License for more details.
  27998. + *
  27999. + * You should have received a copy of the GNU General Public License
  28000. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28001. + */
  28002. +
  28003. +/*
  28004. + * sysfs interface
  28005. + */
  28006. +
  28007. +#include <linux/compat.h>
  28008. +#include <linux/seq_file.h>
  28009. +#include "aufs.h"
  28010. +
  28011. +#ifdef CONFIG_AUFS_FS_MODULE
  28012. +/* this entry violates the "one line per file" policy of sysfs */
  28013. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  28014. + char *buf)
  28015. +{
  28016. + ssize_t err;
  28017. + static char *conf =
  28018. +/* this file is generated at compiling */
  28019. +#include "conf.str"
  28020. + ;
  28021. +
  28022. + err = snprintf(buf, PAGE_SIZE, conf);
  28023. + if (unlikely(err >= PAGE_SIZE))
  28024. + err = -EFBIG;
  28025. + return err;
  28026. +}
  28027. +
  28028. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  28029. +#endif
  28030. +
  28031. +static struct attribute *au_attr[] = {
  28032. +#ifdef CONFIG_AUFS_FS_MODULE
  28033. + &au_config_attr.attr,
  28034. +#endif
  28035. + NULL, /* need to NULL terminate the list of attributes */
  28036. +};
  28037. +
  28038. +static struct attribute_group sysaufs_attr_group_body = {
  28039. + .attrs = au_attr
  28040. +};
  28041. +
  28042. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  28043. +
  28044. +/* ---------------------------------------------------------------------- */
  28045. +
  28046. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  28047. +{
  28048. + int err;
  28049. +
  28050. + SiMustAnyLock(sb);
  28051. +
  28052. + err = 0;
  28053. + if (au_opt_test(au_mntflags(sb), XINO)) {
  28054. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  28055. + seq_putc(seq, '\n');
  28056. + }
  28057. + return err;
  28058. +}
  28059. +
  28060. +/*
  28061. + * the lifetime of branch is independent from the entry under sysfs.
  28062. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  28063. + * unlinked.
  28064. + */
  28065. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  28066. + aufs_bindex_t bindex, int idx)
  28067. +{
  28068. + int err;
  28069. + struct path path;
  28070. + struct dentry *root;
  28071. + struct au_branch *br;
  28072. + au_br_perm_str_t perm;
  28073. +
  28074. + AuDbg("b%d\n", bindex);
  28075. +
  28076. + err = 0;
  28077. + root = sb->s_root;
  28078. + di_read_lock_parent(root, !AuLock_IR);
  28079. + br = au_sbr(sb, bindex);
  28080. +
  28081. + switch (idx) {
  28082. + case AuBrSysfs_BR:
  28083. + path.mnt = au_br_mnt(br);
  28084. + path.dentry = au_h_dptr(root, bindex);
  28085. + err = au_seq_path(seq, &path);
  28086. + if (!err) {
  28087. + au_optstr_br_perm(&perm, br->br_perm);
  28088. + seq_printf(seq, "=%s\n", perm.a);
  28089. + }
  28090. + break;
  28091. + case AuBrSysfs_BRID:
  28092. + seq_printf(seq, "%d\n", br->br_id);
  28093. + break;
  28094. + }
  28095. + di_read_unlock(root, !AuLock_IR);
  28096. + if (unlikely(err || seq_has_overflowed(seq)))
  28097. + err = -E2BIG;
  28098. +
  28099. + return err;
  28100. +}
  28101. +
  28102. +/* ---------------------------------------------------------------------- */
  28103. +
  28104. +static struct seq_file *au_seq(char *p, ssize_t len)
  28105. +{
  28106. + struct seq_file *seq;
  28107. +
  28108. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  28109. + if (seq) {
  28110. + /* mutex_init(&seq.lock); */
  28111. + seq->buf = p;
  28112. + seq->size = len;
  28113. + return seq; /* success */
  28114. + }
  28115. +
  28116. + seq = ERR_PTR(-ENOMEM);
  28117. + return seq;
  28118. +}
  28119. +
  28120. +#define SysaufsBr_PREFIX "br"
  28121. +#define SysaufsBrid_PREFIX "brid"
  28122. +
  28123. +/* todo: file size may exceed PAGE_SIZE */
  28124. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  28125. + char *buf)
  28126. +{
  28127. + ssize_t err;
  28128. + int idx;
  28129. + long l;
  28130. + aufs_bindex_t bbot;
  28131. + struct au_sbinfo *sbinfo;
  28132. + struct super_block *sb;
  28133. + struct seq_file *seq;
  28134. + char *name;
  28135. + struct attribute **cattr;
  28136. +
  28137. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  28138. + sb = sbinfo->si_sb;
  28139. +
  28140. + /*
  28141. + * prevent a race condition between sysfs and aufs.
  28142. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  28143. + * prohibits maintaining the sysfs entries.
  28144. + * hew we acquire read lock after sysfs_get_active_two().
  28145. + * on the other hand, the remount process may maintain the sysfs/aufs
  28146. + * entries after acquiring write lock.
  28147. + * it can cause a deadlock.
  28148. + * simply we gave up processing read here.
  28149. + */
  28150. + err = -EBUSY;
  28151. + if (unlikely(!si_noflush_read_trylock(sb)))
  28152. + goto out;
  28153. +
  28154. + seq = au_seq(buf, PAGE_SIZE);
  28155. + err = PTR_ERR(seq);
  28156. + if (IS_ERR(seq))
  28157. + goto out_unlock;
  28158. +
  28159. + name = (void *)attr->name;
  28160. + cattr = sysaufs_si_attrs;
  28161. + while (*cattr) {
  28162. + if (!strcmp(name, (*cattr)->name)) {
  28163. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  28164. + ->show(seq, sb);
  28165. + goto out_seq;
  28166. + }
  28167. + cattr++;
  28168. + }
  28169. +
  28170. + if (!strncmp(name, SysaufsBrid_PREFIX,
  28171. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  28172. + idx = AuBrSysfs_BRID;
  28173. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  28174. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  28175. + sizeof(SysaufsBr_PREFIX) - 1)) {
  28176. + idx = AuBrSysfs_BR;
  28177. + name += sizeof(SysaufsBr_PREFIX) - 1;
  28178. + } else
  28179. + BUG();
  28180. +
  28181. + err = kstrtol(name, 10, &l);
  28182. + if (!err) {
  28183. + bbot = au_sbbot(sb);
  28184. + if (l <= bbot)
  28185. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  28186. + else
  28187. + err = -ENOENT;
  28188. + }
  28189. +
  28190. +out_seq:
  28191. + if (!err) {
  28192. + err = seq->count;
  28193. + /* sysfs limit */
  28194. + if (unlikely(err == PAGE_SIZE))
  28195. + err = -EFBIG;
  28196. + }
  28197. + au_delayed_kfree(seq);
  28198. +out_unlock:
  28199. + si_read_unlock(sb);
  28200. +out:
  28201. + return err;
  28202. +}
  28203. +
  28204. +/* ---------------------------------------------------------------------- */
  28205. +
  28206. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  28207. +{
  28208. + int err;
  28209. + int16_t brid;
  28210. + aufs_bindex_t bindex, bbot;
  28211. + size_t sz;
  28212. + char *buf;
  28213. + struct seq_file *seq;
  28214. + struct au_branch *br;
  28215. +
  28216. + si_read_lock(sb, AuLock_FLUSH);
  28217. + bbot = au_sbbot(sb);
  28218. + err = bbot + 1;
  28219. + if (!arg)
  28220. + goto out;
  28221. +
  28222. + err = -ENOMEM;
  28223. + buf = (void *)__get_free_page(GFP_NOFS);
  28224. + if (unlikely(!buf))
  28225. + goto out;
  28226. +
  28227. + seq = au_seq(buf, PAGE_SIZE);
  28228. + err = PTR_ERR(seq);
  28229. + if (IS_ERR(seq))
  28230. + goto out_buf;
  28231. +
  28232. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  28233. + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
  28234. + err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
  28235. + if (unlikely(err))
  28236. + break;
  28237. +
  28238. + br = au_sbr(sb, bindex);
  28239. + brid = br->br_id;
  28240. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  28241. + err = __put_user(brid, &arg->id);
  28242. + if (unlikely(err))
  28243. + break;
  28244. +
  28245. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  28246. + err = __put_user(br->br_perm, &arg->perm);
  28247. + if (unlikely(err))
  28248. + break;
  28249. +
  28250. + err = au_seq_path(seq, &br->br_path);
  28251. + if (unlikely(err))
  28252. + break;
  28253. + seq_putc(seq, '\0');
  28254. + if (!seq_has_overflowed(seq)) {
  28255. + err = copy_to_user(arg->path, seq->buf, seq->count);
  28256. + seq->count = 0;
  28257. + if (unlikely(err))
  28258. + break;
  28259. + } else {
  28260. + err = -E2BIG;
  28261. + goto out_seq;
  28262. + }
  28263. + }
  28264. + if (unlikely(err))
  28265. + err = -EFAULT;
  28266. +
  28267. +out_seq:
  28268. + au_delayed_kfree(seq);
  28269. +out_buf:
  28270. + au_delayed_free_page((unsigned long)buf);
  28271. +out:
  28272. + si_read_unlock(sb);
  28273. + return err;
  28274. +}
  28275. +
  28276. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  28277. +{
  28278. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  28279. +}
  28280. +
  28281. +#ifdef CONFIG_COMPAT
  28282. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  28283. +{
  28284. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  28285. +}
  28286. +#endif
  28287. +
  28288. +/* ---------------------------------------------------------------------- */
  28289. +
  28290. +void sysaufs_br_init(struct au_branch *br)
  28291. +{
  28292. + int i;
  28293. + struct au_brsysfs *br_sysfs;
  28294. + struct attribute *attr;
  28295. +
  28296. + br_sysfs = br->br_sysfs;
  28297. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  28298. + attr = &br_sysfs->attr;
  28299. + sysfs_attr_init(attr);
  28300. + attr->name = br_sysfs->name;
  28301. + attr->mode = S_IRUGO;
  28302. + br_sysfs++;
  28303. + }
  28304. +}
  28305. +
  28306. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  28307. +{
  28308. + struct au_branch *br;
  28309. + struct kobject *kobj;
  28310. + struct au_brsysfs *br_sysfs;
  28311. + int i;
  28312. + aufs_bindex_t bbot;
  28313. +
  28314. + dbgaufs_brs_del(sb, bindex);
  28315. +
  28316. + if (!sysaufs_brs)
  28317. + return;
  28318. +
  28319. + kobj = &au_sbi(sb)->si_kobj;
  28320. + bbot = au_sbbot(sb);
  28321. + for (; bindex <= bbot; bindex++) {
  28322. + br = au_sbr(sb, bindex);
  28323. + br_sysfs = br->br_sysfs;
  28324. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  28325. + sysfs_remove_file(kobj, &br_sysfs->attr);
  28326. + br_sysfs++;
  28327. + }
  28328. + }
  28329. +}
  28330. +
  28331. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  28332. +{
  28333. + int err, i;
  28334. + aufs_bindex_t bbot;
  28335. + struct kobject *kobj;
  28336. + struct au_branch *br;
  28337. + struct au_brsysfs *br_sysfs;
  28338. +
  28339. + dbgaufs_brs_add(sb, bindex);
  28340. +
  28341. + if (!sysaufs_brs)
  28342. + return;
  28343. +
  28344. + kobj = &au_sbi(sb)->si_kobj;
  28345. + bbot = au_sbbot(sb);
  28346. + for (; bindex <= bbot; bindex++) {
  28347. + br = au_sbr(sb, bindex);
  28348. + br_sysfs = br->br_sysfs;
  28349. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  28350. + SysaufsBr_PREFIX "%d", bindex);
  28351. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  28352. + SysaufsBrid_PREFIX "%d", bindex);
  28353. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  28354. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  28355. + if (unlikely(err))
  28356. + pr_warn("failed %s under sysfs(%d)\n",
  28357. + br_sysfs->name, err);
  28358. + br_sysfs++;
  28359. + }
  28360. + }
  28361. +}
  28362. --- /dev/null
  28363. +++ linux-4.4/fs/aufs/sysrq.c 2016-10-23 11:20:57.642159951 +0300
  28364. @@ -0,0 +1,157 @@
  28365. +/*
  28366. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  28367. + *
  28368. + * This program, aufs is free software; you can redistribute it and/or modify
  28369. + * it under the terms of the GNU General Public License as published by
  28370. + * the Free Software Foundation; either version 2 of the License, or
  28371. + * (at your option) any later version.
  28372. + *
  28373. + * This program is distributed in the hope that it will be useful,
  28374. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28375. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28376. + * GNU General Public License for more details.
  28377. + *
  28378. + * You should have received a copy of the GNU General Public License
  28379. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28380. + */
  28381. +
  28382. +/*
  28383. + * magic sysrq hanlder
  28384. + */
  28385. +
  28386. +/* #include <linux/sysrq.h> */
  28387. +#include <linux/writeback.h>
  28388. +#include "aufs.h"
  28389. +
  28390. +/* ---------------------------------------------------------------------- */
  28391. +
  28392. +static void sysrq_sb(struct super_block *sb)
  28393. +{
  28394. + char *plevel;
  28395. + struct au_sbinfo *sbinfo;
  28396. + struct file *file;
  28397. + struct au_sphlhead *files;
  28398. + struct au_finfo *finfo;
  28399. +
  28400. + plevel = au_plevel;
  28401. + au_plevel = KERN_WARNING;
  28402. +
  28403. + /* since we define pr_fmt, call printk directly */
  28404. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  28405. +
  28406. + sbinfo = au_sbi(sb);
  28407. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  28408. + pr("superblock\n");
  28409. + au_dpri_sb(sb);
  28410. +
  28411. +#if 0
  28412. + pr("root dentry\n");
  28413. + au_dpri_dentry(sb->s_root);
  28414. + pr("root inode\n");
  28415. + au_dpri_inode(d_inode(sb->s_root));
  28416. +#endif
  28417. +
  28418. +#if 0
  28419. + do {
  28420. + int err, i, j, ndentry;
  28421. + struct au_dcsub_pages dpages;
  28422. + struct au_dpage *dpage;
  28423. +
  28424. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  28425. + if (unlikely(err))
  28426. + break;
  28427. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  28428. + if (!err)
  28429. + for (i = 0; i < dpages.ndpage; i++) {
  28430. + dpage = dpages.dpages + i;
  28431. + ndentry = dpage->ndentry;
  28432. + for (j = 0; j < ndentry; j++)
  28433. + au_dpri_dentry(dpage->dentries[j]);
  28434. + }
  28435. + au_dpages_free(&dpages);
  28436. + } while (0);
  28437. +#endif
  28438. +
  28439. +#if 1
  28440. + {
  28441. + struct inode *i;
  28442. +
  28443. + pr("isolated inode\n");
  28444. + spin_lock(&sb->s_inode_list_lock);
  28445. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  28446. + spin_lock(&i->i_lock);
  28447. + if (1 || hlist_empty(&i->i_dentry))
  28448. + au_dpri_inode(i);
  28449. + spin_unlock(&i->i_lock);
  28450. + }
  28451. + spin_unlock(&sb->s_inode_list_lock);
  28452. + }
  28453. +#endif
  28454. + pr("files\n");
  28455. + files = &au_sbi(sb)->si_files;
  28456. + spin_lock(&files->spin);
  28457. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  28458. + umode_t mode;
  28459. +
  28460. + file = finfo->fi_file;
  28461. + mode = file_inode(file)->i_mode;
  28462. + if (!special_file(mode))
  28463. + au_dpri_file(file);
  28464. + }
  28465. + spin_unlock(&files->spin);
  28466. + pr("done\n");
  28467. +
  28468. +#undef pr
  28469. + au_plevel = plevel;
  28470. +}
  28471. +
  28472. +/* ---------------------------------------------------------------------- */
  28473. +
  28474. +/* module parameter */
  28475. +static char *aufs_sysrq_key = "a";
  28476. +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
  28477. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  28478. +
  28479. +static void au_sysrq(int key __maybe_unused)
  28480. +{
  28481. + struct au_sbinfo *sbinfo;
  28482. +
  28483. + lockdep_off();
  28484. + au_sbilist_lock();
  28485. + hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  28486. + sysrq_sb(sbinfo->si_sb);
  28487. + au_sbilist_unlock();
  28488. + lockdep_on();
  28489. +}
  28490. +
  28491. +static struct sysrq_key_op au_sysrq_op = {
  28492. + .handler = au_sysrq,
  28493. + .help_msg = "Aufs",
  28494. + .action_msg = "Aufs",
  28495. + .enable_mask = SYSRQ_ENABLE_DUMP
  28496. +};
  28497. +
  28498. +/* ---------------------------------------------------------------------- */
  28499. +
  28500. +int __init au_sysrq_init(void)
  28501. +{
  28502. + int err;
  28503. + char key;
  28504. +
  28505. + err = -1;
  28506. + key = *aufs_sysrq_key;
  28507. + if ('a' <= key && key <= 'z')
  28508. + err = register_sysrq_key(key, &au_sysrq_op);
  28509. + if (unlikely(err))
  28510. + pr_err("err %d, sysrq=%c\n", err, key);
  28511. + return err;
  28512. +}
  28513. +
  28514. +void au_sysrq_fin(void)
  28515. +{
  28516. + int err;
  28517. +
  28518. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  28519. + if (unlikely(err))
  28520. + pr_err("err %d (ignored)\n", err);
  28521. +}
  28522. --- /dev/null
  28523. +++ linux-4.4/fs/aufs/vdir.c 2016-10-23 11:20:57.642159951 +0300
  28524. @@ -0,0 +1,899 @@
  28525. +/*
  28526. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  28527. + *
  28528. + * This program, aufs is free software; you can redistribute it and/or modify
  28529. + * it under the terms of the GNU General Public License as published by
  28530. + * the Free Software Foundation; either version 2 of the License, or
  28531. + * (at your option) any later version.
  28532. + *
  28533. + * This program is distributed in the hope that it will be useful,
  28534. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28535. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28536. + * GNU General Public License for more details.
  28537. + *
  28538. + * You should have received a copy of the GNU General Public License
  28539. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28540. + */
  28541. +
  28542. +/*
  28543. + * virtual or vertical directory
  28544. + */
  28545. +
  28546. +#include "aufs.h"
  28547. +
  28548. +static unsigned int calc_size(int nlen)
  28549. +{
  28550. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  28551. +}
  28552. +
  28553. +static int set_deblk_end(union au_vdir_deblk_p *p,
  28554. + union au_vdir_deblk_p *deblk_end)
  28555. +{
  28556. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  28557. + p->de->de_str.len = 0;
  28558. + /* smp_mb(); */
  28559. + return 0;
  28560. + }
  28561. + return -1; /* error */
  28562. +}
  28563. +
  28564. +/* returns true or false */
  28565. +static int is_deblk_end(union au_vdir_deblk_p *p,
  28566. + union au_vdir_deblk_p *deblk_end)
  28567. +{
  28568. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  28569. + return !p->de->de_str.len;
  28570. + return 1;
  28571. +}
  28572. +
  28573. +static unsigned char *last_deblk(struct au_vdir *vdir)
  28574. +{
  28575. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  28576. +}
  28577. +
  28578. +/* ---------------------------------------------------------------------- */
  28579. +
  28580. +/* estimate the appropriate size for name hash table */
  28581. +unsigned int au_rdhash_est(loff_t sz)
  28582. +{
  28583. + unsigned int n;
  28584. +
  28585. + n = UINT_MAX;
  28586. + sz >>= 10;
  28587. + if (sz < n)
  28588. + n = sz;
  28589. + if (sz < AUFS_RDHASH_DEF)
  28590. + n = AUFS_RDHASH_DEF;
  28591. + /* pr_info("n %u\n", n); */
  28592. + return n;
  28593. +}
  28594. +
  28595. +/*
  28596. + * the allocated memory has to be freed by
  28597. + * au_nhash_wh_free() or au_nhash_de_free().
  28598. + */
  28599. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  28600. +{
  28601. + struct hlist_head *head;
  28602. + unsigned int u;
  28603. + size_t sz;
  28604. +
  28605. + sz = sizeof(*nhash->nh_head) * num_hash;
  28606. + head = kmalloc(sz, gfp);
  28607. + if (head) {
  28608. + nhash->nh_num = num_hash;
  28609. + nhash->nh_head = head;
  28610. + for (u = 0; u < num_hash; u++)
  28611. + INIT_HLIST_HEAD(head++);
  28612. + return 0; /* success */
  28613. + }
  28614. +
  28615. + return -ENOMEM;
  28616. +}
  28617. +
  28618. +static void nhash_count(struct hlist_head *head)
  28619. +{
  28620. +#if 0
  28621. + unsigned long n;
  28622. + struct hlist_node *pos;
  28623. +
  28624. + n = 0;
  28625. + hlist_for_each(pos, head)
  28626. + n++;
  28627. + pr_info("%lu\n", n);
  28628. +#endif
  28629. +}
  28630. +
  28631. +static void au_nhash_wh_do_free(struct hlist_head *head)
  28632. +{
  28633. + struct au_vdir_wh *pos;
  28634. + struct hlist_node *node;
  28635. +
  28636. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  28637. + au_delayed_kfree(pos);
  28638. +}
  28639. +
  28640. +static void au_nhash_de_do_free(struct hlist_head *head)
  28641. +{
  28642. + struct au_vdir_dehstr *pos;
  28643. + struct hlist_node *node;
  28644. +
  28645. + hlist_for_each_entry_safe(pos, node, head, hash)
  28646. + au_cache_dfree_vdir_dehstr(pos);
  28647. +}
  28648. +
  28649. +static void au_nhash_do_free(struct au_nhash *nhash,
  28650. + void (*free)(struct hlist_head *head))
  28651. +{
  28652. + unsigned int n;
  28653. + struct hlist_head *head;
  28654. +
  28655. + n = nhash->nh_num;
  28656. + if (!n)
  28657. + return;
  28658. +
  28659. + head = nhash->nh_head;
  28660. + while (n-- > 0) {
  28661. + nhash_count(head);
  28662. + free(head++);
  28663. + }
  28664. + au_delayed_kfree(nhash->nh_head);
  28665. +}
  28666. +
  28667. +void au_nhash_wh_free(struct au_nhash *whlist)
  28668. +{
  28669. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  28670. +}
  28671. +
  28672. +static void au_nhash_de_free(struct au_nhash *delist)
  28673. +{
  28674. + au_nhash_do_free(delist, au_nhash_de_do_free);
  28675. +}
  28676. +
  28677. +/* ---------------------------------------------------------------------- */
  28678. +
  28679. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  28680. + int limit)
  28681. +{
  28682. + int num;
  28683. + unsigned int u, n;
  28684. + struct hlist_head *head;
  28685. + struct au_vdir_wh *pos;
  28686. +
  28687. + num = 0;
  28688. + n = whlist->nh_num;
  28689. + head = whlist->nh_head;
  28690. + for (u = 0; u < n; u++, head++)
  28691. + hlist_for_each_entry(pos, head, wh_hash)
  28692. + if (pos->wh_bindex == btgt && ++num > limit)
  28693. + return 1;
  28694. + return 0;
  28695. +}
  28696. +
  28697. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  28698. + unsigned char *name,
  28699. + unsigned int len)
  28700. +{
  28701. + unsigned int v;
  28702. + /* const unsigned int magic_bit = 12; */
  28703. +
  28704. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  28705. +
  28706. + v = 0;
  28707. + if (len > 8)
  28708. + len = 8;
  28709. + while (len--)
  28710. + v += *name++;
  28711. + /* v = hash_long(v, magic_bit); */
  28712. + v %= nhash->nh_num;
  28713. + return nhash->nh_head + v;
  28714. +}
  28715. +
  28716. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  28717. + int nlen)
  28718. +{
  28719. + return str->len == nlen && !memcmp(str->name, name, nlen);
  28720. +}
  28721. +
  28722. +/* returns found or not */
  28723. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  28724. +{
  28725. + struct hlist_head *head;
  28726. + struct au_vdir_wh *pos;
  28727. + struct au_vdir_destr *str;
  28728. +
  28729. + head = au_name_hash(whlist, name, nlen);
  28730. + hlist_for_each_entry(pos, head, wh_hash) {
  28731. + str = &pos->wh_str;
  28732. + AuDbg("%.*s\n", str->len, str->name);
  28733. + if (au_nhash_test_name(str, name, nlen))
  28734. + return 1;
  28735. + }
  28736. + return 0;
  28737. +}
  28738. +
  28739. +/* returns found(true) or not */
  28740. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  28741. +{
  28742. + struct hlist_head *head;
  28743. + struct au_vdir_dehstr *pos;
  28744. + struct au_vdir_destr *str;
  28745. +
  28746. + head = au_name_hash(delist, name, nlen);
  28747. + hlist_for_each_entry(pos, head, hash) {
  28748. + str = pos->str;
  28749. + AuDbg("%.*s\n", str->len, str->name);
  28750. + if (au_nhash_test_name(str, name, nlen))
  28751. + return 1;
  28752. + }
  28753. + return 0;
  28754. +}
  28755. +
  28756. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  28757. + unsigned char d_type)
  28758. +{
  28759. +#ifdef CONFIG_AUFS_SHWH
  28760. + wh->wh_ino = ino;
  28761. + wh->wh_type = d_type;
  28762. +#endif
  28763. +}
  28764. +
  28765. +/* ---------------------------------------------------------------------- */
  28766. +
  28767. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  28768. + unsigned int d_type, aufs_bindex_t bindex,
  28769. + unsigned char shwh)
  28770. +{
  28771. + int err;
  28772. + struct au_vdir_destr *str;
  28773. + struct au_vdir_wh *wh;
  28774. +
  28775. + AuDbg("%.*s\n", nlen, name);
  28776. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  28777. +
  28778. + err = -ENOMEM;
  28779. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  28780. + if (unlikely(!wh))
  28781. + goto out;
  28782. +
  28783. + err = 0;
  28784. + wh->wh_bindex = bindex;
  28785. + if (shwh)
  28786. + au_shwh_init_wh(wh, ino, d_type);
  28787. + str = &wh->wh_str;
  28788. + str->len = nlen;
  28789. + memcpy(str->name, name, nlen);
  28790. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  28791. + /* smp_mb(); */
  28792. +
  28793. +out:
  28794. + return err;
  28795. +}
  28796. +
  28797. +static int append_deblk(struct au_vdir *vdir)
  28798. +{
  28799. + int err;
  28800. + unsigned long ul;
  28801. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  28802. + union au_vdir_deblk_p p, deblk_end;
  28803. + unsigned char **o;
  28804. +
  28805. + err = -ENOMEM;
  28806. + o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  28807. + GFP_NOFS, /*may_shrink*/0);
  28808. + if (unlikely(!o))
  28809. + goto out;
  28810. +
  28811. + vdir->vd_deblk = o;
  28812. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  28813. + if (p.deblk) {
  28814. + ul = vdir->vd_nblk++;
  28815. + vdir->vd_deblk[ul] = p.deblk;
  28816. + vdir->vd_last.ul = ul;
  28817. + vdir->vd_last.p.deblk = p.deblk;
  28818. + deblk_end.deblk = p.deblk + deblk_sz;
  28819. + err = set_deblk_end(&p, &deblk_end);
  28820. + }
  28821. +
  28822. +out:
  28823. + return err;
  28824. +}
  28825. +
  28826. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  28827. + unsigned int d_type, struct au_nhash *delist)
  28828. +{
  28829. + int err;
  28830. + unsigned int sz;
  28831. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  28832. + union au_vdir_deblk_p p, *room, deblk_end;
  28833. + struct au_vdir_dehstr *dehstr;
  28834. +
  28835. + p.deblk = last_deblk(vdir);
  28836. + deblk_end.deblk = p.deblk + deblk_sz;
  28837. + room = &vdir->vd_last.p;
  28838. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  28839. + || !is_deblk_end(room, &deblk_end));
  28840. +
  28841. + sz = calc_size(nlen);
  28842. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  28843. + err = append_deblk(vdir);
  28844. + if (unlikely(err))
  28845. + goto out;
  28846. +
  28847. + p.deblk = last_deblk(vdir);
  28848. + deblk_end.deblk = p.deblk + deblk_sz;
  28849. + /* smp_mb(); */
  28850. + AuDebugOn(room->deblk != p.deblk);
  28851. + }
  28852. +
  28853. + err = -ENOMEM;
  28854. + dehstr = au_cache_alloc_vdir_dehstr();
  28855. + if (unlikely(!dehstr))
  28856. + goto out;
  28857. +
  28858. + dehstr->str = &room->de->de_str;
  28859. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  28860. + room->de->de_ino = ino;
  28861. + room->de->de_type = d_type;
  28862. + room->de->de_str.len = nlen;
  28863. + memcpy(room->de->de_str.name, name, nlen);
  28864. +
  28865. + err = 0;
  28866. + room->deblk += sz;
  28867. + if (unlikely(set_deblk_end(room, &deblk_end)))
  28868. + err = append_deblk(vdir);
  28869. + /* smp_mb(); */
  28870. +
  28871. +out:
  28872. + return err;
  28873. +}
  28874. +
  28875. +/* ---------------------------------------------------------------------- */
  28876. +
  28877. +void au_vdir_free(struct au_vdir *vdir, int atonce)
  28878. +{
  28879. + unsigned char **deblk;
  28880. +
  28881. + deblk = vdir->vd_deblk;
  28882. + if (!atonce) {
  28883. + while (vdir->vd_nblk--)
  28884. + au_delayed_kfree(*deblk++);
  28885. + au_delayed_kfree(vdir->vd_deblk);
  28886. + au_cache_dfree_vdir(vdir);
  28887. + } else {
  28888. + /* not delayed */
  28889. + while (vdir->vd_nblk--)
  28890. + kfree(*deblk++);
  28891. + kfree(vdir->vd_deblk);
  28892. + au_cache_free_vdir(vdir);
  28893. + }
  28894. +}
  28895. +
  28896. +static struct au_vdir *alloc_vdir(struct file *file)
  28897. +{
  28898. + struct au_vdir *vdir;
  28899. + struct super_block *sb;
  28900. + int err;
  28901. +
  28902. + sb = file->f_path.dentry->d_sb;
  28903. + SiMustAnyLock(sb);
  28904. +
  28905. + err = -ENOMEM;
  28906. + vdir = au_cache_alloc_vdir();
  28907. + if (unlikely(!vdir))
  28908. + goto out;
  28909. +
  28910. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  28911. + if (unlikely(!vdir->vd_deblk))
  28912. + goto out_free;
  28913. +
  28914. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  28915. + if (!vdir->vd_deblk_sz) {
  28916. + /* estimate the appropriate size for deblk */
  28917. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  28918. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  28919. + }
  28920. + vdir->vd_nblk = 0;
  28921. + vdir->vd_version = 0;
  28922. + vdir->vd_jiffy = 0;
  28923. + err = append_deblk(vdir);
  28924. + if (!err)
  28925. + return vdir; /* success */
  28926. +
  28927. + au_delayed_kfree(vdir->vd_deblk);
  28928. +
  28929. +out_free:
  28930. + au_cache_dfree_vdir(vdir);
  28931. +out:
  28932. + vdir = ERR_PTR(err);
  28933. + return vdir;
  28934. +}
  28935. +
  28936. +static int reinit_vdir(struct au_vdir *vdir)
  28937. +{
  28938. + int err;
  28939. + union au_vdir_deblk_p p, deblk_end;
  28940. +
  28941. + while (vdir->vd_nblk > 1) {
  28942. + au_delayed_kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
  28943. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  28944. + vdir->vd_nblk--;
  28945. + }
  28946. + p.deblk = vdir->vd_deblk[0];
  28947. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  28948. + err = set_deblk_end(&p, &deblk_end);
  28949. + /* keep vd_dblk_sz */
  28950. + vdir->vd_last.ul = 0;
  28951. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  28952. + vdir->vd_version = 0;
  28953. + vdir->vd_jiffy = 0;
  28954. + /* smp_mb(); */
  28955. + return err;
  28956. +}
  28957. +
  28958. +/* ---------------------------------------------------------------------- */
  28959. +
  28960. +#define AuFillVdir_CALLED 1
  28961. +#define AuFillVdir_WHABLE (1 << 1)
  28962. +#define AuFillVdir_SHWH (1 << 2)
  28963. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  28964. +#define au_fset_fillvdir(flags, name) \
  28965. + do { (flags) |= AuFillVdir_##name; } while (0)
  28966. +#define au_fclr_fillvdir(flags, name) \
  28967. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  28968. +
  28969. +#ifndef CONFIG_AUFS_SHWH
  28970. +#undef AuFillVdir_SHWH
  28971. +#define AuFillVdir_SHWH 0
  28972. +#endif
  28973. +
  28974. +struct fillvdir_arg {
  28975. + struct dir_context ctx;
  28976. + struct file *file;
  28977. + struct au_vdir *vdir;
  28978. + struct au_nhash delist;
  28979. + struct au_nhash whlist;
  28980. + aufs_bindex_t bindex;
  28981. + unsigned int flags;
  28982. + int err;
  28983. +};
  28984. +
  28985. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  28986. + loff_t offset __maybe_unused, u64 h_ino,
  28987. + unsigned int d_type)
  28988. +{
  28989. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  28990. + char *name = (void *)__name;
  28991. + struct super_block *sb;
  28992. + ino_t ino;
  28993. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  28994. +
  28995. + arg->err = 0;
  28996. + sb = arg->file->f_path.dentry->d_sb;
  28997. + au_fset_fillvdir(arg->flags, CALLED);
  28998. + /* smp_mb(); */
  28999. + if (nlen <= AUFS_WH_PFX_LEN
  29000. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  29001. + if (test_known(&arg->delist, name, nlen)
  29002. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  29003. + goto out; /* already exists or whiteouted */
  29004. +
  29005. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  29006. + if (!arg->err) {
  29007. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  29008. + d_type = DT_UNKNOWN;
  29009. + arg->err = append_de(arg->vdir, name, nlen, ino,
  29010. + d_type, &arg->delist);
  29011. + }
  29012. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  29013. + name += AUFS_WH_PFX_LEN;
  29014. + nlen -= AUFS_WH_PFX_LEN;
  29015. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  29016. + goto out; /* already whiteouted */
  29017. +
  29018. + if (shwh)
  29019. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  29020. + &ino);
  29021. + if (!arg->err) {
  29022. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  29023. + d_type = DT_UNKNOWN;
  29024. + arg->err = au_nhash_append_wh
  29025. + (&arg->whlist, name, nlen, ino, d_type,
  29026. + arg->bindex, shwh);
  29027. + }
  29028. + }
  29029. +
  29030. +out:
  29031. + if (!arg->err)
  29032. + arg->vdir->vd_jiffy = jiffies;
  29033. + /* smp_mb(); */
  29034. + AuTraceErr(arg->err);
  29035. + return arg->err;
  29036. +}
  29037. +
  29038. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  29039. + struct au_nhash *whlist, struct au_nhash *delist)
  29040. +{
  29041. +#ifdef CONFIG_AUFS_SHWH
  29042. + int err;
  29043. + unsigned int nh, u;
  29044. + struct hlist_head *head;
  29045. + struct au_vdir_wh *pos;
  29046. + struct hlist_node *n;
  29047. + char *p, *o;
  29048. + struct au_vdir_destr *destr;
  29049. +
  29050. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  29051. +
  29052. + err = -ENOMEM;
  29053. + o = p = (void *)__get_free_page(GFP_NOFS);
  29054. + if (unlikely(!p))
  29055. + goto out;
  29056. +
  29057. + err = 0;
  29058. + nh = whlist->nh_num;
  29059. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  29060. + p += AUFS_WH_PFX_LEN;
  29061. + for (u = 0; u < nh; u++) {
  29062. + head = whlist->nh_head + u;
  29063. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  29064. + destr = &pos->wh_str;
  29065. + memcpy(p, destr->name, destr->len);
  29066. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  29067. + pos->wh_ino, pos->wh_type, delist);
  29068. + if (unlikely(err))
  29069. + break;
  29070. + }
  29071. + }
  29072. +
  29073. + au_delayed_free_page((unsigned long)o);
  29074. +
  29075. +out:
  29076. + AuTraceErr(err);
  29077. + return err;
  29078. +#else
  29079. + return 0;
  29080. +#endif
  29081. +}
  29082. +
  29083. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  29084. +{
  29085. + int err;
  29086. + unsigned int rdhash;
  29087. + loff_t offset;
  29088. + aufs_bindex_t bbot, bindex, btop;
  29089. + unsigned char shwh;
  29090. + struct file *hf, *file;
  29091. + struct super_block *sb;
  29092. +
  29093. + file = arg->file;
  29094. + sb = file->f_path.dentry->d_sb;
  29095. + SiMustAnyLock(sb);
  29096. +
  29097. + rdhash = au_sbi(sb)->si_rdhash;
  29098. + if (!rdhash)
  29099. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  29100. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  29101. + if (unlikely(err))
  29102. + goto out;
  29103. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  29104. + if (unlikely(err))
  29105. + goto out_delist;
  29106. +
  29107. + err = 0;
  29108. + arg->flags = 0;
  29109. + shwh = 0;
  29110. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  29111. + shwh = 1;
  29112. + au_fset_fillvdir(arg->flags, SHWH);
  29113. + }
  29114. + btop = au_fbtop(file);
  29115. + bbot = au_fbbot_dir(file);
  29116. + for (bindex = btop; !err && bindex <= bbot; bindex++) {
  29117. + hf = au_hf_dir(file, bindex);
  29118. + if (!hf)
  29119. + continue;
  29120. +
  29121. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  29122. + err = offset;
  29123. + if (unlikely(offset))
  29124. + break;
  29125. +
  29126. + arg->bindex = bindex;
  29127. + au_fclr_fillvdir(arg->flags, WHABLE);
  29128. + if (shwh
  29129. + || (bindex != bbot
  29130. + && au_br_whable(au_sbr_perm(sb, bindex))))
  29131. + au_fset_fillvdir(arg->flags, WHABLE);
  29132. + do {
  29133. + arg->err = 0;
  29134. + au_fclr_fillvdir(arg->flags, CALLED);
  29135. + /* smp_mb(); */
  29136. + err = vfsub_iterate_dir(hf, &arg->ctx);
  29137. + if (err >= 0)
  29138. + err = arg->err;
  29139. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  29140. +
  29141. + /*
  29142. + * dir_relax() may be good for concurrency, but aufs should not
  29143. + * use it since it will cause a lockdep problem.
  29144. + */
  29145. + }
  29146. +
  29147. + if (!err && shwh)
  29148. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  29149. +
  29150. + au_nhash_wh_free(&arg->whlist);
  29151. +
  29152. +out_delist:
  29153. + au_nhash_de_free(&arg->delist);
  29154. +out:
  29155. + return err;
  29156. +}
  29157. +
  29158. +static int read_vdir(struct file *file, int may_read)
  29159. +{
  29160. + int err;
  29161. + unsigned long expire;
  29162. + unsigned char do_read;
  29163. + struct fillvdir_arg arg = {
  29164. + .ctx = {
  29165. + .actor = fillvdir
  29166. + }
  29167. + };
  29168. + struct inode *inode;
  29169. + struct au_vdir *vdir, *allocated;
  29170. +
  29171. + err = 0;
  29172. + inode = file_inode(file);
  29173. + IMustLock(inode);
  29174. + SiMustAnyLock(inode->i_sb);
  29175. +
  29176. + allocated = NULL;
  29177. + do_read = 0;
  29178. + expire = au_sbi(inode->i_sb)->si_rdcache;
  29179. + vdir = au_ivdir(inode);
  29180. + if (!vdir) {
  29181. + do_read = 1;
  29182. + vdir = alloc_vdir(file);
  29183. + err = PTR_ERR(vdir);
  29184. + if (IS_ERR(vdir))
  29185. + goto out;
  29186. + err = 0;
  29187. + allocated = vdir;
  29188. + } else if (may_read
  29189. + && (inode->i_version != vdir->vd_version
  29190. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  29191. + do_read = 1;
  29192. + err = reinit_vdir(vdir);
  29193. + if (unlikely(err))
  29194. + goto out;
  29195. + }
  29196. +
  29197. + if (!do_read)
  29198. + return 0; /* success */
  29199. +
  29200. + arg.file = file;
  29201. + arg.vdir = vdir;
  29202. + err = au_do_read_vdir(&arg);
  29203. + if (!err) {
  29204. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  29205. + vdir->vd_version = inode->i_version;
  29206. + vdir->vd_last.ul = 0;
  29207. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  29208. + if (allocated)
  29209. + au_set_ivdir(inode, allocated);
  29210. + } else if (allocated)
  29211. + au_vdir_free(allocated, /*atonce*/0);
  29212. +
  29213. +out:
  29214. + return err;
  29215. +}
  29216. +
  29217. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  29218. +{
  29219. + int err, rerr;
  29220. + unsigned long ul, n;
  29221. + const unsigned int deblk_sz = src->vd_deblk_sz;
  29222. +
  29223. + AuDebugOn(tgt->vd_nblk != 1);
  29224. +
  29225. + err = -ENOMEM;
  29226. + if (tgt->vd_nblk < src->vd_nblk) {
  29227. + unsigned char **p;
  29228. +
  29229. + p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  29230. + GFP_NOFS, /*may_shrink*/0);
  29231. + if (unlikely(!p))
  29232. + goto out;
  29233. + tgt->vd_deblk = p;
  29234. + }
  29235. +
  29236. + if (tgt->vd_deblk_sz != deblk_sz) {
  29237. + unsigned char *p;
  29238. +
  29239. + tgt->vd_deblk_sz = deblk_sz;
  29240. + p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
  29241. + /*may_shrink*/1);
  29242. + if (unlikely(!p))
  29243. + goto out;
  29244. + tgt->vd_deblk[0] = p;
  29245. + }
  29246. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  29247. + tgt->vd_version = src->vd_version;
  29248. + tgt->vd_jiffy = src->vd_jiffy;
  29249. +
  29250. + n = src->vd_nblk;
  29251. + for (ul = 1; ul < n; ul++) {
  29252. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  29253. + GFP_NOFS);
  29254. + if (unlikely(!tgt->vd_deblk[ul]))
  29255. + goto out;
  29256. + tgt->vd_nblk++;
  29257. + }
  29258. + tgt->vd_nblk = n;
  29259. + tgt->vd_last.ul = tgt->vd_last.ul;
  29260. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  29261. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  29262. + - src->vd_deblk[src->vd_last.ul];
  29263. + /* smp_mb(); */
  29264. + return 0; /* success */
  29265. +
  29266. +out:
  29267. + rerr = reinit_vdir(tgt);
  29268. + BUG_ON(rerr);
  29269. + return err;
  29270. +}
  29271. +
  29272. +int au_vdir_init(struct file *file)
  29273. +{
  29274. + int err;
  29275. + struct inode *inode;
  29276. + struct au_vdir *vdir_cache, *allocated;
  29277. +
  29278. + /* test file->f_pos here instead of ctx->pos */
  29279. + err = read_vdir(file, !file->f_pos);
  29280. + if (unlikely(err))
  29281. + goto out;
  29282. +
  29283. + allocated = NULL;
  29284. + vdir_cache = au_fvdir_cache(file);
  29285. + if (!vdir_cache) {
  29286. + vdir_cache = alloc_vdir(file);
  29287. + err = PTR_ERR(vdir_cache);
  29288. + if (IS_ERR(vdir_cache))
  29289. + goto out;
  29290. + allocated = vdir_cache;
  29291. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  29292. + /* test file->f_pos here instead of ctx->pos */
  29293. + err = reinit_vdir(vdir_cache);
  29294. + if (unlikely(err))
  29295. + goto out;
  29296. + } else
  29297. + return 0; /* success */
  29298. +
  29299. + inode = file_inode(file);
  29300. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  29301. + if (!err) {
  29302. + file->f_version = inode->i_version;
  29303. + if (allocated)
  29304. + au_set_fvdir_cache(file, allocated);
  29305. + } else if (allocated)
  29306. + au_vdir_free(allocated, /*atonce*/0);
  29307. +
  29308. +out:
  29309. + return err;
  29310. +}
  29311. +
  29312. +static loff_t calc_offset(struct au_vdir *vdir)
  29313. +{
  29314. + loff_t offset;
  29315. + union au_vdir_deblk_p p;
  29316. +
  29317. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  29318. + offset = vdir->vd_last.p.deblk - p.deblk;
  29319. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  29320. + return offset;
  29321. +}
  29322. +
  29323. +/* returns true or false */
  29324. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  29325. +{
  29326. + int valid;
  29327. + unsigned int deblk_sz;
  29328. + unsigned long ul, n;
  29329. + loff_t offset;
  29330. + union au_vdir_deblk_p p, deblk_end;
  29331. + struct au_vdir *vdir_cache;
  29332. +
  29333. + valid = 1;
  29334. + vdir_cache = au_fvdir_cache(file);
  29335. + offset = calc_offset(vdir_cache);
  29336. + AuDbg("offset %lld\n", offset);
  29337. + if (ctx->pos == offset)
  29338. + goto out;
  29339. +
  29340. + vdir_cache->vd_last.ul = 0;
  29341. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  29342. + if (!ctx->pos)
  29343. + goto out;
  29344. +
  29345. + valid = 0;
  29346. + deblk_sz = vdir_cache->vd_deblk_sz;
  29347. + ul = div64_u64(ctx->pos, deblk_sz);
  29348. + AuDbg("ul %lu\n", ul);
  29349. + if (ul >= vdir_cache->vd_nblk)
  29350. + goto out;
  29351. +
  29352. + n = vdir_cache->vd_nblk;
  29353. + for (; ul < n; ul++) {
  29354. + p.deblk = vdir_cache->vd_deblk[ul];
  29355. + deblk_end.deblk = p.deblk + deblk_sz;
  29356. + offset = ul;
  29357. + offset *= deblk_sz;
  29358. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  29359. + unsigned int l;
  29360. +
  29361. + l = calc_size(p.de->de_str.len);
  29362. + offset += l;
  29363. + p.deblk += l;
  29364. + }
  29365. + if (!is_deblk_end(&p, &deblk_end)) {
  29366. + valid = 1;
  29367. + vdir_cache->vd_last.ul = ul;
  29368. + vdir_cache->vd_last.p = p;
  29369. + break;
  29370. + }
  29371. + }
  29372. +
  29373. +out:
  29374. + /* smp_mb(); */
  29375. + AuTraceErr(!valid);
  29376. + return valid;
  29377. +}
  29378. +
  29379. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  29380. +{
  29381. + unsigned int l, deblk_sz;
  29382. + union au_vdir_deblk_p deblk_end;
  29383. + struct au_vdir *vdir_cache;
  29384. + struct au_vdir_de *de;
  29385. +
  29386. + vdir_cache = au_fvdir_cache(file);
  29387. + if (!seek_vdir(file, ctx))
  29388. + return 0;
  29389. +
  29390. + deblk_sz = vdir_cache->vd_deblk_sz;
  29391. + while (1) {
  29392. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  29393. + deblk_end.deblk += deblk_sz;
  29394. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  29395. + de = vdir_cache->vd_last.p.de;
  29396. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  29397. + de->de_str.len, de->de_str.name, ctx->pos,
  29398. + (unsigned long)de->de_ino, de->de_type);
  29399. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  29400. + de->de_str.len, de->de_ino,
  29401. + de->de_type))) {
  29402. + /* todo: ignore the error caused by udba? */
  29403. + /* return err; */
  29404. + return 0;
  29405. + }
  29406. +
  29407. + l = calc_size(de->de_str.len);
  29408. + vdir_cache->vd_last.p.deblk += l;
  29409. + ctx->pos += l;
  29410. + }
  29411. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  29412. + vdir_cache->vd_last.ul++;
  29413. + vdir_cache->vd_last.p.deblk
  29414. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  29415. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  29416. + continue;
  29417. + }
  29418. + break;
  29419. + }
  29420. +
  29421. + /* smp_mb(); */
  29422. + return 0;
  29423. +}
  29424. --- /dev/null
  29425. +++ linux-4.4/fs/aufs/vfsub.c 2016-10-23 11:20:57.642159951 +0300
  29426. @@ -0,0 +1,866 @@
  29427. +/*
  29428. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  29429. + *
  29430. + * This program, aufs is free software; you can redistribute it and/or modify
  29431. + * it under the terms of the GNU General Public License as published by
  29432. + * the Free Software Foundation; either version 2 of the License, or
  29433. + * (at your option) any later version.
  29434. + *
  29435. + * This program is distributed in the hope that it will be useful,
  29436. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29437. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29438. + * GNU General Public License for more details.
  29439. + *
  29440. + * You should have received a copy of the GNU General Public License
  29441. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29442. + */
  29443. +
  29444. +/*
  29445. + * sub-routines for VFS
  29446. + */
  29447. +
  29448. +#include <linux/namei.h>
  29449. +#include <linux/nsproxy.h>
  29450. +#include <linux/security.h>
  29451. +#include <linux/splice.h>
  29452. +#include "../fs/mount.h"
  29453. +#include "aufs.h"
  29454. +
  29455. +#ifdef CONFIG_AUFS_BR_FUSE
  29456. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
  29457. +{
  29458. + struct nsproxy *ns;
  29459. +
  29460. + if (!au_test_fuse(h_sb) || !au_userns)
  29461. + return 0;
  29462. +
  29463. + ns = current->nsproxy;
  29464. + /* no {get,put}_nsproxy(ns) */
  29465. + return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
  29466. +}
  29467. +#endif
  29468. +
  29469. +/* ---------------------------------------------------------------------- */
  29470. +
  29471. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  29472. +{
  29473. + int err;
  29474. + struct kstat st;
  29475. + struct super_block *h_sb;
  29476. +
  29477. + /* for remote fs, leave work for its getattr or d_revalidate */
  29478. + /* for bad i_attr fs, handle them in aufs_getattr() */
  29479. + /* still some fs may acquire i_mutex. we need to skip them */
  29480. + err = 0;
  29481. + if (!did)
  29482. + did = &err;
  29483. + h_sb = h_path->dentry->d_sb;
  29484. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  29485. + if (*did)
  29486. + err = vfs_getattr(h_path, &st);
  29487. +
  29488. + return err;
  29489. +}
  29490. +
  29491. +/* ---------------------------------------------------------------------- */
  29492. +
  29493. +struct file *vfsub_dentry_open(struct path *path, int flags)
  29494. +{
  29495. + struct file *file;
  29496. +
  29497. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  29498. + current_cred());
  29499. + if (!IS_ERR_OR_NULL(file)
  29500. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  29501. + i_readcount_inc(d_inode(path->dentry));
  29502. +
  29503. + return file;
  29504. +}
  29505. +
  29506. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  29507. +{
  29508. + struct file *file;
  29509. +
  29510. + lockdep_off();
  29511. + file = filp_open(path,
  29512. + oflags /* | __FMODE_NONOTIFY */,
  29513. + mode);
  29514. + lockdep_on();
  29515. + if (IS_ERR(file))
  29516. + goto out;
  29517. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29518. +
  29519. +out:
  29520. + return file;
  29521. +}
  29522. +
  29523. +/*
  29524. + * Ideally this function should call VFS:do_last() in order to keep all its
  29525. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  29526. + * structure such as nameidata. This is a second (or third) best approach.
  29527. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  29528. + */
  29529. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  29530. + struct vfsub_aopen_args *args, struct au_branch *br)
  29531. +{
  29532. + int err;
  29533. + struct file *file = args->file;
  29534. + /* copied from linux/fs/namei.c:atomic_open() */
  29535. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  29536. +
  29537. + IMustLock(dir);
  29538. + AuDebugOn(!dir->i_op->atomic_open);
  29539. +
  29540. + err = au_br_test_oflag(args->open_flag, br);
  29541. + if (unlikely(err))
  29542. + goto out;
  29543. +
  29544. + args->file->f_path.dentry = DENTRY_NOT_SET;
  29545. + args->file->f_path.mnt = au_br_mnt(br);
  29546. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  29547. + args->create_mode, args->opened);
  29548. + if (err >= 0) {
  29549. + /* some filesystems don't set FILE_CREATED while succeeded? */
  29550. + if (*args->opened & FILE_CREATED)
  29551. + fsnotify_create(dir, dentry);
  29552. + } else
  29553. + goto out;
  29554. +
  29555. +
  29556. + if (!err) {
  29557. + /* todo: call VFS:may_open() here */
  29558. + err = open_check_o_direct(file);
  29559. + /* todo: ima_file_check() too? */
  29560. + if (!err && (args->open_flag & __FMODE_EXEC))
  29561. + err = deny_write_access(file);
  29562. + if (unlikely(err))
  29563. + /* note that the file is created and still opened */
  29564. + goto out;
  29565. + }
  29566. +
  29567. + au_br_get(br);
  29568. + fsnotify_open(file);
  29569. +
  29570. +out:
  29571. + return err;
  29572. +}
  29573. +
  29574. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  29575. +{
  29576. + int err;
  29577. +
  29578. + err = kern_path(name, flags, path);
  29579. + if (!err && d_is_positive(path->dentry))
  29580. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  29581. + return err;
  29582. +}
  29583. +
  29584. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  29585. + int len)
  29586. +{
  29587. + struct path path = {
  29588. + .mnt = NULL
  29589. + };
  29590. +
  29591. + /* VFS checks it too, but by WARN_ON_ONCE() */
  29592. + IMustLock(d_inode(parent));
  29593. +
  29594. + path.dentry = lookup_one_len(name, parent, len);
  29595. + if (IS_ERR(path.dentry))
  29596. + goto out;
  29597. + if (d_is_positive(path.dentry))
  29598. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  29599. +
  29600. +out:
  29601. + AuTraceErrPtr(path.dentry);
  29602. + return path.dentry;
  29603. +}
  29604. +
  29605. +void vfsub_call_lkup_one(void *args)
  29606. +{
  29607. + struct vfsub_lkup_one_args *a = args;
  29608. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  29609. +}
  29610. +
  29611. +/* ---------------------------------------------------------------------- */
  29612. +
  29613. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29614. + struct dentry *d2, struct au_hinode *hdir2)
  29615. +{
  29616. + struct dentry *d;
  29617. +
  29618. + lockdep_off();
  29619. + d = lock_rename(d1, d2);
  29620. + lockdep_on();
  29621. + au_hn_suspend(hdir1);
  29622. + if (hdir1 != hdir2)
  29623. + au_hn_suspend(hdir2);
  29624. +
  29625. + return d;
  29626. +}
  29627. +
  29628. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29629. + struct dentry *d2, struct au_hinode *hdir2)
  29630. +{
  29631. + au_hn_resume(hdir1);
  29632. + if (hdir1 != hdir2)
  29633. + au_hn_resume(hdir2);
  29634. + lockdep_off();
  29635. + unlock_rename(d1, d2);
  29636. + lockdep_on();
  29637. +}
  29638. +
  29639. +/* ---------------------------------------------------------------------- */
  29640. +
  29641. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  29642. +{
  29643. + int err;
  29644. + struct dentry *d;
  29645. +
  29646. + IMustLock(dir);
  29647. +
  29648. + d = path->dentry;
  29649. + path->dentry = d->d_parent;
  29650. + err = security_path_mknod(path, d, mode, 0);
  29651. + path->dentry = d;
  29652. + if (unlikely(err))
  29653. + goto out;
  29654. +
  29655. + lockdep_off();
  29656. + err = vfs_create(dir, path->dentry, mode, want_excl);
  29657. + lockdep_on();
  29658. + if (!err) {
  29659. + struct path tmp = *path;
  29660. + int did;
  29661. +
  29662. + vfsub_update_h_iattr(&tmp, &did);
  29663. + if (did) {
  29664. + tmp.dentry = path->dentry->d_parent;
  29665. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29666. + }
  29667. + /*ignore*/
  29668. + }
  29669. +
  29670. +out:
  29671. + return err;
  29672. +}
  29673. +
  29674. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  29675. +{
  29676. + int err;
  29677. + struct dentry *d;
  29678. +
  29679. + IMustLock(dir);
  29680. +
  29681. + d = path->dentry;
  29682. + path->dentry = d->d_parent;
  29683. + err = security_path_symlink(path, d, symname);
  29684. + path->dentry = d;
  29685. + if (unlikely(err))
  29686. + goto out;
  29687. +
  29688. + lockdep_off();
  29689. + err = vfs_symlink(dir, path->dentry, symname);
  29690. + lockdep_on();
  29691. + if (!err) {
  29692. + struct path tmp = *path;
  29693. + int did;
  29694. +
  29695. + vfsub_update_h_iattr(&tmp, &did);
  29696. + if (did) {
  29697. + tmp.dentry = path->dentry->d_parent;
  29698. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29699. + }
  29700. + /*ignore*/
  29701. + }
  29702. +
  29703. +out:
  29704. + return err;
  29705. +}
  29706. +
  29707. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  29708. +{
  29709. + int err;
  29710. + struct dentry *d;
  29711. +
  29712. + IMustLock(dir);
  29713. +
  29714. + d = path->dentry;
  29715. + path->dentry = d->d_parent;
  29716. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  29717. + path->dentry = d;
  29718. + if (unlikely(err))
  29719. + goto out;
  29720. +
  29721. + lockdep_off();
  29722. + err = vfs_mknod(dir, path->dentry, mode, dev);
  29723. + lockdep_on();
  29724. + if (!err) {
  29725. + struct path tmp = *path;
  29726. + int did;
  29727. +
  29728. + vfsub_update_h_iattr(&tmp, &did);
  29729. + if (did) {
  29730. + tmp.dentry = path->dentry->d_parent;
  29731. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29732. + }
  29733. + /*ignore*/
  29734. + }
  29735. +
  29736. +out:
  29737. + return err;
  29738. +}
  29739. +
  29740. +static int au_test_nlink(struct inode *inode)
  29741. +{
  29742. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  29743. +
  29744. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  29745. + || inode->i_nlink < link_max)
  29746. + return 0;
  29747. + return -EMLINK;
  29748. +}
  29749. +
  29750. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  29751. + struct inode **delegated_inode)
  29752. +{
  29753. + int err;
  29754. + struct dentry *d;
  29755. +
  29756. + IMustLock(dir);
  29757. +
  29758. + err = au_test_nlink(d_inode(src_dentry));
  29759. + if (unlikely(err))
  29760. + return err;
  29761. +
  29762. + /* we don't call may_linkat() */
  29763. + d = path->dentry;
  29764. + path->dentry = d->d_parent;
  29765. + err = security_path_link(src_dentry, path, d);
  29766. + path->dentry = d;
  29767. + if (unlikely(err))
  29768. + goto out;
  29769. +
  29770. + lockdep_off();
  29771. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  29772. + lockdep_on();
  29773. + if (!err) {
  29774. + struct path tmp = *path;
  29775. + int did;
  29776. +
  29777. + /* fuse has different memory inode for the same inumber */
  29778. + vfsub_update_h_iattr(&tmp, &did);
  29779. + if (did) {
  29780. + tmp.dentry = path->dentry->d_parent;
  29781. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29782. + tmp.dentry = src_dentry;
  29783. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29784. + }
  29785. + /*ignore*/
  29786. + }
  29787. +
  29788. +out:
  29789. + return err;
  29790. +}
  29791. +
  29792. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  29793. + struct inode *dir, struct path *path,
  29794. + struct inode **delegated_inode)
  29795. +{
  29796. + int err;
  29797. + struct path tmp = {
  29798. + .mnt = path->mnt
  29799. + };
  29800. + struct dentry *d;
  29801. +
  29802. + IMustLock(dir);
  29803. + IMustLock(src_dir);
  29804. +
  29805. + d = path->dentry;
  29806. + path->dentry = d->d_parent;
  29807. + tmp.dentry = src_dentry->d_parent;
  29808. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  29809. + path->dentry = d;
  29810. + if (unlikely(err))
  29811. + goto out;
  29812. +
  29813. + lockdep_off();
  29814. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  29815. + delegated_inode, /*flags*/0);
  29816. + lockdep_on();
  29817. + if (!err) {
  29818. + int did;
  29819. +
  29820. + tmp.dentry = d->d_parent;
  29821. + vfsub_update_h_iattr(&tmp, &did);
  29822. + if (did) {
  29823. + tmp.dentry = src_dentry;
  29824. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29825. + tmp.dentry = src_dentry->d_parent;
  29826. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29827. + }
  29828. + /*ignore*/
  29829. + }
  29830. +
  29831. +out:
  29832. + return err;
  29833. +}
  29834. +
  29835. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  29836. +{
  29837. + int err;
  29838. + struct dentry *d;
  29839. +
  29840. + IMustLock(dir);
  29841. +
  29842. + d = path->dentry;
  29843. + path->dentry = d->d_parent;
  29844. + err = security_path_mkdir(path, d, mode);
  29845. + path->dentry = d;
  29846. + if (unlikely(err))
  29847. + goto out;
  29848. +
  29849. + lockdep_off();
  29850. + err = vfs_mkdir(dir, path->dentry, mode);
  29851. + lockdep_on();
  29852. + if (!err) {
  29853. + struct path tmp = *path;
  29854. + int did;
  29855. +
  29856. + vfsub_update_h_iattr(&tmp, &did);
  29857. + if (did) {
  29858. + tmp.dentry = path->dentry->d_parent;
  29859. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29860. + }
  29861. + /*ignore*/
  29862. + }
  29863. +
  29864. +out:
  29865. + return err;
  29866. +}
  29867. +
  29868. +int vfsub_rmdir(struct inode *dir, struct path *path)
  29869. +{
  29870. + int err;
  29871. + struct dentry *d;
  29872. +
  29873. + IMustLock(dir);
  29874. +
  29875. + d = path->dentry;
  29876. + path->dentry = d->d_parent;
  29877. + err = security_path_rmdir(path, d);
  29878. + path->dentry = d;
  29879. + if (unlikely(err))
  29880. + goto out;
  29881. +
  29882. + lockdep_off();
  29883. + err = vfs_rmdir(dir, path->dentry);
  29884. + lockdep_on();
  29885. + if (!err) {
  29886. + struct path tmp = {
  29887. + .dentry = path->dentry->d_parent,
  29888. + .mnt = path->mnt
  29889. + };
  29890. +
  29891. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  29892. + }
  29893. +
  29894. +out:
  29895. + return err;
  29896. +}
  29897. +
  29898. +/* ---------------------------------------------------------------------- */
  29899. +
  29900. +/* todo: support mmap_sem? */
  29901. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  29902. + loff_t *ppos)
  29903. +{
  29904. + ssize_t err;
  29905. +
  29906. + lockdep_off();
  29907. + err = vfs_read(file, ubuf, count, ppos);
  29908. + lockdep_on();
  29909. + if (err >= 0)
  29910. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29911. + return err;
  29912. +}
  29913. +
  29914. +/* todo: kernel_read()? */
  29915. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  29916. + loff_t *ppos)
  29917. +{
  29918. + ssize_t err;
  29919. + mm_segment_t oldfs;
  29920. + union {
  29921. + void *k;
  29922. + char __user *u;
  29923. + } buf;
  29924. +
  29925. + buf.k = kbuf;
  29926. + oldfs = get_fs();
  29927. + set_fs(KERNEL_DS);
  29928. + err = vfsub_read_u(file, buf.u, count, ppos);
  29929. + set_fs(oldfs);
  29930. + return err;
  29931. +}
  29932. +
  29933. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  29934. + loff_t *ppos)
  29935. +{
  29936. + ssize_t err;
  29937. +
  29938. + lockdep_off();
  29939. + err = vfs_write(file, ubuf, count, ppos);
  29940. + lockdep_on();
  29941. + if (err >= 0)
  29942. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29943. + return err;
  29944. +}
  29945. +
  29946. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  29947. +{
  29948. + ssize_t err;
  29949. + mm_segment_t oldfs;
  29950. + union {
  29951. + void *k;
  29952. + const char __user *u;
  29953. + } buf;
  29954. +
  29955. + buf.k = kbuf;
  29956. + oldfs = get_fs();
  29957. + set_fs(KERNEL_DS);
  29958. + err = vfsub_write_u(file, buf.u, count, ppos);
  29959. + set_fs(oldfs);
  29960. + return err;
  29961. +}
  29962. +
  29963. +int vfsub_flush(struct file *file, fl_owner_t id)
  29964. +{
  29965. + int err;
  29966. +
  29967. + err = 0;
  29968. + if (file->f_op->flush) {
  29969. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  29970. + err = file->f_op->flush(file, id);
  29971. + else {
  29972. + lockdep_off();
  29973. + err = file->f_op->flush(file, id);
  29974. + lockdep_on();
  29975. + }
  29976. + if (!err)
  29977. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  29978. + /*ignore*/
  29979. + }
  29980. + return err;
  29981. +}
  29982. +
  29983. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  29984. +{
  29985. + int err;
  29986. +
  29987. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  29988. +
  29989. + lockdep_off();
  29990. + err = iterate_dir(file, ctx);
  29991. + lockdep_on();
  29992. + if (err >= 0)
  29993. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29994. + return err;
  29995. +}
  29996. +
  29997. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  29998. + struct pipe_inode_info *pipe, size_t len,
  29999. + unsigned int flags)
  30000. +{
  30001. + long err;
  30002. +
  30003. + lockdep_off();
  30004. + err = do_splice_to(in, ppos, pipe, len, flags);
  30005. + lockdep_on();
  30006. + file_accessed(in);
  30007. + if (err >= 0)
  30008. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  30009. + return err;
  30010. +}
  30011. +
  30012. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  30013. + loff_t *ppos, size_t len, unsigned int flags)
  30014. +{
  30015. + long err;
  30016. +
  30017. + lockdep_off();
  30018. + err = do_splice_from(pipe, out, ppos, len, flags);
  30019. + lockdep_on();
  30020. + if (err >= 0)
  30021. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  30022. + return err;
  30023. +}
  30024. +
  30025. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  30026. +{
  30027. + int err;
  30028. +
  30029. + /* file can be NULL */
  30030. + lockdep_off();
  30031. + err = vfs_fsync(file, datasync);
  30032. + lockdep_on();
  30033. + if (!err) {
  30034. + if (!path) {
  30035. + AuDebugOn(!file);
  30036. + path = &file->f_path;
  30037. + }
  30038. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  30039. + }
  30040. + return err;
  30041. +}
  30042. +
  30043. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  30044. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  30045. + struct file *h_file)
  30046. +{
  30047. + int err;
  30048. + struct inode *h_inode;
  30049. + struct super_block *h_sb;
  30050. +
  30051. + if (!h_file) {
  30052. + err = vfsub_truncate(h_path, length);
  30053. + goto out;
  30054. + }
  30055. +
  30056. + h_inode = d_inode(h_path->dentry);
  30057. + h_sb = h_inode->i_sb;
  30058. + lockdep_off();
  30059. + sb_start_write(h_sb);
  30060. + lockdep_on();
  30061. + err = locks_verify_truncate(h_inode, h_file, length);
  30062. + if (!err)
  30063. + err = security_path_truncate(h_path);
  30064. + if (!err) {
  30065. + lockdep_off();
  30066. + err = do_truncate(h_path->dentry, length, attr, h_file);
  30067. + lockdep_on();
  30068. + }
  30069. + lockdep_off();
  30070. + sb_end_write(h_sb);
  30071. + lockdep_on();
  30072. +
  30073. +out:
  30074. + return err;
  30075. +}
  30076. +
  30077. +/* ---------------------------------------------------------------------- */
  30078. +
  30079. +struct au_vfsub_mkdir_args {
  30080. + int *errp;
  30081. + struct inode *dir;
  30082. + struct path *path;
  30083. + int mode;
  30084. +};
  30085. +
  30086. +static void au_call_vfsub_mkdir(void *args)
  30087. +{
  30088. + struct au_vfsub_mkdir_args *a = args;
  30089. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  30090. +}
  30091. +
  30092. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  30093. +{
  30094. + int err, do_sio, wkq_err;
  30095. +
  30096. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  30097. + if (!do_sio) {
  30098. + lockdep_off();
  30099. + err = vfsub_mkdir(dir, path, mode);
  30100. + lockdep_on();
  30101. + } else {
  30102. + struct au_vfsub_mkdir_args args = {
  30103. + .errp = &err,
  30104. + .dir = dir,
  30105. + .path = path,
  30106. + .mode = mode
  30107. + };
  30108. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  30109. + if (unlikely(wkq_err))
  30110. + err = wkq_err;
  30111. + }
  30112. +
  30113. + return err;
  30114. +}
  30115. +
  30116. +struct au_vfsub_rmdir_args {
  30117. + int *errp;
  30118. + struct inode *dir;
  30119. + struct path *path;
  30120. +};
  30121. +
  30122. +static void au_call_vfsub_rmdir(void *args)
  30123. +{
  30124. + struct au_vfsub_rmdir_args *a = args;
  30125. + *a->errp = vfsub_rmdir(a->dir, a->path);
  30126. +}
  30127. +
  30128. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  30129. +{
  30130. + int err, do_sio, wkq_err;
  30131. +
  30132. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  30133. + if (!do_sio) {
  30134. + lockdep_off();
  30135. + err = vfsub_rmdir(dir, path);
  30136. + lockdep_on();
  30137. + } else {
  30138. + struct au_vfsub_rmdir_args args = {
  30139. + .errp = &err,
  30140. + .dir = dir,
  30141. + .path = path
  30142. + };
  30143. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  30144. + if (unlikely(wkq_err))
  30145. + err = wkq_err;
  30146. + }
  30147. +
  30148. + return err;
  30149. +}
  30150. +
  30151. +/* ---------------------------------------------------------------------- */
  30152. +
  30153. +struct notify_change_args {
  30154. + int *errp;
  30155. + struct path *path;
  30156. + struct iattr *ia;
  30157. + struct inode **delegated_inode;
  30158. +};
  30159. +
  30160. +static void call_notify_change(void *args)
  30161. +{
  30162. + struct notify_change_args *a = args;
  30163. + struct inode *h_inode;
  30164. +
  30165. + h_inode = d_inode(a->path->dentry);
  30166. + IMustLock(h_inode);
  30167. +
  30168. + *a->errp = -EPERM;
  30169. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  30170. + lockdep_off();
  30171. + *a->errp = notify_change(a->path->dentry, a->ia,
  30172. + a->delegated_inode);
  30173. + lockdep_on();
  30174. + if (!*a->errp)
  30175. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  30176. + }
  30177. + AuTraceErr(*a->errp);
  30178. +}
  30179. +
  30180. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  30181. + struct inode **delegated_inode)
  30182. +{
  30183. + int err;
  30184. + struct notify_change_args args = {
  30185. + .errp = &err,
  30186. + .path = path,
  30187. + .ia = ia,
  30188. + .delegated_inode = delegated_inode
  30189. + };
  30190. +
  30191. + call_notify_change(&args);
  30192. +
  30193. + return err;
  30194. +}
  30195. +
  30196. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  30197. + struct inode **delegated_inode)
  30198. +{
  30199. + int err, wkq_err;
  30200. + struct notify_change_args args = {
  30201. + .errp = &err,
  30202. + .path = path,
  30203. + .ia = ia,
  30204. + .delegated_inode = delegated_inode
  30205. + };
  30206. +
  30207. + wkq_err = au_wkq_wait(call_notify_change, &args);
  30208. + if (unlikely(wkq_err))
  30209. + err = wkq_err;
  30210. +
  30211. + return err;
  30212. +}
  30213. +
  30214. +/* ---------------------------------------------------------------------- */
  30215. +
  30216. +struct unlink_args {
  30217. + int *errp;
  30218. + struct inode *dir;
  30219. + struct path *path;
  30220. + struct inode **delegated_inode;
  30221. +};
  30222. +
  30223. +static void call_unlink(void *args)
  30224. +{
  30225. + struct unlink_args *a = args;
  30226. + struct dentry *d = a->path->dentry;
  30227. + struct inode *h_inode;
  30228. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  30229. + && au_dcount(d) == 1);
  30230. +
  30231. + IMustLock(a->dir);
  30232. +
  30233. + a->path->dentry = d->d_parent;
  30234. + *a->errp = security_path_unlink(a->path, d);
  30235. + a->path->dentry = d;
  30236. + if (unlikely(*a->errp))
  30237. + return;
  30238. +
  30239. + if (!stop_sillyrename)
  30240. + dget(d);
  30241. + h_inode = NULL;
  30242. + if (d_is_positive(d)) {
  30243. + h_inode = d_inode(d);
  30244. + ihold(h_inode);
  30245. + }
  30246. +
  30247. + lockdep_off();
  30248. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  30249. + lockdep_on();
  30250. + if (!*a->errp) {
  30251. + struct path tmp = {
  30252. + .dentry = d->d_parent,
  30253. + .mnt = a->path->mnt
  30254. + };
  30255. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  30256. + }
  30257. +
  30258. + if (!stop_sillyrename)
  30259. + dput(d);
  30260. + if (h_inode)
  30261. + iput(h_inode);
  30262. +
  30263. + AuTraceErr(*a->errp);
  30264. +}
  30265. +
  30266. +/*
  30267. + * @dir: must be locked.
  30268. + * @dentry: target dentry.
  30269. + */
  30270. +int vfsub_unlink(struct inode *dir, struct path *path,
  30271. + struct inode **delegated_inode, int force)
  30272. +{
  30273. + int err;
  30274. + struct unlink_args args = {
  30275. + .errp = &err,
  30276. + .dir = dir,
  30277. + .path = path,
  30278. + .delegated_inode = delegated_inode
  30279. + };
  30280. +
  30281. + if (!force)
  30282. + call_unlink(&args);
  30283. + else {
  30284. + int wkq_err;
  30285. +
  30286. + wkq_err = au_wkq_wait(call_unlink, &args);
  30287. + if (unlikely(wkq_err))
  30288. + err = wkq_err;
  30289. + }
  30290. +
  30291. + return err;
  30292. +}
  30293. --- /dev/null
  30294. +++ linux-4.4/fs/aufs/vfsub.h 2016-10-23 11:20:57.642159951 +0300
  30295. @@ -0,0 +1,314 @@
  30296. +/*
  30297. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  30298. + *
  30299. + * This program, aufs is free software; you can redistribute it and/or modify
  30300. + * it under the terms of the GNU General Public License as published by
  30301. + * the Free Software Foundation; either version 2 of the License, or
  30302. + * (at your option) any later version.
  30303. + *
  30304. + * This program is distributed in the hope that it will be useful,
  30305. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30306. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30307. + * GNU General Public License for more details.
  30308. + *
  30309. + * You should have received a copy of the GNU General Public License
  30310. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30311. + */
  30312. +
  30313. +/*
  30314. + * sub-routines for VFS
  30315. + */
  30316. +
  30317. +#ifndef __AUFS_VFSUB_H__
  30318. +#define __AUFS_VFSUB_H__
  30319. +
  30320. +#ifdef __KERNEL__
  30321. +
  30322. +#include <linux/fs.h>
  30323. +#include <linux/mount.h>
  30324. +#include <linux/posix_acl.h>
  30325. +#include <linux/xattr.h>
  30326. +#include "debug.h"
  30327. +
  30328. +/* copied from linux/fs/internal.h */
  30329. +/* todo: BAD approach!! */
  30330. +extern void __mnt_drop_write(struct vfsmount *);
  30331. +extern int open_check_o_direct(struct file *f);
  30332. +
  30333. +/* ---------------------------------------------------------------------- */
  30334. +
  30335. +/* lock subclass for lower inode */
  30336. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  30337. +/* reduce? gave up. */
  30338. +enum {
  30339. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  30340. + AuLsc_I_PARENT, /* lower inode, parent first */
  30341. + AuLsc_I_PARENT2, /* copyup dirs */
  30342. + AuLsc_I_PARENT3, /* copyup wh */
  30343. + AuLsc_I_CHILD,
  30344. + AuLsc_I_CHILD2,
  30345. + AuLsc_I_End
  30346. +};
  30347. +
  30348. +/* to debug easier, do not make them inlined functions */
  30349. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  30350. +#define IMustLock(i) MtxMustLock(&(i)->i_mutex)
  30351. +
  30352. +/* ---------------------------------------------------------------------- */
  30353. +
  30354. +static inline void vfsub_drop_nlink(struct inode *inode)
  30355. +{
  30356. + AuDebugOn(!inode->i_nlink);
  30357. + drop_nlink(inode);
  30358. +}
  30359. +
  30360. +static inline void vfsub_dead_dir(struct inode *inode)
  30361. +{
  30362. + AuDebugOn(!S_ISDIR(inode->i_mode));
  30363. + inode->i_flags |= S_DEAD;
  30364. + clear_nlink(inode);
  30365. +}
  30366. +
  30367. +static inline int vfsub_native_ro(struct inode *inode)
  30368. +{
  30369. + return (inode->i_sb->s_flags & MS_RDONLY)
  30370. + || IS_RDONLY(inode)
  30371. + /* || IS_APPEND(inode) */
  30372. + || IS_IMMUTABLE(inode);
  30373. +}
  30374. +
  30375. +#ifdef CONFIG_AUFS_BR_FUSE
  30376. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
  30377. +#else
  30378. +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
  30379. +#endif
  30380. +
  30381. +/* ---------------------------------------------------------------------- */
  30382. +
  30383. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  30384. +struct file *vfsub_dentry_open(struct path *path, int flags);
  30385. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  30386. +struct vfsub_aopen_args {
  30387. + struct file *file;
  30388. + unsigned int open_flag;
  30389. + umode_t create_mode;
  30390. + int *opened;
  30391. +};
  30392. +struct au_branch;
  30393. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  30394. + struct vfsub_aopen_args *args, struct au_branch *br);
  30395. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  30396. +
  30397. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  30398. + int len);
  30399. +
  30400. +struct vfsub_lkup_one_args {
  30401. + struct dentry **errp;
  30402. + struct qstr *name;
  30403. + struct dentry *parent;
  30404. +};
  30405. +
  30406. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  30407. + struct dentry *parent)
  30408. +{
  30409. + return vfsub_lookup_one_len(name->name, parent, name->len);
  30410. +}
  30411. +
  30412. +void vfsub_call_lkup_one(void *args);
  30413. +
  30414. +/* ---------------------------------------------------------------------- */
  30415. +
  30416. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  30417. +{
  30418. + int err;
  30419. +
  30420. + lockdep_off();
  30421. + err = mnt_want_write(mnt);
  30422. + lockdep_on();
  30423. + return err;
  30424. +}
  30425. +
  30426. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  30427. +{
  30428. + lockdep_off();
  30429. + mnt_drop_write(mnt);
  30430. + lockdep_on();
  30431. +}
  30432. +
  30433. +#if 0 /* reserved */
  30434. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  30435. +{
  30436. + lockdep_off();
  30437. + mnt_drop_write_file(file);
  30438. + lockdep_on();
  30439. +}
  30440. +#endif
  30441. +
  30442. +/* ---------------------------------------------------------------------- */
  30443. +
  30444. +struct au_hinode;
  30445. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  30446. + struct dentry *d2, struct au_hinode *hdir2);
  30447. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  30448. + struct dentry *d2, struct au_hinode *hdir2);
  30449. +
  30450. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  30451. + bool want_excl);
  30452. +int vfsub_symlink(struct inode *dir, struct path *path,
  30453. + const char *symname);
  30454. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  30455. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  30456. + struct path *path, struct inode **delegated_inode);
  30457. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  30458. + struct inode *hdir, struct path *path,
  30459. + struct inode **delegated_inode);
  30460. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  30461. +int vfsub_rmdir(struct inode *dir, struct path *path);
  30462. +
  30463. +/* ---------------------------------------------------------------------- */
  30464. +
  30465. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  30466. + loff_t *ppos);
  30467. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  30468. + loff_t *ppos);
  30469. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  30470. + loff_t *ppos);
  30471. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  30472. + loff_t *ppos);
  30473. +int vfsub_flush(struct file *file, fl_owner_t id);
  30474. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  30475. +
  30476. +static inline loff_t vfsub_f_size_read(struct file *file)
  30477. +{
  30478. + return i_size_read(file_inode(file));
  30479. +}
  30480. +
  30481. +static inline unsigned int vfsub_file_flags(struct file *file)
  30482. +{
  30483. + unsigned int flags;
  30484. +
  30485. + spin_lock(&file->f_lock);
  30486. + flags = file->f_flags;
  30487. + spin_unlock(&file->f_lock);
  30488. +
  30489. + return flags;
  30490. +}
  30491. +
  30492. +static inline int vfsub_file_execed(struct file *file)
  30493. +{
  30494. + /* todo: direct access f_flags */
  30495. + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
  30496. +}
  30497. +
  30498. +#if 0 /* reserved */
  30499. +static inline void vfsub_file_accessed(struct file *h_file)
  30500. +{
  30501. + file_accessed(h_file);
  30502. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  30503. +}
  30504. +#endif
  30505. +
  30506. +#if 0 /* reserved */
  30507. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  30508. + struct dentry *h_dentry)
  30509. +{
  30510. + struct path h_path = {
  30511. + .dentry = h_dentry,
  30512. + .mnt = h_mnt
  30513. + };
  30514. + touch_atime(&h_path);
  30515. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  30516. +}
  30517. +#endif
  30518. +
  30519. +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
  30520. + int flags)
  30521. +{
  30522. + return update_time(h_inode, ts, flags);
  30523. + /* no vfsub_update_h_iattr() since we don't have struct path */
  30524. +}
  30525. +
  30526. +#ifdef CONFIG_FS_POSIX_ACL
  30527. +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
  30528. +{
  30529. + int err;
  30530. +
  30531. + err = posix_acl_chmod(h_inode, h_mode);
  30532. + if (err == -EOPNOTSUPP)
  30533. + err = 0;
  30534. + return err;
  30535. +}
  30536. +#else
  30537. +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
  30538. +#endif
  30539. +
  30540. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  30541. + struct pipe_inode_info *pipe, size_t len,
  30542. + unsigned int flags);
  30543. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  30544. + loff_t *ppos, size_t len, unsigned int flags);
  30545. +
  30546. +static inline long vfsub_truncate(struct path *path, loff_t length)
  30547. +{
  30548. + long err;
  30549. +
  30550. + lockdep_off();
  30551. + err = vfs_truncate(path, length);
  30552. + lockdep_on();
  30553. + return err;
  30554. +}
  30555. +
  30556. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  30557. + struct file *h_file);
  30558. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  30559. +
  30560. +/* ---------------------------------------------------------------------- */
  30561. +
  30562. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  30563. +{
  30564. + loff_t err;
  30565. +
  30566. + lockdep_off();
  30567. + err = vfs_llseek(file, offset, origin);
  30568. + lockdep_on();
  30569. + return err;
  30570. +}
  30571. +
  30572. +/* ---------------------------------------------------------------------- */
  30573. +
  30574. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  30575. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  30576. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  30577. + struct inode **delegated_inode);
  30578. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  30579. + struct inode **delegated_inode);
  30580. +int vfsub_unlink(struct inode *dir, struct path *path,
  30581. + struct inode **delegated_inode, int force);
  30582. +
  30583. +/* ---------------------------------------------------------------------- */
  30584. +
  30585. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  30586. + const void *value, size_t size, int flags)
  30587. +{
  30588. + int err;
  30589. +
  30590. + lockdep_off();
  30591. + err = vfs_setxattr(dentry, name, value, size, flags);
  30592. + lockdep_on();
  30593. +
  30594. + return err;
  30595. +}
  30596. +
  30597. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  30598. +{
  30599. + int err;
  30600. +
  30601. + lockdep_off();
  30602. + err = vfs_removexattr(dentry, name);
  30603. + lockdep_on();
  30604. +
  30605. + return err;
  30606. +}
  30607. +
  30608. +#endif /* __KERNEL__ */
  30609. +#endif /* __AUFS_VFSUB_H__ */
  30610. --- /dev/null
  30611. +++ linux-4.4/fs/aufs/wbr_policy.c 2016-10-23 11:20:57.642159951 +0300
  30612. @@ -0,0 +1,765 @@
  30613. +/*
  30614. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  30615. + *
  30616. + * This program, aufs is free software; you can redistribute it and/or modify
  30617. + * it under the terms of the GNU General Public License as published by
  30618. + * the Free Software Foundation; either version 2 of the License, or
  30619. + * (at your option) any later version.
  30620. + *
  30621. + * This program is distributed in the hope that it will be useful,
  30622. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30623. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30624. + * GNU General Public License for more details.
  30625. + *
  30626. + * You should have received a copy of the GNU General Public License
  30627. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30628. + */
  30629. +
  30630. +/*
  30631. + * policies for selecting one among multiple writable branches
  30632. + */
  30633. +
  30634. +#include <linux/statfs.h>
  30635. +#include "aufs.h"
  30636. +
  30637. +/* subset of cpup_attr() */
  30638. +static noinline_for_stack
  30639. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  30640. +{
  30641. + int err, sbits;
  30642. + struct iattr ia;
  30643. + struct inode *h_isrc;
  30644. +
  30645. + h_isrc = d_inode(h_src);
  30646. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  30647. + ia.ia_mode = h_isrc->i_mode;
  30648. + ia.ia_uid = h_isrc->i_uid;
  30649. + ia.ia_gid = h_isrc->i_gid;
  30650. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  30651. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  30652. + /* no delegation since it is just created */
  30653. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  30654. +
  30655. + /* is this nfs only? */
  30656. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  30657. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  30658. + ia.ia_mode = h_isrc->i_mode;
  30659. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  30660. + }
  30661. +
  30662. + return err;
  30663. +}
  30664. +
  30665. +#define AuCpdown_PARENT_OPQ 1
  30666. +#define AuCpdown_WHED (1 << 1)
  30667. +#define AuCpdown_MADE_DIR (1 << 2)
  30668. +#define AuCpdown_DIROPQ (1 << 3)
  30669. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  30670. +#define au_fset_cpdown(flags, name) \
  30671. + do { (flags) |= AuCpdown_##name; } while (0)
  30672. +#define au_fclr_cpdown(flags, name) \
  30673. + do { (flags) &= ~AuCpdown_##name; } while (0)
  30674. +
  30675. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  30676. + unsigned int *flags)
  30677. +{
  30678. + int err;
  30679. + struct dentry *opq_dentry;
  30680. +
  30681. + opq_dentry = au_diropq_create(dentry, bdst);
  30682. + err = PTR_ERR(opq_dentry);
  30683. + if (IS_ERR(opq_dentry))
  30684. + goto out;
  30685. + dput(opq_dentry);
  30686. + au_fset_cpdown(*flags, DIROPQ);
  30687. +
  30688. +out:
  30689. + return err;
  30690. +}
  30691. +
  30692. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  30693. + struct inode *dir, aufs_bindex_t bdst)
  30694. +{
  30695. + int err;
  30696. + struct path h_path;
  30697. + struct au_branch *br;
  30698. +
  30699. + br = au_sbr(dentry->d_sb, bdst);
  30700. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  30701. + err = PTR_ERR(h_path.dentry);
  30702. + if (IS_ERR(h_path.dentry))
  30703. + goto out;
  30704. +
  30705. + err = 0;
  30706. + if (d_is_positive(h_path.dentry)) {
  30707. + h_path.mnt = au_br_mnt(br);
  30708. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  30709. + dentry);
  30710. + }
  30711. + dput(h_path.dentry);
  30712. +
  30713. +out:
  30714. + return err;
  30715. +}
  30716. +
  30717. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  30718. + struct au_pin *pin,
  30719. + struct dentry *h_parent, void *arg)
  30720. +{
  30721. + int err, rerr;
  30722. + aufs_bindex_t bopq, btop;
  30723. + struct path h_path;
  30724. + struct dentry *parent;
  30725. + struct inode *h_dir, *h_inode, *inode, *dir;
  30726. + unsigned int *flags = arg;
  30727. +
  30728. + btop = au_dbtop(dentry);
  30729. + /* dentry is di-locked */
  30730. + parent = dget_parent(dentry);
  30731. + dir = d_inode(parent);
  30732. + h_dir = d_inode(h_parent);
  30733. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  30734. + IMustLock(h_dir);
  30735. +
  30736. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  30737. + if (unlikely(err < 0))
  30738. + goto out;
  30739. + h_path.dentry = au_h_dptr(dentry, bdst);
  30740. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  30741. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
  30742. + S_IRWXU | S_IRUGO | S_IXUGO);
  30743. + if (unlikely(err))
  30744. + goto out_put;
  30745. + au_fset_cpdown(*flags, MADE_DIR);
  30746. +
  30747. + bopq = au_dbdiropq(dentry);
  30748. + au_fclr_cpdown(*flags, WHED);
  30749. + au_fclr_cpdown(*flags, DIROPQ);
  30750. + if (au_dbwh(dentry) == bdst)
  30751. + au_fset_cpdown(*flags, WHED);
  30752. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  30753. + au_fset_cpdown(*flags, PARENT_OPQ);
  30754. + h_inode = d_inode(h_path.dentry);
  30755. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  30756. + if (au_ftest_cpdown(*flags, WHED)) {
  30757. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  30758. + if (unlikely(err)) {
  30759. + mutex_unlock(&h_inode->i_mutex);
  30760. + goto out_dir;
  30761. + }
  30762. + }
  30763. +
  30764. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
  30765. + mutex_unlock(&h_inode->i_mutex);
  30766. + if (unlikely(err))
  30767. + goto out_opq;
  30768. +
  30769. + if (au_ftest_cpdown(*flags, WHED)) {
  30770. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  30771. + if (unlikely(err))
  30772. + goto out_opq;
  30773. + }
  30774. +
  30775. + inode = d_inode(dentry);
  30776. + if (au_ibbot(inode) < bdst)
  30777. + au_set_ibbot(inode, bdst);
  30778. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  30779. + au_hi_flags(inode, /*isdir*/1));
  30780. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  30781. + goto out; /* success */
  30782. +
  30783. + /* revert */
  30784. +out_opq:
  30785. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  30786. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  30787. + rerr = au_diropq_remove(dentry, bdst);
  30788. + mutex_unlock(&h_inode->i_mutex);
  30789. + if (unlikely(rerr)) {
  30790. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  30791. + dentry, bdst, rerr);
  30792. + err = -EIO;
  30793. + goto out;
  30794. + }
  30795. + }
  30796. +out_dir:
  30797. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  30798. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  30799. + if (unlikely(rerr)) {
  30800. + AuIOErr("failed removing %pd b%d (%d)\n",
  30801. + dentry, bdst, rerr);
  30802. + err = -EIO;
  30803. + }
  30804. + }
  30805. +out_put:
  30806. + au_set_h_dptr(dentry, bdst, NULL);
  30807. + if (au_dbbot(dentry) == bdst)
  30808. + au_update_dbbot(dentry);
  30809. +out:
  30810. + dput(parent);
  30811. + return err;
  30812. +}
  30813. +
  30814. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  30815. +{
  30816. + int err;
  30817. + unsigned int flags;
  30818. +
  30819. + flags = 0;
  30820. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  30821. +
  30822. + return err;
  30823. +}
  30824. +
  30825. +/* ---------------------------------------------------------------------- */
  30826. +
  30827. +/* policies for create */
  30828. +
  30829. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  30830. +{
  30831. + int err, i, j, ndentry;
  30832. + aufs_bindex_t bopq;
  30833. + struct au_dcsub_pages dpages;
  30834. + struct au_dpage *dpage;
  30835. + struct dentry **dentries, *parent, *d;
  30836. +
  30837. + err = au_dpages_init(&dpages, GFP_NOFS);
  30838. + if (unlikely(err))
  30839. + goto out;
  30840. + parent = dget_parent(dentry);
  30841. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  30842. + if (unlikely(err))
  30843. + goto out_free;
  30844. +
  30845. + err = bindex;
  30846. + for (i = 0; i < dpages.ndpage; i++) {
  30847. + dpage = dpages.dpages + i;
  30848. + dentries = dpage->dentries;
  30849. + ndentry = dpage->ndentry;
  30850. + for (j = 0; j < ndentry; j++) {
  30851. + d = dentries[j];
  30852. + di_read_lock_parent2(d, !AuLock_IR);
  30853. + bopq = au_dbdiropq(d);
  30854. + di_read_unlock(d, !AuLock_IR);
  30855. + if (bopq >= 0 && bopq < err)
  30856. + err = bopq;
  30857. + }
  30858. + }
  30859. +
  30860. +out_free:
  30861. + dput(parent);
  30862. + au_dpages_free(&dpages);
  30863. +out:
  30864. + return err;
  30865. +}
  30866. +
  30867. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  30868. +{
  30869. + for (; bindex >= 0; bindex--)
  30870. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  30871. + return bindex;
  30872. + return -EROFS;
  30873. +}
  30874. +
  30875. +/* top down parent */
  30876. +static int au_wbr_create_tdp(struct dentry *dentry,
  30877. + unsigned int flags __maybe_unused)
  30878. +{
  30879. + int err;
  30880. + aufs_bindex_t btop, bindex;
  30881. + struct super_block *sb;
  30882. + struct dentry *parent, *h_parent;
  30883. +
  30884. + sb = dentry->d_sb;
  30885. + btop = au_dbtop(dentry);
  30886. + err = btop;
  30887. + if (!au_br_rdonly(au_sbr(sb, btop)))
  30888. + goto out;
  30889. +
  30890. + err = -EROFS;
  30891. + parent = dget_parent(dentry);
  30892. + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
  30893. + h_parent = au_h_dptr(parent, bindex);
  30894. + if (!h_parent || d_is_negative(h_parent))
  30895. + continue;
  30896. +
  30897. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  30898. + err = bindex;
  30899. + break;
  30900. + }
  30901. + }
  30902. + dput(parent);
  30903. +
  30904. + /* bottom up here */
  30905. + if (unlikely(err < 0)) {
  30906. + err = au_wbr_bu(sb, btop - 1);
  30907. + if (err >= 0)
  30908. + err = au_wbr_nonopq(dentry, err);
  30909. + }
  30910. +
  30911. +out:
  30912. + AuDbg("b%d\n", err);
  30913. + return err;
  30914. +}
  30915. +
  30916. +/* ---------------------------------------------------------------------- */
  30917. +
  30918. +/* an exception for the policy other than tdp */
  30919. +static int au_wbr_create_exp(struct dentry *dentry)
  30920. +{
  30921. + int err;
  30922. + aufs_bindex_t bwh, bdiropq;
  30923. + struct dentry *parent;
  30924. +
  30925. + err = -1;
  30926. + bwh = au_dbwh(dentry);
  30927. + parent = dget_parent(dentry);
  30928. + bdiropq = au_dbdiropq(parent);
  30929. + if (bwh >= 0) {
  30930. + if (bdiropq >= 0)
  30931. + err = min(bdiropq, bwh);
  30932. + else
  30933. + err = bwh;
  30934. + AuDbg("%d\n", err);
  30935. + } else if (bdiropq >= 0) {
  30936. + err = bdiropq;
  30937. + AuDbg("%d\n", err);
  30938. + }
  30939. + dput(parent);
  30940. +
  30941. + if (err >= 0)
  30942. + err = au_wbr_nonopq(dentry, err);
  30943. +
  30944. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  30945. + err = -1;
  30946. +
  30947. + AuDbg("%d\n", err);
  30948. + return err;
  30949. +}
  30950. +
  30951. +/* ---------------------------------------------------------------------- */
  30952. +
  30953. +/* round robin */
  30954. +static int au_wbr_create_init_rr(struct super_block *sb)
  30955. +{
  30956. + int err;
  30957. +
  30958. + err = au_wbr_bu(sb, au_sbbot(sb));
  30959. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  30960. + /* smp_mb(); */
  30961. +
  30962. + AuDbg("b%d\n", err);
  30963. + return err;
  30964. +}
  30965. +
  30966. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  30967. +{
  30968. + int err, nbr;
  30969. + unsigned int u;
  30970. + aufs_bindex_t bindex, bbot;
  30971. + struct super_block *sb;
  30972. + atomic_t *next;
  30973. +
  30974. + err = au_wbr_create_exp(dentry);
  30975. + if (err >= 0)
  30976. + goto out;
  30977. +
  30978. + sb = dentry->d_sb;
  30979. + next = &au_sbi(sb)->si_wbr_rr_next;
  30980. + bbot = au_sbbot(sb);
  30981. + nbr = bbot + 1;
  30982. + for (bindex = 0; bindex <= bbot; bindex++) {
  30983. + if (!au_ftest_wbr(flags, DIR)) {
  30984. + err = atomic_dec_return(next) + 1;
  30985. + /* modulo for 0 is meaningless */
  30986. + if (unlikely(!err))
  30987. + err = atomic_dec_return(next) + 1;
  30988. + } else
  30989. + err = atomic_read(next);
  30990. + AuDbg("%d\n", err);
  30991. + u = err;
  30992. + err = u % nbr;
  30993. + AuDbg("%d\n", err);
  30994. + if (!au_br_rdonly(au_sbr(sb, err)))
  30995. + break;
  30996. + err = -EROFS;
  30997. + }
  30998. +
  30999. + if (err >= 0)
  31000. + err = au_wbr_nonopq(dentry, err);
  31001. +
  31002. +out:
  31003. + AuDbg("%d\n", err);
  31004. + return err;
  31005. +}
  31006. +
  31007. +/* ---------------------------------------------------------------------- */
  31008. +
  31009. +/* most free space */
  31010. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  31011. +{
  31012. + struct super_block *sb;
  31013. + struct au_branch *br;
  31014. + struct au_wbr_mfs *mfs;
  31015. + struct dentry *h_parent;
  31016. + aufs_bindex_t bindex, bbot;
  31017. + int err;
  31018. + unsigned long long b, bavail;
  31019. + struct path h_path;
  31020. + /* reduce the stack usage */
  31021. + struct kstatfs *st;
  31022. +
  31023. + st = kmalloc(sizeof(*st), GFP_NOFS);
  31024. + if (unlikely(!st)) {
  31025. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  31026. + return;
  31027. + }
  31028. +
  31029. + bavail = 0;
  31030. + sb = dentry->d_sb;
  31031. + mfs = &au_sbi(sb)->si_wbr_mfs;
  31032. + MtxMustLock(&mfs->mfs_lock);
  31033. + mfs->mfs_bindex = -EROFS;
  31034. + mfs->mfsrr_bytes = 0;
  31035. + if (!parent) {
  31036. + bindex = 0;
  31037. + bbot = au_sbbot(sb);
  31038. + } else {
  31039. + bindex = au_dbtop(parent);
  31040. + bbot = au_dbtaildir(parent);
  31041. + }
  31042. +
  31043. + for (; bindex <= bbot; bindex++) {
  31044. + if (parent) {
  31045. + h_parent = au_h_dptr(parent, bindex);
  31046. + if (!h_parent || d_is_negative(h_parent))
  31047. + continue;
  31048. + }
  31049. + br = au_sbr(sb, bindex);
  31050. + if (au_br_rdonly(br))
  31051. + continue;
  31052. +
  31053. + /* sb->s_root for NFS is unreliable */
  31054. + h_path.mnt = au_br_mnt(br);
  31055. + h_path.dentry = h_path.mnt->mnt_root;
  31056. + err = vfs_statfs(&h_path, st);
  31057. + if (unlikely(err)) {
  31058. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  31059. + continue;
  31060. + }
  31061. +
  31062. + /* when the available size is equal, select the lower one */
  31063. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  31064. + || sizeof(b) < sizeof(st->f_bsize));
  31065. + b = st->f_bavail * st->f_bsize;
  31066. + br->br_wbr->wbr_bytes = b;
  31067. + if (b >= bavail) {
  31068. + bavail = b;
  31069. + mfs->mfs_bindex = bindex;
  31070. + mfs->mfs_jiffy = jiffies;
  31071. + }
  31072. + }
  31073. +
  31074. + mfs->mfsrr_bytes = bavail;
  31075. + AuDbg("b%d\n", mfs->mfs_bindex);
  31076. + au_delayed_kfree(st);
  31077. +}
  31078. +
  31079. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  31080. +{
  31081. + int err;
  31082. + struct dentry *parent;
  31083. + struct super_block *sb;
  31084. + struct au_wbr_mfs *mfs;
  31085. +
  31086. + err = au_wbr_create_exp(dentry);
  31087. + if (err >= 0)
  31088. + goto out;
  31089. +
  31090. + sb = dentry->d_sb;
  31091. + parent = NULL;
  31092. + if (au_ftest_wbr(flags, PARENT))
  31093. + parent = dget_parent(dentry);
  31094. + mfs = &au_sbi(sb)->si_wbr_mfs;
  31095. + mutex_lock(&mfs->mfs_lock);
  31096. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  31097. + || mfs->mfs_bindex < 0
  31098. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  31099. + au_mfs(dentry, parent);
  31100. + mutex_unlock(&mfs->mfs_lock);
  31101. + err = mfs->mfs_bindex;
  31102. + dput(parent);
  31103. +
  31104. + if (err >= 0)
  31105. + err = au_wbr_nonopq(dentry, err);
  31106. +
  31107. +out:
  31108. + AuDbg("b%d\n", err);
  31109. + return err;
  31110. +}
  31111. +
  31112. +static int au_wbr_create_init_mfs(struct super_block *sb)
  31113. +{
  31114. + struct au_wbr_mfs *mfs;
  31115. +
  31116. + mfs = &au_sbi(sb)->si_wbr_mfs;
  31117. + mutex_init(&mfs->mfs_lock);
  31118. + mfs->mfs_jiffy = 0;
  31119. + mfs->mfs_bindex = -EROFS;
  31120. +
  31121. + return 0;
  31122. +}
  31123. +
  31124. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  31125. +{
  31126. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  31127. + return 0;
  31128. +}
  31129. +
  31130. +/* ---------------------------------------------------------------------- */
  31131. +
  31132. +/* most free space and then round robin */
  31133. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  31134. +{
  31135. + int err;
  31136. + struct au_wbr_mfs *mfs;
  31137. +
  31138. + err = au_wbr_create_mfs(dentry, flags);
  31139. + if (err >= 0) {
  31140. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  31141. + mutex_lock(&mfs->mfs_lock);
  31142. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  31143. + err = au_wbr_create_rr(dentry, flags);
  31144. + mutex_unlock(&mfs->mfs_lock);
  31145. + }
  31146. +
  31147. + AuDbg("b%d\n", err);
  31148. + return err;
  31149. +}
  31150. +
  31151. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  31152. +{
  31153. + int err;
  31154. +
  31155. + au_wbr_create_init_mfs(sb); /* ignore */
  31156. + err = au_wbr_create_init_rr(sb);
  31157. +
  31158. + return err;
  31159. +}
  31160. +
  31161. +/* ---------------------------------------------------------------------- */
  31162. +
  31163. +/* top down parent and most free space */
  31164. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  31165. +{
  31166. + int err, e2;
  31167. + unsigned long long b;
  31168. + aufs_bindex_t bindex, btop, bbot;
  31169. + struct super_block *sb;
  31170. + struct dentry *parent, *h_parent;
  31171. + struct au_branch *br;
  31172. +
  31173. + err = au_wbr_create_tdp(dentry, flags);
  31174. + if (unlikely(err < 0))
  31175. + goto out;
  31176. + parent = dget_parent(dentry);
  31177. + btop = au_dbtop(parent);
  31178. + bbot = au_dbtaildir(parent);
  31179. + if (btop == bbot)
  31180. + goto out_parent; /* success */
  31181. +
  31182. + e2 = au_wbr_create_mfs(dentry, flags);
  31183. + if (e2 < 0)
  31184. + goto out_parent; /* success */
  31185. +
  31186. + /* when the available size is equal, select upper one */
  31187. + sb = dentry->d_sb;
  31188. + br = au_sbr(sb, err);
  31189. + b = br->br_wbr->wbr_bytes;
  31190. + AuDbg("b%d, %llu\n", err, b);
  31191. +
  31192. + for (bindex = btop; bindex <= bbot; bindex++) {
  31193. + h_parent = au_h_dptr(parent, bindex);
  31194. + if (!h_parent || d_is_negative(h_parent))
  31195. + continue;
  31196. +
  31197. + br = au_sbr(sb, bindex);
  31198. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  31199. + b = br->br_wbr->wbr_bytes;
  31200. + err = bindex;
  31201. + AuDbg("b%d, %llu\n", err, b);
  31202. + }
  31203. + }
  31204. +
  31205. + if (err >= 0)
  31206. + err = au_wbr_nonopq(dentry, err);
  31207. +
  31208. +out_parent:
  31209. + dput(parent);
  31210. +out:
  31211. + AuDbg("b%d\n", err);
  31212. + return err;
  31213. +}
  31214. +
  31215. +/* ---------------------------------------------------------------------- */
  31216. +
  31217. +/*
  31218. + * - top down parent
  31219. + * - most free space with parent
  31220. + * - most free space round-robin regardless parent
  31221. + */
  31222. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  31223. +{
  31224. + int err;
  31225. + unsigned long long watermark;
  31226. + struct super_block *sb;
  31227. + struct au_branch *br;
  31228. + struct au_wbr_mfs *mfs;
  31229. +
  31230. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  31231. + if (unlikely(err < 0))
  31232. + goto out;
  31233. +
  31234. + sb = dentry->d_sb;
  31235. + br = au_sbr(sb, err);
  31236. + mfs = &au_sbi(sb)->si_wbr_mfs;
  31237. + mutex_lock(&mfs->mfs_lock);
  31238. + watermark = mfs->mfsrr_watermark;
  31239. + mutex_unlock(&mfs->mfs_lock);
  31240. + if (br->br_wbr->wbr_bytes < watermark)
  31241. + /* regardless the parent dir */
  31242. + err = au_wbr_create_mfsrr(dentry, flags);
  31243. +
  31244. +out:
  31245. + AuDbg("b%d\n", err);
  31246. + return err;
  31247. +}
  31248. +
  31249. +/* ---------------------------------------------------------------------- */
  31250. +
  31251. +/* policies for copyup */
  31252. +
  31253. +/* top down parent */
  31254. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  31255. +{
  31256. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  31257. +}
  31258. +
  31259. +/* bottom up parent */
  31260. +static int au_wbr_copyup_bup(struct dentry *dentry)
  31261. +{
  31262. + int err;
  31263. + aufs_bindex_t bindex, btop;
  31264. + struct dentry *parent, *h_parent;
  31265. + struct super_block *sb;
  31266. +
  31267. + err = -EROFS;
  31268. + sb = dentry->d_sb;
  31269. + parent = dget_parent(dentry);
  31270. + btop = au_dbtop(parent);
  31271. + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
  31272. + h_parent = au_h_dptr(parent, bindex);
  31273. + if (!h_parent || d_is_negative(h_parent))
  31274. + continue;
  31275. +
  31276. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  31277. + err = bindex;
  31278. + break;
  31279. + }
  31280. + }
  31281. + dput(parent);
  31282. +
  31283. + /* bottom up here */
  31284. + if (unlikely(err < 0))
  31285. + err = au_wbr_bu(sb, btop - 1);
  31286. +
  31287. + AuDbg("b%d\n", err);
  31288. + return err;
  31289. +}
  31290. +
  31291. +/* bottom up */
  31292. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
  31293. +{
  31294. + int err;
  31295. +
  31296. + err = au_wbr_bu(dentry->d_sb, btop);
  31297. + AuDbg("b%d\n", err);
  31298. + if (err > btop)
  31299. + err = au_wbr_nonopq(dentry, err);
  31300. +
  31301. + AuDbg("b%d\n", err);
  31302. + return err;
  31303. +}
  31304. +
  31305. +static int au_wbr_copyup_bu(struct dentry *dentry)
  31306. +{
  31307. + int err;
  31308. + aufs_bindex_t btop;
  31309. +
  31310. + btop = au_dbtop(dentry);
  31311. + err = au_wbr_do_copyup_bu(dentry, btop);
  31312. + return err;
  31313. +}
  31314. +
  31315. +/* ---------------------------------------------------------------------- */
  31316. +
  31317. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  31318. + [AuWbrCopyup_TDP] = {
  31319. + .copyup = au_wbr_copyup_tdp
  31320. + },
  31321. + [AuWbrCopyup_BUP] = {
  31322. + .copyup = au_wbr_copyup_bup
  31323. + },
  31324. + [AuWbrCopyup_BU] = {
  31325. + .copyup = au_wbr_copyup_bu
  31326. + }
  31327. +};
  31328. +
  31329. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  31330. + [AuWbrCreate_TDP] = {
  31331. + .create = au_wbr_create_tdp
  31332. + },
  31333. + [AuWbrCreate_RR] = {
  31334. + .create = au_wbr_create_rr,
  31335. + .init = au_wbr_create_init_rr
  31336. + },
  31337. + [AuWbrCreate_MFS] = {
  31338. + .create = au_wbr_create_mfs,
  31339. + .init = au_wbr_create_init_mfs,
  31340. + .fin = au_wbr_create_fin_mfs
  31341. + },
  31342. + [AuWbrCreate_MFSV] = {
  31343. + .create = au_wbr_create_mfs,
  31344. + .init = au_wbr_create_init_mfs,
  31345. + .fin = au_wbr_create_fin_mfs
  31346. + },
  31347. + [AuWbrCreate_MFSRR] = {
  31348. + .create = au_wbr_create_mfsrr,
  31349. + .init = au_wbr_create_init_mfsrr,
  31350. + .fin = au_wbr_create_fin_mfs
  31351. + },
  31352. + [AuWbrCreate_MFSRRV] = {
  31353. + .create = au_wbr_create_mfsrr,
  31354. + .init = au_wbr_create_init_mfsrr,
  31355. + .fin = au_wbr_create_fin_mfs
  31356. + },
  31357. + [AuWbrCreate_PMFS] = {
  31358. + .create = au_wbr_create_pmfs,
  31359. + .init = au_wbr_create_init_mfs,
  31360. + .fin = au_wbr_create_fin_mfs
  31361. + },
  31362. + [AuWbrCreate_PMFSV] = {
  31363. + .create = au_wbr_create_pmfs,
  31364. + .init = au_wbr_create_init_mfs,
  31365. + .fin = au_wbr_create_fin_mfs
  31366. + },
  31367. + [AuWbrCreate_PMFSRR] = {
  31368. + .create = au_wbr_create_pmfsrr,
  31369. + .init = au_wbr_create_init_mfsrr,
  31370. + .fin = au_wbr_create_fin_mfs
  31371. + },
  31372. + [AuWbrCreate_PMFSRRV] = {
  31373. + .create = au_wbr_create_pmfsrr,
  31374. + .init = au_wbr_create_init_mfsrr,
  31375. + .fin = au_wbr_create_fin_mfs
  31376. + }
  31377. +};
  31378. --- /dev/null
  31379. +++ linux-4.4/fs/aufs/whout.c 2016-10-23 11:20:57.642159951 +0300
  31380. @@ -0,0 +1,1060 @@
  31381. +/*
  31382. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  31383. + *
  31384. + * This program, aufs is free software; you can redistribute it and/or modify
  31385. + * it under the terms of the GNU General Public License as published by
  31386. + * the Free Software Foundation; either version 2 of the License, or
  31387. + * (at your option) any later version.
  31388. + *
  31389. + * This program is distributed in the hope that it will be useful,
  31390. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31391. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31392. + * GNU General Public License for more details.
  31393. + *
  31394. + * You should have received a copy of the GNU General Public License
  31395. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31396. + */
  31397. +
  31398. +/*
  31399. + * whiteout for logical deletion and opaque directory
  31400. + */
  31401. +
  31402. +#include "aufs.h"
  31403. +
  31404. +#define WH_MASK S_IRUGO
  31405. +
  31406. +/*
  31407. + * If a directory contains this file, then it is opaque. We start with the
  31408. + * .wh. flag so that it is blocked by lookup.
  31409. + */
  31410. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  31411. + sizeof(AUFS_WH_DIROPQ) - 1);
  31412. +
  31413. +/*
  31414. + * generate whiteout name, which is NOT terminated by NULL.
  31415. + * @name: original d_name.name
  31416. + * @len: original d_name.len
  31417. + * @wh: whiteout qstr
  31418. + * returns zero when succeeds, otherwise error.
  31419. + * succeeded value as wh->name should be freed by kfree().
  31420. + */
  31421. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  31422. +{
  31423. + char *p;
  31424. +
  31425. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  31426. + return -ENAMETOOLONG;
  31427. +
  31428. + wh->len = name->len + AUFS_WH_PFX_LEN;
  31429. + p = kmalloc(wh->len, GFP_NOFS);
  31430. + wh->name = p;
  31431. + if (p) {
  31432. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31433. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  31434. + /* smp_mb(); */
  31435. + return 0;
  31436. + }
  31437. + return -ENOMEM;
  31438. +}
  31439. +
  31440. +/* ---------------------------------------------------------------------- */
  31441. +
  31442. +/*
  31443. + * test if the @wh_name exists under @h_parent.
  31444. + * @try_sio specifies the necessary of super-io.
  31445. + */
  31446. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  31447. +{
  31448. + int err;
  31449. + struct dentry *wh_dentry;
  31450. +
  31451. + if (!try_sio)
  31452. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  31453. + else
  31454. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  31455. + err = PTR_ERR(wh_dentry);
  31456. + if (IS_ERR(wh_dentry)) {
  31457. + if (err == -ENAMETOOLONG)
  31458. + err = 0;
  31459. + goto out;
  31460. + }
  31461. +
  31462. + err = 0;
  31463. + if (d_is_negative(wh_dentry))
  31464. + goto out_wh; /* success */
  31465. +
  31466. + err = 1;
  31467. + if (d_is_reg(wh_dentry))
  31468. + goto out_wh; /* success */
  31469. +
  31470. + err = -EIO;
  31471. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  31472. + wh_dentry, d_inode(wh_dentry)->i_mode);
  31473. +
  31474. +out_wh:
  31475. + dput(wh_dentry);
  31476. +out:
  31477. + return err;
  31478. +}
  31479. +
  31480. +/*
  31481. + * test if the @h_dentry sets opaque or not.
  31482. + */
  31483. +int au_diropq_test(struct dentry *h_dentry)
  31484. +{
  31485. + int err;
  31486. + struct inode *h_dir;
  31487. +
  31488. + h_dir = d_inode(h_dentry);
  31489. + err = au_wh_test(h_dentry, &diropq_name,
  31490. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  31491. + return err;
  31492. +}
  31493. +
  31494. +/*
  31495. + * returns a negative dentry whose name is unique and temporary.
  31496. + */
  31497. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  31498. + struct qstr *prefix)
  31499. +{
  31500. + struct dentry *dentry;
  31501. + int i;
  31502. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  31503. + *name, *p;
  31504. + /* strict atomic_t is unnecessary here */
  31505. + static unsigned short cnt;
  31506. + struct qstr qs;
  31507. +
  31508. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  31509. +
  31510. + name = defname;
  31511. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  31512. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  31513. + dentry = ERR_PTR(-ENAMETOOLONG);
  31514. + if (unlikely(qs.len > NAME_MAX))
  31515. + goto out;
  31516. + dentry = ERR_PTR(-ENOMEM);
  31517. + name = kmalloc(qs.len + 1, GFP_NOFS);
  31518. + if (unlikely(!name))
  31519. + goto out;
  31520. + }
  31521. +
  31522. + /* doubly whiteout-ed */
  31523. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  31524. + p = name + AUFS_WH_PFX_LEN * 2;
  31525. + memcpy(p, prefix->name, prefix->len);
  31526. + p += prefix->len;
  31527. + *p++ = '.';
  31528. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  31529. +
  31530. + qs.name = name;
  31531. + for (i = 0; i < 3; i++) {
  31532. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  31533. + dentry = au_sio_lkup_one(&qs, h_parent);
  31534. + if (IS_ERR(dentry) || d_is_negative(dentry))
  31535. + goto out_name;
  31536. + dput(dentry);
  31537. + }
  31538. + /* pr_warn("could not get random name\n"); */
  31539. + dentry = ERR_PTR(-EEXIST);
  31540. + AuDbg("%.*s\n", AuLNPair(&qs));
  31541. + BUG();
  31542. +
  31543. +out_name:
  31544. + if (name != defname)
  31545. + au_delayed_kfree(name);
  31546. +out:
  31547. + AuTraceErrPtr(dentry);
  31548. + return dentry;
  31549. +}
  31550. +
  31551. +/*
  31552. + * rename the @h_dentry on @br to the whiteouted temporary name.
  31553. + */
  31554. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  31555. +{
  31556. + int err;
  31557. + struct path h_path = {
  31558. + .mnt = au_br_mnt(br)
  31559. + };
  31560. + struct inode *h_dir, *delegated;
  31561. + struct dentry *h_parent;
  31562. +
  31563. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  31564. + h_dir = d_inode(h_parent);
  31565. + IMustLock(h_dir);
  31566. +
  31567. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  31568. + err = PTR_ERR(h_path.dentry);
  31569. + if (IS_ERR(h_path.dentry))
  31570. + goto out;
  31571. +
  31572. + /* under the same dir, no need to lock_rename() */
  31573. + delegated = NULL;
  31574. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
  31575. + AuTraceErr(err);
  31576. + if (unlikely(err == -EWOULDBLOCK)) {
  31577. + pr_warn("cannot retry for NFSv4 delegation"
  31578. + " for an internal rename\n");
  31579. + iput(delegated);
  31580. + }
  31581. + dput(h_path.dentry);
  31582. +
  31583. +out:
  31584. + AuTraceErr(err);
  31585. + return err;
  31586. +}
  31587. +
  31588. +/* ---------------------------------------------------------------------- */
  31589. +/*
  31590. + * functions for removing a whiteout
  31591. + */
  31592. +
  31593. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  31594. +{
  31595. + int err, force;
  31596. + struct inode *delegated;
  31597. +
  31598. + /*
  31599. + * forces superio when the dir has a sticky bit.
  31600. + * this may be a violation of unix fs semantics.
  31601. + */
  31602. + force = (h_dir->i_mode & S_ISVTX)
  31603. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  31604. + delegated = NULL;
  31605. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  31606. + if (unlikely(err == -EWOULDBLOCK)) {
  31607. + pr_warn("cannot retry for NFSv4 delegation"
  31608. + " for an internal unlink\n");
  31609. + iput(delegated);
  31610. + }
  31611. + return err;
  31612. +}
  31613. +
  31614. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  31615. + struct dentry *dentry)
  31616. +{
  31617. + int err;
  31618. +
  31619. + err = do_unlink_wh(h_dir, h_path);
  31620. + if (!err && dentry)
  31621. + au_set_dbwh(dentry, -1);
  31622. +
  31623. + return err;
  31624. +}
  31625. +
  31626. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  31627. + struct au_branch *br)
  31628. +{
  31629. + int err;
  31630. + struct path h_path = {
  31631. + .mnt = au_br_mnt(br)
  31632. + };
  31633. +
  31634. + err = 0;
  31635. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  31636. + if (IS_ERR(h_path.dentry))
  31637. + err = PTR_ERR(h_path.dentry);
  31638. + else {
  31639. + if (d_is_reg(h_path.dentry))
  31640. + err = do_unlink_wh(d_inode(h_parent), &h_path);
  31641. + dput(h_path.dentry);
  31642. + }
  31643. +
  31644. + return err;
  31645. +}
  31646. +
  31647. +/* ---------------------------------------------------------------------- */
  31648. +/*
  31649. + * initialize/clean whiteout for a branch
  31650. + */
  31651. +
  31652. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  31653. + const int isdir)
  31654. +{
  31655. + int err;
  31656. + struct inode *delegated;
  31657. +
  31658. + if (d_is_negative(whpath->dentry))
  31659. + return;
  31660. +
  31661. + if (isdir)
  31662. + err = vfsub_rmdir(h_dir, whpath);
  31663. + else {
  31664. + delegated = NULL;
  31665. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  31666. + if (unlikely(err == -EWOULDBLOCK)) {
  31667. + pr_warn("cannot retry for NFSv4 delegation"
  31668. + " for an internal unlink\n");
  31669. + iput(delegated);
  31670. + }
  31671. + }
  31672. + if (unlikely(err))
  31673. + pr_warn("failed removing %pd (%d), ignored.\n",
  31674. + whpath->dentry, err);
  31675. +}
  31676. +
  31677. +static int test_linkable(struct dentry *h_root)
  31678. +{
  31679. + struct inode *h_dir = d_inode(h_root);
  31680. +
  31681. + if (h_dir->i_op->link)
  31682. + return 0;
  31683. +
  31684. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  31685. + h_root, au_sbtype(h_root->d_sb));
  31686. + return -ENOSYS;
  31687. +}
  31688. +
  31689. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  31690. +static int au_whdir(struct inode *h_dir, struct path *path)
  31691. +{
  31692. + int err;
  31693. +
  31694. + err = -EEXIST;
  31695. + if (d_is_negative(path->dentry)) {
  31696. + int mode = S_IRWXU;
  31697. +
  31698. + if (au_test_nfs(path->dentry->d_sb))
  31699. + mode |= S_IXUGO;
  31700. + err = vfsub_mkdir(h_dir, path, mode);
  31701. + } else if (d_is_dir(path->dentry))
  31702. + err = 0;
  31703. + else
  31704. + pr_err("unknown %pd exists\n", path->dentry);
  31705. +
  31706. + return err;
  31707. +}
  31708. +
  31709. +struct au_wh_base {
  31710. + const struct qstr *name;
  31711. + struct dentry *dentry;
  31712. +};
  31713. +
  31714. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  31715. + struct path *h_path)
  31716. +{
  31717. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31718. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  31719. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31720. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31721. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31722. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31723. +}
  31724. +
  31725. +/*
  31726. + * returns tri-state,
  31727. + * minus: error, caller should print the message
  31728. + * zero: succuess
  31729. + * plus: error, caller should NOT print the message
  31730. + */
  31731. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  31732. + int do_plink, struct au_wh_base base[],
  31733. + struct path *h_path)
  31734. +{
  31735. + int err;
  31736. + struct inode *h_dir;
  31737. +
  31738. + h_dir = d_inode(h_root);
  31739. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31740. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  31741. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31742. + if (do_plink) {
  31743. + err = test_linkable(h_root);
  31744. + if (unlikely(err)) {
  31745. + err = 1;
  31746. + goto out;
  31747. + }
  31748. +
  31749. + err = au_whdir(h_dir, h_path);
  31750. + if (unlikely(err))
  31751. + goto out;
  31752. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  31753. + } else
  31754. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31755. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31756. + err = au_whdir(h_dir, h_path);
  31757. + if (unlikely(err))
  31758. + goto out;
  31759. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  31760. +
  31761. +out:
  31762. + return err;
  31763. +}
  31764. +
  31765. +/*
  31766. + * for the moment, aufs supports the branch filesystem which does not support
  31767. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  31768. + * copyup failed. finally, such filesystem will not be used as the writable
  31769. + * branch.
  31770. + *
  31771. + * returns tri-state, see above.
  31772. + */
  31773. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  31774. + int do_plink, struct au_wh_base base[],
  31775. + struct path *h_path)
  31776. +{
  31777. + int err;
  31778. + struct inode *h_dir;
  31779. +
  31780. + WbrWhMustWriteLock(wbr);
  31781. +
  31782. + err = test_linkable(h_root);
  31783. + if (unlikely(err)) {
  31784. + err = 1;
  31785. + goto out;
  31786. + }
  31787. +
  31788. + /*
  31789. + * todo: should this create be done in /sbin/mount.aufs helper?
  31790. + */
  31791. + err = -EEXIST;
  31792. + h_dir = d_inode(h_root);
  31793. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  31794. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31795. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  31796. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  31797. + err = 0;
  31798. + else
  31799. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  31800. + if (unlikely(err))
  31801. + goto out;
  31802. +
  31803. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31804. + if (do_plink) {
  31805. + err = au_whdir(h_dir, h_path);
  31806. + if (unlikely(err))
  31807. + goto out;
  31808. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  31809. + } else
  31810. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31811. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  31812. +
  31813. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31814. + err = au_whdir(h_dir, h_path);
  31815. + if (unlikely(err))
  31816. + goto out;
  31817. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  31818. +
  31819. +out:
  31820. + return err;
  31821. +}
  31822. +
  31823. +/*
  31824. + * initialize the whiteout base file/dir for @br.
  31825. + */
  31826. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  31827. +{
  31828. + int err, i;
  31829. + const unsigned char do_plink
  31830. + = !!au_opt_test(au_mntflags(sb), PLINK);
  31831. + struct inode *h_dir;
  31832. + struct path path = br->br_path;
  31833. + struct dentry *h_root = path.dentry;
  31834. + struct au_wbr *wbr = br->br_wbr;
  31835. + static const struct qstr base_name[] = {
  31836. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  31837. + sizeof(AUFS_BASE_NAME) - 1),
  31838. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  31839. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  31840. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  31841. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  31842. + };
  31843. + struct au_wh_base base[] = {
  31844. + [AuBrWh_BASE] = {
  31845. + .name = base_name + AuBrWh_BASE,
  31846. + .dentry = NULL
  31847. + },
  31848. + [AuBrWh_PLINK] = {
  31849. + .name = base_name + AuBrWh_PLINK,
  31850. + .dentry = NULL
  31851. + },
  31852. + [AuBrWh_ORPH] = {
  31853. + .name = base_name + AuBrWh_ORPH,
  31854. + .dentry = NULL
  31855. + }
  31856. + };
  31857. +
  31858. + if (wbr)
  31859. + WbrWhMustWriteLock(wbr);
  31860. +
  31861. + for (i = 0; i < AuBrWh_Last; i++) {
  31862. + /* doubly whiteouted */
  31863. + struct dentry *d;
  31864. +
  31865. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  31866. + err = PTR_ERR(d);
  31867. + if (IS_ERR(d))
  31868. + goto out;
  31869. +
  31870. + base[i].dentry = d;
  31871. + AuDebugOn(wbr
  31872. + && wbr->wbr_wh[i]
  31873. + && wbr->wbr_wh[i] != base[i].dentry);
  31874. + }
  31875. +
  31876. + if (wbr)
  31877. + for (i = 0; i < AuBrWh_Last; i++) {
  31878. + dput(wbr->wbr_wh[i]);
  31879. + wbr->wbr_wh[i] = NULL;
  31880. + }
  31881. +
  31882. + err = 0;
  31883. + if (!au_br_writable(br->br_perm)) {
  31884. + h_dir = d_inode(h_root);
  31885. + au_wh_init_ro(h_dir, base, &path);
  31886. + } else if (!au_br_wh_linkable(br->br_perm)) {
  31887. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  31888. + if (err > 0)
  31889. + goto out;
  31890. + else if (err)
  31891. + goto out_err;
  31892. + } else {
  31893. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  31894. + if (err > 0)
  31895. + goto out;
  31896. + else if (err)
  31897. + goto out_err;
  31898. + }
  31899. + goto out; /* success */
  31900. +
  31901. +out_err:
  31902. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  31903. + err, h_root, au_sbtype(h_root->d_sb));
  31904. +out:
  31905. + for (i = 0; i < AuBrWh_Last; i++)
  31906. + dput(base[i].dentry);
  31907. + return err;
  31908. +}
  31909. +
  31910. +/* ---------------------------------------------------------------------- */
  31911. +/*
  31912. + * whiteouts are all hard-linked usually.
  31913. + * when its link count reaches a ceiling, we create a new whiteout base
  31914. + * asynchronously.
  31915. + */
  31916. +
  31917. +struct reinit_br_wh {
  31918. + struct super_block *sb;
  31919. + struct au_branch *br;
  31920. +};
  31921. +
  31922. +static void reinit_br_wh(void *arg)
  31923. +{
  31924. + int err;
  31925. + aufs_bindex_t bindex;
  31926. + struct path h_path;
  31927. + struct reinit_br_wh *a = arg;
  31928. + struct au_wbr *wbr;
  31929. + struct inode *dir, *delegated;
  31930. + struct dentry *h_root;
  31931. + struct au_hinode *hdir;
  31932. +
  31933. + err = 0;
  31934. + wbr = a->br->br_wbr;
  31935. + /* big aufs lock */
  31936. + si_noflush_write_lock(a->sb);
  31937. + if (!au_br_writable(a->br->br_perm))
  31938. + goto out;
  31939. + bindex = au_br_index(a->sb, a->br->br_id);
  31940. + if (unlikely(bindex < 0))
  31941. + goto out;
  31942. +
  31943. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  31944. + dir = d_inode(a->sb->s_root);
  31945. + hdir = au_hi(dir, bindex);
  31946. + h_root = au_h_dptr(a->sb->s_root, bindex);
  31947. + AuDebugOn(h_root != au_br_dentry(a->br));
  31948. +
  31949. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  31950. + wbr_wh_write_lock(wbr);
  31951. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  31952. + h_root, a->br);
  31953. + if (!err) {
  31954. + h_path.dentry = wbr->wbr_whbase;
  31955. + h_path.mnt = au_br_mnt(a->br);
  31956. + delegated = NULL;
  31957. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  31958. + /*force*/0);
  31959. + if (unlikely(err == -EWOULDBLOCK)) {
  31960. + pr_warn("cannot retry for NFSv4 delegation"
  31961. + " for an internal unlink\n");
  31962. + iput(delegated);
  31963. + }
  31964. + } else {
  31965. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  31966. + err = 0;
  31967. + }
  31968. + dput(wbr->wbr_whbase);
  31969. + wbr->wbr_whbase = NULL;
  31970. + if (!err)
  31971. + err = au_wh_init(a->br, a->sb);
  31972. + wbr_wh_write_unlock(wbr);
  31973. + au_hn_imtx_unlock(hdir);
  31974. + di_read_unlock(a->sb->s_root, AuLock_IR);
  31975. + if (!err)
  31976. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  31977. +
  31978. +out:
  31979. + if (wbr)
  31980. + atomic_dec(&wbr->wbr_wh_running);
  31981. + au_br_put(a->br);
  31982. + si_write_unlock(a->sb);
  31983. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  31984. + au_delayed_kfree(arg);
  31985. + if (unlikely(err))
  31986. + AuIOErr("err %d\n", err);
  31987. +}
  31988. +
  31989. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  31990. +{
  31991. + int do_dec, wkq_err;
  31992. + struct reinit_br_wh *arg;
  31993. +
  31994. + do_dec = 1;
  31995. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  31996. + goto out;
  31997. +
  31998. + /* ignore ENOMEM */
  31999. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  32000. + if (arg) {
  32001. + /*
  32002. + * dec(wh_running), kfree(arg) and dec(br_count)
  32003. + * in reinit function
  32004. + */
  32005. + arg->sb = sb;
  32006. + arg->br = br;
  32007. + au_br_get(br);
  32008. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  32009. + if (unlikely(wkq_err)) {
  32010. + atomic_dec(&br->br_wbr->wbr_wh_running);
  32011. + au_br_put(br);
  32012. + au_delayed_kfree(arg);
  32013. + }
  32014. + do_dec = 0;
  32015. + }
  32016. +
  32017. +out:
  32018. + if (do_dec)
  32019. + atomic_dec(&br->br_wbr->wbr_wh_running);
  32020. +}
  32021. +
  32022. +/* ---------------------------------------------------------------------- */
  32023. +
  32024. +/*
  32025. + * create the whiteout @wh.
  32026. + */
  32027. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  32028. + struct dentry *wh)
  32029. +{
  32030. + int err;
  32031. + struct path h_path = {
  32032. + .dentry = wh
  32033. + };
  32034. + struct au_branch *br;
  32035. + struct au_wbr *wbr;
  32036. + struct dentry *h_parent;
  32037. + struct inode *h_dir, *delegated;
  32038. +
  32039. + h_parent = wh->d_parent; /* dir inode is locked */
  32040. + h_dir = d_inode(h_parent);
  32041. + IMustLock(h_dir);
  32042. +
  32043. + br = au_sbr(sb, bindex);
  32044. + h_path.mnt = au_br_mnt(br);
  32045. + wbr = br->br_wbr;
  32046. + wbr_wh_read_lock(wbr);
  32047. + if (wbr->wbr_whbase) {
  32048. + delegated = NULL;
  32049. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  32050. + if (unlikely(err == -EWOULDBLOCK)) {
  32051. + pr_warn("cannot retry for NFSv4 delegation"
  32052. + " for an internal link\n");
  32053. + iput(delegated);
  32054. + }
  32055. + if (!err || err != -EMLINK)
  32056. + goto out;
  32057. +
  32058. + /* link count full. re-initialize br_whbase. */
  32059. + kick_reinit_br_wh(sb, br);
  32060. + }
  32061. +
  32062. + /* return this error in this context */
  32063. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  32064. + if (!err)
  32065. + au_fhsm_wrote(sb, bindex, /*force*/0);
  32066. +
  32067. +out:
  32068. + wbr_wh_read_unlock(wbr);
  32069. + return err;
  32070. +}
  32071. +
  32072. +/* ---------------------------------------------------------------------- */
  32073. +
  32074. +/*
  32075. + * create or remove the diropq.
  32076. + */
  32077. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  32078. + unsigned int flags)
  32079. +{
  32080. + struct dentry *opq_dentry, *h_dentry;
  32081. + struct super_block *sb;
  32082. + struct au_branch *br;
  32083. + int err;
  32084. +
  32085. + sb = dentry->d_sb;
  32086. + br = au_sbr(sb, bindex);
  32087. + h_dentry = au_h_dptr(dentry, bindex);
  32088. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  32089. + if (IS_ERR(opq_dentry))
  32090. + goto out;
  32091. +
  32092. + if (au_ftest_diropq(flags, CREATE)) {
  32093. + err = link_or_create_wh(sb, bindex, opq_dentry);
  32094. + if (!err) {
  32095. + au_set_dbdiropq(dentry, bindex);
  32096. + goto out; /* success */
  32097. + }
  32098. + } else {
  32099. + struct path tmp = {
  32100. + .dentry = opq_dentry,
  32101. + .mnt = au_br_mnt(br)
  32102. + };
  32103. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
  32104. + if (!err)
  32105. + au_set_dbdiropq(dentry, -1);
  32106. + }
  32107. + dput(opq_dentry);
  32108. + opq_dentry = ERR_PTR(err);
  32109. +
  32110. +out:
  32111. + return opq_dentry;
  32112. +}
  32113. +
  32114. +struct do_diropq_args {
  32115. + struct dentry **errp;
  32116. + struct dentry *dentry;
  32117. + aufs_bindex_t bindex;
  32118. + unsigned int flags;
  32119. +};
  32120. +
  32121. +static void call_do_diropq(void *args)
  32122. +{
  32123. + struct do_diropq_args *a = args;
  32124. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  32125. +}
  32126. +
  32127. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  32128. + unsigned int flags)
  32129. +{
  32130. + struct dentry *diropq, *h_dentry;
  32131. +
  32132. + h_dentry = au_h_dptr(dentry, bindex);
  32133. + if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
  32134. + diropq = do_diropq(dentry, bindex, flags);
  32135. + else {
  32136. + int wkq_err;
  32137. + struct do_diropq_args args = {
  32138. + .errp = &diropq,
  32139. + .dentry = dentry,
  32140. + .bindex = bindex,
  32141. + .flags = flags
  32142. + };
  32143. +
  32144. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  32145. + if (unlikely(wkq_err))
  32146. + diropq = ERR_PTR(wkq_err);
  32147. + }
  32148. +
  32149. + return diropq;
  32150. +}
  32151. +
  32152. +/* ---------------------------------------------------------------------- */
  32153. +
  32154. +/*
  32155. + * lookup whiteout dentry.
  32156. + * @h_parent: lower parent dentry which must exist and be locked
  32157. + * @base_name: name of dentry which will be whiteouted
  32158. + * returns dentry for whiteout.
  32159. + */
  32160. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  32161. + struct au_branch *br)
  32162. +{
  32163. + int err;
  32164. + struct qstr wh_name;
  32165. + struct dentry *wh_dentry;
  32166. +
  32167. + err = au_wh_name_alloc(&wh_name, base_name);
  32168. + wh_dentry = ERR_PTR(err);
  32169. + if (!err) {
  32170. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  32171. + au_delayed_kfree(wh_name.name);
  32172. + }
  32173. + return wh_dentry;
  32174. +}
  32175. +
  32176. +/*
  32177. + * link/create a whiteout for @dentry on @bindex.
  32178. + */
  32179. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  32180. + struct dentry *h_parent)
  32181. +{
  32182. + struct dentry *wh_dentry;
  32183. + struct super_block *sb;
  32184. + int err;
  32185. +
  32186. + sb = dentry->d_sb;
  32187. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  32188. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  32189. + err = link_or_create_wh(sb, bindex, wh_dentry);
  32190. + if (!err) {
  32191. + au_set_dbwh(dentry, bindex);
  32192. + au_fhsm_wrote(sb, bindex, /*force*/0);
  32193. + } else {
  32194. + dput(wh_dentry);
  32195. + wh_dentry = ERR_PTR(err);
  32196. + }
  32197. + }
  32198. +
  32199. + return wh_dentry;
  32200. +}
  32201. +
  32202. +/* ---------------------------------------------------------------------- */
  32203. +
  32204. +/* Delete all whiteouts in this directory on branch bindex. */
  32205. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  32206. + aufs_bindex_t bindex, struct au_branch *br)
  32207. +{
  32208. + int err;
  32209. + unsigned long ul, n;
  32210. + struct qstr wh_name;
  32211. + char *p;
  32212. + struct hlist_head *head;
  32213. + struct au_vdir_wh *pos;
  32214. + struct au_vdir_destr *str;
  32215. +
  32216. + err = -ENOMEM;
  32217. + p = (void *)__get_free_page(GFP_NOFS);
  32218. + wh_name.name = p;
  32219. + if (unlikely(!wh_name.name))
  32220. + goto out;
  32221. +
  32222. + err = 0;
  32223. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  32224. + p += AUFS_WH_PFX_LEN;
  32225. + n = whlist->nh_num;
  32226. + head = whlist->nh_head;
  32227. + for (ul = 0; !err && ul < n; ul++, head++) {
  32228. + hlist_for_each_entry(pos, head, wh_hash) {
  32229. + if (pos->wh_bindex != bindex)
  32230. + continue;
  32231. +
  32232. + str = &pos->wh_str;
  32233. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  32234. + memcpy(p, str->name, str->len);
  32235. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  32236. + err = unlink_wh_name(h_dentry, &wh_name, br);
  32237. + if (!err)
  32238. + continue;
  32239. + break;
  32240. + }
  32241. + AuIOErr("whiteout name too long %.*s\n",
  32242. + str->len, str->name);
  32243. + err = -EIO;
  32244. + break;
  32245. + }
  32246. + }
  32247. + au_delayed_free_page((unsigned long)wh_name.name);
  32248. +
  32249. +out:
  32250. + return err;
  32251. +}
  32252. +
  32253. +struct del_wh_children_args {
  32254. + int *errp;
  32255. + struct dentry *h_dentry;
  32256. + struct au_nhash *whlist;
  32257. + aufs_bindex_t bindex;
  32258. + struct au_branch *br;
  32259. +};
  32260. +
  32261. +static void call_del_wh_children(void *args)
  32262. +{
  32263. + struct del_wh_children_args *a = args;
  32264. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  32265. +}
  32266. +
  32267. +/* ---------------------------------------------------------------------- */
  32268. +
  32269. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  32270. +{
  32271. + struct au_whtmp_rmdir *whtmp;
  32272. + int err;
  32273. + unsigned int rdhash;
  32274. +
  32275. + SiMustAnyLock(sb);
  32276. +
  32277. + whtmp = kzalloc(sizeof(*whtmp), gfp);
  32278. + if (unlikely(!whtmp)) {
  32279. + whtmp = ERR_PTR(-ENOMEM);
  32280. + goto out;
  32281. + }
  32282. +
  32283. + /* no estimation for dir size */
  32284. + rdhash = au_sbi(sb)->si_rdhash;
  32285. + if (!rdhash)
  32286. + rdhash = AUFS_RDHASH_DEF;
  32287. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  32288. + if (unlikely(err)) {
  32289. + au_delayed_kfree(whtmp);
  32290. + whtmp = ERR_PTR(err);
  32291. + }
  32292. +
  32293. +out:
  32294. + return whtmp;
  32295. +}
  32296. +
  32297. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  32298. +{
  32299. + if (whtmp->br)
  32300. + au_br_put(whtmp->br);
  32301. + dput(whtmp->wh_dentry);
  32302. + iput(whtmp->dir);
  32303. + au_nhash_wh_free(&whtmp->whlist);
  32304. + au_delayed_kfree(whtmp);
  32305. +}
  32306. +
  32307. +/*
  32308. + * rmdir the whiteouted temporary named dir @h_dentry.
  32309. + * @whlist: whiteouted children.
  32310. + */
  32311. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32312. + struct dentry *wh_dentry, struct au_nhash *whlist)
  32313. +{
  32314. + int err;
  32315. + unsigned int h_nlink;
  32316. + struct path h_tmp;
  32317. + struct inode *wh_inode, *h_dir;
  32318. + struct au_branch *br;
  32319. +
  32320. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  32321. + IMustLock(h_dir);
  32322. +
  32323. + br = au_sbr(dir->i_sb, bindex);
  32324. + wh_inode = d_inode(wh_dentry);
  32325. + mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
  32326. +
  32327. + /*
  32328. + * someone else might change some whiteouts while we were sleeping.
  32329. + * it means this whlist may have an obsoleted entry.
  32330. + */
  32331. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  32332. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  32333. + else {
  32334. + int wkq_err;
  32335. + struct del_wh_children_args args = {
  32336. + .errp = &err,
  32337. + .h_dentry = wh_dentry,
  32338. + .whlist = whlist,
  32339. + .bindex = bindex,
  32340. + .br = br
  32341. + };
  32342. +
  32343. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  32344. + if (unlikely(wkq_err))
  32345. + err = wkq_err;
  32346. + }
  32347. + mutex_unlock(&wh_inode->i_mutex);
  32348. +
  32349. + if (!err) {
  32350. + h_tmp.dentry = wh_dentry;
  32351. + h_tmp.mnt = au_br_mnt(br);
  32352. + h_nlink = h_dir->i_nlink;
  32353. + err = vfsub_rmdir(h_dir, &h_tmp);
  32354. + /* some fs doesn't change the parent nlink in some cases */
  32355. + h_nlink -= h_dir->i_nlink;
  32356. + }
  32357. +
  32358. + if (!err) {
  32359. + if (au_ibtop(dir) == bindex) {
  32360. + /* todo: dir->i_mutex is necessary */
  32361. + au_cpup_attr_timesizes(dir);
  32362. + if (h_nlink)
  32363. + vfsub_drop_nlink(dir);
  32364. + }
  32365. + return 0; /* success */
  32366. + }
  32367. +
  32368. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  32369. + return err;
  32370. +}
  32371. +
  32372. +static void call_rmdir_whtmp(void *args)
  32373. +{
  32374. + int err;
  32375. + aufs_bindex_t bindex;
  32376. + struct au_whtmp_rmdir *a = args;
  32377. + struct super_block *sb;
  32378. + struct dentry *h_parent;
  32379. + struct inode *h_dir;
  32380. + struct au_hinode *hdir;
  32381. +
  32382. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  32383. + /* mutex_lock(&a->dir->i_mutex); */
  32384. + err = -EROFS;
  32385. + sb = a->dir->i_sb;
  32386. + si_read_lock(sb, !AuLock_FLUSH);
  32387. + if (!au_br_writable(a->br->br_perm))
  32388. + goto out;
  32389. + bindex = au_br_index(sb, a->br->br_id);
  32390. + if (unlikely(bindex < 0))
  32391. + goto out;
  32392. +
  32393. + err = -EIO;
  32394. + ii_write_lock_parent(a->dir);
  32395. + h_parent = dget_parent(a->wh_dentry);
  32396. + h_dir = d_inode(h_parent);
  32397. + hdir = au_hi(a->dir, bindex);
  32398. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  32399. + if (unlikely(err))
  32400. + goto out_mnt;
  32401. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  32402. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  32403. + a->br);
  32404. + if (!err)
  32405. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  32406. + au_hn_imtx_unlock(hdir);
  32407. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  32408. +
  32409. +out_mnt:
  32410. + dput(h_parent);
  32411. + ii_write_unlock(a->dir);
  32412. +out:
  32413. + /* mutex_unlock(&a->dir->i_mutex); */
  32414. + au_whtmp_rmdir_free(a);
  32415. + si_read_unlock(sb);
  32416. + au_nwt_done(&au_sbi(sb)->si_nowait);
  32417. + if (unlikely(err))
  32418. + AuIOErr("err %d\n", err);
  32419. +}
  32420. +
  32421. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32422. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  32423. +{
  32424. + int wkq_err;
  32425. + struct super_block *sb;
  32426. +
  32427. + IMustLock(dir);
  32428. +
  32429. + /* all post-process will be done in do_rmdir_whtmp(). */
  32430. + sb = dir->i_sb;
  32431. + args->dir = au_igrab(dir);
  32432. + args->br = au_sbr(sb, bindex);
  32433. + au_br_get(args->br);
  32434. + args->wh_dentry = dget(wh_dentry);
  32435. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  32436. + if (unlikely(wkq_err)) {
  32437. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  32438. + au_whtmp_rmdir_free(args);
  32439. + }
  32440. +}
  32441. --- /dev/null
  32442. +++ linux-4.4/fs/aufs/whout.h 2016-10-23 11:20:57.642159951 +0300
  32443. @@ -0,0 +1,85 @@
  32444. +/*
  32445. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  32446. + *
  32447. + * This program, aufs is free software; you can redistribute it and/or modify
  32448. + * it under the terms of the GNU General Public License as published by
  32449. + * the Free Software Foundation; either version 2 of the License, or
  32450. + * (at your option) any later version.
  32451. + *
  32452. + * This program is distributed in the hope that it will be useful,
  32453. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32454. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32455. + * GNU General Public License for more details.
  32456. + *
  32457. + * You should have received a copy of the GNU General Public License
  32458. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32459. + */
  32460. +
  32461. +/*
  32462. + * whiteout for logical deletion and opaque directory
  32463. + */
  32464. +
  32465. +#ifndef __AUFS_WHOUT_H__
  32466. +#define __AUFS_WHOUT_H__
  32467. +
  32468. +#ifdef __KERNEL__
  32469. +
  32470. +#include "dir.h"
  32471. +
  32472. +/* whout.c */
  32473. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  32474. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  32475. +int au_diropq_test(struct dentry *h_dentry);
  32476. +struct au_branch;
  32477. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  32478. + struct qstr *prefix);
  32479. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  32480. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  32481. + struct dentry *dentry);
  32482. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  32483. +
  32484. +/* diropq flags */
  32485. +#define AuDiropq_CREATE 1
  32486. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  32487. +#define au_fset_diropq(flags, name) \
  32488. + do { (flags) |= AuDiropq_##name; } while (0)
  32489. +#define au_fclr_diropq(flags, name) \
  32490. + do { (flags) &= ~AuDiropq_##name; } while (0)
  32491. +
  32492. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  32493. + unsigned int flags);
  32494. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  32495. + struct au_branch *br);
  32496. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  32497. + struct dentry *h_parent);
  32498. +
  32499. +/* real rmdir for the whiteout-ed dir */
  32500. +struct au_whtmp_rmdir {
  32501. + struct inode *dir;
  32502. + struct au_branch *br;
  32503. + struct dentry *wh_dentry;
  32504. + struct au_nhash whlist;
  32505. +};
  32506. +
  32507. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  32508. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  32509. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32510. + struct dentry *wh_dentry, struct au_nhash *whlist);
  32511. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  32512. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  32513. +
  32514. +/* ---------------------------------------------------------------------- */
  32515. +
  32516. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  32517. + aufs_bindex_t bindex)
  32518. +{
  32519. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  32520. +}
  32521. +
  32522. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  32523. +{
  32524. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  32525. +}
  32526. +
  32527. +#endif /* __KERNEL__ */
  32528. +#endif /* __AUFS_WHOUT_H__ */
  32529. --- /dev/null
  32530. +++ linux-4.4/fs/aufs/wkq.c 2016-10-23 11:20:57.642159951 +0300
  32531. @@ -0,0 +1,213 @@
  32532. +/*
  32533. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  32534. + *
  32535. + * This program, aufs is free software; you can redistribute it and/or modify
  32536. + * it under the terms of the GNU General Public License as published by
  32537. + * the Free Software Foundation; either version 2 of the License, or
  32538. + * (at your option) any later version.
  32539. + *
  32540. + * This program is distributed in the hope that it will be useful,
  32541. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32542. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32543. + * GNU General Public License for more details.
  32544. + *
  32545. + * You should have received a copy of the GNU General Public License
  32546. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32547. + */
  32548. +
  32549. +/*
  32550. + * workqueue for asynchronous/super-io operations
  32551. + * todo: try new dredential scheme
  32552. + */
  32553. +
  32554. +#include <linux/module.h>
  32555. +#include "aufs.h"
  32556. +
  32557. +/* internal workqueue named AUFS_WKQ_NAME */
  32558. +
  32559. +static struct workqueue_struct *au_wkq;
  32560. +
  32561. +struct au_wkinfo {
  32562. + struct work_struct wk;
  32563. + struct kobject *kobj;
  32564. +
  32565. + unsigned int flags; /* see wkq.h */
  32566. +
  32567. + au_wkq_func_t func;
  32568. + void *args;
  32569. +
  32570. + struct completion *comp;
  32571. +};
  32572. +
  32573. +/* ---------------------------------------------------------------------- */
  32574. +
  32575. +static void wkq_func(struct work_struct *wk)
  32576. +{
  32577. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  32578. +
  32579. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  32580. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  32581. +
  32582. + wkinfo->func(wkinfo->args);
  32583. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  32584. + complete(wkinfo->comp);
  32585. + else {
  32586. + kobject_put(wkinfo->kobj);
  32587. + module_put(THIS_MODULE); /* todo: ?? */
  32588. + au_delayed_kfree(wkinfo);
  32589. + }
  32590. +}
  32591. +
  32592. +/*
  32593. + * Since struct completion is large, try allocating it dynamically.
  32594. + */
  32595. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  32596. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  32597. +
  32598. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  32599. +{
  32600. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  32601. + if (*comp) {
  32602. + init_completion(*comp);
  32603. + wkinfo->comp = *comp;
  32604. + return 0;
  32605. + }
  32606. + return -ENOMEM;
  32607. +}
  32608. +
  32609. +static void au_wkq_comp_free(struct completion *comp)
  32610. +{
  32611. + au_delayed_kfree(comp);
  32612. +}
  32613. +
  32614. +#else
  32615. +
  32616. +/* no braces */
  32617. +#define AuWkqCompDeclare(name) \
  32618. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  32619. + struct completion *comp = &_ ## name
  32620. +
  32621. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  32622. +{
  32623. + wkinfo->comp = *comp;
  32624. + return 0;
  32625. +}
  32626. +
  32627. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  32628. +{
  32629. + /* empty */
  32630. +}
  32631. +#endif /* 4KSTACKS */
  32632. +
  32633. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  32634. +{
  32635. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  32636. + if (au_wkq_test()) {
  32637. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  32638. + " due to a dead dir by UDBA?\n");
  32639. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  32640. + }
  32641. + } else
  32642. + au_dbg_verify_kthread();
  32643. +
  32644. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  32645. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  32646. + queue_work(au_wkq, &wkinfo->wk);
  32647. + } else {
  32648. + INIT_WORK(&wkinfo->wk, wkq_func);
  32649. + schedule_work(&wkinfo->wk);
  32650. + }
  32651. +}
  32652. +
  32653. +/*
  32654. + * Be careful. It is easy to make deadlock happen.
  32655. + * processA: lock, wkq and wait
  32656. + * processB: wkq and wait, lock in wkq
  32657. + * --> deadlock
  32658. + */
  32659. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  32660. +{
  32661. + int err;
  32662. + AuWkqCompDeclare(comp);
  32663. + struct au_wkinfo wkinfo = {
  32664. + .flags = flags,
  32665. + .func = func,
  32666. + .args = args
  32667. + };
  32668. +
  32669. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  32670. + if (!err) {
  32671. + au_wkq_run(&wkinfo);
  32672. + /* no timeout, no interrupt */
  32673. + wait_for_completion(wkinfo.comp);
  32674. + au_wkq_comp_free(comp);
  32675. + destroy_work_on_stack(&wkinfo.wk);
  32676. + }
  32677. +
  32678. + return err;
  32679. +
  32680. +}
  32681. +
  32682. +/*
  32683. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  32684. + * problem in a concurrent umounting.
  32685. + */
  32686. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  32687. + unsigned int flags)
  32688. +{
  32689. + int err;
  32690. + struct au_wkinfo *wkinfo;
  32691. +
  32692. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  32693. +
  32694. + /*
  32695. + * wkq_func() must free this wkinfo.
  32696. + * it highly depends upon the implementation of workqueue.
  32697. + */
  32698. + err = 0;
  32699. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  32700. + if (wkinfo) {
  32701. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  32702. + wkinfo->flags = flags & ~AuWkq_WAIT;
  32703. + wkinfo->func = func;
  32704. + wkinfo->args = args;
  32705. + wkinfo->comp = NULL;
  32706. + kobject_get(wkinfo->kobj);
  32707. + __module_get(THIS_MODULE); /* todo: ?? */
  32708. +
  32709. + au_wkq_run(wkinfo);
  32710. + } else {
  32711. + err = -ENOMEM;
  32712. + au_nwt_done(&au_sbi(sb)->si_nowait);
  32713. + }
  32714. +
  32715. + return err;
  32716. +}
  32717. +
  32718. +/* ---------------------------------------------------------------------- */
  32719. +
  32720. +void au_nwt_init(struct au_nowait_tasks *nwt)
  32721. +{
  32722. + atomic_set(&nwt->nw_len, 0);
  32723. + /* smp_mb(); */ /* atomic_set */
  32724. + init_waitqueue_head(&nwt->nw_wq);
  32725. +}
  32726. +
  32727. +void au_wkq_fin(void)
  32728. +{
  32729. + destroy_workqueue(au_wkq);
  32730. +}
  32731. +
  32732. +int __init au_wkq_init(void)
  32733. +{
  32734. + int err;
  32735. +
  32736. + err = 0;
  32737. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  32738. + if (IS_ERR(au_wkq))
  32739. + err = PTR_ERR(au_wkq);
  32740. + else if (!au_wkq)
  32741. + err = -ENOMEM;
  32742. +
  32743. + return err;
  32744. +}
  32745. --- /dev/null
  32746. +++ linux-4.4/fs/aufs/wkq.h 2016-10-23 11:20:57.642159951 +0300
  32747. @@ -0,0 +1,91 @@
  32748. +/*
  32749. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  32750. + *
  32751. + * This program, aufs is free software; you can redistribute it and/or modify
  32752. + * it under the terms of the GNU General Public License as published by
  32753. + * the Free Software Foundation; either version 2 of the License, or
  32754. + * (at your option) any later version.
  32755. + *
  32756. + * This program is distributed in the hope that it will be useful,
  32757. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32758. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32759. + * GNU General Public License for more details.
  32760. + *
  32761. + * You should have received a copy of the GNU General Public License
  32762. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32763. + */
  32764. +
  32765. +/*
  32766. + * workqueue for asynchronous/super-io operations
  32767. + * todo: try new credentials management scheme
  32768. + */
  32769. +
  32770. +#ifndef __AUFS_WKQ_H__
  32771. +#define __AUFS_WKQ_H__
  32772. +
  32773. +#ifdef __KERNEL__
  32774. +
  32775. +struct super_block;
  32776. +
  32777. +/* ---------------------------------------------------------------------- */
  32778. +
  32779. +/*
  32780. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  32781. + */
  32782. +struct au_nowait_tasks {
  32783. + atomic_t nw_len;
  32784. + wait_queue_head_t nw_wq;
  32785. +};
  32786. +
  32787. +/* ---------------------------------------------------------------------- */
  32788. +
  32789. +typedef void (*au_wkq_func_t)(void *args);
  32790. +
  32791. +/* wkq flags */
  32792. +#define AuWkq_WAIT 1
  32793. +#define AuWkq_NEST (1 << 1)
  32794. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  32795. +#define au_fset_wkq(flags, name) \
  32796. + do { (flags) |= AuWkq_##name; } while (0)
  32797. +#define au_fclr_wkq(flags, name) \
  32798. + do { (flags) &= ~AuWkq_##name; } while (0)
  32799. +
  32800. +#ifndef CONFIG_AUFS_HNOTIFY
  32801. +#undef AuWkq_NEST
  32802. +#define AuWkq_NEST 0
  32803. +#endif
  32804. +
  32805. +/* wkq.c */
  32806. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  32807. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  32808. + unsigned int flags);
  32809. +void au_nwt_init(struct au_nowait_tasks *nwt);
  32810. +int __init au_wkq_init(void);
  32811. +void au_wkq_fin(void);
  32812. +
  32813. +/* ---------------------------------------------------------------------- */
  32814. +
  32815. +static inline int au_wkq_test(void)
  32816. +{
  32817. + return current->flags & PF_WQ_WORKER;
  32818. +}
  32819. +
  32820. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  32821. +{
  32822. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  32823. +}
  32824. +
  32825. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  32826. +{
  32827. + if (atomic_dec_and_test(&nwt->nw_len))
  32828. + wake_up_all(&nwt->nw_wq);
  32829. +}
  32830. +
  32831. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  32832. +{
  32833. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  32834. + return 0;
  32835. +}
  32836. +
  32837. +#endif /* __KERNEL__ */
  32838. +#endif /* __AUFS_WKQ_H__ */
  32839. --- /dev/null
  32840. +++ linux-4.4/fs/aufs/xattr.c 2016-10-23 11:20:57.642159951 +0300
  32841. @@ -0,0 +1,346 @@
  32842. +/*
  32843. + * Copyright (C) 2014-2016 Junjiro R. Okajima
  32844. + *
  32845. + * This program, aufs is free software; you can redistribute it and/or modify
  32846. + * it under the terms of the GNU General Public License as published by
  32847. + * the Free Software Foundation; either version 2 of the License, or
  32848. + * (at your option) any later version.
  32849. + *
  32850. + * This program is distributed in the hope that it will be useful,
  32851. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32852. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32853. + * GNU General Public License for more details.
  32854. + *
  32855. + * You should have received a copy of the GNU General Public License
  32856. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32857. + */
  32858. +
  32859. +/*
  32860. + * handling xattr functions
  32861. + */
  32862. +
  32863. +#include <linux/xattr.h>
  32864. +#include "aufs.h"
  32865. +
  32866. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  32867. +{
  32868. + if (!ignore_flags)
  32869. + goto out;
  32870. + switch (err) {
  32871. + case -ENOMEM:
  32872. + case -EDQUOT:
  32873. + goto out;
  32874. + }
  32875. +
  32876. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  32877. + err = 0;
  32878. + goto out;
  32879. + }
  32880. +
  32881. +#define cmp(brattr, prefix) do { \
  32882. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  32883. + XATTR_##prefix##_PREFIX_LEN)) { \
  32884. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  32885. + err = 0; \
  32886. + goto out; \
  32887. + } \
  32888. + } while (0)
  32889. +
  32890. + cmp(SEC, SECURITY);
  32891. + cmp(SYS, SYSTEM);
  32892. + cmp(TR, TRUSTED);
  32893. + cmp(USR, USER);
  32894. +#undef cmp
  32895. +
  32896. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  32897. + err = 0;
  32898. +
  32899. +out:
  32900. + return err;
  32901. +}
  32902. +
  32903. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  32904. +
  32905. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  32906. + char *name, char **buf, unsigned int ignore_flags,
  32907. + unsigned int verbose)
  32908. +{
  32909. + int err;
  32910. + ssize_t ssz;
  32911. + struct inode *h_idst;
  32912. +
  32913. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  32914. + err = ssz;
  32915. + if (unlikely(err <= 0)) {
  32916. + if (err == -ENODATA
  32917. + || (err == -EOPNOTSUPP
  32918. + && ((ignore_flags & au_xattr_out_of_list)
  32919. + || (au_test_nfs_noacl(d_inode(h_src))
  32920. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  32921. + || !strcmp(name,
  32922. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  32923. + ))
  32924. + err = 0;
  32925. + if (err && (verbose || au_debug_test()))
  32926. + pr_err("%s, err %d\n", name, err);
  32927. + goto out;
  32928. + }
  32929. +
  32930. + /* unlock it temporary */
  32931. + h_idst = d_inode(h_dst);
  32932. + mutex_unlock(&h_idst->i_mutex);
  32933. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  32934. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  32935. + if (unlikely(err)) {
  32936. + if (verbose || au_debug_test())
  32937. + pr_err("%s, err %d\n", name, err);
  32938. + err = au_xattr_ignore(err, name, ignore_flags);
  32939. + }
  32940. +
  32941. +out:
  32942. + return err;
  32943. +}
  32944. +
  32945. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  32946. + unsigned int verbose)
  32947. +{
  32948. + int err, unlocked, acl_access, acl_default;
  32949. + ssize_t ssz;
  32950. + struct inode *h_isrc, *h_idst;
  32951. + char *value, *p, *o, *e;
  32952. +
  32953. + /* try stopping to update the source inode while we are referencing */
  32954. + /* there should not be the parent-child relationship between them */
  32955. + h_isrc = d_inode(h_src);
  32956. + h_idst = d_inode(h_dst);
  32957. + mutex_unlock(&h_idst->i_mutex);
  32958. + mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
  32959. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  32960. + unlocked = 0;
  32961. +
  32962. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  32963. + ssz = vfs_listxattr(h_src, NULL, 0);
  32964. + err = ssz;
  32965. + if (unlikely(err < 0)) {
  32966. + AuTraceErr(err);
  32967. + if (err == -ENODATA
  32968. + || err == -EOPNOTSUPP)
  32969. + err = 0; /* ignore */
  32970. + goto out;
  32971. + }
  32972. +
  32973. + err = 0;
  32974. + p = NULL;
  32975. + o = NULL;
  32976. + if (ssz) {
  32977. + err = -ENOMEM;
  32978. + p = kmalloc(ssz, GFP_NOFS);
  32979. + o = p;
  32980. + if (unlikely(!p))
  32981. + goto out;
  32982. + err = vfs_listxattr(h_src, p, ssz);
  32983. + }
  32984. + mutex_unlock(&h_isrc->i_mutex);
  32985. + unlocked = 1;
  32986. + AuDbg("err %d, ssz %zd\n", err, ssz);
  32987. + if (unlikely(err < 0))
  32988. + goto out_free;
  32989. +
  32990. + err = 0;
  32991. + e = p + ssz;
  32992. + value = NULL;
  32993. + acl_access = 0;
  32994. + acl_default = 0;
  32995. + while (!err && p < e) {
  32996. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  32997. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  32998. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  32999. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  33000. + - 1);
  33001. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  33002. + verbose);
  33003. + p += strlen(p) + 1;
  33004. + }
  33005. + AuTraceErr(err);
  33006. + ignore_flags |= au_xattr_out_of_list;
  33007. + if (!err && !acl_access) {
  33008. + err = au_do_cpup_xattr(h_dst, h_src,
  33009. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  33010. + ignore_flags, verbose);
  33011. + AuTraceErr(err);
  33012. + }
  33013. + if (!err && !acl_default) {
  33014. + err = au_do_cpup_xattr(h_dst, h_src,
  33015. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  33016. + ignore_flags, verbose);
  33017. + AuTraceErr(err);
  33018. + }
  33019. +
  33020. + if (value)
  33021. + au_delayed_kfree(value);
  33022. +
  33023. +out_free:
  33024. + if (o)
  33025. + au_delayed_kfree(o);
  33026. +out:
  33027. + if (!unlocked)
  33028. + mutex_unlock(&h_isrc->i_mutex);
  33029. + AuTraceErr(err);
  33030. + return err;
  33031. +}
  33032. +
  33033. +/* ---------------------------------------------------------------------- */
  33034. +
  33035. +enum {
  33036. + AU_XATTR_LIST,
  33037. + AU_XATTR_GET
  33038. +};
  33039. +
  33040. +struct au_lgxattr {
  33041. + int type;
  33042. + union {
  33043. + struct {
  33044. + char *list;
  33045. + size_t size;
  33046. + } list;
  33047. + struct {
  33048. + const char *name;
  33049. + void *value;
  33050. + size_t size;
  33051. + } get;
  33052. + } u;
  33053. +};
  33054. +
  33055. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  33056. +{
  33057. + ssize_t err;
  33058. + struct path h_path;
  33059. + struct super_block *sb;
  33060. +
  33061. + sb = dentry->d_sb;
  33062. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  33063. + if (unlikely(err))
  33064. + goto out;
  33065. + err = au_h_path_getattr(dentry, /*force*/1, &h_path);
  33066. + if (unlikely(err))
  33067. + goto out_si;
  33068. + if (unlikely(!h_path.dentry))
  33069. + /* illegally overlapped or something */
  33070. + goto out_di; /* pretending success */
  33071. +
  33072. + /* always topmost entry only */
  33073. + switch (arg->type) {
  33074. + case AU_XATTR_LIST:
  33075. + err = vfs_listxattr(h_path.dentry,
  33076. + arg->u.list.list, arg->u.list.size);
  33077. + break;
  33078. + case AU_XATTR_GET:
  33079. + err = vfs_getxattr(h_path.dentry,
  33080. + arg->u.get.name, arg->u.get.value,
  33081. + arg->u.get.size);
  33082. + break;
  33083. + }
  33084. +
  33085. +out_di:
  33086. + di_read_unlock(dentry, AuLock_IR);
  33087. +out_si:
  33088. + si_read_unlock(sb);
  33089. +out:
  33090. + AuTraceErr(err);
  33091. + return err;
  33092. +}
  33093. +
  33094. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  33095. +{
  33096. + struct au_lgxattr arg = {
  33097. + .type = AU_XATTR_LIST,
  33098. + .u.list = {
  33099. + .list = list,
  33100. + .size = size
  33101. + },
  33102. + };
  33103. +
  33104. + return au_lgxattr(dentry, &arg);
  33105. +}
  33106. +
  33107. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  33108. + size_t size)
  33109. +{
  33110. + struct au_lgxattr arg = {
  33111. + .type = AU_XATTR_GET,
  33112. + .u.get = {
  33113. + .name = name,
  33114. + .value = value,
  33115. + .size = size
  33116. + },
  33117. + };
  33118. +
  33119. + return au_lgxattr(dentry, &arg);
  33120. +}
  33121. +
  33122. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  33123. + size_t size, int flags)
  33124. +{
  33125. + struct au_srxattr arg = {
  33126. + .type = AU_XATTR_SET,
  33127. + .u.set = {
  33128. + .name = name,
  33129. + .value = value,
  33130. + .size = size,
  33131. + .flags = flags
  33132. + },
  33133. + };
  33134. +
  33135. + return au_srxattr(dentry, &arg);
  33136. +}
  33137. +
  33138. +int aufs_removexattr(struct dentry *dentry, const char *name)
  33139. +{
  33140. + struct au_srxattr arg = {
  33141. + .type = AU_XATTR_REMOVE,
  33142. + .u.remove = {
  33143. + .name = name
  33144. + },
  33145. + };
  33146. +
  33147. + return au_srxattr(dentry, &arg);
  33148. +}
  33149. +
  33150. +/* ---------------------------------------------------------------------- */
  33151. +
  33152. +#if 0
  33153. +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  33154. + const char *name, size_t name_len, int type)
  33155. +{
  33156. + return aufs_listxattr(dentry, list, list_size);
  33157. +}
  33158. +
  33159. +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
  33160. + size_t size, int type)
  33161. +{
  33162. + return aufs_getxattr(dentry, name, buffer, size);
  33163. +}
  33164. +
  33165. +static int au_xattr_set(struct dentry *dentry, const char *name,
  33166. + const void *value, size_t size, int flags, int type)
  33167. +{
  33168. + return aufs_setxattr(dentry, name, value, size, flags);
  33169. +}
  33170. +
  33171. +static const struct xattr_handler au_xattr_handler = {
  33172. + /* no prefix, no flags */
  33173. + .list = au_xattr_list,
  33174. + .get = au_xattr_get,
  33175. + .set = au_xattr_set
  33176. + /* why no remove? */
  33177. +};
  33178. +
  33179. +static const struct xattr_handler *au_xattr_handlers[] = {
  33180. + &au_xattr_handler
  33181. +};
  33182. +
  33183. +void au_xattr_init(struct super_block *sb)
  33184. +{
  33185. + /* sb->s_xattr = au_xattr_handlers; */
  33186. +}
  33187. +#endif
  33188. --- /dev/null
  33189. +++ linux-4.4/fs/aufs/xino.c 2016-10-23 11:20:57.642159951 +0300
  33190. @@ -0,0 +1,1318 @@
  33191. +/*
  33192. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  33193. + *
  33194. + * This program, aufs is free software; you can redistribute it and/or modify
  33195. + * it under the terms of the GNU General Public License as published by
  33196. + * the Free Software Foundation; either version 2 of the License, or
  33197. + * (at your option) any later version.
  33198. + *
  33199. + * This program is distributed in the hope that it will be useful,
  33200. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33201. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33202. + * GNU General Public License for more details.
  33203. + *
  33204. + * You should have received a copy of the GNU General Public License
  33205. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33206. + */
  33207. +
  33208. +/*
  33209. + * external inode number translation table and bitmap
  33210. + */
  33211. +
  33212. +#include <linux/seq_file.h>
  33213. +#include <linux/statfs.h>
  33214. +#include "aufs.h"
  33215. +
  33216. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  33217. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
  33218. + loff_t *pos)
  33219. +{
  33220. + ssize_t err;
  33221. + mm_segment_t oldfs;
  33222. + union {
  33223. + void *k;
  33224. + char __user *u;
  33225. + } buf;
  33226. +
  33227. + buf.k = kbuf;
  33228. + oldfs = get_fs();
  33229. + set_fs(KERNEL_DS);
  33230. + do {
  33231. + /* todo: signal_pending? */
  33232. + err = func(file, buf.u, size, pos);
  33233. + } while (err == -EAGAIN || err == -EINTR);
  33234. + set_fs(oldfs);
  33235. +
  33236. +#if 0 /* reserved for future use */
  33237. + if (err > 0)
  33238. + fsnotify_access(file->f_path.dentry);
  33239. +#endif
  33240. +
  33241. + return err;
  33242. +}
  33243. +
  33244. +/* ---------------------------------------------------------------------- */
  33245. +
  33246. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  33247. + size_t size, loff_t *pos);
  33248. +
  33249. +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
  33250. + size_t size, loff_t *pos)
  33251. +{
  33252. + ssize_t err;
  33253. + mm_segment_t oldfs;
  33254. + union {
  33255. + void *k;
  33256. + const char __user *u;
  33257. + } buf;
  33258. + int i;
  33259. + const int prevent_endless = 10;
  33260. +
  33261. + i = 0;
  33262. + buf.k = kbuf;
  33263. + oldfs = get_fs();
  33264. + set_fs(KERNEL_DS);
  33265. + do {
  33266. + err = func(file, buf.u, size, pos);
  33267. + if (err == -EINTR
  33268. + && !au_wkq_test()
  33269. + && fatal_signal_pending(current)) {
  33270. + set_fs(oldfs);
  33271. + err = xino_fwrite_wkq(func, file, kbuf, size, pos);
  33272. + BUG_ON(err == -EINTR);
  33273. + oldfs = get_fs();
  33274. + set_fs(KERNEL_DS);
  33275. + }
  33276. + } while (i++ < prevent_endless
  33277. + && (err == -EAGAIN || err == -EINTR));
  33278. + set_fs(oldfs);
  33279. +
  33280. +#if 0 /* reserved for future use */
  33281. + if (err > 0)
  33282. + fsnotify_modify(file->f_path.dentry);
  33283. +#endif
  33284. +
  33285. + return err;
  33286. +}
  33287. +
  33288. +struct do_xino_fwrite_args {
  33289. + ssize_t *errp;
  33290. + vfs_writef_t func;
  33291. + struct file *file;
  33292. + void *buf;
  33293. + size_t size;
  33294. + loff_t *pos;
  33295. +};
  33296. +
  33297. +static void call_do_xino_fwrite(void *args)
  33298. +{
  33299. + struct do_xino_fwrite_args *a = args;
  33300. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  33301. +}
  33302. +
  33303. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  33304. + size_t size, loff_t *pos)
  33305. +{
  33306. + ssize_t err;
  33307. + int wkq_err;
  33308. + struct do_xino_fwrite_args args = {
  33309. + .errp = &err,
  33310. + .func = func,
  33311. + .file = file,
  33312. + .buf = buf,
  33313. + .size = size,
  33314. + .pos = pos
  33315. + };
  33316. +
  33317. + /*
  33318. + * it breaks RLIMIT_FSIZE and normal user's limit,
  33319. + * users should care about quota and real 'filesystem full.'
  33320. + */
  33321. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  33322. + if (unlikely(wkq_err))
  33323. + err = wkq_err;
  33324. +
  33325. + return err;
  33326. +}
  33327. +
  33328. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  33329. + size_t size, loff_t *pos)
  33330. +{
  33331. + ssize_t err;
  33332. +
  33333. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  33334. + lockdep_off();
  33335. + err = do_xino_fwrite(func, file, buf, size, pos);
  33336. + lockdep_on();
  33337. + } else
  33338. + err = xino_fwrite_wkq(func, file, buf, size, pos);
  33339. +
  33340. + return err;
  33341. +}
  33342. +
  33343. +/* ---------------------------------------------------------------------- */
  33344. +
  33345. +/*
  33346. + * create a new xinofile at the same place/path as @base_file.
  33347. + */
  33348. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
  33349. +{
  33350. + struct file *file;
  33351. + struct dentry *base, *parent;
  33352. + struct inode *dir, *delegated;
  33353. + struct qstr *name;
  33354. + struct path path;
  33355. + int err;
  33356. +
  33357. + base = base_file->f_path.dentry;
  33358. + parent = base->d_parent; /* dir inode is locked */
  33359. + dir = d_inode(parent);
  33360. + IMustLock(dir);
  33361. +
  33362. + file = ERR_PTR(-EINVAL);
  33363. + name = &base->d_name;
  33364. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  33365. + if (IS_ERR(path.dentry)) {
  33366. + file = (void *)path.dentry;
  33367. + pr_err("%pd lookup err %ld\n",
  33368. + base, PTR_ERR(path.dentry));
  33369. + goto out;
  33370. + }
  33371. +
  33372. + /* no need to mnt_want_write() since we call dentry_open() later */
  33373. + err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
  33374. + if (unlikely(err)) {
  33375. + file = ERR_PTR(err);
  33376. + pr_err("%pd create err %d\n", base, err);
  33377. + goto out_dput;
  33378. + }
  33379. +
  33380. + path.mnt = base_file->f_path.mnt;
  33381. + file = vfsub_dentry_open(&path,
  33382. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  33383. + /* | __FMODE_NONOTIFY */);
  33384. + if (IS_ERR(file)) {
  33385. + pr_err("%pd open err %ld\n", base, PTR_ERR(file));
  33386. + goto out_dput;
  33387. + }
  33388. +
  33389. + delegated = NULL;
  33390. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  33391. + if (unlikely(err == -EWOULDBLOCK)) {
  33392. + pr_warn("cannot retry for NFSv4 delegation"
  33393. + " for an internal unlink\n");
  33394. + iput(delegated);
  33395. + }
  33396. + if (unlikely(err)) {
  33397. + pr_err("%pd unlink err %d\n", base, err);
  33398. + goto out_fput;
  33399. + }
  33400. +
  33401. + if (copy_src) {
  33402. + /* no one can touch copy_src xino */
  33403. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  33404. + if (unlikely(err)) {
  33405. + pr_err("%pd copy err %d\n", base, err);
  33406. + goto out_fput;
  33407. + }
  33408. + }
  33409. + goto out_dput; /* success */
  33410. +
  33411. +out_fput:
  33412. + fput(file);
  33413. + file = ERR_PTR(err);
  33414. +out_dput:
  33415. + dput(path.dentry);
  33416. +out:
  33417. + return file;
  33418. +}
  33419. +
  33420. +struct au_xino_lock_dir {
  33421. + struct au_hinode *hdir;
  33422. + struct dentry *parent;
  33423. + struct mutex *mtx;
  33424. +};
  33425. +
  33426. +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
  33427. + struct au_xino_lock_dir *ldir)
  33428. +{
  33429. + aufs_bindex_t brid, bindex;
  33430. +
  33431. + ldir->hdir = NULL;
  33432. + bindex = -1;
  33433. + brid = au_xino_brid(sb);
  33434. + if (brid >= 0)
  33435. + bindex = au_br_index(sb, brid);
  33436. + if (bindex >= 0) {
  33437. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  33438. + au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  33439. + } else {
  33440. + ldir->parent = dget_parent(xino->f_path.dentry);
  33441. + ldir->mtx = &d_inode(ldir->parent)->i_mutex;
  33442. + mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
  33443. + }
  33444. +}
  33445. +
  33446. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  33447. +{
  33448. + if (ldir->hdir)
  33449. + au_hn_imtx_unlock(ldir->hdir);
  33450. + else {
  33451. + mutex_unlock(ldir->mtx);
  33452. + dput(ldir->parent);
  33453. + }
  33454. +}
  33455. +
  33456. +/* ---------------------------------------------------------------------- */
  33457. +
  33458. +/* trucate xino files asynchronously */
  33459. +
  33460. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
  33461. +{
  33462. + int err;
  33463. + unsigned long jiffy;
  33464. + blkcnt_t blocks;
  33465. + aufs_bindex_t bi, bbot;
  33466. + struct kstatfs *st;
  33467. + struct au_branch *br;
  33468. + struct file *new_xino, *file;
  33469. + struct super_block *h_sb;
  33470. + struct au_xino_lock_dir ldir;
  33471. +
  33472. + err = -ENOMEM;
  33473. + st = kmalloc(sizeof(*st), GFP_NOFS);
  33474. + if (unlikely(!st))
  33475. + goto out;
  33476. +
  33477. + err = -EINVAL;
  33478. + bbot = au_sbbot(sb);
  33479. + if (unlikely(bindex < 0 || bbot < bindex))
  33480. + goto out_st;
  33481. + br = au_sbr(sb, bindex);
  33482. + file = br->br_xino.xi_file;
  33483. + if (!file)
  33484. + goto out_st;
  33485. +
  33486. + err = vfs_statfs(&file->f_path, st);
  33487. + if (unlikely(err))
  33488. + AuErr1("statfs err %d, ignored\n", err);
  33489. + jiffy = jiffies;
  33490. + blocks = file_inode(file)->i_blocks;
  33491. + pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  33492. + bindex, (u64)blocks, st->f_bfree, st->f_blocks);
  33493. +
  33494. + au_xino_lock_dir(sb, file, &ldir);
  33495. + /* mnt_want_write() is unnecessary here */
  33496. + new_xino = au_xino_create2(file, file);
  33497. + au_xino_unlock_dir(&ldir);
  33498. + err = PTR_ERR(new_xino);
  33499. + if (IS_ERR(new_xino)) {
  33500. + pr_err("err %d, ignored\n", err);
  33501. + goto out_st;
  33502. + }
  33503. + err = 0;
  33504. + fput(file);
  33505. + br->br_xino.xi_file = new_xino;
  33506. +
  33507. + h_sb = au_br_sb(br);
  33508. + for (bi = 0; bi <= bbot; bi++) {
  33509. + if (unlikely(bi == bindex))
  33510. + continue;
  33511. + br = au_sbr(sb, bi);
  33512. + if (au_br_sb(br) != h_sb)
  33513. + continue;
  33514. +
  33515. + fput(br->br_xino.xi_file);
  33516. + br->br_xino.xi_file = new_xino;
  33517. + get_file(new_xino);
  33518. + }
  33519. +
  33520. + err = vfs_statfs(&new_xino->f_path, st);
  33521. + if (!err) {
  33522. + pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  33523. + bindex, (u64)file_inode(new_xino)->i_blocks,
  33524. + st->f_bfree, st->f_blocks);
  33525. + if (file_inode(new_xino)->i_blocks < blocks)
  33526. + au_sbi(sb)->si_xino_jiffy = jiffy;
  33527. + } else
  33528. + AuErr1("statfs err %d, ignored\n", err);
  33529. +
  33530. +out_st:
  33531. + au_delayed_kfree(st);
  33532. +out:
  33533. + return err;
  33534. +}
  33535. +
  33536. +struct xino_do_trunc_args {
  33537. + struct super_block *sb;
  33538. + struct au_branch *br;
  33539. +};
  33540. +
  33541. +static void xino_do_trunc(void *_args)
  33542. +{
  33543. + struct xino_do_trunc_args *args = _args;
  33544. + struct super_block *sb;
  33545. + struct au_branch *br;
  33546. + struct inode *dir;
  33547. + int err;
  33548. + aufs_bindex_t bindex;
  33549. +
  33550. + err = 0;
  33551. + sb = args->sb;
  33552. + dir = d_inode(sb->s_root);
  33553. + br = args->br;
  33554. +
  33555. + si_noflush_write_lock(sb);
  33556. + ii_read_lock_parent(dir);
  33557. + bindex = au_br_index(sb, br->br_id);
  33558. + err = au_xino_trunc(sb, bindex);
  33559. + ii_read_unlock(dir);
  33560. + if (unlikely(err))
  33561. + pr_warn("err b%d, (%d)\n", bindex, err);
  33562. + atomic_dec(&br->br_xino_running);
  33563. + au_br_put(br);
  33564. + si_write_unlock(sb);
  33565. + au_nwt_done(&au_sbi(sb)->si_nowait);
  33566. + au_delayed_kfree(args);
  33567. +}
  33568. +
  33569. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  33570. +{
  33571. + int err;
  33572. + struct kstatfs st;
  33573. + struct au_sbinfo *sbinfo;
  33574. +
  33575. + /* todo: si_xino_expire and the ratio should be customizable */
  33576. + sbinfo = au_sbi(sb);
  33577. + if (time_before(jiffies,
  33578. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  33579. + return 0;
  33580. +
  33581. + /* truncation border */
  33582. + err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
  33583. + if (unlikely(err)) {
  33584. + AuErr1("statfs err %d, ignored\n", err);
  33585. + return 0;
  33586. + }
  33587. + if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
  33588. + return 0;
  33589. +
  33590. + return 1;
  33591. +}
  33592. +
  33593. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  33594. +{
  33595. + struct xino_do_trunc_args *args;
  33596. + int wkq_err;
  33597. +
  33598. + if (!xino_trunc_test(sb, br))
  33599. + return;
  33600. +
  33601. + if (atomic_inc_return(&br->br_xino_running) > 1)
  33602. + goto out;
  33603. +
  33604. + /* lock and kfree() will be called in trunc_xino() */
  33605. + args = kmalloc(sizeof(*args), GFP_NOFS);
  33606. + if (unlikely(!args)) {
  33607. + AuErr1("no memory\n");
  33608. + goto out;
  33609. + }
  33610. +
  33611. + au_br_get(br);
  33612. + args->sb = sb;
  33613. + args->br = br;
  33614. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  33615. + if (!wkq_err)
  33616. + return; /* success */
  33617. +
  33618. + pr_err("wkq %d\n", wkq_err);
  33619. + au_br_put(br);
  33620. + au_delayed_kfree(args);
  33621. +
  33622. +out:
  33623. + atomic_dec(&br->br_xino_running);
  33624. +}
  33625. +
  33626. +/* ---------------------------------------------------------------------- */
  33627. +
  33628. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  33629. + ino_t h_ino, ino_t ino)
  33630. +{
  33631. + loff_t pos;
  33632. + ssize_t sz;
  33633. +
  33634. + pos = h_ino;
  33635. + if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
  33636. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  33637. + return -EFBIG;
  33638. + }
  33639. + pos *= sizeof(ino);
  33640. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
  33641. + if (sz == sizeof(ino))
  33642. + return 0; /* success */
  33643. +
  33644. + AuIOErr("write failed (%zd)\n", sz);
  33645. + return -EIO;
  33646. +}
  33647. +
  33648. +/*
  33649. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  33650. + * at the position of @h_ino.
  33651. + * even if @ino is zero, it is written to the xinofile and means no entry.
  33652. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  33653. + * try truncating it.
  33654. + */
  33655. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  33656. + ino_t ino)
  33657. +{
  33658. + int err;
  33659. + unsigned int mnt_flags;
  33660. + struct au_branch *br;
  33661. +
  33662. + BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
  33663. + || ((loff_t)-1) > 0);
  33664. + SiMustAnyLock(sb);
  33665. +
  33666. + mnt_flags = au_mntflags(sb);
  33667. + if (!au_opt_test(mnt_flags, XINO))
  33668. + return 0;
  33669. +
  33670. + br = au_sbr(sb, bindex);
  33671. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  33672. + h_ino, ino);
  33673. + if (!err) {
  33674. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  33675. + && au_test_fs_trunc_xino(au_br_sb(br)))
  33676. + xino_try_trunc(sb, br);
  33677. + return 0; /* success */
  33678. + }
  33679. +
  33680. + AuIOErr("write failed (%d)\n", err);
  33681. + return -EIO;
  33682. +}
  33683. +
  33684. +/* ---------------------------------------------------------------------- */
  33685. +
  33686. +/* aufs inode number bitmap */
  33687. +
  33688. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  33689. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  33690. +{
  33691. + ino_t ino;
  33692. +
  33693. + AuDebugOn(bit < 0 || page_bits <= bit);
  33694. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  33695. + return ino;
  33696. +}
  33697. +
  33698. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  33699. +{
  33700. + AuDebugOn(ino < AUFS_FIRST_INO);
  33701. + ino -= AUFS_FIRST_INO;
  33702. + *pindex = ino / page_bits;
  33703. + *bit = ino % page_bits;
  33704. +}
  33705. +
  33706. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  33707. +{
  33708. + int err;
  33709. + loff_t pos;
  33710. + ssize_t sz;
  33711. + struct au_sbinfo *sbinfo;
  33712. + struct file *xib;
  33713. + unsigned long *p;
  33714. +
  33715. + sbinfo = au_sbi(sb);
  33716. + MtxMustLock(&sbinfo->si_xib_mtx);
  33717. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  33718. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  33719. +
  33720. + if (pindex == sbinfo->si_xib_last_pindex)
  33721. + return 0;
  33722. +
  33723. + xib = sbinfo->si_xib;
  33724. + p = sbinfo->si_xib_buf;
  33725. + pos = sbinfo->si_xib_last_pindex;
  33726. + pos *= PAGE_SIZE;
  33727. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  33728. + if (unlikely(sz != PAGE_SIZE))
  33729. + goto out;
  33730. +
  33731. + pos = pindex;
  33732. + pos *= PAGE_SIZE;
  33733. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  33734. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  33735. + else {
  33736. + memset(p, 0, PAGE_SIZE);
  33737. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  33738. + }
  33739. + if (sz == PAGE_SIZE) {
  33740. + sbinfo->si_xib_last_pindex = pindex;
  33741. + return 0; /* success */
  33742. + }
  33743. +
  33744. +out:
  33745. + AuIOErr1("write failed (%zd)\n", sz);
  33746. + err = sz;
  33747. + if (sz >= 0)
  33748. + err = -EIO;
  33749. + return err;
  33750. +}
  33751. +
  33752. +/* ---------------------------------------------------------------------- */
  33753. +
  33754. +static void au_xib_clear_bit(struct inode *inode)
  33755. +{
  33756. + int err, bit;
  33757. + unsigned long pindex;
  33758. + struct super_block *sb;
  33759. + struct au_sbinfo *sbinfo;
  33760. +
  33761. + AuDebugOn(inode->i_nlink);
  33762. +
  33763. + sb = inode->i_sb;
  33764. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  33765. + AuDebugOn(page_bits <= bit);
  33766. + sbinfo = au_sbi(sb);
  33767. + mutex_lock(&sbinfo->si_xib_mtx);
  33768. + err = xib_pindex(sb, pindex);
  33769. + if (!err) {
  33770. + clear_bit(bit, sbinfo->si_xib_buf);
  33771. + sbinfo->si_xib_next_bit = bit;
  33772. + }
  33773. + mutex_unlock(&sbinfo->si_xib_mtx);
  33774. +}
  33775. +
  33776. +/* for s_op->delete_inode() */
  33777. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  33778. +{
  33779. + int err;
  33780. + unsigned int mnt_flags;
  33781. + aufs_bindex_t bindex, bbot, bi;
  33782. + unsigned char try_trunc;
  33783. + struct au_iinfo *iinfo;
  33784. + struct super_block *sb;
  33785. + struct au_hinode *hi;
  33786. + struct inode *h_inode;
  33787. + struct au_branch *br;
  33788. + vfs_writef_t xwrite;
  33789. +
  33790. + AuDebugOn(au_is_bad_inode(inode));
  33791. +
  33792. + sb = inode->i_sb;
  33793. + mnt_flags = au_mntflags(sb);
  33794. + if (!au_opt_test(mnt_flags, XINO)
  33795. + || inode->i_ino == AUFS_ROOT_INO)
  33796. + return;
  33797. +
  33798. + if (unlinked) {
  33799. + au_xigen_inc(inode);
  33800. + au_xib_clear_bit(inode);
  33801. + }
  33802. +
  33803. + iinfo = au_ii(inode);
  33804. + bindex = iinfo->ii_btop;
  33805. + if (bindex < 0)
  33806. + return;
  33807. +
  33808. + xwrite = au_sbi(sb)->si_xwrite;
  33809. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  33810. + hi = au_hinode(iinfo, bindex);
  33811. + bbot = iinfo->ii_bbot;
  33812. + for (; bindex <= bbot; bindex++, hi++) {
  33813. + h_inode = hi->hi_inode;
  33814. + if (!h_inode
  33815. + || (!unlinked && h_inode->i_nlink))
  33816. + continue;
  33817. +
  33818. + /* inode may not be revalidated */
  33819. + bi = au_br_index(sb, hi->hi_id);
  33820. + if (bi < 0)
  33821. + continue;
  33822. +
  33823. + br = au_sbr(sb, bi);
  33824. + err = au_xino_do_write(xwrite, br->br_xino.xi_file,
  33825. + h_inode->i_ino, /*ino*/0);
  33826. + if (!err && try_trunc
  33827. + && au_test_fs_trunc_xino(au_br_sb(br)))
  33828. + xino_try_trunc(sb, br);
  33829. + }
  33830. +}
  33831. +
  33832. +/* get an unused inode number from bitmap */
  33833. +ino_t au_xino_new_ino(struct super_block *sb)
  33834. +{
  33835. + ino_t ino;
  33836. + unsigned long *p, pindex, ul, pend;
  33837. + struct au_sbinfo *sbinfo;
  33838. + struct file *file;
  33839. + int free_bit, err;
  33840. +
  33841. + if (!au_opt_test(au_mntflags(sb), XINO))
  33842. + return iunique(sb, AUFS_FIRST_INO);
  33843. +
  33844. + sbinfo = au_sbi(sb);
  33845. + mutex_lock(&sbinfo->si_xib_mtx);
  33846. + p = sbinfo->si_xib_buf;
  33847. + free_bit = sbinfo->si_xib_next_bit;
  33848. + if (free_bit < page_bits && !test_bit(free_bit, p))
  33849. + goto out; /* success */
  33850. + free_bit = find_first_zero_bit(p, page_bits);
  33851. + if (free_bit < page_bits)
  33852. + goto out; /* success */
  33853. +
  33854. + pindex = sbinfo->si_xib_last_pindex;
  33855. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  33856. + err = xib_pindex(sb, ul);
  33857. + if (unlikely(err))
  33858. + goto out_err;
  33859. + free_bit = find_first_zero_bit(p, page_bits);
  33860. + if (free_bit < page_bits)
  33861. + goto out; /* success */
  33862. + }
  33863. +
  33864. + file = sbinfo->si_xib;
  33865. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  33866. + for (ul = pindex + 1; ul <= pend; ul++) {
  33867. + err = xib_pindex(sb, ul);
  33868. + if (unlikely(err))
  33869. + goto out_err;
  33870. + free_bit = find_first_zero_bit(p, page_bits);
  33871. + if (free_bit < page_bits)
  33872. + goto out; /* success */
  33873. + }
  33874. + BUG();
  33875. +
  33876. +out:
  33877. + set_bit(free_bit, p);
  33878. + sbinfo->si_xib_next_bit = free_bit + 1;
  33879. + pindex = sbinfo->si_xib_last_pindex;
  33880. + mutex_unlock(&sbinfo->si_xib_mtx);
  33881. + ino = xib_calc_ino(pindex, free_bit);
  33882. + AuDbg("i%lu\n", (unsigned long)ino);
  33883. + return ino;
  33884. +out_err:
  33885. + mutex_unlock(&sbinfo->si_xib_mtx);
  33886. + AuDbg("i0\n");
  33887. + return 0;
  33888. +}
  33889. +
  33890. +/*
  33891. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  33892. + * at the position of @h_ino.
  33893. + * if @ino does not exist and @do_new is true, get new one.
  33894. + */
  33895. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  33896. + ino_t *ino)
  33897. +{
  33898. + int err;
  33899. + ssize_t sz;
  33900. + loff_t pos;
  33901. + struct file *file;
  33902. + struct au_sbinfo *sbinfo;
  33903. +
  33904. + *ino = 0;
  33905. + if (!au_opt_test(au_mntflags(sb), XINO))
  33906. + return 0; /* no xino */
  33907. +
  33908. + err = 0;
  33909. + sbinfo = au_sbi(sb);
  33910. + pos = h_ino;
  33911. + if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
  33912. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  33913. + return -EFBIG;
  33914. + }
  33915. + pos *= sizeof(*ino);
  33916. +
  33917. + file = au_sbr(sb, bindex)->br_xino.xi_file;
  33918. + if (vfsub_f_size_read(file) < pos + sizeof(*ino))
  33919. + return 0; /* no ino */
  33920. +
  33921. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
  33922. + if (sz == sizeof(*ino))
  33923. + return 0; /* success */
  33924. +
  33925. + err = sz;
  33926. + if (unlikely(sz >= 0)) {
  33927. + err = -EIO;
  33928. + AuIOErr("xino read error (%zd)\n", sz);
  33929. + }
  33930. +
  33931. + return err;
  33932. +}
  33933. +
  33934. +/* ---------------------------------------------------------------------- */
  33935. +
  33936. +/* create and set a new xino file */
  33937. +
  33938. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
  33939. +{
  33940. + struct file *file;
  33941. + struct dentry *h_parent, *d;
  33942. + struct inode *h_dir, *inode;
  33943. + int err;
  33944. +
  33945. + /*
  33946. + * at mount-time, and the xino file is the default path,
  33947. + * hnotify is disabled so we have no notify events to ignore.
  33948. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  33949. + */
  33950. + file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  33951. + /* | __FMODE_NONOTIFY */,
  33952. + S_IRUGO | S_IWUGO);
  33953. + if (IS_ERR(file)) {
  33954. + if (!silent)
  33955. + pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
  33956. + return file;
  33957. + }
  33958. +
  33959. + /* keep file count */
  33960. + err = 0;
  33961. + inode = file_inode(file);
  33962. + h_parent = dget_parent(file->f_path.dentry);
  33963. + h_dir = d_inode(h_parent);
  33964. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  33965. + /* mnt_want_write() is unnecessary here */
  33966. + /* no delegation since it is just created */
  33967. + if (inode->i_nlink)
  33968. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  33969. + /*force*/0);
  33970. + mutex_unlock(&h_dir->i_mutex);
  33971. + dput(h_parent);
  33972. + if (unlikely(err)) {
  33973. + if (!silent)
  33974. + pr_err("unlink %s(%d)\n", fname, err);
  33975. + goto out;
  33976. + }
  33977. +
  33978. + err = -EINVAL;
  33979. + d = file->f_path.dentry;
  33980. + if (unlikely(sb == d->d_sb)) {
  33981. + if (!silent)
  33982. + pr_err("%s must be outside\n", fname);
  33983. + goto out;
  33984. + }
  33985. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  33986. + if (!silent)
  33987. + pr_err("xino doesn't support %s(%s)\n",
  33988. + fname, au_sbtype(d->d_sb));
  33989. + goto out;
  33990. + }
  33991. + return file; /* success */
  33992. +
  33993. +out:
  33994. + fput(file);
  33995. + file = ERR_PTR(err);
  33996. + return file;
  33997. +}
  33998. +
  33999. +/*
  34000. + * find another branch who is on the same filesystem of the specified
  34001. + * branch{@btgt}. search until @bbot.
  34002. + */
  34003. +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  34004. + aufs_bindex_t bbot)
  34005. +{
  34006. + aufs_bindex_t bindex;
  34007. + struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
  34008. +
  34009. + for (bindex = 0; bindex < btgt; bindex++)
  34010. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  34011. + return bindex;
  34012. + for (bindex++; bindex <= bbot; bindex++)
  34013. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  34014. + return bindex;
  34015. + return -1;
  34016. +}
  34017. +
  34018. +/* ---------------------------------------------------------------------- */
  34019. +
  34020. +/*
  34021. + * initialize the xinofile for the specified branch @br
  34022. + * at the place/path where @base_file indicates.
  34023. + * test whether another branch is on the same filesystem or not,
  34024. + * if @do_test is true.
  34025. + */
  34026. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  34027. + struct file *base_file, int do_test)
  34028. +{
  34029. + int err;
  34030. + ino_t ino;
  34031. + aufs_bindex_t bbot, bindex;
  34032. + struct au_branch *shared_br, *b;
  34033. + struct file *file;
  34034. + struct super_block *tgt_sb;
  34035. +
  34036. + shared_br = NULL;
  34037. + bbot = au_sbbot(sb);
  34038. + if (do_test) {
  34039. + tgt_sb = au_br_sb(br);
  34040. + for (bindex = 0; bindex <= bbot; bindex++) {
  34041. + b = au_sbr(sb, bindex);
  34042. + if (tgt_sb == au_br_sb(b)) {
  34043. + shared_br = b;
  34044. + break;
  34045. + }
  34046. + }
  34047. + }
  34048. +
  34049. + if (!shared_br || !shared_br->br_xino.xi_file) {
  34050. + struct au_xino_lock_dir ldir;
  34051. +
  34052. + au_xino_lock_dir(sb, base_file, &ldir);
  34053. + /* mnt_want_write() is unnecessary here */
  34054. + file = au_xino_create2(base_file, NULL);
  34055. + au_xino_unlock_dir(&ldir);
  34056. + err = PTR_ERR(file);
  34057. + if (IS_ERR(file))
  34058. + goto out;
  34059. + br->br_xino.xi_file = file;
  34060. + } else {
  34061. + br->br_xino.xi_file = shared_br->br_xino.xi_file;
  34062. + get_file(br->br_xino.xi_file);
  34063. + }
  34064. +
  34065. + ino = AUFS_ROOT_INO;
  34066. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  34067. + h_ino, ino);
  34068. + if (unlikely(err)) {
  34069. + fput(br->br_xino.xi_file);
  34070. + br->br_xino.xi_file = NULL;
  34071. + }
  34072. +
  34073. +out:
  34074. + return err;
  34075. +}
  34076. +
  34077. +/* ---------------------------------------------------------------------- */
  34078. +
  34079. +/* trucate a xino bitmap file */
  34080. +
  34081. +/* todo: slow */
  34082. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  34083. +{
  34084. + int err, bit;
  34085. + ssize_t sz;
  34086. + unsigned long pindex;
  34087. + loff_t pos, pend;
  34088. + struct au_sbinfo *sbinfo;
  34089. + vfs_readf_t func;
  34090. + ino_t *ino;
  34091. + unsigned long *p;
  34092. +
  34093. + err = 0;
  34094. + sbinfo = au_sbi(sb);
  34095. + MtxMustLock(&sbinfo->si_xib_mtx);
  34096. + p = sbinfo->si_xib_buf;
  34097. + func = sbinfo->si_xread;
  34098. + pend = vfsub_f_size_read(file);
  34099. + pos = 0;
  34100. + while (pos < pend) {
  34101. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  34102. + err = sz;
  34103. + if (unlikely(sz <= 0))
  34104. + goto out;
  34105. +
  34106. + err = 0;
  34107. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  34108. + if (unlikely(*ino < AUFS_FIRST_INO))
  34109. + continue;
  34110. +
  34111. + xib_calc_bit(*ino, &pindex, &bit);
  34112. + AuDebugOn(page_bits <= bit);
  34113. + err = xib_pindex(sb, pindex);
  34114. + if (!err)
  34115. + set_bit(bit, p);
  34116. + else
  34117. + goto out;
  34118. + }
  34119. + }
  34120. +
  34121. +out:
  34122. + return err;
  34123. +}
  34124. +
  34125. +static int xib_restore(struct super_block *sb)
  34126. +{
  34127. + int err;
  34128. + aufs_bindex_t bindex, bbot;
  34129. + void *page;
  34130. +
  34131. + err = -ENOMEM;
  34132. + page = (void *)__get_free_page(GFP_NOFS);
  34133. + if (unlikely(!page))
  34134. + goto out;
  34135. +
  34136. + err = 0;
  34137. + bbot = au_sbbot(sb);
  34138. + for (bindex = 0; !err && bindex <= bbot; bindex++)
  34139. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
  34140. + err = do_xib_restore
  34141. + (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
  34142. + else
  34143. + AuDbg("b%d\n", bindex);
  34144. + au_delayed_free_page((unsigned long)page);
  34145. +
  34146. +out:
  34147. + return err;
  34148. +}
  34149. +
  34150. +int au_xib_trunc(struct super_block *sb)
  34151. +{
  34152. + int err;
  34153. + ssize_t sz;
  34154. + loff_t pos;
  34155. + struct au_xino_lock_dir ldir;
  34156. + struct au_sbinfo *sbinfo;
  34157. + unsigned long *p;
  34158. + struct file *file;
  34159. +
  34160. + SiMustWriteLock(sb);
  34161. +
  34162. + err = 0;
  34163. + sbinfo = au_sbi(sb);
  34164. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  34165. + goto out;
  34166. +
  34167. + file = sbinfo->si_xib;
  34168. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  34169. + goto out;
  34170. +
  34171. + au_xino_lock_dir(sb, file, &ldir);
  34172. + /* mnt_want_write() is unnecessary here */
  34173. + file = au_xino_create2(sbinfo->si_xib, NULL);
  34174. + au_xino_unlock_dir(&ldir);
  34175. + err = PTR_ERR(file);
  34176. + if (IS_ERR(file))
  34177. + goto out;
  34178. + fput(sbinfo->si_xib);
  34179. + sbinfo->si_xib = file;
  34180. +
  34181. + p = sbinfo->si_xib_buf;
  34182. + memset(p, 0, PAGE_SIZE);
  34183. + pos = 0;
  34184. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  34185. + if (unlikely(sz != PAGE_SIZE)) {
  34186. + err = sz;
  34187. + AuIOErr("err %d\n", err);
  34188. + if (sz >= 0)
  34189. + err = -EIO;
  34190. + goto out;
  34191. + }
  34192. +
  34193. + mutex_lock(&sbinfo->si_xib_mtx);
  34194. + /* mnt_want_write() is unnecessary here */
  34195. + err = xib_restore(sb);
  34196. + mutex_unlock(&sbinfo->si_xib_mtx);
  34197. +
  34198. +out:
  34199. + return err;
  34200. +}
  34201. +
  34202. +/* ---------------------------------------------------------------------- */
  34203. +
  34204. +/*
  34205. + * xino mount option handlers
  34206. + */
  34207. +
  34208. +/* xino bitmap */
  34209. +static void xino_clear_xib(struct super_block *sb)
  34210. +{
  34211. + struct au_sbinfo *sbinfo;
  34212. +
  34213. + SiMustWriteLock(sb);
  34214. +
  34215. + sbinfo = au_sbi(sb);
  34216. + sbinfo->si_xread = NULL;
  34217. + sbinfo->si_xwrite = NULL;
  34218. + if (sbinfo->si_xib)
  34219. + fput(sbinfo->si_xib);
  34220. + sbinfo->si_xib = NULL;
  34221. + if (sbinfo->si_xib_buf)
  34222. + au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
  34223. + sbinfo->si_xib_buf = NULL;
  34224. +}
  34225. +
  34226. +static int au_xino_set_xib(struct super_block *sb, struct file *base)
  34227. +{
  34228. + int err;
  34229. + loff_t pos;
  34230. + struct au_sbinfo *sbinfo;
  34231. + struct file *file;
  34232. +
  34233. + SiMustWriteLock(sb);
  34234. +
  34235. + sbinfo = au_sbi(sb);
  34236. + file = au_xino_create2(base, sbinfo->si_xib);
  34237. + err = PTR_ERR(file);
  34238. + if (IS_ERR(file))
  34239. + goto out;
  34240. + if (sbinfo->si_xib)
  34241. + fput(sbinfo->si_xib);
  34242. + sbinfo->si_xib = file;
  34243. + sbinfo->si_xread = vfs_readf(file);
  34244. + sbinfo->si_xwrite = vfs_writef(file);
  34245. +
  34246. + err = -ENOMEM;
  34247. + if (!sbinfo->si_xib_buf)
  34248. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  34249. + if (unlikely(!sbinfo->si_xib_buf))
  34250. + goto out_unset;
  34251. +
  34252. + sbinfo->si_xib_last_pindex = 0;
  34253. + sbinfo->si_xib_next_bit = 0;
  34254. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  34255. + pos = 0;
  34256. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  34257. + PAGE_SIZE, &pos);
  34258. + if (unlikely(err != PAGE_SIZE))
  34259. + goto out_free;
  34260. + }
  34261. + err = 0;
  34262. + goto out; /* success */
  34263. +
  34264. +out_free:
  34265. + if (sbinfo->si_xib_buf)
  34266. + au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
  34267. + sbinfo->si_xib_buf = NULL;
  34268. + if (err >= 0)
  34269. + err = -EIO;
  34270. +out_unset:
  34271. + fput(sbinfo->si_xib);
  34272. + sbinfo->si_xib = NULL;
  34273. + sbinfo->si_xread = NULL;
  34274. + sbinfo->si_xwrite = NULL;
  34275. +out:
  34276. + return err;
  34277. +}
  34278. +
  34279. +/* xino for each branch */
  34280. +static void xino_clear_br(struct super_block *sb)
  34281. +{
  34282. + aufs_bindex_t bindex, bbot;
  34283. + struct au_branch *br;
  34284. +
  34285. + bbot = au_sbbot(sb);
  34286. + for (bindex = 0; bindex <= bbot; bindex++) {
  34287. + br = au_sbr(sb, bindex);
  34288. + if (!br || !br->br_xino.xi_file)
  34289. + continue;
  34290. +
  34291. + fput(br->br_xino.xi_file);
  34292. + br->br_xino.xi_file = NULL;
  34293. + }
  34294. +}
  34295. +
  34296. +static int au_xino_set_br(struct super_block *sb, struct file *base)
  34297. +{
  34298. + int err;
  34299. + ino_t ino;
  34300. + aufs_bindex_t bindex, bbot, bshared;
  34301. + struct {
  34302. + struct file *old, *new;
  34303. + } *fpair, *p;
  34304. + struct au_branch *br;
  34305. + struct inode *inode;
  34306. + vfs_writef_t writef;
  34307. +
  34308. + SiMustWriteLock(sb);
  34309. +
  34310. + err = -ENOMEM;
  34311. + bbot = au_sbbot(sb);
  34312. + fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
  34313. + if (unlikely(!fpair))
  34314. + goto out;
  34315. +
  34316. + inode = d_inode(sb->s_root);
  34317. + ino = AUFS_ROOT_INO;
  34318. + writef = au_sbi(sb)->si_xwrite;
  34319. + for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
  34320. + bshared = is_sb_shared(sb, bindex, bindex - 1);
  34321. + if (bshared >= 0) {
  34322. + /* shared xino */
  34323. + *p = fpair[bshared];
  34324. + get_file(p->new);
  34325. + }
  34326. +
  34327. + if (!p->new) {
  34328. + /* new xino */
  34329. + br = au_sbr(sb, bindex);
  34330. + p->old = br->br_xino.xi_file;
  34331. + p->new = au_xino_create2(base, br->br_xino.xi_file);
  34332. + err = PTR_ERR(p->new);
  34333. + if (IS_ERR(p->new)) {
  34334. + p->new = NULL;
  34335. + goto out_pair;
  34336. + }
  34337. + }
  34338. +
  34339. + err = au_xino_do_write(writef, p->new,
  34340. + au_h_iptr(inode, bindex)->i_ino, ino);
  34341. + if (unlikely(err))
  34342. + goto out_pair;
  34343. + }
  34344. +
  34345. + for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
  34346. + br = au_sbr(sb, bindex);
  34347. + if (br->br_xino.xi_file)
  34348. + fput(br->br_xino.xi_file);
  34349. + get_file(p->new);
  34350. + br->br_xino.xi_file = p->new;
  34351. + }
  34352. +
  34353. +out_pair:
  34354. + for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
  34355. + if (p->new)
  34356. + fput(p->new);
  34357. + else
  34358. + break;
  34359. + au_delayed_kfree(fpair);
  34360. +out:
  34361. + return err;
  34362. +}
  34363. +
  34364. +void au_xino_clr(struct super_block *sb)
  34365. +{
  34366. + struct au_sbinfo *sbinfo;
  34367. +
  34368. + au_xigen_clr(sb);
  34369. + xino_clear_xib(sb);
  34370. + xino_clear_br(sb);
  34371. + sbinfo = au_sbi(sb);
  34372. + /* lvalue, do not call au_mntflags() */
  34373. + au_opt_clr(sbinfo->si_mntflags, XINO);
  34374. +}
  34375. +
  34376. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
  34377. +{
  34378. + int err, skip;
  34379. + struct dentry *parent, *cur_parent;
  34380. + struct qstr *dname, *cur_name;
  34381. + struct file *cur_xino;
  34382. + struct inode *dir;
  34383. + struct au_sbinfo *sbinfo;
  34384. +
  34385. + SiMustWriteLock(sb);
  34386. +
  34387. + err = 0;
  34388. + sbinfo = au_sbi(sb);
  34389. + parent = dget_parent(xino->file->f_path.dentry);
  34390. + if (remount) {
  34391. + skip = 0;
  34392. + dname = &xino->file->f_path.dentry->d_name;
  34393. + cur_xino = sbinfo->si_xib;
  34394. + if (cur_xino) {
  34395. + cur_parent = dget_parent(cur_xino->f_path.dentry);
  34396. + cur_name = &cur_xino->f_path.dentry->d_name;
  34397. + skip = (cur_parent == parent
  34398. + && au_qstreq(dname, cur_name));
  34399. + dput(cur_parent);
  34400. + }
  34401. + if (skip)
  34402. + goto out;
  34403. + }
  34404. +
  34405. + au_opt_set(sbinfo->si_mntflags, XINO);
  34406. + dir = d_inode(parent);
  34407. + mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
  34408. + /* mnt_want_write() is unnecessary here */
  34409. + err = au_xino_set_xib(sb, xino->file);
  34410. + if (!err)
  34411. + err = au_xigen_set(sb, xino->file);
  34412. + if (!err)
  34413. + err = au_xino_set_br(sb, xino->file);
  34414. + mutex_unlock(&dir->i_mutex);
  34415. + if (!err)
  34416. + goto out; /* success */
  34417. +
  34418. + /* reset all */
  34419. + AuIOErr("failed creating xino(%d).\n", err);
  34420. + au_xigen_clr(sb);
  34421. + xino_clear_xib(sb);
  34422. +
  34423. +out:
  34424. + dput(parent);
  34425. + return err;
  34426. +}
  34427. +
  34428. +/* ---------------------------------------------------------------------- */
  34429. +
  34430. +/*
  34431. + * create a xinofile at the default place/path.
  34432. + */
  34433. +struct file *au_xino_def(struct super_block *sb)
  34434. +{
  34435. + struct file *file;
  34436. + char *page, *p;
  34437. + struct au_branch *br;
  34438. + struct super_block *h_sb;
  34439. + struct path path;
  34440. + aufs_bindex_t bbot, bindex, bwr;
  34441. +
  34442. + br = NULL;
  34443. + bbot = au_sbbot(sb);
  34444. + bwr = -1;
  34445. + for (bindex = 0; bindex <= bbot; bindex++) {
  34446. + br = au_sbr(sb, bindex);
  34447. + if (au_br_writable(br->br_perm)
  34448. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  34449. + bwr = bindex;
  34450. + break;
  34451. + }
  34452. + }
  34453. +
  34454. + if (bwr >= 0) {
  34455. + file = ERR_PTR(-ENOMEM);
  34456. + page = (void *)__get_free_page(GFP_NOFS);
  34457. + if (unlikely(!page))
  34458. + goto out;
  34459. + path.mnt = au_br_mnt(br);
  34460. + path.dentry = au_h_dptr(sb->s_root, bwr);
  34461. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  34462. + file = (void *)p;
  34463. + if (!IS_ERR(p)) {
  34464. + strcat(p, "/" AUFS_XINO_FNAME);
  34465. + AuDbg("%s\n", p);
  34466. + file = au_xino_create(sb, p, /*silent*/0);
  34467. + if (!IS_ERR(file))
  34468. + au_xino_brid_set(sb, br->br_id);
  34469. + }
  34470. + au_delayed_free_page((unsigned long)page);
  34471. + } else {
  34472. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  34473. + if (IS_ERR(file))
  34474. + goto out;
  34475. + h_sb = file->f_path.dentry->d_sb;
  34476. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  34477. + pr_err("xino doesn't support %s(%s)\n",
  34478. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  34479. + fput(file);
  34480. + file = ERR_PTR(-EINVAL);
  34481. + }
  34482. + if (!IS_ERR(file))
  34483. + au_xino_brid_set(sb, -1);
  34484. + }
  34485. +
  34486. +out:
  34487. + return file;
  34488. +}
  34489. +
  34490. +/* ---------------------------------------------------------------------- */
  34491. +
  34492. +int au_xino_path(struct seq_file *seq, struct file *file)
  34493. +{
  34494. + int err;
  34495. +
  34496. + err = au_seq_path(seq, &file->f_path);
  34497. + if (unlikely(err))
  34498. + goto out;
  34499. +
  34500. +#define Deleted "\\040(deleted)"
  34501. + seq->count -= sizeof(Deleted) - 1;
  34502. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  34503. + sizeof(Deleted) - 1));
  34504. +#undef Deleted
  34505. +
  34506. +out:
  34507. + return err;
  34508. +}
  34509. --- /dev/null
  34510. +++ linux-4.4/include/uapi/linux/aufs_type.h 2016-10-23 11:20:57.642159951 +0300
  34511. @@ -0,0 +1,419 @@
  34512. +/*
  34513. + * Copyright (C) 2005-2016 Junjiro R. Okajima
  34514. + *
  34515. + * This program, aufs is free software; you can redistribute it and/or modify
  34516. + * it under the terms of the GNU General Public License as published by
  34517. + * the Free Software Foundation; either version 2 of the License, or
  34518. + * (at your option) any later version.
  34519. + *
  34520. + * This program is distributed in the hope that it will be useful,
  34521. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34522. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34523. + * GNU General Public License for more details.
  34524. + *
  34525. + * You should have received a copy of the GNU General Public License
  34526. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34527. + */
  34528. +
  34529. +#ifndef __AUFS_TYPE_H__
  34530. +#define __AUFS_TYPE_H__
  34531. +
  34532. +#define AUFS_NAME "aufs"
  34533. +
  34534. +#ifdef __KERNEL__
  34535. +/*
  34536. + * define it before including all other headers.
  34537. + * sched.h may use pr_* macros before defining "current", so define the
  34538. + * no-current version first, and re-define later.
  34539. + */
  34540. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  34541. +#include <linux/sched.h>
  34542. +#undef pr_fmt
  34543. +#define pr_fmt(fmt) \
  34544. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  34545. + (int)sizeof(current->comm), current->comm, current->pid
  34546. +#else
  34547. +#include <stdint.h>
  34548. +#include <sys/types.h>
  34549. +#endif /* __KERNEL__ */
  34550. +
  34551. +#include <linux/limits.h>
  34552. +
  34553. +#define AUFS_VERSION "4.4-20160912"
  34554. +
  34555. +/* todo? move this to linux-2.6.19/include/magic.h */
  34556. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  34557. +
  34558. +/* ---------------------------------------------------------------------- */
  34559. +
  34560. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  34561. +typedef int8_t aufs_bindex_t;
  34562. +#define AUFS_BRANCH_MAX 127
  34563. +#else
  34564. +typedef int16_t aufs_bindex_t;
  34565. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  34566. +#define AUFS_BRANCH_MAX 511
  34567. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  34568. +#define AUFS_BRANCH_MAX 1023
  34569. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  34570. +#define AUFS_BRANCH_MAX 32767
  34571. +#endif
  34572. +#endif
  34573. +
  34574. +#ifdef __KERNEL__
  34575. +#ifndef AUFS_BRANCH_MAX
  34576. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  34577. +#endif
  34578. +#endif /* __KERNEL__ */
  34579. +
  34580. +/* ---------------------------------------------------------------------- */
  34581. +
  34582. +#define AUFS_FSTYPE AUFS_NAME
  34583. +
  34584. +#define AUFS_ROOT_INO 2
  34585. +#define AUFS_FIRST_INO 11
  34586. +
  34587. +#define AUFS_WH_PFX ".wh."
  34588. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  34589. +#define AUFS_WH_TMP_LEN 4
  34590. +/* a limit for rmdir/rename a dir and copyup */
  34591. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  34592. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  34593. + - 1 /* dot */\
  34594. + - AUFS_WH_TMP_LEN) /* hex */
  34595. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  34596. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  34597. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  34598. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  34599. +#define AUFS_DIRWH_DEF 3
  34600. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  34601. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  34602. +#define AUFS_RDBLK_DEF 512 /* bytes */
  34603. +#define AUFS_RDHASH_DEF 32
  34604. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  34605. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  34606. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  34607. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  34608. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  34609. +
  34610. +/* pseudo-link maintenace under /proc */
  34611. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  34612. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  34613. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  34614. +
  34615. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  34616. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  34617. +
  34618. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  34619. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  34620. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  34621. +
  34622. +/* doubly whiteouted */
  34623. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  34624. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  34625. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  34626. +
  34627. +/* branch permissions and attributes */
  34628. +#define AUFS_BRPERM_RW "rw"
  34629. +#define AUFS_BRPERM_RO "ro"
  34630. +#define AUFS_BRPERM_RR "rr"
  34631. +#define AUFS_BRATTR_COO_REG "coo_reg"
  34632. +#define AUFS_BRATTR_COO_ALL "coo_all"
  34633. +#define AUFS_BRATTR_FHSM "fhsm"
  34634. +#define AUFS_BRATTR_UNPIN "unpin"
  34635. +#define AUFS_BRATTR_ICEX "icex"
  34636. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  34637. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  34638. +#define AUFS_BRATTR_ICEX_TR "icextr"
  34639. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  34640. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  34641. +#define AUFS_BRRATTR_WH "wh"
  34642. +#define AUFS_BRWATTR_NLWH "nolwh"
  34643. +#define AUFS_BRWATTR_MOO "moo"
  34644. +
  34645. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  34646. +#define AuBrPerm_RO (1 << 1) /* readonly */
  34647. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  34648. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  34649. +
  34650. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  34651. +#define AuBrAttr_COO_ALL (1 << 4)
  34652. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  34653. +
  34654. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  34655. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  34656. + branch. meaningless since
  34657. + linux-3.18-rc1 */
  34658. +
  34659. +/* ignore error in copying XATTR */
  34660. +#define AuBrAttr_ICEX_SEC (1 << 7)
  34661. +#define AuBrAttr_ICEX_SYS (1 << 8)
  34662. +#define AuBrAttr_ICEX_TR (1 << 9)
  34663. +#define AuBrAttr_ICEX_USR (1 << 10)
  34664. +#define AuBrAttr_ICEX_OTH (1 << 11)
  34665. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  34666. + | AuBrAttr_ICEX_SYS \
  34667. + | AuBrAttr_ICEX_TR \
  34668. + | AuBrAttr_ICEX_USR \
  34669. + | AuBrAttr_ICEX_OTH)
  34670. +
  34671. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  34672. +#define AuBrRAttr_Mask AuBrRAttr_WH
  34673. +
  34674. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  34675. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  34676. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  34677. +
  34678. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  34679. +
  34680. +/* #warning test userspace */
  34681. +#ifdef __KERNEL__
  34682. +#ifndef CONFIG_AUFS_FHSM
  34683. +#undef AuBrAttr_FHSM
  34684. +#define AuBrAttr_FHSM 0
  34685. +#endif
  34686. +#ifndef CONFIG_AUFS_XATTR
  34687. +#undef AuBrAttr_ICEX
  34688. +#define AuBrAttr_ICEX 0
  34689. +#undef AuBrAttr_ICEX_SEC
  34690. +#define AuBrAttr_ICEX_SEC 0
  34691. +#undef AuBrAttr_ICEX_SYS
  34692. +#define AuBrAttr_ICEX_SYS 0
  34693. +#undef AuBrAttr_ICEX_TR
  34694. +#define AuBrAttr_ICEX_TR 0
  34695. +#undef AuBrAttr_ICEX_USR
  34696. +#define AuBrAttr_ICEX_USR 0
  34697. +#undef AuBrAttr_ICEX_OTH
  34698. +#define AuBrAttr_ICEX_OTH 0
  34699. +#endif
  34700. +#endif
  34701. +
  34702. +/* the longest combination */
  34703. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  34704. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  34705. + "+" AUFS_BRATTR_COO_REG \
  34706. + "+" AUFS_BRATTR_FHSM \
  34707. + "+" AUFS_BRATTR_UNPIN \
  34708. + "+" AUFS_BRATTR_ICEX_SEC \
  34709. + "+" AUFS_BRATTR_ICEX_SYS \
  34710. + "+" AUFS_BRATTR_ICEX_USR \
  34711. + "+" AUFS_BRATTR_ICEX_OTH \
  34712. + "+" AUFS_BRWATTR_NLWH)
  34713. +
  34714. +typedef struct {
  34715. + char a[AuBrPermStrSz];
  34716. +} au_br_perm_str_t;
  34717. +
  34718. +static inline int au_br_writable(int brperm)
  34719. +{
  34720. + return brperm & AuBrPerm_RW;
  34721. +}
  34722. +
  34723. +static inline int au_br_whable(int brperm)
  34724. +{
  34725. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  34726. +}
  34727. +
  34728. +static inline int au_br_wh_linkable(int brperm)
  34729. +{
  34730. + return !(brperm & AuBrWAttr_NoLinkWH);
  34731. +}
  34732. +
  34733. +static inline int au_br_cmoo(int brperm)
  34734. +{
  34735. + return brperm & AuBrAttr_CMOO_Mask;
  34736. +}
  34737. +
  34738. +static inline int au_br_fhsm(int brperm)
  34739. +{
  34740. + return brperm & AuBrAttr_FHSM;
  34741. +}
  34742. +
  34743. +/* ---------------------------------------------------------------------- */
  34744. +
  34745. +/* ioctl */
  34746. +enum {
  34747. + /* readdir in userspace */
  34748. + AuCtl_RDU,
  34749. + AuCtl_RDU_INO,
  34750. +
  34751. + AuCtl_WBR_FD, /* pathconf wrapper */
  34752. + AuCtl_IBUSY, /* busy inode */
  34753. + AuCtl_MVDOWN, /* move-down */
  34754. + AuCtl_BR, /* info about branches */
  34755. + AuCtl_FHSM_FD /* connection for fhsm */
  34756. +};
  34757. +
  34758. +/* borrowed from linux/include/linux/kernel.h */
  34759. +#ifndef ALIGN
  34760. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  34761. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  34762. +#endif
  34763. +
  34764. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  34765. +#ifndef __aligned
  34766. +#define __aligned(x) __attribute__((aligned(x)))
  34767. +#endif
  34768. +
  34769. +#ifdef __KERNEL__
  34770. +#ifndef __packed
  34771. +#define __packed __attribute__((packed))
  34772. +#endif
  34773. +#endif
  34774. +
  34775. +struct au_rdu_cookie {
  34776. + uint64_t h_pos;
  34777. + int16_t bindex;
  34778. + uint8_t flags;
  34779. + uint8_t pad;
  34780. + uint32_t generation;
  34781. +} __aligned(8);
  34782. +
  34783. +struct au_rdu_ent {
  34784. + uint64_t ino;
  34785. + int16_t bindex;
  34786. + uint8_t type;
  34787. + uint8_t nlen;
  34788. + uint8_t wh;
  34789. + char name[0];
  34790. +} __aligned(8);
  34791. +
  34792. +static inline int au_rdu_len(int nlen)
  34793. +{
  34794. + /* include the terminating NULL */
  34795. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  34796. + sizeof(uint64_t));
  34797. +}
  34798. +
  34799. +union au_rdu_ent_ul {
  34800. + struct au_rdu_ent __user *e;
  34801. + uint64_t ul;
  34802. +};
  34803. +
  34804. +enum {
  34805. + AufsCtlRduV_SZ,
  34806. + AufsCtlRduV_End
  34807. +};
  34808. +
  34809. +struct aufs_rdu {
  34810. + /* input */
  34811. + union {
  34812. + uint64_t sz; /* AuCtl_RDU */
  34813. + uint64_t nent; /* AuCtl_RDU_INO */
  34814. + };
  34815. + union au_rdu_ent_ul ent;
  34816. + uint16_t verify[AufsCtlRduV_End];
  34817. +
  34818. + /* input/output */
  34819. + uint32_t blk;
  34820. +
  34821. + /* output */
  34822. + union au_rdu_ent_ul tail;
  34823. + /* number of entries which were added in a single call */
  34824. + uint64_t rent;
  34825. + uint8_t full;
  34826. + uint8_t shwh;
  34827. +
  34828. + struct au_rdu_cookie cookie;
  34829. +} __aligned(8);
  34830. +
  34831. +/* ---------------------------------------------------------------------- */
  34832. +
  34833. +struct aufs_wbr_fd {
  34834. + uint32_t oflags;
  34835. + int16_t brid;
  34836. +} __aligned(8);
  34837. +
  34838. +/* ---------------------------------------------------------------------- */
  34839. +
  34840. +struct aufs_ibusy {
  34841. + uint64_t ino, h_ino;
  34842. + int16_t bindex;
  34843. +} __aligned(8);
  34844. +
  34845. +/* ---------------------------------------------------------------------- */
  34846. +
  34847. +/* error code for move-down */
  34848. +/* the actual message strings are implemented in aufs-util.git */
  34849. +enum {
  34850. + EAU_MVDOWN_OPAQUE = 1,
  34851. + EAU_MVDOWN_WHITEOUT,
  34852. + EAU_MVDOWN_UPPER,
  34853. + EAU_MVDOWN_BOTTOM,
  34854. + EAU_MVDOWN_NOUPPER,
  34855. + EAU_MVDOWN_NOLOWERBR,
  34856. + EAU_Last
  34857. +};
  34858. +
  34859. +/* flags for move-down */
  34860. +#define AUFS_MVDOWN_DMSG 1
  34861. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  34862. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  34863. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  34864. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  34865. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  34866. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  34867. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  34868. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  34869. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  34870. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  34871. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  34872. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  34873. +
  34874. +/* index for move-down */
  34875. +enum {
  34876. + AUFS_MVDOWN_UPPER,
  34877. + AUFS_MVDOWN_LOWER,
  34878. + AUFS_MVDOWN_NARRAY
  34879. +};
  34880. +
  34881. +/*
  34882. + * additional info of move-down
  34883. + * number of free blocks and inodes.
  34884. + * subset of struct kstatfs, but smaller and always 64bit.
  34885. + */
  34886. +struct aufs_stfs {
  34887. + uint64_t f_blocks;
  34888. + uint64_t f_bavail;
  34889. + uint64_t f_files;
  34890. + uint64_t f_ffree;
  34891. +};
  34892. +
  34893. +struct aufs_stbr {
  34894. + int16_t brid; /* optional input */
  34895. + int16_t bindex; /* output */
  34896. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  34897. +} __aligned(8);
  34898. +
  34899. +struct aufs_mvdown {
  34900. + uint32_t flags; /* input/output */
  34901. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  34902. + int8_t au_errno; /* output */
  34903. +} __aligned(8);
  34904. +
  34905. +/* ---------------------------------------------------------------------- */
  34906. +
  34907. +union aufs_brinfo {
  34908. + /* PATH_MAX may differ between kernel-space and user-space */
  34909. + char _spacer[4096];
  34910. + struct {
  34911. + int16_t id;
  34912. + int perm;
  34913. + char path[0];
  34914. + };
  34915. +} __aligned(8);
  34916. +
  34917. +/* ---------------------------------------------------------------------- */
  34918. +
  34919. +#define AuCtlType 'A'
  34920. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  34921. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  34922. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  34923. + struct aufs_wbr_fd)
  34924. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  34925. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  34926. + struct aufs_mvdown)
  34927. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  34928. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  34929. +
  34930. +#endif /* __AUFS_TYPE_H__ */