xattr.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * xattr.c
  5. *
  6. * Copyright (C) 2004, 2008 Oracle. All rights reserved.
  7. *
  8. * CREDITS:
  9. * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
  10. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public
  14. * License version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #include <linux/capability.h>
  22. #include <linux/fs.h>
  23. #include <linux/types.h>
  24. #include <linux/slab.h>
  25. #include <linux/highmem.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/uio.h>
  28. #include <linux/sched.h>
  29. #include <linux/splice.h>
  30. #include <linux/mount.h>
  31. #include <linux/writeback.h>
  32. #include <linux/falloc.h>
  33. #include <linux/sort.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/string.h>
  37. #include <linux/security.h>
  38. #define MLOG_MASK_PREFIX ML_XATTR
  39. #include <cluster/masklog.h>
  40. #include "ocfs2.h"
  41. #include "alloc.h"
  42. #include "blockcheck.h"
  43. #include "dlmglue.h"
  44. #include "file.h"
  45. #include "symlink.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "journal.h"
  49. #include "ocfs2_fs.h"
  50. #include "suballoc.h"
  51. #include "uptodate.h"
  52. #include "buffer_head_io.h"
  53. #include "super.h"
  54. #include "xattr.h"
  55. #include "refcounttree.h"
  56. struct ocfs2_xattr_def_value_root {
  57. struct ocfs2_xattr_value_root xv;
  58. struct ocfs2_extent_rec er;
  59. };
  60. struct ocfs2_xattr_bucket {
  61. /* The inode these xattrs are associated with */
  62. struct inode *bu_inode;
  63. /* The actual buffers that make up the bucket */
  64. struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  65. /* How many blocks make up one bucket for this filesystem */
  66. int bu_blocks;
  67. };
  68. struct ocfs2_xattr_set_ctxt {
  69. handle_t *handle;
  70. struct ocfs2_alloc_context *meta_ac;
  71. struct ocfs2_alloc_context *data_ac;
  72. struct ocfs2_cached_dealloc_ctxt dealloc;
  73. };
  74. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  75. #define OCFS2_XATTR_INLINE_SIZE 80
  76. #define OCFS2_XATTR_HEADER_GAP 4
  77. #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
  78. - sizeof(struct ocfs2_xattr_header) \
  79. - OCFS2_XATTR_HEADER_GAP)
  80. #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
  81. - sizeof(struct ocfs2_xattr_block) \
  82. - sizeof(struct ocfs2_xattr_header) \
  83. - OCFS2_XATTR_HEADER_GAP)
  84. static struct ocfs2_xattr_def_value_root def_xv = {
  85. .xv.xr_list.l_count = cpu_to_le16(1),
  86. };
  87. struct xattr_handler *ocfs2_xattr_handlers[] = {
  88. &ocfs2_xattr_user_handler,
  89. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  90. &ocfs2_xattr_acl_access_handler,
  91. &ocfs2_xattr_acl_default_handler,
  92. #endif
  93. &ocfs2_xattr_trusted_handler,
  94. &ocfs2_xattr_security_handler,
  95. NULL
  96. };
  97. static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
  98. [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
  99. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  100. [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
  101. = &ocfs2_xattr_acl_access_handler,
  102. [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
  103. = &ocfs2_xattr_acl_default_handler,
  104. #endif
  105. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  106. [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
  107. };
  108. struct ocfs2_xattr_info {
  109. int name_index;
  110. const char *name;
  111. const void *value;
  112. size_t value_len;
  113. };
  114. struct ocfs2_xattr_search {
  115. struct buffer_head *inode_bh;
  116. /*
  117. * xattr_bh point to the block buffer head which has extended attribute
  118. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  119. */
  120. struct buffer_head *xattr_bh;
  121. struct ocfs2_xattr_header *header;
  122. struct ocfs2_xattr_bucket *bucket;
  123. void *base;
  124. void *end;
  125. struct ocfs2_xattr_entry *here;
  126. int not_found;
  127. };
  128. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  129. struct ocfs2_xattr_header *xh,
  130. int index,
  131. int *block_off,
  132. int *new_offset);
  133. static int ocfs2_xattr_block_find(struct inode *inode,
  134. int name_index,
  135. const char *name,
  136. struct ocfs2_xattr_search *xs);
  137. static int ocfs2_xattr_index_block_find(struct inode *inode,
  138. struct buffer_head *root_bh,
  139. int name_index,
  140. const char *name,
  141. struct ocfs2_xattr_search *xs);
  142. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  143. struct ocfs2_xattr_tree_root *xt,
  144. char *buffer,
  145. size_t buffer_size);
  146. static int ocfs2_xattr_create_index_block(struct inode *inode,
  147. struct ocfs2_xattr_search *xs,
  148. struct ocfs2_xattr_set_ctxt *ctxt);
  149. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  150. struct ocfs2_xattr_info *xi,
  151. struct ocfs2_xattr_search *xs,
  152. struct ocfs2_xattr_set_ctxt *ctxt);
  153. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  154. struct buffer_head *xb_bh);
  155. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  156. u64 src_blk, u64 last_blk, u64 to_blk,
  157. unsigned int start_bucket,
  158. u32 *first_hash);
  159. static int ocfs2_prepare_refcount_xattr(struct inode *inode,
  160. struct ocfs2_dinode *di,
  161. struct ocfs2_xattr_info *xi,
  162. struct ocfs2_xattr_search *xis,
  163. struct ocfs2_xattr_search *xbs,
  164. struct ocfs2_refcount_tree **ref_tree,
  165. int *meta_need,
  166. int *credits);
  167. static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
  168. {
  169. return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
  170. }
  171. static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
  172. {
  173. return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
  174. }
  175. static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
  176. {
  177. u16 len = sb->s_blocksize -
  178. offsetof(struct ocfs2_xattr_header, xh_entries);
  179. return len / sizeof(struct ocfs2_xattr_entry);
  180. }
  181. #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
  182. #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
  183. #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
  184. static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
  185. {
  186. struct ocfs2_xattr_bucket *bucket;
  187. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  188. BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
  189. bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
  190. if (bucket) {
  191. bucket->bu_inode = inode;
  192. bucket->bu_blocks = blks;
  193. }
  194. return bucket;
  195. }
  196. static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
  197. {
  198. int i;
  199. for (i = 0; i < bucket->bu_blocks; i++) {
  200. brelse(bucket->bu_bhs[i]);
  201. bucket->bu_bhs[i] = NULL;
  202. }
  203. }
  204. static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
  205. {
  206. if (bucket) {
  207. ocfs2_xattr_bucket_relse(bucket);
  208. bucket->bu_inode = NULL;
  209. kfree(bucket);
  210. }
  211. }
  212. /*
  213. * A bucket that has never been written to disk doesn't need to be
  214. * read. We just need the buffer_heads. Don't call this for
  215. * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
  216. * them fully.
  217. */
  218. static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
  219. u64 xb_blkno)
  220. {
  221. int i, rc = 0;
  222. for (i = 0; i < bucket->bu_blocks; i++) {
  223. bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
  224. xb_blkno + i);
  225. if (!bucket->bu_bhs[i]) {
  226. rc = -EIO;
  227. mlog_errno(rc);
  228. break;
  229. }
  230. if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
  231. bucket->bu_bhs[i]))
  232. ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
  233. bucket->bu_bhs[i]);
  234. }
  235. if (rc)
  236. ocfs2_xattr_bucket_relse(bucket);
  237. return rc;
  238. }
  239. /* Read the xattr bucket at xb_blkno */
  240. static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
  241. u64 xb_blkno)
  242. {
  243. int rc;
  244. rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
  245. bucket->bu_blocks, bucket->bu_bhs, 0,
  246. NULL);
  247. if (!rc) {
  248. spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  249. rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
  250. bucket->bu_bhs,
  251. bucket->bu_blocks,
  252. &bucket_xh(bucket)->xh_check);
  253. spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  254. if (rc)
  255. mlog_errno(rc);
  256. }
  257. if (rc)
  258. ocfs2_xattr_bucket_relse(bucket);
  259. return rc;
  260. }
  261. static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
  262. struct ocfs2_xattr_bucket *bucket,
  263. int type)
  264. {
  265. int i, rc = 0;
  266. for (i = 0; i < bucket->bu_blocks; i++) {
  267. rc = ocfs2_journal_access(handle,
  268. INODE_CACHE(bucket->bu_inode),
  269. bucket->bu_bhs[i], type);
  270. if (rc) {
  271. mlog_errno(rc);
  272. break;
  273. }
  274. }
  275. return rc;
  276. }
  277. static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
  278. struct ocfs2_xattr_bucket *bucket)
  279. {
  280. int i;
  281. spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  282. ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
  283. bucket->bu_bhs, bucket->bu_blocks,
  284. &bucket_xh(bucket)->xh_check);
  285. spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  286. for (i = 0; i < bucket->bu_blocks; i++)
  287. ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
  288. }
  289. static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
  290. struct ocfs2_xattr_bucket *src)
  291. {
  292. int i;
  293. int blocksize = src->bu_inode->i_sb->s_blocksize;
  294. BUG_ON(dest->bu_blocks != src->bu_blocks);
  295. BUG_ON(dest->bu_inode != src->bu_inode);
  296. for (i = 0; i < src->bu_blocks; i++) {
  297. memcpy(bucket_block(dest, i), bucket_block(src, i),
  298. blocksize);
  299. }
  300. }
  301. static int ocfs2_validate_xattr_block(struct super_block *sb,
  302. struct buffer_head *bh)
  303. {
  304. int rc;
  305. struct ocfs2_xattr_block *xb =
  306. (struct ocfs2_xattr_block *)bh->b_data;
  307. mlog(0, "Validating xattr block %llu\n",
  308. (unsigned long long)bh->b_blocknr);
  309. BUG_ON(!buffer_uptodate(bh));
  310. /*
  311. * If the ecc fails, we return the error but otherwise
  312. * leave the filesystem running. We know any error is
  313. * local to this block.
  314. */
  315. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
  316. if (rc)
  317. return rc;
  318. /*
  319. * Errors after here are fatal
  320. */
  321. if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
  322. ocfs2_error(sb,
  323. "Extended attribute block #%llu has bad "
  324. "signature %.*s",
  325. (unsigned long long)bh->b_blocknr, 7,
  326. xb->xb_signature);
  327. return -EINVAL;
  328. }
  329. if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
  330. ocfs2_error(sb,
  331. "Extended attribute block #%llu has an "
  332. "invalid xb_blkno of %llu",
  333. (unsigned long long)bh->b_blocknr,
  334. (unsigned long long)le64_to_cpu(xb->xb_blkno));
  335. return -EINVAL;
  336. }
  337. if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  338. ocfs2_error(sb,
  339. "Extended attribute block #%llu has an invalid "
  340. "xb_fs_generation of #%u",
  341. (unsigned long long)bh->b_blocknr,
  342. le32_to_cpu(xb->xb_fs_generation));
  343. return -EINVAL;
  344. }
  345. return 0;
  346. }
  347. static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
  348. struct buffer_head **bh)
  349. {
  350. int rc;
  351. struct buffer_head *tmp = *bh;
  352. rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
  353. ocfs2_validate_xattr_block);
  354. /* If ocfs2_read_block() got us a new bh, pass it up. */
  355. if (!rc && !*bh)
  356. *bh = tmp;
  357. return rc;
  358. }
  359. static inline const char *ocfs2_xattr_prefix(int name_index)
  360. {
  361. struct xattr_handler *handler = NULL;
  362. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  363. handler = ocfs2_xattr_handler_map[name_index];
  364. return handler ? handler->prefix : NULL;
  365. }
  366. static u32 ocfs2_xattr_name_hash(struct inode *inode,
  367. const char *name,
  368. int name_len)
  369. {
  370. /* Get hash value of uuid from super block */
  371. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  372. int i;
  373. /* hash extended attribute name */
  374. for (i = 0; i < name_len; i++) {
  375. hash = (hash << OCFS2_HASH_SHIFT) ^
  376. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  377. *name++;
  378. }
  379. return hash;
  380. }
  381. /*
  382. * ocfs2_xattr_hash_entry()
  383. *
  384. * Compute the hash of an extended attribute.
  385. */
  386. static void ocfs2_xattr_hash_entry(struct inode *inode,
  387. struct ocfs2_xattr_header *header,
  388. struct ocfs2_xattr_entry *entry)
  389. {
  390. u32 hash = 0;
  391. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  392. hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
  393. entry->xe_name_hash = cpu_to_le32(hash);
  394. return;
  395. }
  396. static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
  397. {
  398. int size = 0;
  399. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  400. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
  401. else
  402. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  403. size += sizeof(struct ocfs2_xattr_entry);
  404. return size;
  405. }
  406. int ocfs2_calc_security_init(struct inode *dir,
  407. struct ocfs2_security_xattr_info *si,
  408. int *want_clusters,
  409. int *xattr_credits,
  410. struct ocfs2_alloc_context **xattr_ac)
  411. {
  412. int ret = 0;
  413. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  414. int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
  415. si->value_len);
  416. /*
  417. * The max space of security xattr taken inline is
  418. * 256(name) + 80(value) + 16(entry) = 352 bytes,
  419. * So reserve one metadata block for it is ok.
  420. */
  421. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
  422. s_size > OCFS2_XATTR_FREE_IN_IBODY) {
  423. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
  424. if (ret) {
  425. mlog_errno(ret);
  426. return ret;
  427. }
  428. *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  429. }
  430. /* reserve clusters for xattr value which will be set in B tree*/
  431. if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
  432. int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
  433. si->value_len);
  434. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  435. new_clusters);
  436. *want_clusters += new_clusters;
  437. }
  438. return ret;
  439. }
  440. int ocfs2_calc_xattr_init(struct inode *dir,
  441. struct buffer_head *dir_bh,
  442. int mode,
  443. struct ocfs2_security_xattr_info *si,
  444. int *want_clusters,
  445. int *xattr_credits,
  446. int *want_meta)
  447. {
  448. int ret = 0;
  449. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  450. int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
  451. if (si->enable)
  452. s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
  453. si->value_len);
  454. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  455. acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
  456. OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
  457. "", NULL, 0);
  458. if (acl_len > 0) {
  459. a_size = ocfs2_xattr_entry_real_size(0, acl_len);
  460. if (S_ISDIR(mode))
  461. a_size <<= 1;
  462. } else if (acl_len != 0 && acl_len != -ENODATA) {
  463. mlog_errno(ret);
  464. return ret;
  465. }
  466. }
  467. if (!(s_size + a_size))
  468. return ret;
  469. /*
  470. * The max space of security xattr taken inline is
  471. * 256(name) + 80(value) + 16(entry) = 352 bytes,
  472. * The max space of acl xattr taken inline is
  473. * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
  474. * when blocksize = 512, may reserve one more cluser for
  475. * xattr bucket, otherwise reserve one metadata block
  476. * for them is ok.
  477. * If this is a new directory with inline data,
  478. * we choose to reserve the entire inline area for
  479. * directory contents and force an external xattr block.
  480. */
  481. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
  482. (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
  483. (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
  484. *want_meta = *want_meta + 1;
  485. *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  486. }
  487. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
  488. (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
  489. *want_clusters += 1;
  490. *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
  491. }
  492. /*
  493. * reserve credits and clusters for xattrs which has large value
  494. * and have to be set outside
  495. */
  496. if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
  497. new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
  498. si->value_len);
  499. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  500. new_clusters);
  501. *want_clusters += new_clusters;
  502. }
  503. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
  504. acl_len > OCFS2_XATTR_INLINE_SIZE) {
  505. /* for directory, it has DEFAULT and ACCESS two types of acls */
  506. new_clusters = (S_ISDIR(mode) ? 2 : 1) *
  507. ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
  508. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  509. new_clusters);
  510. *want_clusters += new_clusters;
  511. }
  512. return ret;
  513. }
  514. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  515. u32 clusters_to_add,
  516. struct ocfs2_xattr_value_buf *vb,
  517. struct ocfs2_xattr_set_ctxt *ctxt)
  518. {
  519. int status = 0;
  520. handle_t *handle = ctxt->handle;
  521. enum ocfs2_alloc_restarted why;
  522. u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
  523. struct ocfs2_extent_tree et;
  524. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  525. ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
  526. status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  527. OCFS2_JOURNAL_ACCESS_WRITE);
  528. if (status < 0) {
  529. mlog_errno(status);
  530. goto leave;
  531. }
  532. prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
  533. status = ocfs2_add_clusters_in_btree(handle,
  534. &et,
  535. &logical_start,
  536. clusters_to_add,
  537. 0,
  538. ctxt->data_ac,
  539. ctxt->meta_ac,
  540. &why);
  541. if (status < 0) {
  542. mlog_errno(status);
  543. goto leave;
  544. }
  545. status = ocfs2_journal_dirty(handle, vb->vb_bh);
  546. if (status < 0) {
  547. mlog_errno(status);
  548. goto leave;
  549. }
  550. clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
  551. /*
  552. * We should have already allocated enough space before the transaction,
  553. * so no need to restart.
  554. */
  555. BUG_ON(why != RESTART_NONE || clusters_to_add);
  556. leave:
  557. return status;
  558. }
  559. static int __ocfs2_remove_xattr_range(struct inode *inode,
  560. struct ocfs2_xattr_value_buf *vb,
  561. u32 cpos, u32 phys_cpos, u32 len,
  562. unsigned int ext_flags,
  563. struct ocfs2_xattr_set_ctxt *ctxt)
  564. {
  565. int ret;
  566. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  567. handle_t *handle = ctxt->handle;
  568. struct ocfs2_extent_tree et;
  569. ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
  570. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  571. OCFS2_JOURNAL_ACCESS_WRITE);
  572. if (ret) {
  573. mlog_errno(ret);
  574. goto out;
  575. }
  576. ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
  577. &ctxt->dealloc);
  578. if (ret) {
  579. mlog_errno(ret);
  580. goto out;
  581. }
  582. le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
  583. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  584. if (ret) {
  585. mlog_errno(ret);
  586. goto out;
  587. }
  588. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  589. ret = ocfs2_decrease_refcount(inode, handle,
  590. ocfs2_blocks_to_clusters(inode->i_sb,
  591. phys_blkno),
  592. len, ctxt->meta_ac, &ctxt->dealloc, 1);
  593. else
  594. ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
  595. phys_blkno, len);
  596. if (ret)
  597. mlog_errno(ret);
  598. out:
  599. return ret;
  600. }
  601. static int ocfs2_xattr_shrink_size(struct inode *inode,
  602. u32 old_clusters,
  603. u32 new_clusters,
  604. struct ocfs2_xattr_value_buf *vb,
  605. struct ocfs2_xattr_set_ctxt *ctxt)
  606. {
  607. int ret = 0;
  608. unsigned int ext_flags;
  609. u32 trunc_len, cpos, phys_cpos, alloc_size;
  610. u64 block;
  611. if (old_clusters <= new_clusters)
  612. return 0;
  613. cpos = new_clusters;
  614. trunc_len = old_clusters - new_clusters;
  615. while (trunc_len) {
  616. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  617. &alloc_size,
  618. &vb->vb_xv->xr_list, &ext_flags);
  619. if (ret) {
  620. mlog_errno(ret);
  621. goto out;
  622. }
  623. if (alloc_size > trunc_len)
  624. alloc_size = trunc_len;
  625. ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
  626. phys_cpos, alloc_size,
  627. ext_flags, ctxt);
  628. if (ret) {
  629. mlog_errno(ret);
  630. goto out;
  631. }
  632. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  633. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
  634. block, alloc_size);
  635. cpos += alloc_size;
  636. trunc_len -= alloc_size;
  637. }
  638. out:
  639. return ret;
  640. }
  641. static int ocfs2_xattr_value_truncate(struct inode *inode,
  642. struct ocfs2_xattr_value_buf *vb,
  643. int len,
  644. struct ocfs2_xattr_set_ctxt *ctxt)
  645. {
  646. int ret;
  647. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  648. u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
  649. if (new_clusters == old_clusters)
  650. return 0;
  651. if (new_clusters > old_clusters)
  652. ret = ocfs2_xattr_extend_allocation(inode,
  653. new_clusters - old_clusters,
  654. vb, ctxt);
  655. else
  656. ret = ocfs2_xattr_shrink_size(inode,
  657. old_clusters, new_clusters,
  658. vb, ctxt);
  659. return ret;
  660. }
  661. static int ocfs2_xattr_list_entry(char *buffer, size_t size,
  662. size_t *result, const char *prefix,
  663. const char *name, int name_len)
  664. {
  665. char *p = buffer + *result;
  666. int prefix_len = strlen(prefix);
  667. int total_len = prefix_len + name_len + 1;
  668. *result += total_len;
  669. /* we are just looking for how big our buffer needs to be */
  670. if (!size)
  671. return 0;
  672. if (*result > size)
  673. return -ERANGE;
  674. memcpy(p, prefix, prefix_len);
  675. memcpy(p + prefix_len, name, name_len);
  676. p[prefix_len + name_len] = '\0';
  677. return 0;
  678. }
  679. static int ocfs2_xattr_list_entries(struct inode *inode,
  680. struct ocfs2_xattr_header *header,
  681. char *buffer, size_t buffer_size)
  682. {
  683. size_t result = 0;
  684. int i, type, ret;
  685. const char *prefix, *name;
  686. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  687. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  688. type = ocfs2_xattr_get_type(entry);
  689. prefix = ocfs2_xattr_prefix(type);
  690. if (prefix) {
  691. name = (const char *)header +
  692. le16_to_cpu(entry->xe_name_offset);
  693. ret = ocfs2_xattr_list_entry(buffer, buffer_size,
  694. &result, prefix, name,
  695. entry->xe_name_len);
  696. if (ret)
  697. return ret;
  698. }
  699. }
  700. return result;
  701. }
  702. static int ocfs2_xattr_ibody_list(struct inode *inode,
  703. struct ocfs2_dinode *di,
  704. char *buffer,
  705. size_t buffer_size)
  706. {
  707. struct ocfs2_xattr_header *header = NULL;
  708. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  709. int ret = 0;
  710. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  711. return ret;
  712. header = (struct ocfs2_xattr_header *)
  713. ((void *)di + inode->i_sb->s_blocksize -
  714. le16_to_cpu(di->i_xattr_inline_size));
  715. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  716. return ret;
  717. }
  718. static int ocfs2_xattr_block_list(struct inode *inode,
  719. struct ocfs2_dinode *di,
  720. char *buffer,
  721. size_t buffer_size)
  722. {
  723. struct buffer_head *blk_bh = NULL;
  724. struct ocfs2_xattr_block *xb;
  725. int ret = 0;
  726. if (!di->i_xattr_loc)
  727. return ret;
  728. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  729. &blk_bh);
  730. if (ret < 0) {
  731. mlog_errno(ret);
  732. return ret;
  733. }
  734. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  735. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  736. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  737. ret = ocfs2_xattr_list_entries(inode, header,
  738. buffer, buffer_size);
  739. } else {
  740. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  741. ret = ocfs2_xattr_tree_list_index_block(inode, xt,
  742. buffer, buffer_size);
  743. }
  744. brelse(blk_bh);
  745. return ret;
  746. }
  747. ssize_t ocfs2_listxattr(struct dentry *dentry,
  748. char *buffer,
  749. size_t size)
  750. {
  751. int ret = 0, i_ret = 0, b_ret = 0;
  752. struct buffer_head *di_bh = NULL;
  753. struct ocfs2_dinode *di = NULL;
  754. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  755. if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
  756. return -EOPNOTSUPP;
  757. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  758. return ret;
  759. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  760. if (ret < 0) {
  761. mlog_errno(ret);
  762. return ret;
  763. }
  764. di = (struct ocfs2_dinode *)di_bh->b_data;
  765. down_read(&oi->ip_xattr_sem);
  766. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  767. if (i_ret < 0)
  768. b_ret = 0;
  769. else {
  770. if (buffer) {
  771. buffer += i_ret;
  772. size -= i_ret;
  773. }
  774. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  775. buffer, size);
  776. if (b_ret < 0)
  777. i_ret = 0;
  778. }
  779. up_read(&oi->ip_xattr_sem);
  780. ocfs2_inode_unlock(dentry->d_inode, 0);
  781. brelse(di_bh);
  782. return i_ret + b_ret;
  783. }
  784. static int ocfs2_xattr_find_entry(int name_index,
  785. const char *name,
  786. struct ocfs2_xattr_search *xs)
  787. {
  788. struct ocfs2_xattr_entry *entry;
  789. size_t name_len;
  790. int i, cmp = 1;
  791. if (name == NULL)
  792. return -EINVAL;
  793. name_len = strlen(name);
  794. entry = xs->here;
  795. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  796. cmp = name_index - ocfs2_xattr_get_type(entry);
  797. if (!cmp)
  798. cmp = name_len - entry->xe_name_len;
  799. if (!cmp)
  800. cmp = memcmp(name, (xs->base +
  801. le16_to_cpu(entry->xe_name_offset)),
  802. name_len);
  803. if (cmp == 0)
  804. break;
  805. entry += 1;
  806. }
  807. xs->here = entry;
  808. return cmp ? -ENODATA : 0;
  809. }
  810. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  811. struct ocfs2_xattr_value_root *xv,
  812. void *buffer,
  813. size_t len)
  814. {
  815. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  816. u64 blkno;
  817. int i, ret = 0;
  818. size_t cplen, blocksize;
  819. struct buffer_head *bh = NULL;
  820. struct ocfs2_extent_list *el;
  821. el = &xv->xr_list;
  822. clusters = le32_to_cpu(xv->xr_clusters);
  823. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  824. blocksize = inode->i_sb->s_blocksize;
  825. cpos = 0;
  826. while (cpos < clusters) {
  827. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  828. &num_clusters, el, NULL);
  829. if (ret) {
  830. mlog_errno(ret);
  831. goto out;
  832. }
  833. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  834. /* Copy ocfs2_xattr_value */
  835. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  836. ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
  837. &bh, NULL);
  838. if (ret) {
  839. mlog_errno(ret);
  840. goto out;
  841. }
  842. cplen = len >= blocksize ? blocksize : len;
  843. memcpy(buffer, bh->b_data, cplen);
  844. len -= cplen;
  845. buffer += cplen;
  846. brelse(bh);
  847. bh = NULL;
  848. if (len == 0)
  849. break;
  850. }
  851. cpos += num_clusters;
  852. }
  853. out:
  854. return ret;
  855. }
  856. static int ocfs2_xattr_ibody_get(struct inode *inode,
  857. int name_index,
  858. const char *name,
  859. void *buffer,
  860. size_t buffer_size,
  861. struct ocfs2_xattr_search *xs)
  862. {
  863. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  864. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  865. struct ocfs2_xattr_value_root *xv;
  866. size_t size;
  867. int ret = 0;
  868. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  869. return -ENODATA;
  870. xs->end = (void *)di + inode->i_sb->s_blocksize;
  871. xs->header = (struct ocfs2_xattr_header *)
  872. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  873. xs->base = (void *)xs->header;
  874. xs->here = xs->header->xh_entries;
  875. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  876. if (ret)
  877. return ret;
  878. size = le64_to_cpu(xs->here->xe_value_size);
  879. if (buffer) {
  880. if (size > buffer_size)
  881. return -ERANGE;
  882. if (ocfs2_xattr_is_local(xs->here)) {
  883. memcpy(buffer, (void *)xs->base +
  884. le16_to_cpu(xs->here->xe_name_offset) +
  885. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  886. } else {
  887. xv = (struct ocfs2_xattr_value_root *)
  888. (xs->base + le16_to_cpu(
  889. xs->here->xe_name_offset) +
  890. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  891. ret = ocfs2_xattr_get_value_outside(inode, xv,
  892. buffer, size);
  893. if (ret < 0) {
  894. mlog_errno(ret);
  895. return ret;
  896. }
  897. }
  898. }
  899. return size;
  900. }
  901. static int ocfs2_xattr_block_get(struct inode *inode,
  902. int name_index,
  903. const char *name,
  904. void *buffer,
  905. size_t buffer_size,
  906. struct ocfs2_xattr_search *xs)
  907. {
  908. struct ocfs2_xattr_block *xb;
  909. struct ocfs2_xattr_value_root *xv;
  910. size_t size;
  911. int ret = -ENODATA, name_offset, name_len, i;
  912. int uninitialized_var(block_off);
  913. xs->bucket = ocfs2_xattr_bucket_new(inode);
  914. if (!xs->bucket) {
  915. ret = -ENOMEM;
  916. mlog_errno(ret);
  917. goto cleanup;
  918. }
  919. ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
  920. if (ret) {
  921. mlog_errno(ret);
  922. goto cleanup;
  923. }
  924. if (xs->not_found) {
  925. ret = -ENODATA;
  926. goto cleanup;
  927. }
  928. xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  929. size = le64_to_cpu(xs->here->xe_value_size);
  930. if (buffer) {
  931. ret = -ERANGE;
  932. if (size > buffer_size)
  933. goto cleanup;
  934. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  935. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  936. i = xs->here - xs->header->xh_entries;
  937. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  938. ret = ocfs2_xattr_bucket_get_name_value(inode,
  939. bucket_xh(xs->bucket),
  940. i,
  941. &block_off,
  942. &name_offset);
  943. xs->base = bucket_block(xs->bucket, block_off);
  944. }
  945. if (ocfs2_xattr_is_local(xs->here)) {
  946. memcpy(buffer, (void *)xs->base +
  947. name_offset + name_len, size);
  948. } else {
  949. xv = (struct ocfs2_xattr_value_root *)
  950. (xs->base + name_offset + name_len);
  951. ret = ocfs2_xattr_get_value_outside(inode, xv,
  952. buffer, size);
  953. if (ret < 0) {
  954. mlog_errno(ret);
  955. goto cleanup;
  956. }
  957. }
  958. }
  959. ret = size;
  960. cleanup:
  961. ocfs2_xattr_bucket_free(xs->bucket);
  962. brelse(xs->xattr_bh);
  963. xs->xattr_bh = NULL;
  964. return ret;
  965. }
  966. int ocfs2_xattr_get_nolock(struct inode *inode,
  967. struct buffer_head *di_bh,
  968. int name_index,
  969. const char *name,
  970. void *buffer,
  971. size_t buffer_size)
  972. {
  973. int ret;
  974. struct ocfs2_dinode *di = NULL;
  975. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  976. struct ocfs2_xattr_search xis = {
  977. .not_found = -ENODATA,
  978. };
  979. struct ocfs2_xattr_search xbs = {
  980. .not_found = -ENODATA,
  981. };
  982. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  983. return -EOPNOTSUPP;
  984. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  985. ret = -ENODATA;
  986. xis.inode_bh = xbs.inode_bh = di_bh;
  987. di = (struct ocfs2_dinode *)di_bh->b_data;
  988. down_read(&oi->ip_xattr_sem);
  989. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  990. buffer_size, &xis);
  991. if (ret == -ENODATA && di->i_xattr_loc)
  992. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  993. buffer_size, &xbs);
  994. up_read(&oi->ip_xattr_sem);
  995. return ret;
  996. }
  997. /* ocfs2_xattr_get()
  998. *
  999. * Copy an extended attribute into the buffer provided.
  1000. * Buffer is NULL to compute the size of buffer required.
  1001. */
  1002. static int ocfs2_xattr_get(struct inode *inode,
  1003. int name_index,
  1004. const char *name,
  1005. void *buffer,
  1006. size_t buffer_size)
  1007. {
  1008. int ret;
  1009. struct buffer_head *di_bh = NULL;
  1010. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  1011. if (ret < 0) {
  1012. mlog_errno(ret);
  1013. return ret;
  1014. }
  1015. ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  1016. name, buffer, buffer_size);
  1017. ocfs2_inode_unlock(inode, 0);
  1018. brelse(di_bh);
  1019. return ret;
  1020. }
  1021. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  1022. handle_t *handle,
  1023. struct ocfs2_xattr_value_buf *vb,
  1024. const void *value,
  1025. int value_len)
  1026. {
  1027. int ret = 0, i, cp_len;
  1028. u16 blocksize = inode->i_sb->s_blocksize;
  1029. u32 p_cluster, num_clusters;
  1030. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  1031. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  1032. u64 blkno;
  1033. struct buffer_head *bh = NULL;
  1034. unsigned int ext_flags;
  1035. struct ocfs2_xattr_value_root *xv = vb->vb_xv;
  1036. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  1037. while (cpos < clusters) {
  1038. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  1039. &num_clusters, &xv->xr_list,
  1040. &ext_flags);
  1041. if (ret) {
  1042. mlog_errno(ret);
  1043. goto out;
  1044. }
  1045. BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
  1046. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  1047. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  1048. ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
  1049. &bh, NULL);
  1050. if (ret) {
  1051. mlog_errno(ret);
  1052. goto out;
  1053. }
  1054. ret = ocfs2_journal_access(handle,
  1055. INODE_CACHE(inode),
  1056. bh,
  1057. OCFS2_JOURNAL_ACCESS_WRITE);
  1058. if (ret < 0) {
  1059. mlog_errno(ret);
  1060. goto out;
  1061. }
  1062. cp_len = value_len > blocksize ? blocksize : value_len;
  1063. memcpy(bh->b_data, value, cp_len);
  1064. value_len -= cp_len;
  1065. value += cp_len;
  1066. if (cp_len < blocksize)
  1067. memset(bh->b_data + cp_len, 0,
  1068. blocksize - cp_len);
  1069. ret = ocfs2_journal_dirty(handle, bh);
  1070. if (ret < 0) {
  1071. mlog_errno(ret);
  1072. goto out;
  1073. }
  1074. brelse(bh);
  1075. bh = NULL;
  1076. /*
  1077. * XXX: do we need to empty all the following
  1078. * blocks in this cluster?
  1079. */
  1080. if (!value_len)
  1081. break;
  1082. }
  1083. cpos += num_clusters;
  1084. }
  1085. out:
  1086. brelse(bh);
  1087. return ret;
  1088. }
  1089. static int ocfs2_xattr_cleanup(struct inode *inode,
  1090. handle_t *handle,
  1091. struct ocfs2_xattr_info *xi,
  1092. struct ocfs2_xattr_search *xs,
  1093. struct ocfs2_xattr_value_buf *vb,
  1094. size_t offs)
  1095. {
  1096. int ret = 0;
  1097. size_t name_len = strlen(xi->name);
  1098. void *val = xs->base + offs;
  1099. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1100. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  1101. OCFS2_JOURNAL_ACCESS_WRITE);
  1102. if (ret) {
  1103. mlog_errno(ret);
  1104. goto out;
  1105. }
  1106. /* Decrease xattr count */
  1107. le16_add_cpu(&xs->header->xh_count, -1);
  1108. /* Remove the xattr entry and tree root which has already be set*/
  1109. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  1110. memset(val, 0, size);
  1111. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  1112. if (ret < 0)
  1113. mlog_errno(ret);
  1114. out:
  1115. return ret;
  1116. }
  1117. static int ocfs2_xattr_update_entry(struct inode *inode,
  1118. handle_t *handle,
  1119. struct ocfs2_xattr_info *xi,
  1120. struct ocfs2_xattr_search *xs,
  1121. struct ocfs2_xattr_value_buf *vb,
  1122. size_t offs)
  1123. {
  1124. int ret;
  1125. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  1126. OCFS2_JOURNAL_ACCESS_WRITE);
  1127. if (ret) {
  1128. mlog_errno(ret);
  1129. goto out;
  1130. }
  1131. xs->here->xe_name_offset = cpu_to_le16(offs);
  1132. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1133. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  1134. ocfs2_xattr_set_local(xs->here, 1);
  1135. else
  1136. ocfs2_xattr_set_local(xs->here, 0);
  1137. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1138. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  1139. if (ret < 0)
  1140. mlog_errno(ret);
  1141. out:
  1142. return ret;
  1143. }
  1144. /*
  1145. * ocfs2_xattr_set_value_outside()
  1146. *
  1147. * Set large size value in B tree.
  1148. */
  1149. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  1150. struct ocfs2_xattr_info *xi,
  1151. struct ocfs2_xattr_search *xs,
  1152. struct ocfs2_xattr_set_ctxt *ctxt,
  1153. struct ocfs2_xattr_value_buf *vb,
  1154. size_t offs)
  1155. {
  1156. size_t name_len = strlen(xi->name);
  1157. void *val = xs->base + offs;
  1158. struct ocfs2_xattr_value_root *xv = NULL;
  1159. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1160. int ret = 0;
  1161. memset(val, 0, size);
  1162. memcpy(val, xi->name, name_len);
  1163. xv = (struct ocfs2_xattr_value_root *)
  1164. (val + OCFS2_XATTR_SIZE(name_len));
  1165. xv->xr_clusters = 0;
  1166. xv->xr_last_eb_blk = 0;
  1167. xv->xr_list.l_tree_depth = 0;
  1168. xv->xr_list.l_count = cpu_to_le16(1);
  1169. xv->xr_list.l_next_free_rec = 0;
  1170. vb->vb_xv = xv;
  1171. ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
  1172. if (ret < 0) {
  1173. mlog_errno(ret);
  1174. return ret;
  1175. }
  1176. ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
  1177. if (ret < 0) {
  1178. mlog_errno(ret);
  1179. return ret;
  1180. }
  1181. ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
  1182. xi->value, xi->value_len);
  1183. if (ret < 0)
  1184. mlog_errno(ret);
  1185. return ret;
  1186. }
  1187. /*
  1188. * ocfs2_xattr_set_entry_local()
  1189. *
  1190. * Set, replace or remove extended attribute in local.
  1191. */
  1192. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  1193. struct ocfs2_xattr_info *xi,
  1194. struct ocfs2_xattr_search *xs,
  1195. struct ocfs2_xattr_entry *last,
  1196. size_t min_offs)
  1197. {
  1198. size_t name_len = strlen(xi->name);
  1199. int i;
  1200. if (xi->value && xs->not_found) {
  1201. /* Insert the new xattr entry. */
  1202. le16_add_cpu(&xs->header->xh_count, 1);
  1203. ocfs2_xattr_set_type(last, xi->name_index);
  1204. ocfs2_xattr_set_local(last, 1);
  1205. last->xe_name_len = name_len;
  1206. } else {
  1207. void *first_val;
  1208. void *val;
  1209. size_t offs, size;
  1210. first_val = xs->base + min_offs;
  1211. offs = le16_to_cpu(xs->here->xe_name_offset);
  1212. val = xs->base + offs;
  1213. if (le64_to_cpu(xs->here->xe_value_size) >
  1214. OCFS2_XATTR_INLINE_SIZE)
  1215. size = OCFS2_XATTR_SIZE(name_len) +
  1216. OCFS2_XATTR_ROOT_SIZE;
  1217. else
  1218. size = OCFS2_XATTR_SIZE(name_len) +
  1219. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1220. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  1221. OCFS2_XATTR_SIZE(xi->value_len)) {
  1222. /* The old and the new value have the
  1223. same size. Just replace the value. */
  1224. ocfs2_xattr_set_local(xs->here, 1);
  1225. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1226. /* Clear value bytes. */
  1227. memset(val + OCFS2_XATTR_SIZE(name_len),
  1228. 0,
  1229. OCFS2_XATTR_SIZE(xi->value_len));
  1230. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1231. xi->value,
  1232. xi->value_len);
  1233. return;
  1234. }
  1235. /* Remove the old name+value. */
  1236. memmove(first_val + size, first_val, val - first_val);
  1237. memset(first_val, 0, size);
  1238. xs->here->xe_name_hash = 0;
  1239. xs->here->xe_name_offset = 0;
  1240. ocfs2_xattr_set_local(xs->here, 1);
  1241. xs->here->xe_value_size = 0;
  1242. min_offs += size;
  1243. /* Adjust all value offsets. */
  1244. last = xs->header->xh_entries;
  1245. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1246. size_t o = le16_to_cpu(last->xe_name_offset);
  1247. if (o < offs)
  1248. last->xe_name_offset = cpu_to_le16(o + size);
  1249. last += 1;
  1250. }
  1251. if (!xi->value) {
  1252. /* Remove the old entry. */
  1253. last -= 1;
  1254. memmove(xs->here, xs->here + 1,
  1255. (void *)last - (void *)xs->here);
  1256. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  1257. le16_add_cpu(&xs->header->xh_count, -1);
  1258. }
  1259. }
  1260. if (xi->value) {
  1261. /* Insert the new name+value. */
  1262. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1263. OCFS2_XATTR_SIZE(xi->value_len);
  1264. void *val = xs->base + min_offs - size;
  1265. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1266. memset(val, 0, size);
  1267. memcpy(val, xi->name, name_len);
  1268. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1269. xi->value,
  1270. xi->value_len);
  1271. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1272. ocfs2_xattr_set_local(xs->here, 1);
  1273. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1274. }
  1275. return;
  1276. }
  1277. /*
  1278. * ocfs2_xattr_set_entry()
  1279. *
  1280. * Set extended attribute entry into inode or block.
  1281. *
  1282. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1283. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1284. * then set value in B tree with set_value_outside().
  1285. */
  1286. static int ocfs2_xattr_set_entry(struct inode *inode,
  1287. struct ocfs2_xattr_info *xi,
  1288. struct ocfs2_xattr_search *xs,
  1289. struct ocfs2_xattr_set_ctxt *ctxt,
  1290. int flag)
  1291. {
  1292. struct ocfs2_xattr_entry *last;
  1293. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1294. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1295. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1296. size_t size_l = 0;
  1297. handle_t *handle = ctxt->handle;
  1298. int free, i, ret;
  1299. struct ocfs2_xattr_info xi_l = {
  1300. .name_index = xi->name_index,
  1301. .name = xi->name,
  1302. .value = xi->value,
  1303. .value_len = xi->value_len,
  1304. };
  1305. struct ocfs2_xattr_value_buf vb = {
  1306. .vb_bh = xs->xattr_bh,
  1307. .vb_access = ocfs2_journal_access_di,
  1308. };
  1309. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1310. BUG_ON(xs->xattr_bh == xs->inode_bh);
  1311. vb.vb_access = ocfs2_journal_access_xb;
  1312. } else
  1313. BUG_ON(xs->xattr_bh != xs->inode_bh);
  1314. /* Compute min_offs, last and free space. */
  1315. last = xs->header->xh_entries;
  1316. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1317. size_t offs = le16_to_cpu(last->xe_name_offset);
  1318. if (offs < min_offs)
  1319. min_offs = offs;
  1320. last += 1;
  1321. }
  1322. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  1323. if (free < 0)
  1324. return -EIO;
  1325. if (!xs->not_found) {
  1326. size_t size = 0;
  1327. if (ocfs2_xattr_is_local(xs->here))
  1328. size = OCFS2_XATTR_SIZE(name_len) +
  1329. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1330. else
  1331. size = OCFS2_XATTR_SIZE(name_len) +
  1332. OCFS2_XATTR_ROOT_SIZE;
  1333. free += (size + sizeof(struct ocfs2_xattr_entry));
  1334. }
  1335. /* Check free space in inode or block */
  1336. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1337. if (free < sizeof(struct ocfs2_xattr_entry) +
  1338. OCFS2_XATTR_SIZE(name_len) +
  1339. OCFS2_XATTR_ROOT_SIZE) {
  1340. ret = -ENOSPC;
  1341. goto out;
  1342. }
  1343. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1344. xi_l.value = (void *)&def_xv;
  1345. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1346. } else if (xi->value) {
  1347. if (free < sizeof(struct ocfs2_xattr_entry) +
  1348. OCFS2_XATTR_SIZE(name_len) +
  1349. OCFS2_XATTR_SIZE(xi->value_len)) {
  1350. ret = -ENOSPC;
  1351. goto out;
  1352. }
  1353. }
  1354. if (!xs->not_found) {
  1355. /* For existing extended attribute */
  1356. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1357. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1358. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1359. void *val = xs->base + offs;
  1360. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1361. /* Replace existing local xattr with tree root */
  1362. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1363. ctxt, &vb, offs);
  1364. if (ret < 0)
  1365. mlog_errno(ret);
  1366. goto out;
  1367. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1368. /* For existing xattr which has value outside */
  1369. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  1370. (val + OCFS2_XATTR_SIZE(name_len));
  1371. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1372. /*
  1373. * If new value need set outside also,
  1374. * first truncate old value to new value,
  1375. * then set new value with set_value_outside().
  1376. */
  1377. ret = ocfs2_xattr_value_truncate(inode,
  1378. &vb,
  1379. xi->value_len,
  1380. ctxt);
  1381. if (ret < 0) {
  1382. mlog_errno(ret);
  1383. goto out;
  1384. }
  1385. ret = ocfs2_xattr_update_entry(inode,
  1386. handle,
  1387. xi,
  1388. xs,
  1389. &vb,
  1390. offs);
  1391. if (ret < 0) {
  1392. mlog_errno(ret);
  1393. goto out;
  1394. }
  1395. ret = __ocfs2_xattr_set_value_outside(inode,
  1396. handle,
  1397. &vb,
  1398. xi->value,
  1399. xi->value_len);
  1400. if (ret < 0)
  1401. mlog_errno(ret);
  1402. goto out;
  1403. } else {
  1404. /*
  1405. * If new value need set in local,
  1406. * just trucate old value to zero.
  1407. */
  1408. ret = ocfs2_xattr_value_truncate(inode,
  1409. &vb,
  1410. 0,
  1411. ctxt);
  1412. if (ret < 0)
  1413. mlog_errno(ret);
  1414. }
  1415. }
  1416. }
  1417. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
  1418. OCFS2_JOURNAL_ACCESS_WRITE);
  1419. if (ret) {
  1420. mlog_errno(ret);
  1421. goto out;
  1422. }
  1423. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1424. ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
  1425. OCFS2_JOURNAL_ACCESS_WRITE);
  1426. if (ret) {
  1427. mlog_errno(ret);
  1428. goto out;
  1429. }
  1430. }
  1431. /*
  1432. * Set value in local, include set tree root in local.
  1433. * This is the first step for value size >INLINE_SIZE.
  1434. */
  1435. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1436. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1437. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1438. if (ret < 0) {
  1439. mlog_errno(ret);
  1440. goto out;
  1441. }
  1442. }
  1443. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1444. (flag & OCFS2_INLINE_XATTR_FL)) {
  1445. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1446. unsigned int xattrsize = osb->s_xattr_inline_size;
  1447. /*
  1448. * Adjust extent record count or inline data size
  1449. * to reserve space for extended attribute.
  1450. */
  1451. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1452. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1453. le16_add_cpu(&idata->id_count, -xattrsize);
  1454. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1455. struct ocfs2_extent_list *el = &di->id2.i_list;
  1456. le16_add_cpu(&el->l_count, -(xattrsize /
  1457. sizeof(struct ocfs2_extent_rec)));
  1458. }
  1459. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1460. }
  1461. /* Update xattr flag */
  1462. spin_lock(&oi->ip_lock);
  1463. oi->ip_dyn_features |= flag;
  1464. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1465. spin_unlock(&oi->ip_lock);
  1466. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1467. if (ret < 0)
  1468. mlog_errno(ret);
  1469. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1470. /*
  1471. * Set value outside in B tree.
  1472. * This is the second step for value size > INLINE_SIZE.
  1473. */
  1474. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1475. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
  1476. &vb, offs);
  1477. if (ret < 0) {
  1478. int ret2;
  1479. mlog_errno(ret);
  1480. /*
  1481. * If set value outside failed, we have to clean
  1482. * the junk tree root we have already set in local.
  1483. */
  1484. ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
  1485. xi, xs, &vb, offs);
  1486. if (ret2 < 0)
  1487. mlog_errno(ret2);
  1488. }
  1489. }
  1490. out:
  1491. return ret;
  1492. }
  1493. static int ocfs2_remove_value_outside(struct inode*inode,
  1494. struct ocfs2_xattr_value_buf *vb,
  1495. struct ocfs2_xattr_header *header)
  1496. {
  1497. int ret = 0, i;
  1498. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1499. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  1500. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  1501. ctxt.handle = ocfs2_start_trans(osb,
  1502. ocfs2_remove_extent_credits(osb->sb));
  1503. if (IS_ERR(ctxt.handle)) {
  1504. ret = PTR_ERR(ctxt.handle);
  1505. mlog_errno(ret);
  1506. goto out;
  1507. }
  1508. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1509. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1510. if (!ocfs2_xattr_is_local(entry)) {
  1511. void *val;
  1512. val = (void *)header +
  1513. le16_to_cpu(entry->xe_name_offset);
  1514. vb->vb_xv = (struct ocfs2_xattr_value_root *)
  1515. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1516. ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
  1517. if (ret < 0) {
  1518. mlog_errno(ret);
  1519. break;
  1520. }
  1521. }
  1522. }
  1523. ocfs2_commit_trans(osb, ctxt.handle);
  1524. ocfs2_schedule_truncate_log_flush(osb, 1);
  1525. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  1526. out:
  1527. return ret;
  1528. }
  1529. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1530. struct buffer_head *di_bh)
  1531. {
  1532. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1533. struct ocfs2_xattr_header *header;
  1534. int ret;
  1535. struct ocfs2_xattr_value_buf vb = {
  1536. .vb_bh = di_bh,
  1537. .vb_access = ocfs2_journal_access_di,
  1538. };
  1539. header = (struct ocfs2_xattr_header *)
  1540. ((void *)di + inode->i_sb->s_blocksize -
  1541. le16_to_cpu(di->i_xattr_inline_size));
  1542. ret = ocfs2_remove_value_outside(inode, &vb, header);
  1543. return ret;
  1544. }
  1545. static int ocfs2_xattr_block_remove(struct inode *inode,
  1546. struct buffer_head *blk_bh)
  1547. {
  1548. struct ocfs2_xattr_block *xb;
  1549. int ret = 0;
  1550. struct ocfs2_xattr_value_buf vb = {
  1551. .vb_bh = blk_bh,
  1552. .vb_access = ocfs2_journal_access_xb,
  1553. };
  1554. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1555. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1556. struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
  1557. ret = ocfs2_remove_value_outside(inode, &vb, header);
  1558. } else
  1559. ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
  1560. return ret;
  1561. }
  1562. static int ocfs2_xattr_free_block(struct inode *inode,
  1563. u64 block)
  1564. {
  1565. struct inode *xb_alloc_inode;
  1566. struct buffer_head *xb_alloc_bh = NULL;
  1567. struct buffer_head *blk_bh = NULL;
  1568. struct ocfs2_xattr_block *xb;
  1569. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1570. handle_t *handle;
  1571. int ret = 0;
  1572. u64 blk, bg_blkno;
  1573. u16 bit;
  1574. ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
  1575. if (ret < 0) {
  1576. mlog_errno(ret);
  1577. goto out;
  1578. }
  1579. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1580. if (ret < 0) {
  1581. mlog_errno(ret);
  1582. goto out;
  1583. }
  1584. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1585. blk = le64_to_cpu(xb->xb_blkno);
  1586. bit = le16_to_cpu(xb->xb_suballoc_bit);
  1587. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1588. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1589. EXTENT_ALLOC_SYSTEM_INODE,
  1590. le16_to_cpu(xb->xb_suballoc_slot));
  1591. if (!xb_alloc_inode) {
  1592. ret = -ENOMEM;
  1593. mlog_errno(ret);
  1594. goto out;
  1595. }
  1596. mutex_lock(&xb_alloc_inode->i_mutex);
  1597. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1598. if (ret < 0) {
  1599. mlog_errno(ret);
  1600. goto out_mutex;
  1601. }
  1602. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  1603. if (IS_ERR(handle)) {
  1604. ret = PTR_ERR(handle);
  1605. mlog_errno(ret);
  1606. goto out_unlock;
  1607. }
  1608. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1609. bit, bg_blkno, 1);
  1610. if (ret < 0)
  1611. mlog_errno(ret);
  1612. ocfs2_commit_trans(osb, handle);
  1613. out_unlock:
  1614. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1615. brelse(xb_alloc_bh);
  1616. out_mutex:
  1617. mutex_unlock(&xb_alloc_inode->i_mutex);
  1618. iput(xb_alloc_inode);
  1619. out:
  1620. brelse(blk_bh);
  1621. return ret;
  1622. }
  1623. /*
  1624. * ocfs2_xattr_remove()
  1625. *
  1626. * Free extended attribute resources associated with this inode.
  1627. */
  1628. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1629. {
  1630. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1631. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1632. handle_t *handle;
  1633. int ret;
  1634. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1635. return 0;
  1636. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1637. return 0;
  1638. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1639. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1640. if (ret < 0) {
  1641. mlog_errno(ret);
  1642. goto out;
  1643. }
  1644. }
  1645. if (di->i_xattr_loc) {
  1646. ret = ocfs2_xattr_free_block(inode,
  1647. le64_to_cpu(di->i_xattr_loc));
  1648. if (ret < 0) {
  1649. mlog_errno(ret);
  1650. goto out;
  1651. }
  1652. }
  1653. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1654. OCFS2_INODE_UPDATE_CREDITS);
  1655. if (IS_ERR(handle)) {
  1656. ret = PTR_ERR(handle);
  1657. mlog_errno(ret);
  1658. goto out;
  1659. }
  1660. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1661. OCFS2_JOURNAL_ACCESS_WRITE);
  1662. if (ret) {
  1663. mlog_errno(ret);
  1664. goto out_commit;
  1665. }
  1666. di->i_xattr_loc = 0;
  1667. spin_lock(&oi->ip_lock);
  1668. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1669. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1670. spin_unlock(&oi->ip_lock);
  1671. ret = ocfs2_journal_dirty(handle, di_bh);
  1672. if (ret < 0)
  1673. mlog_errno(ret);
  1674. out_commit:
  1675. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1676. out:
  1677. return ret;
  1678. }
  1679. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1680. struct ocfs2_dinode *di)
  1681. {
  1682. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1683. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1684. int free;
  1685. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1686. return 0;
  1687. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1688. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1689. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1690. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1691. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1692. le64_to_cpu(di->i_size);
  1693. } else {
  1694. struct ocfs2_extent_list *el = &di->id2.i_list;
  1695. free = (le16_to_cpu(el->l_count) -
  1696. le16_to_cpu(el->l_next_free_rec)) *
  1697. sizeof(struct ocfs2_extent_rec);
  1698. }
  1699. if (free >= xattrsize)
  1700. return 1;
  1701. return 0;
  1702. }
  1703. /*
  1704. * ocfs2_xattr_ibody_find()
  1705. *
  1706. * Find extended attribute in inode block and
  1707. * fill search info into struct ocfs2_xattr_search.
  1708. */
  1709. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1710. int name_index,
  1711. const char *name,
  1712. struct ocfs2_xattr_search *xs)
  1713. {
  1714. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1715. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1716. int ret;
  1717. int has_space = 0;
  1718. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1719. return 0;
  1720. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1721. down_read(&oi->ip_alloc_sem);
  1722. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1723. up_read(&oi->ip_alloc_sem);
  1724. if (!has_space)
  1725. return 0;
  1726. }
  1727. xs->xattr_bh = xs->inode_bh;
  1728. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1729. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1730. xs->header = (struct ocfs2_xattr_header *)
  1731. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1732. else
  1733. xs->header = (struct ocfs2_xattr_header *)
  1734. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1735. xs->base = (void *)xs->header;
  1736. xs->here = xs->header->xh_entries;
  1737. /* Find the named attribute. */
  1738. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1739. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1740. if (ret && ret != -ENODATA)
  1741. return ret;
  1742. xs->not_found = ret;
  1743. }
  1744. return 0;
  1745. }
  1746. /*
  1747. * ocfs2_xattr_ibody_set()
  1748. *
  1749. * Set, replace or remove an extended attribute into inode block.
  1750. *
  1751. */
  1752. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1753. struct ocfs2_xattr_info *xi,
  1754. struct ocfs2_xattr_search *xs,
  1755. struct ocfs2_xattr_set_ctxt *ctxt)
  1756. {
  1757. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1758. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1759. int ret;
  1760. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1761. return -ENOSPC;
  1762. down_write(&oi->ip_alloc_sem);
  1763. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1764. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1765. ret = -ENOSPC;
  1766. goto out;
  1767. }
  1768. }
  1769. ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
  1770. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1771. out:
  1772. up_write(&oi->ip_alloc_sem);
  1773. return ret;
  1774. }
  1775. /*
  1776. * ocfs2_xattr_block_find()
  1777. *
  1778. * Find extended attribute in external block and
  1779. * fill search info into struct ocfs2_xattr_search.
  1780. */
  1781. static int ocfs2_xattr_block_find(struct inode *inode,
  1782. int name_index,
  1783. const char *name,
  1784. struct ocfs2_xattr_search *xs)
  1785. {
  1786. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1787. struct buffer_head *blk_bh = NULL;
  1788. struct ocfs2_xattr_block *xb;
  1789. int ret = 0;
  1790. if (!di->i_xattr_loc)
  1791. return ret;
  1792. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  1793. &blk_bh);
  1794. if (ret < 0) {
  1795. mlog_errno(ret);
  1796. return ret;
  1797. }
  1798. xs->xattr_bh = blk_bh;
  1799. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1800. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1801. xs->header = &xb->xb_attrs.xb_header;
  1802. xs->base = (void *)xs->header;
  1803. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1804. xs->here = xs->header->xh_entries;
  1805. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1806. } else
  1807. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1808. name_index,
  1809. name, xs);
  1810. if (ret && ret != -ENODATA) {
  1811. xs->xattr_bh = NULL;
  1812. goto cleanup;
  1813. }
  1814. xs->not_found = ret;
  1815. return 0;
  1816. cleanup:
  1817. brelse(blk_bh);
  1818. return ret;
  1819. }
  1820. /*
  1821. * ocfs2_xattr_block_set()
  1822. *
  1823. * Set, replace or remove an extended attribute into external block.
  1824. *
  1825. */
  1826. static int ocfs2_xattr_block_set(struct inode *inode,
  1827. struct ocfs2_xattr_info *xi,
  1828. struct ocfs2_xattr_search *xs,
  1829. struct ocfs2_xattr_set_ctxt *ctxt)
  1830. {
  1831. struct buffer_head *new_bh = NULL;
  1832. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1833. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1834. handle_t *handle = ctxt->handle;
  1835. struct ocfs2_xattr_block *xblk = NULL;
  1836. u16 suballoc_bit_start;
  1837. u32 num_got;
  1838. u64 first_blkno;
  1839. int ret;
  1840. if (!xs->xattr_bh) {
  1841. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  1842. xs->inode_bh,
  1843. OCFS2_JOURNAL_ACCESS_CREATE);
  1844. if (ret < 0) {
  1845. mlog_errno(ret);
  1846. goto end;
  1847. }
  1848. ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
  1849. &suballoc_bit_start, &num_got,
  1850. &first_blkno);
  1851. if (ret < 0) {
  1852. mlog_errno(ret);
  1853. goto end;
  1854. }
  1855. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1856. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  1857. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
  1858. new_bh,
  1859. OCFS2_JOURNAL_ACCESS_CREATE);
  1860. if (ret < 0) {
  1861. mlog_errno(ret);
  1862. goto end;
  1863. }
  1864. /* Initialize ocfs2_xattr_block */
  1865. xs->xattr_bh = new_bh;
  1866. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1867. memset(xblk, 0, inode->i_sb->s_blocksize);
  1868. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1869. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1870. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1871. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1872. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1873. xs->header = &xblk->xb_attrs.xb_header;
  1874. xs->base = (void *)xs->header;
  1875. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1876. xs->here = xs->header->xh_entries;
  1877. ret = ocfs2_journal_dirty(handle, new_bh);
  1878. if (ret < 0) {
  1879. mlog_errno(ret);
  1880. goto end;
  1881. }
  1882. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1883. ocfs2_journal_dirty(handle, xs->inode_bh);
  1884. } else
  1885. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1886. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1887. /* Set extended attribute into external block */
  1888. ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
  1889. OCFS2_HAS_XATTR_FL);
  1890. if (!ret || ret != -ENOSPC)
  1891. goto end;
  1892. ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
  1893. if (ret)
  1894. goto end;
  1895. }
  1896. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
  1897. end:
  1898. return ret;
  1899. }
  1900. /* Check whether the new xattr can be inserted into the inode. */
  1901. static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
  1902. struct ocfs2_xattr_info *xi,
  1903. struct ocfs2_xattr_search *xs)
  1904. {
  1905. u64 value_size;
  1906. struct ocfs2_xattr_entry *last;
  1907. int free, i;
  1908. size_t min_offs = xs->end - xs->base;
  1909. if (!xs->header)
  1910. return 0;
  1911. last = xs->header->xh_entries;
  1912. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  1913. size_t offs = le16_to_cpu(last->xe_name_offset);
  1914. if (offs < min_offs)
  1915. min_offs = offs;
  1916. last += 1;
  1917. }
  1918. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  1919. if (free < 0)
  1920. return 0;
  1921. BUG_ON(!xs->not_found);
  1922. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  1923. value_size = OCFS2_XATTR_ROOT_SIZE;
  1924. else
  1925. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  1926. if (free >= sizeof(struct ocfs2_xattr_entry) +
  1927. OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
  1928. return 1;
  1929. return 0;
  1930. }
  1931. static int ocfs2_calc_xattr_set_need(struct inode *inode,
  1932. struct ocfs2_dinode *di,
  1933. struct ocfs2_xattr_info *xi,
  1934. struct ocfs2_xattr_search *xis,
  1935. struct ocfs2_xattr_search *xbs,
  1936. int *clusters_need,
  1937. int *meta_need,
  1938. int *credits_need)
  1939. {
  1940. int ret = 0, old_in_xb = 0;
  1941. int clusters_add = 0, meta_add = 0, credits = 0;
  1942. struct buffer_head *bh = NULL;
  1943. struct ocfs2_xattr_block *xb = NULL;
  1944. struct ocfs2_xattr_entry *xe = NULL;
  1945. struct ocfs2_xattr_value_root *xv = NULL;
  1946. char *base = NULL;
  1947. int name_offset, name_len = 0;
  1948. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  1949. xi->value_len);
  1950. u64 value_size;
  1951. /*
  1952. * Calculate the clusters we need to write.
  1953. * No matter whether we replace an old one or add a new one,
  1954. * we need this for writing.
  1955. */
  1956. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  1957. credits += new_clusters *
  1958. ocfs2_clusters_to_blocks(inode->i_sb, 1);
  1959. if (xis->not_found && xbs->not_found) {
  1960. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1961. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1962. clusters_add += new_clusters;
  1963. credits += ocfs2_calc_extend_credits(inode->i_sb,
  1964. &def_xv.xv.xr_list,
  1965. new_clusters);
  1966. }
  1967. goto meta_guess;
  1968. }
  1969. if (!xis->not_found) {
  1970. xe = xis->here;
  1971. name_offset = le16_to_cpu(xe->xe_name_offset);
  1972. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  1973. base = xis->base;
  1974. credits += OCFS2_INODE_UPDATE_CREDITS;
  1975. } else {
  1976. int i, block_off = 0;
  1977. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  1978. xe = xbs->here;
  1979. name_offset = le16_to_cpu(xe->xe_name_offset);
  1980. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  1981. i = xbs->here - xbs->header->xh_entries;
  1982. old_in_xb = 1;
  1983. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  1984. ret = ocfs2_xattr_bucket_get_name_value(inode,
  1985. bucket_xh(xbs->bucket),
  1986. i, &block_off,
  1987. &name_offset);
  1988. base = bucket_block(xbs->bucket, block_off);
  1989. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1990. } else {
  1991. base = xbs->base;
  1992. credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
  1993. }
  1994. }
  1995. /*
  1996. * delete a xattr doesn't need metadata and cluster allocation.
  1997. * so just calculate the credits and return.
  1998. *
  1999. * The credits for removing the value tree will be extended
  2000. * by ocfs2_remove_extent itself.
  2001. */
  2002. if (!xi->value) {
  2003. if (!ocfs2_xattr_is_local(xe))
  2004. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2005. goto out;
  2006. }
  2007. /* do cluster allocation guess first. */
  2008. value_size = le64_to_cpu(xe->xe_value_size);
  2009. if (old_in_xb) {
  2010. /*
  2011. * In xattr set, we always try to set the xe in inode first,
  2012. * so if it can be inserted into inode successfully, the old
  2013. * one will be removed from the xattr block, and this xattr
  2014. * will be inserted into inode as a new xattr in inode.
  2015. */
  2016. if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
  2017. clusters_add += new_clusters;
  2018. credits += ocfs2_remove_extent_credits(inode->i_sb) +
  2019. OCFS2_INODE_UPDATE_CREDITS;
  2020. if (!ocfs2_xattr_is_local(xe))
  2021. credits += ocfs2_calc_extend_credits(
  2022. inode->i_sb,
  2023. &def_xv.xv.xr_list,
  2024. new_clusters);
  2025. goto out;
  2026. }
  2027. }
  2028. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  2029. /* the new values will be stored outside. */
  2030. u32 old_clusters = 0;
  2031. if (!ocfs2_xattr_is_local(xe)) {
  2032. old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  2033. value_size);
  2034. xv = (struct ocfs2_xattr_value_root *)
  2035. (base + name_offset + name_len);
  2036. value_size = OCFS2_XATTR_ROOT_SIZE;
  2037. } else
  2038. xv = &def_xv.xv;
  2039. if (old_clusters >= new_clusters) {
  2040. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2041. goto out;
  2042. } else {
  2043. meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
  2044. clusters_add += new_clusters - old_clusters;
  2045. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2046. &xv->xr_list,
  2047. new_clusters -
  2048. old_clusters);
  2049. if (value_size >= OCFS2_XATTR_ROOT_SIZE)
  2050. goto out;
  2051. }
  2052. } else {
  2053. /*
  2054. * Now the new value will be stored inside. So if the new
  2055. * value is smaller than the size of value root or the old
  2056. * value, we don't need any allocation, otherwise we have
  2057. * to guess metadata allocation.
  2058. */
  2059. if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
  2060. (!ocfs2_xattr_is_local(xe) &&
  2061. OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
  2062. goto out;
  2063. }
  2064. meta_guess:
  2065. /* calculate metadata allocation. */
  2066. if (di->i_xattr_loc) {
  2067. if (!xbs->xattr_bh) {
  2068. ret = ocfs2_read_xattr_block(inode,
  2069. le64_to_cpu(di->i_xattr_loc),
  2070. &bh);
  2071. if (ret) {
  2072. mlog_errno(ret);
  2073. goto out;
  2074. }
  2075. xb = (struct ocfs2_xattr_block *)bh->b_data;
  2076. } else
  2077. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  2078. /*
  2079. * If there is already an xattr tree, good, we can calculate
  2080. * like other b-trees. Otherwise we may have the chance of
  2081. * create a tree, the credit calculation is borrowed from
  2082. * ocfs2_calc_extend_credits with root_el = NULL. And the
  2083. * new tree will be cluster based, so no meta is needed.
  2084. */
  2085. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  2086. struct ocfs2_extent_list *el =
  2087. &xb->xb_attrs.xb_root.xt_list;
  2088. meta_add += ocfs2_extend_meta_needed(el);
  2089. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2090. el, 1);
  2091. } else
  2092. credits += OCFS2_SUBALLOC_ALLOC + 1;
  2093. /*
  2094. * This cluster will be used either for new bucket or for
  2095. * new xattr block.
  2096. * If the cluster size is the same as the bucket size, one
  2097. * more is needed since we may need to extend the bucket
  2098. * also.
  2099. */
  2100. clusters_add += 1;
  2101. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2102. if (OCFS2_XATTR_BUCKET_SIZE ==
  2103. OCFS2_SB(inode->i_sb)->s_clustersize) {
  2104. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2105. clusters_add += 1;
  2106. }
  2107. } else {
  2108. meta_add += 1;
  2109. credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  2110. }
  2111. out:
  2112. if (clusters_need)
  2113. *clusters_need = clusters_add;
  2114. if (meta_need)
  2115. *meta_need = meta_add;
  2116. if (credits_need)
  2117. *credits_need = credits;
  2118. brelse(bh);
  2119. return ret;
  2120. }
  2121. static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
  2122. struct ocfs2_dinode *di,
  2123. struct ocfs2_xattr_info *xi,
  2124. struct ocfs2_xattr_search *xis,
  2125. struct ocfs2_xattr_search *xbs,
  2126. struct ocfs2_xattr_set_ctxt *ctxt,
  2127. int extra_meta,
  2128. int *credits)
  2129. {
  2130. int clusters_add, meta_add, ret;
  2131. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2132. memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
  2133. ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
  2134. ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
  2135. &clusters_add, &meta_add, credits);
  2136. if (ret) {
  2137. mlog_errno(ret);
  2138. return ret;
  2139. }
  2140. meta_add += extra_meta;
  2141. mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
  2142. "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
  2143. if (meta_add) {
  2144. ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
  2145. &ctxt->meta_ac);
  2146. if (ret) {
  2147. mlog_errno(ret);
  2148. goto out;
  2149. }
  2150. }
  2151. if (clusters_add) {
  2152. ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
  2153. if (ret)
  2154. mlog_errno(ret);
  2155. }
  2156. out:
  2157. if (ret) {
  2158. if (ctxt->meta_ac) {
  2159. ocfs2_free_alloc_context(ctxt->meta_ac);
  2160. ctxt->meta_ac = NULL;
  2161. }
  2162. /*
  2163. * We cannot have an error and a non null ctxt->data_ac.
  2164. */
  2165. }
  2166. return ret;
  2167. }
  2168. static int __ocfs2_xattr_set_handle(struct inode *inode,
  2169. struct ocfs2_dinode *di,
  2170. struct ocfs2_xattr_info *xi,
  2171. struct ocfs2_xattr_search *xis,
  2172. struct ocfs2_xattr_search *xbs,
  2173. struct ocfs2_xattr_set_ctxt *ctxt)
  2174. {
  2175. int ret = 0, credits, old_found;
  2176. if (!xi->value) {
  2177. /* Remove existing extended attribute */
  2178. if (!xis->not_found)
  2179. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2180. else if (!xbs->not_found)
  2181. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2182. } else {
  2183. /* We always try to set extended attribute into inode first*/
  2184. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2185. if (!ret && !xbs->not_found) {
  2186. /*
  2187. * If succeed and that extended attribute existing in
  2188. * external block, then we will remove it.
  2189. */
  2190. xi->value = NULL;
  2191. xi->value_len = 0;
  2192. old_found = xis->not_found;
  2193. xis->not_found = -ENODATA;
  2194. ret = ocfs2_calc_xattr_set_need(inode,
  2195. di,
  2196. xi,
  2197. xis,
  2198. xbs,
  2199. NULL,
  2200. NULL,
  2201. &credits);
  2202. xis->not_found = old_found;
  2203. if (ret) {
  2204. mlog_errno(ret);
  2205. goto out;
  2206. }
  2207. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2208. ctxt->handle->h_buffer_credits);
  2209. if (ret) {
  2210. mlog_errno(ret);
  2211. goto out;
  2212. }
  2213. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2214. } else if (ret == -ENOSPC) {
  2215. if (di->i_xattr_loc && !xbs->xattr_bh) {
  2216. ret = ocfs2_xattr_block_find(inode,
  2217. xi->name_index,
  2218. xi->name, xbs);
  2219. if (ret)
  2220. goto out;
  2221. old_found = xis->not_found;
  2222. xis->not_found = -ENODATA;
  2223. ret = ocfs2_calc_xattr_set_need(inode,
  2224. di,
  2225. xi,
  2226. xis,
  2227. xbs,
  2228. NULL,
  2229. NULL,
  2230. &credits);
  2231. xis->not_found = old_found;
  2232. if (ret) {
  2233. mlog_errno(ret);
  2234. goto out;
  2235. }
  2236. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2237. ctxt->handle->h_buffer_credits);
  2238. if (ret) {
  2239. mlog_errno(ret);
  2240. goto out;
  2241. }
  2242. }
  2243. /*
  2244. * If no space in inode, we will set extended attribute
  2245. * into external block.
  2246. */
  2247. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2248. if (ret)
  2249. goto out;
  2250. if (!xis->not_found) {
  2251. /*
  2252. * If succeed and that extended attribute
  2253. * existing in inode, we will remove it.
  2254. */
  2255. xi->value = NULL;
  2256. xi->value_len = 0;
  2257. xbs->not_found = -ENODATA;
  2258. ret = ocfs2_calc_xattr_set_need(inode,
  2259. di,
  2260. xi,
  2261. xis,
  2262. xbs,
  2263. NULL,
  2264. NULL,
  2265. &credits);
  2266. if (ret) {
  2267. mlog_errno(ret);
  2268. goto out;
  2269. }
  2270. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2271. ctxt->handle->h_buffer_credits);
  2272. if (ret) {
  2273. mlog_errno(ret);
  2274. goto out;
  2275. }
  2276. ret = ocfs2_xattr_ibody_set(inode, xi,
  2277. xis, ctxt);
  2278. }
  2279. }
  2280. }
  2281. if (!ret) {
  2282. /* Update inode ctime. */
  2283. ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
  2284. xis->inode_bh,
  2285. OCFS2_JOURNAL_ACCESS_WRITE);
  2286. if (ret) {
  2287. mlog_errno(ret);
  2288. goto out;
  2289. }
  2290. inode->i_ctime = CURRENT_TIME;
  2291. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  2292. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  2293. ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
  2294. }
  2295. out:
  2296. return ret;
  2297. }
  2298. /*
  2299. * This function only called duing creating inode
  2300. * for init security/acl xattrs of the new inode.
  2301. * All transanction credits have been reserved in mknod.
  2302. */
  2303. int ocfs2_xattr_set_handle(handle_t *handle,
  2304. struct inode *inode,
  2305. struct buffer_head *di_bh,
  2306. int name_index,
  2307. const char *name,
  2308. const void *value,
  2309. size_t value_len,
  2310. int flags,
  2311. struct ocfs2_alloc_context *meta_ac,
  2312. struct ocfs2_alloc_context *data_ac)
  2313. {
  2314. struct ocfs2_dinode *di;
  2315. int ret;
  2316. struct ocfs2_xattr_info xi = {
  2317. .name_index = name_index,
  2318. .name = name,
  2319. .value = value,
  2320. .value_len = value_len,
  2321. };
  2322. struct ocfs2_xattr_search xis = {
  2323. .not_found = -ENODATA,
  2324. };
  2325. struct ocfs2_xattr_search xbs = {
  2326. .not_found = -ENODATA,
  2327. };
  2328. struct ocfs2_xattr_set_ctxt ctxt = {
  2329. .handle = handle,
  2330. .meta_ac = meta_ac,
  2331. .data_ac = data_ac,
  2332. };
  2333. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2334. return -EOPNOTSUPP;
  2335. /*
  2336. * In extreme situation, may need xattr bucket when
  2337. * block size is too small. And we have already reserved
  2338. * the credits for bucket in mknod.
  2339. */
  2340. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
  2341. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2342. if (!xbs.bucket) {
  2343. mlog_errno(-ENOMEM);
  2344. return -ENOMEM;
  2345. }
  2346. }
  2347. xis.inode_bh = xbs.inode_bh = di_bh;
  2348. di = (struct ocfs2_dinode *)di_bh->b_data;
  2349. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2350. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2351. if (ret)
  2352. goto cleanup;
  2353. if (xis.not_found) {
  2354. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2355. if (ret)
  2356. goto cleanup;
  2357. }
  2358. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2359. cleanup:
  2360. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2361. brelse(xbs.xattr_bh);
  2362. ocfs2_xattr_bucket_free(xbs.bucket);
  2363. return ret;
  2364. }
  2365. /*
  2366. * ocfs2_xattr_set()
  2367. *
  2368. * Set, replace or remove an extended attribute for this inode.
  2369. * value is NULL to remove an existing extended attribute, else either
  2370. * create or replace an extended attribute.
  2371. */
  2372. int ocfs2_xattr_set(struct inode *inode,
  2373. int name_index,
  2374. const char *name,
  2375. const void *value,
  2376. size_t value_len,
  2377. int flags)
  2378. {
  2379. struct buffer_head *di_bh = NULL;
  2380. struct ocfs2_dinode *di;
  2381. int ret, credits, ref_meta = 0, ref_credits = 0;
  2382. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2383. struct inode *tl_inode = osb->osb_tl_inode;
  2384. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  2385. struct ocfs2_refcount_tree *ref_tree = NULL;
  2386. struct ocfs2_xattr_info xi = {
  2387. .name_index = name_index,
  2388. .name = name,
  2389. .value = value,
  2390. .value_len = value_len,
  2391. };
  2392. struct ocfs2_xattr_search xis = {
  2393. .not_found = -ENODATA,
  2394. };
  2395. struct ocfs2_xattr_search xbs = {
  2396. .not_found = -ENODATA,
  2397. };
  2398. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2399. return -EOPNOTSUPP;
  2400. /*
  2401. * Only xbs will be used on indexed trees. xis doesn't need a
  2402. * bucket.
  2403. */
  2404. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2405. if (!xbs.bucket) {
  2406. mlog_errno(-ENOMEM);
  2407. return -ENOMEM;
  2408. }
  2409. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  2410. if (ret < 0) {
  2411. mlog_errno(ret);
  2412. goto cleanup_nolock;
  2413. }
  2414. xis.inode_bh = xbs.inode_bh = di_bh;
  2415. di = (struct ocfs2_dinode *)di_bh->b_data;
  2416. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2417. /*
  2418. * Scan inode and external block to find the same name
  2419. * extended attribute and collect search infomation.
  2420. */
  2421. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2422. if (ret)
  2423. goto cleanup;
  2424. if (xis.not_found) {
  2425. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2426. if (ret)
  2427. goto cleanup;
  2428. }
  2429. if (xis.not_found && xbs.not_found) {
  2430. ret = -ENODATA;
  2431. if (flags & XATTR_REPLACE)
  2432. goto cleanup;
  2433. ret = 0;
  2434. if (!value)
  2435. goto cleanup;
  2436. } else {
  2437. ret = -EEXIST;
  2438. if (flags & XATTR_CREATE)
  2439. goto cleanup;
  2440. }
  2441. /* Check whether the value is refcounted and do some prepartion. */
  2442. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
  2443. (!xis.not_found || !xbs.not_found)) {
  2444. ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
  2445. &xis, &xbs, &ref_tree,
  2446. &ref_meta, &ref_credits);
  2447. if (ret) {
  2448. mlog_errno(ret);
  2449. goto cleanup;
  2450. }
  2451. }
  2452. mutex_lock(&tl_inode->i_mutex);
  2453. if (ocfs2_truncate_log_needs_flush(osb)) {
  2454. ret = __ocfs2_flush_truncate_log(osb);
  2455. if (ret < 0) {
  2456. mutex_unlock(&tl_inode->i_mutex);
  2457. mlog_errno(ret);
  2458. goto cleanup;
  2459. }
  2460. }
  2461. mutex_unlock(&tl_inode->i_mutex);
  2462. ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
  2463. &xbs, &ctxt, ref_meta, &credits);
  2464. if (ret) {
  2465. mlog_errno(ret);
  2466. goto cleanup;
  2467. }
  2468. /* we need to update inode's ctime field, so add credit for it. */
  2469. credits += OCFS2_INODE_UPDATE_CREDITS;
  2470. ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
  2471. if (IS_ERR(ctxt.handle)) {
  2472. ret = PTR_ERR(ctxt.handle);
  2473. mlog_errno(ret);
  2474. goto cleanup;
  2475. }
  2476. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2477. ocfs2_commit_trans(osb, ctxt.handle);
  2478. if (ctxt.data_ac)
  2479. ocfs2_free_alloc_context(ctxt.data_ac);
  2480. if (ctxt.meta_ac)
  2481. ocfs2_free_alloc_context(ctxt.meta_ac);
  2482. if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
  2483. ocfs2_schedule_truncate_log_flush(osb, 1);
  2484. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  2485. cleanup:
  2486. if (ref_tree)
  2487. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  2488. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2489. ocfs2_inode_unlock(inode, 1);
  2490. cleanup_nolock:
  2491. brelse(di_bh);
  2492. brelse(xbs.xattr_bh);
  2493. ocfs2_xattr_bucket_free(xbs.bucket);
  2494. return ret;
  2495. }
  2496. /*
  2497. * Find the xattr extent rec which may contains name_hash.
  2498. * e_cpos will be the first name hash of the xattr rec.
  2499. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  2500. */
  2501. static int ocfs2_xattr_get_rec(struct inode *inode,
  2502. u32 name_hash,
  2503. u64 *p_blkno,
  2504. u32 *e_cpos,
  2505. u32 *num_clusters,
  2506. struct ocfs2_extent_list *el)
  2507. {
  2508. int ret = 0, i;
  2509. struct buffer_head *eb_bh = NULL;
  2510. struct ocfs2_extent_block *eb;
  2511. struct ocfs2_extent_rec *rec = NULL;
  2512. u64 e_blkno = 0;
  2513. if (el->l_tree_depth) {
  2514. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
  2515. &eb_bh);
  2516. if (ret) {
  2517. mlog_errno(ret);
  2518. goto out;
  2519. }
  2520. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2521. el = &eb->h_list;
  2522. if (el->l_tree_depth) {
  2523. ocfs2_error(inode->i_sb,
  2524. "Inode %lu has non zero tree depth in "
  2525. "xattr tree block %llu\n", inode->i_ino,
  2526. (unsigned long long)eb_bh->b_blocknr);
  2527. ret = -EROFS;
  2528. goto out;
  2529. }
  2530. }
  2531. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  2532. rec = &el->l_recs[i];
  2533. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  2534. e_blkno = le64_to_cpu(rec->e_blkno);
  2535. break;
  2536. }
  2537. }
  2538. if (!e_blkno) {
  2539. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  2540. "record (%u, %u, 0) in xattr", inode->i_ino,
  2541. le32_to_cpu(rec->e_cpos),
  2542. ocfs2_rec_clusters(el, rec));
  2543. ret = -EROFS;
  2544. goto out;
  2545. }
  2546. *p_blkno = le64_to_cpu(rec->e_blkno);
  2547. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  2548. if (e_cpos)
  2549. *e_cpos = le32_to_cpu(rec->e_cpos);
  2550. out:
  2551. brelse(eb_bh);
  2552. return ret;
  2553. }
  2554. typedef int (xattr_bucket_func)(struct inode *inode,
  2555. struct ocfs2_xattr_bucket *bucket,
  2556. void *para);
  2557. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  2558. struct ocfs2_xattr_bucket *bucket,
  2559. int name_index,
  2560. const char *name,
  2561. u32 name_hash,
  2562. u16 *xe_index,
  2563. int *found)
  2564. {
  2565. int i, ret = 0, cmp = 1, block_off, new_offset;
  2566. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  2567. size_t name_len = strlen(name);
  2568. struct ocfs2_xattr_entry *xe = NULL;
  2569. char *xe_name;
  2570. /*
  2571. * We don't use binary search in the bucket because there
  2572. * may be multiple entries with the same name hash.
  2573. */
  2574. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  2575. xe = &xh->xh_entries[i];
  2576. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  2577. continue;
  2578. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  2579. break;
  2580. cmp = name_index - ocfs2_xattr_get_type(xe);
  2581. if (!cmp)
  2582. cmp = name_len - xe->xe_name_len;
  2583. if (cmp)
  2584. continue;
  2585. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2586. xh,
  2587. i,
  2588. &block_off,
  2589. &new_offset);
  2590. if (ret) {
  2591. mlog_errno(ret);
  2592. break;
  2593. }
  2594. xe_name = bucket_block(bucket, block_off) + new_offset;
  2595. if (!memcmp(name, xe_name, name_len)) {
  2596. *xe_index = i;
  2597. *found = 1;
  2598. ret = 0;
  2599. break;
  2600. }
  2601. }
  2602. return ret;
  2603. }
  2604. /*
  2605. * Find the specified xattr entry in a series of buckets.
  2606. * This series start from p_blkno and last for num_clusters.
  2607. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  2608. * the num of the valid buckets.
  2609. *
  2610. * Return the buffer_head this xattr should reside in. And if the xattr's
  2611. * hash is in the gap of 2 buckets, return the lower bucket.
  2612. */
  2613. static int ocfs2_xattr_bucket_find(struct inode *inode,
  2614. int name_index,
  2615. const char *name,
  2616. u32 name_hash,
  2617. u64 p_blkno,
  2618. u32 first_hash,
  2619. u32 num_clusters,
  2620. struct ocfs2_xattr_search *xs)
  2621. {
  2622. int ret, found = 0;
  2623. struct ocfs2_xattr_header *xh = NULL;
  2624. struct ocfs2_xattr_entry *xe = NULL;
  2625. u16 index = 0;
  2626. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2627. int low_bucket = 0, bucket, high_bucket;
  2628. struct ocfs2_xattr_bucket *search;
  2629. u32 last_hash;
  2630. u64 blkno, lower_blkno = 0;
  2631. search = ocfs2_xattr_bucket_new(inode);
  2632. if (!search) {
  2633. ret = -ENOMEM;
  2634. mlog_errno(ret);
  2635. goto out;
  2636. }
  2637. ret = ocfs2_read_xattr_bucket(search, p_blkno);
  2638. if (ret) {
  2639. mlog_errno(ret);
  2640. goto out;
  2641. }
  2642. xh = bucket_xh(search);
  2643. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  2644. while (low_bucket <= high_bucket) {
  2645. ocfs2_xattr_bucket_relse(search);
  2646. bucket = (low_bucket + high_bucket) / 2;
  2647. blkno = p_blkno + bucket * blk_per_bucket;
  2648. ret = ocfs2_read_xattr_bucket(search, blkno);
  2649. if (ret) {
  2650. mlog_errno(ret);
  2651. goto out;
  2652. }
  2653. xh = bucket_xh(search);
  2654. xe = &xh->xh_entries[0];
  2655. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  2656. high_bucket = bucket - 1;
  2657. continue;
  2658. }
  2659. /*
  2660. * Check whether the hash of the last entry in our
  2661. * bucket is larger than the search one. for an empty
  2662. * bucket, the last one is also the first one.
  2663. */
  2664. if (xh->xh_count)
  2665. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  2666. last_hash = le32_to_cpu(xe->xe_name_hash);
  2667. /* record lower_blkno which may be the insert place. */
  2668. lower_blkno = blkno;
  2669. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  2670. low_bucket = bucket + 1;
  2671. continue;
  2672. }
  2673. /* the searched xattr should reside in this bucket if exists. */
  2674. ret = ocfs2_find_xe_in_bucket(inode, search,
  2675. name_index, name, name_hash,
  2676. &index, &found);
  2677. if (ret) {
  2678. mlog_errno(ret);
  2679. goto out;
  2680. }
  2681. break;
  2682. }
  2683. /*
  2684. * Record the bucket we have found.
  2685. * When the xattr's hash value is in the gap of 2 buckets, we will
  2686. * always set it to the previous bucket.
  2687. */
  2688. if (!lower_blkno)
  2689. lower_blkno = p_blkno;
  2690. /* This should be in cache - we just read it during the search */
  2691. ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
  2692. if (ret) {
  2693. mlog_errno(ret);
  2694. goto out;
  2695. }
  2696. xs->header = bucket_xh(xs->bucket);
  2697. xs->base = bucket_block(xs->bucket, 0);
  2698. xs->end = xs->base + inode->i_sb->s_blocksize;
  2699. if (found) {
  2700. xs->here = &xs->header->xh_entries[index];
  2701. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  2702. (unsigned long long)bucket_blkno(xs->bucket), index);
  2703. } else
  2704. ret = -ENODATA;
  2705. out:
  2706. ocfs2_xattr_bucket_free(search);
  2707. return ret;
  2708. }
  2709. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2710. struct buffer_head *root_bh,
  2711. int name_index,
  2712. const char *name,
  2713. struct ocfs2_xattr_search *xs)
  2714. {
  2715. int ret;
  2716. struct ocfs2_xattr_block *xb =
  2717. (struct ocfs2_xattr_block *)root_bh->b_data;
  2718. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2719. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2720. u64 p_blkno = 0;
  2721. u32 first_hash, num_clusters = 0;
  2722. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  2723. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2724. return -ENODATA;
  2725. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2726. name, name_hash, name_index);
  2727. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2728. &num_clusters, el);
  2729. if (ret) {
  2730. mlog_errno(ret);
  2731. goto out;
  2732. }
  2733. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2734. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2735. "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
  2736. first_hash);
  2737. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2738. p_blkno, first_hash, num_clusters, xs);
  2739. out:
  2740. return ret;
  2741. }
  2742. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2743. u64 blkno,
  2744. u32 clusters,
  2745. xattr_bucket_func *func,
  2746. void *para)
  2747. {
  2748. int i, ret = 0;
  2749. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2750. u32 num_buckets = clusters * bpc;
  2751. struct ocfs2_xattr_bucket *bucket;
  2752. bucket = ocfs2_xattr_bucket_new(inode);
  2753. if (!bucket) {
  2754. mlog_errno(-ENOMEM);
  2755. return -ENOMEM;
  2756. }
  2757. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2758. clusters, (unsigned long long)blkno);
  2759. for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
  2760. ret = ocfs2_read_xattr_bucket(bucket, blkno);
  2761. if (ret) {
  2762. mlog_errno(ret);
  2763. break;
  2764. }
  2765. /*
  2766. * The real bucket num in this series of blocks is stored
  2767. * in the 1st bucket.
  2768. */
  2769. if (i == 0)
  2770. num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
  2771. mlog(0, "iterating xattr bucket %llu, first hash %u\n",
  2772. (unsigned long long)blkno,
  2773. le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
  2774. if (func) {
  2775. ret = func(inode, bucket, para);
  2776. if (ret && ret != -ERANGE)
  2777. mlog_errno(ret);
  2778. /* Fall through to bucket_relse() */
  2779. }
  2780. ocfs2_xattr_bucket_relse(bucket);
  2781. if (ret)
  2782. break;
  2783. }
  2784. ocfs2_xattr_bucket_free(bucket);
  2785. return ret;
  2786. }
  2787. struct ocfs2_xattr_tree_list {
  2788. char *buffer;
  2789. size_t buffer_size;
  2790. size_t result;
  2791. };
  2792. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  2793. struct ocfs2_xattr_header *xh,
  2794. int index,
  2795. int *block_off,
  2796. int *new_offset)
  2797. {
  2798. u16 name_offset;
  2799. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2800. return -EINVAL;
  2801. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2802. *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
  2803. *new_offset = name_offset % inode->i_sb->s_blocksize;
  2804. return 0;
  2805. }
  2806. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2807. struct ocfs2_xattr_bucket *bucket,
  2808. void *para)
  2809. {
  2810. int ret = 0, type;
  2811. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2812. int i, block_off, new_offset;
  2813. const char *prefix, *name;
  2814. for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
  2815. struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
  2816. type = ocfs2_xattr_get_type(entry);
  2817. prefix = ocfs2_xattr_prefix(type);
  2818. if (prefix) {
  2819. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2820. bucket_xh(bucket),
  2821. i,
  2822. &block_off,
  2823. &new_offset);
  2824. if (ret)
  2825. break;
  2826. name = (const char *)bucket_block(bucket, block_off) +
  2827. new_offset;
  2828. ret = ocfs2_xattr_list_entry(xl->buffer,
  2829. xl->buffer_size,
  2830. &xl->result,
  2831. prefix, name,
  2832. entry->xe_name_len);
  2833. if (ret)
  2834. break;
  2835. }
  2836. }
  2837. return ret;
  2838. }
  2839. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2840. struct ocfs2_xattr_tree_root *xt,
  2841. char *buffer,
  2842. size_t buffer_size)
  2843. {
  2844. struct ocfs2_extent_list *el = &xt->xt_list;
  2845. int ret = 0;
  2846. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2847. u64 p_blkno = 0;
  2848. struct ocfs2_xattr_tree_list xl = {
  2849. .buffer = buffer,
  2850. .buffer_size = buffer_size,
  2851. .result = 0,
  2852. };
  2853. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2854. return 0;
  2855. while (name_hash > 0) {
  2856. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2857. &e_cpos, &num_clusters, el);
  2858. if (ret) {
  2859. mlog_errno(ret);
  2860. goto out;
  2861. }
  2862. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  2863. ocfs2_list_xattr_bucket,
  2864. &xl);
  2865. if (ret) {
  2866. if (ret != -ERANGE)
  2867. mlog_errno(ret);
  2868. goto out;
  2869. }
  2870. if (e_cpos == 0)
  2871. break;
  2872. name_hash = e_cpos - 1;
  2873. }
  2874. ret = xl.result;
  2875. out:
  2876. return ret;
  2877. }
  2878. static int cmp_xe(const void *a, const void *b)
  2879. {
  2880. const struct ocfs2_xattr_entry *l = a, *r = b;
  2881. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  2882. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  2883. if (l_hash > r_hash)
  2884. return 1;
  2885. if (l_hash < r_hash)
  2886. return -1;
  2887. return 0;
  2888. }
  2889. static void swap_xe(void *a, void *b, int size)
  2890. {
  2891. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  2892. tmp = *l;
  2893. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  2894. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  2895. }
  2896. /*
  2897. * When the ocfs2_xattr_block is filled up, new bucket will be created
  2898. * and all the xattr entries will be moved to the new bucket.
  2899. * The header goes at the start of the bucket, and the names+values are
  2900. * filled from the end. This is why *target starts as the last buffer.
  2901. * Note: we need to sort the entries since they are not saved in order
  2902. * in the ocfs2_xattr_block.
  2903. */
  2904. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  2905. struct buffer_head *xb_bh,
  2906. struct ocfs2_xattr_bucket *bucket)
  2907. {
  2908. int i, blocksize = inode->i_sb->s_blocksize;
  2909. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2910. u16 offset, size, off_change;
  2911. struct ocfs2_xattr_entry *xe;
  2912. struct ocfs2_xattr_block *xb =
  2913. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2914. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  2915. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  2916. u16 count = le16_to_cpu(xb_xh->xh_count);
  2917. char *src = xb_bh->b_data;
  2918. char *target = bucket_block(bucket, blks - 1);
  2919. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  2920. (unsigned long long)xb_bh->b_blocknr,
  2921. (unsigned long long)bucket_blkno(bucket));
  2922. for (i = 0; i < blks; i++)
  2923. memset(bucket_block(bucket, i), 0, blocksize);
  2924. /*
  2925. * Since the xe_name_offset is based on ocfs2_xattr_header,
  2926. * there is a offset change corresponding to the change of
  2927. * ocfs2_xattr_header's position.
  2928. */
  2929. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2930. xe = &xb_xh->xh_entries[count - 1];
  2931. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  2932. size = blocksize - offset;
  2933. /* copy all the names and values. */
  2934. memcpy(target + offset, src + offset, size);
  2935. /* Init new header now. */
  2936. xh->xh_count = xb_xh->xh_count;
  2937. xh->xh_num_buckets = cpu_to_le16(1);
  2938. xh->xh_name_value_len = cpu_to_le16(size);
  2939. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  2940. /* copy all the entries. */
  2941. target = bucket_block(bucket, 0);
  2942. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  2943. size = count * sizeof(struct ocfs2_xattr_entry);
  2944. memcpy(target + offset, (char *)xb_xh + offset, size);
  2945. /* Change the xe offset for all the xe because of the move. */
  2946. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  2947. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2948. for (i = 0; i < count; i++)
  2949. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  2950. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  2951. offset, size, off_change);
  2952. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  2953. cmp_xe, swap_xe);
  2954. }
  2955. /*
  2956. * After we move xattr from block to index btree, we have to
  2957. * update ocfs2_xattr_search to the new xe and base.
  2958. *
  2959. * When the entry is in xattr block, xattr_bh indicates the storage place.
  2960. * While if the entry is in index b-tree, "bucket" indicates the
  2961. * real place of the xattr.
  2962. */
  2963. static void ocfs2_xattr_update_xattr_search(struct inode *inode,
  2964. struct ocfs2_xattr_search *xs,
  2965. struct buffer_head *old_bh)
  2966. {
  2967. char *buf = old_bh->b_data;
  2968. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  2969. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  2970. int i;
  2971. xs->header = bucket_xh(xs->bucket);
  2972. xs->base = bucket_block(xs->bucket, 0);
  2973. xs->end = xs->base + inode->i_sb->s_blocksize;
  2974. if (xs->not_found)
  2975. return;
  2976. i = xs->here - old_xh->xh_entries;
  2977. xs->here = &xs->header->xh_entries[i];
  2978. }
  2979. static int ocfs2_xattr_create_index_block(struct inode *inode,
  2980. struct ocfs2_xattr_search *xs,
  2981. struct ocfs2_xattr_set_ctxt *ctxt)
  2982. {
  2983. int ret;
  2984. u32 bit_off, len;
  2985. u64 blkno;
  2986. handle_t *handle = ctxt->handle;
  2987. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2988. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2989. struct buffer_head *xb_bh = xs->xattr_bh;
  2990. struct ocfs2_xattr_block *xb =
  2991. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2992. struct ocfs2_xattr_tree_root *xr;
  2993. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  2994. mlog(0, "create xattr index block for %llu\n",
  2995. (unsigned long long)xb_bh->b_blocknr);
  2996. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  2997. BUG_ON(!xs->bucket);
  2998. /*
  2999. * XXX:
  3000. * We can use this lock for now, and maybe move to a dedicated mutex
  3001. * if performance becomes a problem later.
  3002. */
  3003. down_write(&oi->ip_alloc_sem);
  3004. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
  3005. OCFS2_JOURNAL_ACCESS_WRITE);
  3006. if (ret) {
  3007. mlog_errno(ret);
  3008. goto out;
  3009. }
  3010. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
  3011. 1, 1, &bit_off, &len);
  3012. if (ret) {
  3013. mlog_errno(ret);
  3014. goto out;
  3015. }
  3016. /*
  3017. * The bucket may spread in many blocks, and
  3018. * we will only touch the 1st block and the last block
  3019. * in the whole bucket(one for entry and one for data).
  3020. */
  3021. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  3022. mlog(0, "allocate 1 cluster from %llu to xattr block\n",
  3023. (unsigned long long)blkno);
  3024. ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
  3025. if (ret) {
  3026. mlog_errno(ret);
  3027. goto out;
  3028. }
  3029. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  3030. OCFS2_JOURNAL_ACCESS_CREATE);
  3031. if (ret) {
  3032. mlog_errno(ret);
  3033. goto out;
  3034. }
  3035. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
  3036. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  3037. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
  3038. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  3039. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  3040. offsetof(struct ocfs2_xattr_block, xb_attrs));
  3041. xr = &xb->xb_attrs.xb_root;
  3042. xr->xt_clusters = cpu_to_le32(1);
  3043. xr->xt_last_eb_blk = 0;
  3044. xr->xt_list.l_tree_depth = 0;
  3045. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  3046. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  3047. xr->xt_list.l_recs[0].e_cpos = 0;
  3048. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  3049. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  3050. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  3051. ocfs2_journal_dirty(handle, xb_bh);
  3052. out:
  3053. up_write(&oi->ip_alloc_sem);
  3054. return ret;
  3055. }
  3056. static int cmp_xe_offset(const void *a, const void *b)
  3057. {
  3058. const struct ocfs2_xattr_entry *l = a, *r = b;
  3059. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  3060. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  3061. if (l_name_offset < r_name_offset)
  3062. return 1;
  3063. if (l_name_offset > r_name_offset)
  3064. return -1;
  3065. return 0;
  3066. }
  3067. /*
  3068. * defrag a xattr bucket if we find that the bucket has some
  3069. * holes beteen name/value pairs.
  3070. * We will move all the name/value pairs to the end of the bucket
  3071. * so that we can spare some space for insertion.
  3072. */
  3073. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  3074. handle_t *handle,
  3075. struct ocfs2_xattr_bucket *bucket)
  3076. {
  3077. int ret, i;
  3078. size_t end, offset, len, value_len;
  3079. struct ocfs2_xattr_header *xh;
  3080. char *entries, *buf, *bucket_buf = NULL;
  3081. u64 blkno = bucket_blkno(bucket);
  3082. u16 xh_free_start;
  3083. size_t blocksize = inode->i_sb->s_blocksize;
  3084. struct ocfs2_xattr_entry *xe;
  3085. /*
  3086. * In order to make the operation more efficient and generic,
  3087. * we copy all the blocks into a contiguous memory and do the
  3088. * defragment there, so if anything is error, we will not touch
  3089. * the real block.
  3090. */
  3091. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  3092. if (!bucket_buf) {
  3093. ret = -EIO;
  3094. goto out;
  3095. }
  3096. buf = bucket_buf;
  3097. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3098. memcpy(buf, bucket_block(bucket, i), blocksize);
  3099. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  3100. OCFS2_JOURNAL_ACCESS_WRITE);
  3101. if (ret < 0) {
  3102. mlog_errno(ret);
  3103. goto out;
  3104. }
  3105. xh = (struct ocfs2_xattr_header *)bucket_buf;
  3106. entries = (char *)xh->xh_entries;
  3107. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3108. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  3109. "xh_free_start = %u, xh_name_value_len = %u.\n",
  3110. (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
  3111. xh_free_start, le16_to_cpu(xh->xh_name_value_len));
  3112. /*
  3113. * sort all the entries by their offset.
  3114. * the largest will be the first, so that we can
  3115. * move them to the end one by one.
  3116. */
  3117. sort(entries, le16_to_cpu(xh->xh_count),
  3118. sizeof(struct ocfs2_xattr_entry),
  3119. cmp_xe_offset, swap_xe);
  3120. /* Move all name/values to the end of the bucket. */
  3121. xe = xh->xh_entries;
  3122. end = OCFS2_XATTR_BUCKET_SIZE;
  3123. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  3124. offset = le16_to_cpu(xe->xe_name_offset);
  3125. if (ocfs2_xattr_is_local(xe))
  3126. value_len = OCFS2_XATTR_SIZE(
  3127. le64_to_cpu(xe->xe_value_size));
  3128. else
  3129. value_len = OCFS2_XATTR_ROOT_SIZE;
  3130. len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
  3131. /*
  3132. * We must make sure that the name/value pair
  3133. * exist in the same block. So adjust end to
  3134. * the previous block end if needed.
  3135. */
  3136. if (((end - len) / blocksize !=
  3137. (end - 1) / blocksize))
  3138. end = end - end % blocksize;
  3139. if (end > offset + len) {
  3140. memmove(bucket_buf + end - len,
  3141. bucket_buf + offset, len);
  3142. xe->xe_name_offset = cpu_to_le16(end - len);
  3143. }
  3144. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  3145. "bucket %llu\n", (unsigned long long)blkno);
  3146. end -= len;
  3147. }
  3148. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  3149. "bucket %llu\n", (unsigned long long)blkno);
  3150. if (xh_free_start == end)
  3151. goto out;
  3152. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  3153. xh->xh_free_start = cpu_to_le16(end);
  3154. /* sort the entries by their name_hash. */
  3155. sort(entries, le16_to_cpu(xh->xh_count),
  3156. sizeof(struct ocfs2_xattr_entry),
  3157. cmp_xe, swap_xe);
  3158. buf = bucket_buf;
  3159. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3160. memcpy(bucket_block(bucket, i), buf, blocksize);
  3161. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  3162. out:
  3163. kfree(bucket_buf);
  3164. return ret;
  3165. }
  3166. /*
  3167. * prev_blkno points to the start of an existing extent. new_blkno
  3168. * points to a newly allocated extent. Because we know each of our
  3169. * clusters contains more than bucket, we can easily split one cluster
  3170. * at a bucket boundary. So we take the last cluster of the existing
  3171. * extent and split it down the middle. We move the last half of the
  3172. * buckets in the last cluster of the existing extent over to the new
  3173. * extent.
  3174. *
  3175. * first_bh is the buffer at prev_blkno so we can update the existing
  3176. * extent's bucket count. header_bh is the bucket were we were hoping
  3177. * to insert our xattr. If the bucket move places the target in the new
  3178. * extent, we'll update first_bh and header_bh after modifying the old
  3179. * extent.
  3180. *
  3181. * first_hash will be set as the 1st xe's name_hash in the new extent.
  3182. */
  3183. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  3184. handle_t *handle,
  3185. struct ocfs2_xattr_bucket *first,
  3186. struct ocfs2_xattr_bucket *target,
  3187. u64 new_blkno,
  3188. u32 num_clusters,
  3189. u32 *first_hash)
  3190. {
  3191. int ret;
  3192. struct super_block *sb = inode->i_sb;
  3193. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
  3194. int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
  3195. int to_move = num_buckets / 2;
  3196. u64 src_blkno;
  3197. u64 last_cluster_blkno = bucket_blkno(first) +
  3198. ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
  3199. BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
  3200. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
  3201. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  3202. (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
  3203. ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
  3204. last_cluster_blkno, new_blkno,
  3205. to_move, first_hash);
  3206. if (ret) {
  3207. mlog_errno(ret);
  3208. goto out;
  3209. }
  3210. /* This is the first bucket that got moved */
  3211. src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
  3212. /*
  3213. * If the target bucket was part of the moved buckets, we need to
  3214. * update first and target.
  3215. */
  3216. if (bucket_blkno(target) >= src_blkno) {
  3217. /* Find the block for the new target bucket */
  3218. src_blkno = new_blkno +
  3219. (bucket_blkno(target) - src_blkno);
  3220. ocfs2_xattr_bucket_relse(first);
  3221. ocfs2_xattr_bucket_relse(target);
  3222. /*
  3223. * These shouldn't fail - the buffers are in the
  3224. * journal from ocfs2_cp_xattr_bucket().
  3225. */
  3226. ret = ocfs2_read_xattr_bucket(first, new_blkno);
  3227. if (ret) {
  3228. mlog_errno(ret);
  3229. goto out;
  3230. }
  3231. ret = ocfs2_read_xattr_bucket(target, src_blkno);
  3232. if (ret)
  3233. mlog_errno(ret);
  3234. }
  3235. out:
  3236. return ret;
  3237. }
  3238. /*
  3239. * Find the suitable pos when we divide a bucket into 2.
  3240. * We have to make sure the xattrs with the same hash value exist
  3241. * in the same bucket.
  3242. *
  3243. * If this ocfs2_xattr_header covers more than one hash value, find a
  3244. * place where the hash value changes. Try to find the most even split.
  3245. * The most common case is that all entries have different hash values,
  3246. * and the first check we make will find a place to split.
  3247. */
  3248. static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
  3249. {
  3250. struct ocfs2_xattr_entry *entries = xh->xh_entries;
  3251. int count = le16_to_cpu(xh->xh_count);
  3252. int delta, middle = count / 2;
  3253. /*
  3254. * We start at the middle. Each step gets farther away in both
  3255. * directions. We therefore hit the change in hash value
  3256. * nearest to the middle. Note that this loop does not execute for
  3257. * count < 2.
  3258. */
  3259. for (delta = 0; delta < middle; delta++) {
  3260. /* Let's check delta earlier than middle */
  3261. if (cmp_xe(&entries[middle - delta - 1],
  3262. &entries[middle - delta]))
  3263. return middle - delta;
  3264. /* For even counts, don't walk off the end */
  3265. if ((middle + delta + 1) == count)
  3266. continue;
  3267. /* Now try delta past middle */
  3268. if (cmp_xe(&entries[middle + delta],
  3269. &entries[middle + delta + 1]))
  3270. return middle + delta + 1;
  3271. }
  3272. /* Every entry had the same hash */
  3273. return count;
  3274. }
  3275. /*
  3276. * Move some xattrs in old bucket(blk) to new bucket(new_blk).
  3277. * first_hash will record the 1st hash of the new bucket.
  3278. *
  3279. * Normally half of the xattrs will be moved. But we have to make
  3280. * sure that the xattrs with the same hash value are stored in the
  3281. * same bucket. If all the xattrs in this bucket have the same hash
  3282. * value, the new bucket will be initialized as an empty one and the
  3283. * first_hash will be initialized as (hash_value+1).
  3284. */
  3285. static int ocfs2_divide_xattr_bucket(struct inode *inode,
  3286. handle_t *handle,
  3287. u64 blk,
  3288. u64 new_blk,
  3289. u32 *first_hash,
  3290. int new_bucket_head)
  3291. {
  3292. int ret, i;
  3293. int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
  3294. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3295. struct ocfs2_xattr_header *xh;
  3296. struct ocfs2_xattr_entry *xe;
  3297. int blocksize = inode->i_sb->s_blocksize;
  3298. mlog(0, "move some of xattrs from bucket %llu to %llu\n",
  3299. (unsigned long long)blk, (unsigned long long)new_blk);
  3300. s_bucket = ocfs2_xattr_bucket_new(inode);
  3301. t_bucket = ocfs2_xattr_bucket_new(inode);
  3302. if (!s_bucket || !t_bucket) {
  3303. ret = -ENOMEM;
  3304. mlog_errno(ret);
  3305. goto out;
  3306. }
  3307. ret = ocfs2_read_xattr_bucket(s_bucket, blk);
  3308. if (ret) {
  3309. mlog_errno(ret);
  3310. goto out;
  3311. }
  3312. ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
  3313. OCFS2_JOURNAL_ACCESS_WRITE);
  3314. if (ret) {
  3315. mlog_errno(ret);
  3316. goto out;
  3317. }
  3318. /*
  3319. * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
  3320. * there's no need to read it.
  3321. */
  3322. ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
  3323. if (ret) {
  3324. mlog_errno(ret);
  3325. goto out;
  3326. }
  3327. /*
  3328. * Hey, if we're overwriting t_bucket, what difference does
  3329. * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
  3330. * same part of ocfs2_cp_xattr_bucket().
  3331. */
  3332. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3333. new_bucket_head ?
  3334. OCFS2_JOURNAL_ACCESS_CREATE :
  3335. OCFS2_JOURNAL_ACCESS_WRITE);
  3336. if (ret) {
  3337. mlog_errno(ret);
  3338. goto out;
  3339. }
  3340. xh = bucket_xh(s_bucket);
  3341. count = le16_to_cpu(xh->xh_count);
  3342. start = ocfs2_xattr_find_divide_pos(xh);
  3343. if (start == count) {
  3344. xe = &xh->xh_entries[start-1];
  3345. /*
  3346. * initialized a new empty bucket here.
  3347. * The hash value is set as one larger than
  3348. * that of the last entry in the previous bucket.
  3349. */
  3350. for (i = 0; i < t_bucket->bu_blocks; i++)
  3351. memset(bucket_block(t_bucket, i), 0, blocksize);
  3352. xh = bucket_xh(t_bucket);
  3353. xh->xh_free_start = cpu_to_le16(blocksize);
  3354. xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
  3355. le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
  3356. goto set_num_buckets;
  3357. }
  3358. /* copy the whole bucket to the new first. */
  3359. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  3360. /* update the new bucket. */
  3361. xh = bucket_xh(t_bucket);
  3362. /*
  3363. * Calculate the total name/value len and xh_free_start for
  3364. * the old bucket first.
  3365. */
  3366. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  3367. name_value_len = 0;
  3368. for (i = 0; i < start; i++) {
  3369. xe = &xh->xh_entries[i];
  3370. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  3371. if (ocfs2_xattr_is_local(xe))
  3372. xe_len +=
  3373. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3374. else
  3375. xe_len += OCFS2_XATTR_ROOT_SIZE;
  3376. name_value_len += xe_len;
  3377. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  3378. name_offset = le16_to_cpu(xe->xe_name_offset);
  3379. }
  3380. /*
  3381. * Now begin the modification to the new bucket.
  3382. *
  3383. * In the new bucket, We just move the xattr entry to the beginning
  3384. * and don't touch the name/value. So there will be some holes in the
  3385. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  3386. * called.
  3387. */
  3388. xe = &xh->xh_entries[start];
  3389. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  3390. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  3391. (int)((char *)xe - (char *)xh),
  3392. (int)((char *)xh->xh_entries - (char *)xh));
  3393. memmove((char *)xh->xh_entries, (char *)xe, len);
  3394. xe = &xh->xh_entries[count - start];
  3395. len = sizeof(struct ocfs2_xattr_entry) * start;
  3396. memset((char *)xe, 0, len);
  3397. le16_add_cpu(&xh->xh_count, -start);
  3398. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  3399. /* Calculate xh_free_start for the new bucket. */
  3400. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  3401. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  3402. xe = &xh->xh_entries[i];
  3403. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  3404. if (ocfs2_xattr_is_local(xe))
  3405. xe_len +=
  3406. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3407. else
  3408. xe_len += OCFS2_XATTR_ROOT_SIZE;
  3409. if (le16_to_cpu(xe->xe_name_offset) <
  3410. le16_to_cpu(xh->xh_free_start))
  3411. xh->xh_free_start = xe->xe_name_offset;
  3412. }
  3413. set_num_buckets:
  3414. /* set xh->xh_num_buckets for the new xh. */
  3415. if (new_bucket_head)
  3416. xh->xh_num_buckets = cpu_to_le16(1);
  3417. else
  3418. xh->xh_num_buckets = 0;
  3419. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  3420. /* store the first_hash of the new bucket. */
  3421. if (first_hash)
  3422. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3423. /*
  3424. * Now only update the 1st block of the old bucket. If we
  3425. * just added a new empty bucket, there is no need to modify
  3426. * it.
  3427. */
  3428. if (start == count)
  3429. goto out;
  3430. xh = bucket_xh(s_bucket);
  3431. memset(&xh->xh_entries[start], 0,
  3432. sizeof(struct ocfs2_xattr_entry) * (count - start));
  3433. xh->xh_count = cpu_to_le16(start);
  3434. xh->xh_free_start = cpu_to_le16(name_offset);
  3435. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  3436. ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
  3437. out:
  3438. ocfs2_xattr_bucket_free(s_bucket);
  3439. ocfs2_xattr_bucket_free(t_bucket);
  3440. return ret;
  3441. }
  3442. /*
  3443. * Copy xattr from one bucket to another bucket.
  3444. *
  3445. * The caller must make sure that the journal transaction
  3446. * has enough space for journaling.
  3447. */
  3448. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  3449. handle_t *handle,
  3450. u64 s_blkno,
  3451. u64 t_blkno,
  3452. int t_is_new)
  3453. {
  3454. int ret;
  3455. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3456. BUG_ON(s_blkno == t_blkno);
  3457. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  3458. (unsigned long long)s_blkno, (unsigned long long)t_blkno,
  3459. t_is_new);
  3460. s_bucket = ocfs2_xattr_bucket_new(inode);
  3461. t_bucket = ocfs2_xattr_bucket_new(inode);
  3462. if (!s_bucket || !t_bucket) {
  3463. ret = -ENOMEM;
  3464. mlog_errno(ret);
  3465. goto out;
  3466. }
  3467. ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
  3468. if (ret)
  3469. goto out;
  3470. /*
  3471. * Even if !t_is_new, we're overwriting t_bucket. Thus,
  3472. * there's no need to read it.
  3473. */
  3474. ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
  3475. if (ret)
  3476. goto out;
  3477. /*
  3478. * Hey, if we're overwriting t_bucket, what difference does
  3479. * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
  3480. * cluster to fill, we came here from
  3481. * ocfs2_mv_xattr_buckets(), and it is really new -
  3482. * ACCESS_CREATE is required. But we also might have moved data
  3483. * out of t_bucket before extending back into it.
  3484. * ocfs2_add_new_xattr_bucket() can do this - its call to
  3485. * ocfs2_add_new_xattr_cluster() may have created a new extent
  3486. * and copied out the end of the old extent. Then it re-extends
  3487. * the old extent back to create space for new xattrs. That's
  3488. * how we get here, and the bucket isn't really new.
  3489. */
  3490. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3491. t_is_new ?
  3492. OCFS2_JOURNAL_ACCESS_CREATE :
  3493. OCFS2_JOURNAL_ACCESS_WRITE);
  3494. if (ret)
  3495. goto out;
  3496. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  3497. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  3498. out:
  3499. ocfs2_xattr_bucket_free(t_bucket);
  3500. ocfs2_xattr_bucket_free(s_bucket);
  3501. return ret;
  3502. }
  3503. /*
  3504. * src_blk points to the start of an existing extent. last_blk points to
  3505. * last cluster in that extent. to_blk points to a newly allocated
  3506. * extent. We copy the buckets from the cluster at last_blk to the new
  3507. * extent. If start_bucket is non-zero, we skip that many buckets before
  3508. * we start copying. The new extent's xh_num_buckets gets set to the
  3509. * number of buckets we copied. The old extent's xh_num_buckets shrinks
  3510. * by the same amount.
  3511. */
  3512. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  3513. u64 src_blk, u64 last_blk, u64 to_blk,
  3514. unsigned int start_bucket,
  3515. u32 *first_hash)
  3516. {
  3517. int i, ret, credits;
  3518. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3519. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3520. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  3521. struct ocfs2_xattr_bucket *old_first, *new_first;
  3522. mlog(0, "mv xattrs from cluster %llu to %llu\n",
  3523. (unsigned long long)last_blk, (unsigned long long)to_blk);
  3524. BUG_ON(start_bucket >= num_buckets);
  3525. if (start_bucket) {
  3526. num_buckets -= start_bucket;
  3527. last_blk += (start_bucket * blks_per_bucket);
  3528. }
  3529. /* The first bucket of the original extent */
  3530. old_first = ocfs2_xattr_bucket_new(inode);
  3531. /* The first bucket of the new extent */
  3532. new_first = ocfs2_xattr_bucket_new(inode);
  3533. if (!old_first || !new_first) {
  3534. ret = -ENOMEM;
  3535. mlog_errno(ret);
  3536. goto out;
  3537. }
  3538. ret = ocfs2_read_xattr_bucket(old_first, src_blk);
  3539. if (ret) {
  3540. mlog_errno(ret);
  3541. goto out;
  3542. }
  3543. /*
  3544. * We need to update the first bucket of the old extent and all
  3545. * the buckets going to the new extent.
  3546. */
  3547. credits = ((num_buckets + 1) * blks_per_bucket) +
  3548. handle->h_buffer_credits;
  3549. ret = ocfs2_extend_trans(handle, credits);
  3550. if (ret) {
  3551. mlog_errno(ret);
  3552. goto out;
  3553. }
  3554. ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
  3555. OCFS2_JOURNAL_ACCESS_WRITE);
  3556. if (ret) {
  3557. mlog_errno(ret);
  3558. goto out;
  3559. }
  3560. for (i = 0; i < num_buckets; i++) {
  3561. ret = ocfs2_cp_xattr_bucket(inode, handle,
  3562. last_blk + (i * blks_per_bucket),
  3563. to_blk + (i * blks_per_bucket),
  3564. 1);
  3565. if (ret) {
  3566. mlog_errno(ret);
  3567. goto out;
  3568. }
  3569. }
  3570. /*
  3571. * Get the new bucket ready before we dirty anything
  3572. * (This actually shouldn't fail, because we already dirtied
  3573. * it once in ocfs2_cp_xattr_bucket()).
  3574. */
  3575. ret = ocfs2_read_xattr_bucket(new_first, to_blk);
  3576. if (ret) {
  3577. mlog_errno(ret);
  3578. goto out;
  3579. }
  3580. ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
  3581. OCFS2_JOURNAL_ACCESS_WRITE);
  3582. if (ret) {
  3583. mlog_errno(ret);
  3584. goto out;
  3585. }
  3586. /* Now update the headers */
  3587. le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
  3588. ocfs2_xattr_bucket_journal_dirty(handle, old_first);
  3589. bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
  3590. ocfs2_xattr_bucket_journal_dirty(handle, new_first);
  3591. if (first_hash)
  3592. *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
  3593. out:
  3594. ocfs2_xattr_bucket_free(new_first);
  3595. ocfs2_xattr_bucket_free(old_first);
  3596. return ret;
  3597. }
  3598. /*
  3599. * Move some xattrs in this cluster to the new cluster.
  3600. * This function should only be called when bucket size == cluster size.
  3601. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  3602. */
  3603. static int ocfs2_divide_xattr_cluster(struct inode *inode,
  3604. handle_t *handle,
  3605. u64 prev_blk,
  3606. u64 new_blk,
  3607. u32 *first_hash)
  3608. {
  3609. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3610. int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
  3611. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  3612. ret = ocfs2_extend_trans(handle, credits);
  3613. if (ret) {
  3614. mlog_errno(ret);
  3615. return ret;
  3616. }
  3617. /* Move half of the xattr in start_blk to the next bucket. */
  3618. return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
  3619. new_blk, first_hash, 1);
  3620. }
  3621. /*
  3622. * Move some xattrs from the old cluster to the new one since they are not
  3623. * contiguous in ocfs2 xattr tree.
  3624. *
  3625. * new_blk starts a new separate cluster, and we will move some xattrs from
  3626. * prev_blk to it. v_start will be set as the first name hash value in this
  3627. * new cluster so that it can be used as e_cpos during tree insertion and
  3628. * don't collide with our original b-tree operations. first_bh and header_bh
  3629. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  3630. * to extend the insert bucket.
  3631. *
  3632. * The problem is how much xattr should we move to the new one and when should
  3633. * we update first_bh and header_bh?
  3634. * 1. If cluster size > bucket size, that means the previous cluster has more
  3635. * than 1 bucket, so just move half nums of bucket into the new cluster and
  3636. * update the first_bh and header_bh if the insert bucket has been moved
  3637. * to the new cluster.
  3638. * 2. If cluster_size == bucket_size:
  3639. * a) If the previous extent rec has more than one cluster and the insert
  3640. * place isn't in the last cluster, copy the entire last cluster to the
  3641. * new one. This time, we don't need to upate the first_bh and header_bh
  3642. * since they will not be moved into the new cluster.
  3643. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  3644. * the new one. And we set the extend flag to zero if the insert place is
  3645. * moved into the new allocated cluster since no extend is needed.
  3646. */
  3647. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  3648. handle_t *handle,
  3649. struct ocfs2_xattr_bucket *first,
  3650. struct ocfs2_xattr_bucket *target,
  3651. u64 new_blk,
  3652. u32 prev_clusters,
  3653. u32 *v_start,
  3654. int *extend)
  3655. {
  3656. int ret;
  3657. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  3658. (unsigned long long)bucket_blkno(first), prev_clusters,
  3659. (unsigned long long)new_blk);
  3660. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
  3661. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  3662. handle,
  3663. first, target,
  3664. new_blk,
  3665. prev_clusters,
  3666. v_start);
  3667. if (ret)
  3668. mlog_errno(ret);
  3669. } else {
  3670. /* The start of the last cluster in the first extent */
  3671. u64 last_blk = bucket_blkno(first) +
  3672. ((prev_clusters - 1) *
  3673. ocfs2_clusters_to_blocks(inode->i_sb, 1));
  3674. if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
  3675. ret = ocfs2_mv_xattr_buckets(inode, handle,
  3676. bucket_blkno(first),
  3677. last_blk, new_blk, 0,
  3678. v_start);
  3679. if (ret)
  3680. mlog_errno(ret);
  3681. } else {
  3682. ret = ocfs2_divide_xattr_cluster(inode, handle,
  3683. last_blk, new_blk,
  3684. v_start);
  3685. if (ret)
  3686. mlog_errno(ret);
  3687. if ((bucket_blkno(target) == last_blk) && extend)
  3688. *extend = 0;
  3689. }
  3690. }
  3691. return ret;
  3692. }
  3693. /*
  3694. * Add a new cluster for xattr storage.
  3695. *
  3696. * If the new cluster is contiguous with the previous one, it will be
  3697. * appended to the same extent record, and num_clusters will be updated.
  3698. * If not, we will insert a new extent for it and move some xattrs in
  3699. * the last cluster into the new allocated one.
  3700. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  3701. * lose the benefits of hashing because we'll have to search large leaves.
  3702. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  3703. * if it's bigger).
  3704. *
  3705. * first_bh is the first block of the previous extent rec and header_bh
  3706. * indicates the bucket we will insert the new xattrs. They will be updated
  3707. * when the header_bh is moved into the new cluster.
  3708. */
  3709. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  3710. struct buffer_head *root_bh,
  3711. struct ocfs2_xattr_bucket *first,
  3712. struct ocfs2_xattr_bucket *target,
  3713. u32 *num_clusters,
  3714. u32 prev_cpos,
  3715. int *extend,
  3716. struct ocfs2_xattr_set_ctxt *ctxt)
  3717. {
  3718. int ret;
  3719. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3720. u32 prev_clusters = *num_clusters;
  3721. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  3722. u64 block;
  3723. handle_t *handle = ctxt->handle;
  3724. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3725. struct ocfs2_extent_tree et;
  3726. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  3727. "previous xattr blkno = %llu\n",
  3728. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3729. prev_cpos, (unsigned long long)bucket_blkno(first));
  3730. ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
  3731. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  3732. OCFS2_JOURNAL_ACCESS_WRITE);
  3733. if (ret < 0) {
  3734. mlog_errno(ret);
  3735. goto leave;
  3736. }
  3737. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
  3738. clusters_to_add, &bit_off, &num_bits);
  3739. if (ret < 0) {
  3740. if (ret != -ENOSPC)
  3741. mlog_errno(ret);
  3742. goto leave;
  3743. }
  3744. BUG_ON(num_bits > clusters_to_add);
  3745. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  3746. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  3747. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3748. if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
  3749. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  3750. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  3751. /*
  3752. * If this cluster is contiguous with the old one and
  3753. * adding this new cluster, we don't surpass the limit of
  3754. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  3755. * initialized and used like other buckets in the previous
  3756. * cluster.
  3757. * So add it as a contiguous one. The caller will handle
  3758. * its init process.
  3759. */
  3760. v_start = prev_cpos + prev_clusters;
  3761. *num_clusters = prev_clusters + num_bits;
  3762. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  3763. num_bits);
  3764. } else {
  3765. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  3766. handle,
  3767. first,
  3768. target,
  3769. block,
  3770. prev_clusters,
  3771. &v_start,
  3772. extend);
  3773. if (ret) {
  3774. mlog_errno(ret);
  3775. goto leave;
  3776. }
  3777. }
  3778. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  3779. num_bits, (unsigned long long)block, v_start);
  3780. ret = ocfs2_insert_extent(handle, &et, v_start, block,
  3781. num_bits, 0, ctxt->meta_ac);
  3782. if (ret < 0) {
  3783. mlog_errno(ret);
  3784. goto leave;
  3785. }
  3786. ret = ocfs2_journal_dirty(handle, root_bh);
  3787. if (ret < 0)
  3788. mlog_errno(ret);
  3789. leave:
  3790. return ret;
  3791. }
  3792. /*
  3793. * We are given an extent. 'first' is the bucket at the very front of
  3794. * the extent. The extent has space for an additional bucket past
  3795. * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
  3796. * of the target bucket. We wish to shift every bucket past the target
  3797. * down one, filling in that additional space. When we get back to the
  3798. * target, we split the target between itself and the now-empty bucket
  3799. * at target+1 (aka, target_blkno + blks_per_bucket).
  3800. */
  3801. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  3802. handle_t *handle,
  3803. struct ocfs2_xattr_bucket *first,
  3804. u64 target_blk,
  3805. u32 num_clusters)
  3806. {
  3807. int ret, credits;
  3808. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3809. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3810. u64 end_blk;
  3811. u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
  3812. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  3813. "from %llu, len = %u\n", (unsigned long long)target_blk,
  3814. (unsigned long long)bucket_blkno(first), num_clusters);
  3815. /* The extent must have room for an additional bucket */
  3816. BUG_ON(new_bucket >=
  3817. (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
  3818. /* end_blk points to the last existing bucket */
  3819. end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
  3820. /*
  3821. * end_blk is the start of the last existing bucket.
  3822. * Thus, (end_blk - target_blk) covers the target bucket and
  3823. * every bucket after it up to, but not including, the last
  3824. * existing bucket. Then we add the last existing bucket, the
  3825. * new bucket, and the first bucket (3 * blk_per_bucket).
  3826. */
  3827. credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
  3828. handle->h_buffer_credits;
  3829. ret = ocfs2_extend_trans(handle, credits);
  3830. if (ret) {
  3831. mlog_errno(ret);
  3832. goto out;
  3833. }
  3834. ret = ocfs2_xattr_bucket_journal_access(handle, first,
  3835. OCFS2_JOURNAL_ACCESS_WRITE);
  3836. if (ret) {
  3837. mlog_errno(ret);
  3838. goto out;
  3839. }
  3840. while (end_blk != target_blk) {
  3841. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  3842. end_blk + blk_per_bucket, 0);
  3843. if (ret)
  3844. goto out;
  3845. end_blk -= blk_per_bucket;
  3846. }
  3847. /* Move half of the xattr in target_blkno to the next bucket. */
  3848. ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
  3849. target_blk + blk_per_bucket, NULL, 0);
  3850. le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
  3851. ocfs2_xattr_bucket_journal_dirty(handle, first);
  3852. out:
  3853. return ret;
  3854. }
  3855. /*
  3856. * Add new xattr bucket in an extent record and adjust the buckets
  3857. * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
  3858. * bucket we want to insert into.
  3859. *
  3860. * In the easy case, we will move all the buckets after target down by
  3861. * one. Half of target's xattrs will be moved to the next bucket.
  3862. *
  3863. * If current cluster is full, we'll allocate a new one. This may not
  3864. * be contiguous. The underlying calls will make sure that there is
  3865. * space for the insert, shifting buckets around if necessary.
  3866. * 'target' may be moved by those calls.
  3867. */
  3868. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  3869. struct buffer_head *xb_bh,
  3870. struct ocfs2_xattr_bucket *target,
  3871. struct ocfs2_xattr_set_ctxt *ctxt)
  3872. {
  3873. struct ocfs2_xattr_block *xb =
  3874. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3875. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3876. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3877. u32 name_hash =
  3878. le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
  3879. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3880. int ret, num_buckets, extend = 1;
  3881. u64 p_blkno;
  3882. u32 e_cpos, num_clusters;
  3883. /* The bucket at the front of the extent */
  3884. struct ocfs2_xattr_bucket *first;
  3885. mlog(0, "Add new xattr bucket starting from %llu\n",
  3886. (unsigned long long)bucket_blkno(target));
  3887. /* The first bucket of the original extent */
  3888. first = ocfs2_xattr_bucket_new(inode);
  3889. if (!first) {
  3890. ret = -ENOMEM;
  3891. mlog_errno(ret);
  3892. goto out;
  3893. }
  3894. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  3895. &num_clusters, el);
  3896. if (ret) {
  3897. mlog_errno(ret);
  3898. goto out;
  3899. }
  3900. ret = ocfs2_read_xattr_bucket(first, p_blkno);
  3901. if (ret) {
  3902. mlog_errno(ret);
  3903. goto out;
  3904. }
  3905. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  3906. if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
  3907. /*
  3908. * This can move first+target if the target bucket moves
  3909. * to the new extent.
  3910. */
  3911. ret = ocfs2_add_new_xattr_cluster(inode,
  3912. xb_bh,
  3913. first,
  3914. target,
  3915. &num_clusters,
  3916. e_cpos,
  3917. &extend,
  3918. ctxt);
  3919. if (ret) {
  3920. mlog_errno(ret);
  3921. goto out;
  3922. }
  3923. }
  3924. if (extend) {
  3925. ret = ocfs2_extend_xattr_bucket(inode,
  3926. ctxt->handle,
  3927. first,
  3928. bucket_blkno(target),
  3929. num_clusters);
  3930. if (ret)
  3931. mlog_errno(ret);
  3932. }
  3933. out:
  3934. ocfs2_xattr_bucket_free(first);
  3935. return ret;
  3936. }
  3937. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  3938. struct ocfs2_xattr_bucket *bucket,
  3939. int offs)
  3940. {
  3941. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  3942. offs = offs % inode->i_sb->s_blocksize;
  3943. return bucket_block(bucket, block_off) + offs;
  3944. }
  3945. /*
  3946. * Handle the normal xattr set, including replace, delete and new.
  3947. *
  3948. * Note: "local" indicates the real data's locality. So we can't
  3949. * just its bucket locality by its length.
  3950. */
  3951. static void ocfs2_xattr_set_entry_normal(struct inode *inode,
  3952. struct ocfs2_xattr_info *xi,
  3953. struct ocfs2_xattr_search *xs,
  3954. u32 name_hash,
  3955. int local)
  3956. {
  3957. struct ocfs2_xattr_entry *last, *xe;
  3958. int name_len = strlen(xi->name);
  3959. struct ocfs2_xattr_header *xh = xs->header;
  3960. u16 count = le16_to_cpu(xh->xh_count), start;
  3961. size_t blocksize = inode->i_sb->s_blocksize;
  3962. char *val;
  3963. size_t offs, size, new_size;
  3964. last = &xh->xh_entries[count];
  3965. if (!xs->not_found) {
  3966. xe = xs->here;
  3967. offs = le16_to_cpu(xe->xe_name_offset);
  3968. if (ocfs2_xattr_is_local(xe))
  3969. size = OCFS2_XATTR_SIZE(name_len) +
  3970. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3971. else
  3972. size = OCFS2_XATTR_SIZE(name_len) +
  3973. OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3974. /*
  3975. * If the new value will be stored outside, xi->value has been
  3976. * initalized as an empty ocfs2_xattr_value_root, and the same
  3977. * goes with xi->value_len, so we can set new_size safely here.
  3978. * See ocfs2_xattr_set_in_bucket.
  3979. */
  3980. new_size = OCFS2_XATTR_SIZE(name_len) +
  3981. OCFS2_XATTR_SIZE(xi->value_len);
  3982. le16_add_cpu(&xh->xh_name_value_len, -size);
  3983. if (xi->value) {
  3984. if (new_size > size)
  3985. goto set_new_name_value;
  3986. /* Now replace the old value with new one. */
  3987. if (local)
  3988. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3989. else
  3990. xe->xe_value_size = 0;
  3991. val = ocfs2_xattr_bucket_get_val(inode,
  3992. xs->bucket, offs);
  3993. memset(val + OCFS2_XATTR_SIZE(name_len), 0,
  3994. size - OCFS2_XATTR_SIZE(name_len));
  3995. if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
  3996. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  3997. xi->value, xi->value_len);
  3998. le16_add_cpu(&xh->xh_name_value_len, new_size);
  3999. ocfs2_xattr_set_local(xe, local);
  4000. return;
  4001. } else {
  4002. /*
  4003. * Remove the old entry if there is more than one.
  4004. * We don't remove the last entry so that we can
  4005. * use it to indicate the hash value of the empty
  4006. * bucket.
  4007. */
  4008. last -= 1;
  4009. le16_add_cpu(&xh->xh_count, -1);
  4010. if (xh->xh_count) {
  4011. memmove(xe, xe + 1,
  4012. (void *)last - (void *)xe);
  4013. memset(last, 0,
  4014. sizeof(struct ocfs2_xattr_entry));
  4015. } else
  4016. xh->xh_free_start =
  4017. cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  4018. return;
  4019. }
  4020. } else {
  4021. /* find a new entry for insert. */
  4022. int low = 0, high = count - 1, tmp;
  4023. struct ocfs2_xattr_entry *tmp_xe;
  4024. while (low <= high && count) {
  4025. tmp = (low + high) / 2;
  4026. tmp_xe = &xh->xh_entries[tmp];
  4027. if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
  4028. low = tmp + 1;
  4029. else if (name_hash <
  4030. le32_to_cpu(tmp_xe->xe_name_hash))
  4031. high = tmp - 1;
  4032. else {
  4033. low = tmp;
  4034. break;
  4035. }
  4036. }
  4037. xe = &xh->xh_entries[low];
  4038. if (low != count)
  4039. memmove(xe + 1, xe, (void *)last - (void *)xe);
  4040. le16_add_cpu(&xh->xh_count, 1);
  4041. memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
  4042. xe->xe_name_hash = cpu_to_le32(name_hash);
  4043. xe->xe_name_len = name_len;
  4044. ocfs2_xattr_set_type(xe, xi->name_index);
  4045. }
  4046. set_new_name_value:
  4047. /* Insert the new name+value. */
  4048. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
  4049. /*
  4050. * We must make sure that the name/value pair
  4051. * exists in the same block.
  4052. */
  4053. offs = le16_to_cpu(xh->xh_free_start);
  4054. start = offs - size;
  4055. if (start >> inode->i_sb->s_blocksize_bits !=
  4056. (offs - 1) >> inode->i_sb->s_blocksize_bits) {
  4057. offs = offs - offs % blocksize;
  4058. xh->xh_free_start = cpu_to_le16(offs);
  4059. }
  4060. val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
  4061. xe->xe_name_offset = cpu_to_le16(offs - size);
  4062. memset(val, 0, size);
  4063. memcpy(val, xi->name, name_len);
  4064. memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
  4065. xe->xe_value_size = cpu_to_le64(xi->value_len);
  4066. ocfs2_xattr_set_local(xe, local);
  4067. xs->here = xe;
  4068. le16_add_cpu(&xh->xh_free_start, -size);
  4069. le16_add_cpu(&xh->xh_name_value_len, size);
  4070. return;
  4071. }
  4072. /*
  4073. * Set the xattr entry in the specified bucket.
  4074. * The bucket is indicated by xs->bucket and it should have the enough
  4075. * space for the xattr insertion.
  4076. */
  4077. static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
  4078. handle_t *handle,
  4079. struct ocfs2_xattr_info *xi,
  4080. struct ocfs2_xattr_search *xs,
  4081. u32 name_hash,
  4082. int local)
  4083. {
  4084. int ret;
  4085. u64 blkno;
  4086. mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
  4087. (unsigned long)xi->value_len, xi->name_index,
  4088. (unsigned long long)bucket_blkno(xs->bucket));
  4089. if (!xs->bucket->bu_bhs[1]) {
  4090. blkno = bucket_blkno(xs->bucket);
  4091. ocfs2_xattr_bucket_relse(xs->bucket);
  4092. ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
  4093. if (ret) {
  4094. mlog_errno(ret);
  4095. goto out;
  4096. }
  4097. }
  4098. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  4099. OCFS2_JOURNAL_ACCESS_WRITE);
  4100. if (ret < 0) {
  4101. mlog_errno(ret);
  4102. goto out;
  4103. }
  4104. ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
  4105. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  4106. out:
  4107. return ret;
  4108. }
  4109. /*
  4110. * Truncate the specified xe_off entry in xattr bucket.
  4111. * bucket is indicated by header_bh and len is the new length.
  4112. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  4113. *
  4114. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  4115. */
  4116. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  4117. struct ocfs2_xattr_bucket *bucket,
  4118. int xe_off,
  4119. int len,
  4120. struct ocfs2_xattr_set_ctxt *ctxt)
  4121. {
  4122. int ret, offset;
  4123. u64 value_blk;
  4124. struct ocfs2_xattr_entry *xe;
  4125. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4126. size_t blocksize = inode->i_sb->s_blocksize;
  4127. struct ocfs2_xattr_value_buf vb = {
  4128. .vb_access = ocfs2_journal_access,
  4129. };
  4130. xe = &xh->xh_entries[xe_off];
  4131. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  4132. offset = le16_to_cpu(xe->xe_name_offset) +
  4133. OCFS2_XATTR_SIZE(xe->xe_name_len);
  4134. value_blk = offset / blocksize;
  4135. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  4136. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  4137. vb.vb_bh = bucket->bu_bhs[value_blk];
  4138. BUG_ON(!vb.vb_bh);
  4139. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4140. (vb.vb_bh->b_data + offset % blocksize);
  4141. /*
  4142. * From here on out we have to dirty the bucket. The generic
  4143. * value calls only modify one of the bucket's bhs, but we need
  4144. * to send the bucket at once. So if they error, they *could* have
  4145. * modified something. We have to assume they did, and dirty
  4146. * the whole bucket. This leaves us in a consistent state.
  4147. */
  4148. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  4149. xe_off, (unsigned long long)bucket_blkno(bucket), len);
  4150. ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
  4151. if (ret) {
  4152. mlog_errno(ret);
  4153. goto out;
  4154. }
  4155. ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
  4156. OCFS2_JOURNAL_ACCESS_WRITE);
  4157. if (ret) {
  4158. mlog_errno(ret);
  4159. goto out;
  4160. }
  4161. xe->xe_value_size = cpu_to_le64(len);
  4162. ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
  4163. out:
  4164. return ret;
  4165. }
  4166. static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
  4167. struct ocfs2_xattr_search *xs,
  4168. int len,
  4169. struct ocfs2_xattr_set_ctxt *ctxt)
  4170. {
  4171. int ret, offset;
  4172. struct ocfs2_xattr_entry *xe = xs->here;
  4173. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
  4174. BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
  4175. offset = xe - xh->xh_entries;
  4176. ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
  4177. offset, len, ctxt);
  4178. if (ret)
  4179. mlog_errno(ret);
  4180. return ret;
  4181. }
  4182. static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
  4183. handle_t *handle,
  4184. struct ocfs2_xattr_search *xs,
  4185. char *val,
  4186. int value_len)
  4187. {
  4188. int ret, offset, block_off;
  4189. struct ocfs2_xattr_value_root *xv;
  4190. struct ocfs2_xattr_entry *xe = xs->here;
  4191. struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
  4192. void *base;
  4193. struct ocfs2_xattr_value_buf vb = {
  4194. .vb_access = ocfs2_journal_access,
  4195. };
  4196. BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
  4197. ret = ocfs2_xattr_bucket_get_name_value(inode, xh,
  4198. xe - xh->xh_entries,
  4199. &block_off,
  4200. &offset);
  4201. if (ret) {
  4202. mlog_errno(ret);
  4203. goto out;
  4204. }
  4205. base = bucket_block(xs->bucket, block_off);
  4206. xv = (struct ocfs2_xattr_value_root *)(base + offset +
  4207. OCFS2_XATTR_SIZE(xe->xe_name_len));
  4208. vb.vb_xv = xv;
  4209. vb.vb_bh = xs->bucket->bu_bhs[block_off];
  4210. ret = __ocfs2_xattr_set_value_outside(inode, handle,
  4211. &vb, val, value_len);
  4212. if (ret)
  4213. mlog_errno(ret);
  4214. out:
  4215. return ret;
  4216. }
  4217. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  4218. struct buffer_head *root_bh,
  4219. u64 blkno,
  4220. u32 cpos,
  4221. u32 len)
  4222. {
  4223. int ret;
  4224. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4225. struct inode *tl_inode = osb->osb_tl_inode;
  4226. handle_t *handle;
  4227. struct ocfs2_xattr_block *xb =
  4228. (struct ocfs2_xattr_block *)root_bh->b_data;
  4229. struct ocfs2_alloc_context *meta_ac = NULL;
  4230. struct ocfs2_cached_dealloc_ctxt dealloc;
  4231. struct ocfs2_extent_tree et;
  4232. ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
  4233. ocfs2_init_dealloc_ctxt(&dealloc);
  4234. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  4235. cpos, len, (unsigned long long)blkno);
  4236. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
  4237. len);
  4238. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  4239. if (ret) {
  4240. mlog_errno(ret);
  4241. return ret;
  4242. }
  4243. mutex_lock(&tl_inode->i_mutex);
  4244. if (ocfs2_truncate_log_needs_flush(osb)) {
  4245. ret = __ocfs2_flush_truncate_log(osb);
  4246. if (ret < 0) {
  4247. mlog_errno(ret);
  4248. goto out;
  4249. }
  4250. }
  4251. handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
  4252. if (IS_ERR(handle)) {
  4253. ret = -ENOMEM;
  4254. mlog_errno(ret);
  4255. goto out;
  4256. }
  4257. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  4258. OCFS2_JOURNAL_ACCESS_WRITE);
  4259. if (ret) {
  4260. mlog_errno(ret);
  4261. goto out_commit;
  4262. }
  4263. ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
  4264. &dealloc);
  4265. if (ret) {
  4266. mlog_errno(ret);
  4267. goto out_commit;
  4268. }
  4269. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  4270. ret = ocfs2_journal_dirty(handle, root_bh);
  4271. if (ret) {
  4272. mlog_errno(ret);
  4273. goto out_commit;
  4274. }
  4275. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  4276. if (ret)
  4277. mlog_errno(ret);
  4278. out_commit:
  4279. ocfs2_commit_trans(osb, handle);
  4280. out:
  4281. ocfs2_schedule_truncate_log_flush(osb, 1);
  4282. mutex_unlock(&tl_inode->i_mutex);
  4283. if (meta_ac)
  4284. ocfs2_free_alloc_context(meta_ac);
  4285. ocfs2_run_deallocs(osb, &dealloc);
  4286. return ret;
  4287. }
  4288. static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
  4289. handle_t *handle,
  4290. struct ocfs2_xattr_search *xs)
  4291. {
  4292. struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
  4293. struct ocfs2_xattr_entry *last = &xh->xh_entries[
  4294. le16_to_cpu(xh->xh_count) - 1];
  4295. int ret = 0;
  4296. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  4297. OCFS2_JOURNAL_ACCESS_WRITE);
  4298. if (ret) {
  4299. mlog_errno(ret);
  4300. return;
  4301. }
  4302. /* Remove the old entry. */
  4303. memmove(xs->here, xs->here + 1,
  4304. (void *)last - (void *)xs->here);
  4305. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  4306. le16_add_cpu(&xh->xh_count, -1);
  4307. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  4308. }
  4309. /*
  4310. * Set the xattr name/value in the bucket specified in xs.
  4311. *
  4312. * As the new value in xi may be stored in the bucket or in an outside cluster,
  4313. * we divide the whole process into 3 steps:
  4314. * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
  4315. * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
  4316. * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
  4317. * 4. If the clusters for the new outside value can't be allocated, we need
  4318. * to free the xattr we allocated in set.
  4319. */
  4320. static int ocfs2_xattr_set_in_bucket(struct inode *inode,
  4321. struct ocfs2_xattr_info *xi,
  4322. struct ocfs2_xattr_search *xs,
  4323. struct ocfs2_xattr_set_ctxt *ctxt)
  4324. {
  4325. int ret, local = 1;
  4326. size_t value_len;
  4327. char *val = (char *)xi->value;
  4328. struct ocfs2_xattr_entry *xe = xs->here;
  4329. u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
  4330. strlen(xi->name));
  4331. if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
  4332. /*
  4333. * We need to truncate the xattr storage first.
  4334. *
  4335. * If both the old and new value are stored to
  4336. * outside block, we only need to truncate
  4337. * the storage and then set the value outside.
  4338. *
  4339. * If the new value should be stored within block,
  4340. * we should free all the outside block first and
  4341. * the modification to the xattr block will be done
  4342. * by following steps.
  4343. */
  4344. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4345. value_len = xi->value_len;
  4346. else
  4347. value_len = 0;
  4348. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  4349. value_len,
  4350. ctxt);
  4351. if (ret)
  4352. goto out;
  4353. if (value_len)
  4354. goto set_value_outside;
  4355. }
  4356. value_len = xi->value_len;
  4357. /* So we have to handle the inside block change now. */
  4358. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  4359. /*
  4360. * If the new value will be stored outside of block,
  4361. * initalize a new empty value root and insert it first.
  4362. */
  4363. local = 0;
  4364. xi->value = &def_xv;
  4365. xi->value_len = OCFS2_XATTR_ROOT_SIZE;
  4366. }
  4367. ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
  4368. name_hash, local);
  4369. if (ret) {
  4370. mlog_errno(ret);
  4371. goto out;
  4372. }
  4373. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  4374. goto out;
  4375. /* allocate the space now for the outside block storage. */
  4376. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  4377. value_len, ctxt);
  4378. if (ret) {
  4379. mlog_errno(ret);
  4380. if (xs->not_found) {
  4381. /*
  4382. * We can't allocate enough clusters for outside
  4383. * storage and we have allocated xattr already,
  4384. * so need to remove it.
  4385. */
  4386. ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
  4387. }
  4388. goto out;
  4389. }
  4390. set_value_outside:
  4391. ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
  4392. xs, val, value_len);
  4393. out:
  4394. return ret;
  4395. }
  4396. /*
  4397. * check whether the xattr bucket is filled up with the same hash value.
  4398. * If we want to insert the xattr with the same hash, return -ENOSPC.
  4399. * If we want to insert a xattr with different hash value, go ahead
  4400. * and ocfs2_divide_xattr_bucket will handle this.
  4401. */
  4402. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  4403. struct ocfs2_xattr_bucket *bucket,
  4404. const char *name)
  4405. {
  4406. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4407. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  4408. if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
  4409. return 0;
  4410. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  4411. xh->xh_entries[0].xe_name_hash) {
  4412. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  4413. "hash = %u\n",
  4414. (unsigned long long)bucket_blkno(bucket),
  4415. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  4416. return -ENOSPC;
  4417. }
  4418. return 0;
  4419. }
  4420. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  4421. struct ocfs2_xattr_info *xi,
  4422. struct ocfs2_xattr_search *xs,
  4423. struct ocfs2_xattr_set_ctxt *ctxt)
  4424. {
  4425. struct ocfs2_xattr_header *xh;
  4426. struct ocfs2_xattr_entry *xe;
  4427. u16 count, header_size, xh_free_start;
  4428. int free, max_free, need, old;
  4429. size_t value_size = 0, name_len = strlen(xi->name);
  4430. size_t blocksize = inode->i_sb->s_blocksize;
  4431. int ret, allocation = 0;
  4432. mlog_entry("Set xattr %s in xattr index block\n", xi->name);
  4433. try_again:
  4434. xh = xs->header;
  4435. count = le16_to_cpu(xh->xh_count);
  4436. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4437. header_size = sizeof(struct ocfs2_xattr_header) +
  4438. count * sizeof(struct ocfs2_xattr_entry);
  4439. max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
  4440. le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
  4441. mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
  4442. "of %u which exceed block size\n",
  4443. (unsigned long long)bucket_blkno(xs->bucket),
  4444. header_size);
  4445. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4446. value_size = OCFS2_XATTR_ROOT_SIZE;
  4447. else if (xi->value)
  4448. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  4449. if (xs->not_found)
  4450. need = sizeof(struct ocfs2_xattr_entry) +
  4451. OCFS2_XATTR_SIZE(name_len) + value_size;
  4452. else {
  4453. need = value_size + OCFS2_XATTR_SIZE(name_len);
  4454. /*
  4455. * We only replace the old value if the new length is smaller
  4456. * than the old one. Otherwise we will allocate new space in the
  4457. * bucket to store it.
  4458. */
  4459. xe = xs->here;
  4460. if (ocfs2_xattr_is_local(xe))
  4461. old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  4462. else
  4463. old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  4464. if (old >= value_size)
  4465. need = 0;
  4466. }
  4467. free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
  4468. /*
  4469. * We need to make sure the new name/value pair
  4470. * can exist in the same block.
  4471. */
  4472. if (xh_free_start % blocksize < need)
  4473. free -= xh_free_start % blocksize;
  4474. mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
  4475. "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
  4476. " %u\n", xs->not_found,
  4477. (unsigned long long)bucket_blkno(xs->bucket),
  4478. free, need, max_free, le16_to_cpu(xh->xh_free_start),
  4479. le16_to_cpu(xh->xh_name_value_len));
  4480. if (free < need ||
  4481. (xs->not_found &&
  4482. count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
  4483. if (need <= max_free &&
  4484. count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  4485. /*
  4486. * We can create the space by defragment. Since only the
  4487. * name/value will be moved, the xe shouldn't be changed
  4488. * in xs.
  4489. */
  4490. ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
  4491. xs->bucket);
  4492. if (ret) {
  4493. mlog_errno(ret);
  4494. goto out;
  4495. }
  4496. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4497. free = xh_free_start - header_size
  4498. - OCFS2_XATTR_HEADER_GAP;
  4499. if (xh_free_start % blocksize < need)
  4500. free -= xh_free_start % blocksize;
  4501. if (free >= need)
  4502. goto xattr_set;
  4503. mlog(0, "Can't get enough space for xattr insert by "
  4504. "defragment. Need %u bytes, but we have %d, so "
  4505. "allocate new bucket for it.\n", need, free);
  4506. }
  4507. /*
  4508. * We have to add new buckets or clusters and one
  4509. * allocation should leave us enough space for insert.
  4510. */
  4511. BUG_ON(allocation);
  4512. /*
  4513. * We do not allow for overlapping ranges between buckets. And
  4514. * the maximum number of collisions we will allow for then is
  4515. * one bucket's worth, so check it here whether we need to
  4516. * add a new bucket for the insert.
  4517. */
  4518. ret = ocfs2_check_xattr_bucket_collision(inode,
  4519. xs->bucket,
  4520. xi->name);
  4521. if (ret) {
  4522. mlog_errno(ret);
  4523. goto out;
  4524. }
  4525. ret = ocfs2_add_new_xattr_bucket(inode,
  4526. xs->xattr_bh,
  4527. xs->bucket,
  4528. ctxt);
  4529. if (ret) {
  4530. mlog_errno(ret);
  4531. goto out;
  4532. }
  4533. /*
  4534. * ocfs2_add_new_xattr_bucket() will have updated
  4535. * xs->bucket if it moved, but it will not have updated
  4536. * any of the other search fields. Thus, we drop it and
  4537. * re-search. Everything should be cached, so it'll be
  4538. * quick.
  4539. */
  4540. ocfs2_xattr_bucket_relse(xs->bucket);
  4541. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  4542. xi->name_index,
  4543. xi->name, xs);
  4544. if (ret && ret != -ENODATA)
  4545. goto out;
  4546. xs->not_found = ret;
  4547. allocation = 1;
  4548. goto try_again;
  4549. }
  4550. xattr_set:
  4551. ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
  4552. out:
  4553. mlog_exit(ret);
  4554. return ret;
  4555. }
  4556. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4557. struct ocfs2_xattr_bucket *bucket,
  4558. void *para)
  4559. {
  4560. int ret = 0;
  4561. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4562. u16 i;
  4563. struct ocfs2_xattr_entry *xe;
  4564. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4565. struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
  4566. int credits = ocfs2_remove_extent_credits(osb->sb) +
  4567. ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4568. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  4569. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4570. xe = &xh->xh_entries[i];
  4571. if (ocfs2_xattr_is_local(xe))
  4572. continue;
  4573. ctxt.handle = ocfs2_start_trans(osb, credits);
  4574. if (IS_ERR(ctxt.handle)) {
  4575. ret = PTR_ERR(ctxt.handle);
  4576. mlog_errno(ret);
  4577. break;
  4578. }
  4579. ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
  4580. i, 0, &ctxt);
  4581. ocfs2_commit_trans(osb, ctxt.handle);
  4582. if (ret) {
  4583. mlog_errno(ret);
  4584. break;
  4585. }
  4586. }
  4587. ocfs2_schedule_truncate_log_flush(osb, 1);
  4588. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  4589. return ret;
  4590. }
  4591. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  4592. struct buffer_head *xb_bh)
  4593. {
  4594. struct ocfs2_xattr_block *xb =
  4595. (struct ocfs2_xattr_block *)xb_bh->b_data;
  4596. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  4597. int ret = 0;
  4598. u32 name_hash = UINT_MAX, e_cpos, num_clusters;
  4599. u64 p_blkno;
  4600. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4601. return 0;
  4602. while (name_hash > 0) {
  4603. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  4604. &e_cpos, &num_clusters, el);
  4605. if (ret) {
  4606. mlog_errno(ret);
  4607. goto out;
  4608. }
  4609. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  4610. ocfs2_delete_xattr_in_bucket,
  4611. NULL);
  4612. if (ret) {
  4613. mlog_errno(ret);
  4614. goto out;
  4615. }
  4616. ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
  4617. p_blkno, e_cpos, num_clusters);
  4618. if (ret) {
  4619. mlog_errno(ret);
  4620. break;
  4621. }
  4622. if (e_cpos == 0)
  4623. break;
  4624. name_hash = e_cpos - 1;
  4625. }
  4626. out:
  4627. return ret;
  4628. }
  4629. /*
  4630. * Whenever we modify a xattr value root in the bucket(e.g, CoW
  4631. * or change the extent record flag), we need to recalculate
  4632. * the metaecc for the whole bucket. So it is done here.
  4633. *
  4634. * Note:
  4635. * We have to give the extra credits for the caller.
  4636. */
  4637. static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
  4638. handle_t *handle,
  4639. void *para)
  4640. {
  4641. int ret;
  4642. struct ocfs2_xattr_bucket *bucket =
  4643. (struct ocfs2_xattr_bucket *)para;
  4644. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  4645. OCFS2_JOURNAL_ACCESS_WRITE);
  4646. if (ret) {
  4647. mlog_errno(ret);
  4648. return ret;
  4649. }
  4650. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  4651. return 0;
  4652. }
  4653. /*
  4654. * Special action we need if the xattr value is refcounted.
  4655. *
  4656. * 1. If the xattr is refcounted, lock the tree.
  4657. * 2. CoW the xattr if we are setting the new value and the value
  4658. * will be stored outside.
  4659. * 3. In other case, decrease_refcount will work for us, so just
  4660. * lock the refcount tree, calculate the meta and credits is OK.
  4661. *
  4662. * We have to do CoW before ocfs2_init_xattr_set_ctxt since
  4663. * currently CoW is a completed transaction, while this function
  4664. * will also lock the allocators and let us deadlock. So we will
  4665. * CoW the whole xattr value.
  4666. */
  4667. static int ocfs2_prepare_refcount_xattr(struct inode *inode,
  4668. struct ocfs2_dinode *di,
  4669. struct ocfs2_xattr_info *xi,
  4670. struct ocfs2_xattr_search *xis,
  4671. struct ocfs2_xattr_search *xbs,
  4672. struct ocfs2_refcount_tree **ref_tree,
  4673. int *meta_add,
  4674. int *credits)
  4675. {
  4676. int ret = 0;
  4677. struct ocfs2_xattr_block *xb;
  4678. struct ocfs2_xattr_entry *xe;
  4679. char *base;
  4680. u32 p_cluster, num_clusters;
  4681. unsigned int ext_flags;
  4682. int name_offset, name_len;
  4683. struct ocfs2_xattr_value_buf vb;
  4684. struct ocfs2_xattr_bucket *bucket = NULL;
  4685. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4686. struct ocfs2_post_refcount refcount;
  4687. struct ocfs2_post_refcount *p = NULL;
  4688. struct buffer_head *ref_root_bh = NULL;
  4689. if (!xis->not_found) {
  4690. xe = xis->here;
  4691. name_offset = le16_to_cpu(xe->xe_name_offset);
  4692. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  4693. base = xis->base;
  4694. vb.vb_bh = xis->inode_bh;
  4695. vb.vb_access = ocfs2_journal_access_di;
  4696. } else {
  4697. int i, block_off = 0;
  4698. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  4699. xe = xbs->here;
  4700. name_offset = le16_to_cpu(xe->xe_name_offset);
  4701. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  4702. i = xbs->here - xbs->header->xh_entries;
  4703. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  4704. ret = ocfs2_xattr_bucket_get_name_value(inode,
  4705. bucket_xh(xbs->bucket),
  4706. i, &block_off,
  4707. &name_offset);
  4708. if (ret) {
  4709. mlog_errno(ret);
  4710. goto out;
  4711. }
  4712. base = bucket_block(xbs->bucket, block_off);
  4713. vb.vb_bh = xbs->bucket->bu_bhs[block_off];
  4714. vb.vb_access = ocfs2_journal_access;
  4715. if (ocfs2_meta_ecc(osb)) {
  4716. /*create parameters for ocfs2_post_refcount. */
  4717. bucket = xbs->bucket;
  4718. refcount.credits = bucket->bu_blocks;
  4719. refcount.para = bucket;
  4720. refcount.func =
  4721. ocfs2_xattr_bucket_post_refcount;
  4722. p = &refcount;
  4723. }
  4724. } else {
  4725. base = xbs->base;
  4726. vb.vb_bh = xbs->xattr_bh;
  4727. vb.vb_access = ocfs2_journal_access_xb;
  4728. }
  4729. }
  4730. if (ocfs2_xattr_is_local(xe))
  4731. goto out;
  4732. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4733. (base + name_offset + name_len);
  4734. ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
  4735. &num_clusters, &vb.vb_xv->xr_list,
  4736. &ext_flags);
  4737. if (ret) {
  4738. mlog_errno(ret);
  4739. goto out;
  4740. }
  4741. /*
  4742. * We just need to check the 1st extent record, since we always
  4743. * CoW the whole xattr. So there shouldn't be a xattr with
  4744. * some REFCOUNT extent recs after the 1st one.
  4745. */
  4746. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  4747. goto out;
  4748. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  4749. 1, ref_tree, &ref_root_bh);
  4750. if (ret) {
  4751. mlog_errno(ret);
  4752. goto out;
  4753. }
  4754. /*
  4755. * If we are deleting the xattr or the new size will be stored inside,
  4756. * cool, leave it there, the xattr truncate process will remove them
  4757. * for us(it still needs the refcount tree lock and the meta, credits).
  4758. * And the worse case is that every cluster truncate will split the
  4759. * refcount tree, and make the original extent become 3. So we will need
  4760. * 2 * cluster more extent recs at most.
  4761. */
  4762. if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
  4763. ret = ocfs2_refcounted_xattr_delete_need(inode,
  4764. &(*ref_tree)->rf_ci,
  4765. ref_root_bh, vb.vb_xv,
  4766. meta_add, credits);
  4767. if (ret)
  4768. mlog_errno(ret);
  4769. goto out;
  4770. }
  4771. ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
  4772. *ref_tree, ref_root_bh, 0,
  4773. le32_to_cpu(vb.vb_xv->xr_clusters), p);
  4774. if (ret)
  4775. mlog_errno(ret);
  4776. out:
  4777. brelse(ref_root_bh);
  4778. return ret;
  4779. }
  4780. /*
  4781. * 'security' attributes support
  4782. */
  4783. static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
  4784. size_t list_size, const char *name,
  4785. size_t name_len)
  4786. {
  4787. const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
  4788. const size_t total_len = prefix_len + name_len + 1;
  4789. if (list && total_len <= list_size) {
  4790. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  4791. memcpy(list + prefix_len, name, name_len);
  4792. list[prefix_len + name_len] = '\0';
  4793. }
  4794. return total_len;
  4795. }
  4796. static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
  4797. void *buffer, size_t size)
  4798. {
  4799. if (strcmp(name, "") == 0)
  4800. return -EINVAL;
  4801. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
  4802. buffer, size);
  4803. }
  4804. static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
  4805. const void *value, size_t size, int flags)
  4806. {
  4807. if (strcmp(name, "") == 0)
  4808. return -EINVAL;
  4809. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
  4810. size, flags);
  4811. }
  4812. int ocfs2_init_security_get(struct inode *inode,
  4813. struct inode *dir,
  4814. struct ocfs2_security_xattr_info *si)
  4815. {
  4816. /* check whether ocfs2 support feature xattr */
  4817. if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
  4818. return -EOPNOTSUPP;
  4819. return security_inode_init_security(inode, dir, &si->name, &si->value,
  4820. &si->value_len);
  4821. }
  4822. int ocfs2_init_security_set(handle_t *handle,
  4823. struct inode *inode,
  4824. struct buffer_head *di_bh,
  4825. struct ocfs2_security_xattr_info *si,
  4826. struct ocfs2_alloc_context *xattr_ac,
  4827. struct ocfs2_alloc_context *data_ac)
  4828. {
  4829. return ocfs2_xattr_set_handle(handle, inode, di_bh,
  4830. OCFS2_XATTR_INDEX_SECURITY,
  4831. si->name, si->value, si->value_len, 0,
  4832. xattr_ac, data_ac);
  4833. }
  4834. struct xattr_handler ocfs2_xattr_security_handler = {
  4835. .prefix = XATTR_SECURITY_PREFIX,
  4836. .list = ocfs2_xattr_security_list,
  4837. .get = ocfs2_xattr_security_get,
  4838. .set = ocfs2_xattr_security_set,
  4839. };
  4840. /*
  4841. * 'trusted' attributes support
  4842. */
  4843. static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
  4844. size_t list_size, const char *name,
  4845. size_t name_len)
  4846. {
  4847. const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  4848. const size_t total_len = prefix_len + name_len + 1;
  4849. if (list && total_len <= list_size) {
  4850. memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
  4851. memcpy(list + prefix_len, name, name_len);
  4852. list[prefix_len + name_len] = '\0';
  4853. }
  4854. return total_len;
  4855. }
  4856. static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
  4857. void *buffer, size_t size)
  4858. {
  4859. if (strcmp(name, "") == 0)
  4860. return -EINVAL;
  4861. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
  4862. buffer, size);
  4863. }
  4864. static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
  4865. const void *value, size_t size, int flags)
  4866. {
  4867. if (strcmp(name, "") == 0)
  4868. return -EINVAL;
  4869. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
  4870. size, flags);
  4871. }
  4872. struct xattr_handler ocfs2_xattr_trusted_handler = {
  4873. .prefix = XATTR_TRUSTED_PREFIX,
  4874. .list = ocfs2_xattr_trusted_list,
  4875. .get = ocfs2_xattr_trusted_get,
  4876. .set = ocfs2_xattr_trusted_set,
  4877. };
  4878. /*
  4879. * 'user' attributes support
  4880. */
  4881. static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
  4882. size_t list_size, const char *name,
  4883. size_t name_len)
  4884. {
  4885. const size_t prefix_len = XATTR_USER_PREFIX_LEN;
  4886. const size_t total_len = prefix_len + name_len + 1;
  4887. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4888. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4889. return 0;
  4890. if (list && total_len <= list_size) {
  4891. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  4892. memcpy(list + prefix_len, name, name_len);
  4893. list[prefix_len + name_len] = '\0';
  4894. }
  4895. return total_len;
  4896. }
  4897. static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
  4898. void *buffer, size_t size)
  4899. {
  4900. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4901. if (strcmp(name, "") == 0)
  4902. return -EINVAL;
  4903. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4904. return -EOPNOTSUPP;
  4905. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
  4906. buffer, size);
  4907. }
  4908. static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
  4909. const void *value, size_t size, int flags)
  4910. {
  4911. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4912. if (strcmp(name, "") == 0)
  4913. return -EINVAL;
  4914. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4915. return -EOPNOTSUPP;
  4916. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
  4917. size, flags);
  4918. }
  4919. struct xattr_handler ocfs2_xattr_user_handler = {
  4920. .prefix = XATTR_USER_PREFIX,
  4921. .list = ocfs2_xattr_user_list,
  4922. .get = ocfs2_xattr_user_get,
  4923. .set = ocfs2_xattr_user_set,
  4924. };