xattr.c 142 KB

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