dir.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dir.c
  5. *
  6. * Creates, reads, walks and deletes directory-nodes
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * Portions of this code from linux/fs/ext3/dir.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/dir.c
  20. *
  21. * Copyright (C) 1991, 1992 Linux Torvalds
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2 of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public
  34. * License along with this program; if not, write to the
  35. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  36. * Boston, MA 021110-1307, USA.
  37. */
  38. #include <linux/fs.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/highmem.h>
  42. #include <linux/quotaops.h>
  43. #include <linux/sort.h>
  44. #define MLOG_MASK_PREFIX ML_NAMEI
  45. #include <cluster/masklog.h>
  46. #include "ocfs2.h"
  47. #include "alloc.h"
  48. #include "blockcheck.h"
  49. #include "dir.h"
  50. #include "dlmglue.h"
  51. #include "extent_map.h"
  52. #include "file.h"
  53. #include "inode.h"
  54. #include "journal.h"
  55. #include "namei.h"
  56. #include "suballoc.h"
  57. #include "super.h"
  58. #include "sysfile.h"
  59. #include "uptodate.h"
  60. #include "buffer_head_io.h"
  61. #define NAMEI_RA_CHUNKS 2
  62. #define NAMEI_RA_BLOCKS 4
  63. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  64. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  65. static unsigned char ocfs2_filetype_table[] = {
  66. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  67. };
  68. static int ocfs2_do_extend_dir(struct super_block *sb,
  69. handle_t *handle,
  70. struct inode *dir,
  71. struct buffer_head *parent_fe_bh,
  72. struct ocfs2_alloc_context *data_ac,
  73. struct ocfs2_alloc_context *meta_ac,
  74. struct buffer_head **new_bh);
  75. static int ocfs2_dir_indexed(struct inode *inode);
  76. /*
  77. * These are distinct checks because future versions of the file system will
  78. * want to have a trailing dirent structure independent of indexing.
  79. */
  80. static int ocfs2_supports_dir_trailer(struct inode *dir)
  81. {
  82. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  83. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  84. return 0;
  85. return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
  86. }
  87. /*
  88. * "new' here refers to the point at which we're creating a new
  89. * directory via "mkdir()", but also when we're expanding an inline
  90. * directory. In either case, we don't yet have the indexing bit set
  91. * on the directory, so the standard checks will fail in when metaecc
  92. * is turned off. Only directory-initialization type functions should
  93. * use this then. Everything else wants ocfs2_supports_dir_trailer()
  94. */
  95. static int ocfs2_new_dir_wants_trailer(struct inode *dir)
  96. {
  97. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  98. return ocfs2_meta_ecc(osb) ||
  99. ocfs2_supports_indexed_dirs(osb);
  100. }
  101. static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
  102. {
  103. return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
  104. }
  105. #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
  106. /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
  107. * them more consistent? */
  108. struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
  109. void *data)
  110. {
  111. char *p = data;
  112. p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
  113. return (struct ocfs2_dir_block_trailer *)p;
  114. }
  115. /*
  116. * XXX: This is executed once on every dirent. We should consider optimizing
  117. * it.
  118. */
  119. static int ocfs2_skip_dir_trailer(struct inode *dir,
  120. struct ocfs2_dir_entry *de,
  121. unsigned long offset,
  122. unsigned long blklen)
  123. {
  124. unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
  125. if (!ocfs2_supports_dir_trailer(dir))
  126. return 0;
  127. if (offset != toff)
  128. return 0;
  129. return 1;
  130. }
  131. static void ocfs2_init_dir_trailer(struct inode *inode,
  132. struct buffer_head *bh, u16 rec_len)
  133. {
  134. struct ocfs2_dir_block_trailer *trailer;
  135. trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
  136. strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
  137. trailer->db_compat_rec_len =
  138. cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
  139. trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  140. trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
  141. trailer->db_free_rec_len = cpu_to_le16(rec_len);
  142. }
  143. /*
  144. * Link an unindexed block with a dir trailer structure into the index free
  145. * list. This function will modify dirdata_bh, but assumes you've already
  146. * passed it to the journal.
  147. */
  148. static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
  149. struct buffer_head *dx_root_bh,
  150. struct buffer_head *dirdata_bh)
  151. {
  152. int ret;
  153. struct ocfs2_dx_root_block *dx_root;
  154. struct ocfs2_dir_block_trailer *trailer;
  155. ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
  156. OCFS2_JOURNAL_ACCESS_WRITE);
  157. if (ret) {
  158. mlog_errno(ret);
  159. goto out;
  160. }
  161. trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  162. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  163. trailer->db_free_next = dx_root->dr_free_blk;
  164. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  165. ocfs2_journal_dirty(handle, dx_root_bh);
  166. out:
  167. return ret;
  168. }
  169. static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
  170. {
  171. return res->dl_prev_leaf_bh == NULL;
  172. }
  173. void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
  174. {
  175. brelse(res->dl_dx_root_bh);
  176. brelse(res->dl_leaf_bh);
  177. brelse(res->dl_dx_leaf_bh);
  178. brelse(res->dl_prev_leaf_bh);
  179. }
  180. static int ocfs2_dir_indexed(struct inode *inode)
  181. {
  182. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
  183. return 1;
  184. return 0;
  185. }
  186. static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
  187. {
  188. return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
  189. }
  190. /*
  191. * Hashing code adapted from ext3
  192. */
  193. #define DELTA 0x9E3779B9
  194. static void TEA_transform(__u32 buf[4], __u32 const in[])
  195. {
  196. __u32 sum = 0;
  197. __u32 b0 = buf[0], b1 = buf[1];
  198. __u32 a = in[0], b = in[1], c = in[2], d = in[3];
  199. int n = 16;
  200. do {
  201. sum += DELTA;
  202. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  203. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  204. } while (--n);
  205. buf[0] += b0;
  206. buf[1] += b1;
  207. }
  208. static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
  209. {
  210. __u32 pad, val;
  211. int i;
  212. pad = (__u32)len | ((__u32)len << 8);
  213. pad |= pad << 16;
  214. val = pad;
  215. if (len > num*4)
  216. len = num * 4;
  217. for (i = 0; i < len; i++) {
  218. if ((i % 4) == 0)
  219. val = pad;
  220. val = msg[i] + (val << 8);
  221. if ((i % 4) == 3) {
  222. *buf++ = val;
  223. val = pad;
  224. num--;
  225. }
  226. }
  227. if (--num >= 0)
  228. *buf++ = val;
  229. while (--num >= 0)
  230. *buf++ = pad;
  231. }
  232. static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  233. struct ocfs2_dx_hinfo *hinfo)
  234. {
  235. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  236. const char *p;
  237. __u32 in[8], buf[4];
  238. /*
  239. * XXX: Is this really necessary, if the index is never looked
  240. * at by readdir? Is a hash value of '0' a bad idea?
  241. */
  242. if ((len == 1 && !strncmp(".", name, 1)) ||
  243. (len == 2 && !strncmp("..", name, 2))) {
  244. buf[0] = buf[1] = 0;
  245. goto out;
  246. }
  247. #ifdef OCFS2_DEBUG_DX_DIRS
  248. /*
  249. * This makes it very easy to debug indexing problems. We
  250. * should never allow this to be selected without hand editing
  251. * this file though.
  252. */
  253. buf[0] = buf[1] = len;
  254. goto out;
  255. #endif
  256. memcpy(buf, osb->osb_dx_seed, sizeof(buf));
  257. p = name;
  258. while (len > 0) {
  259. str2hashbuf(p, len, in, 4);
  260. TEA_transform(buf, in);
  261. len -= 16;
  262. p += 16;
  263. }
  264. out:
  265. hinfo->major_hash = buf[0];
  266. hinfo->minor_hash = buf[1];
  267. }
  268. /*
  269. * bh passed here can be an inode block or a dir data block, depending
  270. * on the inode inline data flag.
  271. */
  272. static int ocfs2_check_dir_entry(struct inode * dir,
  273. struct ocfs2_dir_entry * de,
  274. struct buffer_head * bh,
  275. unsigned long offset)
  276. {
  277. const char *error_msg = NULL;
  278. const int rlen = le16_to_cpu(de->rec_len);
  279. if (rlen < OCFS2_DIR_REC_LEN(1))
  280. error_msg = "rec_len is smaller than minimal";
  281. else if (rlen % 4 != 0)
  282. error_msg = "rec_len % 4 != 0";
  283. else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
  284. error_msg = "rec_len is too small for name_len";
  285. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  286. error_msg = "directory entry across blocks";
  287. if (error_msg != NULL)
  288. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  289. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  290. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  291. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  292. de->name_len);
  293. return error_msg == NULL ? 1 : 0;
  294. }
  295. static inline int ocfs2_match(int len,
  296. const char * const name,
  297. struct ocfs2_dir_entry *de)
  298. {
  299. if (len != de->name_len)
  300. return 0;
  301. if (!de->inode)
  302. return 0;
  303. return !memcmp(name, de->name, len);
  304. }
  305. /*
  306. * Returns 0 if not found, -1 on failure, and 1 on success
  307. */
  308. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  309. struct inode *dir,
  310. const char *name, int namelen,
  311. unsigned long offset,
  312. char *first_de,
  313. unsigned int bytes,
  314. struct ocfs2_dir_entry **res_dir)
  315. {
  316. struct ocfs2_dir_entry *de;
  317. char *dlimit, *de_buf;
  318. int de_len;
  319. int ret = 0;
  320. mlog_entry_void();
  321. de_buf = first_de;
  322. dlimit = de_buf + bytes;
  323. while (de_buf < dlimit) {
  324. /* this code is executed quadratically often */
  325. /* do minimal checking `by hand' */
  326. de = (struct ocfs2_dir_entry *) de_buf;
  327. if (de_buf + namelen <= dlimit &&
  328. ocfs2_match(namelen, name, de)) {
  329. /* found a match - just to be sure, do a full check */
  330. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  331. ret = -1;
  332. goto bail;
  333. }
  334. *res_dir = de;
  335. ret = 1;
  336. goto bail;
  337. }
  338. /* prevent looping on a bad block */
  339. de_len = le16_to_cpu(de->rec_len);
  340. if (de_len <= 0) {
  341. ret = -1;
  342. goto bail;
  343. }
  344. de_buf += de_len;
  345. offset += de_len;
  346. }
  347. bail:
  348. mlog_exit(ret);
  349. return ret;
  350. }
  351. static struct buffer_head *ocfs2_find_entry_id(const char *name,
  352. int namelen,
  353. struct inode *dir,
  354. struct ocfs2_dir_entry **res_dir)
  355. {
  356. int ret, found;
  357. struct buffer_head *di_bh = NULL;
  358. struct ocfs2_dinode *di;
  359. struct ocfs2_inline_data *data;
  360. ret = ocfs2_read_inode_block(dir, &di_bh);
  361. if (ret) {
  362. mlog_errno(ret);
  363. goto out;
  364. }
  365. di = (struct ocfs2_dinode *)di_bh->b_data;
  366. data = &di->id2.i_data;
  367. found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
  368. data->id_data, i_size_read(dir), res_dir);
  369. if (found == 1)
  370. return di_bh;
  371. brelse(di_bh);
  372. out:
  373. return NULL;
  374. }
  375. static int ocfs2_validate_dir_block(struct super_block *sb,
  376. struct buffer_head *bh)
  377. {
  378. int rc;
  379. struct ocfs2_dir_block_trailer *trailer =
  380. ocfs2_trailer_from_bh(bh, sb);
  381. /*
  382. * We don't validate dirents here, that's handled
  383. * in-place when the code walks them.
  384. */
  385. mlog(0, "Validating dirblock %llu\n",
  386. (unsigned long long)bh->b_blocknr);
  387. BUG_ON(!buffer_uptodate(bh));
  388. /*
  389. * If the ecc fails, we return the error but otherwise
  390. * leave the filesystem running. We know any error is
  391. * local to this block.
  392. *
  393. * Note that we are safe to call this even if the directory
  394. * doesn't have a trailer. Filesystems without metaecc will do
  395. * nothing, and filesystems with it will have one.
  396. */
  397. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
  398. if (rc)
  399. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  400. (unsigned long long)bh->b_blocknr);
  401. return rc;
  402. }
  403. /*
  404. * Validate a directory trailer.
  405. *
  406. * We check the trailer here rather than in ocfs2_validate_dir_block()
  407. * because that function doesn't have the inode to test.
  408. */
  409. static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
  410. {
  411. int rc = 0;
  412. struct ocfs2_dir_block_trailer *trailer;
  413. trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
  414. if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
  415. rc = -EINVAL;
  416. ocfs2_error(dir->i_sb,
  417. "Invalid dirblock #%llu: "
  418. "signature = %.*s\n",
  419. (unsigned long long)bh->b_blocknr, 7,
  420. trailer->db_signature);
  421. goto out;
  422. }
  423. if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
  424. rc = -EINVAL;
  425. ocfs2_error(dir->i_sb,
  426. "Directory block #%llu has an invalid "
  427. "db_blkno of %llu",
  428. (unsigned long long)bh->b_blocknr,
  429. (unsigned long long)le64_to_cpu(trailer->db_blkno));
  430. goto out;
  431. }
  432. if (le64_to_cpu(trailer->db_parent_dinode) !=
  433. OCFS2_I(dir)->ip_blkno) {
  434. rc = -EINVAL;
  435. ocfs2_error(dir->i_sb,
  436. "Directory block #%llu on dinode "
  437. "#%llu has an invalid parent_dinode "
  438. "of %llu",
  439. (unsigned long long)bh->b_blocknr,
  440. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  441. (unsigned long long)le64_to_cpu(trailer->db_blkno));
  442. goto out;
  443. }
  444. out:
  445. return rc;
  446. }
  447. /*
  448. * This function forces all errors to -EIO for consistency with its
  449. * predecessor, ocfs2_bread(). We haven't audited what returning the
  450. * real error codes would do to callers. We log the real codes with
  451. * mlog_errno() before we squash them.
  452. */
  453. static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
  454. struct buffer_head **bh, int flags)
  455. {
  456. int rc = 0;
  457. struct buffer_head *tmp = *bh;
  458. rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
  459. ocfs2_validate_dir_block);
  460. if (rc) {
  461. mlog_errno(rc);
  462. goto out;
  463. }
  464. if (!(flags & OCFS2_BH_READAHEAD) &&
  465. ocfs2_supports_dir_trailer(inode)) {
  466. rc = ocfs2_check_dir_trailer(inode, tmp);
  467. if (rc) {
  468. if (!*bh)
  469. brelse(tmp);
  470. mlog_errno(rc);
  471. goto out;
  472. }
  473. }
  474. /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
  475. if (!*bh)
  476. *bh = tmp;
  477. out:
  478. return rc ? -EIO : 0;
  479. }
  480. /*
  481. * Read the block at 'phys' which belongs to this directory
  482. * inode. This function does no virtual->physical block translation -
  483. * what's passed in is assumed to be a valid directory block.
  484. */
  485. static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
  486. struct buffer_head **bh)
  487. {
  488. int ret;
  489. struct buffer_head *tmp = *bh;
  490. ret = ocfs2_read_block(dir, phys, &tmp, ocfs2_validate_dir_block);
  491. if (ret) {
  492. mlog_errno(ret);
  493. goto out;
  494. }
  495. if (ocfs2_supports_dir_trailer(dir)) {
  496. ret = ocfs2_check_dir_trailer(dir, tmp);
  497. if (ret) {
  498. if (!*bh)
  499. brelse(tmp);
  500. mlog_errno(ret);
  501. goto out;
  502. }
  503. }
  504. if (!ret && !*bh)
  505. *bh = tmp;
  506. out:
  507. return ret;
  508. }
  509. static int ocfs2_validate_dx_root(struct super_block *sb,
  510. struct buffer_head *bh)
  511. {
  512. int ret;
  513. struct ocfs2_dx_root_block *dx_root;
  514. BUG_ON(!buffer_uptodate(bh));
  515. dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
  516. ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
  517. if (ret) {
  518. mlog(ML_ERROR,
  519. "Checksum failed for dir index root block %llu\n",
  520. (unsigned long long)bh->b_blocknr);
  521. return ret;
  522. }
  523. if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
  524. ocfs2_error(sb,
  525. "Dir Index Root # %llu has bad signature %.*s",
  526. (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
  527. 7, dx_root->dr_signature);
  528. return -EINVAL;
  529. }
  530. return 0;
  531. }
  532. static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
  533. struct buffer_head **dx_root_bh)
  534. {
  535. int ret;
  536. u64 blkno = le64_to_cpu(di->i_dx_root);
  537. struct buffer_head *tmp = *dx_root_bh;
  538. ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_root);
  539. /* If ocfs2_read_block() got us a new bh, pass it up. */
  540. if (!ret && !*dx_root_bh)
  541. *dx_root_bh = tmp;
  542. return ret;
  543. }
  544. static int ocfs2_validate_dx_leaf(struct super_block *sb,
  545. struct buffer_head *bh)
  546. {
  547. int ret;
  548. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
  549. BUG_ON(!buffer_uptodate(bh));
  550. ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
  551. if (ret) {
  552. mlog(ML_ERROR,
  553. "Checksum failed for dir index leaf block %llu\n",
  554. (unsigned long long)bh->b_blocknr);
  555. return ret;
  556. }
  557. if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
  558. ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
  559. 7, dx_leaf->dl_signature);
  560. return -EROFS;
  561. }
  562. return 0;
  563. }
  564. static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
  565. struct buffer_head **dx_leaf_bh)
  566. {
  567. int ret;
  568. struct buffer_head *tmp = *dx_leaf_bh;
  569. ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_leaf);
  570. /* If ocfs2_read_block() got us a new bh, pass it up. */
  571. if (!ret && !*dx_leaf_bh)
  572. *dx_leaf_bh = tmp;
  573. return ret;
  574. }
  575. /*
  576. * Read a series of dx_leaf blocks. This expects all buffer_head
  577. * pointers to be NULL on function entry.
  578. */
  579. static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
  580. struct buffer_head **dx_leaf_bhs)
  581. {
  582. int ret;
  583. ret = ocfs2_read_blocks(dir, start, num, dx_leaf_bhs, 0,
  584. ocfs2_validate_dx_leaf);
  585. if (ret)
  586. mlog_errno(ret);
  587. return ret;
  588. }
  589. static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
  590. struct inode *dir,
  591. struct ocfs2_dir_entry **res_dir)
  592. {
  593. struct super_block *sb;
  594. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  595. struct buffer_head *bh, *ret = NULL;
  596. unsigned long start, block, b;
  597. int ra_max = 0; /* Number of bh's in the readahead
  598. buffer, bh_use[] */
  599. int ra_ptr = 0; /* Current index into readahead
  600. buffer */
  601. int num = 0;
  602. int nblocks, i, err;
  603. mlog_entry_void();
  604. sb = dir->i_sb;
  605. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  606. start = OCFS2_I(dir)->ip_dir_start_lookup;
  607. if (start >= nblocks)
  608. start = 0;
  609. block = start;
  610. restart:
  611. do {
  612. /*
  613. * We deal with the read-ahead logic here.
  614. */
  615. if (ra_ptr >= ra_max) {
  616. /* Refill the readahead buffer */
  617. ra_ptr = 0;
  618. b = block;
  619. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  620. /*
  621. * Terminate if we reach the end of the
  622. * directory and must wrap, or if our
  623. * search has finished at this block.
  624. */
  625. if (b >= nblocks || (num && block == start)) {
  626. bh_use[ra_max] = NULL;
  627. break;
  628. }
  629. num++;
  630. bh = NULL;
  631. err = ocfs2_read_dir_block(dir, b++, &bh,
  632. OCFS2_BH_READAHEAD);
  633. bh_use[ra_max] = bh;
  634. }
  635. }
  636. if ((bh = bh_use[ra_ptr++]) == NULL)
  637. goto next;
  638. if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
  639. /* read error, skip block & hope for the best.
  640. * ocfs2_read_dir_block() has released the bh. */
  641. ocfs2_error(dir->i_sb, "reading directory %llu, "
  642. "offset %lu\n",
  643. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  644. block);
  645. goto next;
  646. }
  647. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  648. block << sb->s_blocksize_bits,
  649. bh->b_data, sb->s_blocksize,
  650. res_dir);
  651. if (i == 1) {
  652. OCFS2_I(dir)->ip_dir_start_lookup = block;
  653. ret = bh;
  654. goto cleanup_and_exit;
  655. } else {
  656. brelse(bh);
  657. if (i < 0)
  658. goto cleanup_and_exit;
  659. }
  660. next:
  661. if (++block >= nblocks)
  662. block = 0;
  663. } while (block != start);
  664. /*
  665. * If the directory has grown while we were searching, then
  666. * search the last part of the directory before giving up.
  667. */
  668. block = nblocks;
  669. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  670. if (block < nblocks) {
  671. start = 0;
  672. goto restart;
  673. }
  674. cleanup_and_exit:
  675. /* Clean up the read-ahead blocks */
  676. for (; ra_ptr < ra_max; ra_ptr++)
  677. brelse(bh_use[ra_ptr]);
  678. mlog_exit_ptr(ret);
  679. return ret;
  680. }
  681. static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
  682. struct ocfs2_extent_list *el,
  683. u32 major_hash,
  684. u32 *ret_cpos,
  685. u64 *ret_phys_blkno,
  686. unsigned int *ret_clen)
  687. {
  688. int ret = 0, i, found;
  689. struct buffer_head *eb_bh = NULL;
  690. struct ocfs2_extent_block *eb;
  691. struct ocfs2_extent_rec *rec = NULL;
  692. if (el->l_tree_depth) {
  693. ret = ocfs2_find_leaf(inode, el, major_hash, &eb_bh);
  694. if (ret) {
  695. mlog_errno(ret);
  696. goto out;
  697. }
  698. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  699. el = &eb->h_list;
  700. if (el->l_tree_depth) {
  701. ocfs2_error(inode->i_sb,
  702. "Inode %lu has non zero tree depth in "
  703. "btree tree block %llu\n", inode->i_ino,
  704. (unsigned long long)eb_bh->b_blocknr);
  705. ret = -EROFS;
  706. goto out;
  707. }
  708. }
  709. found = 0;
  710. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  711. rec = &el->l_recs[i];
  712. if (le32_to_cpu(rec->e_cpos) <= major_hash) {
  713. found = 1;
  714. break;
  715. }
  716. }
  717. if (!found) {
  718. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  719. "record (%u, %u, 0) in btree", inode->i_ino,
  720. le32_to_cpu(rec->e_cpos),
  721. ocfs2_rec_clusters(el, rec));
  722. ret = -EROFS;
  723. goto out;
  724. }
  725. if (ret_phys_blkno)
  726. *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
  727. if (ret_cpos)
  728. *ret_cpos = le32_to_cpu(rec->e_cpos);
  729. if (ret_clen)
  730. *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
  731. out:
  732. brelse(eb_bh);
  733. return ret;
  734. }
  735. /*
  736. * Returns the block index, from the start of the cluster which this
  737. * hash belongs too.
  738. */
  739. static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  740. u32 minor_hash)
  741. {
  742. return minor_hash & osb->osb_dx_mask;
  743. }
  744. static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  745. struct ocfs2_dx_hinfo *hinfo)
  746. {
  747. return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
  748. }
  749. static int ocfs2_dx_dir_lookup(struct inode *inode,
  750. struct ocfs2_extent_list *el,
  751. struct ocfs2_dx_hinfo *hinfo,
  752. u32 *ret_cpos,
  753. u64 *ret_phys_blkno)
  754. {
  755. int ret = 0;
  756. unsigned int cend, uninitialized_var(clen);
  757. u32 uninitialized_var(cpos);
  758. u64 uninitialized_var(blkno);
  759. u32 name_hash = hinfo->major_hash;
  760. ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
  761. &clen);
  762. if (ret) {
  763. mlog_errno(ret);
  764. goto out;
  765. }
  766. cend = cpos + clen;
  767. if (name_hash >= cend) {
  768. /* We want the last cluster */
  769. blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
  770. cpos += clen - 1;
  771. } else {
  772. blkno += ocfs2_clusters_to_blocks(inode->i_sb,
  773. name_hash - cpos);
  774. cpos = name_hash;
  775. }
  776. /*
  777. * We now have the cluster which should hold our entry. To
  778. * find the exact block from the start of the cluster to
  779. * search, we take the lower bits of the hash.
  780. */
  781. blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
  782. if (ret_phys_blkno)
  783. *ret_phys_blkno = blkno;
  784. if (ret_cpos)
  785. *ret_cpos = cpos;
  786. out:
  787. return ret;
  788. }
  789. static int ocfs2_dx_dir_search(const char *name, int namelen,
  790. struct inode *dir,
  791. struct ocfs2_dx_root_block *dx_root,
  792. struct ocfs2_dir_lookup_result *res)
  793. {
  794. int ret, i, found;
  795. u64 uninitialized_var(phys);
  796. struct buffer_head *dx_leaf_bh = NULL;
  797. struct ocfs2_dx_leaf *dx_leaf;
  798. struct ocfs2_dx_entry *dx_entry = NULL;
  799. struct buffer_head *dir_ent_bh = NULL;
  800. struct ocfs2_dir_entry *dir_ent = NULL;
  801. struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
  802. struct ocfs2_extent_list *dr_el;
  803. struct ocfs2_dx_entry_list *entry_list;
  804. ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
  805. if (ocfs2_dx_root_inline(dx_root)) {
  806. entry_list = &dx_root->dr_entries;
  807. goto search;
  808. }
  809. dr_el = &dx_root->dr_list;
  810. ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
  811. if (ret) {
  812. mlog_errno(ret);
  813. goto out;
  814. }
  815. mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
  816. "returns: %llu\n",
  817. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  818. namelen, name, hinfo->major_hash, hinfo->minor_hash,
  819. (unsigned long long)phys);
  820. ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
  821. if (ret) {
  822. mlog_errno(ret);
  823. goto out;
  824. }
  825. dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
  826. mlog(0, "leaf info: num_used: %d, count: %d\n",
  827. le16_to_cpu(dx_leaf->dl_list.de_num_used),
  828. le16_to_cpu(dx_leaf->dl_list.de_count));
  829. entry_list = &dx_leaf->dl_list;
  830. search:
  831. /*
  832. * Empty leaf is legal, so no need to check for that.
  833. */
  834. found = 0;
  835. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  836. dx_entry = &entry_list->de_entries[i];
  837. if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
  838. || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
  839. continue;
  840. /*
  841. * Search unindexed leaf block now. We're not
  842. * guaranteed to find anything.
  843. */
  844. ret = ocfs2_read_dir_block_direct(dir,
  845. le64_to_cpu(dx_entry->dx_dirent_blk),
  846. &dir_ent_bh);
  847. if (ret) {
  848. mlog_errno(ret);
  849. goto out;
  850. }
  851. /*
  852. * XXX: We should check the unindexed block here,
  853. * before using it.
  854. */
  855. found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
  856. 0, dir_ent_bh->b_data,
  857. dir->i_sb->s_blocksize, &dir_ent);
  858. if (found == 1)
  859. break;
  860. if (found == -1) {
  861. /* This means we found a bad directory entry. */
  862. ret = -EIO;
  863. mlog_errno(ret);
  864. goto out;
  865. }
  866. brelse(dir_ent_bh);
  867. dir_ent_bh = NULL;
  868. }
  869. if (found <= 0) {
  870. ret = -ENOENT;
  871. goto out;
  872. }
  873. res->dl_leaf_bh = dir_ent_bh;
  874. res->dl_entry = dir_ent;
  875. res->dl_dx_leaf_bh = dx_leaf_bh;
  876. res->dl_dx_entry = dx_entry;
  877. ret = 0;
  878. out:
  879. if (ret) {
  880. brelse(dx_leaf_bh);
  881. brelse(dir_ent_bh);
  882. }
  883. return ret;
  884. }
  885. static int ocfs2_find_entry_dx(const char *name, int namelen,
  886. struct inode *dir,
  887. struct ocfs2_dir_lookup_result *lookup)
  888. {
  889. int ret;
  890. struct buffer_head *di_bh = NULL;
  891. struct ocfs2_dinode *di;
  892. struct buffer_head *dx_root_bh = NULL;
  893. struct ocfs2_dx_root_block *dx_root;
  894. ret = ocfs2_read_inode_block(dir, &di_bh);
  895. if (ret) {
  896. mlog_errno(ret);
  897. goto out;
  898. }
  899. di = (struct ocfs2_dinode *)di_bh->b_data;
  900. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  901. if (ret) {
  902. mlog_errno(ret);
  903. goto out;
  904. }
  905. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  906. ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
  907. if (ret) {
  908. if (ret != -ENOENT)
  909. mlog_errno(ret);
  910. goto out;
  911. }
  912. lookup->dl_dx_root_bh = dx_root_bh;
  913. dx_root_bh = NULL;
  914. out:
  915. brelse(di_bh);
  916. brelse(dx_root_bh);
  917. return ret;
  918. }
  919. /*
  920. * Try to find an entry of the provided name within 'dir'.
  921. *
  922. * If nothing was found, -ENOENT is returned. Otherwise, zero is
  923. * returned and the struct 'res' will contain information useful to
  924. * other directory manipulation functions.
  925. *
  926. * Caller can NOT assume anything about the contents of the
  927. * buffer_heads - they are passed back only so that it can be passed
  928. * into any one of the manipulation functions (add entry, delete
  929. * entry, etc). As an example, bh in the extent directory case is a
  930. * data block, in the inline-data case it actually points to an inode,
  931. * in the indexed directory case, multiple buffers are involved.
  932. */
  933. int ocfs2_find_entry(const char *name, int namelen,
  934. struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
  935. {
  936. struct buffer_head *bh;
  937. struct ocfs2_dir_entry *res_dir = NULL;
  938. if (ocfs2_dir_indexed(dir))
  939. return ocfs2_find_entry_dx(name, namelen, dir, lookup);
  940. /*
  941. * The unindexed dir code only uses part of the lookup
  942. * structure, so there's no reason to push it down further
  943. * than this.
  944. */
  945. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  946. bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
  947. else
  948. bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
  949. if (bh == NULL)
  950. return -ENOENT;
  951. lookup->dl_leaf_bh = bh;
  952. lookup->dl_entry = res_dir;
  953. return 0;
  954. }
  955. /*
  956. * Update inode number and type of a previously found directory entry.
  957. */
  958. int ocfs2_update_entry(struct inode *dir, handle_t *handle,
  959. struct ocfs2_dir_lookup_result *res,
  960. struct inode *new_entry_inode)
  961. {
  962. int ret;
  963. ocfs2_journal_access_func access = ocfs2_journal_access_db;
  964. struct ocfs2_dir_entry *de = res->dl_entry;
  965. struct buffer_head *de_bh = res->dl_leaf_bh;
  966. /*
  967. * The same code works fine for both inline-data and extent
  968. * based directories, so no need to split this up. The only
  969. * difference is the journal_access function.
  970. */
  971. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  972. access = ocfs2_journal_access_di;
  973. ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  974. if (ret) {
  975. mlog_errno(ret);
  976. goto out;
  977. }
  978. de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
  979. ocfs2_set_de_type(de, new_entry_inode->i_mode);
  980. ocfs2_journal_dirty(handle, de_bh);
  981. out:
  982. return ret;
  983. }
  984. /*
  985. * __ocfs2_delete_entry deletes a directory entry by merging it with the
  986. * previous entry
  987. */
  988. static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
  989. struct ocfs2_dir_entry *de_del,
  990. struct buffer_head *bh, char *first_de,
  991. unsigned int bytes)
  992. {
  993. struct ocfs2_dir_entry *de, *pde;
  994. int i, status = -ENOENT;
  995. ocfs2_journal_access_func access = ocfs2_journal_access_db;
  996. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
  997. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  998. access = ocfs2_journal_access_di;
  999. i = 0;
  1000. pde = NULL;
  1001. de = (struct ocfs2_dir_entry *) first_de;
  1002. while (i < bytes) {
  1003. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  1004. status = -EIO;
  1005. mlog_errno(status);
  1006. goto bail;
  1007. }
  1008. if (de == de_del) {
  1009. status = access(handle, dir, bh,
  1010. OCFS2_JOURNAL_ACCESS_WRITE);
  1011. if (status < 0) {
  1012. status = -EIO;
  1013. mlog_errno(status);
  1014. goto bail;
  1015. }
  1016. if (pde)
  1017. le16_add_cpu(&pde->rec_len,
  1018. le16_to_cpu(de->rec_len));
  1019. else
  1020. de->inode = 0;
  1021. dir->i_version++;
  1022. status = ocfs2_journal_dirty(handle, bh);
  1023. goto bail;
  1024. }
  1025. i += le16_to_cpu(de->rec_len);
  1026. pde = de;
  1027. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  1028. }
  1029. bail:
  1030. mlog_exit(status);
  1031. return status;
  1032. }
  1033. static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
  1034. {
  1035. unsigned int hole;
  1036. if (le64_to_cpu(de->inode) == 0)
  1037. hole = le16_to_cpu(de->rec_len);
  1038. else
  1039. hole = le16_to_cpu(de->rec_len) -
  1040. OCFS2_DIR_REC_LEN(de->name_len);
  1041. return hole;
  1042. }
  1043. static int ocfs2_find_max_rec_len(struct super_block *sb,
  1044. struct buffer_head *dirblock_bh)
  1045. {
  1046. int size, this_hole, largest_hole = 0;
  1047. char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
  1048. struct ocfs2_dir_entry *de;
  1049. trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
  1050. size = ocfs2_dir_trailer_blk_off(sb);
  1051. limit = start + size;
  1052. de_buf = start;
  1053. de = (struct ocfs2_dir_entry *)de_buf;
  1054. do {
  1055. if (de_buf != trailer) {
  1056. this_hole = ocfs2_figure_dirent_hole(de);
  1057. if (this_hole > largest_hole)
  1058. largest_hole = this_hole;
  1059. }
  1060. de_buf += le16_to_cpu(de->rec_len);
  1061. de = (struct ocfs2_dir_entry *)de_buf;
  1062. } while (de_buf < limit);
  1063. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  1064. return largest_hole;
  1065. return 0;
  1066. }
  1067. static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
  1068. int index)
  1069. {
  1070. int num_used = le16_to_cpu(entry_list->de_num_used);
  1071. if (num_used == 1 || index == (num_used - 1))
  1072. goto clear;
  1073. memmove(&entry_list->de_entries[index],
  1074. &entry_list->de_entries[index + 1],
  1075. (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
  1076. clear:
  1077. num_used--;
  1078. memset(&entry_list->de_entries[num_used], 0,
  1079. sizeof(struct ocfs2_dx_entry));
  1080. entry_list->de_num_used = cpu_to_le16(num_used);
  1081. }
  1082. static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
  1083. struct ocfs2_dir_lookup_result *lookup)
  1084. {
  1085. int ret, index, max_rec_len, add_to_free_list = 0;
  1086. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  1087. struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
  1088. struct ocfs2_dx_leaf *dx_leaf;
  1089. struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
  1090. struct ocfs2_dir_block_trailer *trailer;
  1091. struct ocfs2_dx_root_block *dx_root;
  1092. struct ocfs2_dx_entry_list *entry_list;
  1093. /*
  1094. * This function gets a bit messy because we might have to
  1095. * modify the root block, regardless of whether the indexed
  1096. * entries are stored inline.
  1097. */
  1098. /*
  1099. * *Only* set 'entry_list' here, based on where we're looking
  1100. * for the indexed entries. Later, we might still want to
  1101. * journal both blocks, based on free list state.
  1102. */
  1103. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1104. if (ocfs2_dx_root_inline(dx_root)) {
  1105. entry_list = &dx_root->dr_entries;
  1106. } else {
  1107. dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
  1108. entry_list = &dx_leaf->dl_list;
  1109. }
  1110. /* Neither of these are a disk corruption - that should have
  1111. * been caught by lookup, before we got here. */
  1112. BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
  1113. BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
  1114. index = (char *)dx_entry - (char *)entry_list->de_entries;
  1115. index /= sizeof(*dx_entry);
  1116. if (index >= le16_to_cpu(entry_list->de_num_used)) {
  1117. mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
  1118. (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
  1119. entry_list, dx_entry);
  1120. return -EIO;
  1121. }
  1122. /*
  1123. * We know that removal of this dirent will leave enough room
  1124. * for a new one, so add this block to the free list if it
  1125. * isn't already there.
  1126. */
  1127. trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  1128. if (trailer->db_free_rec_len == 0)
  1129. add_to_free_list = 1;
  1130. /*
  1131. * Add the block holding our index into the journal before
  1132. * removing the unindexed entry. If we get an error return
  1133. * from __ocfs2_delete_entry(), then it hasn't removed the
  1134. * entry yet. Likewise, successful return means we *must*
  1135. * remove the indexed entry.
  1136. *
  1137. * We're also careful to journal the root tree block here as
  1138. * the entry count needs to be updated. Also, we might be
  1139. * adding to the start of the free list.
  1140. */
  1141. ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
  1142. OCFS2_JOURNAL_ACCESS_WRITE);
  1143. if (ret) {
  1144. mlog_errno(ret);
  1145. goto out;
  1146. }
  1147. if (!ocfs2_dx_root_inline(dx_root)) {
  1148. ret = ocfs2_journal_access_dl(handle, dir,
  1149. lookup->dl_dx_leaf_bh,
  1150. OCFS2_JOURNAL_ACCESS_WRITE);
  1151. if (ret) {
  1152. mlog_errno(ret);
  1153. goto out;
  1154. }
  1155. }
  1156. mlog(0, "Dir %llu: delete entry at index: %d\n",
  1157. (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
  1158. ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
  1159. leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
  1160. if (ret) {
  1161. mlog_errno(ret);
  1162. goto out;
  1163. }
  1164. max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
  1165. trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  1166. if (add_to_free_list) {
  1167. trailer->db_free_next = dx_root->dr_free_blk;
  1168. dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
  1169. ocfs2_journal_dirty(handle, dx_root_bh);
  1170. }
  1171. /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
  1172. ocfs2_journal_dirty(handle, leaf_bh);
  1173. le32_add_cpu(&dx_root->dr_num_entries, -1);
  1174. ocfs2_journal_dirty(handle, dx_root_bh);
  1175. ocfs2_dx_list_remove_entry(entry_list, index);
  1176. if (!ocfs2_dx_root_inline(dx_root))
  1177. ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
  1178. out:
  1179. return ret;
  1180. }
  1181. static inline int ocfs2_delete_entry_id(handle_t *handle,
  1182. struct inode *dir,
  1183. struct ocfs2_dir_entry *de_del,
  1184. struct buffer_head *bh)
  1185. {
  1186. int ret;
  1187. struct buffer_head *di_bh = NULL;
  1188. struct ocfs2_dinode *di;
  1189. struct ocfs2_inline_data *data;
  1190. ret = ocfs2_read_inode_block(dir, &di_bh);
  1191. if (ret) {
  1192. mlog_errno(ret);
  1193. goto out;
  1194. }
  1195. di = (struct ocfs2_dinode *)di_bh->b_data;
  1196. data = &di->id2.i_data;
  1197. ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
  1198. i_size_read(dir));
  1199. brelse(di_bh);
  1200. out:
  1201. return ret;
  1202. }
  1203. static inline int ocfs2_delete_entry_el(handle_t *handle,
  1204. struct inode *dir,
  1205. struct ocfs2_dir_entry *de_del,
  1206. struct buffer_head *bh)
  1207. {
  1208. return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
  1209. bh->b_size);
  1210. }
  1211. /*
  1212. * Delete a directory entry. Hide the details of directory
  1213. * implementation from the caller.
  1214. */
  1215. int ocfs2_delete_entry(handle_t *handle,
  1216. struct inode *dir,
  1217. struct ocfs2_dir_lookup_result *res)
  1218. {
  1219. if (ocfs2_dir_indexed(dir))
  1220. return ocfs2_delete_entry_dx(handle, dir, res);
  1221. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1222. return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
  1223. res->dl_leaf_bh);
  1224. return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
  1225. res->dl_leaf_bh);
  1226. }
  1227. /*
  1228. * Check whether 'de' has enough room to hold an entry of
  1229. * 'new_rec_len' bytes.
  1230. */
  1231. static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
  1232. unsigned int new_rec_len)
  1233. {
  1234. unsigned int de_really_used;
  1235. /* Check whether this is an empty record with enough space */
  1236. if (le64_to_cpu(de->inode) == 0 &&
  1237. le16_to_cpu(de->rec_len) >= new_rec_len)
  1238. return 1;
  1239. /*
  1240. * Record might have free space at the end which we can
  1241. * use.
  1242. */
  1243. de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
  1244. if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
  1245. return 1;
  1246. return 0;
  1247. }
  1248. static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
  1249. struct ocfs2_dx_entry *dx_new_entry)
  1250. {
  1251. int i;
  1252. i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  1253. dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
  1254. le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
  1255. }
  1256. static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
  1257. struct ocfs2_dx_hinfo *hinfo,
  1258. u64 dirent_blk)
  1259. {
  1260. int i;
  1261. struct ocfs2_dx_entry *dx_entry;
  1262. i = le16_to_cpu(entry_list->de_num_used);
  1263. dx_entry = &entry_list->de_entries[i];
  1264. memset(dx_entry, 0, sizeof(*dx_entry));
  1265. dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
  1266. dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
  1267. dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
  1268. le16_add_cpu(&entry_list->de_num_used, 1);
  1269. }
  1270. static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
  1271. struct ocfs2_dx_hinfo *hinfo,
  1272. u64 dirent_blk,
  1273. struct buffer_head *dx_leaf_bh)
  1274. {
  1275. int ret;
  1276. struct ocfs2_dx_leaf *dx_leaf;
  1277. ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
  1278. OCFS2_JOURNAL_ACCESS_WRITE);
  1279. if (ret) {
  1280. mlog_errno(ret);
  1281. goto out;
  1282. }
  1283. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  1284. ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
  1285. ocfs2_journal_dirty(handle, dx_leaf_bh);
  1286. out:
  1287. return ret;
  1288. }
  1289. static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
  1290. struct ocfs2_dx_hinfo *hinfo,
  1291. u64 dirent_blk,
  1292. struct ocfs2_dx_root_block *dx_root)
  1293. {
  1294. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
  1295. }
  1296. static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
  1297. struct ocfs2_dir_lookup_result *lookup)
  1298. {
  1299. int ret = 0;
  1300. struct ocfs2_dx_root_block *dx_root;
  1301. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  1302. ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
  1303. OCFS2_JOURNAL_ACCESS_WRITE);
  1304. if (ret) {
  1305. mlog_errno(ret);
  1306. goto out;
  1307. }
  1308. dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
  1309. if (ocfs2_dx_root_inline(dx_root)) {
  1310. ocfs2_dx_inline_root_insert(dir, handle,
  1311. &lookup->dl_hinfo,
  1312. lookup->dl_leaf_bh->b_blocknr,
  1313. dx_root);
  1314. } else {
  1315. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
  1316. lookup->dl_leaf_bh->b_blocknr,
  1317. lookup->dl_dx_leaf_bh);
  1318. if (ret)
  1319. goto out;
  1320. }
  1321. le32_add_cpu(&dx_root->dr_num_entries, 1);
  1322. ocfs2_journal_dirty(handle, dx_root_bh);
  1323. out:
  1324. return ret;
  1325. }
  1326. static void ocfs2_remove_block_from_free_list(struct inode *dir,
  1327. handle_t *handle,
  1328. struct ocfs2_dir_lookup_result *lookup)
  1329. {
  1330. struct ocfs2_dir_block_trailer *trailer, *prev;
  1331. struct ocfs2_dx_root_block *dx_root;
  1332. struct buffer_head *bh;
  1333. trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  1334. if (ocfs2_free_list_at_root(lookup)) {
  1335. bh = lookup->dl_dx_root_bh;
  1336. dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
  1337. dx_root->dr_free_blk = trailer->db_free_next;
  1338. } else {
  1339. bh = lookup->dl_prev_leaf_bh;
  1340. prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
  1341. prev->db_free_next = trailer->db_free_next;
  1342. }
  1343. trailer->db_free_rec_len = cpu_to_le16(0);
  1344. trailer->db_free_next = cpu_to_le64(0);
  1345. ocfs2_journal_dirty(handle, bh);
  1346. ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  1347. }
  1348. /*
  1349. * This expects that a journal write has been reserved on
  1350. * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
  1351. */
  1352. static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
  1353. struct ocfs2_dir_lookup_result *lookup)
  1354. {
  1355. int max_rec_len;
  1356. struct ocfs2_dir_block_trailer *trailer;
  1357. /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
  1358. max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
  1359. if (max_rec_len) {
  1360. /*
  1361. * There's still room in this block, so no need to remove it
  1362. * from the free list. In this case, we just want to update
  1363. * the rec len accounting.
  1364. */
  1365. trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  1366. trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  1367. ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  1368. } else {
  1369. ocfs2_remove_block_from_free_list(dir, handle, lookup);
  1370. }
  1371. }
  1372. /* we don't always have a dentry for what we want to add, so people
  1373. * like orphan dir can call this instead.
  1374. *
  1375. * The lookup context must have been filled from
  1376. * ocfs2_prepare_dir_for_insert.
  1377. */
  1378. int __ocfs2_add_entry(handle_t *handle,
  1379. struct inode *dir,
  1380. const char *name, int namelen,
  1381. struct inode *inode, u64 blkno,
  1382. struct buffer_head *parent_fe_bh,
  1383. struct ocfs2_dir_lookup_result *lookup)
  1384. {
  1385. unsigned long offset;
  1386. unsigned short rec_len;
  1387. struct ocfs2_dir_entry *de, *de1;
  1388. struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  1389. struct super_block *sb = dir->i_sb;
  1390. int retval, status;
  1391. unsigned int size = sb->s_blocksize;
  1392. struct buffer_head *insert_bh = lookup->dl_leaf_bh;
  1393. char *data_start = insert_bh->b_data;
  1394. mlog_entry_void();
  1395. if (!namelen)
  1396. return -EINVAL;
  1397. if (ocfs2_dir_indexed(dir)) {
  1398. struct buffer_head *bh;
  1399. /*
  1400. * An indexed dir may require that we update the free space
  1401. * list. Reserve a write to the previous node in the list so
  1402. * that we don't fail later.
  1403. *
  1404. * XXX: This can be either a dx_root_block, or an unindexed
  1405. * directory tree leaf block.
  1406. */
  1407. if (ocfs2_free_list_at_root(lookup)) {
  1408. bh = lookup->dl_dx_root_bh;
  1409. retval = ocfs2_journal_access_dr(handle, dir, bh,
  1410. OCFS2_JOURNAL_ACCESS_WRITE);
  1411. } else {
  1412. bh = lookup->dl_prev_leaf_bh;
  1413. retval = ocfs2_journal_access_db(handle, dir, bh,
  1414. OCFS2_JOURNAL_ACCESS_WRITE);
  1415. }
  1416. if (retval) {
  1417. mlog_errno(retval);
  1418. return retval;
  1419. }
  1420. } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1421. data_start = di->id2.i_data.id_data;
  1422. size = i_size_read(dir);
  1423. BUG_ON(insert_bh != parent_fe_bh);
  1424. }
  1425. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1426. offset = 0;
  1427. de = (struct ocfs2_dir_entry *) data_start;
  1428. while (1) {
  1429. BUG_ON((char *)de >= (size + data_start));
  1430. /* These checks should've already been passed by the
  1431. * prepare function, but I guess we can leave them
  1432. * here anyway. */
  1433. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  1434. retval = -ENOENT;
  1435. goto bail;
  1436. }
  1437. if (ocfs2_match(namelen, name, de)) {
  1438. retval = -EEXIST;
  1439. goto bail;
  1440. }
  1441. /* We're guaranteed that we should have space, so we
  1442. * can't possibly have hit the trailer...right? */
  1443. mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
  1444. "Hit dir trailer trying to insert %.*s "
  1445. "(namelen %d) into directory %llu. "
  1446. "offset is %lu, trailer offset is %d\n",
  1447. namelen, name, namelen,
  1448. (unsigned long long)parent_fe_bh->b_blocknr,
  1449. offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
  1450. if (ocfs2_dirent_would_fit(de, rec_len)) {
  1451. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1452. retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  1453. if (retval < 0) {
  1454. mlog_errno(retval);
  1455. goto bail;
  1456. }
  1457. if (insert_bh == parent_fe_bh)
  1458. status = ocfs2_journal_access_di(handle, dir,
  1459. insert_bh,
  1460. OCFS2_JOURNAL_ACCESS_WRITE);
  1461. else {
  1462. status = ocfs2_journal_access_db(handle, dir,
  1463. insert_bh,
  1464. OCFS2_JOURNAL_ACCESS_WRITE);
  1465. if (ocfs2_dir_indexed(dir)) {
  1466. status = ocfs2_dx_dir_insert(dir,
  1467. handle,
  1468. lookup);
  1469. if (status) {
  1470. mlog_errno(status);
  1471. goto bail;
  1472. }
  1473. }
  1474. }
  1475. /* By now the buffer is marked for journaling */
  1476. offset += le16_to_cpu(de->rec_len);
  1477. if (le64_to_cpu(de->inode)) {
  1478. de1 = (struct ocfs2_dir_entry *)((char *) de +
  1479. OCFS2_DIR_REC_LEN(de->name_len));
  1480. de1->rec_len =
  1481. cpu_to_le16(le16_to_cpu(de->rec_len) -
  1482. OCFS2_DIR_REC_LEN(de->name_len));
  1483. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1484. de = de1;
  1485. }
  1486. de->file_type = OCFS2_FT_UNKNOWN;
  1487. if (blkno) {
  1488. de->inode = cpu_to_le64(blkno);
  1489. ocfs2_set_de_type(de, inode->i_mode);
  1490. } else
  1491. de->inode = 0;
  1492. de->name_len = namelen;
  1493. memcpy(de->name, name, namelen);
  1494. if (ocfs2_dir_indexed(dir))
  1495. ocfs2_recalc_free_list(dir, handle, lookup);
  1496. dir->i_version++;
  1497. status = ocfs2_journal_dirty(handle, insert_bh);
  1498. retval = 0;
  1499. goto bail;
  1500. }
  1501. offset += le16_to_cpu(de->rec_len);
  1502. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  1503. }
  1504. /* when you think about it, the assert above should prevent us
  1505. * from ever getting here. */
  1506. retval = -ENOSPC;
  1507. bail:
  1508. mlog_exit(retval);
  1509. return retval;
  1510. }
  1511. static int ocfs2_dir_foreach_blk_id(struct inode *inode,
  1512. u64 *f_version,
  1513. loff_t *f_pos, void *priv,
  1514. filldir_t filldir, int *filldir_err)
  1515. {
  1516. int ret, i, filldir_ret;
  1517. unsigned long offset = *f_pos;
  1518. struct buffer_head *di_bh = NULL;
  1519. struct ocfs2_dinode *di;
  1520. struct ocfs2_inline_data *data;
  1521. struct ocfs2_dir_entry *de;
  1522. ret = ocfs2_read_inode_block(inode, &di_bh);
  1523. if (ret) {
  1524. mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
  1525. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1526. goto out;
  1527. }
  1528. di = (struct ocfs2_dinode *)di_bh->b_data;
  1529. data = &di->id2.i_data;
  1530. while (*f_pos < i_size_read(inode)) {
  1531. revalidate:
  1532. /* If the dir block has changed since the last call to
  1533. * readdir(2), then we might be pointing to an invalid
  1534. * dirent right now. Scan from the start of the block
  1535. * to make sure. */
  1536. if (*f_version != inode->i_version) {
  1537. for (i = 0; i < i_size_read(inode) && i < offset; ) {
  1538. de = (struct ocfs2_dir_entry *)
  1539. (data->id_data + i);
  1540. /* It's too expensive to do a full
  1541. * dirent test each time round this
  1542. * loop, but we do have to test at
  1543. * least that it is non-zero. A
  1544. * failure will be detected in the
  1545. * dirent test below. */
  1546. if (le16_to_cpu(de->rec_len) <
  1547. OCFS2_DIR_REC_LEN(1))
  1548. break;
  1549. i += le16_to_cpu(de->rec_len);
  1550. }
  1551. *f_pos = offset = i;
  1552. *f_version = inode->i_version;
  1553. }
  1554. de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
  1555. if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
  1556. /* On error, skip the f_pos to the end. */
  1557. *f_pos = i_size_read(inode);
  1558. goto out;
  1559. }
  1560. offset += le16_to_cpu(de->rec_len);
  1561. if (le64_to_cpu(de->inode)) {
  1562. /* We might block in the next section
  1563. * if the data destination is
  1564. * currently swapped out. So, use a
  1565. * version stamp to detect whether or
  1566. * not the directory has been modified
  1567. * during the copy operation.
  1568. */
  1569. u64 version = *f_version;
  1570. unsigned char d_type = DT_UNKNOWN;
  1571. if (de->file_type < OCFS2_FT_MAX)
  1572. d_type = ocfs2_filetype_table[de->file_type];
  1573. filldir_ret = filldir(priv, de->name,
  1574. de->name_len,
  1575. *f_pos,
  1576. le64_to_cpu(de->inode),
  1577. d_type);
  1578. if (filldir_ret) {
  1579. if (filldir_err)
  1580. *filldir_err = filldir_ret;
  1581. break;
  1582. }
  1583. if (version != *f_version)
  1584. goto revalidate;
  1585. }
  1586. *f_pos += le16_to_cpu(de->rec_len);
  1587. }
  1588. out:
  1589. brelse(di_bh);
  1590. return 0;
  1591. }
  1592. /*
  1593. * NOTE: This function can be called against unindexed directories,
  1594. * and indexed ones.
  1595. */
  1596. static int ocfs2_dir_foreach_blk_el(struct inode *inode,
  1597. u64 *f_version,
  1598. loff_t *f_pos, void *priv,
  1599. filldir_t filldir, int *filldir_err)
  1600. {
  1601. int error = 0;
  1602. unsigned long offset, blk, last_ra_blk = 0;
  1603. int i, stored;
  1604. struct buffer_head * bh, * tmp;
  1605. struct ocfs2_dir_entry * de;
  1606. struct super_block * sb = inode->i_sb;
  1607. unsigned int ra_sectors = 16;
  1608. stored = 0;
  1609. bh = NULL;
  1610. offset = (*f_pos) & (sb->s_blocksize - 1);
  1611. while (!error && !stored && *f_pos < i_size_read(inode)) {
  1612. blk = (*f_pos) >> sb->s_blocksize_bits;
  1613. if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
  1614. /* Skip the corrupt dirblock and keep trying */
  1615. *f_pos += sb->s_blocksize - offset;
  1616. continue;
  1617. }
  1618. /* The idea here is to begin with 8k read-ahead and to stay
  1619. * 4k ahead of our current position.
  1620. *
  1621. * TODO: Use the pagecache for this. We just need to
  1622. * make sure it's cluster-safe... */
  1623. if (!last_ra_blk
  1624. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  1625. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  1626. i > 0; i--) {
  1627. tmp = NULL;
  1628. if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
  1629. OCFS2_BH_READAHEAD))
  1630. brelse(tmp);
  1631. }
  1632. last_ra_blk = blk;
  1633. ra_sectors = 8;
  1634. }
  1635. revalidate:
  1636. /* If the dir block has changed since the last call to
  1637. * readdir(2), then we might be pointing to an invalid
  1638. * dirent right now. Scan from the start of the block
  1639. * to make sure. */
  1640. if (*f_version != inode->i_version) {
  1641. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  1642. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  1643. /* It's too expensive to do a full
  1644. * dirent test each time round this
  1645. * loop, but we do have to test at
  1646. * least that it is non-zero. A
  1647. * failure will be detected in the
  1648. * dirent test below. */
  1649. if (le16_to_cpu(de->rec_len) <
  1650. OCFS2_DIR_REC_LEN(1))
  1651. break;
  1652. i += le16_to_cpu(de->rec_len);
  1653. }
  1654. offset = i;
  1655. *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
  1656. | offset;
  1657. *f_version = inode->i_version;
  1658. }
  1659. while (!error && *f_pos < i_size_read(inode)
  1660. && offset < sb->s_blocksize) {
  1661. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  1662. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  1663. /* On error, skip the f_pos to the
  1664. next block. */
  1665. *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
  1666. brelse(bh);
  1667. goto out;
  1668. }
  1669. offset += le16_to_cpu(de->rec_len);
  1670. if (le64_to_cpu(de->inode)) {
  1671. /* We might block in the next section
  1672. * if the data destination is
  1673. * currently swapped out. So, use a
  1674. * version stamp to detect whether or
  1675. * not the directory has been modified
  1676. * during the copy operation.
  1677. */
  1678. unsigned long version = *f_version;
  1679. unsigned char d_type = DT_UNKNOWN;
  1680. if (de->file_type < OCFS2_FT_MAX)
  1681. d_type = ocfs2_filetype_table[de->file_type];
  1682. error = filldir(priv, de->name,
  1683. de->name_len,
  1684. *f_pos,
  1685. le64_to_cpu(de->inode),
  1686. d_type);
  1687. if (error) {
  1688. if (filldir_err)
  1689. *filldir_err = error;
  1690. break;
  1691. }
  1692. if (version != *f_version)
  1693. goto revalidate;
  1694. stored ++;
  1695. }
  1696. *f_pos += le16_to_cpu(de->rec_len);
  1697. }
  1698. offset = 0;
  1699. brelse(bh);
  1700. bh = NULL;
  1701. }
  1702. stored = 0;
  1703. out:
  1704. return stored;
  1705. }
  1706. static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
  1707. loff_t *f_pos, void *priv, filldir_t filldir,
  1708. int *filldir_err)
  1709. {
  1710. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1711. return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
  1712. filldir, filldir_err);
  1713. return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
  1714. filldir_err);
  1715. }
  1716. /*
  1717. * This is intended to be called from inside other kernel functions,
  1718. * so we fake some arguments.
  1719. */
  1720. int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
  1721. filldir_t filldir)
  1722. {
  1723. int ret = 0, filldir_err = 0;
  1724. u64 version = inode->i_version;
  1725. while (*f_pos < i_size_read(inode)) {
  1726. ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
  1727. filldir, &filldir_err);
  1728. if (ret || filldir_err)
  1729. break;
  1730. }
  1731. if (ret > 0)
  1732. ret = -EIO;
  1733. return 0;
  1734. }
  1735. /*
  1736. * ocfs2_readdir()
  1737. *
  1738. */
  1739. int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1740. {
  1741. int error = 0;
  1742. struct inode *inode = filp->f_path.dentry->d_inode;
  1743. int lock_level = 0;
  1744. mlog_entry("dirino=%llu\n",
  1745. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1746. error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1747. if (lock_level && error >= 0) {
  1748. /* We release EX lock which used to update atime
  1749. * and get PR lock again to reduce contention
  1750. * on commonly accessed directories. */
  1751. ocfs2_inode_unlock(inode, 1);
  1752. lock_level = 0;
  1753. error = ocfs2_inode_lock(inode, NULL, 0);
  1754. }
  1755. if (error < 0) {
  1756. if (error != -ENOENT)
  1757. mlog_errno(error);
  1758. /* we haven't got any yet, so propagate the error. */
  1759. goto bail_nolock;
  1760. }
  1761. error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
  1762. dirent, filldir, NULL);
  1763. ocfs2_inode_unlock(inode, lock_level);
  1764. bail_nolock:
  1765. mlog_exit(error);
  1766. return error;
  1767. }
  1768. /*
  1769. * NOTE: this should always be called with parent dir i_mutex taken.
  1770. */
  1771. int ocfs2_find_files_on_disk(const char *name,
  1772. int namelen,
  1773. u64 *blkno,
  1774. struct inode *inode,
  1775. struct ocfs2_dir_lookup_result *lookup)
  1776. {
  1777. int status = -ENOENT;
  1778. mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
  1779. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1780. status = ocfs2_find_entry(name, namelen, inode, lookup);
  1781. if (status)
  1782. goto leave;
  1783. *blkno = le64_to_cpu(lookup->dl_entry->inode);
  1784. status = 0;
  1785. leave:
  1786. return status;
  1787. }
  1788. /*
  1789. * Convenience function for callers which just want the block number
  1790. * mapped to a name and don't require the full dirent info, etc.
  1791. */
  1792. int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  1793. int namelen, u64 *blkno)
  1794. {
  1795. int ret;
  1796. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1797. ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
  1798. ocfs2_free_dir_lookup_result(&lookup);
  1799. return ret;
  1800. }
  1801. /* Check for a name within a directory.
  1802. *
  1803. * Return 0 if the name does not exist
  1804. * Return -EEXIST if the directory contains the name
  1805. *
  1806. * Callers should have i_mutex + a cluster lock on dir
  1807. */
  1808. int ocfs2_check_dir_for_entry(struct inode *dir,
  1809. const char *name,
  1810. int namelen)
  1811. {
  1812. int ret;
  1813. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1814. mlog_entry("dir %llu, name '%.*s'\n",
  1815. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  1816. ret = -EEXIST;
  1817. if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
  1818. goto bail;
  1819. ret = 0;
  1820. bail:
  1821. ocfs2_free_dir_lookup_result(&lookup);
  1822. mlog_exit(ret);
  1823. return ret;
  1824. }
  1825. struct ocfs2_empty_dir_priv {
  1826. unsigned seen_dot;
  1827. unsigned seen_dot_dot;
  1828. unsigned seen_other;
  1829. unsigned dx_dir;
  1830. };
  1831. static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
  1832. loff_t pos, u64 ino, unsigned type)
  1833. {
  1834. struct ocfs2_empty_dir_priv *p = priv;
  1835. /*
  1836. * Check the positions of "." and ".." records to be sure
  1837. * they're in the correct place.
  1838. *
  1839. * Indexed directories don't need to proceed past the first
  1840. * two entries, so we end the scan after seeing '..'. Despite
  1841. * that, we allow the scan to proceed In the event that we
  1842. * have a corrupted indexed directory (no dot or dot dot
  1843. * entries). This allows us to double check for existing
  1844. * entries which might not have been found in the index.
  1845. */
  1846. if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  1847. p->seen_dot = 1;
  1848. return 0;
  1849. }
  1850. if (name_len == 2 && !strncmp("..", name, 2) &&
  1851. pos == OCFS2_DIR_REC_LEN(1)) {
  1852. p->seen_dot_dot = 1;
  1853. if (p->dx_dir && p->seen_dot)
  1854. return 1;
  1855. return 0;
  1856. }
  1857. p->seen_other = 1;
  1858. return 1;
  1859. }
  1860. static int ocfs2_empty_dir_dx(struct inode *inode,
  1861. struct ocfs2_empty_dir_priv *priv)
  1862. {
  1863. int ret;
  1864. struct buffer_head *di_bh = NULL;
  1865. struct buffer_head *dx_root_bh = NULL;
  1866. struct ocfs2_dinode *di;
  1867. struct ocfs2_dx_root_block *dx_root;
  1868. priv->dx_dir = 1;
  1869. ret = ocfs2_read_inode_block(inode, &di_bh);
  1870. if (ret) {
  1871. mlog_errno(ret);
  1872. goto out;
  1873. }
  1874. di = (struct ocfs2_dinode *)di_bh->b_data;
  1875. ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
  1876. if (ret) {
  1877. mlog_errno(ret);
  1878. goto out;
  1879. }
  1880. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1881. if (le32_to_cpu(dx_root->dr_num_entries) != 2)
  1882. priv->seen_other = 1;
  1883. out:
  1884. brelse(di_bh);
  1885. brelse(dx_root_bh);
  1886. return ret;
  1887. }
  1888. /*
  1889. * routine to check that the specified directory is empty (for rmdir)
  1890. *
  1891. * Returns 1 if dir is empty, zero otherwise.
  1892. *
  1893. * XXX: This is a performance problem for unindexed directories.
  1894. */
  1895. int ocfs2_empty_dir(struct inode *inode)
  1896. {
  1897. int ret;
  1898. loff_t start = 0;
  1899. struct ocfs2_empty_dir_priv priv;
  1900. memset(&priv, 0, sizeof(priv));
  1901. if (ocfs2_dir_indexed(inode)) {
  1902. ret = ocfs2_empty_dir_dx(inode, &priv);
  1903. if (ret)
  1904. mlog_errno(ret);
  1905. /*
  1906. * We still run ocfs2_dir_foreach to get the checks
  1907. * for "." and "..".
  1908. */
  1909. }
  1910. ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
  1911. if (ret)
  1912. mlog_errno(ret);
  1913. if (!priv.seen_dot || !priv.seen_dot_dot) {
  1914. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  1915. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1916. /*
  1917. * XXX: Is it really safe to allow an unlink to continue?
  1918. */
  1919. return 1;
  1920. }
  1921. return !priv.seen_other;
  1922. }
  1923. /*
  1924. * Fills "." and ".." dirents in a new directory block. Returns dirent for
  1925. * "..", which might be used during creation of a directory with a trailing
  1926. * header. It is otherwise safe to ignore the return code.
  1927. */
  1928. static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
  1929. struct inode *parent,
  1930. char *start,
  1931. unsigned int size)
  1932. {
  1933. struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
  1934. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  1935. de->name_len = 1;
  1936. de->rec_len =
  1937. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1938. strcpy(de->name, ".");
  1939. ocfs2_set_de_type(de, S_IFDIR);
  1940. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  1941. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  1942. de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
  1943. de->name_len = 2;
  1944. strcpy(de->name, "..");
  1945. ocfs2_set_de_type(de, S_IFDIR);
  1946. return de;
  1947. }
  1948. /*
  1949. * This works together with code in ocfs2_mknod_locked() which sets
  1950. * the inline-data flag and initializes the inline-data section.
  1951. */
  1952. static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
  1953. handle_t *handle,
  1954. struct inode *parent,
  1955. struct inode *inode,
  1956. struct buffer_head *di_bh)
  1957. {
  1958. int ret;
  1959. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1960. struct ocfs2_inline_data *data = &di->id2.i_data;
  1961. unsigned int size = le16_to_cpu(data->id_count);
  1962. ret = ocfs2_journal_access_di(handle, inode, di_bh,
  1963. OCFS2_JOURNAL_ACCESS_WRITE);
  1964. if (ret) {
  1965. mlog_errno(ret);
  1966. goto out;
  1967. }
  1968. ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
  1969. ocfs2_journal_dirty(handle, di_bh);
  1970. if (ret) {
  1971. mlog_errno(ret);
  1972. goto out;
  1973. }
  1974. i_size_write(inode, size);
  1975. inode->i_nlink = 2;
  1976. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1977. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1978. if (ret < 0)
  1979. mlog_errno(ret);
  1980. out:
  1981. return ret;
  1982. }
  1983. static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
  1984. handle_t *handle,
  1985. struct inode *parent,
  1986. struct inode *inode,
  1987. struct buffer_head *fe_bh,
  1988. struct ocfs2_alloc_context *data_ac,
  1989. struct buffer_head **ret_new_bh)
  1990. {
  1991. int status;
  1992. unsigned int size = osb->sb->s_blocksize;
  1993. struct buffer_head *new_bh = NULL;
  1994. struct ocfs2_dir_entry *de;
  1995. mlog_entry_void();
  1996. if (ocfs2_new_dir_wants_trailer(inode))
  1997. size = ocfs2_dir_trailer_blk_off(parent->i_sb);
  1998. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  1999. data_ac, NULL, &new_bh);
  2000. if (status < 0) {
  2001. mlog_errno(status);
  2002. goto bail;
  2003. }
  2004. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  2005. status = ocfs2_journal_access_db(handle, inode, new_bh,
  2006. OCFS2_JOURNAL_ACCESS_CREATE);
  2007. if (status < 0) {
  2008. mlog_errno(status);
  2009. goto bail;
  2010. }
  2011. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  2012. de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
  2013. if (ocfs2_new_dir_wants_trailer(inode)) {
  2014. int size = le16_to_cpu(de->rec_len);
  2015. /*
  2016. * Figure out the size of the hole left over after
  2017. * insertion of '.' and '..'. The trailer wants this
  2018. * information.
  2019. */
  2020. size -= OCFS2_DIR_REC_LEN(2);
  2021. size -= sizeof(struct ocfs2_dir_block_trailer);
  2022. ocfs2_init_dir_trailer(inode, new_bh, size);
  2023. }
  2024. status = ocfs2_journal_dirty(handle, new_bh);
  2025. if (status < 0) {
  2026. mlog_errno(status);
  2027. goto bail;
  2028. }
  2029. i_size_write(inode, inode->i_sb->s_blocksize);
  2030. inode->i_nlink = 2;
  2031. inode->i_blocks = ocfs2_inode_sector_count(inode);
  2032. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  2033. if (status < 0) {
  2034. mlog_errno(status);
  2035. goto bail;
  2036. }
  2037. status = 0;
  2038. if (ret_new_bh) {
  2039. *ret_new_bh = new_bh;
  2040. new_bh = NULL;
  2041. }
  2042. bail:
  2043. brelse(new_bh);
  2044. mlog_exit(status);
  2045. return status;
  2046. }
  2047. static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
  2048. handle_t *handle, struct inode *dir,
  2049. struct buffer_head *di_bh,
  2050. struct buffer_head *dirdata_bh,
  2051. struct ocfs2_alloc_context *meta_ac,
  2052. int dx_inline, u32 num_entries,
  2053. struct buffer_head **ret_dx_root_bh)
  2054. {
  2055. int ret;
  2056. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  2057. u16 dr_suballoc_bit;
  2058. u64 dr_blkno;
  2059. unsigned int num_bits;
  2060. struct buffer_head *dx_root_bh = NULL;
  2061. struct ocfs2_dx_root_block *dx_root;
  2062. struct ocfs2_dir_block_trailer *trailer =
  2063. ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  2064. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, &dr_suballoc_bit,
  2065. &num_bits, &dr_blkno);
  2066. if (ret) {
  2067. mlog_errno(ret);
  2068. goto out;
  2069. }
  2070. mlog(0, "Dir %llu, attach new index block: %llu\n",
  2071. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2072. (unsigned long long)dr_blkno);
  2073. dx_root_bh = sb_getblk(osb->sb, dr_blkno);
  2074. if (dx_root_bh == NULL) {
  2075. ret = -EIO;
  2076. goto out;
  2077. }
  2078. ocfs2_set_new_buffer_uptodate(dir, dx_root_bh);
  2079. ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
  2080. OCFS2_JOURNAL_ACCESS_CREATE);
  2081. if (ret < 0) {
  2082. mlog_errno(ret);
  2083. goto out;
  2084. }
  2085. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2086. memset(dx_root, 0, osb->sb->s_blocksize);
  2087. strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
  2088. dx_root->dr_suballoc_slot = cpu_to_le16(osb->slot_num);
  2089. dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
  2090. dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
  2091. dx_root->dr_blkno = cpu_to_le64(dr_blkno);
  2092. dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
  2093. dx_root->dr_num_entries = cpu_to_le32(num_entries);
  2094. if (le16_to_cpu(trailer->db_free_rec_len))
  2095. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  2096. else
  2097. dx_root->dr_free_blk = cpu_to_le64(0);
  2098. if (dx_inline) {
  2099. dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
  2100. dx_root->dr_entries.de_count =
  2101. cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
  2102. } else {
  2103. dx_root->dr_list.l_count =
  2104. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  2105. }
  2106. ret = ocfs2_journal_dirty(handle, dx_root_bh);
  2107. if (ret)
  2108. mlog_errno(ret);
  2109. ret = ocfs2_journal_access_di(handle, dir, di_bh,
  2110. OCFS2_JOURNAL_ACCESS_CREATE);
  2111. if (ret) {
  2112. mlog_errno(ret);
  2113. goto out;
  2114. }
  2115. di->i_dx_root = cpu_to_le64(dr_blkno);
  2116. OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
  2117. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  2118. ret = ocfs2_journal_dirty(handle, di_bh);
  2119. if (ret)
  2120. mlog_errno(ret);
  2121. *ret_dx_root_bh = dx_root_bh;
  2122. dx_root_bh = NULL;
  2123. out:
  2124. brelse(dx_root_bh);
  2125. return ret;
  2126. }
  2127. static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
  2128. handle_t *handle, struct inode *dir,
  2129. struct buffer_head **dx_leaves,
  2130. int num_dx_leaves, u64 start_blk)
  2131. {
  2132. int ret, i;
  2133. struct ocfs2_dx_leaf *dx_leaf;
  2134. struct buffer_head *bh;
  2135. for (i = 0; i < num_dx_leaves; i++) {
  2136. bh = sb_getblk(osb->sb, start_blk + i);
  2137. if (bh == NULL) {
  2138. ret = -EIO;
  2139. goto out;
  2140. }
  2141. dx_leaves[i] = bh;
  2142. ocfs2_set_new_buffer_uptodate(dir, bh);
  2143. ret = ocfs2_journal_access_dl(handle, dir, bh,
  2144. OCFS2_JOURNAL_ACCESS_CREATE);
  2145. if (ret < 0) {
  2146. mlog_errno(ret);
  2147. goto out;
  2148. }
  2149. dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
  2150. memset(dx_leaf, 0, osb->sb->s_blocksize);
  2151. strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
  2152. dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
  2153. dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
  2154. dx_leaf->dl_list.de_count =
  2155. cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
  2156. mlog(0,
  2157. "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
  2158. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2159. (unsigned long long)bh->b_blocknr,
  2160. le16_to_cpu(dx_leaf->dl_list.de_count));
  2161. ocfs2_journal_dirty(handle, bh);
  2162. }
  2163. ret = 0;
  2164. out:
  2165. return ret;
  2166. }
  2167. /*
  2168. * Allocates and formats a new cluster for use in an indexed dir
  2169. * leaf. This version will not do the extent insert, so that it can be
  2170. * used by operations which need careful ordering.
  2171. */
  2172. static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
  2173. u32 cpos, handle_t *handle,
  2174. struct ocfs2_alloc_context *data_ac,
  2175. struct buffer_head **dx_leaves,
  2176. int num_dx_leaves, u64 *ret_phys_blkno)
  2177. {
  2178. int ret;
  2179. u32 phys, num;
  2180. u64 phys_blkno;
  2181. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2182. /*
  2183. * XXX: For create, this should claim cluster for the index
  2184. * *before* the unindexed insert so that we have a better
  2185. * chance of contiguousness as the directory grows in number
  2186. * of entries.
  2187. */
  2188. ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 1, &phys, &num);
  2189. if (ret) {
  2190. mlog_errno(ret);
  2191. goto out;
  2192. }
  2193. /*
  2194. * Format the new cluster first. That way, we're inserting
  2195. * valid data.
  2196. */
  2197. phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
  2198. ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
  2199. num_dx_leaves, phys_blkno);
  2200. if (ret) {
  2201. mlog_errno(ret);
  2202. goto out;
  2203. }
  2204. *ret_phys_blkno = phys_blkno;
  2205. out:
  2206. return ret;
  2207. }
  2208. static int ocfs2_dx_dir_new_cluster(struct inode *dir,
  2209. struct ocfs2_extent_tree *et,
  2210. u32 cpos, handle_t *handle,
  2211. struct ocfs2_alloc_context *data_ac,
  2212. struct ocfs2_alloc_context *meta_ac,
  2213. struct buffer_head **dx_leaves,
  2214. int num_dx_leaves)
  2215. {
  2216. int ret;
  2217. u64 phys_blkno;
  2218. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2219. ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
  2220. num_dx_leaves, &phys_blkno);
  2221. if (ret) {
  2222. mlog_errno(ret);
  2223. goto out;
  2224. }
  2225. ret = ocfs2_insert_extent(osb, handle, dir, et, cpos, phys_blkno, 1, 0,
  2226. meta_ac);
  2227. if (ret)
  2228. mlog_errno(ret);
  2229. out:
  2230. return ret;
  2231. }
  2232. static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
  2233. int *ret_num_leaves)
  2234. {
  2235. int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
  2236. struct buffer_head **dx_leaves;
  2237. dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
  2238. GFP_NOFS);
  2239. if (dx_leaves && ret_num_leaves)
  2240. *ret_num_leaves = num_dx_leaves;
  2241. return dx_leaves;
  2242. }
  2243. static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
  2244. handle_t *handle,
  2245. struct inode *parent,
  2246. struct inode *inode,
  2247. struct buffer_head *di_bh,
  2248. struct ocfs2_alloc_context *data_ac,
  2249. struct ocfs2_alloc_context *meta_ac)
  2250. {
  2251. int ret;
  2252. struct buffer_head *leaf_bh = NULL;
  2253. struct buffer_head *dx_root_bh = NULL;
  2254. struct ocfs2_dx_hinfo hinfo;
  2255. struct ocfs2_dx_root_block *dx_root;
  2256. struct ocfs2_dx_entry_list *entry_list;
  2257. /*
  2258. * Our strategy is to create the directory as though it were
  2259. * unindexed, then add the index block. This works with very
  2260. * little complication since the state of a new directory is a
  2261. * very well known quantity.
  2262. *
  2263. * Essentially, we have two dirents ("." and ".."), in the 1st
  2264. * block which need indexing. These are easily inserted into
  2265. * the index block.
  2266. */
  2267. ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
  2268. data_ac, &leaf_bh);
  2269. if (ret) {
  2270. mlog_errno(ret);
  2271. goto out;
  2272. }
  2273. ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
  2274. meta_ac, 1, 2, &dx_root_bh);
  2275. if (ret) {
  2276. mlog_errno(ret);
  2277. goto out;
  2278. }
  2279. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2280. entry_list = &dx_root->dr_entries;
  2281. /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
  2282. ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
  2283. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2284. ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
  2285. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2286. out:
  2287. brelse(dx_root_bh);
  2288. brelse(leaf_bh);
  2289. return ret;
  2290. }
  2291. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  2292. handle_t *handle,
  2293. struct inode *parent,
  2294. struct inode *inode,
  2295. struct buffer_head *fe_bh,
  2296. struct ocfs2_alloc_context *data_ac,
  2297. struct ocfs2_alloc_context *meta_ac)
  2298. {
  2299. BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
  2300. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  2301. return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
  2302. if (ocfs2_supports_indexed_dirs(osb))
  2303. return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
  2304. data_ac, meta_ac);
  2305. return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
  2306. data_ac, NULL);
  2307. }
  2308. static int ocfs2_dx_dir_index_block(struct inode *dir,
  2309. handle_t *handle,
  2310. struct buffer_head **dx_leaves,
  2311. int num_dx_leaves,
  2312. u32 *num_dx_entries,
  2313. struct buffer_head *dirent_bh)
  2314. {
  2315. int ret, namelen, i;
  2316. char *de_buf, *limit;
  2317. struct ocfs2_dir_entry *de;
  2318. struct buffer_head *dx_leaf_bh;
  2319. struct ocfs2_dx_hinfo hinfo;
  2320. u64 dirent_blk = dirent_bh->b_blocknr;
  2321. de_buf = dirent_bh->b_data;
  2322. limit = de_buf + dir->i_sb->s_blocksize;
  2323. while (de_buf < limit) {
  2324. de = (struct ocfs2_dir_entry *)de_buf;
  2325. namelen = de->name_len;
  2326. if (!namelen || !de->inode)
  2327. goto inc;
  2328. ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
  2329. i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
  2330. dx_leaf_bh = dx_leaves[i];
  2331. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
  2332. dirent_blk, dx_leaf_bh);
  2333. if (ret) {
  2334. mlog_errno(ret);
  2335. goto out;
  2336. }
  2337. *num_dx_entries = *num_dx_entries + 1;
  2338. inc:
  2339. de_buf += le16_to_cpu(de->rec_len);
  2340. }
  2341. out:
  2342. return ret;
  2343. }
  2344. /*
  2345. * XXX: This expects dx_root_bh to already be part of the transaction.
  2346. */
  2347. static void ocfs2_dx_dir_index_root_block(struct inode *dir,
  2348. struct buffer_head *dx_root_bh,
  2349. struct buffer_head *dirent_bh)
  2350. {
  2351. char *de_buf, *limit;
  2352. struct ocfs2_dx_root_block *dx_root;
  2353. struct ocfs2_dir_entry *de;
  2354. struct ocfs2_dx_hinfo hinfo;
  2355. u64 dirent_blk = dirent_bh->b_blocknr;
  2356. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2357. de_buf = dirent_bh->b_data;
  2358. limit = de_buf + dir->i_sb->s_blocksize;
  2359. while (de_buf < limit) {
  2360. de = (struct ocfs2_dir_entry *)de_buf;
  2361. if (!de->name_len || !de->inode)
  2362. goto inc;
  2363. ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
  2364. mlog(0,
  2365. "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
  2366. (unsigned long long)dir->i_ino, hinfo.major_hash,
  2367. hinfo.minor_hash,
  2368. le16_to_cpu(dx_root->dr_entries.de_num_used),
  2369. de->name_len, de->name);
  2370. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
  2371. dirent_blk);
  2372. le32_add_cpu(&dx_root->dr_num_entries, 1);
  2373. inc:
  2374. de_buf += le16_to_cpu(de->rec_len);
  2375. }
  2376. }
  2377. /*
  2378. * Count the number of inline directory entries in di_bh and compare
  2379. * them against the number of entries we can hold in an inline dx root
  2380. * block.
  2381. */
  2382. static int ocfs2_new_dx_should_be_inline(struct inode *dir,
  2383. struct buffer_head *di_bh)
  2384. {
  2385. int dirent_count = 0;
  2386. char *de_buf, *limit;
  2387. struct ocfs2_dir_entry *de;
  2388. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2389. de_buf = di->id2.i_data.id_data;
  2390. limit = de_buf + i_size_read(dir);
  2391. while (de_buf < limit) {
  2392. de = (struct ocfs2_dir_entry *)de_buf;
  2393. if (de->name_len && de->inode)
  2394. dirent_count++;
  2395. de_buf += le16_to_cpu(de->rec_len);
  2396. }
  2397. /* We are careful to leave room for one extra record. */
  2398. return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
  2399. }
  2400. /*
  2401. * Expand rec_len of the rightmost dirent in a directory block so that it
  2402. * contains the end of our valid space for dirents. We do this during
  2403. * expansion from an inline directory to one with extents. The first dir block
  2404. * in that case is taken from the inline data portion of the inode block.
  2405. *
  2406. * This will also return the largest amount of contiguous space for a dirent
  2407. * in the block. That value is *not* necessarily the last dirent, even after
  2408. * expansion. The directory indexing code wants this value for free space
  2409. * accounting. We do this here since we're already walking the entire dir
  2410. * block.
  2411. *
  2412. * We add the dir trailer if this filesystem wants it.
  2413. */
  2414. static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
  2415. struct inode *dir)
  2416. {
  2417. struct super_block *sb = dir->i_sb;
  2418. struct ocfs2_dir_entry *de;
  2419. struct ocfs2_dir_entry *prev_de;
  2420. char *de_buf, *limit;
  2421. unsigned int new_size = sb->s_blocksize;
  2422. unsigned int bytes, this_hole;
  2423. unsigned int largest_hole = 0;
  2424. if (ocfs2_new_dir_wants_trailer(dir))
  2425. new_size = ocfs2_dir_trailer_blk_off(sb);
  2426. bytes = new_size - old_size;
  2427. limit = start + old_size;
  2428. de_buf = start;
  2429. de = (struct ocfs2_dir_entry *)de_buf;
  2430. do {
  2431. this_hole = ocfs2_figure_dirent_hole(de);
  2432. if (this_hole > largest_hole)
  2433. largest_hole = this_hole;
  2434. prev_de = de;
  2435. de_buf += le16_to_cpu(de->rec_len);
  2436. de = (struct ocfs2_dir_entry *)de_buf;
  2437. } while (de_buf < limit);
  2438. le16_add_cpu(&prev_de->rec_len, bytes);
  2439. /* We need to double check this after modification of the final
  2440. * dirent. */
  2441. this_hole = ocfs2_figure_dirent_hole(prev_de);
  2442. if (this_hole > largest_hole)
  2443. largest_hole = this_hole;
  2444. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  2445. return largest_hole;
  2446. return 0;
  2447. }
  2448. /*
  2449. * We allocate enough clusters to fulfill "blocks_wanted", but set
  2450. * i_size to exactly one block. Ocfs2_extend_dir() will handle the
  2451. * rest automatically for us.
  2452. *
  2453. * *first_block_bh is a pointer to the 1st data block allocated to the
  2454. * directory.
  2455. */
  2456. static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
  2457. unsigned int blocks_wanted,
  2458. struct ocfs2_dir_lookup_result *lookup,
  2459. struct buffer_head **first_block_bh)
  2460. {
  2461. u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
  2462. struct super_block *sb = dir->i_sb;
  2463. int ret, i, num_dx_leaves = 0, dx_inline = 0,
  2464. credits = ocfs2_inline_to_extents_credits(sb);
  2465. u64 dx_insert_blkno, blkno,
  2466. bytes = blocks_wanted << sb->s_blocksize_bits;
  2467. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2468. struct ocfs2_inode_info *oi = OCFS2_I(dir);
  2469. struct ocfs2_alloc_context *data_ac;
  2470. struct ocfs2_alloc_context *meta_ac = NULL;
  2471. struct buffer_head *dirdata_bh = NULL;
  2472. struct buffer_head *dx_root_bh = NULL;
  2473. struct buffer_head **dx_leaves = NULL;
  2474. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2475. handle_t *handle;
  2476. struct ocfs2_extent_tree et;
  2477. struct ocfs2_extent_tree dx_et;
  2478. int did_quota = 0, bytes_allocated = 0;
  2479. ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
  2480. alloc = ocfs2_clusters_for_bytes(sb, bytes);
  2481. dx_alloc = 0;
  2482. if (ocfs2_supports_indexed_dirs(osb)) {
  2483. credits += ocfs2_add_dir_index_credits(sb);
  2484. dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
  2485. if (!dx_inline) {
  2486. /* Add one more cluster for an index leaf */
  2487. dx_alloc++;
  2488. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
  2489. &num_dx_leaves);
  2490. if (!dx_leaves) {
  2491. ret = -ENOMEM;
  2492. mlog_errno(ret);
  2493. goto out;
  2494. }
  2495. }
  2496. /* This gets us the dx_root */
  2497. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  2498. if (ret) {
  2499. mlog_errno(ret);
  2500. goto out;
  2501. }
  2502. }
  2503. /*
  2504. * We should never need more than 2 clusters for the unindexed
  2505. * tree - maximum dirent size is far less than one block. In
  2506. * fact, the only time we'd need more than one cluster is if
  2507. * blocksize == clustersize and the dirent won't fit in the
  2508. * extra space that the expansion to a single block gives. As
  2509. * of today, that only happens on 4k/4k file systems.
  2510. */
  2511. BUG_ON(alloc > 2);
  2512. ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
  2513. if (ret) {
  2514. mlog_errno(ret);
  2515. goto out;
  2516. }
  2517. down_write(&oi->ip_alloc_sem);
  2518. /*
  2519. * Prepare for worst case allocation scenario of two separate
  2520. * extents in the unindexed tree.
  2521. */
  2522. if (alloc == 2)
  2523. credits += OCFS2_SUBALLOC_ALLOC;
  2524. handle = ocfs2_start_trans(osb, credits);
  2525. if (IS_ERR(handle)) {
  2526. ret = PTR_ERR(handle);
  2527. mlog_errno(ret);
  2528. goto out_sem;
  2529. }
  2530. if (vfs_dq_alloc_space_nodirty(dir,
  2531. ocfs2_clusters_to_bytes(osb->sb,
  2532. alloc + dx_alloc))) {
  2533. ret = -EDQUOT;
  2534. goto out_commit;
  2535. }
  2536. did_quota = 1;
  2537. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2538. /*
  2539. * Allocate our index cluster first, to maximize the
  2540. * possibility that unindexed leaves grow
  2541. * contiguously.
  2542. */
  2543. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
  2544. dx_leaves, num_dx_leaves,
  2545. &dx_insert_blkno);
  2546. if (ret) {
  2547. mlog_errno(ret);
  2548. goto out_commit;
  2549. }
  2550. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2551. }
  2552. /*
  2553. * Try to claim as many clusters as the bitmap can give though
  2554. * if we only get one now, that's enough to continue. The rest
  2555. * will be claimed after the conversion to extents.
  2556. */
  2557. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
  2558. if (ret) {
  2559. mlog_errno(ret);
  2560. goto out_commit;
  2561. }
  2562. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2563. /*
  2564. * Operations are carefully ordered so that we set up the new
  2565. * data block first. The conversion from inline data to
  2566. * extents follows.
  2567. */
  2568. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2569. dirdata_bh = sb_getblk(sb, blkno);
  2570. if (!dirdata_bh) {
  2571. ret = -EIO;
  2572. mlog_errno(ret);
  2573. goto out_commit;
  2574. }
  2575. ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
  2576. ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
  2577. OCFS2_JOURNAL_ACCESS_CREATE);
  2578. if (ret) {
  2579. mlog_errno(ret);
  2580. goto out_commit;
  2581. }
  2582. memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
  2583. memset(dirdata_bh->b_data + i_size_read(dir), 0,
  2584. sb->s_blocksize - i_size_read(dir));
  2585. i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
  2586. if (ocfs2_new_dir_wants_trailer(dir)) {
  2587. /*
  2588. * Prepare the dir trailer up front. It will otherwise look
  2589. * like a valid dirent. Even if inserting the index fails
  2590. * (unlikely), then all we'll have done is given first dir
  2591. * block a small amount of fragmentation.
  2592. */
  2593. ocfs2_init_dir_trailer(dir, dirdata_bh, i);
  2594. }
  2595. ret = ocfs2_journal_dirty(handle, dirdata_bh);
  2596. if (ret) {
  2597. mlog_errno(ret);
  2598. goto out_commit;
  2599. }
  2600. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2601. /*
  2602. * Dx dirs with an external cluster need to do this up
  2603. * front. Inline dx root's get handled later, after
  2604. * we've allocated our root block. We get passed back
  2605. * a total number of items so that dr_num_entries can
  2606. * be correctly set once the dx_root has been
  2607. * allocated.
  2608. */
  2609. ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
  2610. num_dx_leaves, &num_dx_entries,
  2611. dirdata_bh);
  2612. if (ret) {
  2613. mlog_errno(ret);
  2614. goto out_commit;
  2615. }
  2616. }
  2617. /*
  2618. * Set extent, i_size, etc on the directory. After this, the
  2619. * inode should contain the same exact dirents as before and
  2620. * be fully accessible from system calls.
  2621. *
  2622. * We let the later dirent insert modify c/mtime - to the user
  2623. * the data hasn't changed.
  2624. */
  2625. ret = ocfs2_journal_access_di(handle, dir, di_bh,
  2626. OCFS2_JOURNAL_ACCESS_CREATE);
  2627. if (ret) {
  2628. mlog_errno(ret);
  2629. goto out_commit;
  2630. }
  2631. spin_lock(&oi->ip_lock);
  2632. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  2633. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  2634. spin_unlock(&oi->ip_lock);
  2635. ocfs2_dinode_new_extent_list(dir, di);
  2636. i_size_write(dir, sb->s_blocksize);
  2637. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2638. di->i_size = cpu_to_le64(sb->s_blocksize);
  2639. di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
  2640. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
  2641. /*
  2642. * This should never fail as our extent list is empty and all
  2643. * related blocks have been journaled already.
  2644. */
  2645. ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
  2646. 0, NULL);
  2647. if (ret) {
  2648. mlog_errno(ret);
  2649. goto out_commit;
  2650. }
  2651. /*
  2652. * Set i_blocks after the extent insert for the most up to
  2653. * date ip_clusters value.
  2654. */
  2655. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2656. ret = ocfs2_journal_dirty(handle, di_bh);
  2657. if (ret) {
  2658. mlog_errno(ret);
  2659. goto out_commit;
  2660. }
  2661. if (ocfs2_supports_indexed_dirs(osb)) {
  2662. ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
  2663. dirdata_bh, meta_ac, dx_inline,
  2664. num_dx_entries, &dx_root_bh);
  2665. if (ret) {
  2666. mlog_errno(ret);
  2667. goto out_commit;
  2668. }
  2669. if (dx_inline) {
  2670. ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
  2671. dirdata_bh);
  2672. } else {
  2673. ocfs2_init_dx_root_extent_tree(&dx_et, dir, dx_root_bh);
  2674. ret = ocfs2_insert_extent(osb, handle, dir, &dx_et, 0,
  2675. dx_insert_blkno, 1, 0, NULL);
  2676. if (ret)
  2677. mlog_errno(ret);
  2678. }
  2679. }
  2680. /*
  2681. * We asked for two clusters, but only got one in the 1st
  2682. * pass. Claim the 2nd cluster as a separate extent.
  2683. */
  2684. if (alloc > len) {
  2685. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
  2686. &len);
  2687. if (ret) {
  2688. mlog_errno(ret);
  2689. goto out_commit;
  2690. }
  2691. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2692. ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
  2693. blkno, len, 0, NULL);
  2694. if (ret) {
  2695. mlog_errno(ret);
  2696. goto out_commit;
  2697. }
  2698. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2699. }
  2700. *first_block_bh = dirdata_bh;
  2701. dirdata_bh = NULL;
  2702. if (ocfs2_supports_indexed_dirs(osb)) {
  2703. unsigned int off;
  2704. if (!dx_inline) {
  2705. /*
  2706. * We need to return the correct block within the
  2707. * cluster which should hold our entry.
  2708. */
  2709. off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
  2710. &lookup->dl_hinfo);
  2711. get_bh(dx_leaves[off]);
  2712. lookup->dl_dx_leaf_bh = dx_leaves[off];
  2713. }
  2714. lookup->dl_dx_root_bh = dx_root_bh;
  2715. dx_root_bh = NULL;
  2716. }
  2717. out_commit:
  2718. if (ret < 0 && did_quota)
  2719. vfs_dq_free_space_nodirty(dir, bytes_allocated);
  2720. ocfs2_commit_trans(osb, handle);
  2721. out_sem:
  2722. up_write(&oi->ip_alloc_sem);
  2723. out:
  2724. if (data_ac)
  2725. ocfs2_free_alloc_context(data_ac);
  2726. if (meta_ac)
  2727. ocfs2_free_alloc_context(meta_ac);
  2728. if (dx_leaves) {
  2729. for (i = 0; i < num_dx_leaves; i++)
  2730. brelse(dx_leaves[i]);
  2731. kfree(dx_leaves);
  2732. }
  2733. brelse(dirdata_bh);
  2734. brelse(dx_root_bh);
  2735. return ret;
  2736. }
  2737. /* returns a bh of the 1st new block in the allocation. */
  2738. static int ocfs2_do_extend_dir(struct super_block *sb,
  2739. handle_t *handle,
  2740. struct inode *dir,
  2741. struct buffer_head *parent_fe_bh,
  2742. struct ocfs2_alloc_context *data_ac,
  2743. struct ocfs2_alloc_context *meta_ac,
  2744. struct buffer_head **new_bh)
  2745. {
  2746. int status;
  2747. int extend, did_quota = 0;
  2748. u64 p_blkno, v_blkno;
  2749. spin_lock(&OCFS2_I(dir)->ip_lock);
  2750. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  2751. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2752. if (extend) {
  2753. u32 offset = OCFS2_I(dir)->ip_clusters;
  2754. if (vfs_dq_alloc_space_nodirty(dir,
  2755. ocfs2_clusters_to_bytes(sb, 1))) {
  2756. status = -EDQUOT;
  2757. goto bail;
  2758. }
  2759. did_quota = 1;
  2760. status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
  2761. 1, 0, parent_fe_bh, handle,
  2762. data_ac, meta_ac, NULL);
  2763. BUG_ON(status == -EAGAIN);
  2764. if (status < 0) {
  2765. mlog_errno(status);
  2766. goto bail;
  2767. }
  2768. }
  2769. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  2770. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  2771. if (status < 0) {
  2772. mlog_errno(status);
  2773. goto bail;
  2774. }
  2775. *new_bh = sb_getblk(sb, p_blkno);
  2776. if (!*new_bh) {
  2777. status = -EIO;
  2778. mlog_errno(status);
  2779. goto bail;
  2780. }
  2781. status = 0;
  2782. bail:
  2783. if (did_quota && status < 0)
  2784. vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
  2785. mlog_exit(status);
  2786. return status;
  2787. }
  2788. /*
  2789. * Assumes you already have a cluster lock on the directory.
  2790. *
  2791. * 'blocks_wanted' is only used if we have an inline directory which
  2792. * is to be turned into an extent based one. The size of the dirent to
  2793. * insert might be larger than the space gained by growing to just one
  2794. * block, so we may have to grow the inode by two blocks in that case.
  2795. *
  2796. * If the directory is already indexed, dx_root_bh must be provided.
  2797. */
  2798. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  2799. struct inode *dir,
  2800. struct buffer_head *parent_fe_bh,
  2801. unsigned int blocks_wanted,
  2802. struct ocfs2_dir_lookup_result *lookup,
  2803. struct buffer_head **new_de_bh)
  2804. {
  2805. int status = 0;
  2806. int credits, num_free_extents, drop_alloc_sem = 0;
  2807. loff_t dir_i_size;
  2808. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  2809. struct ocfs2_extent_list *el = &fe->id2.i_list;
  2810. struct ocfs2_alloc_context *data_ac = NULL;
  2811. struct ocfs2_alloc_context *meta_ac = NULL;
  2812. handle_t *handle = NULL;
  2813. struct buffer_head *new_bh = NULL;
  2814. struct ocfs2_dir_entry * de;
  2815. struct super_block *sb = osb->sb;
  2816. struct ocfs2_extent_tree et;
  2817. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  2818. mlog_entry_void();
  2819. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2820. /*
  2821. * This would be a code error as an inline directory should
  2822. * never have an index root.
  2823. */
  2824. BUG_ON(dx_root_bh);
  2825. status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
  2826. blocks_wanted, lookup,
  2827. &new_bh);
  2828. if (status) {
  2829. mlog_errno(status);
  2830. goto bail;
  2831. }
  2832. /* Expansion from inline to an indexed directory will
  2833. * have given us this. */
  2834. dx_root_bh = lookup->dl_dx_root_bh;
  2835. if (blocks_wanted == 1) {
  2836. /*
  2837. * If the new dirent will fit inside the space
  2838. * created by pushing out to one block, then
  2839. * we can complete the operation
  2840. * here. Otherwise we have to expand i_size
  2841. * and format the 2nd block below.
  2842. */
  2843. BUG_ON(new_bh == NULL);
  2844. goto bail_bh;
  2845. }
  2846. /*
  2847. * Get rid of 'new_bh' - we want to format the 2nd
  2848. * data block and return that instead.
  2849. */
  2850. brelse(new_bh);
  2851. new_bh = NULL;
  2852. dir_i_size = i_size_read(dir);
  2853. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2854. goto do_extend;
  2855. }
  2856. dir_i_size = i_size_read(dir);
  2857. mlog(0, "extending dir %llu (i_size = %lld)\n",
  2858. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  2859. /* dir->i_size is always block aligned. */
  2860. spin_lock(&OCFS2_I(dir)->ip_lock);
  2861. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  2862. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2863. ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
  2864. num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
  2865. if (num_free_extents < 0) {
  2866. status = num_free_extents;
  2867. mlog_errno(status);
  2868. goto bail;
  2869. }
  2870. if (!num_free_extents) {
  2871. status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
  2872. if (status < 0) {
  2873. if (status != -ENOSPC)
  2874. mlog_errno(status);
  2875. goto bail;
  2876. }
  2877. }
  2878. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2879. if (status < 0) {
  2880. if (status != -ENOSPC)
  2881. mlog_errno(status);
  2882. goto bail;
  2883. }
  2884. credits = ocfs2_calc_extend_credits(sb, el, 1);
  2885. } else {
  2886. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2887. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2888. }
  2889. do_extend:
  2890. if (ocfs2_dir_indexed(dir))
  2891. credits++; /* For attaching the new dirent block to the
  2892. * dx_root */
  2893. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2894. drop_alloc_sem = 1;
  2895. handle = ocfs2_start_trans(osb, credits);
  2896. if (IS_ERR(handle)) {
  2897. status = PTR_ERR(handle);
  2898. handle = NULL;
  2899. mlog_errno(status);
  2900. goto bail;
  2901. }
  2902. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  2903. data_ac, meta_ac, &new_bh);
  2904. if (status < 0) {
  2905. mlog_errno(status);
  2906. goto bail;
  2907. }
  2908. ocfs2_set_new_buffer_uptodate(dir, new_bh);
  2909. status = ocfs2_journal_access_db(handle, dir, new_bh,
  2910. OCFS2_JOURNAL_ACCESS_CREATE);
  2911. if (status < 0) {
  2912. mlog_errno(status);
  2913. goto bail;
  2914. }
  2915. memset(new_bh->b_data, 0, sb->s_blocksize);
  2916. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  2917. de->inode = 0;
  2918. if (ocfs2_supports_dir_trailer(dir)) {
  2919. de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
  2920. ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
  2921. if (ocfs2_dir_indexed(dir)) {
  2922. status = ocfs2_dx_dir_link_trailer(dir, handle,
  2923. dx_root_bh, new_bh);
  2924. if (status) {
  2925. mlog_errno(status);
  2926. goto bail;
  2927. }
  2928. }
  2929. } else {
  2930. de->rec_len = cpu_to_le16(sb->s_blocksize);
  2931. }
  2932. status = ocfs2_journal_dirty(handle, new_bh);
  2933. if (status < 0) {
  2934. mlog_errno(status);
  2935. goto bail;
  2936. }
  2937. dir_i_size += dir->i_sb->s_blocksize;
  2938. i_size_write(dir, dir_i_size);
  2939. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2940. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  2941. if (status < 0) {
  2942. mlog_errno(status);
  2943. goto bail;
  2944. }
  2945. bail_bh:
  2946. *new_de_bh = new_bh;
  2947. get_bh(*new_de_bh);
  2948. bail:
  2949. if (drop_alloc_sem)
  2950. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  2951. if (handle)
  2952. ocfs2_commit_trans(osb, handle);
  2953. if (data_ac)
  2954. ocfs2_free_alloc_context(data_ac);
  2955. if (meta_ac)
  2956. ocfs2_free_alloc_context(meta_ac);
  2957. brelse(new_bh);
  2958. mlog_exit(status);
  2959. return status;
  2960. }
  2961. static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
  2962. const char *name, int namelen,
  2963. struct buffer_head **ret_de_bh,
  2964. unsigned int *blocks_wanted)
  2965. {
  2966. int ret;
  2967. struct super_block *sb = dir->i_sb;
  2968. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2969. struct ocfs2_dir_entry *de, *last_de = NULL;
  2970. char *de_buf, *limit;
  2971. unsigned long offset = 0;
  2972. unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
  2973. /*
  2974. * This calculates how many free bytes we'd have in block zero, should
  2975. * this function force expansion to an extent tree.
  2976. */
  2977. if (ocfs2_new_dir_wants_trailer(dir))
  2978. free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
  2979. else
  2980. free_space = dir->i_sb->s_blocksize - i_size_read(dir);
  2981. de_buf = di->id2.i_data.id_data;
  2982. limit = de_buf + i_size_read(dir);
  2983. rec_len = OCFS2_DIR_REC_LEN(namelen);
  2984. while (de_buf < limit) {
  2985. de = (struct ocfs2_dir_entry *)de_buf;
  2986. if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
  2987. ret = -ENOENT;
  2988. goto out;
  2989. }
  2990. if (ocfs2_match(namelen, name, de)) {
  2991. ret = -EEXIST;
  2992. goto out;
  2993. }
  2994. /*
  2995. * No need to check for a trailing dirent record here as
  2996. * they're not used for inline dirs.
  2997. */
  2998. if (ocfs2_dirent_would_fit(de, rec_len)) {
  2999. /* Ok, we found a spot. Return this bh and let
  3000. * the caller actually fill it in. */
  3001. *ret_de_bh = di_bh;
  3002. get_bh(*ret_de_bh);
  3003. ret = 0;
  3004. goto out;
  3005. }
  3006. last_de = de;
  3007. de_buf += le16_to_cpu(de->rec_len);
  3008. offset += le16_to_cpu(de->rec_len);
  3009. }
  3010. /*
  3011. * We're going to require expansion of the directory - figure
  3012. * out how many blocks we'll need so that a place for the
  3013. * dirent can be found.
  3014. */
  3015. *blocks_wanted = 1;
  3016. new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
  3017. if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
  3018. *blocks_wanted = 2;
  3019. ret = -ENOSPC;
  3020. out:
  3021. return ret;
  3022. }
  3023. static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
  3024. int namelen, struct buffer_head **ret_de_bh)
  3025. {
  3026. unsigned long offset;
  3027. struct buffer_head *bh = NULL;
  3028. unsigned short rec_len;
  3029. struct ocfs2_dir_entry *de;
  3030. struct super_block *sb = dir->i_sb;
  3031. int status;
  3032. int blocksize = dir->i_sb->s_blocksize;
  3033. status = ocfs2_read_dir_block(dir, 0, &bh, 0);
  3034. if (status) {
  3035. mlog_errno(status);
  3036. goto bail;
  3037. }
  3038. rec_len = OCFS2_DIR_REC_LEN(namelen);
  3039. offset = 0;
  3040. de = (struct ocfs2_dir_entry *) bh->b_data;
  3041. while (1) {
  3042. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  3043. brelse(bh);
  3044. bh = NULL;
  3045. if (i_size_read(dir) <= offset) {
  3046. /*
  3047. * Caller will have to expand this
  3048. * directory.
  3049. */
  3050. status = -ENOSPC;
  3051. goto bail;
  3052. }
  3053. status = ocfs2_read_dir_block(dir,
  3054. offset >> sb->s_blocksize_bits,
  3055. &bh, 0);
  3056. if (status) {
  3057. mlog_errno(status);
  3058. goto bail;
  3059. }
  3060. /* move to next block */
  3061. de = (struct ocfs2_dir_entry *) bh->b_data;
  3062. }
  3063. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  3064. status = -ENOENT;
  3065. goto bail;
  3066. }
  3067. if (ocfs2_match(namelen, name, de)) {
  3068. status = -EEXIST;
  3069. goto bail;
  3070. }
  3071. if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
  3072. blocksize))
  3073. goto next;
  3074. if (ocfs2_dirent_would_fit(de, rec_len)) {
  3075. /* Ok, we found a spot. Return this bh and let
  3076. * the caller actually fill it in. */
  3077. *ret_de_bh = bh;
  3078. get_bh(*ret_de_bh);
  3079. status = 0;
  3080. goto bail;
  3081. }
  3082. next:
  3083. offset += le16_to_cpu(de->rec_len);
  3084. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  3085. }
  3086. status = 0;
  3087. bail:
  3088. brelse(bh);
  3089. mlog_exit(status);
  3090. return status;
  3091. }
  3092. static int dx_leaf_sort_cmp(const void *a, const void *b)
  3093. {
  3094. const struct ocfs2_dx_entry *entry1 = a;
  3095. const struct ocfs2_dx_entry *entry2 = b;
  3096. u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
  3097. u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
  3098. u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
  3099. u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
  3100. if (major_hash1 > major_hash2)
  3101. return 1;
  3102. if (major_hash1 < major_hash2)
  3103. return -1;
  3104. /*
  3105. * It is not strictly necessary to sort by minor
  3106. */
  3107. if (minor_hash1 > minor_hash2)
  3108. return 1;
  3109. if (minor_hash1 < minor_hash2)
  3110. return -1;
  3111. return 0;
  3112. }
  3113. static void dx_leaf_sort_swap(void *a, void *b, int size)
  3114. {
  3115. struct ocfs2_dx_entry *entry1 = a;
  3116. struct ocfs2_dx_entry *entry2 = b;
  3117. struct ocfs2_dx_entry tmp;
  3118. BUG_ON(size != sizeof(*entry1));
  3119. tmp = *entry1;
  3120. *entry1 = *entry2;
  3121. *entry2 = tmp;
  3122. }
  3123. static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
  3124. {
  3125. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3126. int i, num = le16_to_cpu(dl_list->de_num_used);
  3127. for (i = 0; i < (num - 1); i++) {
  3128. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
  3129. le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
  3130. return 0;
  3131. }
  3132. return 1;
  3133. }
  3134. /*
  3135. * Find the optimal value to split this leaf on. This expects the leaf
  3136. * entries to be in sorted order.
  3137. *
  3138. * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
  3139. * the hash we want to insert.
  3140. *
  3141. * This function is only concerned with the major hash - that which
  3142. * determines which cluster an item belongs to.
  3143. */
  3144. static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
  3145. u32 leaf_cpos, u32 insert_hash,
  3146. u32 *split_hash)
  3147. {
  3148. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3149. int i, num_used = le16_to_cpu(dl_list->de_num_used);
  3150. int allsame;
  3151. /*
  3152. * There's a couple rare, but nasty corner cases we have to
  3153. * check for here. All of them involve a leaf where all value
  3154. * have the same hash, which is what we look for first.
  3155. *
  3156. * Most of the time, all of the above is false, and we simply
  3157. * pick the median value for a split.
  3158. */
  3159. allsame = ocfs2_dx_leaf_same_major(dx_leaf);
  3160. if (allsame) {
  3161. u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
  3162. if (val == insert_hash) {
  3163. /*
  3164. * No matter where we would choose to split,
  3165. * the new entry would want to occupy the same
  3166. * block as these. Since there's no space left
  3167. * in their existing block, we know there
  3168. * won't be space after the split.
  3169. */
  3170. return -ENOSPC;
  3171. }
  3172. if (val == leaf_cpos) {
  3173. /*
  3174. * Because val is the same as leaf_cpos (which
  3175. * is the smallest value this leaf can have),
  3176. * yet is not equal to insert_hash, then we
  3177. * know that insert_hash *must* be larger than
  3178. * val (and leaf_cpos). At least cpos+1 in value.
  3179. *
  3180. * We also know then, that there cannot be an
  3181. * adjacent extent (otherwise we'd be looking
  3182. * at it). Choosing this value gives us a
  3183. * chance to get some contiguousness.
  3184. */
  3185. *split_hash = leaf_cpos + 1;
  3186. return 0;
  3187. }
  3188. if (val > insert_hash) {
  3189. /*
  3190. * val can not be the same as insert hash, and
  3191. * also must be larger than leaf_cpos. Also,
  3192. * we know that there can't be a leaf between
  3193. * cpos and val, otherwise the entries with
  3194. * hash 'val' would be there.
  3195. */
  3196. *split_hash = val;
  3197. return 0;
  3198. }
  3199. *split_hash = insert_hash;
  3200. return 0;
  3201. }
  3202. /*
  3203. * Since the records are sorted and the checks above
  3204. * guaranteed that not all records in this block are the same,
  3205. * we simple travel forward, from the median, and pick the 1st
  3206. * record whose value is larger than leaf_cpos.
  3207. */
  3208. for (i = (num_used / 2); i < num_used; i++)
  3209. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
  3210. leaf_cpos)
  3211. break;
  3212. BUG_ON(i == num_used); /* Should be impossible */
  3213. *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
  3214. return 0;
  3215. }
  3216. /*
  3217. * Transfer all entries in orig_dx_leaves whose major hash is equal to or
  3218. * larger than split_hash into new_dx_leaves. We use a temporary
  3219. * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
  3220. *
  3221. * Since the block offset inside a leaf (cluster) is a constant mask
  3222. * of minor_hash, we can optimize - an item at block offset X within
  3223. * the original cluster, will be at offset X within the new cluster.
  3224. */
  3225. static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
  3226. handle_t *handle,
  3227. struct ocfs2_dx_leaf *tmp_dx_leaf,
  3228. struct buffer_head **orig_dx_leaves,
  3229. struct buffer_head **new_dx_leaves,
  3230. int num_dx_leaves)
  3231. {
  3232. int i, j, num_used;
  3233. u32 major_hash;
  3234. struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
  3235. struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
  3236. struct ocfs2_dx_entry *dx_entry;
  3237. tmp_list = &tmp_dx_leaf->dl_list;
  3238. for (i = 0; i < num_dx_leaves; i++) {
  3239. orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
  3240. orig_list = &orig_dx_leaf->dl_list;
  3241. new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
  3242. new_list = &new_dx_leaf->dl_list;
  3243. num_used = le16_to_cpu(orig_list->de_num_used);
  3244. memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
  3245. tmp_list->de_num_used = cpu_to_le16(0);
  3246. memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
  3247. for (j = 0; j < num_used; j++) {
  3248. dx_entry = &orig_list->de_entries[j];
  3249. major_hash = le32_to_cpu(dx_entry->dx_major_hash);
  3250. if (major_hash >= split_hash)
  3251. ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
  3252. dx_entry);
  3253. else
  3254. ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
  3255. dx_entry);
  3256. }
  3257. memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
  3258. ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
  3259. ocfs2_journal_dirty(handle, new_dx_leaves[i]);
  3260. }
  3261. }
  3262. static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
  3263. struct ocfs2_dx_root_block *dx_root)
  3264. {
  3265. int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
  3266. credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
  3267. credits += ocfs2_quota_trans_credits(osb->sb);
  3268. return credits;
  3269. }
  3270. /*
  3271. * Find the median value in dx_leaf_bh and allocate a new leaf to move
  3272. * half our entries into.
  3273. */
  3274. static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
  3275. struct buffer_head *dx_root_bh,
  3276. struct buffer_head *dx_leaf_bh,
  3277. struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
  3278. u64 leaf_blkno)
  3279. {
  3280. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3281. int credits, ret, i, num_used, did_quota = 0;
  3282. u32 cpos, split_hash, insert_hash = hinfo->major_hash;
  3283. u64 orig_leaves_start;
  3284. int num_dx_leaves;
  3285. struct buffer_head **orig_dx_leaves = NULL;
  3286. struct buffer_head **new_dx_leaves = NULL;
  3287. struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
  3288. struct ocfs2_extent_tree et;
  3289. handle_t *handle = NULL;
  3290. struct ocfs2_dx_root_block *dx_root;
  3291. struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
  3292. mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
  3293. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  3294. (unsigned long long)leaf_blkno, insert_hash);
  3295. ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
  3296. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3297. /*
  3298. * XXX: This is a rather large limit. We should use a more
  3299. * realistic value.
  3300. */
  3301. if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
  3302. return -ENOSPC;
  3303. num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  3304. if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3305. mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
  3306. "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
  3307. (unsigned long long)leaf_blkno, num_used);
  3308. ret = -EIO;
  3309. goto out;
  3310. }
  3311. orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3312. if (!orig_dx_leaves) {
  3313. ret = -ENOMEM;
  3314. mlog_errno(ret);
  3315. goto out;
  3316. }
  3317. new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
  3318. if (!new_dx_leaves) {
  3319. ret = -ENOMEM;
  3320. mlog_errno(ret);
  3321. goto out;
  3322. }
  3323. ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
  3324. if (ret) {
  3325. if (ret != -ENOSPC)
  3326. mlog_errno(ret);
  3327. goto out;
  3328. }
  3329. credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
  3330. handle = ocfs2_start_trans(osb, credits);
  3331. if (IS_ERR(handle)) {
  3332. ret = PTR_ERR(handle);
  3333. handle = NULL;
  3334. mlog_errno(ret);
  3335. goto out;
  3336. }
  3337. if (vfs_dq_alloc_space_nodirty(dir,
  3338. ocfs2_clusters_to_bytes(dir->i_sb, 1))) {
  3339. ret = -EDQUOT;
  3340. goto out_commit;
  3341. }
  3342. did_quota = 1;
  3343. ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
  3344. OCFS2_JOURNAL_ACCESS_WRITE);
  3345. if (ret) {
  3346. mlog_errno(ret);
  3347. goto out_commit;
  3348. }
  3349. /*
  3350. * This block is changing anyway, so we can sort it in place.
  3351. */
  3352. sort(dx_leaf->dl_list.de_entries, num_used,
  3353. sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
  3354. dx_leaf_sort_swap);
  3355. ret = ocfs2_journal_dirty(handle, dx_leaf_bh);
  3356. if (ret) {
  3357. mlog_errno(ret);
  3358. goto out_commit;
  3359. }
  3360. ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
  3361. &split_hash);
  3362. if (ret) {
  3363. mlog_errno(ret);
  3364. goto out_commit;
  3365. }
  3366. mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
  3367. leaf_cpos, split_hash, insert_hash);
  3368. /*
  3369. * We have to carefully order operations here. There are items
  3370. * which want to be in the new cluster before insert, but in
  3371. * order to put those items in the new cluster, we alter the
  3372. * old cluster. A failure to insert gets nasty.
  3373. *
  3374. * So, start by reserving writes to the old
  3375. * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
  3376. * the new cluster for us, before inserting it. The insert
  3377. * won't happen if there's an error before that. Once the
  3378. * insert is done then, we can transfer from one leaf into the
  3379. * other without fear of hitting any error.
  3380. */
  3381. /*
  3382. * The leaf transfer wants some scratch space so that we don't
  3383. * wind up doing a bunch of expensive memmove().
  3384. */
  3385. tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
  3386. if (!tmp_dx_leaf) {
  3387. ret = -ENOMEM;
  3388. mlog_errno(ret);
  3389. goto out_commit;
  3390. }
  3391. orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
  3392. ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
  3393. orig_dx_leaves);
  3394. if (ret) {
  3395. mlog_errno(ret);
  3396. goto out_commit;
  3397. }
  3398. for (i = 0; i < num_dx_leaves; i++) {
  3399. ret = ocfs2_journal_access_dl(handle, dir, orig_dx_leaves[i],
  3400. OCFS2_JOURNAL_ACCESS_WRITE);
  3401. if (ret) {
  3402. mlog_errno(ret);
  3403. goto out_commit;
  3404. }
  3405. }
  3406. cpos = split_hash;
  3407. ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
  3408. data_ac, meta_ac, new_dx_leaves,
  3409. num_dx_leaves);
  3410. if (ret) {
  3411. mlog_errno(ret);
  3412. goto out_commit;
  3413. }
  3414. ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
  3415. orig_dx_leaves, new_dx_leaves, num_dx_leaves);
  3416. out_commit:
  3417. if (ret < 0 && did_quota)
  3418. vfs_dq_free_space_nodirty(dir,
  3419. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3420. ocfs2_commit_trans(osb, handle);
  3421. out:
  3422. if (orig_dx_leaves || new_dx_leaves) {
  3423. for (i = 0; i < num_dx_leaves; i++) {
  3424. if (orig_dx_leaves)
  3425. brelse(orig_dx_leaves[i]);
  3426. if (new_dx_leaves)
  3427. brelse(new_dx_leaves[i]);
  3428. }
  3429. kfree(orig_dx_leaves);
  3430. kfree(new_dx_leaves);
  3431. }
  3432. if (meta_ac)
  3433. ocfs2_free_alloc_context(meta_ac);
  3434. if (data_ac)
  3435. ocfs2_free_alloc_context(data_ac);
  3436. kfree(tmp_dx_leaf);
  3437. return ret;
  3438. }
  3439. static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
  3440. struct buffer_head *di_bh,
  3441. struct buffer_head *dx_root_bh,
  3442. const char *name, int namelen,
  3443. struct ocfs2_dir_lookup_result *lookup)
  3444. {
  3445. int ret, rebalanced = 0;
  3446. struct ocfs2_dx_root_block *dx_root;
  3447. struct buffer_head *dx_leaf_bh = NULL;
  3448. struct ocfs2_dx_leaf *dx_leaf;
  3449. u64 blkno;
  3450. u32 leaf_cpos;
  3451. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3452. restart_search:
  3453. ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
  3454. &leaf_cpos, &blkno);
  3455. if (ret) {
  3456. mlog_errno(ret);
  3457. goto out;
  3458. }
  3459. ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
  3460. if (ret) {
  3461. mlog_errno(ret);
  3462. goto out;
  3463. }
  3464. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3465. if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
  3466. le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3467. if (rebalanced) {
  3468. /*
  3469. * Rebalancing should have provided us with
  3470. * space in an appropriate leaf.
  3471. *
  3472. * XXX: Is this an abnormal condition then?
  3473. * Should we print a message here?
  3474. */
  3475. ret = -ENOSPC;
  3476. goto out;
  3477. }
  3478. ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
  3479. &lookup->dl_hinfo, leaf_cpos,
  3480. blkno);
  3481. if (ret) {
  3482. if (ret != -ENOSPC)
  3483. mlog_errno(ret);
  3484. goto out;
  3485. }
  3486. /*
  3487. * Restart the lookup. The rebalance might have
  3488. * changed which block our item fits into. Mark our
  3489. * progress, so we only execute this once.
  3490. */
  3491. brelse(dx_leaf_bh);
  3492. dx_leaf_bh = NULL;
  3493. rebalanced = 1;
  3494. goto restart_search;
  3495. }
  3496. lookup->dl_dx_leaf_bh = dx_leaf_bh;
  3497. dx_leaf_bh = NULL;
  3498. out:
  3499. brelse(dx_leaf_bh);
  3500. return ret;
  3501. }
  3502. static int ocfs2_search_dx_free_list(struct inode *dir,
  3503. struct buffer_head *dx_root_bh,
  3504. int namelen,
  3505. struct ocfs2_dir_lookup_result *lookup)
  3506. {
  3507. int ret = -ENOSPC;
  3508. struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
  3509. struct ocfs2_dir_block_trailer *db;
  3510. u64 next_block;
  3511. int rec_len = OCFS2_DIR_REC_LEN(namelen);
  3512. struct ocfs2_dx_root_block *dx_root;
  3513. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3514. next_block = le64_to_cpu(dx_root->dr_free_blk);
  3515. while (next_block) {
  3516. brelse(prev_leaf_bh);
  3517. prev_leaf_bh = leaf_bh;
  3518. leaf_bh = NULL;
  3519. ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
  3520. if (ret) {
  3521. mlog_errno(ret);
  3522. goto out;
  3523. }
  3524. db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  3525. if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
  3526. lookup->dl_leaf_bh = leaf_bh;
  3527. lookup->dl_prev_leaf_bh = prev_leaf_bh;
  3528. leaf_bh = NULL;
  3529. prev_leaf_bh = NULL;
  3530. break;
  3531. }
  3532. next_block = le64_to_cpu(db->db_free_next);
  3533. }
  3534. if (!next_block)
  3535. ret = -ENOSPC;
  3536. out:
  3537. brelse(leaf_bh);
  3538. brelse(prev_leaf_bh);
  3539. return ret;
  3540. }
  3541. static int ocfs2_expand_inline_dx_root(struct inode *dir,
  3542. struct buffer_head *dx_root_bh)
  3543. {
  3544. int ret, num_dx_leaves, i, j, did_quota = 0;
  3545. struct buffer_head **dx_leaves = NULL;
  3546. struct ocfs2_extent_tree et;
  3547. u64 insert_blkno;
  3548. struct ocfs2_alloc_context *data_ac = NULL;
  3549. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3550. handle_t *handle = NULL;
  3551. struct ocfs2_dx_root_block *dx_root;
  3552. struct ocfs2_dx_entry_list *entry_list;
  3553. struct ocfs2_dx_entry *dx_entry;
  3554. struct ocfs2_dx_leaf *target_leaf;
  3555. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  3556. if (ret) {
  3557. mlog_errno(ret);
  3558. goto out;
  3559. }
  3560. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3561. if (!dx_leaves) {
  3562. ret = -ENOMEM;
  3563. mlog_errno(ret);
  3564. goto out;
  3565. }
  3566. handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
  3567. if (IS_ERR(handle)) {
  3568. ret = PTR_ERR(handle);
  3569. mlog_errno(ret);
  3570. goto out;
  3571. }
  3572. if (vfs_dq_alloc_space_nodirty(dir,
  3573. ocfs2_clusters_to_bytes(osb->sb, 1))) {
  3574. ret = -EDQUOT;
  3575. goto out_commit;
  3576. }
  3577. did_quota = 1;
  3578. /*
  3579. * We do this up front, before the allocation, so that a
  3580. * failure to add the dx_root_bh to the journal won't result
  3581. * us losing clusters.
  3582. */
  3583. ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
  3584. OCFS2_JOURNAL_ACCESS_WRITE);
  3585. if (ret) {
  3586. mlog_errno(ret);
  3587. goto out_commit;
  3588. }
  3589. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
  3590. num_dx_leaves, &insert_blkno);
  3591. if (ret) {
  3592. mlog_errno(ret);
  3593. goto out_commit;
  3594. }
  3595. /*
  3596. * Transfer the entries from our dx_root into the appropriate
  3597. * block
  3598. */
  3599. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3600. entry_list = &dx_root->dr_entries;
  3601. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  3602. dx_entry = &entry_list->de_entries[i];
  3603. j = __ocfs2_dx_dir_hash_idx(osb,
  3604. le32_to_cpu(dx_entry->dx_minor_hash));
  3605. target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
  3606. ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
  3607. /* Each leaf has been passed to the journal already
  3608. * via __ocfs2_dx_dir_new_cluster() */
  3609. }
  3610. dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
  3611. memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
  3612. offsetof(struct ocfs2_dx_root_block, dr_list));
  3613. dx_root->dr_list.l_count =
  3614. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  3615. /* This should never fail considering we start with an empty
  3616. * dx_root. */
  3617. ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
  3618. ret = ocfs2_insert_extent(osb, handle, dir, &et, 0,
  3619. insert_blkno, 1, 0, NULL);
  3620. if (ret)
  3621. mlog_errno(ret);
  3622. did_quota = 0;
  3623. ocfs2_journal_dirty(handle, dx_root_bh);
  3624. out_commit:
  3625. if (ret < 0 && did_quota)
  3626. vfs_dq_free_space_nodirty(dir,
  3627. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3628. ocfs2_commit_trans(osb, handle);
  3629. out:
  3630. if (data_ac)
  3631. ocfs2_free_alloc_context(data_ac);
  3632. if (dx_leaves) {
  3633. for (i = 0; i < num_dx_leaves; i++)
  3634. brelse(dx_leaves[i]);
  3635. kfree(dx_leaves);
  3636. }
  3637. return ret;
  3638. }
  3639. static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
  3640. {
  3641. struct ocfs2_dx_root_block *dx_root;
  3642. struct ocfs2_dx_entry_list *entry_list;
  3643. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3644. entry_list = &dx_root->dr_entries;
  3645. if (le16_to_cpu(entry_list->de_num_used) >=
  3646. le16_to_cpu(entry_list->de_count))
  3647. return -ENOSPC;
  3648. return 0;
  3649. }
  3650. static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
  3651. struct buffer_head *di_bh,
  3652. const char *name,
  3653. int namelen,
  3654. struct ocfs2_dir_lookup_result *lookup)
  3655. {
  3656. int ret, free_dx_root = 1;
  3657. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3658. struct buffer_head *dx_root_bh = NULL;
  3659. struct buffer_head *leaf_bh = NULL;
  3660. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3661. struct ocfs2_dx_root_block *dx_root;
  3662. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3663. if (ret) {
  3664. mlog_errno(ret);
  3665. goto out;
  3666. }
  3667. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3668. if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
  3669. ret = -ENOSPC;
  3670. mlog_errno(ret);
  3671. goto out;
  3672. }
  3673. if (ocfs2_dx_root_inline(dx_root)) {
  3674. ret = ocfs2_inline_dx_has_space(dx_root_bh);
  3675. if (ret == 0)
  3676. goto search_el;
  3677. /*
  3678. * We ran out of room in the root block. Expand it to
  3679. * an extent, then allow ocfs2_find_dir_space_dx to do
  3680. * the rest.
  3681. */
  3682. ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
  3683. if (ret) {
  3684. mlog_errno(ret);
  3685. goto out;
  3686. }
  3687. }
  3688. /*
  3689. * Insert preparation for an indexed directory is split into two
  3690. * steps. The call to find_dir_space_dx reserves room in the index for
  3691. * an additional item. If we run out of space there, it's a real error
  3692. * we can't continue on.
  3693. */
  3694. ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
  3695. namelen, lookup);
  3696. if (ret) {
  3697. mlog_errno(ret);
  3698. goto out;
  3699. }
  3700. search_el:
  3701. /*
  3702. * Next, we need to find space in the unindexed tree. This call
  3703. * searches using the free space linked list. If the unindexed tree
  3704. * lacks sufficient space, we'll expand it below. The expansion code
  3705. * is smart enough to add any new blocks to the free space list.
  3706. */
  3707. ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
  3708. if (ret && ret != -ENOSPC) {
  3709. mlog_errno(ret);
  3710. goto out;
  3711. }
  3712. /* Do this up here - ocfs2_extend_dir might need the dx_root */
  3713. lookup->dl_dx_root_bh = dx_root_bh;
  3714. free_dx_root = 0;
  3715. if (ret == -ENOSPC) {
  3716. ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
  3717. if (ret) {
  3718. mlog_errno(ret);
  3719. goto out;
  3720. }
  3721. /*
  3722. * We make the assumption here that new leaf blocks are added
  3723. * to the front of our free list.
  3724. */
  3725. lookup->dl_prev_leaf_bh = NULL;
  3726. lookup->dl_leaf_bh = leaf_bh;
  3727. }
  3728. out:
  3729. if (free_dx_root)
  3730. brelse(dx_root_bh);
  3731. return ret;
  3732. }
  3733. /*
  3734. * Get a directory ready for insert. Any directory allocation required
  3735. * happens here. Success returns zero, and enough context in the dir
  3736. * lookup result that ocfs2_add_entry() will be able complete the task
  3737. * with minimal performance impact.
  3738. */
  3739. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  3740. struct inode *dir,
  3741. struct buffer_head *parent_fe_bh,
  3742. const char *name,
  3743. int namelen,
  3744. struct ocfs2_dir_lookup_result *lookup)
  3745. {
  3746. int ret;
  3747. unsigned int blocks_wanted = 1;
  3748. struct buffer_head *bh = NULL;
  3749. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  3750. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  3751. if (!namelen) {
  3752. ret = -EINVAL;
  3753. mlog_errno(ret);
  3754. goto out;
  3755. }
  3756. /*
  3757. * Do this up front to reduce confusion.
  3758. *
  3759. * The directory might start inline, then be turned into an
  3760. * indexed one, in which case we'd need to hash deep inside
  3761. * ocfs2_find_dir_space_id(). Since
  3762. * ocfs2_prepare_dx_dir_for_insert() also needs this hash
  3763. * done, there seems no point in spreading out the calls. We
  3764. * can optimize away the case where the file system doesn't
  3765. * support indexing.
  3766. */
  3767. if (ocfs2_supports_indexed_dirs(osb))
  3768. ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
  3769. if (ocfs2_dir_indexed(dir)) {
  3770. ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
  3771. name, namelen, lookup);
  3772. if (ret)
  3773. mlog_errno(ret);
  3774. goto out;
  3775. }
  3776. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  3777. ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
  3778. namelen, &bh, &blocks_wanted);
  3779. } else
  3780. ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
  3781. if (ret && ret != -ENOSPC) {
  3782. mlog_errno(ret);
  3783. goto out;
  3784. }
  3785. if (ret == -ENOSPC) {
  3786. /*
  3787. * We have to expand the directory to add this name.
  3788. */
  3789. BUG_ON(bh);
  3790. ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
  3791. lookup, &bh);
  3792. if (ret) {
  3793. if (ret != -ENOSPC)
  3794. mlog_errno(ret);
  3795. goto out;
  3796. }
  3797. BUG_ON(!bh);
  3798. }
  3799. lookup->dl_leaf_bh = bh;
  3800. bh = NULL;
  3801. out:
  3802. brelse(bh);
  3803. return ret;
  3804. }
  3805. static int ocfs2_dx_dir_remove_index(struct inode *dir,
  3806. struct buffer_head *di_bh,
  3807. struct buffer_head *dx_root_bh)
  3808. {
  3809. int ret;
  3810. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3811. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3812. struct ocfs2_dx_root_block *dx_root;
  3813. struct inode *dx_alloc_inode = NULL;
  3814. struct buffer_head *dx_alloc_bh = NULL;
  3815. handle_t *handle;
  3816. u64 blk;
  3817. u16 bit;
  3818. u64 bg_blkno;
  3819. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3820. dx_alloc_inode = ocfs2_get_system_file_inode(osb,
  3821. EXTENT_ALLOC_SYSTEM_INODE,
  3822. le16_to_cpu(dx_root->dr_suballoc_slot));
  3823. if (!dx_alloc_inode) {
  3824. ret = -ENOMEM;
  3825. mlog_errno(ret);
  3826. goto out;
  3827. }
  3828. mutex_lock(&dx_alloc_inode->i_mutex);
  3829. ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
  3830. if (ret) {
  3831. mlog_errno(ret);
  3832. goto out_mutex;
  3833. }
  3834. handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
  3835. if (IS_ERR(handle)) {
  3836. ret = PTR_ERR(handle);
  3837. mlog_errno(ret);
  3838. goto out_unlock;
  3839. }
  3840. ret = ocfs2_journal_access_di(handle, dir, di_bh,
  3841. OCFS2_JOURNAL_ACCESS_WRITE);
  3842. if (ret) {
  3843. mlog_errno(ret);
  3844. goto out_commit;
  3845. }
  3846. OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
  3847. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  3848. di->i_dx_root = cpu_to_le64(0ULL);
  3849. ocfs2_journal_dirty(handle, di_bh);
  3850. blk = le64_to_cpu(dx_root->dr_blkno);
  3851. bit = le16_to_cpu(dx_root->dr_suballoc_bit);
  3852. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  3853. ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
  3854. bit, bg_blkno, 1);
  3855. if (ret)
  3856. mlog_errno(ret);
  3857. out_commit:
  3858. ocfs2_commit_trans(osb, handle);
  3859. out_unlock:
  3860. ocfs2_inode_unlock(dx_alloc_inode, 1);
  3861. out_mutex:
  3862. mutex_unlock(&dx_alloc_inode->i_mutex);
  3863. brelse(dx_alloc_bh);
  3864. out:
  3865. iput(dx_alloc_inode);
  3866. return ret;
  3867. }
  3868. int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
  3869. {
  3870. int ret;
  3871. unsigned int uninitialized_var(clen);
  3872. u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
  3873. u64 uninitialized_var(blkno);
  3874. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3875. struct buffer_head *dx_root_bh = NULL;
  3876. struct ocfs2_dx_root_block *dx_root;
  3877. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3878. struct ocfs2_cached_dealloc_ctxt dealloc;
  3879. struct ocfs2_extent_tree et;
  3880. ocfs2_init_dealloc_ctxt(&dealloc);
  3881. if (!ocfs2_dir_indexed(dir))
  3882. return 0;
  3883. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3884. if (ret) {
  3885. mlog_errno(ret);
  3886. goto out;
  3887. }
  3888. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3889. if (ocfs2_dx_root_inline(dx_root))
  3890. goto remove_index;
  3891. ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
  3892. /* XXX: What if dr_clusters is too large? */
  3893. while (le32_to_cpu(dx_root->dr_clusters)) {
  3894. ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
  3895. major_hash, &cpos, &blkno, &clen);
  3896. if (ret) {
  3897. mlog_errno(ret);
  3898. goto out;
  3899. }
  3900. p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
  3901. ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen,
  3902. &dealloc);
  3903. if (ret) {
  3904. mlog_errno(ret);
  3905. goto out;
  3906. }
  3907. if (cpos == 0)
  3908. break;
  3909. major_hash = cpos - 1;
  3910. }
  3911. remove_index:
  3912. ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
  3913. if (ret) {
  3914. mlog_errno(ret);
  3915. goto out;
  3916. }
  3917. ocfs2_remove_from_cache(dir, dx_root_bh);
  3918. out:
  3919. ocfs2_schedule_truncate_log_flush(osb, 1);
  3920. ocfs2_run_deallocs(osb, &dealloc);
  3921. brelse(dx_root_bh);
  3922. return ret;
  3923. }