xattr.c 141 KB

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