xattr.c 128 KB

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