dir.c 114 KB

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