1
0

aufs.patch 910 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215
  1. diff -Nur linux-4.1.10.orig/Documentation/ABI/testing/debugfs-aufs linux-4.1.10/Documentation/ABI/testing/debugfs-aufs
  2. --- linux-4.1.10.orig/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-4.1.10/Documentation/ABI/testing/debugfs-aufs 2015-10-22 21:35:53.000000000 +0200
  4. @@ -0,0 +1,50 @@
  5. +What: /debug/aufs/si_<id>/
  6. +Date: March 2009
  7. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  8. +Description:
  9. + Under /debug/aufs, a directory named si_<id> is created
  10. + per aufs mount, where <id> is a unique id generated
  11. + internally.
  12. +
  13. +What: /debug/aufs/si_<id>/plink
  14. +Date: Apr 2013
  15. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  16. +Description:
  17. + It has three lines and shows the information about the
  18. + pseudo-link. The first line is a single number
  19. + representing a number of buckets. The second line is a
  20. + number of pseudo-links per buckets (separated by a
  21. + blank). The last line is a single number representing a
  22. + total number of psedo-links.
  23. + When the aufs mount option 'noplink' is specified, it
  24. + will show "1\n0\n0\n".
  25. +
  26. +What: /debug/aufs/si_<id>/xib
  27. +Date: March 2009
  28. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  29. +Description:
  30. + It shows the consumed blocks by xib (External Inode Number
  31. + Bitmap), its block size and file size.
  32. + When the aufs mount option 'noxino' is specified, it
  33. + will be empty. About XINO files, see the aufs manual.
  34. +
  35. +What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
  36. +Date: March 2009
  37. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  38. +Description:
  39. + It shows the consumed blocks by xino (External Inode Number
  40. + Translation Table), its link count, block size and file
  41. + size.
  42. + When the aufs mount option 'noxino' is specified, it
  43. + will be empty. About XINO files, see the aufs manual.
  44. +
  45. +What: /debug/aufs/si_<id>/xigen
  46. +Date: March 2009
  47. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  48. +Description:
  49. + It shows the consumed blocks by xigen (External Inode
  50. + Generation Table), its block size and file size.
  51. + If CONFIG_AUFS_EXPORT is disabled, this entry will not
  52. + be created.
  53. + When the aufs mount option 'noxino' is specified, it
  54. + will be empty. About XINO files, see the aufs manual.
  55. diff -Nur linux-4.1.10.orig/Documentation/ABI/testing/sysfs-aufs linux-4.1.10/Documentation/ABI/testing/sysfs-aufs
  56. --- linux-4.1.10.orig/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
  57. +++ linux-4.1.10/Documentation/ABI/testing/sysfs-aufs 2015-10-22 21:35:53.000000000 +0200
  58. @@ -0,0 +1,31 @@
  59. +What: /sys/fs/aufs/si_<id>/
  60. +Date: March 2009
  61. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  62. +Description:
  63. + Under /sys/fs/aufs, a directory named si_<id> is created
  64. + per aufs mount, where <id> is a unique id generated
  65. + internally.
  66. +
  67. +What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
  68. +Date: March 2009
  69. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  70. +Description:
  71. + It shows the abolute path of a member directory (which
  72. + is called branch) in aufs, and its permission.
  73. +
  74. +What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
  75. +Date: July 2013
  76. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  77. +Description:
  78. + It shows the id of a member directory (which is called
  79. + branch) in aufs.
  80. +
  81. +What: /sys/fs/aufs/si_<id>/xi_path
  82. +Date: March 2009
  83. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  84. +Description:
  85. + It shows the abolute path of XINO (External Inode Number
  86. + Bitmap, Translation Table and Generation Table) file
  87. + even if it is the default path.
  88. + When the aufs mount option 'noxino' is specified, it
  89. + will be empty. About XINO files, see the aufs manual.
  90. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/01intro.txt linux-4.1.10/Documentation/filesystems/aufs/design/01intro.txt
  91. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
  92. +++ linux-4.1.10/Documentation/filesystems/aufs/design/01intro.txt 2015-10-22 21:35:53.000000000 +0200
  93. @@ -0,0 +1,170 @@
  94. +
  95. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  96. +#
  97. +# This program is free software; you can redistribute it and/or modify
  98. +# it under the terms of the GNU General Public License as published by
  99. +# the Free Software Foundation; either version 2 of the License, or
  100. +# (at your option) any later version.
  101. +#
  102. +# This program is distributed in the hope that it will be useful,
  103. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  104. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  105. +# GNU General Public License for more details.
  106. +#
  107. +# You should have received a copy of the GNU General Public License
  108. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  109. +
  110. +Introduction
  111. +----------------------------------------
  112. +
  113. +aufs [ei ju: ef es] | [a u f s]
  114. +1. abbrev. for "advanced multi-layered unification filesystem".
  115. +2. abbrev. for "another unionfs".
  116. +3. abbrev. for "auf das" in German which means "on the" in English.
  117. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  118. + But "Filesystem aufs Filesystem" is hard to understand.
  119. +
  120. +AUFS is a filesystem with features:
  121. +- multi layered stackable unification filesystem, the member directory
  122. + is called as a branch.
  123. +- branch permission and attribute, 'readonly', 'real-readonly',
  124. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  125. + combination.
  126. +- internal "file copy-on-write".
  127. +- logical deletion, whiteout.
  128. +- dynamic branch manipulation, adding, deleting and changing permission.
  129. +- allow bypassing aufs, user's direct branch access.
  130. +- external inode number translation table and bitmap which maintains the
  131. + persistent aufs inode number.
  132. +- seekable directory, including NFS readdir.
  133. +- file mapping, mmap and sharing pages.
  134. +- pseudo-link, hardlink over branches.
  135. +- loopback mounted filesystem as a branch.
  136. +- several policies to select one among multiple writable branches.
  137. +- revert a single systemcall when an error occurs in aufs.
  138. +- and more...
  139. +
  140. +
  141. +Multi Layered Stackable Unification Filesystem
  142. +----------------------------------------------------------------------
  143. +Most people already knows what it is.
  144. +It is a filesystem which unifies several directories and provides a
  145. +merged single directory. When users access a file, the access will be
  146. +passed/re-directed/converted (sorry, I am not sure which English word is
  147. +correct) to the real file on the member filesystem. The member
  148. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  149. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  150. +readonly branch is handled by creating 'whiteout' on the upper writable
  151. +branch.
  152. +
  153. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  154. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  155. +different approaches to implement the merged-view.
  156. +The former tries putting it into VFS, and the latter implements as a
  157. +separate filesystem.
  158. +(If I misunderstand about these implementations, please let me know and
  159. +I shall correct it. Because it is a long time ago when I read their
  160. +source files last time).
  161. +
  162. +UnionMount's approach will be able to small, but may be hard to share
  163. +branches between several UnionMount since the whiteout in it is
  164. +implemented in the inode on branch filesystem and always
  165. +shared. According to Bharata's post, readdir does not seems to be
  166. +finished yet.
  167. +There are several missing features known in this implementations such as
  168. +- for users, the inode number may change silently. eg. copy-up.
  169. +- link(2) may break by copy-up.
  170. +- read(2) may get an obsoleted filedata (fstat(2) too).
  171. +- fcntl(F_SETLK) may be broken by copy-up.
  172. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  173. + open(O_RDWR).
  174. +
  175. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  176. +merged into mainline. This is another implementation of UnionMount as a
  177. +separated filesystem. All the limitations and known problems which
  178. +UnionMount are equally inherited to "overlay" filesystem.
  179. +
  180. +Unionfs has a longer history. When I started implementing a stackable
  181. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  182. +inode, dentry and file objects and they have an array pointing lower
  183. +same kind objects. After contributing many patches for Unionfs, I
  184. +re-started my project AUFS (Jun 2006).
  185. +
  186. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  187. +implemented my own ideas, approaches and enhancements and it became
  188. +totally different one.
  189. +
  190. +Comparing DM snapshot and fs based implementation
  191. +- the number of bytes to be copied between devices is much smaller.
  192. +- the type of filesystem must be one and only.
  193. +- the fs must be writable, no readonly fs, even for the lower original
  194. + device. so the compression fs will not be usable. but if we use
  195. + loopback mount, we may address this issue.
  196. + for instance,
  197. + mount /cdrom/squashfs.img /sq
  198. + losetup /sq/ext2.img
  199. + losetup /somewhere/cow
  200. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  201. +- it will be difficult (or needs more operations) to extract the
  202. + difference between the original device and COW.
  203. +- DM snapshot-merge may help a lot when users try merging. in the
  204. + fs-layer union, users will use rsync(1).
  205. +
  206. +You may want to read my old paper "Filesystems in LiveCD"
  207. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  208. +
  209. +
  210. +Several characters/aspects/persona of aufs
  211. +----------------------------------------------------------------------
  212. +
  213. +Aufs has several characters, aspects or persona.
  214. +1. a filesystem, callee of VFS helper
  215. +2. sub-VFS, caller of VFS helper for branches
  216. +3. a virtual filesystem which maintains persistent inode number
  217. +4. reader/writer of files on branches such like an application
  218. +
  219. +1. Callee of VFS Helper
  220. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  221. +unlink(2) from an application reaches sys_unlink() kernel function and
  222. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  223. +calls filesystem specific unlink operation. Actually aufs implements the
  224. +unlink operation but it behaves like a redirector.
  225. +
  226. +2. Caller of VFS Helper for Branches
  227. +aufs_unlink() passes the unlink request to the branch filesystem as if
  228. +it were called from VFS. So the called unlink operation of the branch
  229. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  230. +every necessary pre/post operation for the branch filesystem.
  231. +- acquire the lock for the parent dir on a branch
  232. +- lookup in a branch
  233. +- revalidate dentry on a branch
  234. +- mnt_want_write() for a branch
  235. +- vfs_unlink() for a branch
  236. +- mnt_drop_write() for a branch
  237. +- release the lock on a branch
  238. +
  239. +3. Persistent Inode Number
  240. +One of the most important issue for a filesystem is to maintain inode
  241. +numbers. This is particularly important to support exporting a
  242. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  243. +backend block device for its own. But some storage is necessary to
  244. +keep and maintain the inode numbers. It may be a large space and may not
  245. +suit to keep in memory. Aufs rents some space from its first writable
  246. +branch filesystem (by default) and creates file(s) on it. These files
  247. +are created by aufs internally and removed soon (currently) keeping
  248. +opened.
  249. +Note: Because these files are removed, they are totally gone after
  250. + unmounting aufs. It means the inode numbers are not persistent
  251. + across unmount or reboot. I have a plan to make them really
  252. + persistent which will be important for aufs on NFS server.
  253. +
  254. +4. Read/Write Files Internally (copy-on-write)
  255. +Because a branch can be readonly, when you write a file on it, aufs will
  256. +"copy-up" it to the upper writable branch internally. And then write the
  257. +originally requested thing to the file. Generally kernel doesn't
  258. +open/read/write file actively. In aufs, even a single write may cause a
  259. +internal "file copy". This behaviour is very similar to cp(1) command.
  260. +
  261. +Some people may think it is better to pass such work to user space
  262. +helper, instead of doing in kernel space. Actually I am still thinking
  263. +about it. But currently I have implemented it in kernel space.
  264. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/02struct.txt linux-4.1.10/Documentation/filesystems/aufs/design/02struct.txt
  265. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
  266. +++ linux-4.1.10/Documentation/filesystems/aufs/design/02struct.txt 2015-10-22 21:35:53.000000000 +0200
  267. @@ -0,0 +1,258 @@
  268. +
  269. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  270. +#
  271. +# This program is free software; you can redistribute it and/or modify
  272. +# it under the terms of the GNU General Public License as published by
  273. +# the Free Software Foundation; either version 2 of the License, or
  274. +# (at your option) any later version.
  275. +#
  276. +# This program is distributed in the hope that it will be useful,
  277. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  278. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  279. +# GNU General Public License for more details.
  280. +#
  281. +# You should have received a copy of the GNU General Public License
  282. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  283. +
  284. +Basic Aufs Internal Structure
  285. +
  286. +Superblock/Inode/Dentry/File Objects
  287. +----------------------------------------------------------------------
  288. +As like an ordinary filesystem, aufs has its own
  289. +superblock/inode/dentry/file objects. All these objects have a
  290. +dynamically allocated array and store the same kind of pointers to the
  291. +lower filesystem, branch.
  292. +For example, when you build a union with one readwrite branch and one
  293. +readonly, mounted /au, /rw and /ro respectively.
  294. +- /au = /rw + /ro
  295. +- /ro/fileA exists but /rw/fileA
  296. +
  297. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  298. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  299. +- [0] = NULL (because /rw/fileA doesn't exist)
  300. +- [1] = /ro/fileA
  301. +
  302. +This style of an array is essentially same to the aufs
  303. +superblock/inode/dentry/file objects.
  304. +
  305. +Because aufs supports manipulating branches, ie. add/delete/change
  306. +branches dynamically, these objects has its own generation. When
  307. +branches are changed, the generation in aufs superblock is
  308. +incremented. And a generation in other object are compared when it is
  309. +accessed. When a generation in other objects are obsoleted, aufs
  310. +refreshes the internal array.
  311. +
  312. +
  313. +Superblock
  314. +----------------------------------------------------------------------
  315. +Additionally aufs superblock has some data for policies to select one
  316. +among multiple writable branches, XIB files, pseudo-links and kobject.
  317. +See below in detail.
  318. +About the policies which supports copy-down a directory, see
  319. +wbr_policy.txt too.
  320. +
  321. +
  322. +Branch and XINO(External Inode Number Translation Table)
  323. +----------------------------------------------------------------------
  324. +Every branch has its own xino (external inode number translation table)
  325. +file. The xino file is created and unlinked by aufs internally. When two
  326. +members of a union exist on the same filesystem, they share the single
  327. +xino file.
  328. +The struct of a xino file is simple, just a sequence of aufs inode
  329. +numbers which is indexed by the lower inode number.
  330. +In the above sample, assume the inode number of /ro/fileA is i111 and
  331. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  332. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  333. +
  334. +When the inode numbers are not contiguous, the xino file will be sparse
  335. +which has a hole in it and doesn't consume as much disk space as it
  336. +might appear. If your branch filesystem consumes disk space for such
  337. +holes, then you should specify 'xino=' option at mounting aufs.
  338. +
  339. +Aufs has a mount option to free the disk blocks for such holes in XINO
  340. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  341. +meet a problem of disk shortage due to XINO files, then you should try
  342. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  343. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  344. +the holes in XINO files.
  345. +
  346. +Also a writable branch has three kinds of "whiteout bases". All these
  347. +are existed when the branch is joined to aufs, and their names are
  348. +whiteout-ed doubly, so that users will never see their names in aufs
  349. +hierarchy.
  350. +1. a regular file which will be hardlinked to all whiteouts.
  351. +2. a directory to store a pseudo-link.
  352. +3. a directory to store an "orphan"-ed file temporary.
  353. +
  354. +1. Whiteout Base
  355. + When you remove a file on a readonly branch, aufs handles it as a
  356. + logical deletion and creates a whiteout on the upper writable branch
  357. + as a hardlink of this file in order not to consume inode on the
  358. + writable branch.
  359. +2. Pseudo-link Dir
  360. + See below, Pseudo-link.
  361. +3. Step-Parent Dir
  362. + When "fileC" exists on the lower readonly branch only and it is
  363. + opened and removed with its parent dir, and then user writes
  364. + something into it, then aufs copies-up fileC to this
  365. + directory. Because there is no other dir to store fileC. After
  366. + creating a file under this dir, the file is unlinked.
  367. +
  368. +Because aufs supports manipulating branches, ie. add/delete/change
  369. +dynamically, a branch has its own id. When the branch order changes,
  370. +aufs finds the new index by searching the branch id.
  371. +
  372. +
  373. +Pseudo-link
  374. +----------------------------------------------------------------------
  375. +Assume "fileA" exists on the lower readonly branch only and it is
  376. +hardlinked to "fileB" on the branch. When you write something to fileA,
  377. +aufs copies-up it to the upper writable branch. Additionally aufs
  378. +creates a hardlink under the Pseudo-link Directory of the writable
  379. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  380. +simple list. If fileB is read after unlinking fileA, aufs returns
  381. +filedata from the pseudo-link instead of the lower readonly
  382. +branch. Because the pseudo-link is based upon the inode, to keep the
  383. +inode number by xino (see above) is essentially necessary.
  384. +
  385. +All the hardlinks under the Pseudo-link Directory of the writable branch
  386. +should be restored in a proper location later. Aufs provides a utility
  387. +to do this. The userspace helpers executed at remounting and unmounting
  388. +aufs by default.
  389. +During this utility is running, it puts aufs into the pseudo-link
  390. +maintenance mode. In this mode, only the process which began the
  391. +maintenance mode (and its child processes) is allowed to operate in
  392. +aufs. Some other processes which are not related to the pseudo-link will
  393. +be allowed to run too, but the rest have to return an error or wait
  394. +until the maintenance mode ends. If a process already acquires an inode
  395. +mutex (in VFS), it has to return an error.
  396. +
  397. +
  398. +XIB(external inode number bitmap)
  399. +----------------------------------------------------------------------
  400. +Addition to the xino file per a branch, aufs has an external inode number
  401. +bitmap in a superblock object. It is also an internal file such like a
  402. +xino file.
  403. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  404. +not.
  405. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  406. +
  407. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  408. +reduce the number of consumed disk blocks for these files.
  409. +
  410. +
  411. +Virtual or Vertical Dir, and Readdir in Userspace
  412. +----------------------------------------------------------------------
  413. +In order to support multiple layers (branches), aufs readdir operation
  414. +constructs a virtual dir block on memory. For readdir, aufs calls
  415. +vfs_readdir() internally for each dir on branches, merges their entries
  416. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  417. +object. So the file object has its entry list until it is closed. The
  418. +entry list will be updated when the file position is zero and becomes
  419. +obsoleted. This decision is made in aufs automatically.
  420. +
  421. +The dynamically allocated memory block for the name of entries has a
  422. +unit of 512 bytes (by default) and stores the names contiguously (no
  423. +padding). Another block for each entry is handled by kmem_cache too.
  424. +During building dir blocks, aufs creates hash list and judging whether
  425. +the entry is whiteouted by its upper branch or already listed.
  426. +The merged result is cached in the corresponding inode object and
  427. +maintained by a customizable life-time option.
  428. +
  429. +Some people may call it can be a security hole or invite DoS attack
  430. +since the opened and once readdir-ed dir (file object) holds its entry
  431. +list and becomes a pressure for system memory. But I'd say it is similar
  432. +to files under /proc or /sys. The virtual files in them also holds a
  433. +memory page (generally) while they are opened. When an idea to reduce
  434. +memory for them is introduced, it will be applied to aufs too.
  435. +For those who really hate this situation, I've developed readdir(3)
  436. +library which operates this merging in userspace. You just need to set
  437. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  438. +kernel space for readdir(3).
  439. +
  440. +
  441. +Workqueue
  442. +----------------------------------------------------------------------
  443. +Aufs sometimes requires privilege access to a branch. For instance,
  444. +in copy-up/down operation. When a user process is going to make changes
  445. +to a file which exists in the lower readonly branch only, and the mode
  446. +of one of ancestor directories may not be writable by a user
  447. +process. Here aufs copy-up the file with its ancestors and they may
  448. +require privilege to set its owner/group/mode/etc.
  449. +This is a typical case of a application character of aufs (see
  450. +Introduction).
  451. +
  452. +Aufs uses workqueue synchronously for this case. It creates its own
  453. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  454. +passes the request to call mkdir or write (for example), and wait for
  455. +its completion. This approach solves a problem of a signal handler
  456. +simply.
  457. +If aufs didn't adopt the workqueue and changed the privilege of the
  458. +process, then the process may receive the unexpected SIGXFSZ or other
  459. +signals.
  460. +
  461. +Also aufs uses the system global workqueue ("events" kernel thread) too
  462. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  463. +whiteout base and etc. This is unrelated to a privilege.
  464. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  465. +superblock at the beginning, at the same time waits for the completion
  466. +of all queued asynchronous tasks.
  467. +
  468. +
  469. +Whiteout
  470. +----------------------------------------------------------------------
  471. +The whiteout in aufs is very similar to Unionfs's. That is represented
  472. +by its filename. UnionMount takes an approach of a file mode, but I am
  473. +afraid several utilities (find(1) or something) will have to support it.
  474. +
  475. +Basically the whiteout represents "logical deletion" which stops aufs to
  476. +lookup further, but also it represents "dir is opaque" which also stop
  477. +further lookup.
  478. +
  479. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  480. +In order to make several functions in a single systemcall to be
  481. +revertible, aufs adopts an approach to rename a directory to a temporary
  482. +unique whiteouted name.
  483. +For example, in rename(2) dir where the target dir already existed, aufs
  484. +renames the target dir to a temporary unique whiteouted name before the
  485. +actual rename on a branch, and then handles other actions (make it opaque,
  486. +update the attributes, etc). If an error happens in these actions, aufs
  487. +simply renames the whiteouted name back and returns an error. If all are
  488. +succeeded, aufs registers a function to remove the whiteouted unique
  489. +temporary name completely and asynchronously to the system global
  490. +workqueue.
  491. +
  492. +
  493. +Copy-up
  494. +----------------------------------------------------------------------
  495. +It is a well-known feature or concept.
  496. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  497. +internally and makes change to the new file on the upper writable branch.
  498. +When the trigger systemcall does not update the timestamps of the parent
  499. +dir, aufs reverts it after copy-up.
  500. +
  501. +
  502. +Move-down (aufs3.9 and later)
  503. +----------------------------------------------------------------------
  504. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  505. +the lower readonly branch to the upper writable branch when a user
  506. +changes something about the file.
  507. +"Move-down" is an opposite action of copy-up. Basically this action is
  508. +ran manually instead of automatically and internally.
  509. +For desgin and implementation, aufs has to consider these issues.
  510. +- whiteout for the file may exist on the lower branch.
  511. +- ancestor directories may not exist on the lower branch.
  512. +- diropq for the ancestor directories may exist on the upper branch.
  513. +- free space on the lower branch will reduce.
  514. +- another access to the file may happen during moving-down, including
  515. + UDBA (see "Revalidate Dentry and UDBA").
  516. +- the file should not be hard-linked nor pseudo-linked. they should be
  517. + handled by auplink utility later.
  518. +
  519. +Sometimes users want to move-down a file from the upper writable branch
  520. +to the lower readonly or writable branch. For instance,
  521. +- the free space of the upper writable branch is going to run out.
  522. +- create a new intermediate branch between the upper and lower branch.
  523. +- etc.
  524. +
  525. +For this purpose, use "aumvdown" command in aufs-util.git.
  526. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/03atomic_open.txt linux-4.1.10/Documentation/filesystems/aufs/design/03atomic_open.txt
  527. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
  528. +++ linux-4.1.10/Documentation/filesystems/aufs/design/03atomic_open.txt 2015-10-22 21:35:53.000000000 +0200
  529. @@ -0,0 +1,85 @@
  530. +
  531. +# Copyright (C) 2015 Junjiro R. Okajima
  532. +#
  533. +# This program is free software; you can redistribute it and/or modify
  534. +# it under the terms of the GNU General Public License as published by
  535. +# the Free Software Foundation; either version 2 of the License, or
  536. +# (at your option) any later version.
  537. +#
  538. +# This program is distributed in the hope that it will be useful,
  539. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  540. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  541. +# GNU General Public License for more details.
  542. +#
  543. +# You should have received a copy of the GNU General Public License
  544. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  545. +
  546. +Support for a branch who has its ->atomic_open()
  547. +----------------------------------------------------------------------
  548. +The filesystems who implement its ->atomic_open() are not majority. For
  549. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  550. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  551. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  552. +sure whether all filesystems who have ->atomic_open() behave like this,
  553. +but NFSv4 surely returns the error.
  554. +
  555. +In order to support ->atomic_open() for aufs, there are a few
  556. +approaches.
  557. +
  558. +A. Introduce aufs_atomic_open()
  559. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  560. + branch fs.
  561. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  562. + an aufs user Pip Cet's approach
  563. + - calls aufs_create(), VFS finish_open() and notify_change().
  564. + - pass fake-mode to finish_open(), and then correct the mode by
  565. + notify_change().
  566. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  567. + - no aufs_atomic_open().
  568. + - aufs_lookup() registers the TID to an aufs internal object.
  569. + - aufs_create() does nothing when the matching TID is registered, but
  570. + registers the mode.
  571. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  572. + TID is registered.
  573. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  574. + credential
  575. + - no aufs_atomic_open().
  576. + - aufs_create() registers the TID to an internal object. this info
  577. + represents "this process created this file just now."
  578. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  579. + registered TID and re-try open() with superuser's credential.
  580. +
  581. +Pros and cons for each approach.
  582. +
  583. +A.
  584. + - straightforward but highly depends upon VFS internal.
  585. + - the atomic behavaiour is kept.
  586. + - some of parameters such as nameidata are hard to reproduce for
  587. + branch fs.
  588. + - large overhead.
  589. +B.
  590. + - easy to implement.
  591. + - the atomic behavaiour is lost.
  592. +C.
  593. + - the atomic behavaiour is kept.
  594. + - dirty and tricky.
  595. + - VFS checks whether the file is created correctly after calling
  596. + ->create(), which means this approach doesn't work.
  597. +D.
  598. + - easy to implement.
  599. + - the atomic behavaiour is lost.
  600. + - to open a file with superuser's credential and give it to a user
  601. + process is a bad idea, since the file object keeps the credential
  602. + in it. It may affect LSM or something. This approach doesn't work
  603. + either.
  604. +
  605. +The approach A is ideal, but it hard to implement. So here is a
  606. +variation of A, which is to be implemented.
  607. +
  608. +A-1. Introduce aufs_atomic_open()
  609. + - calls branch fs ->atomic_open() if exists. otherwise calls
  610. + vfs_create() and finish_open().
  611. + - the demerit is that the several checks after branch fs
  612. + ->atomic_open() are lost. in the ordinary case, the checks are
  613. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  614. + be implemented in aufs, but not all I am afraid.
  615. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/03lookup.txt linux-4.1.10/Documentation/filesystems/aufs/design/03lookup.txt
  616. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
  617. +++ linux-4.1.10/Documentation/filesystems/aufs/design/03lookup.txt 2015-10-22 21:35:53.000000000 +0200
  618. @@ -0,0 +1,113 @@
  619. +
  620. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  621. +#
  622. +# This program is free software; you can redistribute it and/or modify
  623. +# it under the terms of the GNU General Public License as published by
  624. +# the Free Software Foundation; either version 2 of the License, or
  625. +# (at your option) any later version.
  626. +#
  627. +# This program is distributed in the hope that it will be useful,
  628. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  629. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  630. +# GNU General Public License for more details.
  631. +#
  632. +# You should have received a copy of the GNU General Public License
  633. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  634. +
  635. +Lookup in a Branch
  636. +----------------------------------------------------------------------
  637. +Since aufs has a character of sub-VFS (see Introduction), it operates
  638. +lookup for branches as VFS does. It may be a heavy work. But almost all
  639. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  640. +directly connected to its parent. Digging down the directory hierarchy
  641. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  642. +aufs calls it.
  643. +
  644. +When a branch is a remote filesystem, aufs basically relies upon its
  645. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  646. +them.
  647. +For d_revalidate, aufs implements three levels of revalidate tests. See
  648. +"Revalidate Dentry and UDBA" in detail.
  649. +
  650. +
  651. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  652. +----------------------------------------------------------------------
  653. +Let's try case study.
  654. +- aufs has two branches, upper readwrite and lower readonly.
  655. + /au = /rw + /ro
  656. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  657. +- user invoked "chmod a+rx /au/dirA"
  658. +- the internal copy-up is activated and "/rw/dirA" is created and its
  659. + permission bits are set to world readable.
  660. +- then "/au/dirA" becomes world readable?
  661. +
  662. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  663. +or it may be a natively readonly filesystem. If aufs respects the lower
  664. +branch, it should not respond readdir request from other users. But user
  665. +allowed it by chmod. Should really aufs rejects showing the entries
  666. +under /ro/dirA?
  667. +
  668. +To be honest, I don't have a good solution for this case. So aufs
  669. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  670. +users.
  671. +When dirperm1 is specified, aufs checks only the highest one for the
  672. +directory permission, and shows the entries. Otherwise, as usual, checks
  673. +every dir existing on all branches and rejects the request.
  674. +
  675. +As a side effect, dirperm1 option improves the performance of aufs
  676. +because the number of permission check is reduced when the number of
  677. +branch is many.
  678. +
  679. +
  680. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  681. +----------------------------------------------------------------------
  682. +Generally VFS helpers re-validate a dentry as a part of lookup.
  683. +0. digging down the directory hierarchy.
  684. +1. lock the parent dir by its i_mutex.
  685. +2. lookup the final (child) entry.
  686. +3. revalidate it.
  687. +4. call the actual operation (create, unlink, etc.)
  688. +5. unlock the parent dir
  689. +
  690. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  691. +called. Actually aufs implements it and checks the dentry on a branch is
  692. +still valid.
  693. +But it is not enough. Because aufs has to release the lock for the
  694. +parent dir on a branch at the end of ->lookup() (step 2) and
  695. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  696. +held by VFS.
  697. +If the file on a branch is changed directly, eg. bypassing aufs, after
  698. +aufs released the lock, then the subsequent operation may cause
  699. +something unpleasant result.
  700. +
  701. +This situation is a result of VFS architecture, ->lookup() and
  702. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  703. +design from VFS's point of view. It is just not suitable for sub-VFS
  704. +character in aufs.
  705. +
  706. +Aufs supports such case by three level of revalidation which is
  707. +selectable by user.
  708. +1. Simple Revalidate
  709. + Addition to the native flow in VFS's, confirm the child-parent
  710. + relationship on the branch just after locking the parent dir on the
  711. + branch in the "actual operation" (step 4). When this validation
  712. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  713. + checks the validation of the dentry on branches.
  714. +2. Monitor Changes Internally by Inotify/Fsnotify
  715. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  716. + the dentry on the branch, and returns EBUSY if it finds different
  717. + dentry.
  718. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  719. + during it is in cache. When the event is notified, aufs registers a
  720. + function to kernel 'events' thread by schedule_work(). And the
  721. + function sets some special status to the cached aufs dentry and inode
  722. + private data. If they are not cached, then aufs has nothing to
  723. + do. When the same file is accessed through aufs (step 0-3) later,
  724. + aufs will detect the status and refresh all necessary data.
  725. + In this mode, aufs has to ignore the event which is fired by aufs
  726. + itself.
  727. +3. No Extra Validation
  728. + This is the simplest test and doesn't add any additional revalidation
  729. + test, and skip the revalidation in step 4. It is useful and improves
  730. + aufs performance when system surely hide the aufs branches from user,
  731. + by over-mounting something (or another method).
  732. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/04branch.txt linux-4.1.10/Documentation/filesystems/aufs/design/04branch.txt
  733. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
  734. +++ linux-4.1.10/Documentation/filesystems/aufs/design/04branch.txt 2015-10-22 21:35:53.000000000 +0200
  735. @@ -0,0 +1,74 @@
  736. +
  737. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  738. +#
  739. +# This program is free software; you can redistribute it and/or modify
  740. +# it under the terms of the GNU General Public License as published by
  741. +# the Free Software Foundation; either version 2 of the License, or
  742. +# (at your option) any later version.
  743. +#
  744. +# This program is distributed in the hope that it will be useful,
  745. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  746. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  747. +# GNU General Public License for more details.
  748. +#
  749. +# You should have received a copy of the GNU General Public License
  750. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  751. +
  752. +Branch Manipulation
  753. +
  754. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  755. +and changing its permission/attribute, there are a lot of works to do.
  756. +
  757. +
  758. +Add a Branch
  759. +----------------------------------------------------------------------
  760. +o Confirm the adding dir exists outside of aufs, including loopback
  761. + mount, and its various attributes.
  762. +o Initialize the xino file and whiteout bases if necessary.
  763. + See struct.txt.
  764. +
  765. +o Check the owner/group/mode of the directory
  766. + When the owner/group/mode of the adding directory differs from the
  767. + existing branch, aufs issues a warning because it may impose a
  768. + security risk.
  769. + For example, when a upper writable branch has a world writable empty
  770. + top directory, a malicious user can create any files on the writable
  771. + branch directly, like copy-up and modify manually. If something like
  772. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  773. + writable branch, and the writable branch is world-writable, then a
  774. + malicious guy may create /etc/passwd on the writable branch directly
  775. + and the infected file will be valid in aufs.
  776. + I am afraid it can be a security issue, but aufs can do nothing except
  777. + producing a warning.
  778. +
  779. +
  780. +Delete a Branch
  781. +----------------------------------------------------------------------
  782. +o Confirm the deleting branch is not busy
  783. + To be general, there is one merit to adopt "remount" interface to
  784. + manipulate branches. It is to discard caches. At deleting a branch,
  785. + aufs checks the still cached (and connected) dentries and inodes. If
  786. + there are any, then they are all in-use. An inode without its
  787. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  788. +
  789. + For the cached one, aufs checks whether the same named entry exists on
  790. + other branches.
  791. + If the cached one is a directory, because aufs provides a merged view
  792. + to users, as long as one dir is left on any branch aufs can show the
  793. + dir to users. In this case, the branch can be removed from aufs.
  794. + Otherwise aufs rejects deleting the branch.
  795. +
  796. + If any file on the deleting branch is opened by aufs, then aufs
  797. + rejects deleting.
  798. +
  799. +
  800. +Modify the Permission of a Branch
  801. +----------------------------------------------------------------------
  802. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  803. + See struct.txt.
  804. +
  805. +o rw --> ro: Confirm the modifying branch is not busy
  806. + Aufs rejects the request if any of these conditions are true.
  807. + - a file on the branch is mmap-ed.
  808. + - a regular file on the branch is opened for write and there is no
  809. + same named entry on the upper branch.
  810. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/05wbr_policy.txt linux-4.1.10/Documentation/filesystems/aufs/design/05wbr_policy.txt
  811. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
  812. +++ linux-4.1.10/Documentation/filesystems/aufs/design/05wbr_policy.txt 2015-10-22 21:35:53.000000000 +0200
  813. @@ -0,0 +1,64 @@
  814. +
  815. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  816. +#
  817. +# This program is free software; you can redistribute it and/or modify
  818. +# it under the terms of the GNU General Public License as published by
  819. +# the Free Software Foundation; either version 2 of the License, or
  820. +# (at your option) any later version.
  821. +#
  822. +# This program is distributed in the hope that it will be useful,
  823. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  824. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  825. +# GNU General Public License for more details.
  826. +#
  827. +# You should have received a copy of the GNU General Public License
  828. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  829. +
  830. +Policies to Select One among Multiple Writable Branches
  831. +----------------------------------------------------------------------
  832. +When the number of writable branch is more than one, aufs has to decide
  833. +the target branch for file creation or copy-up. By default, the highest
  834. +writable branch which has the parent (or ancestor) dir of the target
  835. +file is chosen (top-down-parent policy).
  836. +By user's request, aufs implements some other policies to select the
  837. +writable branch, for file creation several policies, round-robin,
  838. +most-free-space, and other policies. For copy-up, top-down-parent,
  839. +bottom-up-parent, bottom-up and others.
  840. +
  841. +As expected, the round-robin policy selects the branch in circular. When
  842. +you have two writable branches and creates 10 new files, 5 files will be
  843. +created for each branch. mkdir(2) systemcall is an exception. When you
  844. +create 10 new directories, all will be created on the same branch.
  845. +And the most-free-space policy selects the one which has most free
  846. +space among the writable branches. The amount of free space will be
  847. +checked by aufs internally, and users can specify its time interval.
  848. +
  849. +The policies for copy-up is more simple,
  850. +top-down-parent is equivalent to the same named on in create policy,
  851. +bottom-up-parent selects the writable branch where the parent dir
  852. +exists and the nearest upper one from the copyup-source,
  853. +bottom-up selects the nearest upper writable branch from the
  854. +copyup-source, regardless the existence of the parent dir.
  855. +
  856. +There are some rules or exceptions to apply these policies.
  857. +- If there is a readonly branch above the policy-selected branch and
  858. + the parent dir is marked as opaque (a variation of whiteout), or the
  859. + target (creating) file is whiteout-ed on the upper readonly branch,
  860. + then the result of the policy is ignored and the target file will be
  861. + created on the nearest upper writable branch than the readonly branch.
  862. +- If there is a writable branch above the policy-selected branch and
  863. + the parent dir is marked as opaque or the target file is whiteouted
  864. + on the branch, then the result of the policy is ignored and the target
  865. + file will be created on the highest one among the upper writable
  866. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  867. + it as usual.
  868. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  869. + They try selecting the branch where the source exists as possible
  870. + since copyup a large file will take long time. If it can't be,
  871. + ie. the branch where the source exists is readonly, then they will
  872. + follow the copyup policy.
  873. +- There is an exception for rename(2) when the target exists.
  874. + If the rename target exists, aufs compares the index of the branches
  875. + where the source and the target exists and selects the higher
  876. + one. If the selected branch is readonly, then aufs follows the
  877. + copyup policy.
  878. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/06fhsm.txt linux-4.1.10/Documentation/filesystems/aufs/design/06fhsm.txt
  879. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
  880. +++ linux-4.1.10/Documentation/filesystems/aufs/design/06fhsm.txt 2015-10-22 21:35:53.000000000 +0200
  881. @@ -0,0 +1,120 @@
  882. +
  883. +# Copyright (C) 2011-2015 Junjiro R. Okajima
  884. +#
  885. +# This program is free software; you can redistribute it and/or modify
  886. +# it under the terms of the GNU General Public License as published by
  887. +# the Free Software Foundation; either version 2 of the License, or
  888. +# (at your option) any later version.
  889. +#
  890. +# This program is distributed in the hope that it will be useful,
  891. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  892. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  893. +# GNU General Public License for more details.
  894. +#
  895. +# You should have received a copy of the GNU General Public License
  896. +# along with this program; if not, write to the Free Software
  897. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  898. +
  899. +
  900. +File-based Hierarchical Storage Management (FHSM)
  901. +----------------------------------------------------------------------
  902. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  903. +storage world. Aufs provides this feature as file-based with multiple
  904. +writable branches, based upon the principle of "Colder, the Lower".
  905. +Here the word "colder" means that the less used files, and "lower" means
  906. +that the position in the order of the stacked branches vertically.
  907. +These multiple writable branches are prioritized, ie. the topmost one
  908. +should be the fastest drive and be used heavily.
  909. +
  910. +o Characters in aufs FHSM story
  911. +- aufs itself and a new branch attribute.
  912. +- a new ioctl interface to move-down and to establish a connection with
  913. + the daemon ("move-down" is a converse of "copy-up").
  914. +- userspace tool and daemon.
  915. +
  916. +The userspace daemon establishes a connection with aufs and waits for
  917. +the notification. The notified information is very similar to struct
  918. +statfs containing the number of consumed blocks and inodes.
  919. +When the consumed blocks/inodes of a branch exceeds the user-specified
  920. +upper watermark, the daemon activates its move-down process until the
  921. +consumed blocks/inodes reaches the user-specified lower watermark.
  922. +
  923. +The actual move-down is done by aufs based upon the request from
  924. +user-space since we need to maintain the inode number and the internal
  925. +pointer arrays in aufs.
  926. +
  927. +Currently aufs FHSM handles the regular files only. Additionally they
  928. +must not be hard-linked nor pseudo-linked.
  929. +
  930. +
  931. +o Cowork of aufs and the user-space daemon
  932. + During the userspace daemon established the connection, aufs sends a
  933. + small notification to it whenever aufs writes something into the
  934. + writable branch. But it may cost high since aufs issues statfs(2)
  935. + internally. So user can specify a new option to cache the
  936. + info. Actually the notification is controlled by these factors.
  937. + + the specified cache time.
  938. + + classified as "force" by aufs internally.
  939. + Until the specified time expires, aufs doesn't send the info
  940. + except the forced cases. When aufs decide forcing, the info is always
  941. + notified to userspace.
  942. + For example, the number of free inodes is generally large enough and
  943. + the shortage of it happens rarely. So aufs doesn't force the
  944. + notification when creating a new file, directory and others. This is
  945. + the typical case which aufs doesn't force.
  946. + When aufs writes the actual filedata and the files consumes any of new
  947. + blocks, the aufs forces notifying.
  948. +
  949. +
  950. +o Interfaces in aufs
  951. +- New branch attribute.
  952. + + fhsm
  953. + Specifies that the branch is managed by FHSM feature. In other word,
  954. + participant in the FHSM.
  955. + When nofhsm is set to the branch, it will not be the source/target
  956. + branch of the move-down operation. This attribute is set
  957. + independently from coo and moo attributes, and if you want full
  958. + FHSM, you should specify them as well.
  959. +- New mount option.
  960. + + fhsm_sec
  961. + Specifies a second to suppress many less important info to be
  962. + notified.
  963. +- New ioctl.
  964. + + AUFS_CTL_FHSM_FD
  965. + create a new file descriptor which userspace can read the notification
  966. + (a subset of struct statfs) from aufs.
  967. +- Module parameter 'brs'
  968. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  969. + be set.
  970. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  971. + When there are two or more branches with fhsm attributes,
  972. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  973. + terminates it. As a result of remounting and branch-manipulation, the
  974. + number of branches with fhsm attribute can be one. In this case,
  975. + /sbin/mount.aufs will terminate the user-space daemon.
  976. +
  977. +
  978. +Finally the operation is done as these steps in kernel-space.
  979. +- make sure that,
  980. + + no one else is using the file.
  981. + + the file is not hard-linked.
  982. + + the file is not pseudo-linked.
  983. + + the file is a regular file.
  984. + + the parent dir is not opaqued.
  985. +- find the target writable branch.
  986. +- make sure the file is not whiteout-ed by the upper (than the target)
  987. + branch.
  988. +- make the parent dir on the target branch.
  989. +- mutex lock the inode on the branch.
  990. +- unlink the whiteout on the target branch (if exists).
  991. +- lookup and create the whiteout-ed temporary name on the target branch.
  992. +- copy the file as the whiteout-ed temporary name on the target branch.
  993. +- rename the whiteout-ed temporary name to the original name.
  994. +- unlink the file on the source branch.
  995. +- maintain the internal pointer array and the external inode number
  996. + table (XINO).
  997. +- maintain the timestamps and other attributes of the parent dir and the
  998. + file.
  999. +
  1000. +And of course, in every step, an error may happen. So the operation
  1001. +should restore the original file state after an error happens.
  1002. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/06mmap.txt linux-4.1.10/Documentation/filesystems/aufs/design/06mmap.txt
  1003. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
  1004. +++ linux-4.1.10/Documentation/filesystems/aufs/design/06mmap.txt 2015-10-22 21:35:53.000000000 +0200
  1005. @@ -0,0 +1,72 @@
  1006. +
  1007. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  1008. +#
  1009. +# This program is free software; you can redistribute it and/or modify
  1010. +# it under the terms of the GNU General Public License as published by
  1011. +# the Free Software Foundation; either version 2 of the License, or
  1012. +# (at your option) any later version.
  1013. +#
  1014. +# This program is distributed in the hope that it will be useful,
  1015. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1016. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1017. +# GNU General Public License for more details.
  1018. +#
  1019. +# You should have received a copy of the GNU General Public License
  1020. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1021. +
  1022. +mmap(2) -- File Memory Mapping
  1023. +----------------------------------------------------------------------
  1024. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1025. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1026. +->mmap().
  1027. +This approach is simple and good, but there is one problem.
  1028. +Under /proc, several entries show the mmapped files by its path (with
  1029. +device and inode number), and the printed path will be the path on the
  1030. +branch fs's instead of virtual aufs's.
  1031. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1032. +user) may expect the path on aufs.
  1033. +
  1034. +To address this issue, aufs adds a new member called vm_prfile in struct
  1035. +vm_area_struct (and struct vm_region). The original vm_file points to
  1036. +the file on the branch fs in order to handle everything correctly as
  1037. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1038. +show-functions in procfs refers to vm_prfile if it is set.
  1039. +Also we need to maintain several other places where touching vm_file
  1040. +such like
  1041. +- fork()/clone() copies vma and the reference count of vm_file is
  1042. + incremented.
  1043. +- merging vma maintains the ref count too.
  1044. +
  1045. +This is not a good approach. It just fakes the printed path. But it
  1046. +leaves all behaviour around f_mapping unchanged. This is surely an
  1047. +advantage.
  1048. +Actually aufs had adopted another complicated approach which calls
  1049. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1050. +approach, aufs met a hard problem and I could not solve it without
  1051. +switching the approach.
  1052. +
  1053. +There may be one more another approach which is
  1054. +- bind-mount the branch-root onto the aufs-root internally
  1055. +- grab the new vfsmount (ie. struct mount)
  1056. +- lazy-umount the branch-root internally
  1057. +- in open(2) the aufs-file, open the branch-file with the hidden
  1058. + vfsmount (instead of the original branch's vfsmount)
  1059. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1060. + but it may be possible from userspace by the mount helper.
  1061. +
  1062. +Adding the internal hidden vfsmount and using it in opening a file, the
  1063. +file path under /proc will be printed correctly. This approach looks
  1064. +smarter, but is not possible I am afraid.
  1065. +- aufs-root may be bind-mount later. when it happens, another hidden
  1066. + vfsmount will be required.
  1067. +- it is hard to get the chance to bind-mount and lazy-umount
  1068. + + in kernel-space, FS can have vfsmount in open(2) via
  1069. + file->f_path, and aufs can know its vfsmount. But several locks are
  1070. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1071. + here, then it may cause a deadlock.
  1072. + + in user-space, bind-mount doesn't invoke the mount helper.
  1073. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1074. + means a new member vm_prinode will be necessary. this is essentially
  1075. + equivalent to vm_prfile described above.
  1076. +
  1077. +I have to give up this "looks-smater" approach.
  1078. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/06xattr.txt linux-4.1.10/Documentation/filesystems/aufs/design/06xattr.txt
  1079. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
  1080. +++ linux-4.1.10/Documentation/filesystems/aufs/design/06xattr.txt 2015-10-22 21:35:53.000000000 +0200
  1081. @@ -0,0 +1,96 @@
  1082. +
  1083. +# Copyright (C) 2014-2015 Junjiro R. Okajima
  1084. +#
  1085. +# This program is free software; you can redistribute it and/or modify
  1086. +# it under the terms of the GNU General Public License as published by
  1087. +# the Free Software Foundation; either version 2 of the License, or
  1088. +# (at your option) any later version.
  1089. +#
  1090. +# This program is distributed in the hope that it will be useful,
  1091. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1092. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1093. +# GNU General Public License for more details.
  1094. +#
  1095. +# You should have received a copy of the GNU General Public License
  1096. +# along with this program; if not, write to the Free Software
  1097. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1098. +
  1099. +
  1100. +Listing XATTR/EA and getting the value
  1101. +----------------------------------------------------------------------
  1102. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1103. +shows the values from the topmost existing file. This behaviour is good
  1104. +for the non-dir entries since the bahaviour exactly matches the shown
  1105. +information. But for the directories, aufs considers all the same named
  1106. +entries on the lower branches. Which means, if one of the lower entry
  1107. +rejects readdir call, then aufs returns an error even if the topmost
  1108. +entry allows it. This behaviour is necessary to respect the branch fs's
  1109. +security, but can make users confused since the user-visible standard
  1110. +attributes don't match the behaviour.
  1111. +To address this issue, aufs has a mount option called dirperm1 which
  1112. +checks the permission for the topmost entry only, and ignores the lower
  1113. +entry's permission.
  1114. +
  1115. +A similar issue can happen around XATTR.
  1116. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1117. +always set. Otherwise these very unpleasant situation would happen.
  1118. +- listxattr(2) may return the duplicated entries.
  1119. +- users may not be able to remove or reset the XATTR forever,
  1120. +
  1121. +
  1122. +XATTR/EA support in the internal (copy,move)-(up,down)
  1123. +----------------------------------------------------------------------
  1124. +Generally the extended attributes of inode are categorized as these.
  1125. +- "security" for LSM and capability.
  1126. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1127. + fs generally.
  1128. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1129. +- "user" for userspace, 'user_xattr' mount option is required for the
  1130. + branch fs generally.
  1131. +
  1132. +Moreover there are some other categories. Aufs handles these rather
  1133. +unpopular categories as the ordinary ones, ie. there is no special
  1134. +condition nor exception.
  1135. +
  1136. +In copy-up, the support for XATTR on the dst branch may differ from the
  1137. +src branch. In this case, the copy-up operation will get an error and
  1138. +the original user operation which triggered the copy-up will fail. It
  1139. +can happen that even all copy-up will fail.
  1140. +When both of src and dst branches support XATTR and if an error occurs
  1141. +during copying XATTR, then the copy-up should fail obviously. That is a
  1142. +good reason and aufs should return an error to userspace. But when only
  1143. +the src branch support that XATTR, aufs should not return an error.
  1144. +For example, the src branch supports ACL but the dst branch doesn't
  1145. +because the dst branch may natively un-support it or temporary
  1146. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1147. +may NOT return an error even if the XATTR is not supported. It is
  1148. +totally up to the branch fs.
  1149. +
  1150. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1151. +fs, then aufs tries removing the just copied entry and returns the error
  1152. +to the userspace. The worst case of this situation will be all copy-up
  1153. +will fail.
  1154. +
  1155. +For the copy-up operation, there two basic approaches.
  1156. +- copy the specified XATTR only (by category above), and return the
  1157. + error unconditionally if it happens.
  1158. +- copy all XATTR, and ignore the error on the specified category only.
  1159. +
  1160. +In order to support XATTR and to implement the correct behaviour, aufs
  1161. +chooses the latter approach and introduces some new branch attributes,
  1162. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1163. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1164. +convenient, "icex" is also provided which means all "icex*" attributes
  1165. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1166. +
  1167. +The meaning of these attributes is to ignore the error from setting
  1168. +XATTR on that branch.
  1169. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1170. +error from the dst branch according to the specified attributes.
  1171. +
  1172. +Some XATTR may have its default value. The default value may come from
  1173. +the parent dir or the environment. If the default value is set at the
  1174. +file creating-time, it will be overwritten by copy-up.
  1175. +Some contradiction may happen I am afraid.
  1176. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1177. +now, aufs implements the branch attributes to ignore the error.
  1178. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/07export.txt linux-4.1.10/Documentation/filesystems/aufs/design/07export.txt
  1179. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
  1180. +++ linux-4.1.10/Documentation/filesystems/aufs/design/07export.txt 2015-10-22 21:35:53.000000000 +0200
  1181. @@ -0,0 +1,58 @@
  1182. +
  1183. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  1184. +#
  1185. +# This program is free software; you can redistribute it and/or modify
  1186. +# it under the terms of the GNU General Public License as published by
  1187. +# the Free Software Foundation; either version 2 of the License, or
  1188. +# (at your option) any later version.
  1189. +#
  1190. +# This program is distributed in the hope that it will be useful,
  1191. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1192. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1193. +# GNU General Public License for more details.
  1194. +#
  1195. +# You should have received a copy of the GNU General Public License
  1196. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1197. +
  1198. +Export Aufs via NFS
  1199. +----------------------------------------------------------------------
  1200. +Here is an approach.
  1201. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1202. + generation.
  1203. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1204. + store it in xigen file.
  1205. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1206. + file. it is necessary even if it is not unlinked, because any data of
  1207. + inode may be changed by UDBA.
  1208. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1209. + build file handle by
  1210. + + branch id (4 bytes)
  1211. + + superblock generation (4 bytes)
  1212. + + inode number (4 or 8 bytes)
  1213. + + parent dir inode number (4 or 8 bytes)
  1214. + + inode generation (4 bytes))
  1215. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1216. + bytes)
  1217. + + file handle for a branch (by exportfs_encode_fh())
  1218. +- fh_to_dentry():
  1219. + + find the index of a branch from its id in handle, and check it is
  1220. + still exist in aufs.
  1221. + + 1st level: get the inode number from handle and search it in cache.
  1222. + + 2nd level: if not found in cache, get the parent inode number from
  1223. + the handle and search it in cache. and then open the found parent
  1224. + dir, find the matching inode number by vfs_readdir() and get its
  1225. + name, and call lookup_one_len() for the target dentry.
  1226. + + 3rd level: if the parent dir is not cached, call
  1227. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1228. + build a pathname of it, convert it a pathname in aufs, call
  1229. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1230. + the 2nd level.
  1231. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1232. + for every branch, but not itself. to get this, (currently) aufs
  1233. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1234. + idea, but I didn't get other approach.
  1235. + + test the generation of the gotten inode.
  1236. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1237. + convert it into ESTALE for NFSD.
  1238. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1239. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1240. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/08shwh.txt linux-4.1.10/Documentation/filesystems/aufs/design/08shwh.txt
  1241. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
  1242. +++ linux-4.1.10/Documentation/filesystems/aufs/design/08shwh.txt 2015-10-22 21:35:53.000000000 +0200
  1243. @@ -0,0 +1,52 @@
  1244. +
  1245. +# Copyright (C) 2005-2015 Junjiro R. Okajima
  1246. +#
  1247. +# This program is free software; you can redistribute it and/or modify
  1248. +# it under the terms of the GNU General Public License as published by
  1249. +# the Free Software Foundation; either version 2 of the License, or
  1250. +# (at your option) any later version.
  1251. +#
  1252. +# This program is distributed in the hope that it will be useful,
  1253. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1254. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1255. +# GNU General Public License for more details.
  1256. +#
  1257. +# You should have received a copy of the GNU General Public License
  1258. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1259. +
  1260. +Show Whiteout Mode (shwh)
  1261. +----------------------------------------------------------------------
  1262. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1263. +them is very useful for users. For instance, creating a new middle layer
  1264. +(branch) by merging existing layers.
  1265. +
  1266. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1267. +When you have three branches,
  1268. +- Bottom: 'system', squashfs (underlying base system), read-only
  1269. +- Middle: 'mods', squashfs, read-only
  1270. +- Top: 'overlay', ram (tmpfs), read-write
  1271. +
  1272. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1273. +the changes made to the system during the session.
  1274. +When larger changes have been made, or smaller changes have accumulated,
  1275. +the size of the saved top layer data grows. At this point, it would be
  1276. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1277. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1278. +restoring save and load speed.
  1279. +
  1280. +This merging is simplified by the use of another aufs mount, of just the
  1281. +two overlay branches using the 'shwh' option.
  1282. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1283. + aufs /livesys/merge_union
  1284. +
  1285. +A merged view of these two branches is then available at
  1286. +/livesys/merge_union, and the new feature is that the whiteouts are
  1287. +visible!
  1288. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1289. +writing to all branches. Also the default mode for all branches is 'ro'.
  1290. +It is now possible to save the combined contents of the two overlay
  1291. +branches to a new squashfs, e.g.:
  1292. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1293. +
  1294. +This new squashfs archive can be stored on the boot device and the
  1295. +initramfs will use it to replace the old one at the next boot.
  1296. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/design/10dynop.txt linux-4.1.10/Documentation/filesystems/aufs/design/10dynop.txt
  1297. --- linux-4.1.10.orig/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
  1298. +++ linux-4.1.10/Documentation/filesystems/aufs/design/10dynop.txt 2015-10-22 21:35:53.000000000 +0200
  1299. @@ -0,0 +1,47 @@
  1300. +
  1301. +# Copyright (C) 2010-2015 Junjiro R. Okajima
  1302. +#
  1303. +# This program is free software; you can redistribute it and/or modify
  1304. +# it under the terms of the GNU General Public License as published by
  1305. +# the Free Software Foundation; either version 2 of the License, or
  1306. +# (at your option) any later version.
  1307. +#
  1308. +# This program is distributed in the hope that it will be useful,
  1309. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1310. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1311. +# GNU General Public License for more details.
  1312. +#
  1313. +# You should have received a copy of the GNU General Public License
  1314. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1315. +
  1316. +Dynamically customizable FS operations
  1317. +----------------------------------------------------------------------
  1318. +Generally FS operations (struct inode_operations, struct
  1319. +address_space_operations, struct file_operations, etc.) are defined as
  1320. +"static const", but it never means that FS have only one set of
  1321. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1322. +three sets, one for XIP, for NOBH, and for normal.
  1323. +Since aufs overrides and redirects these operations, sometimes aufs has
  1324. +to change its behaviour according to the branch FS type. More importantly
  1325. +VFS acts differently if a function (member in the struct) is set or
  1326. +not. It means aufs should have several sets of operations and select one
  1327. +among them according to the branch FS definition.
  1328. +
  1329. +In order to solve this problem and not to affect the behaviour of VFS,
  1330. +aufs defines these operations dynamically. For instance, aufs defines
  1331. +dummy direct_IO function for struct address_space_operations, but it may
  1332. +not be set to the address_space_operations actually. When the branch FS
  1333. +doesn't have it, aufs doesn't set it to its address_space_operations
  1334. +while the function definition itself is still alive. So the behaviour
  1335. +itself will not change, and it will return an error when direct_IO is
  1336. +not set.
  1337. +
  1338. +The lifetime of these dynamically generated operation object is
  1339. +maintained by aufs branch object. When the branch is removed from aufs,
  1340. +the reference counter of the object is decremented. When it reaches
  1341. +zero, the dynamically generated operation object will be freed.
  1342. +
  1343. +This approach is designed to support AIO (io_submit), Direct I/O and
  1344. +XIP (DAX) mainly.
  1345. +Currently this approach is applied to address_space_operations for
  1346. +regular files only.
  1347. diff -Nur linux-4.1.10.orig/Documentation/filesystems/aufs/README linux-4.1.10/Documentation/filesystems/aufs/README
  1348. --- linux-4.1.10.orig/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
  1349. +++ linux-4.1.10/Documentation/filesystems/aufs/README 2015-10-22 21:35:53.000000000 +0200
  1350. @@ -0,0 +1,383 @@
  1351. +
  1352. +Aufs4 -- advanced multi layered unification filesystem version 4.x
  1353. +http://aufs.sf.net
  1354. +Junjiro R. Okajima
  1355. +
  1356. +
  1357. +0. Introduction
  1358. +----------------------------------------
  1359. +In the early days, aufs was entirely re-designed and re-implemented
  1360. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1361. +improvements and implementations, it becomes totally different from
  1362. +Unionfs while keeping the basic features.
  1363. +Recently, Unionfs Version 2.x series begin taking some of the same
  1364. +approaches to aufs1's.
  1365. +Unionfs is being developed by Professor Erez Zadok at Stony Brook
  1366. +University and his team.
  1367. +
  1368. +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
  1369. +If you want older kernel version support, try aufs2-2.6.git or
  1370. +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
  1371. +
  1372. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1373. + According to Christoph Hellwig, linux rejects all union-type
  1374. + filesystems but UnionMount.
  1375. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1376. +
  1377. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1378. + UnionMount, and he pointed out an issue around a directory mutex
  1379. + lock and aufs addressed it. But it is still unsure whether aufs will
  1380. + be merged (or any other union solution).
  1381. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1382. +
  1383. +
  1384. +1. Features
  1385. +----------------------------------------
  1386. +- unite several directories into a single virtual filesystem. The member
  1387. + directory is called as a branch.
  1388. +- you can specify the permission flags to the branch, which are 'readonly',
  1389. + 'readwrite' and 'whiteout-able.'
  1390. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1391. + readonly branch are modifiable logically.
  1392. +- dynamic branch manipulation, add, del.
  1393. +- etc...
  1394. +
  1395. +Also there are many enhancements in aufs, such as:
  1396. +- test only the highest one for the directory permission (dirperm1)
  1397. +- copyup on open (coo=)
  1398. +- 'move' policy for copy-up between two writable branches, after
  1399. + checking free space.
  1400. +- xattr, acl
  1401. +- readdir(3) in userspace.
  1402. +- keep inode number by external inode number table
  1403. +- keep the timestamps of file/dir in internal copyup operation
  1404. +- seekable directory, supporting NFS readdir.
  1405. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1406. + on branch
  1407. +- do not copyup, nor create a whiteout when it is unnecessary
  1408. +- revert a single systemcall when an error occurs in aufs
  1409. +- remount interface instead of ioctl
  1410. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1411. +- loopback mounted filesystem as a branch
  1412. +- kernel thread for removing the dir who has a plenty of whiteouts
  1413. +- support copyup sparse file (a file which has a 'hole' in it)
  1414. +- default permission flags for branches
  1415. +- selectable permission flags for ro branch, whether whiteout can
  1416. + exist or not
  1417. +- export via NFS.
  1418. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1419. +- support multiple writable branches, some policies to select one
  1420. + among multiple writable branches.
  1421. +- a new semantics for link(2) and rename(2) to support multiple
  1422. + writable branches.
  1423. +- no glibc changes are required.
  1424. +- pseudo hardlink (hardlink over branches)
  1425. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1426. + including NFS or remote filesystem branch.
  1427. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1428. +- and more...
  1429. +
  1430. +Currently these features are dropped temporary from aufs4.
  1431. +See design/08plan.txt in detail.
  1432. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1433. + (robr)
  1434. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1435. +
  1436. +Features or just an idea in the future (see also design/*.txt),
  1437. +- reorder the branch index without del/re-add.
  1438. +- permanent xino files for NFSD
  1439. +- an option for refreshing the opened files after add/del branches
  1440. +- light version, without branch manipulation. (unnecessary?)
  1441. +- copyup in userspace
  1442. +- inotify in userspace
  1443. +- readv/writev
  1444. +
  1445. +
  1446. +2. Download
  1447. +----------------------------------------
  1448. +There are three GIT trees for aufs4, aufs4-linux.git,
  1449. +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
  1450. +"aufs-util.git."
  1451. +While the aufs-util is always necessary, you need either of aufs4-linux
  1452. +or aufs4-standalone.
  1453. +
  1454. +The aufs4-linux tree includes the whole linux mainline GIT tree,
  1455. +git://git.kernel.org/.../torvalds/linux.git.
  1456. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1457. +build aufs4 as an external kernel module.
  1458. +Several extra patches are not included in this tree. Only
  1459. +aufs4-standalone tree contains them. They are describe in the later
  1460. +section "Configuration and Compilation."
  1461. +
  1462. +On the other hand, the aufs4-standalone tree has only aufs source files
  1463. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1464. +But you need to apply all aufs patches manually.
  1465. +
  1466. +You will find GIT branches whose name is in form of "aufs4.x" where "x"
  1467. +represents the linux kernel version, "linux-4.x". For instance,
  1468. +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
  1469. +"aufs4.x-rcN" branch.
  1470. +
  1471. +o aufs4-linux tree
  1472. +$ git clone --reference /your/linux/git/tree \
  1473. + git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
  1474. +- if you don't have linux GIT tree, then remove "--reference ..."
  1475. +$ cd aufs4-linux.git
  1476. +$ git checkout origin/aufs4.0
  1477. +
  1478. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1479. +leave the patch-work to GIT.
  1480. +$ cd /your/linux/git/tree
  1481. +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
  1482. +$ git fetch aufs4
  1483. +$ git checkout -b my4.0 v4.0
  1484. +$ (add your local change...)
  1485. +$ git pull aufs4 aufs4.0
  1486. +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
  1487. +- you may need to solve some conflicts between your_changes and
  1488. + aufs4.0. in this case, git-rerere is recommended so that you can
  1489. + solve the similar conflicts automatically when you upgrade to 4.1 or
  1490. + later in the future.
  1491. +
  1492. +o aufs4-standalone tree
  1493. +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
  1494. +$ cd aufs4-standalone.git
  1495. +$ git checkout origin/aufs4.0
  1496. +
  1497. +o aufs-util tree
  1498. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1499. +- note that the public aufs-util.git is on SourceForge instead of
  1500. + GitHUB.
  1501. +$ cd aufs-util.git
  1502. +$ git checkout origin/aufs4.0
  1503. +
  1504. +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1505. +The minor version number, 'x' in '4.x', of aufs may not always
  1506. +follow the minor version number of the kernel.
  1507. +Because changes in the kernel that cause the use of a new
  1508. +minor version number do not always require changes to aufs-util.
  1509. +
  1510. +Since aufs-util has its own minor version number, you may not be
  1511. +able to find a GIT branch in aufs-util for your kernel's
  1512. +exact minor version number.
  1513. +In this case, you should git-checkout the branch for the
  1514. +nearest lower number.
  1515. +
  1516. +For (an unreleased) example:
  1517. +If you are using "linux-4.10" and the "aufs4.10" branch
  1518. +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
  1519. +or something numerically smaller is the branch for your kernel.
  1520. +
  1521. +Also you can view all branches by
  1522. + $ git branch -a
  1523. +
  1524. +
  1525. +3. Configuration and Compilation
  1526. +----------------------------------------
  1527. +Make sure you have git-checkout'ed the correct branch.
  1528. +
  1529. +For aufs4-linux tree,
  1530. +- enable CONFIG_AUFS_FS.
  1531. +- set other aufs configurations if necessary.
  1532. +
  1533. +For aufs4-standalone tree,
  1534. +There are several ways to build.
  1535. +
  1536. +1.
  1537. +- apply ./aufs4-kbuild.patch to your kernel source files.
  1538. +- apply ./aufs4-base.patch too.
  1539. +- apply ./aufs4-mmap.patch too.
  1540. +- apply ./aufs4-standalone.patch too, if you have a plan to set
  1541. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
  1542. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1543. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1544. +- enable CONFIG_AUFS_FS, you can select either
  1545. + =m or =y.
  1546. +- and build your kernel as usual.
  1547. +- install the built kernel.
  1548. + Note: Since linux-3.9, every filesystem module requires an alias
  1549. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1550. + modules.aliases file if you set CONFIG_AUFS_FS=m.
  1551. +- install the header files too by "make headers_install" to the
  1552. + directory where you specify. By default, it is $PWD/usr.
  1553. + "make help" shows a brief note for headers_install.
  1554. +- and reboot your system.
  1555. +
  1556. +2.
  1557. +- module only (CONFIG_AUFS_FS=m).
  1558. +- apply ./aufs4-base.patch to your kernel source files.
  1559. +- apply ./aufs4-mmap.patch too.
  1560. +- apply ./aufs4-standalone.patch too.
  1561. +- build your kernel, don't forget "make headers_install", and reboot.
  1562. +- edit ./config.mk and set other aufs configurations if necessary.
  1563. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1564. + every aufs configurations.
  1565. +- build the module by simple "make".
  1566. + Note: Since linux-3.9, every filesystem module requires an alias
  1567. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1568. + modules.aliases file.
  1569. +- you can specify ${KDIR} make variable which points to your kernel
  1570. + source tree.
  1571. +- install the files
  1572. + + run "make install" to install the aufs module, or copy the built
  1573. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1574. + + run "make install_headers" (instead of headers_install) to install
  1575. + the modified aufs header file (you can specify DESTDIR which is
  1576. + available in aufs standalone version's Makefile only), or copy
  1577. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1578. + you like manually. By default, the target directory is $PWD/usr.
  1579. +- no need to apply aufs4-kbuild.patch, nor copying source files to your
  1580. + kernel source tree.
  1581. +
  1582. +Note: The header file aufs_type.h is necessary to build aufs-util
  1583. + as well as "make headers_install" in the kernel source tree.
  1584. + headers_install is subject to be forgotten, but it is essentially
  1585. + necessary, not only for building aufs-util.
  1586. + You may not meet problems without headers_install in some older
  1587. + version though.
  1588. +
  1589. +And then,
  1590. +- read README in aufs-util, build and install it
  1591. +- note that your distribution may contain an obsoleted version of
  1592. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1593. + utilities, make sure that your compiler refers the correct aufs header
  1594. + file which is built by "make headers_install."
  1595. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1596. + then run "make install_ulib" too. And refer to the aufs manual in
  1597. + detail.
  1598. +
  1599. +There several other patches in aufs4-standalone.git. They are all
  1600. +optional. When you meet some problems, they will help you.
  1601. +- aufs4-loopback.patch
  1602. + Supports a nested loopback mount in a branch-fs. This patch is
  1603. + unnecessary until aufs produces a message like "you may want to try
  1604. + another patch for loopback file".
  1605. +- vfs-ino.patch
  1606. + Modifies a system global kernel internal function get_next_ino() in
  1607. + order to stop assigning 0 for an inode-number. Not directly related to
  1608. + aufs, but recommended generally.
  1609. +- tmpfs-idr.patch
  1610. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1611. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1612. + duplication of inode number, which is important for backup tools and
  1613. + other utilities. When you find aufs XINO files for tmpfs branch
  1614. + growing too much, try this patch.
  1615. +
  1616. +
  1617. +4. Usage
  1618. +----------------------------------------
  1619. +At first, make sure aufs-util are installed, and please read the aufs
  1620. +manual, aufs.5 in aufs-util.git tree.
  1621. +$ man -l aufs.5
  1622. +
  1623. +And then,
  1624. +$ mkdir /tmp/rw /tmp/aufs
  1625. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1626. +
  1627. +Here is another example. The result is equivalent.
  1628. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1629. + Or
  1630. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1631. +# mount -o remount,append:${HOME} /tmp/aufs
  1632. +
  1633. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1634. +you modify a file under /tmp/aufs, the one on your home directory is
  1635. +not affected, instead the same named file will be newly created under
  1636. +/tmp/rw. And all of your modification to a file will be applied to
  1637. +the one under /tmp/rw. This is called the file based Copy on Write
  1638. +(COW) method.
  1639. +Aufs mount options are described in aufs.5.
  1640. +If you run chroot or something and make your aufs as a root directory,
  1641. +then you need to customize the shutdown script. See the aufs manual in
  1642. +detail.
  1643. +
  1644. +Additionally, there are some sample usages of aufs which are a
  1645. +diskless system with network booting, and LiveCD over NFS.
  1646. +See sample dir in CVS tree on SourceForge.
  1647. +
  1648. +
  1649. +5. Contact
  1650. +----------------------------------------
  1651. +When you have any problems or strange behaviour in aufs, please let me
  1652. +know with:
  1653. +- /proc/mounts (instead of the output of mount(8))
  1654. +- /sys/module/aufs/*
  1655. +- /sys/fs/aufs/* (if you have them)
  1656. +- /debug/aufs/* (if you have them)
  1657. +- linux kernel version
  1658. + if your kernel is not plain, for example modified by distributor,
  1659. + the url where i can download its source is necessary too.
  1660. +- aufs version which was printed at loading the module or booting the
  1661. + system, instead of the date you downloaded.
  1662. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1663. +- kernel configuration or /proc/config.gz (if you have it)
  1664. +- behaviour which you think to be incorrect
  1665. +- actual operation, reproducible one is better
  1666. +- mailto: aufs-users at lists.sourceforge.net
  1667. +
  1668. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1669. +and Feature Requests) on SourceForge. Please join and write to
  1670. +aufs-users ML.
  1671. +
  1672. +
  1673. +6. Acknowledgements
  1674. +----------------------------------------
  1675. +Thanks to everyone who have tried and are using aufs, whoever
  1676. +have reported a bug or any feedback.
  1677. +
  1678. +Especially donators:
  1679. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1680. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1681. + scripts) is making "doubling" donations.
  1682. + Unfortunately I cannot list all of the donators, but I really
  1683. + appreciate.
  1684. + It ends Aug 2010, but the ordinary donation URL is still available.
  1685. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1686. +Dai Itasaka made a donation (2007/8).
  1687. +Chuck Smith made a donation (2008/4, 10 and 12).
  1688. +Henk Schoneveld made a donation (2008/9).
  1689. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1690. +Francois Dupoux made a donation (2008/11).
  1691. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1692. + aufs2 GIT tree (2009/2).
  1693. +William Grant made a donation (2009/3).
  1694. +Patrick Lane made a donation (2009/4).
  1695. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1696. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1697. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1698. +Pavel Pronskiy made a donation (2011/2).
  1699. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1700. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1701. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1702. +11).
  1703. +Sam Liddicott made a donation (2011/9).
  1704. +Era Scarecrow made a donation (2013/4).
  1705. +Bor Ratajc made a donation (2013/4).
  1706. +Alessandro Gorreta made a donation (2013/4).
  1707. +POIRETTE Marc made a donation (2013/4).
  1708. +Alessandro Gorreta made a donation (2013/4).
  1709. +lauri kasvandik made a donation (2013/5).
  1710. +"pemasu from Finland" made a donation (2013/7).
  1711. +The Parted Magic Project made a donation (2013/9 and 11).
  1712. +Pavel Barta made a donation (2013/10).
  1713. +Nikolay Pertsev made a donation (2014/5).
  1714. +James B made a donation (2014/7 and 2015/7).
  1715. +Stefano Di Biase made a donation (2014/8).
  1716. +Daniel Epellei made a donation (2015/1).
  1717. +
  1718. +Thank you very much.
  1719. +Donations are always, including future donations, very important and
  1720. +helpful for me to keep on developing aufs.
  1721. +
  1722. +
  1723. +7.
  1724. +----------------------------------------
  1725. +If you are an experienced user, no explanation is needed. Aufs is
  1726. +just a linux filesystem.
  1727. +
  1728. +
  1729. +Enjoy!
  1730. +
  1731. +# Local variables: ;
  1732. +# mode: text;
  1733. +# End: ;
  1734. diff -Nur linux-4.1.10.orig/drivers/block/loop.c linux-4.1.10/drivers/block/loop.c
  1735. --- linux-4.1.10.orig/drivers/block/loop.c 2015-10-03 13:49:38.000000000 +0200
  1736. +++ linux-4.1.10/drivers/block/loop.c 2015-10-22 21:35:53.000000000 +0200
  1737. @@ -538,6 +538,24 @@
  1738. return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
  1739. }
  1740. +/*
  1741. + * for AUFS
  1742. + * no get/put for file.
  1743. + */
  1744. +struct file *loop_backing_file(struct super_block *sb)
  1745. +{
  1746. + struct file *ret;
  1747. + struct loop_device *l;
  1748. +
  1749. + ret = NULL;
  1750. + if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
  1751. + l = sb->s_bdev->bd_disk->private_data;
  1752. + ret = l->lo_backing_file;
  1753. + }
  1754. + return ret;
  1755. +}
  1756. +EXPORT_SYMBOL(loop_backing_file);
  1757. +
  1758. /* loop sysfs attributes */
  1759. static ssize_t loop_attr_show(struct device *dev, char *page,
  1760. diff -Nur linux-4.1.10.orig/fs/aufs/aufs.h linux-4.1.10/fs/aufs/aufs.h
  1761. --- linux-4.1.10.orig/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
  1762. +++ linux-4.1.10/fs/aufs/aufs.h 2015-10-22 21:35:53.000000000 +0200
  1763. @@ -0,0 +1,59 @@
  1764. +/*
  1765. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  1766. + *
  1767. + * This program, aufs is free software; you can redistribute it and/or modify
  1768. + * it under the terms of the GNU General Public License as published by
  1769. + * the Free Software Foundation; either version 2 of the License, or
  1770. + * (at your option) any later version.
  1771. + *
  1772. + * This program is distributed in the hope that it will be useful,
  1773. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1774. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1775. + * GNU General Public License for more details.
  1776. + *
  1777. + * You should have received a copy of the GNU General Public License
  1778. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1779. + */
  1780. +
  1781. +/*
  1782. + * all header files
  1783. + */
  1784. +
  1785. +#ifndef __AUFS_H__
  1786. +#define __AUFS_H__
  1787. +
  1788. +#ifdef __KERNEL__
  1789. +
  1790. +#define AuStub(type, name, body, ...) \
  1791. + static inline type name(__VA_ARGS__) { body; }
  1792. +
  1793. +#define AuStubVoid(name, ...) \
  1794. + AuStub(void, name, , __VA_ARGS__)
  1795. +#define AuStubInt0(name, ...) \
  1796. + AuStub(int, name, return 0, __VA_ARGS__)
  1797. +
  1798. +#include "debug.h"
  1799. +
  1800. +#include "branch.h"
  1801. +#include "cpup.h"
  1802. +#include "dcsub.h"
  1803. +#include "dbgaufs.h"
  1804. +#include "dentry.h"
  1805. +#include "dir.h"
  1806. +#include "dynop.h"
  1807. +#include "file.h"
  1808. +#include "fstype.h"
  1809. +#include "inode.h"
  1810. +#include "loop.h"
  1811. +#include "module.h"
  1812. +#include "opts.h"
  1813. +#include "rwsem.h"
  1814. +#include "spl.h"
  1815. +#include "super.h"
  1816. +#include "sysaufs.h"
  1817. +#include "vfsub.h"
  1818. +#include "whout.h"
  1819. +#include "wkq.h"
  1820. +
  1821. +#endif /* __KERNEL__ */
  1822. +#endif /* __AUFS_H__ */
  1823. diff -Nur linux-4.1.10.orig/fs/aufs/branch.c linux-4.1.10/fs/aufs/branch.c
  1824. --- linux-4.1.10.orig/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
  1825. +++ linux-4.1.10/fs/aufs/branch.c 2015-10-22 21:35:53.000000000 +0200
  1826. @@ -0,0 +1,1414 @@
  1827. +/*
  1828. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  1829. + *
  1830. + * This program, aufs is free software; you can redistribute it and/or modify
  1831. + * it under the terms of the GNU General Public License as published by
  1832. + * the Free Software Foundation; either version 2 of the License, or
  1833. + * (at your option) any later version.
  1834. + *
  1835. + * This program is distributed in the hope that it will be useful,
  1836. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1837. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1838. + * GNU General Public License for more details.
  1839. + *
  1840. + * You should have received a copy of the GNU General Public License
  1841. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1842. + */
  1843. +
  1844. +/*
  1845. + * branch management
  1846. + */
  1847. +
  1848. +#include <linux/compat.h>
  1849. +#include <linux/statfs.h>
  1850. +#include "aufs.h"
  1851. +
  1852. +/*
  1853. + * free a single branch
  1854. + */
  1855. +static void au_br_do_free(struct au_branch *br)
  1856. +{
  1857. + int i;
  1858. + struct au_wbr *wbr;
  1859. + struct au_dykey **key;
  1860. +
  1861. + au_hnotify_fin_br(br);
  1862. +
  1863. + if (br->br_xino.xi_file)
  1864. + fput(br->br_xino.xi_file);
  1865. + mutex_destroy(&br->br_xino.xi_nondir_mtx);
  1866. +
  1867. + AuDebugOn(atomic_read(&br->br_count));
  1868. +
  1869. + wbr = br->br_wbr;
  1870. + if (wbr) {
  1871. + for (i = 0; i < AuBrWh_Last; i++)
  1872. + dput(wbr->wbr_wh[i]);
  1873. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  1874. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  1875. + }
  1876. +
  1877. + if (br->br_fhsm) {
  1878. + au_br_fhsm_fin(br->br_fhsm);
  1879. + kfree(br->br_fhsm);
  1880. + }
  1881. +
  1882. + key = br->br_dykey;
  1883. + for (i = 0; i < AuBrDynOp; i++, key++)
  1884. + if (*key)
  1885. + au_dy_put(*key);
  1886. + else
  1887. + break;
  1888. +
  1889. + /* recursive lock, s_umount of branch's */
  1890. + lockdep_off();
  1891. + path_put(&br->br_path);
  1892. + lockdep_on();
  1893. + kfree(wbr);
  1894. + kfree(br);
  1895. +}
  1896. +
  1897. +/*
  1898. + * frees all branches
  1899. + */
  1900. +void au_br_free(struct au_sbinfo *sbinfo)
  1901. +{
  1902. + aufs_bindex_t bmax;
  1903. + struct au_branch **br;
  1904. +
  1905. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  1906. +
  1907. + bmax = sbinfo->si_bend + 1;
  1908. + br = sbinfo->si_branch;
  1909. + while (bmax--)
  1910. + au_br_do_free(*br++);
  1911. +}
  1912. +
  1913. +/*
  1914. + * find the index of a branch which is specified by @br_id.
  1915. + */
  1916. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  1917. +{
  1918. + aufs_bindex_t bindex, bend;
  1919. +
  1920. + bend = au_sbend(sb);
  1921. + for (bindex = 0; bindex <= bend; bindex++)
  1922. + if (au_sbr_id(sb, bindex) == br_id)
  1923. + return bindex;
  1924. + return -1;
  1925. +}
  1926. +
  1927. +/* ---------------------------------------------------------------------- */
  1928. +
  1929. +/*
  1930. + * add a branch
  1931. + */
  1932. +
  1933. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  1934. + struct dentry *h_root)
  1935. +{
  1936. + if (unlikely(h_adding == h_root
  1937. + || au_test_loopback_overlap(sb, h_adding)))
  1938. + return 1;
  1939. + if (h_adding->d_sb != h_root->d_sb)
  1940. + return 0;
  1941. + return au_test_subdir(h_adding, h_root)
  1942. + || au_test_subdir(h_root, h_adding);
  1943. +}
  1944. +
  1945. +/*
  1946. + * returns a newly allocated branch. @new_nbranch is a number of branches
  1947. + * after adding a branch.
  1948. + */
  1949. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  1950. + int perm)
  1951. +{
  1952. + struct au_branch *add_branch;
  1953. + struct dentry *root;
  1954. + struct inode *inode;
  1955. + int err;
  1956. +
  1957. + err = -ENOMEM;
  1958. + root = sb->s_root;
  1959. + add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
  1960. + if (unlikely(!add_branch))
  1961. + goto out;
  1962. +
  1963. + err = au_hnotify_init_br(add_branch, perm);
  1964. + if (unlikely(err))
  1965. + goto out_br;
  1966. +
  1967. + add_branch->br_wbr = NULL;
  1968. + if (au_br_writable(perm)) {
  1969. + /* may be freed separately at changing the branch permission */
  1970. + add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
  1971. + GFP_NOFS);
  1972. + if (unlikely(!add_branch->br_wbr))
  1973. + goto out_hnotify;
  1974. + }
  1975. +
  1976. + add_branch->br_fhsm = NULL;
  1977. + if (au_br_fhsm(perm)) {
  1978. + err = au_fhsm_br_alloc(add_branch);
  1979. + if (unlikely(err))
  1980. + goto out_wbr;
  1981. + }
  1982. +
  1983. + err = au_sbr_realloc(au_sbi(sb), new_nbranch);
  1984. + if (!err)
  1985. + err = au_di_realloc(au_di(root), new_nbranch);
  1986. + if (!err) {
  1987. + inode = d_inode(root);
  1988. + err = au_ii_realloc(au_ii(inode), new_nbranch);
  1989. + }
  1990. + if (!err)
  1991. + return add_branch; /* success */
  1992. +
  1993. +out_wbr:
  1994. + kfree(add_branch->br_wbr);
  1995. +out_hnotify:
  1996. + au_hnotify_fin_br(add_branch);
  1997. +out_br:
  1998. + kfree(add_branch);
  1999. +out:
  2000. + return ERR_PTR(err);
  2001. +}
  2002. +
  2003. +/*
  2004. + * test if the branch permission is legal or not.
  2005. + */
  2006. +static int test_br(struct inode *inode, int brperm, char *path)
  2007. +{
  2008. + int err;
  2009. +
  2010. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  2011. + if (!err)
  2012. + goto out;
  2013. +
  2014. + err = -EINVAL;
  2015. + pr_err("write permission for readonly mount or inode, %s\n", path);
  2016. +
  2017. +out:
  2018. + return err;
  2019. +}
  2020. +
  2021. +/*
  2022. + * returns:
  2023. + * 0: success, the caller will add it
  2024. + * plus: success, it is already unified, the caller should ignore it
  2025. + * minus: error
  2026. + */
  2027. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2028. +{
  2029. + int err;
  2030. + aufs_bindex_t bend, bindex;
  2031. + struct dentry *root, *h_dentry;
  2032. + struct inode *inode, *h_inode;
  2033. +
  2034. + root = sb->s_root;
  2035. + bend = au_sbend(sb);
  2036. + if (unlikely(bend >= 0
  2037. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2038. + err = 1;
  2039. + if (!remount) {
  2040. + err = -EINVAL;
  2041. + pr_err("%s duplicated\n", add->pathname);
  2042. + }
  2043. + goto out;
  2044. + }
  2045. +
  2046. + err = -ENOSPC; /* -E2BIG; */
  2047. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2048. + || AUFS_BRANCH_MAX - 1 <= bend)) {
  2049. + pr_err("number of branches exceeded %s\n", add->pathname);
  2050. + goto out;
  2051. + }
  2052. +
  2053. + err = -EDOM;
  2054. + if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
  2055. + pr_err("bad index %d\n", add->bindex);
  2056. + goto out;
  2057. + }
  2058. +
  2059. + inode = d_inode(add->path.dentry);
  2060. + err = -ENOENT;
  2061. + if (unlikely(!inode->i_nlink)) {
  2062. + pr_err("no existence %s\n", add->pathname);
  2063. + goto out;
  2064. + }
  2065. +
  2066. + err = -EINVAL;
  2067. + if (unlikely(inode->i_sb == sb)) {
  2068. + pr_err("%s must be outside\n", add->pathname);
  2069. + goto out;
  2070. + }
  2071. +
  2072. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2073. + pr_err("unsupported filesystem, %s (%s)\n",
  2074. + add->pathname, au_sbtype(inode->i_sb));
  2075. + goto out;
  2076. + }
  2077. +
  2078. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2079. + pr_err("already stacked, %s (%s)\n",
  2080. + add->pathname, au_sbtype(inode->i_sb));
  2081. + goto out;
  2082. + }
  2083. +
  2084. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2085. + if (unlikely(err))
  2086. + goto out;
  2087. +
  2088. + if (bend < 0)
  2089. + return 0; /* success */
  2090. +
  2091. + err = -EINVAL;
  2092. + for (bindex = 0; bindex <= bend; bindex++)
  2093. + if (unlikely(test_overlap(sb, add->path.dentry,
  2094. + au_h_dptr(root, bindex)))) {
  2095. + pr_err("%s is overlapped\n", add->pathname);
  2096. + goto out;
  2097. + }
  2098. +
  2099. + err = 0;
  2100. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2101. + h_dentry = au_h_dptr(root, 0);
  2102. + h_inode = d_inode(h_dentry);
  2103. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2104. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2105. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2106. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2107. + add->pathname,
  2108. + i_uid_read(inode), i_gid_read(inode),
  2109. + (inode->i_mode & S_IALLUGO),
  2110. + i_uid_read(h_inode), i_gid_read(h_inode),
  2111. + (h_inode->i_mode & S_IALLUGO));
  2112. + }
  2113. +
  2114. +out:
  2115. + return err;
  2116. +}
  2117. +
  2118. +/*
  2119. + * initialize or clean the whiteouts for an adding branch
  2120. + */
  2121. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2122. + int new_perm)
  2123. +{
  2124. + int err, old_perm;
  2125. + aufs_bindex_t bindex;
  2126. + struct mutex *h_mtx;
  2127. + struct au_wbr *wbr;
  2128. + struct au_hinode *hdir;
  2129. + struct dentry *h_dentry;
  2130. +
  2131. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2132. + if (unlikely(err))
  2133. + goto out;
  2134. +
  2135. + wbr = br->br_wbr;
  2136. + old_perm = br->br_perm;
  2137. + br->br_perm = new_perm;
  2138. + hdir = NULL;
  2139. + h_mtx = NULL;
  2140. + bindex = au_br_index(sb, br->br_id);
  2141. + if (0 <= bindex) {
  2142. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2143. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  2144. + } else {
  2145. + h_dentry = au_br_dentry(br);
  2146. + h_mtx = &d_inode(h_dentry)->i_mutex;
  2147. + mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
  2148. + }
  2149. + if (!wbr)
  2150. + err = au_wh_init(br, sb);
  2151. + else {
  2152. + wbr_wh_write_lock(wbr);
  2153. + err = au_wh_init(br, sb);
  2154. + wbr_wh_write_unlock(wbr);
  2155. + }
  2156. + if (hdir)
  2157. + au_hn_imtx_unlock(hdir);
  2158. + else
  2159. + mutex_unlock(h_mtx);
  2160. + vfsub_mnt_drop_write(au_br_mnt(br));
  2161. + br->br_perm = old_perm;
  2162. +
  2163. + if (!err && wbr && !au_br_writable(new_perm)) {
  2164. + kfree(wbr);
  2165. + br->br_wbr = NULL;
  2166. + }
  2167. +
  2168. +out:
  2169. + return err;
  2170. +}
  2171. +
  2172. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2173. + int perm)
  2174. +{
  2175. + int err;
  2176. + struct kstatfs kst;
  2177. + struct au_wbr *wbr;
  2178. +
  2179. + wbr = br->br_wbr;
  2180. + au_rw_init(&wbr->wbr_wh_rwsem);
  2181. + memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
  2182. + atomic_set(&wbr->wbr_wh_running, 0);
  2183. + wbr->wbr_bytes = 0;
  2184. +
  2185. + /*
  2186. + * a limit for rmdir/rename a dir
  2187. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2188. + */
  2189. + err = vfs_statfs(&br->br_path, &kst);
  2190. + if (unlikely(err))
  2191. + goto out;
  2192. + err = -EINVAL;
  2193. + if (kst.f_namelen >= NAME_MAX)
  2194. + err = au_br_init_wh(sb, br, perm);
  2195. + else
  2196. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2197. + au_br_dentry(br),
  2198. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2199. +
  2200. +out:
  2201. + return err;
  2202. +}
  2203. +
  2204. +/* initialize a new branch */
  2205. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2206. + struct au_opt_add *add)
  2207. +{
  2208. + int err;
  2209. + struct inode *h_inode;
  2210. +
  2211. + err = 0;
  2212. + memset(&br->br_xino, 0, sizeof(br->br_xino));
  2213. + mutex_init(&br->br_xino.xi_nondir_mtx);
  2214. + br->br_perm = add->perm;
  2215. + br->br_path = add->path; /* set first, path_get() later */
  2216. + spin_lock_init(&br->br_dykey_lock);
  2217. + memset(br->br_dykey, 0, sizeof(br->br_dykey));
  2218. + atomic_set(&br->br_count, 0);
  2219. + atomic_set(&br->br_xino_running, 0);
  2220. + br->br_id = au_new_br_id(sb);
  2221. + AuDebugOn(br->br_id < 0);
  2222. +
  2223. + if (au_br_writable(add->perm)) {
  2224. + err = au_wbr_init(br, sb, add->perm);
  2225. + if (unlikely(err))
  2226. + goto out_err;
  2227. + }
  2228. +
  2229. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2230. + h_inode = d_inode(add->path.dentry);
  2231. + err = au_xino_br(sb, br, h_inode->i_ino,
  2232. + au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
  2233. + if (unlikely(err)) {
  2234. + AuDebugOn(br->br_xino.xi_file);
  2235. + goto out_err;
  2236. + }
  2237. + }
  2238. +
  2239. + sysaufs_br_init(br);
  2240. + path_get(&br->br_path);
  2241. + goto out; /* success */
  2242. +
  2243. +out_err:
  2244. + memset(&br->br_path, 0, sizeof(br->br_path));
  2245. +out:
  2246. + return err;
  2247. +}
  2248. +
  2249. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2250. + struct au_branch *br, aufs_bindex_t bend,
  2251. + aufs_bindex_t amount)
  2252. +{
  2253. + struct au_branch **brp;
  2254. +
  2255. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2256. +
  2257. + brp = sbinfo->si_branch + bindex;
  2258. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2259. + *brp = br;
  2260. + sbinfo->si_bend++;
  2261. + if (unlikely(bend < 0))
  2262. + sbinfo->si_bend = 0;
  2263. +}
  2264. +
  2265. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2266. + aufs_bindex_t bend, aufs_bindex_t amount)
  2267. +{
  2268. + struct au_hdentry *hdp;
  2269. +
  2270. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2271. +
  2272. + hdp = dinfo->di_hdentry + bindex;
  2273. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2274. + au_h_dentry_init(hdp);
  2275. + dinfo->di_bend++;
  2276. + if (unlikely(bend < 0))
  2277. + dinfo->di_bstart = 0;
  2278. +}
  2279. +
  2280. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2281. + aufs_bindex_t bend, aufs_bindex_t amount)
  2282. +{
  2283. + struct au_hinode *hip;
  2284. +
  2285. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2286. +
  2287. + hip = iinfo->ii_hinode + bindex;
  2288. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2289. + hip->hi_inode = NULL;
  2290. + au_hn_init(hip);
  2291. + iinfo->ii_bend++;
  2292. + if (unlikely(bend < 0))
  2293. + iinfo->ii_bstart = 0;
  2294. +}
  2295. +
  2296. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2297. + aufs_bindex_t bindex)
  2298. +{
  2299. + struct dentry *root, *h_dentry;
  2300. + struct inode *root_inode, *h_inode;
  2301. + aufs_bindex_t bend, amount;
  2302. +
  2303. + root = sb->s_root;
  2304. + root_inode = d_inode(root);
  2305. + bend = au_sbend(sb);
  2306. + amount = bend + 1 - bindex;
  2307. + h_dentry = au_br_dentry(br);
  2308. + au_sbilist_lock();
  2309. + au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
  2310. + au_br_do_add_hdp(au_di(root), bindex, bend, amount);
  2311. + au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
  2312. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2313. + h_inode = d_inode(h_dentry);
  2314. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2315. + au_sbilist_unlock();
  2316. +}
  2317. +
  2318. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2319. +{
  2320. + int err;
  2321. + aufs_bindex_t bend, add_bindex;
  2322. + struct dentry *root, *h_dentry;
  2323. + struct inode *root_inode;
  2324. + struct au_branch *add_branch;
  2325. +
  2326. + root = sb->s_root;
  2327. + root_inode = d_inode(root);
  2328. + IMustLock(root_inode);
  2329. + err = test_add(sb, add, remount);
  2330. + if (unlikely(err < 0))
  2331. + goto out;
  2332. + if (err) {
  2333. + err = 0;
  2334. + goto out; /* success */
  2335. + }
  2336. +
  2337. + bend = au_sbend(sb);
  2338. + add_branch = au_br_alloc(sb, bend + 2, add->perm);
  2339. + err = PTR_ERR(add_branch);
  2340. + if (IS_ERR(add_branch))
  2341. + goto out;
  2342. +
  2343. + err = au_br_init(add_branch, sb, add);
  2344. + if (unlikely(err)) {
  2345. + au_br_do_free(add_branch);
  2346. + goto out;
  2347. + }
  2348. +
  2349. + add_bindex = add->bindex;
  2350. + if (!remount)
  2351. + au_br_do_add(sb, add_branch, add_bindex);
  2352. + else {
  2353. + sysaufs_brs_del(sb, add_bindex);
  2354. + au_br_do_add(sb, add_branch, add_bindex);
  2355. + sysaufs_brs_add(sb, add_bindex);
  2356. + }
  2357. +
  2358. + h_dentry = add->path.dentry;
  2359. + if (!add_bindex) {
  2360. + au_cpup_attr_all(root_inode, /*force*/1);
  2361. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2362. + } else
  2363. + au_add_nlink(root_inode, d_inode(h_dentry));
  2364. +
  2365. + /*
  2366. + * this test/set prevents aufs from handling unnecesary notify events
  2367. + * of xino files, in case of re-adding a writable branch which was
  2368. + * once detached from aufs.
  2369. + */
  2370. + if (au_xino_brid(sb) < 0
  2371. + && au_br_writable(add_branch->br_perm)
  2372. + && !au_test_fs_bad_xino(h_dentry->d_sb)
  2373. + && add_branch->br_xino.xi_file
  2374. + && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
  2375. + au_xino_brid_set(sb, add_branch->br_id);
  2376. +
  2377. +out:
  2378. + return err;
  2379. +}
  2380. +
  2381. +/* ---------------------------------------------------------------------- */
  2382. +
  2383. +static unsigned long long au_farray_cb(void *a,
  2384. + unsigned long long max __maybe_unused,
  2385. + void *arg)
  2386. +{
  2387. + unsigned long long n;
  2388. + struct file **p, *f;
  2389. + struct au_sphlhead *files;
  2390. + struct au_finfo *finfo;
  2391. + struct super_block *sb = arg;
  2392. +
  2393. + n = 0;
  2394. + p = a;
  2395. + files = &au_sbi(sb)->si_files;
  2396. + spin_lock(&files->spin);
  2397. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  2398. + f = finfo->fi_file;
  2399. + if (file_count(f)
  2400. + && !special_file(file_inode(f)->i_mode)) {
  2401. + get_file(f);
  2402. + *p++ = f;
  2403. + n++;
  2404. + AuDebugOn(n > max);
  2405. + }
  2406. + }
  2407. + spin_unlock(&files->spin);
  2408. +
  2409. + return n;
  2410. +}
  2411. +
  2412. +static struct file **au_farray_alloc(struct super_block *sb,
  2413. + unsigned long long *max)
  2414. +{
  2415. + *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
  2416. + return au_array_alloc(max, au_farray_cb, sb);
  2417. +}
  2418. +
  2419. +static void au_farray_free(struct file **a, unsigned long long max)
  2420. +{
  2421. + unsigned long long ull;
  2422. +
  2423. + for (ull = 0; ull < max; ull++)
  2424. + if (a[ull])
  2425. + fput(a[ull]);
  2426. + au_array_free(a);
  2427. +}
  2428. +
  2429. +/* ---------------------------------------------------------------------- */
  2430. +
  2431. +/*
  2432. + * delete a branch
  2433. + */
  2434. +
  2435. +/* to show the line number, do not make it inlined function */
  2436. +#define AuVerbose(do_info, fmt, ...) do { \
  2437. + if (do_info) \
  2438. + pr_info(fmt, ##__VA_ARGS__); \
  2439. +} while (0)
  2440. +
  2441. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
  2442. + aufs_bindex_t bend)
  2443. +{
  2444. + return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
  2445. +}
  2446. +
  2447. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
  2448. + aufs_bindex_t bend)
  2449. +{
  2450. + return au_test_ibusy(d_inode(dentry), bstart, bend);
  2451. +}
  2452. +
  2453. +/*
  2454. + * test if the branch is deletable or not.
  2455. + */
  2456. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2457. + unsigned int sigen, const unsigned int verbose)
  2458. +{
  2459. + int err, i, j, ndentry;
  2460. + aufs_bindex_t bstart, bend;
  2461. + struct au_dcsub_pages dpages;
  2462. + struct au_dpage *dpage;
  2463. + struct dentry *d;
  2464. +
  2465. + err = au_dpages_init(&dpages, GFP_NOFS);
  2466. + if (unlikely(err))
  2467. + goto out;
  2468. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2469. + if (unlikely(err))
  2470. + goto out_dpages;
  2471. +
  2472. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2473. + dpage = dpages.dpages + i;
  2474. + ndentry = dpage->ndentry;
  2475. + for (j = 0; !err && j < ndentry; j++) {
  2476. + d = dpage->dentries[j];
  2477. + AuDebugOn(au_dcount(d) <= 0);
  2478. + if (!au_digen_test(d, sigen)) {
  2479. + di_read_lock_child(d, AuLock_IR);
  2480. + if (unlikely(au_dbrange_test(d))) {
  2481. + di_read_unlock(d, AuLock_IR);
  2482. + continue;
  2483. + }
  2484. + } else {
  2485. + di_write_lock_child(d);
  2486. + if (unlikely(au_dbrange_test(d))) {
  2487. + di_write_unlock(d);
  2488. + continue;
  2489. + }
  2490. + err = au_reval_dpath(d, sigen);
  2491. + if (!err)
  2492. + di_downgrade_lock(d, AuLock_IR);
  2493. + else {
  2494. + di_write_unlock(d);
  2495. + break;
  2496. + }
  2497. + }
  2498. +
  2499. + /* AuDbgDentry(d); */
  2500. + bstart = au_dbstart(d);
  2501. + bend = au_dbend(d);
  2502. + if (bstart <= bindex
  2503. + && bindex <= bend
  2504. + && au_h_dptr(d, bindex)
  2505. + && au_test_dbusy(d, bstart, bend)) {
  2506. + err = -EBUSY;
  2507. + AuVerbose(verbose, "busy %pd\n", d);
  2508. + AuDbgDentry(d);
  2509. + }
  2510. + di_read_unlock(d, AuLock_IR);
  2511. + }
  2512. + }
  2513. +
  2514. +out_dpages:
  2515. + au_dpages_free(&dpages);
  2516. +out:
  2517. + return err;
  2518. +}
  2519. +
  2520. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2521. + unsigned int sigen, const unsigned int verbose)
  2522. +{
  2523. + int err;
  2524. + unsigned long long max, ull;
  2525. + struct inode *i, **array;
  2526. + aufs_bindex_t bstart, bend;
  2527. +
  2528. + array = au_iarray_alloc(sb, &max);
  2529. + err = PTR_ERR(array);
  2530. + if (IS_ERR(array))
  2531. + goto out;
  2532. +
  2533. + err = 0;
  2534. + AuDbg("b%d\n", bindex);
  2535. + for (ull = 0; !err && ull < max; ull++) {
  2536. + i = array[ull];
  2537. + if (unlikely(!i))
  2538. + break;
  2539. + if (i->i_ino == AUFS_ROOT_INO)
  2540. + continue;
  2541. +
  2542. + /* AuDbgInode(i); */
  2543. + if (au_iigen(i, NULL) == sigen)
  2544. + ii_read_lock_child(i);
  2545. + else {
  2546. + ii_write_lock_child(i);
  2547. + err = au_refresh_hinode_self(i);
  2548. + au_iigen_dec(i);
  2549. + if (!err)
  2550. + ii_downgrade_lock(i);
  2551. + else {
  2552. + ii_write_unlock(i);
  2553. + break;
  2554. + }
  2555. + }
  2556. +
  2557. + bstart = au_ibstart(i);
  2558. + bend = au_ibend(i);
  2559. + if (bstart <= bindex
  2560. + && bindex <= bend
  2561. + && au_h_iptr(i, bindex)
  2562. + && au_test_ibusy(i, bstart, bend)) {
  2563. + err = -EBUSY;
  2564. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2565. + AuDbgInode(i);
  2566. + }
  2567. + ii_read_unlock(i);
  2568. + }
  2569. + au_iarray_free(array, max);
  2570. +
  2571. +out:
  2572. + return err;
  2573. +}
  2574. +
  2575. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2576. + const unsigned int verbose)
  2577. +{
  2578. + int err;
  2579. + unsigned int sigen;
  2580. +
  2581. + sigen = au_sigen(root->d_sb);
  2582. + DiMustNoWaiters(root);
  2583. + IiMustNoWaiters(d_inode(root));
  2584. + di_write_unlock(root);
  2585. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2586. + if (!err)
  2587. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2588. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2589. +
  2590. + return err;
  2591. +}
  2592. +
  2593. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2594. + struct file **to_free, int *idx)
  2595. +{
  2596. + int err;
  2597. + unsigned char matched, root;
  2598. + aufs_bindex_t bindex, bend;
  2599. + struct au_fidir *fidir;
  2600. + struct au_hfile *hfile;
  2601. +
  2602. + err = 0;
  2603. + root = IS_ROOT(file->f_path.dentry);
  2604. + if (root) {
  2605. + get_file(file);
  2606. + to_free[*idx] = file;
  2607. + (*idx)++;
  2608. + goto out;
  2609. + }
  2610. +
  2611. + matched = 0;
  2612. + fidir = au_fi(file)->fi_hdir;
  2613. + AuDebugOn(!fidir);
  2614. + bend = au_fbend_dir(file);
  2615. + for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
  2616. + hfile = fidir->fd_hfile + bindex;
  2617. + if (!hfile->hf_file)
  2618. + continue;
  2619. +
  2620. + if (hfile->hf_br->br_id == br_id) {
  2621. + matched = 1;
  2622. + break;
  2623. + }
  2624. + }
  2625. + if (matched)
  2626. + err = -EBUSY;
  2627. +
  2628. +out:
  2629. + return err;
  2630. +}
  2631. +
  2632. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2633. + struct file **to_free, int opened)
  2634. +{
  2635. + int err, idx;
  2636. + unsigned long long ull, max;
  2637. + aufs_bindex_t bstart;
  2638. + struct file *file, **array;
  2639. + struct dentry *root;
  2640. + struct au_hfile *hfile;
  2641. +
  2642. + array = au_farray_alloc(sb, &max);
  2643. + err = PTR_ERR(array);
  2644. + if (IS_ERR(array))
  2645. + goto out;
  2646. +
  2647. + err = 0;
  2648. + idx = 0;
  2649. + root = sb->s_root;
  2650. + di_write_unlock(root);
  2651. + for (ull = 0; ull < max; ull++) {
  2652. + file = array[ull];
  2653. + if (unlikely(!file))
  2654. + break;
  2655. +
  2656. + /* AuDbg("%pD\n", file); */
  2657. + fi_read_lock(file);
  2658. + bstart = au_fbstart(file);
  2659. + if (!d_is_dir(file->f_path.dentry)) {
  2660. + hfile = &au_fi(file)->fi_htop;
  2661. + if (hfile->hf_br->br_id == br_id)
  2662. + err = -EBUSY;
  2663. + } else
  2664. + err = test_dir_busy(file, br_id, to_free, &idx);
  2665. + fi_read_unlock(file);
  2666. + if (unlikely(err))
  2667. + break;
  2668. + }
  2669. + di_write_lock_child(root);
  2670. + au_farray_free(array, max);
  2671. + AuDebugOn(idx > opened);
  2672. +
  2673. +out:
  2674. + return err;
  2675. +}
  2676. +
  2677. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2678. + aufs_bindex_t br_id)
  2679. +{
  2680. + unsigned long long ull;
  2681. + aufs_bindex_t bindex, bstart, bend, bfound;
  2682. + struct file *file;
  2683. + struct au_fidir *fidir;
  2684. + struct au_hfile *hfile;
  2685. +
  2686. + for (ull = 0; ull < opened; ull++) {
  2687. + file = to_free[ull];
  2688. + if (unlikely(!file))
  2689. + break;
  2690. +
  2691. + /* AuDbg("%pD\n", file); */
  2692. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2693. + bfound = -1;
  2694. + fidir = au_fi(file)->fi_hdir;
  2695. + AuDebugOn(!fidir);
  2696. + fi_write_lock(file);
  2697. + bstart = au_fbstart(file);
  2698. + bend = au_fbend_dir(file);
  2699. + for (bindex = bstart; bindex <= bend; bindex++) {
  2700. + hfile = fidir->fd_hfile + bindex;
  2701. + if (!hfile->hf_file)
  2702. + continue;
  2703. +
  2704. + if (hfile->hf_br->br_id == br_id) {
  2705. + bfound = bindex;
  2706. + break;
  2707. + }
  2708. + }
  2709. + AuDebugOn(bfound < 0);
  2710. + au_set_h_fptr(file, bfound, NULL);
  2711. + if (bfound == bstart) {
  2712. + for (bstart++; bstart <= bend; bstart++)
  2713. + if (au_hf_dir(file, bstart)) {
  2714. + au_set_fbstart(file, bstart);
  2715. + break;
  2716. + }
  2717. + }
  2718. + fi_write_unlock(file);
  2719. + }
  2720. +}
  2721. +
  2722. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2723. + const aufs_bindex_t bindex,
  2724. + const aufs_bindex_t bend)
  2725. +{
  2726. + struct au_branch **brp, **p;
  2727. +
  2728. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2729. +
  2730. + brp = sbinfo->si_branch + bindex;
  2731. + if (bindex < bend)
  2732. + memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
  2733. + sbinfo->si_branch[0 + bend] = NULL;
  2734. + sbinfo->si_bend--;
  2735. +
  2736. + p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
  2737. + if (p)
  2738. + sbinfo->si_branch = p;
  2739. + /* harmless error */
  2740. +}
  2741. +
  2742. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2743. + const aufs_bindex_t bend)
  2744. +{
  2745. + struct au_hdentry *hdp, *p;
  2746. +
  2747. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2748. +
  2749. + hdp = dinfo->di_hdentry;
  2750. + if (bindex < bend)
  2751. + memmove(hdp + bindex, hdp + bindex + 1,
  2752. + sizeof(*hdp) * (bend - bindex));
  2753. + hdp[0 + bend].hd_dentry = NULL;
  2754. + dinfo->di_bend--;
  2755. +
  2756. + p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
  2757. + if (p)
  2758. + dinfo->di_hdentry = p;
  2759. + /* harmless error */
  2760. +}
  2761. +
  2762. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2763. + const aufs_bindex_t bend)
  2764. +{
  2765. + struct au_hinode *hip, *p;
  2766. +
  2767. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2768. +
  2769. + hip = iinfo->ii_hinode + bindex;
  2770. + if (bindex < bend)
  2771. + memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
  2772. + iinfo->ii_hinode[0 + bend].hi_inode = NULL;
  2773. + au_hn_init(iinfo->ii_hinode + bend);
  2774. + iinfo->ii_bend--;
  2775. +
  2776. + p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
  2777. + if (p)
  2778. + iinfo->ii_hinode = p;
  2779. + /* harmless error */
  2780. +}
  2781. +
  2782. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2783. + struct au_branch *br)
  2784. +{
  2785. + aufs_bindex_t bend;
  2786. + struct au_sbinfo *sbinfo;
  2787. + struct dentry *root, *h_root;
  2788. + struct inode *inode, *h_inode;
  2789. + struct au_hinode *hinode;
  2790. +
  2791. + SiMustWriteLock(sb);
  2792. +
  2793. + root = sb->s_root;
  2794. + inode = d_inode(root);
  2795. + sbinfo = au_sbi(sb);
  2796. + bend = sbinfo->si_bend;
  2797. +
  2798. + h_root = au_h_dptr(root, bindex);
  2799. + hinode = au_hi(inode, bindex);
  2800. + h_inode = au_igrab(hinode->hi_inode);
  2801. + au_hiput(hinode);
  2802. +
  2803. + au_sbilist_lock();
  2804. + au_br_do_del_brp(sbinfo, bindex, bend);
  2805. + au_br_do_del_hdp(au_di(root), bindex, bend);
  2806. + au_br_do_del_hip(au_ii(inode), bindex, bend);
  2807. + au_sbilist_unlock();
  2808. +
  2809. + dput(h_root);
  2810. + iput(h_inode);
  2811. + au_br_do_free(br);
  2812. +}
  2813. +
  2814. +static unsigned long long empty_cb(void *array, unsigned long long max,
  2815. + void *arg)
  2816. +{
  2817. + return max;
  2818. +}
  2819. +
  2820. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2821. +{
  2822. + int err, rerr, i;
  2823. + unsigned long long opened;
  2824. + unsigned int mnt_flags;
  2825. + aufs_bindex_t bindex, bend, br_id;
  2826. + unsigned char do_wh, verbose;
  2827. + struct au_branch *br;
  2828. + struct au_wbr *wbr;
  2829. + struct dentry *root;
  2830. + struct file **to_free;
  2831. +
  2832. + err = 0;
  2833. + opened = 0;
  2834. + to_free = NULL;
  2835. + root = sb->s_root;
  2836. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2837. + if (bindex < 0) {
  2838. + if (remount)
  2839. + goto out; /* success */
  2840. + err = -ENOENT;
  2841. + pr_err("%s no such branch\n", del->pathname);
  2842. + goto out;
  2843. + }
  2844. + AuDbg("bindex b%d\n", bindex);
  2845. +
  2846. + err = -EBUSY;
  2847. + mnt_flags = au_mntflags(sb);
  2848. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2849. + bend = au_sbend(sb);
  2850. + if (unlikely(!bend)) {
  2851. + AuVerbose(verbose, "no more branches left\n");
  2852. + goto out;
  2853. + }
  2854. + br = au_sbr(sb, bindex);
  2855. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  2856. +
  2857. + br_id = br->br_id;
  2858. + opened = atomic_read(&br->br_count);
  2859. + if (unlikely(opened)) {
  2860. + to_free = au_array_alloc(&opened, empty_cb, NULL);
  2861. + err = PTR_ERR(to_free);
  2862. + if (IS_ERR(to_free))
  2863. + goto out;
  2864. +
  2865. + err = test_file_busy(sb, br_id, to_free, opened);
  2866. + if (unlikely(err)) {
  2867. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  2868. + goto out;
  2869. + }
  2870. + }
  2871. +
  2872. + wbr = br->br_wbr;
  2873. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  2874. + if (do_wh) {
  2875. + /* instead of WbrWhMustWriteLock(wbr) */
  2876. + SiMustWriteLock(sb);
  2877. + for (i = 0; i < AuBrWh_Last; i++) {
  2878. + dput(wbr->wbr_wh[i]);
  2879. + wbr->wbr_wh[i] = NULL;
  2880. + }
  2881. + }
  2882. +
  2883. + err = test_children_busy(root, bindex, verbose);
  2884. + if (unlikely(err)) {
  2885. + if (do_wh)
  2886. + goto out_wh;
  2887. + goto out;
  2888. + }
  2889. +
  2890. + err = 0;
  2891. + if (to_free) {
  2892. + /*
  2893. + * now we confirmed the branch is deletable.
  2894. + * let's free the remaining opened dirs on the branch.
  2895. + */
  2896. + di_write_unlock(root);
  2897. + br_del_file(to_free, opened, br_id);
  2898. + di_write_lock_child(root);
  2899. + }
  2900. +
  2901. + if (!remount)
  2902. + au_br_do_del(sb, bindex, br);
  2903. + else {
  2904. + sysaufs_brs_del(sb, bindex);
  2905. + au_br_do_del(sb, bindex, br);
  2906. + sysaufs_brs_add(sb, bindex);
  2907. + }
  2908. +
  2909. + if (!bindex) {
  2910. + au_cpup_attr_all(d_inode(root), /*force*/1);
  2911. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  2912. + } else
  2913. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  2914. + if (au_opt_test(mnt_flags, PLINK))
  2915. + au_plink_half_refresh(sb, br_id);
  2916. +
  2917. + if (au_xino_brid(sb) == br_id)
  2918. + au_xino_brid_set(sb, -1);
  2919. + goto out; /* success */
  2920. +
  2921. +out_wh:
  2922. + /* revert */
  2923. + rerr = au_br_init_wh(sb, br, br->br_perm);
  2924. + if (rerr)
  2925. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  2926. + del->pathname, rerr);
  2927. +out:
  2928. + if (to_free)
  2929. + au_farray_free(to_free, opened);
  2930. + return err;
  2931. +}
  2932. +
  2933. +/* ---------------------------------------------------------------------- */
  2934. +
  2935. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  2936. +{
  2937. + int err;
  2938. + aufs_bindex_t bstart, bend;
  2939. + struct aufs_ibusy ibusy;
  2940. + struct inode *inode, *h_inode;
  2941. +
  2942. + err = -EPERM;
  2943. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  2944. + goto out;
  2945. +
  2946. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  2947. + if (!err)
  2948. + err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
  2949. + if (unlikely(err)) {
  2950. + err = -EFAULT;
  2951. + AuTraceErr(err);
  2952. + goto out;
  2953. + }
  2954. +
  2955. + err = -EINVAL;
  2956. + si_read_lock(sb, AuLock_FLUSH);
  2957. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
  2958. + goto out_unlock;
  2959. +
  2960. + err = 0;
  2961. + ibusy.h_ino = 0; /* invalid */
  2962. + inode = ilookup(sb, ibusy.ino);
  2963. + if (!inode
  2964. + || inode->i_ino == AUFS_ROOT_INO
  2965. + || is_bad_inode(inode))
  2966. + goto out_unlock;
  2967. +
  2968. + ii_read_lock_child(inode);
  2969. + bstart = au_ibstart(inode);
  2970. + bend = au_ibend(inode);
  2971. + if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
  2972. + h_inode = au_h_iptr(inode, ibusy.bindex);
  2973. + if (h_inode && au_test_ibusy(inode, bstart, bend))
  2974. + ibusy.h_ino = h_inode->i_ino;
  2975. + }
  2976. + ii_read_unlock(inode);
  2977. + iput(inode);
  2978. +
  2979. +out_unlock:
  2980. + si_read_unlock(sb);
  2981. + if (!err) {
  2982. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  2983. + if (unlikely(err)) {
  2984. + err = -EFAULT;
  2985. + AuTraceErr(err);
  2986. + }
  2987. + }
  2988. +out:
  2989. + return err;
  2990. +}
  2991. +
  2992. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  2993. +{
  2994. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  2995. +}
  2996. +
  2997. +#ifdef CONFIG_COMPAT
  2998. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  2999. +{
  3000. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  3001. +}
  3002. +#endif
  3003. +
  3004. +/* ---------------------------------------------------------------------- */
  3005. +
  3006. +/*
  3007. + * change a branch permission
  3008. + */
  3009. +
  3010. +static void au_warn_ima(void)
  3011. +{
  3012. +#ifdef CONFIG_IMA
  3013. + /* since it doesn't support mark_files_ro() */
  3014. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  3015. +#endif
  3016. +}
  3017. +
  3018. +static int do_need_sigen_inc(int a, int b)
  3019. +{
  3020. + return au_br_whable(a) && !au_br_whable(b);
  3021. +}
  3022. +
  3023. +static int need_sigen_inc(int old, int new)
  3024. +{
  3025. + return do_need_sigen_inc(old, new)
  3026. + || do_need_sigen_inc(new, old);
  3027. +}
  3028. +
  3029. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  3030. +{
  3031. + int err, do_warn;
  3032. + unsigned int mnt_flags;
  3033. + unsigned long long ull, max;
  3034. + aufs_bindex_t br_id;
  3035. + unsigned char verbose, writer;
  3036. + struct file *file, *hf, **array;
  3037. + struct au_hfile *hfile;
  3038. +
  3039. + mnt_flags = au_mntflags(sb);
  3040. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3041. +
  3042. + array = au_farray_alloc(sb, &max);
  3043. + err = PTR_ERR(array);
  3044. + if (IS_ERR(array))
  3045. + goto out;
  3046. +
  3047. + do_warn = 0;
  3048. + br_id = au_sbr_id(sb, bindex);
  3049. + for (ull = 0; ull < max; ull++) {
  3050. + file = array[ull];
  3051. + if (unlikely(!file))
  3052. + break;
  3053. +
  3054. + /* AuDbg("%pD\n", file); */
  3055. + fi_read_lock(file);
  3056. + if (unlikely(au_test_mmapped(file))) {
  3057. + err = -EBUSY;
  3058. + AuVerbose(verbose, "mmapped %pD\n", file);
  3059. + AuDbgFile(file);
  3060. + FiMustNoWaiters(file);
  3061. + fi_read_unlock(file);
  3062. + goto out_array;
  3063. + }
  3064. +
  3065. + hfile = &au_fi(file)->fi_htop;
  3066. + hf = hfile->hf_file;
  3067. + if (!d_is_reg(file->f_path.dentry)
  3068. + || !(file->f_mode & FMODE_WRITE)
  3069. + || hfile->hf_br->br_id != br_id
  3070. + || !(hf->f_mode & FMODE_WRITE))
  3071. + array[ull] = NULL;
  3072. + else {
  3073. + do_warn = 1;
  3074. + get_file(file);
  3075. + }
  3076. +
  3077. + FiMustNoWaiters(file);
  3078. + fi_read_unlock(file);
  3079. + fput(file);
  3080. + }
  3081. +
  3082. + err = 0;
  3083. + if (do_warn)
  3084. + au_warn_ima();
  3085. +
  3086. + for (ull = 0; ull < max; ull++) {
  3087. + file = array[ull];
  3088. + if (!file)
  3089. + continue;
  3090. +
  3091. + /* todo: already flushed? */
  3092. + /*
  3093. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3094. + * approach which resets f_mode and calls mnt_drop_write() and
  3095. + * file_release_write() for each file, because the branch
  3096. + * attribute in aufs world is totally different from the native
  3097. + * fs rw/ro mode.
  3098. + */
  3099. + /* fi_read_lock(file); */
  3100. + hfile = &au_fi(file)->fi_htop;
  3101. + hf = hfile->hf_file;
  3102. + /* fi_read_unlock(file); */
  3103. + spin_lock(&hf->f_lock);
  3104. + writer = !!(hf->f_mode & FMODE_WRITER);
  3105. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3106. + spin_unlock(&hf->f_lock);
  3107. + if (writer) {
  3108. + put_write_access(file_inode(hf));
  3109. + __mnt_drop_write(hf->f_path.mnt);
  3110. + }
  3111. + }
  3112. +
  3113. +out_array:
  3114. + au_farray_free(array, max);
  3115. +out:
  3116. + AuTraceErr(err);
  3117. + return err;
  3118. +}
  3119. +
  3120. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3121. + int *do_refresh)
  3122. +{
  3123. + int err, rerr;
  3124. + aufs_bindex_t bindex;
  3125. + struct dentry *root;
  3126. + struct au_branch *br;
  3127. + struct au_br_fhsm *bf;
  3128. +
  3129. + root = sb->s_root;
  3130. + bindex = au_find_dbindex(root, mod->h_root);
  3131. + if (bindex < 0) {
  3132. + if (remount)
  3133. + return 0; /* success */
  3134. + err = -ENOENT;
  3135. + pr_err("%s no such branch\n", mod->path);
  3136. + goto out;
  3137. + }
  3138. + AuDbg("bindex b%d\n", bindex);
  3139. +
  3140. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3141. + if (unlikely(err))
  3142. + goto out;
  3143. +
  3144. + br = au_sbr(sb, bindex);
  3145. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3146. + if (br->br_perm == mod->perm)
  3147. + return 0; /* success */
  3148. +
  3149. + /* pre-allocate for non-fhsm --> fhsm */
  3150. + bf = NULL;
  3151. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3152. + err = au_fhsm_br_alloc(br);
  3153. + if (unlikely(err))
  3154. + goto out;
  3155. + bf = br->br_fhsm;
  3156. + br->br_fhsm = NULL;
  3157. + }
  3158. +
  3159. + if (au_br_writable(br->br_perm)) {
  3160. + /* remove whiteout base */
  3161. + err = au_br_init_wh(sb, br, mod->perm);
  3162. + if (unlikely(err))
  3163. + goto out_bf;
  3164. +
  3165. + if (!au_br_writable(mod->perm)) {
  3166. + /* rw --> ro, file might be mmapped */
  3167. + DiMustNoWaiters(root);
  3168. + IiMustNoWaiters(d_inode(root));
  3169. + di_write_unlock(root);
  3170. + err = au_br_mod_files_ro(sb, bindex);
  3171. + /* aufs_write_lock() calls ..._child() */
  3172. + di_write_lock_child(root);
  3173. +
  3174. + if (unlikely(err)) {
  3175. + rerr = -ENOMEM;
  3176. + br->br_wbr = kmalloc(sizeof(*br->br_wbr),
  3177. + GFP_NOFS);
  3178. + if (br->br_wbr)
  3179. + rerr = au_wbr_init(br, sb, br->br_perm);
  3180. + if (unlikely(rerr)) {
  3181. + AuIOErr("nested error %d (%d)\n",
  3182. + rerr, err);
  3183. + br->br_perm = mod->perm;
  3184. + }
  3185. + }
  3186. + }
  3187. + } else if (au_br_writable(mod->perm)) {
  3188. + /* ro --> rw */
  3189. + err = -ENOMEM;
  3190. + br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3191. + if (br->br_wbr) {
  3192. + err = au_wbr_init(br, sb, mod->perm);
  3193. + if (unlikely(err)) {
  3194. + kfree(br->br_wbr);
  3195. + br->br_wbr = NULL;
  3196. + }
  3197. + }
  3198. + }
  3199. + if (unlikely(err))
  3200. + goto out_bf;
  3201. +
  3202. + if (au_br_fhsm(br->br_perm)) {
  3203. + if (!au_br_fhsm(mod->perm)) {
  3204. + /* fhsm --> non-fhsm */
  3205. + au_br_fhsm_fin(br->br_fhsm);
  3206. + kfree(br->br_fhsm);
  3207. + br->br_fhsm = NULL;
  3208. + }
  3209. + } else if (au_br_fhsm(mod->perm))
  3210. + /* non-fhsm --> fhsm */
  3211. + br->br_fhsm = bf;
  3212. +
  3213. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3214. + br->br_perm = mod->perm;
  3215. + goto out; /* success */
  3216. +
  3217. +out_bf:
  3218. + kfree(bf);
  3219. +out:
  3220. + AuTraceErr(err);
  3221. + return err;
  3222. +}
  3223. +
  3224. +/* ---------------------------------------------------------------------- */
  3225. +
  3226. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3227. +{
  3228. + int err;
  3229. + struct kstatfs kstfs;
  3230. +
  3231. + err = vfs_statfs(&br->br_path, &kstfs);
  3232. + if (!err) {
  3233. + stfs->f_blocks = kstfs.f_blocks;
  3234. + stfs->f_bavail = kstfs.f_bavail;
  3235. + stfs->f_files = kstfs.f_files;
  3236. + stfs->f_ffree = kstfs.f_ffree;
  3237. + }
  3238. +
  3239. + return err;
  3240. +}
  3241. diff -Nur linux-4.1.10.orig/fs/aufs/branch.h linux-4.1.10/fs/aufs/branch.h
  3242. --- linux-4.1.10.orig/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
  3243. +++ linux-4.1.10/fs/aufs/branch.h 2015-10-22 21:35:53.000000000 +0200
  3244. @@ -0,0 +1,279 @@
  3245. +/*
  3246. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3247. + *
  3248. + * This program, aufs is free software; you can redistribute it and/or modify
  3249. + * it under the terms of the GNU General Public License as published by
  3250. + * the Free Software Foundation; either version 2 of the License, or
  3251. + * (at your option) any later version.
  3252. + *
  3253. + * This program is distributed in the hope that it will be useful,
  3254. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3255. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3256. + * GNU General Public License for more details.
  3257. + *
  3258. + * You should have received a copy of the GNU General Public License
  3259. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3260. + */
  3261. +
  3262. +/*
  3263. + * branch filesystems and xino for them
  3264. + */
  3265. +
  3266. +#ifndef __AUFS_BRANCH_H__
  3267. +#define __AUFS_BRANCH_H__
  3268. +
  3269. +#ifdef __KERNEL__
  3270. +
  3271. +#include <linux/mount.h>
  3272. +#include "dynop.h"
  3273. +#include "rwsem.h"
  3274. +#include "super.h"
  3275. +
  3276. +/* ---------------------------------------------------------------------- */
  3277. +
  3278. +/* a xino file */
  3279. +struct au_xino_file {
  3280. + struct file *xi_file;
  3281. + struct mutex xi_nondir_mtx;
  3282. +
  3283. + /* todo: make xino files an array to support huge inode number */
  3284. +
  3285. +#ifdef CONFIG_DEBUG_FS
  3286. + struct dentry *xi_dbgaufs;
  3287. +#endif
  3288. +};
  3289. +
  3290. +/* File-based Hierarchical Storage Management */
  3291. +struct au_br_fhsm {
  3292. +#ifdef CONFIG_AUFS_FHSM
  3293. + struct mutex bf_lock;
  3294. + unsigned long bf_jiffy;
  3295. + struct aufs_stfs bf_stfs;
  3296. + int bf_readable;
  3297. +#endif
  3298. +};
  3299. +
  3300. +/* members for writable branch only */
  3301. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3302. +struct au_wbr {
  3303. + struct au_rwsem wbr_wh_rwsem;
  3304. + struct dentry *wbr_wh[AuBrWh_Last];
  3305. + atomic_t wbr_wh_running;
  3306. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3307. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3308. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3309. +
  3310. + /* mfs mode */
  3311. + unsigned long long wbr_bytes;
  3312. +};
  3313. +
  3314. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3315. +#define AuBrDynOp (AuDyLast * 4)
  3316. +
  3317. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3318. +/* support for asynchronous destruction */
  3319. +struct au_br_hfsnotify {
  3320. + struct fsnotify_group *hfsn_group;
  3321. +};
  3322. +#endif
  3323. +
  3324. +/* sysfs entries */
  3325. +struct au_brsysfs {
  3326. + char name[16];
  3327. + struct attribute attr;
  3328. +};
  3329. +
  3330. +enum {
  3331. + AuBrSysfs_BR,
  3332. + AuBrSysfs_BRID,
  3333. + AuBrSysfs_Last
  3334. +};
  3335. +
  3336. +/* protected by superblock rwsem */
  3337. +struct au_branch {
  3338. + struct au_xino_file br_xino;
  3339. +
  3340. + aufs_bindex_t br_id;
  3341. +
  3342. + int br_perm;
  3343. + struct path br_path;
  3344. + spinlock_t br_dykey_lock;
  3345. + struct au_dykey *br_dykey[AuBrDynOp];
  3346. + atomic_t br_count;
  3347. +
  3348. + struct au_wbr *br_wbr;
  3349. + struct au_br_fhsm *br_fhsm;
  3350. +
  3351. + /* xino truncation */
  3352. + atomic_t br_xino_running;
  3353. +
  3354. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3355. + struct au_br_hfsnotify *br_hfsn;
  3356. +#endif
  3357. +
  3358. +#ifdef CONFIG_SYSFS
  3359. + /* entries under sysfs per mount-point */
  3360. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3361. +#endif
  3362. +};
  3363. +
  3364. +/* ---------------------------------------------------------------------- */
  3365. +
  3366. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3367. +{
  3368. + return br->br_path.mnt;
  3369. +}
  3370. +
  3371. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3372. +{
  3373. + return br->br_path.dentry;
  3374. +}
  3375. +
  3376. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3377. +{
  3378. + return au_br_mnt(br)->mnt_sb;
  3379. +}
  3380. +
  3381. +static inline int au_br_rdonly(struct au_branch *br)
  3382. +{
  3383. + return ((au_br_sb(br)->s_flags & MS_RDONLY)
  3384. + || !au_br_writable(br->br_perm))
  3385. + ? -EROFS : 0;
  3386. +}
  3387. +
  3388. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3389. +{
  3390. +#ifdef CONFIG_AUFS_HNOTIFY
  3391. + return !(brperm & AuBrPerm_RR);
  3392. +#else
  3393. + return 0;
  3394. +#endif
  3395. +}
  3396. +
  3397. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3398. +{
  3399. + int err, exec_flag;
  3400. +
  3401. + err = 0;
  3402. + exec_flag = oflag & __FMODE_EXEC;
  3403. + if (unlikely(exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC)))
  3404. + err = -EACCES;
  3405. +
  3406. + return err;
  3407. +}
  3408. +
  3409. +/* ---------------------------------------------------------------------- */
  3410. +
  3411. +/* branch.c */
  3412. +struct au_sbinfo;
  3413. +void au_br_free(struct au_sbinfo *sinfo);
  3414. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3415. +struct au_opt_add;
  3416. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3417. +struct au_opt_del;
  3418. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3419. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3420. +#ifdef CONFIG_COMPAT
  3421. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3422. +#endif
  3423. +struct au_opt_mod;
  3424. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3425. + int *do_refresh);
  3426. +struct aufs_stfs;
  3427. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3428. +
  3429. +/* xino.c */
  3430. +static const loff_t au_loff_max = LLONG_MAX;
  3431. +
  3432. +int au_xib_trunc(struct super_block *sb);
  3433. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
  3434. + loff_t *pos);
  3435. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  3436. + size_t size, loff_t *pos);
  3437. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
  3438. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
  3439. +ino_t au_xino_new_ino(struct super_block *sb);
  3440. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3441. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3442. + ino_t ino);
  3443. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3444. + ino_t *ino);
  3445. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3446. + struct file *base_file, int do_test);
  3447. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
  3448. +
  3449. +struct au_opt_xino;
  3450. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
  3451. +void au_xino_clr(struct super_block *sb);
  3452. +struct file *au_xino_def(struct super_block *sb);
  3453. +int au_xino_path(struct seq_file *seq, struct file *file);
  3454. +
  3455. +/* ---------------------------------------------------------------------- */
  3456. +
  3457. +/* Superblock to branch */
  3458. +static inline
  3459. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3460. +{
  3461. + return au_sbr(sb, bindex)->br_id;
  3462. +}
  3463. +
  3464. +static inline
  3465. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3466. +{
  3467. + return au_br_mnt(au_sbr(sb, bindex));
  3468. +}
  3469. +
  3470. +static inline
  3471. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3472. +{
  3473. + return au_br_sb(au_sbr(sb, bindex));
  3474. +}
  3475. +
  3476. +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
  3477. +{
  3478. + atomic_dec(&au_sbr(sb, bindex)->br_count);
  3479. +}
  3480. +
  3481. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3482. +{
  3483. + return au_sbr(sb, bindex)->br_perm;
  3484. +}
  3485. +
  3486. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3487. +{
  3488. + return au_br_whable(au_sbr_perm(sb, bindex));
  3489. +}
  3490. +
  3491. +/* ---------------------------------------------------------------------- */
  3492. +
  3493. +/*
  3494. + * wbr_wh_read_lock, wbr_wh_write_lock
  3495. + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
  3496. + */
  3497. +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
  3498. +
  3499. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
  3500. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
  3501. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
  3502. +
  3503. +/* ---------------------------------------------------------------------- */
  3504. +
  3505. +#ifdef CONFIG_AUFS_FHSM
  3506. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3507. +{
  3508. + mutex_init(&brfhsm->bf_lock);
  3509. + brfhsm->bf_jiffy = 0;
  3510. + brfhsm->bf_readable = 0;
  3511. +}
  3512. +
  3513. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3514. +{
  3515. + mutex_destroy(&brfhsm->bf_lock);
  3516. +}
  3517. +#else
  3518. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3519. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3520. +#endif
  3521. +
  3522. +#endif /* __KERNEL__ */
  3523. +#endif /* __AUFS_BRANCH_H__ */
  3524. diff -Nur linux-4.1.10.orig/fs/aufs/conf.mk linux-4.1.10/fs/aufs/conf.mk
  3525. --- linux-4.1.10.orig/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
  3526. +++ linux-4.1.10/fs/aufs/conf.mk 2015-10-22 21:35:53.000000000 +0200
  3527. @@ -0,0 +1,38 @@
  3528. +
  3529. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3530. +
  3531. +define AuConf
  3532. +ifdef ${1}
  3533. +AuConfStr += ${1}=${${1}}
  3534. +endif
  3535. +endef
  3536. +
  3537. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3538. + SBILIST \
  3539. + HNOTIFY HFSNOTIFY \
  3540. + EXPORT INO_T_64 \
  3541. + XATTR \
  3542. + FHSM \
  3543. + RDU \
  3544. + SHWH \
  3545. + BR_RAMFS \
  3546. + BR_FUSE POLL \
  3547. + BR_HFSPLUS \
  3548. + BDEV_LOOP \
  3549. + DEBUG MAGIC_SYSRQ
  3550. +$(foreach i, ${AuConfAll}, \
  3551. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3552. +
  3553. +AuConfName = ${obj}/conf.str
  3554. +${AuConfName}.tmp: FORCE
  3555. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3556. +${AuConfName}: ${AuConfName}.tmp
  3557. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3558. + echo ' GEN ' $@; \
  3559. + cp -p $< $@; \
  3560. + }
  3561. +FORCE:
  3562. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3563. +${obj}/sysfs.o: ${AuConfName}
  3564. +
  3565. +-include ${srctree}/${src}/conf_priv.mk
  3566. diff -Nur linux-4.1.10.orig/fs/aufs/cpup.c linux-4.1.10/fs/aufs/cpup.c
  3567. --- linux-4.1.10.orig/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
  3568. +++ linux-4.1.10/fs/aufs/cpup.c 2015-10-22 21:35:53.000000000 +0200
  3569. @@ -0,0 +1,1319 @@
  3570. +/*
  3571. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  3572. + *
  3573. + * This program, aufs is free software; you can redistribute it and/or modify
  3574. + * it under the terms of the GNU General Public License as published by
  3575. + * the Free Software Foundation; either version 2 of the License, or
  3576. + * (at your option) any later version.
  3577. + *
  3578. + * This program is distributed in the hope that it will be useful,
  3579. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3580. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3581. + * GNU General Public License for more details.
  3582. + *
  3583. + * You should have received a copy of the GNU General Public License
  3584. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3585. + */
  3586. +
  3587. +/*
  3588. + * copy-up functions, see wbr_policy.c for copy-down
  3589. + */
  3590. +
  3591. +#include <linux/fs_stack.h>
  3592. +#include <linux/mm.h>
  3593. +#include "aufs.h"
  3594. +
  3595. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3596. +{
  3597. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3598. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3599. +
  3600. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3601. +
  3602. + dst->i_flags |= iflags & ~mask;
  3603. + if (au_test_fs_notime(dst->i_sb))
  3604. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3605. +}
  3606. +
  3607. +void au_cpup_attr_timesizes(struct inode *inode)
  3608. +{
  3609. + struct inode *h_inode;
  3610. +
  3611. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  3612. + fsstack_copy_attr_times(inode, h_inode);
  3613. + fsstack_copy_inode_size(inode, h_inode);
  3614. +}
  3615. +
  3616. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3617. +{
  3618. + struct inode *h_inode;
  3619. + struct super_block *sb;
  3620. + aufs_bindex_t bindex, bend;
  3621. +
  3622. + sb = inode->i_sb;
  3623. + bindex = au_ibstart(inode);
  3624. + h_inode = au_h_iptr(inode, bindex);
  3625. + if (!force
  3626. + && !S_ISDIR(h_inode->i_mode)
  3627. + && au_opt_test(au_mntflags(sb), PLINK)
  3628. + && au_plink_test(inode))
  3629. + return;
  3630. +
  3631. + /*
  3632. + * 0 can happen in revalidating.
  3633. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3634. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3635. + * case.
  3636. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3637. + * the incorrect link count.
  3638. + */
  3639. + set_nlink(inode, h_inode->i_nlink);
  3640. +
  3641. + /*
  3642. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3643. + * it may includes whplink directory.
  3644. + */
  3645. + if (S_ISDIR(h_inode->i_mode)) {
  3646. + bend = au_ibend(inode);
  3647. + for (bindex++; bindex <= bend; bindex++) {
  3648. + h_inode = au_h_iptr(inode, bindex);
  3649. + if (h_inode)
  3650. + au_add_nlink(inode, h_inode);
  3651. + }
  3652. + }
  3653. +}
  3654. +
  3655. +void au_cpup_attr_changeable(struct inode *inode)
  3656. +{
  3657. + struct inode *h_inode;
  3658. +
  3659. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  3660. + inode->i_mode = h_inode->i_mode;
  3661. + inode->i_uid = h_inode->i_uid;
  3662. + inode->i_gid = h_inode->i_gid;
  3663. + au_cpup_attr_timesizes(inode);
  3664. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3665. +}
  3666. +
  3667. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3668. +{
  3669. + struct au_iinfo *iinfo = au_ii(inode);
  3670. +
  3671. + IiMustWriteLock(inode);
  3672. +
  3673. + iinfo->ii_higen = h_inode->i_generation;
  3674. + iinfo->ii_hsb1 = h_inode->i_sb;
  3675. +}
  3676. +
  3677. +void au_cpup_attr_all(struct inode *inode, int force)
  3678. +{
  3679. + struct inode *h_inode;
  3680. +
  3681. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  3682. + au_cpup_attr_changeable(inode);
  3683. + if (inode->i_nlink > 0)
  3684. + au_cpup_attr_nlink(inode, force);
  3685. + inode->i_rdev = h_inode->i_rdev;
  3686. + inode->i_blkbits = h_inode->i_blkbits;
  3687. + au_cpup_igen(inode, h_inode);
  3688. +}
  3689. +
  3690. +/* ---------------------------------------------------------------------- */
  3691. +
  3692. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3693. +
  3694. +/* keep the timestamps of the parent dir when cpup */
  3695. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3696. + struct path *h_path)
  3697. +{
  3698. + struct inode *h_inode;
  3699. +
  3700. + dt->dt_dentry = dentry;
  3701. + dt->dt_h_path = *h_path;
  3702. + h_inode = d_inode(h_path->dentry);
  3703. + dt->dt_atime = h_inode->i_atime;
  3704. + dt->dt_mtime = h_inode->i_mtime;
  3705. + /* smp_mb(); */
  3706. +}
  3707. +
  3708. +void au_dtime_revert(struct au_dtime *dt)
  3709. +{
  3710. + struct iattr attr;
  3711. + int err;
  3712. +
  3713. + attr.ia_atime = dt->dt_atime;
  3714. + attr.ia_mtime = dt->dt_mtime;
  3715. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3716. + | ATTR_ATIME | ATTR_ATIME_SET;
  3717. +
  3718. + /* no delegation since this is a directory */
  3719. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3720. + if (unlikely(err))
  3721. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3722. +}
  3723. +
  3724. +/* ---------------------------------------------------------------------- */
  3725. +
  3726. +/* internal use only */
  3727. +struct au_cpup_reg_attr {
  3728. + int valid;
  3729. + struct kstat st;
  3730. + unsigned int iflags; /* inode->i_flags */
  3731. +};
  3732. +
  3733. +static noinline_for_stack
  3734. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  3735. + struct au_cpup_reg_attr *h_src_attr)
  3736. +{
  3737. + int err, sbits, icex;
  3738. + unsigned int mnt_flags;
  3739. + unsigned char verbose;
  3740. + struct iattr ia;
  3741. + struct path h_path;
  3742. + struct inode *h_isrc, *h_idst;
  3743. + struct kstat *h_st;
  3744. + struct au_branch *br;
  3745. +
  3746. + h_path.dentry = au_h_dptr(dst, bindex);
  3747. + h_idst = d_inode(h_path.dentry);
  3748. + br = au_sbr(dst->d_sb, bindex);
  3749. + h_path.mnt = au_br_mnt(br);
  3750. + h_isrc = d_inode(h_src);
  3751. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  3752. + | ATTR_ATIME | ATTR_MTIME
  3753. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  3754. + if (h_src_attr && h_src_attr->valid) {
  3755. + h_st = &h_src_attr->st;
  3756. + ia.ia_uid = h_st->uid;
  3757. + ia.ia_gid = h_st->gid;
  3758. + ia.ia_atime = h_st->atime;
  3759. + ia.ia_mtime = h_st->mtime;
  3760. + if (h_idst->i_mode != h_st->mode
  3761. + && !S_ISLNK(h_idst->i_mode)) {
  3762. + ia.ia_valid |= ATTR_MODE;
  3763. + ia.ia_mode = h_st->mode;
  3764. + }
  3765. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  3766. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  3767. + } else {
  3768. + ia.ia_uid = h_isrc->i_uid;
  3769. + ia.ia_gid = h_isrc->i_gid;
  3770. + ia.ia_atime = h_isrc->i_atime;
  3771. + ia.ia_mtime = h_isrc->i_mtime;
  3772. + if (h_idst->i_mode != h_isrc->i_mode
  3773. + && !S_ISLNK(h_idst->i_mode)) {
  3774. + ia.ia_valid |= ATTR_MODE;
  3775. + ia.ia_mode = h_isrc->i_mode;
  3776. + }
  3777. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  3778. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  3779. + }
  3780. + /* no delegation since it is just created */
  3781. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3782. +
  3783. + /* is this nfs only? */
  3784. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  3785. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  3786. + ia.ia_mode = h_isrc->i_mode;
  3787. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3788. + }
  3789. +
  3790. + icex = br->br_perm & AuBrAttr_ICEX;
  3791. + if (!err) {
  3792. + mnt_flags = au_mntflags(dst->d_sb);
  3793. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3794. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  3795. + }
  3796. +
  3797. + return err;
  3798. +}
  3799. +
  3800. +/* ---------------------------------------------------------------------- */
  3801. +
  3802. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  3803. + char *buf, unsigned long blksize)
  3804. +{
  3805. + int err;
  3806. + size_t sz, rbytes, wbytes;
  3807. + unsigned char all_zero;
  3808. + char *p, *zp;
  3809. + struct mutex *h_mtx;
  3810. + /* reduce stack usage */
  3811. + struct iattr *ia;
  3812. +
  3813. + zp = page_address(ZERO_PAGE(0));
  3814. + if (unlikely(!zp))
  3815. + return -ENOMEM; /* possible? */
  3816. +
  3817. + err = 0;
  3818. + all_zero = 0;
  3819. + while (len) {
  3820. + AuDbg("len %lld\n", len);
  3821. + sz = blksize;
  3822. + if (len < blksize)
  3823. + sz = len;
  3824. +
  3825. + rbytes = 0;
  3826. + /* todo: signal_pending? */
  3827. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  3828. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  3829. + err = rbytes;
  3830. + }
  3831. + if (unlikely(err < 0))
  3832. + break;
  3833. +
  3834. + all_zero = 0;
  3835. + if (len >= rbytes && rbytes == blksize)
  3836. + all_zero = !memcmp(buf, zp, rbytes);
  3837. + if (!all_zero) {
  3838. + wbytes = rbytes;
  3839. + p = buf;
  3840. + while (wbytes) {
  3841. + size_t b;
  3842. +
  3843. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  3844. + err = b;
  3845. + /* todo: signal_pending? */
  3846. + if (unlikely(err == -EAGAIN || err == -EINTR))
  3847. + continue;
  3848. + if (unlikely(err < 0))
  3849. + break;
  3850. + wbytes -= b;
  3851. + p += b;
  3852. + }
  3853. + if (unlikely(err < 0))
  3854. + break;
  3855. + } else {
  3856. + loff_t res;
  3857. +
  3858. + AuLabel(hole);
  3859. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  3860. + err = res;
  3861. + if (unlikely(res < 0))
  3862. + break;
  3863. + }
  3864. + len -= rbytes;
  3865. + err = 0;
  3866. + }
  3867. +
  3868. + /* the last block may be a hole */
  3869. + if (!err && all_zero) {
  3870. + AuLabel(last hole);
  3871. +
  3872. + err = 1;
  3873. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  3874. + /* nfs requires this step to make last hole */
  3875. + /* is this only nfs? */
  3876. + do {
  3877. + /* todo: signal_pending? */
  3878. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  3879. + } while (err == -EAGAIN || err == -EINTR);
  3880. + if (err == 1)
  3881. + dst->f_pos--;
  3882. + }
  3883. +
  3884. + if (err == 1) {
  3885. + ia = (void *)buf;
  3886. + ia->ia_size = dst->f_pos;
  3887. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  3888. + ia->ia_file = dst;
  3889. + h_mtx = &file_inode(dst)->i_mutex;
  3890. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  3891. + /* no delegation since it is just created */
  3892. + err = vfsub_notify_change(&dst->f_path, ia,
  3893. + /*delegated*/NULL);
  3894. + mutex_unlock(h_mtx);
  3895. + }
  3896. + }
  3897. +
  3898. + return err;
  3899. +}
  3900. +
  3901. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  3902. +{
  3903. + int err;
  3904. + unsigned long blksize;
  3905. + unsigned char do_kfree;
  3906. + char *buf;
  3907. +
  3908. + err = -ENOMEM;
  3909. + blksize = dst->f_path.dentry->d_sb->s_blocksize;
  3910. + if (!blksize || PAGE_SIZE < blksize)
  3911. + blksize = PAGE_SIZE;
  3912. + AuDbg("blksize %lu\n", blksize);
  3913. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  3914. + if (do_kfree)
  3915. + buf = kmalloc(blksize, GFP_NOFS);
  3916. + else
  3917. + buf = (void *)__get_free_page(GFP_NOFS);
  3918. + if (unlikely(!buf))
  3919. + goto out;
  3920. +
  3921. + if (len > (1 << 22))
  3922. + AuDbg("copying a large file %lld\n", (long long)len);
  3923. +
  3924. + src->f_pos = 0;
  3925. + dst->f_pos = 0;
  3926. + err = au_do_copy_file(dst, src, len, buf, blksize);
  3927. + if (do_kfree)
  3928. + kfree(buf);
  3929. + else
  3930. + free_page((unsigned long)buf);
  3931. +
  3932. +out:
  3933. + return err;
  3934. +}
  3935. +
  3936. +/*
  3937. + * to support a sparse file which is opened with O_APPEND,
  3938. + * we need to close the file.
  3939. + */
  3940. +static int au_cp_regular(struct au_cp_generic *cpg)
  3941. +{
  3942. + int err, i;
  3943. + enum { SRC, DST };
  3944. + struct {
  3945. + aufs_bindex_t bindex;
  3946. + unsigned int flags;
  3947. + struct dentry *dentry;
  3948. + int force_wr;
  3949. + struct file *file;
  3950. + void *label;
  3951. + } *f, file[] = {
  3952. + {
  3953. + .bindex = cpg->bsrc,
  3954. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  3955. + .label = &&out
  3956. + },
  3957. + {
  3958. + .bindex = cpg->bdst,
  3959. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  3960. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  3961. + .label = &&out_src
  3962. + }
  3963. + };
  3964. + struct super_block *sb;
  3965. +
  3966. + /* bsrc branch can be ro/rw. */
  3967. + sb = cpg->dentry->d_sb;
  3968. + f = file;
  3969. + for (i = 0; i < 2; i++, f++) {
  3970. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  3971. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  3972. + /*file*/NULL, f->force_wr);
  3973. + err = PTR_ERR(f->file);
  3974. + if (IS_ERR(f->file))
  3975. + goto *f->label;
  3976. + }
  3977. +
  3978. + /* try stopping to update while we copyup */
  3979. + IMustLock(d_inode(file[SRC].dentry));
  3980. + err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
  3981. +
  3982. + fput(file[DST].file);
  3983. + au_sbr_put(sb, file[DST].bindex);
  3984. +
  3985. +out_src:
  3986. + fput(file[SRC].file);
  3987. + au_sbr_put(sb, file[SRC].bindex);
  3988. +out:
  3989. + return err;
  3990. +}
  3991. +
  3992. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  3993. + struct au_cpup_reg_attr *h_src_attr)
  3994. +{
  3995. + int err, rerr;
  3996. + loff_t l;
  3997. + struct path h_path;
  3998. + struct inode *h_src_inode, *h_dst_inode;
  3999. +
  4000. + err = 0;
  4001. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4002. + l = i_size_read(h_src_inode);
  4003. + if (cpg->len == -1 || l < cpg->len)
  4004. + cpg->len = l;
  4005. + if (cpg->len) {
  4006. + /* try stopping to update while we are referencing */
  4007. + mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
  4008. + au_pin_hdir_unlock(cpg->pin);
  4009. +
  4010. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4011. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4012. + h_src_attr->iflags = h_src_inode->i_flags;
  4013. + if (!au_test_nfs(h_src_inode->i_sb))
  4014. + err = vfs_getattr(&h_path, &h_src_attr->st);
  4015. + else {
  4016. + mutex_unlock(&h_src_inode->i_mutex);
  4017. + err = vfs_getattr(&h_path, &h_src_attr->st);
  4018. + mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
  4019. + }
  4020. + if (unlikely(err)) {
  4021. + mutex_unlock(&h_src_inode->i_mutex);
  4022. + goto out;
  4023. + }
  4024. + h_src_attr->valid = 1;
  4025. + err = au_cp_regular(cpg);
  4026. + mutex_unlock(&h_src_inode->i_mutex);
  4027. + rerr = au_pin_hdir_relock(cpg->pin);
  4028. + if (!err && rerr)
  4029. + err = rerr;
  4030. + }
  4031. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4032. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4033. + h_dst_inode = d_inode(h_path.dentry);
  4034. + spin_lock(&h_dst_inode->i_lock);
  4035. + h_dst_inode->i_state |= I_LINKABLE;
  4036. + spin_unlock(&h_dst_inode->i_lock);
  4037. + }
  4038. +
  4039. +out:
  4040. + return err;
  4041. +}
  4042. +
  4043. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4044. + struct inode *h_dir)
  4045. +{
  4046. + int err, symlen;
  4047. + mm_segment_t old_fs;
  4048. + union {
  4049. + char *k;
  4050. + char __user *u;
  4051. + } sym;
  4052. + struct inode *h_inode = d_inode(h_src);
  4053. + const struct inode_operations *h_iop = h_inode->i_op;
  4054. +
  4055. + err = -ENOSYS;
  4056. + if (unlikely(!h_iop->readlink))
  4057. + goto out;
  4058. +
  4059. + err = -ENOMEM;
  4060. + sym.k = (void *)__get_free_page(GFP_NOFS);
  4061. + if (unlikely(!sym.k))
  4062. + goto out;
  4063. +
  4064. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  4065. + old_fs = get_fs();
  4066. + set_fs(KERNEL_DS);
  4067. + symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
  4068. + err = symlen;
  4069. + set_fs(old_fs);
  4070. +
  4071. + if (symlen > 0) {
  4072. + sym.k[symlen] = 0;
  4073. + err = vfsub_symlink(h_dir, h_path, sym.k);
  4074. + }
  4075. + free_page((unsigned long)sym.k);
  4076. +
  4077. +out:
  4078. + return err;
  4079. +}
  4080. +
  4081. +static noinline_for_stack
  4082. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4083. + struct au_cpup_reg_attr *h_src_attr)
  4084. +{
  4085. + int err;
  4086. + umode_t mode;
  4087. + unsigned int mnt_flags;
  4088. + unsigned char isdir, isreg, force;
  4089. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4090. + struct au_dtime dt;
  4091. + struct path h_path;
  4092. + struct dentry *h_src, *h_dst, *h_parent;
  4093. + struct inode *h_inode, *h_dir, *dir, *inode;
  4094. + struct super_block *sb;
  4095. +
  4096. + /* bsrc branch can be ro/rw. */
  4097. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4098. + h_inode = d_inode(h_src);
  4099. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4100. +
  4101. + /* try stopping to be referenced while we are creating */
  4102. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4103. + if (au_ftest_cpup(cpg->flags, RENAME))
  4104. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4105. + AUFS_WH_PFX_LEN));
  4106. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4107. + h_dir = d_inode(h_parent);
  4108. + IMustLock(h_dir);
  4109. + AuDebugOn(h_parent != h_dst->d_parent);
  4110. +
  4111. + sb = cpg->dentry->d_sb;
  4112. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4113. + if (do_dt) {
  4114. + h_path.dentry = h_parent;
  4115. + au_dtime_store(&dt, dst_parent, &h_path);
  4116. + }
  4117. + h_path.dentry = h_dst;
  4118. +
  4119. + isreg = 0;
  4120. + isdir = 0;
  4121. + mode = h_inode->i_mode;
  4122. + switch (mode & S_IFMT) {
  4123. + case S_IFREG:
  4124. + isreg = 1;
  4125. + err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
  4126. + /*want_excl*/true);
  4127. + if (!err)
  4128. + err = au_do_cpup_regular(cpg, h_src_attr);
  4129. + break;
  4130. + case S_IFDIR:
  4131. + isdir = 1;
  4132. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4133. + if (!err) {
  4134. + /*
  4135. + * strange behaviour from the users view,
  4136. + * particularry setattr case
  4137. + */
  4138. + dir = d_inode(dst_parent);
  4139. + if (au_ibstart(dir) == cpg->bdst)
  4140. + au_cpup_attr_nlink(dir, /*force*/1);
  4141. + inode = d_inode(cpg->dentry);
  4142. + au_cpup_attr_nlink(inode, /*force*/1);
  4143. + }
  4144. + break;
  4145. + case S_IFLNK:
  4146. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4147. + break;
  4148. + case S_IFCHR:
  4149. + case S_IFBLK:
  4150. + AuDebugOn(!capable(CAP_MKNOD));
  4151. + /*FALLTHROUGH*/
  4152. + case S_IFIFO:
  4153. + case S_IFSOCK:
  4154. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4155. + break;
  4156. + default:
  4157. + AuIOErr("Unknown inode type 0%o\n", mode);
  4158. + err = -EIO;
  4159. + }
  4160. +
  4161. + mnt_flags = au_mntflags(sb);
  4162. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4163. + && !isdir
  4164. + && au_opt_test(mnt_flags, XINO)
  4165. + && (h_inode->i_nlink == 1
  4166. + || (h_inode->i_state & I_LINKABLE))
  4167. + /* todo: unnecessary? */
  4168. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4169. + && cpg->bdst < cpg->bsrc
  4170. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4171. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4172. + /* ignore this error */
  4173. +
  4174. + if (!err) {
  4175. + force = 0;
  4176. + if (isreg) {
  4177. + force = !!cpg->len;
  4178. + if (cpg->len == -1)
  4179. + force = !!i_size_read(h_inode);
  4180. + }
  4181. + au_fhsm_wrote(sb, cpg->bdst, force);
  4182. + }
  4183. +
  4184. + if (do_dt)
  4185. + au_dtime_revert(&dt);
  4186. + return err;
  4187. +}
  4188. +
  4189. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4190. +{
  4191. + int err;
  4192. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4193. + struct inode *h_dir;
  4194. + aufs_bindex_t bdst;
  4195. +
  4196. + dentry = cpg->dentry;
  4197. + bdst = cpg->bdst;
  4198. + h_dentry = au_h_dptr(dentry, bdst);
  4199. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4200. + dget(h_dentry);
  4201. + au_set_h_dptr(dentry, bdst, NULL);
  4202. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4203. + if (!err)
  4204. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4205. + au_set_h_dptr(dentry, bdst, h_dentry);
  4206. + } else {
  4207. + err = 0;
  4208. + parent = dget_parent(dentry);
  4209. + h_parent = au_h_dptr(parent, bdst);
  4210. + dput(parent);
  4211. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  4212. + if (IS_ERR(h_path->dentry))
  4213. + err = PTR_ERR(h_path->dentry);
  4214. + }
  4215. + if (unlikely(err))
  4216. + goto out;
  4217. +
  4218. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4219. + h_dir = d_inode(h_parent);
  4220. + IMustLock(h_dir);
  4221. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4222. + /* no delegation since it is just created */
  4223. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
  4224. + dput(h_path->dentry);
  4225. +
  4226. +out:
  4227. + return err;
  4228. +}
  4229. +
  4230. +/*
  4231. + * copyup the @dentry from @bsrc to @bdst.
  4232. + * the caller must set the both of lower dentries.
  4233. + * @len is for truncating when it is -1 copyup the entire file.
  4234. + * in link/rename cases, @dst_parent may be different from the real one.
  4235. + * basic->bsrc can be larger than basic->bdst.
  4236. + */
  4237. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4238. +{
  4239. + int err, rerr;
  4240. + aufs_bindex_t old_ibstart;
  4241. + unsigned char isdir, plink;
  4242. + struct dentry *h_src, *h_dst, *h_parent;
  4243. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4244. + struct super_block *sb;
  4245. + struct au_branch *br;
  4246. + /* to reuduce stack size */
  4247. + struct {
  4248. + struct au_dtime dt;
  4249. + struct path h_path;
  4250. + struct au_cpup_reg_attr h_src_attr;
  4251. + } *a;
  4252. +
  4253. + err = -ENOMEM;
  4254. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4255. + if (unlikely(!a))
  4256. + goto out;
  4257. + a->h_src_attr.valid = 0;
  4258. +
  4259. + sb = cpg->dentry->d_sb;
  4260. + br = au_sbr(sb, cpg->bdst);
  4261. + a->h_path.mnt = au_br_mnt(br);
  4262. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4263. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4264. + h_dir = d_inode(h_parent);
  4265. + IMustLock(h_dir);
  4266. +
  4267. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4268. + inode = d_inode(cpg->dentry);
  4269. +
  4270. + if (!dst_parent)
  4271. + dst_parent = dget_parent(cpg->dentry);
  4272. + else
  4273. + dget(dst_parent);
  4274. +
  4275. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4276. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4277. + if (dst_inode) {
  4278. + if (unlikely(!plink)) {
  4279. + err = -EIO;
  4280. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4281. + "but plink is disabled\n",
  4282. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4283. + goto out_parent;
  4284. + }
  4285. +
  4286. + if (dst_inode->i_nlink) {
  4287. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4288. +
  4289. + h_src = au_plink_lkup(inode, cpg->bdst);
  4290. + err = PTR_ERR(h_src);
  4291. + if (IS_ERR(h_src))
  4292. + goto out_parent;
  4293. + if (unlikely(d_is_negative(h_src))) {
  4294. + err = -EIO;
  4295. + AuIOErr("i%lu exists on a upper branch "
  4296. + "but not pseudo-linked\n",
  4297. + inode->i_ino);
  4298. + dput(h_src);
  4299. + goto out_parent;
  4300. + }
  4301. +
  4302. + if (do_dt) {
  4303. + a->h_path.dentry = h_parent;
  4304. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4305. + }
  4306. +
  4307. + a->h_path.dentry = h_dst;
  4308. + delegated = NULL;
  4309. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4310. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4311. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4312. + if (do_dt)
  4313. + au_dtime_revert(&a->dt);
  4314. + if (unlikely(err == -EWOULDBLOCK)) {
  4315. + pr_warn("cannot retry for NFSv4 delegation"
  4316. + " for an internal link\n");
  4317. + iput(delegated);
  4318. + }
  4319. + dput(h_src);
  4320. + goto out_parent;
  4321. + } else
  4322. + /* todo: cpup_wh_file? */
  4323. + /* udba work */
  4324. + au_update_ibrange(inode, /*do_put_zero*/1);
  4325. + }
  4326. +
  4327. + isdir = S_ISDIR(inode->i_mode);
  4328. + old_ibstart = au_ibstart(inode);
  4329. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4330. + if (unlikely(err))
  4331. + goto out_rev;
  4332. + dst_inode = d_inode(h_dst);
  4333. + mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
  4334. + /* todo: necessary? */
  4335. + /* au_pin_hdir_unlock(cpg->pin); */
  4336. +
  4337. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  4338. + if (unlikely(err)) {
  4339. + /* todo: necessary? */
  4340. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4341. + mutex_unlock(&dst_inode->i_mutex);
  4342. + goto out_rev;
  4343. + }
  4344. +
  4345. + if (cpg->bdst < old_ibstart) {
  4346. + if (S_ISREG(inode->i_mode)) {
  4347. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4348. + if (unlikely(err)) {
  4349. + /* ignore an error */
  4350. + /* au_pin_hdir_relock(cpg->pin); */
  4351. + mutex_unlock(&dst_inode->i_mutex);
  4352. + goto out_rev;
  4353. + }
  4354. + }
  4355. + au_set_ibstart(inode, cpg->bdst);
  4356. + } else
  4357. + au_set_ibend(inode, cpg->bdst);
  4358. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4359. + au_hi_flags(inode, isdir));
  4360. +
  4361. + /* todo: necessary? */
  4362. + /* err = au_pin_hdir_relock(cpg->pin); */
  4363. + mutex_unlock(&dst_inode->i_mutex);
  4364. + if (unlikely(err))
  4365. + goto out_rev;
  4366. +
  4367. + src_inode = d_inode(h_src);
  4368. + if (!isdir
  4369. + && (src_inode->i_nlink > 1
  4370. + || src_inode->i_state & I_LINKABLE)
  4371. + && plink)
  4372. + au_plink_append(inode, cpg->bdst, h_dst);
  4373. +
  4374. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4375. + a->h_path.dentry = h_dst;
  4376. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4377. + }
  4378. + if (!err)
  4379. + goto out_parent; /* success */
  4380. +
  4381. + /* revert */
  4382. +out_rev:
  4383. + a->h_path.dentry = h_parent;
  4384. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4385. + a->h_path.dentry = h_dst;
  4386. + rerr = 0;
  4387. + if (d_is_positive(h_dst)) {
  4388. + if (!isdir) {
  4389. + /* no delegation since it is just created */
  4390. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4391. + /*delegated*/NULL, /*force*/0);
  4392. + } else
  4393. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4394. + }
  4395. + au_dtime_revert(&a->dt);
  4396. + if (rerr) {
  4397. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4398. + err = -EIO;
  4399. + }
  4400. +out_parent:
  4401. + dput(dst_parent);
  4402. + kfree(a);
  4403. +out:
  4404. + return err;
  4405. +}
  4406. +
  4407. +#if 0 /* reserved */
  4408. +struct au_cpup_single_args {
  4409. + int *errp;
  4410. + struct au_cp_generic *cpg;
  4411. + struct dentry *dst_parent;
  4412. +};
  4413. +
  4414. +static void au_call_cpup_single(void *args)
  4415. +{
  4416. + struct au_cpup_single_args *a = args;
  4417. +
  4418. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4419. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4420. + au_pin_hdir_release(a->cpg->pin);
  4421. +}
  4422. +#endif
  4423. +
  4424. +/*
  4425. + * prevent SIGXFSZ in copy-up.
  4426. + * testing CAP_MKNOD is for generic fs,
  4427. + * but CAP_FSETID is for xfs only, currently.
  4428. + */
  4429. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4430. +{
  4431. + int do_sio;
  4432. + struct super_block *sb;
  4433. + struct inode *h_dir;
  4434. +
  4435. + do_sio = 0;
  4436. + sb = au_pinned_parent(pin)->d_sb;
  4437. + if (!au_wkq_test()
  4438. + && (!au_sbi(sb)->si_plink_maint_pid
  4439. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4440. + switch (mode & S_IFMT) {
  4441. + case S_IFREG:
  4442. + /* no condition about RLIMIT_FSIZE and the file size */
  4443. + do_sio = 1;
  4444. + break;
  4445. + case S_IFCHR:
  4446. + case S_IFBLK:
  4447. + do_sio = !capable(CAP_MKNOD);
  4448. + break;
  4449. + }
  4450. + if (!do_sio)
  4451. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4452. + && !capable(CAP_FSETID));
  4453. + /* this workaround may be removed in the future */
  4454. + if (!do_sio) {
  4455. + h_dir = au_pinned_h_dir(pin);
  4456. + do_sio = h_dir->i_mode & S_ISVTX;
  4457. + }
  4458. + }
  4459. +
  4460. + return do_sio;
  4461. +}
  4462. +
  4463. +#if 0 /* reserved */
  4464. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4465. +{
  4466. + int err, wkq_err;
  4467. + struct dentry *h_dentry;
  4468. +
  4469. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4470. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4471. + err = au_cpup_single(cpg, dst_parent);
  4472. + else {
  4473. + struct au_cpup_single_args args = {
  4474. + .errp = &err,
  4475. + .cpg = cpg,
  4476. + .dst_parent = dst_parent
  4477. + };
  4478. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4479. + if (unlikely(wkq_err))
  4480. + err = wkq_err;
  4481. + }
  4482. +
  4483. + return err;
  4484. +}
  4485. +#endif
  4486. +
  4487. +/*
  4488. + * copyup the @dentry from the first active lower branch to @bdst,
  4489. + * using au_cpup_single().
  4490. + */
  4491. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4492. +{
  4493. + int err;
  4494. + unsigned int flags_orig;
  4495. + struct dentry *dentry;
  4496. +
  4497. + AuDebugOn(cpg->bsrc < 0);
  4498. +
  4499. + dentry = cpg->dentry;
  4500. + DiMustWriteLock(dentry);
  4501. +
  4502. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4503. + if (!err) {
  4504. + flags_orig = cpg->flags;
  4505. + au_fset_cpup(cpg->flags, RENAME);
  4506. + err = au_cpup_single(cpg, NULL);
  4507. + cpg->flags = flags_orig;
  4508. + if (!err)
  4509. + return 0; /* success */
  4510. +
  4511. + /* revert */
  4512. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4513. + au_set_dbstart(dentry, cpg->bsrc);
  4514. + }
  4515. +
  4516. + return err;
  4517. +}
  4518. +
  4519. +struct au_cpup_simple_args {
  4520. + int *errp;
  4521. + struct au_cp_generic *cpg;
  4522. +};
  4523. +
  4524. +static void au_call_cpup_simple(void *args)
  4525. +{
  4526. + struct au_cpup_simple_args *a = args;
  4527. +
  4528. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4529. + *a->errp = au_cpup_simple(a->cpg);
  4530. + au_pin_hdir_release(a->cpg->pin);
  4531. +}
  4532. +
  4533. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4534. +{
  4535. + int err, wkq_err;
  4536. + struct dentry *dentry, *parent;
  4537. + struct file *h_file;
  4538. + struct inode *h_dir;
  4539. +
  4540. + dentry = cpg->dentry;
  4541. + h_file = NULL;
  4542. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4543. + AuDebugOn(cpg->bsrc < 0);
  4544. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4545. + err = PTR_ERR(h_file);
  4546. + if (IS_ERR(h_file))
  4547. + goto out;
  4548. + }
  4549. +
  4550. + parent = dget_parent(dentry);
  4551. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4552. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  4553. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4554. + err = au_cpup_simple(cpg);
  4555. + else {
  4556. + struct au_cpup_simple_args args = {
  4557. + .errp = &err,
  4558. + .cpg = cpg
  4559. + };
  4560. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4561. + if (unlikely(wkq_err))
  4562. + err = wkq_err;
  4563. + }
  4564. +
  4565. + dput(parent);
  4566. + if (h_file)
  4567. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4568. +
  4569. +out:
  4570. + return err;
  4571. +}
  4572. +
  4573. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4574. +{
  4575. + aufs_bindex_t bsrc, bend;
  4576. + struct dentry *dentry, *h_dentry;
  4577. +
  4578. + if (cpg->bsrc < 0) {
  4579. + dentry = cpg->dentry;
  4580. + bend = au_dbend(dentry);
  4581. + for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
  4582. + h_dentry = au_h_dptr(dentry, bsrc);
  4583. + if (h_dentry) {
  4584. + AuDebugOn(d_is_negative(h_dentry));
  4585. + break;
  4586. + }
  4587. + }
  4588. + AuDebugOn(bsrc > bend);
  4589. + cpg->bsrc = bsrc;
  4590. + }
  4591. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4592. + return au_do_sio_cpup_simple(cpg);
  4593. +}
  4594. +
  4595. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4596. +{
  4597. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4598. + return au_do_sio_cpup_simple(cpg);
  4599. +}
  4600. +
  4601. +/* ---------------------------------------------------------------------- */
  4602. +
  4603. +/*
  4604. + * copyup the deleted file for writing.
  4605. + */
  4606. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  4607. + struct file *file)
  4608. +{
  4609. + int err;
  4610. + unsigned int flags_orig;
  4611. + aufs_bindex_t bsrc_orig;
  4612. + struct dentry *h_d_dst, *h_d_start;
  4613. + struct au_dinfo *dinfo;
  4614. + struct au_hdentry *hdp;
  4615. +
  4616. + dinfo = au_di(cpg->dentry);
  4617. + AuRwMustWriteLock(&dinfo->di_rwsem);
  4618. +
  4619. + bsrc_orig = cpg->bsrc;
  4620. + cpg->bsrc = dinfo->di_bstart;
  4621. + hdp = dinfo->di_hdentry;
  4622. + h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
  4623. + dinfo->di_bstart = cpg->bdst;
  4624. + hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
  4625. + h_d_start = NULL;
  4626. + if (file) {
  4627. + h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
  4628. + hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
  4629. + }
  4630. + flags_orig = cpg->flags;
  4631. + cpg->flags = !AuCpup_DTIME;
  4632. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  4633. + cpg->flags = flags_orig;
  4634. + if (file) {
  4635. + if (!err)
  4636. + err = au_reopen_nondir(file);
  4637. + hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
  4638. + }
  4639. + hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
  4640. + dinfo->di_bstart = cpg->bsrc;
  4641. + cpg->bsrc = bsrc_orig;
  4642. +
  4643. + return err;
  4644. +}
  4645. +
  4646. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4647. +{
  4648. + int err;
  4649. + aufs_bindex_t bdst;
  4650. + struct au_dtime dt;
  4651. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  4652. + struct au_branch *br;
  4653. + struct path h_path;
  4654. +
  4655. + dentry = cpg->dentry;
  4656. + bdst = cpg->bdst;
  4657. + br = au_sbr(dentry->d_sb, bdst);
  4658. + parent = dget_parent(dentry);
  4659. + h_parent = au_h_dptr(parent, bdst);
  4660. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  4661. + err = PTR_ERR(wh_dentry);
  4662. + if (IS_ERR(wh_dentry))
  4663. + goto out;
  4664. +
  4665. + h_path.dentry = h_parent;
  4666. + h_path.mnt = au_br_mnt(br);
  4667. + au_dtime_store(&dt, parent, &h_path);
  4668. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  4669. + if (unlikely(err))
  4670. + goto out_wh;
  4671. +
  4672. + dget(wh_dentry);
  4673. + h_path.dentry = wh_dentry;
  4674. + if (!d_is_dir(wh_dentry)) {
  4675. + /* no delegation since it is just created */
  4676. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  4677. + /*delegated*/NULL, /*force*/0);
  4678. + } else
  4679. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  4680. + if (unlikely(err)) {
  4681. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  4682. + wh_dentry, err);
  4683. + err = -EIO;
  4684. + }
  4685. + au_dtime_revert(&dt);
  4686. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  4687. +
  4688. +out_wh:
  4689. + dput(wh_dentry);
  4690. +out:
  4691. + dput(parent);
  4692. + return err;
  4693. +}
  4694. +
  4695. +struct au_cpup_wh_args {
  4696. + int *errp;
  4697. + struct au_cp_generic *cpg;
  4698. + struct file *file;
  4699. +};
  4700. +
  4701. +static void au_call_cpup_wh(void *args)
  4702. +{
  4703. + struct au_cpup_wh_args *a = args;
  4704. +
  4705. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4706. + *a->errp = au_cpup_wh(a->cpg, a->file);
  4707. + au_pin_hdir_release(a->cpg->pin);
  4708. +}
  4709. +
  4710. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4711. +{
  4712. + int err, wkq_err;
  4713. + aufs_bindex_t bdst;
  4714. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  4715. + struct inode *dir, *h_dir, *h_tmpdir;
  4716. + struct au_wbr *wbr;
  4717. + struct au_pin wh_pin, *pin_orig;
  4718. +
  4719. + dentry = cpg->dentry;
  4720. + bdst = cpg->bdst;
  4721. + parent = dget_parent(dentry);
  4722. + dir = d_inode(parent);
  4723. + h_orph = NULL;
  4724. + h_parent = NULL;
  4725. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  4726. + h_tmpdir = h_dir;
  4727. + pin_orig = NULL;
  4728. + if (!h_dir->i_nlink) {
  4729. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  4730. + h_orph = wbr->wbr_orph;
  4731. +
  4732. + h_parent = dget(au_h_dptr(parent, bdst));
  4733. + au_set_h_dptr(parent, bdst, dget(h_orph));
  4734. + h_tmpdir = d_inode(h_orph);
  4735. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  4736. +
  4737. + mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
  4738. + /* todo: au_h_open_pre()? */
  4739. +
  4740. + pin_orig = cpg->pin;
  4741. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  4742. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  4743. + cpg->pin = &wh_pin;
  4744. + }
  4745. +
  4746. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  4747. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4748. + err = au_cpup_wh(cpg, file);
  4749. + else {
  4750. + struct au_cpup_wh_args args = {
  4751. + .errp = &err,
  4752. + .cpg = cpg,
  4753. + .file = file
  4754. + };
  4755. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  4756. + if (unlikely(wkq_err))
  4757. + err = wkq_err;
  4758. + }
  4759. +
  4760. + if (h_orph) {
  4761. + mutex_unlock(&h_tmpdir->i_mutex);
  4762. + /* todo: au_h_open_post()? */
  4763. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  4764. + au_set_h_dptr(parent, bdst, h_parent);
  4765. + AuDebugOn(!pin_orig);
  4766. + cpg->pin = pin_orig;
  4767. + }
  4768. + iput(h_dir);
  4769. + dput(parent);
  4770. +
  4771. + return err;
  4772. +}
  4773. +
  4774. +/* ---------------------------------------------------------------------- */
  4775. +
  4776. +/*
  4777. + * generic routine for both of copy-up and copy-down.
  4778. + */
  4779. +/* cf. revalidate function in file.c */
  4780. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  4781. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  4782. + struct au_pin *pin,
  4783. + struct dentry *h_parent, void *arg),
  4784. + void *arg)
  4785. +{
  4786. + int err;
  4787. + struct au_pin pin;
  4788. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  4789. +
  4790. + err = 0;
  4791. + parent = dget_parent(dentry);
  4792. + if (IS_ROOT(parent))
  4793. + goto out;
  4794. +
  4795. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  4796. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  4797. +
  4798. + /* do not use au_dpage */
  4799. + real_parent = parent;
  4800. + while (1) {
  4801. + dput(parent);
  4802. + parent = dget_parent(dentry);
  4803. + h_parent = au_h_dptr(parent, bdst);
  4804. + if (h_parent)
  4805. + goto out; /* success */
  4806. +
  4807. + /* find top dir which is necessary to cpup */
  4808. + do {
  4809. + d = parent;
  4810. + dput(parent);
  4811. + parent = dget_parent(d);
  4812. + di_read_lock_parent3(parent, !AuLock_IR);
  4813. + h_parent = au_h_dptr(parent, bdst);
  4814. + di_read_unlock(parent, !AuLock_IR);
  4815. + } while (!h_parent);
  4816. +
  4817. + if (d != real_parent)
  4818. + di_write_lock_child3(d);
  4819. +
  4820. + /* somebody else might create while we were sleeping */
  4821. + h_dentry = au_h_dptr(d, bdst);
  4822. + if (!h_dentry || d_is_negative(h_dentry)) {
  4823. + if (h_dentry)
  4824. + au_update_dbstart(d);
  4825. +
  4826. + au_pin_set_dentry(&pin, d);
  4827. + err = au_do_pin(&pin);
  4828. + if (!err) {
  4829. + err = cp(d, bdst, &pin, h_parent, arg);
  4830. + au_unpin(&pin);
  4831. + }
  4832. + }
  4833. +
  4834. + if (d != real_parent)
  4835. + di_write_unlock(d);
  4836. + if (unlikely(err))
  4837. + break;
  4838. + }
  4839. +
  4840. +out:
  4841. + dput(parent);
  4842. + return err;
  4843. +}
  4844. +
  4845. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  4846. + struct au_pin *pin,
  4847. + struct dentry *h_parent __maybe_unused,
  4848. + void *arg __maybe_unused)
  4849. +{
  4850. + struct au_cp_generic cpg = {
  4851. + .dentry = dentry,
  4852. + .bdst = bdst,
  4853. + .bsrc = -1,
  4854. + .len = 0,
  4855. + .pin = pin,
  4856. + .flags = AuCpup_DTIME
  4857. + };
  4858. + return au_sio_cpup_simple(&cpg);
  4859. +}
  4860. +
  4861. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4862. +{
  4863. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  4864. +}
  4865. +
  4866. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  4867. +{
  4868. + int err;
  4869. + struct dentry *parent;
  4870. + struct inode *dir;
  4871. +
  4872. + parent = dget_parent(dentry);
  4873. + dir = d_inode(parent);
  4874. + err = 0;
  4875. + if (au_h_iptr(dir, bdst))
  4876. + goto out;
  4877. +
  4878. + di_read_unlock(parent, AuLock_IR);
  4879. + di_write_lock_parent(parent);
  4880. + /* someone else might change our inode while we were sleeping */
  4881. + if (!au_h_iptr(dir, bdst))
  4882. + err = au_cpup_dirs(dentry, bdst);
  4883. + di_downgrade_lock(parent, AuLock_IR);
  4884. +
  4885. +out:
  4886. + dput(parent);
  4887. + return err;
  4888. +}
  4889. diff -Nur linux-4.1.10.orig/fs/aufs/cpup.h linux-4.1.10/fs/aufs/cpup.h
  4890. --- linux-4.1.10.orig/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
  4891. +++ linux-4.1.10/fs/aufs/cpup.h 2015-10-22 21:35:53.000000000 +0200
  4892. @@ -0,0 +1,94 @@
  4893. +/*
  4894. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4895. + *
  4896. + * This program, aufs is free software; you can redistribute it and/or modify
  4897. + * it under the terms of the GNU General Public License as published by
  4898. + * the Free Software Foundation; either version 2 of the License, or
  4899. + * (at your option) any later version.
  4900. + *
  4901. + * This program is distributed in the hope that it will be useful,
  4902. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4903. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4904. + * GNU General Public License for more details.
  4905. + *
  4906. + * You should have received a copy of the GNU General Public License
  4907. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  4908. + */
  4909. +
  4910. +/*
  4911. + * copy-up/down functions
  4912. + */
  4913. +
  4914. +#ifndef __AUFS_CPUP_H__
  4915. +#define __AUFS_CPUP_H__
  4916. +
  4917. +#ifdef __KERNEL__
  4918. +
  4919. +#include <linux/path.h>
  4920. +
  4921. +struct inode;
  4922. +struct file;
  4923. +struct au_pin;
  4924. +
  4925. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  4926. +void au_cpup_attr_timesizes(struct inode *inode);
  4927. +void au_cpup_attr_nlink(struct inode *inode, int force);
  4928. +void au_cpup_attr_changeable(struct inode *inode);
  4929. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  4930. +void au_cpup_attr_all(struct inode *inode, int force);
  4931. +
  4932. +/* ---------------------------------------------------------------------- */
  4933. +
  4934. +struct au_cp_generic {
  4935. + struct dentry *dentry;
  4936. + aufs_bindex_t bdst, bsrc;
  4937. + loff_t len;
  4938. + struct au_pin *pin;
  4939. + unsigned int flags;
  4940. +};
  4941. +
  4942. +/* cpup flags */
  4943. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  4944. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  4945. + for link(2) */
  4946. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  4947. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  4948. + cpup */
  4949. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  4950. + existing entry */
  4951. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  4952. + the branch is marked as RO */
  4953. +
  4954. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  4955. +#define au_fset_cpup(flags, name) \
  4956. + do { (flags) |= AuCpup_##name; } while (0)
  4957. +#define au_fclr_cpup(flags, name) \
  4958. + do { (flags) &= ~AuCpup_##name; } while (0)
  4959. +
  4960. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  4961. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  4962. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  4963. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  4964. +
  4965. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  4966. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  4967. + struct au_pin *pin,
  4968. + struct dentry *h_parent, void *arg),
  4969. + void *arg);
  4970. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  4971. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  4972. +
  4973. +/* ---------------------------------------------------------------------- */
  4974. +
  4975. +/* keep timestamps when copyup */
  4976. +struct au_dtime {
  4977. + struct dentry *dt_dentry;
  4978. + struct path dt_h_path;
  4979. + struct timespec dt_atime, dt_mtime;
  4980. +};
  4981. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  4982. + struct path *h_path);
  4983. +void au_dtime_revert(struct au_dtime *dt);
  4984. +
  4985. +#endif /* __KERNEL__ */
  4986. +#endif /* __AUFS_CPUP_H__ */
  4987. diff -Nur linux-4.1.10.orig/fs/aufs/dbgaufs.c linux-4.1.10/fs/aufs/dbgaufs.c
  4988. --- linux-4.1.10.orig/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
  4989. +++ linux-4.1.10/fs/aufs/dbgaufs.c 2015-10-22 21:35:53.000000000 +0200
  4990. @@ -0,0 +1,432 @@
  4991. +/*
  4992. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  4993. + *
  4994. + * This program, aufs is free software; you can redistribute it and/or modify
  4995. + * it under the terms of the GNU General Public License as published by
  4996. + * the Free Software Foundation; either version 2 of the License, or
  4997. + * (at your option) any later version.
  4998. + *
  4999. + * This program is distributed in the hope that it will be useful,
  5000. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5001. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5002. + * GNU General Public License for more details.
  5003. + *
  5004. + * You should have received a copy of the GNU General Public License
  5005. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5006. + */
  5007. +
  5008. +/*
  5009. + * debugfs interface
  5010. + */
  5011. +
  5012. +#include <linux/debugfs.h>
  5013. +#include "aufs.h"
  5014. +
  5015. +#ifndef CONFIG_SYSFS
  5016. +#error DEBUG_FS depends upon SYSFS
  5017. +#endif
  5018. +
  5019. +static struct dentry *dbgaufs;
  5020. +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
  5021. +
  5022. +/* 20 is max digits length of ulong 64 */
  5023. +struct dbgaufs_arg {
  5024. + int n;
  5025. + char a[20 * 4];
  5026. +};
  5027. +
  5028. +/*
  5029. + * common function for all XINO files
  5030. + */
  5031. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5032. + struct file *file)
  5033. +{
  5034. + kfree(file->private_data);
  5035. + return 0;
  5036. +}
  5037. +
  5038. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
  5039. +{
  5040. + int err;
  5041. + struct kstat st;
  5042. + struct dbgaufs_arg *p;
  5043. +
  5044. + err = -ENOMEM;
  5045. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5046. + if (unlikely(!p))
  5047. + goto out;
  5048. +
  5049. + err = 0;
  5050. + p->n = 0;
  5051. + file->private_data = p;
  5052. + if (!xf)
  5053. + goto out;
  5054. +
  5055. + err = vfs_getattr(&xf->f_path, &st);
  5056. + if (!err) {
  5057. + if (do_fcnt)
  5058. + p->n = snprintf
  5059. + (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
  5060. + (long)file_count(xf), st.blocks, st.blksize,
  5061. + (long long)st.size);
  5062. + else
  5063. + p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
  5064. + st.blocks, st.blksize,
  5065. + (long long)st.size);
  5066. + AuDebugOn(p->n >= sizeof(p->a));
  5067. + } else {
  5068. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5069. + err = 0;
  5070. + }
  5071. +
  5072. +out:
  5073. + return err;
  5074. +
  5075. +}
  5076. +
  5077. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5078. + size_t count, loff_t *ppos)
  5079. +{
  5080. + struct dbgaufs_arg *p;
  5081. +
  5082. + p = file->private_data;
  5083. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5084. +}
  5085. +
  5086. +/* ---------------------------------------------------------------------- */
  5087. +
  5088. +struct dbgaufs_plink_arg {
  5089. + int n;
  5090. + char a[];
  5091. +};
  5092. +
  5093. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5094. + struct file *file)
  5095. +{
  5096. + free_page((unsigned long)file->private_data);
  5097. + return 0;
  5098. +}
  5099. +
  5100. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5101. +{
  5102. + int err, i, limit;
  5103. + unsigned long n, sum;
  5104. + struct dbgaufs_plink_arg *p;
  5105. + struct au_sbinfo *sbinfo;
  5106. + struct super_block *sb;
  5107. + struct au_sphlhead *sphl;
  5108. +
  5109. + err = -ENOMEM;
  5110. + p = (void *)get_zeroed_page(GFP_NOFS);
  5111. + if (unlikely(!p))
  5112. + goto out;
  5113. +
  5114. + err = -EFBIG;
  5115. + sbinfo = inode->i_private;
  5116. + sb = sbinfo->si_sb;
  5117. + si_noflush_read_lock(sb);
  5118. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5119. + limit = PAGE_SIZE - sizeof(p->n);
  5120. +
  5121. + /* the number of buckets */
  5122. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5123. + p->n += n;
  5124. + limit -= n;
  5125. +
  5126. + sum = 0;
  5127. + for (i = 0, sphl = sbinfo->si_plink;
  5128. + i < AuPlink_NHASH;
  5129. + i++, sphl++) {
  5130. + n = au_sphl_count(sphl);
  5131. + sum += n;
  5132. +
  5133. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5134. + p->n += n;
  5135. + limit -= n;
  5136. + if (unlikely(limit <= 0))
  5137. + goto out_free;
  5138. + }
  5139. + p->a[p->n - 1] = '\n';
  5140. +
  5141. + /* the sum of plinks */
  5142. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5143. + p->n += n;
  5144. + limit -= n;
  5145. + if (unlikely(limit <= 0))
  5146. + goto out_free;
  5147. + } else {
  5148. +#define str "1\n0\n0\n"
  5149. + p->n = sizeof(str) - 1;
  5150. + strcpy(p->a, str);
  5151. +#undef str
  5152. + }
  5153. + si_read_unlock(sb);
  5154. +
  5155. + err = 0;
  5156. + file->private_data = p;
  5157. + goto out; /* success */
  5158. +
  5159. +out_free:
  5160. + free_page((unsigned long)p);
  5161. +out:
  5162. + return err;
  5163. +}
  5164. +
  5165. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5166. + size_t count, loff_t *ppos)
  5167. +{
  5168. + struct dbgaufs_plink_arg *p;
  5169. +
  5170. + p = file->private_data;
  5171. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5172. +}
  5173. +
  5174. +static const struct file_operations dbgaufs_plink_fop = {
  5175. + .owner = THIS_MODULE,
  5176. + .open = dbgaufs_plink_open,
  5177. + .release = dbgaufs_plink_release,
  5178. + .read = dbgaufs_plink_read
  5179. +};
  5180. +
  5181. +/* ---------------------------------------------------------------------- */
  5182. +
  5183. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5184. +{
  5185. + int err;
  5186. + struct au_sbinfo *sbinfo;
  5187. + struct super_block *sb;
  5188. +
  5189. + sbinfo = inode->i_private;
  5190. + sb = sbinfo->si_sb;
  5191. + si_noflush_read_lock(sb);
  5192. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
  5193. + si_read_unlock(sb);
  5194. + return err;
  5195. +}
  5196. +
  5197. +static const struct file_operations dbgaufs_xib_fop = {
  5198. + .owner = THIS_MODULE,
  5199. + .open = dbgaufs_xib_open,
  5200. + .release = dbgaufs_xi_release,
  5201. + .read = dbgaufs_xi_read
  5202. +};
  5203. +
  5204. +/* ---------------------------------------------------------------------- */
  5205. +
  5206. +#define DbgaufsXi_PREFIX "xi"
  5207. +
  5208. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5209. +{
  5210. + int err;
  5211. + long l;
  5212. + struct au_sbinfo *sbinfo;
  5213. + struct super_block *sb;
  5214. + struct file *xf;
  5215. + struct qstr *name;
  5216. +
  5217. + err = -ENOENT;
  5218. + xf = NULL;
  5219. + name = &file->f_path.dentry->d_name;
  5220. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5221. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5222. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5223. + goto out;
  5224. + err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5225. + if (unlikely(err))
  5226. + goto out;
  5227. +
  5228. + sbinfo = inode->i_private;
  5229. + sb = sbinfo->si_sb;
  5230. + si_noflush_read_lock(sb);
  5231. + if (l <= au_sbend(sb)) {
  5232. + xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
  5233. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
  5234. + } else
  5235. + err = -ENOENT;
  5236. + si_read_unlock(sb);
  5237. +
  5238. +out:
  5239. + return err;
  5240. +}
  5241. +
  5242. +static const struct file_operations dbgaufs_xino_fop = {
  5243. + .owner = THIS_MODULE,
  5244. + .open = dbgaufs_xino_open,
  5245. + .release = dbgaufs_xi_release,
  5246. + .read = dbgaufs_xi_read
  5247. +};
  5248. +
  5249. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5250. +{
  5251. + aufs_bindex_t bend;
  5252. + struct au_branch *br;
  5253. + struct au_xino_file *xi;
  5254. +
  5255. + if (!au_sbi(sb)->si_dbgaufs)
  5256. + return;
  5257. +
  5258. + bend = au_sbend(sb);
  5259. + for (; bindex <= bend; bindex++) {
  5260. + br = au_sbr(sb, bindex);
  5261. + xi = &br->br_xino;
  5262. + debugfs_remove(xi->xi_dbgaufs);
  5263. + xi->xi_dbgaufs = NULL;
  5264. + }
  5265. +}
  5266. +
  5267. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  5268. +{
  5269. + struct au_sbinfo *sbinfo;
  5270. + struct dentry *parent;
  5271. + struct au_branch *br;
  5272. + struct au_xino_file *xi;
  5273. + aufs_bindex_t bend;
  5274. + char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
  5275. +
  5276. + sbinfo = au_sbi(sb);
  5277. + parent = sbinfo->si_dbgaufs;
  5278. + if (!parent)
  5279. + return;
  5280. +
  5281. + bend = au_sbend(sb);
  5282. + for (; bindex <= bend; bindex++) {
  5283. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5284. + br = au_sbr(sb, bindex);
  5285. + xi = &br->br_xino;
  5286. + AuDebugOn(xi->xi_dbgaufs);
  5287. + xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5288. + sbinfo, &dbgaufs_xino_fop);
  5289. + /* ignore an error */
  5290. + if (unlikely(!xi->xi_dbgaufs))
  5291. + AuWarn1("failed %s under debugfs\n", name);
  5292. + }
  5293. +}
  5294. +
  5295. +/* ---------------------------------------------------------------------- */
  5296. +
  5297. +#ifdef CONFIG_AUFS_EXPORT
  5298. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5299. +{
  5300. + int err;
  5301. + struct au_sbinfo *sbinfo;
  5302. + struct super_block *sb;
  5303. +
  5304. + sbinfo = inode->i_private;
  5305. + sb = sbinfo->si_sb;
  5306. + si_noflush_read_lock(sb);
  5307. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
  5308. + si_read_unlock(sb);
  5309. + return err;
  5310. +}
  5311. +
  5312. +static const struct file_operations dbgaufs_xigen_fop = {
  5313. + .owner = THIS_MODULE,
  5314. + .open = dbgaufs_xigen_open,
  5315. + .release = dbgaufs_xi_release,
  5316. + .read = dbgaufs_xi_read
  5317. +};
  5318. +
  5319. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5320. +{
  5321. + int err;
  5322. +
  5323. + /*
  5324. + * This function is a dynamic '__init' function actually,
  5325. + * so the tiny check for si_rwsem is unnecessary.
  5326. + */
  5327. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5328. +
  5329. + err = -EIO;
  5330. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5331. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5332. + &dbgaufs_xigen_fop);
  5333. + if (sbinfo->si_dbgaufs_xigen)
  5334. + err = 0;
  5335. +
  5336. + return err;
  5337. +}
  5338. +#else
  5339. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5340. +{
  5341. + return 0;
  5342. +}
  5343. +#endif /* CONFIG_AUFS_EXPORT */
  5344. +
  5345. +/* ---------------------------------------------------------------------- */
  5346. +
  5347. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5348. +{
  5349. + /*
  5350. + * This function is a dynamic '__fin' function actually,
  5351. + * so the tiny check for si_rwsem is unnecessary.
  5352. + */
  5353. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5354. +
  5355. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5356. + sbinfo->si_dbgaufs = NULL;
  5357. + kobject_put(&sbinfo->si_kobj);
  5358. +}
  5359. +
  5360. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5361. +{
  5362. + int err;
  5363. + char name[SysaufsSiNameLen];
  5364. +
  5365. + /*
  5366. + * This function is a dynamic '__init' function actually,
  5367. + * so the tiny check for si_rwsem is unnecessary.
  5368. + */
  5369. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5370. +
  5371. + err = -ENOENT;
  5372. + if (!dbgaufs) {
  5373. + AuErr1("/debug/aufs is uninitialized\n");
  5374. + goto out;
  5375. + }
  5376. +
  5377. + err = -EIO;
  5378. + sysaufs_name(sbinfo, name);
  5379. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5380. + if (unlikely(!sbinfo->si_dbgaufs))
  5381. + goto out;
  5382. + kobject_get(&sbinfo->si_kobj);
  5383. +
  5384. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5385. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5386. + &dbgaufs_xib_fop);
  5387. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5388. + goto out_dir;
  5389. +
  5390. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5391. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5392. + &dbgaufs_plink_fop);
  5393. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5394. + goto out_dir;
  5395. +
  5396. + err = dbgaufs_xigen_init(sbinfo);
  5397. + if (!err)
  5398. + goto out; /* success */
  5399. +
  5400. +out_dir:
  5401. + dbgaufs_si_fin(sbinfo);
  5402. +out:
  5403. + return err;
  5404. +}
  5405. +
  5406. +/* ---------------------------------------------------------------------- */
  5407. +
  5408. +void dbgaufs_fin(void)
  5409. +{
  5410. + debugfs_remove(dbgaufs);
  5411. +}
  5412. +
  5413. +int __init dbgaufs_init(void)
  5414. +{
  5415. + int err;
  5416. +
  5417. + err = -EIO;
  5418. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5419. + if (dbgaufs)
  5420. + err = 0;
  5421. + return err;
  5422. +}
  5423. diff -Nur linux-4.1.10.orig/fs/aufs/dbgaufs.h linux-4.1.10/fs/aufs/dbgaufs.h
  5424. --- linux-4.1.10.orig/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
  5425. +++ linux-4.1.10/fs/aufs/dbgaufs.h 2015-10-22 21:35:53.000000000 +0200
  5426. @@ -0,0 +1,48 @@
  5427. +/*
  5428. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5429. + *
  5430. + * This program, aufs is free software; you can redistribute it and/or modify
  5431. + * it under the terms of the GNU General Public License as published by
  5432. + * the Free Software Foundation; either version 2 of the License, or
  5433. + * (at your option) any later version.
  5434. + *
  5435. + * This program is distributed in the hope that it will be useful,
  5436. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5437. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5438. + * GNU General Public License for more details.
  5439. + *
  5440. + * You should have received a copy of the GNU General Public License
  5441. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5442. + */
  5443. +
  5444. +/*
  5445. + * debugfs interface
  5446. + */
  5447. +
  5448. +#ifndef __DBGAUFS_H__
  5449. +#define __DBGAUFS_H__
  5450. +
  5451. +#ifdef __KERNEL__
  5452. +
  5453. +struct super_block;
  5454. +struct au_sbinfo;
  5455. +
  5456. +#ifdef CONFIG_DEBUG_FS
  5457. +/* dbgaufs.c */
  5458. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5459. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  5460. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5461. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5462. +void dbgaufs_fin(void);
  5463. +int __init dbgaufs_init(void);
  5464. +#else
  5465. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5466. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  5467. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5468. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5469. +AuStubVoid(dbgaufs_fin, void)
  5470. +AuStubInt0(__init dbgaufs_init, void)
  5471. +#endif /* CONFIG_DEBUG_FS */
  5472. +
  5473. +#endif /* __KERNEL__ */
  5474. +#endif /* __DBGAUFS_H__ */
  5475. diff -Nur linux-4.1.10.orig/fs/aufs/dcsub.c linux-4.1.10/fs/aufs/dcsub.c
  5476. --- linux-4.1.10.orig/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
  5477. +++ linux-4.1.10/fs/aufs/dcsub.c 2015-10-22 21:35:53.000000000 +0200
  5478. @@ -0,0 +1,224 @@
  5479. +/*
  5480. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5481. + *
  5482. + * This program, aufs is free software; you can redistribute it and/or modify
  5483. + * it under the terms of the GNU General Public License as published by
  5484. + * the Free Software Foundation; either version 2 of the License, or
  5485. + * (at your option) any later version.
  5486. + *
  5487. + * This program is distributed in the hope that it will be useful,
  5488. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5489. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5490. + * GNU General Public License for more details.
  5491. + *
  5492. + * You should have received a copy of the GNU General Public License
  5493. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5494. + */
  5495. +
  5496. +/*
  5497. + * sub-routines for dentry cache
  5498. + */
  5499. +
  5500. +#include "aufs.h"
  5501. +
  5502. +static void au_dpage_free(struct au_dpage *dpage)
  5503. +{
  5504. + int i;
  5505. + struct dentry **p;
  5506. +
  5507. + p = dpage->dentries;
  5508. + for (i = 0; i < dpage->ndentry; i++)
  5509. + dput(*p++);
  5510. + free_page((unsigned long)dpage->dentries);
  5511. +}
  5512. +
  5513. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  5514. +{
  5515. + int err;
  5516. + void *p;
  5517. +
  5518. + err = -ENOMEM;
  5519. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  5520. + if (unlikely(!dpages->dpages))
  5521. + goto out;
  5522. +
  5523. + p = (void *)__get_free_page(gfp);
  5524. + if (unlikely(!p))
  5525. + goto out_dpages;
  5526. +
  5527. + dpages->dpages[0].ndentry = 0;
  5528. + dpages->dpages[0].dentries = p;
  5529. + dpages->ndpage = 1;
  5530. + return 0; /* success */
  5531. +
  5532. +out_dpages:
  5533. + kfree(dpages->dpages);
  5534. +out:
  5535. + return err;
  5536. +}
  5537. +
  5538. +void au_dpages_free(struct au_dcsub_pages *dpages)
  5539. +{
  5540. + int i;
  5541. + struct au_dpage *p;
  5542. +
  5543. + p = dpages->dpages;
  5544. + for (i = 0; i < dpages->ndpage; i++)
  5545. + au_dpage_free(p++);
  5546. + kfree(dpages->dpages);
  5547. +}
  5548. +
  5549. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  5550. + struct dentry *dentry, gfp_t gfp)
  5551. +{
  5552. + int err, sz;
  5553. + struct au_dpage *dpage;
  5554. + void *p;
  5555. +
  5556. + dpage = dpages->dpages + dpages->ndpage - 1;
  5557. + sz = PAGE_SIZE / sizeof(dentry);
  5558. + if (unlikely(dpage->ndentry >= sz)) {
  5559. + AuLabel(new dpage);
  5560. + err = -ENOMEM;
  5561. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  5562. + p = au_kzrealloc(dpages->dpages, sz,
  5563. + sz + sizeof(*dpages->dpages), gfp);
  5564. + if (unlikely(!p))
  5565. + goto out;
  5566. +
  5567. + dpages->dpages = p;
  5568. + dpage = dpages->dpages + dpages->ndpage;
  5569. + p = (void *)__get_free_page(gfp);
  5570. + if (unlikely(!p))
  5571. + goto out;
  5572. +
  5573. + dpage->ndentry = 0;
  5574. + dpage->dentries = p;
  5575. + dpages->ndpage++;
  5576. + }
  5577. +
  5578. + AuDebugOn(au_dcount(dentry) <= 0);
  5579. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  5580. + return 0; /* success */
  5581. +
  5582. +out:
  5583. + return err;
  5584. +}
  5585. +
  5586. +/* todo: BAD approach */
  5587. +/* copied from linux/fs/dcache.c */
  5588. +enum d_walk_ret {
  5589. + D_WALK_CONTINUE,
  5590. + D_WALK_QUIT,
  5591. + D_WALK_NORETRY,
  5592. + D_WALK_SKIP,
  5593. +};
  5594. +
  5595. +extern void d_walk(struct dentry *parent, void *data,
  5596. + enum d_walk_ret (*enter)(void *, struct dentry *),
  5597. + void (*finish)(void *));
  5598. +
  5599. +struct ac_dpages_arg {
  5600. + int err;
  5601. + struct au_dcsub_pages *dpages;
  5602. + struct super_block *sb;
  5603. + au_dpages_test test;
  5604. + void *arg;
  5605. +};
  5606. +
  5607. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  5608. +{
  5609. + enum d_walk_ret ret;
  5610. + struct ac_dpages_arg *arg = _arg;
  5611. +
  5612. + ret = D_WALK_CONTINUE;
  5613. + if (dentry->d_sb == arg->sb
  5614. + && !IS_ROOT(dentry)
  5615. + && au_dcount(dentry) > 0
  5616. + && au_di(dentry)
  5617. + && (!arg->test || arg->test(dentry, arg->arg))) {
  5618. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  5619. + if (unlikely(arg->err))
  5620. + ret = D_WALK_QUIT;
  5621. + }
  5622. +
  5623. + return ret;
  5624. +}
  5625. +
  5626. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5627. + au_dpages_test test, void *arg)
  5628. +{
  5629. + struct ac_dpages_arg args = {
  5630. + .err = 0,
  5631. + .dpages = dpages,
  5632. + .sb = root->d_sb,
  5633. + .test = test,
  5634. + .arg = arg
  5635. + };
  5636. +
  5637. + d_walk(root, &args, au_call_dpages_append, NULL);
  5638. +
  5639. + return args.err;
  5640. +}
  5641. +
  5642. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5643. + int do_include, au_dpages_test test, void *arg)
  5644. +{
  5645. + int err;
  5646. +
  5647. + err = 0;
  5648. + write_seqlock(&rename_lock);
  5649. + spin_lock(&dentry->d_lock);
  5650. + if (do_include
  5651. + && au_dcount(dentry) > 0
  5652. + && (!test || test(dentry, arg)))
  5653. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5654. + spin_unlock(&dentry->d_lock);
  5655. + if (unlikely(err))
  5656. + goto out;
  5657. +
  5658. + /*
  5659. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  5660. + * mount
  5661. + */
  5662. + while (!IS_ROOT(dentry)) {
  5663. + dentry = dentry->d_parent; /* rename_lock is locked */
  5664. + spin_lock(&dentry->d_lock);
  5665. + if (au_dcount(dentry) > 0
  5666. + && (!test || test(dentry, arg)))
  5667. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  5668. + spin_unlock(&dentry->d_lock);
  5669. + if (unlikely(err))
  5670. + break;
  5671. + }
  5672. +
  5673. +out:
  5674. + write_sequnlock(&rename_lock);
  5675. + return err;
  5676. +}
  5677. +
  5678. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  5679. +{
  5680. + return au_di(dentry) && dentry->d_sb == arg;
  5681. +}
  5682. +
  5683. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5684. + struct dentry *dentry, int do_include)
  5685. +{
  5686. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  5687. + au_dcsub_dpages_aufs, dentry->d_sb);
  5688. +}
  5689. +
  5690. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  5691. +{
  5692. + struct path path[2] = {
  5693. + {
  5694. + .dentry = d1
  5695. + },
  5696. + {
  5697. + .dentry = d2
  5698. + }
  5699. + };
  5700. +
  5701. + return path_is_under(path + 0, path + 1);
  5702. +}
  5703. diff -Nur linux-4.1.10.orig/fs/aufs/dcsub.h linux-4.1.10/fs/aufs/dcsub.h
  5704. --- linux-4.1.10.orig/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
  5705. +++ linux-4.1.10/fs/aufs/dcsub.h 2015-10-22 21:35:53.000000000 +0200
  5706. @@ -0,0 +1,136 @@
  5707. +/*
  5708. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5709. + *
  5710. + * This program, aufs is free software; you can redistribute it and/or modify
  5711. + * it under the terms of the GNU General Public License as published by
  5712. + * the Free Software Foundation; either version 2 of the License, or
  5713. + * (at your option) any later version.
  5714. + *
  5715. + * This program is distributed in the hope that it will be useful,
  5716. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5717. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5718. + * GNU General Public License for more details.
  5719. + *
  5720. + * You should have received a copy of the GNU General Public License
  5721. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5722. + */
  5723. +
  5724. +/*
  5725. + * sub-routines for dentry cache
  5726. + */
  5727. +
  5728. +#ifndef __AUFS_DCSUB_H__
  5729. +#define __AUFS_DCSUB_H__
  5730. +
  5731. +#ifdef __KERNEL__
  5732. +
  5733. +#include <linux/dcache.h>
  5734. +#include <linux/fs.h>
  5735. +
  5736. +struct au_dpage {
  5737. + int ndentry;
  5738. + struct dentry **dentries;
  5739. +};
  5740. +
  5741. +struct au_dcsub_pages {
  5742. + int ndpage;
  5743. + struct au_dpage *dpages;
  5744. +};
  5745. +
  5746. +/* ---------------------------------------------------------------------- */
  5747. +
  5748. +/* dcsub.c */
  5749. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  5750. +void au_dpages_free(struct au_dcsub_pages *dpages);
  5751. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  5752. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  5753. + au_dpages_test test, void *arg);
  5754. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  5755. + int do_include, au_dpages_test test, void *arg);
  5756. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  5757. + struct dentry *dentry, int do_include);
  5758. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  5759. +
  5760. +/* ---------------------------------------------------------------------- */
  5761. +
  5762. +/*
  5763. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  5764. + * include/linux/dcache.h. Try them (in the future).
  5765. + */
  5766. +
  5767. +static inline int au_d_hashed_positive(struct dentry *d)
  5768. +{
  5769. + int err;
  5770. + struct inode *inode = d_inode(d);
  5771. +
  5772. + err = 0;
  5773. + if (unlikely(d_unhashed(d)
  5774. + || d_is_negative(d)
  5775. + || !inode->i_nlink))
  5776. + err = -ENOENT;
  5777. + return err;
  5778. +}
  5779. +
  5780. +static inline int au_d_linkable(struct dentry *d)
  5781. +{
  5782. + int err;
  5783. + struct inode *inode = d_inode(d);
  5784. +
  5785. + err = au_d_hashed_positive(d);
  5786. + if (err
  5787. + && d_is_positive(d)
  5788. + && (inode->i_state & I_LINKABLE))
  5789. + err = 0;
  5790. + return err;
  5791. +}
  5792. +
  5793. +static inline int au_d_alive(struct dentry *d)
  5794. +{
  5795. + int err;
  5796. + struct inode *inode;
  5797. +
  5798. + err = 0;
  5799. + if (!IS_ROOT(d))
  5800. + err = au_d_hashed_positive(d);
  5801. + else {
  5802. + inode = d_inode(d);
  5803. + if (unlikely(d_unlinked(d)
  5804. + || d_is_negative(d)
  5805. + || !inode->i_nlink))
  5806. + err = -ENOENT;
  5807. + }
  5808. + return err;
  5809. +}
  5810. +
  5811. +static inline int au_alive_dir(struct dentry *d)
  5812. +{
  5813. + int err;
  5814. +
  5815. + err = au_d_alive(d);
  5816. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  5817. + err = -ENOENT;
  5818. + return err;
  5819. +}
  5820. +
  5821. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  5822. +{
  5823. + return a->len == b->len
  5824. + && !memcmp(a->name, b->name, a->len);
  5825. +}
  5826. +
  5827. +/*
  5828. + * by the commit
  5829. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  5830. + * taking d_lock
  5831. + * the type of d_lockref.count became int, but the inlined function d_count()
  5832. + * still returns unsigned int.
  5833. + * I don't know why. Maybe it is for every d_count() users?
  5834. + * Anyway au_dcount() lives on.
  5835. + */
  5836. +static inline int au_dcount(struct dentry *d)
  5837. +{
  5838. + return (int)d_count(d);
  5839. +}
  5840. +
  5841. +#endif /* __KERNEL__ */
  5842. +#endif /* __AUFS_DCSUB_H__ */
  5843. diff -Nur linux-4.1.10.orig/fs/aufs/debug.c linux-4.1.10/fs/aufs/debug.c
  5844. --- linux-4.1.10.orig/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
  5845. +++ linux-4.1.10/fs/aufs/debug.c 2015-10-22 21:35:53.000000000 +0200
  5846. @@ -0,0 +1,440 @@
  5847. +/*
  5848. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  5849. + *
  5850. + * This program, aufs is free software; you can redistribute it and/or modify
  5851. + * it under the terms of the GNU General Public License as published by
  5852. + * the Free Software Foundation; either version 2 of the License, or
  5853. + * (at your option) any later version.
  5854. + *
  5855. + * This program is distributed in the hope that it will be useful,
  5856. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5857. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5858. + * GNU General Public License for more details.
  5859. + *
  5860. + * You should have received a copy of the GNU General Public License
  5861. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5862. + */
  5863. +
  5864. +/*
  5865. + * debug print functions
  5866. + */
  5867. +
  5868. +#include "aufs.h"
  5869. +
  5870. +/* Returns 0, or -errno. arg is in kp->arg. */
  5871. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  5872. +{
  5873. + int err, n;
  5874. +
  5875. + err = kstrtoint(val, 0, &n);
  5876. + if (!err) {
  5877. + if (n > 0)
  5878. + au_debug_on();
  5879. + else
  5880. + au_debug_off();
  5881. + }
  5882. + return err;
  5883. +}
  5884. +
  5885. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  5886. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  5887. +{
  5888. + atomic_t *a;
  5889. +
  5890. + a = kp->arg;
  5891. + return sprintf(buffer, "%d", atomic_read(a));
  5892. +}
  5893. +
  5894. +static struct kernel_param_ops param_ops_atomic_t = {
  5895. + .set = param_atomic_t_set,
  5896. + .get = param_atomic_t_get
  5897. + /* void (*free)(void *arg) */
  5898. +};
  5899. +
  5900. +atomic_t aufs_debug = ATOMIC_INIT(0);
  5901. +MODULE_PARM_DESC(debug, "debug print");
  5902. +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
  5903. +
  5904. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  5905. +char *au_plevel = KERN_DEBUG;
  5906. +#define dpri(fmt, ...) do { \
  5907. + if ((au_plevel \
  5908. + && strcmp(au_plevel, KERN_DEBUG)) \
  5909. + || au_debug_test()) \
  5910. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  5911. +} while (0)
  5912. +
  5913. +/* ---------------------------------------------------------------------- */
  5914. +
  5915. +void au_dpri_whlist(struct au_nhash *whlist)
  5916. +{
  5917. + unsigned long ul, n;
  5918. + struct hlist_head *head;
  5919. + struct au_vdir_wh *pos;
  5920. +
  5921. + n = whlist->nh_num;
  5922. + head = whlist->nh_head;
  5923. + for (ul = 0; ul < n; ul++) {
  5924. + hlist_for_each_entry(pos, head, wh_hash)
  5925. + dpri("b%d, %.*s, %d\n",
  5926. + pos->wh_bindex,
  5927. + pos->wh_str.len, pos->wh_str.name,
  5928. + pos->wh_str.len);
  5929. + head++;
  5930. + }
  5931. +}
  5932. +
  5933. +void au_dpri_vdir(struct au_vdir *vdir)
  5934. +{
  5935. + unsigned long ul;
  5936. + union au_vdir_deblk_p p;
  5937. + unsigned char *o;
  5938. +
  5939. + if (!vdir || IS_ERR(vdir)) {
  5940. + dpri("err %ld\n", PTR_ERR(vdir));
  5941. + return;
  5942. + }
  5943. +
  5944. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
  5945. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  5946. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  5947. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  5948. + p.deblk = vdir->vd_deblk[ul];
  5949. + o = p.deblk;
  5950. + dpri("[%lu]: %p\n", ul, o);
  5951. + }
  5952. +}
  5953. +
  5954. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  5955. + struct dentry *wh)
  5956. +{
  5957. + char *n = NULL;
  5958. + int l = 0;
  5959. +
  5960. + if (!inode || IS_ERR(inode)) {
  5961. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  5962. + return -1;
  5963. + }
  5964. +
  5965. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  5966. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  5967. + && sizeof(inode->i_blocks) != sizeof(u64));
  5968. + if (wh) {
  5969. + n = (void *)wh->d_name.name;
  5970. + l = wh->d_name.len;
  5971. + }
  5972. +
  5973. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  5974. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  5975. + bindex, inode,
  5976. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  5977. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  5978. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  5979. + hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
  5980. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  5981. + inode->i_state, inode->i_flags, inode->i_version,
  5982. + inode->i_generation,
  5983. + l ? ", wh " : "", l, n);
  5984. + return 0;
  5985. +}
  5986. +
  5987. +void au_dpri_inode(struct inode *inode)
  5988. +{
  5989. + struct au_iinfo *iinfo;
  5990. + aufs_bindex_t bindex;
  5991. + int err, hn;
  5992. +
  5993. + err = do_pri_inode(-1, inode, -1, NULL);
  5994. + if (err || !au_test_aufs(inode->i_sb))
  5995. + return;
  5996. +
  5997. + iinfo = au_ii(inode);
  5998. + if (!iinfo)
  5999. + return;
  6000. + dpri("i-1: bstart %d, bend %d, gen %d\n",
  6001. + iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
  6002. + if (iinfo->ii_bstart < 0)
  6003. + return;
  6004. + hn = 0;
  6005. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
  6006. + hn = !!au_hn(iinfo->ii_hinode + bindex);
  6007. + do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
  6008. + iinfo->ii_hinode[0 + bindex].hi_whdentry);
  6009. + }
  6010. +}
  6011. +
  6012. +void au_dpri_dalias(struct inode *inode)
  6013. +{
  6014. + struct dentry *d;
  6015. +
  6016. + spin_lock(&inode->i_lock);
  6017. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6018. + au_dpri_dentry(d);
  6019. + spin_unlock(&inode->i_lock);
  6020. +}
  6021. +
  6022. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6023. +{
  6024. + struct dentry *wh = NULL;
  6025. + int hn;
  6026. + struct au_iinfo *iinfo;
  6027. +
  6028. + if (!dentry || IS_ERR(dentry)) {
  6029. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6030. + return -1;
  6031. + }
  6032. + /* do not call dget_parent() here */
  6033. + /* note: access d_xxx without d_lock */
  6034. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6035. + bindex, dentry, dentry,
  6036. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6037. + au_dcount(dentry), dentry->d_flags,
  6038. + d_unhashed(dentry) ? "un" : "");
  6039. + hn = -1;
  6040. + if (bindex >= 0
  6041. + && d_is_positive(dentry)
  6042. + && au_test_aufs(dentry->d_sb)) {
  6043. + iinfo = au_ii(d_inode(dentry));
  6044. + if (iinfo) {
  6045. + hn = !!au_hn(iinfo->ii_hinode + bindex);
  6046. + wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
  6047. + }
  6048. + }
  6049. + do_pri_inode(bindex, d_inode(dentry), hn, wh);
  6050. + return 0;
  6051. +}
  6052. +
  6053. +void au_dpri_dentry(struct dentry *dentry)
  6054. +{
  6055. + struct au_dinfo *dinfo;
  6056. + aufs_bindex_t bindex;
  6057. + int err;
  6058. + struct au_hdentry *hdp;
  6059. +
  6060. + err = do_pri_dentry(-1, dentry);
  6061. + if (err || !au_test_aufs(dentry->d_sb))
  6062. + return;
  6063. +
  6064. + dinfo = au_di(dentry);
  6065. + if (!dinfo)
  6066. + return;
  6067. + dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6068. + dinfo->di_bstart, dinfo->di_bend,
  6069. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6070. + dinfo->di_tmpfile);
  6071. + if (dinfo->di_bstart < 0)
  6072. + return;
  6073. + hdp = dinfo->di_hdentry;
  6074. + for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
  6075. + do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
  6076. +}
  6077. +
  6078. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6079. +{
  6080. + char a[32];
  6081. +
  6082. + if (!file || IS_ERR(file)) {
  6083. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6084. + return -1;
  6085. + }
  6086. + a[0] = 0;
  6087. + if (bindex < 0
  6088. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6089. + && au_test_aufs(file->f_path.dentry->d_sb)
  6090. + && au_fi(file))
  6091. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6092. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6093. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6094. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6095. + file->f_version, file->f_pos, a);
  6096. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6097. + do_pri_dentry(bindex, file->f_path.dentry);
  6098. + return 0;
  6099. +}
  6100. +
  6101. +void au_dpri_file(struct file *file)
  6102. +{
  6103. + struct au_finfo *finfo;
  6104. + struct au_fidir *fidir;
  6105. + struct au_hfile *hfile;
  6106. + aufs_bindex_t bindex;
  6107. + int err;
  6108. +
  6109. + err = do_pri_file(-1, file);
  6110. + if (err
  6111. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6112. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6113. + return;
  6114. +
  6115. + finfo = au_fi(file);
  6116. + if (!finfo)
  6117. + return;
  6118. + if (finfo->fi_btop < 0)
  6119. + return;
  6120. + fidir = finfo->fi_hdir;
  6121. + if (!fidir)
  6122. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6123. + else
  6124. + for (bindex = finfo->fi_btop;
  6125. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6126. + bindex++) {
  6127. + hfile = fidir->fd_hfile + bindex;
  6128. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6129. + }
  6130. +}
  6131. +
  6132. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6133. +{
  6134. + struct vfsmount *mnt;
  6135. + struct super_block *sb;
  6136. +
  6137. + if (!br || IS_ERR(br))
  6138. + goto out;
  6139. + mnt = au_br_mnt(br);
  6140. + if (!mnt || IS_ERR(mnt))
  6141. + goto out;
  6142. + sb = mnt->mnt_sb;
  6143. + if (!sb || IS_ERR(sb))
  6144. + goto out;
  6145. +
  6146. + dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
  6147. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6148. + "xino %d\n",
  6149. + bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
  6150. + br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6151. + sb->s_flags, sb->s_count,
  6152. + atomic_read(&sb->s_active), !!br->br_xino.xi_file);
  6153. + return 0;
  6154. +
  6155. +out:
  6156. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6157. + return -1;
  6158. +}
  6159. +
  6160. +void au_dpri_sb(struct super_block *sb)
  6161. +{
  6162. + struct au_sbinfo *sbinfo;
  6163. + aufs_bindex_t bindex;
  6164. + int err;
  6165. + /* to reuduce stack size */
  6166. + struct {
  6167. + struct vfsmount mnt;
  6168. + struct au_branch fake;
  6169. + } *a;
  6170. +
  6171. + /* this function can be called from magic sysrq */
  6172. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6173. + if (unlikely(!a)) {
  6174. + dpri("no memory\n");
  6175. + return;
  6176. + }
  6177. +
  6178. + a->mnt.mnt_sb = sb;
  6179. + a->fake.br_perm = 0;
  6180. + a->fake.br_path.mnt = &a->mnt;
  6181. + a->fake.br_xino.xi_file = NULL;
  6182. + atomic_set(&a->fake.br_count, 0);
  6183. + smp_mb(); /* atomic_set */
  6184. + err = do_pri_br(-1, &a->fake);
  6185. + kfree(a);
  6186. + dpri("dev 0x%x\n", sb->s_dev);
  6187. + if (err || !au_test_aufs(sb))
  6188. + return;
  6189. +
  6190. + sbinfo = au_sbi(sb);
  6191. + if (!sbinfo)
  6192. + return;
  6193. + dpri("nw %d, gen %u, kobj %d\n",
  6194. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6195. + atomic_read(&sbinfo->si_kobj.kref.refcount));
  6196. + for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
  6197. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6198. +}
  6199. +
  6200. +/* ---------------------------------------------------------------------- */
  6201. +
  6202. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6203. +{
  6204. + struct inode *h_inode, *inode = d_inode(dentry);
  6205. + struct dentry *h_dentry;
  6206. + aufs_bindex_t bindex, bend, bi;
  6207. +
  6208. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6209. + return;
  6210. +
  6211. + bend = au_dbend(dentry);
  6212. + bi = au_ibend(inode);
  6213. + if (bi < bend)
  6214. + bend = bi;
  6215. + bindex = au_dbstart(dentry);
  6216. + bi = au_ibstart(inode);
  6217. + if (bi > bindex)
  6218. + bindex = bi;
  6219. +
  6220. + for (; bindex <= bend; bindex++) {
  6221. + h_dentry = au_h_dptr(dentry, bindex);
  6222. + if (!h_dentry)
  6223. + continue;
  6224. + h_inode = au_h_iptr(inode, bindex);
  6225. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6226. + au_debug_on();
  6227. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6228. + AuDbgDentry(dentry);
  6229. + AuDbgInode(inode);
  6230. + au_debug_off();
  6231. + BUG();
  6232. + }
  6233. + }
  6234. +}
  6235. +
  6236. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6237. +{
  6238. + int err, i, j;
  6239. + struct au_dcsub_pages dpages;
  6240. + struct au_dpage *dpage;
  6241. + struct dentry **dentries;
  6242. +
  6243. + err = au_dpages_init(&dpages, GFP_NOFS);
  6244. + AuDebugOn(err);
  6245. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6246. + AuDebugOn(err);
  6247. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6248. + dpage = dpages.dpages + i;
  6249. + dentries = dpage->dentries;
  6250. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6251. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6252. + }
  6253. + au_dpages_free(&dpages);
  6254. +}
  6255. +
  6256. +void au_dbg_verify_kthread(void)
  6257. +{
  6258. + if (au_wkq_test()) {
  6259. + au_dbg_blocked();
  6260. + /*
  6261. + * It may be recursive, but udba=notify between two aufs mounts,
  6262. + * where a single ro branch is shared, is not a problem.
  6263. + */
  6264. + /* WARN_ON(1); */
  6265. + }
  6266. +}
  6267. +
  6268. +/* ---------------------------------------------------------------------- */
  6269. +
  6270. +int __init au_debug_init(void)
  6271. +{
  6272. + aufs_bindex_t bindex;
  6273. + struct au_vdir_destr destr;
  6274. +
  6275. + bindex = -1;
  6276. + AuDebugOn(bindex >= 0);
  6277. +
  6278. + destr.len = -1;
  6279. + AuDebugOn(destr.len < NAME_MAX);
  6280. +
  6281. +#ifdef CONFIG_4KSTACKS
  6282. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6283. +#endif
  6284. +
  6285. + return 0;
  6286. +}
  6287. diff -Nur linux-4.1.10.orig/fs/aufs/debug.h linux-4.1.10/fs/aufs/debug.h
  6288. --- linux-4.1.10.orig/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
  6289. +++ linux-4.1.10/fs/aufs/debug.h 2015-10-22 21:35:53.000000000 +0200
  6290. @@ -0,0 +1,225 @@
  6291. +/*
  6292. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  6293. + *
  6294. + * This program, aufs is free software; you can redistribute it and/or modify
  6295. + * it under the terms of the GNU General Public License as published by
  6296. + * the Free Software Foundation; either version 2 of the License, or
  6297. + * (at your option) any later version.
  6298. + *
  6299. + * This program is distributed in the hope that it will be useful,
  6300. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6301. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6302. + * GNU General Public License for more details.
  6303. + *
  6304. + * You should have received a copy of the GNU General Public License
  6305. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6306. + */
  6307. +
  6308. +/*
  6309. + * debug print functions
  6310. + */
  6311. +
  6312. +#ifndef __AUFS_DEBUG_H__
  6313. +#define __AUFS_DEBUG_H__
  6314. +
  6315. +#ifdef __KERNEL__
  6316. +
  6317. +#include <linux/atomic.h>
  6318. +#include <linux/module.h>
  6319. +#include <linux/kallsyms.h>
  6320. +#include <linux/sysrq.h>
  6321. +
  6322. +#ifdef CONFIG_AUFS_DEBUG
  6323. +#define AuDebugOn(a) BUG_ON(a)
  6324. +
  6325. +/* module parameter */
  6326. +extern atomic_t aufs_debug;
  6327. +static inline void au_debug_on(void)
  6328. +{
  6329. + atomic_inc(&aufs_debug);
  6330. +}
  6331. +static inline void au_debug_off(void)
  6332. +{
  6333. + atomic_dec_if_positive(&aufs_debug);
  6334. +}
  6335. +
  6336. +static inline int au_debug_test(void)
  6337. +{
  6338. + return atomic_read(&aufs_debug) > 0;
  6339. +}
  6340. +#else
  6341. +#define AuDebugOn(a) do {} while (0)
  6342. +AuStubVoid(au_debug_on, void)
  6343. +AuStubVoid(au_debug_off, void)
  6344. +AuStubInt0(au_debug_test, void)
  6345. +#endif /* CONFIG_AUFS_DEBUG */
  6346. +
  6347. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6348. +
  6349. +/* ---------------------------------------------------------------------- */
  6350. +
  6351. +/* debug print */
  6352. +
  6353. +#define AuDbg(fmt, ...) do { \
  6354. + if (au_debug_test()) \
  6355. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6356. +} while (0)
  6357. +#define AuLabel(l) AuDbg(#l "\n")
  6358. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6359. +#define AuWarn1(fmt, ...) do { \
  6360. + static unsigned char _c; \
  6361. + if (!_c++) \
  6362. + pr_warn(fmt, ##__VA_ARGS__); \
  6363. +} while (0)
  6364. +
  6365. +#define AuErr1(fmt, ...) do { \
  6366. + static unsigned char _c; \
  6367. + if (!_c++) \
  6368. + pr_err(fmt, ##__VA_ARGS__); \
  6369. +} while (0)
  6370. +
  6371. +#define AuIOErr1(fmt, ...) do { \
  6372. + static unsigned char _c; \
  6373. + if (!_c++) \
  6374. + AuIOErr(fmt, ##__VA_ARGS__); \
  6375. +} while (0)
  6376. +
  6377. +#define AuUnsupportMsg "This operation is not supported." \
  6378. + " Please report this application to aufs-users ML."
  6379. +#define AuUnsupport(fmt, ...) do { \
  6380. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6381. + dump_stack(); \
  6382. +} while (0)
  6383. +
  6384. +#define AuTraceErr(e) do { \
  6385. + if (unlikely((e) < 0)) \
  6386. + AuDbg("err %d\n", (int)(e)); \
  6387. +} while (0)
  6388. +
  6389. +#define AuTraceErrPtr(p) do { \
  6390. + if (IS_ERR(p)) \
  6391. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6392. +} while (0)
  6393. +
  6394. +/* dirty macros for debug print, use with "%.*s" and caution */
  6395. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6396. +
  6397. +/* ---------------------------------------------------------------------- */
  6398. +
  6399. +struct dentry;
  6400. +#ifdef CONFIG_AUFS_DEBUG
  6401. +extern struct mutex au_dbg_mtx;
  6402. +extern char *au_plevel;
  6403. +struct au_nhash;
  6404. +void au_dpri_whlist(struct au_nhash *whlist);
  6405. +struct au_vdir;
  6406. +void au_dpri_vdir(struct au_vdir *vdir);
  6407. +struct inode;
  6408. +void au_dpri_inode(struct inode *inode);
  6409. +void au_dpri_dalias(struct inode *inode);
  6410. +void au_dpri_dentry(struct dentry *dentry);
  6411. +struct file;
  6412. +void au_dpri_file(struct file *filp);
  6413. +struct super_block;
  6414. +void au_dpri_sb(struct super_block *sb);
  6415. +
  6416. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6417. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6418. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6419. +void au_dbg_verify_kthread(void);
  6420. +
  6421. +int __init au_debug_init(void);
  6422. +
  6423. +#define AuDbgWhlist(w) do { \
  6424. + mutex_lock(&au_dbg_mtx); \
  6425. + AuDbg(#w "\n"); \
  6426. + au_dpri_whlist(w); \
  6427. + mutex_unlock(&au_dbg_mtx); \
  6428. +} while (0)
  6429. +
  6430. +#define AuDbgVdir(v) do { \
  6431. + mutex_lock(&au_dbg_mtx); \
  6432. + AuDbg(#v "\n"); \
  6433. + au_dpri_vdir(v); \
  6434. + mutex_unlock(&au_dbg_mtx); \
  6435. +} while (0)
  6436. +
  6437. +#define AuDbgInode(i) do { \
  6438. + mutex_lock(&au_dbg_mtx); \
  6439. + AuDbg(#i "\n"); \
  6440. + au_dpri_inode(i); \
  6441. + mutex_unlock(&au_dbg_mtx); \
  6442. +} while (0)
  6443. +
  6444. +#define AuDbgDAlias(i) do { \
  6445. + mutex_lock(&au_dbg_mtx); \
  6446. + AuDbg(#i "\n"); \
  6447. + au_dpri_dalias(i); \
  6448. + mutex_unlock(&au_dbg_mtx); \
  6449. +} while (0)
  6450. +
  6451. +#define AuDbgDentry(d) do { \
  6452. + mutex_lock(&au_dbg_mtx); \
  6453. + AuDbg(#d "\n"); \
  6454. + au_dpri_dentry(d); \
  6455. + mutex_unlock(&au_dbg_mtx); \
  6456. +} while (0)
  6457. +
  6458. +#define AuDbgFile(f) do { \
  6459. + mutex_lock(&au_dbg_mtx); \
  6460. + AuDbg(#f "\n"); \
  6461. + au_dpri_file(f); \
  6462. + mutex_unlock(&au_dbg_mtx); \
  6463. +} while (0)
  6464. +
  6465. +#define AuDbgSb(sb) do { \
  6466. + mutex_lock(&au_dbg_mtx); \
  6467. + AuDbg(#sb "\n"); \
  6468. + au_dpri_sb(sb); \
  6469. + mutex_unlock(&au_dbg_mtx); \
  6470. +} while (0)
  6471. +
  6472. +#define AuDbgSym(addr) do { \
  6473. + char sym[KSYM_SYMBOL_LEN]; \
  6474. + sprint_symbol(sym, (unsigned long)addr); \
  6475. + AuDbg("%s\n", sym); \
  6476. +} while (0)
  6477. +#else
  6478. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6479. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6480. +AuStubVoid(au_dbg_verify_kthread, void)
  6481. +AuStubInt0(__init au_debug_init, void)
  6482. +
  6483. +#define AuDbgWhlist(w) do {} while (0)
  6484. +#define AuDbgVdir(v) do {} while (0)
  6485. +#define AuDbgInode(i) do {} while (0)
  6486. +#define AuDbgDAlias(i) do {} while (0)
  6487. +#define AuDbgDentry(d) do {} while (0)
  6488. +#define AuDbgFile(f) do {} while (0)
  6489. +#define AuDbgSb(sb) do {} while (0)
  6490. +#define AuDbgSym(addr) do {} while (0)
  6491. +#endif /* CONFIG_AUFS_DEBUG */
  6492. +
  6493. +/* ---------------------------------------------------------------------- */
  6494. +
  6495. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  6496. +int __init au_sysrq_init(void);
  6497. +void au_sysrq_fin(void);
  6498. +
  6499. +#ifdef CONFIG_HW_CONSOLE
  6500. +#define au_dbg_blocked() do { \
  6501. + WARN_ON(1); \
  6502. + handle_sysrq('w'); \
  6503. +} while (0)
  6504. +#else
  6505. +AuStubVoid(au_dbg_blocked, void)
  6506. +#endif
  6507. +
  6508. +#else
  6509. +AuStubInt0(__init au_sysrq_init, void)
  6510. +AuStubVoid(au_sysrq_fin, void)
  6511. +AuStubVoid(au_dbg_blocked, void)
  6512. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  6513. +
  6514. +#endif /* __KERNEL__ */
  6515. +#endif /* __AUFS_DEBUG_H__ */
  6516. diff -Nur linux-4.1.10.orig/fs/aufs/dentry.c linux-4.1.10/fs/aufs/dentry.c
  6517. --- linux-4.1.10.orig/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
  6518. +++ linux-4.1.10/fs/aufs/dentry.c 2015-10-22 21:35:53.000000000 +0200
  6519. @@ -0,0 +1,1105 @@
  6520. +/*
  6521. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  6522. + *
  6523. + * This program, aufs is free software; you can redistribute it and/or modify
  6524. + * it under the terms of the GNU General Public License as published by
  6525. + * the Free Software Foundation; either version 2 of the License, or
  6526. + * (at your option) any later version.
  6527. + *
  6528. + * This program is distributed in the hope that it will be useful,
  6529. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6530. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6531. + * GNU General Public License for more details.
  6532. + *
  6533. + * You should have received a copy of the GNU General Public License
  6534. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6535. + */
  6536. +
  6537. +/*
  6538. + * lookup and dentry operations
  6539. + */
  6540. +
  6541. +#include <linux/namei.h>
  6542. +#include "aufs.h"
  6543. +
  6544. +#define AuLkup_ALLOW_NEG 1
  6545. +#define AuLkup_IGNORE_PERM (1 << 1)
  6546. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  6547. +#define au_fset_lkup(flags, name) \
  6548. + do { (flags) |= AuLkup_##name; } while (0)
  6549. +#define au_fclr_lkup(flags, name) \
  6550. + do { (flags) &= ~AuLkup_##name; } while (0)
  6551. +
  6552. +struct au_do_lookup_args {
  6553. + unsigned int flags;
  6554. + mode_t type;
  6555. +};
  6556. +
  6557. +/*
  6558. + * returns positive/negative dentry, NULL or an error.
  6559. + * NULL means whiteout-ed or not-found.
  6560. + */
  6561. +static struct dentry*
  6562. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  6563. + aufs_bindex_t bindex, struct qstr *wh_name,
  6564. + struct au_do_lookup_args *args)
  6565. +{
  6566. + struct dentry *h_dentry;
  6567. + struct inode *h_inode;
  6568. + struct au_branch *br;
  6569. + int wh_found, opq;
  6570. + unsigned char wh_able;
  6571. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  6572. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  6573. + IGNORE_PERM);
  6574. +
  6575. + wh_found = 0;
  6576. + br = au_sbr(dentry->d_sb, bindex);
  6577. + wh_able = !!au_br_whable(br->br_perm);
  6578. + if (wh_able)
  6579. + wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
  6580. + h_dentry = ERR_PTR(wh_found);
  6581. + if (!wh_found)
  6582. + goto real_lookup;
  6583. + if (unlikely(wh_found < 0))
  6584. + goto out;
  6585. +
  6586. + /* We found a whiteout */
  6587. + /* au_set_dbend(dentry, bindex); */
  6588. + au_set_dbwh(dentry, bindex);
  6589. + if (!allow_neg)
  6590. + return NULL; /* success */
  6591. +
  6592. +real_lookup:
  6593. + if (!ignore_perm)
  6594. + h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  6595. + else
  6596. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6597. + if (IS_ERR(h_dentry)) {
  6598. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  6599. + && !allow_neg)
  6600. + h_dentry = NULL;
  6601. + goto out;
  6602. + }
  6603. +
  6604. + h_inode = d_inode(h_dentry);
  6605. + if (d_is_negative(h_dentry)) {
  6606. + if (!allow_neg)
  6607. + goto out_neg;
  6608. + } else if (wh_found
  6609. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  6610. + goto out_neg;
  6611. +
  6612. + if (au_dbend(dentry) <= bindex)
  6613. + au_set_dbend(dentry, bindex);
  6614. + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
  6615. + au_set_dbstart(dentry, bindex);
  6616. + au_set_h_dptr(dentry, bindex, h_dentry);
  6617. +
  6618. + if (!d_is_dir(h_dentry)
  6619. + || !wh_able
  6620. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  6621. + goto out; /* success */
  6622. +
  6623. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  6624. + opq = au_diropq_test(h_dentry);
  6625. + mutex_unlock(&h_inode->i_mutex);
  6626. + if (opq > 0)
  6627. + au_set_dbdiropq(dentry, bindex);
  6628. + else if (unlikely(opq < 0)) {
  6629. + au_set_h_dptr(dentry, bindex, NULL);
  6630. + h_dentry = ERR_PTR(opq);
  6631. + }
  6632. + goto out;
  6633. +
  6634. +out_neg:
  6635. + dput(h_dentry);
  6636. + h_dentry = NULL;
  6637. +out:
  6638. + return h_dentry;
  6639. +}
  6640. +
  6641. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  6642. +{
  6643. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  6644. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  6645. + return -EPERM;
  6646. + return 0;
  6647. +}
  6648. +
  6649. +/*
  6650. + * returns the number of lower positive dentries,
  6651. + * otherwise an error.
  6652. + * can be called at unlinking with @type is zero.
  6653. + */
  6654. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
  6655. +{
  6656. + int npositive, err;
  6657. + aufs_bindex_t bindex, btail, bdiropq;
  6658. + unsigned char isdir, dirperm1;
  6659. + struct qstr whname;
  6660. + struct au_do_lookup_args args = {
  6661. + .flags = 0,
  6662. + .type = type
  6663. + };
  6664. + const struct qstr *name = &dentry->d_name;
  6665. + struct dentry *parent;
  6666. + struct super_block *sb;
  6667. +
  6668. + sb = dentry->d_sb;
  6669. + err = au_test_shwh(sb, name);
  6670. + if (unlikely(err))
  6671. + goto out;
  6672. +
  6673. + err = au_wh_name_alloc(&whname, name);
  6674. + if (unlikely(err))
  6675. + goto out;
  6676. +
  6677. + isdir = !!d_is_dir(dentry);
  6678. + if (!type)
  6679. + au_fset_lkup(args.flags, ALLOW_NEG);
  6680. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  6681. +
  6682. + npositive = 0;
  6683. + parent = dget_parent(dentry);
  6684. + btail = au_dbtaildir(parent);
  6685. + for (bindex = bstart; bindex <= btail; bindex++) {
  6686. + struct dentry *h_parent, *h_dentry;
  6687. + struct inode *h_inode, *h_dir;
  6688. +
  6689. + h_dentry = au_h_dptr(dentry, bindex);
  6690. + if (h_dentry) {
  6691. + if (d_is_positive(h_dentry))
  6692. + npositive++;
  6693. + if (type != S_IFDIR)
  6694. + break;
  6695. + continue;
  6696. + }
  6697. + h_parent = au_h_dptr(parent, bindex);
  6698. + if (!h_parent || !d_is_dir(h_parent))
  6699. + continue;
  6700. +
  6701. + h_dir = d_inode(h_parent);
  6702. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  6703. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
  6704. + &args);
  6705. + mutex_unlock(&h_dir->i_mutex);
  6706. + err = PTR_ERR(h_dentry);
  6707. + if (IS_ERR(h_dentry))
  6708. + goto out_parent;
  6709. + if (h_dentry)
  6710. + au_fclr_lkup(args.flags, ALLOW_NEG);
  6711. + if (dirperm1)
  6712. + au_fset_lkup(args.flags, IGNORE_PERM);
  6713. +
  6714. + if (au_dbwh(dentry) == bindex)
  6715. + break;
  6716. + if (!h_dentry)
  6717. + continue;
  6718. + if (d_is_negative(h_dentry))
  6719. + continue;
  6720. + h_inode = d_inode(h_dentry);
  6721. + npositive++;
  6722. + if (!args.type)
  6723. + args.type = h_inode->i_mode & S_IFMT;
  6724. + if (args.type != S_IFDIR)
  6725. + break;
  6726. + else if (isdir) {
  6727. + /* the type of lower may be different */
  6728. + bdiropq = au_dbdiropq(dentry);
  6729. + if (bdiropq >= 0 && bdiropq <= bindex)
  6730. + break;
  6731. + }
  6732. + }
  6733. +
  6734. + if (npositive) {
  6735. + AuLabel(positive);
  6736. + au_update_dbstart(dentry);
  6737. + }
  6738. + err = npositive;
  6739. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  6740. + && au_dbstart(dentry) < 0)) {
  6741. + err = -EIO;
  6742. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  6743. + dentry, err);
  6744. + }
  6745. +
  6746. +out_parent:
  6747. + dput(parent);
  6748. + kfree(whname.name);
  6749. +out:
  6750. + return err;
  6751. +}
  6752. +
  6753. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  6754. +{
  6755. + struct dentry *dentry;
  6756. + int wkq_err;
  6757. +
  6758. + if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
  6759. + dentry = vfsub_lkup_one(name, parent);
  6760. + else {
  6761. + struct vfsub_lkup_one_args args = {
  6762. + .errp = &dentry,
  6763. + .name = name,
  6764. + .parent = parent
  6765. + };
  6766. +
  6767. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  6768. + if (unlikely(wkq_err))
  6769. + dentry = ERR_PTR(wkq_err);
  6770. + }
  6771. +
  6772. + return dentry;
  6773. +}
  6774. +
  6775. +/*
  6776. + * lookup @dentry on @bindex which should be negative.
  6777. + */
  6778. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  6779. +{
  6780. + int err;
  6781. + struct dentry *parent, *h_parent, *h_dentry;
  6782. + struct au_branch *br;
  6783. +
  6784. + parent = dget_parent(dentry);
  6785. + h_parent = au_h_dptr(parent, bindex);
  6786. + br = au_sbr(dentry->d_sb, bindex);
  6787. + if (wh)
  6788. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  6789. + else
  6790. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  6791. + err = PTR_ERR(h_dentry);
  6792. + if (IS_ERR(h_dentry))
  6793. + goto out;
  6794. + if (unlikely(d_is_positive(h_dentry))) {
  6795. + err = -EIO;
  6796. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  6797. + dput(h_dentry);
  6798. + goto out;
  6799. + }
  6800. +
  6801. + err = 0;
  6802. + if (bindex < au_dbstart(dentry))
  6803. + au_set_dbstart(dentry, bindex);
  6804. + if (au_dbend(dentry) < bindex)
  6805. + au_set_dbend(dentry, bindex);
  6806. + au_set_h_dptr(dentry, bindex, h_dentry);
  6807. +
  6808. +out:
  6809. + dput(parent);
  6810. + return err;
  6811. +}
  6812. +
  6813. +/* ---------------------------------------------------------------------- */
  6814. +
  6815. +/* subset of struct inode */
  6816. +struct au_iattr {
  6817. + unsigned long i_ino;
  6818. + /* unsigned int i_nlink; */
  6819. + kuid_t i_uid;
  6820. + kgid_t i_gid;
  6821. + u64 i_version;
  6822. +/*
  6823. + loff_t i_size;
  6824. + blkcnt_t i_blocks;
  6825. +*/
  6826. + umode_t i_mode;
  6827. +};
  6828. +
  6829. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  6830. +{
  6831. + ia->i_ino = h_inode->i_ino;
  6832. + /* ia->i_nlink = h_inode->i_nlink; */
  6833. + ia->i_uid = h_inode->i_uid;
  6834. + ia->i_gid = h_inode->i_gid;
  6835. + ia->i_version = h_inode->i_version;
  6836. +/*
  6837. + ia->i_size = h_inode->i_size;
  6838. + ia->i_blocks = h_inode->i_blocks;
  6839. +*/
  6840. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  6841. +}
  6842. +
  6843. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  6844. +{
  6845. + return ia->i_ino != h_inode->i_ino
  6846. + /* || ia->i_nlink != h_inode->i_nlink */
  6847. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  6848. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  6849. + || ia->i_version != h_inode->i_version
  6850. +/*
  6851. + || ia->i_size != h_inode->i_size
  6852. + || ia->i_blocks != h_inode->i_blocks
  6853. +*/
  6854. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  6855. +}
  6856. +
  6857. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  6858. + struct au_branch *br)
  6859. +{
  6860. + int err;
  6861. + struct au_iattr ia;
  6862. + struct inode *h_inode;
  6863. + struct dentry *h_d;
  6864. + struct super_block *h_sb;
  6865. +
  6866. + err = 0;
  6867. + memset(&ia, -1, sizeof(ia));
  6868. + h_sb = h_dentry->d_sb;
  6869. + h_inode = NULL;
  6870. + if (d_is_positive(h_dentry)) {
  6871. + h_inode = d_inode(h_dentry);
  6872. + au_iattr_save(&ia, h_inode);
  6873. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  6874. + /* nfs d_revalidate may return 0 for negative dentry */
  6875. + /* fuse d_revalidate always return 0 for negative dentry */
  6876. + goto out;
  6877. +
  6878. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  6879. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  6880. + err = PTR_ERR(h_d);
  6881. + if (IS_ERR(h_d))
  6882. + goto out;
  6883. +
  6884. + err = 0;
  6885. + if (unlikely(h_d != h_dentry
  6886. + || d_inode(h_d) != h_inode
  6887. + || (h_inode && au_iattr_test(&ia, h_inode))))
  6888. + err = au_busy_or_stale();
  6889. + dput(h_d);
  6890. +
  6891. +out:
  6892. + AuTraceErr(err);
  6893. + return err;
  6894. +}
  6895. +
  6896. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  6897. + struct dentry *h_parent, struct au_branch *br)
  6898. +{
  6899. + int err;
  6900. +
  6901. + err = 0;
  6902. + if (udba == AuOpt_UDBA_REVAL
  6903. + && !au_test_fs_remote(h_dentry->d_sb)) {
  6904. + IMustLock(h_dir);
  6905. + err = (d_inode(h_dentry->d_parent) != h_dir);
  6906. + } else if (udba != AuOpt_UDBA_NONE)
  6907. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  6908. +
  6909. + return err;
  6910. +}
  6911. +
  6912. +/* ---------------------------------------------------------------------- */
  6913. +
  6914. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  6915. +{
  6916. + int err;
  6917. + aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
  6918. + struct au_hdentry tmp, *p, *q;
  6919. + struct au_dinfo *dinfo;
  6920. + struct super_block *sb;
  6921. +
  6922. + DiMustWriteLock(dentry);
  6923. +
  6924. + sb = dentry->d_sb;
  6925. + dinfo = au_di(dentry);
  6926. + bend = dinfo->di_bend;
  6927. + bwh = dinfo->di_bwh;
  6928. + bdiropq = dinfo->di_bdiropq;
  6929. + p = dinfo->di_hdentry + dinfo->di_bstart;
  6930. + for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
  6931. + if (!p->hd_dentry)
  6932. + continue;
  6933. +
  6934. + new_bindex = au_br_index(sb, p->hd_id);
  6935. + if (new_bindex == bindex)
  6936. + continue;
  6937. +
  6938. + if (dinfo->di_bwh == bindex)
  6939. + bwh = new_bindex;
  6940. + if (dinfo->di_bdiropq == bindex)
  6941. + bdiropq = new_bindex;
  6942. + if (new_bindex < 0) {
  6943. + au_hdput(p);
  6944. + p->hd_dentry = NULL;
  6945. + continue;
  6946. + }
  6947. +
  6948. + /* swap two lower dentries, and loop again */
  6949. + q = dinfo->di_hdentry + new_bindex;
  6950. + tmp = *q;
  6951. + *q = *p;
  6952. + *p = tmp;
  6953. + if (tmp.hd_dentry) {
  6954. + bindex--;
  6955. + p--;
  6956. + }
  6957. + }
  6958. +
  6959. + dinfo->di_bwh = -1;
  6960. + if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
  6961. + dinfo->di_bwh = bwh;
  6962. +
  6963. + dinfo->di_bdiropq = -1;
  6964. + if (bdiropq >= 0
  6965. + && bdiropq <= au_sbend(sb)
  6966. + && au_sbr_whable(sb, bdiropq))
  6967. + dinfo->di_bdiropq = bdiropq;
  6968. +
  6969. + err = -EIO;
  6970. + dinfo->di_bstart = -1;
  6971. + dinfo->di_bend = -1;
  6972. + bend = au_dbend(parent);
  6973. + p = dinfo->di_hdentry;
  6974. + for (bindex = 0; bindex <= bend; bindex++, p++)
  6975. + if (p->hd_dentry) {
  6976. + dinfo->di_bstart = bindex;
  6977. + break;
  6978. + }
  6979. +
  6980. + if (dinfo->di_bstart >= 0) {
  6981. + p = dinfo->di_hdentry + bend;
  6982. + for (bindex = bend; bindex >= 0; bindex--, p--)
  6983. + if (p->hd_dentry) {
  6984. + dinfo->di_bend = bindex;
  6985. + err = 0;
  6986. + break;
  6987. + }
  6988. + }
  6989. +
  6990. + return err;
  6991. +}
  6992. +
  6993. +static void au_do_hide(struct dentry *dentry)
  6994. +{
  6995. + struct inode *inode;
  6996. +
  6997. + if (d_really_is_positive(dentry)) {
  6998. + inode = d_inode(dentry);
  6999. + if (!d_is_dir(dentry)) {
  7000. + if (inode->i_nlink && !d_unhashed(dentry))
  7001. + drop_nlink(inode);
  7002. + } else {
  7003. + clear_nlink(inode);
  7004. + /* stop next lookup */
  7005. + inode->i_flags |= S_DEAD;
  7006. + }
  7007. + smp_mb(); /* necessary? */
  7008. + }
  7009. + d_drop(dentry);
  7010. +}
  7011. +
  7012. +static int au_hide_children(struct dentry *parent)
  7013. +{
  7014. + int err, i, j, ndentry;
  7015. + struct au_dcsub_pages dpages;
  7016. + struct au_dpage *dpage;
  7017. + struct dentry *dentry;
  7018. +
  7019. + err = au_dpages_init(&dpages, GFP_NOFS);
  7020. + if (unlikely(err))
  7021. + goto out;
  7022. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7023. + if (unlikely(err))
  7024. + goto out_dpages;
  7025. +
  7026. + /* in reverse order */
  7027. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7028. + dpage = dpages.dpages + i;
  7029. + ndentry = dpage->ndentry;
  7030. + for (j = ndentry - 1; j >= 0; j--) {
  7031. + dentry = dpage->dentries[j];
  7032. + if (dentry != parent)
  7033. + au_do_hide(dentry);
  7034. + }
  7035. + }
  7036. +
  7037. +out_dpages:
  7038. + au_dpages_free(&dpages);
  7039. +out:
  7040. + return err;
  7041. +}
  7042. +
  7043. +static void au_hide(struct dentry *dentry)
  7044. +{
  7045. + int err;
  7046. +
  7047. + AuDbgDentry(dentry);
  7048. + if (d_is_dir(dentry)) {
  7049. + /* shrink_dcache_parent(dentry); */
  7050. + err = au_hide_children(dentry);
  7051. + if (unlikely(err))
  7052. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7053. + dentry, err);
  7054. + }
  7055. + au_do_hide(dentry);
  7056. +}
  7057. +
  7058. +/*
  7059. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7060. + *
  7061. + * a dirty branch is added
  7062. + * - on the top of layers
  7063. + * - in the middle of layers
  7064. + * - to the bottom of layers
  7065. + *
  7066. + * on the added branch there exists
  7067. + * - a whiteout
  7068. + * - a diropq
  7069. + * - a same named entry
  7070. + * + exist
  7071. + * * negative --> positive
  7072. + * * positive --> positive
  7073. + * - type is unchanged
  7074. + * - type is changed
  7075. + * + doesn't exist
  7076. + * * negative --> negative
  7077. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7078. + * - none
  7079. + */
  7080. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7081. + struct au_dinfo *tmp)
  7082. +{
  7083. + int err;
  7084. + aufs_bindex_t bindex, bend;
  7085. + struct {
  7086. + struct dentry *dentry;
  7087. + struct inode *inode;
  7088. + mode_t mode;
  7089. + } orig_h, tmp_h;
  7090. + struct au_hdentry *hd;
  7091. + struct inode *inode, *h_inode;
  7092. + struct dentry *h_dentry;
  7093. +
  7094. + err = 0;
  7095. + AuDebugOn(dinfo->di_bstart < 0);
  7096. + orig_h.mode = 0;
  7097. + orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
  7098. + orig_h.inode = NULL;
  7099. + if (d_is_positive(orig_h.dentry)) {
  7100. + orig_h.inode = d_inode(orig_h.dentry);
  7101. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7102. + }
  7103. + memset(&tmp_h, 0, sizeof(tmp_h));
  7104. + if (tmp->di_bstart >= 0) {
  7105. + tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
  7106. + tmp_h.inode = NULL;
  7107. + if (d_is_positive(tmp_h.dentry)) {
  7108. + tmp_h.inode = d_inode(tmp_h.dentry);
  7109. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7110. + }
  7111. + }
  7112. +
  7113. + inode = NULL;
  7114. + if (d_really_is_positive(dentry))
  7115. + inode = d_inode(dentry);
  7116. + if (!orig_h.inode) {
  7117. + AuDbg("nagative originally\n");
  7118. + if (inode) {
  7119. + au_hide(dentry);
  7120. + goto out;
  7121. + }
  7122. + AuDebugOn(inode);
  7123. + AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
  7124. + AuDebugOn(dinfo->di_bdiropq != -1);
  7125. +
  7126. + if (!tmp_h.inode) {
  7127. + AuDbg("negative --> negative\n");
  7128. + /* should have only one negative lower */
  7129. + if (tmp->di_bstart >= 0
  7130. + && tmp->di_bstart < dinfo->di_bstart) {
  7131. + AuDebugOn(tmp->di_bstart != tmp->di_bend);
  7132. + AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
  7133. + au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
  7134. + au_di_cp(dinfo, tmp);
  7135. + hd = tmp->di_hdentry + tmp->di_bstart;
  7136. + au_set_h_dptr(dentry, tmp->di_bstart,
  7137. + dget(hd->hd_dentry));
  7138. + }
  7139. + au_dbg_verify_dinode(dentry);
  7140. + } else {
  7141. + AuDbg("negative --> positive\n");
  7142. + /*
  7143. + * similar to the behaviour of creating with bypassing
  7144. + * aufs.
  7145. + * unhash it in order to force an error in the
  7146. + * succeeding create operation.
  7147. + * we should not set S_DEAD here.
  7148. + */
  7149. + d_drop(dentry);
  7150. + /* au_di_swap(tmp, dinfo); */
  7151. + au_dbg_verify_dinode(dentry);
  7152. + }
  7153. + } else {
  7154. + AuDbg("positive originally\n");
  7155. + /* inode may be NULL */
  7156. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7157. + if (!tmp_h.inode) {
  7158. + AuDbg("positive --> negative\n");
  7159. + /* or bypassing aufs */
  7160. + au_hide(dentry);
  7161. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
  7162. + dinfo->di_bwh = tmp->di_bwh;
  7163. + if (inode)
  7164. + err = au_refresh_hinode_self(inode);
  7165. + au_dbg_verify_dinode(dentry);
  7166. + } else if (orig_h.mode == tmp_h.mode) {
  7167. + AuDbg("positive --> positive, same type\n");
  7168. + if (!S_ISDIR(orig_h.mode)
  7169. + && dinfo->di_bstart > tmp->di_bstart) {
  7170. + /*
  7171. + * similar to the behaviour of removing and
  7172. + * creating.
  7173. + */
  7174. + au_hide(dentry);
  7175. + if (inode)
  7176. + err = au_refresh_hinode_self(inode);
  7177. + au_dbg_verify_dinode(dentry);
  7178. + } else {
  7179. + /* fill empty slots */
  7180. + if (dinfo->di_bstart > tmp->di_bstart)
  7181. + dinfo->di_bstart = tmp->di_bstart;
  7182. + if (dinfo->di_bend < tmp->di_bend)
  7183. + dinfo->di_bend = tmp->di_bend;
  7184. + dinfo->di_bwh = tmp->di_bwh;
  7185. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7186. + hd = tmp->di_hdentry;
  7187. + bend = dinfo->di_bend;
  7188. + for (bindex = tmp->di_bstart; bindex <= bend;
  7189. + bindex++) {
  7190. + if (au_h_dptr(dentry, bindex))
  7191. + continue;
  7192. + h_dentry = hd[bindex].hd_dentry;
  7193. + if (!h_dentry)
  7194. + continue;
  7195. + AuDebugOn(d_is_negative(h_dentry));
  7196. + h_inode = d_inode(h_dentry);
  7197. + AuDebugOn(orig_h.mode
  7198. + != (h_inode->i_mode
  7199. + & S_IFMT));
  7200. + au_set_h_dptr(dentry, bindex,
  7201. + dget(h_dentry));
  7202. + }
  7203. + err = au_refresh_hinode(inode, dentry);
  7204. + au_dbg_verify_dinode(dentry);
  7205. + }
  7206. + } else {
  7207. + AuDbg("positive --> positive, different type\n");
  7208. + /* similar to the behaviour of removing and creating */
  7209. + au_hide(dentry);
  7210. + if (inode)
  7211. + err = au_refresh_hinode_self(inode);
  7212. + au_dbg_verify_dinode(dentry);
  7213. + }
  7214. + }
  7215. +
  7216. +out:
  7217. + return err;
  7218. +}
  7219. +
  7220. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7221. +{
  7222. + int err, ebrange;
  7223. + unsigned int sigen;
  7224. + struct au_dinfo *dinfo, *tmp;
  7225. + struct super_block *sb;
  7226. + struct inode *inode;
  7227. +
  7228. + DiMustWriteLock(dentry);
  7229. + AuDebugOn(IS_ROOT(dentry));
  7230. + AuDebugOn(d_really_is_negative(parent));
  7231. +
  7232. + sb = dentry->d_sb;
  7233. + sigen = au_sigen(sb);
  7234. + err = au_digen_test(parent, sigen);
  7235. + if (unlikely(err))
  7236. + goto out;
  7237. +
  7238. + dinfo = au_di(dentry);
  7239. + err = au_di_realloc(dinfo, au_sbend(sb) + 1);
  7240. + if (unlikely(err))
  7241. + goto out;
  7242. + ebrange = au_dbrange_test(dentry);
  7243. + if (!ebrange)
  7244. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7245. +
  7246. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7247. + AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
  7248. + if (d_really_is_positive(dentry)) {
  7249. + inode = d_inode(dentry);
  7250. + err = au_refresh_hinode_self(inode);
  7251. + }
  7252. + au_dbg_verify_dinode(dentry);
  7253. + if (!err)
  7254. + goto out_dgen; /* success */
  7255. + goto out;
  7256. + }
  7257. +
  7258. + /* temporary dinfo */
  7259. + AuDbgDentry(dentry);
  7260. + err = -ENOMEM;
  7261. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7262. + if (unlikely(!tmp))
  7263. + goto out;
  7264. + au_di_swap(tmp, dinfo);
  7265. + /* returns the number of positive dentries */
  7266. + /*
  7267. + * if current working dir is removed, it returns an error.
  7268. + * but the dentry is legal.
  7269. + */
  7270. + err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
  7271. + AuDbgDentry(dentry);
  7272. + au_di_swap(tmp, dinfo);
  7273. + if (err == -ENOENT)
  7274. + err = 0;
  7275. + if (err >= 0) {
  7276. + /* compare/refresh by dinfo */
  7277. + AuDbgDentry(dentry);
  7278. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7279. + au_dbg_verify_dinode(dentry);
  7280. + AuTraceErr(err);
  7281. + }
  7282. + au_rw_write_unlock(&tmp->di_rwsem);
  7283. + au_di_free(tmp);
  7284. + if (unlikely(err))
  7285. + goto out;
  7286. +
  7287. +out_dgen:
  7288. + au_update_digen(dentry);
  7289. +out:
  7290. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7291. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7292. + AuDbgDentry(dentry);
  7293. + }
  7294. + AuTraceErr(err);
  7295. + return err;
  7296. +}
  7297. +
  7298. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7299. + struct dentry *dentry, aufs_bindex_t bindex)
  7300. +{
  7301. + int err, valid;
  7302. +
  7303. + err = 0;
  7304. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7305. + goto out;
  7306. +
  7307. + AuDbg("b%d\n", bindex);
  7308. + /*
  7309. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7310. + * due to whiteout and branch permission.
  7311. + */
  7312. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7313. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7314. + /* it may return tri-state */
  7315. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7316. +
  7317. + if (unlikely(valid < 0))
  7318. + err = valid;
  7319. + else if (!valid)
  7320. + err = -EINVAL;
  7321. +
  7322. +out:
  7323. + AuTraceErr(err);
  7324. + return err;
  7325. +}
  7326. +
  7327. +/* todo: remove this */
  7328. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7329. + unsigned int flags, int do_udba)
  7330. +{
  7331. + int err;
  7332. + umode_t mode, h_mode;
  7333. + aufs_bindex_t bindex, btail, bstart, ibs, ibe;
  7334. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7335. + struct inode *h_inode, *h_cached_inode;
  7336. + struct dentry *h_dentry;
  7337. + struct qstr *name, *h_name;
  7338. +
  7339. + err = 0;
  7340. + plus = 0;
  7341. + mode = 0;
  7342. + ibs = -1;
  7343. + ibe = -1;
  7344. + unhashed = !!d_unhashed(dentry);
  7345. + is_root = !!IS_ROOT(dentry);
  7346. + name = &dentry->d_name;
  7347. + tmpfile = au_di(dentry)->di_tmpfile;
  7348. +
  7349. + /*
  7350. + * Theoretically, REVAL test should be unnecessary in case of
  7351. + * {FS,I}NOTIFY.
  7352. + * But {fs,i}notify doesn't fire some necessary events,
  7353. + * IN_ATTRIB for atime/nlink/pageio
  7354. + * Let's do REVAL test too.
  7355. + */
  7356. + if (do_udba && inode) {
  7357. + mode = (inode->i_mode & S_IFMT);
  7358. + plus = (inode->i_nlink > 0);
  7359. + ibs = au_ibstart(inode);
  7360. + ibe = au_ibend(inode);
  7361. + }
  7362. +
  7363. + bstart = au_dbstart(dentry);
  7364. + btail = bstart;
  7365. + if (inode && S_ISDIR(inode->i_mode))
  7366. + btail = au_dbtaildir(dentry);
  7367. + for (bindex = bstart; bindex <= btail; bindex++) {
  7368. + h_dentry = au_h_dptr(dentry, bindex);
  7369. + if (!h_dentry)
  7370. + continue;
  7371. +
  7372. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7373. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7374. + spin_lock(&h_dentry->d_lock);
  7375. + h_name = &h_dentry->d_name;
  7376. + if (unlikely(do_udba
  7377. + && !is_root
  7378. + && ((!h_nfs
  7379. + && (unhashed != !!d_unhashed(h_dentry)
  7380. + || (!tmpfile
  7381. + && !au_qstreq(name, h_name))
  7382. + ))
  7383. + || (h_nfs
  7384. + && !(flags & LOOKUP_OPEN)
  7385. + && (h_dentry->d_flags
  7386. + & DCACHE_NFSFS_RENAMED)))
  7387. + )) {
  7388. + int h_unhashed;
  7389. +
  7390. + h_unhashed = d_unhashed(h_dentry);
  7391. + spin_unlock(&h_dentry->d_lock);
  7392. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7393. + unhashed, h_unhashed, dentry, h_dentry);
  7394. + goto err;
  7395. + }
  7396. + spin_unlock(&h_dentry->d_lock);
  7397. +
  7398. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7399. + if (unlikely(err))
  7400. + /* do not goto err, to keep the errno */
  7401. + break;
  7402. +
  7403. + /* todo: plink too? */
  7404. + if (!do_udba)
  7405. + continue;
  7406. +
  7407. + /* UDBA tests */
  7408. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7409. + goto err;
  7410. +
  7411. + h_inode = NULL;
  7412. + if (d_is_positive(h_dentry))
  7413. + h_inode = d_inode(h_dentry);
  7414. + h_plus = plus;
  7415. + h_mode = mode;
  7416. + h_cached_inode = h_inode;
  7417. + if (h_inode) {
  7418. + h_mode = (h_inode->i_mode & S_IFMT);
  7419. + h_plus = (h_inode->i_nlink > 0);
  7420. + }
  7421. + if (inode && ibs <= bindex && bindex <= ibe)
  7422. + h_cached_inode = au_h_iptr(inode, bindex);
  7423. +
  7424. + if (!h_nfs) {
  7425. + if (unlikely(plus != h_plus && !tmpfile))
  7426. + goto err;
  7427. + } else {
  7428. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7429. + && !is_root
  7430. + && !IS_ROOT(h_dentry)
  7431. + && unhashed != d_unhashed(h_dentry)))
  7432. + goto err;
  7433. + }
  7434. + if (unlikely(mode != h_mode
  7435. + || h_cached_inode != h_inode))
  7436. + goto err;
  7437. + continue;
  7438. +
  7439. +err:
  7440. + err = -EINVAL;
  7441. + break;
  7442. + }
  7443. +
  7444. + AuTraceErr(err);
  7445. + return err;
  7446. +}
  7447. +
  7448. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  7449. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7450. +{
  7451. + int err;
  7452. + struct dentry *parent;
  7453. +
  7454. + if (!au_digen_test(dentry, sigen))
  7455. + return 0;
  7456. +
  7457. + parent = dget_parent(dentry);
  7458. + di_read_lock_parent(parent, AuLock_IR);
  7459. + AuDebugOn(au_digen_test(parent, sigen));
  7460. + au_dbg_verify_gen(parent, sigen);
  7461. + err = au_refresh_dentry(dentry, parent);
  7462. + di_read_unlock(parent, AuLock_IR);
  7463. + dput(parent);
  7464. + AuTraceErr(err);
  7465. + return err;
  7466. +}
  7467. +
  7468. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7469. +{
  7470. + int err;
  7471. + struct dentry *d, *parent;
  7472. +
  7473. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  7474. + return simple_reval_dpath(dentry, sigen);
  7475. +
  7476. + /* slow loop, keep it simple and stupid */
  7477. + /* cf: au_cpup_dirs() */
  7478. + err = 0;
  7479. + parent = NULL;
  7480. + while (au_digen_test(dentry, sigen)) {
  7481. + d = dentry;
  7482. + while (1) {
  7483. + dput(parent);
  7484. + parent = dget_parent(d);
  7485. + if (!au_digen_test(parent, sigen))
  7486. + break;
  7487. + d = parent;
  7488. + }
  7489. +
  7490. + if (d != dentry)
  7491. + di_write_lock_child2(d);
  7492. +
  7493. + /* someone might update our dentry while we were sleeping */
  7494. + if (au_digen_test(d, sigen)) {
  7495. + /*
  7496. + * todo: consolidate with simple_reval_dpath(),
  7497. + * do_refresh() and au_reval_for_attr().
  7498. + */
  7499. + di_read_lock_parent(parent, AuLock_IR);
  7500. + err = au_refresh_dentry(d, parent);
  7501. + di_read_unlock(parent, AuLock_IR);
  7502. + }
  7503. +
  7504. + if (d != dentry)
  7505. + di_write_unlock(d);
  7506. + dput(parent);
  7507. + if (unlikely(err))
  7508. + break;
  7509. + }
  7510. +
  7511. + return err;
  7512. +}
  7513. +
  7514. +/*
  7515. + * if valid returns 1, otherwise 0.
  7516. + */
  7517. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  7518. +{
  7519. + int valid, err;
  7520. + unsigned int sigen;
  7521. + unsigned char do_udba;
  7522. + struct super_block *sb;
  7523. + struct inode *inode;
  7524. +
  7525. + /* todo: support rcu-walk? */
  7526. + if (flags & LOOKUP_RCU)
  7527. + return -ECHILD;
  7528. +
  7529. + valid = 0;
  7530. + if (unlikely(!au_di(dentry)))
  7531. + goto out;
  7532. +
  7533. + valid = 1;
  7534. + sb = dentry->d_sb;
  7535. + /*
  7536. + * todo: very ugly
  7537. + * i_mutex of parent dir may be held,
  7538. + * but we should not return 'invalid' due to busy.
  7539. + */
  7540. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  7541. + if (unlikely(err)) {
  7542. + valid = err;
  7543. + AuTraceErr(err);
  7544. + goto out;
  7545. + }
  7546. + inode = NULL;
  7547. + if (d_really_is_positive(dentry))
  7548. + inode = d_inode(dentry);
  7549. + if (unlikely(inode && is_bad_inode(inode))) {
  7550. + err = -EINVAL;
  7551. + AuTraceErr(err);
  7552. + goto out_dgrade;
  7553. + }
  7554. + if (unlikely(au_dbrange_test(dentry))) {
  7555. + err = -EINVAL;
  7556. + AuTraceErr(err);
  7557. + goto out_dgrade;
  7558. + }
  7559. +
  7560. + sigen = au_sigen(sb);
  7561. + if (au_digen_test(dentry, sigen)) {
  7562. + AuDebugOn(IS_ROOT(dentry));
  7563. + err = au_reval_dpath(dentry, sigen);
  7564. + if (unlikely(err)) {
  7565. + AuTraceErr(err);
  7566. + goto out_dgrade;
  7567. + }
  7568. + }
  7569. + di_downgrade_lock(dentry, AuLock_IR);
  7570. +
  7571. + err = -EINVAL;
  7572. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  7573. + && inode
  7574. + && !(inode->i_state && I_LINKABLE)
  7575. + && (IS_DEADDIR(inode) || !inode->i_nlink))
  7576. + goto out_inval;
  7577. +
  7578. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  7579. + if (do_udba && inode) {
  7580. + aufs_bindex_t bstart = au_ibstart(inode);
  7581. + struct inode *h_inode;
  7582. +
  7583. + if (bstart >= 0) {
  7584. + h_inode = au_h_iptr(inode, bstart);
  7585. + if (h_inode && au_test_higen(inode, h_inode))
  7586. + goto out_inval;
  7587. + }
  7588. + }
  7589. +
  7590. + err = h_d_revalidate(dentry, inode, flags, do_udba);
  7591. + if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
  7592. + err = -EIO;
  7593. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  7594. + dentry, err);
  7595. + }
  7596. + goto out_inval;
  7597. +
  7598. +out_dgrade:
  7599. + di_downgrade_lock(dentry, AuLock_IR);
  7600. +out_inval:
  7601. + aufs_read_unlock(dentry, AuLock_IR);
  7602. + AuTraceErr(err);
  7603. + valid = !err;
  7604. +out:
  7605. + if (!valid) {
  7606. + AuDbg("%pd invalid, %d\n", dentry, valid);
  7607. + d_drop(dentry);
  7608. + }
  7609. + return valid;
  7610. +}
  7611. +
  7612. +static void aufs_d_release(struct dentry *dentry)
  7613. +{
  7614. + if (au_di(dentry)) {
  7615. + au_di_fin(dentry);
  7616. + au_hn_di_reinit(dentry);
  7617. + }
  7618. +}
  7619. +
  7620. +const struct dentry_operations aufs_dop = {
  7621. + .d_revalidate = aufs_d_revalidate,
  7622. + .d_weak_revalidate = aufs_d_revalidate,
  7623. + .d_release = aufs_d_release
  7624. +};
  7625. diff -Nur linux-4.1.10.orig/fs/aufs/dentry.h linux-4.1.10/fs/aufs/dentry.h
  7626. --- linux-4.1.10.orig/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
  7627. +++ linux-4.1.10/fs/aufs/dentry.h 2015-10-22 21:35:53.000000000 +0200
  7628. @@ -0,0 +1,233 @@
  7629. +/*
  7630. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7631. + *
  7632. + * This program, aufs is free software; you can redistribute it and/or modify
  7633. + * it under the terms of the GNU General Public License as published by
  7634. + * the Free Software Foundation; either version 2 of the License, or
  7635. + * (at your option) any later version.
  7636. + *
  7637. + * This program is distributed in the hope that it will be useful,
  7638. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7639. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7640. + * GNU General Public License for more details.
  7641. + *
  7642. + * You should have received a copy of the GNU General Public License
  7643. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7644. + */
  7645. +
  7646. +/*
  7647. + * lookup and dentry operations
  7648. + */
  7649. +
  7650. +#ifndef __AUFS_DENTRY_H__
  7651. +#define __AUFS_DENTRY_H__
  7652. +
  7653. +#ifdef __KERNEL__
  7654. +
  7655. +#include <linux/dcache.h>
  7656. +#include "rwsem.h"
  7657. +
  7658. +struct au_hdentry {
  7659. + struct dentry *hd_dentry;
  7660. + aufs_bindex_t hd_id;
  7661. +};
  7662. +
  7663. +struct au_dinfo {
  7664. + atomic_t di_generation;
  7665. +
  7666. + struct au_rwsem di_rwsem;
  7667. + aufs_bindex_t di_bstart, di_bend, di_bwh, di_bdiropq;
  7668. + unsigned char di_tmpfile; /* to allow the different name */
  7669. + struct au_hdentry *di_hdentry;
  7670. +} ____cacheline_aligned_in_smp;
  7671. +
  7672. +/* ---------------------------------------------------------------------- */
  7673. +
  7674. +/* dentry.c */
  7675. +extern const struct dentry_operations aufs_dop;
  7676. +struct au_branch;
  7677. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  7678. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7679. + struct dentry *h_parent, struct au_branch *br);
  7680. +
  7681. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
  7682. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  7683. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  7684. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  7685. +
  7686. +/* dinfo.c */
  7687. +void au_di_init_once(void *_di);
  7688. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  7689. +void au_di_free(struct au_dinfo *dinfo);
  7690. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  7691. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  7692. +int au_di_init(struct dentry *dentry);
  7693. +void au_di_fin(struct dentry *dentry);
  7694. +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
  7695. +
  7696. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  7697. +void di_read_unlock(struct dentry *d, int flags);
  7698. +void di_downgrade_lock(struct dentry *d, int flags);
  7699. +void di_write_lock(struct dentry *d, unsigned int lsc);
  7700. +void di_write_unlock(struct dentry *d);
  7701. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  7702. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  7703. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  7704. +
  7705. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  7706. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  7707. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  7708. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  7709. +
  7710. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  7711. + struct dentry *h_dentry);
  7712. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  7713. +int au_dbrange_test(struct dentry *dentry);
  7714. +void au_update_digen(struct dentry *dentry);
  7715. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  7716. +void au_update_dbstart(struct dentry *dentry);
  7717. +void au_update_dbend(struct dentry *dentry);
  7718. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  7719. +
  7720. +/* ---------------------------------------------------------------------- */
  7721. +
  7722. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  7723. +{
  7724. + return dentry->d_fsdata;
  7725. +}
  7726. +
  7727. +/* ---------------------------------------------------------------------- */
  7728. +
  7729. +/* lock subclass for dinfo */
  7730. +enum {
  7731. + AuLsc_DI_CHILD, /* child first */
  7732. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  7733. + AuLsc_DI_CHILD3, /* copyup dirs */
  7734. + AuLsc_DI_PARENT,
  7735. + AuLsc_DI_PARENT2,
  7736. + AuLsc_DI_PARENT3,
  7737. + AuLsc_DI_TMP /* temp for replacing dinfo */
  7738. +};
  7739. +
  7740. +/*
  7741. + * di_read_lock_child, di_write_lock_child,
  7742. + * di_read_lock_child2, di_write_lock_child2,
  7743. + * di_read_lock_child3, di_write_lock_child3,
  7744. + * di_read_lock_parent, di_write_lock_parent,
  7745. + * di_read_lock_parent2, di_write_lock_parent2,
  7746. + * di_read_lock_parent3, di_write_lock_parent3,
  7747. + */
  7748. +#define AuReadLockFunc(name, lsc) \
  7749. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  7750. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  7751. +
  7752. +#define AuWriteLockFunc(name, lsc) \
  7753. +static inline void di_write_lock_##name(struct dentry *d) \
  7754. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  7755. +
  7756. +#define AuRWLockFuncs(name, lsc) \
  7757. + AuReadLockFunc(name, lsc) \
  7758. + AuWriteLockFunc(name, lsc)
  7759. +
  7760. +AuRWLockFuncs(child, CHILD);
  7761. +AuRWLockFuncs(child2, CHILD2);
  7762. +AuRWLockFuncs(child3, CHILD3);
  7763. +AuRWLockFuncs(parent, PARENT);
  7764. +AuRWLockFuncs(parent2, PARENT2);
  7765. +AuRWLockFuncs(parent3, PARENT3);
  7766. +
  7767. +#undef AuReadLockFunc
  7768. +#undef AuWriteLockFunc
  7769. +#undef AuRWLockFuncs
  7770. +
  7771. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  7772. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  7773. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  7774. +
  7775. +/* ---------------------------------------------------------------------- */
  7776. +
  7777. +/* todo: memory barrier? */
  7778. +static inline unsigned int au_digen(struct dentry *d)
  7779. +{
  7780. + return atomic_read(&au_di(d)->di_generation);
  7781. +}
  7782. +
  7783. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  7784. +{
  7785. + hdentry->hd_dentry = NULL;
  7786. +}
  7787. +
  7788. +static inline void au_hdput(struct au_hdentry *hd)
  7789. +{
  7790. + if (hd)
  7791. + dput(hd->hd_dentry);
  7792. +}
  7793. +
  7794. +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
  7795. +{
  7796. + DiMustAnyLock(dentry);
  7797. + return au_di(dentry)->di_bstart;
  7798. +}
  7799. +
  7800. +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
  7801. +{
  7802. + DiMustAnyLock(dentry);
  7803. + return au_di(dentry)->di_bend;
  7804. +}
  7805. +
  7806. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  7807. +{
  7808. + DiMustAnyLock(dentry);
  7809. + return au_di(dentry)->di_bwh;
  7810. +}
  7811. +
  7812. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  7813. +{
  7814. + DiMustAnyLock(dentry);
  7815. + return au_di(dentry)->di_bdiropq;
  7816. +}
  7817. +
  7818. +/* todo: hard/soft set? */
  7819. +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
  7820. +{
  7821. + DiMustWriteLock(dentry);
  7822. + au_di(dentry)->di_bstart = bindex;
  7823. +}
  7824. +
  7825. +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
  7826. +{
  7827. + DiMustWriteLock(dentry);
  7828. + au_di(dentry)->di_bend = bindex;
  7829. +}
  7830. +
  7831. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  7832. +{
  7833. + DiMustWriteLock(dentry);
  7834. + /* dbwh can be outside of bstart - bend range */
  7835. + au_di(dentry)->di_bwh = bindex;
  7836. +}
  7837. +
  7838. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  7839. +{
  7840. + DiMustWriteLock(dentry);
  7841. + au_di(dentry)->di_bdiropq = bindex;
  7842. +}
  7843. +
  7844. +/* ---------------------------------------------------------------------- */
  7845. +
  7846. +#ifdef CONFIG_AUFS_HNOTIFY
  7847. +static inline void au_digen_dec(struct dentry *d)
  7848. +{
  7849. + atomic_dec(&au_di(d)->di_generation);
  7850. +}
  7851. +
  7852. +static inline void au_hn_di_reinit(struct dentry *dentry)
  7853. +{
  7854. + dentry->d_fsdata = NULL;
  7855. +}
  7856. +#else
  7857. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  7858. +#endif /* CONFIG_AUFS_HNOTIFY */
  7859. +
  7860. +#endif /* __KERNEL__ */
  7861. +#endif /* __AUFS_DENTRY_H__ */
  7862. diff -Nur linux-4.1.10.orig/fs/aufs/dinfo.c linux-4.1.10/fs/aufs/dinfo.c
  7863. --- linux-4.1.10.orig/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
  7864. +++ linux-4.1.10/fs/aufs/dinfo.c 2015-10-22 21:35:53.000000000 +0200
  7865. @@ -0,0 +1,550 @@
  7866. +/*
  7867. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  7868. + *
  7869. + * This program, aufs is free software; you can redistribute it and/or modify
  7870. + * it under the terms of the GNU General Public License as published by
  7871. + * the Free Software Foundation; either version 2 of the License, or
  7872. + * (at your option) any later version.
  7873. + *
  7874. + * This program is distributed in the hope that it will be useful,
  7875. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7876. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7877. + * GNU General Public License for more details.
  7878. + *
  7879. + * You should have received a copy of the GNU General Public License
  7880. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7881. + */
  7882. +
  7883. +/*
  7884. + * dentry private data
  7885. + */
  7886. +
  7887. +#include "aufs.h"
  7888. +
  7889. +void au_di_init_once(void *_dinfo)
  7890. +{
  7891. + struct au_dinfo *dinfo = _dinfo;
  7892. + static struct lock_class_key aufs_di;
  7893. +
  7894. + au_rw_init(&dinfo->di_rwsem);
  7895. + au_rw_class(&dinfo->di_rwsem, &aufs_di);
  7896. +}
  7897. +
  7898. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  7899. +{
  7900. + struct au_dinfo *dinfo;
  7901. + int nbr, i;
  7902. +
  7903. + dinfo = au_cache_alloc_dinfo();
  7904. + if (unlikely(!dinfo))
  7905. + goto out;
  7906. +
  7907. + nbr = au_sbend(sb) + 1;
  7908. + if (nbr <= 0)
  7909. + nbr = 1;
  7910. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  7911. + if (dinfo->di_hdentry) {
  7912. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  7913. + dinfo->di_bstart = -1;
  7914. + dinfo->di_bend = -1;
  7915. + dinfo->di_bwh = -1;
  7916. + dinfo->di_bdiropq = -1;
  7917. + dinfo->di_tmpfile = 0;
  7918. + for (i = 0; i < nbr; i++)
  7919. + dinfo->di_hdentry[i].hd_id = -1;
  7920. + goto out;
  7921. + }
  7922. +
  7923. + au_cache_free_dinfo(dinfo);
  7924. + dinfo = NULL;
  7925. +
  7926. +out:
  7927. + return dinfo;
  7928. +}
  7929. +
  7930. +void au_di_free(struct au_dinfo *dinfo)
  7931. +{
  7932. + struct au_hdentry *p;
  7933. + aufs_bindex_t bend, bindex;
  7934. +
  7935. + /* dentry may not be revalidated */
  7936. + bindex = dinfo->di_bstart;
  7937. + if (bindex >= 0) {
  7938. + bend = dinfo->di_bend;
  7939. + p = dinfo->di_hdentry + bindex;
  7940. + while (bindex++ <= bend)
  7941. + au_hdput(p++);
  7942. + }
  7943. + kfree(dinfo->di_hdentry);
  7944. + au_cache_free_dinfo(dinfo);
  7945. +}
  7946. +
  7947. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  7948. +{
  7949. + struct au_hdentry *p;
  7950. + aufs_bindex_t bi;
  7951. +
  7952. + AuRwMustWriteLock(&a->di_rwsem);
  7953. + AuRwMustWriteLock(&b->di_rwsem);
  7954. +
  7955. +#define DiSwap(v, name) \
  7956. + do { \
  7957. + v = a->di_##name; \
  7958. + a->di_##name = b->di_##name; \
  7959. + b->di_##name = v; \
  7960. + } while (0)
  7961. +
  7962. + DiSwap(p, hdentry);
  7963. + DiSwap(bi, bstart);
  7964. + DiSwap(bi, bend);
  7965. + DiSwap(bi, bwh);
  7966. + DiSwap(bi, bdiropq);
  7967. + /* smp_mb(); */
  7968. +
  7969. +#undef DiSwap
  7970. +}
  7971. +
  7972. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  7973. +{
  7974. + AuRwMustWriteLock(&dst->di_rwsem);
  7975. + AuRwMustWriteLock(&src->di_rwsem);
  7976. +
  7977. + dst->di_bstart = src->di_bstart;
  7978. + dst->di_bend = src->di_bend;
  7979. + dst->di_bwh = src->di_bwh;
  7980. + dst->di_bdiropq = src->di_bdiropq;
  7981. + /* smp_mb(); */
  7982. +}
  7983. +
  7984. +int au_di_init(struct dentry *dentry)
  7985. +{
  7986. + int err;
  7987. + struct super_block *sb;
  7988. + struct au_dinfo *dinfo;
  7989. +
  7990. + err = 0;
  7991. + sb = dentry->d_sb;
  7992. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  7993. + if (dinfo) {
  7994. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  7995. + /* smp_mb(); */ /* atomic_set */
  7996. + dentry->d_fsdata = dinfo;
  7997. + } else
  7998. + err = -ENOMEM;
  7999. +
  8000. + return err;
  8001. +}
  8002. +
  8003. +void au_di_fin(struct dentry *dentry)
  8004. +{
  8005. + struct au_dinfo *dinfo;
  8006. +
  8007. + dinfo = au_di(dentry);
  8008. + AuRwDestroy(&dinfo->di_rwsem);
  8009. + au_di_free(dinfo);
  8010. +}
  8011. +
  8012. +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
  8013. +{
  8014. + int err, sz;
  8015. + struct au_hdentry *hdp;
  8016. +
  8017. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8018. +
  8019. + err = -ENOMEM;
  8020. + sz = sizeof(*hdp) * (dinfo->di_bend + 1);
  8021. + if (!sz)
  8022. + sz = sizeof(*hdp);
  8023. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
  8024. + if (hdp) {
  8025. + dinfo->di_hdentry = hdp;
  8026. + err = 0;
  8027. + }
  8028. +
  8029. + return err;
  8030. +}
  8031. +
  8032. +/* ---------------------------------------------------------------------- */
  8033. +
  8034. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8035. +{
  8036. + switch (lsc) {
  8037. + case AuLsc_DI_CHILD:
  8038. + ii_write_lock_child(inode);
  8039. + break;
  8040. + case AuLsc_DI_CHILD2:
  8041. + ii_write_lock_child2(inode);
  8042. + break;
  8043. + case AuLsc_DI_CHILD3:
  8044. + ii_write_lock_child3(inode);
  8045. + break;
  8046. + case AuLsc_DI_PARENT:
  8047. + ii_write_lock_parent(inode);
  8048. + break;
  8049. + case AuLsc_DI_PARENT2:
  8050. + ii_write_lock_parent2(inode);
  8051. + break;
  8052. + case AuLsc_DI_PARENT3:
  8053. + ii_write_lock_parent3(inode);
  8054. + break;
  8055. + default:
  8056. + BUG();
  8057. + }
  8058. +}
  8059. +
  8060. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8061. +{
  8062. + switch (lsc) {
  8063. + case AuLsc_DI_CHILD:
  8064. + ii_read_lock_child(inode);
  8065. + break;
  8066. + case AuLsc_DI_CHILD2:
  8067. + ii_read_lock_child2(inode);
  8068. + break;
  8069. + case AuLsc_DI_CHILD3:
  8070. + ii_read_lock_child3(inode);
  8071. + break;
  8072. + case AuLsc_DI_PARENT:
  8073. + ii_read_lock_parent(inode);
  8074. + break;
  8075. + case AuLsc_DI_PARENT2:
  8076. + ii_read_lock_parent2(inode);
  8077. + break;
  8078. + case AuLsc_DI_PARENT3:
  8079. + ii_read_lock_parent3(inode);
  8080. + break;
  8081. + default:
  8082. + BUG();
  8083. + }
  8084. +}
  8085. +
  8086. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8087. +{
  8088. + struct inode *inode;
  8089. +
  8090. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8091. + if (d_really_is_positive(d)) {
  8092. + inode = d_inode(d);
  8093. + if (au_ftest_lock(flags, IW))
  8094. + do_ii_write_lock(inode, lsc);
  8095. + else if (au_ftest_lock(flags, IR))
  8096. + do_ii_read_lock(inode, lsc);
  8097. + }
  8098. +}
  8099. +
  8100. +void di_read_unlock(struct dentry *d, int flags)
  8101. +{
  8102. + struct inode *inode;
  8103. +
  8104. + if (d_really_is_positive(d)) {
  8105. + inode = d_inode(d);
  8106. + if (au_ftest_lock(flags, IW)) {
  8107. + au_dbg_verify_dinode(d);
  8108. + ii_write_unlock(inode);
  8109. + } else if (au_ftest_lock(flags, IR)) {
  8110. + au_dbg_verify_dinode(d);
  8111. + ii_read_unlock(inode);
  8112. + }
  8113. + }
  8114. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8115. +}
  8116. +
  8117. +void di_downgrade_lock(struct dentry *d, int flags)
  8118. +{
  8119. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8120. + ii_downgrade_lock(d_inode(d));
  8121. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8122. +}
  8123. +
  8124. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8125. +{
  8126. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8127. + if (d_really_is_positive(d))
  8128. + do_ii_write_lock(d_inode(d), lsc);
  8129. +}
  8130. +
  8131. +void di_write_unlock(struct dentry *d)
  8132. +{
  8133. + au_dbg_verify_dinode(d);
  8134. + if (d_really_is_positive(d))
  8135. + ii_write_unlock(d_inode(d));
  8136. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8137. +}
  8138. +
  8139. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8140. +{
  8141. + AuDebugOn(d1 == d2
  8142. + || d_inode(d1) == d_inode(d2)
  8143. + || d1->d_sb != d2->d_sb);
  8144. +
  8145. + if (isdir && au_test_subdir(d1, d2)) {
  8146. + di_write_lock_child(d1);
  8147. + di_write_lock_child2(d2);
  8148. + } else {
  8149. + /* there should be no races */
  8150. + di_write_lock_child(d2);
  8151. + di_write_lock_child2(d1);
  8152. + }
  8153. +}
  8154. +
  8155. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8156. +{
  8157. + AuDebugOn(d1 == d2
  8158. + || d_inode(d1) == d_inode(d2)
  8159. + || d1->d_sb != d2->d_sb);
  8160. +
  8161. + if (isdir && au_test_subdir(d1, d2)) {
  8162. + di_write_lock_parent(d1);
  8163. + di_write_lock_parent2(d2);
  8164. + } else {
  8165. + /* there should be no races */
  8166. + di_write_lock_parent(d2);
  8167. + di_write_lock_parent2(d1);
  8168. + }
  8169. +}
  8170. +
  8171. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8172. +{
  8173. + di_write_unlock(d1);
  8174. + if (d_inode(d1) == d_inode(d2))
  8175. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8176. + else
  8177. + di_write_unlock(d2);
  8178. +}
  8179. +
  8180. +/* ---------------------------------------------------------------------- */
  8181. +
  8182. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8183. +{
  8184. + struct dentry *d;
  8185. +
  8186. + DiMustAnyLock(dentry);
  8187. +
  8188. + if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
  8189. + return NULL;
  8190. + AuDebugOn(bindex < 0);
  8191. + d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
  8192. + AuDebugOn(d && au_dcount(d) <= 0);
  8193. + return d;
  8194. +}
  8195. +
  8196. +/*
  8197. + * extended version of au_h_dptr().
  8198. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8199. + * error.
  8200. + */
  8201. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8202. +{
  8203. + struct dentry *h_dentry;
  8204. + struct inode *inode, *h_inode;
  8205. +
  8206. + AuDebugOn(d_really_is_negative(dentry));
  8207. +
  8208. + h_dentry = NULL;
  8209. + if (au_dbstart(dentry) <= bindex
  8210. + && bindex <= au_dbend(dentry))
  8211. + h_dentry = au_h_dptr(dentry, bindex);
  8212. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8213. + dget(h_dentry);
  8214. + goto out; /* success */
  8215. + }
  8216. +
  8217. + inode = d_inode(dentry);
  8218. + AuDebugOn(bindex < au_ibstart(inode));
  8219. + AuDebugOn(au_ibend(inode) < bindex);
  8220. + h_inode = au_h_iptr(inode, bindex);
  8221. + h_dentry = d_find_alias(h_inode);
  8222. + if (h_dentry) {
  8223. + if (!IS_ERR(h_dentry)) {
  8224. + if (!au_d_linkable(h_dentry))
  8225. + goto out; /* success */
  8226. + dput(h_dentry);
  8227. + } else
  8228. + goto out;
  8229. + }
  8230. +
  8231. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8232. + h_dentry = au_plink_lkup(inode, bindex);
  8233. + AuDebugOn(!h_dentry);
  8234. + if (!IS_ERR(h_dentry)) {
  8235. + if (!au_d_hashed_positive(h_dentry))
  8236. + goto out; /* success */
  8237. + dput(h_dentry);
  8238. + h_dentry = NULL;
  8239. + }
  8240. + }
  8241. +
  8242. +out:
  8243. + AuDbgDentry(h_dentry);
  8244. + return h_dentry;
  8245. +}
  8246. +
  8247. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8248. +{
  8249. + aufs_bindex_t bend, bwh;
  8250. +
  8251. + bend = au_dbend(dentry);
  8252. + if (0 <= bend) {
  8253. + bwh = au_dbwh(dentry);
  8254. + if (!bwh)
  8255. + return bwh;
  8256. + if (0 < bwh && bwh < bend)
  8257. + return bwh - 1;
  8258. + }
  8259. + return bend;
  8260. +}
  8261. +
  8262. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8263. +{
  8264. + aufs_bindex_t bend, bopq;
  8265. +
  8266. + bend = au_dbtail(dentry);
  8267. + if (0 <= bend) {
  8268. + bopq = au_dbdiropq(dentry);
  8269. + if (0 <= bopq && bopq < bend)
  8270. + bend = bopq;
  8271. + }
  8272. + return bend;
  8273. +}
  8274. +
  8275. +/* ---------------------------------------------------------------------- */
  8276. +
  8277. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8278. + struct dentry *h_dentry)
  8279. +{
  8280. + struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
  8281. + struct au_branch *br;
  8282. +
  8283. + DiMustWriteLock(dentry);
  8284. +
  8285. + au_hdput(hd);
  8286. + hd->hd_dentry = h_dentry;
  8287. + if (h_dentry) {
  8288. + br = au_sbr(dentry->d_sb, bindex);
  8289. + hd->hd_id = br->br_id;
  8290. + }
  8291. +}
  8292. +
  8293. +int au_dbrange_test(struct dentry *dentry)
  8294. +{
  8295. + int err;
  8296. + aufs_bindex_t bstart, bend;
  8297. +
  8298. + err = 0;
  8299. + bstart = au_dbstart(dentry);
  8300. + bend = au_dbend(dentry);
  8301. + if (bstart >= 0)
  8302. + AuDebugOn(bend < 0 && bstart > bend);
  8303. + else {
  8304. + err = -EIO;
  8305. + AuDebugOn(bend >= 0);
  8306. + }
  8307. +
  8308. + return err;
  8309. +}
  8310. +
  8311. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8312. +{
  8313. + int err;
  8314. +
  8315. + err = 0;
  8316. + if (unlikely(au_digen(dentry) != sigen
  8317. + || au_iigen_test(d_inode(dentry), sigen)))
  8318. + err = -EIO;
  8319. +
  8320. + return err;
  8321. +}
  8322. +
  8323. +void au_update_digen(struct dentry *dentry)
  8324. +{
  8325. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8326. + /* smp_mb(); */ /* atomic_set */
  8327. +}
  8328. +
  8329. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8330. +{
  8331. + struct au_dinfo *dinfo;
  8332. + struct dentry *h_d;
  8333. + struct au_hdentry *hdp;
  8334. +
  8335. + DiMustWriteLock(dentry);
  8336. +
  8337. + dinfo = au_di(dentry);
  8338. + if (!dinfo || dinfo->di_bstart < 0)
  8339. + return;
  8340. +
  8341. + hdp = dinfo->di_hdentry;
  8342. + if (do_put_zero) {
  8343. + aufs_bindex_t bindex, bend;
  8344. +
  8345. + bend = dinfo->di_bend;
  8346. + for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
  8347. + h_d = hdp[0 + bindex].hd_dentry;
  8348. + if (h_d && d_is_negative(h_d))
  8349. + au_set_h_dptr(dentry, bindex, NULL);
  8350. + }
  8351. + }
  8352. +
  8353. + dinfo->di_bstart = -1;
  8354. + while (++dinfo->di_bstart <= dinfo->di_bend)
  8355. + if (hdp[0 + dinfo->di_bstart].hd_dentry)
  8356. + break;
  8357. + if (dinfo->di_bstart > dinfo->di_bend) {
  8358. + dinfo->di_bstart = -1;
  8359. + dinfo->di_bend = -1;
  8360. + return;
  8361. + }
  8362. +
  8363. + dinfo->di_bend++;
  8364. + while (0 <= --dinfo->di_bend)
  8365. + if (hdp[0 + dinfo->di_bend].hd_dentry)
  8366. + break;
  8367. + AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
  8368. +}
  8369. +
  8370. +void au_update_dbstart(struct dentry *dentry)
  8371. +{
  8372. + aufs_bindex_t bindex, bend;
  8373. + struct dentry *h_dentry;
  8374. +
  8375. + bend = au_dbend(dentry);
  8376. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
  8377. + h_dentry = au_h_dptr(dentry, bindex);
  8378. + if (!h_dentry)
  8379. + continue;
  8380. + if (d_is_positive(h_dentry)) {
  8381. + au_set_dbstart(dentry, bindex);
  8382. + return;
  8383. + }
  8384. + au_set_h_dptr(dentry, bindex, NULL);
  8385. + }
  8386. +}
  8387. +
  8388. +void au_update_dbend(struct dentry *dentry)
  8389. +{
  8390. + aufs_bindex_t bindex, bstart;
  8391. + struct dentry *h_dentry;
  8392. +
  8393. + bstart = au_dbstart(dentry);
  8394. + for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
  8395. + h_dentry = au_h_dptr(dentry, bindex);
  8396. + if (!h_dentry)
  8397. + continue;
  8398. + if (d_is_positive(h_dentry)) {
  8399. + au_set_dbend(dentry, bindex);
  8400. + return;
  8401. + }
  8402. + au_set_h_dptr(dentry, bindex, NULL);
  8403. + }
  8404. +}
  8405. +
  8406. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  8407. +{
  8408. + aufs_bindex_t bindex, bend;
  8409. +
  8410. + bend = au_dbend(dentry);
  8411. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
  8412. + if (au_h_dptr(dentry, bindex) == h_dentry)
  8413. + return bindex;
  8414. + return -1;
  8415. +}
  8416. diff -Nur linux-4.1.10.orig/fs/aufs/dir.c linux-4.1.10/fs/aufs/dir.c
  8417. --- linux-4.1.10.orig/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
  8418. +++ linux-4.1.10/fs/aufs/dir.c 2015-10-22 21:35:53.000000000 +0200
  8419. @@ -0,0 +1,753 @@
  8420. +/*
  8421. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  8422. + *
  8423. + * This program, aufs is free software; you can redistribute it and/or modify
  8424. + * it under the terms of the GNU General Public License as published by
  8425. + * the Free Software Foundation; either version 2 of the License, or
  8426. + * (at your option) any later version.
  8427. + *
  8428. + * This program is distributed in the hope that it will be useful,
  8429. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8430. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8431. + * GNU General Public License for more details.
  8432. + *
  8433. + * You should have received a copy of the GNU General Public License
  8434. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8435. + */
  8436. +
  8437. +/*
  8438. + * directory operations
  8439. + */
  8440. +
  8441. +#include <linux/fs_stack.h>
  8442. +#include "aufs.h"
  8443. +
  8444. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  8445. +{
  8446. + unsigned int nlink;
  8447. +
  8448. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8449. +
  8450. + nlink = dir->i_nlink;
  8451. + nlink += h_dir->i_nlink - 2;
  8452. + if (h_dir->i_nlink < 2)
  8453. + nlink += 2;
  8454. + smp_mb(); /* for i_nlink */
  8455. + /* 0 can happen in revaliding */
  8456. + set_nlink(dir, nlink);
  8457. +}
  8458. +
  8459. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  8460. +{
  8461. + unsigned int nlink;
  8462. +
  8463. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8464. +
  8465. + nlink = dir->i_nlink;
  8466. + nlink -= h_dir->i_nlink - 2;
  8467. + if (h_dir->i_nlink < 2)
  8468. + nlink -= 2;
  8469. + smp_mb(); /* for i_nlink */
  8470. + /* nlink == 0 means the branch-fs is broken */
  8471. + set_nlink(dir, nlink);
  8472. +}
  8473. +
  8474. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  8475. +{
  8476. + loff_t sz;
  8477. + aufs_bindex_t bindex, bend;
  8478. + struct file *h_file;
  8479. + struct dentry *h_dentry;
  8480. +
  8481. + sz = 0;
  8482. + if (file) {
  8483. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  8484. +
  8485. + bend = au_fbend_dir(file);
  8486. + for (bindex = au_fbstart(file);
  8487. + bindex <= bend && sz < KMALLOC_MAX_SIZE;
  8488. + bindex++) {
  8489. + h_file = au_hf_dir(file, bindex);
  8490. + if (h_file && file_inode(h_file))
  8491. + sz += vfsub_f_size_read(h_file);
  8492. + }
  8493. + } else {
  8494. + AuDebugOn(!dentry);
  8495. + AuDebugOn(!d_is_dir(dentry));
  8496. +
  8497. + bend = au_dbtaildir(dentry);
  8498. + for (bindex = au_dbstart(dentry);
  8499. + bindex <= bend && sz < KMALLOC_MAX_SIZE;
  8500. + bindex++) {
  8501. + h_dentry = au_h_dptr(dentry, bindex);
  8502. + if (h_dentry && d_is_positive(h_dentry))
  8503. + sz += i_size_read(d_inode(h_dentry));
  8504. + }
  8505. + }
  8506. + if (sz < KMALLOC_MAX_SIZE)
  8507. + sz = roundup_pow_of_two(sz);
  8508. + if (sz > KMALLOC_MAX_SIZE)
  8509. + sz = KMALLOC_MAX_SIZE;
  8510. + else if (sz < NAME_MAX) {
  8511. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  8512. + sz = AUFS_RDBLK_DEF;
  8513. + }
  8514. + return sz;
  8515. +}
  8516. +
  8517. +struct au_dir_ts_arg {
  8518. + struct dentry *dentry;
  8519. + aufs_bindex_t brid;
  8520. +};
  8521. +
  8522. +static void au_do_dir_ts(void *arg)
  8523. +{
  8524. + struct au_dir_ts_arg *a = arg;
  8525. + struct au_dtime dt;
  8526. + struct path h_path;
  8527. + struct inode *dir, *h_dir;
  8528. + struct super_block *sb;
  8529. + struct au_branch *br;
  8530. + struct au_hinode *hdir;
  8531. + int err;
  8532. + aufs_bindex_t bstart, bindex;
  8533. +
  8534. + sb = a->dentry->d_sb;
  8535. + if (d_really_is_negative(a->dentry))
  8536. + goto out;
  8537. + aufs_read_lock(a->dentry, AuLock_DW | AuLock_DIR); /* noflush */
  8538. +
  8539. + /* no dir->i_mutex lock */
  8540. + dir = d_inode(a->dentry);
  8541. + bstart = au_ibstart(dir);
  8542. + bindex = au_br_index(sb, a->brid);
  8543. + if (bindex < bstart)
  8544. + goto out_unlock;
  8545. +
  8546. + br = au_sbr(sb, bindex);
  8547. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  8548. + if (!h_path.dentry)
  8549. + goto out_unlock;
  8550. + h_path.mnt = au_br_mnt(br);
  8551. + au_dtime_store(&dt, a->dentry, &h_path);
  8552. +
  8553. + br = au_sbr(sb, bstart);
  8554. + if (!au_br_writable(br->br_perm))
  8555. + goto out_unlock;
  8556. + h_path.dentry = au_h_dptr(a->dentry, bstart);
  8557. + h_path.mnt = au_br_mnt(br);
  8558. + err = vfsub_mnt_want_write(h_path.mnt);
  8559. + if (err)
  8560. + goto out_unlock;
  8561. + hdir = au_hi(dir, bstart);
  8562. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  8563. + h_dir = au_h_iptr(dir, bstart);
  8564. + if (h_dir->i_nlink
  8565. + && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  8566. + dt.dt_h_path = h_path;
  8567. + au_dtime_revert(&dt);
  8568. + }
  8569. + au_hn_imtx_unlock(hdir);
  8570. + vfsub_mnt_drop_write(h_path.mnt);
  8571. + au_cpup_attr_timesizes(dir);
  8572. +
  8573. +out_unlock:
  8574. + aufs_read_unlock(a->dentry, AuLock_DW);
  8575. +out:
  8576. + dput(a->dentry);
  8577. + au_nwt_done(&au_sbi(sb)->si_nowait);
  8578. + kfree(arg);
  8579. +}
  8580. +
  8581. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  8582. +{
  8583. + int perm, wkq_err;
  8584. + aufs_bindex_t bstart;
  8585. + struct au_dir_ts_arg *arg;
  8586. + struct dentry *dentry;
  8587. + struct super_block *sb;
  8588. +
  8589. + IMustLock(dir);
  8590. +
  8591. + dentry = d_find_any_alias(dir);
  8592. + AuDebugOn(!dentry);
  8593. + sb = dentry->d_sb;
  8594. + bstart = au_ibstart(dir);
  8595. + if (bstart == bindex) {
  8596. + au_cpup_attr_timesizes(dir);
  8597. + goto out;
  8598. + }
  8599. +
  8600. + perm = au_sbr_perm(sb, bstart);
  8601. + if (!au_br_writable(perm))
  8602. + goto out;
  8603. +
  8604. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  8605. + if (!arg)
  8606. + goto out;
  8607. +
  8608. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  8609. + arg->brid = au_sbr_id(sb, bindex);
  8610. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  8611. + if (unlikely(wkq_err)) {
  8612. + pr_err("wkq %d\n", wkq_err);
  8613. + dput(dentry);
  8614. + kfree(arg);
  8615. + }
  8616. +
  8617. +out:
  8618. + dput(dentry);
  8619. +}
  8620. +
  8621. +/* ---------------------------------------------------------------------- */
  8622. +
  8623. +static int reopen_dir(struct file *file)
  8624. +{
  8625. + int err;
  8626. + unsigned int flags;
  8627. + aufs_bindex_t bindex, btail, bstart;
  8628. + struct dentry *dentry, *h_dentry;
  8629. + struct file *h_file;
  8630. +
  8631. + /* open all lower dirs */
  8632. + dentry = file->f_path.dentry;
  8633. + bstart = au_dbstart(dentry);
  8634. + for (bindex = au_fbstart(file); bindex < bstart; bindex++)
  8635. + au_set_h_fptr(file, bindex, NULL);
  8636. + au_set_fbstart(file, bstart);
  8637. +
  8638. + btail = au_dbtaildir(dentry);
  8639. + for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
  8640. + au_set_h_fptr(file, bindex, NULL);
  8641. + au_set_fbend_dir(file, btail);
  8642. +
  8643. + flags = vfsub_file_flags(file);
  8644. + for (bindex = bstart; bindex <= btail; bindex++) {
  8645. + h_dentry = au_h_dptr(dentry, bindex);
  8646. + if (!h_dentry)
  8647. + continue;
  8648. + h_file = au_hf_dir(file, bindex);
  8649. + if (h_file)
  8650. + continue;
  8651. +
  8652. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8653. + err = PTR_ERR(h_file);
  8654. + if (IS_ERR(h_file))
  8655. + goto out; /* close all? */
  8656. + au_set_h_fptr(file, bindex, h_file);
  8657. + }
  8658. + au_update_figen(file);
  8659. + /* todo: necessary? */
  8660. + /* file->f_ra = h_file->f_ra; */
  8661. + err = 0;
  8662. +
  8663. +out:
  8664. + return err;
  8665. +}
  8666. +
  8667. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  8668. +{
  8669. + int err;
  8670. + aufs_bindex_t bindex, btail;
  8671. + struct dentry *dentry, *h_dentry;
  8672. +
  8673. + FiMustWriteLock(file);
  8674. + AuDebugOn(h_file);
  8675. +
  8676. + err = 0;
  8677. + dentry = file->f_path.dentry;
  8678. + file->f_version = d_inode(dentry)->i_version;
  8679. + bindex = au_dbstart(dentry);
  8680. + au_set_fbstart(file, bindex);
  8681. + btail = au_dbtaildir(dentry);
  8682. + au_set_fbend_dir(file, btail);
  8683. + for (; !err && bindex <= btail; bindex++) {
  8684. + h_dentry = au_h_dptr(dentry, bindex);
  8685. + if (!h_dentry)
  8686. + continue;
  8687. +
  8688. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  8689. + if (IS_ERR(h_file)) {
  8690. + err = PTR_ERR(h_file);
  8691. + break;
  8692. + }
  8693. + au_set_h_fptr(file, bindex, h_file);
  8694. + }
  8695. + au_update_figen(file);
  8696. + /* todo: necessary? */
  8697. + /* file->f_ra = h_file->f_ra; */
  8698. + if (!err)
  8699. + return 0; /* success */
  8700. +
  8701. + /* close all */
  8702. + for (bindex = au_fbstart(file); bindex <= btail; bindex++)
  8703. + au_set_h_fptr(file, bindex, NULL);
  8704. + au_set_fbstart(file, -1);
  8705. + au_set_fbend_dir(file, -1);
  8706. +
  8707. + return err;
  8708. +}
  8709. +
  8710. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  8711. + struct file *file)
  8712. +{
  8713. + int err;
  8714. + struct super_block *sb;
  8715. + struct au_fidir *fidir;
  8716. +
  8717. + err = -ENOMEM;
  8718. + sb = file->f_path.dentry->d_sb;
  8719. + si_read_lock(sb, AuLock_FLUSH);
  8720. + fidir = au_fidir_alloc(sb);
  8721. + if (fidir) {
  8722. + struct au_do_open_args args = {
  8723. + .open = do_open_dir,
  8724. + .fidir = fidir
  8725. + };
  8726. + err = au_do_open(file, &args);
  8727. + if (unlikely(err))
  8728. + kfree(fidir);
  8729. + }
  8730. + si_read_unlock(sb);
  8731. + return err;
  8732. +}
  8733. +
  8734. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  8735. + struct file *file)
  8736. +{
  8737. + struct au_vdir *vdir_cache;
  8738. + struct au_finfo *finfo;
  8739. + struct au_fidir *fidir;
  8740. + aufs_bindex_t bindex, bend;
  8741. +
  8742. + finfo = au_fi(file);
  8743. + fidir = finfo->fi_hdir;
  8744. + if (fidir) {
  8745. + au_sphl_del(&finfo->fi_hlist,
  8746. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  8747. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  8748. + if (vdir_cache)
  8749. + au_vdir_free(vdir_cache);
  8750. +
  8751. + bindex = finfo->fi_btop;
  8752. + if (bindex >= 0) {
  8753. + /*
  8754. + * calls fput() instead of filp_close(),
  8755. + * since no dnotify or lock for the lower file.
  8756. + */
  8757. + bend = fidir->fd_bbot;
  8758. + for (; bindex <= bend; bindex++)
  8759. + au_set_h_fptr(file, bindex, NULL);
  8760. + }
  8761. + kfree(fidir);
  8762. + finfo->fi_hdir = NULL;
  8763. + }
  8764. + au_finfo_fin(file);
  8765. + return 0;
  8766. +}
  8767. +
  8768. +/* ---------------------------------------------------------------------- */
  8769. +
  8770. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  8771. +{
  8772. + int err;
  8773. + aufs_bindex_t bindex, bend;
  8774. + struct file *h_file;
  8775. +
  8776. + err = 0;
  8777. + bend = au_fbend_dir(file);
  8778. + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
  8779. + h_file = au_hf_dir(file, bindex);
  8780. + if (h_file)
  8781. + err = vfsub_flush(h_file, id);
  8782. + }
  8783. + return err;
  8784. +}
  8785. +
  8786. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  8787. +{
  8788. + return au_do_flush(file, id, au_do_flush_dir);
  8789. +}
  8790. +
  8791. +/* ---------------------------------------------------------------------- */
  8792. +
  8793. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  8794. +{
  8795. + int err;
  8796. + aufs_bindex_t bend, bindex;
  8797. + struct inode *inode;
  8798. + struct super_block *sb;
  8799. +
  8800. + err = 0;
  8801. + sb = dentry->d_sb;
  8802. + inode = d_inode(dentry);
  8803. + IMustLock(inode);
  8804. + bend = au_dbend(dentry);
  8805. + for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
  8806. + struct path h_path;
  8807. +
  8808. + if (au_test_ro(sb, bindex, inode))
  8809. + continue;
  8810. + h_path.dentry = au_h_dptr(dentry, bindex);
  8811. + if (!h_path.dentry)
  8812. + continue;
  8813. +
  8814. + h_path.mnt = au_sbr_mnt(sb, bindex);
  8815. + err = vfsub_fsync(NULL, &h_path, datasync);
  8816. + }
  8817. +
  8818. + return err;
  8819. +}
  8820. +
  8821. +static int au_do_fsync_dir(struct file *file, int datasync)
  8822. +{
  8823. + int err;
  8824. + aufs_bindex_t bend, bindex;
  8825. + struct file *h_file;
  8826. + struct super_block *sb;
  8827. + struct inode *inode;
  8828. +
  8829. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  8830. + if (unlikely(err))
  8831. + goto out;
  8832. +
  8833. + inode = file_inode(file);
  8834. + sb = inode->i_sb;
  8835. + bend = au_fbend_dir(file);
  8836. + for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
  8837. + h_file = au_hf_dir(file, bindex);
  8838. + if (!h_file || au_test_ro(sb, bindex, inode))
  8839. + continue;
  8840. +
  8841. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  8842. + }
  8843. +
  8844. +out:
  8845. + return err;
  8846. +}
  8847. +
  8848. +/*
  8849. + * @file may be NULL
  8850. + */
  8851. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  8852. + int datasync)
  8853. +{
  8854. + int err;
  8855. + struct dentry *dentry;
  8856. + struct inode *inode;
  8857. + struct super_block *sb;
  8858. + struct mutex *mtx;
  8859. +
  8860. + err = 0;
  8861. + dentry = file->f_path.dentry;
  8862. + inode = d_inode(dentry);
  8863. + mtx = &inode->i_mutex;
  8864. + mutex_lock(mtx);
  8865. + sb = dentry->d_sb;
  8866. + si_noflush_read_lock(sb);
  8867. + if (file)
  8868. + err = au_do_fsync_dir(file, datasync);
  8869. + else {
  8870. + di_write_lock_child(dentry);
  8871. + err = au_do_fsync_dir_no_file(dentry, datasync);
  8872. + }
  8873. + au_cpup_attr_timesizes(inode);
  8874. + di_write_unlock(dentry);
  8875. + if (file)
  8876. + fi_write_unlock(file);
  8877. +
  8878. + si_read_unlock(sb);
  8879. + mutex_unlock(mtx);
  8880. + return err;
  8881. +}
  8882. +
  8883. +/* ---------------------------------------------------------------------- */
  8884. +
  8885. +static int aufs_iterate(struct file *file, struct dir_context *ctx)
  8886. +{
  8887. + int err;
  8888. + struct dentry *dentry;
  8889. + struct inode *inode, *h_inode;
  8890. + struct super_block *sb;
  8891. +
  8892. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  8893. +
  8894. + dentry = file->f_path.dentry;
  8895. + inode = d_inode(dentry);
  8896. + IMustLock(inode);
  8897. +
  8898. + sb = dentry->d_sb;
  8899. + si_read_lock(sb, AuLock_FLUSH);
  8900. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
  8901. + if (unlikely(err))
  8902. + goto out;
  8903. + err = au_alive_dir(dentry);
  8904. + if (!err)
  8905. + err = au_vdir_init(file);
  8906. + di_downgrade_lock(dentry, AuLock_IR);
  8907. + if (unlikely(err))
  8908. + goto out_unlock;
  8909. +
  8910. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  8911. + if (!au_test_nfsd()) {
  8912. + err = au_vdir_fill_de(file, ctx);
  8913. + fsstack_copy_attr_atime(inode, h_inode);
  8914. + } else {
  8915. + /*
  8916. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  8917. + * encode_fh() and others.
  8918. + */
  8919. + atomic_inc(&h_inode->i_count);
  8920. + di_read_unlock(dentry, AuLock_IR);
  8921. + si_read_unlock(sb);
  8922. + err = au_vdir_fill_de(file, ctx);
  8923. + fsstack_copy_attr_atime(inode, h_inode);
  8924. + fi_write_unlock(file);
  8925. + iput(h_inode);
  8926. +
  8927. + AuTraceErr(err);
  8928. + return err;
  8929. + }
  8930. +
  8931. +out_unlock:
  8932. + di_read_unlock(dentry, AuLock_IR);
  8933. + fi_write_unlock(file);
  8934. +out:
  8935. + si_read_unlock(sb);
  8936. + return err;
  8937. +}
  8938. +
  8939. +/* ---------------------------------------------------------------------- */
  8940. +
  8941. +#define AuTestEmpty_WHONLY 1
  8942. +#define AuTestEmpty_CALLED (1 << 1)
  8943. +#define AuTestEmpty_SHWH (1 << 2)
  8944. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  8945. +#define au_fset_testempty(flags, name) \
  8946. + do { (flags) |= AuTestEmpty_##name; } while (0)
  8947. +#define au_fclr_testempty(flags, name) \
  8948. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  8949. +
  8950. +#ifndef CONFIG_AUFS_SHWH
  8951. +#undef AuTestEmpty_SHWH
  8952. +#define AuTestEmpty_SHWH 0
  8953. +#endif
  8954. +
  8955. +struct test_empty_arg {
  8956. + struct dir_context ctx;
  8957. + struct au_nhash *whlist;
  8958. + unsigned int flags;
  8959. + int err;
  8960. + aufs_bindex_t bindex;
  8961. +};
  8962. +
  8963. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  8964. + int namelen, loff_t offset __maybe_unused, u64 ino,
  8965. + unsigned int d_type)
  8966. +{
  8967. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  8968. + ctx);
  8969. + char *name = (void *)__name;
  8970. +
  8971. + arg->err = 0;
  8972. + au_fset_testempty(arg->flags, CALLED);
  8973. + /* smp_mb(); */
  8974. + if (name[0] == '.'
  8975. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  8976. + goto out; /* success */
  8977. +
  8978. + if (namelen <= AUFS_WH_PFX_LEN
  8979. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  8980. + if (au_ftest_testempty(arg->flags, WHONLY)
  8981. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  8982. + arg->err = -ENOTEMPTY;
  8983. + goto out;
  8984. + }
  8985. +
  8986. + name += AUFS_WH_PFX_LEN;
  8987. + namelen -= AUFS_WH_PFX_LEN;
  8988. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  8989. + arg->err = au_nhash_append_wh
  8990. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  8991. + au_ftest_testempty(arg->flags, SHWH));
  8992. +
  8993. +out:
  8994. + /* smp_mb(); */
  8995. + AuTraceErr(arg->err);
  8996. + return arg->err;
  8997. +}
  8998. +
  8999. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9000. +{
  9001. + int err;
  9002. + struct file *h_file;
  9003. +
  9004. + h_file = au_h_open(dentry, arg->bindex,
  9005. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9006. + /*file*/NULL, /*force_wr*/0);
  9007. + err = PTR_ERR(h_file);
  9008. + if (IS_ERR(h_file))
  9009. + goto out;
  9010. +
  9011. + err = 0;
  9012. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9013. + && !file_inode(h_file)->i_nlink)
  9014. + goto out_put;
  9015. +
  9016. + do {
  9017. + arg->err = 0;
  9018. + au_fclr_testempty(arg->flags, CALLED);
  9019. + /* smp_mb(); */
  9020. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9021. + if (err >= 0)
  9022. + err = arg->err;
  9023. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9024. +
  9025. +out_put:
  9026. + fput(h_file);
  9027. + au_sbr_put(dentry->d_sb, arg->bindex);
  9028. +out:
  9029. + return err;
  9030. +}
  9031. +
  9032. +struct do_test_empty_args {
  9033. + int *errp;
  9034. + struct dentry *dentry;
  9035. + struct test_empty_arg *arg;
  9036. +};
  9037. +
  9038. +static void call_do_test_empty(void *args)
  9039. +{
  9040. + struct do_test_empty_args *a = args;
  9041. + *a->errp = do_test_empty(a->dentry, a->arg);
  9042. +}
  9043. +
  9044. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9045. +{
  9046. + int err, wkq_err;
  9047. + struct dentry *h_dentry;
  9048. + struct inode *h_inode;
  9049. +
  9050. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9051. + h_inode = d_inode(h_dentry);
  9052. + /* todo: i_mode changes anytime? */
  9053. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  9054. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  9055. + mutex_unlock(&h_inode->i_mutex);
  9056. + if (!err)
  9057. + err = do_test_empty(dentry, arg);
  9058. + else {
  9059. + struct do_test_empty_args args = {
  9060. + .errp = &err,
  9061. + .dentry = dentry,
  9062. + .arg = arg
  9063. + };
  9064. + unsigned int flags = arg->flags;
  9065. +
  9066. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9067. + if (unlikely(wkq_err))
  9068. + err = wkq_err;
  9069. + arg->flags = flags;
  9070. + }
  9071. +
  9072. + return err;
  9073. +}
  9074. +
  9075. +int au_test_empty_lower(struct dentry *dentry)
  9076. +{
  9077. + int err;
  9078. + unsigned int rdhash;
  9079. + aufs_bindex_t bindex, bstart, btail;
  9080. + struct au_nhash whlist;
  9081. + struct test_empty_arg arg = {
  9082. + .ctx = {
  9083. + .actor = test_empty_cb
  9084. + }
  9085. + };
  9086. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9087. +
  9088. + SiMustAnyLock(dentry->d_sb);
  9089. +
  9090. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9091. + if (!rdhash)
  9092. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9093. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9094. + if (unlikely(err))
  9095. + goto out;
  9096. +
  9097. + arg.flags = 0;
  9098. + arg.whlist = &whlist;
  9099. + bstart = au_dbstart(dentry);
  9100. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9101. + au_fset_testempty(arg.flags, SHWH);
  9102. + test_empty = do_test_empty;
  9103. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9104. + test_empty = sio_test_empty;
  9105. + arg.bindex = bstart;
  9106. + err = test_empty(dentry, &arg);
  9107. + if (unlikely(err))
  9108. + goto out_whlist;
  9109. +
  9110. + au_fset_testempty(arg.flags, WHONLY);
  9111. + btail = au_dbtaildir(dentry);
  9112. + for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
  9113. + struct dentry *h_dentry;
  9114. +
  9115. + h_dentry = au_h_dptr(dentry, bindex);
  9116. + if (h_dentry && d_is_positive(h_dentry)) {
  9117. + arg.bindex = bindex;
  9118. + err = test_empty(dentry, &arg);
  9119. + }
  9120. + }
  9121. +
  9122. +out_whlist:
  9123. + au_nhash_wh_free(&whlist);
  9124. +out:
  9125. + return err;
  9126. +}
  9127. +
  9128. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9129. +{
  9130. + int err;
  9131. + struct test_empty_arg arg = {
  9132. + .ctx = {
  9133. + .actor = test_empty_cb
  9134. + }
  9135. + };
  9136. + aufs_bindex_t bindex, btail;
  9137. +
  9138. + err = 0;
  9139. + arg.whlist = whlist;
  9140. + arg.flags = AuTestEmpty_WHONLY;
  9141. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9142. + au_fset_testempty(arg.flags, SHWH);
  9143. + btail = au_dbtaildir(dentry);
  9144. + for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
  9145. + struct dentry *h_dentry;
  9146. +
  9147. + h_dentry = au_h_dptr(dentry, bindex);
  9148. + if (h_dentry && d_is_positive(h_dentry)) {
  9149. + arg.bindex = bindex;
  9150. + err = sio_test_empty(dentry, &arg);
  9151. + }
  9152. + }
  9153. +
  9154. + return err;
  9155. +}
  9156. +
  9157. +/* ---------------------------------------------------------------------- */
  9158. +
  9159. +const struct file_operations aufs_dir_fop = {
  9160. + .owner = THIS_MODULE,
  9161. + .llseek = default_llseek,
  9162. + .read = generic_read_dir,
  9163. + .iterate = aufs_iterate,
  9164. + .unlocked_ioctl = aufs_ioctl_dir,
  9165. +#ifdef CONFIG_COMPAT
  9166. + .compat_ioctl = aufs_compat_ioctl_dir,
  9167. +#endif
  9168. + .open = aufs_open_dir,
  9169. + .release = aufs_release_dir,
  9170. + .flush = aufs_flush_dir,
  9171. + .fsync = aufs_fsync_dir
  9172. +};
  9173. diff -Nur linux-4.1.10.orig/fs/aufs/dir.h linux-4.1.10/fs/aufs/dir.h
  9174. --- linux-4.1.10.orig/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
  9175. +++ linux-4.1.10/fs/aufs/dir.h 2015-10-22 21:35:53.000000000 +0200
  9176. @@ -0,0 +1,131 @@
  9177. +/*
  9178. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  9179. + *
  9180. + * This program, aufs is free software; you can redistribute it and/or modify
  9181. + * it under the terms of the GNU General Public License as published by
  9182. + * the Free Software Foundation; either version 2 of the License, or
  9183. + * (at your option) any later version.
  9184. + *
  9185. + * This program is distributed in the hope that it will be useful,
  9186. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9187. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9188. + * GNU General Public License for more details.
  9189. + *
  9190. + * You should have received a copy of the GNU General Public License
  9191. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9192. + */
  9193. +
  9194. +/*
  9195. + * directory operations
  9196. + */
  9197. +
  9198. +#ifndef __AUFS_DIR_H__
  9199. +#define __AUFS_DIR_H__
  9200. +
  9201. +#ifdef __KERNEL__
  9202. +
  9203. +#include <linux/fs.h>
  9204. +
  9205. +/* ---------------------------------------------------------------------- */
  9206. +
  9207. +/* need to be faster and smaller */
  9208. +
  9209. +struct au_nhash {
  9210. + unsigned int nh_num;
  9211. + struct hlist_head *nh_head;
  9212. +};
  9213. +
  9214. +struct au_vdir_destr {
  9215. + unsigned char len;
  9216. + unsigned char name[0];
  9217. +} __packed;
  9218. +
  9219. +struct au_vdir_dehstr {
  9220. + struct hlist_node hash;
  9221. + struct au_vdir_destr *str;
  9222. +} ____cacheline_aligned_in_smp;
  9223. +
  9224. +struct au_vdir_de {
  9225. + ino_t de_ino;
  9226. + unsigned char de_type;
  9227. + /* caution: packed */
  9228. + struct au_vdir_destr de_str;
  9229. +} __packed;
  9230. +
  9231. +struct au_vdir_wh {
  9232. + struct hlist_node wh_hash;
  9233. +#ifdef CONFIG_AUFS_SHWH
  9234. + ino_t wh_ino;
  9235. + aufs_bindex_t wh_bindex;
  9236. + unsigned char wh_type;
  9237. +#else
  9238. + aufs_bindex_t wh_bindex;
  9239. +#endif
  9240. + /* caution: packed */
  9241. + struct au_vdir_destr wh_str;
  9242. +} __packed;
  9243. +
  9244. +union au_vdir_deblk_p {
  9245. + unsigned char *deblk;
  9246. + struct au_vdir_de *de;
  9247. +};
  9248. +
  9249. +struct au_vdir {
  9250. + unsigned char **vd_deblk;
  9251. + unsigned long vd_nblk;
  9252. + struct {
  9253. + unsigned long ul;
  9254. + union au_vdir_deblk_p p;
  9255. + } vd_last;
  9256. +
  9257. + unsigned long vd_version;
  9258. + unsigned int vd_deblk_sz;
  9259. + unsigned long vd_jiffy;
  9260. +} ____cacheline_aligned_in_smp;
  9261. +
  9262. +/* ---------------------------------------------------------------------- */
  9263. +
  9264. +/* dir.c */
  9265. +extern const struct file_operations aufs_dir_fop;
  9266. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9267. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9268. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9269. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9270. +int au_test_empty_lower(struct dentry *dentry);
  9271. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9272. +
  9273. +/* vdir.c */
  9274. +unsigned int au_rdhash_est(loff_t sz);
  9275. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9276. +void au_nhash_wh_free(struct au_nhash *whlist);
  9277. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9278. + int limit);
  9279. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9280. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9281. + unsigned int d_type, aufs_bindex_t bindex,
  9282. + unsigned char shwh);
  9283. +void au_vdir_free(struct au_vdir *vdir);
  9284. +int au_vdir_init(struct file *file);
  9285. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9286. +
  9287. +/* ioctl.c */
  9288. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9289. +
  9290. +#ifdef CONFIG_AUFS_RDU
  9291. +/* rdu.c */
  9292. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9293. +#ifdef CONFIG_COMPAT
  9294. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9295. + unsigned long arg);
  9296. +#endif
  9297. +#else
  9298. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9299. + unsigned int cmd, unsigned long arg)
  9300. +#ifdef CONFIG_COMPAT
  9301. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9302. + unsigned int cmd, unsigned long arg)
  9303. +#endif
  9304. +#endif
  9305. +
  9306. +#endif /* __KERNEL__ */
  9307. +#endif /* __AUFS_DIR_H__ */
  9308. diff -Nur linux-4.1.10.orig/fs/aufs/dynop.c linux-4.1.10/fs/aufs/dynop.c
  9309. --- linux-4.1.10.orig/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
  9310. +++ linux-4.1.10/fs/aufs/dynop.c 2015-10-22 21:35:53.000000000 +0200
  9311. @@ -0,0 +1,369 @@
  9312. +/*
  9313. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  9314. + *
  9315. + * This program, aufs is free software; you can redistribute it and/or modify
  9316. + * it under the terms of the GNU General Public License as published by
  9317. + * the Free Software Foundation; either version 2 of the License, or
  9318. + * (at your option) any later version.
  9319. + *
  9320. + * This program is distributed in the hope that it will be useful,
  9321. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9322. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9323. + * GNU General Public License for more details.
  9324. + *
  9325. + * You should have received a copy of the GNU General Public License
  9326. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9327. + */
  9328. +
  9329. +/*
  9330. + * dynamically customizable operations for regular files
  9331. + */
  9332. +
  9333. +#include "aufs.h"
  9334. +
  9335. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  9336. +
  9337. +/*
  9338. + * How large will these lists be?
  9339. + * Usually just a few elements, 20-30 at most for each, I guess.
  9340. + */
  9341. +static struct au_splhead dynop[AuDyLast];
  9342. +
  9343. +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
  9344. +{
  9345. + struct au_dykey *key, *tmp;
  9346. + struct list_head *head;
  9347. +
  9348. + key = NULL;
  9349. + head = &spl->head;
  9350. + rcu_read_lock();
  9351. + list_for_each_entry_rcu(tmp, head, dk_list)
  9352. + if (tmp->dk_op.dy_hop == h_op) {
  9353. + key = tmp;
  9354. + kref_get(&key->dk_kref);
  9355. + break;
  9356. + }
  9357. + rcu_read_unlock();
  9358. +
  9359. + return key;
  9360. +}
  9361. +
  9362. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  9363. +{
  9364. + struct au_dykey **k, *found;
  9365. + const void *h_op = key->dk_op.dy_hop;
  9366. + int i;
  9367. +
  9368. + found = NULL;
  9369. + k = br->br_dykey;
  9370. + for (i = 0; i < AuBrDynOp; i++)
  9371. + if (k[i]) {
  9372. + if (k[i]->dk_op.dy_hop == h_op) {
  9373. + found = k[i];
  9374. + break;
  9375. + }
  9376. + } else
  9377. + break;
  9378. + if (!found) {
  9379. + spin_lock(&br->br_dykey_lock);
  9380. + for (; i < AuBrDynOp; i++)
  9381. + if (k[i]) {
  9382. + if (k[i]->dk_op.dy_hop == h_op) {
  9383. + found = k[i];
  9384. + break;
  9385. + }
  9386. + } else {
  9387. + k[i] = key;
  9388. + break;
  9389. + }
  9390. + spin_unlock(&br->br_dykey_lock);
  9391. + BUG_ON(i == AuBrDynOp); /* expand the array */
  9392. + }
  9393. +
  9394. + return found;
  9395. +}
  9396. +
  9397. +/* kref_get() if @key is already added */
  9398. +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
  9399. +{
  9400. + struct au_dykey *tmp, *found;
  9401. + struct list_head *head;
  9402. + const void *h_op = key->dk_op.dy_hop;
  9403. +
  9404. + found = NULL;
  9405. + head = &spl->head;
  9406. + spin_lock(&spl->spin);
  9407. + list_for_each_entry(tmp, head, dk_list)
  9408. + if (tmp->dk_op.dy_hop == h_op) {
  9409. + kref_get(&tmp->dk_kref);
  9410. + found = tmp;
  9411. + break;
  9412. + }
  9413. + if (!found)
  9414. + list_add_rcu(&key->dk_list, head);
  9415. + spin_unlock(&spl->spin);
  9416. +
  9417. + if (!found)
  9418. + DyPrSym(key);
  9419. + return found;
  9420. +}
  9421. +
  9422. +static void dy_free_rcu(struct rcu_head *rcu)
  9423. +{
  9424. + struct au_dykey *key;
  9425. +
  9426. + key = container_of(rcu, struct au_dykey, dk_rcu);
  9427. + DyPrSym(key);
  9428. + kfree(key);
  9429. +}
  9430. +
  9431. +static void dy_free(struct kref *kref)
  9432. +{
  9433. + struct au_dykey *key;
  9434. + struct au_splhead *spl;
  9435. +
  9436. + key = container_of(kref, struct au_dykey, dk_kref);
  9437. + spl = dynop + key->dk_op.dy_type;
  9438. + au_spl_del_rcu(&key->dk_list, spl);
  9439. + call_rcu(&key->dk_rcu, dy_free_rcu);
  9440. +}
  9441. +
  9442. +void au_dy_put(struct au_dykey *key)
  9443. +{
  9444. + kref_put(&key->dk_kref, dy_free);
  9445. +}
  9446. +
  9447. +/* ---------------------------------------------------------------------- */
  9448. +
  9449. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  9450. +
  9451. +#ifdef CONFIG_AUFS_DEBUG
  9452. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  9453. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  9454. +#else
  9455. +#define DyDbgDeclare(cnt) do {} while (0)
  9456. +#define DyDbgInc(cnt) do {} while (0)
  9457. +#endif
  9458. +
  9459. +#define DySet(func, dst, src, h_op, h_sb) do { \
  9460. + DyDbgInc(cnt); \
  9461. + if (h_op->func) { \
  9462. + if (src.func) \
  9463. + dst.func = src.func; \
  9464. + else \
  9465. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  9466. + } \
  9467. +} while (0)
  9468. +
  9469. +#define DySetForce(func, dst, src) do { \
  9470. + AuDebugOn(!src.func); \
  9471. + DyDbgInc(cnt); \
  9472. + dst.func = src.func; \
  9473. +} while (0)
  9474. +
  9475. +#define DySetAop(func) \
  9476. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  9477. +#define DySetAopForce(func) \
  9478. + DySetForce(func, dyaop->da_op, aufs_aop)
  9479. +
  9480. +static void dy_aop(struct au_dykey *key, const void *h_op,
  9481. + struct super_block *h_sb __maybe_unused)
  9482. +{
  9483. + struct au_dyaop *dyaop = (void *)key;
  9484. + const struct address_space_operations *h_aop = h_op;
  9485. + DyDbgDeclare(cnt);
  9486. +
  9487. + AuDbg("%s\n", au_sbtype(h_sb));
  9488. +
  9489. + DySetAop(writepage);
  9490. + DySetAopForce(readpage); /* force */
  9491. + DySetAop(writepages);
  9492. + DySetAop(set_page_dirty);
  9493. + DySetAop(readpages);
  9494. + DySetAop(write_begin);
  9495. + DySetAop(write_end);
  9496. + DySetAop(bmap);
  9497. + DySetAop(invalidatepage);
  9498. + DySetAop(releasepage);
  9499. + DySetAop(freepage);
  9500. + /* this one will be changed according to an aufs mount option */
  9501. + DySetAop(direct_IO);
  9502. + DySetAop(migratepage);
  9503. + DySetAop(launder_page);
  9504. + DySetAop(is_partially_uptodate);
  9505. + DySetAop(is_dirty_writeback);
  9506. + DySetAop(error_remove_page);
  9507. + DySetAop(swap_activate);
  9508. + DySetAop(swap_deactivate);
  9509. +
  9510. + DyDbgSize(cnt, *h_aop);
  9511. +}
  9512. +
  9513. +/* ---------------------------------------------------------------------- */
  9514. +
  9515. +static void dy_bug(struct kref *kref)
  9516. +{
  9517. + BUG();
  9518. +}
  9519. +
  9520. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  9521. +{
  9522. + struct au_dykey *key, *old;
  9523. + struct au_splhead *spl;
  9524. + struct op {
  9525. + unsigned int sz;
  9526. + void (*set)(struct au_dykey *key, const void *h_op,
  9527. + struct super_block *h_sb __maybe_unused);
  9528. + };
  9529. + static const struct op a[] = {
  9530. + [AuDy_AOP] = {
  9531. + .sz = sizeof(struct au_dyaop),
  9532. + .set = dy_aop
  9533. + }
  9534. + };
  9535. + const struct op *p;
  9536. +
  9537. + spl = dynop + op->dy_type;
  9538. + key = dy_gfind_get(spl, op->dy_hop);
  9539. + if (key)
  9540. + goto out_add; /* success */
  9541. +
  9542. + p = a + op->dy_type;
  9543. + key = kzalloc(p->sz, GFP_NOFS);
  9544. + if (unlikely(!key)) {
  9545. + key = ERR_PTR(-ENOMEM);
  9546. + goto out;
  9547. + }
  9548. +
  9549. + key->dk_op.dy_hop = op->dy_hop;
  9550. + kref_init(&key->dk_kref);
  9551. + p->set(key, op->dy_hop, au_br_sb(br));
  9552. + old = dy_gadd(spl, key);
  9553. + if (old) {
  9554. + kfree(key);
  9555. + key = old;
  9556. + }
  9557. +
  9558. +out_add:
  9559. + old = dy_bradd(br, key);
  9560. + if (old)
  9561. + /* its ref-count should never be zero here */
  9562. + kref_put(&key->dk_kref, dy_bug);
  9563. +out:
  9564. + return key;
  9565. +}
  9566. +
  9567. +/* ---------------------------------------------------------------------- */
  9568. +/*
  9569. + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
  9570. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  9571. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  9572. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  9573. + * See the aufs manual in detail.
  9574. + */
  9575. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  9576. +{
  9577. + if (!do_dx)
  9578. + dyaop->da_op.direct_IO = NULL;
  9579. + else
  9580. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  9581. +}
  9582. +
  9583. +static struct au_dyaop *dy_aget(struct au_branch *br,
  9584. + const struct address_space_operations *h_aop,
  9585. + int do_dx)
  9586. +{
  9587. + struct au_dyaop *dyaop;
  9588. + struct au_dynop op;
  9589. +
  9590. + op.dy_type = AuDy_AOP;
  9591. + op.dy_haop = h_aop;
  9592. + dyaop = (void *)dy_get(&op, br);
  9593. + if (IS_ERR(dyaop))
  9594. + goto out;
  9595. + dy_adx(dyaop, do_dx);
  9596. +
  9597. +out:
  9598. + return dyaop;
  9599. +}
  9600. +
  9601. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9602. + struct inode *h_inode)
  9603. +{
  9604. + int err, do_dx;
  9605. + struct super_block *sb;
  9606. + struct au_branch *br;
  9607. + struct au_dyaop *dyaop;
  9608. +
  9609. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  9610. + IiMustWriteLock(inode);
  9611. +
  9612. + sb = inode->i_sb;
  9613. + br = au_sbr(sb, bindex);
  9614. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  9615. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  9616. + err = PTR_ERR(dyaop);
  9617. + if (IS_ERR(dyaop))
  9618. + /* unnecessary to call dy_fput() */
  9619. + goto out;
  9620. +
  9621. + err = 0;
  9622. + inode->i_mapping->a_ops = &dyaop->da_op;
  9623. +
  9624. +out:
  9625. + return err;
  9626. +}
  9627. +
  9628. +/*
  9629. + * Is it safe to replace a_ops during the inode/file is in operation?
  9630. + * Yes, I hope so.
  9631. + */
  9632. +int au_dy_irefresh(struct inode *inode)
  9633. +{
  9634. + int err;
  9635. + aufs_bindex_t bstart;
  9636. + struct inode *h_inode;
  9637. +
  9638. + err = 0;
  9639. + if (S_ISREG(inode->i_mode)) {
  9640. + bstart = au_ibstart(inode);
  9641. + h_inode = au_h_iptr(inode, bstart);
  9642. + err = au_dy_iaop(inode, bstart, h_inode);
  9643. + }
  9644. + return err;
  9645. +}
  9646. +
  9647. +void au_dy_arefresh(int do_dx)
  9648. +{
  9649. + struct au_splhead *spl;
  9650. + struct list_head *head;
  9651. + struct au_dykey *key;
  9652. +
  9653. + spl = dynop + AuDy_AOP;
  9654. + head = &spl->head;
  9655. + spin_lock(&spl->spin);
  9656. + list_for_each_entry(key, head, dk_list)
  9657. + dy_adx((void *)key, do_dx);
  9658. + spin_unlock(&spl->spin);
  9659. +}
  9660. +
  9661. +/* ---------------------------------------------------------------------- */
  9662. +
  9663. +void __init au_dy_init(void)
  9664. +{
  9665. + int i;
  9666. +
  9667. + /* make sure that 'struct au_dykey *' can be any type */
  9668. + BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
  9669. +
  9670. + for (i = 0; i < AuDyLast; i++)
  9671. + au_spl_init(dynop + i);
  9672. +}
  9673. +
  9674. +void au_dy_fin(void)
  9675. +{
  9676. + int i;
  9677. +
  9678. + for (i = 0; i < AuDyLast; i++)
  9679. + WARN_ON(!list_empty(&dynop[i].head));
  9680. +}
  9681. diff -Nur linux-4.1.10.orig/fs/aufs/dynop.h linux-4.1.10/fs/aufs/dynop.h
  9682. --- linux-4.1.10.orig/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
  9683. +++ linux-4.1.10/fs/aufs/dynop.h 2015-10-22 21:35:53.000000000 +0200
  9684. @@ -0,0 +1,74 @@
  9685. +/*
  9686. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  9687. + *
  9688. + * This program, aufs is free software; you can redistribute it and/or modify
  9689. + * it under the terms of the GNU General Public License as published by
  9690. + * the Free Software Foundation; either version 2 of the License, or
  9691. + * (at your option) any later version.
  9692. + *
  9693. + * This program is distributed in the hope that it will be useful,
  9694. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9695. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9696. + * GNU General Public License for more details.
  9697. + *
  9698. + * You should have received a copy of the GNU General Public License
  9699. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9700. + */
  9701. +
  9702. +/*
  9703. + * dynamically customizable operations (for regular files only)
  9704. + */
  9705. +
  9706. +#ifndef __AUFS_DYNOP_H__
  9707. +#define __AUFS_DYNOP_H__
  9708. +
  9709. +#ifdef __KERNEL__
  9710. +
  9711. +#include <linux/fs.h>
  9712. +#include <linux/kref.h>
  9713. +
  9714. +enum {AuDy_AOP, AuDyLast};
  9715. +
  9716. +struct au_dynop {
  9717. + int dy_type;
  9718. + union {
  9719. + const void *dy_hop;
  9720. + const struct address_space_operations *dy_haop;
  9721. + };
  9722. +};
  9723. +
  9724. +struct au_dykey {
  9725. + union {
  9726. + struct list_head dk_list;
  9727. + struct rcu_head dk_rcu;
  9728. + };
  9729. + struct au_dynop dk_op;
  9730. +
  9731. + /*
  9732. + * during I am in the branch local array, kref is gotten. when the
  9733. + * branch is removed, kref is put.
  9734. + */
  9735. + struct kref dk_kref;
  9736. +};
  9737. +
  9738. +/* stop unioning since their sizes are very different from each other */
  9739. +struct au_dyaop {
  9740. + struct au_dykey da_key;
  9741. + struct address_space_operations da_op; /* not const */
  9742. +};
  9743. +
  9744. +/* ---------------------------------------------------------------------- */
  9745. +
  9746. +/* dynop.c */
  9747. +struct au_branch;
  9748. +void au_dy_put(struct au_dykey *key);
  9749. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  9750. + struct inode *h_inode);
  9751. +int au_dy_irefresh(struct inode *inode);
  9752. +void au_dy_arefresh(int do_dio);
  9753. +
  9754. +void __init au_dy_init(void);
  9755. +void au_dy_fin(void);
  9756. +
  9757. +#endif /* __KERNEL__ */
  9758. +#endif /* __AUFS_DYNOP_H__ */
  9759. diff -Nur linux-4.1.10.orig/fs/aufs/export.c linux-4.1.10/fs/aufs/export.c
  9760. --- linux-4.1.10.orig/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
  9761. +++ linux-4.1.10/fs/aufs/export.c 2015-10-22 21:35:53.000000000 +0200
  9762. @@ -0,0 +1,832 @@
  9763. +/*
  9764. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  9765. + *
  9766. + * This program, aufs is free software; you can redistribute it and/or modify
  9767. + * it under the terms of the GNU General Public License as published by
  9768. + * the Free Software Foundation; either version 2 of the License, or
  9769. + * (at your option) any later version.
  9770. + *
  9771. + * This program is distributed in the hope that it will be useful,
  9772. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9773. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9774. + * GNU General Public License for more details.
  9775. + *
  9776. + * You should have received a copy of the GNU General Public License
  9777. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9778. + */
  9779. +
  9780. +/*
  9781. + * export via nfs
  9782. + */
  9783. +
  9784. +#include <linux/exportfs.h>
  9785. +#include <linux/fs_struct.h>
  9786. +#include <linux/namei.h>
  9787. +#include <linux/nsproxy.h>
  9788. +#include <linux/random.h>
  9789. +#include <linux/writeback.h>
  9790. +#include "../fs/mount.h"
  9791. +#include "aufs.h"
  9792. +
  9793. +union conv {
  9794. +#ifdef CONFIG_AUFS_INO_T_64
  9795. + __u32 a[2];
  9796. +#else
  9797. + __u32 a[1];
  9798. +#endif
  9799. + ino_t ino;
  9800. +};
  9801. +
  9802. +static ino_t decode_ino(__u32 *a)
  9803. +{
  9804. + union conv u;
  9805. +
  9806. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  9807. + u.a[0] = a[0];
  9808. +#ifdef CONFIG_AUFS_INO_T_64
  9809. + u.a[1] = a[1];
  9810. +#endif
  9811. + return u.ino;
  9812. +}
  9813. +
  9814. +static void encode_ino(__u32 *a, ino_t ino)
  9815. +{
  9816. + union conv u;
  9817. +
  9818. + u.ino = ino;
  9819. + a[0] = u.a[0];
  9820. +#ifdef CONFIG_AUFS_INO_T_64
  9821. + a[1] = u.a[1];
  9822. +#endif
  9823. +}
  9824. +
  9825. +/* NFS file handle */
  9826. +enum {
  9827. + Fh_br_id,
  9828. + Fh_sigen,
  9829. +#ifdef CONFIG_AUFS_INO_T_64
  9830. + /* support 64bit inode number */
  9831. + Fh_ino1,
  9832. + Fh_ino2,
  9833. + Fh_dir_ino1,
  9834. + Fh_dir_ino2,
  9835. +#else
  9836. + Fh_ino1,
  9837. + Fh_dir_ino1,
  9838. +#endif
  9839. + Fh_igen,
  9840. + Fh_h_type,
  9841. + Fh_tail,
  9842. +
  9843. + Fh_ino = Fh_ino1,
  9844. + Fh_dir_ino = Fh_dir_ino1
  9845. +};
  9846. +
  9847. +static int au_test_anon(struct dentry *dentry)
  9848. +{
  9849. + /* note: read d_flags without d_lock */
  9850. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  9851. +}
  9852. +
  9853. +int au_test_nfsd(void)
  9854. +{
  9855. + int ret;
  9856. + struct task_struct *tsk = current;
  9857. + char comm[sizeof(tsk->comm)];
  9858. +
  9859. + ret = 0;
  9860. + if (tsk->flags & PF_KTHREAD) {
  9861. + get_task_comm(comm, tsk);
  9862. + ret = !strcmp(comm, "nfsd");
  9863. + }
  9864. +
  9865. + return ret;
  9866. +}
  9867. +
  9868. +/* ---------------------------------------------------------------------- */
  9869. +/* inode generation external table */
  9870. +
  9871. +void au_xigen_inc(struct inode *inode)
  9872. +{
  9873. + loff_t pos;
  9874. + ssize_t sz;
  9875. + __u32 igen;
  9876. + struct super_block *sb;
  9877. + struct au_sbinfo *sbinfo;
  9878. +
  9879. + sb = inode->i_sb;
  9880. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  9881. +
  9882. + sbinfo = au_sbi(sb);
  9883. + pos = inode->i_ino;
  9884. + pos *= sizeof(igen);
  9885. + igen = inode->i_generation + 1;
  9886. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  9887. + sizeof(igen), &pos);
  9888. + if (sz == sizeof(igen))
  9889. + return; /* success */
  9890. +
  9891. + if (unlikely(sz >= 0))
  9892. + AuIOErr("xigen error (%zd)\n", sz);
  9893. +}
  9894. +
  9895. +int au_xigen_new(struct inode *inode)
  9896. +{
  9897. + int err;
  9898. + loff_t pos;
  9899. + ssize_t sz;
  9900. + struct super_block *sb;
  9901. + struct au_sbinfo *sbinfo;
  9902. + struct file *file;
  9903. +
  9904. + err = 0;
  9905. + /* todo: dirty, at mount time */
  9906. + if (inode->i_ino == AUFS_ROOT_INO)
  9907. + goto out;
  9908. + sb = inode->i_sb;
  9909. + SiMustAnyLock(sb);
  9910. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  9911. + goto out;
  9912. +
  9913. + err = -EFBIG;
  9914. + pos = inode->i_ino;
  9915. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  9916. + AuIOErr1("too large i%lld\n", pos);
  9917. + goto out;
  9918. + }
  9919. + pos *= sizeof(inode->i_generation);
  9920. +
  9921. + err = 0;
  9922. + sbinfo = au_sbi(sb);
  9923. + file = sbinfo->si_xigen;
  9924. + BUG_ON(!file);
  9925. +
  9926. + if (vfsub_f_size_read(file)
  9927. + < pos + sizeof(inode->i_generation)) {
  9928. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  9929. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  9930. + sizeof(inode->i_generation), &pos);
  9931. + } else
  9932. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  9933. + sizeof(inode->i_generation), &pos);
  9934. + if (sz == sizeof(inode->i_generation))
  9935. + goto out; /* success */
  9936. +
  9937. + err = sz;
  9938. + if (unlikely(sz >= 0)) {
  9939. + err = -EIO;
  9940. + AuIOErr("xigen error (%zd)\n", sz);
  9941. + }
  9942. +
  9943. +out:
  9944. + return err;
  9945. +}
  9946. +
  9947. +int au_xigen_set(struct super_block *sb, struct file *base)
  9948. +{
  9949. + int err;
  9950. + struct au_sbinfo *sbinfo;
  9951. + struct file *file;
  9952. +
  9953. + SiMustWriteLock(sb);
  9954. +
  9955. + sbinfo = au_sbi(sb);
  9956. + file = au_xino_create2(base, sbinfo->si_xigen);
  9957. + err = PTR_ERR(file);
  9958. + if (IS_ERR(file))
  9959. + goto out;
  9960. + err = 0;
  9961. + if (sbinfo->si_xigen)
  9962. + fput(sbinfo->si_xigen);
  9963. + sbinfo->si_xigen = file;
  9964. +
  9965. +out:
  9966. + return err;
  9967. +}
  9968. +
  9969. +void au_xigen_clr(struct super_block *sb)
  9970. +{
  9971. + struct au_sbinfo *sbinfo;
  9972. +
  9973. + SiMustWriteLock(sb);
  9974. +
  9975. + sbinfo = au_sbi(sb);
  9976. + if (sbinfo->si_xigen) {
  9977. + fput(sbinfo->si_xigen);
  9978. + sbinfo->si_xigen = NULL;
  9979. + }
  9980. +}
  9981. +
  9982. +/* ---------------------------------------------------------------------- */
  9983. +
  9984. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  9985. + ino_t dir_ino)
  9986. +{
  9987. + struct dentry *dentry, *d;
  9988. + struct inode *inode;
  9989. + unsigned int sigen;
  9990. +
  9991. + dentry = NULL;
  9992. + inode = ilookup(sb, ino);
  9993. + if (!inode)
  9994. + goto out;
  9995. +
  9996. + dentry = ERR_PTR(-ESTALE);
  9997. + sigen = au_sigen(sb);
  9998. + if (unlikely(is_bad_inode(inode)
  9999. + || IS_DEADDIR(inode)
  10000. + || sigen != au_iigen(inode, NULL)))
  10001. + goto out_iput;
  10002. +
  10003. + dentry = NULL;
  10004. + if (!dir_ino || S_ISDIR(inode->i_mode))
  10005. + dentry = d_find_alias(inode);
  10006. + else {
  10007. + spin_lock(&inode->i_lock);
  10008. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  10009. + spin_lock(&d->d_lock);
  10010. + if (!au_test_anon(d)
  10011. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  10012. + dentry = dget_dlock(d);
  10013. + spin_unlock(&d->d_lock);
  10014. + break;
  10015. + }
  10016. + spin_unlock(&d->d_lock);
  10017. + }
  10018. + spin_unlock(&inode->i_lock);
  10019. + }
  10020. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  10021. + /* need to refresh */
  10022. + dput(dentry);
  10023. + dentry = NULL;
  10024. + }
  10025. +
  10026. +out_iput:
  10027. + iput(inode);
  10028. +out:
  10029. + AuTraceErrPtr(dentry);
  10030. + return dentry;
  10031. +}
  10032. +
  10033. +/* ---------------------------------------------------------------------- */
  10034. +
  10035. +/* todo: dirty? */
  10036. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  10037. +
  10038. +struct au_compare_mnt_args {
  10039. + /* input */
  10040. + struct super_block *sb;
  10041. +
  10042. + /* output */
  10043. + struct vfsmount *mnt;
  10044. +};
  10045. +
  10046. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  10047. +{
  10048. + struct au_compare_mnt_args *a = arg;
  10049. +
  10050. + if (mnt->mnt_sb != a->sb)
  10051. + return 0;
  10052. + a->mnt = mntget(mnt);
  10053. + return 1;
  10054. +}
  10055. +
  10056. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  10057. +{
  10058. + int err;
  10059. + struct path root;
  10060. + struct au_compare_mnt_args args = {
  10061. + .sb = sb
  10062. + };
  10063. +
  10064. + get_fs_root(current->fs, &root);
  10065. + rcu_read_lock();
  10066. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  10067. + rcu_read_unlock();
  10068. + path_put(&root);
  10069. + AuDebugOn(!err);
  10070. + AuDebugOn(!args.mnt);
  10071. + return args.mnt;
  10072. +}
  10073. +
  10074. +struct au_nfsd_si_lock {
  10075. + unsigned int sigen;
  10076. + aufs_bindex_t bindex, br_id;
  10077. + unsigned char force_lock;
  10078. +};
  10079. +
  10080. +static int si_nfsd_read_lock(struct super_block *sb,
  10081. + struct au_nfsd_si_lock *nsi_lock)
  10082. +{
  10083. + int err;
  10084. + aufs_bindex_t bindex;
  10085. +
  10086. + si_read_lock(sb, AuLock_FLUSH);
  10087. +
  10088. + /* branch id may be wrapped around */
  10089. + err = 0;
  10090. + bindex = au_br_index(sb, nsi_lock->br_id);
  10091. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  10092. + goto out; /* success */
  10093. +
  10094. + err = -ESTALE;
  10095. + bindex = -1;
  10096. + if (!nsi_lock->force_lock)
  10097. + si_read_unlock(sb);
  10098. +
  10099. +out:
  10100. + nsi_lock->bindex = bindex;
  10101. + return err;
  10102. +}
  10103. +
  10104. +struct find_name_by_ino {
  10105. + struct dir_context ctx;
  10106. + int called, found;
  10107. + ino_t ino;
  10108. + char *name;
  10109. + int namelen;
  10110. +};
  10111. +
  10112. +static int
  10113. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  10114. + loff_t offset, u64 ino, unsigned int d_type)
  10115. +{
  10116. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  10117. + ctx);
  10118. +
  10119. + a->called++;
  10120. + if (a->ino != ino)
  10121. + return 0;
  10122. +
  10123. + memcpy(a->name, name, namelen);
  10124. + a->namelen = namelen;
  10125. + a->found = 1;
  10126. + return 1;
  10127. +}
  10128. +
  10129. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  10130. + struct au_nfsd_si_lock *nsi_lock)
  10131. +{
  10132. + struct dentry *dentry, *parent;
  10133. + struct file *file;
  10134. + struct inode *dir;
  10135. + struct find_name_by_ino arg = {
  10136. + .ctx = {
  10137. + .actor = find_name_by_ino
  10138. + }
  10139. + };
  10140. + int err;
  10141. +
  10142. + parent = path->dentry;
  10143. + if (nsi_lock)
  10144. + si_read_unlock(parent->d_sb);
  10145. + file = vfsub_dentry_open(path, au_dir_roflags);
  10146. + dentry = (void *)file;
  10147. + if (IS_ERR(file))
  10148. + goto out;
  10149. +
  10150. + dentry = ERR_PTR(-ENOMEM);
  10151. + arg.name = (void *)__get_free_page(GFP_NOFS);
  10152. + if (unlikely(!arg.name))
  10153. + goto out_file;
  10154. + arg.ino = ino;
  10155. + arg.found = 0;
  10156. + do {
  10157. + arg.called = 0;
  10158. + /* smp_mb(); */
  10159. + err = vfsub_iterate_dir(file, &arg.ctx);
  10160. + } while (!err && !arg.found && arg.called);
  10161. + dentry = ERR_PTR(err);
  10162. + if (unlikely(err))
  10163. + goto out_name;
  10164. + /* instead of ENOENT */
  10165. + dentry = ERR_PTR(-ESTALE);
  10166. + if (!arg.found)
  10167. + goto out_name;
  10168. +
  10169. + /* do not call vfsub_lkup_one() */
  10170. + dir = d_inode(parent);
  10171. + mutex_lock(&dir->i_mutex);
  10172. + dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
  10173. + mutex_unlock(&dir->i_mutex);
  10174. + AuTraceErrPtr(dentry);
  10175. + if (IS_ERR(dentry))
  10176. + goto out_name;
  10177. + AuDebugOn(au_test_anon(dentry));
  10178. + if (unlikely(d_really_is_negative(dentry))) {
  10179. + dput(dentry);
  10180. + dentry = ERR_PTR(-ENOENT);
  10181. + }
  10182. +
  10183. +out_name:
  10184. + free_page((unsigned long)arg.name);
  10185. +out_file:
  10186. + fput(file);
  10187. +out:
  10188. + if (unlikely(nsi_lock
  10189. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  10190. + if (!IS_ERR(dentry)) {
  10191. + dput(dentry);
  10192. + dentry = ERR_PTR(-ESTALE);
  10193. + }
  10194. + AuTraceErrPtr(dentry);
  10195. + return dentry;
  10196. +}
  10197. +
  10198. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  10199. + ino_t dir_ino,
  10200. + struct au_nfsd_si_lock *nsi_lock)
  10201. +{
  10202. + struct dentry *dentry;
  10203. + struct path path;
  10204. +
  10205. + if (dir_ino != AUFS_ROOT_INO) {
  10206. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  10207. + dentry = path.dentry;
  10208. + if (!path.dentry || IS_ERR(path.dentry))
  10209. + goto out;
  10210. + AuDebugOn(au_test_anon(path.dentry));
  10211. + } else
  10212. + path.dentry = dget(sb->s_root);
  10213. +
  10214. + path.mnt = au_mnt_get(sb);
  10215. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  10216. + path_put(&path);
  10217. +
  10218. +out:
  10219. + AuTraceErrPtr(dentry);
  10220. + return dentry;
  10221. +}
  10222. +
  10223. +/* ---------------------------------------------------------------------- */
  10224. +
  10225. +static int h_acceptable(void *expv, struct dentry *dentry)
  10226. +{
  10227. + return 1;
  10228. +}
  10229. +
  10230. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  10231. + char *buf, int len, struct super_block *sb)
  10232. +{
  10233. + char *p;
  10234. + int n;
  10235. + struct path path;
  10236. +
  10237. + p = d_path(h_rootpath, buf, len);
  10238. + if (IS_ERR(p))
  10239. + goto out;
  10240. + n = strlen(p);
  10241. +
  10242. + path.mnt = h_rootpath->mnt;
  10243. + path.dentry = h_parent;
  10244. + p = d_path(&path, buf, len);
  10245. + if (IS_ERR(p))
  10246. + goto out;
  10247. + if (n != 1)
  10248. + p += n;
  10249. +
  10250. + path.mnt = au_mnt_get(sb);
  10251. + path.dentry = sb->s_root;
  10252. + p = d_path(&path, buf, len - strlen(p));
  10253. + mntput(path.mnt);
  10254. + if (IS_ERR(p))
  10255. + goto out;
  10256. + if (n != 1)
  10257. + p[strlen(p)] = '/';
  10258. +
  10259. +out:
  10260. + AuTraceErrPtr(p);
  10261. + return p;
  10262. +}
  10263. +
  10264. +static
  10265. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  10266. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  10267. +{
  10268. + struct dentry *dentry, *h_parent, *root;
  10269. + struct super_block *h_sb;
  10270. + char *pathname, *p;
  10271. + struct vfsmount *h_mnt;
  10272. + struct au_branch *br;
  10273. + int err;
  10274. + struct path path;
  10275. +
  10276. + br = au_sbr(sb, nsi_lock->bindex);
  10277. + h_mnt = au_br_mnt(br);
  10278. + h_sb = h_mnt->mnt_sb;
  10279. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  10280. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  10281. + fh_len - Fh_tail, fh[Fh_h_type],
  10282. + h_acceptable, /*context*/NULL);
  10283. + dentry = h_parent;
  10284. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  10285. + AuWarn1("%s decode_fh failed, %ld\n",
  10286. + au_sbtype(h_sb), PTR_ERR(h_parent));
  10287. + goto out;
  10288. + }
  10289. + dentry = NULL;
  10290. + if (unlikely(au_test_anon(h_parent))) {
  10291. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  10292. + au_sbtype(h_sb));
  10293. + goto out_h_parent;
  10294. + }
  10295. +
  10296. + dentry = ERR_PTR(-ENOMEM);
  10297. + pathname = (void *)__get_free_page(GFP_NOFS);
  10298. + if (unlikely(!pathname))
  10299. + goto out_h_parent;
  10300. +
  10301. + root = sb->s_root;
  10302. + path.mnt = h_mnt;
  10303. + di_read_lock_parent(root, !AuLock_IR);
  10304. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  10305. + di_read_unlock(root, !AuLock_IR);
  10306. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  10307. + dentry = (void *)p;
  10308. + if (IS_ERR(p))
  10309. + goto out_pathname;
  10310. +
  10311. + si_read_unlock(sb);
  10312. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  10313. + dentry = ERR_PTR(err);
  10314. + if (unlikely(err))
  10315. + goto out_relock;
  10316. +
  10317. + dentry = ERR_PTR(-ENOENT);
  10318. + AuDebugOn(au_test_anon(path.dentry));
  10319. + if (unlikely(d_really_is_negative(path.dentry)))
  10320. + goto out_path;
  10321. +
  10322. + if (ino != d_inode(path.dentry)->i_ino)
  10323. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  10324. + else
  10325. + dentry = dget(path.dentry);
  10326. +
  10327. +out_path:
  10328. + path_put(&path);
  10329. +out_relock:
  10330. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  10331. + if (!IS_ERR(dentry)) {
  10332. + dput(dentry);
  10333. + dentry = ERR_PTR(-ESTALE);
  10334. + }
  10335. +out_pathname:
  10336. + free_page((unsigned long)pathname);
  10337. +out_h_parent:
  10338. + dput(h_parent);
  10339. +out:
  10340. + AuTraceErrPtr(dentry);
  10341. + return dentry;
  10342. +}
  10343. +
  10344. +/* ---------------------------------------------------------------------- */
  10345. +
  10346. +static struct dentry *
  10347. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  10348. + int fh_type)
  10349. +{
  10350. + struct dentry *dentry;
  10351. + __u32 *fh = fid->raw;
  10352. + struct au_branch *br;
  10353. + ino_t ino, dir_ino;
  10354. + struct au_nfsd_si_lock nsi_lock = {
  10355. + .force_lock = 0
  10356. + };
  10357. +
  10358. + dentry = ERR_PTR(-ESTALE);
  10359. + /* it should never happen, but the file handle is unreliable */
  10360. + if (unlikely(fh_len < Fh_tail))
  10361. + goto out;
  10362. + nsi_lock.sigen = fh[Fh_sigen];
  10363. + nsi_lock.br_id = fh[Fh_br_id];
  10364. +
  10365. + /* branch id may be wrapped around */
  10366. + br = NULL;
  10367. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  10368. + goto out;
  10369. + nsi_lock.force_lock = 1;
  10370. +
  10371. + /* is this inode still cached? */
  10372. + ino = decode_ino(fh + Fh_ino);
  10373. + /* it should never happen */
  10374. + if (unlikely(ino == AUFS_ROOT_INO))
  10375. + goto out;
  10376. +
  10377. + dir_ino = decode_ino(fh + Fh_dir_ino);
  10378. + dentry = decode_by_ino(sb, ino, dir_ino);
  10379. + if (IS_ERR(dentry))
  10380. + goto out_unlock;
  10381. + if (dentry)
  10382. + goto accept;
  10383. +
  10384. + /* is the parent dir cached? */
  10385. + br = au_sbr(sb, nsi_lock.bindex);
  10386. + atomic_inc(&br->br_count);
  10387. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  10388. + if (IS_ERR(dentry))
  10389. + goto out_unlock;
  10390. + if (dentry)
  10391. + goto accept;
  10392. +
  10393. + /* lookup path */
  10394. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  10395. + if (IS_ERR(dentry))
  10396. + goto out_unlock;
  10397. + if (unlikely(!dentry))
  10398. + /* todo?: make it ESTALE */
  10399. + goto out_unlock;
  10400. +
  10401. +accept:
  10402. + if (!au_digen_test(dentry, au_sigen(sb))
  10403. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  10404. + goto out_unlock; /* success */
  10405. +
  10406. + dput(dentry);
  10407. + dentry = ERR_PTR(-ESTALE);
  10408. +out_unlock:
  10409. + if (br)
  10410. + atomic_dec(&br->br_count);
  10411. + si_read_unlock(sb);
  10412. +out:
  10413. + AuTraceErrPtr(dentry);
  10414. + return dentry;
  10415. +}
  10416. +
  10417. +#if 0 /* reserved for future use */
  10418. +/* support subtreecheck option */
  10419. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  10420. + int fh_len, int fh_type)
  10421. +{
  10422. + struct dentry *parent;
  10423. + __u32 *fh = fid->raw;
  10424. + ino_t dir_ino;
  10425. +
  10426. + dir_ino = decode_ino(fh + Fh_dir_ino);
  10427. + parent = decode_by_ino(sb, dir_ino, 0);
  10428. + if (IS_ERR(parent))
  10429. + goto out;
  10430. + if (!parent)
  10431. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  10432. + dir_ino, fh, fh_len);
  10433. +
  10434. +out:
  10435. + AuTraceErrPtr(parent);
  10436. + return parent;
  10437. +}
  10438. +#endif
  10439. +
  10440. +/* ---------------------------------------------------------------------- */
  10441. +
  10442. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  10443. + struct inode *dir)
  10444. +{
  10445. + int err;
  10446. + aufs_bindex_t bindex;
  10447. + struct super_block *sb, *h_sb;
  10448. + struct dentry *dentry, *parent, *h_parent;
  10449. + struct inode *h_dir;
  10450. + struct au_branch *br;
  10451. +
  10452. + err = -ENOSPC;
  10453. + if (unlikely(*max_len <= Fh_tail)) {
  10454. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  10455. + goto out;
  10456. + }
  10457. +
  10458. + err = FILEID_ROOT;
  10459. + if (inode->i_ino == AUFS_ROOT_INO) {
  10460. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  10461. + goto out;
  10462. + }
  10463. +
  10464. + h_parent = NULL;
  10465. + sb = inode->i_sb;
  10466. + err = si_read_lock(sb, AuLock_FLUSH);
  10467. + if (unlikely(err))
  10468. + goto out;
  10469. +
  10470. +#ifdef CONFIG_AUFS_DEBUG
  10471. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  10472. + AuWarn1("NFS-exporting requires xino\n");
  10473. +#endif
  10474. + err = -EIO;
  10475. + parent = NULL;
  10476. + ii_read_lock_child(inode);
  10477. + bindex = au_ibstart(inode);
  10478. + if (!dir) {
  10479. + dentry = d_find_any_alias(inode);
  10480. + if (unlikely(!dentry))
  10481. + goto out_unlock;
  10482. + AuDebugOn(au_test_anon(dentry));
  10483. + parent = dget_parent(dentry);
  10484. + dput(dentry);
  10485. + if (unlikely(!parent))
  10486. + goto out_unlock;
  10487. + if (d_really_is_positive(parent))
  10488. + dir = d_inode(parent);
  10489. + }
  10490. +
  10491. + ii_read_lock_parent(dir);
  10492. + h_dir = au_h_iptr(dir, bindex);
  10493. + ii_read_unlock(dir);
  10494. + if (unlikely(!h_dir))
  10495. + goto out_parent;
  10496. + h_parent = d_find_any_alias(h_dir);
  10497. + if (unlikely(!h_parent))
  10498. + goto out_hparent;
  10499. +
  10500. + err = -EPERM;
  10501. + br = au_sbr(sb, bindex);
  10502. + h_sb = au_br_sb(br);
  10503. + if (unlikely(!h_sb->s_export_op)) {
  10504. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  10505. + goto out_hparent;
  10506. + }
  10507. +
  10508. + fh[Fh_br_id] = br->br_id;
  10509. + fh[Fh_sigen] = au_sigen(sb);
  10510. + encode_ino(fh + Fh_ino, inode->i_ino);
  10511. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  10512. + fh[Fh_igen] = inode->i_generation;
  10513. +
  10514. + *max_len -= Fh_tail;
  10515. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  10516. + max_len,
  10517. + /*connectable or subtreecheck*/0);
  10518. + err = fh[Fh_h_type];
  10519. + *max_len += Fh_tail;
  10520. + /* todo: macros? */
  10521. + if (err != FILEID_INVALID)
  10522. + err = 99;
  10523. + else
  10524. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  10525. +
  10526. +out_hparent:
  10527. + dput(h_parent);
  10528. +out_parent:
  10529. + dput(parent);
  10530. +out_unlock:
  10531. + ii_read_unlock(inode);
  10532. + si_read_unlock(sb);
  10533. +out:
  10534. + if (unlikely(err < 0))
  10535. + err = FILEID_INVALID;
  10536. + return err;
  10537. +}
  10538. +
  10539. +/* ---------------------------------------------------------------------- */
  10540. +
  10541. +static int aufs_commit_metadata(struct inode *inode)
  10542. +{
  10543. + int err;
  10544. + aufs_bindex_t bindex;
  10545. + struct super_block *sb;
  10546. + struct inode *h_inode;
  10547. + int (*f)(struct inode *inode);
  10548. +
  10549. + sb = inode->i_sb;
  10550. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  10551. + ii_write_lock_child(inode);
  10552. + bindex = au_ibstart(inode);
  10553. + AuDebugOn(bindex < 0);
  10554. + h_inode = au_h_iptr(inode, bindex);
  10555. +
  10556. + f = h_inode->i_sb->s_export_op->commit_metadata;
  10557. + if (f)
  10558. + err = f(h_inode);
  10559. + else {
  10560. + struct writeback_control wbc = {
  10561. + .sync_mode = WB_SYNC_ALL,
  10562. + .nr_to_write = 0 /* metadata only */
  10563. + };
  10564. +
  10565. + err = sync_inode(h_inode, &wbc);
  10566. + }
  10567. +
  10568. + au_cpup_attr_timesizes(inode);
  10569. + ii_write_unlock(inode);
  10570. + si_read_unlock(sb);
  10571. + return err;
  10572. +}
  10573. +
  10574. +/* ---------------------------------------------------------------------- */
  10575. +
  10576. +static struct export_operations aufs_export_op = {
  10577. + .fh_to_dentry = aufs_fh_to_dentry,
  10578. + /* .fh_to_parent = aufs_fh_to_parent, */
  10579. + .encode_fh = aufs_encode_fh,
  10580. + .commit_metadata = aufs_commit_metadata
  10581. +};
  10582. +
  10583. +void au_export_init(struct super_block *sb)
  10584. +{
  10585. + struct au_sbinfo *sbinfo;
  10586. + __u32 u;
  10587. +
  10588. + sb->s_export_op = &aufs_export_op;
  10589. + sbinfo = au_sbi(sb);
  10590. + sbinfo->si_xigen = NULL;
  10591. + get_random_bytes(&u, sizeof(u));
  10592. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  10593. + atomic_set(&sbinfo->si_xigen_next, u);
  10594. +}
  10595. diff -Nur linux-4.1.10.orig/fs/aufs/fhsm.c linux-4.1.10/fs/aufs/fhsm.c
  10596. --- linux-4.1.10.orig/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
  10597. +++ linux-4.1.10/fs/aufs/fhsm.c 2015-10-22 21:35:53.000000000 +0200
  10598. @@ -0,0 +1,426 @@
  10599. +/*
  10600. + * Copyright (C) 2011-2015 Junjiro R. Okajima
  10601. + *
  10602. + * This program, aufs is free software; you can redistribute it and/or modify
  10603. + * it under the terms of the GNU General Public License as published by
  10604. + * the Free Software Foundation; either version 2 of the License, or
  10605. + * (at your option) any later version.
  10606. + *
  10607. + * This program is distributed in the hope that it will be useful,
  10608. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10609. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10610. + * GNU General Public License for more details.
  10611. + *
  10612. + * You should have received a copy of the GNU General Public License
  10613. + * along with this program; if not, write to the Free Software
  10614. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  10615. + */
  10616. +
  10617. +/*
  10618. + * File-based Hierarchy Storage Management
  10619. + */
  10620. +
  10621. +#include <linux/anon_inodes.h>
  10622. +#include <linux/poll.h>
  10623. +#include <linux/seq_file.h>
  10624. +#include <linux/statfs.h>
  10625. +#include "aufs.h"
  10626. +
  10627. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  10628. +{
  10629. + struct au_sbinfo *sbinfo;
  10630. + struct au_fhsm *fhsm;
  10631. +
  10632. + SiMustAnyLock(sb);
  10633. +
  10634. + sbinfo = au_sbi(sb);
  10635. + fhsm = &sbinfo->si_fhsm;
  10636. + AuDebugOn(!fhsm);
  10637. + return fhsm->fhsm_bottom;
  10638. +}
  10639. +
  10640. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  10641. +{
  10642. + struct au_sbinfo *sbinfo;
  10643. + struct au_fhsm *fhsm;
  10644. +
  10645. + SiMustWriteLock(sb);
  10646. +
  10647. + sbinfo = au_sbi(sb);
  10648. + fhsm = &sbinfo->si_fhsm;
  10649. + AuDebugOn(!fhsm);
  10650. + fhsm->fhsm_bottom = bindex;
  10651. +}
  10652. +
  10653. +/* ---------------------------------------------------------------------- */
  10654. +
  10655. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  10656. +{
  10657. + struct au_br_fhsm *bf;
  10658. +
  10659. + bf = br->br_fhsm;
  10660. + MtxMustLock(&bf->bf_lock);
  10661. +
  10662. + return !bf->bf_readable
  10663. + || time_after(jiffies,
  10664. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  10665. +}
  10666. +
  10667. +/* ---------------------------------------------------------------------- */
  10668. +
  10669. +static void au_fhsm_notify(struct super_block *sb, int val)
  10670. +{
  10671. + struct au_sbinfo *sbinfo;
  10672. + struct au_fhsm *fhsm;
  10673. +
  10674. + SiMustAnyLock(sb);
  10675. +
  10676. + sbinfo = au_sbi(sb);
  10677. + fhsm = &sbinfo->si_fhsm;
  10678. + if (au_fhsm_pid(fhsm)
  10679. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  10680. + atomic_set(&fhsm->fhsm_readable, val);
  10681. + if (val)
  10682. + wake_up(&fhsm->fhsm_wqh);
  10683. + }
  10684. +}
  10685. +
  10686. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  10687. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  10688. +{
  10689. + int err;
  10690. + struct au_branch *br;
  10691. + struct au_br_fhsm *bf;
  10692. +
  10693. + br = au_sbr(sb, bindex);
  10694. + AuDebugOn(au_br_rdonly(br));
  10695. + bf = br->br_fhsm;
  10696. + AuDebugOn(!bf);
  10697. +
  10698. + if (do_lock)
  10699. + mutex_lock(&bf->bf_lock);
  10700. + else
  10701. + MtxMustLock(&bf->bf_lock);
  10702. +
  10703. + /* sb->s_root for NFS is unreliable */
  10704. + err = au_br_stfs(br, &bf->bf_stfs);
  10705. + if (unlikely(err)) {
  10706. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  10707. + goto out;
  10708. + }
  10709. +
  10710. + bf->bf_jiffy = jiffies;
  10711. + bf->bf_readable = 1;
  10712. + if (do_notify)
  10713. + au_fhsm_notify(sb, /*val*/1);
  10714. + if (rstfs)
  10715. + *rstfs = bf->bf_stfs;
  10716. +
  10717. +out:
  10718. + if (do_lock)
  10719. + mutex_unlock(&bf->bf_lock);
  10720. + au_fhsm_notify(sb, /*val*/1);
  10721. +
  10722. + return err;
  10723. +}
  10724. +
  10725. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  10726. +{
  10727. + int err;
  10728. + struct au_sbinfo *sbinfo;
  10729. + struct au_fhsm *fhsm;
  10730. + struct au_branch *br;
  10731. + struct au_br_fhsm *bf;
  10732. +
  10733. + AuDbg("b%d, force %d\n", bindex, force);
  10734. + SiMustAnyLock(sb);
  10735. +
  10736. + sbinfo = au_sbi(sb);
  10737. + fhsm = &sbinfo->si_fhsm;
  10738. + if (!au_ftest_si(sbinfo, FHSM)
  10739. + || fhsm->fhsm_bottom == bindex)
  10740. + return;
  10741. +
  10742. + br = au_sbr(sb, bindex);
  10743. + bf = br->br_fhsm;
  10744. + AuDebugOn(!bf);
  10745. + mutex_lock(&bf->bf_lock);
  10746. + if (force
  10747. + || au_fhsm_pid(fhsm)
  10748. + || au_fhsm_test_jiffy(sbinfo, br))
  10749. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  10750. + /*do_notify*/1);
  10751. + mutex_unlock(&bf->bf_lock);
  10752. +}
  10753. +
  10754. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  10755. +{
  10756. + aufs_bindex_t bindex, bend;
  10757. + struct au_branch *br;
  10758. +
  10759. + /* exclude the bottom */
  10760. + bend = au_fhsm_bottom(sb);
  10761. + for (bindex = 0; bindex < bend; bindex++) {
  10762. + br = au_sbr(sb, bindex);
  10763. + if (au_br_fhsm(br->br_perm))
  10764. + au_fhsm_wrote(sb, bindex, force);
  10765. + }
  10766. +}
  10767. +
  10768. +/* ---------------------------------------------------------------------- */
  10769. +
  10770. +static unsigned int au_fhsm_poll(struct file *file,
  10771. + struct poll_table_struct *wait)
  10772. +{
  10773. + unsigned int mask;
  10774. + struct au_sbinfo *sbinfo;
  10775. + struct au_fhsm *fhsm;
  10776. +
  10777. + mask = 0;
  10778. + sbinfo = file->private_data;
  10779. + fhsm = &sbinfo->si_fhsm;
  10780. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  10781. + if (atomic_read(&fhsm->fhsm_readable))
  10782. + mask = POLLIN /* | POLLRDNORM */;
  10783. +
  10784. + AuTraceErr((int)mask);
  10785. + return mask;
  10786. +}
  10787. +
  10788. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  10789. + struct aufs_stfs *stfs, __s16 brid)
  10790. +{
  10791. + int err;
  10792. +
  10793. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  10794. + if (!err)
  10795. + err = __put_user(brid, &stbr->brid);
  10796. + if (unlikely(err))
  10797. + err = -EFAULT;
  10798. +
  10799. + return err;
  10800. +}
  10801. +
  10802. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  10803. + struct aufs_stbr __user *stbr, size_t count)
  10804. +{
  10805. + ssize_t err;
  10806. + int nstbr;
  10807. + aufs_bindex_t bindex, bend;
  10808. + struct au_branch *br;
  10809. + struct au_br_fhsm *bf;
  10810. +
  10811. + /* except the bottom branch */
  10812. + err = 0;
  10813. + nstbr = 0;
  10814. + bend = au_fhsm_bottom(sb);
  10815. + for (bindex = 0; !err && bindex < bend; bindex++) {
  10816. + br = au_sbr(sb, bindex);
  10817. + if (!au_br_fhsm(br->br_perm))
  10818. + continue;
  10819. +
  10820. + bf = br->br_fhsm;
  10821. + mutex_lock(&bf->bf_lock);
  10822. + if (bf->bf_readable) {
  10823. + err = -EFAULT;
  10824. + if (count >= sizeof(*stbr))
  10825. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  10826. + br->br_id);
  10827. + if (!err) {
  10828. + bf->bf_readable = 0;
  10829. + count -= sizeof(*stbr);
  10830. + nstbr++;
  10831. + }
  10832. + }
  10833. + mutex_unlock(&bf->bf_lock);
  10834. + }
  10835. + if (!err)
  10836. + err = sizeof(*stbr) * nstbr;
  10837. +
  10838. + return err;
  10839. +}
  10840. +
  10841. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  10842. + loff_t *pos)
  10843. +{
  10844. + ssize_t err;
  10845. + int readable;
  10846. + aufs_bindex_t nfhsm, bindex, bend;
  10847. + struct au_sbinfo *sbinfo;
  10848. + struct au_fhsm *fhsm;
  10849. + struct au_branch *br;
  10850. + struct super_block *sb;
  10851. +
  10852. + err = 0;
  10853. + sbinfo = file->private_data;
  10854. + fhsm = &sbinfo->si_fhsm;
  10855. +need_data:
  10856. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  10857. + if (!atomic_read(&fhsm->fhsm_readable)) {
  10858. + if (vfsub_file_flags(file) & O_NONBLOCK)
  10859. + err = -EAGAIN;
  10860. + else
  10861. + err = wait_event_interruptible_locked_irq
  10862. + (fhsm->fhsm_wqh,
  10863. + atomic_read(&fhsm->fhsm_readable));
  10864. + }
  10865. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  10866. + if (unlikely(err))
  10867. + goto out;
  10868. +
  10869. + /* sb may already be dead */
  10870. + au_rw_read_lock(&sbinfo->si_rwsem);
  10871. + readable = atomic_read(&fhsm->fhsm_readable);
  10872. + if (readable > 0) {
  10873. + sb = sbinfo->si_sb;
  10874. + AuDebugOn(!sb);
  10875. + /* exclude the bottom branch */
  10876. + nfhsm = 0;
  10877. + bend = au_fhsm_bottom(sb);
  10878. + for (bindex = 0; bindex < bend; bindex++) {
  10879. + br = au_sbr(sb, bindex);
  10880. + if (au_br_fhsm(br->br_perm))
  10881. + nfhsm++;
  10882. + }
  10883. + err = -EMSGSIZE;
  10884. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  10885. + atomic_set(&fhsm->fhsm_readable, 0);
  10886. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  10887. + count);
  10888. + }
  10889. + }
  10890. + au_rw_read_unlock(&sbinfo->si_rwsem);
  10891. + if (!readable)
  10892. + goto need_data;
  10893. +
  10894. +out:
  10895. + return err;
  10896. +}
  10897. +
  10898. +static int au_fhsm_release(struct inode *inode, struct file *file)
  10899. +{
  10900. + struct au_sbinfo *sbinfo;
  10901. + struct au_fhsm *fhsm;
  10902. +
  10903. + /* sb may already be dead */
  10904. + sbinfo = file->private_data;
  10905. + fhsm = &sbinfo->si_fhsm;
  10906. + spin_lock(&fhsm->fhsm_spin);
  10907. + fhsm->fhsm_pid = 0;
  10908. + spin_unlock(&fhsm->fhsm_spin);
  10909. + kobject_put(&sbinfo->si_kobj);
  10910. +
  10911. + return 0;
  10912. +}
  10913. +
  10914. +static const struct file_operations au_fhsm_fops = {
  10915. + .owner = THIS_MODULE,
  10916. + .llseek = noop_llseek,
  10917. + .read = au_fhsm_read,
  10918. + .poll = au_fhsm_poll,
  10919. + .release = au_fhsm_release
  10920. +};
  10921. +
  10922. +int au_fhsm_fd(struct super_block *sb, int oflags)
  10923. +{
  10924. + int err, fd;
  10925. + struct au_sbinfo *sbinfo;
  10926. + struct au_fhsm *fhsm;
  10927. +
  10928. + err = -EPERM;
  10929. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  10930. + goto out;
  10931. +
  10932. + err = -EINVAL;
  10933. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  10934. + goto out;
  10935. +
  10936. + err = 0;
  10937. + sbinfo = au_sbi(sb);
  10938. + fhsm = &sbinfo->si_fhsm;
  10939. + spin_lock(&fhsm->fhsm_spin);
  10940. + if (!fhsm->fhsm_pid)
  10941. + fhsm->fhsm_pid = current->pid;
  10942. + else
  10943. + err = -EBUSY;
  10944. + spin_unlock(&fhsm->fhsm_spin);
  10945. + if (unlikely(err))
  10946. + goto out;
  10947. +
  10948. + oflags |= O_RDONLY;
  10949. + /* oflags |= FMODE_NONOTIFY; */
  10950. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  10951. + err = fd;
  10952. + if (unlikely(fd < 0))
  10953. + goto out_pid;
  10954. +
  10955. + /* succeed reglardless 'fhsm' status */
  10956. + kobject_get(&sbinfo->si_kobj);
  10957. + si_noflush_read_lock(sb);
  10958. + if (au_ftest_si(sbinfo, FHSM))
  10959. + au_fhsm_wrote_all(sb, /*force*/0);
  10960. + si_read_unlock(sb);
  10961. + goto out; /* success */
  10962. +
  10963. +out_pid:
  10964. + spin_lock(&fhsm->fhsm_spin);
  10965. + fhsm->fhsm_pid = 0;
  10966. + spin_unlock(&fhsm->fhsm_spin);
  10967. +out:
  10968. + AuTraceErr(err);
  10969. + return err;
  10970. +}
  10971. +
  10972. +/* ---------------------------------------------------------------------- */
  10973. +
  10974. +int au_fhsm_br_alloc(struct au_branch *br)
  10975. +{
  10976. + int err;
  10977. +
  10978. + err = 0;
  10979. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  10980. + if (br->br_fhsm)
  10981. + au_br_fhsm_init(br->br_fhsm);
  10982. + else
  10983. + err = -ENOMEM;
  10984. +
  10985. + return err;
  10986. +}
  10987. +
  10988. +/* ---------------------------------------------------------------------- */
  10989. +
  10990. +void au_fhsm_fin(struct super_block *sb)
  10991. +{
  10992. + au_fhsm_notify(sb, /*val*/-1);
  10993. +}
  10994. +
  10995. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  10996. +{
  10997. + struct au_fhsm *fhsm;
  10998. +
  10999. + fhsm = &sbinfo->si_fhsm;
  11000. + spin_lock_init(&fhsm->fhsm_spin);
  11001. + init_waitqueue_head(&fhsm->fhsm_wqh);
  11002. + atomic_set(&fhsm->fhsm_readable, 0);
  11003. + fhsm->fhsm_expire
  11004. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  11005. + fhsm->fhsm_bottom = -1;
  11006. +}
  11007. +
  11008. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  11009. +{
  11010. + sbinfo->si_fhsm.fhsm_expire
  11011. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  11012. +}
  11013. +
  11014. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  11015. +{
  11016. + unsigned int u;
  11017. +
  11018. + if (!au_ftest_si(sbinfo, FHSM))
  11019. + return;
  11020. +
  11021. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  11022. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  11023. + seq_printf(seq, ",fhsm_sec=%u", u);
  11024. +}
  11025. diff -Nur linux-4.1.10.orig/fs/aufs/file.c linux-4.1.10/fs/aufs/file.c
  11026. --- linux-4.1.10.orig/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
  11027. +++ linux-4.1.10/fs/aufs/file.c 2015-10-22 21:35:53.000000000 +0200
  11028. @@ -0,0 +1,841 @@
  11029. +/*
  11030. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11031. + *
  11032. + * This program, aufs is free software; you can redistribute it and/or modify
  11033. + * it under the terms of the GNU General Public License as published by
  11034. + * the Free Software Foundation; either version 2 of the License, or
  11035. + * (at your option) any later version.
  11036. + *
  11037. + * This program is distributed in the hope that it will be useful,
  11038. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11039. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11040. + * GNU General Public License for more details.
  11041. + *
  11042. + * You should have received a copy of the GNU General Public License
  11043. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11044. + */
  11045. +
  11046. +/*
  11047. + * handling file/dir, and address_space operation
  11048. + */
  11049. +
  11050. +#ifdef CONFIG_AUFS_DEBUG
  11051. +#include <linux/migrate.h>
  11052. +#endif
  11053. +#include <linux/pagemap.h>
  11054. +#include "aufs.h"
  11055. +
  11056. +/* drop flags for writing */
  11057. +unsigned int au_file_roflags(unsigned int flags)
  11058. +{
  11059. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  11060. + flags |= O_RDONLY | O_NOATIME;
  11061. + return flags;
  11062. +}
  11063. +
  11064. +/* common functions to regular file and dir */
  11065. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  11066. + struct file *file, int force_wr)
  11067. +{
  11068. + struct file *h_file;
  11069. + struct dentry *h_dentry;
  11070. + struct inode *h_inode;
  11071. + struct super_block *sb;
  11072. + struct au_branch *br;
  11073. + struct path h_path;
  11074. + int err;
  11075. +
  11076. + /* a race condition can happen between open and unlink/rmdir */
  11077. + h_file = ERR_PTR(-ENOENT);
  11078. + h_dentry = au_h_dptr(dentry, bindex);
  11079. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  11080. + goto out;
  11081. + h_inode = d_inode(h_dentry);
  11082. + spin_lock(&h_dentry->d_lock);
  11083. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  11084. + /* || !d_inode(dentry)->i_nlink */
  11085. + ;
  11086. + spin_unlock(&h_dentry->d_lock);
  11087. + if (unlikely(err))
  11088. + goto out;
  11089. +
  11090. + sb = dentry->d_sb;
  11091. + br = au_sbr(sb, bindex);
  11092. + err = au_br_test_oflag(flags, br);
  11093. + h_file = ERR_PTR(err);
  11094. + if (unlikely(err))
  11095. + goto out;
  11096. +
  11097. + /* drop flags for writing */
  11098. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  11099. + if (force_wr && !(flags & O_WRONLY))
  11100. + force_wr = 0;
  11101. + flags = au_file_roflags(flags);
  11102. + if (force_wr) {
  11103. + h_file = ERR_PTR(-EROFS);
  11104. + flags = au_file_roflags(flags);
  11105. + if (unlikely(vfsub_native_ro(h_inode)
  11106. + || IS_APPEND(h_inode)))
  11107. + goto out;
  11108. + flags &= ~O_ACCMODE;
  11109. + flags |= O_WRONLY;
  11110. + }
  11111. + }
  11112. + flags &= ~O_CREAT;
  11113. + atomic_inc(&br->br_count);
  11114. + h_path.dentry = h_dentry;
  11115. + h_path.mnt = au_br_mnt(br);
  11116. + h_file = vfsub_dentry_open(&h_path, flags);
  11117. + if (IS_ERR(h_file))
  11118. + goto out_br;
  11119. +
  11120. + if (flags & __FMODE_EXEC) {
  11121. + err = deny_write_access(h_file);
  11122. + if (unlikely(err)) {
  11123. + fput(h_file);
  11124. + h_file = ERR_PTR(err);
  11125. + goto out_br;
  11126. + }
  11127. + }
  11128. + fsnotify_open(h_file);
  11129. + goto out; /* success */
  11130. +
  11131. +out_br:
  11132. + atomic_dec(&br->br_count);
  11133. +out:
  11134. + return h_file;
  11135. +}
  11136. +
  11137. +static int au_cmoo(struct dentry *dentry)
  11138. +{
  11139. + int err, cmoo;
  11140. + unsigned int udba;
  11141. + struct path h_path;
  11142. + struct au_pin pin;
  11143. + struct au_cp_generic cpg = {
  11144. + .dentry = dentry,
  11145. + .bdst = -1,
  11146. + .bsrc = -1,
  11147. + .len = -1,
  11148. + .pin = &pin,
  11149. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  11150. + };
  11151. + struct inode *delegated;
  11152. + struct super_block *sb;
  11153. + struct au_sbinfo *sbinfo;
  11154. + struct au_fhsm *fhsm;
  11155. + pid_t pid;
  11156. + struct au_branch *br;
  11157. + struct dentry *parent;
  11158. + struct au_hinode *hdir;
  11159. +
  11160. + DiMustWriteLock(dentry);
  11161. + IiMustWriteLock(d_inode(dentry));
  11162. +
  11163. + err = 0;
  11164. + if (IS_ROOT(dentry))
  11165. + goto out;
  11166. + cpg.bsrc = au_dbstart(dentry);
  11167. + if (!cpg.bsrc)
  11168. + goto out;
  11169. +
  11170. + sb = dentry->d_sb;
  11171. + sbinfo = au_sbi(sb);
  11172. + fhsm = &sbinfo->si_fhsm;
  11173. + pid = au_fhsm_pid(fhsm);
  11174. + if (pid
  11175. + && (current->pid == pid
  11176. + || current->real_parent->pid == pid))
  11177. + goto out;
  11178. +
  11179. + br = au_sbr(sb, cpg.bsrc);
  11180. + cmoo = au_br_cmoo(br->br_perm);
  11181. + if (!cmoo)
  11182. + goto out;
  11183. + if (!d_is_reg(dentry))
  11184. + cmoo &= AuBrAttr_COO_ALL;
  11185. + if (!cmoo)
  11186. + goto out;
  11187. +
  11188. + parent = dget_parent(dentry);
  11189. + di_write_lock_parent(parent);
  11190. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  11191. + cpg.bdst = err;
  11192. + if (unlikely(err < 0)) {
  11193. + err = 0; /* there is no upper writable branch */
  11194. + goto out_dgrade;
  11195. + }
  11196. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  11197. +
  11198. + /* do not respect the coo attrib for the target branch */
  11199. + err = au_cpup_dirs(dentry, cpg.bdst);
  11200. + if (unlikely(err))
  11201. + goto out_dgrade;
  11202. +
  11203. + di_downgrade_lock(parent, AuLock_IR);
  11204. + udba = au_opt_udba(sb);
  11205. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  11206. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11207. + if (unlikely(err))
  11208. + goto out_parent;
  11209. +
  11210. + err = au_sio_cpup_simple(&cpg);
  11211. + au_unpin(&pin);
  11212. + if (unlikely(err))
  11213. + goto out_parent;
  11214. + if (!(cmoo & AuBrWAttr_MOO))
  11215. + goto out_parent; /* success */
  11216. +
  11217. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  11218. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11219. + if (unlikely(err))
  11220. + goto out_parent;
  11221. +
  11222. + h_path.mnt = au_br_mnt(br);
  11223. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  11224. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  11225. + delegated = NULL;
  11226. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  11227. + au_unpin(&pin);
  11228. + /* todo: keep h_dentry or not? */
  11229. + if (unlikely(err == -EWOULDBLOCK)) {
  11230. + pr_warn("cannot retry for NFSv4 delegation"
  11231. + " for an internal unlink\n");
  11232. + iput(delegated);
  11233. + }
  11234. + if (unlikely(err)) {
  11235. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  11236. + dentry, err);
  11237. + err = 0;
  11238. + }
  11239. + goto out_parent; /* success */
  11240. +
  11241. +out_dgrade:
  11242. + di_downgrade_lock(parent, AuLock_IR);
  11243. +out_parent:
  11244. + di_read_unlock(parent, AuLock_IR);
  11245. + dput(parent);
  11246. +out:
  11247. + AuTraceErr(err);
  11248. + return err;
  11249. +}
  11250. +
  11251. +int au_do_open(struct file *file, struct au_do_open_args *args)
  11252. +{
  11253. + int err, no_lock = args->no_lock;
  11254. + struct dentry *dentry;
  11255. + struct au_finfo *finfo;
  11256. +
  11257. + if (!no_lock)
  11258. + err = au_finfo_init(file, args->fidir);
  11259. + else {
  11260. + lockdep_off();
  11261. + err = au_finfo_init(file, args->fidir);
  11262. + lockdep_on();
  11263. + }
  11264. + if (unlikely(err))
  11265. + goto out;
  11266. +
  11267. + dentry = file->f_path.dentry;
  11268. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  11269. + if (!no_lock) {
  11270. + di_write_lock_child(dentry);
  11271. + err = au_cmoo(dentry);
  11272. + di_downgrade_lock(dentry, AuLock_IR);
  11273. + if (!err)
  11274. + err = args->open(file, vfsub_file_flags(file), NULL);
  11275. + di_read_unlock(dentry, AuLock_IR);
  11276. + } else {
  11277. + err = au_cmoo(dentry);
  11278. + if (!err)
  11279. + err = args->open(file, vfsub_file_flags(file),
  11280. + args->h_file);
  11281. + if (!err && au_fbstart(file) != au_dbstart(dentry))
  11282. + /*
  11283. + * cmoo happens after h_file was opened.
  11284. + * need to refresh file later.
  11285. + */
  11286. + atomic_dec(&au_fi(file)->fi_generation);
  11287. + }
  11288. +
  11289. + finfo = au_fi(file);
  11290. + if (!err) {
  11291. + finfo->fi_file = file;
  11292. + au_sphl_add(&finfo->fi_hlist,
  11293. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  11294. + }
  11295. + if (!no_lock)
  11296. + fi_write_unlock(file);
  11297. + else {
  11298. + lockdep_off();
  11299. + fi_write_unlock(file);
  11300. + lockdep_on();
  11301. + }
  11302. + if (unlikely(err)) {
  11303. + finfo->fi_hdir = NULL;
  11304. + au_finfo_fin(file);
  11305. + }
  11306. +
  11307. +out:
  11308. + return err;
  11309. +}
  11310. +
  11311. +int au_reopen_nondir(struct file *file)
  11312. +{
  11313. + int err;
  11314. + aufs_bindex_t bstart;
  11315. + struct dentry *dentry;
  11316. + struct file *h_file, *h_file_tmp;
  11317. +
  11318. + dentry = file->f_path.dentry;
  11319. + bstart = au_dbstart(dentry);
  11320. + h_file_tmp = NULL;
  11321. + if (au_fbstart(file) == bstart) {
  11322. + h_file = au_hf_top(file);
  11323. + if (file->f_mode == h_file->f_mode)
  11324. + return 0; /* success */
  11325. + h_file_tmp = h_file;
  11326. + get_file(h_file_tmp);
  11327. + au_set_h_fptr(file, bstart, NULL);
  11328. + }
  11329. + AuDebugOn(au_fi(file)->fi_hdir);
  11330. + /*
  11331. + * it can happen
  11332. + * file exists on both of rw and ro
  11333. + * open --> dbstart and fbstart are both 0
  11334. + * prepend a branch as rw, "rw" become ro
  11335. + * remove rw/file
  11336. + * delete the top branch, "rw" becomes rw again
  11337. + * --> dbstart is 1, fbstart is still 0
  11338. + * write --> fbstart is 0 but dbstart is 1
  11339. + */
  11340. + /* AuDebugOn(au_fbstart(file) < bstart); */
  11341. +
  11342. + h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
  11343. + file, /*force_wr*/0);
  11344. + err = PTR_ERR(h_file);
  11345. + if (IS_ERR(h_file)) {
  11346. + if (h_file_tmp) {
  11347. + atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
  11348. + au_set_h_fptr(file, bstart, h_file_tmp);
  11349. + h_file_tmp = NULL;
  11350. + }
  11351. + goto out; /* todo: close all? */
  11352. + }
  11353. +
  11354. + err = 0;
  11355. + au_set_fbstart(file, bstart);
  11356. + au_set_h_fptr(file, bstart, h_file);
  11357. + au_update_figen(file);
  11358. + /* todo: necessary? */
  11359. + /* file->f_ra = h_file->f_ra; */
  11360. +
  11361. +out:
  11362. + if (h_file_tmp)
  11363. + fput(h_file_tmp);
  11364. + return err;
  11365. +}
  11366. +
  11367. +/* ---------------------------------------------------------------------- */
  11368. +
  11369. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  11370. + struct dentry *hi_wh)
  11371. +{
  11372. + int err;
  11373. + aufs_bindex_t bstart;
  11374. + struct au_dinfo *dinfo;
  11375. + struct dentry *h_dentry;
  11376. + struct au_hdentry *hdp;
  11377. +
  11378. + dinfo = au_di(file->f_path.dentry);
  11379. + AuRwMustWriteLock(&dinfo->di_rwsem);
  11380. +
  11381. + bstart = dinfo->di_bstart;
  11382. + dinfo->di_bstart = btgt;
  11383. + hdp = dinfo->di_hdentry;
  11384. + h_dentry = hdp[0 + btgt].hd_dentry;
  11385. + hdp[0 + btgt].hd_dentry = hi_wh;
  11386. + err = au_reopen_nondir(file);
  11387. + hdp[0 + btgt].hd_dentry = h_dentry;
  11388. + dinfo->di_bstart = bstart;
  11389. +
  11390. + return err;
  11391. +}
  11392. +
  11393. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  11394. + aufs_bindex_t bcpup, struct au_pin *pin)
  11395. +{
  11396. + int err;
  11397. + struct inode *inode, *h_inode;
  11398. + struct dentry *h_dentry, *hi_wh;
  11399. + struct au_cp_generic cpg = {
  11400. + .dentry = file->f_path.dentry,
  11401. + .bdst = bcpup,
  11402. + .bsrc = -1,
  11403. + .len = len,
  11404. + .pin = pin
  11405. + };
  11406. +
  11407. + au_update_dbstart(cpg.dentry);
  11408. + inode = d_inode(cpg.dentry);
  11409. + h_inode = NULL;
  11410. + if (au_dbstart(cpg.dentry) <= bcpup
  11411. + && au_dbend(cpg.dentry) >= bcpup) {
  11412. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  11413. + if (h_dentry && d_is_positive(h_dentry))
  11414. + h_inode = d_inode(h_dentry);
  11415. + }
  11416. + hi_wh = au_hi_wh(inode, bcpup);
  11417. + if (!hi_wh && !h_inode)
  11418. + err = au_sio_cpup_wh(&cpg, file);
  11419. + else
  11420. + /* already copied-up after unlink */
  11421. + err = au_reopen_wh(file, bcpup, hi_wh);
  11422. +
  11423. + if (!err
  11424. + && (inode->i_nlink > 1
  11425. + || (inode->i_state & I_LINKABLE))
  11426. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  11427. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  11428. +
  11429. + return err;
  11430. +}
  11431. +
  11432. +/*
  11433. + * prepare the @file for writing.
  11434. + */
  11435. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  11436. +{
  11437. + int err;
  11438. + aufs_bindex_t dbstart;
  11439. + struct dentry *parent;
  11440. + struct inode *inode;
  11441. + struct super_block *sb;
  11442. + struct file *h_file;
  11443. + struct au_cp_generic cpg = {
  11444. + .dentry = file->f_path.dentry,
  11445. + .bdst = -1,
  11446. + .bsrc = -1,
  11447. + .len = len,
  11448. + .pin = pin,
  11449. + .flags = AuCpup_DTIME
  11450. + };
  11451. +
  11452. + sb = cpg.dentry->d_sb;
  11453. + inode = d_inode(cpg.dentry);
  11454. + cpg.bsrc = au_fbstart(file);
  11455. + err = au_test_ro(sb, cpg.bsrc, inode);
  11456. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  11457. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  11458. + /*flags*/0);
  11459. + goto out;
  11460. + }
  11461. +
  11462. + /* need to cpup or reopen */
  11463. + parent = dget_parent(cpg.dentry);
  11464. + di_write_lock_parent(parent);
  11465. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11466. + cpg.bdst = err;
  11467. + if (unlikely(err < 0))
  11468. + goto out_dgrade;
  11469. + err = 0;
  11470. +
  11471. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  11472. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  11473. + if (unlikely(err))
  11474. + goto out_dgrade;
  11475. + }
  11476. +
  11477. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11478. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11479. + if (unlikely(err))
  11480. + goto out_dgrade;
  11481. +
  11482. + dbstart = au_dbstart(cpg.dentry);
  11483. + if (dbstart <= cpg.bdst)
  11484. + cpg.bsrc = cpg.bdst;
  11485. +
  11486. + if (dbstart <= cpg.bdst /* just reopen */
  11487. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  11488. + ) {
  11489. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  11490. + if (IS_ERR(h_file))
  11491. + err = PTR_ERR(h_file);
  11492. + else {
  11493. + di_downgrade_lock(parent, AuLock_IR);
  11494. + if (dbstart > cpg.bdst)
  11495. + err = au_sio_cpup_simple(&cpg);
  11496. + if (!err)
  11497. + err = au_reopen_nondir(file);
  11498. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  11499. + }
  11500. + } else { /* copyup as wh and reopen */
  11501. + /*
  11502. + * since writable hfsplus branch is not supported,
  11503. + * h_open_pre/post() are unnecessary.
  11504. + */
  11505. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  11506. + di_downgrade_lock(parent, AuLock_IR);
  11507. + }
  11508. +
  11509. + if (!err) {
  11510. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  11511. + goto out_dput; /* success */
  11512. + }
  11513. + au_unpin(pin);
  11514. + goto out_unlock;
  11515. +
  11516. +out_dgrade:
  11517. + di_downgrade_lock(parent, AuLock_IR);
  11518. +out_unlock:
  11519. + di_read_unlock(parent, AuLock_IR);
  11520. +out_dput:
  11521. + dput(parent);
  11522. +out:
  11523. + return err;
  11524. +}
  11525. +
  11526. +/* ---------------------------------------------------------------------- */
  11527. +
  11528. +int au_do_flush(struct file *file, fl_owner_t id,
  11529. + int (*flush)(struct file *file, fl_owner_t id))
  11530. +{
  11531. + int err;
  11532. + struct super_block *sb;
  11533. + struct inode *inode;
  11534. +
  11535. + inode = file_inode(file);
  11536. + sb = inode->i_sb;
  11537. + si_noflush_read_lock(sb);
  11538. + fi_read_lock(file);
  11539. + ii_read_lock_child(inode);
  11540. +
  11541. + err = flush(file, id);
  11542. + au_cpup_attr_timesizes(inode);
  11543. +
  11544. + ii_read_unlock(inode);
  11545. + fi_read_unlock(file);
  11546. + si_read_unlock(sb);
  11547. + return err;
  11548. +}
  11549. +
  11550. +/* ---------------------------------------------------------------------- */
  11551. +
  11552. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  11553. +{
  11554. + int err;
  11555. + struct au_pin pin;
  11556. + struct au_finfo *finfo;
  11557. + struct dentry *parent, *hi_wh;
  11558. + struct inode *inode;
  11559. + struct super_block *sb;
  11560. + struct au_cp_generic cpg = {
  11561. + .dentry = file->f_path.dentry,
  11562. + .bdst = -1,
  11563. + .bsrc = -1,
  11564. + .len = -1,
  11565. + .pin = &pin,
  11566. + .flags = AuCpup_DTIME
  11567. + };
  11568. +
  11569. + FiMustWriteLock(file);
  11570. +
  11571. + err = 0;
  11572. + finfo = au_fi(file);
  11573. + sb = cpg.dentry->d_sb;
  11574. + inode = d_inode(cpg.dentry);
  11575. + cpg.bdst = au_ibstart(inode);
  11576. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  11577. + goto out;
  11578. +
  11579. + parent = dget_parent(cpg.dentry);
  11580. + if (au_test_ro(sb, cpg.bdst, inode)) {
  11581. + di_read_lock_parent(parent, !AuLock_IR);
  11582. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  11583. + cpg.bdst = err;
  11584. + di_read_unlock(parent, !AuLock_IR);
  11585. + if (unlikely(err < 0))
  11586. + goto out_parent;
  11587. + err = 0;
  11588. + }
  11589. +
  11590. + di_read_lock_parent(parent, AuLock_IR);
  11591. + hi_wh = au_hi_wh(inode, cpg.bdst);
  11592. + if (!S_ISDIR(inode->i_mode)
  11593. + && au_opt_test(au_mntflags(sb), PLINK)
  11594. + && au_plink_test(inode)
  11595. + && !d_unhashed(cpg.dentry)
  11596. + && cpg.bdst < au_dbstart(cpg.dentry)) {
  11597. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  11598. + if (unlikely(err))
  11599. + goto out_unlock;
  11600. +
  11601. + /* always superio. */
  11602. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  11603. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  11604. + if (!err) {
  11605. + err = au_sio_cpup_simple(&cpg);
  11606. + au_unpin(&pin);
  11607. + }
  11608. + } else if (hi_wh) {
  11609. + /* already copied-up after unlink */
  11610. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  11611. + *need_reopen = 0;
  11612. + }
  11613. +
  11614. +out_unlock:
  11615. + di_read_unlock(parent, AuLock_IR);
  11616. +out_parent:
  11617. + dput(parent);
  11618. +out:
  11619. + return err;
  11620. +}
  11621. +
  11622. +static void au_do_refresh_dir(struct file *file)
  11623. +{
  11624. + aufs_bindex_t bindex, bend, new_bindex, brid;
  11625. + struct au_hfile *p, tmp, *q;
  11626. + struct au_finfo *finfo;
  11627. + struct super_block *sb;
  11628. + struct au_fidir *fidir;
  11629. +
  11630. + FiMustWriteLock(file);
  11631. +
  11632. + sb = file->f_path.dentry->d_sb;
  11633. + finfo = au_fi(file);
  11634. + fidir = finfo->fi_hdir;
  11635. + AuDebugOn(!fidir);
  11636. + p = fidir->fd_hfile + finfo->fi_btop;
  11637. + brid = p->hf_br->br_id;
  11638. + bend = fidir->fd_bbot;
  11639. + for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
  11640. + if (!p->hf_file)
  11641. + continue;
  11642. +
  11643. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  11644. + if (new_bindex == bindex)
  11645. + continue;
  11646. + if (new_bindex < 0) {
  11647. + au_set_h_fptr(file, bindex, NULL);
  11648. + continue;
  11649. + }
  11650. +
  11651. + /* swap two lower inode, and loop again */
  11652. + q = fidir->fd_hfile + new_bindex;
  11653. + tmp = *q;
  11654. + *q = *p;
  11655. + *p = tmp;
  11656. + if (tmp.hf_file) {
  11657. + bindex--;
  11658. + p--;
  11659. + }
  11660. + }
  11661. +
  11662. + p = fidir->fd_hfile;
  11663. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  11664. + bend = au_sbend(sb);
  11665. + for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
  11666. + finfo->fi_btop++, p++)
  11667. + if (p->hf_file) {
  11668. + if (file_inode(p->hf_file))
  11669. + break;
  11670. + au_hfput(p, file);
  11671. + }
  11672. + } else {
  11673. + bend = au_br_index(sb, brid);
  11674. + for (finfo->fi_btop = 0; finfo->fi_btop < bend;
  11675. + finfo->fi_btop++, p++)
  11676. + if (p->hf_file)
  11677. + au_hfput(p, file);
  11678. + bend = au_sbend(sb);
  11679. + }
  11680. +
  11681. + p = fidir->fd_hfile + bend;
  11682. + for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
  11683. + fidir->fd_bbot--, p--)
  11684. + if (p->hf_file) {
  11685. + if (file_inode(p->hf_file))
  11686. + break;
  11687. + au_hfput(p, file);
  11688. + }
  11689. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  11690. +}
  11691. +
  11692. +/*
  11693. + * after branch manipulating, refresh the file.
  11694. + */
  11695. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  11696. +{
  11697. + int err, need_reopen;
  11698. + aufs_bindex_t bend, bindex;
  11699. + struct dentry *dentry;
  11700. + struct au_finfo *finfo;
  11701. + struct au_hfile *hfile;
  11702. +
  11703. + dentry = file->f_path.dentry;
  11704. + finfo = au_fi(file);
  11705. + if (!finfo->fi_hdir) {
  11706. + hfile = &finfo->fi_htop;
  11707. + AuDebugOn(!hfile->hf_file);
  11708. + bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
  11709. + AuDebugOn(bindex < 0);
  11710. + if (bindex != finfo->fi_btop)
  11711. + au_set_fbstart(file, bindex);
  11712. + } else {
  11713. + err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
  11714. + if (unlikely(err))
  11715. + goto out;
  11716. + au_do_refresh_dir(file);
  11717. + }
  11718. +
  11719. + err = 0;
  11720. + need_reopen = 1;
  11721. + if (!au_test_mmapped(file))
  11722. + err = au_file_refresh_by_inode(file, &need_reopen);
  11723. + if (!err && need_reopen && !d_unlinked(dentry))
  11724. + err = reopen(file);
  11725. + if (!err) {
  11726. + au_update_figen(file);
  11727. + goto out; /* success */
  11728. + }
  11729. +
  11730. + /* error, close all lower files */
  11731. + if (finfo->fi_hdir) {
  11732. + bend = au_fbend_dir(file);
  11733. + for (bindex = au_fbstart(file); bindex <= bend; bindex++)
  11734. + au_set_h_fptr(file, bindex, NULL);
  11735. + }
  11736. +
  11737. +out:
  11738. + return err;
  11739. +}
  11740. +
  11741. +/* common function to regular file and dir */
  11742. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  11743. + int wlock)
  11744. +{
  11745. + int err;
  11746. + unsigned int sigen, figen;
  11747. + aufs_bindex_t bstart;
  11748. + unsigned char pseudo_link;
  11749. + struct dentry *dentry;
  11750. + struct inode *inode;
  11751. +
  11752. + err = 0;
  11753. + dentry = file->f_path.dentry;
  11754. + inode = d_inode(dentry);
  11755. + sigen = au_sigen(dentry->d_sb);
  11756. + fi_write_lock(file);
  11757. + figen = au_figen(file);
  11758. + di_write_lock_child(dentry);
  11759. + bstart = au_dbstart(dentry);
  11760. + pseudo_link = (bstart != au_ibstart(inode));
  11761. + if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
  11762. + if (!wlock) {
  11763. + di_downgrade_lock(dentry, AuLock_IR);
  11764. + fi_downgrade_lock(file);
  11765. + }
  11766. + goto out; /* success */
  11767. + }
  11768. +
  11769. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  11770. + if (au_digen_test(dentry, sigen)) {
  11771. + err = au_reval_dpath(dentry, sigen);
  11772. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  11773. + }
  11774. +
  11775. + if (!err)
  11776. + err = refresh_file(file, reopen);
  11777. + if (!err) {
  11778. + if (!wlock) {
  11779. + di_downgrade_lock(dentry, AuLock_IR);
  11780. + fi_downgrade_lock(file);
  11781. + }
  11782. + } else {
  11783. + di_write_unlock(dentry);
  11784. + fi_write_unlock(file);
  11785. + }
  11786. +
  11787. +out:
  11788. + return err;
  11789. +}
  11790. +
  11791. +/* ---------------------------------------------------------------------- */
  11792. +
  11793. +/* cf. aufs_nopage() */
  11794. +/* for madvise(2) */
  11795. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  11796. +{
  11797. + unlock_page(page);
  11798. + return 0;
  11799. +}
  11800. +
  11801. +/* it will never be called, but necessary to support O_DIRECT */
  11802. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  11803. + loff_t offset)
  11804. +{ BUG(); return 0; }
  11805. +
  11806. +/* they will never be called. */
  11807. +#ifdef CONFIG_AUFS_DEBUG
  11808. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  11809. + loff_t pos, unsigned len, unsigned flags,
  11810. + struct page **pagep, void **fsdata)
  11811. +{ AuUnsupport(); return 0; }
  11812. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  11813. + loff_t pos, unsigned len, unsigned copied,
  11814. + struct page *page, void *fsdata)
  11815. +{ AuUnsupport(); return 0; }
  11816. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  11817. +{ AuUnsupport(); return 0; }
  11818. +
  11819. +static int aufs_set_page_dirty(struct page *page)
  11820. +{ AuUnsupport(); return 0; }
  11821. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  11822. + unsigned int length)
  11823. +{ AuUnsupport(); }
  11824. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  11825. +{ AuUnsupport(); return 0; }
  11826. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  11827. + struct page *page, enum migrate_mode mode)
  11828. +{ AuUnsupport(); return 0; }
  11829. +static int aufs_launder_page(struct page *page)
  11830. +{ AuUnsupport(); return 0; }
  11831. +static int aufs_is_partially_uptodate(struct page *page,
  11832. + unsigned long from,
  11833. + unsigned long count)
  11834. +{ AuUnsupport(); return 0; }
  11835. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  11836. + bool *writeback)
  11837. +{ AuUnsupport(); }
  11838. +static int aufs_error_remove_page(struct address_space *mapping,
  11839. + struct page *page)
  11840. +{ AuUnsupport(); return 0; }
  11841. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  11842. + sector_t *span)
  11843. +{ AuUnsupport(); return 0; }
  11844. +static void aufs_swap_deactivate(struct file *file)
  11845. +{ AuUnsupport(); }
  11846. +#endif /* CONFIG_AUFS_DEBUG */
  11847. +
  11848. +const struct address_space_operations aufs_aop = {
  11849. + .readpage = aufs_readpage,
  11850. + .direct_IO = aufs_direct_IO,
  11851. +#ifdef CONFIG_AUFS_DEBUG
  11852. + .writepage = aufs_writepage,
  11853. + /* no writepages, because of writepage */
  11854. + .set_page_dirty = aufs_set_page_dirty,
  11855. + /* no readpages, because of readpage */
  11856. + .write_begin = aufs_write_begin,
  11857. + .write_end = aufs_write_end,
  11858. + /* no bmap, no block device */
  11859. + .invalidatepage = aufs_invalidatepage,
  11860. + .releasepage = aufs_releasepage,
  11861. + .migratepage = aufs_migratepage,
  11862. + .launder_page = aufs_launder_page,
  11863. + .is_partially_uptodate = aufs_is_partially_uptodate,
  11864. + .is_dirty_writeback = aufs_is_dirty_writeback,
  11865. + .error_remove_page = aufs_error_remove_page,
  11866. + .swap_activate = aufs_swap_activate,
  11867. + .swap_deactivate = aufs_swap_deactivate
  11868. +#endif /* CONFIG_AUFS_DEBUG */
  11869. +};
  11870. diff -Nur linux-4.1.10.orig/fs/aufs/file.h linux-4.1.10/fs/aufs/file.h
  11871. --- linux-4.1.10.orig/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
  11872. +++ linux-4.1.10/fs/aufs/file.h 2015-10-22 21:35:53.000000000 +0200
  11873. @@ -0,0 +1,291 @@
  11874. +/*
  11875. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  11876. + *
  11877. + * This program, aufs is free software; you can redistribute it and/or modify
  11878. + * it under the terms of the GNU General Public License as published by
  11879. + * the Free Software Foundation; either version 2 of the License, or
  11880. + * (at your option) any later version.
  11881. + *
  11882. + * This program is distributed in the hope that it will be useful,
  11883. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11884. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11885. + * GNU General Public License for more details.
  11886. + *
  11887. + * You should have received a copy of the GNU General Public License
  11888. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11889. + */
  11890. +
  11891. +/*
  11892. + * file operations
  11893. + */
  11894. +
  11895. +#ifndef __AUFS_FILE_H__
  11896. +#define __AUFS_FILE_H__
  11897. +
  11898. +#ifdef __KERNEL__
  11899. +
  11900. +#include <linux/file.h>
  11901. +#include <linux/fs.h>
  11902. +#include <linux/poll.h>
  11903. +#include "rwsem.h"
  11904. +
  11905. +struct au_branch;
  11906. +struct au_hfile {
  11907. + struct file *hf_file;
  11908. + struct au_branch *hf_br;
  11909. +};
  11910. +
  11911. +struct au_vdir;
  11912. +struct au_fidir {
  11913. + aufs_bindex_t fd_bbot;
  11914. + aufs_bindex_t fd_nent;
  11915. + struct au_vdir *fd_vdir_cache;
  11916. + struct au_hfile fd_hfile[];
  11917. +};
  11918. +
  11919. +static inline int au_fidir_sz(int nent)
  11920. +{
  11921. + AuDebugOn(nent < 0);
  11922. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  11923. +}
  11924. +
  11925. +struct au_finfo {
  11926. + atomic_t fi_generation;
  11927. +
  11928. + struct au_rwsem fi_rwsem;
  11929. + aufs_bindex_t fi_btop;
  11930. +
  11931. + /* do not union them */
  11932. + struct { /* for non-dir */
  11933. + struct au_hfile fi_htop;
  11934. + atomic_t fi_mmapped;
  11935. + };
  11936. + struct au_fidir *fi_hdir; /* for dir only */
  11937. +
  11938. + struct hlist_node fi_hlist;
  11939. + struct file *fi_file; /* very ugly */
  11940. +} ____cacheline_aligned_in_smp;
  11941. +
  11942. +/* ---------------------------------------------------------------------- */
  11943. +
  11944. +/* file.c */
  11945. +extern const struct address_space_operations aufs_aop;
  11946. +unsigned int au_file_roflags(unsigned int flags);
  11947. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  11948. + struct file *file, int force_wr);
  11949. +struct au_do_open_args {
  11950. + int no_lock;
  11951. + int (*open)(struct file *file, int flags,
  11952. + struct file *h_file);
  11953. + struct au_fidir *fidir;
  11954. + struct file *h_file;
  11955. +};
  11956. +int au_do_open(struct file *file, struct au_do_open_args *args);
  11957. +int au_reopen_nondir(struct file *file);
  11958. +struct au_pin;
  11959. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  11960. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  11961. + int wlock);
  11962. +int au_do_flush(struct file *file, fl_owner_t id,
  11963. + int (*flush)(struct file *file, fl_owner_t id));
  11964. +
  11965. +/* poll.c */
  11966. +#ifdef CONFIG_AUFS_POLL
  11967. +unsigned int aufs_poll(struct file *file, poll_table *wait);
  11968. +#endif
  11969. +
  11970. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  11971. +/* hfsplus.c */
  11972. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  11973. + int force_wr);
  11974. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  11975. + struct file *h_file);
  11976. +#else
  11977. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  11978. + aufs_bindex_t bindex, int force_wr)
  11979. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  11980. + struct file *h_file);
  11981. +#endif
  11982. +
  11983. +/* f_op.c */
  11984. +extern const struct file_operations aufs_file_fop;
  11985. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  11986. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  11987. +struct file *au_read_pre(struct file *file, int keep_fi);
  11988. +
  11989. +/* finfo.c */
  11990. +void au_hfput(struct au_hfile *hf, struct file *file);
  11991. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  11992. + struct file *h_file);
  11993. +
  11994. +void au_update_figen(struct file *file);
  11995. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  11996. +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
  11997. +
  11998. +void au_fi_init_once(void *_fi);
  11999. +void au_finfo_fin(struct file *file);
  12000. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  12001. +
  12002. +/* ioctl.c */
  12003. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  12004. +#ifdef CONFIG_COMPAT
  12005. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  12006. + unsigned long arg);
  12007. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  12008. + unsigned long arg);
  12009. +#endif
  12010. +
  12011. +/* ---------------------------------------------------------------------- */
  12012. +
  12013. +static inline struct au_finfo *au_fi(struct file *file)
  12014. +{
  12015. + return file->private_data;
  12016. +}
  12017. +
  12018. +/* ---------------------------------------------------------------------- */
  12019. +
  12020. +/*
  12021. + * fi_read_lock, fi_write_lock,
  12022. + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
  12023. + */
  12024. +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
  12025. +
  12026. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  12027. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  12028. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  12029. +
  12030. +/* ---------------------------------------------------------------------- */
  12031. +
  12032. +/* todo: hard/soft set? */
  12033. +static inline aufs_bindex_t au_fbstart(struct file *file)
  12034. +{
  12035. + FiMustAnyLock(file);
  12036. + return au_fi(file)->fi_btop;
  12037. +}
  12038. +
  12039. +static inline aufs_bindex_t au_fbend_dir(struct file *file)
  12040. +{
  12041. + FiMustAnyLock(file);
  12042. + AuDebugOn(!au_fi(file)->fi_hdir);
  12043. + return au_fi(file)->fi_hdir->fd_bbot;
  12044. +}
  12045. +
  12046. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  12047. +{
  12048. + FiMustAnyLock(file);
  12049. + AuDebugOn(!au_fi(file)->fi_hdir);
  12050. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  12051. +}
  12052. +
  12053. +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
  12054. +{
  12055. + FiMustWriteLock(file);
  12056. + au_fi(file)->fi_btop = bindex;
  12057. +}
  12058. +
  12059. +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
  12060. +{
  12061. + FiMustWriteLock(file);
  12062. + AuDebugOn(!au_fi(file)->fi_hdir);
  12063. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  12064. +}
  12065. +
  12066. +static inline void au_set_fvdir_cache(struct file *file,
  12067. + struct au_vdir *vdir_cache)
  12068. +{
  12069. + FiMustWriteLock(file);
  12070. + AuDebugOn(!au_fi(file)->fi_hdir);
  12071. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  12072. +}
  12073. +
  12074. +static inline struct file *au_hf_top(struct file *file)
  12075. +{
  12076. + FiMustAnyLock(file);
  12077. + AuDebugOn(au_fi(file)->fi_hdir);
  12078. + return au_fi(file)->fi_htop.hf_file;
  12079. +}
  12080. +
  12081. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  12082. +{
  12083. + FiMustAnyLock(file);
  12084. + AuDebugOn(!au_fi(file)->fi_hdir);
  12085. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  12086. +}
  12087. +
  12088. +/* todo: memory barrier? */
  12089. +static inline unsigned int au_figen(struct file *f)
  12090. +{
  12091. + return atomic_read(&au_fi(f)->fi_generation);
  12092. +}
  12093. +
  12094. +static inline void au_set_mmapped(struct file *f)
  12095. +{
  12096. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  12097. + return;
  12098. + pr_warn("fi_mmapped wrapped around\n");
  12099. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  12100. + ;
  12101. +}
  12102. +
  12103. +static inline void au_unset_mmapped(struct file *f)
  12104. +{
  12105. + atomic_dec(&au_fi(f)->fi_mmapped);
  12106. +}
  12107. +
  12108. +static inline int au_test_mmapped(struct file *f)
  12109. +{
  12110. + return atomic_read(&au_fi(f)->fi_mmapped);
  12111. +}
  12112. +
  12113. +/* customize vma->vm_file */
  12114. +
  12115. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  12116. + struct file *file)
  12117. +{
  12118. + struct file *f;
  12119. +
  12120. + f = vma->vm_file;
  12121. + get_file(file);
  12122. + vma->vm_file = file;
  12123. + fput(f);
  12124. +}
  12125. +
  12126. +#ifdef CONFIG_MMU
  12127. +#define AuDbgVmRegion(file, vma) do {} while (0)
  12128. +
  12129. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  12130. + struct file *file)
  12131. +{
  12132. + au_do_vm_file_reset(vma, file);
  12133. +}
  12134. +#else
  12135. +#define AuDbgVmRegion(file, vma) \
  12136. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  12137. +
  12138. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  12139. + struct file *file)
  12140. +{
  12141. + struct file *f;
  12142. +
  12143. + au_do_vm_file_reset(vma, file);
  12144. + f = vma->vm_region->vm_file;
  12145. + get_file(file);
  12146. + vma->vm_region->vm_file = file;
  12147. + fput(f);
  12148. +}
  12149. +#endif /* CONFIG_MMU */
  12150. +
  12151. +/* handle vma->vm_prfile */
  12152. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  12153. + struct file *file)
  12154. +{
  12155. + get_file(file);
  12156. + vma->vm_prfile = file;
  12157. +#ifndef CONFIG_MMU
  12158. + get_file(file);
  12159. + vma->vm_region->vm_prfile = file;
  12160. +#endif
  12161. +}
  12162. +
  12163. +#endif /* __KERNEL__ */
  12164. +#endif /* __AUFS_FILE_H__ */
  12165. diff -Nur linux-4.1.10.orig/fs/aufs/finfo.c linux-4.1.10/fs/aufs/finfo.c
  12166. --- linux-4.1.10.orig/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
  12167. +++ linux-4.1.10/fs/aufs/finfo.c 2015-10-22 21:35:53.000000000 +0200
  12168. @@ -0,0 +1,157 @@
  12169. +/*
  12170. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  12171. + *
  12172. + * This program, aufs is free software; you can redistribute it and/or modify
  12173. + * it under the terms of the GNU General Public License as published by
  12174. + * the Free Software Foundation; either version 2 of the License, or
  12175. + * (at your option) any later version.
  12176. + *
  12177. + * This program is distributed in the hope that it will be useful,
  12178. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12179. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12180. + * GNU General Public License for more details.
  12181. + *
  12182. + * You should have received a copy of the GNU General Public License
  12183. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12184. + */
  12185. +
  12186. +/*
  12187. + * file private data
  12188. + */
  12189. +
  12190. +#include "aufs.h"
  12191. +
  12192. +void au_hfput(struct au_hfile *hf, struct file *file)
  12193. +{
  12194. + /* todo: direct access f_flags */
  12195. + if (vfsub_file_flags(file) & __FMODE_EXEC)
  12196. + allow_write_access(hf->hf_file);
  12197. + fput(hf->hf_file);
  12198. + hf->hf_file = NULL;
  12199. + atomic_dec(&hf->hf_br->br_count);
  12200. + hf->hf_br = NULL;
  12201. +}
  12202. +
  12203. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  12204. +{
  12205. + struct au_finfo *finfo = au_fi(file);
  12206. + struct au_hfile *hf;
  12207. + struct au_fidir *fidir;
  12208. +
  12209. + fidir = finfo->fi_hdir;
  12210. + if (!fidir) {
  12211. + AuDebugOn(finfo->fi_btop != bindex);
  12212. + hf = &finfo->fi_htop;
  12213. + } else
  12214. + hf = fidir->fd_hfile + bindex;
  12215. +
  12216. + if (hf && hf->hf_file)
  12217. + au_hfput(hf, file);
  12218. + if (val) {
  12219. + FiMustWriteLock(file);
  12220. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  12221. + hf->hf_file = val;
  12222. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  12223. + }
  12224. +}
  12225. +
  12226. +void au_update_figen(struct file *file)
  12227. +{
  12228. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  12229. + /* smp_mb(); */ /* atomic_set */
  12230. +}
  12231. +
  12232. +/* ---------------------------------------------------------------------- */
  12233. +
  12234. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  12235. +{
  12236. + struct au_fidir *fidir;
  12237. + int nbr;
  12238. +
  12239. + nbr = au_sbend(sb) + 1;
  12240. + if (nbr < 2)
  12241. + nbr = 2; /* initial allocate for 2 branches */
  12242. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  12243. + if (fidir) {
  12244. + fidir->fd_bbot = -1;
  12245. + fidir->fd_nent = nbr;
  12246. + fidir->fd_vdir_cache = NULL;
  12247. + }
  12248. +
  12249. + return fidir;
  12250. +}
  12251. +
  12252. +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
  12253. +{
  12254. + int err;
  12255. + struct au_fidir *fidir, *p;
  12256. +
  12257. + AuRwMustWriteLock(&finfo->fi_rwsem);
  12258. + fidir = finfo->fi_hdir;
  12259. + AuDebugOn(!fidir);
  12260. +
  12261. + err = -ENOMEM;
  12262. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  12263. + GFP_NOFS);
  12264. + if (p) {
  12265. + p->fd_nent = nbr;
  12266. + finfo->fi_hdir = p;
  12267. + err = 0;
  12268. + }
  12269. +
  12270. + return err;
  12271. +}
  12272. +
  12273. +/* ---------------------------------------------------------------------- */
  12274. +
  12275. +void au_finfo_fin(struct file *file)
  12276. +{
  12277. + struct au_finfo *finfo;
  12278. +
  12279. + au_nfiles_dec(file->f_path.dentry->d_sb);
  12280. +
  12281. + finfo = au_fi(file);
  12282. + AuDebugOn(finfo->fi_hdir);
  12283. + AuRwDestroy(&finfo->fi_rwsem);
  12284. + au_cache_free_finfo(finfo);
  12285. +}
  12286. +
  12287. +void au_fi_init_once(void *_finfo)
  12288. +{
  12289. + struct au_finfo *finfo = _finfo;
  12290. + static struct lock_class_key aufs_fi;
  12291. +
  12292. + au_rw_init(&finfo->fi_rwsem);
  12293. + au_rw_class(&finfo->fi_rwsem, &aufs_fi);
  12294. +}
  12295. +
  12296. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  12297. +{
  12298. + int err;
  12299. + struct au_finfo *finfo;
  12300. + struct dentry *dentry;
  12301. +
  12302. + err = -ENOMEM;
  12303. + dentry = file->f_path.dentry;
  12304. + finfo = au_cache_alloc_finfo();
  12305. + if (unlikely(!finfo))
  12306. + goto out;
  12307. +
  12308. + err = 0;
  12309. + au_nfiles_inc(dentry->d_sb);
  12310. + /* verbose coding for lock class name */
  12311. + if (!fidir)
  12312. + au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
  12313. + else
  12314. + au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
  12315. + au_rw_write_lock(&finfo->fi_rwsem);
  12316. + finfo->fi_btop = -1;
  12317. + finfo->fi_hdir = fidir;
  12318. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  12319. + /* smp_mb(); */ /* atomic_set */
  12320. +
  12321. + file->private_data = finfo;
  12322. +
  12323. +out:
  12324. + return err;
  12325. +}
  12326. diff -Nur linux-4.1.10.orig/fs/aufs/f_op.c linux-4.1.10/fs/aufs/f_op.c
  12327. --- linux-4.1.10.orig/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
  12328. +++ linux-4.1.10/fs/aufs/f_op.c 2015-10-22 21:35:53.000000000 +0200
  12329. @@ -0,0 +1,738 @@
  12330. +/*
  12331. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  12332. + *
  12333. + * This program, aufs is free software; you can redistribute it and/or modify
  12334. + * it under the terms of the GNU General Public License as published by
  12335. + * the Free Software Foundation; either version 2 of the License, or
  12336. + * (at your option) any later version.
  12337. + *
  12338. + * This program is distributed in the hope that it will be useful,
  12339. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12340. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12341. + * GNU General Public License for more details.
  12342. + *
  12343. + * You should have received a copy of the GNU General Public License
  12344. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12345. + */
  12346. +
  12347. +/*
  12348. + * file and vm operations
  12349. + */
  12350. +
  12351. +#include <linux/aio.h>
  12352. +#include <linux/fs_stack.h>
  12353. +#include <linux/mman.h>
  12354. +#include <linux/security.h>
  12355. +#include "aufs.h"
  12356. +
  12357. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  12358. +{
  12359. + int err;
  12360. + aufs_bindex_t bindex;
  12361. + struct dentry *dentry;
  12362. + struct au_finfo *finfo;
  12363. + struct inode *h_inode;
  12364. +
  12365. + FiMustWriteLock(file);
  12366. +
  12367. + err = 0;
  12368. + dentry = file->f_path.dentry;
  12369. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  12370. + finfo = au_fi(file);
  12371. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  12372. + atomic_set(&finfo->fi_mmapped, 0);
  12373. + bindex = au_dbstart(dentry);
  12374. + if (!h_file)
  12375. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  12376. + else
  12377. + get_file(h_file);
  12378. + if (IS_ERR(h_file))
  12379. + err = PTR_ERR(h_file);
  12380. + else {
  12381. + if ((flags & __O_TMPFILE)
  12382. + && !(flags & O_EXCL)) {
  12383. + h_inode = file_inode(h_file);
  12384. + spin_lock(&h_inode->i_lock);
  12385. + h_inode->i_state |= I_LINKABLE;
  12386. + spin_unlock(&h_inode->i_lock);
  12387. + }
  12388. + au_set_fbstart(file, bindex);
  12389. + au_set_h_fptr(file, bindex, h_file);
  12390. + au_update_figen(file);
  12391. + /* todo: necessary? */
  12392. + /* file->f_ra = h_file->f_ra; */
  12393. + }
  12394. +
  12395. + return err;
  12396. +}
  12397. +
  12398. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  12399. + struct file *file)
  12400. +{
  12401. + int err;
  12402. + struct super_block *sb;
  12403. + struct au_do_open_args args = {
  12404. + .open = au_do_open_nondir
  12405. + };
  12406. +
  12407. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  12408. + file, vfsub_file_flags(file), file->f_mode);
  12409. +
  12410. + sb = file->f_path.dentry->d_sb;
  12411. + si_read_lock(sb, AuLock_FLUSH);
  12412. + err = au_do_open(file, &args);
  12413. + si_read_unlock(sb);
  12414. + return err;
  12415. +}
  12416. +
  12417. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  12418. +{
  12419. + struct au_finfo *finfo;
  12420. + aufs_bindex_t bindex;
  12421. +
  12422. + finfo = au_fi(file);
  12423. + au_sphl_del(&finfo->fi_hlist,
  12424. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  12425. + bindex = finfo->fi_btop;
  12426. + if (bindex >= 0)
  12427. + au_set_h_fptr(file, bindex, NULL);
  12428. +
  12429. + au_finfo_fin(file);
  12430. + return 0;
  12431. +}
  12432. +
  12433. +/* ---------------------------------------------------------------------- */
  12434. +
  12435. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  12436. +{
  12437. + int err;
  12438. + struct file *h_file;
  12439. +
  12440. + err = 0;
  12441. + h_file = au_hf_top(file);
  12442. + if (h_file)
  12443. + err = vfsub_flush(h_file, id);
  12444. + return err;
  12445. +}
  12446. +
  12447. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  12448. +{
  12449. + return au_do_flush(file, id, au_do_flush_nondir);
  12450. +}
  12451. +
  12452. +/* ---------------------------------------------------------------------- */
  12453. +/*
  12454. + * read and write functions acquire [fdi]_rwsem once, but release before
  12455. + * mmap_sem. This is because to stop a race condition between mmap(2).
  12456. + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
  12457. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  12458. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  12459. + */
  12460. +
  12461. +/* Callers should call au_read_post() or fput() in the end */
  12462. +struct file *au_read_pre(struct file *file, int keep_fi)
  12463. +{
  12464. + struct file *h_file;
  12465. + int err;
  12466. +
  12467. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
  12468. + if (!err) {
  12469. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  12470. + h_file = au_hf_top(file);
  12471. + get_file(h_file);
  12472. + if (!keep_fi)
  12473. + fi_read_unlock(file);
  12474. + } else
  12475. + h_file = ERR_PTR(err);
  12476. +
  12477. + return h_file;
  12478. +}
  12479. +
  12480. +static void au_read_post(struct inode *inode, struct file *h_file)
  12481. +{
  12482. + /* update without lock, I don't think it a problem */
  12483. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  12484. + fput(h_file);
  12485. +}
  12486. +
  12487. +struct au_write_pre {
  12488. + blkcnt_t blks;
  12489. + aufs_bindex_t bstart;
  12490. +};
  12491. +
  12492. +/*
  12493. + * return with iinfo is write-locked
  12494. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  12495. + * end
  12496. + */
  12497. +static struct file *au_write_pre(struct file *file, int do_ready,
  12498. + struct au_write_pre *wpre)
  12499. +{
  12500. + struct file *h_file;
  12501. + struct dentry *dentry;
  12502. + int err;
  12503. + struct au_pin pin;
  12504. +
  12505. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  12506. + h_file = ERR_PTR(err);
  12507. + if (unlikely(err))
  12508. + goto out;
  12509. +
  12510. + dentry = file->f_path.dentry;
  12511. + if (do_ready) {
  12512. + err = au_ready_to_write(file, -1, &pin);
  12513. + if (unlikely(err)) {
  12514. + h_file = ERR_PTR(err);
  12515. + di_write_unlock(dentry);
  12516. + goto out_fi;
  12517. + }
  12518. + }
  12519. +
  12520. + di_downgrade_lock(dentry, /*flags*/0);
  12521. + if (wpre)
  12522. + wpre->bstart = au_fbstart(file);
  12523. + h_file = au_hf_top(file);
  12524. + get_file(h_file);
  12525. + if (wpre)
  12526. + wpre->blks = file_inode(h_file)->i_blocks;
  12527. + if (do_ready)
  12528. + au_unpin(&pin);
  12529. + di_read_unlock(dentry, /*flags*/0);
  12530. +
  12531. +out_fi:
  12532. + fi_write_unlock(file);
  12533. +out:
  12534. + return h_file;
  12535. +}
  12536. +
  12537. +static void au_write_post(struct inode *inode, struct file *h_file,
  12538. + struct au_write_pre *wpre, ssize_t written)
  12539. +{
  12540. + struct inode *h_inode;
  12541. +
  12542. + au_cpup_attr_timesizes(inode);
  12543. + AuDebugOn(au_ibstart(inode) != wpre->bstart);
  12544. + h_inode = file_inode(h_file);
  12545. + inode->i_mode = h_inode->i_mode;
  12546. + ii_write_unlock(inode);
  12547. + fput(h_file);
  12548. +
  12549. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  12550. + if (written > 0)
  12551. + au_fhsm_wrote(inode->i_sb, wpre->bstart,
  12552. + /*force*/h_inode->i_blocks > wpre->blks);
  12553. +}
  12554. +
  12555. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  12556. + loff_t *ppos)
  12557. +{
  12558. + ssize_t err;
  12559. + struct inode *inode;
  12560. + struct file *h_file;
  12561. + struct super_block *sb;
  12562. +
  12563. + inode = file_inode(file);
  12564. + sb = inode->i_sb;
  12565. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12566. +
  12567. + h_file = au_read_pre(file, /*keep_fi*/0);
  12568. + err = PTR_ERR(h_file);
  12569. + if (IS_ERR(h_file))
  12570. + goto out;
  12571. +
  12572. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  12573. + err = vfsub_read_u(h_file, buf, count, ppos);
  12574. + /* todo: necessary? */
  12575. + /* file->f_ra = h_file->f_ra; */
  12576. + au_read_post(inode, h_file);
  12577. +
  12578. +out:
  12579. + si_read_unlock(sb);
  12580. + return err;
  12581. +}
  12582. +
  12583. +/*
  12584. + * todo: very ugly
  12585. + * it locks both of i_mutex and si_rwsem for read in safe.
  12586. + * if the plink maintenance mode continues forever (that is the problem),
  12587. + * may loop forever.
  12588. + */
  12589. +static void au_mtx_and_read_lock(struct inode *inode)
  12590. +{
  12591. + int err;
  12592. + struct super_block *sb = inode->i_sb;
  12593. +
  12594. + while (1) {
  12595. + mutex_lock(&inode->i_mutex);
  12596. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  12597. + if (!err)
  12598. + break;
  12599. + mutex_unlock(&inode->i_mutex);
  12600. + si_read_lock(sb, AuLock_NOPLMW);
  12601. + si_read_unlock(sb);
  12602. + }
  12603. +}
  12604. +
  12605. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  12606. + size_t count, loff_t *ppos)
  12607. +{
  12608. + ssize_t err;
  12609. + struct au_write_pre wpre;
  12610. + struct inode *inode;
  12611. + struct file *h_file;
  12612. + char __user *buf = (char __user *)ubuf;
  12613. +
  12614. + inode = file_inode(file);
  12615. + au_mtx_and_read_lock(inode);
  12616. +
  12617. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12618. + err = PTR_ERR(h_file);
  12619. + if (IS_ERR(h_file))
  12620. + goto out;
  12621. +
  12622. + err = vfsub_write_u(h_file, buf, count, ppos);
  12623. + au_write_post(inode, h_file, &wpre, err);
  12624. +
  12625. +out:
  12626. + si_read_unlock(inode->i_sb);
  12627. + mutex_unlock(&inode->i_mutex);
  12628. + return err;
  12629. +}
  12630. +
  12631. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  12632. + struct iov_iter *iov_iter)
  12633. +{
  12634. + ssize_t err;
  12635. + struct file *file;
  12636. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  12637. +
  12638. + err = security_file_permission(h_file, rw);
  12639. + if (unlikely(err))
  12640. + goto out;
  12641. +
  12642. + err = -ENOSYS;
  12643. + iter = NULL;
  12644. + if (rw == MAY_READ)
  12645. + iter = h_file->f_op->read_iter;
  12646. + else if (rw == MAY_WRITE)
  12647. + iter = h_file->f_op->write_iter;
  12648. +
  12649. + file = kio->ki_filp;
  12650. + kio->ki_filp = h_file;
  12651. + if (iter) {
  12652. + lockdep_off();
  12653. + err = iter(kio, iov_iter);
  12654. + lockdep_on();
  12655. + } else
  12656. + /* currently there is no such fs */
  12657. + WARN_ON_ONCE(1);
  12658. + kio->ki_filp = file;
  12659. +
  12660. +out:
  12661. + return err;
  12662. +}
  12663. +
  12664. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12665. +{
  12666. + ssize_t err;
  12667. + struct file *file, *h_file;
  12668. + struct inode *inode;
  12669. + struct super_block *sb;
  12670. +
  12671. + file = kio->ki_filp;
  12672. + inode = file_inode(file);
  12673. + sb = inode->i_sb;
  12674. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12675. +
  12676. + h_file = au_read_pre(file, /*keep_fi*/0);
  12677. + err = PTR_ERR(h_file);
  12678. + if (IS_ERR(h_file))
  12679. + goto out;
  12680. +
  12681. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  12682. + /* todo: necessary? */
  12683. + /* file->f_ra = h_file->f_ra; */
  12684. + au_read_post(inode, h_file);
  12685. +
  12686. +out:
  12687. + si_read_unlock(sb);
  12688. + return err;
  12689. +}
  12690. +
  12691. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  12692. +{
  12693. + ssize_t err;
  12694. + struct au_write_pre wpre;
  12695. + struct inode *inode;
  12696. + struct file *file, *h_file;
  12697. +
  12698. + file = kio->ki_filp;
  12699. + inode = file_inode(file);
  12700. + au_mtx_and_read_lock(inode);
  12701. +
  12702. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12703. + err = PTR_ERR(h_file);
  12704. + if (IS_ERR(h_file))
  12705. + goto out;
  12706. +
  12707. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  12708. + au_write_post(inode, h_file, &wpre, err);
  12709. +
  12710. +out:
  12711. + si_read_unlock(inode->i_sb);
  12712. + mutex_unlock(&inode->i_mutex);
  12713. + return err;
  12714. +}
  12715. +
  12716. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  12717. + struct pipe_inode_info *pipe, size_t len,
  12718. + unsigned int flags)
  12719. +{
  12720. + ssize_t err;
  12721. + struct file *h_file;
  12722. + struct inode *inode;
  12723. + struct super_block *sb;
  12724. +
  12725. + inode = file_inode(file);
  12726. + sb = inode->i_sb;
  12727. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12728. +
  12729. + h_file = au_read_pre(file, /*keep_fi*/1);
  12730. + err = PTR_ERR(h_file);
  12731. + if (IS_ERR(h_file))
  12732. + goto out;
  12733. +
  12734. + if (au_test_loopback_kthread()) {
  12735. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  12736. + if (file->f_mapping != h_file->f_mapping) {
  12737. + file->f_mapping = h_file->f_mapping;
  12738. + smp_mb(); /* unnecessary? */
  12739. + }
  12740. + }
  12741. + fi_read_unlock(file);
  12742. +
  12743. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  12744. + /* todo: necessasry? */
  12745. + /* file->f_ra = h_file->f_ra; */
  12746. + au_read_post(inode, h_file);
  12747. +
  12748. +out:
  12749. + si_read_unlock(sb);
  12750. + return err;
  12751. +}
  12752. +
  12753. +static ssize_t
  12754. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  12755. + size_t len, unsigned int flags)
  12756. +{
  12757. + ssize_t err;
  12758. + struct au_write_pre wpre;
  12759. + struct inode *inode;
  12760. + struct file *h_file;
  12761. +
  12762. + inode = file_inode(file);
  12763. + au_mtx_and_read_lock(inode);
  12764. +
  12765. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12766. + err = PTR_ERR(h_file);
  12767. + if (IS_ERR(h_file))
  12768. + goto out;
  12769. +
  12770. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  12771. + au_write_post(inode, h_file, &wpre, err);
  12772. +
  12773. +out:
  12774. + si_read_unlock(inode->i_sb);
  12775. + mutex_unlock(&inode->i_mutex);
  12776. + return err;
  12777. +}
  12778. +
  12779. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  12780. + loff_t len)
  12781. +{
  12782. + long err;
  12783. + struct au_write_pre wpre;
  12784. + struct inode *inode;
  12785. + struct file *h_file;
  12786. +
  12787. + inode = file_inode(file);
  12788. + au_mtx_and_read_lock(inode);
  12789. +
  12790. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12791. + err = PTR_ERR(h_file);
  12792. + if (IS_ERR(h_file))
  12793. + goto out;
  12794. +
  12795. + lockdep_off();
  12796. + err = vfs_fallocate(h_file, mode, offset, len);
  12797. + lockdep_on();
  12798. + au_write_post(inode, h_file, &wpre, /*written*/1);
  12799. +
  12800. +out:
  12801. + si_read_unlock(inode->i_sb);
  12802. + mutex_unlock(&inode->i_mutex);
  12803. + return err;
  12804. +}
  12805. +
  12806. +/* ---------------------------------------------------------------------- */
  12807. +
  12808. +/*
  12809. + * The locking order around current->mmap_sem.
  12810. + * - in most and regular cases
  12811. + * file I/O syscall -- aufs_read() or something
  12812. + * -- si_rwsem for read -- mmap_sem
  12813. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  12814. + * - in mmap case
  12815. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  12816. + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
  12817. + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
  12818. + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  12819. + * It means that when aufs acquires si_rwsem for write, the process should never
  12820. + * acquire mmap_sem.
  12821. + *
  12822. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  12823. + * problem either since any directory is not able to be mmap-ed.
  12824. + * The similar scenario is applied to aufs_readlink() too.
  12825. + */
  12826. +
  12827. +#if 0 /* stop calling security_file_mmap() */
  12828. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  12829. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  12830. +
  12831. +static unsigned long au_arch_prot_conv(unsigned long flags)
  12832. +{
  12833. + /* currently ppc64 only */
  12834. +#ifdef CONFIG_PPC64
  12835. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  12836. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  12837. + return AuConv_VM_PROT(flags, SAO);
  12838. +#else
  12839. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  12840. + return 0;
  12841. +#endif
  12842. +}
  12843. +
  12844. +static unsigned long au_prot_conv(unsigned long flags)
  12845. +{
  12846. + return AuConv_VM_PROT(flags, READ)
  12847. + | AuConv_VM_PROT(flags, WRITE)
  12848. + | AuConv_VM_PROT(flags, EXEC)
  12849. + | au_arch_prot_conv(flags);
  12850. +}
  12851. +
  12852. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  12853. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  12854. +
  12855. +static unsigned long au_flag_conv(unsigned long flags)
  12856. +{
  12857. + return AuConv_VM_MAP(flags, GROWSDOWN)
  12858. + | AuConv_VM_MAP(flags, DENYWRITE)
  12859. + | AuConv_VM_MAP(flags, LOCKED);
  12860. +}
  12861. +#endif
  12862. +
  12863. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  12864. +{
  12865. + int err;
  12866. + const unsigned char wlock
  12867. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  12868. + struct super_block *sb;
  12869. + struct file *h_file;
  12870. + struct inode *inode;
  12871. +
  12872. + AuDbgVmRegion(file, vma);
  12873. +
  12874. + inode = file_inode(file);
  12875. + sb = inode->i_sb;
  12876. + lockdep_off();
  12877. + si_read_lock(sb, AuLock_NOPLMW);
  12878. +
  12879. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  12880. + lockdep_on();
  12881. + err = PTR_ERR(h_file);
  12882. + if (IS_ERR(h_file))
  12883. + goto out;
  12884. +
  12885. + err = 0;
  12886. + au_set_mmapped(file);
  12887. + au_vm_file_reset(vma, h_file);
  12888. + /*
  12889. + * we cannot call security_mmap_file() here since it may acquire
  12890. + * mmap_sem or i_mutex.
  12891. + *
  12892. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  12893. + * au_flag_conv(vma->vm_flags));
  12894. + */
  12895. + if (!err)
  12896. + err = h_file->f_op->mmap(h_file, vma);
  12897. + if (!err) {
  12898. + au_vm_prfile_set(vma, file);
  12899. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  12900. + goto out_fput; /* success */
  12901. + }
  12902. + au_unset_mmapped(file);
  12903. + au_vm_file_reset(vma, file);
  12904. +
  12905. +out_fput:
  12906. + lockdep_off();
  12907. + ii_write_unlock(inode);
  12908. + lockdep_on();
  12909. + fput(h_file);
  12910. +out:
  12911. + lockdep_off();
  12912. + si_read_unlock(sb);
  12913. + lockdep_on();
  12914. + AuTraceErr(err);
  12915. + return err;
  12916. +}
  12917. +
  12918. +/* ---------------------------------------------------------------------- */
  12919. +
  12920. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  12921. + int datasync)
  12922. +{
  12923. + int err;
  12924. + struct au_write_pre wpre;
  12925. + struct inode *inode;
  12926. + struct file *h_file;
  12927. +
  12928. + err = 0; /* -EBADF; */ /* posix? */
  12929. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  12930. + goto out;
  12931. +
  12932. + inode = file_inode(file);
  12933. + au_mtx_and_read_lock(inode);
  12934. +
  12935. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12936. + err = PTR_ERR(h_file);
  12937. + if (IS_ERR(h_file))
  12938. + goto out_unlock;
  12939. +
  12940. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  12941. + au_write_post(inode, h_file, &wpre, /*written*/0);
  12942. +
  12943. +out_unlock:
  12944. + si_read_unlock(inode->i_sb);
  12945. + mutex_unlock(&inode->i_mutex);
  12946. +out:
  12947. + return err;
  12948. +}
  12949. +
  12950. +/* no one supports this operation, currently */
  12951. +#if 0
  12952. +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
  12953. +{
  12954. + int err;
  12955. + struct au_write_pre wpre;
  12956. + struct inode *inode;
  12957. + struct file *file, *h_file;
  12958. +
  12959. + err = 0; /* -EBADF; */ /* posix? */
  12960. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  12961. + goto out;
  12962. +
  12963. + file = kio->ki_filp;
  12964. + inode = file_inode(file);
  12965. + au_mtx_and_read_lock(inode);
  12966. +
  12967. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  12968. + err = PTR_ERR(h_file);
  12969. + if (IS_ERR(h_file))
  12970. + goto out_unlock;
  12971. +
  12972. + err = -ENOSYS;
  12973. + h_file = au_hf_top(file);
  12974. + if (h_file->f_op->aio_fsync) {
  12975. + struct mutex *h_mtx;
  12976. +
  12977. + h_mtx = &file_inode(h_file)->i_mutex;
  12978. + if (!is_sync_kiocb(kio)) {
  12979. + get_file(h_file);
  12980. + fput(file);
  12981. + }
  12982. + kio->ki_filp = h_file;
  12983. + err = h_file->f_op->aio_fsync(kio, datasync);
  12984. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  12985. + if (!err)
  12986. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
  12987. + /*ignore*/
  12988. + mutex_unlock(h_mtx);
  12989. + }
  12990. + au_write_post(inode, h_file, &wpre, /*written*/0);
  12991. +
  12992. +out_unlock:
  12993. + si_read_unlock(inode->sb);
  12994. + mutex_unlock(&inode->i_mutex);
  12995. +out:
  12996. + return err;
  12997. +}
  12998. +#endif
  12999. +
  13000. +static int aufs_fasync(int fd, struct file *file, int flag)
  13001. +{
  13002. + int err;
  13003. + struct file *h_file;
  13004. + struct super_block *sb;
  13005. +
  13006. + sb = file->f_path.dentry->d_sb;
  13007. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  13008. +
  13009. + h_file = au_read_pre(file, /*keep_fi*/0);
  13010. + err = PTR_ERR(h_file);
  13011. + if (IS_ERR(h_file))
  13012. + goto out;
  13013. +
  13014. + if (h_file->f_op->fasync)
  13015. + err = h_file->f_op->fasync(fd, h_file, flag);
  13016. + fput(h_file); /* instead of au_read_post() */
  13017. +
  13018. +out:
  13019. + si_read_unlock(sb);
  13020. + return err;
  13021. +}
  13022. +
  13023. +/* ---------------------------------------------------------------------- */
  13024. +
  13025. +/* no one supports this operation, currently */
  13026. +#if 0
  13027. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  13028. + size_t len, loff_t *pos, int more)
  13029. +{
  13030. +}
  13031. +#endif
  13032. +
  13033. +/* ---------------------------------------------------------------------- */
  13034. +
  13035. +const struct file_operations aufs_file_fop = {
  13036. + .owner = THIS_MODULE,
  13037. +
  13038. + .llseek = default_llseek,
  13039. +
  13040. + .read = aufs_read,
  13041. + .write = aufs_write,
  13042. + .read_iter = aufs_read_iter,
  13043. + .write_iter = aufs_write_iter,
  13044. +
  13045. +#ifdef CONFIG_AUFS_POLL
  13046. + .poll = aufs_poll,
  13047. +#endif
  13048. + .unlocked_ioctl = aufs_ioctl_nondir,
  13049. +#ifdef CONFIG_COMPAT
  13050. + .compat_ioctl = aufs_compat_ioctl_nondir,
  13051. +#endif
  13052. + .mmap = aufs_mmap,
  13053. + .open = aufs_open_nondir,
  13054. + .flush = aufs_flush_nondir,
  13055. + .release = aufs_release_nondir,
  13056. + .fsync = aufs_fsync_nondir,
  13057. + /* .aio_fsync = aufs_aio_fsync_nondir, */
  13058. + .fasync = aufs_fasync,
  13059. + /* .sendpage = aufs_sendpage, */
  13060. + .splice_write = aufs_splice_write,
  13061. + .splice_read = aufs_splice_read,
  13062. +#if 0
  13063. + .aio_splice_write = aufs_aio_splice_write,
  13064. + .aio_splice_read = aufs_aio_splice_read,
  13065. +#endif
  13066. + .fallocate = aufs_fallocate
  13067. +};
  13068. diff -Nur linux-4.1.10.orig/fs/aufs/fstype.h linux-4.1.10/fs/aufs/fstype.h
  13069. --- linux-4.1.10.orig/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
  13070. +++ linux-4.1.10/fs/aufs/fstype.h 2015-10-22 21:35:53.000000000 +0200
  13071. @@ -0,0 +1,400 @@
  13072. +/*
  13073. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13074. + *
  13075. + * This program, aufs is free software; you can redistribute it and/or modify
  13076. + * it under the terms of the GNU General Public License as published by
  13077. + * the Free Software Foundation; either version 2 of the License, or
  13078. + * (at your option) any later version.
  13079. + *
  13080. + * This program is distributed in the hope that it will be useful,
  13081. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13082. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13083. + * GNU General Public License for more details.
  13084. + *
  13085. + * You should have received a copy of the GNU General Public License
  13086. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13087. + */
  13088. +
  13089. +/*
  13090. + * judging filesystem type
  13091. + */
  13092. +
  13093. +#ifndef __AUFS_FSTYPE_H__
  13094. +#define __AUFS_FSTYPE_H__
  13095. +
  13096. +#ifdef __KERNEL__
  13097. +
  13098. +#include <linux/fs.h>
  13099. +#include <linux/magic.h>
  13100. +#include <linux/romfs_fs.h>
  13101. +#include <linux/nfs_fs.h>
  13102. +
  13103. +static inline int au_test_aufs(struct super_block *sb)
  13104. +{
  13105. + return sb->s_magic == AUFS_SUPER_MAGIC;
  13106. +}
  13107. +
  13108. +static inline const char *au_sbtype(struct super_block *sb)
  13109. +{
  13110. + return sb->s_type->name;
  13111. +}
  13112. +
  13113. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  13114. +{
  13115. +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
  13116. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  13117. +#else
  13118. + return 0;
  13119. +#endif
  13120. +}
  13121. +
  13122. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  13123. +{
  13124. +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
  13125. + return sb->s_magic == ROMFS_MAGIC;
  13126. +#else
  13127. + return 0;
  13128. +#endif
  13129. +}
  13130. +
  13131. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  13132. +{
  13133. +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
  13134. + return sb->s_magic == CRAMFS_MAGIC;
  13135. +#endif
  13136. + return 0;
  13137. +}
  13138. +
  13139. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  13140. +{
  13141. +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
  13142. + return sb->s_magic == NFS_SUPER_MAGIC;
  13143. +#else
  13144. + return 0;
  13145. +#endif
  13146. +}
  13147. +
  13148. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  13149. +{
  13150. +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
  13151. + return sb->s_magic == FUSE_SUPER_MAGIC;
  13152. +#else
  13153. + return 0;
  13154. +#endif
  13155. +}
  13156. +
  13157. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  13158. +{
  13159. +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
  13160. + return sb->s_magic == XFS_SB_MAGIC;
  13161. +#else
  13162. + return 0;
  13163. +#endif
  13164. +}
  13165. +
  13166. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  13167. +{
  13168. +#ifdef CONFIG_TMPFS
  13169. + return sb->s_magic == TMPFS_MAGIC;
  13170. +#else
  13171. + return 0;
  13172. +#endif
  13173. +}
  13174. +
  13175. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  13176. +{
  13177. +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
  13178. + return !strcmp(au_sbtype(sb), "ecryptfs");
  13179. +#else
  13180. + return 0;
  13181. +#endif
  13182. +}
  13183. +
  13184. +static inline int au_test_ramfs(struct super_block *sb)
  13185. +{
  13186. + return sb->s_magic == RAMFS_MAGIC;
  13187. +}
  13188. +
  13189. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  13190. +{
  13191. +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
  13192. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  13193. +#else
  13194. + return 0;
  13195. +#endif
  13196. +}
  13197. +
  13198. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  13199. +{
  13200. +#ifdef CONFIG_PROC_FS
  13201. + return sb->s_magic == PROC_SUPER_MAGIC;
  13202. +#else
  13203. + return 0;
  13204. +#endif
  13205. +}
  13206. +
  13207. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  13208. +{
  13209. +#ifdef CONFIG_SYSFS
  13210. + return sb->s_magic == SYSFS_MAGIC;
  13211. +#else
  13212. + return 0;
  13213. +#endif
  13214. +}
  13215. +
  13216. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  13217. +{
  13218. +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
  13219. + return sb->s_magic == CONFIGFS_MAGIC;
  13220. +#else
  13221. + return 0;
  13222. +#endif
  13223. +}
  13224. +
  13225. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  13226. +{
  13227. +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
  13228. + return sb->s_magic == MINIX3_SUPER_MAGIC
  13229. + || sb->s_magic == MINIX2_SUPER_MAGIC
  13230. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  13231. + || sb->s_magic == MINIX_SUPER_MAGIC
  13232. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  13233. +#else
  13234. + return 0;
  13235. +#endif
  13236. +}
  13237. +
  13238. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  13239. +{
  13240. +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
  13241. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  13242. +#else
  13243. + return 0;
  13244. +#endif
  13245. +}
  13246. +
  13247. +static inline int au_test_msdos(struct super_block *sb)
  13248. +{
  13249. + return au_test_fat(sb);
  13250. +}
  13251. +
  13252. +static inline int au_test_vfat(struct super_block *sb)
  13253. +{
  13254. + return au_test_fat(sb);
  13255. +}
  13256. +
  13257. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  13258. +{
  13259. +#ifdef CONFIG_SECURITYFS
  13260. + return sb->s_magic == SECURITYFS_MAGIC;
  13261. +#else
  13262. + return 0;
  13263. +#endif
  13264. +}
  13265. +
  13266. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  13267. +{
  13268. +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
  13269. + return sb->s_magic == SQUASHFS_MAGIC;
  13270. +#else
  13271. + return 0;
  13272. +#endif
  13273. +}
  13274. +
  13275. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  13276. +{
  13277. +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
  13278. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  13279. +#else
  13280. + return 0;
  13281. +#endif
  13282. +}
  13283. +
  13284. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  13285. +{
  13286. +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
  13287. + return sb->s_magic == XENFS_SUPER_MAGIC;
  13288. +#else
  13289. + return 0;
  13290. +#endif
  13291. +}
  13292. +
  13293. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  13294. +{
  13295. +#ifdef CONFIG_DEBUG_FS
  13296. + return sb->s_magic == DEBUGFS_MAGIC;
  13297. +#else
  13298. + return 0;
  13299. +#endif
  13300. +}
  13301. +
  13302. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  13303. +{
  13304. +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
  13305. + return sb->s_magic == NILFS_SUPER_MAGIC;
  13306. +#else
  13307. + return 0;
  13308. +#endif
  13309. +}
  13310. +
  13311. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  13312. +{
  13313. +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
  13314. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  13315. +#else
  13316. + return 0;
  13317. +#endif
  13318. +}
  13319. +
  13320. +/* ---------------------------------------------------------------------- */
  13321. +/*
  13322. + * they can't be an aufs branch.
  13323. + */
  13324. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  13325. +{
  13326. + return
  13327. +#ifndef CONFIG_AUFS_BR_RAMFS
  13328. + au_test_ramfs(sb) ||
  13329. +#endif
  13330. + au_test_procfs(sb)
  13331. + || au_test_sysfs(sb)
  13332. + || au_test_configfs(sb)
  13333. + || au_test_debugfs(sb)
  13334. + || au_test_securityfs(sb)
  13335. + || au_test_xenfs(sb)
  13336. + || au_test_ecryptfs(sb)
  13337. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  13338. + || au_test_aufs(sb); /* will be supported in next version */
  13339. +}
  13340. +
  13341. +static inline int au_test_fs_remote(struct super_block *sb)
  13342. +{
  13343. + return !au_test_tmpfs(sb)
  13344. +#ifdef CONFIG_AUFS_BR_RAMFS
  13345. + && !au_test_ramfs(sb)
  13346. +#endif
  13347. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  13348. +}
  13349. +
  13350. +/* ---------------------------------------------------------------------- */
  13351. +
  13352. +/*
  13353. + * Note: these functions (below) are created after reading ->getattr() in all
  13354. + * filesystems under linux/fs. it means we have to do so in every update...
  13355. + */
  13356. +
  13357. +/*
  13358. + * some filesystems require getattr to refresh the inode attributes before
  13359. + * referencing.
  13360. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  13361. + * and leave the work for d_revalidate()
  13362. + */
  13363. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  13364. +{
  13365. + return au_test_nfs(sb)
  13366. + || au_test_fuse(sb)
  13367. + /* || au_test_btrfs(sb) */ /* untested */
  13368. + ;
  13369. +}
  13370. +
  13371. +/*
  13372. + * filesystems which don't maintain i_size or i_blocks.
  13373. + */
  13374. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  13375. +{
  13376. + return au_test_xfs(sb)
  13377. + || au_test_btrfs(sb)
  13378. + || au_test_ubifs(sb)
  13379. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  13380. + /* || au_test_minix(sb) */ /* untested */
  13381. + ;
  13382. +}
  13383. +
  13384. +/*
  13385. + * filesystems which don't store the correct value in some of their inode
  13386. + * attributes.
  13387. + */
  13388. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  13389. +{
  13390. + return au_test_fs_bad_iattr_size(sb)
  13391. + || au_test_fat(sb)
  13392. + || au_test_msdos(sb)
  13393. + || au_test_vfat(sb);
  13394. +}
  13395. +
  13396. +/* they don't check i_nlink in link(2) */
  13397. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  13398. +{
  13399. + return au_test_tmpfs(sb)
  13400. +#ifdef CONFIG_AUFS_BR_RAMFS
  13401. + || au_test_ramfs(sb)
  13402. +#endif
  13403. + || au_test_ubifs(sb)
  13404. + || au_test_hfsplus(sb);
  13405. +}
  13406. +
  13407. +/*
  13408. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  13409. + */
  13410. +static inline int au_test_fs_notime(struct super_block *sb)
  13411. +{
  13412. + return au_test_nfs(sb)
  13413. + || au_test_fuse(sb)
  13414. + || au_test_ubifs(sb)
  13415. + ;
  13416. +}
  13417. +
  13418. +/* temporary support for i#1 in cramfs */
  13419. +static inline int au_test_fs_unique_ino(struct inode *inode)
  13420. +{
  13421. + if (au_test_cramfs(inode->i_sb))
  13422. + return inode->i_ino != 1;
  13423. + return 1;
  13424. +}
  13425. +
  13426. +/* ---------------------------------------------------------------------- */
  13427. +
  13428. +/*
  13429. + * the filesystem where the xino files placed must support i/o after unlink and
  13430. + * maintain i_size and i_blocks.
  13431. + */
  13432. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  13433. +{
  13434. + return au_test_fs_remote(sb)
  13435. + || au_test_fs_bad_iattr_size(sb)
  13436. + /* don't want unnecessary work for xino */
  13437. + || au_test_aufs(sb)
  13438. + || au_test_ecryptfs(sb)
  13439. + || au_test_nilfs(sb);
  13440. +}
  13441. +
  13442. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  13443. +{
  13444. + return au_test_tmpfs(sb)
  13445. + || au_test_ramfs(sb);
  13446. +}
  13447. +
  13448. +/*
  13449. + * test if the @sb is real-readonly.
  13450. + */
  13451. +static inline int au_test_fs_rr(struct super_block *sb)
  13452. +{
  13453. + return au_test_squashfs(sb)
  13454. + || au_test_iso9660(sb)
  13455. + || au_test_cramfs(sb)
  13456. + || au_test_romfs(sb);
  13457. +}
  13458. +
  13459. +/*
  13460. + * test if the @inode is nfs with 'noacl' option
  13461. + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
  13462. + */
  13463. +static inline int au_test_nfs_noacl(struct inode *inode)
  13464. +{
  13465. + return au_test_nfs(inode->i_sb)
  13466. + /* && IS_POSIXACL(inode) */
  13467. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  13468. +}
  13469. +
  13470. +#endif /* __KERNEL__ */
  13471. +#endif /* __AUFS_FSTYPE_H__ */
  13472. diff -Nur linux-4.1.10.orig/fs/aufs/hfsnotify.c linux-4.1.10/fs/aufs/hfsnotify.c
  13473. --- linux-4.1.10.orig/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
  13474. +++ linux-4.1.10/fs/aufs/hfsnotify.c 2015-10-22 21:35:53.000000000 +0200
  13475. @@ -0,0 +1,288 @@
  13476. +/*
  13477. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13478. + *
  13479. + * This program, aufs is free software; you can redistribute it and/or modify
  13480. + * it under the terms of the GNU General Public License as published by
  13481. + * the Free Software Foundation; either version 2 of the License, or
  13482. + * (at your option) any later version.
  13483. + *
  13484. + * This program is distributed in the hope that it will be useful,
  13485. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13486. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13487. + * GNU General Public License for more details.
  13488. + *
  13489. + * You should have received a copy of the GNU General Public License
  13490. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13491. + */
  13492. +
  13493. +/*
  13494. + * fsnotify for the lower directories
  13495. + */
  13496. +
  13497. +#include "aufs.h"
  13498. +
  13499. +/* FS_IN_IGNORED is unnecessary */
  13500. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  13501. + | FS_CREATE | FS_EVENT_ON_CHILD);
  13502. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  13503. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  13504. +
  13505. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  13506. +{
  13507. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  13508. + hn_mark);
  13509. + AuDbg("here\n");
  13510. + au_cache_free_hnotify(hn);
  13511. + smp_mb__before_atomic();
  13512. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  13513. + wake_up(&au_hfsn_wq);
  13514. +}
  13515. +
  13516. +static int au_hfsn_alloc(struct au_hinode *hinode)
  13517. +{
  13518. + int err;
  13519. + struct au_hnotify *hn;
  13520. + struct super_block *sb;
  13521. + struct au_branch *br;
  13522. + struct fsnotify_mark *mark;
  13523. + aufs_bindex_t bindex;
  13524. +
  13525. + hn = hinode->hi_notify;
  13526. + sb = hn->hn_aufs_inode->i_sb;
  13527. + bindex = au_br_index(sb, hinode->hi_id);
  13528. + br = au_sbr(sb, bindex);
  13529. + AuDebugOn(!br->br_hfsn);
  13530. +
  13531. + mark = &hn->hn_mark;
  13532. + fsnotify_init_mark(mark, au_hfsn_free_mark);
  13533. + mark->mask = AuHfsnMask;
  13534. + /*
  13535. + * by udba rename or rmdir, aufs assign a new inode to the known
  13536. + * h_inode, so specify 1 to allow dups.
  13537. + */
  13538. + lockdep_off();
  13539. + err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
  13540. + /*mnt*/NULL, /*allow_dups*/1);
  13541. + /* even if err */
  13542. + fsnotify_put_mark(mark);
  13543. + lockdep_on();
  13544. +
  13545. + return err;
  13546. +}
  13547. +
  13548. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  13549. +{
  13550. + struct fsnotify_mark *mark;
  13551. + unsigned long long ull;
  13552. + struct fsnotify_group *group;
  13553. +
  13554. + ull = atomic64_inc_return(&au_hfsn_ifree);
  13555. + BUG_ON(!ull);
  13556. +
  13557. + mark = &hn->hn_mark;
  13558. + spin_lock(&mark->lock);
  13559. + group = mark->group;
  13560. + fsnotify_get_group(group);
  13561. + spin_unlock(&mark->lock);
  13562. + lockdep_off();
  13563. + fsnotify_destroy_mark(mark, group);
  13564. + fsnotify_put_group(group);
  13565. + lockdep_on();
  13566. +
  13567. + /* free hn by myself */
  13568. + return 0;
  13569. +}
  13570. +
  13571. +/* ---------------------------------------------------------------------- */
  13572. +
  13573. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  13574. +{
  13575. + struct fsnotify_mark *mark;
  13576. +
  13577. + mark = &hinode->hi_notify->hn_mark;
  13578. + spin_lock(&mark->lock);
  13579. + if (do_set) {
  13580. + AuDebugOn(mark->mask & AuHfsnMask);
  13581. + mark->mask |= AuHfsnMask;
  13582. + } else {
  13583. + AuDebugOn(!(mark->mask & AuHfsnMask));
  13584. + mark->mask &= ~AuHfsnMask;
  13585. + }
  13586. + spin_unlock(&mark->lock);
  13587. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  13588. +}
  13589. +
  13590. +/* ---------------------------------------------------------------------- */
  13591. +
  13592. +/* #define AuDbgHnotify */
  13593. +#ifdef AuDbgHnotify
  13594. +static char *au_hfsn_name(u32 mask)
  13595. +{
  13596. +#ifdef CONFIG_AUFS_DEBUG
  13597. +#define test_ret(flag) \
  13598. + do { \
  13599. + if (mask & flag) \
  13600. + return #flag; \
  13601. + } while (0)
  13602. + test_ret(FS_ACCESS);
  13603. + test_ret(FS_MODIFY);
  13604. + test_ret(FS_ATTRIB);
  13605. + test_ret(FS_CLOSE_WRITE);
  13606. + test_ret(FS_CLOSE_NOWRITE);
  13607. + test_ret(FS_OPEN);
  13608. + test_ret(FS_MOVED_FROM);
  13609. + test_ret(FS_MOVED_TO);
  13610. + test_ret(FS_CREATE);
  13611. + test_ret(FS_DELETE);
  13612. + test_ret(FS_DELETE_SELF);
  13613. + test_ret(FS_MOVE_SELF);
  13614. + test_ret(FS_UNMOUNT);
  13615. + test_ret(FS_Q_OVERFLOW);
  13616. + test_ret(FS_IN_IGNORED);
  13617. + test_ret(FS_ISDIR);
  13618. + test_ret(FS_IN_ONESHOT);
  13619. + test_ret(FS_EVENT_ON_CHILD);
  13620. + return "";
  13621. +#undef test_ret
  13622. +#else
  13623. + return "??";
  13624. +#endif
  13625. +}
  13626. +#endif
  13627. +
  13628. +/* ---------------------------------------------------------------------- */
  13629. +
  13630. +static void au_hfsn_free_group(struct fsnotify_group *group)
  13631. +{
  13632. + struct au_br_hfsnotify *hfsn = group->private;
  13633. +
  13634. + AuDbg("here\n");
  13635. + kfree(hfsn);
  13636. +}
  13637. +
  13638. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  13639. + struct inode *inode,
  13640. + struct fsnotify_mark *inode_mark,
  13641. + struct fsnotify_mark *vfsmount_mark,
  13642. + u32 mask, void *data, int data_type,
  13643. + const unsigned char *file_name, u32 cookie)
  13644. +{
  13645. + int err;
  13646. + struct au_hnotify *hnotify;
  13647. + struct inode *h_dir, *h_inode;
  13648. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  13649. +
  13650. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  13651. +
  13652. + err = 0;
  13653. + /* if FS_UNMOUNT happens, there must be another bug */
  13654. + AuDebugOn(mask & FS_UNMOUNT);
  13655. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  13656. + goto out;
  13657. +
  13658. + h_dir = inode;
  13659. + h_inode = NULL;
  13660. +#ifdef AuDbgHnotify
  13661. + au_debug_on();
  13662. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  13663. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  13664. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  13665. + h_dir->i_ino, mask, au_hfsn_name(mask),
  13666. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  13667. + /* WARN_ON(1); */
  13668. + }
  13669. + au_debug_off();
  13670. +#endif
  13671. +
  13672. + AuDebugOn(!inode_mark);
  13673. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  13674. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  13675. +
  13676. +out:
  13677. + return err;
  13678. +}
  13679. +
  13680. +static struct fsnotify_ops au_hfsn_ops = {
  13681. + .handle_event = au_hfsn_handle_event,
  13682. + .free_group_priv = au_hfsn_free_group
  13683. +};
  13684. +
  13685. +/* ---------------------------------------------------------------------- */
  13686. +
  13687. +static void au_hfsn_fin_br(struct au_branch *br)
  13688. +{
  13689. + struct au_br_hfsnotify *hfsn;
  13690. +
  13691. + hfsn = br->br_hfsn;
  13692. + if (hfsn) {
  13693. + lockdep_off();
  13694. + fsnotify_put_group(hfsn->hfsn_group);
  13695. + lockdep_on();
  13696. + }
  13697. +}
  13698. +
  13699. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  13700. +{
  13701. + int err;
  13702. + struct fsnotify_group *group;
  13703. + struct au_br_hfsnotify *hfsn;
  13704. +
  13705. + err = 0;
  13706. + br->br_hfsn = NULL;
  13707. + if (!au_br_hnotifyable(perm))
  13708. + goto out;
  13709. +
  13710. + err = -ENOMEM;
  13711. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  13712. + if (unlikely(!hfsn))
  13713. + goto out;
  13714. +
  13715. + err = 0;
  13716. + group = fsnotify_alloc_group(&au_hfsn_ops);
  13717. + if (IS_ERR(group)) {
  13718. + err = PTR_ERR(group);
  13719. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  13720. + goto out_hfsn;
  13721. + }
  13722. +
  13723. + group->private = hfsn;
  13724. + hfsn->hfsn_group = group;
  13725. + br->br_hfsn = hfsn;
  13726. + goto out; /* success */
  13727. +
  13728. +out_hfsn:
  13729. + kfree(hfsn);
  13730. +out:
  13731. + return err;
  13732. +}
  13733. +
  13734. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  13735. +{
  13736. + int err;
  13737. +
  13738. + err = 0;
  13739. + if (!br->br_hfsn)
  13740. + err = au_hfsn_init_br(br, perm);
  13741. +
  13742. + return err;
  13743. +}
  13744. +
  13745. +/* ---------------------------------------------------------------------- */
  13746. +
  13747. +static void au_hfsn_fin(void)
  13748. +{
  13749. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  13750. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  13751. +}
  13752. +
  13753. +const struct au_hnotify_op au_hnotify_op = {
  13754. + .ctl = au_hfsn_ctl,
  13755. + .alloc = au_hfsn_alloc,
  13756. + .free = au_hfsn_free,
  13757. +
  13758. + .fin = au_hfsn_fin,
  13759. +
  13760. + .reset_br = au_hfsn_reset_br,
  13761. + .fin_br = au_hfsn_fin_br,
  13762. + .init_br = au_hfsn_init_br
  13763. +};
  13764. diff -Nur linux-4.1.10.orig/fs/aufs/hfsplus.c linux-4.1.10/fs/aufs/hfsplus.c
  13765. --- linux-4.1.10.orig/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
  13766. +++ linux-4.1.10/fs/aufs/hfsplus.c 2015-10-22 21:35:53.000000000 +0200
  13767. @@ -0,0 +1,56 @@
  13768. +/*
  13769. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  13770. + *
  13771. + * This program, aufs is free software; you can redistribute it and/or modify
  13772. + * it under the terms of the GNU General Public License as published by
  13773. + * the Free Software Foundation; either version 2 of the License, or
  13774. + * (at your option) any later version.
  13775. + *
  13776. + * This program is distributed in the hope that it will be useful,
  13777. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13778. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13779. + * GNU General Public License for more details.
  13780. + *
  13781. + * You should have received a copy of the GNU General Public License
  13782. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13783. + */
  13784. +
  13785. +/*
  13786. + * special support for filesystems which aqucires an inode mutex
  13787. + * at final closing a file, eg, hfsplus.
  13788. + *
  13789. + * This trick is very simple and stupid, just to open the file before really
  13790. + * neceeary open to tell hfsplus that this is not the final closing.
  13791. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  13792. + * and au_h_open_post() after releasing it.
  13793. + */
  13794. +
  13795. +#include "aufs.h"
  13796. +
  13797. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  13798. + int force_wr)
  13799. +{
  13800. + struct file *h_file;
  13801. + struct dentry *h_dentry;
  13802. +
  13803. + h_dentry = au_h_dptr(dentry, bindex);
  13804. + AuDebugOn(!h_dentry);
  13805. + AuDebugOn(d_is_negative(h_dentry));
  13806. +
  13807. + h_file = NULL;
  13808. + if (au_test_hfsplus(h_dentry->d_sb)
  13809. + && d_is_reg(h_dentry))
  13810. + h_file = au_h_open(dentry, bindex,
  13811. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  13812. + /*file*/NULL, force_wr);
  13813. + return h_file;
  13814. +}
  13815. +
  13816. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  13817. + struct file *h_file)
  13818. +{
  13819. + if (h_file) {
  13820. + fput(h_file);
  13821. + au_sbr_put(dentry->d_sb, bindex);
  13822. + }
  13823. +}
  13824. diff -Nur linux-4.1.10.orig/fs/aufs/hnotify.c linux-4.1.10/fs/aufs/hnotify.c
  13825. --- linux-4.1.10.orig/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
  13826. +++ linux-4.1.10/fs/aufs/hnotify.c 2015-10-22 21:35:53.000000000 +0200
  13827. @@ -0,0 +1,710 @@
  13828. +/*
  13829. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  13830. + *
  13831. + * This program, aufs is free software; you can redistribute it and/or modify
  13832. + * it under the terms of the GNU General Public License as published by
  13833. + * the Free Software Foundation; either version 2 of the License, or
  13834. + * (at your option) any later version.
  13835. + *
  13836. + * This program is distributed in the hope that it will be useful,
  13837. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13838. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13839. + * GNU General Public License for more details.
  13840. + *
  13841. + * You should have received a copy of the GNU General Public License
  13842. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13843. + */
  13844. +
  13845. +/*
  13846. + * abstraction to notify the direct changes on lower directories
  13847. + */
  13848. +
  13849. +#include "aufs.h"
  13850. +
  13851. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  13852. +{
  13853. + int err;
  13854. + struct au_hnotify *hn;
  13855. +
  13856. + err = -ENOMEM;
  13857. + hn = au_cache_alloc_hnotify();
  13858. + if (hn) {
  13859. + hn->hn_aufs_inode = inode;
  13860. + hinode->hi_notify = hn;
  13861. + err = au_hnotify_op.alloc(hinode);
  13862. + AuTraceErr(err);
  13863. + if (unlikely(err)) {
  13864. + hinode->hi_notify = NULL;
  13865. + au_cache_free_hnotify(hn);
  13866. + /*
  13867. + * The upper dir was removed by udba, but the same named
  13868. + * dir left. In this case, aufs assignes a new inode
  13869. + * number and set the monitor again.
  13870. + * For the lower dir, the old monitnor is still left.
  13871. + */
  13872. + if (err == -EEXIST)
  13873. + err = 0;
  13874. + }
  13875. + }
  13876. +
  13877. + AuTraceErr(err);
  13878. + return err;
  13879. +}
  13880. +
  13881. +void au_hn_free(struct au_hinode *hinode)
  13882. +{
  13883. + struct au_hnotify *hn;
  13884. +
  13885. + hn = hinode->hi_notify;
  13886. + if (hn) {
  13887. + hinode->hi_notify = NULL;
  13888. + if (au_hnotify_op.free(hinode, hn))
  13889. + au_cache_free_hnotify(hn);
  13890. + }
  13891. +}
  13892. +
  13893. +/* ---------------------------------------------------------------------- */
  13894. +
  13895. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  13896. +{
  13897. + if (hinode->hi_notify)
  13898. + au_hnotify_op.ctl(hinode, do_set);
  13899. +}
  13900. +
  13901. +void au_hn_reset(struct inode *inode, unsigned int flags)
  13902. +{
  13903. + aufs_bindex_t bindex, bend;
  13904. + struct inode *hi;
  13905. + struct dentry *iwhdentry;
  13906. +
  13907. + bend = au_ibend(inode);
  13908. + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
  13909. + hi = au_h_iptr(inode, bindex);
  13910. + if (!hi)
  13911. + continue;
  13912. +
  13913. + /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
  13914. + iwhdentry = au_hi_wh(inode, bindex);
  13915. + if (iwhdentry)
  13916. + dget(iwhdentry);
  13917. + au_igrab(hi);
  13918. + au_set_h_iptr(inode, bindex, NULL, 0);
  13919. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  13920. + flags & ~AuHi_XINO);
  13921. + iput(hi);
  13922. + dput(iwhdentry);
  13923. + /* mutex_unlock(&hi->i_mutex); */
  13924. + }
  13925. +}
  13926. +
  13927. +/* ---------------------------------------------------------------------- */
  13928. +
  13929. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  13930. +{
  13931. + int err;
  13932. + aufs_bindex_t bindex, bend, bfound, bstart;
  13933. + struct inode *h_i;
  13934. +
  13935. + err = 0;
  13936. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  13937. + pr_warn("branch root dir was changed\n");
  13938. + goto out;
  13939. + }
  13940. +
  13941. + bfound = -1;
  13942. + bend = au_ibend(inode);
  13943. + bstart = au_ibstart(inode);
  13944. +#if 0 /* reserved for future use */
  13945. + if (bindex == bend) {
  13946. + /* keep this ino in rename case */
  13947. + goto out;
  13948. + }
  13949. +#endif
  13950. + for (bindex = bstart; bindex <= bend; bindex++)
  13951. + if (au_h_iptr(inode, bindex) == h_inode) {
  13952. + bfound = bindex;
  13953. + break;
  13954. + }
  13955. + if (bfound < 0)
  13956. + goto out;
  13957. +
  13958. + for (bindex = bstart; bindex <= bend; bindex++) {
  13959. + h_i = au_h_iptr(inode, bindex);
  13960. + if (!h_i)
  13961. + continue;
  13962. +
  13963. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  13964. + /* ignore this error */
  13965. + /* bad action? */
  13966. + }
  13967. +
  13968. + /* children inode number will be broken */
  13969. +
  13970. +out:
  13971. + AuTraceErr(err);
  13972. + return err;
  13973. +}
  13974. +
  13975. +static int hn_gen_tree(struct dentry *dentry)
  13976. +{
  13977. + int err, i, j, ndentry;
  13978. + struct au_dcsub_pages dpages;
  13979. + struct au_dpage *dpage;
  13980. + struct dentry **dentries;
  13981. +
  13982. + err = au_dpages_init(&dpages, GFP_NOFS);
  13983. + if (unlikely(err))
  13984. + goto out;
  13985. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  13986. + if (unlikely(err))
  13987. + goto out_dpages;
  13988. +
  13989. + for (i = 0; i < dpages.ndpage; i++) {
  13990. + dpage = dpages.dpages + i;
  13991. + dentries = dpage->dentries;
  13992. + ndentry = dpage->ndentry;
  13993. + for (j = 0; j < ndentry; j++) {
  13994. + struct dentry *d;
  13995. +
  13996. + d = dentries[j];
  13997. + if (IS_ROOT(d))
  13998. + continue;
  13999. +
  14000. + au_digen_dec(d);
  14001. + if (d_really_is_positive(d))
  14002. + /* todo: reset children xino?
  14003. + cached children only? */
  14004. + au_iigen_dec(d_inode(d));
  14005. + }
  14006. + }
  14007. +
  14008. +out_dpages:
  14009. + au_dpages_free(&dpages);
  14010. +
  14011. +#if 0
  14012. + /* discard children */
  14013. + dentry_unhash(dentry);
  14014. + dput(dentry);
  14015. +#endif
  14016. +out:
  14017. + return err;
  14018. +}
  14019. +
  14020. +/*
  14021. + * return 0 if processed.
  14022. + */
  14023. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  14024. + const unsigned int isdir)
  14025. +{
  14026. + int err;
  14027. + struct dentry *d;
  14028. + struct qstr *dname;
  14029. +
  14030. + err = 1;
  14031. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14032. + pr_warn("branch root dir was changed\n");
  14033. + err = 0;
  14034. + goto out;
  14035. + }
  14036. +
  14037. + if (!isdir) {
  14038. + AuDebugOn(!name);
  14039. + au_iigen_dec(inode);
  14040. + spin_lock(&inode->i_lock);
  14041. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  14042. + spin_lock(&d->d_lock);
  14043. + dname = &d->d_name;
  14044. + if (dname->len != nlen
  14045. + && memcmp(dname->name, name, nlen)) {
  14046. + spin_unlock(&d->d_lock);
  14047. + continue;
  14048. + }
  14049. + err = 0;
  14050. + au_digen_dec(d);
  14051. + spin_unlock(&d->d_lock);
  14052. + break;
  14053. + }
  14054. + spin_unlock(&inode->i_lock);
  14055. + } else {
  14056. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  14057. + d = d_find_any_alias(inode);
  14058. + if (!d) {
  14059. + au_iigen_dec(inode);
  14060. + goto out;
  14061. + }
  14062. +
  14063. + spin_lock(&d->d_lock);
  14064. + dname = &d->d_name;
  14065. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  14066. + spin_unlock(&d->d_lock);
  14067. + err = hn_gen_tree(d);
  14068. + spin_lock(&d->d_lock);
  14069. + }
  14070. + spin_unlock(&d->d_lock);
  14071. + dput(d);
  14072. + }
  14073. +
  14074. +out:
  14075. + AuTraceErr(err);
  14076. + return err;
  14077. +}
  14078. +
  14079. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  14080. +{
  14081. + int err;
  14082. +
  14083. + if (IS_ROOT(dentry)) {
  14084. + pr_warn("branch root dir was changed\n");
  14085. + return 0;
  14086. + }
  14087. +
  14088. + err = 0;
  14089. + if (!isdir) {
  14090. + au_digen_dec(dentry);
  14091. + if (d_really_is_positive(dentry))
  14092. + au_iigen_dec(d_inode(dentry));
  14093. + } else {
  14094. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  14095. + if (d_really_is_positive(dentry))
  14096. + err = hn_gen_tree(dentry);
  14097. + }
  14098. +
  14099. + AuTraceErr(err);
  14100. + return err;
  14101. +}
  14102. +
  14103. +/* ---------------------------------------------------------------------- */
  14104. +
  14105. +/* hnotify job flags */
  14106. +#define AuHnJob_XINO0 1
  14107. +#define AuHnJob_GEN (1 << 1)
  14108. +#define AuHnJob_DIRENT (1 << 2)
  14109. +#define AuHnJob_ISDIR (1 << 3)
  14110. +#define AuHnJob_TRYXINO0 (1 << 4)
  14111. +#define AuHnJob_MNTPNT (1 << 5)
  14112. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  14113. +#define au_fset_hnjob(flags, name) \
  14114. + do { (flags) |= AuHnJob_##name; } while (0)
  14115. +#define au_fclr_hnjob(flags, name) \
  14116. + do { (flags) &= ~AuHnJob_##name; } while (0)
  14117. +
  14118. +enum {
  14119. + AuHn_CHILD,
  14120. + AuHn_PARENT,
  14121. + AuHnLast
  14122. +};
  14123. +
  14124. +struct au_hnotify_args {
  14125. + struct inode *h_dir, *dir, *h_child_inode;
  14126. + u32 mask;
  14127. + unsigned int flags[AuHnLast];
  14128. + unsigned int h_child_nlen;
  14129. + char h_child_name[];
  14130. +};
  14131. +
  14132. +struct hn_job_args {
  14133. + unsigned int flags;
  14134. + struct inode *inode, *h_inode, *dir, *h_dir;
  14135. + struct dentry *dentry;
  14136. + char *h_name;
  14137. + int h_nlen;
  14138. +};
  14139. +
  14140. +static int hn_job(struct hn_job_args *a)
  14141. +{
  14142. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  14143. + int e;
  14144. +
  14145. + /* reset xino */
  14146. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  14147. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  14148. +
  14149. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  14150. + && a->inode
  14151. + && a->h_inode) {
  14152. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  14153. + if (!a->h_inode->i_nlink
  14154. + && !(a->h_inode->i_state & I_LINKABLE))
  14155. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  14156. + mutex_unlock(&a->h_inode->i_mutex);
  14157. + }
  14158. +
  14159. + /* make the generation obsolete */
  14160. + if (au_ftest_hnjob(a->flags, GEN)) {
  14161. + e = -1;
  14162. + if (a->inode)
  14163. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  14164. + isdir);
  14165. + if (e && a->dentry)
  14166. + hn_gen_by_name(a->dentry, isdir);
  14167. + /* ignore this error */
  14168. + }
  14169. +
  14170. + /* make dir entries obsolete */
  14171. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  14172. + struct au_vdir *vdir;
  14173. +
  14174. + vdir = au_ivdir(a->inode);
  14175. + if (vdir)
  14176. + vdir->vd_jiffy = 0;
  14177. + /* IMustLock(a->inode); */
  14178. + /* a->inode->i_version++; */
  14179. + }
  14180. +
  14181. + /* can do nothing but warn */
  14182. + if (au_ftest_hnjob(a->flags, MNTPNT)
  14183. + && a->dentry
  14184. + && d_mountpoint(a->dentry))
  14185. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  14186. +
  14187. + return 0;
  14188. +}
  14189. +
  14190. +/* ---------------------------------------------------------------------- */
  14191. +
  14192. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  14193. + struct inode *dir)
  14194. +{
  14195. + struct dentry *dentry, *d, *parent;
  14196. + struct qstr *dname;
  14197. +
  14198. + parent = d_find_any_alias(dir);
  14199. + if (!parent)
  14200. + return NULL;
  14201. +
  14202. + dentry = NULL;
  14203. + spin_lock(&parent->d_lock);
  14204. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  14205. + /* AuDbg("%pd\n", d); */
  14206. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  14207. + dname = &d->d_name;
  14208. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  14209. + goto cont_unlock;
  14210. + if (au_di(d))
  14211. + au_digen_dec(d);
  14212. + else
  14213. + goto cont_unlock;
  14214. + if (au_dcount(d) > 0) {
  14215. + dentry = dget_dlock(d);
  14216. + spin_unlock(&d->d_lock);
  14217. + break;
  14218. + }
  14219. +
  14220. +cont_unlock:
  14221. + spin_unlock(&d->d_lock);
  14222. + }
  14223. + spin_unlock(&parent->d_lock);
  14224. + dput(parent);
  14225. +
  14226. + if (dentry)
  14227. + di_write_lock_child(dentry);
  14228. +
  14229. + return dentry;
  14230. +}
  14231. +
  14232. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  14233. + aufs_bindex_t bindex, ino_t h_ino)
  14234. +{
  14235. + struct inode *inode;
  14236. + ino_t ino;
  14237. + int err;
  14238. +
  14239. + inode = NULL;
  14240. + err = au_xino_read(sb, bindex, h_ino, &ino);
  14241. + if (!err && ino)
  14242. + inode = ilookup(sb, ino);
  14243. + if (!inode)
  14244. + goto out;
  14245. +
  14246. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  14247. + pr_warn("wrong root branch\n");
  14248. + iput(inode);
  14249. + inode = NULL;
  14250. + goto out;
  14251. + }
  14252. +
  14253. + ii_write_lock_child(inode);
  14254. +
  14255. +out:
  14256. + return inode;
  14257. +}
  14258. +
  14259. +static void au_hn_bh(void *_args)
  14260. +{
  14261. + struct au_hnotify_args *a = _args;
  14262. + struct super_block *sb;
  14263. + aufs_bindex_t bindex, bend, bfound;
  14264. + unsigned char xino, try_iput;
  14265. + int err;
  14266. + struct inode *inode;
  14267. + ino_t h_ino;
  14268. + struct hn_job_args args;
  14269. + struct dentry *dentry;
  14270. + struct au_sbinfo *sbinfo;
  14271. +
  14272. + AuDebugOn(!_args);
  14273. + AuDebugOn(!a->h_dir);
  14274. + AuDebugOn(!a->dir);
  14275. + AuDebugOn(!a->mask);
  14276. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  14277. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  14278. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  14279. +
  14280. + inode = NULL;
  14281. + dentry = NULL;
  14282. + /*
  14283. + * do not lock a->dir->i_mutex here
  14284. + * because of d_revalidate() may cause a deadlock.
  14285. + */
  14286. + sb = a->dir->i_sb;
  14287. + AuDebugOn(!sb);
  14288. + sbinfo = au_sbi(sb);
  14289. + AuDebugOn(!sbinfo);
  14290. + si_write_lock(sb, AuLock_NOPLMW);
  14291. +
  14292. + ii_read_lock_parent(a->dir);
  14293. + bfound = -1;
  14294. + bend = au_ibend(a->dir);
  14295. + for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
  14296. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  14297. + bfound = bindex;
  14298. + break;
  14299. + }
  14300. + ii_read_unlock(a->dir);
  14301. + if (unlikely(bfound < 0))
  14302. + goto out;
  14303. +
  14304. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  14305. + h_ino = 0;
  14306. + if (a->h_child_inode)
  14307. + h_ino = a->h_child_inode->i_ino;
  14308. +
  14309. + if (a->h_child_nlen
  14310. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  14311. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  14312. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  14313. + a->dir);
  14314. + try_iput = 0;
  14315. + if (dentry && d_really_is_positive(dentry))
  14316. + inode = d_inode(dentry);
  14317. + if (xino && !inode && h_ino
  14318. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  14319. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  14320. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  14321. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  14322. + try_iput = 1;
  14323. + }
  14324. +
  14325. + args.flags = a->flags[AuHn_CHILD];
  14326. + args.dentry = dentry;
  14327. + args.inode = inode;
  14328. + args.h_inode = a->h_child_inode;
  14329. + args.dir = a->dir;
  14330. + args.h_dir = a->h_dir;
  14331. + args.h_name = a->h_child_name;
  14332. + args.h_nlen = a->h_child_nlen;
  14333. + err = hn_job(&args);
  14334. + if (dentry) {
  14335. + if (au_di(dentry))
  14336. + di_write_unlock(dentry);
  14337. + dput(dentry);
  14338. + }
  14339. + if (inode && try_iput) {
  14340. + ii_write_unlock(inode);
  14341. + iput(inode);
  14342. + }
  14343. +
  14344. + ii_write_lock_parent(a->dir);
  14345. + args.flags = a->flags[AuHn_PARENT];
  14346. + args.dentry = NULL;
  14347. + args.inode = a->dir;
  14348. + args.h_inode = a->h_dir;
  14349. + args.dir = NULL;
  14350. + args.h_dir = NULL;
  14351. + args.h_name = NULL;
  14352. + args.h_nlen = 0;
  14353. + err = hn_job(&args);
  14354. + ii_write_unlock(a->dir);
  14355. +
  14356. +out:
  14357. + iput(a->h_child_inode);
  14358. + iput(a->h_dir);
  14359. + iput(a->dir);
  14360. + si_write_unlock(sb);
  14361. + au_nwt_done(&sbinfo->si_nowait);
  14362. + kfree(a);
  14363. +}
  14364. +
  14365. +/* ---------------------------------------------------------------------- */
  14366. +
  14367. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  14368. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  14369. +{
  14370. + int err, len;
  14371. + unsigned int flags[AuHnLast], f;
  14372. + unsigned char isdir, isroot, wh;
  14373. + struct inode *dir;
  14374. + struct au_hnotify_args *args;
  14375. + char *p, *h_child_name;
  14376. +
  14377. + err = 0;
  14378. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  14379. + dir = igrab(hnotify->hn_aufs_inode);
  14380. + if (!dir)
  14381. + goto out;
  14382. +
  14383. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  14384. + wh = 0;
  14385. + h_child_name = (void *)h_child_qstr->name;
  14386. + len = h_child_qstr->len;
  14387. + if (h_child_name) {
  14388. + if (len > AUFS_WH_PFX_LEN
  14389. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  14390. + h_child_name += AUFS_WH_PFX_LEN;
  14391. + len -= AUFS_WH_PFX_LEN;
  14392. + wh = 1;
  14393. + }
  14394. + }
  14395. +
  14396. + isdir = 0;
  14397. + if (h_child_inode)
  14398. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  14399. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  14400. + flags[AuHn_CHILD] = 0;
  14401. + if (isdir)
  14402. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  14403. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  14404. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  14405. + switch (mask & FS_EVENTS_POSS_ON_CHILD) {
  14406. + case FS_MOVED_FROM:
  14407. + case FS_MOVED_TO:
  14408. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  14409. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  14410. + /*FALLTHROUGH*/
  14411. + case FS_CREATE:
  14412. + AuDebugOn(!h_child_name);
  14413. + break;
  14414. +
  14415. + case FS_DELETE:
  14416. + /*
  14417. + * aufs never be able to get this child inode.
  14418. + * revalidation should be in d_revalidate()
  14419. + * by checking i_nlink, i_generation or d_unhashed().
  14420. + */
  14421. + AuDebugOn(!h_child_name);
  14422. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  14423. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  14424. + break;
  14425. +
  14426. + default:
  14427. + AuDebugOn(1);
  14428. + }
  14429. +
  14430. + if (wh)
  14431. + h_child_inode = NULL;
  14432. +
  14433. + err = -ENOMEM;
  14434. + /* iput() and kfree() will be called in au_hnotify() */
  14435. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  14436. + if (unlikely(!args)) {
  14437. + AuErr1("no memory\n");
  14438. + iput(dir);
  14439. + goto out;
  14440. + }
  14441. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  14442. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  14443. + args->mask = mask;
  14444. + args->dir = dir;
  14445. + args->h_dir = igrab(h_dir);
  14446. + if (h_child_inode)
  14447. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  14448. + args->h_child_inode = h_child_inode;
  14449. + args->h_child_nlen = len;
  14450. + if (len) {
  14451. + p = (void *)args;
  14452. + p += sizeof(*args);
  14453. + memcpy(p, h_child_name, len);
  14454. + p[len] = 0;
  14455. + }
  14456. +
  14457. + /* NFS fires the event for silly-renamed one from kworker */
  14458. + f = 0;
  14459. + if (!dir->i_nlink
  14460. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  14461. + f = AuWkq_NEST;
  14462. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  14463. + if (unlikely(err)) {
  14464. + pr_err("wkq %d\n", err);
  14465. + iput(args->h_child_inode);
  14466. + iput(args->h_dir);
  14467. + iput(args->dir);
  14468. + kfree(args);
  14469. + }
  14470. +
  14471. +out:
  14472. + return err;
  14473. +}
  14474. +
  14475. +/* ---------------------------------------------------------------------- */
  14476. +
  14477. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  14478. +{
  14479. + int err;
  14480. +
  14481. + AuDebugOn(!(udba & AuOptMask_UDBA));
  14482. +
  14483. + err = 0;
  14484. + if (au_hnotify_op.reset_br)
  14485. + err = au_hnotify_op.reset_br(udba, br, perm);
  14486. +
  14487. + return err;
  14488. +}
  14489. +
  14490. +int au_hnotify_init_br(struct au_branch *br, int perm)
  14491. +{
  14492. + int err;
  14493. +
  14494. + err = 0;
  14495. + if (au_hnotify_op.init_br)
  14496. + err = au_hnotify_op.init_br(br, perm);
  14497. +
  14498. + return err;
  14499. +}
  14500. +
  14501. +void au_hnotify_fin_br(struct au_branch *br)
  14502. +{
  14503. + if (au_hnotify_op.fin_br)
  14504. + au_hnotify_op.fin_br(br);
  14505. +}
  14506. +
  14507. +static void au_hn_destroy_cache(void)
  14508. +{
  14509. + kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
  14510. + au_cachep[AuCache_HNOTIFY] = NULL;
  14511. +}
  14512. +
  14513. +int __init au_hnotify_init(void)
  14514. +{
  14515. + int err;
  14516. +
  14517. + err = -ENOMEM;
  14518. + au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
  14519. + if (au_cachep[AuCache_HNOTIFY]) {
  14520. + err = 0;
  14521. + if (au_hnotify_op.init)
  14522. + err = au_hnotify_op.init();
  14523. + if (unlikely(err))
  14524. + au_hn_destroy_cache();
  14525. + }
  14526. + AuTraceErr(err);
  14527. + return err;
  14528. +}
  14529. +
  14530. +void au_hnotify_fin(void)
  14531. +{
  14532. + if (au_hnotify_op.fin)
  14533. + au_hnotify_op.fin();
  14534. + /* cf. au_cache_fin() */
  14535. + if (au_cachep[AuCache_HNOTIFY])
  14536. + au_hn_destroy_cache();
  14537. +}
  14538. diff -Nur linux-4.1.10.orig/fs/aufs/iinfo.c linux-4.1.10/fs/aufs/iinfo.c
  14539. --- linux-4.1.10.orig/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
  14540. +++ linux-4.1.10/fs/aufs/iinfo.c 2015-10-22 21:35:53.000000000 +0200
  14541. @@ -0,0 +1,277 @@
  14542. +/*
  14543. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  14544. + *
  14545. + * This program, aufs is free software; you can redistribute it and/or modify
  14546. + * it under the terms of the GNU General Public License as published by
  14547. + * the Free Software Foundation; either version 2 of the License, or
  14548. + * (at your option) any later version.
  14549. + *
  14550. + * This program is distributed in the hope that it will be useful,
  14551. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14552. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14553. + * GNU General Public License for more details.
  14554. + *
  14555. + * You should have received a copy of the GNU General Public License
  14556. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14557. + */
  14558. +
  14559. +/*
  14560. + * inode private data
  14561. + */
  14562. +
  14563. +#include "aufs.h"
  14564. +
  14565. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  14566. +{
  14567. + struct inode *h_inode;
  14568. +
  14569. + IiMustAnyLock(inode);
  14570. +
  14571. + h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
  14572. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14573. + return h_inode;
  14574. +}
  14575. +
  14576. +/* todo: hard/soft set? */
  14577. +void au_hiput(struct au_hinode *hinode)
  14578. +{
  14579. + au_hn_free(hinode);
  14580. + dput(hinode->hi_whdentry);
  14581. + iput(hinode->hi_inode);
  14582. +}
  14583. +
  14584. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  14585. +{
  14586. + unsigned int flags;
  14587. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  14588. +
  14589. + flags = 0;
  14590. + if (au_opt_test(mnt_flags, XINO))
  14591. + au_fset_hi(flags, XINO);
  14592. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  14593. + au_fset_hi(flags, HNOTIFY);
  14594. + return flags;
  14595. +}
  14596. +
  14597. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  14598. + struct inode *h_inode, unsigned int flags)
  14599. +{
  14600. + struct au_hinode *hinode;
  14601. + struct inode *hi;
  14602. + struct au_iinfo *iinfo = au_ii(inode);
  14603. +
  14604. + IiMustWriteLock(inode);
  14605. +
  14606. + hinode = iinfo->ii_hinode + bindex;
  14607. + hi = hinode->hi_inode;
  14608. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  14609. +
  14610. + if (hi)
  14611. + au_hiput(hinode);
  14612. + hinode->hi_inode = h_inode;
  14613. + if (h_inode) {
  14614. + int err;
  14615. + struct super_block *sb = inode->i_sb;
  14616. + struct au_branch *br;
  14617. +
  14618. + AuDebugOn(inode->i_mode
  14619. + && (h_inode->i_mode & S_IFMT)
  14620. + != (inode->i_mode & S_IFMT));
  14621. + if (bindex == iinfo->ii_bstart)
  14622. + au_cpup_igen(inode, h_inode);
  14623. + br = au_sbr(sb, bindex);
  14624. + hinode->hi_id = br->br_id;
  14625. + if (au_ftest_hi(flags, XINO)) {
  14626. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  14627. + inode->i_ino);
  14628. + if (unlikely(err))
  14629. + AuIOErr1("failed au_xino_write() %d\n", err);
  14630. + }
  14631. +
  14632. + if (au_ftest_hi(flags, HNOTIFY)
  14633. + && au_br_hnotifyable(br->br_perm)) {
  14634. + err = au_hn_alloc(hinode, inode);
  14635. + if (unlikely(err))
  14636. + AuIOErr1("au_hn_alloc() %d\n", err);
  14637. + }
  14638. + }
  14639. +}
  14640. +
  14641. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  14642. + struct dentry *h_wh)
  14643. +{
  14644. + struct au_hinode *hinode;
  14645. +
  14646. + IiMustWriteLock(inode);
  14647. +
  14648. + hinode = au_ii(inode)->ii_hinode + bindex;
  14649. + AuDebugOn(hinode->hi_whdentry);
  14650. + hinode->hi_whdentry = h_wh;
  14651. +}
  14652. +
  14653. +void au_update_iigen(struct inode *inode, int half)
  14654. +{
  14655. + struct au_iinfo *iinfo;
  14656. + struct au_iigen *iigen;
  14657. + unsigned int sigen;
  14658. +
  14659. + sigen = au_sigen(inode->i_sb);
  14660. + iinfo = au_ii(inode);
  14661. + iigen = &iinfo->ii_generation;
  14662. + spin_lock(&iinfo->ii_genspin);
  14663. + iigen->ig_generation = sigen;
  14664. + if (half)
  14665. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  14666. + else
  14667. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  14668. + spin_unlock(&iinfo->ii_genspin);
  14669. +}
  14670. +
  14671. +/* it may be called at remount time, too */
  14672. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  14673. +{
  14674. + struct au_iinfo *iinfo;
  14675. + aufs_bindex_t bindex, bend;
  14676. +
  14677. + iinfo = au_ii(inode);
  14678. + if (!iinfo)
  14679. + return;
  14680. +
  14681. + IiMustWriteLock(inode);
  14682. +
  14683. + if (do_put_zero && iinfo->ii_bstart >= 0) {
  14684. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
  14685. + bindex++) {
  14686. + struct inode *h_i;
  14687. +
  14688. + h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
  14689. + if (h_i
  14690. + && !h_i->i_nlink
  14691. + && !(h_i->i_state & I_LINKABLE))
  14692. + au_set_h_iptr(inode, bindex, NULL, 0);
  14693. + }
  14694. + }
  14695. +
  14696. + iinfo->ii_bstart = -1;
  14697. + iinfo->ii_bend = -1;
  14698. + bend = au_sbend(inode->i_sb);
  14699. + for (bindex = 0; bindex <= bend; bindex++)
  14700. + if (iinfo->ii_hinode[0 + bindex].hi_inode) {
  14701. + iinfo->ii_bstart = bindex;
  14702. + break;
  14703. + }
  14704. + if (iinfo->ii_bstart >= 0)
  14705. + for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
  14706. + if (iinfo->ii_hinode[0 + bindex].hi_inode) {
  14707. + iinfo->ii_bend = bindex;
  14708. + break;
  14709. + }
  14710. + AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
  14711. +}
  14712. +
  14713. +/* ---------------------------------------------------------------------- */
  14714. +
  14715. +void au_icntnr_init_once(void *_c)
  14716. +{
  14717. + struct au_icntnr *c = _c;
  14718. + struct au_iinfo *iinfo = &c->iinfo;
  14719. + static struct lock_class_key aufs_ii;
  14720. +
  14721. + spin_lock_init(&iinfo->ii_genspin);
  14722. + au_rw_init(&iinfo->ii_rwsem);
  14723. + au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
  14724. + inode_init_once(&c->vfs_inode);
  14725. +}
  14726. +
  14727. +int au_iinfo_init(struct inode *inode)
  14728. +{
  14729. + struct au_iinfo *iinfo;
  14730. + struct super_block *sb;
  14731. + int nbr, i;
  14732. +
  14733. + sb = inode->i_sb;
  14734. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  14735. + nbr = au_sbend(sb) + 1;
  14736. + if (unlikely(nbr <= 0))
  14737. + nbr = 1;
  14738. + iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  14739. + if (iinfo->ii_hinode) {
  14740. + au_ninodes_inc(sb);
  14741. + for (i = 0; i < nbr; i++)
  14742. + iinfo->ii_hinode[i].hi_id = -1;
  14743. +
  14744. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  14745. + iinfo->ii_bstart = -1;
  14746. + iinfo->ii_bend = -1;
  14747. + iinfo->ii_vdir = NULL;
  14748. + return 0;
  14749. + }
  14750. + return -ENOMEM;
  14751. +}
  14752. +
  14753. +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
  14754. +{
  14755. + int err, sz;
  14756. + struct au_hinode *hip;
  14757. +
  14758. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  14759. +
  14760. + err = -ENOMEM;
  14761. + sz = sizeof(*hip) * (iinfo->ii_bend + 1);
  14762. + if (!sz)
  14763. + sz = sizeof(*hip);
  14764. + hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
  14765. + if (hip) {
  14766. + iinfo->ii_hinode = hip;
  14767. + err = 0;
  14768. + }
  14769. +
  14770. + return err;
  14771. +}
  14772. +
  14773. +void au_iinfo_fin(struct inode *inode)
  14774. +{
  14775. + struct au_iinfo *iinfo;
  14776. + struct au_hinode *hi;
  14777. + struct super_block *sb;
  14778. + aufs_bindex_t bindex, bend;
  14779. + const unsigned char unlinked = !inode->i_nlink;
  14780. +
  14781. + iinfo = au_ii(inode);
  14782. + /* bad_inode case */
  14783. + if (!iinfo)
  14784. + return;
  14785. +
  14786. + sb = inode->i_sb;
  14787. + au_ninodes_dec(sb);
  14788. + if (si_pid_test(sb))
  14789. + au_xino_delete_inode(inode, unlinked);
  14790. + else {
  14791. + /*
  14792. + * it is safe to hide the dependency between sbinfo and
  14793. + * sb->s_umount.
  14794. + */
  14795. + lockdep_off();
  14796. + si_noflush_read_lock(sb);
  14797. + au_xino_delete_inode(inode, unlinked);
  14798. + si_read_unlock(sb);
  14799. + lockdep_on();
  14800. + }
  14801. +
  14802. + if (iinfo->ii_vdir)
  14803. + au_vdir_free(iinfo->ii_vdir);
  14804. +
  14805. + bindex = iinfo->ii_bstart;
  14806. + if (bindex >= 0) {
  14807. + hi = iinfo->ii_hinode + bindex;
  14808. + bend = iinfo->ii_bend;
  14809. + while (bindex++ <= bend) {
  14810. + if (hi->hi_inode)
  14811. + au_hiput(hi);
  14812. + hi++;
  14813. + }
  14814. + }
  14815. + kfree(iinfo->ii_hinode);
  14816. + iinfo->ii_hinode = NULL;
  14817. + AuRwDestroy(&iinfo->ii_rwsem);
  14818. +}
  14819. diff -Nur linux-4.1.10.orig/fs/aufs/inode.c linux-4.1.10/fs/aufs/inode.c
  14820. --- linux-4.1.10.orig/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
  14821. +++ linux-4.1.10/fs/aufs/inode.c 2015-10-22 21:35:53.000000000 +0200
  14822. @@ -0,0 +1,500 @@
  14823. +/*
  14824. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  14825. + *
  14826. + * This program, aufs is free software; you can redistribute it and/or modify
  14827. + * it under the terms of the GNU General Public License as published by
  14828. + * the Free Software Foundation; either version 2 of the License, or
  14829. + * (at your option) any later version.
  14830. + *
  14831. + * This program is distributed in the hope that it will be useful,
  14832. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14833. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14834. + * GNU General Public License for more details.
  14835. + *
  14836. + * You should have received a copy of the GNU General Public License
  14837. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14838. + */
  14839. +
  14840. +/*
  14841. + * inode functions
  14842. + */
  14843. +
  14844. +#include "aufs.h"
  14845. +
  14846. +struct inode *au_igrab(struct inode *inode)
  14847. +{
  14848. + if (inode) {
  14849. + AuDebugOn(!atomic_read(&inode->i_count));
  14850. + ihold(inode);
  14851. + }
  14852. + return inode;
  14853. +}
  14854. +
  14855. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  14856. +{
  14857. + au_cpup_attr_all(inode, /*force*/0);
  14858. + au_update_iigen(inode, /*half*/1);
  14859. + if (do_version)
  14860. + inode->i_version++;
  14861. +}
  14862. +
  14863. +static int au_ii_refresh(struct inode *inode, int *update)
  14864. +{
  14865. + int err, e;
  14866. + umode_t type;
  14867. + aufs_bindex_t bindex, new_bindex;
  14868. + struct super_block *sb;
  14869. + struct au_iinfo *iinfo;
  14870. + struct au_hinode *p, *q, tmp;
  14871. +
  14872. + IiMustWriteLock(inode);
  14873. +
  14874. + *update = 0;
  14875. + sb = inode->i_sb;
  14876. + type = inode->i_mode & S_IFMT;
  14877. + iinfo = au_ii(inode);
  14878. + err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
  14879. + if (unlikely(err))
  14880. + goto out;
  14881. +
  14882. + AuDebugOn(iinfo->ii_bstart < 0);
  14883. + p = iinfo->ii_hinode + iinfo->ii_bstart;
  14884. + for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
  14885. + bindex++, p++) {
  14886. + if (!p->hi_inode)
  14887. + continue;
  14888. +
  14889. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  14890. + new_bindex = au_br_index(sb, p->hi_id);
  14891. + if (new_bindex == bindex)
  14892. + continue;
  14893. +
  14894. + if (new_bindex < 0) {
  14895. + *update = 1;
  14896. + au_hiput(p);
  14897. + p->hi_inode = NULL;
  14898. + continue;
  14899. + }
  14900. +
  14901. + if (new_bindex < iinfo->ii_bstart)
  14902. + iinfo->ii_bstart = new_bindex;
  14903. + if (iinfo->ii_bend < new_bindex)
  14904. + iinfo->ii_bend = new_bindex;
  14905. + /* swap two lower inode, and loop again */
  14906. + q = iinfo->ii_hinode + new_bindex;
  14907. + tmp = *q;
  14908. + *q = *p;
  14909. + *p = tmp;
  14910. + if (tmp.hi_inode) {
  14911. + bindex--;
  14912. + p--;
  14913. + }
  14914. + }
  14915. + au_update_ibrange(inode, /*do_put_zero*/0);
  14916. + e = au_dy_irefresh(inode);
  14917. + if (unlikely(e && !err))
  14918. + err = e;
  14919. +
  14920. +out:
  14921. + AuTraceErr(err);
  14922. + return err;
  14923. +}
  14924. +
  14925. +int au_refresh_hinode_self(struct inode *inode)
  14926. +{
  14927. + int err, update;
  14928. +
  14929. + err = au_ii_refresh(inode, &update);
  14930. + if (!err)
  14931. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  14932. +
  14933. + AuTraceErr(err);
  14934. + return err;
  14935. +}
  14936. +
  14937. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  14938. +{
  14939. + int err, e, update;
  14940. + unsigned int flags;
  14941. + umode_t mode;
  14942. + aufs_bindex_t bindex, bend;
  14943. + unsigned char isdir;
  14944. + struct au_hinode *p;
  14945. + struct au_iinfo *iinfo;
  14946. +
  14947. + err = au_ii_refresh(inode, &update);
  14948. + if (unlikely(err))
  14949. + goto out;
  14950. +
  14951. + update = 0;
  14952. + iinfo = au_ii(inode);
  14953. + p = iinfo->ii_hinode + iinfo->ii_bstart;
  14954. + mode = (inode->i_mode & S_IFMT);
  14955. + isdir = S_ISDIR(mode);
  14956. + flags = au_hi_flags(inode, isdir);
  14957. + bend = au_dbend(dentry);
  14958. + for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
  14959. + struct inode *h_i, *h_inode;
  14960. + struct dentry *h_d;
  14961. +
  14962. + h_d = au_h_dptr(dentry, bindex);
  14963. + if (!h_d || d_is_negative(h_d))
  14964. + continue;
  14965. +
  14966. + h_inode = d_inode(h_d);
  14967. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  14968. + if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
  14969. + h_i = au_h_iptr(inode, bindex);
  14970. + if (h_i) {
  14971. + if (h_i == h_inode)
  14972. + continue;
  14973. + err = -EIO;
  14974. + break;
  14975. + }
  14976. + }
  14977. + if (bindex < iinfo->ii_bstart)
  14978. + iinfo->ii_bstart = bindex;
  14979. + if (iinfo->ii_bend < bindex)
  14980. + iinfo->ii_bend = bindex;
  14981. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  14982. + update = 1;
  14983. + }
  14984. + au_update_ibrange(inode, /*do_put_zero*/0);
  14985. + e = au_dy_irefresh(inode);
  14986. + if (unlikely(e && !err))
  14987. + err = e;
  14988. + if (!err)
  14989. + au_refresh_hinode_attr(inode, update && isdir);
  14990. +
  14991. +out:
  14992. + AuTraceErr(err);
  14993. + return err;
  14994. +}
  14995. +
  14996. +static int set_inode(struct inode *inode, struct dentry *dentry)
  14997. +{
  14998. + int err;
  14999. + unsigned int flags;
  15000. + umode_t mode;
  15001. + aufs_bindex_t bindex, bstart, btail;
  15002. + unsigned char isdir;
  15003. + struct dentry *h_dentry;
  15004. + struct inode *h_inode;
  15005. + struct au_iinfo *iinfo;
  15006. +
  15007. + IiMustWriteLock(inode);
  15008. +
  15009. + err = 0;
  15010. + isdir = 0;
  15011. + bstart = au_dbstart(dentry);
  15012. + h_dentry = au_h_dptr(dentry, bstart);
  15013. + h_inode = d_inode(h_dentry);
  15014. + mode = h_inode->i_mode;
  15015. + switch (mode & S_IFMT) {
  15016. + case S_IFREG:
  15017. + btail = au_dbtail(dentry);
  15018. + inode->i_op = &aufs_iop;
  15019. + inode->i_fop = &aufs_file_fop;
  15020. + err = au_dy_iaop(inode, bstart, h_inode);
  15021. + if (unlikely(err))
  15022. + goto out;
  15023. + break;
  15024. + case S_IFDIR:
  15025. + isdir = 1;
  15026. + btail = au_dbtaildir(dentry);
  15027. + inode->i_op = &aufs_dir_iop;
  15028. + inode->i_fop = &aufs_dir_fop;
  15029. + break;
  15030. + case S_IFLNK:
  15031. + btail = au_dbtail(dentry);
  15032. + inode->i_op = &aufs_symlink_iop;
  15033. + break;
  15034. + case S_IFBLK:
  15035. + case S_IFCHR:
  15036. + case S_IFIFO:
  15037. + case S_IFSOCK:
  15038. + btail = au_dbtail(dentry);
  15039. + inode->i_op = &aufs_iop;
  15040. + init_special_inode(inode, mode, h_inode->i_rdev);
  15041. + break;
  15042. + default:
  15043. + AuIOErr("Unknown file type 0%o\n", mode);
  15044. + err = -EIO;
  15045. + goto out;
  15046. + }
  15047. +
  15048. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  15049. + flags = au_hi_flags(inode, isdir);
  15050. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  15051. + && au_ftest_hi(flags, HNOTIFY)
  15052. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  15053. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  15054. + au_fclr_hi(flags, HNOTIFY);
  15055. + iinfo = au_ii(inode);
  15056. + iinfo->ii_bstart = bstart;
  15057. + iinfo->ii_bend = btail;
  15058. + for (bindex = bstart; bindex <= btail; bindex++) {
  15059. + h_dentry = au_h_dptr(dentry, bindex);
  15060. + if (h_dentry)
  15061. + au_set_h_iptr(inode, bindex,
  15062. + au_igrab(d_inode(h_dentry)), flags);
  15063. + }
  15064. + au_cpup_attr_all(inode, /*force*/1);
  15065. + /*
  15066. + * to force calling aufs_get_acl() every time,
  15067. + * do not call cache_no_acl() for aufs inode.
  15068. + */
  15069. +
  15070. +out:
  15071. + return err;
  15072. +}
  15073. +
  15074. +/*
  15075. + * successful returns with iinfo write_locked
  15076. + * minus: errno
  15077. + * zero: success, matched
  15078. + * plus: no error, but unmatched
  15079. + */
  15080. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  15081. +{
  15082. + int err;
  15083. + unsigned int gen;
  15084. + struct au_iigen iigen;
  15085. + aufs_bindex_t bindex, bend;
  15086. + struct inode *h_inode, *h_dinode;
  15087. + struct dentry *h_dentry;
  15088. +
  15089. + /*
  15090. + * before this function, if aufs got any iinfo lock, it must be only
  15091. + * one, the parent dir.
  15092. + * it can happen by UDBA and the obsoleted inode number.
  15093. + */
  15094. + err = -EIO;
  15095. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  15096. + goto out;
  15097. +
  15098. + err = 1;
  15099. + ii_write_lock_new_child(inode);
  15100. + h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
  15101. + h_dinode = d_inode(h_dentry);
  15102. + bend = au_ibend(inode);
  15103. + for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
  15104. + h_inode = au_h_iptr(inode, bindex);
  15105. + if (!h_inode || h_inode != h_dinode)
  15106. + continue;
  15107. +
  15108. + err = 0;
  15109. + gen = au_iigen(inode, &iigen);
  15110. + if (gen == au_digen(dentry)
  15111. + && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
  15112. + break;
  15113. +
  15114. + /* fully refresh inode using dentry */
  15115. + err = au_refresh_hinode(inode, dentry);
  15116. + if (!err)
  15117. + au_update_iigen(inode, /*half*/0);
  15118. + break;
  15119. + }
  15120. +
  15121. + if (unlikely(err))
  15122. + ii_write_unlock(inode);
  15123. +out:
  15124. + return err;
  15125. +}
  15126. +
  15127. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  15128. + unsigned int d_type, ino_t *ino)
  15129. +{
  15130. + int err;
  15131. + struct mutex *mtx;
  15132. +
  15133. + /* prevent hardlinked inode number from race condition */
  15134. + mtx = NULL;
  15135. + if (d_type != DT_DIR) {
  15136. + mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
  15137. + mutex_lock(mtx);
  15138. + }
  15139. + err = au_xino_read(sb, bindex, h_ino, ino);
  15140. + if (unlikely(err))
  15141. + goto out;
  15142. +
  15143. + if (!*ino) {
  15144. + err = -EIO;
  15145. + *ino = au_xino_new_ino(sb);
  15146. + if (unlikely(!*ino))
  15147. + goto out;
  15148. + err = au_xino_write(sb, bindex, h_ino, *ino);
  15149. + if (unlikely(err))
  15150. + goto out;
  15151. + }
  15152. +
  15153. +out:
  15154. + if (mtx)
  15155. + mutex_unlock(mtx);
  15156. + return err;
  15157. +}
  15158. +
  15159. +/* successful returns with iinfo write_locked */
  15160. +/* todo: return with unlocked? */
  15161. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  15162. +{
  15163. + struct inode *inode, *h_inode;
  15164. + struct dentry *h_dentry;
  15165. + struct super_block *sb;
  15166. + struct mutex *mtx;
  15167. + ino_t h_ino, ino;
  15168. + int err;
  15169. + aufs_bindex_t bstart;
  15170. +
  15171. + sb = dentry->d_sb;
  15172. + bstart = au_dbstart(dentry);
  15173. + h_dentry = au_h_dptr(dentry, bstart);
  15174. + h_inode = d_inode(h_dentry);
  15175. + h_ino = h_inode->i_ino;
  15176. +
  15177. + /*
  15178. + * stop 'race'-ing between hardlinks under different
  15179. + * parents.
  15180. + */
  15181. + mtx = NULL;
  15182. + if (!d_is_dir(h_dentry))
  15183. + mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
  15184. +
  15185. +new_ino:
  15186. + if (mtx)
  15187. + mutex_lock(mtx);
  15188. + err = au_xino_read(sb, bstart, h_ino, &ino);
  15189. + inode = ERR_PTR(err);
  15190. + if (unlikely(err))
  15191. + goto out;
  15192. +
  15193. + if (!ino) {
  15194. + ino = au_xino_new_ino(sb);
  15195. + if (unlikely(!ino)) {
  15196. + inode = ERR_PTR(-EIO);
  15197. + goto out;
  15198. + }
  15199. + }
  15200. +
  15201. + AuDbg("i%lu\n", (unsigned long)ino);
  15202. + inode = au_iget_locked(sb, ino);
  15203. + err = PTR_ERR(inode);
  15204. + if (IS_ERR(inode))
  15205. + goto out;
  15206. +
  15207. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  15208. + if (inode->i_state & I_NEW) {
  15209. + /* verbose coding for lock class name */
  15210. + if (unlikely(d_is_symlink(h_dentry)))
  15211. + au_rw_class(&au_ii(inode)->ii_rwsem,
  15212. + au_lc_key + AuLcSymlink_IIINFO);
  15213. + else if (unlikely(d_is_dir(h_dentry)))
  15214. + au_rw_class(&au_ii(inode)->ii_rwsem,
  15215. + au_lc_key + AuLcDir_IIINFO);
  15216. + else /* likely */
  15217. + au_rw_class(&au_ii(inode)->ii_rwsem,
  15218. + au_lc_key + AuLcNonDir_IIINFO);
  15219. +
  15220. + ii_write_lock_new_child(inode);
  15221. + err = set_inode(inode, dentry);
  15222. + if (!err) {
  15223. + unlock_new_inode(inode);
  15224. + goto out; /* success */
  15225. + }
  15226. +
  15227. + /*
  15228. + * iget_failed() calls iput(), but we need to call
  15229. + * ii_write_unlock() after iget_failed(). so dirty hack for
  15230. + * i_count.
  15231. + */
  15232. + atomic_inc(&inode->i_count);
  15233. + iget_failed(inode);
  15234. + ii_write_unlock(inode);
  15235. + au_xino_write(sb, bstart, h_ino, /*ino*/0);
  15236. + /* ignore this error */
  15237. + goto out_iput;
  15238. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  15239. + /*
  15240. + * horrible race condition between lookup, readdir and copyup
  15241. + * (or something).
  15242. + */
  15243. + if (mtx)
  15244. + mutex_unlock(mtx);
  15245. + err = reval_inode(inode, dentry);
  15246. + if (unlikely(err < 0)) {
  15247. + mtx = NULL;
  15248. + goto out_iput;
  15249. + }
  15250. +
  15251. + if (!err) {
  15252. + mtx = NULL;
  15253. + goto out; /* success */
  15254. + } else if (mtx)
  15255. + mutex_lock(mtx);
  15256. + }
  15257. +
  15258. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  15259. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  15260. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  15261. + bstart, au_sbtype(h_dentry->d_sb), dentry,
  15262. + (unsigned long)h_ino, (unsigned long)ino);
  15263. + ino = 0;
  15264. + err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
  15265. + if (!err) {
  15266. + iput(inode);
  15267. + if (mtx)
  15268. + mutex_unlock(mtx);
  15269. + goto new_ino;
  15270. + }
  15271. +
  15272. +out_iput:
  15273. + iput(inode);
  15274. + inode = ERR_PTR(err);
  15275. +out:
  15276. + if (mtx)
  15277. + mutex_unlock(mtx);
  15278. + return inode;
  15279. +}
  15280. +
  15281. +/* ---------------------------------------------------------------------- */
  15282. +
  15283. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  15284. + struct inode *inode)
  15285. +{
  15286. + int err;
  15287. + struct inode *hi;
  15288. +
  15289. + err = au_br_rdonly(au_sbr(sb, bindex));
  15290. +
  15291. + /* pseudo-link after flushed may happen out of bounds */
  15292. + if (!err
  15293. + && inode
  15294. + && au_ibstart(inode) <= bindex
  15295. + && bindex <= au_ibend(inode)) {
  15296. + /*
  15297. + * permission check is unnecessary since vfsub routine
  15298. + * will be called later
  15299. + */
  15300. + hi = au_h_iptr(inode, bindex);
  15301. + if (hi)
  15302. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  15303. + }
  15304. +
  15305. + return err;
  15306. +}
  15307. +
  15308. +int au_test_h_perm(struct inode *h_inode, int mask)
  15309. +{
  15310. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  15311. + return 0;
  15312. + return inode_permission(h_inode, mask);
  15313. +}
  15314. +
  15315. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  15316. +{
  15317. + if (au_test_nfs(h_inode->i_sb)
  15318. + && (mask & MAY_WRITE)
  15319. + && S_ISDIR(h_inode->i_mode))
  15320. + mask |= MAY_READ; /* force permission check */
  15321. + return au_test_h_perm(h_inode, mask);
  15322. +}
  15323. diff -Nur linux-4.1.10.orig/fs/aufs/inode.h linux-4.1.10/fs/aufs/inode.h
  15324. --- linux-4.1.10.orig/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
  15325. +++ linux-4.1.10/fs/aufs/inode.h 2015-10-22 21:35:53.000000000 +0200
  15326. @@ -0,0 +1,673 @@
  15327. +/*
  15328. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  15329. + *
  15330. + * This program, aufs is free software; you can redistribute it and/or modify
  15331. + * it under the terms of the GNU General Public License as published by
  15332. + * the Free Software Foundation; either version 2 of the License, or
  15333. + * (at your option) any later version.
  15334. + *
  15335. + * This program is distributed in the hope that it will be useful,
  15336. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15337. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15338. + * GNU General Public License for more details.
  15339. + *
  15340. + * You should have received a copy of the GNU General Public License
  15341. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15342. + */
  15343. +
  15344. +/*
  15345. + * inode operations
  15346. + */
  15347. +
  15348. +#ifndef __AUFS_INODE_H__
  15349. +#define __AUFS_INODE_H__
  15350. +
  15351. +#ifdef __KERNEL__
  15352. +
  15353. +#include <linux/fsnotify.h>
  15354. +#include "rwsem.h"
  15355. +
  15356. +struct vfsmount;
  15357. +
  15358. +struct au_hnotify {
  15359. +#ifdef CONFIG_AUFS_HNOTIFY
  15360. +#ifdef CONFIG_AUFS_HFSNOTIFY
  15361. + /* never use fsnotify_add_vfsmount_mark() */
  15362. + struct fsnotify_mark hn_mark;
  15363. +#endif
  15364. + struct inode *hn_aufs_inode; /* no get/put */
  15365. +#endif
  15366. +} ____cacheline_aligned_in_smp;
  15367. +
  15368. +struct au_hinode {
  15369. + struct inode *hi_inode;
  15370. + aufs_bindex_t hi_id;
  15371. +#ifdef CONFIG_AUFS_HNOTIFY
  15372. + struct au_hnotify *hi_notify;
  15373. +#endif
  15374. +
  15375. + /* reference to the copied-up whiteout with get/put */
  15376. + struct dentry *hi_whdentry;
  15377. +};
  15378. +
  15379. +/* ig_flags */
  15380. +#define AuIG_HALF_REFRESHED 1
  15381. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  15382. +#define au_ig_fset(flags, name) \
  15383. + do { (flags) |= AuIG_##name; } while (0)
  15384. +#define au_ig_fclr(flags, name) \
  15385. + do { (flags) &= ~AuIG_##name; } while (0)
  15386. +
  15387. +struct au_iigen {
  15388. + __u32 ig_generation, ig_flags;
  15389. +};
  15390. +
  15391. +struct au_vdir;
  15392. +struct au_iinfo {
  15393. + spinlock_t ii_genspin;
  15394. + struct au_iigen ii_generation;
  15395. + struct super_block *ii_hsb1; /* no get/put */
  15396. +
  15397. + struct au_rwsem ii_rwsem;
  15398. + aufs_bindex_t ii_bstart, ii_bend;
  15399. + __u32 ii_higen;
  15400. + struct au_hinode *ii_hinode;
  15401. + struct au_vdir *ii_vdir;
  15402. +};
  15403. +
  15404. +struct au_icntnr {
  15405. + struct au_iinfo iinfo;
  15406. + struct inode vfs_inode;
  15407. +} ____cacheline_aligned_in_smp;
  15408. +
  15409. +/* au_pin flags */
  15410. +#define AuPin_DI_LOCKED 1
  15411. +#define AuPin_MNT_WRITE (1 << 1)
  15412. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  15413. +#define au_fset_pin(flags, name) \
  15414. + do { (flags) |= AuPin_##name; } while (0)
  15415. +#define au_fclr_pin(flags, name) \
  15416. + do { (flags) &= ~AuPin_##name; } while (0)
  15417. +
  15418. +struct au_pin {
  15419. + /* input */
  15420. + struct dentry *dentry;
  15421. + unsigned int udba;
  15422. + unsigned char lsc_di, lsc_hi, flags;
  15423. + aufs_bindex_t bindex;
  15424. +
  15425. + /* output */
  15426. + struct dentry *parent;
  15427. + struct au_hinode *hdir;
  15428. + struct vfsmount *h_mnt;
  15429. +
  15430. + /* temporary unlock/relock for copyup */
  15431. + struct dentry *h_dentry, *h_parent;
  15432. + struct au_branch *br;
  15433. + struct task_struct *task;
  15434. +};
  15435. +
  15436. +void au_pin_hdir_unlock(struct au_pin *p);
  15437. +int au_pin_hdir_lock(struct au_pin *p);
  15438. +int au_pin_hdir_relock(struct au_pin *p);
  15439. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
  15440. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  15441. +void au_pin_hdir_release(struct au_pin *p);
  15442. +
  15443. +/* ---------------------------------------------------------------------- */
  15444. +
  15445. +static inline struct au_iinfo *au_ii(struct inode *inode)
  15446. +{
  15447. + struct au_iinfo *iinfo;
  15448. +
  15449. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  15450. + if (iinfo->ii_hinode)
  15451. + return iinfo;
  15452. + return NULL; /* debugging bad_inode case */
  15453. +}
  15454. +
  15455. +/* ---------------------------------------------------------------------- */
  15456. +
  15457. +/* inode.c */
  15458. +struct inode *au_igrab(struct inode *inode);
  15459. +int au_refresh_hinode_self(struct inode *inode);
  15460. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  15461. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  15462. + unsigned int d_type, ino_t *ino);
  15463. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  15464. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  15465. + struct inode *inode);
  15466. +int au_test_h_perm(struct inode *h_inode, int mask);
  15467. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  15468. +
  15469. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  15470. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  15471. +{
  15472. +#ifdef CONFIG_AUFS_SHWH
  15473. + return au_ino(sb, bindex, h_ino, d_type, ino);
  15474. +#else
  15475. + return 0;
  15476. +#endif
  15477. +}
  15478. +
  15479. +/* i_op.c */
  15480. +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
  15481. +
  15482. +/* au_wr_dir flags */
  15483. +#define AuWrDir_ADD_ENTRY 1
  15484. +#define AuWrDir_ISDIR (1 << 1)
  15485. +#define AuWrDir_TMPFILE (1 << 2)
  15486. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  15487. +#define au_fset_wrdir(flags, name) \
  15488. + do { (flags) |= AuWrDir_##name; } while (0)
  15489. +#define au_fclr_wrdir(flags, name) \
  15490. + do { (flags) &= ~AuWrDir_##name; } while (0)
  15491. +
  15492. +struct au_wr_dir_args {
  15493. + aufs_bindex_t force_btgt;
  15494. + unsigned char flags;
  15495. +};
  15496. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  15497. + struct au_wr_dir_args *args);
  15498. +
  15499. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  15500. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  15501. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  15502. + unsigned int udba, unsigned char flags);
  15503. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  15504. + unsigned int udba, unsigned char flags) __must_check;
  15505. +int au_do_pin(struct au_pin *pin) __must_check;
  15506. +void au_unpin(struct au_pin *pin);
  15507. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  15508. +
  15509. +#define AuIcpup_DID_CPUP 1
  15510. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  15511. +#define au_fset_icpup(flags, name) \
  15512. + do { (flags) |= AuIcpup_##name; } while (0)
  15513. +#define au_fclr_icpup(flags, name) \
  15514. + do { (flags) &= ~AuIcpup_##name; } while (0)
  15515. +
  15516. +struct au_icpup_args {
  15517. + unsigned char flags;
  15518. + unsigned char pin_flags;
  15519. + aufs_bindex_t btgt;
  15520. + unsigned int udba;
  15521. + struct au_pin pin;
  15522. + struct path h_path;
  15523. + struct inode *h_inode;
  15524. +};
  15525. +
  15526. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  15527. + struct au_icpup_args *a);
  15528. +
  15529. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
  15530. +
  15531. +/* i_op_add.c */
  15532. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  15533. + struct dentry *h_parent, int isdir);
  15534. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  15535. + dev_t dev);
  15536. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  15537. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  15538. + bool want_excl);
  15539. +struct vfsub_aopen_args;
  15540. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  15541. + struct vfsub_aopen_args *args);
  15542. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  15543. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  15544. + struct dentry *dentry);
  15545. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  15546. +
  15547. +/* i_op_del.c */
  15548. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  15549. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  15550. + struct dentry *h_parent, int isdir);
  15551. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  15552. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  15553. +
  15554. +/* i_op_ren.c */
  15555. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  15556. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  15557. + struct inode *dir, struct dentry *dentry);
  15558. +
  15559. +/* iinfo.c */
  15560. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  15561. +void au_hiput(struct au_hinode *hinode);
  15562. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  15563. + struct dentry *h_wh);
  15564. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  15565. +
  15566. +/* hinode flags */
  15567. +#define AuHi_XINO 1
  15568. +#define AuHi_HNOTIFY (1 << 1)
  15569. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  15570. +#define au_fset_hi(flags, name) \
  15571. + do { (flags) |= AuHi_##name; } while (0)
  15572. +#define au_fclr_hi(flags, name) \
  15573. + do { (flags) &= ~AuHi_##name; } while (0)
  15574. +
  15575. +#ifndef CONFIG_AUFS_HNOTIFY
  15576. +#undef AuHi_HNOTIFY
  15577. +#define AuHi_HNOTIFY 0
  15578. +#endif
  15579. +
  15580. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  15581. + struct inode *h_inode, unsigned int flags);
  15582. +
  15583. +void au_update_iigen(struct inode *inode, int half);
  15584. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  15585. +
  15586. +void au_icntnr_init_once(void *_c);
  15587. +int au_iinfo_init(struct inode *inode);
  15588. +void au_iinfo_fin(struct inode *inode);
  15589. +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
  15590. +
  15591. +#ifdef CONFIG_PROC_FS
  15592. +/* plink.c */
  15593. +int au_plink_maint(struct super_block *sb, int flags);
  15594. +struct au_sbinfo;
  15595. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  15596. +int au_plink_maint_enter(struct super_block *sb);
  15597. +#ifdef CONFIG_AUFS_DEBUG
  15598. +void au_plink_list(struct super_block *sb);
  15599. +#else
  15600. +AuStubVoid(au_plink_list, struct super_block *sb)
  15601. +#endif
  15602. +int au_plink_test(struct inode *inode);
  15603. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  15604. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  15605. + struct dentry *h_dentry);
  15606. +void au_plink_put(struct super_block *sb, int verbose);
  15607. +void au_plink_clean(struct super_block *sb, int verbose);
  15608. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  15609. +#else
  15610. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  15611. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  15612. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  15613. +AuStubVoid(au_plink_list, struct super_block *sb);
  15614. +AuStubInt0(au_plink_test, struct inode *inode);
  15615. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  15616. + struct inode *inode, aufs_bindex_t bindex);
  15617. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  15618. + struct dentry *h_dentry);
  15619. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  15620. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  15621. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  15622. +#endif /* CONFIG_PROC_FS */
  15623. +
  15624. +#ifdef CONFIG_AUFS_XATTR
  15625. +/* xattr.c */
  15626. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  15627. + unsigned int verbose);
  15628. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  15629. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  15630. + size_t size);
  15631. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  15632. + size_t size, int flags);
  15633. +int aufs_removexattr(struct dentry *dentry, const char *name);
  15634. +
  15635. +/* void au_xattr_init(struct super_block *sb); */
  15636. +#else
  15637. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  15638. + int ignore_flags, unsigned int verbose);
  15639. +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
  15640. +#endif
  15641. +
  15642. +#ifdef CONFIG_FS_POSIX_ACL
  15643. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  15644. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  15645. +#endif
  15646. +
  15647. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  15648. +enum {
  15649. + AU_XATTR_SET,
  15650. + AU_XATTR_REMOVE,
  15651. + AU_ACL_SET
  15652. +};
  15653. +
  15654. +struct au_srxattr {
  15655. + int type;
  15656. + union {
  15657. + struct {
  15658. + const char *name;
  15659. + const void *value;
  15660. + size_t size;
  15661. + int flags;
  15662. + } set;
  15663. + struct {
  15664. + const char *name;
  15665. + } remove;
  15666. + struct {
  15667. + struct posix_acl *acl;
  15668. + int type;
  15669. + } acl_set;
  15670. + } u;
  15671. +};
  15672. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
  15673. +#endif
  15674. +
  15675. +/* ---------------------------------------------------------------------- */
  15676. +
  15677. +/* lock subclass for iinfo */
  15678. +enum {
  15679. + AuLsc_II_CHILD, /* child first */
  15680. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  15681. + AuLsc_II_CHILD3, /* copyup dirs */
  15682. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  15683. + AuLsc_II_PARENT2,
  15684. + AuLsc_II_PARENT3, /* copyup dirs */
  15685. + AuLsc_II_NEW_CHILD
  15686. +};
  15687. +
  15688. +/*
  15689. + * ii_read_lock_child, ii_write_lock_child,
  15690. + * ii_read_lock_child2, ii_write_lock_child2,
  15691. + * ii_read_lock_child3, ii_write_lock_child3,
  15692. + * ii_read_lock_parent, ii_write_lock_parent,
  15693. + * ii_read_lock_parent2, ii_write_lock_parent2,
  15694. + * ii_read_lock_parent3, ii_write_lock_parent3,
  15695. + * ii_read_lock_new_child, ii_write_lock_new_child,
  15696. + */
  15697. +#define AuReadLockFunc(name, lsc) \
  15698. +static inline void ii_read_lock_##name(struct inode *i) \
  15699. +{ \
  15700. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15701. +}
  15702. +
  15703. +#define AuWriteLockFunc(name, lsc) \
  15704. +static inline void ii_write_lock_##name(struct inode *i) \
  15705. +{ \
  15706. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  15707. +}
  15708. +
  15709. +#define AuRWLockFuncs(name, lsc) \
  15710. + AuReadLockFunc(name, lsc) \
  15711. + AuWriteLockFunc(name, lsc)
  15712. +
  15713. +AuRWLockFuncs(child, CHILD);
  15714. +AuRWLockFuncs(child2, CHILD2);
  15715. +AuRWLockFuncs(child3, CHILD3);
  15716. +AuRWLockFuncs(parent, PARENT);
  15717. +AuRWLockFuncs(parent2, PARENT2);
  15718. +AuRWLockFuncs(parent3, PARENT3);
  15719. +AuRWLockFuncs(new_child, NEW_CHILD);
  15720. +
  15721. +#undef AuReadLockFunc
  15722. +#undef AuWriteLockFunc
  15723. +#undef AuRWLockFuncs
  15724. +
  15725. +/*
  15726. + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
  15727. + */
  15728. +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
  15729. +
  15730. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  15731. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  15732. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  15733. +
  15734. +/* ---------------------------------------------------------------------- */
  15735. +
  15736. +static inline void au_icntnr_init(struct au_icntnr *c)
  15737. +{
  15738. +#ifdef CONFIG_AUFS_DEBUG
  15739. + c->vfs_inode.i_mode = 0;
  15740. +#endif
  15741. +}
  15742. +
  15743. +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
  15744. +{
  15745. + unsigned int gen;
  15746. + struct au_iinfo *iinfo;
  15747. +
  15748. + iinfo = au_ii(inode);
  15749. + spin_lock(&iinfo->ii_genspin);
  15750. + if (iigen)
  15751. + *iigen = iinfo->ii_generation;
  15752. + gen = iinfo->ii_generation.ig_generation;
  15753. + spin_unlock(&iinfo->ii_genspin);
  15754. +
  15755. + return gen;
  15756. +}
  15757. +
  15758. +/* tiny test for inode number */
  15759. +/* tmpfs generation is too rough */
  15760. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  15761. +{
  15762. + struct au_iinfo *iinfo;
  15763. +
  15764. + iinfo = au_ii(inode);
  15765. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  15766. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  15767. + && iinfo->ii_higen == h_inode->i_generation);
  15768. +}
  15769. +
  15770. +static inline void au_iigen_dec(struct inode *inode)
  15771. +{
  15772. + struct au_iinfo *iinfo;
  15773. +
  15774. + iinfo = au_ii(inode);
  15775. + spin_lock(&iinfo->ii_genspin);
  15776. + iinfo->ii_generation.ig_generation--;
  15777. + spin_unlock(&iinfo->ii_genspin);
  15778. +}
  15779. +
  15780. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  15781. +{
  15782. + int err;
  15783. +
  15784. + err = 0;
  15785. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  15786. + err = -EIO;
  15787. +
  15788. + return err;
  15789. +}
  15790. +
  15791. +/* ---------------------------------------------------------------------- */
  15792. +
  15793. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  15794. + aufs_bindex_t bindex)
  15795. +{
  15796. + IiMustAnyLock(inode);
  15797. + return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
  15798. +}
  15799. +
  15800. +static inline aufs_bindex_t au_ibstart(struct inode *inode)
  15801. +{
  15802. + IiMustAnyLock(inode);
  15803. + return au_ii(inode)->ii_bstart;
  15804. +}
  15805. +
  15806. +static inline aufs_bindex_t au_ibend(struct inode *inode)
  15807. +{
  15808. + IiMustAnyLock(inode);
  15809. + return au_ii(inode)->ii_bend;
  15810. +}
  15811. +
  15812. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  15813. +{
  15814. + IiMustAnyLock(inode);
  15815. + return au_ii(inode)->ii_vdir;
  15816. +}
  15817. +
  15818. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  15819. +{
  15820. + IiMustAnyLock(inode);
  15821. + return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
  15822. +}
  15823. +
  15824. +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
  15825. +{
  15826. + IiMustWriteLock(inode);
  15827. + au_ii(inode)->ii_bstart = bindex;
  15828. +}
  15829. +
  15830. +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
  15831. +{
  15832. + IiMustWriteLock(inode);
  15833. + au_ii(inode)->ii_bend = bindex;
  15834. +}
  15835. +
  15836. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  15837. +{
  15838. + IiMustWriteLock(inode);
  15839. + au_ii(inode)->ii_vdir = vdir;
  15840. +}
  15841. +
  15842. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  15843. +{
  15844. + IiMustAnyLock(inode);
  15845. + return au_ii(inode)->ii_hinode + bindex;
  15846. +}
  15847. +
  15848. +/* ---------------------------------------------------------------------- */
  15849. +
  15850. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  15851. +{
  15852. + if (pin)
  15853. + return pin->parent;
  15854. + return NULL;
  15855. +}
  15856. +
  15857. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  15858. +{
  15859. + if (pin && pin->hdir)
  15860. + return pin->hdir->hi_inode;
  15861. + return NULL;
  15862. +}
  15863. +
  15864. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  15865. +{
  15866. + if (pin)
  15867. + return pin->hdir;
  15868. + return NULL;
  15869. +}
  15870. +
  15871. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  15872. +{
  15873. + if (pin)
  15874. + pin->dentry = dentry;
  15875. +}
  15876. +
  15877. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  15878. + unsigned char lflag)
  15879. +{
  15880. + if (pin) {
  15881. + if (lflag)
  15882. + au_fset_pin(pin->flags, DI_LOCKED);
  15883. + else
  15884. + au_fclr_pin(pin->flags, DI_LOCKED);
  15885. + }
  15886. +}
  15887. +
  15888. +#if 0 /* reserved */
  15889. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  15890. +{
  15891. + if (pin) {
  15892. + dput(pin->parent);
  15893. + pin->parent = dget(parent);
  15894. + }
  15895. +}
  15896. +#endif
  15897. +
  15898. +/* ---------------------------------------------------------------------- */
  15899. +
  15900. +struct au_branch;
  15901. +#ifdef CONFIG_AUFS_HNOTIFY
  15902. +struct au_hnotify_op {
  15903. + void (*ctl)(struct au_hinode *hinode, int do_set);
  15904. + int (*alloc)(struct au_hinode *hinode);
  15905. +
  15906. + /*
  15907. + * if it returns true, the the caller should free hinode->hi_notify,
  15908. + * otherwise ->free() frees it.
  15909. + */
  15910. + int (*free)(struct au_hinode *hinode,
  15911. + struct au_hnotify *hn) __must_check;
  15912. +
  15913. + void (*fin)(void);
  15914. + int (*init)(void);
  15915. +
  15916. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  15917. + void (*fin_br)(struct au_branch *br);
  15918. + int (*init_br)(struct au_branch *br, int perm);
  15919. +};
  15920. +
  15921. +/* hnotify.c */
  15922. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  15923. +void au_hn_free(struct au_hinode *hinode);
  15924. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  15925. +void au_hn_reset(struct inode *inode, unsigned int flags);
  15926. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  15927. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  15928. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  15929. +int au_hnotify_init_br(struct au_branch *br, int perm);
  15930. +void au_hnotify_fin_br(struct au_branch *br);
  15931. +int __init au_hnotify_init(void);
  15932. +void au_hnotify_fin(void);
  15933. +
  15934. +/* hfsnotify.c */
  15935. +extern const struct au_hnotify_op au_hnotify_op;
  15936. +
  15937. +static inline
  15938. +void au_hn_init(struct au_hinode *hinode)
  15939. +{
  15940. + hinode->hi_notify = NULL;
  15941. +}
  15942. +
  15943. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  15944. +{
  15945. + return hinode->hi_notify;
  15946. +}
  15947. +
  15948. +#else
  15949. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  15950. + struct au_hinode *hinode __maybe_unused,
  15951. + struct inode *inode __maybe_unused)
  15952. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  15953. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  15954. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  15955. + int do_set __maybe_unused)
  15956. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  15957. + unsigned int flags __maybe_unused)
  15958. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  15959. + struct au_branch *br __maybe_unused,
  15960. + int perm __maybe_unused)
  15961. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  15962. + int perm __maybe_unused)
  15963. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  15964. +AuStubInt0(__init au_hnotify_init, void)
  15965. +AuStubVoid(au_hnotify_fin, void)
  15966. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  15967. +#endif /* CONFIG_AUFS_HNOTIFY */
  15968. +
  15969. +static inline void au_hn_suspend(struct au_hinode *hdir)
  15970. +{
  15971. + au_hn_ctl(hdir, /*do_set*/0);
  15972. +}
  15973. +
  15974. +static inline void au_hn_resume(struct au_hinode *hdir)
  15975. +{
  15976. + au_hn_ctl(hdir, /*do_set*/1);
  15977. +}
  15978. +
  15979. +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
  15980. +{
  15981. + mutex_lock(&hdir->hi_inode->i_mutex);
  15982. + au_hn_suspend(hdir);
  15983. +}
  15984. +
  15985. +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
  15986. + unsigned int sc __maybe_unused)
  15987. +{
  15988. + mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
  15989. + au_hn_suspend(hdir);
  15990. +}
  15991. +
  15992. +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
  15993. +{
  15994. + au_hn_resume(hdir);
  15995. + mutex_unlock(&hdir->hi_inode->i_mutex);
  15996. +}
  15997. +
  15998. +#endif /* __KERNEL__ */
  15999. +#endif /* __AUFS_INODE_H__ */
  16000. diff -Nur linux-4.1.10.orig/fs/aufs/ioctl.c linux-4.1.10/fs/aufs/ioctl.c
  16001. --- linux-4.1.10.orig/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
  16002. +++ linux-4.1.10/fs/aufs/ioctl.c 2015-10-22 21:35:53.000000000 +0200
  16003. @@ -0,0 +1,219 @@
  16004. +/*
  16005. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  16006. + *
  16007. + * This program, aufs is free software; you can redistribute it and/or modify
  16008. + * it under the terms of the GNU General Public License as published by
  16009. + * the Free Software Foundation; either version 2 of the License, or
  16010. + * (at your option) any later version.
  16011. + *
  16012. + * This program is distributed in the hope that it will be useful,
  16013. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16014. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16015. + * GNU General Public License for more details.
  16016. + *
  16017. + * You should have received a copy of the GNU General Public License
  16018. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16019. + */
  16020. +
  16021. +/*
  16022. + * ioctl
  16023. + * plink-management and readdir in userspace.
  16024. + * assist the pathconf(3) wrapper library.
  16025. + * move-down
  16026. + * File-based Hierarchical Storage Management.
  16027. + */
  16028. +
  16029. +#include <linux/compat.h>
  16030. +#include <linux/file.h>
  16031. +#include "aufs.h"
  16032. +
  16033. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  16034. +{
  16035. + int err, fd;
  16036. + aufs_bindex_t wbi, bindex, bend;
  16037. + struct file *h_file;
  16038. + struct super_block *sb;
  16039. + struct dentry *root;
  16040. + struct au_branch *br;
  16041. + struct aufs_wbr_fd wbrfd = {
  16042. + .oflags = au_dir_roflags,
  16043. + .brid = -1
  16044. + };
  16045. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  16046. + | O_NOATIME | O_CLOEXEC;
  16047. +
  16048. + AuDebugOn(wbrfd.oflags & ~valid);
  16049. +
  16050. + if (arg) {
  16051. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  16052. + if (unlikely(err)) {
  16053. + err = -EFAULT;
  16054. + goto out;
  16055. + }
  16056. +
  16057. + err = -EINVAL;
  16058. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  16059. + wbrfd.oflags |= au_dir_roflags;
  16060. + AuDbg("0%o\n", wbrfd.oflags);
  16061. + if (unlikely(wbrfd.oflags & ~valid))
  16062. + goto out;
  16063. + }
  16064. +
  16065. + fd = get_unused_fd_flags(0);
  16066. + err = fd;
  16067. + if (unlikely(fd < 0))
  16068. + goto out;
  16069. +
  16070. + h_file = ERR_PTR(-EINVAL);
  16071. + wbi = 0;
  16072. + br = NULL;
  16073. + sb = path->dentry->d_sb;
  16074. + root = sb->s_root;
  16075. + aufs_read_lock(root, AuLock_IR);
  16076. + bend = au_sbend(sb);
  16077. + if (wbrfd.brid >= 0) {
  16078. + wbi = au_br_index(sb, wbrfd.brid);
  16079. + if (unlikely(wbi < 0 || wbi > bend))
  16080. + goto out_unlock;
  16081. + }
  16082. +
  16083. + h_file = ERR_PTR(-ENOENT);
  16084. + br = au_sbr(sb, wbi);
  16085. + if (!au_br_writable(br->br_perm)) {
  16086. + if (arg)
  16087. + goto out_unlock;
  16088. +
  16089. + bindex = wbi + 1;
  16090. + wbi = -1;
  16091. + for (; bindex <= bend; bindex++) {
  16092. + br = au_sbr(sb, bindex);
  16093. + if (au_br_writable(br->br_perm)) {
  16094. + wbi = bindex;
  16095. + br = au_sbr(sb, wbi);
  16096. + break;
  16097. + }
  16098. + }
  16099. + }
  16100. + AuDbg("wbi %d\n", wbi);
  16101. + if (wbi >= 0)
  16102. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  16103. + /*force_wr*/0);
  16104. +
  16105. +out_unlock:
  16106. + aufs_read_unlock(root, AuLock_IR);
  16107. + err = PTR_ERR(h_file);
  16108. + if (IS_ERR(h_file))
  16109. + goto out_fd;
  16110. +
  16111. + atomic_dec(&br->br_count); /* cf. au_h_open() */
  16112. + fd_install(fd, h_file);
  16113. + err = fd;
  16114. + goto out; /* success */
  16115. +
  16116. +out_fd:
  16117. + put_unused_fd(fd);
  16118. +out:
  16119. + AuTraceErr(err);
  16120. + return err;
  16121. +}
  16122. +
  16123. +/* ---------------------------------------------------------------------- */
  16124. +
  16125. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  16126. +{
  16127. + long err;
  16128. + struct dentry *dentry;
  16129. +
  16130. + switch (cmd) {
  16131. + case AUFS_CTL_RDU:
  16132. + case AUFS_CTL_RDU_INO:
  16133. + err = au_rdu_ioctl(file, cmd, arg);
  16134. + break;
  16135. +
  16136. + case AUFS_CTL_WBR_FD:
  16137. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  16138. + break;
  16139. +
  16140. + case AUFS_CTL_IBUSY:
  16141. + err = au_ibusy_ioctl(file, arg);
  16142. + break;
  16143. +
  16144. + case AUFS_CTL_BRINFO:
  16145. + err = au_brinfo_ioctl(file, arg);
  16146. + break;
  16147. +
  16148. + case AUFS_CTL_FHSM_FD:
  16149. + dentry = file->f_path.dentry;
  16150. + if (IS_ROOT(dentry))
  16151. + err = au_fhsm_fd(dentry->d_sb, arg);
  16152. + else
  16153. + err = -ENOTTY;
  16154. + break;
  16155. +
  16156. + default:
  16157. + /* do not call the lower */
  16158. + AuDbg("0x%x\n", cmd);
  16159. + err = -ENOTTY;
  16160. + }
  16161. +
  16162. + AuTraceErr(err);
  16163. + return err;
  16164. +}
  16165. +
  16166. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  16167. +{
  16168. + long err;
  16169. +
  16170. + switch (cmd) {
  16171. + case AUFS_CTL_MVDOWN:
  16172. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  16173. + break;
  16174. +
  16175. + case AUFS_CTL_WBR_FD:
  16176. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  16177. + break;
  16178. +
  16179. + default:
  16180. + /* do not call the lower */
  16181. + AuDbg("0x%x\n", cmd);
  16182. + err = -ENOTTY;
  16183. + }
  16184. +
  16185. + AuTraceErr(err);
  16186. + return err;
  16187. +}
  16188. +
  16189. +#ifdef CONFIG_COMPAT
  16190. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  16191. + unsigned long arg)
  16192. +{
  16193. + long err;
  16194. +
  16195. + switch (cmd) {
  16196. + case AUFS_CTL_RDU:
  16197. + case AUFS_CTL_RDU_INO:
  16198. + err = au_rdu_compat_ioctl(file, cmd, arg);
  16199. + break;
  16200. +
  16201. + case AUFS_CTL_IBUSY:
  16202. + err = au_ibusy_compat_ioctl(file, arg);
  16203. + break;
  16204. +
  16205. + case AUFS_CTL_BRINFO:
  16206. + err = au_brinfo_compat_ioctl(file, arg);
  16207. + break;
  16208. +
  16209. + default:
  16210. + err = aufs_ioctl_dir(file, cmd, arg);
  16211. + }
  16212. +
  16213. + AuTraceErr(err);
  16214. + return err;
  16215. +}
  16216. +
  16217. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  16218. + unsigned long arg)
  16219. +{
  16220. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  16221. +}
  16222. +#endif
  16223. diff -Nur linux-4.1.10.orig/fs/aufs/i_op_add.c linux-4.1.10/fs/aufs/i_op_add.c
  16224. --- linux-4.1.10.orig/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
  16225. +++ linux-4.1.10/fs/aufs/i_op_add.c 2015-10-22 21:35:53.000000000 +0200
  16226. @@ -0,0 +1,932 @@
  16227. +/*
  16228. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  16229. + *
  16230. + * This program, aufs is free software; you can redistribute it and/or modify
  16231. + * it under the terms of the GNU General Public License as published by
  16232. + * the Free Software Foundation; either version 2 of the License, or
  16233. + * (at your option) any later version.
  16234. + *
  16235. + * This program is distributed in the hope that it will be useful,
  16236. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16237. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16238. + * GNU General Public License for more details.
  16239. + *
  16240. + * You should have received a copy of the GNU General Public License
  16241. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16242. + */
  16243. +
  16244. +/*
  16245. + * inode operations (add entry)
  16246. + */
  16247. +
  16248. +#include "aufs.h"
  16249. +
  16250. +/*
  16251. + * final procedure of adding a new entry, except link(2).
  16252. + * remove whiteout, instantiate, copyup the parent dir's times and size
  16253. + * and update version.
  16254. + * if it failed, re-create the removed whiteout.
  16255. + */
  16256. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  16257. + struct dentry *wh_dentry, struct dentry *dentry)
  16258. +{
  16259. + int err, rerr;
  16260. + aufs_bindex_t bwh;
  16261. + struct path h_path;
  16262. + struct super_block *sb;
  16263. + struct inode *inode, *h_dir;
  16264. + struct dentry *wh;
  16265. +
  16266. + bwh = -1;
  16267. + sb = dir->i_sb;
  16268. + if (wh_dentry) {
  16269. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  16270. + IMustLock(h_dir);
  16271. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  16272. + bwh = au_dbwh(dentry);
  16273. + h_path.dentry = wh_dentry;
  16274. + h_path.mnt = au_sbr_mnt(sb, bindex);
  16275. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  16276. + dentry);
  16277. + if (unlikely(err))
  16278. + goto out;
  16279. + }
  16280. +
  16281. + inode = au_new_inode(dentry, /*must_new*/1);
  16282. + if (!IS_ERR(inode)) {
  16283. + d_instantiate(dentry, inode);
  16284. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  16285. + IMustLock(dir);
  16286. + au_dir_ts(dir, bindex);
  16287. + dir->i_version++;
  16288. + au_fhsm_wrote(sb, bindex, /*force*/0);
  16289. + return 0; /* success */
  16290. + }
  16291. +
  16292. + err = PTR_ERR(inode);
  16293. + if (!wh_dentry)
  16294. + goto out;
  16295. +
  16296. + /* revert */
  16297. + /* dir inode is locked */
  16298. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  16299. + rerr = PTR_ERR(wh);
  16300. + if (IS_ERR(wh)) {
  16301. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  16302. + dentry, err, rerr);
  16303. + err = -EIO;
  16304. + } else
  16305. + dput(wh);
  16306. +
  16307. +out:
  16308. + return err;
  16309. +}
  16310. +
  16311. +static int au_d_may_add(struct dentry *dentry)
  16312. +{
  16313. + int err;
  16314. +
  16315. + err = 0;
  16316. + if (unlikely(d_unhashed(dentry)))
  16317. + err = -ENOENT;
  16318. + if (unlikely(d_really_is_positive(dentry)))
  16319. + err = -EEXIST;
  16320. + return err;
  16321. +}
  16322. +
  16323. +/*
  16324. + * simple tests for the adding inode operations.
  16325. + * following the checks in vfs, plus the parent-child relationship.
  16326. + */
  16327. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  16328. + struct dentry *h_parent, int isdir)
  16329. +{
  16330. + int err;
  16331. + umode_t h_mode;
  16332. + struct dentry *h_dentry;
  16333. + struct inode *h_inode;
  16334. +
  16335. + err = -ENAMETOOLONG;
  16336. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16337. + goto out;
  16338. +
  16339. + h_dentry = au_h_dptr(dentry, bindex);
  16340. + if (d_really_is_negative(dentry)) {
  16341. + err = -EEXIST;
  16342. + if (unlikely(d_is_positive(h_dentry)))
  16343. + goto out;
  16344. + } else {
  16345. + /* rename(2) case */
  16346. + err = -EIO;
  16347. + if (unlikely(d_is_negative(h_dentry)))
  16348. + goto out;
  16349. + h_inode = d_inode(h_dentry);
  16350. + if (unlikely(!h_inode->i_nlink))
  16351. + goto out;
  16352. +
  16353. + h_mode = h_inode->i_mode;
  16354. + if (!isdir) {
  16355. + err = -EISDIR;
  16356. + if (unlikely(S_ISDIR(h_mode)))
  16357. + goto out;
  16358. + } else if (unlikely(!S_ISDIR(h_mode))) {
  16359. + err = -ENOTDIR;
  16360. + goto out;
  16361. + }
  16362. + }
  16363. +
  16364. + err = 0;
  16365. + /* expected parent dir is locked */
  16366. + if (unlikely(h_parent != h_dentry->d_parent))
  16367. + err = -EIO;
  16368. +
  16369. +out:
  16370. + AuTraceErr(err);
  16371. + return err;
  16372. +}
  16373. +
  16374. +/*
  16375. + * initial procedure of adding a new entry.
  16376. + * prepare writable branch and the parent dir, lock it,
  16377. + * and lookup whiteout for the new entry.
  16378. + */
  16379. +static struct dentry*
  16380. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  16381. + struct dentry *src_dentry, struct au_pin *pin,
  16382. + struct au_wr_dir_args *wr_dir_args)
  16383. +{
  16384. + struct dentry *wh_dentry, *h_parent;
  16385. + struct super_block *sb;
  16386. + struct au_branch *br;
  16387. + int err;
  16388. + unsigned int udba;
  16389. + aufs_bindex_t bcpup;
  16390. +
  16391. + AuDbg("%pd\n", dentry);
  16392. +
  16393. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  16394. + bcpup = err;
  16395. + wh_dentry = ERR_PTR(err);
  16396. + if (unlikely(err < 0))
  16397. + goto out;
  16398. +
  16399. + sb = dentry->d_sb;
  16400. + udba = au_opt_udba(sb);
  16401. + err = au_pin(pin, dentry, bcpup, udba,
  16402. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16403. + wh_dentry = ERR_PTR(err);
  16404. + if (unlikely(err))
  16405. + goto out;
  16406. +
  16407. + h_parent = au_pinned_h_parent(pin);
  16408. + if (udba != AuOpt_UDBA_NONE
  16409. + && au_dbstart(dentry) == bcpup)
  16410. + err = au_may_add(dentry, bcpup, h_parent,
  16411. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  16412. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  16413. + err = -ENAMETOOLONG;
  16414. + wh_dentry = ERR_PTR(err);
  16415. + if (unlikely(err))
  16416. + goto out_unpin;
  16417. +
  16418. + br = au_sbr(sb, bcpup);
  16419. + if (dt) {
  16420. + struct path tmp = {
  16421. + .dentry = h_parent,
  16422. + .mnt = au_br_mnt(br)
  16423. + };
  16424. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  16425. + }
  16426. +
  16427. + wh_dentry = NULL;
  16428. + if (bcpup != au_dbwh(dentry))
  16429. + goto out; /* success */
  16430. +
  16431. + /*
  16432. + * ENAMETOOLONG here means that if we allowed create such name, then it
  16433. + * would not be able to removed in the future. So we don't allow such
  16434. + * name here and we don't handle ENAMETOOLONG differently here.
  16435. + */
  16436. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  16437. +
  16438. +out_unpin:
  16439. + if (IS_ERR(wh_dentry))
  16440. + au_unpin(pin);
  16441. +out:
  16442. + return wh_dentry;
  16443. +}
  16444. +
  16445. +/* ---------------------------------------------------------------------- */
  16446. +
  16447. +enum { Mknod, Symlink, Creat };
  16448. +struct simple_arg {
  16449. + int type;
  16450. + union {
  16451. + struct {
  16452. + umode_t mode;
  16453. + bool want_excl;
  16454. + bool try_aopen;
  16455. + struct vfsub_aopen_args *aopen;
  16456. + } c;
  16457. + struct {
  16458. + const char *symname;
  16459. + } s;
  16460. + struct {
  16461. + umode_t mode;
  16462. + dev_t dev;
  16463. + } m;
  16464. + } u;
  16465. +};
  16466. +
  16467. +static int add_simple(struct inode *dir, struct dentry *dentry,
  16468. + struct simple_arg *arg)
  16469. +{
  16470. + int err, rerr;
  16471. + aufs_bindex_t bstart;
  16472. + unsigned char created;
  16473. + const unsigned char try_aopen
  16474. + = (arg->type == Creat && arg->u.c.try_aopen);
  16475. + struct dentry *wh_dentry, *parent;
  16476. + struct inode *h_dir;
  16477. + struct super_block *sb;
  16478. + struct au_branch *br;
  16479. + /* to reuduce stack size */
  16480. + struct {
  16481. + struct au_dtime dt;
  16482. + struct au_pin pin;
  16483. + struct path h_path;
  16484. + struct au_wr_dir_args wr_dir_args;
  16485. + } *a;
  16486. +
  16487. + AuDbg("%pd\n", dentry);
  16488. + IMustLock(dir);
  16489. +
  16490. + err = -ENOMEM;
  16491. + a = kmalloc(sizeof(*a), GFP_NOFS);
  16492. + if (unlikely(!a))
  16493. + goto out;
  16494. + a->wr_dir_args.force_btgt = -1;
  16495. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  16496. +
  16497. + parent = dentry->d_parent; /* dir inode is locked */
  16498. + if (!try_aopen) {
  16499. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  16500. + if (unlikely(err))
  16501. + goto out_free;
  16502. + }
  16503. + err = au_d_may_add(dentry);
  16504. + if (unlikely(err))
  16505. + goto out_unlock;
  16506. + if (!try_aopen)
  16507. + di_write_lock_parent(parent);
  16508. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  16509. + &a->pin, &a->wr_dir_args);
  16510. + err = PTR_ERR(wh_dentry);
  16511. + if (IS_ERR(wh_dentry))
  16512. + goto out_parent;
  16513. +
  16514. + bstart = au_dbstart(dentry);
  16515. + sb = dentry->d_sb;
  16516. + br = au_sbr(sb, bstart);
  16517. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  16518. + a->h_path.mnt = au_br_mnt(br);
  16519. + h_dir = au_pinned_h_dir(&a->pin);
  16520. + switch (arg->type) {
  16521. + case Creat:
  16522. + err = 0;
  16523. + if (!try_aopen || !h_dir->i_op->atomic_open)
  16524. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  16525. + arg->u.c.want_excl);
  16526. + else
  16527. + err = vfsub_atomic_open(h_dir, a->h_path.dentry,
  16528. + arg->u.c.aopen, br);
  16529. + break;
  16530. + case Symlink:
  16531. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  16532. + break;
  16533. + case Mknod:
  16534. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  16535. + arg->u.m.dev);
  16536. + break;
  16537. + default:
  16538. + BUG();
  16539. + }
  16540. + created = !err;
  16541. + if (!err)
  16542. + err = epilog(dir, bstart, wh_dentry, dentry);
  16543. +
  16544. + /* revert */
  16545. + if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
  16546. + /* no delegation since it is just created */
  16547. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  16548. + /*force*/0);
  16549. + if (rerr) {
  16550. + AuIOErr("%pd revert failure(%d, %d)\n",
  16551. + dentry, err, rerr);
  16552. + err = -EIO;
  16553. + }
  16554. + au_dtime_revert(&a->dt);
  16555. + }
  16556. +
  16557. + if (!err && try_aopen && !h_dir->i_op->atomic_open)
  16558. + *arg->u.c.aopen->opened |= FILE_CREATED;
  16559. +
  16560. + au_unpin(&a->pin);
  16561. + dput(wh_dentry);
  16562. +
  16563. +out_parent:
  16564. + if (!try_aopen)
  16565. + di_write_unlock(parent);
  16566. +out_unlock:
  16567. + if (unlikely(err)) {
  16568. + au_update_dbstart(dentry);
  16569. + d_drop(dentry);
  16570. + }
  16571. + if (!try_aopen)
  16572. + aufs_read_unlock(dentry, AuLock_DW);
  16573. +out_free:
  16574. + kfree(a);
  16575. +out:
  16576. + return err;
  16577. +}
  16578. +
  16579. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  16580. + dev_t dev)
  16581. +{
  16582. + struct simple_arg arg = {
  16583. + .type = Mknod,
  16584. + .u.m = {
  16585. + .mode = mode,
  16586. + .dev = dev
  16587. + }
  16588. + };
  16589. + return add_simple(dir, dentry, &arg);
  16590. +}
  16591. +
  16592. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  16593. +{
  16594. + struct simple_arg arg = {
  16595. + .type = Symlink,
  16596. + .u.s.symname = symname
  16597. + };
  16598. + return add_simple(dir, dentry, &arg);
  16599. +}
  16600. +
  16601. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  16602. + bool want_excl)
  16603. +{
  16604. + struct simple_arg arg = {
  16605. + .type = Creat,
  16606. + .u.c = {
  16607. + .mode = mode,
  16608. + .want_excl = want_excl
  16609. + }
  16610. + };
  16611. + return add_simple(dir, dentry, &arg);
  16612. +}
  16613. +
  16614. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  16615. + struct vfsub_aopen_args *aopen_args)
  16616. +{
  16617. + struct simple_arg arg = {
  16618. + .type = Creat,
  16619. + .u.c = {
  16620. + .mode = aopen_args->create_mode,
  16621. + .want_excl = aopen_args->open_flag & O_EXCL,
  16622. + .try_aopen = true,
  16623. + .aopen = aopen_args
  16624. + }
  16625. + };
  16626. + return add_simple(dir, dentry, &arg);
  16627. +}
  16628. +
  16629. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  16630. +{
  16631. + int err;
  16632. + aufs_bindex_t bindex;
  16633. + struct super_block *sb;
  16634. + struct dentry *parent, *h_parent, *h_dentry;
  16635. + struct inode *h_dir, *inode;
  16636. + struct vfsmount *h_mnt;
  16637. + struct au_wr_dir_args wr_dir_args = {
  16638. + .force_btgt = -1,
  16639. + .flags = AuWrDir_TMPFILE
  16640. + };
  16641. +
  16642. + /* copy-up may happen */
  16643. + mutex_lock(&dir->i_mutex);
  16644. +
  16645. + sb = dir->i_sb;
  16646. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  16647. + if (unlikely(err))
  16648. + goto out;
  16649. +
  16650. + err = au_di_init(dentry);
  16651. + if (unlikely(err))
  16652. + goto out_si;
  16653. +
  16654. + err = -EBUSY;
  16655. + parent = d_find_any_alias(dir);
  16656. + AuDebugOn(!parent);
  16657. + di_write_lock_parent(parent);
  16658. + if (unlikely(d_inode(parent) != dir))
  16659. + goto out_parent;
  16660. +
  16661. + err = au_digen_test(parent, au_sigen(sb));
  16662. + if (unlikely(err))
  16663. + goto out_parent;
  16664. +
  16665. + bindex = au_dbstart(parent);
  16666. + au_set_dbstart(dentry, bindex);
  16667. + au_set_dbend(dentry, bindex);
  16668. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  16669. + bindex = err;
  16670. + if (unlikely(err < 0))
  16671. + goto out_parent;
  16672. +
  16673. + err = -EOPNOTSUPP;
  16674. + h_dir = au_h_iptr(dir, bindex);
  16675. + if (unlikely(!h_dir->i_op->tmpfile))
  16676. + goto out_parent;
  16677. +
  16678. + h_mnt = au_sbr_mnt(sb, bindex);
  16679. + err = vfsub_mnt_want_write(h_mnt);
  16680. + if (unlikely(err))
  16681. + goto out_parent;
  16682. +
  16683. + h_parent = au_h_dptr(parent, bindex);
  16684. + err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
  16685. + if (unlikely(err))
  16686. + goto out_mnt;
  16687. +
  16688. + err = -ENOMEM;
  16689. + h_dentry = d_alloc(h_parent, &dentry->d_name);
  16690. + if (unlikely(!h_dentry))
  16691. + goto out_mnt;
  16692. +
  16693. + err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
  16694. + if (unlikely(err))
  16695. + goto out_dentry;
  16696. +
  16697. + au_set_dbstart(dentry, bindex);
  16698. + au_set_dbend(dentry, bindex);
  16699. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  16700. + inode = au_new_inode(dentry, /*must_new*/1);
  16701. + if (IS_ERR(inode)) {
  16702. + err = PTR_ERR(inode);
  16703. + au_set_h_dptr(dentry, bindex, NULL);
  16704. + au_set_dbstart(dentry, -1);
  16705. + au_set_dbend(dentry, -1);
  16706. + } else {
  16707. + if (!inode->i_nlink)
  16708. + set_nlink(inode, 1);
  16709. + d_tmpfile(dentry, inode);
  16710. + au_di(dentry)->di_tmpfile = 1;
  16711. +
  16712. + /* update without i_mutex */
  16713. + if (au_ibstart(dir) == au_dbstart(dentry))
  16714. + au_cpup_attr_timesizes(dir);
  16715. + }
  16716. +
  16717. +out_dentry:
  16718. + dput(h_dentry);
  16719. +out_mnt:
  16720. + vfsub_mnt_drop_write(h_mnt);
  16721. +out_parent:
  16722. + di_write_unlock(parent);
  16723. + dput(parent);
  16724. + di_write_unlock(dentry);
  16725. + if (!err)
  16726. +#if 0
  16727. + /* verbose coding for lock class name */
  16728. + au_rw_class(&au_di(dentry)->di_rwsem,
  16729. + au_lc_key + AuLcNonDir_DIINFO);
  16730. +#else
  16731. + ;
  16732. +#endif
  16733. + else {
  16734. + au_di_fin(dentry);
  16735. + dentry->d_fsdata = NULL;
  16736. + }
  16737. +out_si:
  16738. + si_read_unlock(sb);
  16739. +out:
  16740. + mutex_unlock(&dir->i_mutex);
  16741. + return err;
  16742. +}
  16743. +
  16744. +/* ---------------------------------------------------------------------- */
  16745. +
  16746. +struct au_link_args {
  16747. + aufs_bindex_t bdst, bsrc;
  16748. + struct au_pin pin;
  16749. + struct path h_path;
  16750. + struct dentry *src_parent, *parent;
  16751. +};
  16752. +
  16753. +static int au_cpup_before_link(struct dentry *src_dentry,
  16754. + struct au_link_args *a)
  16755. +{
  16756. + int err;
  16757. + struct dentry *h_src_dentry;
  16758. + struct au_cp_generic cpg = {
  16759. + .dentry = src_dentry,
  16760. + .bdst = a->bdst,
  16761. + .bsrc = a->bsrc,
  16762. + .len = -1,
  16763. + .pin = &a->pin,
  16764. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  16765. + };
  16766. +
  16767. + di_read_lock_parent(a->src_parent, AuLock_IR);
  16768. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  16769. + if (unlikely(err))
  16770. + goto out;
  16771. +
  16772. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  16773. + err = au_pin(&a->pin, src_dentry, a->bdst,
  16774. + au_opt_udba(src_dentry->d_sb),
  16775. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16776. + if (unlikely(err))
  16777. + goto out;
  16778. +
  16779. + err = au_sio_cpup_simple(&cpg);
  16780. + au_unpin(&a->pin);
  16781. +
  16782. +out:
  16783. + di_read_unlock(a->src_parent, AuLock_IR);
  16784. + return err;
  16785. +}
  16786. +
  16787. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  16788. + struct au_link_args *a)
  16789. +{
  16790. + int err;
  16791. + unsigned char plink;
  16792. + aufs_bindex_t bend;
  16793. + struct dentry *h_src_dentry;
  16794. + struct inode *h_inode, *inode, *delegated;
  16795. + struct super_block *sb;
  16796. + struct file *h_file;
  16797. +
  16798. + plink = 0;
  16799. + h_inode = NULL;
  16800. + sb = src_dentry->d_sb;
  16801. + inode = d_inode(src_dentry);
  16802. + if (au_ibstart(inode) <= a->bdst)
  16803. + h_inode = au_h_iptr(inode, a->bdst);
  16804. + if (!h_inode || !h_inode->i_nlink) {
  16805. + /* copyup src_dentry as the name of dentry. */
  16806. + bend = au_dbend(dentry);
  16807. + if (bend < a->bsrc)
  16808. + au_set_dbend(dentry, a->bsrc);
  16809. + au_set_h_dptr(dentry, a->bsrc,
  16810. + dget(au_h_dptr(src_dentry, a->bsrc)));
  16811. + dget(a->h_path.dentry);
  16812. + au_set_h_dptr(dentry, a->bdst, NULL);
  16813. + AuDbg("temporary d_inode...\n");
  16814. + spin_lock(&dentry->d_lock);
  16815. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  16816. + spin_unlock(&dentry->d_lock);
  16817. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  16818. + if (IS_ERR(h_file))
  16819. + err = PTR_ERR(h_file);
  16820. + else {
  16821. + struct au_cp_generic cpg = {
  16822. + .dentry = dentry,
  16823. + .bdst = a->bdst,
  16824. + .bsrc = -1,
  16825. + .len = -1,
  16826. + .pin = &a->pin,
  16827. + .flags = AuCpup_KEEPLINO
  16828. + };
  16829. + err = au_sio_cpup_simple(&cpg);
  16830. + au_h_open_post(dentry, a->bsrc, h_file);
  16831. + if (!err) {
  16832. + dput(a->h_path.dentry);
  16833. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  16834. + } else
  16835. + au_set_h_dptr(dentry, a->bdst,
  16836. + a->h_path.dentry);
  16837. + }
  16838. + spin_lock(&dentry->d_lock);
  16839. + dentry->d_inode = NULL; /* restore */
  16840. + spin_unlock(&dentry->d_lock);
  16841. + AuDbg("temporary d_inode...done\n");
  16842. + au_set_h_dptr(dentry, a->bsrc, NULL);
  16843. + au_set_dbend(dentry, bend);
  16844. + } else {
  16845. + /* the inode of src_dentry already exists on a.bdst branch */
  16846. + h_src_dentry = d_find_alias(h_inode);
  16847. + if (!h_src_dentry && au_plink_test(inode)) {
  16848. + plink = 1;
  16849. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  16850. + err = PTR_ERR(h_src_dentry);
  16851. + if (IS_ERR(h_src_dentry))
  16852. + goto out;
  16853. +
  16854. + if (unlikely(d_is_negative(h_src_dentry))) {
  16855. + dput(h_src_dentry);
  16856. + h_src_dentry = NULL;
  16857. + }
  16858. +
  16859. + }
  16860. + if (h_src_dentry) {
  16861. + delegated = NULL;
  16862. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  16863. + &a->h_path, &delegated);
  16864. + if (unlikely(err == -EWOULDBLOCK)) {
  16865. + pr_warn("cannot retry for NFSv4 delegation"
  16866. + " for an internal link\n");
  16867. + iput(delegated);
  16868. + }
  16869. + dput(h_src_dentry);
  16870. + } else {
  16871. + AuIOErr("no dentry found for hi%lu on b%d\n",
  16872. + h_inode->i_ino, a->bdst);
  16873. + err = -EIO;
  16874. + }
  16875. + }
  16876. +
  16877. + if (!err && !plink)
  16878. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  16879. +
  16880. +out:
  16881. + AuTraceErr(err);
  16882. + return err;
  16883. +}
  16884. +
  16885. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  16886. + struct dentry *dentry)
  16887. +{
  16888. + int err, rerr;
  16889. + struct au_dtime dt;
  16890. + struct au_link_args *a;
  16891. + struct dentry *wh_dentry, *h_src_dentry;
  16892. + struct inode *inode, *delegated;
  16893. + struct super_block *sb;
  16894. + struct au_wr_dir_args wr_dir_args = {
  16895. + /* .force_btgt = -1, */
  16896. + .flags = AuWrDir_ADD_ENTRY
  16897. + };
  16898. +
  16899. + IMustLock(dir);
  16900. + inode = d_inode(src_dentry);
  16901. + IMustLock(inode);
  16902. +
  16903. + err = -ENOMEM;
  16904. + a = kzalloc(sizeof(*a), GFP_NOFS);
  16905. + if (unlikely(!a))
  16906. + goto out;
  16907. +
  16908. + a->parent = dentry->d_parent; /* dir inode is locked */
  16909. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  16910. + AuLock_NOPLM | AuLock_GEN);
  16911. + if (unlikely(err))
  16912. + goto out_kfree;
  16913. + err = au_d_linkable(src_dentry);
  16914. + if (unlikely(err))
  16915. + goto out_unlock;
  16916. + err = au_d_may_add(dentry);
  16917. + if (unlikely(err))
  16918. + goto out_unlock;
  16919. +
  16920. + a->src_parent = dget_parent(src_dentry);
  16921. + wr_dir_args.force_btgt = au_ibstart(inode);
  16922. +
  16923. + di_write_lock_parent(a->parent);
  16924. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  16925. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  16926. + &wr_dir_args);
  16927. + err = PTR_ERR(wh_dentry);
  16928. + if (IS_ERR(wh_dentry))
  16929. + goto out_parent;
  16930. +
  16931. + err = 0;
  16932. + sb = dentry->d_sb;
  16933. + a->bdst = au_dbstart(dentry);
  16934. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  16935. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  16936. + a->bsrc = au_ibstart(inode);
  16937. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  16938. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  16939. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  16940. + if (!h_src_dentry) {
  16941. + a->bsrc = au_dbstart(src_dentry);
  16942. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  16943. + AuDebugOn(!h_src_dentry);
  16944. + } else if (IS_ERR(h_src_dentry)) {
  16945. + err = PTR_ERR(h_src_dentry);
  16946. + goto out_parent;
  16947. + }
  16948. +
  16949. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  16950. + if (a->bdst < a->bsrc
  16951. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  16952. + err = au_cpup_or_link(src_dentry, dentry, a);
  16953. + else {
  16954. + delegated = NULL;
  16955. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  16956. + &a->h_path, &delegated);
  16957. + if (unlikely(err == -EWOULDBLOCK)) {
  16958. + pr_warn("cannot retry for NFSv4 delegation"
  16959. + " for an internal link\n");
  16960. + iput(delegated);
  16961. + }
  16962. + }
  16963. + dput(h_src_dentry);
  16964. + } else {
  16965. + /*
  16966. + * copyup src_dentry to the branch we process,
  16967. + * and then link(2) to it.
  16968. + */
  16969. + dput(h_src_dentry);
  16970. + if (a->bdst < a->bsrc
  16971. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  16972. + au_unpin(&a->pin);
  16973. + di_write_unlock(a->parent);
  16974. + err = au_cpup_before_link(src_dentry, a);
  16975. + di_write_lock_parent(a->parent);
  16976. + if (!err)
  16977. + err = au_pin(&a->pin, dentry, a->bdst,
  16978. + au_opt_udba(sb),
  16979. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  16980. + if (unlikely(err))
  16981. + goto out_wh;
  16982. + }
  16983. + if (!err) {
  16984. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  16985. + err = -ENOENT;
  16986. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  16987. + delegated = NULL;
  16988. + err = vfsub_link(h_src_dentry,
  16989. + au_pinned_h_dir(&a->pin),
  16990. + &a->h_path, &delegated);
  16991. + if (unlikely(err == -EWOULDBLOCK)) {
  16992. + pr_warn("cannot retry"
  16993. + " for NFSv4 delegation"
  16994. + " for an internal link\n");
  16995. + iput(delegated);
  16996. + }
  16997. + }
  16998. + }
  16999. + }
  17000. + if (unlikely(err))
  17001. + goto out_unpin;
  17002. +
  17003. + if (wh_dentry) {
  17004. + a->h_path.dentry = wh_dentry;
  17005. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  17006. + dentry);
  17007. + if (unlikely(err))
  17008. + goto out_revert;
  17009. + }
  17010. +
  17011. + au_dir_ts(dir, a->bdst);
  17012. + dir->i_version++;
  17013. + inc_nlink(inode);
  17014. + inode->i_ctime = dir->i_ctime;
  17015. + d_instantiate(dentry, au_igrab(inode));
  17016. + if (d_unhashed(a->h_path.dentry))
  17017. + /* some filesystem calls d_drop() */
  17018. + d_drop(dentry);
  17019. + /* some filesystems consume an inode even hardlink */
  17020. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  17021. + goto out_unpin; /* success */
  17022. +
  17023. +out_revert:
  17024. + /* no delegation since it is just created */
  17025. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  17026. + /*delegated*/NULL, /*force*/0);
  17027. + if (unlikely(rerr)) {
  17028. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  17029. + err = -EIO;
  17030. + }
  17031. + au_dtime_revert(&dt);
  17032. +out_unpin:
  17033. + au_unpin(&a->pin);
  17034. +out_wh:
  17035. + dput(wh_dentry);
  17036. +out_parent:
  17037. + di_write_unlock(a->parent);
  17038. + dput(a->src_parent);
  17039. +out_unlock:
  17040. + if (unlikely(err)) {
  17041. + au_update_dbstart(dentry);
  17042. + d_drop(dentry);
  17043. + }
  17044. + aufs_read_and_write_unlock2(dentry, src_dentry);
  17045. +out_kfree:
  17046. + kfree(a);
  17047. +out:
  17048. + AuTraceErr(err);
  17049. + return err;
  17050. +}
  17051. +
  17052. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  17053. +{
  17054. + int err, rerr;
  17055. + aufs_bindex_t bindex;
  17056. + unsigned char diropq;
  17057. + struct path h_path;
  17058. + struct dentry *wh_dentry, *parent, *opq_dentry;
  17059. + struct mutex *h_mtx;
  17060. + struct super_block *sb;
  17061. + struct {
  17062. + struct au_pin pin;
  17063. + struct au_dtime dt;
  17064. + } *a; /* reduce the stack usage */
  17065. + struct au_wr_dir_args wr_dir_args = {
  17066. + .force_btgt = -1,
  17067. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  17068. + };
  17069. +
  17070. + IMustLock(dir);
  17071. +
  17072. + err = -ENOMEM;
  17073. + a = kmalloc(sizeof(*a), GFP_NOFS);
  17074. + if (unlikely(!a))
  17075. + goto out;
  17076. +
  17077. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  17078. + if (unlikely(err))
  17079. + goto out_free;
  17080. + err = au_d_may_add(dentry);
  17081. + if (unlikely(err))
  17082. + goto out_unlock;
  17083. +
  17084. + parent = dentry->d_parent; /* dir inode is locked */
  17085. + di_write_lock_parent(parent);
  17086. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  17087. + &a->pin, &wr_dir_args);
  17088. + err = PTR_ERR(wh_dentry);
  17089. + if (IS_ERR(wh_dentry))
  17090. + goto out_parent;
  17091. +
  17092. + sb = dentry->d_sb;
  17093. + bindex = au_dbstart(dentry);
  17094. + h_path.dentry = au_h_dptr(dentry, bindex);
  17095. + h_path.mnt = au_sbr_mnt(sb, bindex);
  17096. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  17097. + if (unlikely(err))
  17098. + goto out_unpin;
  17099. +
  17100. + /* make the dir opaque */
  17101. + diropq = 0;
  17102. + h_mtx = &d_inode(h_path.dentry)->i_mutex;
  17103. + if (wh_dentry
  17104. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  17105. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  17106. + opq_dentry = au_diropq_create(dentry, bindex);
  17107. + mutex_unlock(h_mtx);
  17108. + err = PTR_ERR(opq_dentry);
  17109. + if (IS_ERR(opq_dentry))
  17110. + goto out_dir;
  17111. + dput(opq_dentry);
  17112. + diropq = 1;
  17113. + }
  17114. +
  17115. + err = epilog(dir, bindex, wh_dentry, dentry);
  17116. + if (!err) {
  17117. + inc_nlink(dir);
  17118. + goto out_unpin; /* success */
  17119. + }
  17120. +
  17121. + /* revert */
  17122. + if (diropq) {
  17123. + AuLabel(revert opq);
  17124. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  17125. + rerr = au_diropq_remove(dentry, bindex);
  17126. + mutex_unlock(h_mtx);
  17127. + if (rerr) {
  17128. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  17129. + dentry, err, rerr);
  17130. + err = -EIO;
  17131. + }
  17132. + }
  17133. +
  17134. +out_dir:
  17135. + AuLabel(revert dir);
  17136. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  17137. + if (rerr) {
  17138. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  17139. + dentry, err, rerr);
  17140. + err = -EIO;
  17141. + }
  17142. + au_dtime_revert(&a->dt);
  17143. +out_unpin:
  17144. + au_unpin(&a->pin);
  17145. + dput(wh_dentry);
  17146. +out_parent:
  17147. + di_write_unlock(parent);
  17148. +out_unlock:
  17149. + if (unlikely(err)) {
  17150. + au_update_dbstart(dentry);
  17151. + d_drop(dentry);
  17152. + }
  17153. + aufs_read_unlock(dentry, AuLock_DW);
  17154. +out_free:
  17155. + kfree(a);
  17156. +out:
  17157. + return err;
  17158. +}
  17159. diff -Nur linux-4.1.10.orig/fs/aufs/i_op.c linux-4.1.10/fs/aufs/i_op.c
  17160. --- linux-4.1.10.orig/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
  17161. +++ linux-4.1.10/fs/aufs/i_op.c 2015-10-22 21:35:53.000000000 +0200
  17162. @@ -0,0 +1,1447 @@
  17163. +/*
  17164. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  17165. + *
  17166. + * This program, aufs is free software; you can redistribute it and/or modify
  17167. + * it under the terms of the GNU General Public License as published by
  17168. + * the Free Software Foundation; either version 2 of the License, or
  17169. + * (at your option) any later version.
  17170. + *
  17171. + * This program is distributed in the hope that it will be useful,
  17172. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17173. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17174. + * GNU General Public License for more details.
  17175. + *
  17176. + * You should have received a copy of the GNU General Public License
  17177. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17178. + */
  17179. +
  17180. +/*
  17181. + * inode operations (except add/del/rename)
  17182. + */
  17183. +
  17184. +#include <linux/device_cgroup.h>
  17185. +#include <linux/fs_stack.h>
  17186. +#include <linux/mm.h>
  17187. +#include <linux/namei.h>
  17188. +#include <linux/security.h>
  17189. +#include "aufs.h"
  17190. +
  17191. +static int h_permission(struct inode *h_inode, int mask,
  17192. + struct vfsmount *h_mnt, int brperm)
  17193. +{
  17194. + int err;
  17195. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  17196. +
  17197. + err = -EACCES;
  17198. + if ((write_mask && IS_IMMUTABLE(h_inode))
  17199. + || ((mask & MAY_EXEC)
  17200. + && S_ISREG(h_inode->i_mode)
  17201. + && ((h_mnt->mnt_flags & MNT_NOEXEC)
  17202. + || !(h_inode->i_mode & S_IXUGO))))
  17203. + goto out;
  17204. +
  17205. + /*
  17206. + * - skip the lower fs test in the case of write to ro branch.
  17207. + * - nfs dir permission write check is optimized, but a policy for
  17208. + * link/rename requires a real check.
  17209. + * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
  17210. + * in this case, generic_permission() returns -EOPNOTSUPP.
  17211. + */
  17212. + if ((write_mask && !au_br_writable(brperm))
  17213. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  17214. + && write_mask && !(mask & MAY_READ))
  17215. + || !h_inode->i_op->permission) {
  17216. + /* AuLabel(generic_permission); */
  17217. + /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
  17218. + err = generic_permission(h_inode, mask);
  17219. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  17220. + err = h_inode->i_op->permission(h_inode, mask);
  17221. + AuTraceErr(err);
  17222. + } else {
  17223. + /* AuLabel(h_inode->permission); */
  17224. + err = h_inode->i_op->permission(h_inode, mask);
  17225. + AuTraceErr(err);
  17226. + }
  17227. +
  17228. + if (!err)
  17229. + err = devcgroup_inode_permission(h_inode, mask);
  17230. + if (!err)
  17231. + err = security_inode_permission(h_inode, mask);
  17232. +
  17233. +#if 0
  17234. + if (!err) {
  17235. + /* todo: do we need to call ima_path_check()? */
  17236. + struct path h_path = {
  17237. + .dentry =
  17238. + .mnt = h_mnt
  17239. + };
  17240. + err = ima_path_check(&h_path,
  17241. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  17242. + IMA_COUNT_LEAVE);
  17243. + }
  17244. +#endif
  17245. +
  17246. +out:
  17247. + return err;
  17248. +}
  17249. +
  17250. +static int aufs_permission(struct inode *inode, int mask)
  17251. +{
  17252. + int err;
  17253. + aufs_bindex_t bindex, bend;
  17254. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  17255. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  17256. + struct inode *h_inode;
  17257. + struct super_block *sb;
  17258. + struct au_branch *br;
  17259. +
  17260. + /* todo: support rcu-walk? */
  17261. + if (mask & MAY_NOT_BLOCK)
  17262. + return -ECHILD;
  17263. +
  17264. + sb = inode->i_sb;
  17265. + si_read_lock(sb, AuLock_FLUSH);
  17266. + ii_read_lock_child(inode);
  17267. +#if 0
  17268. + err = au_iigen_test(inode, au_sigen(sb));
  17269. + if (unlikely(err))
  17270. + goto out;
  17271. +#endif
  17272. +
  17273. + if (!isdir
  17274. + || write_mask
  17275. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  17276. + err = au_busy_or_stale();
  17277. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  17278. + if (unlikely(!h_inode
  17279. + || (h_inode->i_mode & S_IFMT)
  17280. + != (inode->i_mode & S_IFMT)))
  17281. + goto out;
  17282. +
  17283. + err = 0;
  17284. + bindex = au_ibstart(inode);
  17285. + br = au_sbr(sb, bindex);
  17286. + err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
  17287. + if (write_mask
  17288. + && !err
  17289. + && !special_file(h_inode->i_mode)) {
  17290. + /* test whether the upper writable branch exists */
  17291. + err = -EROFS;
  17292. + for (; bindex >= 0; bindex--)
  17293. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  17294. + err = 0;
  17295. + break;
  17296. + }
  17297. + }
  17298. + goto out;
  17299. + }
  17300. +
  17301. + /* non-write to dir */
  17302. + err = 0;
  17303. + bend = au_ibend(inode);
  17304. + for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
  17305. + h_inode = au_h_iptr(inode, bindex);
  17306. + if (h_inode) {
  17307. + err = au_busy_or_stale();
  17308. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  17309. + break;
  17310. +
  17311. + br = au_sbr(sb, bindex);
  17312. + err = h_permission(h_inode, mask, au_br_mnt(br),
  17313. + br->br_perm);
  17314. + }
  17315. + }
  17316. +
  17317. +out:
  17318. + ii_read_unlock(inode);
  17319. + si_read_unlock(sb);
  17320. + return err;
  17321. +}
  17322. +
  17323. +/* ---------------------------------------------------------------------- */
  17324. +
  17325. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  17326. + unsigned int flags)
  17327. +{
  17328. + struct dentry *ret, *parent;
  17329. + struct inode *inode;
  17330. + struct super_block *sb;
  17331. + int err, npositive;
  17332. +
  17333. + IMustLock(dir);
  17334. +
  17335. + /* todo: support rcu-walk? */
  17336. + ret = ERR_PTR(-ECHILD);
  17337. + if (flags & LOOKUP_RCU)
  17338. + goto out;
  17339. +
  17340. + ret = ERR_PTR(-ENAMETOOLONG);
  17341. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  17342. + goto out;
  17343. +
  17344. + sb = dir->i_sb;
  17345. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  17346. + ret = ERR_PTR(err);
  17347. + if (unlikely(err))
  17348. + goto out;
  17349. +
  17350. + err = au_di_init(dentry);
  17351. + ret = ERR_PTR(err);
  17352. + if (unlikely(err))
  17353. + goto out_si;
  17354. +
  17355. + inode = NULL;
  17356. + npositive = 0; /* suppress a warning */
  17357. + parent = dentry->d_parent; /* dir inode is locked */
  17358. + di_read_lock_parent(parent, AuLock_IR);
  17359. + err = au_alive_dir(parent);
  17360. + if (!err)
  17361. + err = au_digen_test(parent, au_sigen(sb));
  17362. + if (!err) {
  17363. + npositive = au_lkup_dentry(dentry, au_dbstart(parent),
  17364. + /*type*/0);
  17365. + err = npositive;
  17366. + }
  17367. + di_read_unlock(parent, AuLock_IR);
  17368. + ret = ERR_PTR(err);
  17369. + if (unlikely(err < 0))
  17370. + goto out_unlock;
  17371. +
  17372. + if (npositive) {
  17373. + inode = au_new_inode(dentry, /*must_new*/0);
  17374. + if (IS_ERR(inode)) {
  17375. + ret = (void *)inode;
  17376. + inode = NULL;
  17377. + goto out_unlock;
  17378. + }
  17379. + }
  17380. +
  17381. + if (inode)
  17382. + atomic_inc(&inode->i_count);
  17383. + ret = d_splice_alias(inode, dentry);
  17384. +#if 0
  17385. + if (unlikely(d_need_lookup(dentry))) {
  17386. + spin_lock(&dentry->d_lock);
  17387. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  17388. + spin_unlock(&dentry->d_lock);
  17389. + } else
  17390. +#endif
  17391. + if (inode) {
  17392. + if (!IS_ERR(ret)) {
  17393. + iput(inode);
  17394. + if (ret && ret != dentry)
  17395. + ii_write_unlock(inode);
  17396. + } else {
  17397. + ii_write_unlock(inode);
  17398. + iput(inode);
  17399. + inode = NULL;
  17400. + }
  17401. + }
  17402. +
  17403. +out_unlock:
  17404. + di_write_unlock(dentry);
  17405. + if (inode) {
  17406. + /* verbose coding for lock class name */
  17407. + if (unlikely(S_ISLNK(inode->i_mode)))
  17408. + au_rw_class(&au_di(dentry)->di_rwsem,
  17409. + au_lc_key + AuLcSymlink_DIINFO);
  17410. + else if (unlikely(S_ISDIR(inode->i_mode)))
  17411. + au_rw_class(&au_di(dentry)->di_rwsem,
  17412. + au_lc_key + AuLcDir_DIINFO);
  17413. + else /* likely */
  17414. + au_rw_class(&au_di(dentry)->di_rwsem,
  17415. + au_lc_key + AuLcNonDir_DIINFO);
  17416. + }
  17417. +out_si:
  17418. + si_read_unlock(sb);
  17419. +out:
  17420. + return ret;
  17421. +}
  17422. +
  17423. +/* ---------------------------------------------------------------------- */
  17424. +
  17425. +struct aopen_node {
  17426. + struct hlist_node hlist;
  17427. + struct file *file, *h_file;
  17428. +};
  17429. +
  17430. +static int au_do_aopen(struct inode *inode, struct file *file)
  17431. +{
  17432. + struct au_sphlhead *aopen;
  17433. + struct aopen_node *node;
  17434. + struct au_do_open_args args = {
  17435. + .no_lock = 1,
  17436. + .open = au_do_open_nondir
  17437. + };
  17438. +
  17439. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  17440. + spin_lock(&aopen->spin);
  17441. + hlist_for_each_entry(node, &aopen->head, hlist)
  17442. + if (node->file == file) {
  17443. + args.h_file = node->h_file;
  17444. + break;
  17445. + }
  17446. + spin_unlock(&aopen->spin);
  17447. + /* AuDebugOn(!args.h_file); */
  17448. +
  17449. + return au_do_open(file, &args);
  17450. +}
  17451. +
  17452. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  17453. + struct file *file, unsigned int open_flag,
  17454. + umode_t create_mode, int *opened)
  17455. +{
  17456. + int err, h_opened = *opened;
  17457. + struct dentry *parent;
  17458. + struct dentry *d;
  17459. + struct au_sphlhead *aopen;
  17460. + struct vfsub_aopen_args args = {
  17461. + .open_flag = open_flag,
  17462. + .create_mode = create_mode,
  17463. + .opened = &h_opened
  17464. + };
  17465. + struct aopen_node aopen_node = {
  17466. + .file = file
  17467. + };
  17468. +
  17469. + IMustLock(dir);
  17470. + AuDbg("open_flag 0x%x\n", open_flag);
  17471. + AuDbgDentry(dentry);
  17472. +
  17473. + err = 0;
  17474. + if (!au_di(dentry)) {
  17475. + d = aufs_lookup(dir, dentry, /*flags*/0);
  17476. + if (IS_ERR(d)) {
  17477. + err = PTR_ERR(d);
  17478. + goto out;
  17479. + } else if (d) {
  17480. + /*
  17481. + * obsoleted dentry found.
  17482. + * another error will be returned later.
  17483. + */
  17484. + d_drop(d);
  17485. + dput(d);
  17486. + AuDbgDentry(d);
  17487. + }
  17488. + AuDbgDentry(dentry);
  17489. + }
  17490. +
  17491. + if (d_is_positive(dentry)
  17492. + || d_unhashed(dentry)
  17493. + || d_unlinked(dentry)
  17494. + || !(open_flag & O_CREAT))
  17495. + goto out_no_open;
  17496. +
  17497. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  17498. + if (unlikely(err))
  17499. + goto out;
  17500. +
  17501. + parent = dentry->d_parent; /* dir is locked */
  17502. + di_write_lock_parent(parent);
  17503. + err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
  17504. + if (unlikely(err))
  17505. + goto out_unlock;
  17506. +
  17507. + AuDbgDentry(dentry);
  17508. + if (d_is_positive(dentry))
  17509. + goto out_unlock;
  17510. +
  17511. + args.file = get_empty_filp();
  17512. + err = PTR_ERR(args.file);
  17513. + if (IS_ERR(args.file))
  17514. + goto out_unlock;
  17515. +
  17516. + args.file->f_flags = file->f_flags;
  17517. + err = au_aopen_or_create(dir, dentry, &args);
  17518. + AuTraceErr(err);
  17519. + AuDbgFile(args.file);
  17520. + if (unlikely(err < 0)) {
  17521. + if (h_opened & FILE_OPENED)
  17522. + fput(args.file);
  17523. + else
  17524. + put_filp(args.file);
  17525. + goto out_unlock;
  17526. + }
  17527. +
  17528. + /* some filesystems don't set FILE_CREATED while succeeded? */
  17529. + *opened |= FILE_CREATED;
  17530. + if (h_opened & FILE_OPENED)
  17531. + aopen_node.h_file = args.file;
  17532. + else {
  17533. + put_filp(args.file);
  17534. + args.file = NULL;
  17535. + }
  17536. + aopen = &au_sbi(dir->i_sb)->si_aopen;
  17537. + au_sphl_add(&aopen_node.hlist, aopen);
  17538. + err = finish_open(file, dentry, au_do_aopen, opened);
  17539. + au_sphl_del(&aopen_node.hlist, aopen);
  17540. + AuTraceErr(err);
  17541. + AuDbgFile(file);
  17542. + if (aopen_node.h_file)
  17543. + fput(aopen_node.h_file);
  17544. +
  17545. +out_unlock:
  17546. + di_write_unlock(parent);
  17547. + aufs_read_unlock(dentry, AuLock_DW);
  17548. + AuDbgDentry(dentry);
  17549. + if (unlikely(err))
  17550. + goto out;
  17551. +out_no_open:
  17552. + if (!err && !(*opened & FILE_CREATED)) {
  17553. + AuLabel(out_no_open);
  17554. + dget(dentry);
  17555. + err = finish_no_open(file, dentry);
  17556. + }
  17557. +out:
  17558. + AuDbg("%pd%s%s\n", dentry,
  17559. + (*opened & FILE_CREATED) ? " created" : "",
  17560. + (*opened & FILE_OPENED) ? " opened" : "");
  17561. + AuTraceErr(err);
  17562. + return err;
  17563. +}
  17564. +
  17565. +
  17566. +/* ---------------------------------------------------------------------- */
  17567. +
  17568. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  17569. + const unsigned char add_entry, aufs_bindex_t bcpup,
  17570. + aufs_bindex_t bstart)
  17571. +{
  17572. + int err;
  17573. + struct dentry *h_parent;
  17574. + struct inode *h_dir;
  17575. +
  17576. + if (add_entry)
  17577. + IMustLock(d_inode(parent));
  17578. + else
  17579. + di_write_lock_parent(parent);
  17580. +
  17581. + err = 0;
  17582. + if (!au_h_dptr(parent, bcpup)) {
  17583. + if (bstart > bcpup)
  17584. + err = au_cpup_dirs(dentry, bcpup);
  17585. + else if (bstart < bcpup)
  17586. + err = au_cpdown_dirs(dentry, bcpup);
  17587. + else
  17588. + BUG();
  17589. + }
  17590. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  17591. + h_parent = au_h_dptr(parent, bcpup);
  17592. + h_dir = d_inode(h_parent);
  17593. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  17594. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  17595. + /* todo: no unlock here */
  17596. + mutex_unlock(&h_dir->i_mutex);
  17597. +
  17598. + AuDbg("bcpup %d\n", bcpup);
  17599. + if (!err) {
  17600. + if (d_really_is_negative(dentry))
  17601. + au_set_h_dptr(dentry, bstart, NULL);
  17602. + au_update_dbrange(dentry, /*do_put_zero*/0);
  17603. + }
  17604. + }
  17605. +
  17606. + if (!add_entry)
  17607. + di_write_unlock(parent);
  17608. + if (!err)
  17609. + err = bcpup; /* success */
  17610. +
  17611. + AuTraceErr(err);
  17612. + return err;
  17613. +}
  17614. +
  17615. +/*
  17616. + * decide the branch and the parent dir where we will create a new entry.
  17617. + * returns new bindex or an error.
  17618. + * copyup the parent dir if needed.
  17619. + */
  17620. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  17621. + struct au_wr_dir_args *args)
  17622. +{
  17623. + int err;
  17624. + unsigned int flags;
  17625. + aufs_bindex_t bcpup, bstart, src_bstart;
  17626. + const unsigned char add_entry
  17627. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  17628. + | au_ftest_wrdir(args->flags, TMPFILE);
  17629. + struct super_block *sb;
  17630. + struct dentry *parent;
  17631. + struct au_sbinfo *sbinfo;
  17632. +
  17633. + sb = dentry->d_sb;
  17634. + sbinfo = au_sbi(sb);
  17635. + parent = dget_parent(dentry);
  17636. + bstart = au_dbstart(dentry);
  17637. + bcpup = bstart;
  17638. + if (args->force_btgt < 0) {
  17639. + if (src_dentry) {
  17640. + src_bstart = au_dbstart(src_dentry);
  17641. + if (src_bstart < bstart)
  17642. + bcpup = src_bstart;
  17643. + } else if (add_entry) {
  17644. + flags = 0;
  17645. + if (au_ftest_wrdir(args->flags, ISDIR))
  17646. + au_fset_wbr(flags, DIR);
  17647. + err = AuWbrCreate(sbinfo, dentry, flags);
  17648. + bcpup = err;
  17649. + }
  17650. +
  17651. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  17652. + if (add_entry)
  17653. + err = AuWbrCopyup(sbinfo, dentry);
  17654. + else {
  17655. + if (!IS_ROOT(dentry)) {
  17656. + di_read_lock_parent(parent, !AuLock_IR);
  17657. + err = AuWbrCopyup(sbinfo, dentry);
  17658. + di_read_unlock(parent, !AuLock_IR);
  17659. + } else
  17660. + err = AuWbrCopyup(sbinfo, dentry);
  17661. + }
  17662. + bcpup = err;
  17663. + if (unlikely(err < 0))
  17664. + goto out;
  17665. + }
  17666. + } else {
  17667. + bcpup = args->force_btgt;
  17668. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  17669. + }
  17670. +
  17671. + AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
  17672. + err = bcpup;
  17673. + if (bcpup == bstart)
  17674. + goto out; /* success */
  17675. +
  17676. + /* copyup the new parent into the branch we process */
  17677. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
  17678. + if (err >= 0) {
  17679. + if (d_really_is_negative(dentry)) {
  17680. + au_set_h_dptr(dentry, bstart, NULL);
  17681. + au_set_dbstart(dentry, bcpup);
  17682. + au_set_dbend(dentry, bcpup);
  17683. + }
  17684. + AuDebugOn(add_entry
  17685. + && !au_ftest_wrdir(args->flags, TMPFILE)
  17686. + && !au_h_dptr(dentry, bcpup));
  17687. + }
  17688. +
  17689. +out:
  17690. + dput(parent);
  17691. + return err;
  17692. +}
  17693. +
  17694. +/* ---------------------------------------------------------------------- */
  17695. +
  17696. +void au_pin_hdir_unlock(struct au_pin *p)
  17697. +{
  17698. + if (p->hdir)
  17699. + au_hn_imtx_unlock(p->hdir);
  17700. +}
  17701. +
  17702. +int au_pin_hdir_lock(struct au_pin *p)
  17703. +{
  17704. + int err;
  17705. +
  17706. + err = 0;
  17707. + if (!p->hdir)
  17708. + goto out;
  17709. +
  17710. + /* even if an error happens later, keep this lock */
  17711. + au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
  17712. +
  17713. + err = -EBUSY;
  17714. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  17715. + goto out;
  17716. +
  17717. + err = 0;
  17718. + if (p->h_dentry)
  17719. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  17720. + p->h_parent, p->br);
  17721. +
  17722. +out:
  17723. + return err;
  17724. +}
  17725. +
  17726. +int au_pin_hdir_relock(struct au_pin *p)
  17727. +{
  17728. + int err, i;
  17729. + struct inode *h_i;
  17730. + struct dentry *h_d[] = {
  17731. + p->h_dentry,
  17732. + p->h_parent
  17733. + };
  17734. +
  17735. + err = au_pin_hdir_lock(p);
  17736. + if (unlikely(err))
  17737. + goto out;
  17738. +
  17739. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  17740. + if (!h_d[i])
  17741. + continue;
  17742. + if (d_is_positive(h_d[i])) {
  17743. + h_i = d_inode(h_d[i]);
  17744. + err = !h_i->i_nlink;
  17745. + }
  17746. + }
  17747. +
  17748. +out:
  17749. + return err;
  17750. +}
  17751. +
  17752. +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  17753. +{
  17754. +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
  17755. + p->hdir->hi_inode->i_mutex.owner = task;
  17756. +#endif
  17757. +}
  17758. +
  17759. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  17760. +{
  17761. + if (p->hdir) {
  17762. + mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
  17763. + p->lsc_hi, 0, NULL, _RET_IP_);
  17764. + au_pin_hdir_set_owner(p, current);
  17765. + }
  17766. +}
  17767. +
  17768. +void au_pin_hdir_release(struct au_pin *p)
  17769. +{
  17770. + if (p->hdir) {
  17771. + au_pin_hdir_set_owner(p, p->task);
  17772. + mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
  17773. + }
  17774. +}
  17775. +
  17776. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  17777. +{
  17778. + if (pin && pin->parent)
  17779. + return au_h_dptr(pin->parent, pin->bindex);
  17780. + return NULL;
  17781. +}
  17782. +
  17783. +void au_unpin(struct au_pin *p)
  17784. +{
  17785. + if (p->hdir)
  17786. + au_pin_hdir_unlock(p);
  17787. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  17788. + vfsub_mnt_drop_write(p->h_mnt);
  17789. + if (!p->hdir)
  17790. + return;
  17791. +
  17792. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17793. + di_read_unlock(p->parent, AuLock_IR);
  17794. + iput(p->hdir->hi_inode);
  17795. + dput(p->parent);
  17796. + p->parent = NULL;
  17797. + p->hdir = NULL;
  17798. + p->h_mnt = NULL;
  17799. + /* do not clear p->task */
  17800. +}
  17801. +
  17802. +int au_do_pin(struct au_pin *p)
  17803. +{
  17804. + int err;
  17805. + struct super_block *sb;
  17806. + struct inode *h_dir;
  17807. +
  17808. + err = 0;
  17809. + sb = p->dentry->d_sb;
  17810. + p->br = au_sbr(sb, p->bindex);
  17811. + if (IS_ROOT(p->dentry)) {
  17812. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  17813. + p->h_mnt = au_br_mnt(p->br);
  17814. + err = vfsub_mnt_want_write(p->h_mnt);
  17815. + if (unlikely(err)) {
  17816. + au_fclr_pin(p->flags, MNT_WRITE);
  17817. + goto out_err;
  17818. + }
  17819. + }
  17820. + goto out;
  17821. + }
  17822. +
  17823. + p->h_dentry = NULL;
  17824. + if (p->bindex <= au_dbend(p->dentry))
  17825. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  17826. +
  17827. + p->parent = dget_parent(p->dentry);
  17828. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17829. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  17830. +
  17831. + h_dir = NULL;
  17832. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  17833. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  17834. + if (p->hdir)
  17835. + h_dir = p->hdir->hi_inode;
  17836. +
  17837. + /*
  17838. + * udba case, or
  17839. + * if DI_LOCKED is not set, then p->parent may be different
  17840. + * and h_parent can be NULL.
  17841. + */
  17842. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  17843. + err = -EBUSY;
  17844. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17845. + di_read_unlock(p->parent, AuLock_IR);
  17846. + dput(p->parent);
  17847. + p->parent = NULL;
  17848. + goto out_err;
  17849. + }
  17850. +
  17851. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  17852. + p->h_mnt = au_br_mnt(p->br);
  17853. + err = vfsub_mnt_want_write(p->h_mnt);
  17854. + if (unlikely(err)) {
  17855. + au_fclr_pin(p->flags, MNT_WRITE);
  17856. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  17857. + di_read_unlock(p->parent, AuLock_IR);
  17858. + dput(p->parent);
  17859. + p->parent = NULL;
  17860. + goto out_err;
  17861. + }
  17862. + }
  17863. +
  17864. + au_igrab(h_dir);
  17865. + err = au_pin_hdir_lock(p);
  17866. + if (!err)
  17867. + goto out; /* success */
  17868. +
  17869. + au_unpin(p);
  17870. +
  17871. +out_err:
  17872. + pr_err("err %d\n", err);
  17873. + err = au_busy_or_stale();
  17874. +out:
  17875. + return err;
  17876. +}
  17877. +
  17878. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  17879. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  17880. + unsigned int udba, unsigned char flags)
  17881. +{
  17882. + p->dentry = dentry;
  17883. + p->udba = udba;
  17884. + p->lsc_di = lsc_di;
  17885. + p->lsc_hi = lsc_hi;
  17886. + p->flags = flags;
  17887. + p->bindex = bindex;
  17888. +
  17889. + p->parent = NULL;
  17890. + p->hdir = NULL;
  17891. + p->h_mnt = NULL;
  17892. +
  17893. + p->h_dentry = NULL;
  17894. + p->h_parent = NULL;
  17895. + p->br = NULL;
  17896. + p->task = current;
  17897. +}
  17898. +
  17899. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  17900. + unsigned int udba, unsigned char flags)
  17901. +{
  17902. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  17903. + udba, flags);
  17904. + return au_do_pin(pin);
  17905. +}
  17906. +
  17907. +/* ---------------------------------------------------------------------- */
  17908. +
  17909. +/*
  17910. + * ->setattr() and ->getattr() are called in various cases.
  17911. + * chmod, stat: dentry is revalidated.
  17912. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  17913. + * unhashed.
  17914. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  17915. + */
  17916. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  17917. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  17918. +{
  17919. + int err;
  17920. + struct dentry *parent;
  17921. +
  17922. + err = 0;
  17923. + if (au_digen_test(dentry, sigen)) {
  17924. + parent = dget_parent(dentry);
  17925. + di_read_lock_parent(parent, AuLock_IR);
  17926. + err = au_refresh_dentry(dentry, parent);
  17927. + di_read_unlock(parent, AuLock_IR);
  17928. + dput(parent);
  17929. + }
  17930. +
  17931. + AuTraceErr(err);
  17932. + return err;
  17933. +}
  17934. +
  17935. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  17936. + struct au_icpup_args *a)
  17937. +{
  17938. + int err;
  17939. + loff_t sz;
  17940. + aufs_bindex_t bstart, ibstart;
  17941. + struct dentry *hi_wh, *parent;
  17942. + struct inode *inode;
  17943. + struct au_wr_dir_args wr_dir_args = {
  17944. + .force_btgt = -1,
  17945. + .flags = 0
  17946. + };
  17947. +
  17948. + if (d_is_dir(dentry))
  17949. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  17950. + /* plink or hi_wh() case */
  17951. + bstart = au_dbstart(dentry);
  17952. + inode = d_inode(dentry);
  17953. + ibstart = au_ibstart(inode);
  17954. + if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
  17955. + wr_dir_args.force_btgt = ibstart;
  17956. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  17957. + if (unlikely(err < 0))
  17958. + goto out;
  17959. + a->btgt = err;
  17960. + if (err != bstart)
  17961. + au_fset_icpup(a->flags, DID_CPUP);
  17962. +
  17963. + err = 0;
  17964. + a->pin_flags = AuPin_MNT_WRITE;
  17965. + parent = NULL;
  17966. + if (!IS_ROOT(dentry)) {
  17967. + au_fset_pin(a->pin_flags, DI_LOCKED);
  17968. + parent = dget_parent(dentry);
  17969. + di_write_lock_parent(parent);
  17970. + }
  17971. +
  17972. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  17973. + if (unlikely(err))
  17974. + goto out_parent;
  17975. +
  17976. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  17977. + sz = -1;
  17978. + a->h_inode = d_inode(a->h_path.dentry);
  17979. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  17980. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  17981. + if (ia->ia_size < i_size_read(a->h_inode))
  17982. + sz = ia->ia_size;
  17983. + mutex_unlock(&a->h_inode->i_mutex);
  17984. + }
  17985. +
  17986. + hi_wh = NULL;
  17987. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  17988. + hi_wh = au_hi_wh(inode, a->btgt);
  17989. + if (!hi_wh) {
  17990. + struct au_cp_generic cpg = {
  17991. + .dentry = dentry,
  17992. + .bdst = a->btgt,
  17993. + .bsrc = -1,
  17994. + .len = sz,
  17995. + .pin = &a->pin
  17996. + };
  17997. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  17998. + if (unlikely(err))
  17999. + goto out_unlock;
  18000. + hi_wh = au_hi_wh(inode, a->btgt);
  18001. + /* todo: revalidate hi_wh? */
  18002. + }
  18003. + }
  18004. +
  18005. + if (parent) {
  18006. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  18007. + di_downgrade_lock(parent, AuLock_IR);
  18008. + dput(parent);
  18009. + parent = NULL;
  18010. + }
  18011. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  18012. + goto out; /* success */
  18013. +
  18014. + if (!d_unhashed(dentry)) {
  18015. + struct au_cp_generic cpg = {
  18016. + .dentry = dentry,
  18017. + .bdst = a->btgt,
  18018. + .bsrc = bstart,
  18019. + .len = sz,
  18020. + .pin = &a->pin,
  18021. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  18022. + };
  18023. + err = au_sio_cpup_simple(&cpg);
  18024. + if (!err)
  18025. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  18026. + } else if (!hi_wh)
  18027. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  18028. + else
  18029. + a->h_path.dentry = hi_wh; /* do not dget here */
  18030. +
  18031. +out_unlock:
  18032. + a->h_inode = d_inode(a->h_path.dentry);
  18033. + if (!err)
  18034. + goto out; /* success */
  18035. + au_unpin(&a->pin);
  18036. +out_parent:
  18037. + if (parent) {
  18038. + di_write_unlock(parent);
  18039. + dput(parent);
  18040. + }
  18041. +out:
  18042. + if (!err)
  18043. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  18044. + return err;
  18045. +}
  18046. +
  18047. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  18048. +{
  18049. + int err;
  18050. + struct inode *inode, *delegated;
  18051. + struct super_block *sb;
  18052. + struct file *file;
  18053. + struct au_icpup_args *a;
  18054. +
  18055. + inode = d_inode(dentry);
  18056. + IMustLock(inode);
  18057. +
  18058. + err = -ENOMEM;
  18059. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18060. + if (unlikely(!a))
  18061. + goto out;
  18062. +
  18063. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  18064. + ia->ia_valid &= ~ATTR_MODE;
  18065. +
  18066. + file = NULL;
  18067. + sb = dentry->d_sb;
  18068. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18069. + if (unlikely(err))
  18070. + goto out_kfree;
  18071. +
  18072. + if (ia->ia_valid & ATTR_FILE) {
  18073. + /* currently ftruncate(2) only */
  18074. + AuDebugOn(!d_is_reg(dentry));
  18075. + file = ia->ia_file;
  18076. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
  18077. + if (unlikely(err))
  18078. + goto out_si;
  18079. + ia->ia_file = au_hf_top(file);
  18080. + a->udba = AuOpt_UDBA_NONE;
  18081. + } else {
  18082. + /* fchmod() doesn't pass ia_file */
  18083. + a->udba = au_opt_udba(sb);
  18084. + di_write_lock_child(dentry);
  18085. + /* no d_unlinked(), to set UDBA_NONE for root */
  18086. + if (d_unhashed(dentry))
  18087. + a->udba = AuOpt_UDBA_NONE;
  18088. + if (a->udba != AuOpt_UDBA_NONE) {
  18089. + AuDebugOn(IS_ROOT(dentry));
  18090. + err = au_reval_for_attr(dentry, au_sigen(sb));
  18091. + if (unlikely(err))
  18092. + goto out_dentry;
  18093. + }
  18094. + }
  18095. +
  18096. + err = au_pin_and_icpup(dentry, ia, a);
  18097. + if (unlikely(err < 0))
  18098. + goto out_dentry;
  18099. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  18100. + ia->ia_file = NULL;
  18101. + ia->ia_valid &= ~ATTR_FILE;
  18102. + }
  18103. +
  18104. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  18105. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  18106. + == (ATTR_MODE | ATTR_CTIME)) {
  18107. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  18108. + if (unlikely(err))
  18109. + goto out_unlock;
  18110. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  18111. + && (ia->ia_valid & ATTR_CTIME)) {
  18112. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  18113. + if (unlikely(err))
  18114. + goto out_unlock;
  18115. + }
  18116. +
  18117. + if (ia->ia_valid & ATTR_SIZE) {
  18118. + struct file *f;
  18119. +
  18120. + if (ia->ia_size < i_size_read(inode))
  18121. + /* unmap only */
  18122. + truncate_setsize(inode, ia->ia_size);
  18123. +
  18124. + f = NULL;
  18125. + if (ia->ia_valid & ATTR_FILE)
  18126. + f = ia->ia_file;
  18127. + mutex_unlock(&a->h_inode->i_mutex);
  18128. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  18129. + mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
  18130. + } else {
  18131. + delegated = NULL;
  18132. + while (1) {
  18133. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  18134. + if (delegated) {
  18135. + err = break_deleg_wait(&delegated);
  18136. + if (!err)
  18137. + continue;
  18138. + }
  18139. + break;
  18140. + }
  18141. + }
  18142. + if (!err)
  18143. + au_cpup_attr_changeable(inode);
  18144. +
  18145. +out_unlock:
  18146. + mutex_unlock(&a->h_inode->i_mutex);
  18147. + au_unpin(&a->pin);
  18148. + if (unlikely(err))
  18149. + au_update_dbstart(dentry);
  18150. +out_dentry:
  18151. + di_write_unlock(dentry);
  18152. + if (file) {
  18153. + fi_write_unlock(file);
  18154. + ia->ia_file = file;
  18155. + ia->ia_valid |= ATTR_FILE;
  18156. + }
  18157. +out_si:
  18158. + si_read_unlock(sb);
  18159. +out_kfree:
  18160. + kfree(a);
  18161. +out:
  18162. + AuTraceErr(err);
  18163. + return err;
  18164. +}
  18165. +
  18166. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  18167. +static int au_h_path_to_set_attr(struct dentry *dentry,
  18168. + struct au_icpup_args *a, struct path *h_path)
  18169. +{
  18170. + int err;
  18171. + struct super_block *sb;
  18172. +
  18173. + sb = dentry->d_sb;
  18174. + a->udba = au_opt_udba(sb);
  18175. + /* no d_unlinked(), to set UDBA_NONE for root */
  18176. + if (d_unhashed(dentry))
  18177. + a->udba = AuOpt_UDBA_NONE;
  18178. + if (a->udba != AuOpt_UDBA_NONE) {
  18179. + AuDebugOn(IS_ROOT(dentry));
  18180. + err = au_reval_for_attr(dentry, au_sigen(sb));
  18181. + if (unlikely(err))
  18182. + goto out;
  18183. + }
  18184. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  18185. + if (unlikely(err < 0))
  18186. + goto out;
  18187. +
  18188. + h_path->dentry = a->h_path.dentry;
  18189. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  18190. +
  18191. +out:
  18192. + return err;
  18193. +}
  18194. +
  18195. +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
  18196. +{
  18197. + int err;
  18198. + struct path h_path;
  18199. + struct super_block *sb;
  18200. + struct au_icpup_args *a;
  18201. + struct inode *inode, *h_inode;
  18202. +
  18203. + inode = d_inode(dentry);
  18204. + IMustLock(inode);
  18205. +
  18206. + err = -ENOMEM;
  18207. + a = kzalloc(sizeof(*a), GFP_NOFS);
  18208. + if (unlikely(!a))
  18209. + goto out;
  18210. +
  18211. + sb = dentry->d_sb;
  18212. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18213. + if (unlikely(err))
  18214. + goto out_kfree;
  18215. +
  18216. + h_path.dentry = NULL; /* silence gcc */
  18217. + di_write_lock_child(dentry);
  18218. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  18219. + if (unlikely(err))
  18220. + goto out_di;
  18221. +
  18222. + mutex_unlock(&a->h_inode->i_mutex);
  18223. + switch (arg->type) {
  18224. + case AU_XATTR_SET:
  18225. + err = vfsub_setxattr(h_path.dentry,
  18226. + arg->u.set.name, arg->u.set.value,
  18227. + arg->u.set.size, arg->u.set.flags);
  18228. + break;
  18229. + case AU_XATTR_REMOVE:
  18230. + err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
  18231. + break;
  18232. + case AU_ACL_SET:
  18233. + err = -EOPNOTSUPP;
  18234. + h_inode = d_inode(h_path.dentry);
  18235. + if (h_inode->i_op->set_acl)
  18236. + err = h_inode->i_op->set_acl(h_inode,
  18237. + arg->u.acl_set.acl,
  18238. + arg->u.acl_set.type);
  18239. + break;
  18240. + }
  18241. + if (!err)
  18242. + au_cpup_attr_timesizes(inode);
  18243. +
  18244. + au_unpin(&a->pin);
  18245. + if (unlikely(err))
  18246. + au_update_dbstart(dentry);
  18247. +
  18248. +out_di:
  18249. + di_write_unlock(dentry);
  18250. + si_read_unlock(sb);
  18251. +out_kfree:
  18252. + kfree(a);
  18253. +out:
  18254. + AuTraceErr(err);
  18255. + return err;
  18256. +}
  18257. +#endif
  18258. +
  18259. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  18260. + unsigned int nlink)
  18261. +{
  18262. + unsigned int n;
  18263. +
  18264. + inode->i_mode = st->mode;
  18265. + /* don't i_[ug]id_write() here */
  18266. + inode->i_uid = st->uid;
  18267. + inode->i_gid = st->gid;
  18268. + inode->i_atime = st->atime;
  18269. + inode->i_mtime = st->mtime;
  18270. + inode->i_ctime = st->ctime;
  18271. +
  18272. + au_cpup_attr_nlink(inode, /*force*/0);
  18273. + if (S_ISDIR(inode->i_mode)) {
  18274. + n = inode->i_nlink;
  18275. + n -= nlink;
  18276. + n += st->nlink;
  18277. + smp_mb(); /* for i_nlink */
  18278. + /* 0 can happen */
  18279. + set_nlink(inode, n);
  18280. + }
  18281. +
  18282. + spin_lock(&inode->i_lock);
  18283. + inode->i_blocks = st->blocks;
  18284. + i_size_write(inode, st->size);
  18285. + spin_unlock(&inode->i_lock);
  18286. +}
  18287. +
  18288. +/*
  18289. + * common routine for aufs_getattr() and aufs_getxattr().
  18290. + * returns zero or negative (an error).
  18291. + * @dentry will be read-locked in success.
  18292. + */
  18293. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
  18294. +{
  18295. + int err;
  18296. + unsigned int mnt_flags, sigen;
  18297. + unsigned char udba_none;
  18298. + aufs_bindex_t bindex;
  18299. + struct super_block *sb, *h_sb;
  18300. + struct inode *inode;
  18301. +
  18302. + h_path->mnt = NULL;
  18303. + h_path->dentry = NULL;
  18304. +
  18305. + err = 0;
  18306. + sb = dentry->d_sb;
  18307. + mnt_flags = au_mntflags(sb);
  18308. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  18309. +
  18310. + /* support fstat(2) */
  18311. + if (!d_unlinked(dentry) && !udba_none) {
  18312. + sigen = au_sigen(sb);
  18313. + err = au_digen_test(dentry, sigen);
  18314. + if (!err) {
  18315. + di_read_lock_child(dentry, AuLock_IR);
  18316. + err = au_dbrange_test(dentry);
  18317. + if (unlikely(err)) {
  18318. + di_read_unlock(dentry, AuLock_IR);
  18319. + goto out;
  18320. + }
  18321. + } else {
  18322. + AuDebugOn(IS_ROOT(dentry));
  18323. + di_write_lock_child(dentry);
  18324. + err = au_dbrange_test(dentry);
  18325. + if (!err)
  18326. + err = au_reval_for_attr(dentry, sigen);
  18327. + if (!err)
  18328. + di_downgrade_lock(dentry, AuLock_IR);
  18329. + else {
  18330. + di_write_unlock(dentry);
  18331. + goto out;
  18332. + }
  18333. + }
  18334. + } else
  18335. + di_read_lock_child(dentry, AuLock_IR);
  18336. +
  18337. + inode = d_inode(dentry);
  18338. + bindex = au_ibstart(inode);
  18339. + h_path->mnt = au_sbr_mnt(sb, bindex);
  18340. + h_sb = h_path->mnt->mnt_sb;
  18341. + if (!force
  18342. + && !au_test_fs_bad_iattr(h_sb)
  18343. + && udba_none)
  18344. + goto out; /* success */
  18345. +
  18346. + if (au_dbstart(dentry) == bindex)
  18347. + h_path->dentry = au_h_dptr(dentry, bindex);
  18348. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  18349. + h_path->dentry = au_plink_lkup(inode, bindex);
  18350. + if (IS_ERR(h_path->dentry))
  18351. + /* pretending success */
  18352. + h_path->dentry = NULL;
  18353. + else
  18354. + dput(h_path->dentry);
  18355. + }
  18356. +
  18357. +out:
  18358. + return err;
  18359. +}
  18360. +
  18361. +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
  18362. + struct dentry *dentry, struct kstat *st)
  18363. +{
  18364. + int err;
  18365. + unsigned char positive;
  18366. + struct path h_path;
  18367. + struct inode *inode;
  18368. + struct super_block *sb;
  18369. +
  18370. + inode = d_inode(dentry);
  18371. + sb = dentry->d_sb;
  18372. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18373. + if (unlikely(err))
  18374. + goto out;
  18375. + err = au_h_path_getattr(dentry, /*force*/0, &h_path);
  18376. + if (unlikely(err))
  18377. + goto out_si;
  18378. + if (unlikely(!h_path.dentry))
  18379. + /* illegally overlapped or something */
  18380. + goto out_fill; /* pretending success */
  18381. +
  18382. + positive = d_is_positive(h_path.dentry);
  18383. + if (positive)
  18384. + err = vfs_getattr(&h_path, st);
  18385. + if (!err) {
  18386. + if (positive)
  18387. + au_refresh_iattr(inode, st,
  18388. + d_inode(h_path.dentry)->i_nlink);
  18389. + goto out_fill; /* success */
  18390. + }
  18391. + AuTraceErr(err);
  18392. + goto out_di;
  18393. +
  18394. +out_fill:
  18395. + generic_fillattr(inode, st);
  18396. +out_di:
  18397. + di_read_unlock(dentry, AuLock_IR);
  18398. +out_si:
  18399. + si_read_unlock(sb);
  18400. +out:
  18401. + AuTraceErr(err);
  18402. + return err;
  18403. +}
  18404. +
  18405. +/* ---------------------------------------------------------------------- */
  18406. +
  18407. +static int h_readlink(struct dentry *dentry, int bindex, char __user *buf,
  18408. + int bufsiz)
  18409. +{
  18410. + int err;
  18411. + struct super_block *sb;
  18412. + struct dentry *h_dentry;
  18413. + struct inode *inode, *h_inode;
  18414. +
  18415. + err = -EINVAL;
  18416. + h_dentry = au_h_dptr(dentry, bindex);
  18417. + h_inode = d_inode(h_dentry);
  18418. + if (unlikely(!h_inode->i_op->readlink))
  18419. + goto out;
  18420. +
  18421. + err = security_inode_readlink(h_dentry);
  18422. + if (unlikely(err))
  18423. + goto out;
  18424. +
  18425. + sb = dentry->d_sb;
  18426. + inode = d_inode(dentry);
  18427. + if (!au_test_ro(sb, bindex, inode)) {
  18428. + vfsub_touch_atime(au_sbr_mnt(sb, bindex), h_dentry);
  18429. + fsstack_copy_attr_atime(inode, h_inode);
  18430. + }
  18431. + err = h_inode->i_op->readlink(h_dentry, buf, bufsiz);
  18432. +
  18433. +out:
  18434. + return err;
  18435. +}
  18436. +
  18437. +static int aufs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  18438. +{
  18439. + int err;
  18440. +
  18441. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  18442. + if (unlikely(err))
  18443. + goto out;
  18444. + err = au_d_hashed_positive(dentry);
  18445. + if (!err)
  18446. + err = h_readlink(dentry, au_dbstart(dentry), buf, bufsiz);
  18447. + aufs_read_unlock(dentry, AuLock_IR);
  18448. +
  18449. +out:
  18450. + return err;
  18451. +}
  18452. +
  18453. +static void *aufs_follow_link(struct dentry *dentry, struct nameidata *nd)
  18454. +{
  18455. + int err;
  18456. + mm_segment_t old_fs;
  18457. + union {
  18458. + char *k;
  18459. + char __user *u;
  18460. + } buf;
  18461. +
  18462. + err = -ENOMEM;
  18463. + buf.k = (void *)__get_free_page(GFP_NOFS);
  18464. + if (unlikely(!buf.k))
  18465. + goto out;
  18466. +
  18467. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  18468. + if (unlikely(err))
  18469. + goto out_name;
  18470. +
  18471. + err = au_d_hashed_positive(dentry);
  18472. + if (!err) {
  18473. + old_fs = get_fs();
  18474. + set_fs(KERNEL_DS);
  18475. + err = h_readlink(dentry, au_dbstart(dentry), buf.u, PATH_MAX);
  18476. + set_fs(old_fs);
  18477. + }
  18478. + aufs_read_unlock(dentry, AuLock_IR);
  18479. +
  18480. + if (err >= 0) {
  18481. + buf.k[err] = 0;
  18482. + /* will be freed by put_link */
  18483. + nd_set_link(nd, buf.k);
  18484. + return NULL; /* success */
  18485. + }
  18486. +
  18487. +out_name:
  18488. + free_page((unsigned long)buf.k);
  18489. +out:
  18490. + AuTraceErr(err);
  18491. + return ERR_PTR(err);
  18492. +}
  18493. +
  18494. +static void aufs_put_link(struct dentry *dentry __maybe_unused,
  18495. + struct nameidata *nd, void *cookie __maybe_unused)
  18496. +{
  18497. + char *p;
  18498. +
  18499. + p = nd_get_link(nd);
  18500. + if (!IS_ERR_OR_NULL(p))
  18501. + free_page((unsigned long)p);
  18502. +}
  18503. +
  18504. +/* ---------------------------------------------------------------------- */
  18505. +
  18506. +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
  18507. +{
  18508. + int err;
  18509. + struct super_block *sb;
  18510. + struct inode *h_inode;
  18511. +
  18512. + sb = inode->i_sb;
  18513. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  18514. + lockdep_off();
  18515. + si_read_lock(sb, AuLock_FLUSH);
  18516. + ii_write_lock_child(inode);
  18517. + lockdep_on();
  18518. + h_inode = au_h_iptr(inode, au_ibstart(inode));
  18519. + err = vfsub_update_time(h_inode, ts, flags);
  18520. + lockdep_off();
  18521. + if (!err)
  18522. + au_cpup_attr_timesizes(inode);
  18523. + ii_write_unlock(inode);
  18524. + si_read_unlock(sb);
  18525. + lockdep_on();
  18526. +
  18527. + if (!err && (flags & S_VERSION))
  18528. + inode_inc_iversion(inode);
  18529. +
  18530. + return err;
  18531. +}
  18532. +
  18533. +/* ---------------------------------------------------------------------- */
  18534. +
  18535. +struct inode_operations aufs_symlink_iop = {
  18536. + .permission = aufs_permission,
  18537. +#ifdef CONFIG_FS_POSIX_ACL
  18538. + .get_acl = aufs_get_acl,
  18539. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  18540. +#endif
  18541. +
  18542. + .setattr = aufs_setattr,
  18543. + .getattr = aufs_getattr,
  18544. +
  18545. +#ifdef CONFIG_AUFS_XATTR
  18546. + .setxattr = aufs_setxattr,
  18547. + .getxattr = aufs_getxattr,
  18548. + .listxattr = aufs_listxattr,
  18549. + .removexattr = aufs_removexattr,
  18550. +#endif
  18551. +
  18552. + .readlink = aufs_readlink,
  18553. + .follow_link = aufs_follow_link,
  18554. + .put_link = aufs_put_link,
  18555. +
  18556. + /* .update_time = aufs_update_time */
  18557. +};
  18558. +
  18559. +struct inode_operations aufs_dir_iop = {
  18560. + .create = aufs_create,
  18561. + .lookup = aufs_lookup,
  18562. + .link = aufs_link,
  18563. + .unlink = aufs_unlink,
  18564. + .symlink = aufs_symlink,
  18565. + .mkdir = aufs_mkdir,
  18566. + .rmdir = aufs_rmdir,
  18567. + .mknod = aufs_mknod,
  18568. + .rename = aufs_rename,
  18569. +
  18570. + .permission = aufs_permission,
  18571. +#ifdef CONFIG_FS_POSIX_ACL
  18572. + .get_acl = aufs_get_acl,
  18573. + .set_acl = aufs_set_acl,
  18574. +#endif
  18575. +
  18576. + .setattr = aufs_setattr,
  18577. + .getattr = aufs_getattr,
  18578. +
  18579. +#ifdef CONFIG_AUFS_XATTR
  18580. + .setxattr = aufs_setxattr,
  18581. + .getxattr = aufs_getxattr,
  18582. + .listxattr = aufs_listxattr,
  18583. + .removexattr = aufs_removexattr,
  18584. +#endif
  18585. +
  18586. + .update_time = aufs_update_time,
  18587. + .atomic_open = aufs_atomic_open,
  18588. + .tmpfile = aufs_tmpfile
  18589. +};
  18590. +
  18591. +struct inode_operations aufs_iop = {
  18592. + .permission = aufs_permission,
  18593. +#ifdef CONFIG_FS_POSIX_ACL
  18594. + .get_acl = aufs_get_acl,
  18595. + .set_acl = aufs_set_acl,
  18596. +#endif
  18597. +
  18598. + .setattr = aufs_setattr,
  18599. + .getattr = aufs_getattr,
  18600. +
  18601. +#ifdef CONFIG_AUFS_XATTR
  18602. + .setxattr = aufs_setxattr,
  18603. + .getxattr = aufs_getxattr,
  18604. + .listxattr = aufs_listxattr,
  18605. + .removexattr = aufs_removexattr,
  18606. +#endif
  18607. +
  18608. + .update_time = aufs_update_time
  18609. +};
  18610. diff -Nur linux-4.1.10.orig/fs/aufs/i_op_del.c linux-4.1.10/fs/aufs/i_op_del.c
  18611. --- linux-4.1.10.orig/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
  18612. +++ linux-4.1.10/fs/aufs/i_op_del.c 2015-10-22 21:35:53.000000000 +0200
  18613. @@ -0,0 +1,510 @@
  18614. +/*
  18615. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  18616. + *
  18617. + * This program, aufs is free software; you can redistribute it and/or modify
  18618. + * it under the terms of the GNU General Public License as published by
  18619. + * the Free Software Foundation; either version 2 of the License, or
  18620. + * (at your option) any later version.
  18621. + *
  18622. + * This program is distributed in the hope that it will be useful,
  18623. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18624. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18625. + * GNU General Public License for more details.
  18626. + *
  18627. + * You should have received a copy of the GNU General Public License
  18628. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18629. + */
  18630. +
  18631. +/*
  18632. + * inode operations (del entry)
  18633. + */
  18634. +
  18635. +#include "aufs.h"
  18636. +
  18637. +/*
  18638. + * decide if a new whiteout for @dentry is necessary or not.
  18639. + * when it is necessary, prepare the parent dir for the upper branch whose
  18640. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  18641. + * be done by caller.
  18642. + * return value:
  18643. + * 0: wh is unnecessary
  18644. + * plus: wh is necessary
  18645. + * minus: error
  18646. + */
  18647. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  18648. +{
  18649. + int need_wh, err;
  18650. + aufs_bindex_t bstart;
  18651. + struct super_block *sb;
  18652. +
  18653. + sb = dentry->d_sb;
  18654. + bstart = au_dbstart(dentry);
  18655. + if (*bcpup < 0) {
  18656. + *bcpup = bstart;
  18657. + if (au_test_ro(sb, bstart, d_inode(dentry))) {
  18658. + err = AuWbrCopyup(au_sbi(sb), dentry);
  18659. + *bcpup = err;
  18660. + if (unlikely(err < 0))
  18661. + goto out;
  18662. + }
  18663. + } else
  18664. + AuDebugOn(bstart < *bcpup
  18665. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  18666. + AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
  18667. +
  18668. + if (*bcpup != bstart) {
  18669. + err = au_cpup_dirs(dentry, *bcpup);
  18670. + if (unlikely(err))
  18671. + goto out;
  18672. + need_wh = 1;
  18673. + } else {
  18674. + struct au_dinfo *dinfo, *tmp;
  18675. +
  18676. + need_wh = -ENOMEM;
  18677. + dinfo = au_di(dentry);
  18678. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  18679. + if (tmp) {
  18680. + au_di_cp(tmp, dinfo);
  18681. + au_di_swap(tmp, dinfo);
  18682. + /* returns the number of positive dentries */
  18683. + need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
  18684. + au_di_swap(tmp, dinfo);
  18685. + au_rw_write_unlock(&tmp->di_rwsem);
  18686. + au_di_free(tmp);
  18687. + }
  18688. + }
  18689. + AuDbg("need_wh %d\n", need_wh);
  18690. + err = need_wh;
  18691. +
  18692. +out:
  18693. + return err;
  18694. +}
  18695. +
  18696. +/*
  18697. + * simple tests for the del-entry operations.
  18698. + * following the checks in vfs, plus the parent-child relationship.
  18699. + */
  18700. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  18701. + struct dentry *h_parent, int isdir)
  18702. +{
  18703. + int err;
  18704. + umode_t h_mode;
  18705. + struct dentry *h_dentry, *h_latest;
  18706. + struct inode *h_inode;
  18707. +
  18708. + h_dentry = au_h_dptr(dentry, bindex);
  18709. + if (d_really_is_positive(dentry)) {
  18710. + err = -ENOENT;
  18711. + if (unlikely(d_is_negative(h_dentry)))
  18712. + goto out;
  18713. + h_inode = d_inode(h_dentry);
  18714. + if (unlikely(!h_inode->i_nlink))
  18715. + goto out;
  18716. +
  18717. + h_mode = h_inode->i_mode;
  18718. + if (!isdir) {
  18719. + err = -EISDIR;
  18720. + if (unlikely(S_ISDIR(h_mode)))
  18721. + goto out;
  18722. + } else if (unlikely(!S_ISDIR(h_mode))) {
  18723. + err = -ENOTDIR;
  18724. + goto out;
  18725. + }
  18726. + } else {
  18727. + /* rename(2) case */
  18728. + err = -EIO;
  18729. + if (unlikely(d_is_positive(h_dentry)))
  18730. + goto out;
  18731. + }
  18732. +
  18733. + err = -ENOENT;
  18734. + /* expected parent dir is locked */
  18735. + if (unlikely(h_parent != h_dentry->d_parent))
  18736. + goto out;
  18737. + err = 0;
  18738. +
  18739. + /*
  18740. + * rmdir a dir may break the consistency on some filesystem.
  18741. + * let's try heavy test.
  18742. + */
  18743. + err = -EACCES;
  18744. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  18745. + && au_test_h_perm(d_inode(h_parent),
  18746. + MAY_EXEC | MAY_WRITE)))
  18747. + goto out;
  18748. +
  18749. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  18750. + err = -EIO;
  18751. + if (IS_ERR(h_latest))
  18752. + goto out;
  18753. + if (h_latest == h_dentry)
  18754. + err = 0;
  18755. + dput(h_latest);
  18756. +
  18757. +out:
  18758. + return err;
  18759. +}
  18760. +
  18761. +/*
  18762. + * decide the branch where we operate for @dentry. the branch index will be set
  18763. + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
  18764. + * dir for reverting.
  18765. + * when a new whiteout is necessary, create it.
  18766. + */
  18767. +static struct dentry*
  18768. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  18769. + struct au_dtime *dt, struct au_pin *pin)
  18770. +{
  18771. + struct dentry *wh_dentry;
  18772. + struct super_block *sb;
  18773. + struct path h_path;
  18774. + int err, need_wh;
  18775. + unsigned int udba;
  18776. + aufs_bindex_t bcpup;
  18777. +
  18778. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  18779. + wh_dentry = ERR_PTR(need_wh);
  18780. + if (unlikely(need_wh < 0))
  18781. + goto out;
  18782. +
  18783. + sb = dentry->d_sb;
  18784. + udba = au_opt_udba(sb);
  18785. + bcpup = *rbcpup;
  18786. + err = au_pin(pin, dentry, bcpup, udba,
  18787. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  18788. + wh_dentry = ERR_PTR(err);
  18789. + if (unlikely(err))
  18790. + goto out;
  18791. +
  18792. + h_path.dentry = au_pinned_h_parent(pin);
  18793. + if (udba != AuOpt_UDBA_NONE
  18794. + && au_dbstart(dentry) == bcpup) {
  18795. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  18796. + wh_dentry = ERR_PTR(err);
  18797. + if (unlikely(err))
  18798. + goto out_unpin;
  18799. + }
  18800. +
  18801. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  18802. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  18803. + wh_dentry = NULL;
  18804. + if (!need_wh)
  18805. + goto out; /* success, no need to create whiteout */
  18806. +
  18807. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  18808. + if (IS_ERR(wh_dentry))
  18809. + goto out_unpin;
  18810. +
  18811. + /* returns with the parent is locked and wh_dentry is dget-ed */
  18812. + goto out; /* success */
  18813. +
  18814. +out_unpin:
  18815. + au_unpin(pin);
  18816. +out:
  18817. + return wh_dentry;
  18818. +}
  18819. +
  18820. +/*
  18821. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  18822. + * in order to be revertible and save time for removing many child whiteouts
  18823. + * under the dir.
  18824. + * returns 1 when there are too many child whiteout and caller should remove
  18825. + * them asynchronously. returns 0 when the number of children is enough small to
  18826. + * remove now or the branch fs is a remote fs.
  18827. + * otherwise return an error.
  18828. + */
  18829. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  18830. + struct au_nhash *whlist, struct inode *dir)
  18831. +{
  18832. + int rmdir_later, err, dirwh;
  18833. + struct dentry *h_dentry;
  18834. + struct super_block *sb;
  18835. + struct inode *inode;
  18836. +
  18837. + sb = dentry->d_sb;
  18838. + SiMustAnyLock(sb);
  18839. + h_dentry = au_h_dptr(dentry, bindex);
  18840. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  18841. + if (unlikely(err))
  18842. + goto out;
  18843. +
  18844. + /* stop monitoring */
  18845. + inode = d_inode(dentry);
  18846. + au_hn_free(au_hi(inode, bindex));
  18847. +
  18848. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  18849. + dirwh = au_sbi(sb)->si_dirwh;
  18850. + rmdir_later = (dirwh <= 1);
  18851. + if (!rmdir_later)
  18852. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  18853. + dirwh);
  18854. + if (rmdir_later)
  18855. + return rmdir_later;
  18856. + }
  18857. +
  18858. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  18859. + if (unlikely(err)) {
  18860. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  18861. + h_dentry, bindex, err);
  18862. + err = 0;
  18863. + }
  18864. +
  18865. +out:
  18866. + AuTraceErr(err);
  18867. + return err;
  18868. +}
  18869. +
  18870. +/*
  18871. + * final procedure for deleting a entry.
  18872. + * maintain dentry and iattr.
  18873. + */
  18874. +static void epilog(struct inode *dir, struct dentry *dentry,
  18875. + aufs_bindex_t bindex)
  18876. +{
  18877. + struct inode *inode;
  18878. +
  18879. + inode = d_inode(dentry);
  18880. + d_drop(dentry);
  18881. + inode->i_ctime = dir->i_ctime;
  18882. +
  18883. + au_dir_ts(dir, bindex);
  18884. + dir->i_version++;
  18885. +}
  18886. +
  18887. +/*
  18888. + * when an error happened, remove the created whiteout and revert everything.
  18889. + */
  18890. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  18891. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  18892. + struct dentry *dentry, struct au_dtime *dt)
  18893. +{
  18894. + int rerr;
  18895. + struct path h_path = {
  18896. + .dentry = wh_dentry,
  18897. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  18898. + };
  18899. +
  18900. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  18901. + if (!rerr) {
  18902. + au_set_dbwh(dentry, bwh);
  18903. + au_dtime_revert(dt);
  18904. + return 0;
  18905. + }
  18906. +
  18907. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  18908. + return -EIO;
  18909. +}
  18910. +
  18911. +/* ---------------------------------------------------------------------- */
  18912. +
  18913. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  18914. +{
  18915. + int err;
  18916. + aufs_bindex_t bwh, bindex, bstart;
  18917. + struct inode *inode, *h_dir, *delegated;
  18918. + struct dentry *parent, *wh_dentry;
  18919. + /* to reuduce stack size */
  18920. + struct {
  18921. + struct au_dtime dt;
  18922. + struct au_pin pin;
  18923. + struct path h_path;
  18924. + } *a;
  18925. +
  18926. + IMustLock(dir);
  18927. +
  18928. + err = -ENOMEM;
  18929. + a = kmalloc(sizeof(*a), GFP_NOFS);
  18930. + if (unlikely(!a))
  18931. + goto out;
  18932. +
  18933. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  18934. + if (unlikely(err))
  18935. + goto out_free;
  18936. + err = au_d_hashed_positive(dentry);
  18937. + if (unlikely(err))
  18938. + goto out_unlock;
  18939. + inode = d_inode(dentry);
  18940. + IMustLock(inode);
  18941. + err = -EISDIR;
  18942. + if (unlikely(d_is_dir(dentry)))
  18943. + goto out_unlock; /* possible? */
  18944. +
  18945. + bstart = au_dbstart(dentry);
  18946. + bwh = au_dbwh(dentry);
  18947. + bindex = -1;
  18948. + parent = dentry->d_parent; /* dir inode is locked */
  18949. + di_write_lock_parent(parent);
  18950. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  18951. + &a->pin);
  18952. + err = PTR_ERR(wh_dentry);
  18953. + if (IS_ERR(wh_dentry))
  18954. + goto out_parent;
  18955. +
  18956. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
  18957. + a->h_path.dentry = au_h_dptr(dentry, bstart);
  18958. + dget(a->h_path.dentry);
  18959. + if (bindex == bstart) {
  18960. + h_dir = au_pinned_h_dir(&a->pin);
  18961. + delegated = NULL;
  18962. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  18963. + if (unlikely(err == -EWOULDBLOCK)) {
  18964. + pr_warn("cannot retry for NFSv4 delegation"
  18965. + " for an internal unlink\n");
  18966. + iput(delegated);
  18967. + }
  18968. + } else {
  18969. + /* dir inode is locked */
  18970. + h_dir = d_inode(wh_dentry->d_parent);
  18971. + IMustLock(h_dir);
  18972. + err = 0;
  18973. + }
  18974. +
  18975. + if (!err) {
  18976. + vfsub_drop_nlink(inode);
  18977. + epilog(dir, dentry, bindex);
  18978. +
  18979. + /* update target timestamps */
  18980. + if (bindex == bstart) {
  18981. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  18982. + /*ignore*/
  18983. + inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  18984. + } else
  18985. + /* todo: this timestamp may be reverted later */
  18986. + inode->i_ctime = h_dir->i_ctime;
  18987. + goto out_unpin; /* success */
  18988. + }
  18989. +
  18990. + /* revert */
  18991. + if (wh_dentry) {
  18992. + int rerr;
  18993. +
  18994. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  18995. + &a->dt);
  18996. + if (rerr)
  18997. + err = rerr;
  18998. + }
  18999. +
  19000. +out_unpin:
  19001. + au_unpin(&a->pin);
  19002. + dput(wh_dentry);
  19003. + dput(a->h_path.dentry);
  19004. +out_parent:
  19005. + di_write_unlock(parent);
  19006. +out_unlock:
  19007. + aufs_read_unlock(dentry, AuLock_DW);
  19008. +out_free:
  19009. + kfree(a);
  19010. +out:
  19011. + return err;
  19012. +}
  19013. +
  19014. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  19015. +{
  19016. + int err, rmdir_later;
  19017. + aufs_bindex_t bwh, bindex, bstart;
  19018. + struct inode *inode;
  19019. + struct dentry *parent, *wh_dentry, *h_dentry;
  19020. + struct au_whtmp_rmdir *args;
  19021. + /* to reuduce stack size */
  19022. + struct {
  19023. + struct au_dtime dt;
  19024. + struct au_pin pin;
  19025. + } *a;
  19026. +
  19027. + IMustLock(dir);
  19028. +
  19029. + err = -ENOMEM;
  19030. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19031. + if (unlikely(!a))
  19032. + goto out;
  19033. +
  19034. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  19035. + if (unlikely(err))
  19036. + goto out_free;
  19037. + err = au_alive_dir(dentry);
  19038. + if (unlikely(err))
  19039. + goto out_unlock;
  19040. + inode = d_inode(dentry);
  19041. + IMustLock(inode);
  19042. + err = -ENOTDIR;
  19043. + if (unlikely(!d_is_dir(dentry)))
  19044. + goto out_unlock; /* possible? */
  19045. +
  19046. + err = -ENOMEM;
  19047. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  19048. + if (unlikely(!args))
  19049. + goto out_unlock;
  19050. +
  19051. + parent = dentry->d_parent; /* dir inode is locked */
  19052. + di_write_lock_parent(parent);
  19053. + err = au_test_empty(dentry, &args->whlist);
  19054. + if (unlikely(err))
  19055. + goto out_parent;
  19056. +
  19057. + bstart = au_dbstart(dentry);
  19058. + bwh = au_dbwh(dentry);
  19059. + bindex = -1;
  19060. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  19061. + &a->pin);
  19062. + err = PTR_ERR(wh_dentry);
  19063. + if (IS_ERR(wh_dentry))
  19064. + goto out_parent;
  19065. +
  19066. + h_dentry = au_h_dptr(dentry, bstart);
  19067. + dget(h_dentry);
  19068. + rmdir_later = 0;
  19069. + if (bindex == bstart) {
  19070. + err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
  19071. + if (err > 0) {
  19072. + rmdir_later = err;
  19073. + err = 0;
  19074. + }
  19075. + } else {
  19076. + /* stop monitoring */
  19077. + au_hn_free(au_hi(inode, bstart));
  19078. +
  19079. + /* dir inode is locked */
  19080. + IMustLock(d_inode(wh_dentry->d_parent));
  19081. + err = 0;
  19082. + }
  19083. +
  19084. + if (!err) {
  19085. + vfsub_dead_dir(inode);
  19086. + au_set_dbdiropq(dentry, -1);
  19087. + epilog(dir, dentry, bindex);
  19088. +
  19089. + if (rmdir_later) {
  19090. + au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
  19091. + args = NULL;
  19092. + }
  19093. +
  19094. + goto out_unpin; /* success */
  19095. + }
  19096. +
  19097. + /* revert */
  19098. + AuLabel(revert);
  19099. + if (wh_dentry) {
  19100. + int rerr;
  19101. +
  19102. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  19103. + &a->dt);
  19104. + if (rerr)
  19105. + err = rerr;
  19106. + }
  19107. +
  19108. +out_unpin:
  19109. + au_unpin(&a->pin);
  19110. + dput(wh_dentry);
  19111. + dput(h_dentry);
  19112. +out_parent:
  19113. + di_write_unlock(parent);
  19114. + if (args)
  19115. + au_whtmp_rmdir_free(args);
  19116. +out_unlock:
  19117. + aufs_read_unlock(dentry, AuLock_DW);
  19118. +out_free:
  19119. + kfree(a);
  19120. +out:
  19121. + AuTraceErr(err);
  19122. + return err;
  19123. +}
  19124. diff -Nur linux-4.1.10.orig/fs/aufs/i_op_ren.c linux-4.1.10/fs/aufs/i_op_ren.c
  19125. --- linux-4.1.10.orig/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
  19126. +++ linux-4.1.10/fs/aufs/i_op_ren.c 2015-10-22 21:35:53.000000000 +0200
  19127. @@ -0,0 +1,1017 @@
  19128. +/*
  19129. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  19130. + *
  19131. + * This program, aufs is free software; you can redistribute it and/or modify
  19132. + * it under the terms of the GNU General Public License as published by
  19133. + * the Free Software Foundation; either version 2 of the License, or
  19134. + * (at your option) any later version.
  19135. + *
  19136. + * This program is distributed in the hope that it will be useful,
  19137. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19138. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19139. + * GNU General Public License for more details.
  19140. + *
  19141. + * You should have received a copy of the GNU General Public License
  19142. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19143. + */
  19144. +
  19145. +/*
  19146. + * inode operation (rename entry)
  19147. + * todo: this is crazy monster
  19148. + */
  19149. +
  19150. +#include "aufs.h"
  19151. +
  19152. +enum { AuSRC, AuDST, AuSrcDst };
  19153. +enum { AuPARENT, AuCHILD, AuParentChild };
  19154. +
  19155. +#define AuRen_ISDIR 1
  19156. +#define AuRen_ISSAMEDIR (1 << 1)
  19157. +#define AuRen_WHSRC (1 << 2)
  19158. +#define AuRen_WHDST (1 << 3)
  19159. +#define AuRen_MNT_WRITE (1 << 4)
  19160. +#define AuRen_DT_DSTDIR (1 << 5)
  19161. +#define AuRen_DIROPQ (1 << 6)
  19162. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  19163. +#define au_fset_ren(flags, name) \
  19164. + do { (flags) |= AuRen_##name; } while (0)
  19165. +#define au_fclr_ren(flags, name) \
  19166. + do { (flags) &= ~AuRen_##name; } while (0)
  19167. +
  19168. +struct au_ren_args {
  19169. + struct {
  19170. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  19171. + *wh_dentry;
  19172. + struct inode *dir, *inode;
  19173. + struct au_hinode *hdir;
  19174. + struct au_dtime dt[AuParentChild];
  19175. + aufs_bindex_t bstart;
  19176. + } sd[AuSrcDst];
  19177. +
  19178. +#define src_dentry sd[AuSRC].dentry
  19179. +#define src_dir sd[AuSRC].dir
  19180. +#define src_inode sd[AuSRC].inode
  19181. +#define src_h_dentry sd[AuSRC].h_dentry
  19182. +#define src_parent sd[AuSRC].parent
  19183. +#define src_h_parent sd[AuSRC].h_parent
  19184. +#define src_wh_dentry sd[AuSRC].wh_dentry
  19185. +#define src_hdir sd[AuSRC].hdir
  19186. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  19187. +#define src_dt sd[AuSRC].dt
  19188. +#define src_bstart sd[AuSRC].bstart
  19189. +
  19190. +#define dst_dentry sd[AuDST].dentry
  19191. +#define dst_dir sd[AuDST].dir
  19192. +#define dst_inode sd[AuDST].inode
  19193. +#define dst_h_dentry sd[AuDST].h_dentry
  19194. +#define dst_parent sd[AuDST].parent
  19195. +#define dst_h_parent sd[AuDST].h_parent
  19196. +#define dst_wh_dentry sd[AuDST].wh_dentry
  19197. +#define dst_hdir sd[AuDST].hdir
  19198. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  19199. +#define dst_dt sd[AuDST].dt
  19200. +#define dst_bstart sd[AuDST].bstart
  19201. +
  19202. + struct dentry *h_trap;
  19203. + struct au_branch *br;
  19204. + struct au_hinode *src_hinode;
  19205. + struct path h_path;
  19206. + struct au_nhash whlist;
  19207. + aufs_bindex_t btgt, src_bwh, src_bdiropq;
  19208. +
  19209. + unsigned int flags;
  19210. +
  19211. + struct au_whtmp_rmdir *thargs;
  19212. + struct dentry *h_dst;
  19213. +};
  19214. +
  19215. +/* ---------------------------------------------------------------------- */
  19216. +
  19217. +/*
  19218. + * functions for reverting.
  19219. + * when an error happened in a single rename systemcall, we should revert
  19220. + * everything as if nothing happened.
  19221. + * we don't need to revert the copied-up/down the parent dir since they are
  19222. + * harmless.
  19223. + */
  19224. +
  19225. +#define RevertFailure(fmt, ...) do { \
  19226. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  19227. + ##__VA_ARGS__, err, rerr); \
  19228. + err = -EIO; \
  19229. +} while (0)
  19230. +
  19231. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  19232. +{
  19233. + int rerr;
  19234. +
  19235. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  19236. + rerr = au_diropq_remove(a->src_dentry, a->btgt);
  19237. + au_hn_imtx_unlock(a->src_hinode);
  19238. + au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
  19239. + if (rerr)
  19240. + RevertFailure("remove diropq %pd", a->src_dentry);
  19241. +}
  19242. +
  19243. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  19244. +{
  19245. + int rerr;
  19246. + struct inode *delegated;
  19247. +
  19248. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  19249. + a->src_h_parent);
  19250. + rerr = PTR_ERR(a->h_path.dentry);
  19251. + if (IS_ERR(a->h_path.dentry)) {
  19252. + RevertFailure("lkup one %pd", a->src_dentry);
  19253. + return;
  19254. + }
  19255. +
  19256. + delegated = NULL;
  19257. + rerr = vfsub_rename(a->dst_h_dir,
  19258. + au_h_dptr(a->src_dentry, a->btgt),
  19259. + a->src_h_dir, &a->h_path, &delegated);
  19260. + if (unlikely(rerr == -EWOULDBLOCK)) {
  19261. + pr_warn("cannot retry for NFSv4 delegation"
  19262. + " for an internal rename\n");
  19263. + iput(delegated);
  19264. + }
  19265. + d_drop(a->h_path.dentry);
  19266. + dput(a->h_path.dentry);
  19267. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  19268. + if (rerr)
  19269. + RevertFailure("rename %pd", a->src_dentry);
  19270. +}
  19271. +
  19272. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  19273. +{
  19274. + int rerr;
  19275. + struct inode *delegated;
  19276. +
  19277. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  19278. + a->dst_h_parent);
  19279. + rerr = PTR_ERR(a->h_path.dentry);
  19280. + if (IS_ERR(a->h_path.dentry)) {
  19281. + RevertFailure("lkup one %pd", a->dst_dentry);
  19282. + return;
  19283. + }
  19284. + if (d_is_positive(a->h_path.dentry)) {
  19285. + d_drop(a->h_path.dentry);
  19286. + dput(a->h_path.dentry);
  19287. + return;
  19288. + }
  19289. +
  19290. + delegated = NULL;
  19291. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  19292. + &delegated);
  19293. + if (unlikely(rerr == -EWOULDBLOCK)) {
  19294. + pr_warn("cannot retry for NFSv4 delegation"
  19295. + " for an internal rename\n");
  19296. + iput(delegated);
  19297. + }
  19298. + d_drop(a->h_path.dentry);
  19299. + dput(a->h_path.dentry);
  19300. + if (!rerr)
  19301. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  19302. + else
  19303. + RevertFailure("rename %pd", a->h_dst);
  19304. +}
  19305. +
  19306. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  19307. +{
  19308. + int rerr;
  19309. +
  19310. + a->h_path.dentry = a->src_wh_dentry;
  19311. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  19312. + au_set_dbwh(a->src_dentry, a->src_bwh);
  19313. + if (rerr)
  19314. + RevertFailure("unlink %pd", a->src_wh_dentry);
  19315. +}
  19316. +#undef RevertFailure
  19317. +
  19318. +/* ---------------------------------------------------------------------- */
  19319. +
  19320. +/*
  19321. + * when we have to copyup the renaming entry, do it with the rename-target name
  19322. + * in order to minimize the cost (the later actual rename is unnecessary).
  19323. + * otherwise rename it on the target branch.
  19324. + */
  19325. +static int au_ren_or_cpup(struct au_ren_args *a)
  19326. +{
  19327. + int err;
  19328. + struct dentry *d;
  19329. + struct inode *delegated;
  19330. +
  19331. + d = a->src_dentry;
  19332. + if (au_dbstart(d) == a->btgt) {
  19333. + a->h_path.dentry = a->dst_h_dentry;
  19334. + if (au_ftest_ren(a->flags, DIROPQ)
  19335. + && au_dbdiropq(d) == a->btgt)
  19336. + au_fclr_ren(a->flags, DIROPQ);
  19337. + AuDebugOn(au_dbstart(d) != a->btgt);
  19338. + delegated = NULL;
  19339. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  19340. + a->dst_h_dir, &a->h_path, &delegated);
  19341. + if (unlikely(err == -EWOULDBLOCK)) {
  19342. + pr_warn("cannot retry for NFSv4 delegation"
  19343. + " for an internal rename\n");
  19344. + iput(delegated);
  19345. + }
  19346. + } else
  19347. + BUG();
  19348. +
  19349. + if (!err && a->h_dst)
  19350. + /* it will be set to dinfo later */
  19351. + dget(a->h_dst);
  19352. +
  19353. + return err;
  19354. +}
  19355. +
  19356. +/* cf. aufs_rmdir() */
  19357. +static int au_ren_del_whtmp(struct au_ren_args *a)
  19358. +{
  19359. + int err;
  19360. + struct inode *dir;
  19361. +
  19362. + dir = a->dst_dir;
  19363. + SiMustAnyLock(dir->i_sb);
  19364. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  19365. + au_sbi(dir->i_sb)->si_dirwh)
  19366. + || au_test_fs_remote(a->h_dst->d_sb)) {
  19367. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  19368. + if (unlikely(err))
  19369. + pr_warn("failed removing whtmp dir %pd (%d), "
  19370. + "ignored.\n", a->h_dst, err);
  19371. + } else {
  19372. + au_nhash_wh_free(&a->thargs->whlist);
  19373. + a->thargs->whlist = a->whlist;
  19374. + a->whlist.nh_num = 0;
  19375. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  19376. + dput(a->h_dst);
  19377. + a->thargs = NULL;
  19378. + }
  19379. +
  19380. + return 0;
  19381. +}
  19382. +
  19383. +/* make it 'opaque' dir. */
  19384. +static int au_ren_diropq(struct au_ren_args *a)
  19385. +{
  19386. + int err;
  19387. + struct dentry *diropq;
  19388. +
  19389. + err = 0;
  19390. + a->src_bdiropq = au_dbdiropq(a->src_dentry);
  19391. + a->src_hinode = au_hi(a->src_inode, a->btgt);
  19392. + au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
  19393. + diropq = au_diropq_create(a->src_dentry, a->btgt);
  19394. + au_hn_imtx_unlock(a->src_hinode);
  19395. + if (IS_ERR(diropq))
  19396. + err = PTR_ERR(diropq);
  19397. + else
  19398. + dput(diropq);
  19399. +
  19400. + return err;
  19401. +}
  19402. +
  19403. +static int do_rename(struct au_ren_args *a)
  19404. +{
  19405. + int err;
  19406. + struct dentry *d, *h_d;
  19407. +
  19408. + /* prepare workqueue args for asynchronous rmdir */
  19409. + h_d = a->dst_h_dentry;
  19410. + if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
  19411. + err = -ENOMEM;
  19412. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
  19413. + if (unlikely(!a->thargs))
  19414. + goto out;
  19415. + a->h_dst = dget(h_d);
  19416. + }
  19417. +
  19418. + /* create whiteout for src_dentry */
  19419. + if (au_ftest_ren(a->flags, WHSRC)) {
  19420. + a->src_bwh = au_dbwh(a->src_dentry);
  19421. + AuDebugOn(a->src_bwh >= 0);
  19422. + a->src_wh_dentry
  19423. + = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
  19424. + err = PTR_ERR(a->src_wh_dentry);
  19425. + if (IS_ERR(a->src_wh_dentry))
  19426. + goto out_thargs;
  19427. + }
  19428. +
  19429. + /* lookup whiteout for dentry */
  19430. + if (au_ftest_ren(a->flags, WHDST)) {
  19431. + h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
  19432. + a->br);
  19433. + err = PTR_ERR(h_d);
  19434. + if (IS_ERR(h_d))
  19435. + goto out_whsrc;
  19436. + if (d_is_negative(h_d))
  19437. + dput(h_d);
  19438. + else
  19439. + a->dst_wh_dentry = h_d;
  19440. + }
  19441. +
  19442. + /* rename dentry to tmpwh */
  19443. + if (a->thargs) {
  19444. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  19445. + if (unlikely(err))
  19446. + goto out_whdst;
  19447. +
  19448. + d = a->dst_dentry;
  19449. + au_set_h_dptr(d, a->btgt, NULL);
  19450. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  19451. + if (unlikely(err))
  19452. + goto out_whtmp;
  19453. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  19454. + }
  19455. +
  19456. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_bstart != a->btgt);
  19457. +
  19458. + /* rename by vfs_rename or cpup */
  19459. + d = a->dst_dentry;
  19460. + if (au_ftest_ren(a->flags, ISDIR)
  19461. + && (a->dst_wh_dentry
  19462. + || au_dbdiropq(d) == a->btgt
  19463. + /* hide the lower to keep xino */
  19464. + || a->btgt < au_dbend(d)
  19465. + || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
  19466. + au_fset_ren(a->flags, DIROPQ);
  19467. + err = au_ren_or_cpup(a);
  19468. + if (unlikely(err))
  19469. + /* leave the copied-up one */
  19470. + goto out_whtmp;
  19471. +
  19472. + /* make dir opaque */
  19473. + if (au_ftest_ren(a->flags, DIROPQ)) {
  19474. + err = au_ren_diropq(a);
  19475. + if (unlikely(err))
  19476. + goto out_rename;
  19477. + }
  19478. +
  19479. + /* update target timestamps */
  19480. + AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
  19481. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  19482. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  19483. + a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  19484. +
  19485. + /* remove whiteout for dentry */
  19486. + if (a->dst_wh_dentry) {
  19487. + a->h_path.dentry = a->dst_wh_dentry;
  19488. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  19489. + a->dst_dentry);
  19490. + if (unlikely(err))
  19491. + goto out_diropq;
  19492. + }
  19493. +
  19494. + /* remove whtmp */
  19495. + if (a->thargs)
  19496. + au_ren_del_whtmp(a); /* ignore this error */
  19497. +
  19498. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  19499. + err = 0;
  19500. + goto out_success;
  19501. +
  19502. +out_diropq:
  19503. + if (au_ftest_ren(a->flags, DIROPQ))
  19504. + au_ren_rev_diropq(err, a);
  19505. +out_rename:
  19506. + au_ren_rev_rename(err, a);
  19507. + dput(a->h_dst);
  19508. +out_whtmp:
  19509. + if (a->thargs)
  19510. + au_ren_rev_whtmp(err, a);
  19511. +out_whdst:
  19512. + dput(a->dst_wh_dentry);
  19513. + a->dst_wh_dentry = NULL;
  19514. +out_whsrc:
  19515. + if (a->src_wh_dentry)
  19516. + au_ren_rev_whsrc(err, a);
  19517. +out_success:
  19518. + dput(a->src_wh_dentry);
  19519. + dput(a->dst_wh_dentry);
  19520. +out_thargs:
  19521. + if (a->thargs) {
  19522. + dput(a->h_dst);
  19523. + au_whtmp_rmdir_free(a->thargs);
  19524. + a->thargs = NULL;
  19525. + }
  19526. +out:
  19527. + return err;
  19528. +}
  19529. +
  19530. +/* ---------------------------------------------------------------------- */
  19531. +
  19532. +/*
  19533. + * test if @dentry dir can be rename destination or not.
  19534. + * success means, it is a logically empty dir.
  19535. + */
  19536. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  19537. +{
  19538. + return au_test_empty(dentry, whlist);
  19539. +}
  19540. +
  19541. +/*
  19542. + * test if @dentry dir can be rename source or not.
  19543. + * if it can, return 0 and @children is filled.
  19544. + * success means,
  19545. + * - it is a logically empty dir.
  19546. + * - or, it exists on writable branch and has no children including whiteouts
  19547. + * on the lower branch.
  19548. + */
  19549. +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
  19550. +{
  19551. + int err;
  19552. + unsigned int rdhash;
  19553. + aufs_bindex_t bstart;
  19554. +
  19555. + bstart = au_dbstart(dentry);
  19556. + if (bstart != btgt) {
  19557. + struct au_nhash whlist;
  19558. +
  19559. + SiMustAnyLock(dentry->d_sb);
  19560. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  19561. + if (!rdhash)
  19562. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  19563. + dentry));
  19564. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  19565. + if (unlikely(err))
  19566. + goto out;
  19567. + err = au_test_empty(dentry, &whlist);
  19568. + au_nhash_wh_free(&whlist);
  19569. + goto out;
  19570. + }
  19571. +
  19572. + if (bstart == au_dbtaildir(dentry))
  19573. + return 0; /* success */
  19574. +
  19575. + err = au_test_empty_lower(dentry);
  19576. +
  19577. +out:
  19578. + if (err == -ENOTEMPTY) {
  19579. + AuWarn1("renaming dir who has child(ren) on multiple branches,"
  19580. + " is not supported\n");
  19581. + err = -EXDEV;
  19582. + }
  19583. + return err;
  19584. +}
  19585. +
  19586. +/* side effect: sets whlist and h_dentry */
  19587. +static int au_ren_may_dir(struct au_ren_args *a)
  19588. +{
  19589. + int err;
  19590. + unsigned int rdhash;
  19591. + struct dentry *d;
  19592. +
  19593. + d = a->dst_dentry;
  19594. + SiMustAnyLock(d->d_sb);
  19595. +
  19596. + err = 0;
  19597. + if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
  19598. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  19599. + if (!rdhash)
  19600. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  19601. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  19602. + if (unlikely(err))
  19603. + goto out;
  19604. +
  19605. + au_set_dbstart(d, a->dst_bstart);
  19606. + err = may_rename_dstdir(d, &a->whlist);
  19607. + au_set_dbstart(d, a->btgt);
  19608. + }
  19609. + a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
  19610. + if (unlikely(err))
  19611. + goto out;
  19612. +
  19613. + d = a->src_dentry;
  19614. + a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
  19615. + if (au_ftest_ren(a->flags, ISDIR)) {
  19616. + err = may_rename_srcdir(d, a->btgt);
  19617. + if (unlikely(err)) {
  19618. + au_nhash_wh_free(&a->whlist);
  19619. + a->whlist.nh_num = 0;
  19620. + }
  19621. + }
  19622. +out:
  19623. + return err;
  19624. +}
  19625. +
  19626. +/* ---------------------------------------------------------------------- */
  19627. +
  19628. +/*
  19629. + * simple tests for rename.
  19630. + * following the checks in vfs, plus the parent-child relationship.
  19631. + */
  19632. +static int au_may_ren(struct au_ren_args *a)
  19633. +{
  19634. + int err, isdir;
  19635. + struct inode *h_inode;
  19636. +
  19637. + if (a->src_bstart == a->btgt) {
  19638. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  19639. + au_ftest_ren(a->flags, ISDIR));
  19640. + if (unlikely(err))
  19641. + goto out;
  19642. + err = -EINVAL;
  19643. + if (unlikely(a->src_h_dentry == a->h_trap))
  19644. + goto out;
  19645. + }
  19646. +
  19647. + err = 0;
  19648. + if (a->dst_bstart != a->btgt)
  19649. + goto out;
  19650. +
  19651. + err = -ENOTEMPTY;
  19652. + if (unlikely(a->dst_h_dentry == a->h_trap))
  19653. + goto out;
  19654. +
  19655. + err = -EIO;
  19656. + isdir = !!au_ftest_ren(a->flags, ISDIR);
  19657. + if (d_really_is_negative(a->dst_dentry)) {
  19658. + if (d_is_negative(a->dst_h_dentry))
  19659. + err = au_may_add(a->dst_dentry, a->btgt,
  19660. + a->dst_h_parent, isdir);
  19661. + } else {
  19662. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  19663. + goto out;
  19664. + h_inode = d_inode(a->dst_h_dentry);
  19665. + if (h_inode->i_nlink)
  19666. + err = au_may_del(a->dst_dentry, a->btgt,
  19667. + a->dst_h_parent, isdir);
  19668. + }
  19669. +
  19670. +out:
  19671. + if (unlikely(err == -ENOENT || err == -EEXIST))
  19672. + err = -EIO;
  19673. + AuTraceErr(err);
  19674. + return err;
  19675. +}
  19676. +
  19677. +/* ---------------------------------------------------------------------- */
  19678. +
  19679. +/*
  19680. + * locking order
  19681. + * (VFS)
  19682. + * - src_dir and dir by lock_rename()
  19683. + * - inode if exitsts
  19684. + * (aufs)
  19685. + * - lock all
  19686. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  19687. + * + si_read_lock
  19688. + * + di_write_lock2_child()
  19689. + * + di_write_lock_child()
  19690. + * + ii_write_lock_child()
  19691. + * + di_write_lock_child2()
  19692. + * + ii_write_lock_child2()
  19693. + * + src_parent and parent
  19694. + * + di_write_lock_parent()
  19695. + * + ii_write_lock_parent()
  19696. + * + di_write_lock_parent2()
  19697. + * + ii_write_lock_parent2()
  19698. + * + lower src_dir and dir by vfsub_lock_rename()
  19699. + * + verify the every relationships between child and parent. if any
  19700. + * of them failed, unlock all and return -EBUSY.
  19701. + */
  19702. +static void au_ren_unlock(struct au_ren_args *a)
  19703. +{
  19704. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  19705. + a->dst_h_parent, a->dst_hdir);
  19706. + if (au_ftest_ren(a->flags, MNT_WRITE))
  19707. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  19708. +}
  19709. +
  19710. +static int au_ren_lock(struct au_ren_args *a)
  19711. +{
  19712. + int err;
  19713. + unsigned int udba;
  19714. +
  19715. + err = 0;
  19716. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  19717. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  19718. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  19719. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  19720. +
  19721. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  19722. + if (unlikely(err))
  19723. + goto out;
  19724. + au_fset_ren(a->flags, MNT_WRITE);
  19725. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  19726. + a->dst_h_parent, a->dst_hdir);
  19727. + udba = au_opt_udba(a->src_dentry->d_sb);
  19728. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  19729. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  19730. + err = au_busy_or_stale();
  19731. + if (!err && au_dbstart(a->src_dentry) == a->btgt)
  19732. + err = au_h_verify(a->src_h_dentry, udba,
  19733. + d_inode(a->src_h_parent), a->src_h_parent,
  19734. + a->br);
  19735. + if (!err && au_dbstart(a->dst_dentry) == a->btgt)
  19736. + err = au_h_verify(a->dst_h_dentry, udba,
  19737. + d_inode(a->dst_h_parent), a->dst_h_parent,
  19738. + a->br);
  19739. + if (!err)
  19740. + goto out; /* success */
  19741. +
  19742. + err = au_busy_or_stale();
  19743. + au_ren_unlock(a);
  19744. +
  19745. +out:
  19746. + return err;
  19747. +}
  19748. +
  19749. +/* ---------------------------------------------------------------------- */
  19750. +
  19751. +static void au_ren_refresh_dir(struct au_ren_args *a)
  19752. +{
  19753. + struct inode *dir;
  19754. +
  19755. + dir = a->dst_dir;
  19756. + dir->i_version++;
  19757. + if (au_ftest_ren(a->flags, ISDIR)) {
  19758. + /* is this updating defined in POSIX? */
  19759. + au_cpup_attr_timesizes(a->src_inode);
  19760. + au_cpup_attr_nlink(dir, /*force*/1);
  19761. + }
  19762. +
  19763. + au_dir_ts(dir, a->btgt);
  19764. +
  19765. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  19766. + return;
  19767. +
  19768. + dir = a->src_dir;
  19769. + dir->i_version++;
  19770. + if (au_ftest_ren(a->flags, ISDIR))
  19771. + au_cpup_attr_nlink(dir, /*force*/1);
  19772. + au_dir_ts(dir, a->btgt);
  19773. +}
  19774. +
  19775. +static void au_ren_refresh(struct au_ren_args *a)
  19776. +{
  19777. + aufs_bindex_t bend, bindex;
  19778. + struct dentry *d, *h_d;
  19779. + struct inode *i, *h_i;
  19780. + struct super_block *sb;
  19781. +
  19782. + d = a->dst_dentry;
  19783. + d_drop(d);
  19784. + if (a->h_dst)
  19785. + /* already dget-ed by au_ren_or_cpup() */
  19786. + au_set_h_dptr(d, a->btgt, a->h_dst);
  19787. +
  19788. + i = a->dst_inode;
  19789. + if (i) {
  19790. + if (!au_ftest_ren(a->flags, ISDIR))
  19791. + vfsub_drop_nlink(i);
  19792. + else {
  19793. + vfsub_dead_dir(i);
  19794. + au_cpup_attr_timesizes(i);
  19795. + }
  19796. + au_update_dbrange(d, /*do_put_zero*/1);
  19797. + } else {
  19798. + bend = a->btgt;
  19799. + for (bindex = au_dbstart(d); bindex < bend; bindex++)
  19800. + au_set_h_dptr(d, bindex, NULL);
  19801. + bend = au_dbend(d);
  19802. + for (bindex = a->btgt + 1; bindex <= bend; bindex++)
  19803. + au_set_h_dptr(d, bindex, NULL);
  19804. + au_update_dbrange(d, /*do_put_zero*/0);
  19805. + }
  19806. +
  19807. + d = a->src_dentry;
  19808. + au_set_dbwh(d, -1);
  19809. + bend = au_dbend(d);
  19810. + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
  19811. + h_d = au_h_dptr(d, bindex);
  19812. + if (h_d)
  19813. + au_set_h_dptr(d, bindex, NULL);
  19814. + }
  19815. + au_set_dbend(d, a->btgt);
  19816. +
  19817. + sb = d->d_sb;
  19818. + i = a->src_inode;
  19819. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  19820. + return; /* success */
  19821. +
  19822. + bend = au_ibend(i);
  19823. + for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
  19824. + h_i = au_h_iptr(i, bindex);
  19825. + if (h_i) {
  19826. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  19827. + /* ignore this error */
  19828. + au_set_h_iptr(i, bindex, NULL, 0);
  19829. + }
  19830. + }
  19831. + au_set_ibend(i, a->btgt);
  19832. +}
  19833. +
  19834. +/* ---------------------------------------------------------------------- */
  19835. +
  19836. +/* mainly for link(2) and rename(2) */
  19837. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  19838. +{
  19839. + aufs_bindex_t bdiropq, bwh;
  19840. + struct dentry *parent;
  19841. + struct au_branch *br;
  19842. +
  19843. + parent = dentry->d_parent;
  19844. + IMustLock(d_inode(parent)); /* dir is locked */
  19845. +
  19846. + bdiropq = au_dbdiropq(parent);
  19847. + bwh = au_dbwh(dentry);
  19848. + br = au_sbr(dentry->d_sb, btgt);
  19849. + if (au_br_rdonly(br)
  19850. + || (0 <= bdiropq && bdiropq < btgt)
  19851. + || (0 <= bwh && bwh < btgt))
  19852. + btgt = -1;
  19853. +
  19854. + AuDbg("btgt %d\n", btgt);
  19855. + return btgt;
  19856. +}
  19857. +
  19858. +/* sets src_bstart, dst_bstart and btgt */
  19859. +static int au_ren_wbr(struct au_ren_args *a)
  19860. +{
  19861. + int err;
  19862. + struct au_wr_dir_args wr_dir_args = {
  19863. + /* .force_btgt = -1, */
  19864. + .flags = AuWrDir_ADD_ENTRY
  19865. + };
  19866. +
  19867. + a->src_bstart = au_dbstart(a->src_dentry);
  19868. + a->dst_bstart = au_dbstart(a->dst_dentry);
  19869. + if (au_ftest_ren(a->flags, ISDIR))
  19870. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  19871. + wr_dir_args.force_btgt = a->src_bstart;
  19872. + if (a->dst_inode && a->dst_bstart < a->src_bstart)
  19873. + wr_dir_args.force_btgt = a->dst_bstart;
  19874. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  19875. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  19876. + a->btgt = err;
  19877. +
  19878. + return err;
  19879. +}
  19880. +
  19881. +static void au_ren_dt(struct au_ren_args *a)
  19882. +{
  19883. + a->h_path.dentry = a->src_h_parent;
  19884. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  19885. + if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
  19886. + a->h_path.dentry = a->dst_h_parent;
  19887. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  19888. + }
  19889. +
  19890. + au_fclr_ren(a->flags, DT_DSTDIR);
  19891. + if (!au_ftest_ren(a->flags, ISDIR))
  19892. + return;
  19893. +
  19894. + a->h_path.dentry = a->src_h_dentry;
  19895. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  19896. + if (d_is_positive(a->dst_h_dentry)) {
  19897. + au_fset_ren(a->flags, DT_DSTDIR);
  19898. + a->h_path.dentry = a->dst_h_dentry;
  19899. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  19900. + }
  19901. +}
  19902. +
  19903. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  19904. +{
  19905. + struct dentry *h_d;
  19906. + struct mutex *h_mtx;
  19907. +
  19908. + au_dtime_revert(a->src_dt + AuPARENT);
  19909. + if (!au_ftest_ren(a->flags, ISSAMEDIR))
  19910. + au_dtime_revert(a->dst_dt + AuPARENT);
  19911. +
  19912. + if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
  19913. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  19914. + h_mtx = &d_inode(h_d)->i_mutex;
  19915. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  19916. + au_dtime_revert(a->src_dt + AuCHILD);
  19917. + mutex_unlock(h_mtx);
  19918. +
  19919. + if (au_ftest_ren(a->flags, DT_DSTDIR)) {
  19920. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  19921. + h_mtx = &d_inode(h_d)->i_mutex;
  19922. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
  19923. + au_dtime_revert(a->dst_dt + AuCHILD);
  19924. + mutex_unlock(h_mtx);
  19925. + }
  19926. + }
  19927. +}
  19928. +
  19929. +/* ---------------------------------------------------------------------- */
  19930. +
  19931. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  19932. + struct inode *_dst_dir, struct dentry *_dst_dentry)
  19933. +{
  19934. + int err, flags;
  19935. + /* reduce stack space */
  19936. + struct au_ren_args *a;
  19937. +
  19938. + AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
  19939. + IMustLock(_src_dir);
  19940. + IMustLock(_dst_dir);
  19941. +
  19942. + err = -ENOMEM;
  19943. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  19944. + a = kzalloc(sizeof(*a), GFP_NOFS);
  19945. + if (unlikely(!a))
  19946. + goto out;
  19947. +
  19948. + a->src_dir = _src_dir;
  19949. + a->src_dentry = _src_dentry;
  19950. + a->src_inode = NULL;
  19951. + if (d_really_is_positive(a->src_dentry))
  19952. + a->src_inode = d_inode(a->src_dentry);
  19953. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  19954. + a->dst_dir = _dst_dir;
  19955. + a->dst_dentry = _dst_dentry;
  19956. + a->dst_inode = NULL;
  19957. + if (d_really_is_positive(a->dst_dentry))
  19958. + a->dst_inode = d_inode(a->dst_dentry);
  19959. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  19960. + if (a->dst_inode) {
  19961. + IMustLock(a->dst_inode);
  19962. + au_igrab(a->dst_inode);
  19963. + }
  19964. +
  19965. + err = -ENOTDIR;
  19966. + flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  19967. + if (d_is_dir(a->src_dentry)) {
  19968. + au_fset_ren(a->flags, ISDIR);
  19969. + if (unlikely(d_really_is_positive(a->dst_dentry)
  19970. + && !d_is_dir(a->dst_dentry)))
  19971. + goto out_free;
  19972. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  19973. + AuLock_DIR | flags);
  19974. + } else
  19975. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  19976. + flags);
  19977. + if (unlikely(err))
  19978. + goto out_free;
  19979. +
  19980. + err = au_d_hashed_positive(a->src_dentry);
  19981. + if (unlikely(err))
  19982. + goto out_unlock;
  19983. + err = -ENOENT;
  19984. + if (a->dst_inode) {
  19985. + /*
  19986. + * If it is a dir, VFS unhash dst_dentry before this
  19987. + * function. It means we cannot rely upon d_unhashed().
  19988. + */
  19989. + if (unlikely(!a->dst_inode->i_nlink))
  19990. + goto out_unlock;
  19991. + if (!S_ISDIR(a->dst_inode->i_mode)) {
  19992. + err = au_d_hashed_positive(a->dst_dentry);
  19993. + if (unlikely(err))
  19994. + goto out_unlock;
  19995. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  19996. + goto out_unlock;
  19997. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  19998. + goto out_unlock;
  19999. +
  20000. + /*
  20001. + * is it possible?
  20002. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  20003. + * there may exist a problem somewhere else.
  20004. + */
  20005. + err = -EINVAL;
  20006. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  20007. + goto out_unlock;
  20008. +
  20009. + au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
  20010. + di_write_lock_parent(a->dst_parent);
  20011. +
  20012. + /* which branch we process */
  20013. + err = au_ren_wbr(a);
  20014. + if (unlikely(err < 0))
  20015. + goto out_parent;
  20016. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  20017. + a->h_path.mnt = au_br_mnt(a->br);
  20018. +
  20019. + /* are they available to be renamed */
  20020. + err = au_ren_may_dir(a);
  20021. + if (unlikely(err))
  20022. + goto out_children;
  20023. +
  20024. + /* prepare the writable parent dir on the same branch */
  20025. + if (a->dst_bstart == a->btgt) {
  20026. + au_fset_ren(a->flags, WHDST);
  20027. + } else {
  20028. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  20029. + if (unlikely(err))
  20030. + goto out_children;
  20031. + }
  20032. +
  20033. + if (a->src_dir != a->dst_dir) {
  20034. + /*
  20035. + * this temporary unlock is safe,
  20036. + * because both dir->i_mutex are locked.
  20037. + */
  20038. + di_write_unlock(a->dst_parent);
  20039. + di_write_lock_parent(a->src_parent);
  20040. + err = au_wr_dir_need_wh(a->src_dentry,
  20041. + au_ftest_ren(a->flags, ISDIR),
  20042. + &a->btgt);
  20043. + di_write_unlock(a->src_parent);
  20044. + di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
  20045. + au_fclr_ren(a->flags, ISSAMEDIR);
  20046. + } else
  20047. + err = au_wr_dir_need_wh(a->src_dentry,
  20048. + au_ftest_ren(a->flags, ISDIR),
  20049. + &a->btgt);
  20050. + if (unlikely(err < 0))
  20051. + goto out_children;
  20052. + if (err)
  20053. + au_fset_ren(a->flags, WHSRC);
  20054. +
  20055. + /* cpup src */
  20056. + if (a->src_bstart != a->btgt) {
  20057. + struct au_pin pin;
  20058. +
  20059. + err = au_pin(&pin, a->src_dentry, a->btgt,
  20060. + au_opt_udba(a->src_dentry->d_sb),
  20061. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20062. + if (!err) {
  20063. + struct au_cp_generic cpg = {
  20064. + .dentry = a->src_dentry,
  20065. + .bdst = a->btgt,
  20066. + .bsrc = a->src_bstart,
  20067. + .len = -1,
  20068. + .pin = &pin,
  20069. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  20070. + };
  20071. + AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
  20072. + err = au_sio_cpup_simple(&cpg);
  20073. + au_unpin(&pin);
  20074. + }
  20075. + if (unlikely(err))
  20076. + goto out_children;
  20077. + a->src_bstart = a->btgt;
  20078. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  20079. + au_fset_ren(a->flags, WHSRC);
  20080. + }
  20081. +
  20082. + /* lock them all */
  20083. + err = au_ren_lock(a);
  20084. + if (unlikely(err))
  20085. + /* leave the copied-up one */
  20086. + goto out_children;
  20087. +
  20088. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  20089. + err = au_may_ren(a);
  20090. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  20091. + err = -ENAMETOOLONG;
  20092. + if (unlikely(err))
  20093. + goto out_hdir;
  20094. +
  20095. + /* store timestamps to be revertible */
  20096. + au_ren_dt(a);
  20097. +
  20098. + /* here we go */
  20099. + err = do_rename(a);
  20100. + if (unlikely(err))
  20101. + goto out_dt;
  20102. +
  20103. + /* update dir attributes */
  20104. + au_ren_refresh_dir(a);
  20105. +
  20106. + /* dput/iput all lower dentries */
  20107. + au_ren_refresh(a);
  20108. +
  20109. + goto out_hdir; /* success */
  20110. +
  20111. +out_dt:
  20112. + au_ren_rev_dt(err, a);
  20113. +out_hdir:
  20114. + au_ren_unlock(a);
  20115. +out_children:
  20116. + au_nhash_wh_free(&a->whlist);
  20117. + if (err && a->dst_inode && a->dst_bstart != a->btgt) {
  20118. + AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
  20119. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  20120. + au_set_dbstart(a->dst_dentry, a->dst_bstart);
  20121. + }
  20122. +out_parent:
  20123. + if (!err)
  20124. + d_move(a->src_dentry, a->dst_dentry);
  20125. + else {
  20126. + au_update_dbstart(a->dst_dentry);
  20127. + if (!a->dst_inode)
  20128. + d_drop(a->dst_dentry);
  20129. + }
  20130. + if (au_ftest_ren(a->flags, ISSAMEDIR))
  20131. + di_write_unlock(a->dst_parent);
  20132. + else
  20133. + di_write_unlock2(a->src_parent, a->dst_parent);
  20134. +out_unlock:
  20135. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  20136. +out_free:
  20137. + iput(a->dst_inode);
  20138. + if (a->thargs)
  20139. + au_whtmp_rmdir_free(a->thargs);
  20140. + kfree(a);
  20141. +out:
  20142. + AuTraceErr(err);
  20143. + return err;
  20144. +}
  20145. diff -Nur linux-4.1.10.orig/fs/aufs/Kconfig linux-4.1.10/fs/aufs/Kconfig
  20146. --- linux-4.1.10.orig/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
  20147. +++ linux-4.1.10/fs/aufs/Kconfig 2015-10-22 21:36:44.000000000 +0200
  20148. @@ -0,0 +1,185 @@
  20149. +config AUFS_FS
  20150. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  20151. + help
  20152. + Aufs is a stackable unification filesystem such as Unionfs,
  20153. + which unifies several directories and provides a merged single
  20154. + directory.
  20155. + In the early days, aufs was entirely re-designed and
  20156. + re-implemented Unionfs Version 1.x series. Introducing many
  20157. + original ideas, approaches and improvements, it becomes totally
  20158. + different from Unionfs while keeping the basic features.
  20159. +
  20160. +if AUFS_FS
  20161. +choice
  20162. + prompt "Maximum number of branches"
  20163. + default AUFS_BRANCH_MAX_127
  20164. + help
  20165. + Specifies the maximum number of branches (or member directories)
  20166. + in a single aufs. The larger value consumes more system
  20167. + resources and has a minor impact to performance.
  20168. +config AUFS_BRANCH_MAX_127
  20169. + bool "127"
  20170. + help
  20171. + Specifies the maximum number of branches (or member directories)
  20172. + in a single aufs. The larger value consumes more system
  20173. + resources and has a minor impact to performance.
  20174. +config AUFS_BRANCH_MAX_511
  20175. + bool "511"
  20176. + help
  20177. + Specifies the maximum number of branches (or member directories)
  20178. + in a single aufs. The larger value consumes more system
  20179. + resources and has a minor impact to performance.
  20180. +config AUFS_BRANCH_MAX_1023
  20181. + bool "1023"
  20182. + help
  20183. + Specifies the maximum number of branches (or member directories)
  20184. + in a single aufs. The larger value consumes more system
  20185. + resources and has a minor impact to performance.
  20186. +config AUFS_BRANCH_MAX_32767
  20187. + bool "32767"
  20188. + help
  20189. + Specifies the maximum number of branches (or member directories)
  20190. + in a single aufs. The larger value consumes more system
  20191. + resources and has a minor impact to performance.
  20192. +endchoice
  20193. +
  20194. +config AUFS_SBILIST
  20195. + bool
  20196. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  20197. + default y
  20198. + help
  20199. + Automatic configuration for internal use.
  20200. + When aufs supports Magic SysRq or /proc, enabled automatically.
  20201. +
  20202. +config AUFS_HNOTIFY
  20203. + bool "Detect direct branch access (bypassing aufs)"
  20204. + help
  20205. + If you want to modify files on branches directly, eg. bypassing aufs,
  20206. + and want aufs to detect the changes of them fully, then enable this
  20207. + option and use 'udba=notify' mount option.
  20208. + Currently there is only one available configuration, "fsnotify".
  20209. + It will have a negative impact to the performance.
  20210. + See detail in aufs.5.
  20211. +
  20212. +choice
  20213. + prompt "method" if AUFS_HNOTIFY
  20214. + default AUFS_HFSNOTIFY
  20215. +config AUFS_HFSNOTIFY
  20216. + bool "fsnotify"
  20217. + select FSNOTIFY
  20218. +endchoice
  20219. +
  20220. +config AUFS_EXPORT
  20221. + bool "NFS-exportable aufs"
  20222. + select EXPORTFS
  20223. + help
  20224. + If you want to export your mounted aufs via NFS, then enable this
  20225. + option. There are several requirements for this configuration.
  20226. + See detail in aufs.5.
  20227. +
  20228. +config AUFS_INO_T_64
  20229. + bool
  20230. + depends on AUFS_EXPORT
  20231. + depends on 64BIT && !(ALPHA || S390)
  20232. + default y
  20233. + help
  20234. + Automatic configuration for internal use.
  20235. + /* typedef unsigned long/int __kernel_ino_t */
  20236. + /* alpha and s390x are int */
  20237. +
  20238. +config AUFS_XATTR
  20239. + bool "support for XATTR/EA (including Security Labels)"
  20240. + help
  20241. + If your branch fs supports XATTR/EA and you want to make them
  20242. + available in aufs too, then enable this opsion and specify the
  20243. + branch attributes for EA.
  20244. + See detail in aufs.5.
  20245. +
  20246. +config AUFS_FHSM
  20247. + bool "File-based Hierarchical Storage Management"
  20248. + help
  20249. + Hierarchical Storage Management (or HSM) is a well-known feature
  20250. + in the storage world. Aufs provides this feature as file-based.
  20251. + with multiple branches.
  20252. + These multiple branches are prioritized, ie. the topmost one
  20253. + should be the fastest drive and be used heavily.
  20254. +
  20255. +config AUFS_RDU
  20256. + bool "Readdir in userspace"
  20257. + help
  20258. + Aufs has two methods to provide a merged view for a directory,
  20259. + by a user-space library and by kernel-space natively. The latter
  20260. + is always enabled but sometimes large and slow.
  20261. + If you enable this option, install the library in aufs2-util
  20262. + package, and set some environment variables for your readdir(3),
  20263. + then the work will be handled in user-space which generally
  20264. + shows better performance in most cases.
  20265. + See detail in aufs.5.
  20266. +
  20267. +config AUFS_SHWH
  20268. + bool "Show whiteouts"
  20269. + help
  20270. + If you want to make the whiteouts in aufs visible, then enable
  20271. + this option and specify 'shwh' mount option. Although it may
  20272. + sounds like philosophy or something, but in technically it
  20273. + simply shows the name of whiteout with keeping its behaviour.
  20274. +
  20275. +config AUFS_BR_RAMFS
  20276. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  20277. + help
  20278. + If you want to use ramfs as an aufs branch fs, then enable this
  20279. + option. Generally tmpfs is recommended.
  20280. + Aufs prohibited them to be a branch fs by default, because
  20281. + initramfs becomes unusable after switch_root or something
  20282. + generally. If you sets initramfs as an aufs branch and boot your
  20283. + system by switch_root, you will meet a problem easily since the
  20284. + files in initramfs may be inaccessible.
  20285. + Unless you are going to use ramfs as an aufs branch fs without
  20286. + switch_root or something, leave it N.
  20287. +
  20288. +config AUFS_BR_FUSE
  20289. + bool "Fuse fs as an aufs branch"
  20290. + depends on FUSE_FS
  20291. + select AUFS_POLL
  20292. + help
  20293. + If you want to use fuse-based userspace filesystem as an aufs
  20294. + branch fs, then enable this option.
  20295. + It implements the internal poll(2) operation which is
  20296. + implemented by fuse only (curretnly).
  20297. +
  20298. +config AUFS_POLL
  20299. + bool
  20300. + help
  20301. + Automatic configuration for internal use.
  20302. +
  20303. +config AUFS_BR_HFSPLUS
  20304. + bool "Hfsplus as an aufs branch"
  20305. + depends on HFSPLUS_FS
  20306. + default y
  20307. + help
  20308. + If you want to use hfsplus fs as an aufs branch fs, then enable
  20309. + this option. This option introduces a small overhead at
  20310. + copying-up a file on hfsplus.
  20311. +
  20312. +config AUFS_BDEV_LOOP
  20313. + bool
  20314. + depends on BLK_DEV_LOOP
  20315. + default y
  20316. + help
  20317. + Automatic configuration for internal use.
  20318. + Convert =[ym] into =y.
  20319. +
  20320. +config AUFS_DEBUG
  20321. + bool "Debug aufs"
  20322. + help
  20323. + Enable this to compile aufs internal debug code.
  20324. + It will have a negative impact to the performance.
  20325. +
  20326. +config AUFS_MAGIC_SYSRQ
  20327. + bool
  20328. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  20329. + default y
  20330. + help
  20331. + Automatic configuration for internal use.
  20332. + When aufs supports Magic SysRq, enabled automatically.
  20333. +endif
  20334. diff -Nur linux-4.1.10.orig/fs/aufs/loop.c linux-4.1.10/fs/aufs/loop.c
  20335. --- linux-4.1.10.orig/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
  20336. +++ linux-4.1.10/fs/aufs/loop.c 2015-10-22 21:35:53.000000000 +0200
  20337. @@ -0,0 +1,145 @@
  20338. +/*
  20339. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  20340. + *
  20341. + * This program, aufs is free software; you can redistribute it and/or modify
  20342. + * it under the terms of the GNU General Public License as published by
  20343. + * the Free Software Foundation; either version 2 of the License, or
  20344. + * (at your option) any later version.
  20345. + *
  20346. + * This program is distributed in the hope that it will be useful,
  20347. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20348. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20349. + * GNU General Public License for more details.
  20350. + *
  20351. + * You should have received a copy of the GNU General Public License
  20352. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20353. + */
  20354. +
  20355. +/*
  20356. + * support for loopback block device as a branch
  20357. + */
  20358. +
  20359. +#include "aufs.h"
  20360. +
  20361. +/* added into drivers/block/loop.c */
  20362. +static struct file *(*backing_file_func)(struct super_block *sb);
  20363. +
  20364. +/*
  20365. + * test if two lower dentries have overlapping branches.
  20366. + */
  20367. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  20368. +{
  20369. + struct super_block *h_sb;
  20370. + struct file *backing_file;
  20371. +
  20372. + if (unlikely(!backing_file_func)) {
  20373. + /* don't load "loop" module here */
  20374. + backing_file_func = symbol_get(loop_backing_file);
  20375. + if (unlikely(!backing_file_func))
  20376. + /* "loop" module is not loaded */
  20377. + return 0;
  20378. + }
  20379. +
  20380. + h_sb = h_adding->d_sb;
  20381. + backing_file = backing_file_func(h_sb);
  20382. + if (!backing_file)
  20383. + return 0;
  20384. +
  20385. + h_adding = backing_file->f_path.dentry;
  20386. + /*
  20387. + * h_adding can be local NFS.
  20388. + * in this case aufs cannot detect the loop.
  20389. + */
  20390. + if (unlikely(h_adding->d_sb == sb))
  20391. + return 1;
  20392. + return !!au_test_subdir(h_adding, sb->s_root);
  20393. +}
  20394. +
  20395. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  20396. +int au_test_loopback_kthread(void)
  20397. +{
  20398. + int ret;
  20399. + struct task_struct *tsk = current;
  20400. + char c, comm[sizeof(tsk->comm)];
  20401. +
  20402. + ret = 0;
  20403. + if (tsk->flags & PF_KTHREAD) {
  20404. + get_task_comm(comm, tsk);
  20405. + c = comm[4];
  20406. + ret = ('0' <= c && c <= '9'
  20407. + && !strncmp(comm, "loop", 4));
  20408. + }
  20409. +
  20410. + return ret;
  20411. +}
  20412. +
  20413. +/* ---------------------------------------------------------------------- */
  20414. +
  20415. +#define au_warn_loopback_step 16
  20416. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  20417. +static unsigned long *au_warn_loopback_array;
  20418. +
  20419. +void au_warn_loopback(struct super_block *h_sb)
  20420. +{
  20421. + int i, new_nelem;
  20422. + unsigned long *a, magic;
  20423. + static DEFINE_SPINLOCK(spin);
  20424. +
  20425. + magic = h_sb->s_magic;
  20426. + spin_lock(&spin);
  20427. + a = au_warn_loopback_array;
  20428. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  20429. + if (a[i] == magic) {
  20430. + spin_unlock(&spin);
  20431. + return;
  20432. + }
  20433. +
  20434. + /* h_sb is new to us, print it */
  20435. + if (i < au_warn_loopback_nelem) {
  20436. + a[i] = magic;
  20437. + goto pr;
  20438. + }
  20439. +
  20440. + /* expand the array */
  20441. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  20442. + a = au_kzrealloc(au_warn_loopback_array,
  20443. + au_warn_loopback_nelem * sizeof(unsigned long),
  20444. + new_nelem * sizeof(unsigned long), GFP_ATOMIC);
  20445. + if (a) {
  20446. + au_warn_loopback_nelem = new_nelem;
  20447. + au_warn_loopback_array = a;
  20448. + a[i] = magic;
  20449. + goto pr;
  20450. + }
  20451. +
  20452. + spin_unlock(&spin);
  20453. + AuWarn1("realloc failed, ignored\n");
  20454. + return;
  20455. +
  20456. +pr:
  20457. + spin_unlock(&spin);
  20458. + pr_warn("you may want to try another patch for loopback file "
  20459. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  20460. +}
  20461. +
  20462. +int au_loopback_init(void)
  20463. +{
  20464. + int err;
  20465. + struct super_block *sb __maybe_unused;
  20466. +
  20467. + AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
  20468. +
  20469. + err = 0;
  20470. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  20471. + sizeof(unsigned long), GFP_NOFS);
  20472. + if (unlikely(!au_warn_loopback_array))
  20473. + err = -ENOMEM;
  20474. +
  20475. + return err;
  20476. +}
  20477. +
  20478. +void au_loopback_fin(void)
  20479. +{
  20480. + symbol_put(loop_backing_file);
  20481. + kfree(au_warn_loopback_array);
  20482. +}
  20483. diff -Nur linux-4.1.10.orig/fs/aufs/loop.h linux-4.1.10/fs/aufs/loop.h
  20484. --- linux-4.1.10.orig/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
  20485. +++ linux-4.1.10/fs/aufs/loop.h 2015-10-22 21:35:53.000000000 +0200
  20486. @@ -0,0 +1,52 @@
  20487. +/*
  20488. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  20489. + *
  20490. + * This program, aufs is free software; you can redistribute it and/or modify
  20491. + * it under the terms of the GNU General Public License as published by
  20492. + * the Free Software Foundation; either version 2 of the License, or
  20493. + * (at your option) any later version.
  20494. + *
  20495. + * This program is distributed in the hope that it will be useful,
  20496. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20497. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20498. + * GNU General Public License for more details.
  20499. + *
  20500. + * You should have received a copy of the GNU General Public License
  20501. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20502. + */
  20503. +
  20504. +/*
  20505. + * support for loopback mount as a branch
  20506. + */
  20507. +
  20508. +#ifndef __AUFS_LOOP_H__
  20509. +#define __AUFS_LOOP_H__
  20510. +
  20511. +#ifdef __KERNEL__
  20512. +
  20513. +struct dentry;
  20514. +struct super_block;
  20515. +
  20516. +#ifdef CONFIG_AUFS_BDEV_LOOP
  20517. +/* drivers/block/loop.c */
  20518. +struct file *loop_backing_file(struct super_block *sb);
  20519. +
  20520. +/* loop.c */
  20521. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  20522. +int au_test_loopback_kthread(void);
  20523. +void au_warn_loopback(struct super_block *h_sb);
  20524. +
  20525. +int au_loopback_init(void);
  20526. +void au_loopback_fin(void);
  20527. +#else
  20528. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  20529. + struct dentry *h_adding)
  20530. +AuStubInt0(au_test_loopback_kthread, void)
  20531. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  20532. +
  20533. +AuStubInt0(au_loopback_init, void)
  20534. +AuStubVoid(au_loopback_fin, void)
  20535. +#endif /* BLK_DEV_LOOP */
  20536. +
  20537. +#endif /* __KERNEL__ */
  20538. +#endif /* __AUFS_LOOP_H__ */
  20539. diff -Nur linux-4.1.10.orig/fs/aufs/magic.mk linux-4.1.10/fs/aufs/magic.mk
  20540. --- linux-4.1.10.orig/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
  20541. +++ linux-4.1.10/fs/aufs/magic.mk 2015-10-22 21:35:53.000000000 +0200
  20542. @@ -0,0 +1,30 @@
  20543. +
  20544. +# defined in ${srctree}/fs/fuse/inode.c
  20545. +# tristate
  20546. +ifdef CONFIG_FUSE_FS
  20547. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  20548. +endif
  20549. +
  20550. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  20551. +# tristate
  20552. +ifdef CONFIG_XFS_FS
  20553. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  20554. +endif
  20555. +
  20556. +# defined in ${srctree}/fs/configfs/mount.c
  20557. +# tristate
  20558. +ifdef CONFIG_CONFIGFS_FS
  20559. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  20560. +endif
  20561. +
  20562. +# defined in ${srctree}/fs/ubifs/ubifs.h
  20563. +# tristate
  20564. +ifdef CONFIG_UBIFS_FS
  20565. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  20566. +endif
  20567. +
  20568. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  20569. +# tristate
  20570. +ifdef CONFIG_HFSPLUS_FS
  20571. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  20572. +endif
  20573. diff -Nur linux-4.1.10.orig/fs/aufs/Makefile linux-4.1.10/fs/aufs/Makefile
  20574. --- linux-4.1.10.orig/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
  20575. +++ linux-4.1.10/fs/aufs/Makefile 2015-10-22 21:35:53.000000000 +0200
  20576. @@ -0,0 +1,44 @@
  20577. +
  20578. +include ${src}/magic.mk
  20579. +ifeq (${CONFIG_AUFS_FS},m)
  20580. +include ${src}/conf.mk
  20581. +endif
  20582. +-include ${src}/priv_def.mk
  20583. +
  20584. +# cf. include/linux/kernel.h
  20585. +# enable pr_debug
  20586. +ccflags-y += -DDEBUG
  20587. +# sparse requires the full pathname
  20588. +ifdef M
  20589. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  20590. +else
  20591. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  20592. +endif
  20593. +
  20594. +obj-$(CONFIG_AUFS_FS) += aufs.o
  20595. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  20596. + wkq.o vfsub.o dcsub.o \
  20597. + cpup.o whout.o wbr_policy.o \
  20598. + dinfo.o dentry.o \
  20599. + dynop.o \
  20600. + finfo.o file.o f_op.o \
  20601. + dir.o vdir.o \
  20602. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  20603. + mvdown.o ioctl.o
  20604. +
  20605. +# all are boolean
  20606. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  20607. +aufs-$(CONFIG_SYSFS) += sysfs.o
  20608. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  20609. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  20610. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  20611. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  20612. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  20613. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  20614. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  20615. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  20616. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  20617. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  20618. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  20619. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  20620. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  20621. diff -Nur linux-4.1.10.orig/fs/aufs/module.c linux-4.1.10/fs/aufs/module.c
  20622. --- linux-4.1.10.orig/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
  20623. +++ linux-4.1.10/fs/aufs/module.c 2015-10-22 21:35:53.000000000 +0200
  20624. @@ -0,0 +1,218 @@
  20625. +/*
  20626. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  20627. + *
  20628. + * This program, aufs is free software; you can redistribute it and/or modify
  20629. + * it under the terms of the GNU General Public License as published by
  20630. + * the Free Software Foundation; either version 2 of the License, or
  20631. + * (at your option) any later version.
  20632. + *
  20633. + * This program is distributed in the hope that it will be useful,
  20634. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20635. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20636. + * GNU General Public License for more details.
  20637. + *
  20638. + * You should have received a copy of the GNU General Public License
  20639. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20640. + */
  20641. +
  20642. +/*
  20643. + * module global variables and operations
  20644. + */
  20645. +
  20646. +#include <linux/module.h>
  20647. +#include <linux/seq_file.h>
  20648. +#include "aufs.h"
  20649. +
  20650. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
  20651. +{
  20652. + if (new_sz <= nused)
  20653. + return p;
  20654. +
  20655. + p = krealloc(p, new_sz, gfp);
  20656. + if (p)
  20657. + memset(p + nused, 0, new_sz - nused);
  20658. + return p;
  20659. +}
  20660. +
  20661. +/* ---------------------------------------------------------------------- */
  20662. +
  20663. +/*
  20664. + * aufs caches
  20665. + */
  20666. +struct kmem_cache *au_cachep[AuCache_Last];
  20667. +static int __init au_cache_init(void)
  20668. +{
  20669. + au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  20670. + if (au_cachep[AuCache_DINFO])
  20671. + /* SLAB_DESTROY_BY_RCU */
  20672. + au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  20673. + au_icntnr_init_once);
  20674. + if (au_cachep[AuCache_ICNTNR])
  20675. + au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
  20676. + au_fi_init_once);
  20677. + if (au_cachep[AuCache_FINFO])
  20678. + au_cachep[AuCache_VDIR] = AuCache(au_vdir);
  20679. + if (au_cachep[AuCache_VDIR])
  20680. + au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  20681. + if (au_cachep[AuCache_DEHSTR])
  20682. + return 0;
  20683. +
  20684. + return -ENOMEM;
  20685. +}
  20686. +
  20687. +static void au_cache_fin(void)
  20688. +{
  20689. + int i;
  20690. +
  20691. + /*
  20692. + * Make sure all delayed rcu free inodes are flushed before we
  20693. + * destroy cache.
  20694. + */
  20695. + rcu_barrier();
  20696. +
  20697. + /* excluding AuCache_HNOTIFY */
  20698. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  20699. + for (i = 0; i < AuCache_HNOTIFY; i++)
  20700. + if (au_cachep[i]) {
  20701. + kmem_cache_destroy(au_cachep[i]);
  20702. + au_cachep[i] = NULL;
  20703. + }
  20704. +}
  20705. +
  20706. +/* ---------------------------------------------------------------------- */
  20707. +
  20708. +int au_dir_roflags;
  20709. +
  20710. +#ifdef CONFIG_AUFS_SBILIST
  20711. +/*
  20712. + * iterate_supers_type() doesn't protect us from
  20713. + * remounting (branch management)
  20714. + */
  20715. +struct au_splhead au_sbilist;
  20716. +#endif
  20717. +
  20718. +struct lock_class_key au_lc_key[AuLcKey_Last];
  20719. +
  20720. +/*
  20721. + * functions for module interface.
  20722. + */
  20723. +MODULE_LICENSE("GPL");
  20724. +/* MODULE_LICENSE("GPL v2"); */
  20725. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  20726. +MODULE_DESCRIPTION(AUFS_NAME
  20727. + " -- Advanced multi layered unification filesystem");
  20728. +MODULE_VERSION(AUFS_VERSION);
  20729. +MODULE_ALIAS_FS(AUFS_NAME);
  20730. +
  20731. +/* this module parameter has no meaning when SYSFS is disabled */
  20732. +int sysaufs_brs = 1;
  20733. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  20734. +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
  20735. +
  20736. +/* this module parameter has no meaning when USER_NS is disabled */
  20737. +static bool au_userns;
  20738. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  20739. +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
  20740. +
  20741. +/* ---------------------------------------------------------------------- */
  20742. +
  20743. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  20744. +
  20745. +int au_seq_path(struct seq_file *seq, struct path *path)
  20746. +{
  20747. + int err;
  20748. +
  20749. + err = seq_path(seq, path, au_esc_chars);
  20750. + if (err > 0)
  20751. + err = 0;
  20752. + else if (err < 0)
  20753. + err = -ENOMEM;
  20754. +
  20755. + return err;
  20756. +}
  20757. +
  20758. +/* ---------------------------------------------------------------------- */
  20759. +
  20760. +static int __init aufs_init(void)
  20761. +{
  20762. + int err, i;
  20763. + char *p;
  20764. +
  20765. + p = au_esc_chars;
  20766. + for (i = 1; i <= ' '; i++)
  20767. + *p++ = i;
  20768. + *p++ = '\\';
  20769. + *p++ = '\x7f';
  20770. + *p = 0;
  20771. +
  20772. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  20773. +
  20774. + au_sbilist_init();
  20775. + sysaufs_brs_init();
  20776. + au_debug_init();
  20777. + au_dy_init();
  20778. + err = sysaufs_init();
  20779. + if (unlikely(err))
  20780. + goto out;
  20781. + err = au_procfs_init();
  20782. + if (unlikely(err))
  20783. + goto out_sysaufs;
  20784. + err = au_wkq_init();
  20785. + if (unlikely(err))
  20786. + goto out_procfs;
  20787. + err = au_loopback_init();
  20788. + if (unlikely(err))
  20789. + goto out_wkq;
  20790. + err = au_hnotify_init();
  20791. + if (unlikely(err))
  20792. + goto out_loopback;
  20793. + err = au_sysrq_init();
  20794. + if (unlikely(err))
  20795. + goto out_hin;
  20796. + err = au_cache_init();
  20797. + if (unlikely(err))
  20798. + goto out_sysrq;
  20799. +
  20800. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  20801. + err = register_filesystem(&aufs_fs_type);
  20802. + if (unlikely(err))
  20803. + goto out_cache;
  20804. +
  20805. + /* since we define pr_fmt, call printk directly */
  20806. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  20807. + goto out; /* success */
  20808. +
  20809. +out_cache:
  20810. + au_cache_fin();
  20811. +out_sysrq:
  20812. + au_sysrq_fin();
  20813. +out_hin:
  20814. + au_hnotify_fin();
  20815. +out_loopback:
  20816. + au_loopback_fin();
  20817. +out_wkq:
  20818. + au_wkq_fin();
  20819. +out_procfs:
  20820. + au_procfs_fin();
  20821. +out_sysaufs:
  20822. + sysaufs_fin();
  20823. + au_dy_fin();
  20824. +out:
  20825. + return err;
  20826. +}
  20827. +
  20828. +static void __exit aufs_exit(void)
  20829. +{
  20830. + unregister_filesystem(&aufs_fs_type);
  20831. + au_cache_fin();
  20832. + au_sysrq_fin();
  20833. + au_hnotify_fin();
  20834. + au_loopback_fin();
  20835. + au_wkq_fin();
  20836. + au_procfs_fin();
  20837. + sysaufs_fin();
  20838. + au_dy_fin();
  20839. +}
  20840. +
  20841. +module_init(aufs_init);
  20842. +module_exit(aufs_exit);
  20843. diff -Nur linux-4.1.10.orig/fs/aufs/module.h linux-4.1.10/fs/aufs/module.h
  20844. --- linux-4.1.10.orig/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
  20845. +++ linux-4.1.10/fs/aufs/module.h 2015-10-22 21:35:53.000000000 +0200
  20846. @@ -0,0 +1,104 @@
  20847. +/*
  20848. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  20849. + *
  20850. + * This program, aufs is free software; you can redistribute it and/or modify
  20851. + * it under the terms of the GNU General Public License as published by
  20852. + * the Free Software Foundation; either version 2 of the License, or
  20853. + * (at your option) any later version.
  20854. + *
  20855. + * This program is distributed in the hope that it will be useful,
  20856. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20857. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20858. + * GNU General Public License for more details.
  20859. + *
  20860. + * You should have received a copy of the GNU General Public License
  20861. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20862. + */
  20863. +
  20864. +/*
  20865. + * module initialization and module-global
  20866. + */
  20867. +
  20868. +#ifndef __AUFS_MODULE_H__
  20869. +#define __AUFS_MODULE_H__
  20870. +
  20871. +#ifdef __KERNEL__
  20872. +
  20873. +#include <linux/slab.h>
  20874. +
  20875. +struct path;
  20876. +struct seq_file;
  20877. +
  20878. +/* module parameters */
  20879. +extern int sysaufs_brs;
  20880. +
  20881. +/* ---------------------------------------------------------------------- */
  20882. +
  20883. +extern int au_dir_roflags;
  20884. +
  20885. +enum {
  20886. + AuLcNonDir_FIINFO,
  20887. + AuLcNonDir_DIINFO,
  20888. + AuLcNonDir_IIINFO,
  20889. +
  20890. + AuLcDir_FIINFO,
  20891. + AuLcDir_DIINFO,
  20892. + AuLcDir_IIINFO,
  20893. +
  20894. + AuLcSymlink_DIINFO,
  20895. + AuLcSymlink_IIINFO,
  20896. +
  20897. + AuLcKey_Last
  20898. +};
  20899. +extern struct lock_class_key au_lc_key[AuLcKey_Last];
  20900. +
  20901. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
  20902. +int au_seq_path(struct seq_file *seq, struct path *path);
  20903. +
  20904. +#ifdef CONFIG_PROC_FS
  20905. +/* procfs.c */
  20906. +int __init au_procfs_init(void);
  20907. +void au_procfs_fin(void);
  20908. +#else
  20909. +AuStubInt0(au_procfs_init, void);
  20910. +AuStubVoid(au_procfs_fin, void);
  20911. +#endif
  20912. +
  20913. +/* ---------------------------------------------------------------------- */
  20914. +
  20915. +/* kmem cache */
  20916. +enum {
  20917. + AuCache_DINFO,
  20918. + AuCache_ICNTNR,
  20919. + AuCache_FINFO,
  20920. + AuCache_VDIR,
  20921. + AuCache_DEHSTR,
  20922. + AuCache_HNOTIFY, /* must be last */
  20923. + AuCache_Last
  20924. +};
  20925. +
  20926. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  20927. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  20928. +#define AuCacheCtor(type, ctor) \
  20929. + kmem_cache_create(#type, sizeof(struct type), \
  20930. + __alignof__(struct type), AuCacheFlags, ctor)
  20931. +
  20932. +extern struct kmem_cache *au_cachep[];
  20933. +
  20934. +#define AuCacheFuncs(name, index) \
  20935. +static inline struct au_##name *au_cache_alloc_##name(void) \
  20936. +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
  20937. +static inline void au_cache_free_##name(struct au_##name *p) \
  20938. +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
  20939. +
  20940. +AuCacheFuncs(dinfo, DINFO);
  20941. +AuCacheFuncs(icntnr, ICNTNR);
  20942. +AuCacheFuncs(finfo, FINFO);
  20943. +AuCacheFuncs(vdir, VDIR);
  20944. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  20945. +#ifdef CONFIG_AUFS_HNOTIFY
  20946. +AuCacheFuncs(hnotify, HNOTIFY);
  20947. +#endif
  20948. +
  20949. +#endif /* __KERNEL__ */
  20950. +#endif /* __AUFS_MODULE_H__ */
  20951. diff -Nur linux-4.1.10.orig/fs/aufs/mvdown.c linux-4.1.10/fs/aufs/mvdown.c
  20952. --- linux-4.1.10.orig/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
  20953. +++ linux-4.1.10/fs/aufs/mvdown.c 2015-10-22 21:35:53.000000000 +0200
  20954. @@ -0,0 +1,694 @@
  20955. +/*
  20956. + * Copyright (C) 2011-2015 Junjiro R. Okajima
  20957. + *
  20958. + * This program, aufs is free software; you can redistribute it and/or modify
  20959. + * it under the terms of the GNU General Public License as published by
  20960. + * the Free Software Foundation; either version 2 of the License, or
  20961. + * (at your option) any later version.
  20962. + *
  20963. + * This program is distributed in the hope that it will be useful,
  20964. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20965. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20966. + * GNU General Public License for more details.
  20967. + *
  20968. + * You should have received a copy of the GNU General Public License
  20969. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20970. + */
  20971. +
  20972. +/*
  20973. + * move-down, opposite of copy-up
  20974. + */
  20975. +
  20976. +#include "aufs.h"
  20977. +
  20978. +struct au_mvd_args {
  20979. + struct {
  20980. + struct super_block *h_sb;
  20981. + struct dentry *h_parent;
  20982. + struct au_hinode *hdir;
  20983. + struct inode *h_dir, *h_inode;
  20984. + struct au_pin pin;
  20985. + } info[AUFS_MVDOWN_NARRAY];
  20986. +
  20987. + struct aufs_mvdown mvdown;
  20988. + struct dentry *dentry, *parent;
  20989. + struct inode *inode, *dir;
  20990. + struct super_block *sb;
  20991. + aufs_bindex_t bopq, bwh, bfound;
  20992. + unsigned char rename_lock;
  20993. +};
  20994. +
  20995. +#define mvd_errno mvdown.au_errno
  20996. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  20997. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  20998. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  20999. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  21000. +
  21001. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  21002. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  21003. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  21004. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  21005. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  21006. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  21007. +
  21008. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  21009. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  21010. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  21011. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  21012. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  21013. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  21014. +
  21015. +#define AU_MVD_PR(flag, ...) do { \
  21016. + if (flag) \
  21017. + pr_err(__VA_ARGS__); \
  21018. + } while (0)
  21019. +
  21020. +static int find_lower_writable(struct au_mvd_args *a)
  21021. +{
  21022. + struct super_block *sb;
  21023. + aufs_bindex_t bindex, bend;
  21024. + struct au_branch *br;
  21025. +
  21026. + sb = a->sb;
  21027. + bindex = a->mvd_bsrc;
  21028. + bend = au_sbend(sb);
  21029. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  21030. + for (bindex++; bindex <= bend; bindex++) {
  21031. + br = au_sbr(sb, bindex);
  21032. + if (au_br_fhsm(br->br_perm)
  21033. + && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
  21034. + return bindex;
  21035. + }
  21036. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  21037. + for (bindex++; bindex <= bend; bindex++) {
  21038. + br = au_sbr(sb, bindex);
  21039. + if (!au_br_rdonly(br))
  21040. + return bindex;
  21041. + }
  21042. + else
  21043. + for (bindex++; bindex <= bend; bindex++) {
  21044. + br = au_sbr(sb, bindex);
  21045. + if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
  21046. + if (au_br_rdonly(br))
  21047. + a->mvdown.flags
  21048. + |= AUFS_MVDOWN_ROLOWER_R;
  21049. + return bindex;
  21050. + }
  21051. + }
  21052. +
  21053. + return -1;
  21054. +}
  21055. +
  21056. +/* make the parent dir on bdst */
  21057. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  21058. +{
  21059. + int err;
  21060. +
  21061. + err = 0;
  21062. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  21063. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  21064. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  21065. + a->mvd_h_dst_parent = NULL;
  21066. + if (au_dbend(a->parent) >= a->mvd_bdst)
  21067. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  21068. + if (!a->mvd_h_dst_parent) {
  21069. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  21070. + if (unlikely(err)) {
  21071. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  21072. + goto out;
  21073. + }
  21074. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  21075. + }
  21076. +
  21077. +out:
  21078. + AuTraceErr(err);
  21079. + return err;
  21080. +}
  21081. +
  21082. +/* lock them all */
  21083. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  21084. +{
  21085. + int err;
  21086. + struct dentry *h_trap;
  21087. +
  21088. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  21089. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  21090. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  21091. + au_opt_udba(a->sb),
  21092. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21093. + AuTraceErr(err);
  21094. + if (unlikely(err)) {
  21095. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  21096. + goto out;
  21097. + }
  21098. +
  21099. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  21100. + a->rename_lock = 0;
  21101. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  21102. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  21103. + au_opt_udba(a->sb),
  21104. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21105. + err = au_do_pin(&a->mvd_pin_src);
  21106. + AuTraceErr(err);
  21107. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  21108. + if (unlikely(err)) {
  21109. + AU_MVD_PR(dmsg, "pin_src failed\n");
  21110. + goto out_dst;
  21111. + }
  21112. + goto out; /* success */
  21113. + }
  21114. +
  21115. + a->rename_lock = 1;
  21116. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  21117. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  21118. + au_opt_udba(a->sb),
  21119. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  21120. + AuTraceErr(err);
  21121. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  21122. + if (unlikely(err)) {
  21123. + AU_MVD_PR(dmsg, "pin_src failed\n");
  21124. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21125. + goto out_dst;
  21126. + }
  21127. + au_pin_hdir_unlock(&a->mvd_pin_src);
  21128. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21129. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21130. + if (h_trap) {
  21131. + err = (h_trap != a->mvd_h_src_parent);
  21132. + if (err)
  21133. + err = (h_trap != a->mvd_h_dst_parent);
  21134. + }
  21135. + BUG_ON(err); /* it should never happen */
  21136. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  21137. + err = -EBUSY;
  21138. + AuTraceErr(err);
  21139. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21140. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21141. + au_pin_hdir_lock(&a->mvd_pin_src);
  21142. + au_unpin(&a->mvd_pin_src);
  21143. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21144. + goto out_dst;
  21145. + }
  21146. + goto out; /* success */
  21147. +
  21148. +out_dst:
  21149. + au_unpin(&a->mvd_pin_dst);
  21150. +out:
  21151. + AuTraceErr(err);
  21152. + return err;
  21153. +}
  21154. +
  21155. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  21156. +{
  21157. + if (!a->rename_lock)
  21158. + au_unpin(&a->mvd_pin_src);
  21159. + else {
  21160. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  21161. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  21162. + au_pin_hdir_lock(&a->mvd_pin_src);
  21163. + au_unpin(&a->mvd_pin_src);
  21164. + au_pin_hdir_lock(&a->mvd_pin_dst);
  21165. + }
  21166. + au_unpin(&a->mvd_pin_dst);
  21167. +}
  21168. +
  21169. +/* copy-down the file */
  21170. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  21171. +{
  21172. + int err;
  21173. + struct au_cp_generic cpg = {
  21174. + .dentry = a->dentry,
  21175. + .bdst = a->mvd_bdst,
  21176. + .bsrc = a->mvd_bsrc,
  21177. + .len = -1,
  21178. + .pin = &a->mvd_pin_dst,
  21179. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  21180. + };
  21181. +
  21182. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  21183. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  21184. + au_fset_cpup(cpg.flags, OVERWRITE);
  21185. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  21186. + au_fset_cpup(cpg.flags, RWDST);
  21187. + err = au_sio_cpdown_simple(&cpg);
  21188. + if (unlikely(err))
  21189. + AU_MVD_PR(dmsg, "cpdown failed\n");
  21190. +
  21191. + AuTraceErr(err);
  21192. + return err;
  21193. +}
  21194. +
  21195. +/*
  21196. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  21197. + * were sleeping
  21198. + */
  21199. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  21200. +{
  21201. + int err;
  21202. + struct path h_path;
  21203. + struct au_branch *br;
  21204. + struct inode *delegated;
  21205. +
  21206. + br = au_sbr(a->sb, a->mvd_bdst);
  21207. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  21208. + err = PTR_ERR(h_path.dentry);
  21209. + if (IS_ERR(h_path.dentry)) {
  21210. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  21211. + goto out;
  21212. + }
  21213. +
  21214. + err = 0;
  21215. + if (d_is_positive(h_path.dentry)) {
  21216. + h_path.mnt = au_br_mnt(br);
  21217. + delegated = NULL;
  21218. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  21219. + &delegated, /*force*/0);
  21220. + if (unlikely(err == -EWOULDBLOCK)) {
  21221. + pr_warn("cannot retry for NFSv4 delegation"
  21222. + " for an internal unlink\n");
  21223. + iput(delegated);
  21224. + }
  21225. + if (unlikely(err))
  21226. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  21227. + }
  21228. + dput(h_path.dentry);
  21229. +
  21230. +out:
  21231. + AuTraceErr(err);
  21232. + return err;
  21233. +}
  21234. +
  21235. +/*
  21236. + * unlink the topmost h_dentry
  21237. + */
  21238. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  21239. +{
  21240. + int err;
  21241. + struct path h_path;
  21242. + struct inode *delegated;
  21243. +
  21244. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  21245. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  21246. + delegated = NULL;
  21247. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  21248. + if (unlikely(err == -EWOULDBLOCK)) {
  21249. + pr_warn("cannot retry for NFSv4 delegation"
  21250. + " for an internal unlink\n");
  21251. + iput(delegated);
  21252. + }
  21253. + if (unlikely(err))
  21254. + AU_MVD_PR(dmsg, "unlink failed\n");
  21255. +
  21256. + AuTraceErr(err);
  21257. + return err;
  21258. +}
  21259. +
  21260. +/* Since mvdown succeeded, we ignore an error of this function */
  21261. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  21262. +{
  21263. + int err;
  21264. + struct au_branch *br;
  21265. +
  21266. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  21267. + br = au_sbr(a->sb, a->mvd_bsrc);
  21268. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  21269. + if (!err) {
  21270. + br = au_sbr(a->sb, a->mvd_bdst);
  21271. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  21272. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  21273. + }
  21274. + if (!err)
  21275. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  21276. + else
  21277. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  21278. +}
  21279. +
  21280. +/*
  21281. + * copy-down the file and unlink the bsrc file.
  21282. + * - unlink the bdst whout if exist
  21283. + * - copy-down the file (with whtmp name and rename)
  21284. + * - unlink the bsrc file
  21285. + */
  21286. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  21287. +{
  21288. + int err;
  21289. +
  21290. + err = au_do_mkdir(dmsg, a);
  21291. + if (!err)
  21292. + err = au_do_lock(dmsg, a);
  21293. + if (unlikely(err))
  21294. + goto out;
  21295. +
  21296. + /*
  21297. + * do not revert the activities we made on bdst since they should be
  21298. + * harmless in aufs.
  21299. + */
  21300. +
  21301. + err = au_do_cpdown(dmsg, a);
  21302. + if (!err)
  21303. + err = au_do_unlink_wh(dmsg, a);
  21304. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  21305. + err = au_do_unlink(dmsg, a);
  21306. + if (unlikely(err))
  21307. + goto out_unlock;
  21308. +
  21309. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  21310. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  21311. + if (find_lower_writable(a) < 0)
  21312. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  21313. +
  21314. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  21315. + au_do_stfs(dmsg, a);
  21316. +
  21317. + /* maintain internal array */
  21318. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  21319. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  21320. + au_set_dbstart(a->dentry, a->mvd_bdst);
  21321. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  21322. + au_set_ibstart(a->inode, a->mvd_bdst);
  21323. + }
  21324. + if (au_dbend(a->dentry) < a->mvd_bdst)
  21325. + au_set_dbend(a->dentry, a->mvd_bdst);
  21326. + if (au_ibend(a->inode) < a->mvd_bdst)
  21327. + au_set_ibend(a->inode, a->mvd_bdst);
  21328. +
  21329. +out_unlock:
  21330. + au_do_unlock(dmsg, a);
  21331. +out:
  21332. + AuTraceErr(err);
  21333. + return err;
  21334. +}
  21335. +
  21336. +/* ---------------------------------------------------------------------- */
  21337. +
  21338. +/* make sure the file is idle */
  21339. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  21340. +{
  21341. + int err, plinked;
  21342. +
  21343. + err = 0;
  21344. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  21345. + if (au_dbstart(a->dentry) == a->mvd_bsrc
  21346. + && au_dcount(a->dentry) == 1
  21347. + && atomic_read(&a->inode->i_count) == 1
  21348. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  21349. + && (!plinked || !au_plink_test(a->inode))
  21350. + && a->inode->i_nlink == 1)
  21351. + goto out;
  21352. +
  21353. + err = -EBUSY;
  21354. + AU_MVD_PR(dmsg,
  21355. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  21356. + a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
  21357. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  21358. + a->mvd_h_src_inode->i_nlink,
  21359. + plinked, plinked ? au_plink_test(a->inode) : 0);
  21360. +
  21361. +out:
  21362. + AuTraceErr(err);
  21363. + return err;
  21364. +}
  21365. +
  21366. +/* make sure the parent dir is fine */
  21367. +static int au_mvd_args_parent(const unsigned char dmsg,
  21368. + struct au_mvd_args *a)
  21369. +{
  21370. + int err;
  21371. + aufs_bindex_t bindex;
  21372. +
  21373. + err = 0;
  21374. + if (unlikely(au_alive_dir(a->parent))) {
  21375. + err = -ENOENT;
  21376. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  21377. + goto out;
  21378. + }
  21379. +
  21380. + a->bopq = au_dbdiropq(a->parent);
  21381. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  21382. + AuDbg("b%d\n", bindex);
  21383. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  21384. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  21385. + err = -EINVAL;
  21386. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  21387. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  21388. + a->bopq, a->mvd_bdst);
  21389. + }
  21390. +
  21391. +out:
  21392. + AuTraceErr(err);
  21393. + return err;
  21394. +}
  21395. +
  21396. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  21397. + struct au_mvd_args *a)
  21398. +{
  21399. + int err;
  21400. + struct au_dinfo *dinfo, *tmp;
  21401. +
  21402. + /* lookup the next lower positive entry */
  21403. + err = -ENOMEM;
  21404. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  21405. + if (unlikely(!tmp))
  21406. + goto out;
  21407. +
  21408. + a->bfound = -1;
  21409. + a->bwh = -1;
  21410. + dinfo = au_di(a->dentry);
  21411. + au_di_cp(tmp, dinfo);
  21412. + au_di_swap(tmp, dinfo);
  21413. +
  21414. + /* returns the number of positive dentries */
  21415. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
  21416. + if (!err)
  21417. + a->bwh = au_dbwh(a->dentry);
  21418. + else if (err > 0)
  21419. + a->bfound = au_dbstart(a->dentry);
  21420. +
  21421. + au_di_swap(tmp, dinfo);
  21422. + au_rw_write_unlock(&tmp->di_rwsem);
  21423. + au_di_free(tmp);
  21424. + if (unlikely(err < 0))
  21425. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  21426. +
  21427. + /*
  21428. + * here, we have these cases.
  21429. + * bfound == -1
  21430. + * no positive dentry under bsrc. there are more sub-cases.
  21431. + * bwh < 0
  21432. + * there no whiteout, we can safely move-down.
  21433. + * bwh <= bsrc
  21434. + * impossible
  21435. + * bsrc < bwh && bwh < bdst
  21436. + * there is a whiteout on RO branch. cannot proceed.
  21437. + * bwh == bdst
  21438. + * there is a whiteout on the RW target branch. it should
  21439. + * be removed.
  21440. + * bdst < bwh
  21441. + * there is a whiteout somewhere unrelated branch.
  21442. + * -1 < bfound && bfound <= bsrc
  21443. + * impossible.
  21444. + * bfound < bdst
  21445. + * found, but it is on RO branch between bsrc and bdst. cannot
  21446. + * proceed.
  21447. + * bfound == bdst
  21448. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  21449. + * error.
  21450. + * bdst < bfound
  21451. + * found, after we create the file on bdst, it will be hidden.
  21452. + */
  21453. +
  21454. + AuDebugOn(a->bfound == -1
  21455. + && a->bwh != -1
  21456. + && a->bwh <= a->mvd_bsrc);
  21457. + AuDebugOn(-1 < a->bfound
  21458. + && a->bfound <= a->mvd_bsrc);
  21459. +
  21460. + err = -EINVAL;
  21461. + if (a->bfound == -1
  21462. + && a->mvd_bsrc < a->bwh
  21463. + && a->bwh != -1
  21464. + && a->bwh < a->mvd_bdst) {
  21465. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  21466. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  21467. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  21468. + goto out;
  21469. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  21470. + a->mvd_errno = EAU_MVDOWN_UPPER;
  21471. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  21472. + a->mvd_bdst, a->bfound);
  21473. + goto out;
  21474. + }
  21475. +
  21476. + err = 0; /* success */
  21477. +
  21478. +out:
  21479. + AuTraceErr(err);
  21480. + return err;
  21481. +}
  21482. +
  21483. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  21484. +{
  21485. + int err;
  21486. +
  21487. + err = 0;
  21488. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  21489. + && a->bfound == a->mvd_bdst)
  21490. + err = -EEXIST;
  21491. + AuTraceErr(err);
  21492. + return err;
  21493. +}
  21494. +
  21495. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  21496. +{
  21497. + int err;
  21498. + struct au_branch *br;
  21499. +
  21500. + err = -EISDIR;
  21501. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  21502. + goto out;
  21503. +
  21504. + err = -EINVAL;
  21505. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  21506. + a->mvd_bsrc = au_ibstart(a->inode);
  21507. + else {
  21508. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  21509. + if (unlikely(a->mvd_bsrc < 0
  21510. + || (a->mvd_bsrc < au_dbstart(a->dentry)
  21511. + || au_dbend(a->dentry) < a->mvd_bsrc
  21512. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  21513. + || (a->mvd_bsrc < au_ibstart(a->inode)
  21514. + || au_ibend(a->inode) < a->mvd_bsrc
  21515. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  21516. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  21517. + AU_MVD_PR(dmsg, "no upper\n");
  21518. + goto out;
  21519. + }
  21520. + }
  21521. + if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
  21522. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  21523. + AU_MVD_PR(dmsg, "on the bottom\n");
  21524. + goto out;
  21525. + }
  21526. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  21527. + br = au_sbr(a->sb, a->mvd_bsrc);
  21528. + err = au_br_rdonly(br);
  21529. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  21530. + if (unlikely(err))
  21531. + goto out;
  21532. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  21533. + || IS_APPEND(a->mvd_h_src_inode))) {
  21534. + if (err)
  21535. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  21536. + /* go on */
  21537. + } else
  21538. + goto out;
  21539. +
  21540. + err = -EINVAL;
  21541. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  21542. + a->mvd_bdst = find_lower_writable(a);
  21543. + if (unlikely(a->mvd_bdst < 0)) {
  21544. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  21545. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  21546. + goto out;
  21547. + }
  21548. + } else {
  21549. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  21550. + if (unlikely(a->mvd_bdst < 0
  21551. + || au_sbend(a->sb) < a->mvd_bdst)) {
  21552. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  21553. + AU_MVD_PR(dmsg, "no lower brid\n");
  21554. + goto out;
  21555. + }
  21556. + }
  21557. +
  21558. + err = au_mvd_args_busy(dmsg, a);
  21559. + if (!err)
  21560. + err = au_mvd_args_parent(dmsg, a);
  21561. + if (!err)
  21562. + err = au_mvd_args_intermediate(dmsg, a);
  21563. + if (!err)
  21564. + err = au_mvd_args_exist(dmsg, a);
  21565. + if (!err)
  21566. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  21567. +
  21568. +out:
  21569. + AuTraceErr(err);
  21570. + return err;
  21571. +}
  21572. +
  21573. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  21574. +{
  21575. + int err, e;
  21576. + unsigned char dmsg;
  21577. + struct au_mvd_args *args;
  21578. +
  21579. + err = -EPERM;
  21580. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  21581. + goto out;
  21582. +
  21583. + err = -ENOMEM;
  21584. + args = kmalloc(sizeof(*args), GFP_NOFS);
  21585. + if (unlikely(!args))
  21586. + goto out;
  21587. +
  21588. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  21589. + if (!err)
  21590. + err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
  21591. + if (unlikely(err)) {
  21592. + err = -EFAULT;
  21593. + AuTraceErr(err);
  21594. + goto out_free;
  21595. + }
  21596. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  21597. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  21598. + args->mvdown.au_errno = 0;
  21599. + args->dentry = dentry;
  21600. + args->inode = d_inode(dentry);
  21601. + args->sb = dentry->d_sb;
  21602. +
  21603. + err = -ENOENT;
  21604. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  21605. + args->parent = dget_parent(dentry);
  21606. + args->dir = d_inode(args->parent);
  21607. + mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
  21608. + dput(args->parent);
  21609. + if (unlikely(args->parent != dentry->d_parent)) {
  21610. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  21611. + goto out_dir;
  21612. + }
  21613. +
  21614. + mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
  21615. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
  21616. + if (unlikely(err))
  21617. + goto out_inode;
  21618. +
  21619. + di_write_lock_parent(args->parent);
  21620. + err = au_mvd_args(dmsg, args);
  21621. + if (unlikely(err))
  21622. + goto out_parent;
  21623. +
  21624. + err = au_do_mvdown(dmsg, args);
  21625. + if (unlikely(err))
  21626. + goto out_parent;
  21627. +
  21628. + au_cpup_attr_timesizes(args->dir);
  21629. + au_cpup_attr_timesizes(args->inode);
  21630. + au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
  21631. + /* au_digen_dec(dentry); */
  21632. +
  21633. +out_parent:
  21634. + di_write_unlock(args->parent);
  21635. + aufs_read_unlock(dentry, AuLock_DW);
  21636. +out_inode:
  21637. + mutex_unlock(&args->inode->i_mutex);
  21638. +out_dir:
  21639. + mutex_unlock(&args->dir->i_mutex);
  21640. +out_free:
  21641. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  21642. + if (unlikely(e))
  21643. + err = -EFAULT;
  21644. + kfree(args);
  21645. +out:
  21646. + AuTraceErr(err);
  21647. + return err;
  21648. +}
  21649. diff -Nur linux-4.1.10.orig/fs/aufs/opts.c linux-4.1.10/fs/aufs/opts.c
  21650. --- linux-4.1.10.orig/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
  21651. +++ linux-4.1.10/fs/aufs/opts.c 2015-10-22 21:35:53.000000000 +0200
  21652. @@ -0,0 +1,1835 @@
  21653. +/*
  21654. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  21655. + *
  21656. + * This program, aufs is free software; you can redistribute it and/or modify
  21657. + * it under the terms of the GNU General Public License as published by
  21658. + * the Free Software Foundation; either version 2 of the License, or
  21659. + * (at your option) any later version.
  21660. + *
  21661. + * This program is distributed in the hope that it will be useful,
  21662. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21663. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21664. + * GNU General Public License for more details.
  21665. + *
  21666. + * You should have received a copy of the GNU General Public License
  21667. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21668. + */
  21669. +
  21670. +/*
  21671. + * mount options/flags
  21672. + */
  21673. +
  21674. +#include <linux/namei.h>
  21675. +#include <linux/types.h> /* a distribution requires */
  21676. +#include <linux/parser.h>
  21677. +#include "aufs.h"
  21678. +
  21679. +/* ---------------------------------------------------------------------- */
  21680. +
  21681. +enum {
  21682. + Opt_br,
  21683. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  21684. + Opt_idel, Opt_imod,
  21685. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  21686. + Opt_rdblk_def, Opt_rdhash_def,
  21687. + Opt_xino, Opt_noxino,
  21688. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  21689. + Opt_trunc_xino_path, Opt_itrunc_xino,
  21690. + Opt_trunc_xib, Opt_notrunc_xib,
  21691. + Opt_shwh, Opt_noshwh,
  21692. + Opt_plink, Opt_noplink, Opt_list_plink,
  21693. + Opt_udba,
  21694. + Opt_dio, Opt_nodio,
  21695. + Opt_diropq_a, Opt_diropq_w,
  21696. + Opt_warn_perm, Opt_nowarn_perm,
  21697. + Opt_wbr_copyup, Opt_wbr_create,
  21698. + Opt_fhsm_sec,
  21699. + Opt_verbose, Opt_noverbose,
  21700. + Opt_sum, Opt_nosum, Opt_wsum,
  21701. + Opt_dirperm1, Opt_nodirperm1,
  21702. + Opt_acl, Opt_noacl,
  21703. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  21704. +};
  21705. +
  21706. +static match_table_t options = {
  21707. + {Opt_br, "br=%s"},
  21708. + {Opt_br, "br:%s"},
  21709. +
  21710. + {Opt_add, "add=%d:%s"},
  21711. + {Opt_add, "add:%d:%s"},
  21712. + {Opt_add, "ins=%d:%s"},
  21713. + {Opt_add, "ins:%d:%s"},
  21714. + {Opt_append, "append=%s"},
  21715. + {Opt_append, "append:%s"},
  21716. + {Opt_prepend, "prepend=%s"},
  21717. + {Opt_prepend, "prepend:%s"},
  21718. +
  21719. + {Opt_del, "del=%s"},
  21720. + {Opt_del, "del:%s"},
  21721. + /* {Opt_idel, "idel:%d"}, */
  21722. + {Opt_mod, "mod=%s"},
  21723. + {Opt_mod, "mod:%s"},
  21724. + /* {Opt_imod, "imod:%d:%s"}, */
  21725. +
  21726. + {Opt_dirwh, "dirwh=%d"},
  21727. +
  21728. + {Opt_xino, "xino=%s"},
  21729. + {Opt_noxino, "noxino"},
  21730. + {Opt_trunc_xino, "trunc_xino"},
  21731. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  21732. + {Opt_notrunc_xino, "notrunc_xino"},
  21733. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  21734. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  21735. + /* {Opt_zxino, "zxino=%s"}, */
  21736. + {Opt_trunc_xib, "trunc_xib"},
  21737. + {Opt_notrunc_xib, "notrunc_xib"},
  21738. +
  21739. +#ifdef CONFIG_PROC_FS
  21740. + {Opt_plink, "plink"},
  21741. +#else
  21742. + {Opt_ignore_silent, "plink"},
  21743. +#endif
  21744. +
  21745. + {Opt_noplink, "noplink"},
  21746. +
  21747. +#ifdef CONFIG_AUFS_DEBUG
  21748. + {Opt_list_plink, "list_plink"},
  21749. +#endif
  21750. +
  21751. + {Opt_udba, "udba=%s"},
  21752. +
  21753. + {Opt_dio, "dio"},
  21754. + {Opt_nodio, "nodio"},
  21755. +
  21756. +#ifdef CONFIG_AUFS_FHSM
  21757. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  21758. +#else
  21759. + {Opt_ignore_silent, "fhsm_sec=%d"},
  21760. +#endif
  21761. +
  21762. + {Opt_diropq_a, "diropq=always"},
  21763. + {Opt_diropq_a, "diropq=a"},
  21764. + {Opt_diropq_w, "diropq=whiteouted"},
  21765. + {Opt_diropq_w, "diropq=w"},
  21766. +
  21767. + {Opt_warn_perm, "warn_perm"},
  21768. + {Opt_nowarn_perm, "nowarn_perm"},
  21769. +
  21770. + /* keep them temporary */
  21771. + {Opt_ignore_silent, "nodlgt"},
  21772. + {Opt_ignore_silent, "clean_plink"},
  21773. +
  21774. +#ifdef CONFIG_AUFS_SHWH
  21775. + {Opt_shwh, "shwh"},
  21776. +#endif
  21777. + {Opt_noshwh, "noshwh"},
  21778. +
  21779. + {Opt_dirperm1, "dirperm1"},
  21780. + {Opt_nodirperm1, "nodirperm1"},
  21781. +
  21782. + {Opt_verbose, "verbose"},
  21783. + {Opt_verbose, "v"},
  21784. + {Opt_noverbose, "noverbose"},
  21785. + {Opt_noverbose, "quiet"},
  21786. + {Opt_noverbose, "q"},
  21787. + {Opt_noverbose, "silent"},
  21788. +
  21789. + {Opt_sum, "sum"},
  21790. + {Opt_nosum, "nosum"},
  21791. + {Opt_wsum, "wsum"},
  21792. +
  21793. + {Opt_rdcache, "rdcache=%d"},
  21794. + {Opt_rdblk, "rdblk=%d"},
  21795. + {Opt_rdblk_def, "rdblk=def"},
  21796. + {Opt_rdhash, "rdhash=%d"},
  21797. + {Opt_rdhash_def, "rdhash=def"},
  21798. +
  21799. + {Opt_wbr_create, "create=%s"},
  21800. + {Opt_wbr_create, "create_policy=%s"},
  21801. + {Opt_wbr_copyup, "cpup=%s"},
  21802. + {Opt_wbr_copyup, "copyup=%s"},
  21803. + {Opt_wbr_copyup, "copyup_policy=%s"},
  21804. +
  21805. + /* generic VFS flag */
  21806. +#ifdef CONFIG_FS_POSIX_ACL
  21807. + {Opt_acl, "acl"},
  21808. + {Opt_noacl, "noacl"},
  21809. +#else
  21810. + {Opt_ignore_silent, "acl"},
  21811. + {Opt_ignore_silent, "noacl"},
  21812. +#endif
  21813. +
  21814. + /* internal use for the scripts */
  21815. + {Opt_ignore_silent, "si=%s"},
  21816. +
  21817. + {Opt_br, "dirs=%s"},
  21818. + {Opt_ignore, "debug=%d"},
  21819. + {Opt_ignore, "delete=whiteout"},
  21820. + {Opt_ignore, "delete=all"},
  21821. + {Opt_ignore, "imap=%s"},
  21822. +
  21823. + /* temporary workaround, due to old mount(8)? */
  21824. + {Opt_ignore_silent, "relatime"},
  21825. +
  21826. + {Opt_err, NULL}
  21827. +};
  21828. +
  21829. +/* ---------------------------------------------------------------------- */
  21830. +
  21831. +static const char *au_parser_pattern(int val, match_table_t tbl)
  21832. +{
  21833. + struct match_token *p;
  21834. +
  21835. + p = tbl;
  21836. + while (p->pattern) {
  21837. + if (p->token == val)
  21838. + return p->pattern;
  21839. + p++;
  21840. + }
  21841. + BUG();
  21842. + return "??";
  21843. +}
  21844. +
  21845. +static const char *au_optstr(int *val, match_table_t tbl)
  21846. +{
  21847. + struct match_token *p;
  21848. + int v;
  21849. +
  21850. + v = *val;
  21851. + if (!v)
  21852. + goto out;
  21853. + p = tbl;
  21854. + while (p->pattern) {
  21855. + if (p->token
  21856. + && (v & p->token) == p->token) {
  21857. + *val &= ~p->token;
  21858. + return p->pattern;
  21859. + }
  21860. + p++;
  21861. + }
  21862. +
  21863. +out:
  21864. + return NULL;
  21865. +}
  21866. +
  21867. +/* ---------------------------------------------------------------------- */
  21868. +
  21869. +static match_table_t brperm = {
  21870. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  21871. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  21872. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  21873. + {0, NULL}
  21874. +};
  21875. +
  21876. +static match_table_t brattr = {
  21877. + /* general */
  21878. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  21879. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  21880. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  21881. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  21882. +#ifdef CONFIG_AUFS_FHSM
  21883. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  21884. +#endif
  21885. +#ifdef CONFIG_AUFS_XATTR
  21886. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  21887. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  21888. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  21889. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  21890. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  21891. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  21892. +#endif
  21893. +
  21894. + /* ro/rr branch */
  21895. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  21896. +
  21897. + /* rw branch */
  21898. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  21899. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  21900. +
  21901. + {0, NULL}
  21902. +};
  21903. +
  21904. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  21905. +{
  21906. + int attr, v;
  21907. + char *p;
  21908. +
  21909. + attr = 0;
  21910. + do {
  21911. + p = strchr(str, '+');
  21912. + if (p)
  21913. + *p = 0;
  21914. + v = match_token(str, table, args);
  21915. + if (v) {
  21916. + if (v & AuBrAttr_CMOO_Mask)
  21917. + attr &= ~AuBrAttr_CMOO_Mask;
  21918. + attr |= v;
  21919. + } else {
  21920. + if (p)
  21921. + *p = '+';
  21922. + pr_warn("ignored branch attribute %s\n", str);
  21923. + break;
  21924. + }
  21925. + if (p)
  21926. + str = p + 1;
  21927. + } while (p);
  21928. +
  21929. + return attr;
  21930. +}
  21931. +
  21932. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  21933. +{
  21934. + int sz;
  21935. + const char *p;
  21936. + char *q;
  21937. +
  21938. + q = str->a;
  21939. + *q = 0;
  21940. + p = au_optstr(&perm, brattr);
  21941. + if (p) {
  21942. + sz = strlen(p);
  21943. + memcpy(q, p, sz + 1);
  21944. + q += sz;
  21945. + } else
  21946. + goto out;
  21947. +
  21948. + do {
  21949. + p = au_optstr(&perm, brattr);
  21950. + if (p) {
  21951. + *q++ = '+';
  21952. + sz = strlen(p);
  21953. + memcpy(q, p, sz + 1);
  21954. + q += sz;
  21955. + }
  21956. + } while (p);
  21957. +
  21958. +out:
  21959. + return q - str->a;
  21960. +}
  21961. +
  21962. +static int noinline_for_stack br_perm_val(char *perm)
  21963. +{
  21964. + int val, bad, sz;
  21965. + char *p;
  21966. + substring_t args[MAX_OPT_ARGS];
  21967. + au_br_perm_str_t attr;
  21968. +
  21969. + p = strchr(perm, '+');
  21970. + if (p)
  21971. + *p = 0;
  21972. + val = match_token(perm, brperm, args);
  21973. + if (!val) {
  21974. + if (p)
  21975. + *p = '+';
  21976. + pr_warn("ignored branch permission %s\n", perm);
  21977. + val = AuBrPerm_RO;
  21978. + goto out;
  21979. + }
  21980. + if (!p)
  21981. + goto out;
  21982. +
  21983. + val |= br_attr_val(p + 1, brattr, args);
  21984. +
  21985. + bad = 0;
  21986. + switch (val & AuBrPerm_Mask) {
  21987. + case AuBrPerm_RO:
  21988. + case AuBrPerm_RR:
  21989. + bad = val & AuBrWAttr_Mask;
  21990. + val &= ~AuBrWAttr_Mask;
  21991. + break;
  21992. + case AuBrPerm_RW:
  21993. + bad = val & AuBrRAttr_Mask;
  21994. + val &= ~AuBrRAttr_Mask;
  21995. + break;
  21996. + }
  21997. +
  21998. + /*
  21999. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  22000. + * does not treat it as an error, just warning.
  22001. + * this is a tiny guard for the user operation.
  22002. + */
  22003. + if (val & AuBrAttr_UNPIN) {
  22004. + bad |= AuBrAttr_UNPIN;
  22005. + val &= ~AuBrAttr_UNPIN;
  22006. + }
  22007. +
  22008. + if (unlikely(bad)) {
  22009. + sz = au_do_optstr_br_attr(&attr, bad);
  22010. + AuDebugOn(!sz);
  22011. + pr_warn("ignored branch attribute %s\n", attr.a);
  22012. + }
  22013. +
  22014. +out:
  22015. + return val;
  22016. +}
  22017. +
  22018. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  22019. +{
  22020. + au_br_perm_str_t attr;
  22021. + const char *p;
  22022. + char *q;
  22023. + int sz;
  22024. +
  22025. + q = str->a;
  22026. + p = au_optstr(&perm, brperm);
  22027. + AuDebugOn(!p || !*p);
  22028. + sz = strlen(p);
  22029. + memcpy(q, p, sz + 1);
  22030. + q += sz;
  22031. +
  22032. + sz = au_do_optstr_br_attr(&attr, perm);
  22033. + if (sz) {
  22034. + *q++ = '+';
  22035. + memcpy(q, attr.a, sz + 1);
  22036. + }
  22037. +
  22038. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  22039. +}
  22040. +
  22041. +/* ---------------------------------------------------------------------- */
  22042. +
  22043. +static match_table_t udbalevel = {
  22044. + {AuOpt_UDBA_REVAL, "reval"},
  22045. + {AuOpt_UDBA_NONE, "none"},
  22046. +#ifdef CONFIG_AUFS_HNOTIFY
  22047. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  22048. +#ifdef CONFIG_AUFS_HFSNOTIFY
  22049. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  22050. +#endif
  22051. +#endif
  22052. + {-1, NULL}
  22053. +};
  22054. +
  22055. +static int noinline_for_stack udba_val(char *str)
  22056. +{
  22057. + substring_t args[MAX_OPT_ARGS];
  22058. +
  22059. + return match_token(str, udbalevel, args);
  22060. +}
  22061. +
  22062. +const char *au_optstr_udba(int udba)
  22063. +{
  22064. + return au_parser_pattern(udba, udbalevel);
  22065. +}
  22066. +
  22067. +/* ---------------------------------------------------------------------- */
  22068. +
  22069. +static match_table_t au_wbr_create_policy = {
  22070. + {AuWbrCreate_TDP, "tdp"},
  22071. + {AuWbrCreate_TDP, "top-down-parent"},
  22072. + {AuWbrCreate_RR, "rr"},
  22073. + {AuWbrCreate_RR, "round-robin"},
  22074. + {AuWbrCreate_MFS, "mfs"},
  22075. + {AuWbrCreate_MFS, "most-free-space"},
  22076. + {AuWbrCreate_MFSV, "mfs:%d"},
  22077. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  22078. +
  22079. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  22080. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  22081. + {AuWbrCreate_PMFS, "pmfs"},
  22082. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  22083. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  22084. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  22085. +
  22086. + {-1, NULL}
  22087. +};
  22088. +
  22089. +/*
  22090. + * cf. linux/lib/parser.c and cmdline.c
  22091. + * gave up calling memparse() since it uses simple_strtoull() instead of
  22092. + * kstrto...().
  22093. + */
  22094. +static int noinline_for_stack
  22095. +au_match_ull(substring_t *s, unsigned long long *result)
  22096. +{
  22097. + int err;
  22098. + unsigned int len;
  22099. + char a[32];
  22100. +
  22101. + err = -ERANGE;
  22102. + len = s->to - s->from;
  22103. + if (len + 1 <= sizeof(a)) {
  22104. + memcpy(a, s->from, len);
  22105. + a[len] = '\0';
  22106. + err = kstrtoull(a, 0, result);
  22107. + }
  22108. + return err;
  22109. +}
  22110. +
  22111. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  22112. + struct au_opt_wbr_create *create)
  22113. +{
  22114. + int err;
  22115. + unsigned long long ull;
  22116. +
  22117. + err = 0;
  22118. + if (!au_match_ull(arg, &ull))
  22119. + create->mfsrr_watermark = ull;
  22120. + else {
  22121. + pr_err("bad integer in %s\n", str);
  22122. + err = -EINVAL;
  22123. + }
  22124. +
  22125. + return err;
  22126. +}
  22127. +
  22128. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  22129. + struct au_opt_wbr_create *create)
  22130. +{
  22131. + int n, err;
  22132. +
  22133. + err = 0;
  22134. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  22135. + create->mfs_second = n;
  22136. + else {
  22137. + pr_err("bad integer in %s\n", str);
  22138. + err = -EINVAL;
  22139. + }
  22140. +
  22141. + return err;
  22142. +}
  22143. +
  22144. +static int noinline_for_stack
  22145. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  22146. +{
  22147. + int err, e;
  22148. + substring_t args[MAX_OPT_ARGS];
  22149. +
  22150. + err = match_token(str, au_wbr_create_policy, args);
  22151. + create->wbr_create = err;
  22152. + switch (err) {
  22153. + case AuWbrCreate_MFSRRV:
  22154. + case AuWbrCreate_PMFSRRV:
  22155. + e = au_wbr_mfs_wmark(&args[0], str, create);
  22156. + if (!e)
  22157. + e = au_wbr_mfs_sec(&args[1], str, create);
  22158. + if (unlikely(e))
  22159. + err = e;
  22160. + break;
  22161. + case AuWbrCreate_MFSRR:
  22162. + case AuWbrCreate_PMFSRR:
  22163. + e = au_wbr_mfs_wmark(&args[0], str, create);
  22164. + if (unlikely(e)) {
  22165. + err = e;
  22166. + break;
  22167. + }
  22168. + /*FALLTHROUGH*/
  22169. + case AuWbrCreate_MFS:
  22170. + case AuWbrCreate_PMFS:
  22171. + create->mfs_second = AUFS_MFS_DEF_SEC;
  22172. + break;
  22173. + case AuWbrCreate_MFSV:
  22174. + case AuWbrCreate_PMFSV:
  22175. + e = au_wbr_mfs_sec(&args[0], str, create);
  22176. + if (unlikely(e))
  22177. + err = e;
  22178. + break;
  22179. + }
  22180. +
  22181. + return err;
  22182. +}
  22183. +
  22184. +const char *au_optstr_wbr_create(int wbr_create)
  22185. +{
  22186. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  22187. +}
  22188. +
  22189. +static match_table_t au_wbr_copyup_policy = {
  22190. + {AuWbrCopyup_TDP, "tdp"},
  22191. + {AuWbrCopyup_TDP, "top-down-parent"},
  22192. + {AuWbrCopyup_BUP, "bup"},
  22193. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  22194. + {AuWbrCopyup_BU, "bu"},
  22195. + {AuWbrCopyup_BU, "bottom-up"},
  22196. + {-1, NULL}
  22197. +};
  22198. +
  22199. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  22200. +{
  22201. + substring_t args[MAX_OPT_ARGS];
  22202. +
  22203. + return match_token(str, au_wbr_copyup_policy, args);
  22204. +}
  22205. +
  22206. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  22207. +{
  22208. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  22209. +}
  22210. +
  22211. +/* ---------------------------------------------------------------------- */
  22212. +
  22213. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  22214. +
  22215. +static void dump_opts(struct au_opts *opts)
  22216. +{
  22217. +#ifdef CONFIG_AUFS_DEBUG
  22218. + /* reduce stack space */
  22219. + union {
  22220. + struct au_opt_add *add;
  22221. + struct au_opt_del *del;
  22222. + struct au_opt_mod *mod;
  22223. + struct au_opt_xino *xino;
  22224. + struct au_opt_xino_itrunc *xino_itrunc;
  22225. + struct au_opt_wbr_create *create;
  22226. + } u;
  22227. + struct au_opt *opt;
  22228. +
  22229. + opt = opts->opt;
  22230. + while (opt->type != Opt_tail) {
  22231. + switch (opt->type) {
  22232. + case Opt_add:
  22233. + u.add = &opt->add;
  22234. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  22235. + u.add->bindex, u.add->pathname, u.add->perm,
  22236. + u.add->path.dentry);
  22237. + break;
  22238. + case Opt_del:
  22239. + case Opt_idel:
  22240. + u.del = &opt->del;
  22241. + AuDbg("del {%s, %p}\n",
  22242. + u.del->pathname, u.del->h_path.dentry);
  22243. + break;
  22244. + case Opt_mod:
  22245. + case Opt_imod:
  22246. + u.mod = &opt->mod;
  22247. + AuDbg("mod {%s, 0x%x, %p}\n",
  22248. + u.mod->path, u.mod->perm, u.mod->h_root);
  22249. + break;
  22250. + case Opt_append:
  22251. + u.add = &opt->add;
  22252. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  22253. + u.add->bindex, u.add->pathname, u.add->perm,
  22254. + u.add->path.dentry);
  22255. + break;
  22256. + case Opt_prepend:
  22257. + u.add = &opt->add;
  22258. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  22259. + u.add->bindex, u.add->pathname, u.add->perm,
  22260. + u.add->path.dentry);
  22261. + break;
  22262. + case Opt_dirwh:
  22263. + AuDbg("dirwh %d\n", opt->dirwh);
  22264. + break;
  22265. + case Opt_rdcache:
  22266. + AuDbg("rdcache %d\n", opt->rdcache);
  22267. + break;
  22268. + case Opt_rdblk:
  22269. + AuDbg("rdblk %u\n", opt->rdblk);
  22270. + break;
  22271. + case Opt_rdblk_def:
  22272. + AuDbg("rdblk_def\n");
  22273. + break;
  22274. + case Opt_rdhash:
  22275. + AuDbg("rdhash %u\n", opt->rdhash);
  22276. + break;
  22277. + case Opt_rdhash_def:
  22278. + AuDbg("rdhash_def\n");
  22279. + break;
  22280. + case Opt_xino:
  22281. + u.xino = &opt->xino;
  22282. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  22283. + break;
  22284. + case Opt_trunc_xino:
  22285. + AuLabel(trunc_xino);
  22286. + break;
  22287. + case Opt_notrunc_xino:
  22288. + AuLabel(notrunc_xino);
  22289. + break;
  22290. + case Opt_trunc_xino_path:
  22291. + case Opt_itrunc_xino:
  22292. + u.xino_itrunc = &opt->xino_itrunc;
  22293. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  22294. + break;
  22295. + case Opt_noxino:
  22296. + AuLabel(noxino);
  22297. + break;
  22298. + case Opt_trunc_xib:
  22299. + AuLabel(trunc_xib);
  22300. + break;
  22301. + case Opt_notrunc_xib:
  22302. + AuLabel(notrunc_xib);
  22303. + break;
  22304. + case Opt_shwh:
  22305. + AuLabel(shwh);
  22306. + break;
  22307. + case Opt_noshwh:
  22308. + AuLabel(noshwh);
  22309. + break;
  22310. + case Opt_dirperm1:
  22311. + AuLabel(dirperm1);
  22312. + break;
  22313. + case Opt_nodirperm1:
  22314. + AuLabel(nodirperm1);
  22315. + break;
  22316. + case Opt_plink:
  22317. + AuLabel(plink);
  22318. + break;
  22319. + case Opt_noplink:
  22320. + AuLabel(noplink);
  22321. + break;
  22322. + case Opt_list_plink:
  22323. + AuLabel(list_plink);
  22324. + break;
  22325. + case Opt_udba:
  22326. + AuDbg("udba %d, %s\n",
  22327. + opt->udba, au_optstr_udba(opt->udba));
  22328. + break;
  22329. + case Opt_dio:
  22330. + AuLabel(dio);
  22331. + break;
  22332. + case Opt_nodio:
  22333. + AuLabel(nodio);
  22334. + break;
  22335. + case Opt_diropq_a:
  22336. + AuLabel(diropq_a);
  22337. + break;
  22338. + case Opt_diropq_w:
  22339. + AuLabel(diropq_w);
  22340. + break;
  22341. + case Opt_warn_perm:
  22342. + AuLabel(warn_perm);
  22343. + break;
  22344. + case Opt_nowarn_perm:
  22345. + AuLabel(nowarn_perm);
  22346. + break;
  22347. + case Opt_verbose:
  22348. + AuLabel(verbose);
  22349. + break;
  22350. + case Opt_noverbose:
  22351. + AuLabel(noverbose);
  22352. + break;
  22353. + case Opt_sum:
  22354. + AuLabel(sum);
  22355. + break;
  22356. + case Opt_nosum:
  22357. + AuLabel(nosum);
  22358. + break;
  22359. + case Opt_wsum:
  22360. + AuLabel(wsum);
  22361. + break;
  22362. + case Opt_wbr_create:
  22363. + u.create = &opt->wbr_create;
  22364. + AuDbg("create %d, %s\n", u.create->wbr_create,
  22365. + au_optstr_wbr_create(u.create->wbr_create));
  22366. + switch (u.create->wbr_create) {
  22367. + case AuWbrCreate_MFSV:
  22368. + case AuWbrCreate_PMFSV:
  22369. + AuDbg("%d sec\n", u.create->mfs_second);
  22370. + break;
  22371. + case AuWbrCreate_MFSRR:
  22372. + AuDbg("%llu watermark\n",
  22373. + u.create->mfsrr_watermark);
  22374. + break;
  22375. + case AuWbrCreate_MFSRRV:
  22376. + case AuWbrCreate_PMFSRRV:
  22377. + AuDbg("%llu watermark, %d sec\n",
  22378. + u.create->mfsrr_watermark,
  22379. + u.create->mfs_second);
  22380. + break;
  22381. + }
  22382. + break;
  22383. + case Opt_wbr_copyup:
  22384. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  22385. + au_optstr_wbr_copyup(opt->wbr_copyup));
  22386. + break;
  22387. + case Opt_fhsm_sec:
  22388. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  22389. + break;
  22390. + case Opt_acl:
  22391. + AuLabel(acl);
  22392. + break;
  22393. + case Opt_noacl:
  22394. + AuLabel(noacl);
  22395. + break;
  22396. + default:
  22397. + BUG();
  22398. + }
  22399. + opt++;
  22400. + }
  22401. +#endif
  22402. +}
  22403. +
  22404. +void au_opts_free(struct au_opts *opts)
  22405. +{
  22406. + struct au_opt *opt;
  22407. +
  22408. + opt = opts->opt;
  22409. + while (opt->type != Opt_tail) {
  22410. + switch (opt->type) {
  22411. + case Opt_add:
  22412. + case Opt_append:
  22413. + case Opt_prepend:
  22414. + path_put(&opt->add.path);
  22415. + break;
  22416. + case Opt_del:
  22417. + case Opt_idel:
  22418. + path_put(&opt->del.h_path);
  22419. + break;
  22420. + case Opt_mod:
  22421. + case Opt_imod:
  22422. + dput(opt->mod.h_root);
  22423. + break;
  22424. + case Opt_xino:
  22425. + fput(opt->xino.file);
  22426. + break;
  22427. + }
  22428. + opt++;
  22429. + }
  22430. +}
  22431. +
  22432. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  22433. + aufs_bindex_t bindex)
  22434. +{
  22435. + int err;
  22436. + struct au_opt_add *add = &opt->add;
  22437. + char *p;
  22438. +
  22439. + add->bindex = bindex;
  22440. + add->perm = AuBrPerm_RO;
  22441. + add->pathname = opt_str;
  22442. + p = strchr(opt_str, '=');
  22443. + if (p) {
  22444. + *p++ = 0;
  22445. + if (*p)
  22446. + add->perm = br_perm_val(p);
  22447. + }
  22448. +
  22449. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  22450. + if (!err) {
  22451. + if (!p) {
  22452. + add->perm = AuBrPerm_RO;
  22453. + if (au_test_fs_rr(add->path.dentry->d_sb))
  22454. + add->perm = AuBrPerm_RR;
  22455. + else if (!bindex && !(sb_flags & MS_RDONLY))
  22456. + add->perm = AuBrPerm_RW;
  22457. + }
  22458. + opt->type = Opt_add;
  22459. + goto out;
  22460. + }
  22461. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  22462. + err = -EINVAL;
  22463. +
  22464. +out:
  22465. + return err;
  22466. +}
  22467. +
  22468. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  22469. +{
  22470. + int err;
  22471. +
  22472. + del->pathname = args[0].from;
  22473. + AuDbg("del path %s\n", del->pathname);
  22474. +
  22475. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  22476. + if (unlikely(err))
  22477. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  22478. +
  22479. + return err;
  22480. +}
  22481. +
  22482. +#if 0 /* reserved for future use */
  22483. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  22484. + struct au_opt_del *del, substring_t args[])
  22485. +{
  22486. + int err;
  22487. + struct dentry *root;
  22488. +
  22489. + err = -EINVAL;
  22490. + root = sb->s_root;
  22491. + aufs_read_lock(root, AuLock_FLUSH);
  22492. + if (bindex < 0 || au_sbend(sb) < bindex) {
  22493. + pr_err("out of bounds, %d\n", bindex);
  22494. + goto out;
  22495. + }
  22496. +
  22497. + err = 0;
  22498. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  22499. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  22500. +
  22501. +out:
  22502. + aufs_read_unlock(root, !AuLock_IR);
  22503. + return err;
  22504. +}
  22505. +#endif
  22506. +
  22507. +static int noinline_for_stack
  22508. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  22509. +{
  22510. + int err;
  22511. + struct path path;
  22512. + char *p;
  22513. +
  22514. + err = -EINVAL;
  22515. + mod->path = args[0].from;
  22516. + p = strchr(mod->path, '=');
  22517. + if (unlikely(!p)) {
  22518. + pr_err("no permssion %s\n", args[0].from);
  22519. + goto out;
  22520. + }
  22521. +
  22522. + *p++ = 0;
  22523. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  22524. + if (unlikely(err)) {
  22525. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  22526. + goto out;
  22527. + }
  22528. +
  22529. + mod->perm = br_perm_val(p);
  22530. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  22531. + mod->h_root = dget(path.dentry);
  22532. + path_put(&path);
  22533. +
  22534. +out:
  22535. + return err;
  22536. +}
  22537. +
  22538. +#if 0 /* reserved for future use */
  22539. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  22540. + struct au_opt_mod *mod, substring_t args[])
  22541. +{
  22542. + int err;
  22543. + struct dentry *root;
  22544. +
  22545. + err = -EINVAL;
  22546. + root = sb->s_root;
  22547. + aufs_read_lock(root, AuLock_FLUSH);
  22548. + if (bindex < 0 || au_sbend(sb) < bindex) {
  22549. + pr_err("out of bounds, %d\n", bindex);
  22550. + goto out;
  22551. + }
  22552. +
  22553. + err = 0;
  22554. + mod->perm = br_perm_val(args[1].from);
  22555. + AuDbg("mod path %s, perm 0x%x, %s\n",
  22556. + mod->path, mod->perm, args[1].from);
  22557. + mod->h_root = dget(au_h_dptr(root, bindex));
  22558. +
  22559. +out:
  22560. + aufs_read_unlock(root, !AuLock_IR);
  22561. + return err;
  22562. +}
  22563. +#endif
  22564. +
  22565. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  22566. + substring_t args[])
  22567. +{
  22568. + int err;
  22569. + struct file *file;
  22570. +
  22571. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  22572. + err = PTR_ERR(file);
  22573. + if (IS_ERR(file))
  22574. + goto out;
  22575. +
  22576. + err = -EINVAL;
  22577. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  22578. + fput(file);
  22579. + pr_err("%s must be outside\n", args[0].from);
  22580. + goto out;
  22581. + }
  22582. +
  22583. + err = 0;
  22584. + xino->file = file;
  22585. + xino->path = args[0].from;
  22586. +
  22587. +out:
  22588. + return err;
  22589. +}
  22590. +
  22591. +static int noinline_for_stack
  22592. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  22593. + struct au_opt_xino_itrunc *xino_itrunc,
  22594. + substring_t args[])
  22595. +{
  22596. + int err;
  22597. + aufs_bindex_t bend, bindex;
  22598. + struct path path;
  22599. + struct dentry *root;
  22600. +
  22601. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  22602. + if (unlikely(err)) {
  22603. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  22604. + goto out;
  22605. + }
  22606. +
  22607. + xino_itrunc->bindex = -1;
  22608. + root = sb->s_root;
  22609. + aufs_read_lock(root, AuLock_FLUSH);
  22610. + bend = au_sbend(sb);
  22611. + for (bindex = 0; bindex <= bend; bindex++) {
  22612. + if (au_h_dptr(root, bindex) == path.dentry) {
  22613. + xino_itrunc->bindex = bindex;
  22614. + break;
  22615. + }
  22616. + }
  22617. + aufs_read_unlock(root, !AuLock_IR);
  22618. + path_put(&path);
  22619. +
  22620. + if (unlikely(xino_itrunc->bindex < 0)) {
  22621. + pr_err("no such branch %s\n", args[0].from);
  22622. + err = -EINVAL;
  22623. + }
  22624. +
  22625. +out:
  22626. + return err;
  22627. +}
  22628. +
  22629. +/* called without aufs lock */
  22630. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  22631. +{
  22632. + int err, n, token;
  22633. + aufs_bindex_t bindex;
  22634. + unsigned char skipped;
  22635. + struct dentry *root;
  22636. + struct au_opt *opt, *opt_tail;
  22637. + char *opt_str;
  22638. + /* reduce the stack space */
  22639. + union {
  22640. + struct au_opt_xino_itrunc *xino_itrunc;
  22641. + struct au_opt_wbr_create *create;
  22642. + } u;
  22643. + struct {
  22644. + substring_t args[MAX_OPT_ARGS];
  22645. + } *a;
  22646. +
  22647. + err = -ENOMEM;
  22648. + a = kmalloc(sizeof(*a), GFP_NOFS);
  22649. + if (unlikely(!a))
  22650. + goto out;
  22651. +
  22652. + root = sb->s_root;
  22653. + err = 0;
  22654. + bindex = 0;
  22655. + opt = opts->opt;
  22656. + opt_tail = opt + opts->max_opt - 1;
  22657. + opt->type = Opt_tail;
  22658. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  22659. + err = -EINVAL;
  22660. + skipped = 0;
  22661. + token = match_token(opt_str, options, a->args);
  22662. + switch (token) {
  22663. + case Opt_br:
  22664. + err = 0;
  22665. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  22666. + && *opt_str) {
  22667. + err = opt_add(opt, opt_str, opts->sb_flags,
  22668. + bindex++);
  22669. + if (unlikely(!err && ++opt > opt_tail)) {
  22670. + err = -E2BIG;
  22671. + break;
  22672. + }
  22673. + opt->type = Opt_tail;
  22674. + skipped = 1;
  22675. + }
  22676. + break;
  22677. + case Opt_add:
  22678. + if (unlikely(match_int(&a->args[0], &n))) {
  22679. + pr_err("bad integer in %s\n", opt_str);
  22680. + break;
  22681. + }
  22682. + bindex = n;
  22683. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  22684. + bindex);
  22685. + if (!err)
  22686. + opt->type = token;
  22687. + break;
  22688. + case Opt_append:
  22689. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  22690. + /*dummy bindex*/1);
  22691. + if (!err)
  22692. + opt->type = token;
  22693. + break;
  22694. + case Opt_prepend:
  22695. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  22696. + /*bindex*/0);
  22697. + if (!err)
  22698. + opt->type = token;
  22699. + break;
  22700. + case Opt_del:
  22701. + err = au_opts_parse_del(&opt->del, a->args);
  22702. + if (!err)
  22703. + opt->type = token;
  22704. + break;
  22705. +#if 0 /* reserved for future use */
  22706. + case Opt_idel:
  22707. + del->pathname = "(indexed)";
  22708. + if (unlikely(match_int(&args[0], &n))) {
  22709. + pr_err("bad integer in %s\n", opt_str);
  22710. + break;
  22711. + }
  22712. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  22713. + if (!err)
  22714. + opt->type = token;
  22715. + break;
  22716. +#endif
  22717. + case Opt_mod:
  22718. + err = au_opts_parse_mod(&opt->mod, a->args);
  22719. + if (!err)
  22720. + opt->type = token;
  22721. + break;
  22722. +#ifdef IMOD /* reserved for future use */
  22723. + case Opt_imod:
  22724. + u.mod->path = "(indexed)";
  22725. + if (unlikely(match_int(&a->args[0], &n))) {
  22726. + pr_err("bad integer in %s\n", opt_str);
  22727. + break;
  22728. + }
  22729. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  22730. + if (!err)
  22731. + opt->type = token;
  22732. + break;
  22733. +#endif
  22734. + case Opt_xino:
  22735. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  22736. + if (!err)
  22737. + opt->type = token;
  22738. + break;
  22739. +
  22740. + case Opt_trunc_xino_path:
  22741. + err = au_opts_parse_xino_itrunc_path
  22742. + (sb, &opt->xino_itrunc, a->args);
  22743. + if (!err)
  22744. + opt->type = token;
  22745. + break;
  22746. +
  22747. + case Opt_itrunc_xino:
  22748. + u.xino_itrunc = &opt->xino_itrunc;
  22749. + if (unlikely(match_int(&a->args[0], &n))) {
  22750. + pr_err("bad integer in %s\n", opt_str);
  22751. + break;
  22752. + }
  22753. + u.xino_itrunc->bindex = n;
  22754. + aufs_read_lock(root, AuLock_FLUSH);
  22755. + if (n < 0 || au_sbend(sb) < n) {
  22756. + pr_err("out of bounds, %d\n", n);
  22757. + aufs_read_unlock(root, !AuLock_IR);
  22758. + break;
  22759. + }
  22760. + aufs_read_unlock(root, !AuLock_IR);
  22761. + err = 0;
  22762. + opt->type = token;
  22763. + break;
  22764. +
  22765. + case Opt_dirwh:
  22766. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  22767. + break;
  22768. + err = 0;
  22769. + opt->type = token;
  22770. + break;
  22771. +
  22772. + case Opt_rdcache:
  22773. + if (unlikely(match_int(&a->args[0], &n))) {
  22774. + pr_err("bad integer in %s\n", opt_str);
  22775. + break;
  22776. + }
  22777. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  22778. + pr_err("rdcache must be smaller than %d\n",
  22779. + AUFS_RDCACHE_MAX);
  22780. + break;
  22781. + }
  22782. + opt->rdcache = n;
  22783. + err = 0;
  22784. + opt->type = token;
  22785. + break;
  22786. + case Opt_rdblk:
  22787. + if (unlikely(match_int(&a->args[0], &n)
  22788. + || n < 0
  22789. + || n > KMALLOC_MAX_SIZE)) {
  22790. + pr_err("bad integer in %s\n", opt_str);
  22791. + break;
  22792. + }
  22793. + if (unlikely(n && n < NAME_MAX)) {
  22794. + pr_err("rdblk must be larger than %d\n",
  22795. + NAME_MAX);
  22796. + break;
  22797. + }
  22798. + opt->rdblk = n;
  22799. + err = 0;
  22800. + opt->type = token;
  22801. + break;
  22802. + case Opt_rdhash:
  22803. + if (unlikely(match_int(&a->args[0], &n)
  22804. + || n < 0
  22805. + || n * sizeof(struct hlist_head)
  22806. + > KMALLOC_MAX_SIZE)) {
  22807. + pr_err("bad integer in %s\n", opt_str);
  22808. + break;
  22809. + }
  22810. + opt->rdhash = n;
  22811. + err = 0;
  22812. + opt->type = token;
  22813. + break;
  22814. +
  22815. + case Opt_trunc_xino:
  22816. + case Opt_notrunc_xino:
  22817. + case Opt_noxino:
  22818. + case Opt_trunc_xib:
  22819. + case Opt_notrunc_xib:
  22820. + case Opt_shwh:
  22821. + case Opt_noshwh:
  22822. + case Opt_dirperm1:
  22823. + case Opt_nodirperm1:
  22824. + case Opt_plink:
  22825. + case Opt_noplink:
  22826. + case Opt_list_plink:
  22827. + case Opt_dio:
  22828. + case Opt_nodio:
  22829. + case Opt_diropq_a:
  22830. + case Opt_diropq_w:
  22831. + case Opt_warn_perm:
  22832. + case Opt_nowarn_perm:
  22833. + case Opt_verbose:
  22834. + case Opt_noverbose:
  22835. + case Opt_sum:
  22836. + case Opt_nosum:
  22837. + case Opt_wsum:
  22838. + case Opt_rdblk_def:
  22839. + case Opt_rdhash_def:
  22840. + case Opt_acl:
  22841. + case Opt_noacl:
  22842. + err = 0;
  22843. + opt->type = token;
  22844. + break;
  22845. +
  22846. + case Opt_udba:
  22847. + opt->udba = udba_val(a->args[0].from);
  22848. + if (opt->udba >= 0) {
  22849. + err = 0;
  22850. + opt->type = token;
  22851. + } else
  22852. + pr_err("wrong value, %s\n", opt_str);
  22853. + break;
  22854. +
  22855. + case Opt_wbr_create:
  22856. + u.create = &opt->wbr_create;
  22857. + u.create->wbr_create
  22858. + = au_wbr_create_val(a->args[0].from, u.create);
  22859. + if (u.create->wbr_create >= 0) {
  22860. + err = 0;
  22861. + opt->type = token;
  22862. + } else
  22863. + pr_err("wrong value, %s\n", opt_str);
  22864. + break;
  22865. + case Opt_wbr_copyup:
  22866. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  22867. + if (opt->wbr_copyup >= 0) {
  22868. + err = 0;
  22869. + opt->type = token;
  22870. + } else
  22871. + pr_err("wrong value, %s\n", opt_str);
  22872. + break;
  22873. +
  22874. + case Opt_fhsm_sec:
  22875. + if (unlikely(match_int(&a->args[0], &n)
  22876. + || n < 0)) {
  22877. + pr_err("bad integer in %s\n", opt_str);
  22878. + break;
  22879. + }
  22880. + if (sysaufs_brs) {
  22881. + opt->fhsm_second = n;
  22882. + opt->type = token;
  22883. + } else
  22884. + pr_warn("ignored %s\n", opt_str);
  22885. + err = 0;
  22886. + break;
  22887. +
  22888. + case Opt_ignore:
  22889. + pr_warn("ignored %s\n", opt_str);
  22890. + /*FALLTHROUGH*/
  22891. + case Opt_ignore_silent:
  22892. + skipped = 1;
  22893. + err = 0;
  22894. + break;
  22895. + case Opt_err:
  22896. + pr_err("unknown option %s\n", opt_str);
  22897. + break;
  22898. + }
  22899. +
  22900. + if (!err && !skipped) {
  22901. + if (unlikely(++opt > opt_tail)) {
  22902. + err = -E2BIG;
  22903. + opt--;
  22904. + opt->type = Opt_tail;
  22905. + break;
  22906. + }
  22907. + opt->type = Opt_tail;
  22908. + }
  22909. + }
  22910. +
  22911. + kfree(a);
  22912. + dump_opts(opts);
  22913. + if (unlikely(err))
  22914. + au_opts_free(opts);
  22915. +
  22916. +out:
  22917. + return err;
  22918. +}
  22919. +
  22920. +static int au_opt_wbr_create(struct super_block *sb,
  22921. + struct au_opt_wbr_create *create)
  22922. +{
  22923. + int err;
  22924. + struct au_sbinfo *sbinfo;
  22925. +
  22926. + SiMustWriteLock(sb);
  22927. +
  22928. + err = 1; /* handled */
  22929. + sbinfo = au_sbi(sb);
  22930. + if (sbinfo->si_wbr_create_ops->fin) {
  22931. + err = sbinfo->si_wbr_create_ops->fin(sb);
  22932. + if (!err)
  22933. + err = 1;
  22934. + }
  22935. +
  22936. + sbinfo->si_wbr_create = create->wbr_create;
  22937. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  22938. + switch (create->wbr_create) {
  22939. + case AuWbrCreate_MFSRRV:
  22940. + case AuWbrCreate_MFSRR:
  22941. + case AuWbrCreate_PMFSRR:
  22942. + case AuWbrCreate_PMFSRRV:
  22943. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  22944. + /*FALLTHROUGH*/
  22945. + case AuWbrCreate_MFS:
  22946. + case AuWbrCreate_MFSV:
  22947. + case AuWbrCreate_PMFS:
  22948. + case AuWbrCreate_PMFSV:
  22949. + sbinfo->si_wbr_mfs.mfs_expire
  22950. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  22951. + break;
  22952. + }
  22953. +
  22954. + if (sbinfo->si_wbr_create_ops->init)
  22955. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  22956. +
  22957. + return err;
  22958. +}
  22959. +
  22960. +/*
  22961. + * returns,
  22962. + * plus: processed without an error
  22963. + * zero: unprocessed
  22964. + */
  22965. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  22966. + struct au_opts *opts)
  22967. +{
  22968. + int err;
  22969. + struct au_sbinfo *sbinfo;
  22970. +
  22971. + SiMustWriteLock(sb);
  22972. +
  22973. + err = 1; /* handled */
  22974. + sbinfo = au_sbi(sb);
  22975. + switch (opt->type) {
  22976. + case Opt_udba:
  22977. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  22978. + sbinfo->si_mntflags |= opt->udba;
  22979. + opts->given_udba |= opt->udba;
  22980. + break;
  22981. +
  22982. + case Opt_plink:
  22983. + au_opt_set(sbinfo->si_mntflags, PLINK);
  22984. + break;
  22985. + case Opt_noplink:
  22986. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  22987. + au_plink_put(sb, /*verbose*/1);
  22988. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  22989. + break;
  22990. + case Opt_list_plink:
  22991. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  22992. + au_plink_list(sb);
  22993. + break;
  22994. +
  22995. + case Opt_dio:
  22996. + au_opt_set(sbinfo->si_mntflags, DIO);
  22997. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  22998. + break;
  22999. + case Opt_nodio:
  23000. + au_opt_clr(sbinfo->si_mntflags, DIO);
  23001. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  23002. + break;
  23003. +
  23004. + case Opt_fhsm_sec:
  23005. + au_fhsm_set(sbinfo, opt->fhsm_second);
  23006. + break;
  23007. +
  23008. + case Opt_diropq_a:
  23009. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  23010. + break;
  23011. + case Opt_diropq_w:
  23012. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  23013. + break;
  23014. +
  23015. + case Opt_warn_perm:
  23016. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  23017. + break;
  23018. + case Opt_nowarn_perm:
  23019. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  23020. + break;
  23021. +
  23022. + case Opt_verbose:
  23023. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  23024. + break;
  23025. + case Opt_noverbose:
  23026. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  23027. + break;
  23028. +
  23029. + case Opt_sum:
  23030. + au_opt_set(sbinfo->si_mntflags, SUM);
  23031. + break;
  23032. + case Opt_wsum:
  23033. + au_opt_clr(sbinfo->si_mntflags, SUM);
  23034. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  23035. + case Opt_nosum:
  23036. + au_opt_clr(sbinfo->si_mntflags, SUM);
  23037. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  23038. + break;
  23039. +
  23040. + case Opt_wbr_create:
  23041. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  23042. + break;
  23043. + case Opt_wbr_copyup:
  23044. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  23045. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  23046. + break;
  23047. +
  23048. + case Opt_dirwh:
  23049. + sbinfo->si_dirwh = opt->dirwh;
  23050. + break;
  23051. +
  23052. + case Opt_rdcache:
  23053. + sbinfo->si_rdcache
  23054. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  23055. + break;
  23056. + case Opt_rdblk:
  23057. + sbinfo->si_rdblk = opt->rdblk;
  23058. + break;
  23059. + case Opt_rdblk_def:
  23060. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  23061. + break;
  23062. + case Opt_rdhash:
  23063. + sbinfo->si_rdhash = opt->rdhash;
  23064. + break;
  23065. + case Opt_rdhash_def:
  23066. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  23067. + break;
  23068. +
  23069. + case Opt_shwh:
  23070. + au_opt_set(sbinfo->si_mntflags, SHWH);
  23071. + break;
  23072. + case Opt_noshwh:
  23073. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  23074. + break;
  23075. +
  23076. + case Opt_dirperm1:
  23077. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  23078. + break;
  23079. + case Opt_nodirperm1:
  23080. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  23081. + break;
  23082. +
  23083. + case Opt_trunc_xino:
  23084. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  23085. + break;
  23086. + case Opt_notrunc_xino:
  23087. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  23088. + break;
  23089. +
  23090. + case Opt_trunc_xino_path:
  23091. + case Opt_itrunc_xino:
  23092. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
  23093. + if (!err)
  23094. + err = 1;
  23095. + break;
  23096. +
  23097. + case Opt_trunc_xib:
  23098. + au_fset_opts(opts->flags, TRUNC_XIB);
  23099. + break;
  23100. + case Opt_notrunc_xib:
  23101. + au_fclr_opts(opts->flags, TRUNC_XIB);
  23102. + break;
  23103. +
  23104. + case Opt_acl:
  23105. + sb->s_flags |= MS_POSIXACL;
  23106. + break;
  23107. + case Opt_noacl:
  23108. + sb->s_flags &= ~MS_POSIXACL;
  23109. + break;
  23110. +
  23111. + default:
  23112. + err = 0;
  23113. + break;
  23114. + }
  23115. +
  23116. + return err;
  23117. +}
  23118. +
  23119. +/*
  23120. + * returns tri-state.
  23121. + * plus: processed without an error
  23122. + * zero: unprocessed
  23123. + * minus: error
  23124. + */
  23125. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  23126. + struct au_opts *opts)
  23127. +{
  23128. + int err, do_refresh;
  23129. +
  23130. + err = 0;
  23131. + switch (opt->type) {
  23132. + case Opt_append:
  23133. + opt->add.bindex = au_sbend(sb) + 1;
  23134. + if (opt->add.bindex < 0)
  23135. + opt->add.bindex = 0;
  23136. + goto add;
  23137. + case Opt_prepend:
  23138. + opt->add.bindex = 0;
  23139. + add: /* indented label */
  23140. + case Opt_add:
  23141. + err = au_br_add(sb, &opt->add,
  23142. + au_ftest_opts(opts->flags, REMOUNT));
  23143. + if (!err) {
  23144. + err = 1;
  23145. + au_fset_opts(opts->flags, REFRESH);
  23146. + }
  23147. + break;
  23148. +
  23149. + case Opt_del:
  23150. + case Opt_idel:
  23151. + err = au_br_del(sb, &opt->del,
  23152. + au_ftest_opts(opts->flags, REMOUNT));
  23153. + if (!err) {
  23154. + err = 1;
  23155. + au_fset_opts(opts->flags, TRUNC_XIB);
  23156. + au_fset_opts(opts->flags, REFRESH);
  23157. + }
  23158. + break;
  23159. +
  23160. + case Opt_mod:
  23161. + case Opt_imod:
  23162. + err = au_br_mod(sb, &opt->mod,
  23163. + au_ftest_opts(opts->flags, REMOUNT),
  23164. + &do_refresh);
  23165. + if (!err) {
  23166. + err = 1;
  23167. + if (do_refresh)
  23168. + au_fset_opts(opts->flags, REFRESH);
  23169. + }
  23170. + break;
  23171. + }
  23172. +
  23173. + return err;
  23174. +}
  23175. +
  23176. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  23177. + struct au_opt_xino **opt_xino,
  23178. + struct au_opts *opts)
  23179. +{
  23180. + int err;
  23181. + aufs_bindex_t bend, bindex;
  23182. + struct dentry *root, *parent, *h_root;
  23183. +
  23184. + err = 0;
  23185. + switch (opt->type) {
  23186. + case Opt_xino:
  23187. + err = au_xino_set(sb, &opt->xino,
  23188. + !!au_ftest_opts(opts->flags, REMOUNT));
  23189. + if (unlikely(err))
  23190. + break;
  23191. +
  23192. + *opt_xino = &opt->xino;
  23193. + au_xino_brid_set(sb, -1);
  23194. +
  23195. + /* safe d_parent access */
  23196. + parent = opt->xino.file->f_path.dentry->d_parent;
  23197. + root = sb->s_root;
  23198. + bend = au_sbend(sb);
  23199. + for (bindex = 0; bindex <= bend; bindex++) {
  23200. + h_root = au_h_dptr(root, bindex);
  23201. + if (h_root == parent) {
  23202. + au_xino_brid_set(sb, au_sbr_id(sb, bindex));
  23203. + break;
  23204. + }
  23205. + }
  23206. + break;
  23207. +
  23208. + case Opt_noxino:
  23209. + au_xino_clr(sb);
  23210. + au_xino_brid_set(sb, -1);
  23211. + *opt_xino = (void *)-1;
  23212. + break;
  23213. + }
  23214. +
  23215. + return err;
  23216. +}
  23217. +
  23218. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  23219. + unsigned int pending)
  23220. +{
  23221. + int err, fhsm;
  23222. + aufs_bindex_t bindex, bend;
  23223. + unsigned char do_plink, skip, do_free;
  23224. + struct au_branch *br;
  23225. + struct au_wbr *wbr;
  23226. + struct dentry *root;
  23227. + struct inode *dir, *h_dir;
  23228. + struct au_sbinfo *sbinfo;
  23229. + struct au_hinode *hdir;
  23230. +
  23231. + SiMustAnyLock(sb);
  23232. +
  23233. + sbinfo = au_sbi(sb);
  23234. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  23235. +
  23236. + if (!(sb_flags & MS_RDONLY)) {
  23237. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  23238. + pr_warn("first branch should be rw\n");
  23239. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  23240. + pr_warn("shwh should be used with ro\n");
  23241. + }
  23242. +
  23243. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  23244. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  23245. + pr_warn("udba=*notify requires xino\n");
  23246. +
  23247. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  23248. + pr_warn("dirperm1 breaks the protection"
  23249. + " by the permission bits on the lower branch\n");
  23250. +
  23251. + err = 0;
  23252. + fhsm = 0;
  23253. + root = sb->s_root;
  23254. + dir = d_inode(root);
  23255. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  23256. + bend = au_sbend(sb);
  23257. + for (bindex = 0; !err && bindex <= bend; bindex++) {
  23258. + skip = 0;
  23259. + h_dir = au_h_iptr(dir, bindex);
  23260. + br = au_sbr(sb, bindex);
  23261. +
  23262. + if ((br->br_perm & AuBrAttr_ICEX)
  23263. + && !h_dir->i_op->listxattr)
  23264. + br->br_perm &= ~AuBrAttr_ICEX;
  23265. +#if 0
  23266. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  23267. + && (au_br_sb(br)->s_flags & MS_NOSEC))
  23268. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  23269. +#endif
  23270. +
  23271. + do_free = 0;
  23272. + wbr = br->br_wbr;
  23273. + if (wbr)
  23274. + wbr_wh_read_lock(wbr);
  23275. +
  23276. + if (!au_br_writable(br->br_perm)) {
  23277. + do_free = !!wbr;
  23278. + skip = (!wbr
  23279. + || (!wbr->wbr_whbase
  23280. + && !wbr->wbr_plink
  23281. + && !wbr->wbr_orph));
  23282. + } else if (!au_br_wh_linkable(br->br_perm)) {
  23283. + /* skip = (!br->br_whbase && !br->br_orph); */
  23284. + skip = (!wbr || !wbr->wbr_whbase);
  23285. + if (skip && wbr) {
  23286. + if (do_plink)
  23287. + skip = !!wbr->wbr_plink;
  23288. + else
  23289. + skip = !wbr->wbr_plink;
  23290. + }
  23291. + } else {
  23292. + /* skip = (br->br_whbase && br->br_ohph); */
  23293. + skip = (wbr && wbr->wbr_whbase);
  23294. + if (skip) {
  23295. + if (do_plink)
  23296. + skip = !!wbr->wbr_plink;
  23297. + else
  23298. + skip = !wbr->wbr_plink;
  23299. + }
  23300. + }
  23301. + if (wbr)
  23302. + wbr_wh_read_unlock(wbr);
  23303. +
  23304. + if (au_br_fhsm(br->br_perm)) {
  23305. + fhsm++;
  23306. + AuDebugOn(!br->br_fhsm);
  23307. + }
  23308. +
  23309. + if (skip)
  23310. + continue;
  23311. +
  23312. + hdir = au_hi(dir, bindex);
  23313. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  23314. + if (wbr)
  23315. + wbr_wh_write_lock(wbr);
  23316. + err = au_wh_init(br, sb);
  23317. + if (wbr)
  23318. + wbr_wh_write_unlock(wbr);
  23319. + au_hn_imtx_unlock(hdir);
  23320. +
  23321. + if (!err && do_free) {
  23322. + kfree(wbr);
  23323. + br->br_wbr = NULL;
  23324. + }
  23325. + }
  23326. +
  23327. + if (fhsm >= 2) {
  23328. + au_fset_si(sbinfo, FHSM);
  23329. + for (bindex = bend; bindex >= 0; bindex--) {
  23330. + br = au_sbr(sb, bindex);
  23331. + if (au_br_fhsm(br->br_perm)) {
  23332. + au_fhsm_set_bottom(sb, bindex);
  23333. + break;
  23334. + }
  23335. + }
  23336. + } else {
  23337. + au_fclr_si(sbinfo, FHSM);
  23338. + au_fhsm_set_bottom(sb, -1);
  23339. + }
  23340. +
  23341. + return err;
  23342. +}
  23343. +
  23344. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  23345. +{
  23346. + int err;
  23347. + unsigned int tmp;
  23348. + aufs_bindex_t bindex, bend;
  23349. + struct au_opt *opt;
  23350. + struct au_opt_xino *opt_xino, xino;
  23351. + struct au_sbinfo *sbinfo;
  23352. + struct au_branch *br;
  23353. + struct inode *dir;
  23354. +
  23355. + SiMustWriteLock(sb);
  23356. +
  23357. + err = 0;
  23358. + opt_xino = NULL;
  23359. + opt = opts->opt;
  23360. + while (err >= 0 && opt->type != Opt_tail)
  23361. + err = au_opt_simple(sb, opt++, opts);
  23362. + if (err > 0)
  23363. + err = 0;
  23364. + else if (unlikely(err < 0))
  23365. + goto out;
  23366. +
  23367. + /* disable xino and udba temporary */
  23368. + sbinfo = au_sbi(sb);
  23369. + tmp = sbinfo->si_mntflags;
  23370. + au_opt_clr(sbinfo->si_mntflags, XINO);
  23371. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  23372. +
  23373. + opt = opts->opt;
  23374. + while (err >= 0 && opt->type != Opt_tail)
  23375. + err = au_opt_br(sb, opt++, opts);
  23376. + if (err > 0)
  23377. + err = 0;
  23378. + else if (unlikely(err < 0))
  23379. + goto out;
  23380. +
  23381. + bend = au_sbend(sb);
  23382. + if (unlikely(bend < 0)) {
  23383. + err = -EINVAL;
  23384. + pr_err("no branches\n");
  23385. + goto out;
  23386. + }
  23387. +
  23388. + if (au_opt_test(tmp, XINO))
  23389. + au_opt_set(sbinfo->si_mntflags, XINO);
  23390. + opt = opts->opt;
  23391. + while (!err && opt->type != Opt_tail)
  23392. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  23393. + if (unlikely(err))
  23394. + goto out;
  23395. +
  23396. + err = au_opts_verify(sb, sb->s_flags, tmp);
  23397. + if (unlikely(err))
  23398. + goto out;
  23399. +
  23400. + /* restore xino */
  23401. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  23402. + xino.file = au_xino_def(sb);
  23403. + err = PTR_ERR(xino.file);
  23404. + if (IS_ERR(xino.file))
  23405. + goto out;
  23406. +
  23407. + err = au_xino_set(sb, &xino, /*remount*/0);
  23408. + fput(xino.file);
  23409. + if (unlikely(err))
  23410. + goto out;
  23411. + }
  23412. +
  23413. + /* restore udba */
  23414. + tmp &= AuOptMask_UDBA;
  23415. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  23416. + sbinfo->si_mntflags |= tmp;
  23417. + bend = au_sbend(sb);
  23418. + for (bindex = 0; bindex <= bend; bindex++) {
  23419. + br = au_sbr(sb, bindex);
  23420. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  23421. + if (unlikely(err))
  23422. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  23423. + bindex, err);
  23424. + /* go on even if err */
  23425. + }
  23426. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  23427. + dir = d_inode(sb->s_root);
  23428. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  23429. + }
  23430. +
  23431. +out:
  23432. + return err;
  23433. +}
  23434. +
  23435. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  23436. +{
  23437. + int err, rerr;
  23438. + struct inode *dir;
  23439. + struct au_opt_xino *opt_xino;
  23440. + struct au_opt *opt;
  23441. + struct au_sbinfo *sbinfo;
  23442. +
  23443. + SiMustWriteLock(sb);
  23444. +
  23445. + dir = d_inode(sb->s_root);
  23446. + sbinfo = au_sbi(sb);
  23447. + err = 0;
  23448. + opt_xino = NULL;
  23449. + opt = opts->opt;
  23450. + while (err >= 0 && opt->type != Opt_tail) {
  23451. + err = au_opt_simple(sb, opt, opts);
  23452. + if (!err)
  23453. + err = au_opt_br(sb, opt, opts);
  23454. + if (!err)
  23455. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  23456. + opt++;
  23457. + }
  23458. + if (err > 0)
  23459. + err = 0;
  23460. + AuTraceErr(err);
  23461. + /* go on even err */
  23462. +
  23463. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  23464. + if (unlikely(rerr && !err))
  23465. + err = rerr;
  23466. +
  23467. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  23468. + rerr = au_xib_trunc(sb);
  23469. + if (unlikely(rerr && !err))
  23470. + err = rerr;
  23471. + }
  23472. +
  23473. + /* will be handled by the caller */
  23474. + if (!au_ftest_opts(opts->flags, REFRESH)
  23475. + && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
  23476. + au_fset_opts(opts->flags, REFRESH);
  23477. +
  23478. + AuDbg("status 0x%x\n", opts->flags);
  23479. + return err;
  23480. +}
  23481. +
  23482. +/* ---------------------------------------------------------------------- */
  23483. +
  23484. +unsigned int au_opt_udba(struct super_block *sb)
  23485. +{
  23486. + return au_mntflags(sb) & AuOptMask_UDBA;
  23487. +}
  23488. diff -Nur linux-4.1.10.orig/fs/aufs/opts.h linux-4.1.10/fs/aufs/opts.h
  23489. --- linux-4.1.10.orig/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
  23490. +++ linux-4.1.10/fs/aufs/opts.h 2015-10-22 21:35:53.000000000 +0200
  23491. @@ -0,0 +1,210 @@
  23492. +/*
  23493. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23494. + *
  23495. + * This program, aufs is free software; you can redistribute it and/or modify
  23496. + * it under the terms of the GNU General Public License as published by
  23497. + * the Free Software Foundation; either version 2 of the License, or
  23498. + * (at your option) any later version.
  23499. + *
  23500. + * This program is distributed in the hope that it will be useful,
  23501. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23502. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23503. + * GNU General Public License for more details.
  23504. + *
  23505. + * You should have received a copy of the GNU General Public License
  23506. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23507. + */
  23508. +
  23509. +/*
  23510. + * mount options/flags
  23511. + */
  23512. +
  23513. +#ifndef __AUFS_OPTS_H__
  23514. +#define __AUFS_OPTS_H__
  23515. +
  23516. +#ifdef __KERNEL__
  23517. +
  23518. +#include <linux/path.h>
  23519. +
  23520. +struct file;
  23521. +struct super_block;
  23522. +
  23523. +/* ---------------------------------------------------------------------- */
  23524. +
  23525. +/* mount flags */
  23526. +#define AuOpt_XINO 1 /* external inode number bitmap
  23527. + and translation table */
  23528. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  23529. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  23530. +#define AuOpt_UDBA_REVAL (1 << 3)
  23531. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  23532. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  23533. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  23534. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  23535. + bits */
  23536. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  23537. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  23538. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  23539. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  23540. +#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
  23541. +#define AuOpt_DIO (1 << 14) /* direct io */
  23542. +
  23543. +#ifndef CONFIG_AUFS_HNOTIFY
  23544. +#undef AuOpt_UDBA_HNOTIFY
  23545. +#define AuOpt_UDBA_HNOTIFY 0
  23546. +#endif
  23547. +#ifndef CONFIG_AUFS_SHWH
  23548. +#undef AuOpt_SHWH
  23549. +#define AuOpt_SHWH 0
  23550. +#endif
  23551. +
  23552. +#define AuOpt_Def (AuOpt_XINO \
  23553. + | AuOpt_UDBA_REVAL \
  23554. + | AuOpt_PLINK \
  23555. + /* | AuOpt_DIRPERM1 */ \
  23556. + | AuOpt_WARN_PERM)
  23557. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  23558. + | AuOpt_UDBA_REVAL \
  23559. + | AuOpt_UDBA_HNOTIFY)
  23560. +
  23561. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  23562. +#define au_opt_set(flags, name) do { \
  23563. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  23564. + ((flags) |= AuOpt_##name); \
  23565. +} while (0)
  23566. +#define au_opt_set_udba(flags, name) do { \
  23567. + (flags) &= ~AuOptMask_UDBA; \
  23568. + ((flags) |= AuOpt_##name); \
  23569. +} while (0)
  23570. +#define au_opt_clr(flags, name) do { \
  23571. + ((flags) &= ~AuOpt_##name); \
  23572. +} while (0)
  23573. +
  23574. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  23575. +{
  23576. +#ifdef CONFIG_PROC_FS
  23577. + return mntflags;
  23578. +#else
  23579. + return mntflags & ~AuOpt_PLINK;
  23580. +#endif
  23581. +}
  23582. +
  23583. +/* ---------------------------------------------------------------------- */
  23584. +
  23585. +/* policies to select one among multiple writable branches */
  23586. +enum {
  23587. + AuWbrCreate_TDP, /* top down parent */
  23588. + AuWbrCreate_RR, /* round robin */
  23589. + AuWbrCreate_MFS, /* most free space */
  23590. + AuWbrCreate_MFSV, /* mfs with seconds */
  23591. + AuWbrCreate_MFSRR, /* mfs then rr */
  23592. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  23593. + AuWbrCreate_PMFS, /* parent and mfs */
  23594. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  23595. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  23596. + AuWbrCreate_PMFSRRV, /* plus seconds */
  23597. +
  23598. + AuWbrCreate_Def = AuWbrCreate_TDP
  23599. +};
  23600. +
  23601. +enum {
  23602. + AuWbrCopyup_TDP, /* top down parent */
  23603. + AuWbrCopyup_BUP, /* bottom up parent */
  23604. + AuWbrCopyup_BU, /* bottom up */
  23605. +
  23606. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  23607. +};
  23608. +
  23609. +/* ---------------------------------------------------------------------- */
  23610. +
  23611. +struct au_opt_add {
  23612. + aufs_bindex_t bindex;
  23613. + char *pathname;
  23614. + int perm;
  23615. + struct path path;
  23616. +};
  23617. +
  23618. +struct au_opt_del {
  23619. + char *pathname;
  23620. + struct path h_path;
  23621. +};
  23622. +
  23623. +struct au_opt_mod {
  23624. + char *path;
  23625. + int perm;
  23626. + struct dentry *h_root;
  23627. +};
  23628. +
  23629. +struct au_opt_xino {
  23630. + char *path;
  23631. + struct file *file;
  23632. +};
  23633. +
  23634. +struct au_opt_xino_itrunc {
  23635. + aufs_bindex_t bindex;
  23636. +};
  23637. +
  23638. +struct au_opt_wbr_create {
  23639. + int wbr_create;
  23640. + int mfs_second;
  23641. + unsigned long long mfsrr_watermark;
  23642. +};
  23643. +
  23644. +struct au_opt {
  23645. + int type;
  23646. + union {
  23647. + struct au_opt_xino xino;
  23648. + struct au_opt_xino_itrunc xino_itrunc;
  23649. + struct au_opt_add add;
  23650. + struct au_opt_del del;
  23651. + struct au_opt_mod mod;
  23652. + int dirwh;
  23653. + int rdcache;
  23654. + unsigned int rdblk;
  23655. + unsigned int rdhash;
  23656. + int udba;
  23657. + struct au_opt_wbr_create wbr_create;
  23658. + int wbr_copyup;
  23659. + unsigned int fhsm_second;
  23660. + };
  23661. +};
  23662. +
  23663. +/* opts flags */
  23664. +#define AuOpts_REMOUNT 1
  23665. +#define AuOpts_REFRESH (1 << 1)
  23666. +#define AuOpts_TRUNC_XIB (1 << 2)
  23667. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  23668. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  23669. +#define au_fset_opts(flags, name) \
  23670. + do { (flags) |= AuOpts_##name; } while (0)
  23671. +#define au_fclr_opts(flags, name) \
  23672. + do { (flags) &= ~AuOpts_##name; } while (0)
  23673. +
  23674. +struct au_opts {
  23675. + struct au_opt *opt;
  23676. + int max_opt;
  23677. +
  23678. + unsigned int given_udba;
  23679. + unsigned int flags;
  23680. + unsigned long sb_flags;
  23681. +};
  23682. +
  23683. +/* ---------------------------------------------------------------------- */
  23684. +
  23685. +/* opts.c */
  23686. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  23687. +const char *au_optstr_udba(int udba);
  23688. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  23689. +const char *au_optstr_wbr_create(int wbr_create);
  23690. +
  23691. +void au_opts_free(struct au_opts *opts);
  23692. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  23693. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  23694. + unsigned int pending);
  23695. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  23696. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  23697. +
  23698. +unsigned int au_opt_udba(struct super_block *sb);
  23699. +
  23700. +#endif /* __KERNEL__ */
  23701. +#endif /* __AUFS_OPTS_H__ */
  23702. diff -Nur linux-4.1.10.orig/fs/aufs/plink.c linux-4.1.10/fs/aufs/plink.c
  23703. --- linux-4.1.10.orig/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
  23704. +++ linux-4.1.10/fs/aufs/plink.c 2015-10-22 21:35:53.000000000 +0200
  23705. @@ -0,0 +1,528 @@
  23706. +/*
  23707. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  23708. + *
  23709. + * This program, aufs is free software; you can redistribute it and/or modify
  23710. + * it under the terms of the GNU General Public License as published by
  23711. + * the Free Software Foundation; either version 2 of the License, or
  23712. + * (at your option) any later version.
  23713. + *
  23714. + * This program is distributed in the hope that it will be useful,
  23715. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23716. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23717. + * GNU General Public License for more details.
  23718. + *
  23719. + * You should have received a copy of the GNU General Public License
  23720. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23721. + */
  23722. +
  23723. +/*
  23724. + * pseudo-link
  23725. + */
  23726. +
  23727. +#include "aufs.h"
  23728. +
  23729. +/*
  23730. + * the pseudo-link maintenance mode.
  23731. + * during a user process maintains the pseudo-links,
  23732. + * prohibit adding a new plink and branch manipulation.
  23733. + *
  23734. + * Flags
  23735. + * NOPLM:
  23736. + * For entry functions which will handle plink, and i_mutex is already held
  23737. + * in VFS.
  23738. + * They cannot wait and should return an error at once.
  23739. + * Callers has to check the error.
  23740. + * NOPLMW:
  23741. + * For entry functions which will handle plink, but i_mutex is not held
  23742. + * in VFS.
  23743. + * They can wait the plink maintenance mode to finish.
  23744. + *
  23745. + * They behave like F_SETLK and F_SETLKW.
  23746. + * If the caller never handle plink, then both flags are unnecessary.
  23747. + */
  23748. +
  23749. +int au_plink_maint(struct super_block *sb, int flags)
  23750. +{
  23751. + int err;
  23752. + pid_t pid, ppid;
  23753. + struct au_sbinfo *sbi;
  23754. +
  23755. + SiMustAnyLock(sb);
  23756. +
  23757. + err = 0;
  23758. + if (!au_opt_test(au_mntflags(sb), PLINK))
  23759. + goto out;
  23760. +
  23761. + sbi = au_sbi(sb);
  23762. + pid = sbi->si_plink_maint_pid;
  23763. + if (!pid || pid == current->pid)
  23764. + goto out;
  23765. +
  23766. + /* todo: it highly depends upon /sbin/mount.aufs */
  23767. + rcu_read_lock();
  23768. + ppid = task_pid_vnr(rcu_dereference(current->real_parent));
  23769. + rcu_read_unlock();
  23770. + if (pid == ppid)
  23771. + goto out;
  23772. +
  23773. + if (au_ftest_lock(flags, NOPLMW)) {
  23774. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  23775. + /* AuDebugOn(!lockdep_depth(current)); */
  23776. + while (sbi->si_plink_maint_pid) {
  23777. + si_read_unlock(sb);
  23778. + /* gave up wake_up_bit() */
  23779. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  23780. +
  23781. + if (au_ftest_lock(flags, FLUSH))
  23782. + au_nwt_flush(&sbi->si_nowait);
  23783. + si_noflush_read_lock(sb);
  23784. + }
  23785. + } else if (au_ftest_lock(flags, NOPLM)) {
  23786. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  23787. + err = -EAGAIN;
  23788. + }
  23789. +
  23790. +out:
  23791. + return err;
  23792. +}
  23793. +
  23794. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  23795. +{
  23796. + spin_lock(&sbinfo->si_plink_maint_lock);
  23797. + sbinfo->si_plink_maint_pid = 0;
  23798. + spin_unlock(&sbinfo->si_plink_maint_lock);
  23799. + wake_up_all(&sbinfo->si_plink_wq);
  23800. +}
  23801. +
  23802. +int au_plink_maint_enter(struct super_block *sb)
  23803. +{
  23804. + int err;
  23805. + struct au_sbinfo *sbinfo;
  23806. +
  23807. + err = 0;
  23808. + sbinfo = au_sbi(sb);
  23809. + /* make sure i am the only one in this fs */
  23810. + si_write_lock(sb, AuLock_FLUSH);
  23811. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  23812. + spin_lock(&sbinfo->si_plink_maint_lock);
  23813. + if (!sbinfo->si_plink_maint_pid)
  23814. + sbinfo->si_plink_maint_pid = current->pid;
  23815. + else
  23816. + err = -EBUSY;
  23817. + spin_unlock(&sbinfo->si_plink_maint_lock);
  23818. + }
  23819. + si_write_unlock(sb);
  23820. +
  23821. + return err;
  23822. +}
  23823. +
  23824. +/* ---------------------------------------------------------------------- */
  23825. +
  23826. +#ifdef CONFIG_AUFS_DEBUG
  23827. +void au_plink_list(struct super_block *sb)
  23828. +{
  23829. + int i;
  23830. + struct au_sbinfo *sbinfo;
  23831. + struct hlist_head *plink_hlist;
  23832. + struct pseudo_link *plink;
  23833. +
  23834. + SiMustAnyLock(sb);
  23835. +
  23836. + sbinfo = au_sbi(sb);
  23837. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  23838. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  23839. +
  23840. + for (i = 0; i < AuPlink_NHASH; i++) {
  23841. + plink_hlist = &sbinfo->si_plink[i].head;
  23842. + rcu_read_lock();
  23843. + hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
  23844. + AuDbg("%lu\n", plink->inode->i_ino);
  23845. + rcu_read_unlock();
  23846. + }
  23847. +}
  23848. +#endif
  23849. +
  23850. +/* is the inode pseudo-linked? */
  23851. +int au_plink_test(struct inode *inode)
  23852. +{
  23853. + int found, i;
  23854. + struct au_sbinfo *sbinfo;
  23855. + struct hlist_head *plink_hlist;
  23856. + struct pseudo_link *plink;
  23857. +
  23858. + sbinfo = au_sbi(inode->i_sb);
  23859. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  23860. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  23861. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  23862. +
  23863. + found = 0;
  23864. + i = au_plink_hash(inode->i_ino);
  23865. + plink_hlist = &sbinfo->si_plink[i].head;
  23866. + rcu_read_lock();
  23867. + hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
  23868. + if (plink->inode == inode) {
  23869. + found = 1;
  23870. + break;
  23871. + }
  23872. + rcu_read_unlock();
  23873. + return found;
  23874. +}
  23875. +
  23876. +/* ---------------------------------------------------------------------- */
  23877. +
  23878. +/*
  23879. + * generate a name for plink.
  23880. + * the file will be stored under AUFS_WH_PLINKDIR.
  23881. + */
  23882. +/* 20 is max digits length of ulong 64 */
  23883. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  23884. +
  23885. +static int plink_name(char *name, int len, struct inode *inode,
  23886. + aufs_bindex_t bindex)
  23887. +{
  23888. + int rlen;
  23889. + struct inode *h_inode;
  23890. +
  23891. + h_inode = au_h_iptr(inode, bindex);
  23892. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  23893. + return rlen;
  23894. +}
  23895. +
  23896. +struct au_do_plink_lkup_args {
  23897. + struct dentry **errp;
  23898. + struct qstr *tgtname;
  23899. + struct dentry *h_parent;
  23900. + struct au_branch *br;
  23901. +};
  23902. +
  23903. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  23904. + struct dentry *h_parent,
  23905. + struct au_branch *br)
  23906. +{
  23907. + struct dentry *h_dentry;
  23908. + struct mutex *h_mtx;
  23909. +
  23910. + h_mtx = &d_inode(h_parent)->i_mutex;
  23911. + mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
  23912. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  23913. + mutex_unlock(h_mtx);
  23914. + return h_dentry;
  23915. +}
  23916. +
  23917. +static void au_call_do_plink_lkup(void *args)
  23918. +{
  23919. + struct au_do_plink_lkup_args *a = args;
  23920. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  23921. +}
  23922. +
  23923. +/* lookup the plink-ed @inode under the branch at @bindex */
  23924. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  23925. +{
  23926. + struct dentry *h_dentry, *h_parent;
  23927. + struct au_branch *br;
  23928. + int wkq_err;
  23929. + char a[PLINK_NAME_LEN];
  23930. + struct qstr tgtname = QSTR_INIT(a, 0);
  23931. +
  23932. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  23933. +
  23934. + br = au_sbr(inode->i_sb, bindex);
  23935. + h_parent = br->br_wbr->wbr_plink;
  23936. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  23937. +
  23938. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  23939. + struct au_do_plink_lkup_args args = {
  23940. + .errp = &h_dentry,
  23941. + .tgtname = &tgtname,
  23942. + .h_parent = h_parent,
  23943. + .br = br
  23944. + };
  23945. +
  23946. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  23947. + if (unlikely(wkq_err))
  23948. + h_dentry = ERR_PTR(wkq_err);
  23949. + } else
  23950. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  23951. +
  23952. + return h_dentry;
  23953. +}
  23954. +
  23955. +/* create a pseudo-link */
  23956. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  23957. + struct dentry *h_dentry, struct au_branch *br)
  23958. +{
  23959. + int err;
  23960. + struct path h_path = {
  23961. + .mnt = au_br_mnt(br)
  23962. + };
  23963. + struct inode *h_dir, *delegated;
  23964. +
  23965. + h_dir = d_inode(h_parent);
  23966. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
  23967. +again:
  23968. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  23969. + err = PTR_ERR(h_path.dentry);
  23970. + if (IS_ERR(h_path.dentry))
  23971. + goto out;
  23972. +
  23973. + err = 0;
  23974. + /* wh.plink dir is not monitored */
  23975. + /* todo: is it really safe? */
  23976. + if (d_is_positive(h_path.dentry)
  23977. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  23978. + delegated = NULL;
  23979. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  23980. + if (unlikely(err == -EWOULDBLOCK)) {
  23981. + pr_warn("cannot retry for NFSv4 delegation"
  23982. + " for an internal unlink\n");
  23983. + iput(delegated);
  23984. + }
  23985. + dput(h_path.dentry);
  23986. + h_path.dentry = NULL;
  23987. + if (!err)
  23988. + goto again;
  23989. + }
  23990. + if (!err && d_is_negative(h_path.dentry)) {
  23991. + delegated = NULL;
  23992. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  23993. + if (unlikely(err == -EWOULDBLOCK)) {
  23994. + pr_warn("cannot retry for NFSv4 delegation"
  23995. + " for an internal link\n");
  23996. + iput(delegated);
  23997. + }
  23998. + }
  23999. + dput(h_path.dentry);
  24000. +
  24001. +out:
  24002. + mutex_unlock(&h_dir->i_mutex);
  24003. + return err;
  24004. +}
  24005. +
  24006. +struct do_whplink_args {
  24007. + int *errp;
  24008. + struct qstr *tgt;
  24009. + struct dentry *h_parent;
  24010. + struct dentry *h_dentry;
  24011. + struct au_branch *br;
  24012. +};
  24013. +
  24014. +static void call_do_whplink(void *args)
  24015. +{
  24016. + struct do_whplink_args *a = args;
  24017. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  24018. +}
  24019. +
  24020. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  24021. + aufs_bindex_t bindex, struct au_branch *br)
  24022. +{
  24023. + int err, wkq_err;
  24024. + struct au_wbr *wbr;
  24025. + struct dentry *h_parent;
  24026. + char a[PLINK_NAME_LEN];
  24027. + struct qstr tgtname = QSTR_INIT(a, 0);
  24028. +
  24029. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  24030. + h_parent = wbr->wbr_plink;
  24031. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  24032. +
  24033. + /* always superio. */
  24034. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  24035. + struct do_whplink_args args = {
  24036. + .errp = &err,
  24037. + .tgt = &tgtname,
  24038. + .h_parent = h_parent,
  24039. + .h_dentry = h_dentry,
  24040. + .br = br
  24041. + };
  24042. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  24043. + if (unlikely(wkq_err))
  24044. + err = wkq_err;
  24045. + } else
  24046. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  24047. +
  24048. + return err;
  24049. +}
  24050. +
  24051. +/* free a single plink */
  24052. +static void do_put_plink(struct pseudo_link *plink, int do_del)
  24053. +{
  24054. + if (do_del)
  24055. + hlist_del(&plink->hlist);
  24056. + iput(plink->inode);
  24057. + kfree(plink);
  24058. +}
  24059. +
  24060. +static void do_put_plink_rcu(struct rcu_head *rcu)
  24061. +{
  24062. + struct pseudo_link *plink;
  24063. +
  24064. + plink = container_of(rcu, struct pseudo_link, rcu);
  24065. + iput(plink->inode);
  24066. + kfree(plink);
  24067. +}
  24068. +
  24069. +/*
  24070. + * create a new pseudo-link for @h_dentry on @bindex.
  24071. + * the linked inode is held in aufs @inode.
  24072. + */
  24073. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  24074. + struct dentry *h_dentry)
  24075. +{
  24076. + struct super_block *sb;
  24077. + struct au_sbinfo *sbinfo;
  24078. + struct hlist_head *plink_hlist;
  24079. + struct pseudo_link *plink, *tmp;
  24080. + struct au_sphlhead *sphl;
  24081. + int found, err, cnt, i;
  24082. +
  24083. + sb = inode->i_sb;
  24084. + sbinfo = au_sbi(sb);
  24085. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24086. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24087. +
  24088. + found = au_plink_test(inode);
  24089. + if (found)
  24090. + return;
  24091. +
  24092. + i = au_plink_hash(inode->i_ino);
  24093. + sphl = sbinfo->si_plink + i;
  24094. + plink_hlist = &sphl->head;
  24095. + tmp = kmalloc(sizeof(*plink), GFP_NOFS);
  24096. + if (tmp)
  24097. + tmp->inode = au_igrab(inode);
  24098. + else {
  24099. + err = -ENOMEM;
  24100. + goto out;
  24101. + }
  24102. +
  24103. + spin_lock(&sphl->spin);
  24104. + hlist_for_each_entry(plink, plink_hlist, hlist) {
  24105. + if (plink->inode == inode) {
  24106. + found = 1;
  24107. + break;
  24108. + }
  24109. + }
  24110. + if (!found)
  24111. + hlist_add_head_rcu(&tmp->hlist, plink_hlist);
  24112. + spin_unlock(&sphl->spin);
  24113. + if (!found) {
  24114. + cnt = au_sphl_count(sphl);
  24115. +#define msg "unexpectedly unblanced or too many pseudo-links"
  24116. + if (cnt > AUFS_PLINK_WARN)
  24117. + AuWarn1(msg ", %d\n", cnt);
  24118. +#undef msg
  24119. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  24120. + } else {
  24121. + do_put_plink(tmp, 0);
  24122. + return;
  24123. + }
  24124. +
  24125. +out:
  24126. + if (unlikely(err)) {
  24127. + pr_warn("err %d, damaged pseudo link.\n", err);
  24128. + if (tmp) {
  24129. + au_sphl_del_rcu(&tmp->hlist, sphl);
  24130. + call_rcu(&tmp->rcu, do_put_plink_rcu);
  24131. + }
  24132. + }
  24133. +}
  24134. +
  24135. +/* free all plinks */
  24136. +void au_plink_put(struct super_block *sb, int verbose)
  24137. +{
  24138. + int i, warned;
  24139. + struct au_sbinfo *sbinfo;
  24140. + struct hlist_head *plink_hlist;
  24141. + struct hlist_node *tmp;
  24142. + struct pseudo_link *plink;
  24143. +
  24144. + SiMustWriteLock(sb);
  24145. +
  24146. + sbinfo = au_sbi(sb);
  24147. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24148. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24149. +
  24150. + /* no spin_lock since sbinfo is write-locked */
  24151. + warned = 0;
  24152. + for (i = 0; i < AuPlink_NHASH; i++) {
  24153. + plink_hlist = &sbinfo->si_plink[i].head;
  24154. + if (!warned && verbose && !hlist_empty(plink_hlist)) {
  24155. + pr_warn("pseudo-link is not flushed");
  24156. + warned = 1;
  24157. + }
  24158. + hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
  24159. + do_put_plink(plink, 0);
  24160. + INIT_HLIST_HEAD(plink_hlist);
  24161. + }
  24162. +}
  24163. +
  24164. +void au_plink_clean(struct super_block *sb, int verbose)
  24165. +{
  24166. + struct dentry *root;
  24167. +
  24168. + root = sb->s_root;
  24169. + aufs_write_lock(root);
  24170. + if (au_opt_test(au_mntflags(sb), PLINK))
  24171. + au_plink_put(sb, verbose);
  24172. + aufs_write_unlock(root);
  24173. +}
  24174. +
  24175. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  24176. +{
  24177. + int do_put;
  24178. + aufs_bindex_t bstart, bend, bindex;
  24179. +
  24180. + do_put = 0;
  24181. + bstart = au_ibstart(inode);
  24182. + bend = au_ibend(inode);
  24183. + if (bstart >= 0) {
  24184. + for (bindex = bstart; bindex <= bend; bindex++) {
  24185. + if (!au_h_iptr(inode, bindex)
  24186. + || au_ii_br_id(inode, bindex) != br_id)
  24187. + continue;
  24188. + au_set_h_iptr(inode, bindex, NULL, 0);
  24189. + do_put = 1;
  24190. + break;
  24191. + }
  24192. + if (do_put)
  24193. + for (bindex = bstart; bindex <= bend; bindex++)
  24194. + if (au_h_iptr(inode, bindex)) {
  24195. + do_put = 0;
  24196. + break;
  24197. + }
  24198. + } else
  24199. + do_put = 1;
  24200. +
  24201. + return do_put;
  24202. +}
  24203. +
  24204. +/* free the plinks on a branch specified by @br_id */
  24205. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  24206. +{
  24207. + struct au_sbinfo *sbinfo;
  24208. + struct hlist_head *plink_hlist;
  24209. + struct hlist_node *tmp;
  24210. + struct pseudo_link *plink;
  24211. + struct inode *inode;
  24212. + int i, do_put;
  24213. +
  24214. + SiMustWriteLock(sb);
  24215. +
  24216. + sbinfo = au_sbi(sb);
  24217. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  24218. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  24219. +
  24220. + /* no spin_lock since sbinfo is write-locked */
  24221. + for (i = 0; i < AuPlink_NHASH; i++) {
  24222. + plink_hlist = &sbinfo->si_plink[i].head;
  24223. + hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
  24224. + inode = au_igrab(plink->inode);
  24225. + ii_write_lock_child(inode);
  24226. + do_put = au_plink_do_half_refresh(inode, br_id);
  24227. + if (do_put)
  24228. + do_put_plink(plink, 1);
  24229. + ii_write_unlock(inode);
  24230. + iput(inode);
  24231. + }
  24232. + }
  24233. +}
  24234. diff -Nur linux-4.1.10.orig/fs/aufs/poll.c linux-4.1.10/fs/aufs/poll.c
  24235. --- linux-4.1.10.orig/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
  24236. +++ linux-4.1.10/fs/aufs/poll.c 2015-10-22 21:35:53.000000000 +0200
  24237. @@ -0,0 +1,52 @@
  24238. +/*
  24239. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24240. + *
  24241. + * This program, aufs is free software; you can redistribute it and/or modify
  24242. + * it under the terms of the GNU General Public License as published by
  24243. + * the Free Software Foundation; either version 2 of the License, or
  24244. + * (at your option) any later version.
  24245. + *
  24246. + * This program is distributed in the hope that it will be useful,
  24247. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24248. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24249. + * GNU General Public License for more details.
  24250. + *
  24251. + * You should have received a copy of the GNU General Public License
  24252. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24253. + */
  24254. +
  24255. +/*
  24256. + * poll operation
  24257. + * There is only one filesystem which implements ->poll operation, currently.
  24258. + */
  24259. +
  24260. +#include "aufs.h"
  24261. +
  24262. +unsigned int aufs_poll(struct file *file, poll_table *wait)
  24263. +{
  24264. + unsigned int mask;
  24265. + int err;
  24266. + struct file *h_file;
  24267. + struct super_block *sb;
  24268. +
  24269. + /* We should pretend an error happened. */
  24270. + mask = POLLERR /* | POLLIN | POLLOUT */;
  24271. + sb = file->f_path.dentry->d_sb;
  24272. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  24273. +
  24274. + h_file = au_read_pre(file, /*keep_fi*/0);
  24275. + err = PTR_ERR(h_file);
  24276. + if (IS_ERR(h_file))
  24277. + goto out;
  24278. +
  24279. + /* it is not an error if h_file has no operation */
  24280. + mask = DEFAULT_POLLMASK;
  24281. + if (h_file->f_op->poll)
  24282. + mask = h_file->f_op->poll(h_file, wait);
  24283. + fput(h_file); /* instead of au_read_post() */
  24284. +
  24285. +out:
  24286. + si_read_unlock(sb);
  24287. + AuTraceErr((int)mask);
  24288. + return mask;
  24289. +}
  24290. diff -Nur linux-4.1.10.orig/fs/aufs/posix_acl.c linux-4.1.10/fs/aufs/posix_acl.c
  24291. --- linux-4.1.10.orig/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
  24292. +++ linux-4.1.10/fs/aufs/posix_acl.c 2015-10-22 21:35:53.000000000 +0200
  24293. @@ -0,0 +1,99 @@
  24294. +/*
  24295. + * Copyright (C) 2014-2015 Junjiro R. Okajima
  24296. + *
  24297. + * This program, aufs is free software; you can redistribute it and/or modify
  24298. + * it under the terms of the GNU General Public License as published by
  24299. + * the Free Software Foundation; either version 2 of the License, or
  24300. + * (at your option) any later version.
  24301. + *
  24302. + * This program is distributed in the hope that it will be useful,
  24303. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24304. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24305. + * GNU General Public License for more details.
  24306. + *
  24307. + * You should have received a copy of the GNU General Public License
  24308. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24309. + */
  24310. +
  24311. +/*
  24312. + * posix acl operations
  24313. + */
  24314. +
  24315. +#include <linux/fs.h>
  24316. +#include <linux/posix_acl.h>
  24317. +#include "aufs.h"
  24318. +
  24319. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  24320. +{
  24321. + struct posix_acl *acl;
  24322. + int err;
  24323. + aufs_bindex_t bindex;
  24324. + struct inode *h_inode;
  24325. + struct super_block *sb;
  24326. +
  24327. + acl = NULL;
  24328. + sb = inode->i_sb;
  24329. + si_read_lock(sb, AuLock_FLUSH);
  24330. + ii_read_lock_child(inode);
  24331. + if (!(sb->s_flags & MS_POSIXACL))
  24332. + goto out;
  24333. +
  24334. + bindex = au_ibstart(inode);
  24335. + h_inode = au_h_iptr(inode, bindex);
  24336. + if (unlikely(!h_inode
  24337. + || ((h_inode->i_mode & S_IFMT)
  24338. + != (inode->i_mode & S_IFMT)))) {
  24339. + err = au_busy_or_stale();
  24340. + acl = ERR_PTR(err);
  24341. + goto out;
  24342. + }
  24343. +
  24344. + /* always topmost only */
  24345. + acl = get_acl(h_inode, type);
  24346. +
  24347. +out:
  24348. + ii_read_unlock(inode);
  24349. + si_read_unlock(sb);
  24350. +
  24351. + AuTraceErrPtr(acl);
  24352. + return acl;
  24353. +}
  24354. +
  24355. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  24356. +{
  24357. + int err;
  24358. + ssize_t ssz;
  24359. + struct dentry *dentry;
  24360. + struct au_srxattr arg = {
  24361. + .type = AU_ACL_SET,
  24362. + .u.acl_set = {
  24363. + .acl = acl,
  24364. + .type = type
  24365. + },
  24366. + };
  24367. +
  24368. + mutex_lock(&inode->i_mutex);
  24369. + if (inode->i_ino == AUFS_ROOT_INO)
  24370. + dentry = dget(inode->i_sb->s_root);
  24371. + else {
  24372. + dentry = d_find_alias(inode);
  24373. + if (!dentry)
  24374. + dentry = d_find_any_alias(inode);
  24375. + if (!dentry) {
  24376. + pr_warn("cannot handle this inode, "
  24377. + "please report to aufs-users ML\n");
  24378. + err = -ENOENT;
  24379. + goto out;
  24380. + }
  24381. + }
  24382. +
  24383. + ssz = au_srxattr(dentry, &arg);
  24384. + dput(dentry);
  24385. + err = ssz;
  24386. + if (ssz >= 0)
  24387. + err = 0;
  24388. +
  24389. +out:
  24390. + mutex_unlock(&inode->i_mutex);
  24391. + return err;
  24392. +}
  24393. diff -Nur linux-4.1.10.orig/fs/aufs/procfs.c linux-4.1.10/fs/aufs/procfs.c
  24394. --- linux-4.1.10.orig/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
  24395. +++ linux-4.1.10/fs/aufs/procfs.c 2015-10-22 21:35:53.000000000 +0200
  24396. @@ -0,0 +1,169 @@
  24397. +/*
  24398. + * Copyright (C) 2010-2015 Junjiro R. Okajima
  24399. + *
  24400. + * This program, aufs is free software; you can redistribute it and/or modify
  24401. + * it under the terms of the GNU General Public License as published by
  24402. + * the Free Software Foundation; either version 2 of the License, or
  24403. + * (at your option) any later version.
  24404. + *
  24405. + * This program is distributed in the hope that it will be useful,
  24406. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24407. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24408. + * GNU General Public License for more details.
  24409. + *
  24410. + * You should have received a copy of the GNU General Public License
  24411. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24412. + */
  24413. +
  24414. +/*
  24415. + * procfs interfaces
  24416. + */
  24417. +
  24418. +#include <linux/proc_fs.h>
  24419. +#include "aufs.h"
  24420. +
  24421. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  24422. +{
  24423. + struct au_sbinfo *sbinfo;
  24424. +
  24425. + sbinfo = file->private_data;
  24426. + if (sbinfo) {
  24427. + au_plink_maint_leave(sbinfo);
  24428. + kobject_put(&sbinfo->si_kobj);
  24429. + }
  24430. +
  24431. + return 0;
  24432. +}
  24433. +
  24434. +static void au_procfs_plm_write_clean(struct file *file)
  24435. +{
  24436. + struct au_sbinfo *sbinfo;
  24437. +
  24438. + sbinfo = file->private_data;
  24439. + if (sbinfo)
  24440. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  24441. +}
  24442. +
  24443. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  24444. +{
  24445. + int err;
  24446. + struct super_block *sb;
  24447. + struct au_sbinfo *sbinfo;
  24448. +
  24449. + err = -EBUSY;
  24450. + if (unlikely(file->private_data))
  24451. + goto out;
  24452. +
  24453. + sb = NULL;
  24454. + /* don't use au_sbilist_lock() here */
  24455. + spin_lock(&au_sbilist.spin);
  24456. + list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  24457. + if (id == sysaufs_si_id(sbinfo)) {
  24458. + kobject_get(&sbinfo->si_kobj);
  24459. + sb = sbinfo->si_sb;
  24460. + break;
  24461. + }
  24462. + spin_unlock(&au_sbilist.spin);
  24463. +
  24464. + err = -EINVAL;
  24465. + if (unlikely(!sb))
  24466. + goto out;
  24467. +
  24468. + err = au_plink_maint_enter(sb);
  24469. + if (!err)
  24470. + /* keep kobject_get() */
  24471. + file->private_data = sbinfo;
  24472. + else
  24473. + kobject_put(&sbinfo->si_kobj);
  24474. +out:
  24475. + return err;
  24476. +}
  24477. +
  24478. +/*
  24479. + * Accept a valid "si=xxxx" only.
  24480. + * Once it is accepted successfully, accept "clean" too.
  24481. + */
  24482. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  24483. + size_t count, loff_t *ppos)
  24484. +{
  24485. + ssize_t err;
  24486. + unsigned long id;
  24487. + /* last newline is allowed */
  24488. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  24489. +
  24490. + err = -EACCES;
  24491. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  24492. + goto out;
  24493. +
  24494. + err = -EINVAL;
  24495. + if (unlikely(count > sizeof(buf)))
  24496. + goto out;
  24497. +
  24498. + err = copy_from_user(buf, ubuf, count);
  24499. + if (unlikely(err)) {
  24500. + err = -EFAULT;
  24501. + goto out;
  24502. + }
  24503. + buf[count] = 0;
  24504. +
  24505. + err = -EINVAL;
  24506. + if (!strcmp("clean", buf)) {
  24507. + au_procfs_plm_write_clean(file);
  24508. + goto out_success;
  24509. + } else if (unlikely(strncmp("si=", buf, 3)))
  24510. + goto out;
  24511. +
  24512. + err = kstrtoul(buf + 3, 16, &id);
  24513. + if (unlikely(err))
  24514. + goto out;
  24515. +
  24516. + err = au_procfs_plm_write_si(file, id);
  24517. + if (unlikely(err))
  24518. + goto out;
  24519. +
  24520. +out_success:
  24521. + err = count; /* success */
  24522. +out:
  24523. + return err;
  24524. +}
  24525. +
  24526. +static const struct file_operations au_procfs_plm_fop = {
  24527. + .write = au_procfs_plm_write,
  24528. + .release = au_procfs_plm_release,
  24529. + .owner = THIS_MODULE
  24530. +};
  24531. +
  24532. +/* ---------------------------------------------------------------------- */
  24533. +
  24534. +static struct proc_dir_entry *au_procfs_dir;
  24535. +
  24536. +void au_procfs_fin(void)
  24537. +{
  24538. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  24539. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  24540. +}
  24541. +
  24542. +int __init au_procfs_init(void)
  24543. +{
  24544. + int err;
  24545. + struct proc_dir_entry *entry;
  24546. +
  24547. + err = -ENOMEM;
  24548. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  24549. + if (unlikely(!au_procfs_dir))
  24550. + goto out;
  24551. +
  24552. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
  24553. + au_procfs_dir, &au_procfs_plm_fop);
  24554. + if (unlikely(!entry))
  24555. + goto out_dir;
  24556. +
  24557. + err = 0;
  24558. + goto out; /* success */
  24559. +
  24560. +
  24561. +out_dir:
  24562. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  24563. +out:
  24564. + return err;
  24565. +}
  24566. diff -Nur linux-4.1.10.orig/fs/aufs/rdu.c linux-4.1.10/fs/aufs/rdu.c
  24567. --- linux-4.1.10.orig/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
  24568. +++ linux-4.1.10/fs/aufs/rdu.c 2015-10-22 21:35:53.000000000 +0200
  24569. @@ -0,0 +1,388 @@
  24570. +/*
  24571. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24572. + *
  24573. + * This program, aufs is free software; you can redistribute it and/or modify
  24574. + * it under the terms of the GNU General Public License as published by
  24575. + * the Free Software Foundation; either version 2 of the License, or
  24576. + * (at your option) any later version.
  24577. + *
  24578. + * This program is distributed in the hope that it will be useful,
  24579. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24580. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24581. + * GNU General Public License for more details.
  24582. + *
  24583. + * You should have received a copy of the GNU General Public License
  24584. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24585. + */
  24586. +
  24587. +/*
  24588. + * readdir in userspace.
  24589. + */
  24590. +
  24591. +#include <linux/compat.h>
  24592. +#include <linux/fs_stack.h>
  24593. +#include <linux/security.h>
  24594. +#include "aufs.h"
  24595. +
  24596. +/* bits for struct aufs_rdu.flags */
  24597. +#define AuRdu_CALLED 1
  24598. +#define AuRdu_CONT (1 << 1)
  24599. +#define AuRdu_FULL (1 << 2)
  24600. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  24601. +#define au_fset_rdu(flags, name) \
  24602. + do { (flags) |= AuRdu_##name; } while (0)
  24603. +#define au_fclr_rdu(flags, name) \
  24604. + do { (flags) &= ~AuRdu_##name; } while (0)
  24605. +
  24606. +struct au_rdu_arg {
  24607. + struct dir_context ctx;
  24608. + struct aufs_rdu *rdu;
  24609. + union au_rdu_ent_ul ent;
  24610. + unsigned long end;
  24611. +
  24612. + struct super_block *sb;
  24613. + int err;
  24614. +};
  24615. +
  24616. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  24617. + loff_t offset, u64 h_ino, unsigned int d_type)
  24618. +{
  24619. + int err, len;
  24620. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  24621. + struct aufs_rdu *rdu = arg->rdu;
  24622. + struct au_rdu_ent ent;
  24623. +
  24624. + err = 0;
  24625. + arg->err = 0;
  24626. + au_fset_rdu(rdu->cookie.flags, CALLED);
  24627. + len = au_rdu_len(nlen);
  24628. + if (arg->ent.ul + len < arg->end) {
  24629. + ent.ino = h_ino;
  24630. + ent.bindex = rdu->cookie.bindex;
  24631. + ent.type = d_type;
  24632. + ent.nlen = nlen;
  24633. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  24634. + ent.type = DT_UNKNOWN;
  24635. +
  24636. + /* unnecessary to support mmap_sem since this is a dir */
  24637. + err = -EFAULT;
  24638. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  24639. + goto out;
  24640. + if (copy_to_user(arg->ent.e->name, name, nlen))
  24641. + goto out;
  24642. + /* the terminating NULL */
  24643. + if (__put_user(0, arg->ent.e->name + nlen))
  24644. + goto out;
  24645. + err = 0;
  24646. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  24647. + arg->ent.ul += len;
  24648. + rdu->rent++;
  24649. + } else {
  24650. + err = -EFAULT;
  24651. + au_fset_rdu(rdu->cookie.flags, FULL);
  24652. + rdu->full = 1;
  24653. + rdu->tail = arg->ent;
  24654. + }
  24655. +
  24656. +out:
  24657. + /* AuTraceErr(err); */
  24658. + return err;
  24659. +}
  24660. +
  24661. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  24662. +{
  24663. + int err;
  24664. + loff_t offset;
  24665. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  24666. +
  24667. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  24668. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  24669. + err = offset;
  24670. + if (unlikely(offset != cookie->h_pos))
  24671. + goto out;
  24672. +
  24673. + err = 0;
  24674. + do {
  24675. + arg->err = 0;
  24676. + au_fclr_rdu(cookie->flags, CALLED);
  24677. + /* smp_mb(); */
  24678. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  24679. + if (err >= 0)
  24680. + err = arg->err;
  24681. + } while (!err
  24682. + && au_ftest_rdu(cookie->flags, CALLED)
  24683. + && !au_ftest_rdu(cookie->flags, FULL));
  24684. + cookie->h_pos = h_file->f_pos;
  24685. +
  24686. +out:
  24687. + AuTraceErr(err);
  24688. + return err;
  24689. +}
  24690. +
  24691. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  24692. +{
  24693. + int err;
  24694. + aufs_bindex_t bend;
  24695. + struct au_rdu_arg arg = {
  24696. + .ctx = {
  24697. + .actor = au_rdu_fill
  24698. + }
  24699. + };
  24700. + struct dentry *dentry;
  24701. + struct inode *inode;
  24702. + struct file *h_file;
  24703. + struct au_rdu_cookie *cookie = &rdu->cookie;
  24704. +
  24705. + err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
  24706. + if (unlikely(err)) {
  24707. + err = -EFAULT;
  24708. + AuTraceErr(err);
  24709. + goto out;
  24710. + }
  24711. + rdu->rent = 0;
  24712. + rdu->tail = rdu->ent;
  24713. + rdu->full = 0;
  24714. + arg.rdu = rdu;
  24715. + arg.ent = rdu->ent;
  24716. + arg.end = arg.ent.ul;
  24717. + arg.end += rdu->sz;
  24718. +
  24719. + err = -ENOTDIR;
  24720. + if (unlikely(!file->f_op->iterate))
  24721. + goto out;
  24722. +
  24723. + err = security_file_permission(file, MAY_READ);
  24724. + AuTraceErr(err);
  24725. + if (unlikely(err))
  24726. + goto out;
  24727. +
  24728. + dentry = file->f_path.dentry;
  24729. + inode = d_inode(dentry);
  24730. +#if 1
  24731. + mutex_lock(&inode->i_mutex);
  24732. +#else
  24733. + err = mutex_lock_killable(&inode->i_mutex);
  24734. + AuTraceErr(err);
  24735. + if (unlikely(err))
  24736. + goto out;
  24737. +#endif
  24738. +
  24739. + arg.sb = inode->i_sb;
  24740. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  24741. + if (unlikely(err))
  24742. + goto out_mtx;
  24743. + err = au_alive_dir(dentry);
  24744. + if (unlikely(err))
  24745. + goto out_si;
  24746. + /* todo: reval? */
  24747. + fi_read_lock(file);
  24748. +
  24749. + err = -EAGAIN;
  24750. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  24751. + && cookie->generation != au_figen(file)))
  24752. + goto out_unlock;
  24753. +
  24754. + err = 0;
  24755. + if (!rdu->blk) {
  24756. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  24757. + if (!rdu->blk)
  24758. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  24759. + }
  24760. + bend = au_fbstart(file);
  24761. + if (cookie->bindex < bend)
  24762. + cookie->bindex = bend;
  24763. + bend = au_fbend_dir(file);
  24764. + /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
  24765. + for (; !err && cookie->bindex <= bend;
  24766. + cookie->bindex++, cookie->h_pos = 0) {
  24767. + h_file = au_hf_dir(file, cookie->bindex);
  24768. + if (!h_file)
  24769. + continue;
  24770. +
  24771. + au_fclr_rdu(cookie->flags, FULL);
  24772. + err = au_rdu_do(h_file, &arg);
  24773. + AuTraceErr(err);
  24774. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  24775. + break;
  24776. + }
  24777. + AuDbg("rent %llu\n", rdu->rent);
  24778. +
  24779. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  24780. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  24781. + au_fset_rdu(cookie->flags, CONT);
  24782. + cookie->generation = au_figen(file);
  24783. + }
  24784. +
  24785. + ii_read_lock_child(inode);
  24786. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
  24787. + ii_read_unlock(inode);
  24788. +
  24789. +out_unlock:
  24790. + fi_read_unlock(file);
  24791. +out_si:
  24792. + si_read_unlock(arg.sb);
  24793. +out_mtx:
  24794. + mutex_unlock(&inode->i_mutex);
  24795. +out:
  24796. + AuTraceErr(err);
  24797. + return err;
  24798. +}
  24799. +
  24800. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  24801. +{
  24802. + int err;
  24803. + ino_t ino;
  24804. + unsigned long long nent;
  24805. + union au_rdu_ent_ul *u;
  24806. + struct au_rdu_ent ent;
  24807. + struct super_block *sb;
  24808. +
  24809. + err = 0;
  24810. + nent = rdu->nent;
  24811. + u = &rdu->ent;
  24812. + sb = file->f_path.dentry->d_sb;
  24813. + si_read_lock(sb, AuLock_FLUSH);
  24814. + while (nent-- > 0) {
  24815. + /* unnecessary to support mmap_sem since this is a dir */
  24816. + err = copy_from_user(&ent, u->e, sizeof(ent));
  24817. + if (!err)
  24818. + err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
  24819. + if (unlikely(err)) {
  24820. + err = -EFAULT;
  24821. + AuTraceErr(err);
  24822. + break;
  24823. + }
  24824. +
  24825. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  24826. + if (!ent.wh)
  24827. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  24828. + else
  24829. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  24830. + &ino);
  24831. + if (unlikely(err)) {
  24832. + AuTraceErr(err);
  24833. + break;
  24834. + }
  24835. +
  24836. + err = __put_user(ino, &u->e->ino);
  24837. + if (unlikely(err)) {
  24838. + err = -EFAULT;
  24839. + AuTraceErr(err);
  24840. + break;
  24841. + }
  24842. + u->ul += au_rdu_len(ent.nlen);
  24843. + }
  24844. + si_read_unlock(sb);
  24845. +
  24846. + return err;
  24847. +}
  24848. +
  24849. +/* ---------------------------------------------------------------------- */
  24850. +
  24851. +static int au_rdu_verify(struct aufs_rdu *rdu)
  24852. +{
  24853. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  24854. + "%llu, b%d, 0x%x, g%u}\n",
  24855. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  24856. + rdu->blk,
  24857. + rdu->rent, rdu->shwh, rdu->full,
  24858. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  24859. + rdu->cookie.generation);
  24860. +
  24861. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  24862. + return 0;
  24863. +
  24864. + AuDbg("%u:%u\n",
  24865. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  24866. + return -EINVAL;
  24867. +}
  24868. +
  24869. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  24870. +{
  24871. + long err, e;
  24872. + struct aufs_rdu rdu;
  24873. + void __user *p = (void __user *)arg;
  24874. +
  24875. + err = copy_from_user(&rdu, p, sizeof(rdu));
  24876. + if (unlikely(err)) {
  24877. + err = -EFAULT;
  24878. + AuTraceErr(err);
  24879. + goto out;
  24880. + }
  24881. + err = au_rdu_verify(&rdu);
  24882. + if (unlikely(err))
  24883. + goto out;
  24884. +
  24885. + switch (cmd) {
  24886. + case AUFS_CTL_RDU:
  24887. + err = au_rdu(file, &rdu);
  24888. + if (unlikely(err))
  24889. + break;
  24890. +
  24891. + e = copy_to_user(p, &rdu, sizeof(rdu));
  24892. + if (unlikely(e)) {
  24893. + err = -EFAULT;
  24894. + AuTraceErr(err);
  24895. + }
  24896. + break;
  24897. + case AUFS_CTL_RDU_INO:
  24898. + err = au_rdu_ino(file, &rdu);
  24899. + break;
  24900. +
  24901. + default:
  24902. + /* err = -ENOTTY; */
  24903. + err = -EINVAL;
  24904. + }
  24905. +
  24906. +out:
  24907. + AuTraceErr(err);
  24908. + return err;
  24909. +}
  24910. +
  24911. +#ifdef CONFIG_COMPAT
  24912. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  24913. +{
  24914. + long err, e;
  24915. + struct aufs_rdu rdu;
  24916. + void __user *p = compat_ptr(arg);
  24917. +
  24918. + /* todo: get_user()? */
  24919. + err = copy_from_user(&rdu, p, sizeof(rdu));
  24920. + if (unlikely(err)) {
  24921. + err = -EFAULT;
  24922. + AuTraceErr(err);
  24923. + goto out;
  24924. + }
  24925. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  24926. + err = au_rdu_verify(&rdu);
  24927. + if (unlikely(err))
  24928. + goto out;
  24929. +
  24930. + switch (cmd) {
  24931. + case AUFS_CTL_RDU:
  24932. + err = au_rdu(file, &rdu);
  24933. + if (unlikely(err))
  24934. + break;
  24935. +
  24936. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  24937. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  24938. + e = copy_to_user(p, &rdu, sizeof(rdu));
  24939. + if (unlikely(e)) {
  24940. + err = -EFAULT;
  24941. + AuTraceErr(err);
  24942. + }
  24943. + break;
  24944. + case AUFS_CTL_RDU_INO:
  24945. + err = au_rdu_ino(file, &rdu);
  24946. + break;
  24947. +
  24948. + default:
  24949. + /* err = -ENOTTY; */
  24950. + err = -EINVAL;
  24951. + }
  24952. +
  24953. +out:
  24954. + AuTraceErr(err);
  24955. + return err;
  24956. +}
  24957. +#endif
  24958. diff -Nur linux-4.1.10.orig/fs/aufs/rwsem.h linux-4.1.10/fs/aufs/rwsem.h
  24959. --- linux-4.1.10.orig/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
  24960. +++ linux-4.1.10/fs/aufs/rwsem.h 2015-10-22 21:35:53.000000000 +0200
  24961. @@ -0,0 +1,191 @@
  24962. +/*
  24963. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  24964. + *
  24965. + * This program, aufs is free software; you can redistribute it and/or modify
  24966. + * it under the terms of the GNU General Public License as published by
  24967. + * the Free Software Foundation; either version 2 of the License, or
  24968. + * (at your option) any later version.
  24969. + *
  24970. + * This program is distributed in the hope that it will be useful,
  24971. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24972. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24973. + * GNU General Public License for more details.
  24974. + *
  24975. + * You should have received a copy of the GNU General Public License
  24976. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24977. + */
  24978. +
  24979. +/*
  24980. + * simple read-write semaphore wrappers
  24981. + */
  24982. +
  24983. +#ifndef __AUFS_RWSEM_H__
  24984. +#define __AUFS_RWSEM_H__
  24985. +
  24986. +#ifdef __KERNEL__
  24987. +
  24988. +#include "debug.h"
  24989. +
  24990. +struct au_rwsem {
  24991. + struct rw_semaphore rwsem;
  24992. +#ifdef CONFIG_AUFS_DEBUG
  24993. + /* just for debugging, not almighty counter */
  24994. + atomic_t rcnt, wcnt;
  24995. +#endif
  24996. +};
  24997. +
  24998. +#ifdef CONFIG_AUFS_DEBUG
  24999. +#define AuDbgCntInit(rw) do { \
  25000. + atomic_set(&(rw)->rcnt, 0); \
  25001. + atomic_set(&(rw)->wcnt, 0); \
  25002. + smp_mb(); /* atomic set */ \
  25003. +} while (0)
  25004. +
  25005. +#define AuDbgRcntInc(rw) atomic_inc(&(rw)->rcnt)
  25006. +#define AuDbgRcntDec(rw) WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
  25007. +#define AuDbgWcntInc(rw) atomic_inc(&(rw)->wcnt)
  25008. +#define AuDbgWcntDec(rw) WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
  25009. +#else
  25010. +#define AuDbgCntInit(rw) do {} while (0)
  25011. +#define AuDbgRcntInc(rw) do {} while (0)
  25012. +#define AuDbgRcntDec(rw) do {} while (0)
  25013. +#define AuDbgWcntInc(rw) do {} while (0)
  25014. +#define AuDbgWcntDec(rw) do {} while (0)
  25015. +#endif /* CONFIG_AUFS_DEBUG */
  25016. +
  25017. +/* to debug easier, do not make them inlined functions */
  25018. +#define AuRwMustNoWaiters(rw) AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
  25019. +/* rwsem_is_locked() is unusable */
  25020. +#define AuRwMustReadLock(rw) AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
  25021. +#define AuRwMustWriteLock(rw) AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
  25022. +#define AuRwMustAnyLock(rw) AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
  25023. + && atomic_read(&(rw)->wcnt) <= 0)
  25024. +#define AuRwDestroy(rw) AuDebugOn(atomic_read(&(rw)->rcnt) \
  25025. + || atomic_read(&(rw)->wcnt))
  25026. +
  25027. +#define au_rw_class(rw, key) lockdep_set_class(&(rw)->rwsem, key)
  25028. +
  25029. +static inline void au_rw_init(struct au_rwsem *rw)
  25030. +{
  25031. + AuDbgCntInit(rw);
  25032. + init_rwsem(&rw->rwsem);
  25033. +}
  25034. +
  25035. +static inline void au_rw_init_wlock(struct au_rwsem *rw)
  25036. +{
  25037. + au_rw_init(rw);
  25038. + down_write(&rw->rwsem);
  25039. + AuDbgWcntInc(rw);
  25040. +}
  25041. +
  25042. +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
  25043. + unsigned int lsc)
  25044. +{
  25045. + au_rw_init(rw);
  25046. + down_write_nested(&rw->rwsem, lsc);
  25047. + AuDbgWcntInc(rw);
  25048. +}
  25049. +
  25050. +static inline void au_rw_read_lock(struct au_rwsem *rw)
  25051. +{
  25052. + down_read(&rw->rwsem);
  25053. + AuDbgRcntInc(rw);
  25054. +}
  25055. +
  25056. +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
  25057. +{
  25058. + down_read_nested(&rw->rwsem, lsc);
  25059. + AuDbgRcntInc(rw);
  25060. +}
  25061. +
  25062. +static inline void au_rw_read_unlock(struct au_rwsem *rw)
  25063. +{
  25064. + AuRwMustReadLock(rw);
  25065. + AuDbgRcntDec(rw);
  25066. + up_read(&rw->rwsem);
  25067. +}
  25068. +
  25069. +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
  25070. +{
  25071. + AuRwMustWriteLock(rw);
  25072. + AuDbgRcntInc(rw);
  25073. + AuDbgWcntDec(rw);
  25074. + downgrade_write(&rw->rwsem);
  25075. +}
  25076. +
  25077. +static inline void au_rw_write_lock(struct au_rwsem *rw)
  25078. +{
  25079. + down_write(&rw->rwsem);
  25080. + AuDbgWcntInc(rw);
  25081. +}
  25082. +
  25083. +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
  25084. + unsigned int lsc)
  25085. +{
  25086. + down_write_nested(&rw->rwsem, lsc);
  25087. + AuDbgWcntInc(rw);
  25088. +}
  25089. +
  25090. +static inline void au_rw_write_unlock(struct au_rwsem *rw)
  25091. +{
  25092. + AuRwMustWriteLock(rw);
  25093. + AuDbgWcntDec(rw);
  25094. + up_write(&rw->rwsem);
  25095. +}
  25096. +
  25097. +/* why is not _nested version defined */
  25098. +static inline int au_rw_read_trylock(struct au_rwsem *rw)
  25099. +{
  25100. + int ret;
  25101. +
  25102. + ret = down_read_trylock(&rw->rwsem);
  25103. + if (ret)
  25104. + AuDbgRcntInc(rw);
  25105. + return ret;
  25106. +}
  25107. +
  25108. +static inline int au_rw_write_trylock(struct au_rwsem *rw)
  25109. +{
  25110. + int ret;
  25111. +
  25112. + ret = down_write_trylock(&rw->rwsem);
  25113. + if (ret)
  25114. + AuDbgWcntInc(rw);
  25115. + return ret;
  25116. +}
  25117. +
  25118. +#undef AuDbgCntInit
  25119. +#undef AuDbgRcntInc
  25120. +#undef AuDbgRcntDec
  25121. +#undef AuDbgWcntInc
  25122. +#undef AuDbgWcntDec
  25123. +
  25124. +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  25125. +static inline void prefix##_read_lock(param) \
  25126. +{ au_rw_read_lock(rwsem); } \
  25127. +static inline void prefix##_write_lock(param) \
  25128. +{ au_rw_write_lock(rwsem); } \
  25129. +static inline int prefix##_read_trylock(param) \
  25130. +{ return au_rw_read_trylock(rwsem); } \
  25131. +static inline int prefix##_write_trylock(param) \
  25132. +{ return au_rw_write_trylock(rwsem); }
  25133. +/* why is not _nested version defined */
  25134. +/* static inline void prefix##_read_trylock_nested(param, lsc)
  25135. +{ au_rw_read_trylock_nested(rwsem, lsc)); }
  25136. +static inline void prefix##_write_trylock_nestd(param, lsc)
  25137. +{ au_rw_write_trylock_nested(rwsem, lsc); } */
  25138. +
  25139. +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
  25140. +static inline void prefix##_read_unlock(param) \
  25141. +{ au_rw_read_unlock(rwsem); } \
  25142. +static inline void prefix##_write_unlock(param) \
  25143. +{ au_rw_write_unlock(rwsem); } \
  25144. +static inline void prefix##_downgrade_lock(param) \
  25145. +{ au_rw_dgrade_lock(rwsem); }
  25146. +
  25147. +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
  25148. + AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
  25149. + AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
  25150. +
  25151. +#endif /* __KERNEL__ */
  25152. +#endif /* __AUFS_RWSEM_H__ */
  25153. diff -Nur linux-4.1.10.orig/fs/aufs/sbinfo.c linux-4.1.10/fs/aufs/sbinfo.c
  25154. --- linux-4.1.10.orig/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
  25155. +++ linux-4.1.10/fs/aufs/sbinfo.c 2015-10-22 21:35:53.000000000 +0200
  25156. @@ -0,0 +1,356 @@
  25157. +/*
  25158. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25159. + *
  25160. + * This program, aufs is free software; you can redistribute it and/or modify
  25161. + * it under the terms of the GNU General Public License as published by
  25162. + * the Free Software Foundation; either version 2 of the License, or
  25163. + * (at your option) any later version.
  25164. + *
  25165. + * This program is distributed in the hope that it will be useful,
  25166. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25167. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25168. + * GNU General Public License for more details.
  25169. + *
  25170. + * You should have received a copy of the GNU General Public License
  25171. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25172. + */
  25173. +
  25174. +/*
  25175. + * superblock private data
  25176. + */
  25177. +
  25178. +#include "aufs.h"
  25179. +
  25180. +/*
  25181. + * they are necessary regardless sysfs is disabled.
  25182. + */
  25183. +void au_si_free(struct kobject *kobj)
  25184. +{
  25185. + int i;
  25186. + struct au_sbinfo *sbinfo;
  25187. + char *locked __maybe_unused; /* debug only */
  25188. +
  25189. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  25190. + for (i = 0; i < AuPlink_NHASH; i++)
  25191. + AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
  25192. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  25193. +
  25194. + au_rw_write_lock(&sbinfo->si_rwsem);
  25195. + au_br_free(sbinfo);
  25196. + au_rw_write_unlock(&sbinfo->si_rwsem);
  25197. +
  25198. + AuDebugOn(radix_tree_gang_lookup
  25199. + (&sbinfo->au_si_pid.tree, (void **)&locked,
  25200. + /*first_index*/PID_MAX_DEFAULT - 1,
  25201. + /*max_items*/sizeof(locked)/sizeof(*locked)));
  25202. +
  25203. + kfree(sbinfo->si_branch);
  25204. + kfree(sbinfo->au_si_pid.bitmap);
  25205. + mutex_destroy(&sbinfo->si_xib_mtx);
  25206. + AuRwDestroy(&sbinfo->si_rwsem);
  25207. +
  25208. + kfree(sbinfo);
  25209. +}
  25210. +
  25211. +int au_si_alloc(struct super_block *sb)
  25212. +{
  25213. + int err, i;
  25214. + struct au_sbinfo *sbinfo;
  25215. + static struct lock_class_key aufs_si;
  25216. +
  25217. + err = -ENOMEM;
  25218. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  25219. + if (unlikely(!sbinfo))
  25220. + goto out;
  25221. +
  25222. + BUILD_BUG_ON(sizeof(unsigned long) !=
  25223. + sizeof(*sbinfo->au_si_pid.bitmap));
  25224. + sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
  25225. + sizeof(*sbinfo->au_si_pid.bitmap),
  25226. + GFP_NOFS);
  25227. + if (unlikely(!sbinfo->au_si_pid.bitmap))
  25228. + goto out_sbinfo;
  25229. +
  25230. + /* will be reallocated separately */
  25231. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  25232. + if (unlikely(!sbinfo->si_branch))
  25233. + goto out_pidmap;
  25234. +
  25235. + err = sysaufs_si_init(sbinfo);
  25236. + if (unlikely(err))
  25237. + goto out_br;
  25238. +
  25239. + au_nwt_init(&sbinfo->si_nowait);
  25240. + au_rw_init_wlock(&sbinfo->si_rwsem);
  25241. + au_rw_class(&sbinfo->si_rwsem, &aufs_si);
  25242. + spin_lock_init(&sbinfo->au_si_pid.tree_lock);
  25243. + INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
  25244. +
  25245. + atomic_long_set(&sbinfo->si_ninodes, 0);
  25246. + atomic_long_set(&sbinfo->si_nfiles, 0);
  25247. +
  25248. + sbinfo->si_bend = -1;
  25249. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  25250. +
  25251. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  25252. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  25253. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  25254. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  25255. +
  25256. + au_fhsm_init(sbinfo);
  25257. +
  25258. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  25259. +
  25260. + sbinfo->si_xino_jiffy = jiffies;
  25261. + sbinfo->si_xino_expire
  25262. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  25263. + mutex_init(&sbinfo->si_xib_mtx);
  25264. + sbinfo->si_xino_brid = -1;
  25265. + /* leave si_xib_last_pindex and si_xib_next_bit */
  25266. +
  25267. + au_sphl_init(&sbinfo->si_aopen);
  25268. +
  25269. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  25270. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  25271. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  25272. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  25273. +
  25274. + for (i = 0; i < AuPlink_NHASH; i++)
  25275. + au_sphl_init(sbinfo->si_plink + i);
  25276. + init_waitqueue_head(&sbinfo->si_plink_wq);
  25277. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  25278. +
  25279. + au_sphl_init(&sbinfo->si_files);
  25280. +
  25281. + /* leave other members for sysaufs and si_mnt. */
  25282. + sbinfo->si_sb = sb;
  25283. + sb->s_fs_info = sbinfo;
  25284. + si_pid_set(sb);
  25285. + return 0; /* success */
  25286. +
  25287. +out_br:
  25288. + kfree(sbinfo->si_branch);
  25289. +out_pidmap:
  25290. + kfree(sbinfo->au_si_pid.bitmap);
  25291. +out_sbinfo:
  25292. + kfree(sbinfo);
  25293. +out:
  25294. + return err;
  25295. +}
  25296. +
  25297. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
  25298. +{
  25299. + int err, sz;
  25300. + struct au_branch **brp;
  25301. +
  25302. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  25303. +
  25304. + err = -ENOMEM;
  25305. + sz = sizeof(*brp) * (sbinfo->si_bend + 1);
  25306. + if (unlikely(!sz))
  25307. + sz = sizeof(*brp);
  25308. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
  25309. + if (brp) {
  25310. + sbinfo->si_branch = brp;
  25311. + err = 0;
  25312. + }
  25313. +
  25314. + return err;
  25315. +}
  25316. +
  25317. +/* ---------------------------------------------------------------------- */
  25318. +
  25319. +unsigned int au_sigen_inc(struct super_block *sb)
  25320. +{
  25321. + unsigned int gen;
  25322. + struct inode *inode;
  25323. +
  25324. + SiMustWriteLock(sb);
  25325. +
  25326. + gen = ++au_sbi(sb)->si_generation;
  25327. + au_update_digen(sb->s_root);
  25328. + inode = d_inode(sb->s_root);
  25329. + au_update_iigen(inode, /*half*/0);
  25330. + inode->i_version++;
  25331. + return gen;
  25332. +}
  25333. +
  25334. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  25335. +{
  25336. + aufs_bindex_t br_id;
  25337. + int i;
  25338. + struct au_sbinfo *sbinfo;
  25339. +
  25340. + SiMustWriteLock(sb);
  25341. +
  25342. + sbinfo = au_sbi(sb);
  25343. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  25344. + br_id = ++sbinfo->si_last_br_id;
  25345. + AuDebugOn(br_id < 0);
  25346. + if (br_id && au_br_index(sb, br_id) < 0)
  25347. + return br_id;
  25348. + }
  25349. +
  25350. + return -1;
  25351. +}
  25352. +
  25353. +/* ---------------------------------------------------------------------- */
  25354. +
  25355. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  25356. +int si_read_lock(struct super_block *sb, int flags)
  25357. +{
  25358. + int err;
  25359. +
  25360. + err = 0;
  25361. + if (au_ftest_lock(flags, FLUSH))
  25362. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25363. +
  25364. + si_noflush_read_lock(sb);
  25365. + err = au_plink_maint(sb, flags);
  25366. + if (unlikely(err))
  25367. + si_read_unlock(sb);
  25368. +
  25369. + return err;
  25370. +}
  25371. +
  25372. +int si_write_lock(struct super_block *sb, int flags)
  25373. +{
  25374. + int err;
  25375. +
  25376. + if (au_ftest_lock(flags, FLUSH))
  25377. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  25378. +
  25379. + si_noflush_write_lock(sb);
  25380. + err = au_plink_maint(sb, flags);
  25381. + if (unlikely(err))
  25382. + si_write_unlock(sb);
  25383. +
  25384. + return err;
  25385. +}
  25386. +
  25387. +/* dentry and super_block lock. call at entry point */
  25388. +int aufs_read_lock(struct dentry *dentry, int flags)
  25389. +{
  25390. + int err;
  25391. + struct super_block *sb;
  25392. +
  25393. + sb = dentry->d_sb;
  25394. + err = si_read_lock(sb, flags);
  25395. + if (unlikely(err))
  25396. + goto out;
  25397. +
  25398. + if (au_ftest_lock(flags, DW))
  25399. + di_write_lock_child(dentry);
  25400. + else
  25401. + di_read_lock_child(dentry, flags);
  25402. +
  25403. + if (au_ftest_lock(flags, GEN)) {
  25404. + err = au_digen_test(dentry, au_sigen(sb));
  25405. + AuDebugOn(!err && au_dbrange_test(dentry));
  25406. + if (unlikely(err))
  25407. + aufs_read_unlock(dentry, flags);
  25408. + }
  25409. +
  25410. +out:
  25411. + return err;
  25412. +}
  25413. +
  25414. +void aufs_read_unlock(struct dentry *dentry, int flags)
  25415. +{
  25416. + if (au_ftest_lock(flags, DW))
  25417. + di_write_unlock(dentry);
  25418. + else
  25419. + di_read_unlock(dentry, flags);
  25420. + si_read_unlock(dentry->d_sb);
  25421. +}
  25422. +
  25423. +void aufs_write_lock(struct dentry *dentry)
  25424. +{
  25425. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  25426. + di_write_lock_child(dentry);
  25427. +}
  25428. +
  25429. +void aufs_write_unlock(struct dentry *dentry)
  25430. +{
  25431. + di_write_unlock(dentry);
  25432. + si_write_unlock(dentry->d_sb);
  25433. +}
  25434. +
  25435. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  25436. +{
  25437. + int err;
  25438. + unsigned int sigen;
  25439. + struct super_block *sb;
  25440. +
  25441. + sb = d1->d_sb;
  25442. + err = si_read_lock(sb, flags);
  25443. + if (unlikely(err))
  25444. + goto out;
  25445. +
  25446. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
  25447. +
  25448. + if (au_ftest_lock(flags, GEN)) {
  25449. + sigen = au_sigen(sb);
  25450. + err = au_digen_test(d1, sigen);
  25451. + AuDebugOn(!err && au_dbrange_test(d1));
  25452. + if (!err) {
  25453. + err = au_digen_test(d2, sigen);
  25454. + AuDebugOn(!err && au_dbrange_test(d2));
  25455. + }
  25456. + if (unlikely(err))
  25457. + aufs_read_and_write_unlock2(d1, d2);
  25458. + }
  25459. +
  25460. +out:
  25461. + return err;
  25462. +}
  25463. +
  25464. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  25465. +{
  25466. + di_write_unlock2(d1, d2);
  25467. + si_read_unlock(d1->d_sb);
  25468. +}
  25469. +
  25470. +/* ---------------------------------------------------------------------- */
  25471. +
  25472. +int si_pid_test_slow(struct super_block *sb)
  25473. +{
  25474. + void *p;
  25475. +
  25476. + rcu_read_lock();
  25477. + p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
  25478. + rcu_read_unlock();
  25479. +
  25480. + return (long)!!p;
  25481. +}
  25482. +
  25483. +void si_pid_set_slow(struct super_block *sb)
  25484. +{
  25485. + int err;
  25486. + struct au_sbinfo *sbinfo;
  25487. +
  25488. + AuDebugOn(si_pid_test_slow(sb));
  25489. +
  25490. + sbinfo = au_sbi(sb);
  25491. + err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  25492. + AuDebugOn(err);
  25493. + spin_lock(&sbinfo->au_si_pid.tree_lock);
  25494. + err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
  25495. + /*any valid ptr*/sb);
  25496. + spin_unlock(&sbinfo->au_si_pid.tree_lock);
  25497. + AuDebugOn(err);
  25498. + radix_tree_preload_end();
  25499. +}
  25500. +
  25501. +void si_pid_clr_slow(struct super_block *sb)
  25502. +{
  25503. + void *p;
  25504. + struct au_sbinfo *sbinfo;
  25505. +
  25506. + AuDebugOn(!si_pid_test_slow(sb));
  25507. +
  25508. + sbinfo = au_sbi(sb);
  25509. + spin_lock(&sbinfo->au_si_pid.tree_lock);
  25510. + p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
  25511. + spin_unlock(&sbinfo->au_si_pid.tree_lock);
  25512. +}
  25513. diff -Nur linux-4.1.10.orig/fs/aufs/spl.h linux-4.1.10/fs/aufs/spl.h
  25514. --- linux-4.1.10.orig/fs/aufs/spl.h 1970-01-01 01:00:00.000000000 +0100
  25515. +++ linux-4.1.10/fs/aufs/spl.h 2015-10-22 21:35:53.000000000 +0200
  25516. @@ -0,0 +1,111 @@
  25517. +/*
  25518. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25519. + *
  25520. + * This program, aufs is free software; you can redistribute it and/or modify
  25521. + * it under the terms of the GNU General Public License as published by
  25522. + * the Free Software Foundation; either version 2 of the License, or
  25523. + * (at your option) any later version.
  25524. + *
  25525. + * This program is distributed in the hope that it will be useful,
  25526. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25527. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25528. + * GNU General Public License for more details.
  25529. + *
  25530. + * You should have received a copy of the GNU General Public License
  25531. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25532. + */
  25533. +
  25534. +/*
  25535. + * simple list protected by a spinlock
  25536. + */
  25537. +
  25538. +#ifndef __AUFS_SPL_H__
  25539. +#define __AUFS_SPL_H__
  25540. +
  25541. +#ifdef __KERNEL__
  25542. +
  25543. +struct au_splhead {
  25544. + spinlock_t spin;
  25545. + struct list_head head;
  25546. +};
  25547. +
  25548. +static inline void au_spl_init(struct au_splhead *spl)
  25549. +{
  25550. + spin_lock_init(&spl->spin);
  25551. + INIT_LIST_HEAD(&spl->head);
  25552. +}
  25553. +
  25554. +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
  25555. +{
  25556. + spin_lock(&spl->spin);
  25557. + list_add(list, &spl->head);
  25558. + spin_unlock(&spl->spin);
  25559. +}
  25560. +
  25561. +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
  25562. +{
  25563. + spin_lock(&spl->spin);
  25564. + list_del(list);
  25565. + spin_unlock(&spl->spin);
  25566. +}
  25567. +
  25568. +static inline void au_spl_del_rcu(struct list_head *list,
  25569. + struct au_splhead *spl)
  25570. +{
  25571. + spin_lock(&spl->spin);
  25572. + list_del_rcu(list);
  25573. + spin_unlock(&spl->spin);
  25574. +}
  25575. +
  25576. +/* ---------------------------------------------------------------------- */
  25577. +
  25578. +struct au_sphlhead {
  25579. + spinlock_t spin;
  25580. + struct hlist_head head;
  25581. +};
  25582. +
  25583. +static inline void au_sphl_init(struct au_sphlhead *sphl)
  25584. +{
  25585. + spin_lock_init(&sphl->spin);
  25586. + INIT_HLIST_HEAD(&sphl->head);
  25587. +}
  25588. +
  25589. +static inline void au_sphl_add(struct hlist_node *hlist,
  25590. + struct au_sphlhead *sphl)
  25591. +{
  25592. + spin_lock(&sphl->spin);
  25593. + hlist_add_head(hlist, &sphl->head);
  25594. + spin_unlock(&sphl->spin);
  25595. +}
  25596. +
  25597. +static inline void au_sphl_del(struct hlist_node *hlist,
  25598. + struct au_sphlhead *sphl)
  25599. +{
  25600. + spin_lock(&sphl->spin);
  25601. + hlist_del(hlist);
  25602. + spin_unlock(&sphl->spin);
  25603. +}
  25604. +
  25605. +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
  25606. + struct au_sphlhead *sphl)
  25607. +{
  25608. + spin_lock(&sphl->spin);
  25609. + hlist_del_rcu(hlist);
  25610. + spin_unlock(&sphl->spin);
  25611. +}
  25612. +
  25613. +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
  25614. +{
  25615. + unsigned long cnt;
  25616. + struct hlist_node *pos;
  25617. +
  25618. + cnt = 0;
  25619. + spin_lock(&sphl->spin);
  25620. + hlist_for_each(pos, &sphl->head)
  25621. + cnt++;
  25622. + spin_unlock(&sphl->spin);
  25623. + return cnt;
  25624. +}
  25625. +
  25626. +#endif /* __KERNEL__ */
  25627. +#endif /* __AUFS_SPL_H__ */
  25628. diff -Nur linux-4.1.10.orig/fs/aufs/super.c linux-4.1.10/fs/aufs/super.c
  25629. --- linux-4.1.10.orig/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
  25630. +++ linux-4.1.10/fs/aufs/super.c 2015-10-22 21:35:53.000000000 +0200
  25631. @@ -0,0 +1,1004 @@
  25632. +/*
  25633. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  25634. + *
  25635. + * This program, aufs is free software; you can redistribute it and/or modify
  25636. + * it under the terms of the GNU General Public License as published by
  25637. + * the Free Software Foundation; either version 2 of the License, or
  25638. + * (at your option) any later version.
  25639. + *
  25640. + * This program is distributed in the hope that it will be useful,
  25641. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25642. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25643. + * GNU General Public License for more details.
  25644. + *
  25645. + * You should have received a copy of the GNU General Public License
  25646. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25647. + */
  25648. +
  25649. +/*
  25650. + * mount and super_block operations
  25651. + */
  25652. +
  25653. +#include <linux/mm.h>
  25654. +#include <linux/seq_file.h>
  25655. +#include <linux/statfs.h>
  25656. +#include <linux/vmalloc.h>
  25657. +#include "aufs.h"
  25658. +
  25659. +/*
  25660. + * super_operations
  25661. + */
  25662. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  25663. +{
  25664. + struct au_icntnr *c;
  25665. +
  25666. + c = au_cache_alloc_icntnr();
  25667. + if (c) {
  25668. + au_icntnr_init(c);
  25669. + c->vfs_inode.i_version = 1; /* sigen(sb); */
  25670. + c->iinfo.ii_hinode = NULL;
  25671. + return &c->vfs_inode;
  25672. + }
  25673. + return NULL;
  25674. +}
  25675. +
  25676. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  25677. +{
  25678. + struct inode *inode = container_of(head, struct inode, i_rcu);
  25679. +
  25680. + INIT_HLIST_HEAD(&inode->i_dentry);
  25681. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  25682. +}
  25683. +
  25684. +static void aufs_destroy_inode(struct inode *inode)
  25685. +{
  25686. + au_iinfo_fin(inode);
  25687. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  25688. +}
  25689. +
  25690. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  25691. +{
  25692. + struct inode *inode;
  25693. + int err;
  25694. +
  25695. + inode = iget_locked(sb, ino);
  25696. + if (unlikely(!inode)) {
  25697. + inode = ERR_PTR(-ENOMEM);
  25698. + goto out;
  25699. + }
  25700. + if (!(inode->i_state & I_NEW))
  25701. + goto out;
  25702. +
  25703. + err = au_xigen_new(inode);
  25704. + if (!err)
  25705. + err = au_iinfo_init(inode);
  25706. + if (!err)
  25707. + inode->i_version++;
  25708. + else {
  25709. + iget_failed(inode);
  25710. + inode = ERR_PTR(err);
  25711. + }
  25712. +
  25713. +out:
  25714. + /* never return NULL */
  25715. + AuDebugOn(!inode);
  25716. + AuTraceErrPtr(inode);
  25717. + return inode;
  25718. +}
  25719. +
  25720. +/* lock free root dinfo */
  25721. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  25722. +{
  25723. + int err;
  25724. + aufs_bindex_t bindex, bend;
  25725. + struct path path;
  25726. + struct au_hdentry *hdp;
  25727. + struct au_branch *br;
  25728. + au_br_perm_str_t perm;
  25729. +
  25730. + err = 0;
  25731. + bend = au_sbend(sb);
  25732. + hdp = au_di(sb->s_root)->di_hdentry;
  25733. + for (bindex = 0; !err && bindex <= bend; bindex++) {
  25734. + br = au_sbr(sb, bindex);
  25735. + path.mnt = au_br_mnt(br);
  25736. + path.dentry = hdp[bindex].hd_dentry;
  25737. + err = au_seq_path(seq, &path);
  25738. + if (!err) {
  25739. + au_optstr_br_perm(&perm, br->br_perm);
  25740. + err = seq_printf(seq, "=%s", perm.a);
  25741. + if (err == -1)
  25742. + err = -E2BIG;
  25743. + }
  25744. + if (!err && bindex != bend)
  25745. + err = seq_putc(seq, ':');
  25746. + }
  25747. +
  25748. + return err;
  25749. +}
  25750. +
  25751. +static void au_show_wbr_create(struct seq_file *m, int v,
  25752. + struct au_sbinfo *sbinfo)
  25753. +{
  25754. + const char *pat;
  25755. +
  25756. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  25757. +
  25758. + seq_puts(m, ",create=");
  25759. + pat = au_optstr_wbr_create(v);
  25760. + switch (v) {
  25761. + case AuWbrCreate_TDP:
  25762. + case AuWbrCreate_RR:
  25763. + case AuWbrCreate_MFS:
  25764. + case AuWbrCreate_PMFS:
  25765. + seq_puts(m, pat);
  25766. + break;
  25767. + case AuWbrCreate_MFSV:
  25768. + seq_printf(m, /*pat*/"mfs:%lu",
  25769. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  25770. + / MSEC_PER_SEC);
  25771. + break;
  25772. + case AuWbrCreate_PMFSV:
  25773. + seq_printf(m, /*pat*/"pmfs:%lu",
  25774. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  25775. + / MSEC_PER_SEC);
  25776. + break;
  25777. + case AuWbrCreate_MFSRR:
  25778. + seq_printf(m, /*pat*/"mfsrr:%llu",
  25779. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  25780. + break;
  25781. + case AuWbrCreate_MFSRRV:
  25782. + seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
  25783. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  25784. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  25785. + / MSEC_PER_SEC);
  25786. + break;
  25787. + case AuWbrCreate_PMFSRR:
  25788. + seq_printf(m, /*pat*/"pmfsrr:%llu",
  25789. + sbinfo->si_wbr_mfs.mfsrr_watermark);
  25790. + break;
  25791. + case AuWbrCreate_PMFSRRV:
  25792. + seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
  25793. + sbinfo->si_wbr_mfs.mfsrr_watermark,
  25794. + jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
  25795. + / MSEC_PER_SEC);
  25796. + break;
  25797. + }
  25798. +}
  25799. +
  25800. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  25801. +{
  25802. +#ifdef CONFIG_SYSFS
  25803. + return 0;
  25804. +#else
  25805. + int err;
  25806. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  25807. + aufs_bindex_t bindex, brid;
  25808. + struct qstr *name;
  25809. + struct file *f;
  25810. + struct dentry *d, *h_root;
  25811. + struct au_hdentry *hdp;
  25812. +
  25813. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  25814. +
  25815. + err = 0;
  25816. + f = au_sbi(sb)->si_xib;
  25817. + if (!f)
  25818. + goto out;
  25819. +
  25820. + /* stop printing the default xino path on the first writable branch */
  25821. + h_root = NULL;
  25822. + brid = au_xino_brid(sb);
  25823. + if (brid >= 0) {
  25824. + bindex = au_br_index(sb, brid);
  25825. + hdp = au_di(sb->s_root)->di_hdentry;
  25826. + h_root = hdp[0 + bindex].hd_dentry;
  25827. + }
  25828. + d = f->f_path.dentry;
  25829. + name = &d->d_name;
  25830. + /* safe ->d_parent because the file is unlinked */
  25831. + if (d->d_parent == h_root
  25832. + && name->len == len
  25833. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  25834. + goto out;
  25835. +
  25836. + seq_puts(seq, ",xino=");
  25837. + err = au_xino_path(seq, f);
  25838. +
  25839. +out:
  25840. + return err;
  25841. +#endif
  25842. +}
  25843. +
  25844. +/* seq_file will re-call me in case of too long string */
  25845. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  25846. +{
  25847. + int err;
  25848. + unsigned int mnt_flags, v;
  25849. + struct super_block *sb;
  25850. + struct au_sbinfo *sbinfo;
  25851. +
  25852. +#define AuBool(name, str) do { \
  25853. + v = au_opt_test(mnt_flags, name); \
  25854. + if (v != au_opt_test(AuOpt_Def, name)) \
  25855. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  25856. +} while (0)
  25857. +
  25858. +#define AuStr(name, str) do { \
  25859. + v = mnt_flags & AuOptMask_##name; \
  25860. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  25861. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  25862. +} while (0)
  25863. +
  25864. +#define AuUInt(name, str, val) do { \
  25865. + if (val != AUFS_##name##_DEF) \
  25866. + seq_printf(m, "," #str "=%u", val); \
  25867. +} while (0)
  25868. +
  25869. + sb = dentry->d_sb;
  25870. + if (sb->s_flags & MS_POSIXACL)
  25871. + seq_puts(m, ",acl");
  25872. +
  25873. + /* lock free root dinfo */
  25874. + si_noflush_read_lock(sb);
  25875. + sbinfo = au_sbi(sb);
  25876. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  25877. +
  25878. + mnt_flags = au_mntflags(sb);
  25879. + if (au_opt_test(mnt_flags, XINO)) {
  25880. + err = au_show_xino(m, sb);
  25881. + if (unlikely(err))
  25882. + goto out;
  25883. + } else
  25884. + seq_puts(m, ",noxino");
  25885. +
  25886. + AuBool(TRUNC_XINO, trunc_xino);
  25887. + AuStr(UDBA, udba);
  25888. + AuBool(SHWH, shwh);
  25889. + AuBool(PLINK, plink);
  25890. + AuBool(DIO, dio);
  25891. + AuBool(DIRPERM1, dirperm1);
  25892. +
  25893. + v = sbinfo->si_wbr_create;
  25894. + if (v != AuWbrCreate_Def)
  25895. + au_show_wbr_create(m, v, sbinfo);
  25896. +
  25897. + v = sbinfo->si_wbr_copyup;
  25898. + if (v != AuWbrCopyup_Def)
  25899. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  25900. +
  25901. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  25902. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  25903. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  25904. +
  25905. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  25906. +
  25907. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  25908. + AuUInt(RDCACHE, rdcache, v);
  25909. +
  25910. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  25911. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  25912. +
  25913. + au_fhsm_show(m, sbinfo);
  25914. +
  25915. + AuBool(SUM, sum);
  25916. + /* AuBool(SUM_W, wsum); */
  25917. + AuBool(WARN_PERM, warn_perm);
  25918. + AuBool(VERBOSE, verbose);
  25919. +
  25920. +out:
  25921. + /* be sure to print "br:" last */
  25922. + if (!sysaufs_brs) {
  25923. + seq_puts(m, ",br:");
  25924. + au_show_brs(m, sb);
  25925. + }
  25926. + si_read_unlock(sb);
  25927. + return 0;
  25928. +
  25929. +#undef AuBool
  25930. +#undef AuStr
  25931. +#undef AuUInt
  25932. +}
  25933. +
  25934. +/* ---------------------------------------------------------------------- */
  25935. +
  25936. +/* sum mode which returns the summation for statfs(2) */
  25937. +
  25938. +static u64 au_add_till_max(u64 a, u64 b)
  25939. +{
  25940. + u64 old;
  25941. +
  25942. + old = a;
  25943. + a += b;
  25944. + if (old <= a)
  25945. + return a;
  25946. + return ULLONG_MAX;
  25947. +}
  25948. +
  25949. +static u64 au_mul_till_max(u64 a, long mul)
  25950. +{
  25951. + u64 old;
  25952. +
  25953. + old = a;
  25954. + a *= mul;
  25955. + if (old <= a)
  25956. + return a;
  25957. + return ULLONG_MAX;
  25958. +}
  25959. +
  25960. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  25961. +{
  25962. + int err;
  25963. + long bsize, factor;
  25964. + u64 blocks, bfree, bavail, files, ffree;
  25965. + aufs_bindex_t bend, bindex, i;
  25966. + unsigned char shared;
  25967. + struct path h_path;
  25968. + struct super_block *h_sb;
  25969. +
  25970. + err = 0;
  25971. + bsize = LONG_MAX;
  25972. + files = 0;
  25973. + ffree = 0;
  25974. + blocks = 0;
  25975. + bfree = 0;
  25976. + bavail = 0;
  25977. + bend = au_sbend(sb);
  25978. + for (bindex = 0; bindex <= bend; bindex++) {
  25979. + h_path.mnt = au_sbr_mnt(sb, bindex);
  25980. + h_sb = h_path.mnt->mnt_sb;
  25981. + shared = 0;
  25982. + for (i = 0; !shared && i < bindex; i++)
  25983. + shared = (au_sbr_sb(sb, i) == h_sb);
  25984. + if (shared)
  25985. + continue;
  25986. +
  25987. + /* sb->s_root for NFS is unreliable */
  25988. + h_path.dentry = h_path.mnt->mnt_root;
  25989. + err = vfs_statfs(&h_path, buf);
  25990. + if (unlikely(err))
  25991. + goto out;
  25992. +
  25993. + if (bsize > buf->f_bsize) {
  25994. + /*
  25995. + * we will reduce bsize, so we have to expand blocks
  25996. + * etc. to match them again
  25997. + */
  25998. + factor = (bsize / buf->f_bsize);
  25999. + blocks = au_mul_till_max(blocks, factor);
  26000. + bfree = au_mul_till_max(bfree, factor);
  26001. + bavail = au_mul_till_max(bavail, factor);
  26002. + bsize = buf->f_bsize;
  26003. + }
  26004. +
  26005. + factor = (buf->f_bsize / bsize);
  26006. + blocks = au_add_till_max(blocks,
  26007. + au_mul_till_max(buf->f_blocks, factor));
  26008. + bfree = au_add_till_max(bfree,
  26009. + au_mul_till_max(buf->f_bfree, factor));
  26010. + bavail = au_add_till_max(bavail,
  26011. + au_mul_till_max(buf->f_bavail, factor));
  26012. + files = au_add_till_max(files, buf->f_files);
  26013. + ffree = au_add_till_max(ffree, buf->f_ffree);
  26014. + }
  26015. +
  26016. + buf->f_bsize = bsize;
  26017. + buf->f_blocks = blocks;
  26018. + buf->f_bfree = bfree;
  26019. + buf->f_bavail = bavail;
  26020. + buf->f_files = files;
  26021. + buf->f_ffree = ffree;
  26022. + buf->f_frsize = 0;
  26023. +
  26024. +out:
  26025. + return err;
  26026. +}
  26027. +
  26028. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  26029. +{
  26030. + int err;
  26031. + struct path h_path;
  26032. + struct super_block *sb;
  26033. +
  26034. + /* lock free root dinfo */
  26035. + sb = dentry->d_sb;
  26036. + si_noflush_read_lock(sb);
  26037. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  26038. + /* sb->s_root for NFS is unreliable */
  26039. + h_path.mnt = au_sbr_mnt(sb, 0);
  26040. + h_path.dentry = h_path.mnt->mnt_root;
  26041. + err = vfs_statfs(&h_path, buf);
  26042. + } else
  26043. + err = au_statfs_sum(sb, buf);
  26044. + si_read_unlock(sb);
  26045. +
  26046. + if (!err) {
  26047. + buf->f_type = AUFS_SUPER_MAGIC;
  26048. + buf->f_namelen = AUFS_MAX_NAMELEN;
  26049. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  26050. + }
  26051. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  26052. +
  26053. + return err;
  26054. +}
  26055. +
  26056. +/* ---------------------------------------------------------------------- */
  26057. +
  26058. +static int aufs_sync_fs(struct super_block *sb, int wait)
  26059. +{
  26060. + int err, e;
  26061. + aufs_bindex_t bend, bindex;
  26062. + struct au_branch *br;
  26063. + struct super_block *h_sb;
  26064. +
  26065. + err = 0;
  26066. + si_noflush_read_lock(sb);
  26067. + bend = au_sbend(sb);
  26068. + for (bindex = 0; bindex <= bend; bindex++) {
  26069. + br = au_sbr(sb, bindex);
  26070. + if (!au_br_writable(br->br_perm))
  26071. + continue;
  26072. +
  26073. + h_sb = au_sbr_sb(sb, bindex);
  26074. + if (h_sb->s_op->sync_fs) {
  26075. + e = h_sb->s_op->sync_fs(h_sb, wait);
  26076. + if (unlikely(e && !err))
  26077. + err = e;
  26078. + /* go on even if an error happens */
  26079. + }
  26080. + }
  26081. + si_read_unlock(sb);
  26082. +
  26083. + return err;
  26084. +}
  26085. +
  26086. +/* ---------------------------------------------------------------------- */
  26087. +
  26088. +/* final actions when unmounting a file system */
  26089. +static void aufs_put_super(struct super_block *sb)
  26090. +{
  26091. + struct au_sbinfo *sbinfo;
  26092. +
  26093. + sbinfo = au_sbi(sb);
  26094. + if (!sbinfo)
  26095. + return;
  26096. +
  26097. + dbgaufs_si_fin(sbinfo);
  26098. + kobject_put(&sbinfo->si_kobj);
  26099. +}
  26100. +
  26101. +/* ---------------------------------------------------------------------- */
  26102. +
  26103. +void au_array_free(void *array)
  26104. +{
  26105. + if (array) {
  26106. + if (!is_vmalloc_addr(array))
  26107. + kfree(array);
  26108. + else
  26109. + vfree(array);
  26110. + }
  26111. +}
  26112. +
  26113. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
  26114. +{
  26115. + void *array;
  26116. + unsigned long long n, sz;
  26117. +
  26118. + array = NULL;
  26119. + n = 0;
  26120. + if (!*hint)
  26121. + goto out;
  26122. +
  26123. + if (*hint > ULLONG_MAX / sizeof(array)) {
  26124. + array = ERR_PTR(-EMFILE);
  26125. + pr_err("hint %llu\n", *hint);
  26126. + goto out;
  26127. + }
  26128. +
  26129. + sz = sizeof(array) * *hint;
  26130. + array = kzalloc(sz, GFP_NOFS);
  26131. + if (unlikely(!array))
  26132. + array = vzalloc(sz);
  26133. + if (unlikely(!array)) {
  26134. + array = ERR_PTR(-ENOMEM);
  26135. + goto out;
  26136. + }
  26137. +
  26138. + n = cb(array, *hint, arg);
  26139. + AuDebugOn(n > *hint);
  26140. +
  26141. +out:
  26142. + *hint = n;
  26143. + return array;
  26144. +}
  26145. +
  26146. +static unsigned long long au_iarray_cb(void *a,
  26147. + unsigned long long max __maybe_unused,
  26148. + void *arg)
  26149. +{
  26150. + unsigned long long n;
  26151. + struct inode **p, *inode;
  26152. + struct list_head *head;
  26153. +
  26154. + n = 0;
  26155. + p = a;
  26156. + head = arg;
  26157. + spin_lock(&inode_sb_list_lock);
  26158. + list_for_each_entry(inode, head, i_sb_list) {
  26159. + if (!is_bad_inode(inode)
  26160. + && au_ii(inode)->ii_bstart >= 0) {
  26161. + spin_lock(&inode->i_lock);
  26162. + if (atomic_read(&inode->i_count)) {
  26163. + au_igrab(inode);
  26164. + *p++ = inode;
  26165. + n++;
  26166. + AuDebugOn(n > max);
  26167. + }
  26168. + spin_unlock(&inode->i_lock);
  26169. + }
  26170. + }
  26171. + spin_unlock(&inode_sb_list_lock);
  26172. +
  26173. + return n;
  26174. +}
  26175. +
  26176. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  26177. +{
  26178. + *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
  26179. + return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
  26180. +}
  26181. +
  26182. +void au_iarray_free(struct inode **a, unsigned long long max)
  26183. +{
  26184. + unsigned long long ull;
  26185. +
  26186. + for (ull = 0; ull < max; ull++)
  26187. + iput(a[ull]);
  26188. + au_array_free(a);
  26189. +}
  26190. +
  26191. +/* ---------------------------------------------------------------------- */
  26192. +
  26193. +/*
  26194. + * refresh dentry and inode at remount time.
  26195. + */
  26196. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  26197. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  26198. + struct dentry *parent)
  26199. +{
  26200. + int err;
  26201. +
  26202. + di_write_lock_child(dentry);
  26203. + di_read_lock_parent(parent, AuLock_IR);
  26204. + err = au_refresh_dentry(dentry, parent);
  26205. + if (!err && dir_flags)
  26206. + au_hn_reset(d_inode(dentry), dir_flags);
  26207. + di_read_unlock(parent, AuLock_IR);
  26208. + di_write_unlock(dentry);
  26209. +
  26210. + return err;
  26211. +}
  26212. +
  26213. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  26214. + struct au_sbinfo *sbinfo,
  26215. + const unsigned int dir_flags)
  26216. +{
  26217. + int err;
  26218. + struct dentry *parent;
  26219. +
  26220. + err = 0;
  26221. + parent = dget_parent(dentry);
  26222. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  26223. + if (d_really_is_positive(dentry)) {
  26224. + if (!d_is_dir(dentry))
  26225. + err = au_do_refresh(dentry, /*dir_flags*/0,
  26226. + parent);
  26227. + else {
  26228. + err = au_do_refresh(dentry, dir_flags, parent);
  26229. + if (unlikely(err))
  26230. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  26231. + }
  26232. + } else
  26233. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  26234. + AuDbgDentry(dentry);
  26235. + }
  26236. + dput(parent);
  26237. +
  26238. + AuTraceErr(err);
  26239. + return err;
  26240. +}
  26241. +
  26242. +static int au_refresh_d(struct super_block *sb)
  26243. +{
  26244. + int err, i, j, ndentry, e;
  26245. + unsigned int sigen;
  26246. + struct au_dcsub_pages dpages;
  26247. + struct au_dpage *dpage;
  26248. + struct dentry **dentries, *d;
  26249. + struct au_sbinfo *sbinfo;
  26250. + struct dentry *root = sb->s_root;
  26251. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  26252. +
  26253. + err = au_dpages_init(&dpages, GFP_NOFS);
  26254. + if (unlikely(err))
  26255. + goto out;
  26256. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  26257. + if (unlikely(err))
  26258. + goto out_dpages;
  26259. +
  26260. + sigen = au_sigen(sb);
  26261. + sbinfo = au_sbi(sb);
  26262. + for (i = 0; i < dpages.ndpage; i++) {
  26263. + dpage = dpages.dpages + i;
  26264. + dentries = dpage->dentries;
  26265. + ndentry = dpage->ndentry;
  26266. + for (j = 0; j < ndentry; j++) {
  26267. + d = dentries[j];
  26268. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
  26269. + if (unlikely(e && !err))
  26270. + err = e;
  26271. + /* go on even err */
  26272. + }
  26273. + }
  26274. +
  26275. +out_dpages:
  26276. + au_dpages_free(&dpages);
  26277. +out:
  26278. + return err;
  26279. +}
  26280. +
  26281. +static int au_refresh_i(struct super_block *sb)
  26282. +{
  26283. + int err, e;
  26284. + unsigned int sigen;
  26285. + unsigned long long max, ull;
  26286. + struct inode *inode, **array;
  26287. +
  26288. + array = au_iarray_alloc(sb, &max);
  26289. + err = PTR_ERR(array);
  26290. + if (IS_ERR(array))
  26291. + goto out;
  26292. +
  26293. + err = 0;
  26294. + sigen = au_sigen(sb);
  26295. + for (ull = 0; ull < max; ull++) {
  26296. + inode = array[ull];
  26297. + if (unlikely(!inode))
  26298. + break;
  26299. + if (au_iigen(inode, NULL) != sigen) {
  26300. + ii_write_lock_child(inode);
  26301. + e = au_refresh_hinode_self(inode);
  26302. + ii_write_unlock(inode);
  26303. + if (unlikely(e)) {
  26304. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  26305. + if (!err)
  26306. + err = e;
  26307. + /* go on even if err */
  26308. + }
  26309. + }
  26310. + }
  26311. +
  26312. + au_iarray_free(array, max);
  26313. +
  26314. +out:
  26315. + return err;
  26316. +}
  26317. +
  26318. +static void au_remount_refresh(struct super_block *sb)
  26319. +{
  26320. + int err, e;
  26321. + unsigned int udba;
  26322. + aufs_bindex_t bindex, bend;
  26323. + struct dentry *root;
  26324. + struct inode *inode;
  26325. + struct au_branch *br;
  26326. +
  26327. + au_sigen_inc(sb);
  26328. + au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
  26329. +
  26330. + root = sb->s_root;
  26331. + DiMustNoWaiters(root);
  26332. + inode = d_inode(root);
  26333. + IiMustNoWaiters(inode);
  26334. +
  26335. + udba = au_opt_udba(sb);
  26336. + bend = au_sbend(sb);
  26337. + for (bindex = 0; bindex <= bend; bindex++) {
  26338. + br = au_sbr(sb, bindex);
  26339. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  26340. + if (unlikely(err))
  26341. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  26342. + bindex, err);
  26343. + /* go on even if err */
  26344. + }
  26345. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  26346. +
  26347. + di_write_unlock(root);
  26348. + err = au_refresh_d(sb);
  26349. + e = au_refresh_i(sb);
  26350. + if (unlikely(e && !err))
  26351. + err = e;
  26352. + /* aufs_write_lock() calls ..._child() */
  26353. + di_write_lock_child(root);
  26354. +
  26355. + au_cpup_attr_all(inode, /*force*/1);
  26356. +
  26357. + if (unlikely(err))
  26358. + AuIOErr("refresh failed, ignored, %d\n", err);
  26359. +}
  26360. +
  26361. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  26362. +static int cvt_err(int err)
  26363. +{
  26364. + AuTraceErr(err);
  26365. +
  26366. + switch (err) {
  26367. + case -ENOENT:
  26368. + case -ENOTDIR:
  26369. + case -EEXIST:
  26370. + case -EIO:
  26371. + err = -EINVAL;
  26372. + }
  26373. + return err;
  26374. +}
  26375. +
  26376. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  26377. +{
  26378. + int err, do_dx;
  26379. + unsigned int mntflags;
  26380. + struct au_opts opts;
  26381. + struct dentry *root;
  26382. + struct inode *inode;
  26383. + struct au_sbinfo *sbinfo;
  26384. +
  26385. + err = 0;
  26386. + root = sb->s_root;
  26387. + if (!data || !*data) {
  26388. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  26389. + if (!err) {
  26390. + di_write_lock_child(root);
  26391. + err = au_opts_verify(sb, *flags, /*pending*/0);
  26392. + aufs_write_unlock(root);
  26393. + }
  26394. + goto out;
  26395. + }
  26396. +
  26397. + err = -ENOMEM;
  26398. + memset(&opts, 0, sizeof(opts));
  26399. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  26400. + if (unlikely(!opts.opt))
  26401. + goto out;
  26402. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  26403. + opts.flags = AuOpts_REMOUNT;
  26404. + opts.sb_flags = *flags;
  26405. +
  26406. + /* parse it before aufs lock */
  26407. + err = au_opts_parse(sb, data, &opts);
  26408. + if (unlikely(err))
  26409. + goto out_opts;
  26410. +
  26411. + sbinfo = au_sbi(sb);
  26412. + inode = d_inode(root);
  26413. + mutex_lock(&inode->i_mutex);
  26414. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  26415. + if (unlikely(err))
  26416. + goto out_mtx;
  26417. + di_write_lock_child(root);
  26418. +
  26419. + /* au_opts_remount() may return an error */
  26420. + err = au_opts_remount(sb, &opts);
  26421. + au_opts_free(&opts);
  26422. +
  26423. + if (au_ftest_opts(opts.flags, REFRESH))
  26424. + au_remount_refresh(sb);
  26425. +
  26426. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  26427. + mntflags = au_mntflags(sb);
  26428. + do_dx = !!au_opt_test(mntflags, DIO);
  26429. + au_dy_arefresh(do_dx);
  26430. + }
  26431. +
  26432. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  26433. + aufs_write_unlock(root);
  26434. +
  26435. +out_mtx:
  26436. + mutex_unlock(&inode->i_mutex);
  26437. +out_opts:
  26438. + free_page((unsigned long)opts.opt);
  26439. +out:
  26440. + err = cvt_err(err);
  26441. + AuTraceErr(err);
  26442. + return err;
  26443. +}
  26444. +
  26445. +static const struct super_operations aufs_sop = {
  26446. + .alloc_inode = aufs_alloc_inode,
  26447. + .destroy_inode = aufs_destroy_inode,
  26448. + /* always deleting, no clearing */
  26449. + .drop_inode = generic_delete_inode,
  26450. + .show_options = aufs_show_options,
  26451. + .statfs = aufs_statfs,
  26452. + .put_super = aufs_put_super,
  26453. + .sync_fs = aufs_sync_fs,
  26454. + .remount_fs = aufs_remount_fs
  26455. +};
  26456. +
  26457. +/* ---------------------------------------------------------------------- */
  26458. +
  26459. +static int alloc_root(struct super_block *sb)
  26460. +{
  26461. + int err;
  26462. + struct inode *inode;
  26463. + struct dentry *root;
  26464. +
  26465. + err = -ENOMEM;
  26466. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  26467. + err = PTR_ERR(inode);
  26468. + if (IS_ERR(inode))
  26469. + goto out;
  26470. +
  26471. + inode->i_op = &aufs_dir_iop;
  26472. + inode->i_fop = &aufs_dir_fop;
  26473. + inode->i_mode = S_IFDIR;
  26474. + set_nlink(inode, 2);
  26475. + unlock_new_inode(inode);
  26476. +
  26477. + root = d_make_root(inode);
  26478. + if (unlikely(!root))
  26479. + goto out;
  26480. + err = PTR_ERR(root);
  26481. + if (IS_ERR(root))
  26482. + goto out;
  26483. +
  26484. + err = au_di_init(root);
  26485. + if (!err) {
  26486. + sb->s_root = root;
  26487. + return 0; /* success */
  26488. + }
  26489. + dput(root);
  26490. +
  26491. +out:
  26492. + return err;
  26493. +}
  26494. +
  26495. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  26496. + int silent __maybe_unused)
  26497. +{
  26498. + int err;
  26499. + struct au_opts opts;
  26500. + struct dentry *root;
  26501. + struct inode *inode;
  26502. + char *arg = raw_data;
  26503. +
  26504. + if (unlikely(!arg || !*arg)) {
  26505. + err = -EINVAL;
  26506. + pr_err("no arg\n");
  26507. + goto out;
  26508. + }
  26509. +
  26510. + err = -ENOMEM;
  26511. + memset(&opts, 0, sizeof(opts));
  26512. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  26513. + if (unlikely(!opts.opt))
  26514. + goto out;
  26515. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  26516. + opts.sb_flags = sb->s_flags;
  26517. +
  26518. + err = au_si_alloc(sb);
  26519. + if (unlikely(err))
  26520. + goto out_opts;
  26521. +
  26522. + /* all timestamps always follow the ones on the branch */
  26523. + sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
  26524. + sb->s_op = &aufs_sop;
  26525. + sb->s_d_op = &aufs_dop;
  26526. + sb->s_magic = AUFS_SUPER_MAGIC;
  26527. + sb->s_maxbytes = 0;
  26528. + sb->s_stack_depth = 1;
  26529. + au_export_init(sb);
  26530. + /* au_xattr_init(sb); */
  26531. +
  26532. + err = alloc_root(sb);
  26533. + if (unlikely(err)) {
  26534. + si_write_unlock(sb);
  26535. + goto out_info;
  26536. + }
  26537. + root = sb->s_root;
  26538. + inode = d_inode(root);
  26539. +
  26540. + /*
  26541. + * actually we can parse options regardless aufs lock here.
  26542. + * but at remount time, parsing must be done before aufs lock.
  26543. + * so we follow the same rule.
  26544. + */
  26545. + ii_write_lock_parent(inode);
  26546. + aufs_write_unlock(root);
  26547. + err = au_opts_parse(sb, arg, &opts);
  26548. + if (unlikely(err))
  26549. + goto out_root;
  26550. +
  26551. + /* lock vfs_inode first, then aufs. */
  26552. + mutex_lock(&inode->i_mutex);
  26553. + aufs_write_lock(root);
  26554. + err = au_opts_mount(sb, &opts);
  26555. + au_opts_free(&opts);
  26556. + aufs_write_unlock(root);
  26557. + mutex_unlock(&inode->i_mutex);
  26558. + if (!err)
  26559. + goto out_opts; /* success */
  26560. +
  26561. +out_root:
  26562. + dput(root);
  26563. + sb->s_root = NULL;
  26564. +out_info:
  26565. + dbgaufs_si_fin(au_sbi(sb));
  26566. + kobject_put(&au_sbi(sb)->si_kobj);
  26567. + sb->s_fs_info = NULL;
  26568. +out_opts:
  26569. + free_page((unsigned long)opts.opt);
  26570. +out:
  26571. + AuTraceErr(err);
  26572. + err = cvt_err(err);
  26573. + AuTraceErr(err);
  26574. + return err;
  26575. +}
  26576. +
  26577. +/* ---------------------------------------------------------------------- */
  26578. +
  26579. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  26580. + const char *dev_name __maybe_unused,
  26581. + void *raw_data)
  26582. +{
  26583. + struct dentry *root;
  26584. + struct super_block *sb;
  26585. +
  26586. + /* all timestamps always follow the ones on the branch */
  26587. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  26588. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  26589. + if (IS_ERR(root))
  26590. + goto out;
  26591. +
  26592. + sb = root->d_sb;
  26593. + si_write_lock(sb, !AuLock_FLUSH);
  26594. + sysaufs_brs_add(sb, 0);
  26595. + si_write_unlock(sb);
  26596. + au_sbilist_add(sb);
  26597. +
  26598. +out:
  26599. + return root;
  26600. +}
  26601. +
  26602. +static void aufs_kill_sb(struct super_block *sb)
  26603. +{
  26604. + struct au_sbinfo *sbinfo;
  26605. +
  26606. + sbinfo = au_sbi(sb);
  26607. + if (sbinfo) {
  26608. + au_sbilist_del(sb);
  26609. + aufs_write_lock(sb->s_root);
  26610. + au_fhsm_fin(sb);
  26611. + if (sbinfo->si_wbr_create_ops->fin)
  26612. + sbinfo->si_wbr_create_ops->fin(sb);
  26613. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  26614. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  26615. + au_remount_refresh(sb);
  26616. + }
  26617. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  26618. + au_plink_put(sb, /*verbose*/1);
  26619. + au_xino_clr(sb);
  26620. + sbinfo->si_sb = NULL;
  26621. + aufs_write_unlock(sb->s_root);
  26622. + au_nwt_flush(&sbinfo->si_nowait);
  26623. + }
  26624. + kill_anon_super(sb);
  26625. +}
  26626. +
  26627. +struct file_system_type aufs_fs_type = {
  26628. + .name = AUFS_FSTYPE,
  26629. + /* a race between rename and others */
  26630. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  26631. + .mount = aufs_mount,
  26632. + .kill_sb = aufs_kill_sb,
  26633. + /* no need to __module_get() and module_put(). */
  26634. + .owner = THIS_MODULE,
  26635. +};
  26636. diff -Nur linux-4.1.10.orig/fs/aufs/super.h linux-4.1.10/fs/aufs/super.h
  26637. --- linux-4.1.10.orig/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
  26638. +++ linux-4.1.10/fs/aufs/super.h 2015-10-22 21:35:53.000000000 +0200
  26639. @@ -0,0 +1,635 @@
  26640. +/*
  26641. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  26642. + *
  26643. + * This program, aufs is free software; you can redistribute it and/or modify
  26644. + * it under the terms of the GNU General Public License as published by
  26645. + * the Free Software Foundation; either version 2 of the License, or
  26646. + * (at your option) any later version.
  26647. + *
  26648. + * This program is distributed in the hope that it will be useful,
  26649. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26650. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26651. + * GNU General Public License for more details.
  26652. + *
  26653. + * You should have received a copy of the GNU General Public License
  26654. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26655. + */
  26656. +
  26657. +/*
  26658. + * super_block operations
  26659. + */
  26660. +
  26661. +#ifndef __AUFS_SUPER_H__
  26662. +#define __AUFS_SUPER_H__
  26663. +
  26664. +#ifdef __KERNEL__
  26665. +
  26666. +#include <linux/fs.h>
  26667. +#include <linux/kobject.h>
  26668. +#include "rwsem.h"
  26669. +#include "spl.h"
  26670. +#include "wkq.h"
  26671. +
  26672. +/* policies to select one among multiple writable branches */
  26673. +struct au_wbr_copyup_operations {
  26674. + int (*copyup)(struct dentry *dentry);
  26675. +};
  26676. +
  26677. +#define AuWbr_DIR 1 /* target is a dir */
  26678. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  26679. +
  26680. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  26681. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  26682. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  26683. +
  26684. +struct au_wbr_create_operations {
  26685. + int (*create)(struct dentry *dentry, unsigned int flags);
  26686. + int (*init)(struct super_block *sb);
  26687. + int (*fin)(struct super_block *sb);
  26688. +};
  26689. +
  26690. +struct au_wbr_mfs {
  26691. + struct mutex mfs_lock; /* protect this structure */
  26692. + unsigned long mfs_jiffy;
  26693. + unsigned long mfs_expire;
  26694. + aufs_bindex_t mfs_bindex;
  26695. +
  26696. + unsigned long long mfsrr_bytes;
  26697. + unsigned long long mfsrr_watermark;
  26698. +};
  26699. +
  26700. +struct pseudo_link {
  26701. + union {
  26702. + struct hlist_node hlist;
  26703. + struct rcu_head rcu;
  26704. + };
  26705. + struct inode *inode;
  26706. +};
  26707. +
  26708. +#define AuPlink_NHASH 100
  26709. +static inline int au_plink_hash(ino_t ino)
  26710. +{
  26711. + return ino % AuPlink_NHASH;
  26712. +}
  26713. +
  26714. +/* File-based Hierarchical Storage Management */
  26715. +struct au_fhsm {
  26716. +#ifdef CONFIG_AUFS_FHSM
  26717. + /* allow only one process who can receive the notification */
  26718. + spinlock_t fhsm_spin;
  26719. + pid_t fhsm_pid;
  26720. + wait_queue_head_t fhsm_wqh;
  26721. + atomic_t fhsm_readable;
  26722. +
  26723. + /* these are protected by si_rwsem */
  26724. + unsigned long fhsm_expire;
  26725. + aufs_bindex_t fhsm_bottom;
  26726. +#endif
  26727. +};
  26728. +
  26729. +struct au_branch;
  26730. +struct au_sbinfo {
  26731. + /* nowait tasks in the system-wide workqueue */
  26732. + struct au_nowait_tasks si_nowait;
  26733. +
  26734. + /*
  26735. + * tried sb->s_umount, but failed due to the dependecy between i_mutex.
  26736. + * rwsem for au_sbinfo is necessary.
  26737. + */
  26738. + struct au_rwsem si_rwsem;
  26739. +
  26740. + /* prevent recursive locking in deleting inode */
  26741. + struct {
  26742. + unsigned long *bitmap;
  26743. + spinlock_t tree_lock;
  26744. + struct radix_tree_root tree;
  26745. + } au_si_pid;
  26746. +
  26747. + /*
  26748. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  26749. + * remount.
  26750. + */
  26751. + atomic_long_t si_ninodes, si_nfiles;
  26752. +
  26753. + /* branch management */
  26754. + unsigned int si_generation;
  26755. +
  26756. + /* see AuSi_ flags */
  26757. + unsigned char au_si_status;
  26758. +
  26759. + aufs_bindex_t si_bend;
  26760. +
  26761. + /* dirty trick to keep br_id plus */
  26762. + unsigned int si_last_br_id :
  26763. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  26764. + struct au_branch **si_branch;
  26765. +
  26766. + /* policy to select a writable branch */
  26767. + unsigned char si_wbr_copyup;
  26768. + unsigned char si_wbr_create;
  26769. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  26770. + struct au_wbr_create_operations *si_wbr_create_ops;
  26771. +
  26772. + /* round robin */
  26773. + atomic_t si_wbr_rr_next;
  26774. +
  26775. + /* most free space */
  26776. + struct au_wbr_mfs si_wbr_mfs;
  26777. +
  26778. + /* File-based Hierarchical Storage Management */
  26779. + struct au_fhsm si_fhsm;
  26780. +
  26781. + /* mount flags */
  26782. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  26783. + unsigned int si_mntflags;
  26784. +
  26785. + /* external inode number (bitmap and translation table) */
  26786. + vfs_readf_t si_xread;
  26787. + vfs_writef_t si_xwrite;
  26788. + struct file *si_xib;
  26789. + struct mutex si_xib_mtx; /* protect xib members */
  26790. + unsigned long *si_xib_buf;
  26791. + unsigned long si_xib_last_pindex;
  26792. + int si_xib_next_bit;
  26793. + aufs_bindex_t si_xino_brid;
  26794. + unsigned long si_xino_jiffy;
  26795. + unsigned long si_xino_expire;
  26796. + /* reserved for future use */
  26797. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  26798. +
  26799. +#ifdef CONFIG_AUFS_EXPORT
  26800. + /* i_generation */
  26801. + struct file *si_xigen;
  26802. + atomic_t si_xigen_next;
  26803. +#endif
  26804. +
  26805. + /* dirty trick to suppoer atomic_open */
  26806. + struct au_sphlhead si_aopen;
  26807. +
  26808. + /* vdir parameters */
  26809. + unsigned long si_rdcache; /* max cache time in jiffies */
  26810. + unsigned int si_rdblk; /* deblk size */
  26811. + unsigned int si_rdhash; /* hash size */
  26812. +
  26813. + /*
  26814. + * If the number of whiteouts are larger than si_dirwh, leave all of
  26815. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  26816. + * future fsck.aufs or kernel thread will remove them later.
  26817. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  26818. + */
  26819. + unsigned int si_dirwh;
  26820. +
  26821. + /* pseudo_link list */
  26822. + struct au_sphlhead si_plink[AuPlink_NHASH];
  26823. + wait_queue_head_t si_plink_wq;
  26824. + spinlock_t si_plink_maint_lock;
  26825. + pid_t si_plink_maint_pid;
  26826. +
  26827. + /* file list */
  26828. + struct au_sphlhead si_files;
  26829. +
  26830. + /*
  26831. + * sysfs and lifetime management.
  26832. + * this is not a small structure and it may be a waste of memory in case
  26833. + * of sysfs is disabled, particulary when many aufs-es are mounted.
  26834. + * but using sysfs is majority.
  26835. + */
  26836. + struct kobject si_kobj;
  26837. +#ifdef CONFIG_DEBUG_FS
  26838. + struct dentry *si_dbgaufs;
  26839. + struct dentry *si_dbgaufs_plink;
  26840. + struct dentry *si_dbgaufs_xib;
  26841. +#ifdef CONFIG_AUFS_EXPORT
  26842. + struct dentry *si_dbgaufs_xigen;
  26843. +#endif
  26844. +#endif
  26845. +
  26846. +#ifdef CONFIG_AUFS_SBILIST
  26847. + struct list_head si_list;
  26848. +#endif
  26849. +
  26850. + /* dirty, necessary for unmounting, sysfs and sysrq */
  26851. + struct super_block *si_sb;
  26852. +};
  26853. +
  26854. +/* sbinfo status flags */
  26855. +/*
  26856. + * set true when refresh_dirs() failed at remount time.
  26857. + * then try refreshing dirs at access time again.
  26858. + * if it is false, refreshing dirs at access time is unnecesary
  26859. + */
  26860. +#define AuSi_FAILED_REFRESH_DIR 1
  26861. +
  26862. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  26863. +
  26864. +#ifndef CONFIG_AUFS_FHSM
  26865. +#undef AuSi_FHSM
  26866. +#define AuSi_FHSM 0
  26867. +#endif
  26868. +
  26869. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  26870. + unsigned int flag)
  26871. +{
  26872. + AuRwMustAnyLock(&sbi->si_rwsem);
  26873. + return sbi->au_si_status & flag;
  26874. +}
  26875. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  26876. +#define au_fset_si(sbinfo, name) do { \
  26877. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  26878. + (sbinfo)->au_si_status |= AuSi_##name; \
  26879. +} while (0)
  26880. +#define au_fclr_si(sbinfo, name) do { \
  26881. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  26882. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  26883. +} while (0)
  26884. +
  26885. +/* ---------------------------------------------------------------------- */
  26886. +
  26887. +/* policy to select one among writable branches */
  26888. +#define AuWbrCopyup(sbinfo, ...) \
  26889. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  26890. +#define AuWbrCreate(sbinfo, ...) \
  26891. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  26892. +
  26893. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  26894. +#define AuLock_DW 1 /* write-lock dentry */
  26895. +#define AuLock_IR (1 << 1) /* read-lock inode */
  26896. +#define AuLock_IW (1 << 2) /* write-lock inode */
  26897. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  26898. +#define AuLock_DIR (1 << 4) /* target is a dir */
  26899. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  26900. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  26901. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  26902. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  26903. +#define au_fset_lock(flags, name) \
  26904. + do { (flags) |= AuLock_##name; } while (0)
  26905. +#define au_fclr_lock(flags, name) \
  26906. + do { (flags) &= ~AuLock_##name; } while (0)
  26907. +
  26908. +/* ---------------------------------------------------------------------- */
  26909. +
  26910. +/* super.c */
  26911. +extern struct file_system_type aufs_fs_type;
  26912. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  26913. +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
  26914. + void *arg);
  26915. +void au_array_free(void *array);
  26916. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
  26917. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  26918. +void au_iarray_free(struct inode **a, unsigned long long max);
  26919. +
  26920. +/* sbinfo.c */
  26921. +void au_si_free(struct kobject *kobj);
  26922. +int au_si_alloc(struct super_block *sb);
  26923. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
  26924. +
  26925. +unsigned int au_sigen_inc(struct super_block *sb);
  26926. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  26927. +
  26928. +int si_read_lock(struct super_block *sb, int flags);
  26929. +int si_write_lock(struct super_block *sb, int flags);
  26930. +int aufs_read_lock(struct dentry *dentry, int flags);
  26931. +void aufs_read_unlock(struct dentry *dentry, int flags);
  26932. +void aufs_write_lock(struct dentry *dentry);
  26933. +void aufs_write_unlock(struct dentry *dentry);
  26934. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  26935. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  26936. +
  26937. +int si_pid_test_slow(struct super_block *sb);
  26938. +void si_pid_set_slow(struct super_block *sb);
  26939. +void si_pid_clr_slow(struct super_block *sb);
  26940. +
  26941. +/* wbr_policy.c */
  26942. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  26943. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  26944. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  26945. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  26946. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
  26947. +
  26948. +/* mvdown.c */
  26949. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  26950. +
  26951. +#ifdef CONFIG_AUFS_FHSM
  26952. +/* fhsm.c */
  26953. +
  26954. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  26955. +{
  26956. + pid_t pid;
  26957. +
  26958. + spin_lock(&fhsm->fhsm_spin);
  26959. + pid = fhsm->fhsm_pid;
  26960. + spin_unlock(&fhsm->fhsm_spin);
  26961. +
  26962. + return pid;
  26963. +}
  26964. +
  26965. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  26966. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  26967. +int au_fhsm_fd(struct super_block *sb, int oflags);
  26968. +int au_fhsm_br_alloc(struct au_branch *br);
  26969. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  26970. +void au_fhsm_fin(struct super_block *sb);
  26971. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  26972. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  26973. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  26974. +#else
  26975. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  26976. + int force)
  26977. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  26978. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  26979. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  26980. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  26981. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  26982. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  26983. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  26984. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  26985. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  26986. +#endif
  26987. +
  26988. +/* ---------------------------------------------------------------------- */
  26989. +
  26990. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  26991. +{
  26992. + return sb->s_fs_info;
  26993. +}
  26994. +
  26995. +/* ---------------------------------------------------------------------- */
  26996. +
  26997. +#ifdef CONFIG_AUFS_EXPORT
  26998. +int au_test_nfsd(void);
  26999. +void au_export_init(struct super_block *sb);
  27000. +void au_xigen_inc(struct inode *inode);
  27001. +int au_xigen_new(struct inode *inode);
  27002. +int au_xigen_set(struct super_block *sb, struct file *base);
  27003. +void au_xigen_clr(struct super_block *sb);
  27004. +
  27005. +static inline int au_busy_or_stale(void)
  27006. +{
  27007. + if (!au_test_nfsd())
  27008. + return -EBUSY;
  27009. + return -ESTALE;
  27010. +}
  27011. +#else
  27012. +AuStubInt0(au_test_nfsd, void)
  27013. +AuStubVoid(au_export_init, struct super_block *sb)
  27014. +AuStubVoid(au_xigen_inc, struct inode *inode)
  27015. +AuStubInt0(au_xigen_new, struct inode *inode)
  27016. +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
  27017. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  27018. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  27019. +#endif /* CONFIG_AUFS_EXPORT */
  27020. +
  27021. +/* ---------------------------------------------------------------------- */
  27022. +
  27023. +#ifdef CONFIG_AUFS_SBILIST
  27024. +/* module.c */
  27025. +extern struct au_splhead au_sbilist;
  27026. +
  27027. +static inline void au_sbilist_init(void)
  27028. +{
  27029. + au_spl_init(&au_sbilist);
  27030. +}
  27031. +
  27032. +static inline void au_sbilist_add(struct super_block *sb)
  27033. +{
  27034. + au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
  27035. +}
  27036. +
  27037. +static inline void au_sbilist_del(struct super_block *sb)
  27038. +{
  27039. + au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
  27040. +}
  27041. +
  27042. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  27043. +static inline void au_sbilist_lock(void)
  27044. +{
  27045. + spin_lock(&au_sbilist.spin);
  27046. +}
  27047. +
  27048. +static inline void au_sbilist_unlock(void)
  27049. +{
  27050. + spin_unlock(&au_sbilist.spin);
  27051. +}
  27052. +#define AuGFP_SBILIST GFP_ATOMIC
  27053. +#else
  27054. +AuStubVoid(au_sbilist_lock, void)
  27055. +AuStubVoid(au_sbilist_unlock, void)
  27056. +#define AuGFP_SBILIST GFP_NOFS
  27057. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  27058. +#else
  27059. +AuStubVoid(au_sbilist_init, void)
  27060. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  27061. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  27062. +AuStubVoid(au_sbilist_lock, void)
  27063. +AuStubVoid(au_sbilist_unlock, void)
  27064. +#define AuGFP_SBILIST GFP_NOFS
  27065. +#endif
  27066. +
  27067. +/* ---------------------------------------------------------------------- */
  27068. +
  27069. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  27070. +{
  27071. + /*
  27072. + * This function is a dynamic '__init' function actually,
  27073. + * so the tiny check for si_rwsem is unnecessary.
  27074. + */
  27075. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  27076. +#ifdef CONFIG_DEBUG_FS
  27077. + sbinfo->si_dbgaufs = NULL;
  27078. + sbinfo->si_dbgaufs_plink = NULL;
  27079. + sbinfo->si_dbgaufs_xib = NULL;
  27080. +#ifdef CONFIG_AUFS_EXPORT
  27081. + sbinfo->si_dbgaufs_xigen = NULL;
  27082. +#endif
  27083. +#endif
  27084. +}
  27085. +
  27086. +/* ---------------------------------------------------------------------- */
  27087. +
  27088. +static inline pid_t si_pid_bit(void)
  27089. +{
  27090. + /* the origin of pid is 1, but the bitmap's is 0 */
  27091. + return current->pid - 1;
  27092. +}
  27093. +
  27094. +static inline int si_pid_test(struct super_block *sb)
  27095. +{
  27096. + pid_t bit;
  27097. +
  27098. + bit = si_pid_bit();
  27099. + if (bit < PID_MAX_DEFAULT)
  27100. + return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  27101. + return si_pid_test_slow(sb);
  27102. +}
  27103. +
  27104. +static inline void si_pid_set(struct super_block *sb)
  27105. +{
  27106. + pid_t bit;
  27107. +
  27108. + bit = si_pid_bit();
  27109. + if (bit < PID_MAX_DEFAULT) {
  27110. + AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
  27111. + set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  27112. + /* smp_mb(); */
  27113. + } else
  27114. + si_pid_set_slow(sb);
  27115. +}
  27116. +
  27117. +static inline void si_pid_clr(struct super_block *sb)
  27118. +{
  27119. + pid_t bit;
  27120. +
  27121. + bit = si_pid_bit();
  27122. + if (bit < PID_MAX_DEFAULT) {
  27123. + AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
  27124. + clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
  27125. + /* smp_mb(); */
  27126. + } else
  27127. + si_pid_clr_slow(sb);
  27128. +}
  27129. +
  27130. +/* ---------------------------------------------------------------------- */
  27131. +
  27132. +/* lock superblock. mainly for entry point functions */
  27133. +/*
  27134. + * __si_read_lock, __si_write_lock,
  27135. + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
  27136. + */
  27137. +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
  27138. +
  27139. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  27140. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  27141. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  27142. +
  27143. +static inline void si_noflush_read_lock(struct super_block *sb)
  27144. +{
  27145. + __si_read_lock(sb);
  27146. + si_pid_set(sb);
  27147. +}
  27148. +
  27149. +static inline int si_noflush_read_trylock(struct super_block *sb)
  27150. +{
  27151. + int locked;
  27152. +
  27153. + locked = __si_read_trylock(sb);
  27154. + if (locked)
  27155. + si_pid_set(sb);
  27156. + return locked;
  27157. +}
  27158. +
  27159. +static inline void si_noflush_write_lock(struct super_block *sb)
  27160. +{
  27161. + __si_write_lock(sb);
  27162. + si_pid_set(sb);
  27163. +}
  27164. +
  27165. +static inline int si_noflush_write_trylock(struct super_block *sb)
  27166. +{
  27167. + int locked;
  27168. +
  27169. + locked = __si_write_trylock(sb);
  27170. + if (locked)
  27171. + si_pid_set(sb);
  27172. + return locked;
  27173. +}
  27174. +
  27175. +#if 0 /* reserved */
  27176. +static inline int si_read_trylock(struct super_block *sb, int flags)
  27177. +{
  27178. + if (au_ftest_lock(flags, FLUSH))
  27179. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  27180. + return si_noflush_read_trylock(sb);
  27181. +}
  27182. +#endif
  27183. +
  27184. +static inline void si_read_unlock(struct super_block *sb)
  27185. +{
  27186. + si_pid_clr(sb);
  27187. + __si_read_unlock(sb);
  27188. +}
  27189. +
  27190. +#if 0 /* reserved */
  27191. +static inline int si_write_trylock(struct super_block *sb, int flags)
  27192. +{
  27193. + if (au_ftest_lock(flags, FLUSH))
  27194. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  27195. + return si_noflush_write_trylock(sb);
  27196. +}
  27197. +#endif
  27198. +
  27199. +static inline void si_write_unlock(struct super_block *sb)
  27200. +{
  27201. + si_pid_clr(sb);
  27202. + __si_write_unlock(sb);
  27203. +}
  27204. +
  27205. +#if 0 /* reserved */
  27206. +static inline void si_downgrade_lock(struct super_block *sb)
  27207. +{
  27208. + __si_downgrade_lock(sb);
  27209. +}
  27210. +#endif
  27211. +
  27212. +/* ---------------------------------------------------------------------- */
  27213. +
  27214. +static inline aufs_bindex_t au_sbend(struct super_block *sb)
  27215. +{
  27216. + SiMustAnyLock(sb);
  27217. + return au_sbi(sb)->si_bend;
  27218. +}
  27219. +
  27220. +static inline unsigned int au_mntflags(struct super_block *sb)
  27221. +{
  27222. + SiMustAnyLock(sb);
  27223. + return au_sbi(sb)->si_mntflags;
  27224. +}
  27225. +
  27226. +static inline unsigned int au_sigen(struct super_block *sb)
  27227. +{
  27228. + SiMustAnyLock(sb);
  27229. + return au_sbi(sb)->si_generation;
  27230. +}
  27231. +
  27232. +static inline void au_ninodes_inc(struct super_block *sb)
  27233. +{
  27234. + atomic_long_inc(&au_sbi(sb)->si_ninodes);
  27235. +}
  27236. +
  27237. +static inline void au_ninodes_dec(struct super_block *sb)
  27238. +{
  27239. + AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
  27240. + atomic_long_dec(&au_sbi(sb)->si_ninodes);
  27241. +}
  27242. +
  27243. +static inline void au_nfiles_inc(struct super_block *sb)
  27244. +{
  27245. + atomic_long_inc(&au_sbi(sb)->si_nfiles);
  27246. +}
  27247. +
  27248. +static inline void au_nfiles_dec(struct super_block *sb)
  27249. +{
  27250. + AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
  27251. + atomic_long_dec(&au_sbi(sb)->si_nfiles);
  27252. +}
  27253. +
  27254. +static inline struct au_branch *au_sbr(struct super_block *sb,
  27255. + aufs_bindex_t bindex)
  27256. +{
  27257. + SiMustAnyLock(sb);
  27258. + return au_sbi(sb)->si_branch[0 + bindex];
  27259. +}
  27260. +
  27261. +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
  27262. +{
  27263. + SiMustWriteLock(sb);
  27264. + au_sbi(sb)->si_xino_brid = brid;
  27265. +}
  27266. +
  27267. +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
  27268. +{
  27269. + SiMustAnyLock(sb);
  27270. + return au_sbi(sb)->si_xino_brid;
  27271. +}
  27272. +
  27273. +#endif /* __KERNEL__ */
  27274. +#endif /* __AUFS_SUPER_H__ */
  27275. diff -Nur linux-4.1.10.orig/fs/aufs/sysaufs.c linux-4.1.10/fs/aufs/sysaufs.c
  27276. --- linux-4.1.10.orig/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
  27277. +++ linux-4.1.10/fs/aufs/sysaufs.c 2015-10-22 21:35:53.000000000 +0200
  27278. @@ -0,0 +1,104 @@
  27279. +/*
  27280. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27281. + *
  27282. + * This program, aufs is free software; you can redistribute it and/or modify
  27283. + * it under the terms of the GNU General Public License as published by
  27284. + * the Free Software Foundation; either version 2 of the License, or
  27285. + * (at your option) any later version.
  27286. + *
  27287. + * This program is distributed in the hope that it will be useful,
  27288. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27289. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27290. + * GNU General Public License for more details.
  27291. + *
  27292. + * You should have received a copy of the GNU General Public License
  27293. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27294. + */
  27295. +
  27296. +/*
  27297. + * sysfs interface and lifetime management
  27298. + * they are necessary regardless sysfs is disabled.
  27299. + */
  27300. +
  27301. +#include <linux/random.h>
  27302. +#include "aufs.h"
  27303. +
  27304. +unsigned long sysaufs_si_mask;
  27305. +struct kset *sysaufs_kset;
  27306. +
  27307. +#define AuSiAttr(_name) { \
  27308. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  27309. + .show = sysaufs_si_##_name, \
  27310. +}
  27311. +
  27312. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  27313. +struct attribute *sysaufs_si_attrs[] = {
  27314. + &sysaufs_si_attr_xi_path.attr,
  27315. + NULL,
  27316. +};
  27317. +
  27318. +static const struct sysfs_ops au_sbi_ops = {
  27319. + .show = sysaufs_si_show
  27320. +};
  27321. +
  27322. +static struct kobj_type au_sbi_ktype = {
  27323. + .release = au_si_free,
  27324. + .sysfs_ops = &au_sbi_ops,
  27325. + .default_attrs = sysaufs_si_attrs
  27326. +};
  27327. +
  27328. +/* ---------------------------------------------------------------------- */
  27329. +
  27330. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  27331. +{
  27332. + int err;
  27333. +
  27334. + sbinfo->si_kobj.kset = sysaufs_kset;
  27335. + /* cf. sysaufs_name() */
  27336. + err = kobject_init_and_add
  27337. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  27338. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  27339. +
  27340. + dbgaufs_si_null(sbinfo);
  27341. + if (!err) {
  27342. + err = dbgaufs_si_init(sbinfo);
  27343. + if (unlikely(err))
  27344. + kobject_put(&sbinfo->si_kobj);
  27345. + }
  27346. + return err;
  27347. +}
  27348. +
  27349. +void sysaufs_fin(void)
  27350. +{
  27351. + dbgaufs_fin();
  27352. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  27353. + kset_unregister(sysaufs_kset);
  27354. +}
  27355. +
  27356. +int __init sysaufs_init(void)
  27357. +{
  27358. + int err;
  27359. +
  27360. + do {
  27361. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  27362. + } while (!sysaufs_si_mask);
  27363. +
  27364. + err = -EINVAL;
  27365. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  27366. + if (unlikely(!sysaufs_kset))
  27367. + goto out;
  27368. + err = PTR_ERR(sysaufs_kset);
  27369. + if (IS_ERR(sysaufs_kset))
  27370. + goto out;
  27371. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  27372. + if (unlikely(err)) {
  27373. + kset_unregister(sysaufs_kset);
  27374. + goto out;
  27375. + }
  27376. +
  27377. + err = dbgaufs_init();
  27378. + if (unlikely(err))
  27379. + sysaufs_fin();
  27380. +out:
  27381. + return err;
  27382. +}
  27383. diff -Nur linux-4.1.10.orig/fs/aufs/sysaufs.h linux-4.1.10/fs/aufs/sysaufs.h
  27384. --- linux-4.1.10.orig/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
  27385. +++ linux-4.1.10/fs/aufs/sysaufs.h 2015-10-22 21:35:53.000000000 +0200
  27386. @@ -0,0 +1,101 @@
  27387. +/*
  27388. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27389. + *
  27390. + * This program, aufs is free software; you can redistribute it and/or modify
  27391. + * it under the terms of the GNU General Public License as published by
  27392. + * the Free Software Foundation; either version 2 of the License, or
  27393. + * (at your option) any later version.
  27394. + *
  27395. + * This program is distributed in the hope that it will be useful,
  27396. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27397. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27398. + * GNU General Public License for more details.
  27399. + *
  27400. + * You should have received a copy of the GNU General Public License
  27401. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27402. + */
  27403. +
  27404. +/*
  27405. + * sysfs interface and mount lifetime management
  27406. + */
  27407. +
  27408. +#ifndef __SYSAUFS_H__
  27409. +#define __SYSAUFS_H__
  27410. +
  27411. +#ifdef __KERNEL__
  27412. +
  27413. +#include <linux/sysfs.h>
  27414. +#include "module.h"
  27415. +
  27416. +struct super_block;
  27417. +struct au_sbinfo;
  27418. +
  27419. +struct sysaufs_si_attr {
  27420. + struct attribute attr;
  27421. + int (*show)(struct seq_file *seq, struct super_block *sb);
  27422. +};
  27423. +
  27424. +/* ---------------------------------------------------------------------- */
  27425. +
  27426. +/* sysaufs.c */
  27427. +extern unsigned long sysaufs_si_mask;
  27428. +extern struct kset *sysaufs_kset;
  27429. +extern struct attribute *sysaufs_si_attrs[];
  27430. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  27431. +int __init sysaufs_init(void);
  27432. +void sysaufs_fin(void);
  27433. +
  27434. +/* ---------------------------------------------------------------------- */
  27435. +
  27436. +/* some people doesn't like to show a pointer in kernel */
  27437. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  27438. +{
  27439. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  27440. +}
  27441. +
  27442. +#define SysaufsSiNamePrefix "si_"
  27443. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  27444. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  27445. +{
  27446. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  27447. + sysaufs_si_id(sbinfo));
  27448. +}
  27449. +
  27450. +struct au_branch;
  27451. +#ifdef CONFIG_SYSFS
  27452. +/* sysfs.c */
  27453. +extern struct attribute_group *sysaufs_attr_group;
  27454. +
  27455. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  27456. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  27457. + char *buf);
  27458. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  27459. +#ifdef CONFIG_COMPAT
  27460. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  27461. +#endif
  27462. +
  27463. +void sysaufs_br_init(struct au_branch *br);
  27464. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  27465. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  27466. +
  27467. +#define sysaufs_brs_init() do {} while (0)
  27468. +
  27469. +#else
  27470. +#define sysaufs_attr_group NULL
  27471. +
  27472. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  27473. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  27474. + struct attribute *attr, char *buf)
  27475. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  27476. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  27477. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  27478. +
  27479. +static inline void sysaufs_brs_init(void)
  27480. +{
  27481. + sysaufs_brs = 0;
  27482. +}
  27483. +
  27484. +#endif /* CONFIG_SYSFS */
  27485. +
  27486. +#endif /* __KERNEL__ */
  27487. +#endif /* __SYSAUFS_H__ */
  27488. diff -Nur linux-4.1.10.orig/fs/aufs/sysfs.c linux-4.1.10/fs/aufs/sysfs.c
  27489. --- linux-4.1.10.orig/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
  27490. +++ linux-4.1.10/fs/aufs/sysfs.c 2015-10-22 21:35:53.000000000 +0200
  27491. @@ -0,0 +1,376 @@
  27492. +/*
  27493. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27494. + *
  27495. + * This program, aufs is free software; you can redistribute it and/or modify
  27496. + * it under the terms of the GNU General Public License as published by
  27497. + * the Free Software Foundation; either version 2 of the License, or
  27498. + * (at your option) any later version.
  27499. + *
  27500. + * This program is distributed in the hope that it will be useful,
  27501. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27502. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27503. + * GNU General Public License for more details.
  27504. + *
  27505. + * You should have received a copy of the GNU General Public License
  27506. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27507. + */
  27508. +
  27509. +/*
  27510. + * sysfs interface
  27511. + */
  27512. +
  27513. +#include <linux/compat.h>
  27514. +#include <linux/seq_file.h>
  27515. +#include "aufs.h"
  27516. +
  27517. +#ifdef CONFIG_AUFS_FS_MODULE
  27518. +/* this entry violates the "one line per file" policy of sysfs */
  27519. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  27520. + char *buf)
  27521. +{
  27522. + ssize_t err;
  27523. + static char *conf =
  27524. +/* this file is generated at compiling */
  27525. +#include "conf.str"
  27526. + ;
  27527. +
  27528. + err = snprintf(buf, PAGE_SIZE, conf);
  27529. + if (unlikely(err >= PAGE_SIZE))
  27530. + err = -EFBIG;
  27531. + return err;
  27532. +}
  27533. +
  27534. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  27535. +#endif
  27536. +
  27537. +static struct attribute *au_attr[] = {
  27538. +#ifdef CONFIG_AUFS_FS_MODULE
  27539. + &au_config_attr.attr,
  27540. +#endif
  27541. + NULL, /* need to NULL terminate the list of attributes */
  27542. +};
  27543. +
  27544. +static struct attribute_group sysaufs_attr_group_body = {
  27545. + .attrs = au_attr
  27546. +};
  27547. +
  27548. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  27549. +
  27550. +/* ---------------------------------------------------------------------- */
  27551. +
  27552. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  27553. +{
  27554. + int err;
  27555. +
  27556. + SiMustAnyLock(sb);
  27557. +
  27558. + err = 0;
  27559. + if (au_opt_test(au_mntflags(sb), XINO)) {
  27560. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  27561. + seq_putc(seq, '\n');
  27562. + }
  27563. + return err;
  27564. +}
  27565. +
  27566. +/*
  27567. + * the lifetime of branch is independent from the entry under sysfs.
  27568. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  27569. + * unlinked.
  27570. + */
  27571. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  27572. + aufs_bindex_t bindex, int idx)
  27573. +{
  27574. + int err;
  27575. + struct path path;
  27576. + struct dentry *root;
  27577. + struct au_branch *br;
  27578. + au_br_perm_str_t perm;
  27579. +
  27580. + AuDbg("b%d\n", bindex);
  27581. +
  27582. + err = 0;
  27583. + root = sb->s_root;
  27584. + di_read_lock_parent(root, !AuLock_IR);
  27585. + br = au_sbr(sb, bindex);
  27586. +
  27587. + switch (idx) {
  27588. + case AuBrSysfs_BR:
  27589. + path.mnt = au_br_mnt(br);
  27590. + path.dentry = au_h_dptr(root, bindex);
  27591. + err = au_seq_path(seq, &path);
  27592. + if (!err) {
  27593. + au_optstr_br_perm(&perm, br->br_perm);
  27594. + err = seq_printf(seq, "=%s\n", perm.a);
  27595. + }
  27596. + break;
  27597. + case AuBrSysfs_BRID:
  27598. + err = seq_printf(seq, "%d\n", br->br_id);
  27599. + break;
  27600. + }
  27601. + di_read_unlock(root, !AuLock_IR);
  27602. + if (err == -1)
  27603. + err = -E2BIG;
  27604. +
  27605. + return err;
  27606. +}
  27607. +
  27608. +/* ---------------------------------------------------------------------- */
  27609. +
  27610. +static struct seq_file *au_seq(char *p, ssize_t len)
  27611. +{
  27612. + struct seq_file *seq;
  27613. +
  27614. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  27615. + if (seq) {
  27616. + /* mutex_init(&seq.lock); */
  27617. + seq->buf = p;
  27618. + seq->size = len;
  27619. + return seq; /* success */
  27620. + }
  27621. +
  27622. + seq = ERR_PTR(-ENOMEM);
  27623. + return seq;
  27624. +}
  27625. +
  27626. +#define SysaufsBr_PREFIX "br"
  27627. +#define SysaufsBrid_PREFIX "brid"
  27628. +
  27629. +/* todo: file size may exceed PAGE_SIZE */
  27630. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  27631. + char *buf)
  27632. +{
  27633. + ssize_t err;
  27634. + int idx;
  27635. + long l;
  27636. + aufs_bindex_t bend;
  27637. + struct au_sbinfo *sbinfo;
  27638. + struct super_block *sb;
  27639. + struct seq_file *seq;
  27640. + char *name;
  27641. + struct attribute **cattr;
  27642. +
  27643. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  27644. + sb = sbinfo->si_sb;
  27645. +
  27646. + /*
  27647. + * prevent a race condition between sysfs and aufs.
  27648. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  27649. + * prohibits maintaining the sysfs entries.
  27650. + * hew we acquire read lock after sysfs_get_active_two().
  27651. + * on the other hand, the remount process may maintain the sysfs/aufs
  27652. + * entries after acquiring write lock.
  27653. + * it can cause a deadlock.
  27654. + * simply we gave up processing read here.
  27655. + */
  27656. + err = -EBUSY;
  27657. + if (unlikely(!si_noflush_read_trylock(sb)))
  27658. + goto out;
  27659. +
  27660. + seq = au_seq(buf, PAGE_SIZE);
  27661. + err = PTR_ERR(seq);
  27662. + if (IS_ERR(seq))
  27663. + goto out_unlock;
  27664. +
  27665. + name = (void *)attr->name;
  27666. + cattr = sysaufs_si_attrs;
  27667. + while (*cattr) {
  27668. + if (!strcmp(name, (*cattr)->name)) {
  27669. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  27670. + ->show(seq, sb);
  27671. + goto out_seq;
  27672. + }
  27673. + cattr++;
  27674. + }
  27675. +
  27676. + if (!strncmp(name, SysaufsBrid_PREFIX,
  27677. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  27678. + idx = AuBrSysfs_BRID;
  27679. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  27680. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  27681. + sizeof(SysaufsBr_PREFIX) - 1)) {
  27682. + idx = AuBrSysfs_BR;
  27683. + name += sizeof(SysaufsBr_PREFIX) - 1;
  27684. + } else
  27685. + BUG();
  27686. +
  27687. + err = kstrtol(name, 10, &l);
  27688. + if (!err) {
  27689. + bend = au_sbend(sb);
  27690. + if (l <= bend)
  27691. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  27692. + else
  27693. + err = -ENOENT;
  27694. + }
  27695. +
  27696. +out_seq:
  27697. + if (!err) {
  27698. + err = seq->count;
  27699. + /* sysfs limit */
  27700. + if (unlikely(err == PAGE_SIZE))
  27701. + err = -EFBIG;
  27702. + }
  27703. + kfree(seq);
  27704. +out_unlock:
  27705. + si_read_unlock(sb);
  27706. +out:
  27707. + return err;
  27708. +}
  27709. +
  27710. +/* ---------------------------------------------------------------------- */
  27711. +
  27712. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  27713. +{
  27714. + int err;
  27715. + int16_t brid;
  27716. + aufs_bindex_t bindex, bend;
  27717. + size_t sz;
  27718. + char *buf;
  27719. + struct seq_file *seq;
  27720. + struct au_branch *br;
  27721. +
  27722. + si_read_lock(sb, AuLock_FLUSH);
  27723. + bend = au_sbend(sb);
  27724. + err = bend + 1;
  27725. + if (!arg)
  27726. + goto out;
  27727. +
  27728. + err = -ENOMEM;
  27729. + buf = (void *)__get_free_page(GFP_NOFS);
  27730. + if (unlikely(!buf))
  27731. + goto out;
  27732. +
  27733. + seq = au_seq(buf, PAGE_SIZE);
  27734. + err = PTR_ERR(seq);
  27735. + if (IS_ERR(seq))
  27736. + goto out_buf;
  27737. +
  27738. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  27739. + for (bindex = 0; bindex <= bend; bindex++, arg++) {
  27740. + err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
  27741. + if (unlikely(err))
  27742. + break;
  27743. +
  27744. + br = au_sbr(sb, bindex);
  27745. + brid = br->br_id;
  27746. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  27747. + err = __put_user(brid, &arg->id);
  27748. + if (unlikely(err))
  27749. + break;
  27750. +
  27751. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  27752. + err = __put_user(br->br_perm, &arg->perm);
  27753. + if (unlikely(err))
  27754. + break;
  27755. +
  27756. + err = au_seq_path(seq, &br->br_path);
  27757. + if (unlikely(err))
  27758. + break;
  27759. + err = seq_putc(seq, '\0');
  27760. + if (!err && seq->count <= sz) {
  27761. + err = copy_to_user(arg->path, seq->buf, seq->count);
  27762. + seq->count = 0;
  27763. + if (unlikely(err))
  27764. + break;
  27765. + } else {
  27766. + err = -E2BIG;
  27767. + goto out_seq;
  27768. + }
  27769. + }
  27770. + if (unlikely(err))
  27771. + err = -EFAULT;
  27772. +
  27773. +out_seq:
  27774. + kfree(seq);
  27775. +out_buf:
  27776. + free_page((unsigned long)buf);
  27777. +out:
  27778. + si_read_unlock(sb);
  27779. + return err;
  27780. +}
  27781. +
  27782. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  27783. +{
  27784. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  27785. +}
  27786. +
  27787. +#ifdef CONFIG_COMPAT
  27788. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  27789. +{
  27790. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  27791. +}
  27792. +#endif
  27793. +
  27794. +/* ---------------------------------------------------------------------- */
  27795. +
  27796. +void sysaufs_br_init(struct au_branch *br)
  27797. +{
  27798. + int i;
  27799. + struct au_brsysfs *br_sysfs;
  27800. + struct attribute *attr;
  27801. +
  27802. + br_sysfs = br->br_sysfs;
  27803. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  27804. + attr = &br_sysfs->attr;
  27805. + sysfs_attr_init(attr);
  27806. + attr->name = br_sysfs->name;
  27807. + attr->mode = S_IRUGO;
  27808. + br_sysfs++;
  27809. + }
  27810. +}
  27811. +
  27812. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  27813. +{
  27814. + struct au_branch *br;
  27815. + struct kobject *kobj;
  27816. + struct au_brsysfs *br_sysfs;
  27817. + int i;
  27818. + aufs_bindex_t bend;
  27819. +
  27820. + dbgaufs_brs_del(sb, bindex);
  27821. +
  27822. + if (!sysaufs_brs)
  27823. + return;
  27824. +
  27825. + kobj = &au_sbi(sb)->si_kobj;
  27826. + bend = au_sbend(sb);
  27827. + for (; bindex <= bend; bindex++) {
  27828. + br = au_sbr(sb, bindex);
  27829. + br_sysfs = br->br_sysfs;
  27830. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  27831. + sysfs_remove_file(kobj, &br_sysfs->attr);
  27832. + br_sysfs++;
  27833. + }
  27834. + }
  27835. +}
  27836. +
  27837. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  27838. +{
  27839. + int err, i;
  27840. + aufs_bindex_t bend;
  27841. + struct kobject *kobj;
  27842. + struct au_branch *br;
  27843. + struct au_brsysfs *br_sysfs;
  27844. +
  27845. + dbgaufs_brs_add(sb, bindex);
  27846. +
  27847. + if (!sysaufs_brs)
  27848. + return;
  27849. +
  27850. + kobj = &au_sbi(sb)->si_kobj;
  27851. + bend = au_sbend(sb);
  27852. + for (; bindex <= bend; bindex++) {
  27853. + br = au_sbr(sb, bindex);
  27854. + br_sysfs = br->br_sysfs;
  27855. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  27856. + SysaufsBr_PREFIX "%d", bindex);
  27857. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  27858. + SysaufsBrid_PREFIX "%d", bindex);
  27859. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  27860. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  27861. + if (unlikely(err))
  27862. + pr_warn("failed %s under sysfs(%d)\n",
  27863. + br_sysfs->name, err);
  27864. + br_sysfs++;
  27865. + }
  27866. + }
  27867. +}
  27868. diff -Nur linux-4.1.10.orig/fs/aufs/sysrq.c linux-4.1.10/fs/aufs/sysrq.c
  27869. --- linux-4.1.10.orig/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
  27870. +++ linux-4.1.10/fs/aufs/sysrq.c 2015-10-22 21:35:53.000000000 +0200
  27871. @@ -0,0 +1,157 @@
  27872. +/*
  27873. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  27874. + *
  27875. + * This program, aufs is free software; you can redistribute it and/or modify
  27876. + * it under the terms of the GNU General Public License as published by
  27877. + * the Free Software Foundation; either version 2 of the License, or
  27878. + * (at your option) any later version.
  27879. + *
  27880. + * This program is distributed in the hope that it will be useful,
  27881. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27882. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27883. + * GNU General Public License for more details.
  27884. + *
  27885. + * You should have received a copy of the GNU General Public License
  27886. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27887. + */
  27888. +
  27889. +/*
  27890. + * magic sysrq hanlder
  27891. + */
  27892. +
  27893. +/* #include <linux/sysrq.h> */
  27894. +#include <linux/writeback.h>
  27895. +#include "aufs.h"
  27896. +
  27897. +/* ---------------------------------------------------------------------- */
  27898. +
  27899. +static void sysrq_sb(struct super_block *sb)
  27900. +{
  27901. + char *plevel;
  27902. + struct au_sbinfo *sbinfo;
  27903. + struct file *file;
  27904. + struct au_sphlhead *files;
  27905. + struct au_finfo *finfo;
  27906. +
  27907. + plevel = au_plevel;
  27908. + au_plevel = KERN_WARNING;
  27909. +
  27910. + /* since we define pr_fmt, call printk directly */
  27911. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  27912. +
  27913. + sbinfo = au_sbi(sb);
  27914. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  27915. + pr("superblock\n");
  27916. + au_dpri_sb(sb);
  27917. +
  27918. +#if 0
  27919. + pr("root dentry\n");
  27920. + au_dpri_dentry(sb->s_root);
  27921. + pr("root inode\n");
  27922. + au_dpri_inode(d_inode(sb->s_root));
  27923. +#endif
  27924. +
  27925. +#if 0
  27926. + do {
  27927. + int err, i, j, ndentry;
  27928. + struct au_dcsub_pages dpages;
  27929. + struct au_dpage *dpage;
  27930. +
  27931. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  27932. + if (unlikely(err))
  27933. + break;
  27934. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  27935. + if (!err)
  27936. + for (i = 0; i < dpages.ndpage; i++) {
  27937. + dpage = dpages.dpages + i;
  27938. + ndentry = dpage->ndentry;
  27939. + for (j = 0; j < ndentry; j++)
  27940. + au_dpri_dentry(dpage->dentries[j]);
  27941. + }
  27942. + au_dpages_free(&dpages);
  27943. + } while (0);
  27944. +#endif
  27945. +
  27946. +#if 1
  27947. + {
  27948. + struct inode *i;
  27949. +
  27950. + pr("isolated inode\n");
  27951. + spin_lock(&inode_sb_list_lock);
  27952. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  27953. + spin_lock(&i->i_lock);
  27954. + if (1 || hlist_empty(&i->i_dentry))
  27955. + au_dpri_inode(i);
  27956. + spin_unlock(&i->i_lock);
  27957. + }
  27958. + spin_unlock(&inode_sb_list_lock);
  27959. + }
  27960. +#endif
  27961. + pr("files\n");
  27962. + files = &au_sbi(sb)->si_files;
  27963. + spin_lock(&files->spin);
  27964. + hlist_for_each_entry(finfo, &files->head, fi_hlist) {
  27965. + umode_t mode;
  27966. +
  27967. + file = finfo->fi_file;
  27968. + mode = file_inode(file)->i_mode;
  27969. + if (!special_file(mode))
  27970. + au_dpri_file(file);
  27971. + }
  27972. + spin_unlock(&files->spin);
  27973. + pr("done\n");
  27974. +
  27975. +#undef pr
  27976. + au_plevel = plevel;
  27977. +}
  27978. +
  27979. +/* ---------------------------------------------------------------------- */
  27980. +
  27981. +/* module parameter */
  27982. +static char *aufs_sysrq_key = "a";
  27983. +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
  27984. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  27985. +
  27986. +static void au_sysrq(int key __maybe_unused)
  27987. +{
  27988. + struct au_sbinfo *sbinfo;
  27989. +
  27990. + lockdep_off();
  27991. + au_sbilist_lock();
  27992. + list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
  27993. + sysrq_sb(sbinfo->si_sb);
  27994. + au_sbilist_unlock();
  27995. + lockdep_on();
  27996. +}
  27997. +
  27998. +static struct sysrq_key_op au_sysrq_op = {
  27999. + .handler = au_sysrq,
  28000. + .help_msg = "Aufs",
  28001. + .action_msg = "Aufs",
  28002. + .enable_mask = SYSRQ_ENABLE_DUMP
  28003. +};
  28004. +
  28005. +/* ---------------------------------------------------------------------- */
  28006. +
  28007. +int __init au_sysrq_init(void)
  28008. +{
  28009. + int err;
  28010. + char key;
  28011. +
  28012. + err = -1;
  28013. + key = *aufs_sysrq_key;
  28014. + if ('a' <= key && key <= 'z')
  28015. + err = register_sysrq_key(key, &au_sysrq_op);
  28016. + if (unlikely(err))
  28017. + pr_err("err %d, sysrq=%c\n", err, key);
  28018. + return err;
  28019. +}
  28020. +
  28021. +void au_sysrq_fin(void)
  28022. +{
  28023. + int err;
  28024. +
  28025. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  28026. + if (unlikely(err))
  28027. + pr_err("err %d (ignored)\n", err);
  28028. +}
  28029. diff -Nur linux-4.1.10.orig/fs/aufs/vdir.c linux-4.1.10/fs/aufs/vdir.c
  28030. --- linux-4.1.10.orig/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
  28031. +++ linux-4.1.10/fs/aufs/vdir.c 2015-10-22 21:35:53.000000000 +0200
  28032. @@ -0,0 +1,888 @@
  28033. +/*
  28034. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  28035. + *
  28036. + * This program, aufs is free software; you can redistribute it and/or modify
  28037. + * it under the terms of the GNU General Public License as published by
  28038. + * the Free Software Foundation; either version 2 of the License, or
  28039. + * (at your option) any later version.
  28040. + *
  28041. + * This program is distributed in the hope that it will be useful,
  28042. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28043. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28044. + * GNU General Public License for more details.
  28045. + *
  28046. + * You should have received a copy of the GNU General Public License
  28047. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28048. + */
  28049. +
  28050. +/*
  28051. + * virtual or vertical directory
  28052. + */
  28053. +
  28054. +#include "aufs.h"
  28055. +
  28056. +static unsigned int calc_size(int nlen)
  28057. +{
  28058. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  28059. +}
  28060. +
  28061. +static int set_deblk_end(union au_vdir_deblk_p *p,
  28062. + union au_vdir_deblk_p *deblk_end)
  28063. +{
  28064. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  28065. + p->de->de_str.len = 0;
  28066. + /* smp_mb(); */
  28067. + return 0;
  28068. + }
  28069. + return -1; /* error */
  28070. +}
  28071. +
  28072. +/* returns true or false */
  28073. +static int is_deblk_end(union au_vdir_deblk_p *p,
  28074. + union au_vdir_deblk_p *deblk_end)
  28075. +{
  28076. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  28077. + return !p->de->de_str.len;
  28078. + return 1;
  28079. +}
  28080. +
  28081. +static unsigned char *last_deblk(struct au_vdir *vdir)
  28082. +{
  28083. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  28084. +}
  28085. +
  28086. +/* ---------------------------------------------------------------------- */
  28087. +
  28088. +/* estimate the appropriate size for name hash table */
  28089. +unsigned int au_rdhash_est(loff_t sz)
  28090. +{
  28091. + unsigned int n;
  28092. +
  28093. + n = UINT_MAX;
  28094. + sz >>= 10;
  28095. + if (sz < n)
  28096. + n = sz;
  28097. + if (sz < AUFS_RDHASH_DEF)
  28098. + n = AUFS_RDHASH_DEF;
  28099. + /* pr_info("n %u\n", n); */
  28100. + return n;
  28101. +}
  28102. +
  28103. +/*
  28104. + * the allocated memory has to be freed by
  28105. + * au_nhash_wh_free() or au_nhash_de_free().
  28106. + */
  28107. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  28108. +{
  28109. + struct hlist_head *head;
  28110. + unsigned int u;
  28111. + size_t sz;
  28112. +
  28113. + sz = sizeof(*nhash->nh_head) * num_hash;
  28114. + head = kmalloc(sz, gfp);
  28115. + if (head) {
  28116. + nhash->nh_num = num_hash;
  28117. + nhash->nh_head = head;
  28118. + for (u = 0; u < num_hash; u++)
  28119. + INIT_HLIST_HEAD(head++);
  28120. + return 0; /* success */
  28121. + }
  28122. +
  28123. + return -ENOMEM;
  28124. +}
  28125. +
  28126. +static void nhash_count(struct hlist_head *head)
  28127. +{
  28128. +#if 0
  28129. + unsigned long n;
  28130. + struct hlist_node *pos;
  28131. +
  28132. + n = 0;
  28133. + hlist_for_each(pos, head)
  28134. + n++;
  28135. + pr_info("%lu\n", n);
  28136. +#endif
  28137. +}
  28138. +
  28139. +static void au_nhash_wh_do_free(struct hlist_head *head)
  28140. +{
  28141. + struct au_vdir_wh *pos;
  28142. + struct hlist_node *node;
  28143. +
  28144. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  28145. + kfree(pos);
  28146. +}
  28147. +
  28148. +static void au_nhash_de_do_free(struct hlist_head *head)
  28149. +{
  28150. + struct au_vdir_dehstr *pos;
  28151. + struct hlist_node *node;
  28152. +
  28153. + hlist_for_each_entry_safe(pos, node, head, hash)
  28154. + au_cache_free_vdir_dehstr(pos);
  28155. +}
  28156. +
  28157. +static void au_nhash_do_free(struct au_nhash *nhash,
  28158. + void (*free)(struct hlist_head *head))
  28159. +{
  28160. + unsigned int n;
  28161. + struct hlist_head *head;
  28162. +
  28163. + n = nhash->nh_num;
  28164. + if (!n)
  28165. + return;
  28166. +
  28167. + head = nhash->nh_head;
  28168. + while (n-- > 0) {
  28169. + nhash_count(head);
  28170. + free(head++);
  28171. + }
  28172. + kfree(nhash->nh_head);
  28173. +}
  28174. +
  28175. +void au_nhash_wh_free(struct au_nhash *whlist)
  28176. +{
  28177. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  28178. +}
  28179. +
  28180. +static void au_nhash_de_free(struct au_nhash *delist)
  28181. +{
  28182. + au_nhash_do_free(delist, au_nhash_de_do_free);
  28183. +}
  28184. +
  28185. +/* ---------------------------------------------------------------------- */
  28186. +
  28187. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  28188. + int limit)
  28189. +{
  28190. + int num;
  28191. + unsigned int u, n;
  28192. + struct hlist_head *head;
  28193. + struct au_vdir_wh *pos;
  28194. +
  28195. + num = 0;
  28196. + n = whlist->nh_num;
  28197. + head = whlist->nh_head;
  28198. + for (u = 0; u < n; u++, head++)
  28199. + hlist_for_each_entry(pos, head, wh_hash)
  28200. + if (pos->wh_bindex == btgt && ++num > limit)
  28201. + return 1;
  28202. + return 0;
  28203. +}
  28204. +
  28205. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  28206. + unsigned char *name,
  28207. + unsigned int len)
  28208. +{
  28209. + unsigned int v;
  28210. + /* const unsigned int magic_bit = 12; */
  28211. +
  28212. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  28213. +
  28214. + v = 0;
  28215. + while (len--)
  28216. + v += *name++;
  28217. + /* v = hash_long(v, magic_bit); */
  28218. + v %= nhash->nh_num;
  28219. + return nhash->nh_head + v;
  28220. +}
  28221. +
  28222. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  28223. + int nlen)
  28224. +{
  28225. + return str->len == nlen && !memcmp(str->name, name, nlen);
  28226. +}
  28227. +
  28228. +/* returns found or not */
  28229. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  28230. +{
  28231. + struct hlist_head *head;
  28232. + struct au_vdir_wh *pos;
  28233. + struct au_vdir_destr *str;
  28234. +
  28235. + head = au_name_hash(whlist, name, nlen);
  28236. + hlist_for_each_entry(pos, head, wh_hash) {
  28237. + str = &pos->wh_str;
  28238. + AuDbg("%.*s\n", str->len, str->name);
  28239. + if (au_nhash_test_name(str, name, nlen))
  28240. + return 1;
  28241. + }
  28242. + return 0;
  28243. +}
  28244. +
  28245. +/* returns found(true) or not */
  28246. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  28247. +{
  28248. + struct hlist_head *head;
  28249. + struct au_vdir_dehstr *pos;
  28250. + struct au_vdir_destr *str;
  28251. +
  28252. + head = au_name_hash(delist, name, nlen);
  28253. + hlist_for_each_entry(pos, head, hash) {
  28254. + str = pos->str;
  28255. + AuDbg("%.*s\n", str->len, str->name);
  28256. + if (au_nhash_test_name(str, name, nlen))
  28257. + return 1;
  28258. + }
  28259. + return 0;
  28260. +}
  28261. +
  28262. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  28263. + unsigned char d_type)
  28264. +{
  28265. +#ifdef CONFIG_AUFS_SHWH
  28266. + wh->wh_ino = ino;
  28267. + wh->wh_type = d_type;
  28268. +#endif
  28269. +}
  28270. +
  28271. +/* ---------------------------------------------------------------------- */
  28272. +
  28273. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  28274. + unsigned int d_type, aufs_bindex_t bindex,
  28275. + unsigned char shwh)
  28276. +{
  28277. + int err;
  28278. + struct au_vdir_destr *str;
  28279. + struct au_vdir_wh *wh;
  28280. +
  28281. + AuDbg("%.*s\n", nlen, name);
  28282. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  28283. +
  28284. + err = -ENOMEM;
  28285. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  28286. + if (unlikely(!wh))
  28287. + goto out;
  28288. +
  28289. + err = 0;
  28290. + wh->wh_bindex = bindex;
  28291. + if (shwh)
  28292. + au_shwh_init_wh(wh, ino, d_type);
  28293. + str = &wh->wh_str;
  28294. + str->len = nlen;
  28295. + memcpy(str->name, name, nlen);
  28296. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  28297. + /* smp_mb(); */
  28298. +
  28299. +out:
  28300. + return err;
  28301. +}
  28302. +
  28303. +static int append_deblk(struct au_vdir *vdir)
  28304. +{
  28305. + int err;
  28306. + unsigned long ul;
  28307. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  28308. + union au_vdir_deblk_p p, deblk_end;
  28309. + unsigned char **o;
  28310. +
  28311. + err = -ENOMEM;
  28312. + o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  28313. + GFP_NOFS);
  28314. + if (unlikely(!o))
  28315. + goto out;
  28316. +
  28317. + vdir->vd_deblk = o;
  28318. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  28319. + if (p.deblk) {
  28320. + ul = vdir->vd_nblk++;
  28321. + vdir->vd_deblk[ul] = p.deblk;
  28322. + vdir->vd_last.ul = ul;
  28323. + vdir->vd_last.p.deblk = p.deblk;
  28324. + deblk_end.deblk = p.deblk + deblk_sz;
  28325. + err = set_deblk_end(&p, &deblk_end);
  28326. + }
  28327. +
  28328. +out:
  28329. + return err;
  28330. +}
  28331. +
  28332. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  28333. + unsigned int d_type, struct au_nhash *delist)
  28334. +{
  28335. + int err;
  28336. + unsigned int sz;
  28337. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  28338. + union au_vdir_deblk_p p, *room, deblk_end;
  28339. + struct au_vdir_dehstr *dehstr;
  28340. +
  28341. + p.deblk = last_deblk(vdir);
  28342. + deblk_end.deblk = p.deblk + deblk_sz;
  28343. + room = &vdir->vd_last.p;
  28344. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  28345. + || !is_deblk_end(room, &deblk_end));
  28346. +
  28347. + sz = calc_size(nlen);
  28348. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  28349. + err = append_deblk(vdir);
  28350. + if (unlikely(err))
  28351. + goto out;
  28352. +
  28353. + p.deblk = last_deblk(vdir);
  28354. + deblk_end.deblk = p.deblk + deblk_sz;
  28355. + /* smp_mb(); */
  28356. + AuDebugOn(room->deblk != p.deblk);
  28357. + }
  28358. +
  28359. + err = -ENOMEM;
  28360. + dehstr = au_cache_alloc_vdir_dehstr();
  28361. + if (unlikely(!dehstr))
  28362. + goto out;
  28363. +
  28364. + dehstr->str = &room->de->de_str;
  28365. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  28366. + room->de->de_ino = ino;
  28367. + room->de->de_type = d_type;
  28368. + room->de->de_str.len = nlen;
  28369. + memcpy(room->de->de_str.name, name, nlen);
  28370. +
  28371. + err = 0;
  28372. + room->deblk += sz;
  28373. + if (unlikely(set_deblk_end(room, &deblk_end)))
  28374. + err = append_deblk(vdir);
  28375. + /* smp_mb(); */
  28376. +
  28377. +out:
  28378. + return err;
  28379. +}
  28380. +
  28381. +/* ---------------------------------------------------------------------- */
  28382. +
  28383. +void au_vdir_free(struct au_vdir *vdir)
  28384. +{
  28385. + unsigned char **deblk;
  28386. +
  28387. + deblk = vdir->vd_deblk;
  28388. + while (vdir->vd_nblk--)
  28389. + kfree(*deblk++);
  28390. + kfree(vdir->vd_deblk);
  28391. + au_cache_free_vdir(vdir);
  28392. +}
  28393. +
  28394. +static struct au_vdir *alloc_vdir(struct file *file)
  28395. +{
  28396. + struct au_vdir *vdir;
  28397. + struct super_block *sb;
  28398. + int err;
  28399. +
  28400. + sb = file->f_path.dentry->d_sb;
  28401. + SiMustAnyLock(sb);
  28402. +
  28403. + err = -ENOMEM;
  28404. + vdir = au_cache_alloc_vdir();
  28405. + if (unlikely(!vdir))
  28406. + goto out;
  28407. +
  28408. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  28409. + if (unlikely(!vdir->vd_deblk))
  28410. + goto out_free;
  28411. +
  28412. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  28413. + if (!vdir->vd_deblk_sz) {
  28414. + /* estimate the appropriate size for deblk */
  28415. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  28416. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  28417. + }
  28418. + vdir->vd_nblk = 0;
  28419. + vdir->vd_version = 0;
  28420. + vdir->vd_jiffy = 0;
  28421. + err = append_deblk(vdir);
  28422. + if (!err)
  28423. + return vdir; /* success */
  28424. +
  28425. + kfree(vdir->vd_deblk);
  28426. +
  28427. +out_free:
  28428. + au_cache_free_vdir(vdir);
  28429. +out:
  28430. + vdir = ERR_PTR(err);
  28431. + return vdir;
  28432. +}
  28433. +
  28434. +static int reinit_vdir(struct au_vdir *vdir)
  28435. +{
  28436. + int err;
  28437. + union au_vdir_deblk_p p, deblk_end;
  28438. +
  28439. + while (vdir->vd_nblk > 1) {
  28440. + kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
  28441. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  28442. + vdir->vd_nblk--;
  28443. + }
  28444. + p.deblk = vdir->vd_deblk[0];
  28445. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  28446. + err = set_deblk_end(&p, &deblk_end);
  28447. + /* keep vd_dblk_sz */
  28448. + vdir->vd_last.ul = 0;
  28449. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  28450. + vdir->vd_version = 0;
  28451. + vdir->vd_jiffy = 0;
  28452. + /* smp_mb(); */
  28453. + return err;
  28454. +}
  28455. +
  28456. +/* ---------------------------------------------------------------------- */
  28457. +
  28458. +#define AuFillVdir_CALLED 1
  28459. +#define AuFillVdir_WHABLE (1 << 1)
  28460. +#define AuFillVdir_SHWH (1 << 2)
  28461. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  28462. +#define au_fset_fillvdir(flags, name) \
  28463. + do { (flags) |= AuFillVdir_##name; } while (0)
  28464. +#define au_fclr_fillvdir(flags, name) \
  28465. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  28466. +
  28467. +#ifndef CONFIG_AUFS_SHWH
  28468. +#undef AuFillVdir_SHWH
  28469. +#define AuFillVdir_SHWH 0
  28470. +#endif
  28471. +
  28472. +struct fillvdir_arg {
  28473. + struct dir_context ctx;
  28474. + struct file *file;
  28475. + struct au_vdir *vdir;
  28476. + struct au_nhash delist;
  28477. + struct au_nhash whlist;
  28478. + aufs_bindex_t bindex;
  28479. + unsigned int flags;
  28480. + int err;
  28481. +};
  28482. +
  28483. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  28484. + loff_t offset __maybe_unused, u64 h_ino,
  28485. + unsigned int d_type)
  28486. +{
  28487. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  28488. + char *name = (void *)__name;
  28489. + struct super_block *sb;
  28490. + ino_t ino;
  28491. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  28492. +
  28493. + arg->err = 0;
  28494. + sb = arg->file->f_path.dentry->d_sb;
  28495. + au_fset_fillvdir(arg->flags, CALLED);
  28496. + /* smp_mb(); */
  28497. + if (nlen <= AUFS_WH_PFX_LEN
  28498. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  28499. + if (test_known(&arg->delist, name, nlen)
  28500. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  28501. + goto out; /* already exists or whiteouted */
  28502. +
  28503. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  28504. + if (!arg->err) {
  28505. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  28506. + d_type = DT_UNKNOWN;
  28507. + arg->err = append_de(arg->vdir, name, nlen, ino,
  28508. + d_type, &arg->delist);
  28509. + }
  28510. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  28511. + name += AUFS_WH_PFX_LEN;
  28512. + nlen -= AUFS_WH_PFX_LEN;
  28513. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  28514. + goto out; /* already whiteouted */
  28515. +
  28516. + if (shwh)
  28517. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  28518. + &ino);
  28519. + if (!arg->err) {
  28520. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  28521. + d_type = DT_UNKNOWN;
  28522. + arg->err = au_nhash_append_wh
  28523. + (&arg->whlist, name, nlen, ino, d_type,
  28524. + arg->bindex, shwh);
  28525. + }
  28526. + }
  28527. +
  28528. +out:
  28529. + if (!arg->err)
  28530. + arg->vdir->vd_jiffy = jiffies;
  28531. + /* smp_mb(); */
  28532. + AuTraceErr(arg->err);
  28533. + return arg->err;
  28534. +}
  28535. +
  28536. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  28537. + struct au_nhash *whlist, struct au_nhash *delist)
  28538. +{
  28539. +#ifdef CONFIG_AUFS_SHWH
  28540. + int err;
  28541. + unsigned int nh, u;
  28542. + struct hlist_head *head;
  28543. + struct au_vdir_wh *pos;
  28544. + struct hlist_node *n;
  28545. + char *p, *o;
  28546. + struct au_vdir_destr *destr;
  28547. +
  28548. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  28549. +
  28550. + err = -ENOMEM;
  28551. + o = p = (void *)__get_free_page(GFP_NOFS);
  28552. + if (unlikely(!p))
  28553. + goto out;
  28554. +
  28555. + err = 0;
  28556. + nh = whlist->nh_num;
  28557. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  28558. + p += AUFS_WH_PFX_LEN;
  28559. + for (u = 0; u < nh; u++) {
  28560. + head = whlist->nh_head + u;
  28561. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  28562. + destr = &pos->wh_str;
  28563. + memcpy(p, destr->name, destr->len);
  28564. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  28565. + pos->wh_ino, pos->wh_type, delist);
  28566. + if (unlikely(err))
  28567. + break;
  28568. + }
  28569. + }
  28570. +
  28571. + free_page((unsigned long)o);
  28572. +
  28573. +out:
  28574. + AuTraceErr(err);
  28575. + return err;
  28576. +#else
  28577. + return 0;
  28578. +#endif
  28579. +}
  28580. +
  28581. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  28582. +{
  28583. + int err;
  28584. + unsigned int rdhash;
  28585. + loff_t offset;
  28586. + aufs_bindex_t bend, bindex, bstart;
  28587. + unsigned char shwh;
  28588. + struct file *hf, *file;
  28589. + struct super_block *sb;
  28590. +
  28591. + file = arg->file;
  28592. + sb = file->f_path.dentry->d_sb;
  28593. + SiMustAnyLock(sb);
  28594. +
  28595. + rdhash = au_sbi(sb)->si_rdhash;
  28596. + if (!rdhash)
  28597. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  28598. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  28599. + if (unlikely(err))
  28600. + goto out;
  28601. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  28602. + if (unlikely(err))
  28603. + goto out_delist;
  28604. +
  28605. + err = 0;
  28606. + arg->flags = 0;
  28607. + shwh = 0;
  28608. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  28609. + shwh = 1;
  28610. + au_fset_fillvdir(arg->flags, SHWH);
  28611. + }
  28612. + bstart = au_fbstart(file);
  28613. + bend = au_fbend_dir(file);
  28614. + for (bindex = bstart; !err && bindex <= bend; bindex++) {
  28615. + hf = au_hf_dir(file, bindex);
  28616. + if (!hf)
  28617. + continue;
  28618. +
  28619. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  28620. + err = offset;
  28621. + if (unlikely(offset))
  28622. + break;
  28623. +
  28624. + arg->bindex = bindex;
  28625. + au_fclr_fillvdir(arg->flags, WHABLE);
  28626. + if (shwh
  28627. + || (bindex != bend
  28628. + && au_br_whable(au_sbr_perm(sb, bindex))))
  28629. + au_fset_fillvdir(arg->flags, WHABLE);
  28630. + do {
  28631. + arg->err = 0;
  28632. + au_fclr_fillvdir(arg->flags, CALLED);
  28633. + /* smp_mb(); */
  28634. + err = vfsub_iterate_dir(hf, &arg->ctx);
  28635. + if (err >= 0)
  28636. + err = arg->err;
  28637. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  28638. +
  28639. + /*
  28640. + * dir_relax() may be good for concurrency, but aufs should not
  28641. + * use it since it will cause a lockdep problem.
  28642. + */
  28643. + }
  28644. +
  28645. + if (!err && shwh)
  28646. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  28647. +
  28648. + au_nhash_wh_free(&arg->whlist);
  28649. +
  28650. +out_delist:
  28651. + au_nhash_de_free(&arg->delist);
  28652. +out:
  28653. + return err;
  28654. +}
  28655. +
  28656. +static int read_vdir(struct file *file, int may_read)
  28657. +{
  28658. + int err;
  28659. + unsigned long expire;
  28660. + unsigned char do_read;
  28661. + struct fillvdir_arg arg = {
  28662. + .ctx = {
  28663. + .actor = fillvdir
  28664. + }
  28665. + };
  28666. + struct inode *inode;
  28667. + struct au_vdir *vdir, *allocated;
  28668. +
  28669. + err = 0;
  28670. + inode = file_inode(file);
  28671. + IMustLock(inode);
  28672. + SiMustAnyLock(inode->i_sb);
  28673. +
  28674. + allocated = NULL;
  28675. + do_read = 0;
  28676. + expire = au_sbi(inode->i_sb)->si_rdcache;
  28677. + vdir = au_ivdir(inode);
  28678. + if (!vdir) {
  28679. + do_read = 1;
  28680. + vdir = alloc_vdir(file);
  28681. + err = PTR_ERR(vdir);
  28682. + if (IS_ERR(vdir))
  28683. + goto out;
  28684. + err = 0;
  28685. + allocated = vdir;
  28686. + } else if (may_read
  28687. + && (inode->i_version != vdir->vd_version
  28688. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  28689. + do_read = 1;
  28690. + err = reinit_vdir(vdir);
  28691. + if (unlikely(err))
  28692. + goto out;
  28693. + }
  28694. +
  28695. + if (!do_read)
  28696. + return 0; /* success */
  28697. +
  28698. + arg.file = file;
  28699. + arg.vdir = vdir;
  28700. + err = au_do_read_vdir(&arg);
  28701. + if (!err) {
  28702. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  28703. + vdir->vd_version = inode->i_version;
  28704. + vdir->vd_last.ul = 0;
  28705. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  28706. + if (allocated)
  28707. + au_set_ivdir(inode, allocated);
  28708. + } else if (allocated)
  28709. + au_vdir_free(allocated);
  28710. +
  28711. +out:
  28712. + return err;
  28713. +}
  28714. +
  28715. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  28716. +{
  28717. + int err, rerr;
  28718. + unsigned long ul, n;
  28719. + const unsigned int deblk_sz = src->vd_deblk_sz;
  28720. +
  28721. + AuDebugOn(tgt->vd_nblk != 1);
  28722. +
  28723. + err = -ENOMEM;
  28724. + if (tgt->vd_nblk < src->vd_nblk) {
  28725. + unsigned char **p;
  28726. +
  28727. + p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  28728. + GFP_NOFS);
  28729. + if (unlikely(!p))
  28730. + goto out;
  28731. + tgt->vd_deblk = p;
  28732. + }
  28733. +
  28734. + if (tgt->vd_deblk_sz != deblk_sz) {
  28735. + unsigned char *p;
  28736. +
  28737. + tgt->vd_deblk_sz = deblk_sz;
  28738. + p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
  28739. + if (unlikely(!p))
  28740. + goto out;
  28741. + tgt->vd_deblk[0] = p;
  28742. + }
  28743. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  28744. + tgt->vd_version = src->vd_version;
  28745. + tgt->vd_jiffy = src->vd_jiffy;
  28746. +
  28747. + n = src->vd_nblk;
  28748. + for (ul = 1; ul < n; ul++) {
  28749. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  28750. + GFP_NOFS);
  28751. + if (unlikely(!tgt->vd_deblk[ul]))
  28752. + goto out;
  28753. + tgt->vd_nblk++;
  28754. + }
  28755. + tgt->vd_nblk = n;
  28756. + tgt->vd_last.ul = tgt->vd_last.ul;
  28757. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  28758. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  28759. + - src->vd_deblk[src->vd_last.ul];
  28760. + /* smp_mb(); */
  28761. + return 0; /* success */
  28762. +
  28763. +out:
  28764. + rerr = reinit_vdir(tgt);
  28765. + BUG_ON(rerr);
  28766. + return err;
  28767. +}
  28768. +
  28769. +int au_vdir_init(struct file *file)
  28770. +{
  28771. + int err;
  28772. + struct inode *inode;
  28773. + struct au_vdir *vdir_cache, *allocated;
  28774. +
  28775. + /* test file->f_pos here instead of ctx->pos */
  28776. + err = read_vdir(file, !file->f_pos);
  28777. + if (unlikely(err))
  28778. + goto out;
  28779. +
  28780. + allocated = NULL;
  28781. + vdir_cache = au_fvdir_cache(file);
  28782. + if (!vdir_cache) {
  28783. + vdir_cache = alloc_vdir(file);
  28784. + err = PTR_ERR(vdir_cache);
  28785. + if (IS_ERR(vdir_cache))
  28786. + goto out;
  28787. + allocated = vdir_cache;
  28788. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  28789. + /* test file->f_pos here instead of ctx->pos */
  28790. + err = reinit_vdir(vdir_cache);
  28791. + if (unlikely(err))
  28792. + goto out;
  28793. + } else
  28794. + return 0; /* success */
  28795. +
  28796. + inode = file_inode(file);
  28797. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  28798. + if (!err) {
  28799. + file->f_version = inode->i_version;
  28800. + if (allocated)
  28801. + au_set_fvdir_cache(file, allocated);
  28802. + } else if (allocated)
  28803. + au_vdir_free(allocated);
  28804. +
  28805. +out:
  28806. + return err;
  28807. +}
  28808. +
  28809. +static loff_t calc_offset(struct au_vdir *vdir)
  28810. +{
  28811. + loff_t offset;
  28812. + union au_vdir_deblk_p p;
  28813. +
  28814. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  28815. + offset = vdir->vd_last.p.deblk - p.deblk;
  28816. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  28817. + return offset;
  28818. +}
  28819. +
  28820. +/* returns true or false */
  28821. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  28822. +{
  28823. + int valid;
  28824. + unsigned int deblk_sz;
  28825. + unsigned long ul, n;
  28826. + loff_t offset;
  28827. + union au_vdir_deblk_p p, deblk_end;
  28828. + struct au_vdir *vdir_cache;
  28829. +
  28830. + valid = 1;
  28831. + vdir_cache = au_fvdir_cache(file);
  28832. + offset = calc_offset(vdir_cache);
  28833. + AuDbg("offset %lld\n", offset);
  28834. + if (ctx->pos == offset)
  28835. + goto out;
  28836. +
  28837. + vdir_cache->vd_last.ul = 0;
  28838. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  28839. + if (!ctx->pos)
  28840. + goto out;
  28841. +
  28842. + valid = 0;
  28843. + deblk_sz = vdir_cache->vd_deblk_sz;
  28844. + ul = div64_u64(ctx->pos, deblk_sz);
  28845. + AuDbg("ul %lu\n", ul);
  28846. + if (ul >= vdir_cache->vd_nblk)
  28847. + goto out;
  28848. +
  28849. + n = vdir_cache->vd_nblk;
  28850. + for (; ul < n; ul++) {
  28851. + p.deblk = vdir_cache->vd_deblk[ul];
  28852. + deblk_end.deblk = p.deblk + deblk_sz;
  28853. + offset = ul;
  28854. + offset *= deblk_sz;
  28855. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  28856. + unsigned int l;
  28857. +
  28858. + l = calc_size(p.de->de_str.len);
  28859. + offset += l;
  28860. + p.deblk += l;
  28861. + }
  28862. + if (!is_deblk_end(&p, &deblk_end)) {
  28863. + valid = 1;
  28864. + vdir_cache->vd_last.ul = ul;
  28865. + vdir_cache->vd_last.p = p;
  28866. + break;
  28867. + }
  28868. + }
  28869. +
  28870. +out:
  28871. + /* smp_mb(); */
  28872. + AuTraceErr(!valid);
  28873. + return valid;
  28874. +}
  28875. +
  28876. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  28877. +{
  28878. + unsigned int l, deblk_sz;
  28879. + union au_vdir_deblk_p deblk_end;
  28880. + struct au_vdir *vdir_cache;
  28881. + struct au_vdir_de *de;
  28882. +
  28883. + vdir_cache = au_fvdir_cache(file);
  28884. + if (!seek_vdir(file, ctx))
  28885. + return 0;
  28886. +
  28887. + deblk_sz = vdir_cache->vd_deblk_sz;
  28888. + while (1) {
  28889. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  28890. + deblk_end.deblk += deblk_sz;
  28891. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  28892. + de = vdir_cache->vd_last.p.de;
  28893. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  28894. + de->de_str.len, de->de_str.name, ctx->pos,
  28895. + (unsigned long)de->de_ino, de->de_type);
  28896. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  28897. + de->de_str.len, de->de_ino,
  28898. + de->de_type))) {
  28899. + /* todo: ignore the error caused by udba? */
  28900. + /* return err; */
  28901. + return 0;
  28902. + }
  28903. +
  28904. + l = calc_size(de->de_str.len);
  28905. + vdir_cache->vd_last.p.deblk += l;
  28906. + ctx->pos += l;
  28907. + }
  28908. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  28909. + vdir_cache->vd_last.ul++;
  28910. + vdir_cache->vd_last.p.deblk
  28911. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  28912. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  28913. + continue;
  28914. + }
  28915. + break;
  28916. + }
  28917. +
  28918. + /* smp_mb(); */
  28919. + return 0;
  28920. +}
  28921. diff -Nur linux-4.1.10.orig/fs/aufs/vfsub.c linux-4.1.10/fs/aufs/vfsub.c
  28922. --- linux-4.1.10.orig/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
  28923. +++ linux-4.1.10/fs/aufs/vfsub.c 2015-10-22 21:35:53.000000000 +0200
  28924. @@ -0,0 +1,848 @@
  28925. +/*
  28926. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  28927. + *
  28928. + * This program, aufs is free software; you can redistribute it and/or modify
  28929. + * it under the terms of the GNU General Public License as published by
  28930. + * the Free Software Foundation; either version 2 of the License, or
  28931. + * (at your option) any later version.
  28932. + *
  28933. + * This program is distributed in the hope that it will be useful,
  28934. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28935. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28936. + * GNU General Public License for more details.
  28937. + *
  28938. + * You should have received a copy of the GNU General Public License
  28939. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28940. + */
  28941. +
  28942. +/*
  28943. + * sub-routines for VFS
  28944. + */
  28945. +
  28946. +#include <linux/namei.h>
  28947. +#include <linux/security.h>
  28948. +#include <linux/splice.h>
  28949. +#include "aufs.h"
  28950. +
  28951. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  28952. +{
  28953. + int err;
  28954. + struct kstat st;
  28955. + struct super_block *h_sb;
  28956. +
  28957. + /* for remote fs, leave work for its getattr or d_revalidate */
  28958. + /* for bad i_attr fs, handle them in aufs_getattr() */
  28959. + /* still some fs may acquire i_mutex. we need to skip them */
  28960. + err = 0;
  28961. + if (!did)
  28962. + did = &err;
  28963. + h_sb = h_path->dentry->d_sb;
  28964. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  28965. + if (*did)
  28966. + err = vfs_getattr(h_path, &st);
  28967. +
  28968. + return err;
  28969. +}
  28970. +
  28971. +/* ---------------------------------------------------------------------- */
  28972. +
  28973. +struct file *vfsub_dentry_open(struct path *path, int flags)
  28974. +{
  28975. + struct file *file;
  28976. +
  28977. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  28978. + current_cred());
  28979. + if (!IS_ERR_OR_NULL(file)
  28980. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  28981. + i_readcount_inc(d_inode(path->dentry));
  28982. +
  28983. + return file;
  28984. +}
  28985. +
  28986. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  28987. +{
  28988. + struct file *file;
  28989. +
  28990. + lockdep_off();
  28991. + file = filp_open(path,
  28992. + oflags /* | __FMODE_NONOTIFY */,
  28993. + mode);
  28994. + lockdep_on();
  28995. + if (IS_ERR(file))
  28996. + goto out;
  28997. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  28998. +
  28999. +out:
  29000. + return file;
  29001. +}
  29002. +
  29003. +/*
  29004. + * Ideally this function should call VFS:do_last() in order to keep all its
  29005. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  29006. + * structure such as nameidata. This is a second (or third) best approach.
  29007. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  29008. + */
  29009. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  29010. + struct vfsub_aopen_args *args, struct au_branch *br)
  29011. +{
  29012. + int err;
  29013. + struct file *file = args->file;
  29014. + /* copied from linux/fs/namei.c:atomic_open() */
  29015. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  29016. +
  29017. + IMustLock(dir);
  29018. + AuDebugOn(!dir->i_op->atomic_open);
  29019. +
  29020. + err = au_br_test_oflag(args->open_flag, br);
  29021. + if (unlikely(err))
  29022. + goto out;
  29023. +
  29024. + args->file->f_path.dentry = DENTRY_NOT_SET;
  29025. + args->file->f_path.mnt = au_br_mnt(br);
  29026. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  29027. + args->create_mode, args->opened);
  29028. + if (err >= 0) {
  29029. + /* some filesystems don't set FILE_CREATED while succeeded? */
  29030. + if (*args->opened & FILE_CREATED)
  29031. + fsnotify_create(dir, dentry);
  29032. + } else
  29033. + goto out;
  29034. +
  29035. +
  29036. + if (!err) {
  29037. + /* todo: call VFS:may_open() here */
  29038. + err = open_check_o_direct(file);
  29039. + /* todo: ima_file_check() too? */
  29040. + if (!err && (args->open_flag & __FMODE_EXEC))
  29041. + err = deny_write_access(file);
  29042. + if (unlikely(err))
  29043. + /* note that the file is created and still opened */
  29044. + goto out;
  29045. + }
  29046. +
  29047. + atomic_inc(&br->br_count);
  29048. + fsnotify_open(file);
  29049. +
  29050. +out:
  29051. + return err;
  29052. +}
  29053. +
  29054. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  29055. +{
  29056. + int err;
  29057. +
  29058. + err = kern_path(name, flags, path);
  29059. + if (!err && d_is_positive(path->dentry))
  29060. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  29061. + return err;
  29062. +}
  29063. +
  29064. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  29065. + int len)
  29066. +{
  29067. + struct path path = {
  29068. + .mnt = NULL
  29069. + };
  29070. +
  29071. + /* VFS checks it too, but by WARN_ON_ONCE() */
  29072. + IMustLock(d_inode(parent));
  29073. +
  29074. + path.dentry = lookup_one_len(name, parent, len);
  29075. + if (IS_ERR(path.dentry))
  29076. + goto out;
  29077. + if (d_is_positive(path.dentry))
  29078. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  29079. +
  29080. +out:
  29081. + AuTraceErrPtr(path.dentry);
  29082. + return path.dentry;
  29083. +}
  29084. +
  29085. +void vfsub_call_lkup_one(void *args)
  29086. +{
  29087. + struct vfsub_lkup_one_args *a = args;
  29088. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  29089. +}
  29090. +
  29091. +/* ---------------------------------------------------------------------- */
  29092. +
  29093. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29094. + struct dentry *d2, struct au_hinode *hdir2)
  29095. +{
  29096. + struct dentry *d;
  29097. +
  29098. + lockdep_off();
  29099. + d = lock_rename(d1, d2);
  29100. + lockdep_on();
  29101. + au_hn_suspend(hdir1);
  29102. + if (hdir1 != hdir2)
  29103. + au_hn_suspend(hdir2);
  29104. +
  29105. + return d;
  29106. +}
  29107. +
  29108. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29109. + struct dentry *d2, struct au_hinode *hdir2)
  29110. +{
  29111. + au_hn_resume(hdir1);
  29112. + if (hdir1 != hdir2)
  29113. + au_hn_resume(hdir2);
  29114. + lockdep_off();
  29115. + unlock_rename(d1, d2);
  29116. + lockdep_on();
  29117. +}
  29118. +
  29119. +/* ---------------------------------------------------------------------- */
  29120. +
  29121. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  29122. +{
  29123. + int err;
  29124. + struct dentry *d;
  29125. +
  29126. + IMustLock(dir);
  29127. +
  29128. + d = path->dentry;
  29129. + path->dentry = d->d_parent;
  29130. + err = security_path_mknod(path, d, mode, 0);
  29131. + path->dentry = d;
  29132. + if (unlikely(err))
  29133. + goto out;
  29134. +
  29135. + lockdep_off();
  29136. + err = vfs_create(dir, path->dentry, mode, want_excl);
  29137. + lockdep_on();
  29138. + if (!err) {
  29139. + struct path tmp = *path;
  29140. + int did;
  29141. +
  29142. + vfsub_update_h_iattr(&tmp, &did);
  29143. + if (did) {
  29144. + tmp.dentry = path->dentry->d_parent;
  29145. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29146. + }
  29147. + /*ignore*/
  29148. + }
  29149. +
  29150. +out:
  29151. + return err;
  29152. +}
  29153. +
  29154. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  29155. +{
  29156. + int err;
  29157. + struct dentry *d;
  29158. +
  29159. + IMustLock(dir);
  29160. +
  29161. + d = path->dentry;
  29162. + path->dentry = d->d_parent;
  29163. + err = security_path_symlink(path, d, symname);
  29164. + path->dentry = d;
  29165. + if (unlikely(err))
  29166. + goto out;
  29167. +
  29168. + lockdep_off();
  29169. + err = vfs_symlink(dir, path->dentry, symname);
  29170. + lockdep_on();
  29171. + if (!err) {
  29172. + struct path tmp = *path;
  29173. + int did;
  29174. +
  29175. + vfsub_update_h_iattr(&tmp, &did);
  29176. + if (did) {
  29177. + tmp.dentry = path->dentry->d_parent;
  29178. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29179. + }
  29180. + /*ignore*/
  29181. + }
  29182. +
  29183. +out:
  29184. + return err;
  29185. +}
  29186. +
  29187. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  29188. +{
  29189. + int err;
  29190. + struct dentry *d;
  29191. +
  29192. + IMustLock(dir);
  29193. +
  29194. + d = path->dentry;
  29195. + path->dentry = d->d_parent;
  29196. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  29197. + path->dentry = d;
  29198. + if (unlikely(err))
  29199. + goto out;
  29200. +
  29201. + lockdep_off();
  29202. + err = vfs_mknod(dir, path->dentry, mode, dev);
  29203. + lockdep_on();
  29204. + if (!err) {
  29205. + struct path tmp = *path;
  29206. + int did;
  29207. +
  29208. + vfsub_update_h_iattr(&tmp, &did);
  29209. + if (did) {
  29210. + tmp.dentry = path->dentry->d_parent;
  29211. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29212. + }
  29213. + /*ignore*/
  29214. + }
  29215. +
  29216. +out:
  29217. + return err;
  29218. +}
  29219. +
  29220. +static int au_test_nlink(struct inode *inode)
  29221. +{
  29222. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  29223. +
  29224. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  29225. + || inode->i_nlink < link_max)
  29226. + return 0;
  29227. + return -EMLINK;
  29228. +}
  29229. +
  29230. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  29231. + struct inode **delegated_inode)
  29232. +{
  29233. + int err;
  29234. + struct dentry *d;
  29235. +
  29236. + IMustLock(dir);
  29237. +
  29238. + err = au_test_nlink(d_inode(src_dentry));
  29239. + if (unlikely(err))
  29240. + return err;
  29241. +
  29242. + /* we don't call may_linkat() */
  29243. + d = path->dentry;
  29244. + path->dentry = d->d_parent;
  29245. + err = security_path_link(src_dentry, path, d);
  29246. + path->dentry = d;
  29247. + if (unlikely(err))
  29248. + goto out;
  29249. +
  29250. + lockdep_off();
  29251. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  29252. + lockdep_on();
  29253. + if (!err) {
  29254. + struct path tmp = *path;
  29255. + int did;
  29256. +
  29257. + /* fuse has different memory inode for the same inumber */
  29258. + vfsub_update_h_iattr(&tmp, &did);
  29259. + if (did) {
  29260. + tmp.dentry = path->dentry->d_parent;
  29261. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29262. + tmp.dentry = src_dentry;
  29263. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29264. + }
  29265. + /*ignore*/
  29266. + }
  29267. +
  29268. +out:
  29269. + return err;
  29270. +}
  29271. +
  29272. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  29273. + struct inode *dir, struct path *path,
  29274. + struct inode **delegated_inode)
  29275. +{
  29276. + int err;
  29277. + struct path tmp = {
  29278. + .mnt = path->mnt
  29279. + };
  29280. + struct dentry *d;
  29281. +
  29282. + IMustLock(dir);
  29283. + IMustLock(src_dir);
  29284. +
  29285. + d = path->dentry;
  29286. + path->dentry = d->d_parent;
  29287. + tmp.dentry = src_dentry->d_parent;
  29288. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  29289. + path->dentry = d;
  29290. + if (unlikely(err))
  29291. + goto out;
  29292. +
  29293. + lockdep_off();
  29294. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  29295. + delegated_inode, /*flags*/0);
  29296. + lockdep_on();
  29297. + if (!err) {
  29298. + int did;
  29299. +
  29300. + tmp.dentry = d->d_parent;
  29301. + vfsub_update_h_iattr(&tmp, &did);
  29302. + if (did) {
  29303. + tmp.dentry = src_dentry;
  29304. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29305. + tmp.dentry = src_dentry->d_parent;
  29306. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29307. + }
  29308. + /*ignore*/
  29309. + }
  29310. +
  29311. +out:
  29312. + return err;
  29313. +}
  29314. +
  29315. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  29316. +{
  29317. + int err;
  29318. + struct dentry *d;
  29319. +
  29320. + IMustLock(dir);
  29321. +
  29322. + d = path->dentry;
  29323. + path->dentry = d->d_parent;
  29324. + err = security_path_mkdir(path, d, mode);
  29325. + path->dentry = d;
  29326. + if (unlikely(err))
  29327. + goto out;
  29328. +
  29329. + lockdep_off();
  29330. + err = vfs_mkdir(dir, path->dentry, mode);
  29331. + lockdep_on();
  29332. + if (!err) {
  29333. + struct path tmp = *path;
  29334. + int did;
  29335. +
  29336. + vfsub_update_h_iattr(&tmp, &did);
  29337. + if (did) {
  29338. + tmp.dentry = path->dentry->d_parent;
  29339. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  29340. + }
  29341. + /*ignore*/
  29342. + }
  29343. +
  29344. +out:
  29345. + return err;
  29346. +}
  29347. +
  29348. +int vfsub_rmdir(struct inode *dir, struct path *path)
  29349. +{
  29350. + int err;
  29351. + struct dentry *d;
  29352. +
  29353. + IMustLock(dir);
  29354. +
  29355. + d = path->dentry;
  29356. + path->dentry = d->d_parent;
  29357. + err = security_path_rmdir(path, d);
  29358. + path->dentry = d;
  29359. + if (unlikely(err))
  29360. + goto out;
  29361. +
  29362. + lockdep_off();
  29363. + err = vfs_rmdir(dir, path->dentry);
  29364. + lockdep_on();
  29365. + if (!err) {
  29366. + struct path tmp = {
  29367. + .dentry = path->dentry->d_parent,
  29368. + .mnt = path->mnt
  29369. + };
  29370. +
  29371. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  29372. + }
  29373. +
  29374. +out:
  29375. + return err;
  29376. +}
  29377. +
  29378. +/* ---------------------------------------------------------------------- */
  29379. +
  29380. +/* todo: support mmap_sem? */
  29381. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  29382. + loff_t *ppos)
  29383. +{
  29384. + ssize_t err;
  29385. +
  29386. + lockdep_off();
  29387. + err = vfs_read(file, ubuf, count, ppos);
  29388. + lockdep_on();
  29389. + if (err >= 0)
  29390. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29391. + return err;
  29392. +}
  29393. +
  29394. +/* todo: kernel_read()? */
  29395. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  29396. + loff_t *ppos)
  29397. +{
  29398. + ssize_t err;
  29399. + mm_segment_t oldfs;
  29400. + union {
  29401. + void *k;
  29402. + char __user *u;
  29403. + } buf;
  29404. +
  29405. + buf.k = kbuf;
  29406. + oldfs = get_fs();
  29407. + set_fs(KERNEL_DS);
  29408. + err = vfsub_read_u(file, buf.u, count, ppos);
  29409. + set_fs(oldfs);
  29410. + return err;
  29411. +}
  29412. +
  29413. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  29414. + loff_t *ppos)
  29415. +{
  29416. + ssize_t err;
  29417. +
  29418. + lockdep_off();
  29419. + err = vfs_write(file, ubuf, count, ppos);
  29420. + lockdep_on();
  29421. + if (err >= 0)
  29422. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29423. + return err;
  29424. +}
  29425. +
  29426. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  29427. +{
  29428. + ssize_t err;
  29429. + mm_segment_t oldfs;
  29430. + union {
  29431. + void *k;
  29432. + const char __user *u;
  29433. + } buf;
  29434. +
  29435. + buf.k = kbuf;
  29436. + oldfs = get_fs();
  29437. + set_fs(KERNEL_DS);
  29438. + err = vfsub_write_u(file, buf.u, count, ppos);
  29439. + set_fs(oldfs);
  29440. + return err;
  29441. +}
  29442. +
  29443. +int vfsub_flush(struct file *file, fl_owner_t id)
  29444. +{
  29445. + int err;
  29446. +
  29447. + err = 0;
  29448. + if (file->f_op->flush) {
  29449. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  29450. + err = file->f_op->flush(file, id);
  29451. + else {
  29452. + lockdep_off();
  29453. + err = file->f_op->flush(file, id);
  29454. + lockdep_on();
  29455. + }
  29456. + if (!err)
  29457. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  29458. + /*ignore*/
  29459. + }
  29460. + return err;
  29461. +}
  29462. +
  29463. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  29464. +{
  29465. + int err;
  29466. +
  29467. + AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
  29468. +
  29469. + lockdep_off();
  29470. + err = iterate_dir(file, ctx);
  29471. + lockdep_on();
  29472. + if (err >= 0)
  29473. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  29474. + return err;
  29475. +}
  29476. +
  29477. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  29478. + struct pipe_inode_info *pipe, size_t len,
  29479. + unsigned int flags)
  29480. +{
  29481. + long err;
  29482. +
  29483. + lockdep_off();
  29484. + err = do_splice_to(in, ppos, pipe, len, flags);
  29485. + lockdep_on();
  29486. + file_accessed(in);
  29487. + if (err >= 0)
  29488. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  29489. + return err;
  29490. +}
  29491. +
  29492. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  29493. + loff_t *ppos, size_t len, unsigned int flags)
  29494. +{
  29495. + long err;
  29496. +
  29497. + lockdep_off();
  29498. + err = do_splice_from(pipe, out, ppos, len, flags);
  29499. + lockdep_on();
  29500. + if (err >= 0)
  29501. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  29502. + return err;
  29503. +}
  29504. +
  29505. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  29506. +{
  29507. + int err;
  29508. +
  29509. + /* file can be NULL */
  29510. + lockdep_off();
  29511. + err = vfs_fsync(file, datasync);
  29512. + lockdep_on();
  29513. + if (!err) {
  29514. + if (!path) {
  29515. + AuDebugOn(!file);
  29516. + path = &file->f_path;
  29517. + }
  29518. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  29519. + }
  29520. + return err;
  29521. +}
  29522. +
  29523. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  29524. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  29525. + struct file *h_file)
  29526. +{
  29527. + int err;
  29528. + struct inode *h_inode;
  29529. + struct super_block *h_sb;
  29530. +
  29531. + if (!h_file) {
  29532. + err = vfsub_truncate(h_path, length);
  29533. + goto out;
  29534. + }
  29535. +
  29536. + h_inode = d_inode(h_path->dentry);
  29537. + h_sb = h_inode->i_sb;
  29538. + lockdep_off();
  29539. + sb_start_write(h_sb);
  29540. + lockdep_on();
  29541. + err = locks_verify_truncate(h_inode, h_file, length);
  29542. + if (!err)
  29543. + err = security_path_truncate(h_path);
  29544. + if (!err) {
  29545. + lockdep_off();
  29546. + err = do_truncate(h_path->dentry, length, attr, h_file);
  29547. + lockdep_on();
  29548. + }
  29549. + lockdep_off();
  29550. + sb_end_write(h_sb);
  29551. + lockdep_on();
  29552. +
  29553. +out:
  29554. + return err;
  29555. +}
  29556. +
  29557. +/* ---------------------------------------------------------------------- */
  29558. +
  29559. +struct au_vfsub_mkdir_args {
  29560. + int *errp;
  29561. + struct inode *dir;
  29562. + struct path *path;
  29563. + int mode;
  29564. +};
  29565. +
  29566. +static void au_call_vfsub_mkdir(void *args)
  29567. +{
  29568. + struct au_vfsub_mkdir_args *a = args;
  29569. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  29570. +}
  29571. +
  29572. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  29573. +{
  29574. + int err, do_sio, wkq_err;
  29575. +
  29576. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  29577. + if (!do_sio) {
  29578. + lockdep_off();
  29579. + err = vfsub_mkdir(dir, path, mode);
  29580. + lockdep_on();
  29581. + } else {
  29582. + struct au_vfsub_mkdir_args args = {
  29583. + .errp = &err,
  29584. + .dir = dir,
  29585. + .path = path,
  29586. + .mode = mode
  29587. + };
  29588. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  29589. + if (unlikely(wkq_err))
  29590. + err = wkq_err;
  29591. + }
  29592. +
  29593. + return err;
  29594. +}
  29595. +
  29596. +struct au_vfsub_rmdir_args {
  29597. + int *errp;
  29598. + struct inode *dir;
  29599. + struct path *path;
  29600. +};
  29601. +
  29602. +static void au_call_vfsub_rmdir(void *args)
  29603. +{
  29604. + struct au_vfsub_rmdir_args *a = args;
  29605. + *a->errp = vfsub_rmdir(a->dir, a->path);
  29606. +}
  29607. +
  29608. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  29609. +{
  29610. + int err, do_sio, wkq_err;
  29611. +
  29612. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  29613. + if (!do_sio) {
  29614. + lockdep_off();
  29615. + err = vfsub_rmdir(dir, path);
  29616. + lockdep_on();
  29617. + } else {
  29618. + struct au_vfsub_rmdir_args args = {
  29619. + .errp = &err,
  29620. + .dir = dir,
  29621. + .path = path
  29622. + };
  29623. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  29624. + if (unlikely(wkq_err))
  29625. + err = wkq_err;
  29626. + }
  29627. +
  29628. + return err;
  29629. +}
  29630. +
  29631. +/* ---------------------------------------------------------------------- */
  29632. +
  29633. +struct notify_change_args {
  29634. + int *errp;
  29635. + struct path *path;
  29636. + struct iattr *ia;
  29637. + struct inode **delegated_inode;
  29638. +};
  29639. +
  29640. +static void call_notify_change(void *args)
  29641. +{
  29642. + struct notify_change_args *a = args;
  29643. + struct inode *h_inode;
  29644. +
  29645. + h_inode = d_inode(a->path->dentry);
  29646. + IMustLock(h_inode);
  29647. +
  29648. + *a->errp = -EPERM;
  29649. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  29650. + lockdep_off();
  29651. + *a->errp = notify_change(a->path->dentry, a->ia,
  29652. + a->delegated_inode);
  29653. + lockdep_on();
  29654. + if (!*a->errp)
  29655. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  29656. + }
  29657. + AuTraceErr(*a->errp);
  29658. +}
  29659. +
  29660. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  29661. + struct inode **delegated_inode)
  29662. +{
  29663. + int err;
  29664. + struct notify_change_args args = {
  29665. + .errp = &err,
  29666. + .path = path,
  29667. + .ia = ia,
  29668. + .delegated_inode = delegated_inode
  29669. + };
  29670. +
  29671. + call_notify_change(&args);
  29672. +
  29673. + return err;
  29674. +}
  29675. +
  29676. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  29677. + struct inode **delegated_inode)
  29678. +{
  29679. + int err, wkq_err;
  29680. + struct notify_change_args args = {
  29681. + .errp = &err,
  29682. + .path = path,
  29683. + .ia = ia,
  29684. + .delegated_inode = delegated_inode
  29685. + };
  29686. +
  29687. + wkq_err = au_wkq_wait(call_notify_change, &args);
  29688. + if (unlikely(wkq_err))
  29689. + err = wkq_err;
  29690. +
  29691. + return err;
  29692. +}
  29693. +
  29694. +/* ---------------------------------------------------------------------- */
  29695. +
  29696. +struct unlink_args {
  29697. + int *errp;
  29698. + struct inode *dir;
  29699. + struct path *path;
  29700. + struct inode **delegated_inode;
  29701. +};
  29702. +
  29703. +static void call_unlink(void *args)
  29704. +{
  29705. + struct unlink_args *a = args;
  29706. + struct dentry *d = a->path->dentry;
  29707. + struct inode *h_inode;
  29708. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  29709. + && au_dcount(d) == 1);
  29710. +
  29711. + IMustLock(a->dir);
  29712. +
  29713. + a->path->dentry = d->d_parent;
  29714. + *a->errp = security_path_unlink(a->path, d);
  29715. + a->path->dentry = d;
  29716. + if (unlikely(*a->errp))
  29717. + return;
  29718. +
  29719. + if (!stop_sillyrename)
  29720. + dget(d);
  29721. + h_inode = NULL;
  29722. + if (d_is_positive(d)) {
  29723. + h_inode = d_inode(d);
  29724. + ihold(h_inode);
  29725. + }
  29726. +
  29727. + lockdep_off();
  29728. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  29729. + lockdep_on();
  29730. + if (!*a->errp) {
  29731. + struct path tmp = {
  29732. + .dentry = d->d_parent,
  29733. + .mnt = a->path->mnt
  29734. + };
  29735. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  29736. + }
  29737. +
  29738. + if (!stop_sillyrename)
  29739. + dput(d);
  29740. + if (h_inode)
  29741. + iput(h_inode);
  29742. +
  29743. + AuTraceErr(*a->errp);
  29744. +}
  29745. +
  29746. +/*
  29747. + * @dir: must be locked.
  29748. + * @dentry: target dentry.
  29749. + */
  29750. +int vfsub_unlink(struct inode *dir, struct path *path,
  29751. + struct inode **delegated_inode, int force)
  29752. +{
  29753. + int err;
  29754. + struct unlink_args args = {
  29755. + .errp = &err,
  29756. + .dir = dir,
  29757. + .path = path,
  29758. + .delegated_inode = delegated_inode
  29759. + };
  29760. +
  29761. + if (!force)
  29762. + call_unlink(&args);
  29763. + else {
  29764. + int wkq_err;
  29765. +
  29766. + wkq_err = au_wkq_wait(call_unlink, &args);
  29767. + if (unlikely(wkq_err))
  29768. + err = wkq_err;
  29769. + }
  29770. +
  29771. + return err;
  29772. +}
  29773. diff -Nur linux-4.1.10.orig/fs/aufs/vfsub.h linux-4.1.10/fs/aufs/vfsub.h
  29774. --- linux-4.1.10.orig/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
  29775. +++ linux-4.1.10/fs/aufs/vfsub.h 2015-10-22 21:35:53.000000000 +0200
  29776. @@ -0,0 +1,286 @@
  29777. +/*
  29778. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  29779. + *
  29780. + * This program, aufs is free software; you can redistribute it and/or modify
  29781. + * it under the terms of the GNU General Public License as published by
  29782. + * the Free Software Foundation; either version 2 of the License, or
  29783. + * (at your option) any later version.
  29784. + *
  29785. + * This program is distributed in the hope that it will be useful,
  29786. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29787. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29788. + * GNU General Public License for more details.
  29789. + *
  29790. + * You should have received a copy of the GNU General Public License
  29791. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29792. + */
  29793. +
  29794. +/*
  29795. + * sub-routines for VFS
  29796. + */
  29797. +
  29798. +#ifndef __AUFS_VFSUB_H__
  29799. +#define __AUFS_VFSUB_H__
  29800. +
  29801. +#ifdef __KERNEL__
  29802. +
  29803. +#include <linux/fs.h>
  29804. +#include <linux/mount.h>
  29805. +#include <linux/xattr.h>
  29806. +#include "debug.h"
  29807. +
  29808. +/* copied from linux/fs/internal.h */
  29809. +/* todo: BAD approach!! */
  29810. +extern void __mnt_drop_write(struct vfsmount *);
  29811. +extern spinlock_t inode_sb_list_lock;
  29812. +extern int open_check_o_direct(struct file *f);
  29813. +
  29814. +/* ---------------------------------------------------------------------- */
  29815. +
  29816. +/* lock subclass for lower inode */
  29817. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  29818. +/* reduce? gave up. */
  29819. +enum {
  29820. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  29821. + AuLsc_I_PARENT, /* lower inode, parent first */
  29822. + AuLsc_I_PARENT2, /* copyup dirs */
  29823. + AuLsc_I_PARENT3, /* copyup wh */
  29824. + AuLsc_I_CHILD,
  29825. + AuLsc_I_CHILD2,
  29826. + AuLsc_I_End
  29827. +};
  29828. +
  29829. +/* to debug easier, do not make them inlined functions */
  29830. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  29831. +#define IMustLock(i) MtxMustLock(&(i)->i_mutex)
  29832. +
  29833. +/* ---------------------------------------------------------------------- */
  29834. +
  29835. +static inline void vfsub_drop_nlink(struct inode *inode)
  29836. +{
  29837. + AuDebugOn(!inode->i_nlink);
  29838. + drop_nlink(inode);
  29839. +}
  29840. +
  29841. +static inline void vfsub_dead_dir(struct inode *inode)
  29842. +{
  29843. + AuDebugOn(!S_ISDIR(inode->i_mode));
  29844. + inode->i_flags |= S_DEAD;
  29845. + clear_nlink(inode);
  29846. +}
  29847. +
  29848. +static inline int vfsub_native_ro(struct inode *inode)
  29849. +{
  29850. + return (inode->i_sb->s_flags & MS_RDONLY)
  29851. + || IS_RDONLY(inode)
  29852. + /* || IS_APPEND(inode) */
  29853. + || IS_IMMUTABLE(inode);
  29854. +}
  29855. +
  29856. +/* ---------------------------------------------------------------------- */
  29857. +
  29858. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  29859. +struct file *vfsub_dentry_open(struct path *path, int flags);
  29860. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  29861. +struct vfsub_aopen_args {
  29862. + struct file *file;
  29863. + unsigned int open_flag;
  29864. + umode_t create_mode;
  29865. + int *opened;
  29866. +};
  29867. +struct au_branch;
  29868. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  29869. + struct vfsub_aopen_args *args, struct au_branch *br);
  29870. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  29871. +
  29872. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  29873. + int len);
  29874. +
  29875. +struct vfsub_lkup_one_args {
  29876. + struct dentry **errp;
  29877. + struct qstr *name;
  29878. + struct dentry *parent;
  29879. +};
  29880. +
  29881. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  29882. + struct dentry *parent)
  29883. +{
  29884. + return vfsub_lookup_one_len(name->name, parent, name->len);
  29885. +}
  29886. +
  29887. +void vfsub_call_lkup_one(void *args);
  29888. +
  29889. +/* ---------------------------------------------------------------------- */
  29890. +
  29891. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  29892. +{
  29893. + int err;
  29894. +
  29895. + lockdep_off();
  29896. + err = mnt_want_write(mnt);
  29897. + lockdep_on();
  29898. + return err;
  29899. +}
  29900. +
  29901. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  29902. +{
  29903. + lockdep_off();
  29904. + mnt_drop_write(mnt);
  29905. + lockdep_on();
  29906. +}
  29907. +
  29908. +#if 0 /* reserved */
  29909. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  29910. +{
  29911. + lockdep_off();
  29912. + mnt_drop_write_file(file);
  29913. + lockdep_on();
  29914. +}
  29915. +#endif
  29916. +
  29917. +/* ---------------------------------------------------------------------- */
  29918. +
  29919. +struct au_hinode;
  29920. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29921. + struct dentry *d2, struct au_hinode *hdir2);
  29922. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  29923. + struct dentry *d2, struct au_hinode *hdir2);
  29924. +
  29925. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  29926. + bool want_excl);
  29927. +int vfsub_symlink(struct inode *dir, struct path *path,
  29928. + const char *symname);
  29929. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  29930. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  29931. + struct path *path, struct inode **delegated_inode);
  29932. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  29933. + struct inode *hdir, struct path *path,
  29934. + struct inode **delegated_inode);
  29935. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  29936. +int vfsub_rmdir(struct inode *dir, struct path *path);
  29937. +
  29938. +/* ---------------------------------------------------------------------- */
  29939. +
  29940. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  29941. + loff_t *ppos);
  29942. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  29943. + loff_t *ppos);
  29944. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  29945. + loff_t *ppos);
  29946. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  29947. + loff_t *ppos);
  29948. +int vfsub_flush(struct file *file, fl_owner_t id);
  29949. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  29950. +
  29951. +static inline loff_t vfsub_f_size_read(struct file *file)
  29952. +{
  29953. + return i_size_read(file_inode(file));
  29954. +}
  29955. +
  29956. +static inline unsigned int vfsub_file_flags(struct file *file)
  29957. +{
  29958. + unsigned int flags;
  29959. +
  29960. + spin_lock(&file->f_lock);
  29961. + flags = file->f_flags;
  29962. + spin_unlock(&file->f_lock);
  29963. +
  29964. + return flags;
  29965. +}
  29966. +
  29967. +#if 0 /* reserved */
  29968. +static inline void vfsub_file_accessed(struct file *h_file)
  29969. +{
  29970. + file_accessed(h_file);
  29971. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  29972. +}
  29973. +#endif
  29974. +
  29975. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  29976. + struct dentry *h_dentry)
  29977. +{
  29978. + struct path h_path = {
  29979. + .dentry = h_dentry,
  29980. + .mnt = h_mnt
  29981. + };
  29982. + touch_atime(&h_path);
  29983. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  29984. +}
  29985. +
  29986. +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
  29987. + int flags)
  29988. +{
  29989. + return generic_update_time(h_inode, ts, flags);
  29990. + /* no vfsub_update_h_iattr() since we don't have struct path */
  29991. +}
  29992. +
  29993. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  29994. + struct pipe_inode_info *pipe, size_t len,
  29995. + unsigned int flags);
  29996. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  29997. + loff_t *ppos, size_t len, unsigned int flags);
  29998. +
  29999. +static inline long vfsub_truncate(struct path *path, loff_t length)
  30000. +{
  30001. + long err;
  30002. +
  30003. + lockdep_off();
  30004. + err = vfs_truncate(path, length);
  30005. + lockdep_on();
  30006. + return err;
  30007. +}
  30008. +
  30009. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  30010. + struct file *h_file);
  30011. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  30012. +
  30013. +/* ---------------------------------------------------------------------- */
  30014. +
  30015. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  30016. +{
  30017. + loff_t err;
  30018. +
  30019. + lockdep_off();
  30020. + err = vfs_llseek(file, offset, origin);
  30021. + lockdep_on();
  30022. + return err;
  30023. +}
  30024. +
  30025. +/* ---------------------------------------------------------------------- */
  30026. +
  30027. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  30028. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  30029. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  30030. + struct inode **delegated_inode);
  30031. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  30032. + struct inode **delegated_inode);
  30033. +int vfsub_unlink(struct inode *dir, struct path *path,
  30034. + struct inode **delegated_inode, int force);
  30035. +
  30036. +/* ---------------------------------------------------------------------- */
  30037. +
  30038. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  30039. + const void *value, size_t size, int flags)
  30040. +{
  30041. + int err;
  30042. +
  30043. + lockdep_off();
  30044. + err = vfs_setxattr(dentry, name, value, size, flags);
  30045. + lockdep_on();
  30046. +
  30047. + return err;
  30048. +}
  30049. +
  30050. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  30051. +{
  30052. + int err;
  30053. +
  30054. + lockdep_off();
  30055. + err = vfs_removexattr(dentry, name);
  30056. + lockdep_on();
  30057. +
  30058. + return err;
  30059. +}
  30060. +
  30061. +#endif /* __KERNEL__ */
  30062. +#endif /* __AUFS_VFSUB_H__ */
  30063. diff -Nur linux-4.1.10.orig/fs/aufs/wbr_policy.c linux-4.1.10/fs/aufs/wbr_policy.c
  30064. --- linux-4.1.10.orig/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
  30065. +++ linux-4.1.10/fs/aufs/wbr_policy.c 2015-10-22 21:35:53.000000000 +0200
  30066. @@ -0,0 +1,765 @@
  30067. +/*
  30068. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30069. + *
  30070. + * This program, aufs is free software; you can redistribute it and/or modify
  30071. + * it under the terms of the GNU General Public License as published by
  30072. + * the Free Software Foundation; either version 2 of the License, or
  30073. + * (at your option) any later version.
  30074. + *
  30075. + * This program is distributed in the hope that it will be useful,
  30076. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30077. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30078. + * GNU General Public License for more details.
  30079. + *
  30080. + * You should have received a copy of the GNU General Public License
  30081. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30082. + */
  30083. +
  30084. +/*
  30085. + * policies for selecting one among multiple writable branches
  30086. + */
  30087. +
  30088. +#include <linux/statfs.h>
  30089. +#include "aufs.h"
  30090. +
  30091. +/* subset of cpup_attr() */
  30092. +static noinline_for_stack
  30093. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  30094. +{
  30095. + int err, sbits;
  30096. + struct iattr ia;
  30097. + struct inode *h_isrc;
  30098. +
  30099. + h_isrc = d_inode(h_src);
  30100. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  30101. + ia.ia_mode = h_isrc->i_mode;
  30102. + ia.ia_uid = h_isrc->i_uid;
  30103. + ia.ia_gid = h_isrc->i_gid;
  30104. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  30105. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  30106. + /* no delegation since it is just created */
  30107. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  30108. +
  30109. + /* is this nfs only? */
  30110. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  30111. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  30112. + ia.ia_mode = h_isrc->i_mode;
  30113. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  30114. + }
  30115. +
  30116. + return err;
  30117. +}
  30118. +
  30119. +#define AuCpdown_PARENT_OPQ 1
  30120. +#define AuCpdown_WHED (1 << 1)
  30121. +#define AuCpdown_MADE_DIR (1 << 2)
  30122. +#define AuCpdown_DIROPQ (1 << 3)
  30123. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  30124. +#define au_fset_cpdown(flags, name) \
  30125. + do { (flags) |= AuCpdown_##name; } while (0)
  30126. +#define au_fclr_cpdown(flags, name) \
  30127. + do { (flags) &= ~AuCpdown_##name; } while (0)
  30128. +
  30129. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  30130. + unsigned int *flags)
  30131. +{
  30132. + int err;
  30133. + struct dentry *opq_dentry;
  30134. +
  30135. + opq_dentry = au_diropq_create(dentry, bdst);
  30136. + err = PTR_ERR(opq_dentry);
  30137. + if (IS_ERR(opq_dentry))
  30138. + goto out;
  30139. + dput(opq_dentry);
  30140. + au_fset_cpdown(*flags, DIROPQ);
  30141. +
  30142. +out:
  30143. + return err;
  30144. +}
  30145. +
  30146. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  30147. + struct inode *dir, aufs_bindex_t bdst)
  30148. +{
  30149. + int err;
  30150. + struct path h_path;
  30151. + struct au_branch *br;
  30152. +
  30153. + br = au_sbr(dentry->d_sb, bdst);
  30154. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  30155. + err = PTR_ERR(h_path.dentry);
  30156. + if (IS_ERR(h_path.dentry))
  30157. + goto out;
  30158. +
  30159. + err = 0;
  30160. + if (d_is_positive(h_path.dentry)) {
  30161. + h_path.mnt = au_br_mnt(br);
  30162. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  30163. + dentry);
  30164. + }
  30165. + dput(h_path.dentry);
  30166. +
  30167. +out:
  30168. + return err;
  30169. +}
  30170. +
  30171. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  30172. + struct au_pin *pin,
  30173. + struct dentry *h_parent, void *arg)
  30174. +{
  30175. + int err, rerr;
  30176. + aufs_bindex_t bopq, bstart;
  30177. + struct path h_path;
  30178. + struct dentry *parent;
  30179. + struct inode *h_dir, *h_inode, *inode, *dir;
  30180. + unsigned int *flags = arg;
  30181. +
  30182. + bstart = au_dbstart(dentry);
  30183. + /* dentry is di-locked */
  30184. + parent = dget_parent(dentry);
  30185. + dir = d_inode(parent);
  30186. + h_dir = d_inode(h_parent);
  30187. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  30188. + IMustLock(h_dir);
  30189. +
  30190. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  30191. + if (unlikely(err < 0))
  30192. + goto out;
  30193. + h_path.dentry = au_h_dptr(dentry, bdst);
  30194. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  30195. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
  30196. + S_IRWXU | S_IRUGO | S_IXUGO);
  30197. + if (unlikely(err))
  30198. + goto out_put;
  30199. + au_fset_cpdown(*flags, MADE_DIR);
  30200. +
  30201. + bopq = au_dbdiropq(dentry);
  30202. + au_fclr_cpdown(*flags, WHED);
  30203. + au_fclr_cpdown(*flags, DIROPQ);
  30204. + if (au_dbwh(dentry) == bdst)
  30205. + au_fset_cpdown(*flags, WHED);
  30206. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  30207. + au_fset_cpdown(*flags, PARENT_OPQ);
  30208. + h_inode = d_inode(h_path.dentry);
  30209. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  30210. + if (au_ftest_cpdown(*flags, WHED)) {
  30211. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  30212. + if (unlikely(err)) {
  30213. + mutex_unlock(&h_inode->i_mutex);
  30214. + goto out_dir;
  30215. + }
  30216. + }
  30217. +
  30218. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
  30219. + mutex_unlock(&h_inode->i_mutex);
  30220. + if (unlikely(err))
  30221. + goto out_opq;
  30222. +
  30223. + if (au_ftest_cpdown(*flags, WHED)) {
  30224. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  30225. + if (unlikely(err))
  30226. + goto out_opq;
  30227. + }
  30228. +
  30229. + inode = d_inode(dentry);
  30230. + if (au_ibend(inode) < bdst)
  30231. + au_set_ibend(inode, bdst);
  30232. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  30233. + au_hi_flags(inode, /*isdir*/1));
  30234. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  30235. + goto out; /* success */
  30236. +
  30237. + /* revert */
  30238. +out_opq:
  30239. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  30240. + mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
  30241. + rerr = au_diropq_remove(dentry, bdst);
  30242. + mutex_unlock(&h_inode->i_mutex);
  30243. + if (unlikely(rerr)) {
  30244. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  30245. + dentry, bdst, rerr);
  30246. + err = -EIO;
  30247. + goto out;
  30248. + }
  30249. + }
  30250. +out_dir:
  30251. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  30252. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  30253. + if (unlikely(rerr)) {
  30254. + AuIOErr("failed removing %pd b%d (%d)\n",
  30255. + dentry, bdst, rerr);
  30256. + err = -EIO;
  30257. + }
  30258. + }
  30259. +out_put:
  30260. + au_set_h_dptr(dentry, bdst, NULL);
  30261. + if (au_dbend(dentry) == bdst)
  30262. + au_update_dbend(dentry);
  30263. +out:
  30264. + dput(parent);
  30265. + return err;
  30266. +}
  30267. +
  30268. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  30269. +{
  30270. + int err;
  30271. + unsigned int flags;
  30272. +
  30273. + flags = 0;
  30274. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  30275. +
  30276. + return err;
  30277. +}
  30278. +
  30279. +/* ---------------------------------------------------------------------- */
  30280. +
  30281. +/* policies for create */
  30282. +
  30283. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  30284. +{
  30285. + int err, i, j, ndentry;
  30286. + aufs_bindex_t bopq;
  30287. + struct au_dcsub_pages dpages;
  30288. + struct au_dpage *dpage;
  30289. + struct dentry **dentries, *parent, *d;
  30290. +
  30291. + err = au_dpages_init(&dpages, GFP_NOFS);
  30292. + if (unlikely(err))
  30293. + goto out;
  30294. + parent = dget_parent(dentry);
  30295. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  30296. + if (unlikely(err))
  30297. + goto out_free;
  30298. +
  30299. + err = bindex;
  30300. + for (i = 0; i < dpages.ndpage; i++) {
  30301. + dpage = dpages.dpages + i;
  30302. + dentries = dpage->dentries;
  30303. + ndentry = dpage->ndentry;
  30304. + for (j = 0; j < ndentry; j++) {
  30305. + d = dentries[j];
  30306. + di_read_lock_parent2(d, !AuLock_IR);
  30307. + bopq = au_dbdiropq(d);
  30308. + di_read_unlock(d, !AuLock_IR);
  30309. + if (bopq >= 0 && bopq < err)
  30310. + err = bopq;
  30311. + }
  30312. + }
  30313. +
  30314. +out_free:
  30315. + dput(parent);
  30316. + au_dpages_free(&dpages);
  30317. +out:
  30318. + return err;
  30319. +}
  30320. +
  30321. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  30322. +{
  30323. + for (; bindex >= 0; bindex--)
  30324. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  30325. + return bindex;
  30326. + return -EROFS;
  30327. +}
  30328. +
  30329. +/* top down parent */
  30330. +static int au_wbr_create_tdp(struct dentry *dentry,
  30331. + unsigned int flags __maybe_unused)
  30332. +{
  30333. + int err;
  30334. + aufs_bindex_t bstart, bindex;
  30335. + struct super_block *sb;
  30336. + struct dentry *parent, *h_parent;
  30337. +
  30338. + sb = dentry->d_sb;
  30339. + bstart = au_dbstart(dentry);
  30340. + err = bstart;
  30341. + if (!au_br_rdonly(au_sbr(sb, bstart)))
  30342. + goto out;
  30343. +
  30344. + err = -EROFS;
  30345. + parent = dget_parent(dentry);
  30346. + for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
  30347. + h_parent = au_h_dptr(parent, bindex);
  30348. + if (!h_parent || d_is_negative(h_parent))
  30349. + continue;
  30350. +
  30351. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  30352. + err = bindex;
  30353. + break;
  30354. + }
  30355. + }
  30356. + dput(parent);
  30357. +
  30358. + /* bottom up here */
  30359. + if (unlikely(err < 0)) {
  30360. + err = au_wbr_bu(sb, bstart - 1);
  30361. + if (err >= 0)
  30362. + err = au_wbr_nonopq(dentry, err);
  30363. + }
  30364. +
  30365. +out:
  30366. + AuDbg("b%d\n", err);
  30367. + return err;
  30368. +}
  30369. +
  30370. +/* ---------------------------------------------------------------------- */
  30371. +
  30372. +/* an exception for the policy other than tdp */
  30373. +static int au_wbr_create_exp(struct dentry *dentry)
  30374. +{
  30375. + int err;
  30376. + aufs_bindex_t bwh, bdiropq;
  30377. + struct dentry *parent;
  30378. +
  30379. + err = -1;
  30380. + bwh = au_dbwh(dentry);
  30381. + parent = dget_parent(dentry);
  30382. + bdiropq = au_dbdiropq(parent);
  30383. + if (bwh >= 0) {
  30384. + if (bdiropq >= 0)
  30385. + err = min(bdiropq, bwh);
  30386. + else
  30387. + err = bwh;
  30388. + AuDbg("%d\n", err);
  30389. + } else if (bdiropq >= 0) {
  30390. + err = bdiropq;
  30391. + AuDbg("%d\n", err);
  30392. + }
  30393. + dput(parent);
  30394. +
  30395. + if (err >= 0)
  30396. + err = au_wbr_nonopq(dentry, err);
  30397. +
  30398. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  30399. + err = -1;
  30400. +
  30401. + AuDbg("%d\n", err);
  30402. + return err;
  30403. +}
  30404. +
  30405. +/* ---------------------------------------------------------------------- */
  30406. +
  30407. +/* round robin */
  30408. +static int au_wbr_create_init_rr(struct super_block *sb)
  30409. +{
  30410. + int err;
  30411. +
  30412. + err = au_wbr_bu(sb, au_sbend(sb));
  30413. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  30414. + /* smp_mb(); */
  30415. +
  30416. + AuDbg("b%d\n", err);
  30417. + return err;
  30418. +}
  30419. +
  30420. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  30421. +{
  30422. + int err, nbr;
  30423. + unsigned int u;
  30424. + aufs_bindex_t bindex, bend;
  30425. + struct super_block *sb;
  30426. + atomic_t *next;
  30427. +
  30428. + err = au_wbr_create_exp(dentry);
  30429. + if (err >= 0)
  30430. + goto out;
  30431. +
  30432. + sb = dentry->d_sb;
  30433. + next = &au_sbi(sb)->si_wbr_rr_next;
  30434. + bend = au_sbend(sb);
  30435. + nbr = bend + 1;
  30436. + for (bindex = 0; bindex <= bend; bindex++) {
  30437. + if (!au_ftest_wbr(flags, DIR)) {
  30438. + err = atomic_dec_return(next) + 1;
  30439. + /* modulo for 0 is meaningless */
  30440. + if (unlikely(!err))
  30441. + err = atomic_dec_return(next) + 1;
  30442. + } else
  30443. + err = atomic_read(next);
  30444. + AuDbg("%d\n", err);
  30445. + u = err;
  30446. + err = u % nbr;
  30447. + AuDbg("%d\n", err);
  30448. + if (!au_br_rdonly(au_sbr(sb, err)))
  30449. + break;
  30450. + err = -EROFS;
  30451. + }
  30452. +
  30453. + if (err >= 0)
  30454. + err = au_wbr_nonopq(dentry, err);
  30455. +
  30456. +out:
  30457. + AuDbg("%d\n", err);
  30458. + return err;
  30459. +}
  30460. +
  30461. +/* ---------------------------------------------------------------------- */
  30462. +
  30463. +/* most free space */
  30464. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  30465. +{
  30466. + struct super_block *sb;
  30467. + struct au_branch *br;
  30468. + struct au_wbr_mfs *mfs;
  30469. + struct dentry *h_parent;
  30470. + aufs_bindex_t bindex, bend;
  30471. + int err;
  30472. + unsigned long long b, bavail;
  30473. + struct path h_path;
  30474. + /* reduce the stack usage */
  30475. + struct kstatfs *st;
  30476. +
  30477. + st = kmalloc(sizeof(*st), GFP_NOFS);
  30478. + if (unlikely(!st)) {
  30479. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  30480. + return;
  30481. + }
  30482. +
  30483. + bavail = 0;
  30484. + sb = dentry->d_sb;
  30485. + mfs = &au_sbi(sb)->si_wbr_mfs;
  30486. + MtxMustLock(&mfs->mfs_lock);
  30487. + mfs->mfs_bindex = -EROFS;
  30488. + mfs->mfsrr_bytes = 0;
  30489. + if (!parent) {
  30490. + bindex = 0;
  30491. + bend = au_sbend(sb);
  30492. + } else {
  30493. + bindex = au_dbstart(parent);
  30494. + bend = au_dbtaildir(parent);
  30495. + }
  30496. +
  30497. + for (; bindex <= bend; bindex++) {
  30498. + if (parent) {
  30499. + h_parent = au_h_dptr(parent, bindex);
  30500. + if (!h_parent || d_is_negative(h_parent))
  30501. + continue;
  30502. + }
  30503. + br = au_sbr(sb, bindex);
  30504. + if (au_br_rdonly(br))
  30505. + continue;
  30506. +
  30507. + /* sb->s_root for NFS is unreliable */
  30508. + h_path.mnt = au_br_mnt(br);
  30509. + h_path.dentry = h_path.mnt->mnt_root;
  30510. + err = vfs_statfs(&h_path, st);
  30511. + if (unlikely(err)) {
  30512. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  30513. + continue;
  30514. + }
  30515. +
  30516. + /* when the available size is equal, select the lower one */
  30517. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  30518. + || sizeof(b) < sizeof(st->f_bsize));
  30519. + b = st->f_bavail * st->f_bsize;
  30520. + br->br_wbr->wbr_bytes = b;
  30521. + if (b >= bavail) {
  30522. + bavail = b;
  30523. + mfs->mfs_bindex = bindex;
  30524. + mfs->mfs_jiffy = jiffies;
  30525. + }
  30526. + }
  30527. +
  30528. + mfs->mfsrr_bytes = bavail;
  30529. + AuDbg("b%d\n", mfs->mfs_bindex);
  30530. + kfree(st);
  30531. +}
  30532. +
  30533. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  30534. +{
  30535. + int err;
  30536. + struct dentry *parent;
  30537. + struct super_block *sb;
  30538. + struct au_wbr_mfs *mfs;
  30539. +
  30540. + err = au_wbr_create_exp(dentry);
  30541. + if (err >= 0)
  30542. + goto out;
  30543. +
  30544. + sb = dentry->d_sb;
  30545. + parent = NULL;
  30546. + if (au_ftest_wbr(flags, PARENT))
  30547. + parent = dget_parent(dentry);
  30548. + mfs = &au_sbi(sb)->si_wbr_mfs;
  30549. + mutex_lock(&mfs->mfs_lock);
  30550. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  30551. + || mfs->mfs_bindex < 0
  30552. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  30553. + au_mfs(dentry, parent);
  30554. + mutex_unlock(&mfs->mfs_lock);
  30555. + err = mfs->mfs_bindex;
  30556. + dput(parent);
  30557. +
  30558. + if (err >= 0)
  30559. + err = au_wbr_nonopq(dentry, err);
  30560. +
  30561. +out:
  30562. + AuDbg("b%d\n", err);
  30563. + return err;
  30564. +}
  30565. +
  30566. +static int au_wbr_create_init_mfs(struct super_block *sb)
  30567. +{
  30568. + struct au_wbr_mfs *mfs;
  30569. +
  30570. + mfs = &au_sbi(sb)->si_wbr_mfs;
  30571. + mutex_init(&mfs->mfs_lock);
  30572. + mfs->mfs_jiffy = 0;
  30573. + mfs->mfs_bindex = -EROFS;
  30574. +
  30575. + return 0;
  30576. +}
  30577. +
  30578. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  30579. +{
  30580. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  30581. + return 0;
  30582. +}
  30583. +
  30584. +/* ---------------------------------------------------------------------- */
  30585. +
  30586. +/* most free space and then round robin */
  30587. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  30588. +{
  30589. + int err;
  30590. + struct au_wbr_mfs *mfs;
  30591. +
  30592. + err = au_wbr_create_mfs(dentry, flags);
  30593. + if (err >= 0) {
  30594. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  30595. + mutex_lock(&mfs->mfs_lock);
  30596. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  30597. + err = au_wbr_create_rr(dentry, flags);
  30598. + mutex_unlock(&mfs->mfs_lock);
  30599. + }
  30600. +
  30601. + AuDbg("b%d\n", err);
  30602. + return err;
  30603. +}
  30604. +
  30605. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  30606. +{
  30607. + int err;
  30608. +
  30609. + au_wbr_create_init_mfs(sb); /* ignore */
  30610. + err = au_wbr_create_init_rr(sb);
  30611. +
  30612. + return err;
  30613. +}
  30614. +
  30615. +/* ---------------------------------------------------------------------- */
  30616. +
  30617. +/* top down parent and most free space */
  30618. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  30619. +{
  30620. + int err, e2;
  30621. + unsigned long long b;
  30622. + aufs_bindex_t bindex, bstart, bend;
  30623. + struct super_block *sb;
  30624. + struct dentry *parent, *h_parent;
  30625. + struct au_branch *br;
  30626. +
  30627. + err = au_wbr_create_tdp(dentry, flags);
  30628. + if (unlikely(err < 0))
  30629. + goto out;
  30630. + parent = dget_parent(dentry);
  30631. + bstart = au_dbstart(parent);
  30632. + bend = au_dbtaildir(parent);
  30633. + if (bstart == bend)
  30634. + goto out_parent; /* success */
  30635. +
  30636. + e2 = au_wbr_create_mfs(dentry, flags);
  30637. + if (e2 < 0)
  30638. + goto out_parent; /* success */
  30639. +
  30640. + /* when the available size is equal, select upper one */
  30641. + sb = dentry->d_sb;
  30642. + br = au_sbr(sb, err);
  30643. + b = br->br_wbr->wbr_bytes;
  30644. + AuDbg("b%d, %llu\n", err, b);
  30645. +
  30646. + for (bindex = bstart; bindex <= bend; bindex++) {
  30647. + h_parent = au_h_dptr(parent, bindex);
  30648. + if (!h_parent || d_is_negative(h_parent))
  30649. + continue;
  30650. +
  30651. + br = au_sbr(sb, bindex);
  30652. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  30653. + b = br->br_wbr->wbr_bytes;
  30654. + err = bindex;
  30655. + AuDbg("b%d, %llu\n", err, b);
  30656. + }
  30657. + }
  30658. +
  30659. + if (err >= 0)
  30660. + err = au_wbr_nonopq(dentry, err);
  30661. +
  30662. +out_parent:
  30663. + dput(parent);
  30664. +out:
  30665. + AuDbg("b%d\n", err);
  30666. + return err;
  30667. +}
  30668. +
  30669. +/* ---------------------------------------------------------------------- */
  30670. +
  30671. +/*
  30672. + * - top down parent
  30673. + * - most free space with parent
  30674. + * - most free space round-robin regardless parent
  30675. + */
  30676. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  30677. +{
  30678. + int err;
  30679. + unsigned long long watermark;
  30680. + struct super_block *sb;
  30681. + struct au_branch *br;
  30682. + struct au_wbr_mfs *mfs;
  30683. +
  30684. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  30685. + if (unlikely(err < 0))
  30686. + goto out;
  30687. +
  30688. + sb = dentry->d_sb;
  30689. + br = au_sbr(sb, err);
  30690. + mfs = &au_sbi(sb)->si_wbr_mfs;
  30691. + mutex_lock(&mfs->mfs_lock);
  30692. + watermark = mfs->mfsrr_watermark;
  30693. + mutex_unlock(&mfs->mfs_lock);
  30694. + if (br->br_wbr->wbr_bytes < watermark)
  30695. + /* regardless the parent dir */
  30696. + err = au_wbr_create_mfsrr(dentry, flags);
  30697. +
  30698. +out:
  30699. + AuDbg("b%d\n", err);
  30700. + return err;
  30701. +}
  30702. +
  30703. +/* ---------------------------------------------------------------------- */
  30704. +
  30705. +/* policies for copyup */
  30706. +
  30707. +/* top down parent */
  30708. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  30709. +{
  30710. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  30711. +}
  30712. +
  30713. +/* bottom up parent */
  30714. +static int au_wbr_copyup_bup(struct dentry *dentry)
  30715. +{
  30716. + int err;
  30717. + aufs_bindex_t bindex, bstart;
  30718. + struct dentry *parent, *h_parent;
  30719. + struct super_block *sb;
  30720. +
  30721. + err = -EROFS;
  30722. + sb = dentry->d_sb;
  30723. + parent = dget_parent(dentry);
  30724. + bstart = au_dbstart(parent);
  30725. + for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
  30726. + h_parent = au_h_dptr(parent, bindex);
  30727. + if (!h_parent || d_is_negative(h_parent))
  30728. + continue;
  30729. +
  30730. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  30731. + err = bindex;
  30732. + break;
  30733. + }
  30734. + }
  30735. + dput(parent);
  30736. +
  30737. + /* bottom up here */
  30738. + if (unlikely(err < 0))
  30739. + err = au_wbr_bu(sb, bstart - 1);
  30740. +
  30741. + AuDbg("b%d\n", err);
  30742. + return err;
  30743. +}
  30744. +
  30745. +/* bottom up */
  30746. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
  30747. +{
  30748. + int err;
  30749. +
  30750. + err = au_wbr_bu(dentry->d_sb, bstart);
  30751. + AuDbg("b%d\n", err);
  30752. + if (err > bstart)
  30753. + err = au_wbr_nonopq(dentry, err);
  30754. +
  30755. + AuDbg("b%d\n", err);
  30756. + return err;
  30757. +}
  30758. +
  30759. +static int au_wbr_copyup_bu(struct dentry *dentry)
  30760. +{
  30761. + int err;
  30762. + aufs_bindex_t bstart;
  30763. +
  30764. + bstart = au_dbstart(dentry);
  30765. + err = au_wbr_do_copyup_bu(dentry, bstart);
  30766. + return err;
  30767. +}
  30768. +
  30769. +/* ---------------------------------------------------------------------- */
  30770. +
  30771. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  30772. + [AuWbrCopyup_TDP] = {
  30773. + .copyup = au_wbr_copyup_tdp
  30774. + },
  30775. + [AuWbrCopyup_BUP] = {
  30776. + .copyup = au_wbr_copyup_bup
  30777. + },
  30778. + [AuWbrCopyup_BU] = {
  30779. + .copyup = au_wbr_copyup_bu
  30780. + }
  30781. +};
  30782. +
  30783. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  30784. + [AuWbrCreate_TDP] = {
  30785. + .create = au_wbr_create_tdp
  30786. + },
  30787. + [AuWbrCreate_RR] = {
  30788. + .create = au_wbr_create_rr,
  30789. + .init = au_wbr_create_init_rr
  30790. + },
  30791. + [AuWbrCreate_MFS] = {
  30792. + .create = au_wbr_create_mfs,
  30793. + .init = au_wbr_create_init_mfs,
  30794. + .fin = au_wbr_create_fin_mfs
  30795. + },
  30796. + [AuWbrCreate_MFSV] = {
  30797. + .create = au_wbr_create_mfs,
  30798. + .init = au_wbr_create_init_mfs,
  30799. + .fin = au_wbr_create_fin_mfs
  30800. + },
  30801. + [AuWbrCreate_MFSRR] = {
  30802. + .create = au_wbr_create_mfsrr,
  30803. + .init = au_wbr_create_init_mfsrr,
  30804. + .fin = au_wbr_create_fin_mfs
  30805. + },
  30806. + [AuWbrCreate_MFSRRV] = {
  30807. + .create = au_wbr_create_mfsrr,
  30808. + .init = au_wbr_create_init_mfsrr,
  30809. + .fin = au_wbr_create_fin_mfs
  30810. + },
  30811. + [AuWbrCreate_PMFS] = {
  30812. + .create = au_wbr_create_pmfs,
  30813. + .init = au_wbr_create_init_mfs,
  30814. + .fin = au_wbr_create_fin_mfs
  30815. + },
  30816. + [AuWbrCreate_PMFSV] = {
  30817. + .create = au_wbr_create_pmfs,
  30818. + .init = au_wbr_create_init_mfs,
  30819. + .fin = au_wbr_create_fin_mfs
  30820. + },
  30821. + [AuWbrCreate_PMFSRR] = {
  30822. + .create = au_wbr_create_pmfsrr,
  30823. + .init = au_wbr_create_init_mfsrr,
  30824. + .fin = au_wbr_create_fin_mfs
  30825. + },
  30826. + [AuWbrCreate_PMFSRRV] = {
  30827. + .create = au_wbr_create_pmfsrr,
  30828. + .init = au_wbr_create_init_mfsrr,
  30829. + .fin = au_wbr_create_fin_mfs
  30830. + }
  30831. +};
  30832. diff -Nur linux-4.1.10.orig/fs/aufs/whout.c linux-4.1.10/fs/aufs/whout.c
  30833. --- linux-4.1.10.orig/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
  30834. +++ linux-4.1.10/fs/aufs/whout.c 2015-10-22 21:35:53.000000000 +0200
  30835. @@ -0,0 +1,1063 @@
  30836. +/*
  30837. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  30838. + *
  30839. + * This program, aufs is free software; you can redistribute it and/or modify
  30840. + * it under the terms of the GNU General Public License as published by
  30841. + * the Free Software Foundation; either version 2 of the License, or
  30842. + * (at your option) any later version.
  30843. + *
  30844. + * This program is distributed in the hope that it will be useful,
  30845. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30846. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30847. + * GNU General Public License for more details.
  30848. + *
  30849. + * You should have received a copy of the GNU General Public License
  30850. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30851. + */
  30852. +
  30853. +/*
  30854. + * whiteout for logical deletion and opaque directory
  30855. + */
  30856. +
  30857. +#include "aufs.h"
  30858. +
  30859. +#define WH_MASK S_IRUGO
  30860. +
  30861. +/*
  30862. + * If a directory contains this file, then it is opaque. We start with the
  30863. + * .wh. flag so that it is blocked by lookup.
  30864. + */
  30865. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  30866. + sizeof(AUFS_WH_DIROPQ) - 1);
  30867. +
  30868. +/*
  30869. + * generate whiteout name, which is NOT terminated by NULL.
  30870. + * @name: original d_name.name
  30871. + * @len: original d_name.len
  30872. + * @wh: whiteout qstr
  30873. + * returns zero when succeeds, otherwise error.
  30874. + * succeeded value as wh->name should be freed by kfree().
  30875. + */
  30876. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  30877. +{
  30878. + char *p;
  30879. +
  30880. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  30881. + return -ENAMETOOLONG;
  30882. +
  30883. + wh->len = name->len + AUFS_WH_PFX_LEN;
  30884. + p = kmalloc(wh->len, GFP_NOFS);
  30885. + wh->name = p;
  30886. + if (p) {
  30887. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  30888. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  30889. + /* smp_mb(); */
  30890. + return 0;
  30891. + }
  30892. + return -ENOMEM;
  30893. +}
  30894. +
  30895. +/* ---------------------------------------------------------------------- */
  30896. +
  30897. +/*
  30898. + * test if the @wh_name exists under @h_parent.
  30899. + * @try_sio specifies the necessary of super-io.
  30900. + */
  30901. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  30902. +{
  30903. + int err;
  30904. + struct dentry *wh_dentry;
  30905. +
  30906. + if (!try_sio)
  30907. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  30908. + else
  30909. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  30910. + err = PTR_ERR(wh_dentry);
  30911. + if (IS_ERR(wh_dentry)) {
  30912. + if (err == -ENAMETOOLONG)
  30913. + err = 0;
  30914. + goto out;
  30915. + }
  30916. +
  30917. + err = 0;
  30918. + if (d_is_negative(wh_dentry))
  30919. + goto out_wh; /* success */
  30920. +
  30921. + err = 1;
  30922. + if (d_is_reg(wh_dentry))
  30923. + goto out_wh; /* success */
  30924. +
  30925. + err = -EIO;
  30926. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  30927. + wh_dentry, d_inode(wh_dentry)->i_mode);
  30928. +
  30929. +out_wh:
  30930. + dput(wh_dentry);
  30931. +out:
  30932. + return err;
  30933. +}
  30934. +
  30935. +/*
  30936. + * test if the @h_dentry sets opaque or not.
  30937. + */
  30938. +int au_diropq_test(struct dentry *h_dentry)
  30939. +{
  30940. + int err;
  30941. + struct inode *h_dir;
  30942. +
  30943. + h_dir = d_inode(h_dentry);
  30944. + err = au_wh_test(h_dentry, &diropq_name,
  30945. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  30946. + return err;
  30947. +}
  30948. +
  30949. +/*
  30950. + * returns a negative dentry whose name is unique and temporary.
  30951. + */
  30952. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  30953. + struct qstr *prefix)
  30954. +{
  30955. + struct dentry *dentry;
  30956. + int i;
  30957. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  30958. + *name, *p;
  30959. + /* strict atomic_t is unnecessary here */
  30960. + static unsigned short cnt;
  30961. + struct qstr qs;
  30962. +
  30963. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  30964. +
  30965. + name = defname;
  30966. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  30967. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  30968. + dentry = ERR_PTR(-ENAMETOOLONG);
  30969. + if (unlikely(qs.len > NAME_MAX))
  30970. + goto out;
  30971. + dentry = ERR_PTR(-ENOMEM);
  30972. + name = kmalloc(qs.len + 1, GFP_NOFS);
  30973. + if (unlikely(!name))
  30974. + goto out;
  30975. + }
  30976. +
  30977. + /* doubly whiteout-ed */
  30978. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  30979. + p = name + AUFS_WH_PFX_LEN * 2;
  30980. + memcpy(p, prefix->name, prefix->len);
  30981. + p += prefix->len;
  30982. + *p++ = '.';
  30983. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  30984. +
  30985. + qs.name = name;
  30986. + for (i = 0; i < 3; i++) {
  30987. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  30988. + dentry = au_sio_lkup_one(&qs, h_parent);
  30989. + if (IS_ERR(dentry) || d_is_negative(dentry))
  30990. + goto out_name;
  30991. + dput(dentry);
  30992. + }
  30993. + /* pr_warn("could not get random name\n"); */
  30994. + dentry = ERR_PTR(-EEXIST);
  30995. + AuDbg("%.*s\n", AuLNPair(&qs));
  30996. + BUG();
  30997. +
  30998. +out_name:
  30999. + if (name != defname)
  31000. + kfree(name);
  31001. +out:
  31002. + AuTraceErrPtr(dentry);
  31003. + return dentry;
  31004. +}
  31005. +
  31006. +/*
  31007. + * rename the @h_dentry on @br to the whiteouted temporary name.
  31008. + */
  31009. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  31010. +{
  31011. + int err;
  31012. + struct path h_path = {
  31013. + .mnt = au_br_mnt(br)
  31014. + };
  31015. + struct inode *h_dir, *delegated;
  31016. + struct dentry *h_parent;
  31017. +
  31018. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  31019. + h_dir = d_inode(h_parent);
  31020. + IMustLock(h_dir);
  31021. +
  31022. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  31023. + err = PTR_ERR(h_path.dentry);
  31024. + if (IS_ERR(h_path.dentry))
  31025. + goto out;
  31026. +
  31027. + /* under the same dir, no need to lock_rename() */
  31028. + delegated = NULL;
  31029. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
  31030. + AuTraceErr(err);
  31031. + if (unlikely(err == -EWOULDBLOCK)) {
  31032. + pr_warn("cannot retry for NFSv4 delegation"
  31033. + " for an internal rename\n");
  31034. + iput(delegated);
  31035. + }
  31036. + dput(h_path.dentry);
  31037. +
  31038. +out:
  31039. + AuTraceErr(err);
  31040. + return err;
  31041. +}
  31042. +
  31043. +/* ---------------------------------------------------------------------- */
  31044. +/*
  31045. + * functions for removing a whiteout
  31046. + */
  31047. +
  31048. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  31049. +{
  31050. + int err, force;
  31051. + struct inode *delegated;
  31052. +
  31053. + /*
  31054. + * forces superio when the dir has a sticky bit.
  31055. + * this may be a violation of unix fs semantics.
  31056. + */
  31057. + force = (h_dir->i_mode & S_ISVTX)
  31058. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  31059. + delegated = NULL;
  31060. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  31061. + if (unlikely(err == -EWOULDBLOCK)) {
  31062. + pr_warn("cannot retry for NFSv4 delegation"
  31063. + " for an internal unlink\n");
  31064. + iput(delegated);
  31065. + }
  31066. + return err;
  31067. +}
  31068. +
  31069. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  31070. + struct dentry *dentry)
  31071. +{
  31072. + int err;
  31073. +
  31074. + err = do_unlink_wh(h_dir, h_path);
  31075. + if (!err && dentry)
  31076. + au_set_dbwh(dentry, -1);
  31077. +
  31078. + return err;
  31079. +}
  31080. +
  31081. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  31082. + struct au_branch *br)
  31083. +{
  31084. + int err;
  31085. + struct path h_path = {
  31086. + .mnt = au_br_mnt(br)
  31087. + };
  31088. +
  31089. + err = 0;
  31090. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  31091. + if (IS_ERR(h_path.dentry))
  31092. + err = PTR_ERR(h_path.dentry);
  31093. + else {
  31094. + if (d_is_reg(h_path.dentry))
  31095. + err = do_unlink_wh(d_inode(h_parent), &h_path);
  31096. + dput(h_path.dentry);
  31097. + }
  31098. +
  31099. + return err;
  31100. +}
  31101. +
  31102. +/* ---------------------------------------------------------------------- */
  31103. +/*
  31104. + * initialize/clean whiteout for a branch
  31105. + */
  31106. +
  31107. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  31108. + const int isdir)
  31109. +{
  31110. + int err;
  31111. + struct inode *delegated;
  31112. +
  31113. + if (d_is_negative(whpath->dentry))
  31114. + return;
  31115. +
  31116. + if (isdir)
  31117. + err = vfsub_rmdir(h_dir, whpath);
  31118. + else {
  31119. + delegated = NULL;
  31120. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  31121. + if (unlikely(err == -EWOULDBLOCK)) {
  31122. + pr_warn("cannot retry for NFSv4 delegation"
  31123. + " for an internal unlink\n");
  31124. + iput(delegated);
  31125. + }
  31126. + }
  31127. + if (unlikely(err))
  31128. + pr_warn("failed removing %pd (%d), ignored.\n",
  31129. + whpath->dentry, err);
  31130. +}
  31131. +
  31132. +static int test_linkable(struct dentry *h_root)
  31133. +{
  31134. + struct inode *h_dir = d_inode(h_root);
  31135. +
  31136. + if (h_dir->i_op->link)
  31137. + return 0;
  31138. +
  31139. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  31140. + h_root, au_sbtype(h_root->d_sb));
  31141. + return -ENOSYS;
  31142. +}
  31143. +
  31144. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  31145. +static int au_whdir(struct inode *h_dir, struct path *path)
  31146. +{
  31147. + int err;
  31148. +
  31149. + err = -EEXIST;
  31150. + if (d_is_negative(path->dentry)) {
  31151. + int mode = S_IRWXU;
  31152. +
  31153. + if (au_test_nfs(path->dentry->d_sb))
  31154. + mode |= S_IXUGO;
  31155. + err = vfsub_mkdir(h_dir, path, mode);
  31156. + } else if (d_is_dir(path->dentry))
  31157. + err = 0;
  31158. + else
  31159. + pr_err("unknown %pd exists\n", path->dentry);
  31160. +
  31161. + return err;
  31162. +}
  31163. +
  31164. +struct au_wh_base {
  31165. + const struct qstr *name;
  31166. + struct dentry *dentry;
  31167. +};
  31168. +
  31169. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  31170. + struct path *h_path)
  31171. +{
  31172. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31173. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  31174. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31175. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31176. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31177. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31178. +}
  31179. +
  31180. +/*
  31181. + * returns tri-state,
  31182. + * minus: error, caller should print the message
  31183. + * zero: succuess
  31184. + * plus: error, caller should NOT print the message
  31185. + */
  31186. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  31187. + int do_plink, struct au_wh_base base[],
  31188. + struct path *h_path)
  31189. +{
  31190. + int err;
  31191. + struct inode *h_dir;
  31192. +
  31193. + h_dir = d_inode(h_root);
  31194. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31195. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  31196. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31197. + if (do_plink) {
  31198. + err = test_linkable(h_root);
  31199. + if (unlikely(err)) {
  31200. + err = 1;
  31201. + goto out;
  31202. + }
  31203. +
  31204. + err = au_whdir(h_dir, h_path);
  31205. + if (unlikely(err))
  31206. + goto out;
  31207. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  31208. + } else
  31209. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31210. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31211. + err = au_whdir(h_dir, h_path);
  31212. + if (unlikely(err))
  31213. + goto out;
  31214. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  31215. +
  31216. +out:
  31217. + return err;
  31218. +}
  31219. +
  31220. +/*
  31221. + * for the moment, aufs supports the branch filesystem which does not support
  31222. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  31223. + * copyup failed. finally, such filesystem will not be used as the writable
  31224. + * branch.
  31225. + *
  31226. + * returns tri-state, see above.
  31227. + */
  31228. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  31229. + int do_plink, struct au_wh_base base[],
  31230. + struct path *h_path)
  31231. +{
  31232. + int err;
  31233. + struct inode *h_dir;
  31234. +
  31235. + WbrWhMustWriteLock(wbr);
  31236. +
  31237. + err = test_linkable(h_root);
  31238. + if (unlikely(err)) {
  31239. + err = 1;
  31240. + goto out;
  31241. + }
  31242. +
  31243. + /*
  31244. + * todo: should this create be done in /sbin/mount.aufs helper?
  31245. + */
  31246. + err = -EEXIST;
  31247. + h_dir = d_inode(h_root);
  31248. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  31249. + h_path->dentry = base[AuBrWh_BASE].dentry;
  31250. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  31251. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  31252. + err = 0;
  31253. + else
  31254. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  31255. + if (unlikely(err))
  31256. + goto out;
  31257. +
  31258. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  31259. + if (do_plink) {
  31260. + err = au_whdir(h_dir, h_path);
  31261. + if (unlikely(err))
  31262. + goto out;
  31263. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  31264. + } else
  31265. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  31266. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  31267. +
  31268. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  31269. + err = au_whdir(h_dir, h_path);
  31270. + if (unlikely(err))
  31271. + goto out;
  31272. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  31273. +
  31274. +out:
  31275. + return err;
  31276. +}
  31277. +
  31278. +/*
  31279. + * initialize the whiteout base file/dir for @br.
  31280. + */
  31281. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  31282. +{
  31283. + int err, i;
  31284. + const unsigned char do_plink
  31285. + = !!au_opt_test(au_mntflags(sb), PLINK);
  31286. + struct inode *h_dir;
  31287. + struct path path = br->br_path;
  31288. + struct dentry *h_root = path.dentry;
  31289. + struct au_wbr *wbr = br->br_wbr;
  31290. + static const struct qstr base_name[] = {
  31291. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  31292. + sizeof(AUFS_BASE_NAME) - 1),
  31293. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  31294. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  31295. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  31296. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  31297. + };
  31298. + struct au_wh_base base[] = {
  31299. + [AuBrWh_BASE] = {
  31300. + .name = base_name + AuBrWh_BASE,
  31301. + .dentry = NULL
  31302. + },
  31303. + [AuBrWh_PLINK] = {
  31304. + .name = base_name + AuBrWh_PLINK,
  31305. + .dentry = NULL
  31306. + },
  31307. + [AuBrWh_ORPH] = {
  31308. + .name = base_name + AuBrWh_ORPH,
  31309. + .dentry = NULL
  31310. + }
  31311. + };
  31312. +
  31313. + if (wbr)
  31314. + WbrWhMustWriteLock(wbr);
  31315. +
  31316. + for (i = 0; i < AuBrWh_Last; i++) {
  31317. + /* doubly whiteouted */
  31318. + struct dentry *d;
  31319. +
  31320. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  31321. + err = PTR_ERR(d);
  31322. + if (IS_ERR(d))
  31323. + goto out;
  31324. +
  31325. + base[i].dentry = d;
  31326. + AuDebugOn(wbr
  31327. + && wbr->wbr_wh[i]
  31328. + && wbr->wbr_wh[i] != base[i].dentry);
  31329. + }
  31330. +
  31331. + if (wbr)
  31332. + for (i = 0; i < AuBrWh_Last; i++) {
  31333. + dput(wbr->wbr_wh[i]);
  31334. + wbr->wbr_wh[i] = NULL;
  31335. + }
  31336. +
  31337. + err = 0;
  31338. + if (!au_br_writable(br->br_perm)) {
  31339. + h_dir = d_inode(h_root);
  31340. + au_wh_init_ro(h_dir, base, &path);
  31341. + } else if (!au_br_wh_linkable(br->br_perm)) {
  31342. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  31343. + if (err > 0)
  31344. + goto out;
  31345. + else if (err)
  31346. + goto out_err;
  31347. + } else {
  31348. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  31349. + if (err > 0)
  31350. + goto out;
  31351. + else if (err)
  31352. + goto out_err;
  31353. + }
  31354. + goto out; /* success */
  31355. +
  31356. +out_err:
  31357. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  31358. + err, h_root, au_sbtype(h_root->d_sb));
  31359. +out:
  31360. + for (i = 0; i < AuBrWh_Last; i++)
  31361. + dput(base[i].dentry);
  31362. + return err;
  31363. +}
  31364. +
  31365. +/* ---------------------------------------------------------------------- */
  31366. +/*
  31367. + * whiteouts are all hard-linked usually.
  31368. + * when its link count reaches a ceiling, we create a new whiteout base
  31369. + * asynchronously.
  31370. + */
  31371. +
  31372. +struct reinit_br_wh {
  31373. + struct super_block *sb;
  31374. + struct au_branch *br;
  31375. +};
  31376. +
  31377. +static void reinit_br_wh(void *arg)
  31378. +{
  31379. + int err;
  31380. + aufs_bindex_t bindex;
  31381. + struct path h_path;
  31382. + struct reinit_br_wh *a = arg;
  31383. + struct au_wbr *wbr;
  31384. + struct inode *dir, *delegated;
  31385. + struct dentry *h_root;
  31386. + struct au_hinode *hdir;
  31387. +
  31388. + err = 0;
  31389. + wbr = a->br->br_wbr;
  31390. + /* big aufs lock */
  31391. + si_noflush_write_lock(a->sb);
  31392. + if (!au_br_writable(a->br->br_perm))
  31393. + goto out;
  31394. + bindex = au_br_index(a->sb, a->br->br_id);
  31395. + if (unlikely(bindex < 0))
  31396. + goto out;
  31397. +
  31398. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  31399. + dir = d_inode(a->sb->s_root);
  31400. + hdir = au_hi(dir, bindex);
  31401. + h_root = au_h_dptr(a->sb->s_root, bindex);
  31402. + AuDebugOn(h_root != au_br_dentry(a->br));
  31403. +
  31404. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  31405. + wbr_wh_write_lock(wbr);
  31406. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  31407. + h_root, a->br);
  31408. + if (!err) {
  31409. + h_path.dentry = wbr->wbr_whbase;
  31410. + h_path.mnt = au_br_mnt(a->br);
  31411. + delegated = NULL;
  31412. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  31413. + /*force*/0);
  31414. + if (unlikely(err == -EWOULDBLOCK)) {
  31415. + pr_warn("cannot retry for NFSv4 delegation"
  31416. + " for an internal unlink\n");
  31417. + iput(delegated);
  31418. + }
  31419. + } else {
  31420. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  31421. + err = 0;
  31422. + }
  31423. + dput(wbr->wbr_whbase);
  31424. + wbr->wbr_whbase = NULL;
  31425. + if (!err)
  31426. + err = au_wh_init(a->br, a->sb);
  31427. + wbr_wh_write_unlock(wbr);
  31428. + au_hn_imtx_unlock(hdir);
  31429. + di_read_unlock(a->sb->s_root, AuLock_IR);
  31430. + if (!err)
  31431. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  31432. +
  31433. +out:
  31434. + if (wbr)
  31435. + atomic_dec(&wbr->wbr_wh_running);
  31436. + atomic_dec(&a->br->br_count);
  31437. + si_write_unlock(a->sb);
  31438. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  31439. + kfree(arg);
  31440. + if (unlikely(err))
  31441. + AuIOErr("err %d\n", err);
  31442. +}
  31443. +
  31444. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  31445. +{
  31446. + int do_dec, wkq_err;
  31447. + struct reinit_br_wh *arg;
  31448. +
  31449. + do_dec = 1;
  31450. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  31451. + goto out;
  31452. +
  31453. + /* ignore ENOMEM */
  31454. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  31455. + if (arg) {
  31456. + /*
  31457. + * dec(wh_running), kfree(arg) and dec(br_count)
  31458. + * in reinit function
  31459. + */
  31460. + arg->sb = sb;
  31461. + arg->br = br;
  31462. + atomic_inc(&br->br_count);
  31463. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  31464. + if (unlikely(wkq_err)) {
  31465. + atomic_dec(&br->br_wbr->wbr_wh_running);
  31466. + atomic_dec(&br->br_count);
  31467. + kfree(arg);
  31468. + }
  31469. + do_dec = 0;
  31470. + }
  31471. +
  31472. +out:
  31473. + if (do_dec)
  31474. + atomic_dec(&br->br_wbr->wbr_wh_running);
  31475. +}
  31476. +
  31477. +/* ---------------------------------------------------------------------- */
  31478. +
  31479. +/*
  31480. + * create the whiteout @wh.
  31481. + */
  31482. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  31483. + struct dentry *wh)
  31484. +{
  31485. + int err;
  31486. + struct path h_path = {
  31487. + .dentry = wh
  31488. + };
  31489. + struct au_branch *br;
  31490. + struct au_wbr *wbr;
  31491. + struct dentry *h_parent;
  31492. + struct inode *h_dir, *delegated;
  31493. +
  31494. + h_parent = wh->d_parent; /* dir inode is locked */
  31495. + h_dir = d_inode(h_parent);
  31496. + IMustLock(h_dir);
  31497. +
  31498. + br = au_sbr(sb, bindex);
  31499. + h_path.mnt = au_br_mnt(br);
  31500. + wbr = br->br_wbr;
  31501. + wbr_wh_read_lock(wbr);
  31502. + if (wbr->wbr_whbase) {
  31503. + delegated = NULL;
  31504. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  31505. + if (unlikely(err == -EWOULDBLOCK)) {
  31506. + pr_warn("cannot retry for NFSv4 delegation"
  31507. + " for an internal link\n");
  31508. + iput(delegated);
  31509. + }
  31510. + if (!err || err != -EMLINK)
  31511. + goto out;
  31512. +
  31513. + /* link count full. re-initialize br_whbase. */
  31514. + kick_reinit_br_wh(sb, br);
  31515. + }
  31516. +
  31517. + /* return this error in this context */
  31518. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  31519. + if (!err)
  31520. + au_fhsm_wrote(sb, bindex, /*force*/0);
  31521. +
  31522. +out:
  31523. + wbr_wh_read_unlock(wbr);
  31524. + return err;
  31525. +}
  31526. +
  31527. +/* ---------------------------------------------------------------------- */
  31528. +
  31529. +/*
  31530. + * create or remove the diropq.
  31531. + */
  31532. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  31533. + unsigned int flags)
  31534. +{
  31535. + struct dentry *opq_dentry, *h_dentry;
  31536. + struct super_block *sb;
  31537. + struct au_branch *br;
  31538. + int err;
  31539. +
  31540. + sb = dentry->d_sb;
  31541. + br = au_sbr(sb, bindex);
  31542. + h_dentry = au_h_dptr(dentry, bindex);
  31543. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  31544. + if (IS_ERR(opq_dentry))
  31545. + goto out;
  31546. +
  31547. + if (au_ftest_diropq(flags, CREATE)) {
  31548. + err = link_or_create_wh(sb, bindex, opq_dentry);
  31549. + if (!err) {
  31550. + au_set_dbdiropq(dentry, bindex);
  31551. + goto out; /* success */
  31552. + }
  31553. + } else {
  31554. + struct path tmp = {
  31555. + .dentry = opq_dentry,
  31556. + .mnt = au_br_mnt(br)
  31557. + };
  31558. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
  31559. + if (!err)
  31560. + au_set_dbdiropq(dentry, -1);
  31561. + }
  31562. + dput(opq_dentry);
  31563. + opq_dentry = ERR_PTR(err);
  31564. +
  31565. +out:
  31566. + return opq_dentry;
  31567. +}
  31568. +
  31569. +struct do_diropq_args {
  31570. + struct dentry **errp;
  31571. + struct dentry *dentry;
  31572. + aufs_bindex_t bindex;
  31573. + unsigned int flags;
  31574. +};
  31575. +
  31576. +static void call_do_diropq(void *args)
  31577. +{
  31578. + struct do_diropq_args *a = args;
  31579. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  31580. +}
  31581. +
  31582. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  31583. + unsigned int flags)
  31584. +{
  31585. + struct dentry *diropq, *h_dentry;
  31586. +
  31587. + h_dentry = au_h_dptr(dentry, bindex);
  31588. + if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
  31589. + diropq = do_diropq(dentry, bindex, flags);
  31590. + else {
  31591. + int wkq_err;
  31592. + struct do_diropq_args args = {
  31593. + .errp = &diropq,
  31594. + .dentry = dentry,
  31595. + .bindex = bindex,
  31596. + .flags = flags
  31597. + };
  31598. +
  31599. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  31600. + if (unlikely(wkq_err))
  31601. + diropq = ERR_PTR(wkq_err);
  31602. + }
  31603. +
  31604. + return diropq;
  31605. +}
  31606. +
  31607. +/* ---------------------------------------------------------------------- */
  31608. +
  31609. +/*
  31610. + * lookup whiteout dentry.
  31611. + * @h_parent: lower parent dentry which must exist and be locked
  31612. + * @base_name: name of dentry which will be whiteouted
  31613. + * returns dentry for whiteout.
  31614. + */
  31615. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  31616. + struct au_branch *br)
  31617. +{
  31618. + int err;
  31619. + struct qstr wh_name;
  31620. + struct dentry *wh_dentry;
  31621. +
  31622. + err = au_wh_name_alloc(&wh_name, base_name);
  31623. + wh_dentry = ERR_PTR(err);
  31624. + if (!err) {
  31625. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  31626. + kfree(wh_name.name);
  31627. + }
  31628. + return wh_dentry;
  31629. +}
  31630. +
  31631. +/*
  31632. + * link/create a whiteout for @dentry on @bindex.
  31633. + */
  31634. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  31635. + struct dentry *h_parent)
  31636. +{
  31637. + struct dentry *wh_dentry;
  31638. + struct super_block *sb;
  31639. + int err;
  31640. +
  31641. + sb = dentry->d_sb;
  31642. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  31643. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  31644. + err = link_or_create_wh(sb, bindex, wh_dentry);
  31645. + if (!err) {
  31646. + au_set_dbwh(dentry, bindex);
  31647. + au_fhsm_wrote(sb, bindex, /*force*/0);
  31648. + } else {
  31649. + dput(wh_dentry);
  31650. + wh_dentry = ERR_PTR(err);
  31651. + }
  31652. + }
  31653. +
  31654. + return wh_dentry;
  31655. +}
  31656. +
  31657. +/* ---------------------------------------------------------------------- */
  31658. +
  31659. +/* Delete all whiteouts in this directory on branch bindex. */
  31660. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  31661. + aufs_bindex_t bindex, struct au_branch *br)
  31662. +{
  31663. + int err;
  31664. + unsigned long ul, n;
  31665. + struct qstr wh_name;
  31666. + char *p;
  31667. + struct hlist_head *head;
  31668. + struct au_vdir_wh *pos;
  31669. + struct au_vdir_destr *str;
  31670. +
  31671. + err = -ENOMEM;
  31672. + p = (void *)__get_free_page(GFP_NOFS);
  31673. + wh_name.name = p;
  31674. + if (unlikely(!wh_name.name))
  31675. + goto out;
  31676. +
  31677. + err = 0;
  31678. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31679. + p += AUFS_WH_PFX_LEN;
  31680. + n = whlist->nh_num;
  31681. + head = whlist->nh_head;
  31682. + for (ul = 0; !err && ul < n; ul++, head++) {
  31683. + hlist_for_each_entry(pos, head, wh_hash) {
  31684. + if (pos->wh_bindex != bindex)
  31685. + continue;
  31686. +
  31687. + str = &pos->wh_str;
  31688. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  31689. + memcpy(p, str->name, str->len);
  31690. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  31691. + err = unlink_wh_name(h_dentry, &wh_name, br);
  31692. + if (!err)
  31693. + continue;
  31694. + break;
  31695. + }
  31696. + AuIOErr("whiteout name too long %.*s\n",
  31697. + str->len, str->name);
  31698. + err = -EIO;
  31699. + break;
  31700. + }
  31701. + }
  31702. + free_page((unsigned long)wh_name.name);
  31703. +
  31704. +out:
  31705. + return err;
  31706. +}
  31707. +
  31708. +struct del_wh_children_args {
  31709. + int *errp;
  31710. + struct dentry *h_dentry;
  31711. + struct au_nhash *whlist;
  31712. + aufs_bindex_t bindex;
  31713. + struct au_branch *br;
  31714. +};
  31715. +
  31716. +static void call_del_wh_children(void *args)
  31717. +{
  31718. + struct del_wh_children_args *a = args;
  31719. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  31720. +}
  31721. +
  31722. +/* ---------------------------------------------------------------------- */
  31723. +
  31724. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  31725. +{
  31726. + struct au_whtmp_rmdir *whtmp;
  31727. + int err;
  31728. + unsigned int rdhash;
  31729. +
  31730. + SiMustAnyLock(sb);
  31731. +
  31732. + whtmp = kmalloc(sizeof(*whtmp), gfp);
  31733. + if (unlikely(!whtmp)) {
  31734. + whtmp = ERR_PTR(-ENOMEM);
  31735. + goto out;
  31736. + }
  31737. +
  31738. + whtmp->dir = NULL;
  31739. + whtmp->br = NULL;
  31740. + whtmp->wh_dentry = NULL;
  31741. + /* no estimation for dir size */
  31742. + rdhash = au_sbi(sb)->si_rdhash;
  31743. + if (!rdhash)
  31744. + rdhash = AUFS_RDHASH_DEF;
  31745. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  31746. + if (unlikely(err)) {
  31747. + kfree(whtmp);
  31748. + whtmp = ERR_PTR(err);
  31749. + }
  31750. +
  31751. +out:
  31752. + return whtmp;
  31753. +}
  31754. +
  31755. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  31756. +{
  31757. + if (whtmp->br)
  31758. + atomic_dec(&whtmp->br->br_count);
  31759. + dput(whtmp->wh_dentry);
  31760. + iput(whtmp->dir);
  31761. + au_nhash_wh_free(&whtmp->whlist);
  31762. + kfree(whtmp);
  31763. +}
  31764. +
  31765. +/*
  31766. + * rmdir the whiteouted temporary named dir @h_dentry.
  31767. + * @whlist: whiteouted children.
  31768. + */
  31769. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  31770. + struct dentry *wh_dentry, struct au_nhash *whlist)
  31771. +{
  31772. + int err;
  31773. + unsigned int h_nlink;
  31774. + struct path h_tmp;
  31775. + struct inode *wh_inode, *h_dir;
  31776. + struct au_branch *br;
  31777. +
  31778. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  31779. + IMustLock(h_dir);
  31780. +
  31781. + br = au_sbr(dir->i_sb, bindex);
  31782. + wh_inode = d_inode(wh_dentry);
  31783. + mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
  31784. +
  31785. + /*
  31786. + * someone else might change some whiteouts while we were sleeping.
  31787. + * it means this whlist may have an obsoleted entry.
  31788. + */
  31789. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  31790. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  31791. + else {
  31792. + int wkq_err;
  31793. + struct del_wh_children_args args = {
  31794. + .errp = &err,
  31795. + .h_dentry = wh_dentry,
  31796. + .whlist = whlist,
  31797. + .bindex = bindex,
  31798. + .br = br
  31799. + };
  31800. +
  31801. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  31802. + if (unlikely(wkq_err))
  31803. + err = wkq_err;
  31804. + }
  31805. + mutex_unlock(&wh_inode->i_mutex);
  31806. +
  31807. + if (!err) {
  31808. + h_tmp.dentry = wh_dentry;
  31809. + h_tmp.mnt = au_br_mnt(br);
  31810. + h_nlink = h_dir->i_nlink;
  31811. + err = vfsub_rmdir(h_dir, &h_tmp);
  31812. + /* some fs doesn't change the parent nlink in some cases */
  31813. + h_nlink -= h_dir->i_nlink;
  31814. + }
  31815. +
  31816. + if (!err) {
  31817. + if (au_ibstart(dir) == bindex) {
  31818. + /* todo: dir->i_mutex is necessary */
  31819. + au_cpup_attr_timesizes(dir);
  31820. + if (h_nlink)
  31821. + vfsub_drop_nlink(dir);
  31822. + }
  31823. + return 0; /* success */
  31824. + }
  31825. +
  31826. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  31827. + return err;
  31828. +}
  31829. +
  31830. +static void call_rmdir_whtmp(void *args)
  31831. +{
  31832. + int err;
  31833. + aufs_bindex_t bindex;
  31834. + struct au_whtmp_rmdir *a = args;
  31835. + struct super_block *sb;
  31836. + struct dentry *h_parent;
  31837. + struct inode *h_dir;
  31838. + struct au_hinode *hdir;
  31839. +
  31840. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  31841. + /* mutex_lock(&a->dir->i_mutex); */
  31842. + err = -EROFS;
  31843. + sb = a->dir->i_sb;
  31844. + si_read_lock(sb, !AuLock_FLUSH);
  31845. + if (!au_br_writable(a->br->br_perm))
  31846. + goto out;
  31847. + bindex = au_br_index(sb, a->br->br_id);
  31848. + if (unlikely(bindex < 0))
  31849. + goto out;
  31850. +
  31851. + err = -EIO;
  31852. + ii_write_lock_parent(a->dir);
  31853. + h_parent = dget_parent(a->wh_dentry);
  31854. + h_dir = d_inode(h_parent);
  31855. + hdir = au_hi(a->dir, bindex);
  31856. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  31857. + if (unlikely(err))
  31858. + goto out_mnt;
  31859. + au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
  31860. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  31861. + a->br);
  31862. + if (!err)
  31863. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  31864. + au_hn_imtx_unlock(hdir);
  31865. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  31866. +
  31867. +out_mnt:
  31868. + dput(h_parent);
  31869. + ii_write_unlock(a->dir);
  31870. +out:
  31871. + /* mutex_unlock(&a->dir->i_mutex); */
  31872. + au_whtmp_rmdir_free(a);
  31873. + si_read_unlock(sb);
  31874. + au_nwt_done(&au_sbi(sb)->si_nowait);
  31875. + if (unlikely(err))
  31876. + AuIOErr("err %d\n", err);
  31877. +}
  31878. +
  31879. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  31880. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  31881. +{
  31882. + int wkq_err;
  31883. + struct super_block *sb;
  31884. +
  31885. + IMustLock(dir);
  31886. +
  31887. + /* all post-process will be done in do_rmdir_whtmp(). */
  31888. + sb = dir->i_sb;
  31889. + args->dir = au_igrab(dir);
  31890. + args->br = au_sbr(sb, bindex);
  31891. + atomic_inc(&args->br->br_count);
  31892. + args->wh_dentry = dget(wh_dentry);
  31893. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  31894. + if (unlikely(wkq_err)) {
  31895. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  31896. + au_whtmp_rmdir_free(args);
  31897. + }
  31898. +}
  31899. diff -Nur linux-4.1.10.orig/fs/aufs/whout.h linux-4.1.10/fs/aufs/whout.h
  31900. --- linux-4.1.10.orig/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
  31901. +++ linux-4.1.10/fs/aufs/whout.h 2015-10-22 21:35:53.000000000 +0200
  31902. @@ -0,0 +1,85 @@
  31903. +/*
  31904. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  31905. + *
  31906. + * This program, aufs is free software; you can redistribute it and/or modify
  31907. + * it under the terms of the GNU General Public License as published by
  31908. + * the Free Software Foundation; either version 2 of the License, or
  31909. + * (at your option) any later version.
  31910. + *
  31911. + * This program is distributed in the hope that it will be useful,
  31912. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31913. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31914. + * GNU General Public License for more details.
  31915. + *
  31916. + * You should have received a copy of the GNU General Public License
  31917. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31918. + */
  31919. +
  31920. +/*
  31921. + * whiteout for logical deletion and opaque directory
  31922. + */
  31923. +
  31924. +#ifndef __AUFS_WHOUT_H__
  31925. +#define __AUFS_WHOUT_H__
  31926. +
  31927. +#ifdef __KERNEL__
  31928. +
  31929. +#include "dir.h"
  31930. +
  31931. +/* whout.c */
  31932. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  31933. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  31934. +int au_diropq_test(struct dentry *h_dentry);
  31935. +struct au_branch;
  31936. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  31937. + struct qstr *prefix);
  31938. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  31939. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  31940. + struct dentry *dentry);
  31941. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  31942. +
  31943. +/* diropq flags */
  31944. +#define AuDiropq_CREATE 1
  31945. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  31946. +#define au_fset_diropq(flags, name) \
  31947. + do { (flags) |= AuDiropq_##name; } while (0)
  31948. +#define au_fclr_diropq(flags, name) \
  31949. + do { (flags) &= ~AuDiropq_##name; } while (0)
  31950. +
  31951. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  31952. + unsigned int flags);
  31953. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  31954. + struct au_branch *br);
  31955. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  31956. + struct dentry *h_parent);
  31957. +
  31958. +/* real rmdir for the whiteout-ed dir */
  31959. +struct au_whtmp_rmdir {
  31960. + struct inode *dir;
  31961. + struct au_branch *br;
  31962. + struct dentry *wh_dentry;
  31963. + struct au_nhash whlist;
  31964. +};
  31965. +
  31966. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  31967. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  31968. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  31969. + struct dentry *wh_dentry, struct au_nhash *whlist);
  31970. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  31971. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  31972. +
  31973. +/* ---------------------------------------------------------------------- */
  31974. +
  31975. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  31976. + aufs_bindex_t bindex)
  31977. +{
  31978. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  31979. +}
  31980. +
  31981. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  31982. +{
  31983. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  31984. +}
  31985. +
  31986. +#endif /* __KERNEL__ */
  31987. +#endif /* __AUFS_WHOUT_H__ */
  31988. diff -Nur linux-4.1.10.orig/fs/aufs/wkq.c linux-4.1.10/fs/aufs/wkq.c
  31989. --- linux-4.1.10.orig/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
  31990. +++ linux-4.1.10/fs/aufs/wkq.c 2015-10-22 21:35:53.000000000 +0200
  31991. @@ -0,0 +1,213 @@
  31992. +/*
  31993. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  31994. + *
  31995. + * This program, aufs is free software; you can redistribute it and/or modify
  31996. + * it under the terms of the GNU General Public License as published by
  31997. + * the Free Software Foundation; either version 2 of the License, or
  31998. + * (at your option) any later version.
  31999. + *
  32000. + * This program is distributed in the hope that it will be useful,
  32001. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32002. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32003. + * GNU General Public License for more details.
  32004. + *
  32005. + * You should have received a copy of the GNU General Public License
  32006. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32007. + */
  32008. +
  32009. +/*
  32010. + * workqueue for asynchronous/super-io operations
  32011. + * todo: try new dredential scheme
  32012. + */
  32013. +
  32014. +#include <linux/module.h>
  32015. +#include "aufs.h"
  32016. +
  32017. +/* internal workqueue named AUFS_WKQ_NAME */
  32018. +
  32019. +static struct workqueue_struct *au_wkq;
  32020. +
  32021. +struct au_wkinfo {
  32022. + struct work_struct wk;
  32023. + struct kobject *kobj;
  32024. +
  32025. + unsigned int flags; /* see wkq.h */
  32026. +
  32027. + au_wkq_func_t func;
  32028. + void *args;
  32029. +
  32030. + struct completion *comp;
  32031. +};
  32032. +
  32033. +/* ---------------------------------------------------------------------- */
  32034. +
  32035. +static void wkq_func(struct work_struct *wk)
  32036. +{
  32037. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  32038. +
  32039. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  32040. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  32041. +
  32042. + wkinfo->func(wkinfo->args);
  32043. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  32044. + complete(wkinfo->comp);
  32045. + else {
  32046. + kobject_put(wkinfo->kobj);
  32047. + module_put(THIS_MODULE); /* todo: ?? */
  32048. + kfree(wkinfo);
  32049. + }
  32050. +}
  32051. +
  32052. +/*
  32053. + * Since struct completion is large, try allocating it dynamically.
  32054. + */
  32055. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  32056. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  32057. +
  32058. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  32059. +{
  32060. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  32061. + if (*comp) {
  32062. + init_completion(*comp);
  32063. + wkinfo->comp = *comp;
  32064. + return 0;
  32065. + }
  32066. + return -ENOMEM;
  32067. +}
  32068. +
  32069. +static void au_wkq_comp_free(struct completion *comp)
  32070. +{
  32071. + kfree(comp);
  32072. +}
  32073. +
  32074. +#else
  32075. +
  32076. +/* no braces */
  32077. +#define AuWkqCompDeclare(name) \
  32078. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  32079. + struct completion *comp = &_ ## name
  32080. +
  32081. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  32082. +{
  32083. + wkinfo->comp = *comp;
  32084. + return 0;
  32085. +}
  32086. +
  32087. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  32088. +{
  32089. + /* empty */
  32090. +}
  32091. +#endif /* 4KSTACKS */
  32092. +
  32093. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  32094. +{
  32095. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  32096. + if (au_wkq_test()) {
  32097. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  32098. + " due to a dead dir by UDBA?\n");
  32099. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  32100. + }
  32101. + } else
  32102. + au_dbg_verify_kthread();
  32103. +
  32104. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  32105. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  32106. + queue_work(au_wkq, &wkinfo->wk);
  32107. + } else {
  32108. + INIT_WORK(&wkinfo->wk, wkq_func);
  32109. + schedule_work(&wkinfo->wk);
  32110. + }
  32111. +}
  32112. +
  32113. +/*
  32114. + * Be careful. It is easy to make deadlock happen.
  32115. + * processA: lock, wkq and wait
  32116. + * processB: wkq and wait, lock in wkq
  32117. + * --> deadlock
  32118. + */
  32119. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  32120. +{
  32121. + int err;
  32122. + AuWkqCompDeclare(comp);
  32123. + struct au_wkinfo wkinfo = {
  32124. + .flags = flags,
  32125. + .func = func,
  32126. + .args = args
  32127. + };
  32128. +
  32129. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  32130. + if (!err) {
  32131. + au_wkq_run(&wkinfo);
  32132. + /* no timeout, no interrupt */
  32133. + wait_for_completion(wkinfo.comp);
  32134. + au_wkq_comp_free(comp);
  32135. + destroy_work_on_stack(&wkinfo.wk);
  32136. + }
  32137. +
  32138. + return err;
  32139. +
  32140. +}
  32141. +
  32142. +/*
  32143. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  32144. + * problem in a concurrent umounting.
  32145. + */
  32146. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  32147. + unsigned int flags)
  32148. +{
  32149. + int err;
  32150. + struct au_wkinfo *wkinfo;
  32151. +
  32152. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  32153. +
  32154. + /*
  32155. + * wkq_func() must free this wkinfo.
  32156. + * it highly depends upon the implementation of workqueue.
  32157. + */
  32158. + err = 0;
  32159. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  32160. + if (wkinfo) {
  32161. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  32162. + wkinfo->flags = flags & ~AuWkq_WAIT;
  32163. + wkinfo->func = func;
  32164. + wkinfo->args = args;
  32165. + wkinfo->comp = NULL;
  32166. + kobject_get(wkinfo->kobj);
  32167. + __module_get(THIS_MODULE); /* todo: ?? */
  32168. +
  32169. + au_wkq_run(wkinfo);
  32170. + } else {
  32171. + err = -ENOMEM;
  32172. + au_nwt_done(&au_sbi(sb)->si_nowait);
  32173. + }
  32174. +
  32175. + return err;
  32176. +}
  32177. +
  32178. +/* ---------------------------------------------------------------------- */
  32179. +
  32180. +void au_nwt_init(struct au_nowait_tasks *nwt)
  32181. +{
  32182. + atomic_set(&nwt->nw_len, 0);
  32183. + /* smp_mb(); */ /* atomic_set */
  32184. + init_waitqueue_head(&nwt->nw_wq);
  32185. +}
  32186. +
  32187. +void au_wkq_fin(void)
  32188. +{
  32189. + destroy_workqueue(au_wkq);
  32190. +}
  32191. +
  32192. +int __init au_wkq_init(void)
  32193. +{
  32194. + int err;
  32195. +
  32196. + err = 0;
  32197. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  32198. + if (IS_ERR(au_wkq))
  32199. + err = PTR_ERR(au_wkq);
  32200. + else if (!au_wkq)
  32201. + err = -ENOMEM;
  32202. +
  32203. + return err;
  32204. +}
  32205. diff -Nur linux-4.1.10.orig/fs/aufs/wkq.h linux-4.1.10/fs/aufs/wkq.h
  32206. --- linux-4.1.10.orig/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
  32207. +++ linux-4.1.10/fs/aufs/wkq.h 2015-10-22 21:35:53.000000000 +0200
  32208. @@ -0,0 +1,91 @@
  32209. +/*
  32210. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  32211. + *
  32212. + * This program, aufs is free software; you can redistribute it and/or modify
  32213. + * it under the terms of the GNU General Public License as published by
  32214. + * the Free Software Foundation; either version 2 of the License, or
  32215. + * (at your option) any later version.
  32216. + *
  32217. + * This program is distributed in the hope that it will be useful,
  32218. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32219. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32220. + * GNU General Public License for more details.
  32221. + *
  32222. + * You should have received a copy of the GNU General Public License
  32223. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32224. + */
  32225. +
  32226. +/*
  32227. + * workqueue for asynchronous/super-io operations
  32228. + * todo: try new credentials management scheme
  32229. + */
  32230. +
  32231. +#ifndef __AUFS_WKQ_H__
  32232. +#define __AUFS_WKQ_H__
  32233. +
  32234. +#ifdef __KERNEL__
  32235. +
  32236. +struct super_block;
  32237. +
  32238. +/* ---------------------------------------------------------------------- */
  32239. +
  32240. +/*
  32241. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  32242. + */
  32243. +struct au_nowait_tasks {
  32244. + atomic_t nw_len;
  32245. + wait_queue_head_t nw_wq;
  32246. +};
  32247. +
  32248. +/* ---------------------------------------------------------------------- */
  32249. +
  32250. +typedef void (*au_wkq_func_t)(void *args);
  32251. +
  32252. +/* wkq flags */
  32253. +#define AuWkq_WAIT 1
  32254. +#define AuWkq_NEST (1 << 1)
  32255. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  32256. +#define au_fset_wkq(flags, name) \
  32257. + do { (flags) |= AuWkq_##name; } while (0)
  32258. +#define au_fclr_wkq(flags, name) \
  32259. + do { (flags) &= ~AuWkq_##name; } while (0)
  32260. +
  32261. +#ifndef CONFIG_AUFS_HNOTIFY
  32262. +#undef AuWkq_NEST
  32263. +#define AuWkq_NEST 0
  32264. +#endif
  32265. +
  32266. +/* wkq.c */
  32267. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  32268. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  32269. + unsigned int flags);
  32270. +void au_nwt_init(struct au_nowait_tasks *nwt);
  32271. +int __init au_wkq_init(void);
  32272. +void au_wkq_fin(void);
  32273. +
  32274. +/* ---------------------------------------------------------------------- */
  32275. +
  32276. +static inline int au_wkq_test(void)
  32277. +{
  32278. + return current->flags & PF_WQ_WORKER;
  32279. +}
  32280. +
  32281. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  32282. +{
  32283. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  32284. +}
  32285. +
  32286. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  32287. +{
  32288. + if (atomic_dec_and_test(&nwt->nw_len))
  32289. + wake_up_all(&nwt->nw_wq);
  32290. +}
  32291. +
  32292. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  32293. +{
  32294. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  32295. + return 0;
  32296. +}
  32297. +
  32298. +#endif /* __KERNEL__ */
  32299. +#endif /* __AUFS_WKQ_H__ */
  32300. diff -Nur linux-4.1.10.orig/fs/aufs/xattr.c linux-4.1.10/fs/aufs/xattr.c
  32301. --- linux-4.1.10.orig/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
  32302. +++ linux-4.1.10/fs/aufs/xattr.c 2015-10-22 21:35:53.000000000 +0200
  32303. @@ -0,0 +1,344 @@
  32304. +/*
  32305. + * Copyright (C) 2014-2015 Junjiro R. Okajima
  32306. + *
  32307. + * This program, aufs is free software; you can redistribute it and/or modify
  32308. + * it under the terms of the GNU General Public License as published by
  32309. + * the Free Software Foundation; either version 2 of the License, or
  32310. + * (at your option) any later version.
  32311. + *
  32312. + * This program is distributed in the hope that it will be useful,
  32313. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32314. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32315. + * GNU General Public License for more details.
  32316. + *
  32317. + * You should have received a copy of the GNU General Public License
  32318. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32319. + */
  32320. +
  32321. +/*
  32322. + * handling xattr functions
  32323. + */
  32324. +
  32325. +#include <linux/xattr.h>
  32326. +#include "aufs.h"
  32327. +
  32328. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  32329. +{
  32330. + if (!ignore_flags)
  32331. + goto out;
  32332. + switch (err) {
  32333. + case -ENOMEM:
  32334. + case -EDQUOT:
  32335. + goto out;
  32336. + }
  32337. +
  32338. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  32339. + err = 0;
  32340. + goto out;
  32341. + }
  32342. +
  32343. +#define cmp(brattr, prefix) do { \
  32344. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  32345. + XATTR_##prefix##_PREFIX_LEN)) { \
  32346. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  32347. + err = 0; \
  32348. + goto out; \
  32349. + } \
  32350. + } while (0)
  32351. +
  32352. + cmp(SEC, SECURITY);
  32353. + cmp(SYS, SYSTEM);
  32354. + cmp(TR, TRUSTED);
  32355. + cmp(USR, USER);
  32356. +#undef cmp
  32357. +
  32358. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  32359. + err = 0;
  32360. +
  32361. +out:
  32362. + return err;
  32363. +}
  32364. +
  32365. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  32366. +
  32367. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  32368. + char *name, char **buf, unsigned int ignore_flags,
  32369. + unsigned int verbose)
  32370. +{
  32371. + int err;
  32372. + ssize_t ssz;
  32373. + struct inode *h_idst;
  32374. +
  32375. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  32376. + err = ssz;
  32377. + if (unlikely(err <= 0)) {
  32378. + if (err == -ENODATA
  32379. + || (err == -EOPNOTSUPP
  32380. + && ((ignore_flags & au_xattr_out_of_list)
  32381. + || (au_test_nfs_noacl(d_inode(h_src))
  32382. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  32383. + || !strcmp(name,
  32384. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  32385. + ))
  32386. + err = 0;
  32387. + if (err && (verbose || au_debug_test()))
  32388. + pr_err("%s, err %d\n", name, err);
  32389. + goto out;
  32390. + }
  32391. +
  32392. + /* unlock it temporary */
  32393. + h_idst = d_inode(h_dst);
  32394. + mutex_unlock(&h_idst->i_mutex);
  32395. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  32396. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  32397. + if (unlikely(err)) {
  32398. + if (verbose || au_debug_test())
  32399. + pr_err("%s, err %d\n", name, err);
  32400. + err = au_xattr_ignore(err, name, ignore_flags);
  32401. + }
  32402. +
  32403. +out:
  32404. + return err;
  32405. +}
  32406. +
  32407. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  32408. + unsigned int verbose)
  32409. +{
  32410. + int err, unlocked, acl_access, acl_default;
  32411. + ssize_t ssz;
  32412. + struct inode *h_isrc, *h_idst;
  32413. + char *value, *p, *o, *e;
  32414. +
  32415. + /* try stopping to update the source inode while we are referencing */
  32416. + /* there should not be the parent-child relationship between them */
  32417. + h_isrc = d_inode(h_src);
  32418. + h_idst = d_inode(h_dst);
  32419. + mutex_unlock(&h_idst->i_mutex);
  32420. + mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
  32421. + mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
  32422. + unlocked = 0;
  32423. +
  32424. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  32425. + ssz = vfs_listxattr(h_src, NULL, 0);
  32426. + err = ssz;
  32427. + if (unlikely(err < 0)) {
  32428. + AuTraceErr(err);
  32429. + if (err == -ENODATA
  32430. + || err == -EOPNOTSUPP)
  32431. + err = 0; /* ignore */
  32432. + goto out;
  32433. + }
  32434. +
  32435. + err = 0;
  32436. + p = NULL;
  32437. + o = NULL;
  32438. + if (ssz) {
  32439. + err = -ENOMEM;
  32440. + p = kmalloc(ssz, GFP_NOFS);
  32441. + o = p;
  32442. + if (unlikely(!p))
  32443. + goto out;
  32444. + err = vfs_listxattr(h_src, p, ssz);
  32445. + }
  32446. + mutex_unlock(&h_isrc->i_mutex);
  32447. + unlocked = 1;
  32448. + AuDbg("err %d, ssz %zd\n", err, ssz);
  32449. + if (unlikely(err < 0))
  32450. + goto out_free;
  32451. +
  32452. + err = 0;
  32453. + e = p + ssz;
  32454. + value = NULL;
  32455. + acl_access = 0;
  32456. + acl_default = 0;
  32457. + while (!err && p < e) {
  32458. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  32459. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  32460. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  32461. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  32462. + - 1);
  32463. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  32464. + verbose);
  32465. + p += strlen(p) + 1;
  32466. + }
  32467. + AuTraceErr(err);
  32468. + ignore_flags |= au_xattr_out_of_list;
  32469. + if (!err && !acl_access) {
  32470. + err = au_do_cpup_xattr(h_dst, h_src,
  32471. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  32472. + ignore_flags, verbose);
  32473. + AuTraceErr(err);
  32474. + }
  32475. + if (!err && !acl_default) {
  32476. + err = au_do_cpup_xattr(h_dst, h_src,
  32477. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  32478. + ignore_flags, verbose);
  32479. + AuTraceErr(err);
  32480. + }
  32481. +
  32482. + kfree(value);
  32483. +
  32484. +out_free:
  32485. + kfree(o);
  32486. +out:
  32487. + if (!unlocked)
  32488. + mutex_unlock(&h_isrc->i_mutex);
  32489. + AuTraceErr(err);
  32490. + return err;
  32491. +}
  32492. +
  32493. +/* ---------------------------------------------------------------------- */
  32494. +
  32495. +enum {
  32496. + AU_XATTR_LIST,
  32497. + AU_XATTR_GET
  32498. +};
  32499. +
  32500. +struct au_lgxattr {
  32501. + int type;
  32502. + union {
  32503. + struct {
  32504. + char *list;
  32505. + size_t size;
  32506. + } list;
  32507. + struct {
  32508. + const char *name;
  32509. + void *value;
  32510. + size_t size;
  32511. + } get;
  32512. + } u;
  32513. +};
  32514. +
  32515. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  32516. +{
  32517. + ssize_t err;
  32518. + struct path h_path;
  32519. + struct super_block *sb;
  32520. +
  32521. + sb = dentry->d_sb;
  32522. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  32523. + if (unlikely(err))
  32524. + goto out;
  32525. + err = au_h_path_getattr(dentry, /*force*/1, &h_path);
  32526. + if (unlikely(err))
  32527. + goto out_si;
  32528. + if (unlikely(!h_path.dentry))
  32529. + /* illegally overlapped or something */
  32530. + goto out_di; /* pretending success */
  32531. +
  32532. + /* always topmost entry only */
  32533. + switch (arg->type) {
  32534. + case AU_XATTR_LIST:
  32535. + err = vfs_listxattr(h_path.dentry,
  32536. + arg->u.list.list, arg->u.list.size);
  32537. + break;
  32538. + case AU_XATTR_GET:
  32539. + err = vfs_getxattr(h_path.dentry,
  32540. + arg->u.get.name, arg->u.get.value,
  32541. + arg->u.get.size);
  32542. + break;
  32543. + }
  32544. +
  32545. +out_di:
  32546. + di_read_unlock(dentry, AuLock_IR);
  32547. +out_si:
  32548. + si_read_unlock(sb);
  32549. +out:
  32550. + AuTraceErr(err);
  32551. + return err;
  32552. +}
  32553. +
  32554. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  32555. +{
  32556. + struct au_lgxattr arg = {
  32557. + .type = AU_XATTR_LIST,
  32558. + .u.list = {
  32559. + .list = list,
  32560. + .size = size
  32561. + },
  32562. + };
  32563. +
  32564. + return au_lgxattr(dentry, &arg);
  32565. +}
  32566. +
  32567. +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
  32568. + size_t size)
  32569. +{
  32570. + struct au_lgxattr arg = {
  32571. + .type = AU_XATTR_GET,
  32572. + .u.get = {
  32573. + .name = name,
  32574. + .value = value,
  32575. + .size = size
  32576. + },
  32577. + };
  32578. +
  32579. + return au_lgxattr(dentry, &arg);
  32580. +}
  32581. +
  32582. +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
  32583. + size_t size, int flags)
  32584. +{
  32585. + struct au_srxattr arg = {
  32586. + .type = AU_XATTR_SET,
  32587. + .u.set = {
  32588. + .name = name,
  32589. + .value = value,
  32590. + .size = size,
  32591. + .flags = flags
  32592. + },
  32593. + };
  32594. +
  32595. + return au_srxattr(dentry, &arg);
  32596. +}
  32597. +
  32598. +int aufs_removexattr(struct dentry *dentry, const char *name)
  32599. +{
  32600. + struct au_srxattr arg = {
  32601. + .type = AU_XATTR_REMOVE,
  32602. + .u.remove = {
  32603. + .name = name
  32604. + },
  32605. + };
  32606. +
  32607. + return au_srxattr(dentry, &arg);
  32608. +}
  32609. +
  32610. +/* ---------------------------------------------------------------------- */
  32611. +
  32612. +#if 0
  32613. +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
  32614. + const char *name, size_t name_len, int type)
  32615. +{
  32616. + return aufs_listxattr(dentry, list, list_size);
  32617. +}
  32618. +
  32619. +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
  32620. + size_t size, int type)
  32621. +{
  32622. + return aufs_getxattr(dentry, name, buffer, size);
  32623. +}
  32624. +
  32625. +static int au_xattr_set(struct dentry *dentry, const char *name,
  32626. + const void *value, size_t size, int flags, int type)
  32627. +{
  32628. + return aufs_setxattr(dentry, name, value, size, flags);
  32629. +}
  32630. +
  32631. +static const struct xattr_handler au_xattr_handler = {
  32632. + /* no prefix, no flags */
  32633. + .list = au_xattr_list,
  32634. + .get = au_xattr_get,
  32635. + .set = au_xattr_set
  32636. + /* why no remove? */
  32637. +};
  32638. +
  32639. +static const struct xattr_handler *au_xattr_handlers[] = {
  32640. + &au_xattr_handler
  32641. +};
  32642. +
  32643. +void au_xattr_init(struct super_block *sb)
  32644. +{
  32645. + /* sb->s_xattr = au_xattr_handlers; */
  32646. +}
  32647. +#endif
  32648. diff -Nur linux-4.1.10.orig/fs/aufs/xino.c linux-4.1.10/fs/aufs/xino.c
  32649. --- linux-4.1.10.orig/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
  32650. +++ linux-4.1.10/fs/aufs/xino.c 2015-10-22 21:35:53.000000000 +0200
  32651. @@ -0,0 +1,1296 @@
  32652. +/*
  32653. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  32654. + *
  32655. + * This program, aufs is free software; you can redistribute it and/or modify
  32656. + * it under the terms of the GNU General Public License as published by
  32657. + * the Free Software Foundation; either version 2 of the License, or
  32658. + * (at your option) any later version.
  32659. + *
  32660. + * This program is distributed in the hope that it will be useful,
  32661. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32662. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32663. + * GNU General Public License for more details.
  32664. + *
  32665. + * You should have received a copy of the GNU General Public License
  32666. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32667. + */
  32668. +
  32669. +/*
  32670. + * external inode number translation table and bitmap
  32671. + */
  32672. +
  32673. +#include <linux/seq_file.h>
  32674. +#include <linux/statfs.h>
  32675. +#include "aufs.h"
  32676. +
  32677. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  32678. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
  32679. + loff_t *pos)
  32680. +{
  32681. + ssize_t err;
  32682. + mm_segment_t oldfs;
  32683. + union {
  32684. + void *k;
  32685. + char __user *u;
  32686. + } buf;
  32687. +
  32688. + buf.k = kbuf;
  32689. + oldfs = get_fs();
  32690. + set_fs(KERNEL_DS);
  32691. + do {
  32692. + /* todo: signal_pending? */
  32693. + err = func(file, buf.u, size, pos);
  32694. + } while (err == -EAGAIN || err == -EINTR);
  32695. + set_fs(oldfs);
  32696. +
  32697. +#if 0 /* reserved for future use */
  32698. + if (err > 0)
  32699. + fsnotify_access(file->f_path.dentry);
  32700. +#endif
  32701. +
  32702. + return err;
  32703. +}
  32704. +
  32705. +/* ---------------------------------------------------------------------- */
  32706. +
  32707. +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
  32708. + size_t size, loff_t *pos)
  32709. +{
  32710. + ssize_t err;
  32711. + mm_segment_t oldfs;
  32712. + union {
  32713. + void *k;
  32714. + const char __user *u;
  32715. + } buf;
  32716. +
  32717. + buf.k = kbuf;
  32718. + oldfs = get_fs();
  32719. + set_fs(KERNEL_DS);
  32720. + do {
  32721. + /* todo: signal_pending? */
  32722. + err = func(file, buf.u, size, pos);
  32723. + } while (err == -EAGAIN || err == -EINTR);
  32724. + set_fs(oldfs);
  32725. +
  32726. +#if 0 /* reserved for future use */
  32727. + if (err > 0)
  32728. + fsnotify_modify(file->f_path.dentry);
  32729. +#endif
  32730. +
  32731. + return err;
  32732. +}
  32733. +
  32734. +struct do_xino_fwrite_args {
  32735. + ssize_t *errp;
  32736. + vfs_writef_t func;
  32737. + struct file *file;
  32738. + void *buf;
  32739. + size_t size;
  32740. + loff_t *pos;
  32741. +};
  32742. +
  32743. +static void call_do_xino_fwrite(void *args)
  32744. +{
  32745. + struct do_xino_fwrite_args *a = args;
  32746. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  32747. +}
  32748. +
  32749. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  32750. + size_t size, loff_t *pos)
  32751. +{
  32752. + ssize_t err;
  32753. +
  32754. + /* todo: signal block and no wkq? */
  32755. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  32756. + lockdep_off();
  32757. + err = do_xino_fwrite(func, file, buf, size, pos);
  32758. + lockdep_on();
  32759. + } else {
  32760. + /*
  32761. + * it breaks RLIMIT_FSIZE and normal user's limit,
  32762. + * users should care about quota and real 'filesystem full.'
  32763. + */
  32764. + int wkq_err;
  32765. + struct do_xino_fwrite_args args = {
  32766. + .errp = &err,
  32767. + .func = func,
  32768. + .file = file,
  32769. + .buf = buf,
  32770. + .size = size,
  32771. + .pos = pos
  32772. + };
  32773. +
  32774. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  32775. + if (unlikely(wkq_err))
  32776. + err = wkq_err;
  32777. + }
  32778. +
  32779. + return err;
  32780. +}
  32781. +
  32782. +/* ---------------------------------------------------------------------- */
  32783. +
  32784. +/*
  32785. + * create a new xinofile at the same place/path as @base_file.
  32786. + */
  32787. +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
  32788. +{
  32789. + struct file *file;
  32790. + struct dentry *base, *parent;
  32791. + struct inode *dir, *delegated;
  32792. + struct qstr *name;
  32793. + struct path path;
  32794. + int err;
  32795. +
  32796. + base = base_file->f_path.dentry;
  32797. + parent = base->d_parent; /* dir inode is locked */
  32798. + dir = d_inode(parent);
  32799. + IMustLock(dir);
  32800. +
  32801. + file = ERR_PTR(-EINVAL);
  32802. + name = &base->d_name;
  32803. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  32804. + if (IS_ERR(path.dentry)) {
  32805. + file = (void *)path.dentry;
  32806. + pr_err("%pd lookup err %ld\n",
  32807. + base, PTR_ERR(path.dentry));
  32808. + goto out;
  32809. + }
  32810. +
  32811. + /* no need to mnt_want_write() since we call dentry_open() later */
  32812. + err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
  32813. + if (unlikely(err)) {
  32814. + file = ERR_PTR(err);
  32815. + pr_err("%pd create err %d\n", base, err);
  32816. + goto out_dput;
  32817. + }
  32818. +
  32819. + path.mnt = base_file->f_path.mnt;
  32820. + file = vfsub_dentry_open(&path,
  32821. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  32822. + /* | __FMODE_NONOTIFY */);
  32823. + if (IS_ERR(file)) {
  32824. + pr_err("%pd open err %ld\n", base, PTR_ERR(file));
  32825. + goto out_dput;
  32826. + }
  32827. +
  32828. + delegated = NULL;
  32829. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  32830. + if (unlikely(err == -EWOULDBLOCK)) {
  32831. + pr_warn("cannot retry for NFSv4 delegation"
  32832. + " for an internal unlink\n");
  32833. + iput(delegated);
  32834. + }
  32835. + if (unlikely(err)) {
  32836. + pr_err("%pd unlink err %d\n", base, err);
  32837. + goto out_fput;
  32838. + }
  32839. +
  32840. + if (copy_src) {
  32841. + /* no one can touch copy_src xino */
  32842. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  32843. + if (unlikely(err)) {
  32844. + pr_err("%pd copy err %d\n", base, err);
  32845. + goto out_fput;
  32846. + }
  32847. + }
  32848. + goto out_dput; /* success */
  32849. +
  32850. +out_fput:
  32851. + fput(file);
  32852. + file = ERR_PTR(err);
  32853. +out_dput:
  32854. + dput(path.dentry);
  32855. +out:
  32856. + return file;
  32857. +}
  32858. +
  32859. +struct au_xino_lock_dir {
  32860. + struct au_hinode *hdir;
  32861. + struct dentry *parent;
  32862. + struct mutex *mtx;
  32863. +};
  32864. +
  32865. +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
  32866. + struct au_xino_lock_dir *ldir)
  32867. +{
  32868. + aufs_bindex_t brid, bindex;
  32869. +
  32870. + ldir->hdir = NULL;
  32871. + bindex = -1;
  32872. + brid = au_xino_brid(sb);
  32873. + if (brid >= 0)
  32874. + bindex = au_br_index(sb, brid);
  32875. + if (bindex >= 0) {
  32876. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  32877. + au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  32878. + } else {
  32879. + ldir->parent = dget_parent(xino->f_path.dentry);
  32880. + ldir->mtx = &d_inode(ldir->parent)->i_mutex;
  32881. + mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
  32882. + }
  32883. +}
  32884. +
  32885. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  32886. +{
  32887. + if (ldir->hdir)
  32888. + au_hn_imtx_unlock(ldir->hdir);
  32889. + else {
  32890. + mutex_unlock(ldir->mtx);
  32891. + dput(ldir->parent);
  32892. + }
  32893. +}
  32894. +
  32895. +/* ---------------------------------------------------------------------- */
  32896. +
  32897. +/* trucate xino files asynchronously */
  32898. +
  32899. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
  32900. +{
  32901. + int err;
  32902. + unsigned long jiffy;
  32903. + blkcnt_t blocks;
  32904. + aufs_bindex_t bi, bend;
  32905. + struct kstatfs *st;
  32906. + struct au_branch *br;
  32907. + struct file *new_xino, *file;
  32908. + struct super_block *h_sb;
  32909. + struct au_xino_lock_dir ldir;
  32910. +
  32911. + err = -ENOMEM;
  32912. + st = kzalloc(sizeof(*st), GFP_NOFS);
  32913. + if (unlikely(!st))
  32914. + goto out;
  32915. +
  32916. + err = -EINVAL;
  32917. + bend = au_sbend(sb);
  32918. + if (unlikely(bindex < 0 || bend < bindex))
  32919. + goto out_st;
  32920. + br = au_sbr(sb, bindex);
  32921. + file = br->br_xino.xi_file;
  32922. + if (!file)
  32923. + goto out_st;
  32924. +
  32925. + err = vfs_statfs(&file->f_path, st);
  32926. + if (unlikely(err))
  32927. + AuErr1("statfs err %d, ignored\n", err);
  32928. + jiffy = jiffies;
  32929. + blocks = file_inode(file)->i_blocks;
  32930. + pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  32931. + bindex, (u64)blocks, st->f_bfree, st->f_blocks);
  32932. +
  32933. + au_xino_lock_dir(sb, file, &ldir);
  32934. + /* mnt_want_write() is unnecessary here */
  32935. + new_xino = au_xino_create2(file, file);
  32936. + au_xino_unlock_dir(&ldir);
  32937. + err = PTR_ERR(new_xino);
  32938. + if (IS_ERR(new_xino)) {
  32939. + pr_err("err %d, ignored\n", err);
  32940. + goto out_st;
  32941. + }
  32942. + err = 0;
  32943. + fput(file);
  32944. + br->br_xino.xi_file = new_xino;
  32945. +
  32946. + h_sb = au_br_sb(br);
  32947. + for (bi = 0; bi <= bend; bi++) {
  32948. + if (unlikely(bi == bindex))
  32949. + continue;
  32950. + br = au_sbr(sb, bi);
  32951. + if (au_br_sb(br) != h_sb)
  32952. + continue;
  32953. +
  32954. + fput(br->br_xino.xi_file);
  32955. + br->br_xino.xi_file = new_xino;
  32956. + get_file(new_xino);
  32957. + }
  32958. +
  32959. + err = vfs_statfs(&new_xino->f_path, st);
  32960. + if (!err) {
  32961. + pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
  32962. + bindex, (u64)file_inode(new_xino)->i_blocks,
  32963. + st->f_bfree, st->f_blocks);
  32964. + if (file_inode(new_xino)->i_blocks < blocks)
  32965. + au_sbi(sb)->si_xino_jiffy = jiffy;
  32966. + } else
  32967. + AuErr1("statfs err %d, ignored\n", err);
  32968. +
  32969. +out_st:
  32970. + kfree(st);
  32971. +out:
  32972. + return err;
  32973. +}
  32974. +
  32975. +struct xino_do_trunc_args {
  32976. + struct super_block *sb;
  32977. + struct au_branch *br;
  32978. +};
  32979. +
  32980. +static void xino_do_trunc(void *_args)
  32981. +{
  32982. + struct xino_do_trunc_args *args = _args;
  32983. + struct super_block *sb;
  32984. + struct au_branch *br;
  32985. + struct inode *dir;
  32986. + int err;
  32987. + aufs_bindex_t bindex;
  32988. +
  32989. + err = 0;
  32990. + sb = args->sb;
  32991. + dir = d_inode(sb->s_root);
  32992. + br = args->br;
  32993. +
  32994. + si_noflush_write_lock(sb);
  32995. + ii_read_lock_parent(dir);
  32996. + bindex = au_br_index(sb, br->br_id);
  32997. + err = au_xino_trunc(sb, bindex);
  32998. + ii_read_unlock(dir);
  32999. + if (unlikely(err))
  33000. + pr_warn("err b%d, (%d)\n", bindex, err);
  33001. + atomic_dec(&br->br_xino_running);
  33002. + atomic_dec(&br->br_count);
  33003. + si_write_unlock(sb);
  33004. + au_nwt_done(&au_sbi(sb)->si_nowait);
  33005. + kfree(args);
  33006. +}
  33007. +
  33008. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  33009. +{
  33010. + int err;
  33011. + struct kstatfs st;
  33012. + struct au_sbinfo *sbinfo;
  33013. +
  33014. + /* todo: si_xino_expire and the ratio should be customizable */
  33015. + sbinfo = au_sbi(sb);
  33016. + if (time_before(jiffies,
  33017. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  33018. + return 0;
  33019. +
  33020. + /* truncation border */
  33021. + err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
  33022. + if (unlikely(err)) {
  33023. + AuErr1("statfs err %d, ignored\n", err);
  33024. + return 0;
  33025. + }
  33026. + if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
  33027. + return 0;
  33028. +
  33029. + return 1;
  33030. +}
  33031. +
  33032. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  33033. +{
  33034. + struct xino_do_trunc_args *args;
  33035. + int wkq_err;
  33036. +
  33037. + if (!xino_trunc_test(sb, br))
  33038. + return;
  33039. +
  33040. + if (atomic_inc_return(&br->br_xino_running) > 1)
  33041. + goto out;
  33042. +
  33043. + /* lock and kfree() will be called in trunc_xino() */
  33044. + args = kmalloc(sizeof(*args), GFP_NOFS);
  33045. + if (unlikely(!args)) {
  33046. + AuErr1("no memory\n");
  33047. + goto out_args;
  33048. + }
  33049. +
  33050. + atomic_inc(&br->br_count);
  33051. + args->sb = sb;
  33052. + args->br = br;
  33053. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  33054. + if (!wkq_err)
  33055. + return; /* success */
  33056. +
  33057. + pr_err("wkq %d\n", wkq_err);
  33058. + atomic_dec(&br->br_count);
  33059. +
  33060. +out_args:
  33061. + kfree(args);
  33062. +out:
  33063. + atomic_dec(&br->br_xino_running);
  33064. +}
  33065. +
  33066. +/* ---------------------------------------------------------------------- */
  33067. +
  33068. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  33069. + ino_t h_ino, ino_t ino)
  33070. +{
  33071. + loff_t pos;
  33072. + ssize_t sz;
  33073. +
  33074. + pos = h_ino;
  33075. + if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
  33076. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  33077. + return -EFBIG;
  33078. + }
  33079. + pos *= sizeof(ino);
  33080. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
  33081. + if (sz == sizeof(ino))
  33082. + return 0; /* success */
  33083. +
  33084. + AuIOErr("write failed (%zd)\n", sz);
  33085. + return -EIO;
  33086. +}
  33087. +
  33088. +/*
  33089. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  33090. + * at the position of @h_ino.
  33091. + * even if @ino is zero, it is written to the xinofile and means no entry.
  33092. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  33093. + * try truncating it.
  33094. + */
  33095. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  33096. + ino_t ino)
  33097. +{
  33098. + int err;
  33099. + unsigned int mnt_flags;
  33100. + struct au_branch *br;
  33101. +
  33102. + BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
  33103. + || ((loff_t)-1) > 0);
  33104. + SiMustAnyLock(sb);
  33105. +
  33106. + mnt_flags = au_mntflags(sb);
  33107. + if (!au_opt_test(mnt_flags, XINO))
  33108. + return 0;
  33109. +
  33110. + br = au_sbr(sb, bindex);
  33111. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  33112. + h_ino, ino);
  33113. + if (!err) {
  33114. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  33115. + && au_test_fs_trunc_xino(au_br_sb(br)))
  33116. + xino_try_trunc(sb, br);
  33117. + return 0; /* success */
  33118. + }
  33119. +
  33120. + AuIOErr("write failed (%d)\n", err);
  33121. + return -EIO;
  33122. +}
  33123. +
  33124. +/* ---------------------------------------------------------------------- */
  33125. +
  33126. +/* aufs inode number bitmap */
  33127. +
  33128. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  33129. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  33130. +{
  33131. + ino_t ino;
  33132. +
  33133. + AuDebugOn(bit < 0 || page_bits <= bit);
  33134. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  33135. + return ino;
  33136. +}
  33137. +
  33138. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  33139. +{
  33140. + AuDebugOn(ino < AUFS_FIRST_INO);
  33141. + ino -= AUFS_FIRST_INO;
  33142. + *pindex = ino / page_bits;
  33143. + *bit = ino % page_bits;
  33144. +}
  33145. +
  33146. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  33147. +{
  33148. + int err;
  33149. + loff_t pos;
  33150. + ssize_t sz;
  33151. + struct au_sbinfo *sbinfo;
  33152. + struct file *xib;
  33153. + unsigned long *p;
  33154. +
  33155. + sbinfo = au_sbi(sb);
  33156. + MtxMustLock(&sbinfo->si_xib_mtx);
  33157. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  33158. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  33159. +
  33160. + if (pindex == sbinfo->si_xib_last_pindex)
  33161. + return 0;
  33162. +
  33163. + xib = sbinfo->si_xib;
  33164. + p = sbinfo->si_xib_buf;
  33165. + pos = sbinfo->si_xib_last_pindex;
  33166. + pos *= PAGE_SIZE;
  33167. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  33168. + if (unlikely(sz != PAGE_SIZE))
  33169. + goto out;
  33170. +
  33171. + pos = pindex;
  33172. + pos *= PAGE_SIZE;
  33173. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  33174. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  33175. + else {
  33176. + memset(p, 0, PAGE_SIZE);
  33177. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  33178. + }
  33179. + if (sz == PAGE_SIZE) {
  33180. + sbinfo->si_xib_last_pindex = pindex;
  33181. + return 0; /* success */
  33182. + }
  33183. +
  33184. +out:
  33185. + AuIOErr1("write failed (%zd)\n", sz);
  33186. + err = sz;
  33187. + if (sz >= 0)
  33188. + err = -EIO;
  33189. + return err;
  33190. +}
  33191. +
  33192. +/* ---------------------------------------------------------------------- */
  33193. +
  33194. +static void au_xib_clear_bit(struct inode *inode)
  33195. +{
  33196. + int err, bit;
  33197. + unsigned long pindex;
  33198. + struct super_block *sb;
  33199. + struct au_sbinfo *sbinfo;
  33200. +
  33201. + AuDebugOn(inode->i_nlink);
  33202. +
  33203. + sb = inode->i_sb;
  33204. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  33205. + AuDebugOn(page_bits <= bit);
  33206. + sbinfo = au_sbi(sb);
  33207. + mutex_lock(&sbinfo->si_xib_mtx);
  33208. + err = xib_pindex(sb, pindex);
  33209. + if (!err) {
  33210. + clear_bit(bit, sbinfo->si_xib_buf);
  33211. + sbinfo->si_xib_next_bit = bit;
  33212. + }
  33213. + mutex_unlock(&sbinfo->si_xib_mtx);
  33214. +}
  33215. +
  33216. +/* for s_op->delete_inode() */
  33217. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  33218. +{
  33219. + int err;
  33220. + unsigned int mnt_flags;
  33221. + aufs_bindex_t bindex, bend, bi;
  33222. + unsigned char try_trunc;
  33223. + struct au_iinfo *iinfo;
  33224. + struct super_block *sb;
  33225. + struct au_hinode *hi;
  33226. + struct inode *h_inode;
  33227. + struct au_branch *br;
  33228. + vfs_writef_t xwrite;
  33229. +
  33230. + sb = inode->i_sb;
  33231. + mnt_flags = au_mntflags(sb);
  33232. + if (!au_opt_test(mnt_flags, XINO)
  33233. + || inode->i_ino == AUFS_ROOT_INO)
  33234. + return;
  33235. +
  33236. + if (unlinked) {
  33237. + au_xigen_inc(inode);
  33238. + au_xib_clear_bit(inode);
  33239. + }
  33240. +
  33241. + iinfo = au_ii(inode);
  33242. + if (!iinfo)
  33243. + return;
  33244. +
  33245. + bindex = iinfo->ii_bstart;
  33246. + if (bindex < 0)
  33247. + return;
  33248. +
  33249. + xwrite = au_sbi(sb)->si_xwrite;
  33250. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  33251. + hi = iinfo->ii_hinode + bindex;
  33252. + bend = iinfo->ii_bend;
  33253. + for (; bindex <= bend; bindex++, hi++) {
  33254. + h_inode = hi->hi_inode;
  33255. + if (!h_inode
  33256. + || (!unlinked && h_inode->i_nlink))
  33257. + continue;
  33258. +
  33259. + /* inode may not be revalidated */
  33260. + bi = au_br_index(sb, hi->hi_id);
  33261. + if (bi < 0)
  33262. + continue;
  33263. +
  33264. + br = au_sbr(sb, bi);
  33265. + err = au_xino_do_write(xwrite, br->br_xino.xi_file,
  33266. + h_inode->i_ino, /*ino*/0);
  33267. + if (!err && try_trunc
  33268. + && au_test_fs_trunc_xino(au_br_sb(br)))
  33269. + xino_try_trunc(sb, br);
  33270. + }
  33271. +}
  33272. +
  33273. +/* get an unused inode number from bitmap */
  33274. +ino_t au_xino_new_ino(struct super_block *sb)
  33275. +{
  33276. + ino_t ino;
  33277. + unsigned long *p, pindex, ul, pend;
  33278. + struct au_sbinfo *sbinfo;
  33279. + struct file *file;
  33280. + int free_bit, err;
  33281. +
  33282. + if (!au_opt_test(au_mntflags(sb), XINO))
  33283. + return iunique(sb, AUFS_FIRST_INO);
  33284. +
  33285. + sbinfo = au_sbi(sb);
  33286. + mutex_lock(&sbinfo->si_xib_mtx);
  33287. + p = sbinfo->si_xib_buf;
  33288. + free_bit = sbinfo->si_xib_next_bit;
  33289. + if (free_bit < page_bits && !test_bit(free_bit, p))
  33290. + goto out; /* success */
  33291. + free_bit = find_first_zero_bit(p, page_bits);
  33292. + if (free_bit < page_bits)
  33293. + goto out; /* success */
  33294. +
  33295. + pindex = sbinfo->si_xib_last_pindex;
  33296. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  33297. + err = xib_pindex(sb, ul);
  33298. + if (unlikely(err))
  33299. + goto out_err;
  33300. + free_bit = find_first_zero_bit(p, page_bits);
  33301. + if (free_bit < page_bits)
  33302. + goto out; /* success */
  33303. + }
  33304. +
  33305. + file = sbinfo->si_xib;
  33306. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  33307. + for (ul = pindex + 1; ul <= pend; ul++) {
  33308. + err = xib_pindex(sb, ul);
  33309. + if (unlikely(err))
  33310. + goto out_err;
  33311. + free_bit = find_first_zero_bit(p, page_bits);
  33312. + if (free_bit < page_bits)
  33313. + goto out; /* success */
  33314. + }
  33315. + BUG();
  33316. +
  33317. +out:
  33318. + set_bit(free_bit, p);
  33319. + sbinfo->si_xib_next_bit = free_bit + 1;
  33320. + pindex = sbinfo->si_xib_last_pindex;
  33321. + mutex_unlock(&sbinfo->si_xib_mtx);
  33322. + ino = xib_calc_ino(pindex, free_bit);
  33323. + AuDbg("i%lu\n", (unsigned long)ino);
  33324. + return ino;
  33325. +out_err:
  33326. + mutex_unlock(&sbinfo->si_xib_mtx);
  33327. + AuDbg("i0\n");
  33328. + return 0;
  33329. +}
  33330. +
  33331. +/*
  33332. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  33333. + * at the position of @h_ino.
  33334. + * if @ino does not exist and @do_new is true, get new one.
  33335. + */
  33336. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  33337. + ino_t *ino)
  33338. +{
  33339. + int err;
  33340. + ssize_t sz;
  33341. + loff_t pos;
  33342. + struct file *file;
  33343. + struct au_sbinfo *sbinfo;
  33344. +
  33345. + *ino = 0;
  33346. + if (!au_opt_test(au_mntflags(sb), XINO))
  33347. + return 0; /* no xino */
  33348. +
  33349. + err = 0;
  33350. + sbinfo = au_sbi(sb);
  33351. + pos = h_ino;
  33352. + if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
  33353. + AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
  33354. + return -EFBIG;
  33355. + }
  33356. + pos *= sizeof(*ino);
  33357. +
  33358. + file = au_sbr(sb, bindex)->br_xino.xi_file;
  33359. + if (vfsub_f_size_read(file) < pos + sizeof(*ino))
  33360. + return 0; /* no ino */
  33361. +
  33362. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
  33363. + if (sz == sizeof(*ino))
  33364. + return 0; /* success */
  33365. +
  33366. + err = sz;
  33367. + if (unlikely(sz >= 0)) {
  33368. + err = -EIO;
  33369. + AuIOErr("xino read error (%zd)\n", sz);
  33370. + }
  33371. +
  33372. + return err;
  33373. +}
  33374. +
  33375. +/* ---------------------------------------------------------------------- */
  33376. +
  33377. +/* create and set a new xino file */
  33378. +
  33379. +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
  33380. +{
  33381. + struct file *file;
  33382. + struct dentry *h_parent, *d;
  33383. + struct inode *h_dir, *inode;
  33384. + int err;
  33385. +
  33386. + /*
  33387. + * at mount-time, and the xino file is the default path,
  33388. + * hnotify is disabled so we have no notify events to ignore.
  33389. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  33390. + */
  33391. + file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  33392. + /* | __FMODE_NONOTIFY */,
  33393. + S_IRUGO | S_IWUGO);
  33394. + if (IS_ERR(file)) {
  33395. + if (!silent)
  33396. + pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
  33397. + return file;
  33398. + }
  33399. +
  33400. + /* keep file count */
  33401. + err = 0;
  33402. + inode = file_inode(file);
  33403. + h_parent = dget_parent(file->f_path.dentry);
  33404. + h_dir = d_inode(h_parent);
  33405. + mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
  33406. + /* mnt_want_write() is unnecessary here */
  33407. + /* no delegation since it is just created */
  33408. + if (inode->i_nlink)
  33409. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  33410. + /*force*/0);
  33411. + mutex_unlock(&h_dir->i_mutex);
  33412. + dput(h_parent);
  33413. + if (unlikely(err)) {
  33414. + if (!silent)
  33415. + pr_err("unlink %s(%d)\n", fname, err);
  33416. + goto out;
  33417. + }
  33418. +
  33419. + err = -EINVAL;
  33420. + d = file->f_path.dentry;
  33421. + if (unlikely(sb == d->d_sb)) {
  33422. + if (!silent)
  33423. + pr_err("%s must be outside\n", fname);
  33424. + goto out;
  33425. + }
  33426. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  33427. + if (!silent)
  33428. + pr_err("xino doesn't support %s(%s)\n",
  33429. + fname, au_sbtype(d->d_sb));
  33430. + goto out;
  33431. + }
  33432. + return file; /* success */
  33433. +
  33434. +out:
  33435. + fput(file);
  33436. + file = ERR_PTR(err);
  33437. + return file;
  33438. +}
  33439. +
  33440. +/*
  33441. + * find another branch who is on the same filesystem of the specified
  33442. + * branch{@btgt}. search until @bend.
  33443. + */
  33444. +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  33445. + aufs_bindex_t bend)
  33446. +{
  33447. + aufs_bindex_t bindex;
  33448. + struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
  33449. +
  33450. + for (bindex = 0; bindex < btgt; bindex++)
  33451. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  33452. + return bindex;
  33453. + for (bindex++; bindex <= bend; bindex++)
  33454. + if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
  33455. + return bindex;
  33456. + return -1;
  33457. +}
  33458. +
  33459. +/* ---------------------------------------------------------------------- */
  33460. +
  33461. +/*
  33462. + * initialize the xinofile for the specified branch @br
  33463. + * at the place/path where @base_file indicates.
  33464. + * test whether another branch is on the same filesystem or not,
  33465. + * if @do_test is true.
  33466. + */
  33467. +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  33468. + struct file *base_file, int do_test)
  33469. +{
  33470. + int err;
  33471. + ino_t ino;
  33472. + aufs_bindex_t bend, bindex;
  33473. + struct au_branch *shared_br, *b;
  33474. + struct file *file;
  33475. + struct super_block *tgt_sb;
  33476. +
  33477. + shared_br = NULL;
  33478. + bend = au_sbend(sb);
  33479. + if (do_test) {
  33480. + tgt_sb = au_br_sb(br);
  33481. + for (bindex = 0; bindex <= bend; bindex++) {
  33482. + b = au_sbr(sb, bindex);
  33483. + if (tgt_sb == au_br_sb(b)) {
  33484. + shared_br = b;
  33485. + break;
  33486. + }
  33487. + }
  33488. + }
  33489. +
  33490. + if (!shared_br || !shared_br->br_xino.xi_file) {
  33491. + struct au_xino_lock_dir ldir;
  33492. +
  33493. + au_xino_lock_dir(sb, base_file, &ldir);
  33494. + /* mnt_want_write() is unnecessary here */
  33495. + file = au_xino_create2(base_file, NULL);
  33496. + au_xino_unlock_dir(&ldir);
  33497. + err = PTR_ERR(file);
  33498. + if (IS_ERR(file))
  33499. + goto out;
  33500. + br->br_xino.xi_file = file;
  33501. + } else {
  33502. + br->br_xino.xi_file = shared_br->br_xino.xi_file;
  33503. + get_file(br->br_xino.xi_file);
  33504. + }
  33505. +
  33506. + ino = AUFS_ROOT_INO;
  33507. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
  33508. + h_ino, ino);
  33509. + if (unlikely(err)) {
  33510. + fput(br->br_xino.xi_file);
  33511. + br->br_xino.xi_file = NULL;
  33512. + }
  33513. +
  33514. +out:
  33515. + return err;
  33516. +}
  33517. +
  33518. +/* ---------------------------------------------------------------------- */
  33519. +
  33520. +/* trucate a xino bitmap file */
  33521. +
  33522. +/* todo: slow */
  33523. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  33524. +{
  33525. + int err, bit;
  33526. + ssize_t sz;
  33527. + unsigned long pindex;
  33528. + loff_t pos, pend;
  33529. + struct au_sbinfo *sbinfo;
  33530. + vfs_readf_t func;
  33531. + ino_t *ino;
  33532. + unsigned long *p;
  33533. +
  33534. + err = 0;
  33535. + sbinfo = au_sbi(sb);
  33536. + MtxMustLock(&sbinfo->si_xib_mtx);
  33537. + p = sbinfo->si_xib_buf;
  33538. + func = sbinfo->si_xread;
  33539. + pend = vfsub_f_size_read(file);
  33540. + pos = 0;
  33541. + while (pos < pend) {
  33542. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  33543. + err = sz;
  33544. + if (unlikely(sz <= 0))
  33545. + goto out;
  33546. +
  33547. + err = 0;
  33548. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  33549. + if (unlikely(*ino < AUFS_FIRST_INO))
  33550. + continue;
  33551. +
  33552. + xib_calc_bit(*ino, &pindex, &bit);
  33553. + AuDebugOn(page_bits <= bit);
  33554. + err = xib_pindex(sb, pindex);
  33555. + if (!err)
  33556. + set_bit(bit, p);
  33557. + else
  33558. + goto out;
  33559. + }
  33560. + }
  33561. +
  33562. +out:
  33563. + return err;
  33564. +}
  33565. +
  33566. +static int xib_restore(struct super_block *sb)
  33567. +{
  33568. + int err;
  33569. + aufs_bindex_t bindex, bend;
  33570. + void *page;
  33571. +
  33572. + err = -ENOMEM;
  33573. + page = (void *)__get_free_page(GFP_NOFS);
  33574. + if (unlikely(!page))
  33575. + goto out;
  33576. +
  33577. + err = 0;
  33578. + bend = au_sbend(sb);
  33579. + for (bindex = 0; !err && bindex <= bend; bindex++)
  33580. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
  33581. + err = do_xib_restore
  33582. + (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
  33583. + else
  33584. + AuDbg("b%d\n", bindex);
  33585. + free_page((unsigned long)page);
  33586. +
  33587. +out:
  33588. + return err;
  33589. +}
  33590. +
  33591. +int au_xib_trunc(struct super_block *sb)
  33592. +{
  33593. + int err;
  33594. + ssize_t sz;
  33595. + loff_t pos;
  33596. + struct au_xino_lock_dir ldir;
  33597. + struct au_sbinfo *sbinfo;
  33598. + unsigned long *p;
  33599. + struct file *file;
  33600. +
  33601. + SiMustWriteLock(sb);
  33602. +
  33603. + err = 0;
  33604. + sbinfo = au_sbi(sb);
  33605. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  33606. + goto out;
  33607. +
  33608. + file = sbinfo->si_xib;
  33609. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  33610. + goto out;
  33611. +
  33612. + au_xino_lock_dir(sb, file, &ldir);
  33613. + /* mnt_want_write() is unnecessary here */
  33614. + file = au_xino_create2(sbinfo->si_xib, NULL);
  33615. + au_xino_unlock_dir(&ldir);
  33616. + err = PTR_ERR(file);
  33617. + if (IS_ERR(file))
  33618. + goto out;
  33619. + fput(sbinfo->si_xib);
  33620. + sbinfo->si_xib = file;
  33621. +
  33622. + p = sbinfo->si_xib_buf;
  33623. + memset(p, 0, PAGE_SIZE);
  33624. + pos = 0;
  33625. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  33626. + if (unlikely(sz != PAGE_SIZE)) {
  33627. + err = sz;
  33628. + AuIOErr("err %d\n", err);
  33629. + if (sz >= 0)
  33630. + err = -EIO;
  33631. + goto out;
  33632. + }
  33633. +
  33634. + mutex_lock(&sbinfo->si_xib_mtx);
  33635. + /* mnt_want_write() is unnecessary here */
  33636. + err = xib_restore(sb);
  33637. + mutex_unlock(&sbinfo->si_xib_mtx);
  33638. +
  33639. +out:
  33640. + return err;
  33641. +}
  33642. +
  33643. +/* ---------------------------------------------------------------------- */
  33644. +
  33645. +/*
  33646. + * xino mount option handlers
  33647. + */
  33648. +
  33649. +/* xino bitmap */
  33650. +static void xino_clear_xib(struct super_block *sb)
  33651. +{
  33652. + struct au_sbinfo *sbinfo;
  33653. +
  33654. + SiMustWriteLock(sb);
  33655. +
  33656. + sbinfo = au_sbi(sb);
  33657. + sbinfo->si_xread = NULL;
  33658. + sbinfo->si_xwrite = NULL;
  33659. + if (sbinfo->si_xib)
  33660. + fput(sbinfo->si_xib);
  33661. + sbinfo->si_xib = NULL;
  33662. + free_page((unsigned long)sbinfo->si_xib_buf);
  33663. + sbinfo->si_xib_buf = NULL;
  33664. +}
  33665. +
  33666. +static int au_xino_set_xib(struct super_block *sb, struct file *base)
  33667. +{
  33668. + int err;
  33669. + loff_t pos;
  33670. + struct au_sbinfo *sbinfo;
  33671. + struct file *file;
  33672. +
  33673. + SiMustWriteLock(sb);
  33674. +
  33675. + sbinfo = au_sbi(sb);
  33676. + file = au_xino_create2(base, sbinfo->si_xib);
  33677. + err = PTR_ERR(file);
  33678. + if (IS_ERR(file))
  33679. + goto out;
  33680. + if (sbinfo->si_xib)
  33681. + fput(sbinfo->si_xib);
  33682. + sbinfo->si_xib = file;
  33683. + sbinfo->si_xread = vfs_readf(file);
  33684. + sbinfo->si_xwrite = vfs_writef(file);
  33685. +
  33686. + err = -ENOMEM;
  33687. + if (!sbinfo->si_xib_buf)
  33688. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  33689. + if (unlikely(!sbinfo->si_xib_buf))
  33690. + goto out_unset;
  33691. +
  33692. + sbinfo->si_xib_last_pindex = 0;
  33693. + sbinfo->si_xib_next_bit = 0;
  33694. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  33695. + pos = 0;
  33696. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  33697. + PAGE_SIZE, &pos);
  33698. + if (unlikely(err != PAGE_SIZE))
  33699. + goto out_free;
  33700. + }
  33701. + err = 0;
  33702. + goto out; /* success */
  33703. +
  33704. +out_free:
  33705. + free_page((unsigned long)sbinfo->si_xib_buf);
  33706. + sbinfo->si_xib_buf = NULL;
  33707. + if (err >= 0)
  33708. + err = -EIO;
  33709. +out_unset:
  33710. + fput(sbinfo->si_xib);
  33711. + sbinfo->si_xib = NULL;
  33712. + sbinfo->si_xread = NULL;
  33713. + sbinfo->si_xwrite = NULL;
  33714. +out:
  33715. + return err;
  33716. +}
  33717. +
  33718. +/* xino for each branch */
  33719. +static void xino_clear_br(struct super_block *sb)
  33720. +{
  33721. + aufs_bindex_t bindex, bend;
  33722. + struct au_branch *br;
  33723. +
  33724. + bend = au_sbend(sb);
  33725. + for (bindex = 0; bindex <= bend; bindex++) {
  33726. + br = au_sbr(sb, bindex);
  33727. + if (!br || !br->br_xino.xi_file)
  33728. + continue;
  33729. +
  33730. + fput(br->br_xino.xi_file);
  33731. + br->br_xino.xi_file = NULL;
  33732. + }
  33733. +}
  33734. +
  33735. +static int au_xino_set_br(struct super_block *sb, struct file *base)
  33736. +{
  33737. + int err;
  33738. + ino_t ino;
  33739. + aufs_bindex_t bindex, bend, bshared;
  33740. + struct {
  33741. + struct file *old, *new;
  33742. + } *fpair, *p;
  33743. + struct au_branch *br;
  33744. + struct inode *inode;
  33745. + vfs_writef_t writef;
  33746. +
  33747. + SiMustWriteLock(sb);
  33748. +
  33749. + err = -ENOMEM;
  33750. + bend = au_sbend(sb);
  33751. + fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
  33752. + if (unlikely(!fpair))
  33753. + goto out;
  33754. +
  33755. + inode = d_inode(sb->s_root);
  33756. + ino = AUFS_ROOT_INO;
  33757. + writef = au_sbi(sb)->si_xwrite;
  33758. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
  33759. + br = au_sbr(sb, bindex);
  33760. + bshared = is_sb_shared(sb, bindex, bindex - 1);
  33761. + if (bshared >= 0) {
  33762. + /* shared xino */
  33763. + *p = fpair[bshared];
  33764. + get_file(p->new);
  33765. + }
  33766. +
  33767. + if (!p->new) {
  33768. + /* new xino */
  33769. + p->old = br->br_xino.xi_file;
  33770. + p->new = au_xino_create2(base, br->br_xino.xi_file);
  33771. + err = PTR_ERR(p->new);
  33772. + if (IS_ERR(p->new)) {
  33773. + p->new = NULL;
  33774. + goto out_pair;
  33775. + }
  33776. + }
  33777. +
  33778. + err = au_xino_do_write(writef, p->new,
  33779. + au_h_iptr(inode, bindex)->i_ino, ino);
  33780. + if (unlikely(err))
  33781. + goto out_pair;
  33782. + }
  33783. +
  33784. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
  33785. + br = au_sbr(sb, bindex);
  33786. + if (br->br_xino.xi_file)
  33787. + fput(br->br_xino.xi_file);
  33788. + get_file(p->new);
  33789. + br->br_xino.xi_file = p->new;
  33790. + }
  33791. +
  33792. +out_pair:
  33793. + for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
  33794. + if (p->new)
  33795. + fput(p->new);
  33796. + else
  33797. + break;
  33798. + kfree(fpair);
  33799. +out:
  33800. + return err;
  33801. +}
  33802. +
  33803. +void au_xino_clr(struct super_block *sb)
  33804. +{
  33805. + struct au_sbinfo *sbinfo;
  33806. +
  33807. + au_xigen_clr(sb);
  33808. + xino_clear_xib(sb);
  33809. + xino_clear_br(sb);
  33810. + sbinfo = au_sbi(sb);
  33811. + /* lvalue, do not call au_mntflags() */
  33812. + au_opt_clr(sbinfo->si_mntflags, XINO);
  33813. +}
  33814. +
  33815. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
  33816. +{
  33817. + int err, skip;
  33818. + struct dentry *parent, *cur_parent;
  33819. + struct qstr *dname, *cur_name;
  33820. + struct file *cur_xino;
  33821. + struct inode *dir;
  33822. + struct au_sbinfo *sbinfo;
  33823. +
  33824. + SiMustWriteLock(sb);
  33825. +
  33826. + err = 0;
  33827. + sbinfo = au_sbi(sb);
  33828. + parent = dget_parent(xino->file->f_path.dentry);
  33829. + if (remount) {
  33830. + skip = 0;
  33831. + dname = &xino->file->f_path.dentry->d_name;
  33832. + cur_xino = sbinfo->si_xib;
  33833. + if (cur_xino) {
  33834. + cur_parent = dget_parent(cur_xino->f_path.dentry);
  33835. + cur_name = &cur_xino->f_path.dentry->d_name;
  33836. + skip = (cur_parent == parent
  33837. + && au_qstreq(dname, cur_name));
  33838. + dput(cur_parent);
  33839. + }
  33840. + if (skip)
  33841. + goto out;
  33842. + }
  33843. +
  33844. + au_opt_set(sbinfo->si_mntflags, XINO);
  33845. + dir = d_inode(parent);
  33846. + mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
  33847. + /* mnt_want_write() is unnecessary here */
  33848. + err = au_xino_set_xib(sb, xino->file);
  33849. + if (!err)
  33850. + err = au_xigen_set(sb, xino->file);
  33851. + if (!err)
  33852. + err = au_xino_set_br(sb, xino->file);
  33853. + mutex_unlock(&dir->i_mutex);
  33854. + if (!err)
  33855. + goto out; /* success */
  33856. +
  33857. + /* reset all */
  33858. + AuIOErr("failed creating xino(%d).\n", err);
  33859. + au_xigen_clr(sb);
  33860. + xino_clear_xib(sb);
  33861. +
  33862. +out:
  33863. + dput(parent);
  33864. + return err;
  33865. +}
  33866. +
  33867. +/* ---------------------------------------------------------------------- */
  33868. +
  33869. +/*
  33870. + * create a xinofile at the default place/path.
  33871. + */
  33872. +struct file *au_xino_def(struct super_block *sb)
  33873. +{
  33874. + struct file *file;
  33875. + char *page, *p;
  33876. + struct au_branch *br;
  33877. + struct super_block *h_sb;
  33878. + struct path path;
  33879. + aufs_bindex_t bend, bindex, bwr;
  33880. +
  33881. + br = NULL;
  33882. + bend = au_sbend(sb);
  33883. + bwr = -1;
  33884. + for (bindex = 0; bindex <= bend; bindex++) {
  33885. + br = au_sbr(sb, bindex);
  33886. + if (au_br_writable(br->br_perm)
  33887. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  33888. + bwr = bindex;
  33889. + break;
  33890. + }
  33891. + }
  33892. +
  33893. + if (bwr >= 0) {
  33894. + file = ERR_PTR(-ENOMEM);
  33895. + page = (void *)__get_free_page(GFP_NOFS);
  33896. + if (unlikely(!page))
  33897. + goto out;
  33898. + path.mnt = au_br_mnt(br);
  33899. + path.dentry = au_h_dptr(sb->s_root, bwr);
  33900. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  33901. + file = (void *)p;
  33902. + if (!IS_ERR(p)) {
  33903. + strcat(p, "/" AUFS_XINO_FNAME);
  33904. + AuDbg("%s\n", p);
  33905. + file = au_xino_create(sb, p, /*silent*/0);
  33906. + if (!IS_ERR(file))
  33907. + au_xino_brid_set(sb, br->br_id);
  33908. + }
  33909. + free_page((unsigned long)page);
  33910. + } else {
  33911. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  33912. + if (IS_ERR(file))
  33913. + goto out;
  33914. + h_sb = file->f_path.dentry->d_sb;
  33915. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  33916. + pr_err("xino doesn't support %s(%s)\n",
  33917. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  33918. + fput(file);
  33919. + file = ERR_PTR(-EINVAL);
  33920. + }
  33921. + if (!IS_ERR(file))
  33922. + au_xino_brid_set(sb, -1);
  33923. + }
  33924. +
  33925. +out:
  33926. + return file;
  33927. +}
  33928. +
  33929. +/* ---------------------------------------------------------------------- */
  33930. +
  33931. +int au_xino_path(struct seq_file *seq, struct file *file)
  33932. +{
  33933. + int err;
  33934. +
  33935. + err = au_seq_path(seq, &file->f_path);
  33936. + if (unlikely(err))
  33937. + goto out;
  33938. +
  33939. +#define Deleted "\\040(deleted)"
  33940. + seq->count -= sizeof(Deleted) - 1;
  33941. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  33942. + sizeof(Deleted) - 1));
  33943. +#undef Deleted
  33944. +
  33945. +out:
  33946. + return err;
  33947. +}
  33948. diff -Nur linux-4.1.10.orig/fs/buffer.c linux-4.1.10/fs/buffer.c
  33949. --- linux-4.1.10.orig/fs/buffer.c 2015-10-03 13:49:38.000000000 +0200
  33950. +++ linux-4.1.10/fs/buffer.c 2015-10-22 21:35:53.000000000 +0200
  33951. @@ -2450,7 +2450,7 @@
  33952. * Update file times before taking page lock. We may end up failing the
  33953. * fault so this update may be superfluous but who really cares...
  33954. */
  33955. - file_update_time(vma->vm_file);
  33956. + vma_file_update_time(vma);
  33957. ret = __block_page_mkwrite(vma, vmf, get_block);
  33958. sb_end_pagefault(sb);
  33959. diff -Nur linux-4.1.10.orig/fs/dcache.c linux-4.1.10/fs/dcache.c
  33960. --- linux-4.1.10.orig/fs/dcache.c 2015-10-03 13:49:38.000000000 +0200
  33961. +++ linux-4.1.10/fs/dcache.c 2015-10-22 21:35:53.000000000 +0200
  33962. @@ -1167,7 +1167,7 @@
  33963. *
  33964. * The @enter() and @finish() callbacks are called with d_lock held.
  33965. */
  33966. -static void d_walk(struct dentry *parent, void *data,
  33967. +void d_walk(struct dentry *parent, void *data,
  33968. enum d_walk_ret (*enter)(void *, struct dentry *),
  33969. void (*finish)(void *))
  33970. {
  33971. @@ -1272,6 +1272,7 @@
  33972. seq = 1;
  33973. goto again;
  33974. }
  33975. +EXPORT_SYMBOL(d_walk);
  33976. /*
  33977. * Search for at least 1 mount point in the dentry's subdirs.
  33978. diff -Nur linux-4.1.10.orig/fs/file_table.c linux-4.1.10/fs/file_table.c
  33979. --- linux-4.1.10.orig/fs/file_table.c 2015-10-03 13:49:38.000000000 +0200
  33980. +++ linux-4.1.10/fs/file_table.c 2015-10-22 21:35:53.000000000 +0200
  33981. @@ -147,6 +147,7 @@
  33982. }
  33983. return ERR_PTR(-ENFILE);
  33984. }
  33985. +EXPORT_SYMBOL(get_empty_filp);
  33986. /**
  33987. * alloc_file - allocate and initialize a 'struct file'
  33988. @@ -308,6 +309,7 @@
  33989. file_free(file);
  33990. }
  33991. }
  33992. +EXPORT_SYMBOL(put_filp);
  33993. void __init files_init(unsigned long mempages)
  33994. {
  33995. diff -Nur linux-4.1.10.orig/fs/inode.c linux-4.1.10/fs/inode.c
  33996. --- linux-4.1.10.orig/fs/inode.c 2015-10-03 13:49:38.000000000 +0200
  33997. +++ linux-4.1.10/fs/inode.c 2015-10-22 21:35:53.000000000 +0200
  33998. @@ -58,6 +58,7 @@
  33999. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
  34000. __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
  34001. +EXPORT_SYMBOL(inode_sb_list_lock);
  34002. /*
  34003. * Empty aops. Can be used for the cases where the user does not
  34004. diff -Nur linux-4.1.10.orig/fs/Kconfig linux-4.1.10/fs/Kconfig
  34005. --- linux-4.1.10.orig/fs/Kconfig 2015-10-03 13:49:38.000000000 +0200
  34006. +++ linux-4.1.10/fs/Kconfig 2015-10-22 21:35:53.000000000 +0200
  34007. @@ -218,6 +218,7 @@
  34008. source "fs/sysv/Kconfig"
  34009. source "fs/ufs/Kconfig"
  34010. source "fs/exofs/Kconfig"
  34011. +source "fs/aufs/Kconfig"
  34012. endif # MISC_FILESYSTEMS
  34013. diff -Nur linux-4.1.10.orig/fs/Makefile linux-4.1.10/fs/Makefile
  34014. --- linux-4.1.10.orig/fs/Makefile 2015-10-03 13:49:38.000000000 +0200
  34015. +++ linux-4.1.10/fs/Makefile 2015-10-22 21:35:53.000000000 +0200
  34016. @@ -127,3 +127,4 @@
  34017. obj-$(CONFIG_CEPH_FS) += ceph/
  34018. obj-$(CONFIG_PSTORE) += pstore/
  34019. obj-$(CONFIG_EFIVAR_FS) += efivarfs/
  34020. +obj-$(CONFIG_AUFS_FS) += aufs/
  34021. diff -Nur linux-4.1.10.orig/fs/namespace.c linux-4.1.10/fs/namespace.c
  34022. --- linux-4.1.10.orig/fs/namespace.c 2015-10-03 13:49:38.000000000 +0200
  34023. +++ linux-4.1.10/fs/namespace.c 2015-10-22 21:35:53.000000000 +0200
  34024. @@ -463,6 +463,7 @@
  34025. mnt_dec_writers(real_mount(mnt));
  34026. preempt_enable();
  34027. }
  34028. +EXPORT_SYMBOL_GPL(__mnt_drop_write);
  34029. /**
  34030. * mnt_drop_write - give up write access to a mount
  34031. @@ -1792,6 +1793,7 @@
  34032. }
  34033. return 0;
  34034. }
  34035. +EXPORT_SYMBOL(iterate_mounts);
  34036. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  34037. {
  34038. diff -Nur linux-4.1.10.orig/fs/notify/group.c linux-4.1.10/fs/notify/group.c
  34039. --- linux-4.1.10.orig/fs/notify/group.c 2015-10-03 13:49:38.000000000 +0200
  34040. +++ linux-4.1.10/fs/notify/group.c 2015-10-22 21:35:53.000000000 +0200
  34041. @@ -22,6 +22,7 @@
  34042. #include <linux/srcu.h>
  34043. #include <linux/rculist.h>
  34044. #include <linux/wait.h>
  34045. +#include <linux/module.h>
  34046. #include <linux/fsnotify_backend.h>
  34047. #include "fsnotify.h"
  34048. @@ -72,6 +73,7 @@
  34049. {
  34050. atomic_inc(&group->refcnt);
  34051. }
  34052. +EXPORT_SYMBOL(fsnotify_get_group);
  34053. /*
  34054. * Drop a reference to a group. Free it if it's through.
  34055. @@ -81,6 +83,7 @@
  34056. if (atomic_dec_and_test(&group->refcnt))
  34057. fsnotify_final_destroy_group(group);
  34058. }
  34059. +EXPORT_SYMBOL(fsnotify_put_group);
  34060. /*
  34061. * Create a new fsnotify_group and hold a reference for the group returned.
  34062. @@ -109,6 +112,7 @@
  34063. return group;
  34064. }
  34065. +EXPORT_SYMBOL(fsnotify_alloc_group);
  34066. int fsnotify_fasync(int fd, struct file *file, int on)
  34067. {
  34068. diff -Nur linux-4.1.10.orig/fs/notify/mark.c linux-4.1.10/fs/notify/mark.c
  34069. --- linux-4.1.10.orig/fs/notify/mark.c 2015-10-03 13:49:38.000000000 +0200
  34070. +++ linux-4.1.10/fs/notify/mark.c 2015-10-22 21:35:53.000000000 +0200
  34071. @@ -109,6 +109,7 @@
  34072. mark->free_mark(mark);
  34073. }
  34074. }
  34075. +EXPORT_SYMBOL(fsnotify_put_mark);
  34076. /* Calculate mask of events for a list of marks */
  34077. u32 fsnotify_recalc_mask(struct hlist_head *head)
  34078. @@ -202,6 +203,7 @@
  34079. fsnotify_destroy_mark_locked(mark, group);
  34080. mutex_unlock(&group->mark_mutex);
  34081. }
  34082. +EXPORT_SYMBOL(fsnotify_destroy_mark);
  34083. /*
  34084. * Destroy all marks in the given list. The marks must be already detached from
  34085. @@ -376,6 +378,7 @@
  34086. return ret;
  34087. }
  34088. +EXPORT_SYMBOL(fsnotify_add_mark);
  34089. int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
  34090. struct inode *inode, struct vfsmount *mnt, int allow_dups)
  34091. @@ -475,6 +478,7 @@
  34092. atomic_set(&mark->refcnt, 1);
  34093. mark->free_mark = free_mark;
  34094. }
  34095. +EXPORT_SYMBOL(fsnotify_init_mark);
  34096. static int fsnotify_mark_destroy(void *ignored)
  34097. {
  34098. diff -Nur linux-4.1.10.orig/fs/open.c linux-4.1.10/fs/open.c
  34099. --- linux-4.1.10.orig/fs/open.c 2015-10-03 13:49:38.000000000 +0200
  34100. +++ linux-4.1.10/fs/open.c 2015-10-22 21:35:53.000000000 +0200
  34101. @@ -62,6 +62,7 @@
  34102. mutex_unlock(&dentry->d_inode->i_mutex);
  34103. return ret;
  34104. }
  34105. +EXPORT_SYMBOL(do_truncate);
  34106. long vfs_truncate(struct path *path, loff_t length)
  34107. {
  34108. @@ -676,6 +677,7 @@
  34109. }
  34110. return 0;
  34111. }
  34112. +EXPORT_SYMBOL(open_check_o_direct);
  34113. static int do_dentry_open(struct file *f,
  34114. int (*open)(struct inode *, struct file *),
  34115. diff -Nur linux-4.1.10.orig/fs/proc/base.c linux-4.1.10/fs/proc/base.c
  34116. --- linux-4.1.10.orig/fs/proc/base.c 2015-10-03 13:49:38.000000000 +0200
  34117. +++ linux-4.1.10/fs/proc/base.c 2015-10-22 21:35:53.000000000 +0200
  34118. @@ -1744,7 +1744,7 @@
  34119. down_read(&mm->mmap_sem);
  34120. vma = find_exact_vma(mm, vm_start, vm_end);
  34121. if (vma && vma->vm_file) {
  34122. - *path = vma->vm_file->f_path;
  34123. + *path = vma_pr_or_file(vma)->f_path;
  34124. path_get(path);
  34125. rc = 0;
  34126. }
  34127. diff -Nur linux-4.1.10.orig/fs/proc/nommu.c linux-4.1.10/fs/proc/nommu.c
  34128. --- linux-4.1.10.orig/fs/proc/nommu.c 2015-10-03 13:49:38.000000000 +0200
  34129. +++ linux-4.1.10/fs/proc/nommu.c 2015-10-22 21:35:53.000000000 +0200
  34130. @@ -45,7 +45,10 @@
  34131. file = region->vm_file;
  34132. if (file) {
  34133. - struct inode *inode = file_inode(region->vm_file);
  34134. + struct inode *inode;
  34135. +
  34136. + file = vmr_pr_or_file(region);
  34137. + inode = file_inode(file);
  34138. dev = inode->i_sb->s_dev;
  34139. ino = inode->i_ino;
  34140. }
  34141. diff -Nur linux-4.1.10.orig/fs/proc/task_mmu.c linux-4.1.10/fs/proc/task_mmu.c
  34142. --- linux-4.1.10.orig/fs/proc/task_mmu.c 2015-10-03 13:49:38.000000000 +0200
  34143. +++ linux-4.1.10/fs/proc/task_mmu.c 2015-10-22 21:35:53.000000000 +0200
  34144. @@ -279,7 +279,10 @@
  34145. const char *name = NULL;
  34146. if (file) {
  34147. - struct inode *inode = file_inode(vma->vm_file);
  34148. + struct inode *inode;
  34149. +
  34150. + file = vma_pr_or_file(vma);
  34151. + inode = file_inode(file);
  34152. dev = inode->i_sb->s_dev;
  34153. ino = inode->i_ino;
  34154. pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
  34155. @@ -1479,7 +1482,7 @@
  34156. struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
  34157. struct vm_area_struct *vma = v;
  34158. struct numa_maps *md = &numa_priv->md;
  34159. - struct file *file = vma->vm_file;
  34160. + struct file *file = vma_pr_or_file(vma);
  34161. struct mm_struct *mm = vma->vm_mm;
  34162. struct mm_walk walk = {
  34163. .hugetlb_entry = gather_hugetlb_stats,
  34164. diff -Nur linux-4.1.10.orig/fs/proc/task_nommu.c linux-4.1.10/fs/proc/task_nommu.c
  34165. --- linux-4.1.10.orig/fs/proc/task_nommu.c 2015-10-03 13:49:38.000000000 +0200
  34166. +++ linux-4.1.10/fs/proc/task_nommu.c 2015-10-22 21:35:53.000000000 +0200
  34167. @@ -160,7 +160,10 @@
  34168. file = vma->vm_file;
  34169. if (file) {
  34170. - struct inode *inode = file_inode(vma->vm_file);
  34171. + struct inode *inode;
  34172. +
  34173. + file = vma_pr_or_file(vma);
  34174. + inode = file_inode(file);
  34175. dev = inode->i_sb->s_dev;
  34176. ino = inode->i_ino;
  34177. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  34178. diff -Nur linux-4.1.10.orig/fs/read_write.c linux-4.1.10/fs/read_write.c
  34179. --- linux-4.1.10.orig/fs/read_write.c 2015-10-03 13:49:38.000000000 +0200
  34180. +++ linux-4.1.10/fs/read_write.c 2015-10-22 21:35:53.000000000 +0200
  34181. @@ -494,6 +494,30 @@
  34182. }
  34183. EXPORT_SYMBOL(__vfs_write);
  34184. +vfs_readf_t vfs_readf(struct file *file)
  34185. +{
  34186. + const struct file_operations *fop = file->f_op;
  34187. +
  34188. + if (fop->read)
  34189. + return fop->read;
  34190. + if (fop->read_iter)
  34191. + return new_sync_read;
  34192. + return ERR_PTR(-ENOSYS);
  34193. +}
  34194. +EXPORT_SYMBOL(vfs_readf);
  34195. +
  34196. +vfs_writef_t vfs_writef(struct file *file)
  34197. +{
  34198. + const struct file_operations *fop = file->f_op;
  34199. +
  34200. + if (fop->write)
  34201. + return fop->write;
  34202. + if (fop->write_iter)
  34203. + return new_sync_write;
  34204. + return ERR_PTR(-ENOSYS);
  34205. +}
  34206. +EXPORT_SYMBOL(vfs_writef);
  34207. +
  34208. ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
  34209. {
  34210. mm_segment_t old_fs;
  34211. diff -Nur linux-4.1.10.orig/fs/splice.c linux-4.1.10/fs/splice.c
  34212. --- linux-4.1.10.orig/fs/splice.c 2015-10-03 13:49:38.000000000 +0200
  34213. +++ linux-4.1.10/fs/splice.c 2015-10-22 21:35:53.000000000 +0200
  34214. @@ -1101,8 +1101,8 @@
  34215. /*
  34216. * Attempt to initiate a splice from pipe to file.
  34217. */
  34218. -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  34219. - loff_t *ppos, size_t len, unsigned int flags)
  34220. +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  34221. + loff_t *ppos, size_t len, unsigned int flags)
  34222. {
  34223. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
  34224. loff_t *, size_t, unsigned int);
  34225. @@ -1114,13 +1114,14 @@
  34226. return splice_write(pipe, out, ppos, len, flags);
  34227. }
  34228. +EXPORT_SYMBOL(do_splice_from);
  34229. /*
  34230. * Attempt to initiate a splice from a file to a pipe.
  34231. */
  34232. -static long do_splice_to(struct file *in, loff_t *ppos,
  34233. - struct pipe_inode_info *pipe, size_t len,
  34234. - unsigned int flags)
  34235. +long do_splice_to(struct file *in, loff_t *ppos,
  34236. + struct pipe_inode_info *pipe, size_t len,
  34237. + unsigned int flags)
  34238. {
  34239. ssize_t (*splice_read)(struct file *, loff_t *,
  34240. struct pipe_inode_info *, size_t, unsigned int);
  34241. @@ -1140,6 +1141,7 @@
  34242. return splice_read(in, ppos, pipe, len, flags);
  34243. }
  34244. +EXPORT_SYMBOL(do_splice_to);
  34245. /**
  34246. * splice_direct_to_actor - splices data directly between two non-pipes
  34247. diff -Nur linux-4.1.10.orig/fs/xattr.c linux-4.1.10/fs/xattr.c
  34248. --- linux-4.1.10.orig/fs/xattr.c 2015-10-03 13:49:38.000000000 +0200
  34249. +++ linux-4.1.10/fs/xattr.c 2015-10-22 21:35:53.000000000 +0200
  34250. @@ -207,6 +207,7 @@
  34251. *xattr_value = value;
  34252. return error;
  34253. }
  34254. +EXPORT_SYMBOL(vfs_getxattr_alloc);
  34255. /* Compare an extended attribute value with the given value */
  34256. int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
  34257. diff -Nur linux-4.1.10.orig/include/linux/file.h linux-4.1.10/include/linux/file.h
  34258. --- linux-4.1.10.orig/include/linux/file.h 2015-10-03 13:49:38.000000000 +0200
  34259. +++ linux-4.1.10/include/linux/file.h 2015-10-22 21:35:53.000000000 +0200
  34260. @@ -19,6 +19,7 @@
  34261. struct path;
  34262. extern struct file *alloc_file(struct path *, fmode_t mode,
  34263. const struct file_operations *fop);
  34264. +extern struct file *get_empty_filp(void);
  34265. static inline void fput_light(struct file *file, int fput_needed)
  34266. {
  34267. diff -Nur linux-4.1.10.orig/include/linux/fs.h linux-4.1.10/include/linux/fs.h
  34268. --- linux-4.1.10.orig/include/linux/fs.h 2015-10-03 13:49:38.000000000 +0200
  34269. +++ linux-4.1.10/include/linux/fs.h 2015-10-22 21:35:53.000000000 +0200
  34270. @@ -1649,6 +1649,12 @@
  34271. struct iovec *fast_pointer,
  34272. struct iovec **ret_pointer);
  34273. +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
  34274. +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
  34275. + loff_t *);
  34276. +vfs_readf_t vfs_readf(struct file *file);
  34277. +vfs_writef_t vfs_writef(struct file *file);
  34278. +
  34279. extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
  34280. extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
  34281. extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
  34282. diff -Nur linux-4.1.10.orig/include/linux/mm.h linux-4.1.10/include/linux/mm.h
  34283. --- linux-4.1.10.orig/include/linux/mm.h 2015-10-03 13:49:38.000000000 +0200
  34284. +++ linux-4.1.10/include/linux/mm.h 2015-10-22 21:35:53.000000000 +0200
  34285. @@ -1200,6 +1200,28 @@
  34286. }
  34287. #endif
  34288. +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
  34289. +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
  34290. + int);
  34291. +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
  34292. +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
  34293. +
  34294. +#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
  34295. + __LINE__)
  34296. +#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
  34297. + __LINE__)
  34298. +#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
  34299. +#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
  34300. +
  34301. +#ifndef CONFIG_MMU
  34302. +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
  34303. +extern void vmr_do_fput(struct vm_region *, const char[], int);
  34304. +
  34305. +#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
  34306. + __LINE__)
  34307. +#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
  34308. +#endif /* !CONFIG_MMU */
  34309. +
  34310. extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
  34311. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  34312. void *buf, int len, int write);
  34313. diff -Nur linux-4.1.10.orig/include/linux/mm_types.h linux-4.1.10/include/linux/mm_types.h
  34314. --- linux-4.1.10.orig/include/linux/mm_types.h 2015-10-03 13:49:38.000000000 +0200
  34315. +++ linux-4.1.10/include/linux/mm_types.h 2015-10-22 21:35:53.000000000 +0200
  34316. @@ -232,6 +232,7 @@
  34317. unsigned long vm_top; /* region allocated to here */
  34318. unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
  34319. struct file *vm_file; /* the backing file or NULL */
  34320. + struct file *vm_prfile; /* the virtual backing file or NULL */
  34321. int vm_usage; /* region usage count (access under nommu_region_sem) */
  34322. bool vm_icache_flushed : 1; /* true if the icache has been flushed for
  34323. @@ -296,6 +297,7 @@
  34324. unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
  34325. units, *not* PAGE_CACHE_SIZE */
  34326. struct file * vm_file; /* File we map to (can be NULL). */
  34327. + struct file *vm_prfile; /* shadow of vm_file */
  34328. void * vm_private_data; /* was vm_pte (shared mem) */
  34329. #ifndef CONFIG_MMU
  34330. diff -Nur linux-4.1.10.orig/include/linux/splice.h linux-4.1.10/include/linux/splice.h
  34331. --- linux-4.1.10.orig/include/linux/splice.h 2015-10-03 13:49:38.000000000 +0200
  34332. +++ linux-4.1.10/include/linux/splice.h 2015-10-22 21:35:53.000000000 +0200
  34333. @@ -83,4 +83,10 @@
  34334. extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
  34335. extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
  34336. +
  34337. +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
  34338. + loff_t *ppos, size_t len, unsigned int flags);
  34339. +extern long do_splice_to(struct file *in, loff_t *ppos,
  34340. + struct pipe_inode_info *pipe, size_t len,
  34341. + unsigned int flags);
  34342. #endif
  34343. diff -Nur linux-4.1.10.orig/include/uapi/linux/aufs_type.h linux-4.1.10/include/uapi/linux/aufs_type.h
  34344. --- linux-4.1.10.orig/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
  34345. +++ linux-4.1.10/include/uapi/linux/aufs_type.h 2015-10-22 21:35:53.000000000 +0200
  34346. @@ -0,0 +1,419 @@
  34347. +/*
  34348. + * Copyright (C) 2005-2015 Junjiro R. Okajima
  34349. + *
  34350. + * This program, aufs is free software; you can redistribute it and/or modify
  34351. + * it under the terms of the GNU General Public License as published by
  34352. + * the Free Software Foundation; either version 2 of the License, or
  34353. + * (at your option) any later version.
  34354. + *
  34355. + * This program is distributed in the hope that it will be useful,
  34356. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34357. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34358. + * GNU General Public License for more details.
  34359. + *
  34360. + * You should have received a copy of the GNU General Public License
  34361. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34362. + */
  34363. +
  34364. +#ifndef __AUFS_TYPE_H__
  34365. +#define __AUFS_TYPE_H__
  34366. +
  34367. +#define AUFS_NAME "aufs"
  34368. +
  34369. +#ifdef __KERNEL__
  34370. +/*
  34371. + * define it before including all other headers.
  34372. + * sched.h may use pr_* macros before defining "current", so define the
  34373. + * no-current version first, and re-define later.
  34374. + */
  34375. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  34376. +#include <linux/sched.h>
  34377. +#undef pr_fmt
  34378. +#define pr_fmt(fmt) \
  34379. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  34380. + (int)sizeof(current->comm), current->comm, current->pid
  34381. +#else
  34382. +#include <stdint.h>
  34383. +#include <sys/types.h>
  34384. +#endif /* __KERNEL__ */
  34385. +
  34386. +#include <linux/limits.h>
  34387. +
  34388. +#define AUFS_VERSION "4.1-20151012"
  34389. +
  34390. +/* todo? move this to linux-2.6.19/include/magic.h */
  34391. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  34392. +
  34393. +/* ---------------------------------------------------------------------- */
  34394. +
  34395. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  34396. +typedef int8_t aufs_bindex_t;
  34397. +#define AUFS_BRANCH_MAX 127
  34398. +#else
  34399. +typedef int16_t aufs_bindex_t;
  34400. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  34401. +#define AUFS_BRANCH_MAX 511
  34402. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  34403. +#define AUFS_BRANCH_MAX 1023
  34404. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  34405. +#define AUFS_BRANCH_MAX 32767
  34406. +#endif
  34407. +#endif
  34408. +
  34409. +#ifdef __KERNEL__
  34410. +#ifndef AUFS_BRANCH_MAX
  34411. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  34412. +#endif
  34413. +#endif /* __KERNEL__ */
  34414. +
  34415. +/* ---------------------------------------------------------------------- */
  34416. +
  34417. +#define AUFS_FSTYPE AUFS_NAME
  34418. +
  34419. +#define AUFS_ROOT_INO 2
  34420. +#define AUFS_FIRST_INO 11
  34421. +
  34422. +#define AUFS_WH_PFX ".wh."
  34423. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  34424. +#define AUFS_WH_TMP_LEN 4
  34425. +/* a limit for rmdir/rename a dir and copyup */
  34426. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  34427. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  34428. + - 1 /* dot */\
  34429. + - AUFS_WH_TMP_LEN) /* hex */
  34430. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  34431. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  34432. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  34433. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  34434. +#define AUFS_DIRWH_DEF 3
  34435. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  34436. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  34437. +#define AUFS_RDBLK_DEF 512 /* bytes */
  34438. +#define AUFS_RDHASH_DEF 32
  34439. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  34440. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  34441. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  34442. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  34443. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  34444. +
  34445. +/* pseudo-link maintenace under /proc */
  34446. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  34447. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  34448. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  34449. +
  34450. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  34451. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  34452. +
  34453. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  34454. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  34455. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  34456. +
  34457. +/* doubly whiteouted */
  34458. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  34459. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  34460. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  34461. +
  34462. +/* branch permissions and attributes */
  34463. +#define AUFS_BRPERM_RW "rw"
  34464. +#define AUFS_BRPERM_RO "ro"
  34465. +#define AUFS_BRPERM_RR "rr"
  34466. +#define AUFS_BRATTR_COO_REG "coo_reg"
  34467. +#define AUFS_BRATTR_COO_ALL "coo_all"
  34468. +#define AUFS_BRATTR_FHSM "fhsm"
  34469. +#define AUFS_BRATTR_UNPIN "unpin"
  34470. +#define AUFS_BRATTR_ICEX "icex"
  34471. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  34472. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  34473. +#define AUFS_BRATTR_ICEX_TR "icextr"
  34474. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  34475. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  34476. +#define AUFS_BRRATTR_WH "wh"
  34477. +#define AUFS_BRWATTR_NLWH "nolwh"
  34478. +#define AUFS_BRWATTR_MOO "moo"
  34479. +
  34480. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  34481. +#define AuBrPerm_RO (1 << 1) /* readonly */
  34482. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  34483. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  34484. +
  34485. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  34486. +#define AuBrAttr_COO_ALL (1 << 4)
  34487. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  34488. +
  34489. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  34490. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  34491. + branch. meaningless since
  34492. + linux-3.18-rc1 */
  34493. +
  34494. +/* ignore error in copying XATTR */
  34495. +#define AuBrAttr_ICEX_SEC (1 << 7)
  34496. +#define AuBrAttr_ICEX_SYS (1 << 8)
  34497. +#define AuBrAttr_ICEX_TR (1 << 9)
  34498. +#define AuBrAttr_ICEX_USR (1 << 10)
  34499. +#define AuBrAttr_ICEX_OTH (1 << 11)
  34500. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  34501. + | AuBrAttr_ICEX_SYS \
  34502. + | AuBrAttr_ICEX_TR \
  34503. + | AuBrAttr_ICEX_USR \
  34504. + | AuBrAttr_ICEX_OTH)
  34505. +
  34506. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  34507. +#define AuBrRAttr_Mask AuBrRAttr_WH
  34508. +
  34509. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  34510. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  34511. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  34512. +
  34513. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  34514. +
  34515. +/* #warning test userspace */
  34516. +#ifdef __KERNEL__
  34517. +#ifndef CONFIG_AUFS_FHSM
  34518. +#undef AuBrAttr_FHSM
  34519. +#define AuBrAttr_FHSM 0
  34520. +#endif
  34521. +#ifndef CONFIG_AUFS_XATTR
  34522. +#undef AuBrAttr_ICEX
  34523. +#define AuBrAttr_ICEX 0
  34524. +#undef AuBrAttr_ICEX_SEC
  34525. +#define AuBrAttr_ICEX_SEC 0
  34526. +#undef AuBrAttr_ICEX_SYS
  34527. +#define AuBrAttr_ICEX_SYS 0
  34528. +#undef AuBrAttr_ICEX_TR
  34529. +#define AuBrAttr_ICEX_TR 0
  34530. +#undef AuBrAttr_ICEX_USR
  34531. +#define AuBrAttr_ICEX_USR 0
  34532. +#undef AuBrAttr_ICEX_OTH
  34533. +#define AuBrAttr_ICEX_OTH 0
  34534. +#endif
  34535. +#endif
  34536. +
  34537. +/* the longest combination */
  34538. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  34539. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  34540. + "+" AUFS_BRATTR_COO_REG \
  34541. + "+" AUFS_BRATTR_FHSM \
  34542. + "+" AUFS_BRATTR_UNPIN \
  34543. + "+" AUFS_BRATTR_ICEX_SEC \
  34544. + "+" AUFS_BRATTR_ICEX_SYS \
  34545. + "+" AUFS_BRATTR_ICEX_USR \
  34546. + "+" AUFS_BRATTR_ICEX_OTH \
  34547. + "+" AUFS_BRWATTR_NLWH)
  34548. +
  34549. +typedef struct {
  34550. + char a[AuBrPermStrSz];
  34551. +} au_br_perm_str_t;
  34552. +
  34553. +static inline int au_br_writable(int brperm)
  34554. +{
  34555. + return brperm & AuBrPerm_RW;
  34556. +}
  34557. +
  34558. +static inline int au_br_whable(int brperm)
  34559. +{
  34560. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  34561. +}
  34562. +
  34563. +static inline int au_br_wh_linkable(int brperm)
  34564. +{
  34565. + return !(brperm & AuBrWAttr_NoLinkWH);
  34566. +}
  34567. +
  34568. +static inline int au_br_cmoo(int brperm)
  34569. +{
  34570. + return brperm & AuBrAttr_CMOO_Mask;
  34571. +}
  34572. +
  34573. +static inline int au_br_fhsm(int brperm)
  34574. +{
  34575. + return brperm & AuBrAttr_FHSM;
  34576. +}
  34577. +
  34578. +/* ---------------------------------------------------------------------- */
  34579. +
  34580. +/* ioctl */
  34581. +enum {
  34582. + /* readdir in userspace */
  34583. + AuCtl_RDU,
  34584. + AuCtl_RDU_INO,
  34585. +
  34586. + AuCtl_WBR_FD, /* pathconf wrapper */
  34587. + AuCtl_IBUSY, /* busy inode */
  34588. + AuCtl_MVDOWN, /* move-down */
  34589. + AuCtl_BR, /* info about branches */
  34590. + AuCtl_FHSM_FD /* connection for fhsm */
  34591. +};
  34592. +
  34593. +/* borrowed from linux/include/linux/kernel.h */
  34594. +#ifndef ALIGN
  34595. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  34596. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  34597. +#endif
  34598. +
  34599. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  34600. +#ifndef __aligned
  34601. +#define __aligned(x) __attribute__((aligned(x)))
  34602. +#endif
  34603. +
  34604. +#ifdef __KERNEL__
  34605. +#ifndef __packed
  34606. +#define __packed __attribute__((packed))
  34607. +#endif
  34608. +#endif
  34609. +
  34610. +struct au_rdu_cookie {
  34611. + uint64_t h_pos;
  34612. + int16_t bindex;
  34613. + uint8_t flags;
  34614. + uint8_t pad;
  34615. + uint32_t generation;
  34616. +} __aligned(8);
  34617. +
  34618. +struct au_rdu_ent {
  34619. + uint64_t ino;
  34620. + int16_t bindex;
  34621. + uint8_t type;
  34622. + uint8_t nlen;
  34623. + uint8_t wh;
  34624. + char name[0];
  34625. +} __aligned(8);
  34626. +
  34627. +static inline int au_rdu_len(int nlen)
  34628. +{
  34629. + /* include the terminating NULL */
  34630. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  34631. + sizeof(uint64_t));
  34632. +}
  34633. +
  34634. +union au_rdu_ent_ul {
  34635. + struct au_rdu_ent __user *e;
  34636. + uint64_t ul;
  34637. +};
  34638. +
  34639. +enum {
  34640. + AufsCtlRduV_SZ,
  34641. + AufsCtlRduV_End
  34642. +};
  34643. +
  34644. +struct aufs_rdu {
  34645. + /* input */
  34646. + union {
  34647. + uint64_t sz; /* AuCtl_RDU */
  34648. + uint64_t nent; /* AuCtl_RDU_INO */
  34649. + };
  34650. + union au_rdu_ent_ul ent;
  34651. + uint16_t verify[AufsCtlRduV_End];
  34652. +
  34653. + /* input/output */
  34654. + uint32_t blk;
  34655. +
  34656. + /* output */
  34657. + union au_rdu_ent_ul tail;
  34658. + /* number of entries which were added in a single call */
  34659. + uint64_t rent;
  34660. + uint8_t full;
  34661. + uint8_t shwh;
  34662. +
  34663. + struct au_rdu_cookie cookie;
  34664. +} __aligned(8);
  34665. +
  34666. +/* ---------------------------------------------------------------------- */
  34667. +
  34668. +struct aufs_wbr_fd {
  34669. + uint32_t oflags;
  34670. + int16_t brid;
  34671. +} __aligned(8);
  34672. +
  34673. +/* ---------------------------------------------------------------------- */
  34674. +
  34675. +struct aufs_ibusy {
  34676. + uint64_t ino, h_ino;
  34677. + int16_t bindex;
  34678. +} __aligned(8);
  34679. +
  34680. +/* ---------------------------------------------------------------------- */
  34681. +
  34682. +/* error code for move-down */
  34683. +/* the actual message strings are implemented in aufs-util.git */
  34684. +enum {
  34685. + EAU_MVDOWN_OPAQUE = 1,
  34686. + EAU_MVDOWN_WHITEOUT,
  34687. + EAU_MVDOWN_UPPER,
  34688. + EAU_MVDOWN_BOTTOM,
  34689. + EAU_MVDOWN_NOUPPER,
  34690. + EAU_MVDOWN_NOLOWERBR,
  34691. + EAU_Last
  34692. +};
  34693. +
  34694. +/* flags for move-down */
  34695. +#define AUFS_MVDOWN_DMSG 1
  34696. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  34697. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  34698. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  34699. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  34700. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  34701. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  34702. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  34703. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  34704. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  34705. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  34706. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  34707. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  34708. +
  34709. +/* index for move-down */
  34710. +enum {
  34711. + AUFS_MVDOWN_UPPER,
  34712. + AUFS_MVDOWN_LOWER,
  34713. + AUFS_MVDOWN_NARRAY
  34714. +};
  34715. +
  34716. +/*
  34717. + * additional info of move-down
  34718. + * number of free blocks and inodes.
  34719. + * subset of struct kstatfs, but smaller and always 64bit.
  34720. + */
  34721. +struct aufs_stfs {
  34722. + uint64_t f_blocks;
  34723. + uint64_t f_bavail;
  34724. + uint64_t f_files;
  34725. + uint64_t f_ffree;
  34726. +};
  34727. +
  34728. +struct aufs_stbr {
  34729. + int16_t brid; /* optional input */
  34730. + int16_t bindex; /* output */
  34731. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  34732. +} __aligned(8);
  34733. +
  34734. +struct aufs_mvdown {
  34735. + uint32_t flags; /* input/output */
  34736. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  34737. + int8_t au_errno; /* output */
  34738. +} __aligned(8);
  34739. +
  34740. +/* ---------------------------------------------------------------------- */
  34741. +
  34742. +union aufs_brinfo {
  34743. + /* PATH_MAX may differ between kernel-space and user-space */
  34744. + char _spacer[4096];
  34745. + struct {
  34746. + int16_t id;
  34747. + int perm;
  34748. + char path[0];
  34749. + };
  34750. +} __aligned(8);
  34751. +
  34752. +/* ---------------------------------------------------------------------- */
  34753. +
  34754. +#define AuCtlType 'A'
  34755. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  34756. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  34757. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  34758. + struct aufs_wbr_fd)
  34759. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  34760. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  34761. + struct aufs_mvdown)
  34762. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  34763. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  34764. +
  34765. +#endif /* __AUFS_TYPE_H__ */
  34766. diff -Nur linux-4.1.10.orig/include/uapi/linux/Kbuild linux-4.1.10/include/uapi/linux/Kbuild
  34767. --- linux-4.1.10.orig/include/uapi/linux/Kbuild 2015-10-03 13:49:38.000000000 +0200
  34768. +++ linux-4.1.10/include/uapi/linux/Kbuild 2015-10-22 21:35:53.000000000 +0200
  34769. @@ -59,6 +59,7 @@
  34770. header-y += atm_tcp.h
  34771. header-y += atm_zatm.h
  34772. header-y += audit.h
  34773. +header-y += aufs_type.h
  34774. header-y += auto_fs4.h
  34775. header-y += auto_fs.h
  34776. header-y += auxvec.h
  34777. diff -Nur linux-4.1.10.orig/kernel/fork.c linux-4.1.10/kernel/fork.c
  34778. --- linux-4.1.10.orig/kernel/fork.c 2015-10-03 13:49:38.000000000 +0200
  34779. +++ linux-4.1.10/kernel/fork.c 2015-10-22 21:35:53.000000000 +0200
  34780. @@ -456,7 +456,7 @@
  34781. struct inode *inode = file_inode(file);
  34782. struct address_space *mapping = file->f_mapping;
  34783. - get_file(file);
  34784. + vma_get_file(tmp);
  34785. if (tmp->vm_flags & VM_DENYWRITE)
  34786. atomic_dec(&inode->i_writecount);
  34787. i_mmap_lock_write(mapping);
  34788. diff -Nur linux-4.1.10.orig/MAINTAINERS linux-4.1.10/MAINTAINERS
  34789. --- linux-4.1.10.orig/MAINTAINERS 2015-10-03 13:49:38.000000000 +0200
  34790. +++ linux-4.1.10/MAINTAINERS 2015-10-22 21:35:53.000000000 +0200
  34791. @@ -1880,6 +1880,19 @@
  34792. F: include/uapi/linux/audit.h
  34793. F: kernel/audit*
  34794. +AUFS (advanced multi layered unification filesystem) FILESYSTEM
  34795. +M: "J. R. Okajima" <hooanon05g@gmail.com>
  34796. +L: linux-unionfs@vger.kernel.org
  34797. +L: aufs-users@lists.sourceforge.net (members only)
  34798. +W: http://aufs.sourceforge.net
  34799. +T: git://github.com/sfjro/aufs4-linux.git
  34800. +S: Supported
  34801. +F: Documentation/filesystems/aufs/
  34802. +F: Documentation/ABI/testing/debugfs-aufs
  34803. +F: Documentation/ABI/testing/sysfs-aufs
  34804. +F: fs/aufs/
  34805. +F: include/uapi/linux/aufs_type.h
  34806. +
  34807. AUXILIARY DISPLAY DRIVERS
  34808. M: Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
  34809. W: http://miguelojeda.es/auxdisplay.htm
  34810. diff -Nur linux-4.1.10.orig/mm/filemap.c linux-4.1.10/mm/filemap.c
  34811. --- linux-4.1.10.orig/mm/filemap.c 2015-10-03 13:49:38.000000000 +0200
  34812. +++ linux-4.1.10/mm/filemap.c 2015-10-22 21:35:53.000000000 +0200
  34813. @@ -2062,7 +2062,7 @@
  34814. int ret = VM_FAULT_LOCKED;
  34815. sb_start_pagefault(inode->i_sb);
  34816. - file_update_time(vma->vm_file);
  34817. + vma_file_update_time(vma);
  34818. lock_page(page);
  34819. if (page->mapping != inode->i_mapping) {
  34820. unlock_page(page);
  34821. diff -Nur linux-4.1.10.orig/mm/Makefile linux-4.1.10/mm/Makefile
  34822. --- linux-4.1.10.orig/mm/Makefile 2015-10-03 13:49:38.000000000 +0200
  34823. +++ linux-4.1.10/mm/Makefile 2015-10-22 21:35:53.000000000 +0200
  34824. @@ -21,7 +21,7 @@
  34825. mm_init.o mmu_context.o percpu.o slab_common.o \
  34826. compaction.o vmacache.o \
  34827. interval_tree.o list_lru.o workingset.o \
  34828. - debug.o $(mmu-y)
  34829. + prfile.o debug.o $(mmu-y)
  34830. obj-y += init-mm.o
  34831. diff -Nur linux-4.1.10.orig/mm/memory.c linux-4.1.10/mm/memory.c
  34832. --- linux-4.1.10.orig/mm/memory.c 2015-10-03 13:49:38.000000000 +0200
  34833. +++ linux-4.1.10/mm/memory.c 2015-10-22 21:35:53.000000000 +0200
  34834. @@ -2034,7 +2034,7 @@
  34835. }
  34836. if (!page_mkwrite)
  34837. - file_update_time(vma->vm_file);
  34838. + vma_file_update_time(vma);
  34839. }
  34840. return VM_FAULT_WRITE;
  34841. diff -Nur linux-4.1.10.orig/mm/mmap.c linux-4.1.10/mm/mmap.c
  34842. --- linux-4.1.10.orig/mm/mmap.c 2015-10-03 13:49:38.000000000 +0200
  34843. +++ linux-4.1.10/mm/mmap.c 2015-10-22 21:35:53.000000000 +0200
  34844. @@ -274,7 +274,7 @@
  34845. if (vma->vm_ops && vma->vm_ops->close)
  34846. vma->vm_ops->close(vma);
  34847. if (vma->vm_file)
  34848. - fput(vma->vm_file);
  34849. + vma_fput(vma);
  34850. mpol_put(vma_policy(vma));
  34851. kmem_cache_free(vm_area_cachep, vma);
  34852. return next;
  34853. @@ -886,7 +886,7 @@
  34854. if (remove_next) {
  34855. if (file) {
  34856. uprobe_munmap(next, next->vm_start, next->vm_end);
  34857. - fput(file);
  34858. + vma_fput(vma);
  34859. }
  34860. if (next->anon_vma)
  34861. anon_vma_merge(vma, next);
  34862. @@ -1671,8 +1671,8 @@
  34863. return addr;
  34864. unmap_and_free_vma:
  34865. + vma_fput(vma);
  34866. vma->vm_file = NULL;
  34867. - fput(file);
  34868. /* Undo any partial mapping done by a device driver. */
  34869. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  34870. @@ -2473,7 +2473,7 @@
  34871. goto out_free_mpol;
  34872. if (new->vm_file)
  34873. - get_file(new->vm_file);
  34874. + vma_get_file(new);
  34875. if (new->vm_ops && new->vm_ops->open)
  34876. new->vm_ops->open(new);
  34877. @@ -2492,7 +2492,7 @@
  34878. if (new->vm_ops && new->vm_ops->close)
  34879. new->vm_ops->close(new);
  34880. if (new->vm_file)
  34881. - fput(new->vm_file);
  34882. + vma_fput(new);
  34883. unlink_anon_vmas(new);
  34884. out_free_mpol:
  34885. mpol_put(vma_policy(new));
  34886. @@ -2635,7 +2635,6 @@
  34887. struct vm_area_struct *vma;
  34888. unsigned long populate = 0;
  34889. unsigned long ret = -EINVAL;
  34890. - struct file *file;
  34891. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
  34892. "See Documentation/vm/remap_file_pages.txt.\n",
  34893. @@ -2679,10 +2678,10 @@
  34894. munlock_vma_pages_range(vma, start, start + size);
  34895. }
  34896. - file = get_file(vma->vm_file);
  34897. + vma_get_file(vma);
  34898. ret = do_mmap_pgoff(vma->vm_file, start, size,
  34899. prot, flags, pgoff, &populate);
  34900. - fput(file);
  34901. + vma_fput(vma);
  34902. out:
  34903. up_write(&mm->mmap_sem);
  34904. if (populate)
  34905. @@ -2949,7 +2948,7 @@
  34906. if (anon_vma_clone(new_vma, vma))
  34907. goto out_free_mempol;
  34908. if (new_vma->vm_file)
  34909. - get_file(new_vma->vm_file);
  34910. + vma_get_file(new_vma);
  34911. if (new_vma->vm_ops && new_vma->vm_ops->open)
  34912. new_vma->vm_ops->open(new_vma);
  34913. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  34914. diff -Nur linux-4.1.10.orig/mm/nommu.c linux-4.1.10/mm/nommu.c
  34915. --- linux-4.1.10.orig/mm/nommu.c 2015-10-03 13:49:38.000000000 +0200
  34916. +++ linux-4.1.10/mm/nommu.c 2015-10-22 21:35:53.000000000 +0200
  34917. @@ -693,7 +693,7 @@
  34918. up_write(&nommu_region_sem);
  34919. if (region->vm_file)
  34920. - fput(region->vm_file);
  34921. + vmr_fput(region);
  34922. /* IO memory and memory shared directly out of the pagecache
  34923. * from ramfs/tmpfs mustn't be released here */
  34924. @@ -858,7 +858,7 @@
  34925. if (vma->vm_ops && vma->vm_ops->close)
  34926. vma->vm_ops->close(vma);
  34927. if (vma->vm_file)
  34928. - fput(vma->vm_file);
  34929. + vma_fput(vma);
  34930. put_nommu_region(vma->vm_region);
  34931. kmem_cache_free(vm_area_cachep, vma);
  34932. }
  34933. @@ -1398,7 +1398,7 @@
  34934. goto error_just_free;
  34935. }
  34936. }
  34937. - fput(region->vm_file);
  34938. + vmr_fput(region);
  34939. kmem_cache_free(vm_region_jar, region);
  34940. region = pregion;
  34941. result = start;
  34942. @@ -1474,10 +1474,10 @@
  34943. up_write(&nommu_region_sem);
  34944. error:
  34945. if (region->vm_file)
  34946. - fput(region->vm_file);
  34947. + vmr_fput(region);
  34948. kmem_cache_free(vm_region_jar, region);
  34949. if (vma->vm_file)
  34950. - fput(vma->vm_file);
  34951. + vma_fput(vma);
  34952. kmem_cache_free(vm_area_cachep, vma);
  34953. kleave(" = %d", ret);
  34954. return ret;
  34955. diff -Nur linux-4.1.10.orig/mm/prfile.c linux-4.1.10/mm/prfile.c
  34956. --- linux-4.1.10.orig/mm/prfile.c 1970-01-01 01:00:00.000000000 +0100
  34957. +++ linux-4.1.10/mm/prfile.c 2015-10-22 21:35:53.000000000 +0200
  34958. @@ -0,0 +1,86 @@
  34959. +/*
  34960. + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
  34961. + * in /proc/PID/maps.
  34962. + * Call these functions via macros defined in linux/mm.h.
  34963. + *
  34964. + * See Documentation/filesystems/aufs/design/06mmap.txt
  34965. + *
  34966. + * Copyright (c) 2014 Junjro R. Okajima
  34967. + * Copyright (c) 2014 Ian Campbell
  34968. + */
  34969. +
  34970. +#include <linux/mm.h>
  34971. +#include <linux/file.h>
  34972. +#include <linux/fs.h>
  34973. +
  34974. +/* #define PRFILE_TRACE */
  34975. +static inline void prfile_trace(struct file *f, struct file *pr,
  34976. + const char func[], int line, const char func2[])
  34977. +{
  34978. +#ifdef PRFILE_TRACE
  34979. + if (pr)
  34980. + pr_info("%s:%d: %s, %s\n", func, line, func2,
  34981. + f ? (char *)f->f_path.dentry->d_name.name : "(null)");
  34982. +#endif
  34983. +}
  34984. +
  34985. +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
  34986. + int line)
  34987. +{
  34988. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  34989. +
  34990. + prfile_trace(f, pr, func, line, __func__);
  34991. + file_update_time(f);
  34992. + if (f && pr)
  34993. + file_update_time(pr);
  34994. +}
  34995. +
  34996. +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
  34997. + int line)
  34998. +{
  34999. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  35000. +
  35001. + prfile_trace(f, pr, func, line, __func__);
  35002. + return (f && pr) ? pr : f;
  35003. +}
  35004. +
  35005. +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
  35006. +{
  35007. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  35008. +
  35009. + prfile_trace(f, pr, func, line, __func__);
  35010. + get_file(f);
  35011. + if (f && pr)
  35012. + get_file(pr);
  35013. +}
  35014. +
  35015. +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
  35016. +{
  35017. + struct file *f = vma->vm_file, *pr = vma->vm_prfile;
  35018. +
  35019. + prfile_trace(f, pr, func, line, __func__);
  35020. + fput(f);
  35021. + if (f && pr)
  35022. + fput(pr);
  35023. +}
  35024. +
  35025. +#ifndef CONFIG_MMU
  35026. +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
  35027. + int line)
  35028. +{
  35029. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  35030. +
  35031. + prfile_trace(f, pr, func, line, __func__);
  35032. + return (f && pr) ? pr : f;
  35033. +}
  35034. +
  35035. +void vmr_do_fput(struct vm_region *region, const char func[], int line)
  35036. +{
  35037. + struct file *f = region->vm_file, *pr = region->vm_prfile;
  35038. +
  35039. + prfile_trace(f, pr, func, line, __func__);
  35040. + fput(f);
  35041. + if (f && pr)
  35042. + fput(pr);
  35043. +}
  35044. +#endif /* !CONFIG_MMU */
  35045. diff -Nur linux-4.1.10.orig/security/commoncap.c linux-4.1.10/security/commoncap.c
  35046. --- linux-4.1.10.orig/security/commoncap.c 2015-10-03 13:49:38.000000000 +0200
  35047. +++ linux-4.1.10/security/commoncap.c 2015-10-22 21:35:53.000000000 +0200
  35048. @@ -975,9 +975,11 @@
  35049. }
  35050. return ret;
  35051. }
  35052. +EXPORT_SYMBOL(cap_mmap_addr);
  35053. int cap_mmap_file(struct file *file, unsigned long reqprot,
  35054. unsigned long prot, unsigned long flags)
  35055. {
  35056. return 0;
  35057. }
  35058. +EXPORT_SYMBOL(cap_mmap_file);
  35059. diff -Nur linux-4.1.10.orig/security/device_cgroup.c linux-4.1.10/security/device_cgroup.c
  35060. --- linux-4.1.10.orig/security/device_cgroup.c 2015-10-03 13:49:38.000000000 +0200
  35061. +++ linux-4.1.10/security/device_cgroup.c 2015-10-22 21:35:53.000000000 +0200
  35062. @@ -7,6 +7,7 @@
  35063. #include <linux/device_cgroup.h>
  35064. #include <linux/cgroup.h>
  35065. #include <linux/ctype.h>
  35066. +#include <linux/export.h>
  35067. #include <linux/list.h>
  35068. #include <linux/uaccess.h>
  35069. #include <linux/seq_file.h>
  35070. @@ -849,6 +850,7 @@
  35071. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  35072. access);
  35073. }
  35074. +EXPORT_SYMBOL(__devcgroup_inode_permission);
  35075. int devcgroup_inode_mknod(int mode, dev_t dev)
  35076. {
  35077. diff -Nur linux-4.1.10.orig/security/security.c linux-4.1.10/security/security.c
  35078. --- linux-4.1.10.orig/security/security.c 2015-10-03 13:49:38.000000000 +0200
  35079. +++ linux-4.1.10/security/security.c 2015-10-22 21:35:53.000000000 +0200
  35080. @@ -430,6 +430,7 @@
  35081. return 0;
  35082. return security_ops->path_rmdir(dir, dentry);
  35083. }
  35084. +EXPORT_SYMBOL(security_path_rmdir);
  35085. int security_path_unlink(struct path *dir, struct dentry *dentry)
  35086. {
  35087. @@ -446,6 +447,7 @@
  35088. return 0;
  35089. return security_ops->path_symlink(dir, dentry, old_name);
  35090. }
  35091. +EXPORT_SYMBOL(security_path_symlink);
  35092. int security_path_link(struct dentry *old_dentry, struct path *new_dir,
  35093. struct dentry *new_dentry)
  35094. @@ -454,6 +456,7 @@
  35095. return 0;
  35096. return security_ops->path_link(old_dentry, new_dir, new_dentry);
  35097. }
  35098. +EXPORT_SYMBOL(security_path_link);
  35099. int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
  35100. struct path *new_dir, struct dentry *new_dentry,
  35101. @@ -481,6 +484,7 @@
  35102. return 0;
  35103. return security_ops->path_truncate(path);
  35104. }
  35105. +EXPORT_SYMBOL(security_path_truncate);
  35106. int security_path_chmod(struct path *path, umode_t mode)
  35107. {
  35108. @@ -488,6 +492,7 @@
  35109. return 0;
  35110. return security_ops->path_chmod(path, mode);
  35111. }
  35112. +EXPORT_SYMBOL(security_path_chmod);
  35113. int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
  35114. {
  35115. @@ -495,6 +500,7 @@
  35116. return 0;
  35117. return security_ops->path_chown(path, uid, gid);
  35118. }
  35119. +EXPORT_SYMBOL(security_path_chown);
  35120. int security_path_chroot(struct path *path)
  35121. {
  35122. @@ -580,6 +586,7 @@
  35123. return 0;
  35124. return security_ops->inode_readlink(dentry);
  35125. }
  35126. +EXPORT_SYMBOL(security_inode_readlink);
  35127. int security_inode_follow_link(struct dentry *dentry, struct nameidata *nd)
  35128. {
  35129. @@ -594,6 +601,7 @@
  35130. return 0;
  35131. return security_ops->inode_permission(inode, mask);
  35132. }
  35133. +EXPORT_SYMBOL(security_inode_permission);
  35134. int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
  35135. {
  35136. @@ -716,6 +724,7 @@
  35137. return fsnotify_perm(file, mask);
  35138. }
  35139. +EXPORT_SYMBOL(security_file_permission);
  35140. int security_file_alloc(struct file *file)
  35141. {
  35142. @@ -775,6 +784,7 @@
  35143. return ret;
  35144. return ima_file_mmap(file, prot);
  35145. }
  35146. +EXPORT_SYMBOL(security_mmap_file);
  35147. int security_mmap_addr(unsigned long addr)
  35148. {