xattr.c 135 KB

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