xattr.c 122 KB

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