xattr.c 141 KB

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