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. break;
  1637. }
  1638. if (le64_to_cpu(de->inode)) {
  1639. unsigned char d_type = DT_UNKNOWN;
  1640. if (de->file_type < OCFS2_FT_MAX)
  1641. d_type = ocfs2_filetype_table[de->file_type];
  1642. if (!dir_emit(ctx, de->name,
  1643. de->name_len,
  1644. le64_to_cpu(de->inode),
  1645. d_type)) {
  1646. brelse(bh);
  1647. return 0;
  1648. }
  1649. stored++;
  1650. }
  1651. offset += le16_to_cpu(de->rec_len);
  1652. ctx->pos += le16_to_cpu(de->rec_len);
  1653. }
  1654. offset = 0;
  1655. brelse(bh);
  1656. bh = NULL;
  1657. if (!persist && stored)
  1658. break;
  1659. }
  1660. return 0;
  1661. }
  1662. static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
  1663. struct dir_context *ctx,
  1664. bool persist)
  1665. {
  1666. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1667. return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
  1668. return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
  1669. }
  1670. /*
  1671. * This is intended to be called from inside other kernel functions,
  1672. * so we fake some arguments.
  1673. */
  1674. int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
  1675. {
  1676. u64 version = inode_query_iversion(inode);
  1677. ocfs2_dir_foreach_blk(inode, &version, ctx, true);
  1678. return 0;
  1679. }
  1680. /*
  1681. * ocfs2_readdir()
  1682. *
  1683. */
  1684. int ocfs2_readdir(struct file *file, struct dir_context *ctx)
  1685. {
  1686. int error = 0;
  1687. struct inode *inode = file_inode(file);
  1688. int lock_level = 0;
  1689. trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
  1690. error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1);
  1691. if (lock_level && error >= 0) {
  1692. /* We release EX lock which used to update atime
  1693. * and get PR lock again to reduce contention
  1694. * on commonly accessed directories. */
  1695. ocfs2_inode_unlock(inode, 1);
  1696. lock_level = 0;
  1697. error = ocfs2_inode_lock(inode, NULL, 0);
  1698. }
  1699. if (error < 0) {
  1700. if (error != -ENOENT)
  1701. mlog_errno(error);
  1702. /* we haven't got any yet, so propagate the error. */
  1703. goto bail_nolock;
  1704. }
  1705. error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false);
  1706. ocfs2_inode_unlock(inode, lock_level);
  1707. if (error)
  1708. mlog_errno(error);
  1709. bail_nolock:
  1710. return error;
  1711. }
  1712. /*
  1713. * NOTE: this should always be called with parent dir i_mutex taken.
  1714. */
  1715. int ocfs2_find_files_on_disk(const char *name,
  1716. int namelen,
  1717. u64 *blkno,
  1718. struct inode *inode,
  1719. struct ocfs2_dir_lookup_result *lookup)
  1720. {
  1721. int status = -ENOENT;
  1722. trace_ocfs2_find_files_on_disk(namelen, name, blkno,
  1723. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1724. status = ocfs2_find_entry(name, namelen, inode, lookup);
  1725. if (status)
  1726. goto leave;
  1727. *blkno = le64_to_cpu(lookup->dl_entry->inode);
  1728. status = 0;
  1729. leave:
  1730. return status;
  1731. }
  1732. /*
  1733. * Convenience function for callers which just want the block number
  1734. * mapped to a name and don't require the full dirent info, etc.
  1735. */
  1736. int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  1737. int namelen, u64 *blkno)
  1738. {
  1739. int ret;
  1740. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1741. ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
  1742. ocfs2_free_dir_lookup_result(&lookup);
  1743. return ret;
  1744. }
  1745. /* Check for a name within a directory.
  1746. *
  1747. * Return 0 if the name does not exist
  1748. * Return -EEXIST if the directory contains the name
  1749. *
  1750. * Callers should have i_mutex + a cluster lock on dir
  1751. */
  1752. int ocfs2_check_dir_for_entry(struct inode *dir,
  1753. const char *name,
  1754. int namelen)
  1755. {
  1756. int ret = 0;
  1757. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1758. trace_ocfs2_check_dir_for_entry(
  1759. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  1760. if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) {
  1761. ret = -EEXIST;
  1762. mlog_errno(ret);
  1763. }
  1764. ocfs2_free_dir_lookup_result(&lookup);
  1765. return ret;
  1766. }
  1767. struct ocfs2_empty_dir_priv {
  1768. struct dir_context ctx;
  1769. unsigned seen_dot;
  1770. unsigned seen_dot_dot;
  1771. unsigned seen_other;
  1772. unsigned dx_dir;
  1773. };
  1774. static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
  1775. int name_len, loff_t pos, u64 ino,
  1776. unsigned type)
  1777. {
  1778. struct ocfs2_empty_dir_priv *p =
  1779. container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
  1780. /*
  1781. * Check the positions of "." and ".." records to be sure
  1782. * they're in the correct place.
  1783. *
  1784. * Indexed directories don't need to proceed past the first
  1785. * two entries, so we end the scan after seeing '..'. Despite
  1786. * that, we allow the scan to proceed In the event that we
  1787. * have a corrupted indexed directory (no dot or dot dot
  1788. * entries). This allows us to double check for existing
  1789. * entries which might not have been found in the index.
  1790. */
  1791. if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  1792. p->seen_dot = 1;
  1793. return 0;
  1794. }
  1795. if (name_len == 2 && !strncmp("..", name, 2) &&
  1796. pos == OCFS2_DIR_REC_LEN(1)) {
  1797. p->seen_dot_dot = 1;
  1798. if (p->dx_dir && p->seen_dot)
  1799. return 1;
  1800. return 0;
  1801. }
  1802. p->seen_other = 1;
  1803. return 1;
  1804. }
  1805. static int ocfs2_empty_dir_dx(struct inode *inode,
  1806. struct ocfs2_empty_dir_priv *priv)
  1807. {
  1808. int ret;
  1809. struct buffer_head *di_bh = NULL;
  1810. struct buffer_head *dx_root_bh = NULL;
  1811. struct ocfs2_dinode *di;
  1812. struct ocfs2_dx_root_block *dx_root;
  1813. priv->dx_dir = 1;
  1814. ret = ocfs2_read_inode_block(inode, &di_bh);
  1815. if (ret) {
  1816. mlog_errno(ret);
  1817. goto out;
  1818. }
  1819. di = (struct ocfs2_dinode *)di_bh->b_data;
  1820. ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
  1821. if (ret) {
  1822. mlog_errno(ret);
  1823. goto out;
  1824. }
  1825. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1826. if (le32_to_cpu(dx_root->dr_num_entries) != 2)
  1827. priv->seen_other = 1;
  1828. out:
  1829. brelse(di_bh);
  1830. brelse(dx_root_bh);
  1831. return ret;
  1832. }
  1833. /*
  1834. * routine to check that the specified directory is empty (for rmdir)
  1835. *
  1836. * Returns 1 if dir is empty, zero otherwise.
  1837. *
  1838. * XXX: This is a performance problem for unindexed directories.
  1839. */
  1840. int ocfs2_empty_dir(struct inode *inode)
  1841. {
  1842. int ret;
  1843. struct ocfs2_empty_dir_priv priv = {
  1844. .ctx.actor = ocfs2_empty_dir_filldir,
  1845. };
  1846. if (ocfs2_dir_indexed(inode)) {
  1847. ret = ocfs2_empty_dir_dx(inode, &priv);
  1848. if (ret)
  1849. mlog_errno(ret);
  1850. /*
  1851. * We still run ocfs2_dir_foreach to get the checks
  1852. * for "." and "..".
  1853. */
  1854. }
  1855. ret = ocfs2_dir_foreach(inode, &priv.ctx);
  1856. if (ret)
  1857. mlog_errno(ret);
  1858. if (!priv.seen_dot || !priv.seen_dot_dot) {
  1859. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  1860. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1861. /*
  1862. * XXX: Is it really safe to allow an unlink to continue?
  1863. */
  1864. return 1;
  1865. }
  1866. return !priv.seen_other;
  1867. }
  1868. /*
  1869. * Fills "." and ".." dirents in a new directory block. Returns dirent for
  1870. * "..", which might be used during creation of a directory with a trailing
  1871. * header. It is otherwise safe to ignore the return code.
  1872. */
  1873. static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
  1874. struct inode *parent,
  1875. char *start,
  1876. unsigned int size)
  1877. {
  1878. struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
  1879. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  1880. de->name_len = 1;
  1881. de->rec_len =
  1882. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1883. strcpy(de->name, ".");
  1884. ocfs2_set_de_type(de, S_IFDIR);
  1885. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  1886. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  1887. de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
  1888. de->name_len = 2;
  1889. strcpy(de->name, "..");
  1890. ocfs2_set_de_type(de, S_IFDIR);
  1891. return de;
  1892. }
  1893. /*
  1894. * This works together with code in ocfs2_mknod_locked() which sets
  1895. * the inline-data flag and initializes the inline-data section.
  1896. */
  1897. static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
  1898. handle_t *handle,
  1899. struct inode *parent,
  1900. struct inode *inode,
  1901. struct buffer_head *di_bh)
  1902. {
  1903. int ret;
  1904. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1905. struct ocfs2_inline_data *data = &di->id2.i_data;
  1906. unsigned int size = le16_to_cpu(data->id_count);
  1907. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1908. OCFS2_JOURNAL_ACCESS_WRITE);
  1909. if (ret) {
  1910. mlog_errno(ret);
  1911. goto out;
  1912. }
  1913. ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
  1914. ocfs2_journal_dirty(handle, di_bh);
  1915. i_size_write(inode, size);
  1916. set_nlink(inode, 2);
  1917. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1918. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1919. if (ret < 0)
  1920. mlog_errno(ret);
  1921. out:
  1922. return ret;
  1923. }
  1924. static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
  1925. handle_t *handle,
  1926. struct inode *parent,
  1927. struct inode *inode,
  1928. struct buffer_head *fe_bh,
  1929. struct ocfs2_alloc_context *data_ac,
  1930. struct buffer_head **ret_new_bh)
  1931. {
  1932. int status;
  1933. unsigned int size = osb->sb->s_blocksize;
  1934. struct buffer_head *new_bh = NULL;
  1935. struct ocfs2_dir_entry *de;
  1936. if (ocfs2_new_dir_wants_trailer(inode))
  1937. size = ocfs2_dir_trailer_blk_off(parent->i_sb);
  1938. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  1939. data_ac, NULL, &new_bh);
  1940. if (status < 0) {
  1941. mlog_errno(status);
  1942. goto bail;
  1943. }
  1944. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  1945. status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
  1946. OCFS2_JOURNAL_ACCESS_CREATE);
  1947. if (status < 0) {
  1948. mlog_errno(status);
  1949. goto bail;
  1950. }
  1951. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  1952. de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
  1953. if (ocfs2_new_dir_wants_trailer(inode)) {
  1954. int size = le16_to_cpu(de->rec_len);
  1955. /*
  1956. * Figure out the size of the hole left over after
  1957. * insertion of '.' and '..'. The trailer wants this
  1958. * information.
  1959. */
  1960. size -= OCFS2_DIR_REC_LEN(2);
  1961. size -= sizeof(struct ocfs2_dir_block_trailer);
  1962. ocfs2_init_dir_trailer(inode, new_bh, size);
  1963. }
  1964. ocfs2_journal_dirty(handle, new_bh);
  1965. i_size_write(inode, inode->i_sb->s_blocksize);
  1966. set_nlink(inode, 2);
  1967. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1968. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  1969. if (status < 0) {
  1970. mlog_errno(status);
  1971. goto bail;
  1972. }
  1973. status = 0;
  1974. if (ret_new_bh) {
  1975. *ret_new_bh = new_bh;
  1976. new_bh = NULL;
  1977. }
  1978. bail:
  1979. brelse(new_bh);
  1980. return status;
  1981. }
  1982. static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
  1983. handle_t *handle, struct inode *dir,
  1984. struct buffer_head *di_bh,
  1985. struct buffer_head *dirdata_bh,
  1986. struct ocfs2_alloc_context *meta_ac,
  1987. int dx_inline, u32 num_entries,
  1988. struct buffer_head **ret_dx_root_bh)
  1989. {
  1990. int ret;
  1991. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  1992. u16 dr_suballoc_bit;
  1993. u64 suballoc_loc, dr_blkno;
  1994. unsigned int num_bits;
  1995. struct buffer_head *dx_root_bh = NULL;
  1996. struct ocfs2_dx_root_block *dx_root;
  1997. struct ocfs2_dir_block_trailer *trailer =
  1998. ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  1999. ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  2000. &dr_suballoc_bit, &num_bits, &dr_blkno);
  2001. if (ret) {
  2002. mlog_errno(ret);
  2003. goto out;
  2004. }
  2005. trace_ocfs2_dx_dir_attach_index(
  2006. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2007. (unsigned long long)dr_blkno);
  2008. dx_root_bh = sb_getblk(osb->sb, dr_blkno);
  2009. if (dx_root_bh == NULL) {
  2010. ret = -ENOMEM;
  2011. goto out;
  2012. }
  2013. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
  2014. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  2015. OCFS2_JOURNAL_ACCESS_CREATE);
  2016. if (ret < 0) {
  2017. mlog_errno(ret);
  2018. goto out;
  2019. }
  2020. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2021. memset(dx_root, 0, osb->sb->s_blocksize);
  2022. strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
  2023. dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
  2024. dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
  2025. dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
  2026. dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
  2027. dx_root->dr_blkno = cpu_to_le64(dr_blkno);
  2028. dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
  2029. dx_root->dr_num_entries = cpu_to_le32(num_entries);
  2030. if (le16_to_cpu(trailer->db_free_rec_len))
  2031. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  2032. else
  2033. dx_root->dr_free_blk = cpu_to_le64(0);
  2034. if (dx_inline) {
  2035. dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
  2036. dx_root->dr_entries.de_count =
  2037. cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
  2038. } else {
  2039. dx_root->dr_list.l_count =
  2040. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  2041. }
  2042. ocfs2_journal_dirty(handle, dx_root_bh);
  2043. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  2044. OCFS2_JOURNAL_ACCESS_CREATE);
  2045. if (ret) {
  2046. mlog_errno(ret);
  2047. goto out;
  2048. }
  2049. di->i_dx_root = cpu_to_le64(dr_blkno);
  2050. spin_lock(&OCFS2_I(dir)->ip_lock);
  2051. OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
  2052. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  2053. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2054. ocfs2_journal_dirty(handle, di_bh);
  2055. *ret_dx_root_bh = dx_root_bh;
  2056. dx_root_bh = NULL;
  2057. out:
  2058. brelse(dx_root_bh);
  2059. return ret;
  2060. }
  2061. static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
  2062. handle_t *handle, struct inode *dir,
  2063. struct buffer_head **dx_leaves,
  2064. int num_dx_leaves, u64 start_blk)
  2065. {
  2066. int ret, i;
  2067. struct ocfs2_dx_leaf *dx_leaf;
  2068. struct buffer_head *bh;
  2069. for (i = 0; i < num_dx_leaves; i++) {
  2070. bh = sb_getblk(osb->sb, start_blk + i);
  2071. if (bh == NULL) {
  2072. ret = -ENOMEM;
  2073. goto out;
  2074. }
  2075. dx_leaves[i] = bh;
  2076. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
  2077. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
  2078. OCFS2_JOURNAL_ACCESS_CREATE);
  2079. if (ret < 0) {
  2080. mlog_errno(ret);
  2081. goto out;
  2082. }
  2083. dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
  2084. memset(dx_leaf, 0, osb->sb->s_blocksize);
  2085. strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
  2086. dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
  2087. dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
  2088. dx_leaf->dl_list.de_count =
  2089. cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
  2090. trace_ocfs2_dx_dir_format_cluster(
  2091. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2092. (unsigned long long)bh->b_blocknr,
  2093. le16_to_cpu(dx_leaf->dl_list.de_count));
  2094. ocfs2_journal_dirty(handle, bh);
  2095. }
  2096. ret = 0;
  2097. out:
  2098. return ret;
  2099. }
  2100. /*
  2101. * Allocates and formats a new cluster for use in an indexed dir
  2102. * leaf. This version will not do the extent insert, so that it can be
  2103. * used by operations which need careful ordering.
  2104. */
  2105. static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
  2106. u32 cpos, handle_t *handle,
  2107. struct ocfs2_alloc_context *data_ac,
  2108. struct buffer_head **dx_leaves,
  2109. int num_dx_leaves, u64 *ret_phys_blkno)
  2110. {
  2111. int ret;
  2112. u32 phys, num;
  2113. u64 phys_blkno;
  2114. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2115. /*
  2116. * XXX: For create, this should claim cluster for the index
  2117. * *before* the unindexed insert so that we have a better
  2118. * chance of contiguousness as the directory grows in number
  2119. * of entries.
  2120. */
  2121. ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
  2122. if (ret) {
  2123. mlog_errno(ret);
  2124. goto out;
  2125. }
  2126. /*
  2127. * Format the new cluster first. That way, we're inserting
  2128. * valid data.
  2129. */
  2130. phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
  2131. ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
  2132. num_dx_leaves, phys_blkno);
  2133. if (ret) {
  2134. mlog_errno(ret);
  2135. goto out;
  2136. }
  2137. *ret_phys_blkno = phys_blkno;
  2138. out:
  2139. return ret;
  2140. }
  2141. static int ocfs2_dx_dir_new_cluster(struct inode *dir,
  2142. struct ocfs2_extent_tree *et,
  2143. u32 cpos, handle_t *handle,
  2144. struct ocfs2_alloc_context *data_ac,
  2145. struct ocfs2_alloc_context *meta_ac,
  2146. struct buffer_head **dx_leaves,
  2147. int num_dx_leaves)
  2148. {
  2149. int ret;
  2150. u64 phys_blkno;
  2151. ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
  2152. num_dx_leaves, &phys_blkno);
  2153. if (ret) {
  2154. mlog_errno(ret);
  2155. goto out;
  2156. }
  2157. ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
  2158. meta_ac);
  2159. if (ret)
  2160. mlog_errno(ret);
  2161. out:
  2162. return ret;
  2163. }
  2164. static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
  2165. int *ret_num_leaves)
  2166. {
  2167. int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
  2168. struct buffer_head **dx_leaves;
  2169. dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
  2170. GFP_NOFS);
  2171. if (dx_leaves && ret_num_leaves)
  2172. *ret_num_leaves = num_dx_leaves;
  2173. return dx_leaves;
  2174. }
  2175. static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
  2176. handle_t *handle,
  2177. struct inode *parent,
  2178. struct inode *inode,
  2179. struct buffer_head *di_bh,
  2180. struct ocfs2_alloc_context *data_ac,
  2181. struct ocfs2_alloc_context *meta_ac)
  2182. {
  2183. int ret;
  2184. struct buffer_head *leaf_bh = NULL;
  2185. struct buffer_head *dx_root_bh = NULL;
  2186. struct ocfs2_dx_hinfo hinfo;
  2187. struct ocfs2_dx_root_block *dx_root;
  2188. struct ocfs2_dx_entry_list *entry_list;
  2189. /*
  2190. * Our strategy is to create the directory as though it were
  2191. * unindexed, then add the index block. This works with very
  2192. * little complication since the state of a new directory is a
  2193. * very well known quantity.
  2194. *
  2195. * Essentially, we have two dirents ("." and ".."), in the 1st
  2196. * block which need indexing. These are easily inserted into
  2197. * the index block.
  2198. */
  2199. ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
  2200. data_ac, &leaf_bh);
  2201. if (ret) {
  2202. mlog_errno(ret);
  2203. goto out;
  2204. }
  2205. ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
  2206. meta_ac, 1, 2, &dx_root_bh);
  2207. if (ret) {
  2208. mlog_errno(ret);
  2209. goto out;
  2210. }
  2211. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2212. entry_list = &dx_root->dr_entries;
  2213. /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
  2214. ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
  2215. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2216. ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
  2217. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2218. out:
  2219. brelse(dx_root_bh);
  2220. brelse(leaf_bh);
  2221. return ret;
  2222. }
  2223. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  2224. handle_t *handle,
  2225. struct inode *parent,
  2226. struct inode *inode,
  2227. struct buffer_head *fe_bh,
  2228. struct ocfs2_alloc_context *data_ac,
  2229. struct ocfs2_alloc_context *meta_ac)
  2230. {
  2231. BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
  2232. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  2233. return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
  2234. if (ocfs2_supports_indexed_dirs(osb))
  2235. return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
  2236. data_ac, meta_ac);
  2237. return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
  2238. data_ac, NULL);
  2239. }
  2240. static int ocfs2_dx_dir_index_block(struct inode *dir,
  2241. handle_t *handle,
  2242. struct buffer_head **dx_leaves,
  2243. int num_dx_leaves,
  2244. u32 *num_dx_entries,
  2245. struct buffer_head *dirent_bh)
  2246. {
  2247. int ret = 0, namelen, i;
  2248. char *de_buf, *limit;
  2249. struct ocfs2_dir_entry *de;
  2250. struct buffer_head *dx_leaf_bh;
  2251. struct ocfs2_dx_hinfo hinfo;
  2252. u64 dirent_blk = dirent_bh->b_blocknr;
  2253. de_buf = dirent_bh->b_data;
  2254. limit = de_buf + dir->i_sb->s_blocksize;
  2255. while (de_buf < limit) {
  2256. de = (struct ocfs2_dir_entry *)de_buf;
  2257. namelen = de->name_len;
  2258. if (!namelen || !de->inode)
  2259. goto inc;
  2260. ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
  2261. i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
  2262. dx_leaf_bh = dx_leaves[i];
  2263. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
  2264. dirent_blk, dx_leaf_bh);
  2265. if (ret) {
  2266. mlog_errno(ret);
  2267. goto out;
  2268. }
  2269. *num_dx_entries = *num_dx_entries + 1;
  2270. inc:
  2271. de_buf += le16_to_cpu(de->rec_len);
  2272. }
  2273. out:
  2274. return ret;
  2275. }
  2276. /*
  2277. * XXX: This expects dx_root_bh to already be part of the transaction.
  2278. */
  2279. static void ocfs2_dx_dir_index_root_block(struct inode *dir,
  2280. struct buffer_head *dx_root_bh,
  2281. struct buffer_head *dirent_bh)
  2282. {
  2283. char *de_buf, *limit;
  2284. struct ocfs2_dx_root_block *dx_root;
  2285. struct ocfs2_dir_entry *de;
  2286. struct ocfs2_dx_hinfo hinfo;
  2287. u64 dirent_blk = dirent_bh->b_blocknr;
  2288. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2289. de_buf = dirent_bh->b_data;
  2290. limit = de_buf + dir->i_sb->s_blocksize;
  2291. while (de_buf < limit) {
  2292. de = (struct ocfs2_dir_entry *)de_buf;
  2293. if (!de->name_len || !de->inode)
  2294. goto inc;
  2295. ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
  2296. trace_ocfs2_dx_dir_index_root_block(
  2297. (unsigned long long)dir->i_ino,
  2298. hinfo.major_hash, hinfo.minor_hash,
  2299. de->name_len, de->name,
  2300. le16_to_cpu(dx_root->dr_entries.de_num_used));
  2301. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
  2302. dirent_blk);
  2303. le32_add_cpu(&dx_root->dr_num_entries, 1);
  2304. inc:
  2305. de_buf += le16_to_cpu(de->rec_len);
  2306. }
  2307. }
  2308. /*
  2309. * Count the number of inline directory entries in di_bh and compare
  2310. * them against the number of entries we can hold in an inline dx root
  2311. * block.
  2312. */
  2313. static int ocfs2_new_dx_should_be_inline(struct inode *dir,
  2314. struct buffer_head *di_bh)
  2315. {
  2316. int dirent_count = 0;
  2317. char *de_buf, *limit;
  2318. struct ocfs2_dir_entry *de;
  2319. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2320. de_buf = di->id2.i_data.id_data;
  2321. limit = de_buf + i_size_read(dir);
  2322. while (de_buf < limit) {
  2323. de = (struct ocfs2_dir_entry *)de_buf;
  2324. if (de->name_len && de->inode)
  2325. dirent_count++;
  2326. de_buf += le16_to_cpu(de->rec_len);
  2327. }
  2328. /* We are careful to leave room for one extra record. */
  2329. return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
  2330. }
  2331. /*
  2332. * Expand rec_len of the rightmost dirent in a directory block so that it
  2333. * contains the end of our valid space for dirents. We do this during
  2334. * expansion from an inline directory to one with extents. The first dir block
  2335. * in that case is taken from the inline data portion of the inode block.
  2336. *
  2337. * This will also return the largest amount of contiguous space for a dirent
  2338. * in the block. That value is *not* necessarily the last dirent, even after
  2339. * expansion. The directory indexing code wants this value for free space
  2340. * accounting. We do this here since we're already walking the entire dir
  2341. * block.
  2342. *
  2343. * We add the dir trailer if this filesystem wants it.
  2344. */
  2345. static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
  2346. struct inode *dir)
  2347. {
  2348. struct super_block *sb = dir->i_sb;
  2349. struct ocfs2_dir_entry *de;
  2350. struct ocfs2_dir_entry *prev_de;
  2351. char *de_buf, *limit;
  2352. unsigned int new_size = sb->s_blocksize;
  2353. unsigned int bytes, this_hole;
  2354. unsigned int largest_hole = 0;
  2355. if (ocfs2_new_dir_wants_trailer(dir))
  2356. new_size = ocfs2_dir_trailer_blk_off(sb);
  2357. bytes = new_size - old_size;
  2358. limit = start + old_size;
  2359. de_buf = start;
  2360. de = (struct ocfs2_dir_entry *)de_buf;
  2361. do {
  2362. this_hole = ocfs2_figure_dirent_hole(de);
  2363. if (this_hole > largest_hole)
  2364. largest_hole = this_hole;
  2365. prev_de = de;
  2366. de_buf += le16_to_cpu(de->rec_len);
  2367. de = (struct ocfs2_dir_entry *)de_buf;
  2368. } while (de_buf < limit);
  2369. le16_add_cpu(&prev_de->rec_len, bytes);
  2370. /* We need to double check this after modification of the final
  2371. * dirent. */
  2372. this_hole = ocfs2_figure_dirent_hole(prev_de);
  2373. if (this_hole > largest_hole)
  2374. largest_hole = this_hole;
  2375. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  2376. return largest_hole;
  2377. return 0;
  2378. }
  2379. /*
  2380. * We allocate enough clusters to fulfill "blocks_wanted", but set
  2381. * i_size to exactly one block. Ocfs2_extend_dir() will handle the
  2382. * rest automatically for us.
  2383. *
  2384. * *first_block_bh is a pointer to the 1st data block allocated to the
  2385. * directory.
  2386. */
  2387. static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
  2388. unsigned int blocks_wanted,
  2389. struct ocfs2_dir_lookup_result *lookup,
  2390. struct buffer_head **first_block_bh)
  2391. {
  2392. u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
  2393. struct super_block *sb = dir->i_sb;
  2394. int ret, i, num_dx_leaves = 0, dx_inline = 0,
  2395. credits = ocfs2_inline_to_extents_credits(sb);
  2396. u64 dx_insert_blkno, blkno,
  2397. bytes = blocks_wanted << sb->s_blocksize_bits;
  2398. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2399. struct ocfs2_inode_info *oi = OCFS2_I(dir);
  2400. struct ocfs2_alloc_context *data_ac = NULL;
  2401. struct ocfs2_alloc_context *meta_ac = NULL;
  2402. struct buffer_head *dirdata_bh = NULL;
  2403. struct buffer_head *dx_root_bh = NULL;
  2404. struct buffer_head **dx_leaves = NULL;
  2405. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2406. handle_t *handle;
  2407. struct ocfs2_extent_tree et;
  2408. struct ocfs2_extent_tree dx_et;
  2409. int did_quota = 0, bytes_allocated = 0;
  2410. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
  2411. alloc = ocfs2_clusters_for_bytes(sb, bytes);
  2412. dx_alloc = 0;
  2413. down_write(&oi->ip_alloc_sem);
  2414. if (ocfs2_supports_indexed_dirs(osb)) {
  2415. credits += ocfs2_add_dir_index_credits(sb);
  2416. dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
  2417. if (!dx_inline) {
  2418. /* Add one more cluster for an index leaf */
  2419. dx_alloc++;
  2420. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
  2421. &num_dx_leaves);
  2422. if (!dx_leaves) {
  2423. ret = -ENOMEM;
  2424. mlog_errno(ret);
  2425. goto out;
  2426. }
  2427. }
  2428. /* This gets us the dx_root */
  2429. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  2430. if (ret) {
  2431. mlog_errno(ret);
  2432. goto out;
  2433. }
  2434. }
  2435. /*
  2436. * We should never need more than 2 clusters for the unindexed
  2437. * tree - maximum dirent size is far less than one block. In
  2438. * fact, the only time we'd need more than one cluster is if
  2439. * blocksize == clustersize and the dirent won't fit in the
  2440. * extra space that the expansion to a single block gives. As
  2441. * of today, that only happens on 4k/4k file systems.
  2442. */
  2443. BUG_ON(alloc > 2);
  2444. ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
  2445. if (ret) {
  2446. mlog_errno(ret);
  2447. goto out;
  2448. }
  2449. /*
  2450. * Prepare for worst case allocation scenario of two separate
  2451. * extents in the unindexed tree.
  2452. */
  2453. if (alloc == 2)
  2454. credits += OCFS2_SUBALLOC_ALLOC;
  2455. handle = ocfs2_start_trans(osb, credits);
  2456. if (IS_ERR(handle)) {
  2457. ret = PTR_ERR(handle);
  2458. mlog_errno(ret);
  2459. goto out;
  2460. }
  2461. ret = dquot_alloc_space_nodirty(dir,
  2462. ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
  2463. if (ret)
  2464. goto out_commit;
  2465. did_quota = 1;
  2466. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2467. /*
  2468. * Allocate our index cluster first, to maximize the
  2469. * possibility that unindexed leaves grow
  2470. * contiguously.
  2471. */
  2472. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
  2473. dx_leaves, num_dx_leaves,
  2474. &dx_insert_blkno);
  2475. if (ret) {
  2476. mlog_errno(ret);
  2477. goto out_commit;
  2478. }
  2479. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2480. }
  2481. /*
  2482. * Try to claim as many clusters as the bitmap can give though
  2483. * if we only get one now, that's enough to continue. The rest
  2484. * will be claimed after the conversion to extents.
  2485. */
  2486. if (ocfs2_dir_resv_allowed(osb))
  2487. data_ac->ac_resv = &oi->ip_la_data_resv;
  2488. ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
  2489. if (ret) {
  2490. mlog_errno(ret);
  2491. goto out_commit;
  2492. }
  2493. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2494. /*
  2495. * Operations are carefully ordered so that we set up the new
  2496. * data block first. The conversion from inline data to
  2497. * extents follows.
  2498. */
  2499. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2500. dirdata_bh = sb_getblk(sb, blkno);
  2501. if (!dirdata_bh) {
  2502. ret = -ENOMEM;
  2503. mlog_errno(ret);
  2504. goto out_commit;
  2505. }
  2506. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
  2507. ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
  2508. OCFS2_JOURNAL_ACCESS_CREATE);
  2509. if (ret) {
  2510. mlog_errno(ret);
  2511. goto out_commit;
  2512. }
  2513. memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
  2514. memset(dirdata_bh->b_data + i_size_read(dir), 0,
  2515. sb->s_blocksize - i_size_read(dir));
  2516. i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
  2517. if (ocfs2_new_dir_wants_trailer(dir)) {
  2518. /*
  2519. * Prepare the dir trailer up front. It will otherwise look
  2520. * like a valid dirent. Even if inserting the index fails
  2521. * (unlikely), then all we'll have done is given first dir
  2522. * block a small amount of fragmentation.
  2523. */
  2524. ocfs2_init_dir_trailer(dir, dirdata_bh, i);
  2525. }
  2526. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  2527. ocfs2_journal_dirty(handle, dirdata_bh);
  2528. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2529. /*
  2530. * Dx dirs with an external cluster need to do this up
  2531. * front. Inline dx root's get handled later, after
  2532. * we've allocated our root block. We get passed back
  2533. * a total number of items so that dr_num_entries can
  2534. * be correctly set once the dx_root has been
  2535. * allocated.
  2536. */
  2537. ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
  2538. num_dx_leaves, &num_dx_entries,
  2539. dirdata_bh);
  2540. if (ret) {
  2541. mlog_errno(ret);
  2542. goto out_commit;
  2543. }
  2544. }
  2545. /*
  2546. * Set extent, i_size, etc on the directory. After this, the
  2547. * inode should contain the same exact dirents as before and
  2548. * be fully accessible from system calls.
  2549. *
  2550. * We let the later dirent insert modify c/mtime - to the user
  2551. * the data hasn't changed.
  2552. */
  2553. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  2554. OCFS2_JOURNAL_ACCESS_CREATE);
  2555. if (ret) {
  2556. mlog_errno(ret);
  2557. goto out_commit;
  2558. }
  2559. spin_lock(&oi->ip_lock);
  2560. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  2561. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  2562. spin_unlock(&oi->ip_lock);
  2563. ocfs2_dinode_new_extent_list(dir, di);
  2564. i_size_write(dir, sb->s_blocksize);
  2565. dir->i_mtime = dir->i_ctime = current_time(dir);
  2566. di->i_size = cpu_to_le64(sb->s_blocksize);
  2567. di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
  2568. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
  2569. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  2570. /*
  2571. * This should never fail as our extent list is empty and all
  2572. * related blocks have been journaled already.
  2573. */
  2574. ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
  2575. 0, NULL);
  2576. if (ret) {
  2577. mlog_errno(ret);
  2578. goto out_commit;
  2579. }
  2580. /*
  2581. * Set i_blocks after the extent insert for the most up to
  2582. * date ip_clusters value.
  2583. */
  2584. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2585. ocfs2_journal_dirty(handle, di_bh);
  2586. if (ocfs2_supports_indexed_dirs(osb)) {
  2587. ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
  2588. dirdata_bh, meta_ac, dx_inline,
  2589. num_dx_entries, &dx_root_bh);
  2590. if (ret) {
  2591. mlog_errno(ret);
  2592. goto out_commit;
  2593. }
  2594. if (dx_inline) {
  2595. ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
  2596. dirdata_bh);
  2597. } else {
  2598. ocfs2_init_dx_root_extent_tree(&dx_et,
  2599. INODE_CACHE(dir),
  2600. dx_root_bh);
  2601. ret = ocfs2_insert_extent(handle, &dx_et, 0,
  2602. dx_insert_blkno, 1, 0, NULL);
  2603. if (ret)
  2604. mlog_errno(ret);
  2605. }
  2606. }
  2607. /*
  2608. * We asked for two clusters, but only got one in the 1st
  2609. * pass. Claim the 2nd cluster as a separate extent.
  2610. */
  2611. if (alloc > len) {
  2612. ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
  2613. &len);
  2614. if (ret) {
  2615. mlog_errno(ret);
  2616. goto out_commit;
  2617. }
  2618. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2619. ret = ocfs2_insert_extent(handle, &et, 1,
  2620. blkno, len, 0, NULL);
  2621. if (ret) {
  2622. mlog_errno(ret);
  2623. goto out_commit;
  2624. }
  2625. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2626. }
  2627. *first_block_bh = dirdata_bh;
  2628. dirdata_bh = NULL;
  2629. if (ocfs2_supports_indexed_dirs(osb)) {
  2630. unsigned int off;
  2631. if (!dx_inline) {
  2632. /*
  2633. * We need to return the correct block within the
  2634. * cluster which should hold our entry.
  2635. */
  2636. off = ocfs2_dx_dir_hash_idx(osb,
  2637. &lookup->dl_hinfo);
  2638. get_bh(dx_leaves[off]);
  2639. lookup->dl_dx_leaf_bh = dx_leaves[off];
  2640. }
  2641. lookup->dl_dx_root_bh = dx_root_bh;
  2642. dx_root_bh = NULL;
  2643. }
  2644. out_commit:
  2645. if (ret < 0 && did_quota)
  2646. dquot_free_space_nodirty(dir, bytes_allocated);
  2647. ocfs2_commit_trans(osb, handle);
  2648. out:
  2649. up_write(&oi->ip_alloc_sem);
  2650. if (data_ac)
  2651. ocfs2_free_alloc_context(data_ac);
  2652. if (meta_ac)
  2653. ocfs2_free_alloc_context(meta_ac);
  2654. if (dx_leaves) {
  2655. for (i = 0; i < num_dx_leaves; i++)
  2656. brelse(dx_leaves[i]);
  2657. kfree(dx_leaves);
  2658. }
  2659. brelse(dirdata_bh);
  2660. brelse(dx_root_bh);
  2661. return ret;
  2662. }
  2663. /* returns a bh of the 1st new block in the allocation. */
  2664. static int ocfs2_do_extend_dir(struct super_block *sb,
  2665. handle_t *handle,
  2666. struct inode *dir,
  2667. struct buffer_head *parent_fe_bh,
  2668. struct ocfs2_alloc_context *data_ac,
  2669. struct ocfs2_alloc_context *meta_ac,
  2670. struct buffer_head **new_bh)
  2671. {
  2672. int status;
  2673. int extend, did_quota = 0;
  2674. u64 p_blkno, v_blkno;
  2675. spin_lock(&OCFS2_I(dir)->ip_lock);
  2676. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  2677. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2678. if (extend) {
  2679. u32 offset = OCFS2_I(dir)->ip_clusters;
  2680. status = dquot_alloc_space_nodirty(dir,
  2681. ocfs2_clusters_to_bytes(sb, 1));
  2682. if (status)
  2683. goto bail;
  2684. did_quota = 1;
  2685. status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
  2686. 1, 0, parent_fe_bh, handle,
  2687. data_ac, meta_ac, NULL);
  2688. BUG_ON(status == -EAGAIN);
  2689. if (status < 0) {
  2690. mlog_errno(status);
  2691. goto bail;
  2692. }
  2693. }
  2694. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  2695. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  2696. if (status < 0) {
  2697. mlog_errno(status);
  2698. goto bail;
  2699. }
  2700. *new_bh = sb_getblk(sb, p_blkno);
  2701. if (!*new_bh) {
  2702. status = -ENOMEM;
  2703. mlog_errno(status);
  2704. goto bail;
  2705. }
  2706. status = 0;
  2707. bail:
  2708. if (did_quota && status < 0)
  2709. dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
  2710. return status;
  2711. }
  2712. /*
  2713. * Assumes you already have a cluster lock on the directory.
  2714. *
  2715. * 'blocks_wanted' is only used if we have an inline directory which
  2716. * is to be turned into an extent based one. The size of the dirent to
  2717. * insert might be larger than the space gained by growing to just one
  2718. * block, so we may have to grow the inode by two blocks in that case.
  2719. *
  2720. * If the directory is already indexed, dx_root_bh must be provided.
  2721. */
  2722. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  2723. struct inode *dir,
  2724. struct buffer_head *parent_fe_bh,
  2725. unsigned int blocks_wanted,
  2726. struct ocfs2_dir_lookup_result *lookup,
  2727. struct buffer_head **new_de_bh)
  2728. {
  2729. int status = 0;
  2730. int credits, num_free_extents, drop_alloc_sem = 0;
  2731. loff_t dir_i_size;
  2732. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  2733. struct ocfs2_extent_list *el = &fe->id2.i_list;
  2734. struct ocfs2_alloc_context *data_ac = NULL;
  2735. struct ocfs2_alloc_context *meta_ac = NULL;
  2736. handle_t *handle = NULL;
  2737. struct buffer_head *new_bh = NULL;
  2738. struct ocfs2_dir_entry * de;
  2739. struct super_block *sb = osb->sb;
  2740. struct ocfs2_extent_tree et;
  2741. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  2742. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2743. /*
  2744. * This would be a code error as an inline directory should
  2745. * never have an index root.
  2746. */
  2747. BUG_ON(dx_root_bh);
  2748. status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
  2749. blocks_wanted, lookup,
  2750. &new_bh);
  2751. if (status) {
  2752. mlog_errno(status);
  2753. goto bail;
  2754. }
  2755. /* Expansion from inline to an indexed directory will
  2756. * have given us this. */
  2757. dx_root_bh = lookup->dl_dx_root_bh;
  2758. if (blocks_wanted == 1) {
  2759. /*
  2760. * If the new dirent will fit inside the space
  2761. * created by pushing out to one block, then
  2762. * we can complete the operation
  2763. * here. Otherwise we have to expand i_size
  2764. * and format the 2nd block below.
  2765. */
  2766. BUG_ON(new_bh == NULL);
  2767. goto bail_bh;
  2768. }
  2769. /*
  2770. * Get rid of 'new_bh' - we want to format the 2nd
  2771. * data block and return that instead.
  2772. */
  2773. brelse(new_bh);
  2774. new_bh = NULL;
  2775. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2776. drop_alloc_sem = 1;
  2777. dir_i_size = i_size_read(dir);
  2778. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2779. goto do_extend;
  2780. }
  2781. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2782. drop_alloc_sem = 1;
  2783. dir_i_size = i_size_read(dir);
  2784. trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
  2785. dir_i_size);
  2786. /* dir->i_size is always block aligned. */
  2787. spin_lock(&OCFS2_I(dir)->ip_lock);
  2788. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  2789. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2790. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
  2791. parent_fe_bh);
  2792. num_free_extents = ocfs2_num_free_extents(&et);
  2793. if (num_free_extents < 0) {
  2794. status = num_free_extents;
  2795. mlog_errno(status);
  2796. goto bail;
  2797. }
  2798. if (!num_free_extents) {
  2799. status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
  2800. if (status < 0) {
  2801. if (status != -ENOSPC)
  2802. mlog_errno(status);
  2803. goto bail;
  2804. }
  2805. }
  2806. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2807. if (status < 0) {
  2808. if (status != -ENOSPC)
  2809. mlog_errno(status);
  2810. goto bail;
  2811. }
  2812. if (ocfs2_dir_resv_allowed(osb))
  2813. data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
  2814. credits = ocfs2_calc_extend_credits(sb, el);
  2815. } else {
  2816. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2817. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2818. }
  2819. do_extend:
  2820. if (ocfs2_dir_indexed(dir))
  2821. credits++; /* For attaching the new dirent block to the
  2822. * dx_root */
  2823. handle = ocfs2_start_trans(osb, credits);
  2824. if (IS_ERR(handle)) {
  2825. status = PTR_ERR(handle);
  2826. handle = NULL;
  2827. mlog_errno(status);
  2828. goto bail;
  2829. }
  2830. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  2831. data_ac, meta_ac, &new_bh);
  2832. if (status < 0) {
  2833. mlog_errno(status);
  2834. goto bail;
  2835. }
  2836. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
  2837. status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
  2838. OCFS2_JOURNAL_ACCESS_CREATE);
  2839. if (status < 0) {
  2840. mlog_errno(status);
  2841. goto bail;
  2842. }
  2843. memset(new_bh->b_data, 0, sb->s_blocksize);
  2844. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  2845. de->inode = 0;
  2846. if (ocfs2_supports_dir_trailer(dir)) {
  2847. de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
  2848. ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
  2849. if (ocfs2_dir_indexed(dir)) {
  2850. status = ocfs2_dx_dir_link_trailer(dir, handle,
  2851. dx_root_bh, new_bh);
  2852. if (status) {
  2853. mlog_errno(status);
  2854. goto bail;
  2855. }
  2856. }
  2857. } else {
  2858. de->rec_len = cpu_to_le16(sb->s_blocksize);
  2859. }
  2860. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  2861. ocfs2_journal_dirty(handle, new_bh);
  2862. dir_i_size += dir->i_sb->s_blocksize;
  2863. i_size_write(dir, dir_i_size);
  2864. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2865. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  2866. if (status < 0) {
  2867. mlog_errno(status);
  2868. goto bail;
  2869. }
  2870. bail_bh:
  2871. *new_de_bh = new_bh;
  2872. get_bh(*new_de_bh);
  2873. bail:
  2874. if (handle)
  2875. ocfs2_commit_trans(osb, handle);
  2876. if (drop_alloc_sem)
  2877. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  2878. if (data_ac)
  2879. ocfs2_free_alloc_context(data_ac);
  2880. if (meta_ac)
  2881. ocfs2_free_alloc_context(meta_ac);
  2882. brelse(new_bh);
  2883. return status;
  2884. }
  2885. static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
  2886. const char *name, int namelen,
  2887. struct buffer_head **ret_de_bh,
  2888. unsigned int *blocks_wanted)
  2889. {
  2890. int ret;
  2891. struct super_block *sb = dir->i_sb;
  2892. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2893. struct ocfs2_dir_entry *de, *last_de = NULL;
  2894. char *de_buf, *limit;
  2895. unsigned long offset = 0;
  2896. unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
  2897. /*
  2898. * This calculates how many free bytes we'd have in block zero, should
  2899. * this function force expansion to an extent tree.
  2900. */
  2901. if (ocfs2_new_dir_wants_trailer(dir))
  2902. free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
  2903. else
  2904. free_space = dir->i_sb->s_blocksize - i_size_read(dir);
  2905. de_buf = di->id2.i_data.id_data;
  2906. limit = de_buf + i_size_read(dir);
  2907. rec_len = OCFS2_DIR_REC_LEN(namelen);
  2908. while (de_buf < limit) {
  2909. de = (struct ocfs2_dir_entry *)de_buf;
  2910. if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
  2911. ret = -ENOENT;
  2912. goto out;
  2913. }
  2914. if (ocfs2_match(namelen, name, de)) {
  2915. ret = -EEXIST;
  2916. goto out;
  2917. }
  2918. /*
  2919. * No need to check for a trailing dirent record here as
  2920. * they're not used for inline dirs.
  2921. */
  2922. if (ocfs2_dirent_would_fit(de, rec_len)) {
  2923. /* Ok, we found a spot. Return this bh and let
  2924. * the caller actually fill it in. */
  2925. *ret_de_bh = di_bh;
  2926. get_bh(*ret_de_bh);
  2927. ret = 0;
  2928. goto out;
  2929. }
  2930. last_de = de;
  2931. de_buf += le16_to_cpu(de->rec_len);
  2932. offset += le16_to_cpu(de->rec_len);
  2933. }
  2934. /*
  2935. * We're going to require expansion of the directory - figure
  2936. * out how many blocks we'll need so that a place for the
  2937. * dirent can be found.
  2938. */
  2939. *blocks_wanted = 1;
  2940. new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
  2941. if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
  2942. *blocks_wanted = 2;
  2943. ret = -ENOSPC;
  2944. out:
  2945. return ret;
  2946. }
  2947. static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
  2948. int namelen, struct buffer_head **ret_de_bh)
  2949. {
  2950. unsigned long offset;
  2951. struct buffer_head *bh = NULL;
  2952. unsigned short rec_len;
  2953. struct ocfs2_dir_entry *de;
  2954. struct super_block *sb = dir->i_sb;
  2955. int status;
  2956. int blocksize = dir->i_sb->s_blocksize;
  2957. status = ocfs2_read_dir_block(dir, 0, &bh, 0);
  2958. if (status)
  2959. goto bail;
  2960. rec_len = OCFS2_DIR_REC_LEN(namelen);
  2961. offset = 0;
  2962. de = (struct ocfs2_dir_entry *) bh->b_data;
  2963. while (1) {
  2964. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  2965. brelse(bh);
  2966. bh = NULL;
  2967. if (i_size_read(dir) <= offset) {
  2968. /*
  2969. * Caller will have to expand this
  2970. * directory.
  2971. */
  2972. status = -ENOSPC;
  2973. goto bail;
  2974. }
  2975. status = ocfs2_read_dir_block(dir,
  2976. offset >> sb->s_blocksize_bits,
  2977. &bh, 0);
  2978. if (status)
  2979. goto bail;
  2980. /* move to next block */
  2981. de = (struct ocfs2_dir_entry *) bh->b_data;
  2982. }
  2983. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  2984. status = -ENOENT;
  2985. goto bail;
  2986. }
  2987. if (ocfs2_match(namelen, name, de)) {
  2988. status = -EEXIST;
  2989. goto bail;
  2990. }
  2991. if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
  2992. blocksize))
  2993. goto next;
  2994. if (ocfs2_dirent_would_fit(de, rec_len)) {
  2995. /* Ok, we found a spot. Return this bh and let
  2996. * the caller actually fill it in. */
  2997. *ret_de_bh = bh;
  2998. get_bh(*ret_de_bh);
  2999. status = 0;
  3000. goto bail;
  3001. }
  3002. next:
  3003. offset += le16_to_cpu(de->rec_len);
  3004. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  3005. }
  3006. bail:
  3007. brelse(bh);
  3008. if (status)
  3009. mlog_errno(status);
  3010. return status;
  3011. }
  3012. static int dx_leaf_sort_cmp(const void *a, const void *b)
  3013. {
  3014. const struct ocfs2_dx_entry *entry1 = a;
  3015. const struct ocfs2_dx_entry *entry2 = b;
  3016. u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
  3017. u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
  3018. u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
  3019. u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
  3020. if (major_hash1 > major_hash2)
  3021. return 1;
  3022. if (major_hash1 < major_hash2)
  3023. return -1;
  3024. /*
  3025. * It is not strictly necessary to sort by minor
  3026. */
  3027. if (minor_hash1 > minor_hash2)
  3028. return 1;
  3029. if (minor_hash1 < minor_hash2)
  3030. return -1;
  3031. return 0;
  3032. }
  3033. static void dx_leaf_sort_swap(void *a, void *b, int size)
  3034. {
  3035. struct ocfs2_dx_entry *entry1 = a;
  3036. struct ocfs2_dx_entry *entry2 = b;
  3037. BUG_ON(size != sizeof(*entry1));
  3038. swap(*entry1, *entry2);
  3039. }
  3040. static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
  3041. {
  3042. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3043. int i, num = le16_to_cpu(dl_list->de_num_used);
  3044. for (i = 0; i < (num - 1); i++) {
  3045. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
  3046. le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
  3047. return 0;
  3048. }
  3049. return 1;
  3050. }
  3051. /*
  3052. * Find the optimal value to split this leaf on. This expects the leaf
  3053. * entries to be in sorted order.
  3054. *
  3055. * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
  3056. * the hash we want to insert.
  3057. *
  3058. * This function is only concerned with the major hash - that which
  3059. * determines which cluster an item belongs to.
  3060. */
  3061. static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
  3062. u32 leaf_cpos, u32 insert_hash,
  3063. u32 *split_hash)
  3064. {
  3065. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3066. int i, num_used = le16_to_cpu(dl_list->de_num_used);
  3067. int allsame;
  3068. /*
  3069. * There's a couple rare, but nasty corner cases we have to
  3070. * check for here. All of them involve a leaf where all value
  3071. * have the same hash, which is what we look for first.
  3072. *
  3073. * Most of the time, all of the above is false, and we simply
  3074. * pick the median value for a split.
  3075. */
  3076. allsame = ocfs2_dx_leaf_same_major(dx_leaf);
  3077. if (allsame) {
  3078. u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
  3079. if (val == insert_hash) {
  3080. /*
  3081. * No matter where we would choose to split,
  3082. * the new entry would want to occupy the same
  3083. * block as these. Since there's no space left
  3084. * in their existing block, we know there
  3085. * won't be space after the split.
  3086. */
  3087. return -ENOSPC;
  3088. }
  3089. if (val == leaf_cpos) {
  3090. /*
  3091. * Because val is the same as leaf_cpos (which
  3092. * is the smallest value this leaf can have),
  3093. * yet is not equal to insert_hash, then we
  3094. * know that insert_hash *must* be larger than
  3095. * val (and leaf_cpos). At least cpos+1 in value.
  3096. *
  3097. * We also know then, that there cannot be an
  3098. * adjacent extent (otherwise we'd be looking
  3099. * at it). Choosing this value gives us a
  3100. * chance to get some contiguousness.
  3101. */
  3102. *split_hash = leaf_cpos + 1;
  3103. return 0;
  3104. }
  3105. if (val > insert_hash) {
  3106. /*
  3107. * val can not be the same as insert hash, and
  3108. * also must be larger than leaf_cpos. Also,
  3109. * we know that there can't be a leaf between
  3110. * cpos and val, otherwise the entries with
  3111. * hash 'val' would be there.
  3112. */
  3113. *split_hash = val;
  3114. return 0;
  3115. }
  3116. *split_hash = insert_hash;
  3117. return 0;
  3118. }
  3119. /*
  3120. * Since the records are sorted and the checks above
  3121. * guaranteed that not all records in this block are the same,
  3122. * we simple travel forward, from the median, and pick the 1st
  3123. * record whose value is larger than leaf_cpos.
  3124. */
  3125. for (i = (num_used / 2); i < num_used; i++)
  3126. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
  3127. leaf_cpos)
  3128. break;
  3129. BUG_ON(i == num_used); /* Should be impossible */
  3130. *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
  3131. return 0;
  3132. }
  3133. /*
  3134. * Transfer all entries in orig_dx_leaves whose major hash is equal to or
  3135. * larger than split_hash into new_dx_leaves. We use a temporary
  3136. * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
  3137. *
  3138. * Since the block offset inside a leaf (cluster) is a constant mask
  3139. * of minor_hash, we can optimize - an item at block offset X within
  3140. * the original cluster, will be at offset X within the new cluster.
  3141. */
  3142. static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
  3143. handle_t *handle,
  3144. struct ocfs2_dx_leaf *tmp_dx_leaf,
  3145. struct buffer_head **orig_dx_leaves,
  3146. struct buffer_head **new_dx_leaves,
  3147. int num_dx_leaves)
  3148. {
  3149. int i, j, num_used;
  3150. u32 major_hash;
  3151. struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
  3152. struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
  3153. struct ocfs2_dx_entry *dx_entry;
  3154. tmp_list = &tmp_dx_leaf->dl_list;
  3155. for (i = 0; i < num_dx_leaves; i++) {
  3156. orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
  3157. orig_list = &orig_dx_leaf->dl_list;
  3158. new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
  3159. new_list = &new_dx_leaf->dl_list;
  3160. num_used = le16_to_cpu(orig_list->de_num_used);
  3161. memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
  3162. tmp_list->de_num_used = cpu_to_le16(0);
  3163. memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
  3164. for (j = 0; j < num_used; j++) {
  3165. dx_entry = &orig_list->de_entries[j];
  3166. major_hash = le32_to_cpu(dx_entry->dx_major_hash);
  3167. if (major_hash >= split_hash)
  3168. ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
  3169. dx_entry);
  3170. else
  3171. ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
  3172. dx_entry);
  3173. }
  3174. memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
  3175. ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
  3176. ocfs2_journal_dirty(handle, new_dx_leaves[i]);
  3177. }
  3178. }
  3179. static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
  3180. struct ocfs2_dx_root_block *dx_root)
  3181. {
  3182. int credits = ocfs2_clusters_to_blocks(osb->sb, 3);
  3183. credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
  3184. credits += ocfs2_quota_trans_credits(osb->sb);
  3185. return credits;
  3186. }
  3187. /*
  3188. * Find the median value in dx_leaf_bh and allocate a new leaf to move
  3189. * half our entries into.
  3190. */
  3191. static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
  3192. struct buffer_head *dx_root_bh,
  3193. struct buffer_head *dx_leaf_bh,
  3194. struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
  3195. u64 leaf_blkno)
  3196. {
  3197. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3198. int credits, ret, i, num_used, did_quota = 0;
  3199. u32 cpos, split_hash, insert_hash = hinfo->major_hash;
  3200. u64 orig_leaves_start;
  3201. int num_dx_leaves;
  3202. struct buffer_head **orig_dx_leaves = NULL;
  3203. struct buffer_head **new_dx_leaves = NULL;
  3204. struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
  3205. struct ocfs2_extent_tree et;
  3206. handle_t *handle = NULL;
  3207. struct ocfs2_dx_root_block *dx_root;
  3208. struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
  3209. trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
  3210. (unsigned long long)leaf_blkno,
  3211. insert_hash);
  3212. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3213. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3214. /*
  3215. * XXX: This is a rather large limit. We should use a more
  3216. * realistic value.
  3217. */
  3218. if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
  3219. return -ENOSPC;
  3220. num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  3221. if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3222. mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
  3223. "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
  3224. (unsigned long long)leaf_blkno, num_used);
  3225. ret = -EIO;
  3226. goto out;
  3227. }
  3228. orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3229. if (!orig_dx_leaves) {
  3230. ret = -ENOMEM;
  3231. mlog_errno(ret);
  3232. goto out;
  3233. }
  3234. new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
  3235. if (!new_dx_leaves) {
  3236. ret = -ENOMEM;
  3237. mlog_errno(ret);
  3238. goto out;
  3239. }
  3240. ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
  3241. if (ret) {
  3242. if (ret != -ENOSPC)
  3243. mlog_errno(ret);
  3244. goto out;
  3245. }
  3246. credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
  3247. handle = ocfs2_start_trans(osb, credits);
  3248. if (IS_ERR(handle)) {
  3249. ret = PTR_ERR(handle);
  3250. handle = NULL;
  3251. mlog_errno(ret);
  3252. goto out;
  3253. }
  3254. ret = dquot_alloc_space_nodirty(dir,
  3255. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3256. if (ret)
  3257. goto out_commit;
  3258. did_quota = 1;
  3259. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
  3260. OCFS2_JOURNAL_ACCESS_WRITE);
  3261. if (ret) {
  3262. mlog_errno(ret);
  3263. goto out_commit;
  3264. }
  3265. /*
  3266. * This block is changing anyway, so we can sort it in place.
  3267. */
  3268. sort(dx_leaf->dl_list.de_entries, num_used,
  3269. sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
  3270. dx_leaf_sort_swap);
  3271. ocfs2_journal_dirty(handle, dx_leaf_bh);
  3272. ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
  3273. &split_hash);
  3274. if (ret) {
  3275. mlog_errno(ret);
  3276. goto out_commit;
  3277. }
  3278. trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
  3279. /*
  3280. * We have to carefully order operations here. There are items
  3281. * which want to be in the new cluster before insert, but in
  3282. * order to put those items in the new cluster, we alter the
  3283. * old cluster. A failure to insert gets nasty.
  3284. *
  3285. * So, start by reserving writes to the old
  3286. * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
  3287. * the new cluster for us, before inserting it. The insert
  3288. * won't happen if there's an error before that. Once the
  3289. * insert is done then, we can transfer from one leaf into the
  3290. * other without fear of hitting any error.
  3291. */
  3292. /*
  3293. * The leaf transfer wants some scratch space so that we don't
  3294. * wind up doing a bunch of expensive memmove().
  3295. */
  3296. tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
  3297. if (!tmp_dx_leaf) {
  3298. ret = -ENOMEM;
  3299. mlog_errno(ret);
  3300. goto out_commit;
  3301. }
  3302. orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
  3303. ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
  3304. orig_dx_leaves);
  3305. if (ret) {
  3306. mlog_errno(ret);
  3307. goto out_commit;
  3308. }
  3309. cpos = split_hash;
  3310. ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
  3311. data_ac, meta_ac, new_dx_leaves,
  3312. num_dx_leaves);
  3313. if (ret) {
  3314. mlog_errno(ret);
  3315. goto out_commit;
  3316. }
  3317. for (i = 0; i < num_dx_leaves; i++) {
  3318. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  3319. orig_dx_leaves[i],
  3320. OCFS2_JOURNAL_ACCESS_WRITE);
  3321. if (ret) {
  3322. mlog_errno(ret);
  3323. goto out_commit;
  3324. }
  3325. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  3326. new_dx_leaves[i],
  3327. OCFS2_JOURNAL_ACCESS_WRITE);
  3328. if (ret) {
  3329. mlog_errno(ret);
  3330. goto out_commit;
  3331. }
  3332. }
  3333. ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
  3334. orig_dx_leaves, new_dx_leaves, num_dx_leaves);
  3335. out_commit:
  3336. if (ret < 0 && did_quota)
  3337. dquot_free_space_nodirty(dir,
  3338. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3339. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  3340. ocfs2_commit_trans(osb, handle);
  3341. out:
  3342. if (orig_dx_leaves || new_dx_leaves) {
  3343. for (i = 0; i < num_dx_leaves; i++) {
  3344. if (orig_dx_leaves)
  3345. brelse(orig_dx_leaves[i]);
  3346. if (new_dx_leaves)
  3347. brelse(new_dx_leaves[i]);
  3348. }
  3349. kfree(orig_dx_leaves);
  3350. kfree(new_dx_leaves);
  3351. }
  3352. if (meta_ac)
  3353. ocfs2_free_alloc_context(meta_ac);
  3354. if (data_ac)
  3355. ocfs2_free_alloc_context(data_ac);
  3356. kfree(tmp_dx_leaf);
  3357. return ret;
  3358. }
  3359. static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
  3360. struct buffer_head *di_bh,
  3361. struct buffer_head *dx_root_bh,
  3362. const char *name, int namelen,
  3363. struct ocfs2_dir_lookup_result *lookup)
  3364. {
  3365. int ret, rebalanced = 0;
  3366. struct ocfs2_dx_root_block *dx_root;
  3367. struct buffer_head *dx_leaf_bh = NULL;
  3368. struct ocfs2_dx_leaf *dx_leaf;
  3369. u64 blkno;
  3370. u32 leaf_cpos;
  3371. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3372. restart_search:
  3373. ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
  3374. &leaf_cpos, &blkno);
  3375. if (ret) {
  3376. mlog_errno(ret);
  3377. goto out;
  3378. }
  3379. ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
  3380. if (ret) {
  3381. mlog_errno(ret);
  3382. goto out;
  3383. }
  3384. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3385. if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
  3386. le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3387. if (rebalanced) {
  3388. /*
  3389. * Rebalancing should have provided us with
  3390. * space in an appropriate leaf.
  3391. *
  3392. * XXX: Is this an abnormal condition then?
  3393. * Should we print a message here?
  3394. */
  3395. ret = -ENOSPC;
  3396. goto out;
  3397. }
  3398. ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
  3399. &lookup->dl_hinfo, leaf_cpos,
  3400. blkno);
  3401. if (ret) {
  3402. if (ret != -ENOSPC)
  3403. mlog_errno(ret);
  3404. goto out;
  3405. }
  3406. /*
  3407. * Restart the lookup. The rebalance might have
  3408. * changed which block our item fits into. Mark our
  3409. * progress, so we only execute this once.
  3410. */
  3411. brelse(dx_leaf_bh);
  3412. dx_leaf_bh = NULL;
  3413. rebalanced = 1;
  3414. goto restart_search;
  3415. }
  3416. lookup->dl_dx_leaf_bh = dx_leaf_bh;
  3417. dx_leaf_bh = NULL;
  3418. out:
  3419. brelse(dx_leaf_bh);
  3420. return ret;
  3421. }
  3422. static int ocfs2_search_dx_free_list(struct inode *dir,
  3423. struct buffer_head *dx_root_bh,
  3424. int namelen,
  3425. struct ocfs2_dir_lookup_result *lookup)
  3426. {
  3427. int ret = -ENOSPC;
  3428. struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
  3429. struct ocfs2_dir_block_trailer *db;
  3430. u64 next_block;
  3431. int rec_len = OCFS2_DIR_REC_LEN(namelen);
  3432. struct ocfs2_dx_root_block *dx_root;
  3433. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3434. next_block = le64_to_cpu(dx_root->dr_free_blk);
  3435. while (next_block) {
  3436. brelse(prev_leaf_bh);
  3437. prev_leaf_bh = leaf_bh;
  3438. leaf_bh = NULL;
  3439. ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
  3440. if (ret) {
  3441. mlog_errno(ret);
  3442. goto out;
  3443. }
  3444. db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  3445. if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
  3446. lookup->dl_leaf_bh = leaf_bh;
  3447. lookup->dl_prev_leaf_bh = prev_leaf_bh;
  3448. leaf_bh = NULL;
  3449. prev_leaf_bh = NULL;
  3450. break;
  3451. }
  3452. next_block = le64_to_cpu(db->db_free_next);
  3453. }
  3454. if (!next_block)
  3455. ret = -ENOSPC;
  3456. out:
  3457. brelse(leaf_bh);
  3458. brelse(prev_leaf_bh);
  3459. return ret;
  3460. }
  3461. static int ocfs2_expand_inline_dx_root(struct inode *dir,
  3462. struct buffer_head *dx_root_bh)
  3463. {
  3464. int ret, num_dx_leaves, i, j, did_quota = 0;
  3465. struct buffer_head **dx_leaves = NULL;
  3466. struct ocfs2_extent_tree et;
  3467. u64 insert_blkno;
  3468. struct ocfs2_alloc_context *data_ac = NULL;
  3469. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3470. handle_t *handle = NULL;
  3471. struct ocfs2_dx_root_block *dx_root;
  3472. struct ocfs2_dx_entry_list *entry_list;
  3473. struct ocfs2_dx_entry *dx_entry;
  3474. struct ocfs2_dx_leaf *target_leaf;
  3475. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  3476. if (ret) {
  3477. mlog_errno(ret);
  3478. goto out;
  3479. }
  3480. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3481. if (!dx_leaves) {
  3482. ret = -ENOMEM;
  3483. mlog_errno(ret);
  3484. goto out;
  3485. }
  3486. handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
  3487. if (IS_ERR(handle)) {
  3488. ret = PTR_ERR(handle);
  3489. mlog_errno(ret);
  3490. goto out;
  3491. }
  3492. ret = dquot_alloc_space_nodirty(dir,
  3493. ocfs2_clusters_to_bytes(osb->sb, 1));
  3494. if (ret)
  3495. goto out_commit;
  3496. did_quota = 1;
  3497. /*
  3498. * We do this up front, before the allocation, so that a
  3499. * failure to add the dx_root_bh to the journal won't result
  3500. * us losing clusters.
  3501. */
  3502. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  3503. OCFS2_JOURNAL_ACCESS_WRITE);
  3504. if (ret) {
  3505. mlog_errno(ret);
  3506. goto out_commit;
  3507. }
  3508. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
  3509. num_dx_leaves, &insert_blkno);
  3510. if (ret) {
  3511. mlog_errno(ret);
  3512. goto out_commit;
  3513. }
  3514. /*
  3515. * Transfer the entries from our dx_root into the appropriate
  3516. * block
  3517. */
  3518. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3519. entry_list = &dx_root->dr_entries;
  3520. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  3521. dx_entry = &entry_list->de_entries[i];
  3522. j = __ocfs2_dx_dir_hash_idx(osb,
  3523. le32_to_cpu(dx_entry->dx_minor_hash));
  3524. target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
  3525. ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
  3526. /* Each leaf has been passed to the journal already
  3527. * via __ocfs2_dx_dir_new_cluster() */
  3528. }
  3529. dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
  3530. memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
  3531. offsetof(struct ocfs2_dx_root_block, dr_list));
  3532. dx_root->dr_list.l_count =
  3533. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  3534. /* This should never fail considering we start with an empty
  3535. * dx_root. */
  3536. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3537. ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
  3538. if (ret)
  3539. mlog_errno(ret);
  3540. did_quota = 0;
  3541. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  3542. ocfs2_journal_dirty(handle, dx_root_bh);
  3543. out_commit:
  3544. if (ret < 0 && did_quota)
  3545. dquot_free_space_nodirty(dir,
  3546. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3547. ocfs2_commit_trans(osb, handle);
  3548. out:
  3549. if (data_ac)
  3550. ocfs2_free_alloc_context(data_ac);
  3551. if (dx_leaves) {
  3552. for (i = 0; i < num_dx_leaves; i++)
  3553. brelse(dx_leaves[i]);
  3554. kfree(dx_leaves);
  3555. }
  3556. return ret;
  3557. }
  3558. static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
  3559. {
  3560. struct ocfs2_dx_root_block *dx_root;
  3561. struct ocfs2_dx_entry_list *entry_list;
  3562. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3563. entry_list = &dx_root->dr_entries;
  3564. if (le16_to_cpu(entry_list->de_num_used) >=
  3565. le16_to_cpu(entry_list->de_count))
  3566. return -ENOSPC;
  3567. return 0;
  3568. }
  3569. static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
  3570. struct buffer_head *di_bh,
  3571. const char *name,
  3572. int namelen,
  3573. struct ocfs2_dir_lookup_result *lookup)
  3574. {
  3575. int ret, free_dx_root = 1;
  3576. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3577. struct buffer_head *dx_root_bh = NULL;
  3578. struct buffer_head *leaf_bh = NULL;
  3579. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3580. struct ocfs2_dx_root_block *dx_root;
  3581. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3582. if (ret) {
  3583. mlog_errno(ret);
  3584. goto out;
  3585. }
  3586. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3587. if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
  3588. ret = -ENOSPC;
  3589. mlog_errno(ret);
  3590. goto out;
  3591. }
  3592. if (ocfs2_dx_root_inline(dx_root)) {
  3593. ret = ocfs2_inline_dx_has_space(dx_root_bh);
  3594. if (ret == 0)
  3595. goto search_el;
  3596. /*
  3597. * We ran out of room in the root block. Expand it to
  3598. * an extent, then allow ocfs2_find_dir_space_dx to do
  3599. * the rest.
  3600. */
  3601. ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
  3602. if (ret) {
  3603. mlog_errno(ret);
  3604. goto out;
  3605. }
  3606. }
  3607. /*
  3608. * Insert preparation for an indexed directory is split into two
  3609. * steps. The call to find_dir_space_dx reserves room in the index for
  3610. * an additional item. If we run out of space there, it's a real error
  3611. * we can't continue on.
  3612. */
  3613. ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
  3614. namelen, lookup);
  3615. if (ret) {
  3616. mlog_errno(ret);
  3617. goto out;
  3618. }
  3619. search_el:
  3620. /*
  3621. * Next, we need to find space in the unindexed tree. This call
  3622. * searches using the free space linked list. If the unindexed tree
  3623. * lacks sufficient space, we'll expand it below. The expansion code
  3624. * is smart enough to add any new blocks to the free space list.
  3625. */
  3626. ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
  3627. if (ret && ret != -ENOSPC) {
  3628. mlog_errno(ret);
  3629. goto out;
  3630. }
  3631. /* Do this up here - ocfs2_extend_dir might need the dx_root */
  3632. lookup->dl_dx_root_bh = dx_root_bh;
  3633. free_dx_root = 0;
  3634. if (ret == -ENOSPC) {
  3635. ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
  3636. if (ret) {
  3637. mlog_errno(ret);
  3638. goto out;
  3639. }
  3640. /*
  3641. * We make the assumption here that new leaf blocks are added
  3642. * to the front of our free list.
  3643. */
  3644. lookup->dl_prev_leaf_bh = NULL;
  3645. lookup->dl_leaf_bh = leaf_bh;
  3646. }
  3647. out:
  3648. if (free_dx_root)
  3649. brelse(dx_root_bh);
  3650. return ret;
  3651. }
  3652. /*
  3653. * Get a directory ready for insert. Any directory allocation required
  3654. * happens here. Success returns zero, and enough context in the dir
  3655. * lookup result that ocfs2_add_entry() will be able complete the task
  3656. * with minimal performance impact.
  3657. */
  3658. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  3659. struct inode *dir,
  3660. struct buffer_head *parent_fe_bh,
  3661. const char *name,
  3662. int namelen,
  3663. struct ocfs2_dir_lookup_result *lookup)
  3664. {
  3665. int ret;
  3666. unsigned int blocks_wanted = 1;
  3667. struct buffer_head *bh = NULL;
  3668. trace_ocfs2_prepare_dir_for_insert(
  3669. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
  3670. if (!namelen) {
  3671. ret = -EINVAL;
  3672. mlog_errno(ret);
  3673. goto out;
  3674. }
  3675. /*
  3676. * Do this up front to reduce confusion.
  3677. *
  3678. * The directory might start inline, then be turned into an
  3679. * indexed one, in which case we'd need to hash deep inside
  3680. * ocfs2_find_dir_space_id(). Since
  3681. * ocfs2_prepare_dx_dir_for_insert() also needs this hash
  3682. * done, there seems no point in spreading out the calls. We
  3683. * can optimize away the case where the file system doesn't
  3684. * support indexing.
  3685. */
  3686. if (ocfs2_supports_indexed_dirs(osb))
  3687. ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
  3688. if (ocfs2_dir_indexed(dir)) {
  3689. ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
  3690. name, namelen, lookup);
  3691. if (ret)
  3692. mlog_errno(ret);
  3693. goto out;
  3694. }
  3695. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  3696. ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
  3697. namelen, &bh, &blocks_wanted);
  3698. } else
  3699. ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
  3700. if (ret && ret != -ENOSPC) {
  3701. mlog_errno(ret);
  3702. goto out;
  3703. }
  3704. if (ret == -ENOSPC) {
  3705. /*
  3706. * We have to expand the directory to add this name.
  3707. */
  3708. BUG_ON(bh);
  3709. ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
  3710. lookup, &bh);
  3711. if (ret) {
  3712. if (ret != -ENOSPC)
  3713. mlog_errno(ret);
  3714. goto out;
  3715. }
  3716. BUG_ON(!bh);
  3717. }
  3718. lookup->dl_leaf_bh = bh;
  3719. bh = NULL;
  3720. out:
  3721. brelse(bh);
  3722. return ret;
  3723. }
  3724. static int ocfs2_dx_dir_remove_index(struct inode *dir,
  3725. struct buffer_head *di_bh,
  3726. struct buffer_head *dx_root_bh)
  3727. {
  3728. int ret;
  3729. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3730. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3731. struct ocfs2_dx_root_block *dx_root;
  3732. struct inode *dx_alloc_inode = NULL;
  3733. struct buffer_head *dx_alloc_bh = NULL;
  3734. handle_t *handle;
  3735. u64 blk;
  3736. u16 bit;
  3737. u64 bg_blkno;
  3738. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3739. dx_alloc_inode = ocfs2_get_system_file_inode(osb,
  3740. EXTENT_ALLOC_SYSTEM_INODE,
  3741. le16_to_cpu(dx_root->dr_suballoc_slot));
  3742. if (!dx_alloc_inode) {
  3743. ret = -ENOMEM;
  3744. mlog_errno(ret);
  3745. goto out;
  3746. }
  3747. inode_lock(dx_alloc_inode);
  3748. ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
  3749. if (ret) {
  3750. mlog_errno(ret);
  3751. goto out_mutex;
  3752. }
  3753. handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
  3754. if (IS_ERR(handle)) {
  3755. ret = PTR_ERR(handle);
  3756. mlog_errno(ret);
  3757. goto out_unlock;
  3758. }
  3759. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  3760. OCFS2_JOURNAL_ACCESS_WRITE);
  3761. if (ret) {
  3762. mlog_errno(ret);
  3763. goto out_commit;
  3764. }
  3765. spin_lock(&OCFS2_I(dir)->ip_lock);
  3766. OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
  3767. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  3768. spin_unlock(&OCFS2_I(dir)->ip_lock);
  3769. di->i_dx_root = cpu_to_le64(0ULL);
  3770. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  3771. ocfs2_journal_dirty(handle, di_bh);
  3772. blk = le64_to_cpu(dx_root->dr_blkno);
  3773. bit = le16_to_cpu(dx_root->dr_suballoc_bit);
  3774. if (dx_root->dr_suballoc_loc)
  3775. bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
  3776. else
  3777. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  3778. ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
  3779. bit, bg_blkno, 1);
  3780. if (ret)
  3781. mlog_errno(ret);
  3782. out_commit:
  3783. ocfs2_commit_trans(osb, handle);
  3784. out_unlock:
  3785. ocfs2_inode_unlock(dx_alloc_inode, 1);
  3786. out_mutex:
  3787. inode_unlock(dx_alloc_inode);
  3788. brelse(dx_alloc_bh);
  3789. out:
  3790. iput(dx_alloc_inode);
  3791. return ret;
  3792. }
  3793. int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
  3794. {
  3795. int ret;
  3796. unsigned int uninitialized_var(clen);
  3797. u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
  3798. u64 uninitialized_var(blkno);
  3799. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3800. struct buffer_head *dx_root_bh = NULL;
  3801. struct ocfs2_dx_root_block *dx_root;
  3802. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3803. struct ocfs2_cached_dealloc_ctxt dealloc;
  3804. struct ocfs2_extent_tree et;
  3805. ocfs2_init_dealloc_ctxt(&dealloc);
  3806. if (!ocfs2_dir_indexed(dir))
  3807. return 0;
  3808. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3809. if (ret) {
  3810. mlog_errno(ret);
  3811. goto out;
  3812. }
  3813. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3814. if (ocfs2_dx_root_inline(dx_root))
  3815. goto remove_index;
  3816. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3817. /* XXX: What if dr_clusters is too large? */
  3818. while (le32_to_cpu(dx_root->dr_clusters)) {
  3819. ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
  3820. major_hash, &cpos, &blkno, &clen);
  3821. if (ret) {
  3822. mlog_errno(ret);
  3823. goto out;
  3824. }
  3825. p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
  3826. ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
  3827. &dealloc, 0, false);
  3828. if (ret) {
  3829. mlog_errno(ret);
  3830. goto out;
  3831. }
  3832. if (cpos == 0)
  3833. break;
  3834. major_hash = cpos - 1;
  3835. }
  3836. remove_index:
  3837. ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
  3838. if (ret) {
  3839. mlog_errno(ret);
  3840. goto out;
  3841. }
  3842. ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
  3843. out:
  3844. ocfs2_schedule_truncate_log_flush(osb, 1);
  3845. ocfs2_run_deallocs(osb, &dealloc);
  3846. brelse(dx_root_bh);
  3847. return ret;
  3848. }