xattr.c 122 KB

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