xattr.c 139 KB

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