xattr.c 120 KB

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