namei.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/time.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/quotaops.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/bio.h>
  35. #include "ext4.h"
  36. #include "ext4_jbd2.h"
  37. #include "xattr.h"
  38. #include "acl.h"
  39. #include <trace/events/ext4.h>
  40. /*
  41. * define how far ahead to read directories while searching them.
  42. */
  43. #define NAMEI_RA_CHUNKS 2
  44. #define NAMEI_RA_BLOCKS 4
  45. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  46. static struct buffer_head *ext4_append(handle_t *handle,
  47. struct inode *inode,
  48. ext4_lblk_t *block)
  49. {
  50. struct buffer_head *bh;
  51. int err;
  52. if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
  53. ((inode->i_size >> 10) >=
  54. EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
  55. return ERR_PTR(-ENOSPC);
  56. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  57. bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
  58. if (IS_ERR(bh))
  59. return bh;
  60. inode->i_size += inode->i_sb->s_blocksize;
  61. EXT4_I(inode)->i_disksize = inode->i_size;
  62. BUFFER_TRACE(bh, "get_write_access");
  63. err = ext4_journal_get_write_access(handle, bh);
  64. if (err) {
  65. brelse(bh);
  66. ext4_std_error(inode->i_sb, err);
  67. return ERR_PTR(err);
  68. }
  69. return bh;
  70. }
  71. static int ext4_dx_csum_verify(struct inode *inode,
  72. struct ext4_dir_entry *dirent);
  73. typedef enum {
  74. EITHER, INDEX, DIRENT
  75. } dirblock_type_t;
  76. #define ext4_read_dirblock(inode, block, type) \
  77. __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
  78. static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
  79. ext4_lblk_t block,
  80. dirblock_type_t type,
  81. const char *func,
  82. unsigned int line)
  83. {
  84. struct buffer_head *bh;
  85. struct ext4_dir_entry *dirent;
  86. int is_dx_block = 0;
  87. bh = ext4_bread(NULL, inode, block, 0);
  88. if (IS_ERR(bh)) {
  89. __ext4_warning(inode->i_sb, func, line,
  90. "inode #%lu: lblock %lu: comm %s: "
  91. "error %ld reading directory block",
  92. inode->i_ino, (unsigned long)block,
  93. current->comm, PTR_ERR(bh));
  94. return bh;
  95. }
  96. if (!bh) {
  97. ext4_error_inode(inode, func, line, block,
  98. "Directory hole found");
  99. return ERR_PTR(-EFSCORRUPTED);
  100. }
  101. dirent = (struct ext4_dir_entry *) bh->b_data;
  102. /* Determine whether or not we have an index block */
  103. if (is_dx(inode)) {
  104. if (block == 0)
  105. is_dx_block = 1;
  106. else if (ext4_rec_len_from_disk(dirent->rec_len,
  107. inode->i_sb->s_blocksize) ==
  108. inode->i_sb->s_blocksize)
  109. is_dx_block = 1;
  110. }
  111. if (!is_dx_block && type == INDEX) {
  112. ext4_error_inode(inode, func, line, block,
  113. "directory leaf block found instead of index block");
  114. return ERR_PTR(-EFSCORRUPTED);
  115. }
  116. if (!ext4_has_metadata_csum(inode->i_sb) ||
  117. buffer_verified(bh))
  118. return bh;
  119. /*
  120. * An empty leaf block can get mistaken for a index block; for
  121. * this reason, we can only check the index checksum when the
  122. * caller is sure it should be an index block.
  123. */
  124. if (is_dx_block && type == INDEX) {
  125. if (ext4_dx_csum_verify(inode, dirent))
  126. set_buffer_verified(bh);
  127. else {
  128. ext4_error_inode(inode, func, line, block,
  129. "Directory index failed checksum");
  130. brelse(bh);
  131. return ERR_PTR(-EFSBADCRC);
  132. }
  133. }
  134. if (!is_dx_block) {
  135. if (ext4_dirent_csum_verify(inode, dirent))
  136. set_buffer_verified(bh);
  137. else {
  138. ext4_error_inode(inode, func, line, block,
  139. "Directory block failed checksum");
  140. brelse(bh);
  141. return ERR_PTR(-EFSBADCRC);
  142. }
  143. }
  144. return bh;
  145. }
  146. #ifndef assert
  147. #define assert(test) J_ASSERT(test)
  148. #endif
  149. #ifdef DX_DEBUG
  150. #define dxtrace(command) command
  151. #else
  152. #define dxtrace(command)
  153. #endif
  154. struct fake_dirent
  155. {
  156. __le32 inode;
  157. __le16 rec_len;
  158. u8 name_len;
  159. u8 file_type;
  160. };
  161. struct dx_countlimit
  162. {
  163. __le16 limit;
  164. __le16 count;
  165. };
  166. struct dx_entry
  167. {
  168. __le32 hash;
  169. __le32 block;
  170. };
  171. /*
  172. * dx_root_info is laid out so that if it should somehow get overlaid by a
  173. * dirent the two low bits of the hash version will be zero. Therefore, the
  174. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  175. */
  176. struct dx_root
  177. {
  178. struct fake_dirent dot;
  179. char dot_name[4];
  180. struct fake_dirent dotdot;
  181. char dotdot_name[4];
  182. struct dx_root_info
  183. {
  184. __le32 reserved_zero;
  185. u8 hash_version;
  186. u8 info_length; /* 8 */
  187. u8 indirect_levels;
  188. u8 unused_flags;
  189. }
  190. info;
  191. struct dx_entry entries[0];
  192. };
  193. struct dx_node
  194. {
  195. struct fake_dirent fake;
  196. struct dx_entry entries[0];
  197. };
  198. struct dx_frame
  199. {
  200. struct buffer_head *bh;
  201. struct dx_entry *entries;
  202. struct dx_entry *at;
  203. };
  204. struct dx_map_entry
  205. {
  206. u32 hash;
  207. u16 offs;
  208. u16 size;
  209. };
  210. /*
  211. * This goes at the end of each htree block.
  212. */
  213. struct dx_tail {
  214. u32 dt_reserved;
  215. __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
  216. };
  217. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  218. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  219. static inline unsigned dx_get_hash(struct dx_entry *entry);
  220. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  221. static unsigned dx_get_count(struct dx_entry *entries);
  222. static unsigned dx_get_limit(struct dx_entry *entries);
  223. static void dx_set_count(struct dx_entry *entries, unsigned value);
  224. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  225. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  226. static unsigned dx_node_limit(struct inode *dir);
  227. static struct dx_frame *dx_probe(struct ext4_filename *fname,
  228. struct inode *dir,
  229. struct dx_hash_info *hinfo,
  230. struct dx_frame *frame);
  231. static void dx_release(struct dx_frame *frames);
  232. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  233. unsigned blocksize, struct dx_hash_info *hinfo,
  234. struct dx_map_entry map[]);
  235. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  236. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  237. struct dx_map_entry *offsets, int count, unsigned blocksize);
  238. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  239. static void dx_insert_block(struct dx_frame *frame,
  240. u32 hash, ext4_lblk_t block);
  241. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  242. struct dx_frame *frame,
  243. struct dx_frame *frames,
  244. __u32 *start_hash);
  245. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  246. struct ext4_filename *fname,
  247. struct ext4_dir_entry_2 **res_dir);
  248. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  249. struct dentry *dentry, struct inode *inode);
  250. /* checksumming functions */
  251. void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
  252. unsigned int blocksize)
  253. {
  254. memset(t, 0, sizeof(struct ext4_dir_entry_tail));
  255. t->det_rec_len = ext4_rec_len_to_disk(
  256. sizeof(struct ext4_dir_entry_tail), blocksize);
  257. t->det_reserved_ft = EXT4_FT_DIR_CSUM;
  258. }
  259. /* Walk through a dirent block to find a checksum "dirent" at the tail */
  260. static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
  261. struct ext4_dir_entry *de)
  262. {
  263. struct ext4_dir_entry_tail *t;
  264. #ifdef PARANOID
  265. struct ext4_dir_entry *d, *top;
  266. d = de;
  267. top = (struct ext4_dir_entry *)(((void *)de) +
  268. (EXT4_BLOCK_SIZE(inode->i_sb) -
  269. sizeof(struct ext4_dir_entry_tail)));
  270. while (d < top && d->rec_len)
  271. d = (struct ext4_dir_entry *)(((void *)d) +
  272. le16_to_cpu(d->rec_len));
  273. if (d != top)
  274. return NULL;
  275. t = (struct ext4_dir_entry_tail *)d;
  276. #else
  277. t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
  278. #endif
  279. if (t->det_reserved_zero1 ||
  280. le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
  281. t->det_reserved_zero2 ||
  282. t->det_reserved_ft != EXT4_FT_DIR_CSUM)
  283. return NULL;
  284. return t;
  285. }
  286. static __le32 ext4_dirent_csum(struct inode *inode,
  287. struct ext4_dir_entry *dirent, int size)
  288. {
  289. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  290. struct ext4_inode_info *ei = EXT4_I(inode);
  291. __u32 csum;
  292. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  293. return cpu_to_le32(csum);
  294. }
  295. #define warn_no_space_for_csum(inode) \
  296. __warn_no_space_for_csum((inode), __func__, __LINE__)
  297. static void __warn_no_space_for_csum(struct inode *inode, const char *func,
  298. unsigned int line)
  299. {
  300. __ext4_warning_inode(inode, func, line,
  301. "No space for directory leaf checksum. Please run e2fsck -D.");
  302. }
  303. int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
  304. {
  305. struct ext4_dir_entry_tail *t;
  306. if (!ext4_has_metadata_csum(inode->i_sb))
  307. return 1;
  308. t = get_dirent_tail(inode, dirent);
  309. if (!t) {
  310. warn_no_space_for_csum(inode);
  311. return 0;
  312. }
  313. if (t->det_checksum != ext4_dirent_csum(inode, dirent,
  314. (void *)t - (void *)dirent))
  315. return 0;
  316. return 1;
  317. }
  318. static void ext4_dirent_csum_set(struct inode *inode,
  319. struct ext4_dir_entry *dirent)
  320. {
  321. struct ext4_dir_entry_tail *t;
  322. if (!ext4_has_metadata_csum(inode->i_sb))
  323. return;
  324. t = get_dirent_tail(inode, dirent);
  325. if (!t) {
  326. warn_no_space_for_csum(inode);
  327. return;
  328. }
  329. t->det_checksum = ext4_dirent_csum(inode, dirent,
  330. (void *)t - (void *)dirent);
  331. }
  332. int ext4_handle_dirty_dirent_node(handle_t *handle,
  333. struct inode *inode,
  334. struct buffer_head *bh)
  335. {
  336. ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  337. return ext4_handle_dirty_metadata(handle, inode, bh);
  338. }
  339. static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
  340. struct ext4_dir_entry *dirent,
  341. int *offset)
  342. {
  343. struct ext4_dir_entry *dp;
  344. struct dx_root_info *root;
  345. int count_offset;
  346. if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
  347. count_offset = 8;
  348. else if (le16_to_cpu(dirent->rec_len) == 12) {
  349. dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
  350. if (le16_to_cpu(dp->rec_len) !=
  351. EXT4_BLOCK_SIZE(inode->i_sb) - 12)
  352. return NULL;
  353. root = (struct dx_root_info *)(((void *)dp + 12));
  354. if (root->reserved_zero ||
  355. root->info_length != sizeof(struct dx_root_info))
  356. return NULL;
  357. count_offset = 32;
  358. } else
  359. return NULL;
  360. if (offset)
  361. *offset = count_offset;
  362. return (struct dx_countlimit *)(((void *)dirent) + count_offset);
  363. }
  364. static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
  365. int count_offset, int count, struct dx_tail *t)
  366. {
  367. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  368. struct ext4_inode_info *ei = EXT4_I(inode);
  369. __u32 csum;
  370. __le32 save_csum;
  371. int size;
  372. size = count_offset + (count * sizeof(struct dx_entry));
  373. save_csum = t->dt_checksum;
  374. t->dt_checksum = 0;
  375. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  376. csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
  377. t->dt_checksum = save_csum;
  378. return cpu_to_le32(csum);
  379. }
  380. static int ext4_dx_csum_verify(struct inode *inode,
  381. struct ext4_dir_entry *dirent)
  382. {
  383. struct dx_countlimit *c;
  384. struct dx_tail *t;
  385. int count_offset, limit, count;
  386. if (!ext4_has_metadata_csum(inode->i_sb))
  387. return 1;
  388. c = get_dx_countlimit(inode, dirent, &count_offset);
  389. if (!c) {
  390. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  391. return 1;
  392. }
  393. limit = le16_to_cpu(c->limit);
  394. count = le16_to_cpu(c->count);
  395. if (count_offset + (limit * sizeof(struct dx_entry)) >
  396. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  397. warn_no_space_for_csum(inode);
  398. return 1;
  399. }
  400. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  401. if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
  402. count, t))
  403. return 0;
  404. return 1;
  405. }
  406. static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
  407. {
  408. struct dx_countlimit *c;
  409. struct dx_tail *t;
  410. int count_offset, limit, count;
  411. if (!ext4_has_metadata_csum(inode->i_sb))
  412. return;
  413. c = get_dx_countlimit(inode, dirent, &count_offset);
  414. if (!c) {
  415. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  416. return;
  417. }
  418. limit = le16_to_cpu(c->limit);
  419. count = le16_to_cpu(c->count);
  420. if (count_offset + (limit * sizeof(struct dx_entry)) >
  421. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  422. warn_no_space_for_csum(inode);
  423. return;
  424. }
  425. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  426. t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
  427. }
  428. static inline int ext4_handle_dirty_dx_node(handle_t *handle,
  429. struct inode *inode,
  430. struct buffer_head *bh)
  431. {
  432. ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  433. return ext4_handle_dirty_metadata(handle, inode, bh);
  434. }
  435. /*
  436. * p is at least 6 bytes before the end of page
  437. */
  438. static inline struct ext4_dir_entry_2 *
  439. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  440. {
  441. return (struct ext4_dir_entry_2 *)((char *)p +
  442. ext4_rec_len_from_disk(p->rec_len, blocksize));
  443. }
  444. /*
  445. * Future: use high four bits of block for coalesce-on-delete flags
  446. * Mask them off for now.
  447. */
  448. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  449. {
  450. return le32_to_cpu(entry->block) & 0x00ffffff;
  451. }
  452. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  453. {
  454. entry->block = cpu_to_le32(value);
  455. }
  456. static inline unsigned dx_get_hash(struct dx_entry *entry)
  457. {
  458. return le32_to_cpu(entry->hash);
  459. }
  460. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  461. {
  462. entry->hash = cpu_to_le32(value);
  463. }
  464. static inline unsigned dx_get_count(struct dx_entry *entries)
  465. {
  466. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  467. }
  468. static inline unsigned dx_get_limit(struct dx_entry *entries)
  469. {
  470. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  471. }
  472. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  473. {
  474. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  475. }
  476. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  477. {
  478. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  479. }
  480. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  481. {
  482. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  483. EXT4_DIR_REC_LEN(2) - infosize;
  484. if (ext4_has_metadata_csum(dir->i_sb))
  485. entry_space -= sizeof(struct dx_tail);
  486. return entry_space / sizeof(struct dx_entry);
  487. }
  488. static inline unsigned dx_node_limit(struct inode *dir)
  489. {
  490. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  491. if (ext4_has_metadata_csum(dir->i_sb))
  492. entry_space -= sizeof(struct dx_tail);
  493. return entry_space / sizeof(struct dx_entry);
  494. }
  495. /*
  496. * Debug
  497. */
  498. #ifdef DX_DEBUG
  499. static void dx_show_index(char * label, struct dx_entry *entries)
  500. {
  501. int i, n = dx_get_count (entries);
  502. printk(KERN_DEBUG "%s index ", label);
  503. for (i = 0; i < n; i++) {
  504. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  505. 0, (unsigned long)dx_get_block(entries + i));
  506. }
  507. printk("\n");
  508. }
  509. struct stats
  510. {
  511. unsigned names;
  512. unsigned space;
  513. unsigned bcount;
  514. };
  515. static struct stats dx_show_leaf(struct inode *dir,
  516. struct dx_hash_info *hinfo,
  517. struct ext4_dir_entry_2 *de,
  518. int size, int show_names)
  519. {
  520. unsigned names = 0, space = 0;
  521. char *base = (char *) de;
  522. struct dx_hash_info h = *hinfo;
  523. printk("names: ");
  524. while ((char *) de < base + size)
  525. {
  526. if (de->inode)
  527. {
  528. if (show_names)
  529. {
  530. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  531. int len;
  532. char *name;
  533. struct ext4_str fname_crypto_str
  534. = {.name = NULL, .len = 0};
  535. int res = 0;
  536. name = de->name;
  537. len = de->name_len;
  538. if (ext4_encrypted_inode(inode))
  539. res = ext4_get_encryption_info(dir);
  540. if (res) {
  541. printk(KERN_WARNING "Error setting up"
  542. " fname crypto: %d\n", res);
  543. }
  544. if (ctx == NULL) {
  545. /* Directory is not encrypted */
  546. ext4fs_dirhash(de->name,
  547. de->name_len, &h);
  548. printk("%*.s:(U)%x.%u ", len,
  549. name, h.hash,
  550. (unsigned) ((char *) de
  551. - base));
  552. } else {
  553. /* Directory is encrypted */
  554. res = ext4_fname_crypto_alloc_buffer(
  555. ctx, de->name_len,
  556. &fname_crypto_str);
  557. if (res < 0) {
  558. printk(KERN_WARNING "Error "
  559. "allocating crypto "
  560. "buffer--skipping "
  561. "crypto\n");
  562. ctx = NULL;
  563. }
  564. res = ext4_fname_disk_to_usr(ctx, NULL, de,
  565. &fname_crypto_str);
  566. if (res < 0) {
  567. printk(KERN_WARNING "Error "
  568. "converting filename "
  569. "from disk to usr"
  570. "\n");
  571. name = "??";
  572. len = 2;
  573. } else {
  574. name = fname_crypto_str.name;
  575. len = fname_crypto_str.len;
  576. }
  577. ext4fs_dirhash(de->name, de->name_len,
  578. &h);
  579. printk("%*.s:(E)%x.%u ", len, name,
  580. h.hash, (unsigned) ((char *) de
  581. - base));
  582. ext4_fname_crypto_free_buffer(
  583. &fname_crypto_str);
  584. }
  585. #else
  586. int len = de->name_len;
  587. char *name = de->name;
  588. ext4fs_dirhash(de->name, de->name_len, &h);
  589. printk("%*.s:%x.%u ", len, name, h.hash,
  590. (unsigned) ((char *) de - base));
  591. #endif
  592. }
  593. space += EXT4_DIR_REC_LEN(de->name_len);
  594. names++;
  595. }
  596. de = ext4_next_entry(de, size);
  597. }
  598. printk("(%i)\n", names);
  599. return (struct stats) { names, space, 1 };
  600. }
  601. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  602. struct dx_entry *entries, int levels)
  603. {
  604. unsigned blocksize = dir->i_sb->s_blocksize;
  605. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  606. unsigned bcount = 0;
  607. struct buffer_head *bh;
  608. printk("%i indexed blocks...\n", count);
  609. for (i = 0; i < count; i++, entries++)
  610. {
  611. ext4_lblk_t block = dx_get_block(entries);
  612. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  613. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  614. struct stats stats;
  615. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  616. bh = ext4_bread(NULL,dir, block, 0);
  617. if (!bh || IS_ERR(bh))
  618. continue;
  619. stats = levels?
  620. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  621. dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
  622. bh->b_data, blocksize, 0);
  623. names += stats.names;
  624. space += stats.space;
  625. bcount += stats.bcount;
  626. brelse(bh);
  627. }
  628. if (bcount)
  629. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  630. levels ? "" : " ", names, space/bcount,
  631. (space/bcount)*100/blocksize);
  632. return (struct stats) { names, space, bcount};
  633. }
  634. #endif /* DX_DEBUG */
  635. /*
  636. * Probe for a directory leaf block to search.
  637. *
  638. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  639. * error in the directory index, and the caller should fall back to
  640. * searching the directory normally. The callers of dx_probe **MUST**
  641. * check for this error code, and make sure it never gets reflected
  642. * back to userspace.
  643. */
  644. static struct dx_frame *
  645. dx_probe(struct ext4_filename *fname, struct inode *dir,
  646. struct dx_hash_info *hinfo, struct dx_frame *frame_in)
  647. {
  648. unsigned count, indirect;
  649. struct dx_entry *at, *entries, *p, *q, *m;
  650. struct dx_root *root;
  651. struct dx_frame *frame = frame_in;
  652. struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
  653. u32 hash;
  654. frame->bh = ext4_read_dirblock(dir, 0, INDEX);
  655. if (IS_ERR(frame->bh))
  656. return (struct dx_frame *) frame->bh;
  657. root = (struct dx_root *) frame->bh->b_data;
  658. if (root->info.hash_version != DX_HASH_TEA &&
  659. root->info.hash_version != DX_HASH_HALF_MD4 &&
  660. root->info.hash_version != DX_HASH_LEGACY) {
  661. ext4_warning_inode(dir, "Unrecognised inode hash code %u",
  662. root->info.hash_version);
  663. goto fail;
  664. }
  665. if (fname)
  666. hinfo = &fname->hinfo;
  667. hinfo->hash_version = root->info.hash_version;
  668. if (hinfo->hash_version <= DX_HASH_TEA)
  669. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  670. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  671. if (fname && fname_name(fname))
  672. ext4fs_dirhash(fname_name(fname), fname_len(fname), hinfo);
  673. hash = hinfo->hash;
  674. if (root->info.unused_flags & 1) {
  675. ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
  676. root->info.unused_flags);
  677. goto fail;
  678. }
  679. indirect = root->info.indirect_levels;
  680. if (indirect > 1) {
  681. ext4_warning_inode(dir, "Unimplemented hash depth: %#06x",
  682. root->info.indirect_levels);
  683. goto fail;
  684. }
  685. entries = (struct dx_entry *)(((char *)&root->info) +
  686. root->info.info_length);
  687. if (dx_get_limit(entries) != dx_root_limit(dir,
  688. root->info.info_length)) {
  689. ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
  690. dx_get_limit(entries),
  691. dx_root_limit(dir, root->info.info_length));
  692. goto fail;
  693. }
  694. dxtrace(printk("Look up %x", hash));
  695. while (1) {
  696. count = dx_get_count(entries);
  697. if (!count || count > dx_get_limit(entries)) {
  698. ext4_warning_inode(dir,
  699. "dx entry: count %u beyond limit %u",
  700. count, dx_get_limit(entries));
  701. goto fail;
  702. }
  703. p = entries + 1;
  704. q = entries + count - 1;
  705. while (p <= q) {
  706. m = p + (q - p) / 2;
  707. dxtrace(printk("."));
  708. if (dx_get_hash(m) > hash)
  709. q = m - 1;
  710. else
  711. p = m + 1;
  712. }
  713. if (0) { // linear search cross check
  714. unsigned n = count - 1;
  715. at = entries;
  716. while (n--)
  717. {
  718. dxtrace(printk(","));
  719. if (dx_get_hash(++at) > hash)
  720. {
  721. at--;
  722. break;
  723. }
  724. }
  725. assert (at == p - 1);
  726. }
  727. at = p - 1;
  728. dxtrace(printk(" %x->%u\n", at == entries ? 0 : dx_get_hash(at),
  729. dx_get_block(at)));
  730. frame->entries = entries;
  731. frame->at = at;
  732. if (!indirect--)
  733. return frame;
  734. frame++;
  735. frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
  736. if (IS_ERR(frame->bh)) {
  737. ret_err = (struct dx_frame *) frame->bh;
  738. frame->bh = NULL;
  739. goto fail;
  740. }
  741. entries = ((struct dx_node *) frame->bh->b_data)->entries;
  742. if (dx_get_limit(entries) != dx_node_limit(dir)) {
  743. ext4_warning_inode(dir,
  744. "dx entry: limit %u != node limit %u",
  745. dx_get_limit(entries), dx_node_limit(dir));
  746. goto fail;
  747. }
  748. }
  749. fail:
  750. while (frame >= frame_in) {
  751. brelse(frame->bh);
  752. frame--;
  753. }
  754. if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
  755. ext4_warning_inode(dir,
  756. "Corrupt directory, running e2fsck is recommended");
  757. return ret_err;
  758. }
  759. static void dx_release(struct dx_frame *frames)
  760. {
  761. if (frames[0].bh == NULL)
  762. return;
  763. if (((struct dx_root *)frames[0].bh->b_data)->info.indirect_levels)
  764. brelse(frames[1].bh);
  765. brelse(frames[0].bh);
  766. }
  767. /*
  768. * This function increments the frame pointer to search the next leaf
  769. * block, and reads in the necessary intervening nodes if the search
  770. * should be necessary. Whether or not the search is necessary is
  771. * controlled by the hash parameter. If the hash value is even, then
  772. * the search is only continued if the next block starts with that
  773. * hash value. This is used if we are searching for a specific file.
  774. *
  775. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  776. *
  777. * This function returns 1 if the caller should continue to search,
  778. * or 0 if it should not. If there is an error reading one of the
  779. * index blocks, it will a negative error code.
  780. *
  781. * If start_hash is non-null, it will be filled in with the starting
  782. * hash of the next page.
  783. */
  784. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  785. struct dx_frame *frame,
  786. struct dx_frame *frames,
  787. __u32 *start_hash)
  788. {
  789. struct dx_frame *p;
  790. struct buffer_head *bh;
  791. int num_frames = 0;
  792. __u32 bhash;
  793. p = frame;
  794. /*
  795. * Find the next leaf page by incrementing the frame pointer.
  796. * If we run out of entries in the interior node, loop around and
  797. * increment pointer in the parent node. When we break out of
  798. * this loop, num_frames indicates the number of interior
  799. * nodes need to be read.
  800. */
  801. while (1) {
  802. if (++(p->at) < p->entries + dx_get_count(p->entries))
  803. break;
  804. if (p == frames)
  805. return 0;
  806. num_frames++;
  807. p--;
  808. }
  809. /*
  810. * If the hash is 1, then continue only if the next page has a
  811. * continuation hash of any value. This is used for readdir
  812. * handling. Otherwise, check to see if the hash matches the
  813. * desired contiuation hash. If it doesn't, return since
  814. * there's no point to read in the successive index pages.
  815. */
  816. bhash = dx_get_hash(p->at);
  817. if (start_hash)
  818. *start_hash = bhash;
  819. if ((hash & 1) == 0) {
  820. if ((bhash & ~1) != hash)
  821. return 0;
  822. }
  823. /*
  824. * If the hash is HASH_NB_ALWAYS, we always go to the next
  825. * block so no check is necessary
  826. */
  827. while (num_frames--) {
  828. bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
  829. if (IS_ERR(bh))
  830. return PTR_ERR(bh);
  831. p++;
  832. brelse(p->bh);
  833. p->bh = bh;
  834. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  835. }
  836. return 1;
  837. }
  838. /*
  839. * This function fills a red-black tree with information from a
  840. * directory block. It returns the number directory entries loaded
  841. * into the tree. If there is an error it is returned in err.
  842. */
  843. static int htree_dirblock_to_tree(struct file *dir_file,
  844. struct inode *dir, ext4_lblk_t block,
  845. struct dx_hash_info *hinfo,
  846. __u32 start_hash, __u32 start_minor_hash)
  847. {
  848. struct buffer_head *bh;
  849. struct ext4_dir_entry_2 *de, *top;
  850. int err = 0, count = 0;
  851. struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
  852. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  853. (unsigned long)block));
  854. bh = ext4_read_dirblock(dir, block, DIRENT);
  855. if (IS_ERR(bh))
  856. return PTR_ERR(bh);
  857. de = (struct ext4_dir_entry_2 *) bh->b_data;
  858. top = (struct ext4_dir_entry_2 *) ((char *) de +
  859. dir->i_sb->s_blocksize -
  860. EXT4_DIR_REC_LEN(0));
  861. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  862. /* Check if the directory is encrypted */
  863. if (ext4_encrypted_inode(dir)) {
  864. err = ext4_get_encryption_info(dir);
  865. if (err < 0) {
  866. brelse(bh);
  867. return err;
  868. }
  869. err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
  870. &fname_crypto_str);
  871. if (err < 0) {
  872. brelse(bh);
  873. return err;
  874. }
  875. }
  876. #endif
  877. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  878. if (ext4_check_dir_entry(dir, NULL, de, bh,
  879. bh->b_data, bh->b_size,
  880. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  881. + ((char *)de - bh->b_data))) {
  882. /* silently ignore the rest of the block */
  883. break;
  884. }
  885. ext4fs_dirhash(de->name, de->name_len, hinfo);
  886. if ((hinfo->hash < start_hash) ||
  887. ((hinfo->hash == start_hash) &&
  888. (hinfo->minor_hash < start_minor_hash)))
  889. continue;
  890. if (de->inode == 0)
  891. continue;
  892. if (!ext4_encrypted_inode(dir)) {
  893. tmp_str.name = de->name;
  894. tmp_str.len = de->name_len;
  895. err = ext4_htree_store_dirent(dir_file,
  896. hinfo->hash, hinfo->minor_hash, de,
  897. &tmp_str);
  898. } else {
  899. int save_len = fname_crypto_str.len;
  900. /* Directory is encrypted */
  901. err = ext4_fname_disk_to_usr(dir, hinfo, de,
  902. &fname_crypto_str);
  903. if (err < 0) {
  904. count = err;
  905. goto errout;
  906. }
  907. err = ext4_htree_store_dirent(dir_file,
  908. hinfo->hash, hinfo->minor_hash, de,
  909. &fname_crypto_str);
  910. fname_crypto_str.len = save_len;
  911. }
  912. if (err != 0) {
  913. count = err;
  914. goto errout;
  915. }
  916. count++;
  917. }
  918. errout:
  919. brelse(bh);
  920. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  921. ext4_fname_crypto_free_buffer(&fname_crypto_str);
  922. #endif
  923. return count;
  924. }
  925. /*
  926. * This function fills a red-black tree with information from a
  927. * directory. We start scanning the directory in hash order, starting
  928. * at start_hash and start_minor_hash.
  929. *
  930. * This function returns the number of entries inserted into the tree,
  931. * or a negative error code.
  932. */
  933. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  934. __u32 start_minor_hash, __u32 *next_hash)
  935. {
  936. struct dx_hash_info hinfo;
  937. struct ext4_dir_entry_2 *de;
  938. struct dx_frame frames[2], *frame;
  939. struct inode *dir;
  940. ext4_lblk_t block;
  941. int count = 0;
  942. int ret, err;
  943. __u32 hashval;
  944. struct ext4_str tmp_str;
  945. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  946. start_hash, start_minor_hash));
  947. dir = file_inode(dir_file);
  948. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  949. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  950. if (hinfo.hash_version <= DX_HASH_TEA)
  951. hinfo.hash_version +=
  952. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  953. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  954. if (ext4_has_inline_data(dir)) {
  955. int has_inline_data = 1;
  956. count = htree_inlinedir_to_tree(dir_file, dir, 0,
  957. &hinfo, start_hash,
  958. start_minor_hash,
  959. &has_inline_data);
  960. if (has_inline_data) {
  961. *next_hash = ~0;
  962. return count;
  963. }
  964. }
  965. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  966. start_hash, start_minor_hash);
  967. *next_hash = ~0;
  968. return count;
  969. }
  970. hinfo.hash = start_hash;
  971. hinfo.minor_hash = 0;
  972. frame = dx_probe(NULL, dir, &hinfo, frames);
  973. if (IS_ERR(frame))
  974. return PTR_ERR(frame);
  975. /* Add '.' and '..' from the htree header */
  976. if (!start_hash && !start_minor_hash) {
  977. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  978. tmp_str.name = de->name;
  979. tmp_str.len = de->name_len;
  980. err = ext4_htree_store_dirent(dir_file, 0, 0,
  981. de, &tmp_str);
  982. if (err != 0)
  983. goto errout;
  984. count++;
  985. }
  986. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  987. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  988. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  989. tmp_str.name = de->name;
  990. tmp_str.len = de->name_len;
  991. err = ext4_htree_store_dirent(dir_file, 2, 0,
  992. de, &tmp_str);
  993. if (err != 0)
  994. goto errout;
  995. count++;
  996. }
  997. while (1) {
  998. block = dx_get_block(frame->at);
  999. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  1000. start_hash, start_minor_hash);
  1001. if (ret < 0) {
  1002. err = ret;
  1003. goto errout;
  1004. }
  1005. count += ret;
  1006. hashval = ~0;
  1007. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  1008. frame, frames, &hashval);
  1009. *next_hash = hashval;
  1010. if (ret < 0) {
  1011. err = ret;
  1012. goto errout;
  1013. }
  1014. /*
  1015. * Stop if: (a) there are no more entries, or
  1016. * (b) we have inserted at least one entry and the
  1017. * next hash value is not a continuation
  1018. */
  1019. if ((ret == 0) ||
  1020. (count && ((hashval & 1) == 0)))
  1021. break;
  1022. }
  1023. dx_release(frames);
  1024. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  1025. "next hash: %x\n", count, *next_hash));
  1026. return count;
  1027. errout:
  1028. dx_release(frames);
  1029. return (err);
  1030. }
  1031. static inline int search_dirblock(struct buffer_head *bh,
  1032. struct inode *dir,
  1033. struct ext4_filename *fname,
  1034. const struct qstr *d_name,
  1035. unsigned int offset,
  1036. struct ext4_dir_entry_2 **res_dir)
  1037. {
  1038. return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
  1039. fname, d_name, offset, res_dir);
  1040. }
  1041. /*
  1042. * Directory block splitting, compacting
  1043. */
  1044. /*
  1045. * Create map of hash values, offsets, and sizes, stored at end of block.
  1046. * Returns number of entries mapped.
  1047. */
  1048. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  1049. unsigned blocksize, struct dx_hash_info *hinfo,
  1050. struct dx_map_entry *map_tail)
  1051. {
  1052. int count = 0;
  1053. char *base = (char *) de;
  1054. struct dx_hash_info h = *hinfo;
  1055. while ((char *) de < base + blocksize) {
  1056. if (de->name_len && de->inode) {
  1057. ext4fs_dirhash(de->name, de->name_len, &h);
  1058. map_tail--;
  1059. map_tail->hash = h.hash;
  1060. map_tail->offs = ((char *) de - base)>>2;
  1061. map_tail->size = le16_to_cpu(de->rec_len);
  1062. count++;
  1063. cond_resched();
  1064. }
  1065. /* XXX: do we need to check rec_len == 0 case? -Chris */
  1066. de = ext4_next_entry(de, blocksize);
  1067. }
  1068. return count;
  1069. }
  1070. /* Sort map by hash value */
  1071. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  1072. {
  1073. struct dx_map_entry *p, *q, *top = map + count - 1;
  1074. int more;
  1075. /* Combsort until bubble sort doesn't suck */
  1076. while (count > 2) {
  1077. count = count*10/13;
  1078. if (count - 9 < 2) /* 9, 10 -> 11 */
  1079. count = 11;
  1080. for (p = top, q = p - count; q >= map; p--, q--)
  1081. if (p->hash < q->hash)
  1082. swap(*p, *q);
  1083. }
  1084. /* Garden variety bubble sort */
  1085. do {
  1086. more = 0;
  1087. q = top;
  1088. while (q-- > map) {
  1089. if (q[1].hash >= q[0].hash)
  1090. continue;
  1091. swap(*(q+1), *q);
  1092. more = 1;
  1093. }
  1094. } while(more);
  1095. }
  1096. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  1097. {
  1098. struct dx_entry *entries = frame->entries;
  1099. struct dx_entry *old = frame->at, *new = old + 1;
  1100. int count = dx_get_count(entries);
  1101. assert(count < dx_get_limit(entries));
  1102. assert(old < entries + count);
  1103. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  1104. dx_set_hash(new, hash);
  1105. dx_set_block(new, block);
  1106. dx_set_count(entries, count + 1);
  1107. }
  1108. /*
  1109. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  1110. *
  1111. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  1112. * `de != NULL' is guaranteed by caller.
  1113. */
  1114. static inline int ext4_match(struct ext4_filename *fname,
  1115. struct ext4_dir_entry_2 *de)
  1116. {
  1117. const void *name = fname_name(fname);
  1118. u32 len = fname_len(fname);
  1119. if (!de->inode)
  1120. return 0;
  1121. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1122. if (unlikely(!name)) {
  1123. if (fname->usr_fname->name[0] == '_') {
  1124. int ret;
  1125. if (de->name_len < 16)
  1126. return 0;
  1127. ret = memcmp(de->name + de->name_len - 16,
  1128. fname->crypto_buf.name + 8, 16);
  1129. return (ret == 0) ? 1 : 0;
  1130. }
  1131. name = fname->crypto_buf.name;
  1132. len = fname->crypto_buf.len;
  1133. }
  1134. #endif
  1135. if (de->name_len != len)
  1136. return 0;
  1137. return (memcmp(de->name, name, len) == 0) ? 1 : 0;
  1138. }
  1139. /*
  1140. * Returns 0 if not found, -1 on failure, and 1 on success
  1141. */
  1142. int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
  1143. struct inode *dir, struct ext4_filename *fname,
  1144. const struct qstr *d_name,
  1145. unsigned int offset, struct ext4_dir_entry_2 **res_dir)
  1146. {
  1147. struct ext4_dir_entry_2 * de;
  1148. char * dlimit;
  1149. int de_len;
  1150. int res;
  1151. de = (struct ext4_dir_entry_2 *)search_buf;
  1152. dlimit = search_buf + buf_size;
  1153. while ((char *) de < dlimit) {
  1154. /* this code is executed quadratically often */
  1155. /* do minimal checking `by hand' */
  1156. if ((char *) de + de->name_len <= dlimit) {
  1157. res = ext4_match(fname, de);
  1158. if (res < 0) {
  1159. res = -1;
  1160. goto return_result;
  1161. }
  1162. if (res > 0) {
  1163. /* found a match - just to be sure, do
  1164. * a full check */
  1165. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1166. bh->b_data,
  1167. bh->b_size, offset)) {
  1168. res = -1;
  1169. goto return_result;
  1170. }
  1171. *res_dir = de;
  1172. res = 1;
  1173. goto return_result;
  1174. }
  1175. }
  1176. /* prevent looping on a bad block */
  1177. de_len = ext4_rec_len_from_disk(de->rec_len,
  1178. dir->i_sb->s_blocksize);
  1179. if (de_len <= 0) {
  1180. res = -1;
  1181. goto return_result;
  1182. }
  1183. offset += de_len;
  1184. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  1185. }
  1186. res = 0;
  1187. return_result:
  1188. return res;
  1189. }
  1190. static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
  1191. struct ext4_dir_entry *de)
  1192. {
  1193. struct super_block *sb = dir->i_sb;
  1194. if (!is_dx(dir))
  1195. return 0;
  1196. if (block == 0)
  1197. return 1;
  1198. if (de->inode == 0 &&
  1199. ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
  1200. sb->s_blocksize)
  1201. return 1;
  1202. return 0;
  1203. }
  1204. /*
  1205. * ext4_find_entry()
  1206. *
  1207. * finds an entry in the specified directory with the wanted name. It
  1208. * returns the cache buffer in which the entry was found, and the entry
  1209. * itself (as a parameter - res_dir). It does NOT read the inode of the
  1210. * entry - you'll have to do that yourself if you want to.
  1211. *
  1212. * The returned buffer_head has ->b_count elevated. The caller is expected
  1213. * to brelse() it when appropriate.
  1214. */
  1215. static struct buffer_head * ext4_find_entry (struct inode *dir,
  1216. const struct qstr *d_name,
  1217. struct ext4_dir_entry_2 **res_dir,
  1218. int *inlined)
  1219. {
  1220. struct super_block *sb;
  1221. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1222. struct buffer_head *bh, *ret = NULL;
  1223. ext4_lblk_t start, block, b;
  1224. const u8 *name = d_name->name;
  1225. int ra_max = 0; /* Number of bh's in the readahead
  1226. buffer, bh_use[] */
  1227. int ra_ptr = 0; /* Current index into readahead
  1228. buffer */
  1229. int num = 0;
  1230. ext4_lblk_t nblocks;
  1231. int i, namelen, retval;
  1232. struct ext4_filename fname;
  1233. *res_dir = NULL;
  1234. sb = dir->i_sb;
  1235. namelen = d_name->len;
  1236. if (namelen > EXT4_NAME_LEN)
  1237. return NULL;
  1238. retval = ext4_fname_setup_filename(dir, d_name, 1, &fname);
  1239. if (retval)
  1240. return ERR_PTR(retval);
  1241. if (ext4_has_inline_data(dir)) {
  1242. int has_inline_data = 1;
  1243. ret = ext4_find_inline_entry(dir, &fname, d_name, res_dir,
  1244. &has_inline_data);
  1245. if (has_inline_data) {
  1246. if (inlined)
  1247. *inlined = 1;
  1248. goto cleanup_and_exit;
  1249. }
  1250. }
  1251. if ((namelen <= 2) && (name[0] == '.') &&
  1252. (name[1] == '.' || name[1] == '\0')) {
  1253. /*
  1254. * "." or ".." will only be in the first block
  1255. * NFS may look up ".."; "." should be handled by the VFS
  1256. */
  1257. block = start = 0;
  1258. nblocks = 1;
  1259. goto restart;
  1260. }
  1261. if (is_dx(dir)) {
  1262. ret = ext4_dx_find_entry(dir, &fname, res_dir);
  1263. /*
  1264. * On success, or if the error was file not found,
  1265. * return. Otherwise, fall back to doing a search the
  1266. * old fashioned way.
  1267. */
  1268. if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
  1269. goto cleanup_and_exit;
  1270. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  1271. "falling back\n"));
  1272. }
  1273. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1274. start = EXT4_I(dir)->i_dir_start_lookup;
  1275. if (start >= nblocks)
  1276. start = 0;
  1277. block = start;
  1278. restart:
  1279. do {
  1280. /*
  1281. * We deal with the read-ahead logic here.
  1282. */
  1283. if (ra_ptr >= ra_max) {
  1284. /* Refill the readahead buffer */
  1285. ra_ptr = 0;
  1286. b = block;
  1287. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  1288. /*
  1289. * Terminate if we reach the end of the
  1290. * directory and must wrap, or if our
  1291. * search has finished at this block.
  1292. */
  1293. if (b >= nblocks || (num && block == start)) {
  1294. bh_use[ra_max] = NULL;
  1295. break;
  1296. }
  1297. num++;
  1298. bh = ext4_getblk(NULL, dir, b++, 0);
  1299. if (IS_ERR(bh)) {
  1300. if (ra_max == 0) {
  1301. ret = bh;
  1302. goto cleanup_and_exit;
  1303. }
  1304. break;
  1305. }
  1306. bh_use[ra_max] = bh;
  1307. if (bh)
  1308. ll_rw_block(READ | REQ_META | REQ_PRIO,
  1309. 1, &bh);
  1310. }
  1311. }
  1312. if ((bh = bh_use[ra_ptr++]) == NULL)
  1313. goto next;
  1314. wait_on_buffer(bh);
  1315. if (!buffer_uptodate(bh)) {
  1316. /* read error, skip block & hope for the best */
  1317. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  1318. (unsigned long) block);
  1319. brelse(bh);
  1320. goto next;
  1321. }
  1322. if (!buffer_verified(bh) &&
  1323. !is_dx_internal_node(dir, block,
  1324. (struct ext4_dir_entry *)bh->b_data) &&
  1325. !ext4_dirent_csum_verify(dir,
  1326. (struct ext4_dir_entry *)bh->b_data)) {
  1327. EXT4_ERROR_INODE(dir, "checksumming directory "
  1328. "block %lu", (unsigned long)block);
  1329. brelse(bh);
  1330. goto next;
  1331. }
  1332. set_buffer_verified(bh);
  1333. i = search_dirblock(bh, dir, &fname, d_name,
  1334. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  1335. if (i == 1) {
  1336. EXT4_I(dir)->i_dir_start_lookup = block;
  1337. ret = bh;
  1338. goto cleanup_and_exit;
  1339. } else {
  1340. brelse(bh);
  1341. if (i < 0)
  1342. goto cleanup_and_exit;
  1343. }
  1344. next:
  1345. if (++block >= nblocks)
  1346. block = 0;
  1347. } while (block != start);
  1348. /*
  1349. * If the directory has grown while we were searching, then
  1350. * search the last part of the directory before giving up.
  1351. */
  1352. block = nblocks;
  1353. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1354. if (block < nblocks) {
  1355. start = 0;
  1356. goto restart;
  1357. }
  1358. cleanup_and_exit:
  1359. /* Clean up the read-ahead blocks */
  1360. for (; ra_ptr < ra_max; ra_ptr++)
  1361. brelse(bh_use[ra_ptr]);
  1362. ext4_fname_free_filename(&fname);
  1363. return ret;
  1364. }
  1365. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  1366. struct ext4_filename *fname,
  1367. struct ext4_dir_entry_2 **res_dir)
  1368. {
  1369. struct super_block * sb = dir->i_sb;
  1370. struct dx_frame frames[2], *frame;
  1371. const struct qstr *d_name = fname->usr_fname;
  1372. struct buffer_head *bh;
  1373. ext4_lblk_t block;
  1374. int retval;
  1375. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1376. *res_dir = NULL;
  1377. #endif
  1378. frame = dx_probe(fname, dir, NULL, frames);
  1379. if (IS_ERR(frame))
  1380. return (struct buffer_head *) frame;
  1381. do {
  1382. block = dx_get_block(frame->at);
  1383. bh = ext4_read_dirblock(dir, block, DIRENT);
  1384. if (IS_ERR(bh))
  1385. goto errout;
  1386. retval = search_dirblock(bh, dir, fname, d_name,
  1387. block << EXT4_BLOCK_SIZE_BITS(sb),
  1388. res_dir);
  1389. if (retval == 1)
  1390. goto success;
  1391. brelse(bh);
  1392. if (retval == -1) {
  1393. bh = ERR_PTR(ERR_BAD_DX_DIR);
  1394. goto errout;
  1395. }
  1396. /* Check to see if we should continue to search */
  1397. retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
  1398. frames, NULL);
  1399. if (retval < 0) {
  1400. ext4_warning_inode(dir,
  1401. "error %d reading directory index block",
  1402. retval);
  1403. bh = ERR_PTR(retval);
  1404. goto errout;
  1405. }
  1406. } while (retval == 1);
  1407. bh = NULL;
  1408. errout:
  1409. dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
  1410. success:
  1411. dx_release(frames);
  1412. return bh;
  1413. }
  1414. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1415. {
  1416. struct inode *inode;
  1417. struct ext4_dir_entry_2 *de;
  1418. struct buffer_head *bh;
  1419. if (dentry->d_name.len > EXT4_NAME_LEN)
  1420. return ERR_PTR(-ENAMETOOLONG);
  1421. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  1422. if (IS_ERR(bh))
  1423. return (struct dentry *) bh;
  1424. inode = NULL;
  1425. if (bh) {
  1426. __u32 ino = le32_to_cpu(de->inode);
  1427. brelse(bh);
  1428. if (!ext4_valid_inum(dir->i_sb, ino)) {
  1429. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  1430. return ERR_PTR(-EFSCORRUPTED);
  1431. }
  1432. if (unlikely(ino == dir->i_ino)) {
  1433. EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
  1434. dentry);
  1435. return ERR_PTR(-EFSCORRUPTED);
  1436. }
  1437. inode = ext4_iget_normal(dir->i_sb, ino);
  1438. if (inode == ERR_PTR(-ESTALE)) {
  1439. EXT4_ERROR_INODE(dir,
  1440. "deleted inode referenced: %u",
  1441. ino);
  1442. return ERR_PTR(-EFSCORRUPTED);
  1443. }
  1444. if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
  1445. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1446. S_ISLNK(inode->i_mode)) &&
  1447. !ext4_is_child_context_consistent_with_parent(dir,
  1448. inode)) {
  1449. iput(inode);
  1450. ext4_warning(inode->i_sb,
  1451. "Inconsistent encryption contexts: %lu/%lu\n",
  1452. (unsigned long) dir->i_ino,
  1453. (unsigned long) inode->i_ino);
  1454. return ERR_PTR(-EPERM);
  1455. }
  1456. }
  1457. return d_splice_alias(inode, dentry);
  1458. }
  1459. struct dentry *ext4_get_parent(struct dentry *child)
  1460. {
  1461. __u32 ino;
  1462. static const struct qstr dotdot = QSTR_INIT("..", 2);
  1463. struct ext4_dir_entry_2 * de;
  1464. struct buffer_head *bh;
  1465. bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
  1466. if (IS_ERR(bh))
  1467. return (struct dentry *) bh;
  1468. if (!bh)
  1469. return ERR_PTR(-ENOENT);
  1470. ino = le32_to_cpu(de->inode);
  1471. brelse(bh);
  1472. if (!ext4_valid_inum(d_inode(child)->i_sb, ino)) {
  1473. EXT4_ERROR_INODE(d_inode(child),
  1474. "bad parent inode number: %u", ino);
  1475. return ERR_PTR(-EFSCORRUPTED);
  1476. }
  1477. return d_obtain_alias(ext4_iget_normal(d_inode(child)->i_sb, ino));
  1478. }
  1479. /*
  1480. * Move count entries from end of map between two memory locations.
  1481. * Returns pointer to last entry moved.
  1482. */
  1483. static struct ext4_dir_entry_2 *
  1484. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1485. unsigned blocksize)
  1486. {
  1487. unsigned rec_len = 0;
  1488. while (count--) {
  1489. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1490. (from + (map->offs<<2));
  1491. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1492. memcpy (to, de, rec_len);
  1493. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1494. ext4_rec_len_to_disk(rec_len, blocksize);
  1495. de->inode = 0;
  1496. map++;
  1497. to += rec_len;
  1498. }
  1499. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1500. }
  1501. /*
  1502. * Compact each dir entry in the range to the minimal rec_len.
  1503. * Returns pointer to last entry in range.
  1504. */
  1505. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1506. {
  1507. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1508. unsigned rec_len = 0;
  1509. prev = to = de;
  1510. while ((char*)de < base + blocksize) {
  1511. next = ext4_next_entry(de, blocksize);
  1512. if (de->inode && de->name_len) {
  1513. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1514. if (de > to)
  1515. memmove(to, de, rec_len);
  1516. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1517. prev = to;
  1518. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1519. }
  1520. de = next;
  1521. }
  1522. return prev;
  1523. }
  1524. /*
  1525. * Split a full leaf block to make room for a new dir entry.
  1526. * Allocate a new block, and move entries so that they are approx. equally full.
  1527. * Returns pointer to de in block into which the new entry will be inserted.
  1528. */
  1529. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1530. struct buffer_head **bh,struct dx_frame *frame,
  1531. struct dx_hash_info *hinfo)
  1532. {
  1533. unsigned blocksize = dir->i_sb->s_blocksize;
  1534. unsigned count, continued;
  1535. struct buffer_head *bh2;
  1536. ext4_lblk_t newblock;
  1537. u32 hash2;
  1538. struct dx_map_entry *map;
  1539. char *data1 = (*bh)->b_data, *data2;
  1540. unsigned split, move, size;
  1541. struct ext4_dir_entry_2 *de = NULL, *de2;
  1542. struct ext4_dir_entry_tail *t;
  1543. int csum_size = 0;
  1544. int err = 0, i;
  1545. if (ext4_has_metadata_csum(dir->i_sb))
  1546. csum_size = sizeof(struct ext4_dir_entry_tail);
  1547. bh2 = ext4_append(handle, dir, &newblock);
  1548. if (IS_ERR(bh2)) {
  1549. brelse(*bh);
  1550. *bh = NULL;
  1551. return (struct ext4_dir_entry_2 *) bh2;
  1552. }
  1553. BUFFER_TRACE(*bh, "get_write_access");
  1554. err = ext4_journal_get_write_access(handle, *bh);
  1555. if (err)
  1556. goto journal_error;
  1557. BUFFER_TRACE(frame->bh, "get_write_access");
  1558. err = ext4_journal_get_write_access(handle, frame->bh);
  1559. if (err)
  1560. goto journal_error;
  1561. data2 = bh2->b_data;
  1562. /* create map in the end of data2 block */
  1563. map = (struct dx_map_entry *) (data2 + blocksize);
  1564. count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
  1565. blocksize, hinfo, map);
  1566. map -= count;
  1567. dx_sort_map(map, count);
  1568. /* Split the existing block in the middle, size-wise */
  1569. size = 0;
  1570. move = 0;
  1571. for (i = count-1; i >= 0; i--) {
  1572. /* is more than half of this entry in 2nd half of the block? */
  1573. if (size + map[i].size/2 > blocksize/2)
  1574. break;
  1575. size += map[i].size;
  1576. move++;
  1577. }
  1578. /* map index at which we will split */
  1579. split = count - move;
  1580. hash2 = map[split].hash;
  1581. continued = hash2 == map[split - 1].hash;
  1582. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1583. (unsigned long)dx_get_block(frame->at),
  1584. hash2, split, count-split));
  1585. /* Fancy dance to stay within two buffers */
  1586. de2 = dx_move_dirents(data1, data2, map + split, count - split,
  1587. blocksize);
  1588. de = dx_pack_dirents(data1, blocksize);
  1589. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1590. (char *) de,
  1591. blocksize);
  1592. de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  1593. (char *) de2,
  1594. blocksize);
  1595. if (csum_size) {
  1596. t = EXT4_DIRENT_TAIL(data2, blocksize);
  1597. initialize_dirent_tail(t, blocksize);
  1598. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1599. initialize_dirent_tail(t, blocksize);
  1600. }
  1601. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
  1602. blocksize, 1));
  1603. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
  1604. blocksize, 1));
  1605. /* Which block gets the new entry? */
  1606. if (hinfo->hash >= hash2) {
  1607. swap(*bh, bh2);
  1608. de = de2;
  1609. }
  1610. dx_insert_block(frame, hash2 + continued, newblock);
  1611. err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1612. if (err)
  1613. goto journal_error;
  1614. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1615. if (err)
  1616. goto journal_error;
  1617. brelse(bh2);
  1618. dxtrace(dx_show_index("frame", frame->entries));
  1619. return de;
  1620. journal_error:
  1621. brelse(*bh);
  1622. brelse(bh2);
  1623. *bh = NULL;
  1624. ext4_std_error(dir->i_sb, err);
  1625. return ERR_PTR(err);
  1626. }
  1627. int ext4_find_dest_de(struct inode *dir, struct inode *inode,
  1628. struct buffer_head *bh,
  1629. void *buf, int buf_size,
  1630. struct ext4_filename *fname,
  1631. struct ext4_dir_entry_2 **dest_de)
  1632. {
  1633. struct ext4_dir_entry_2 *de;
  1634. unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
  1635. int nlen, rlen;
  1636. unsigned int offset = 0;
  1637. char *top;
  1638. int res;
  1639. de = (struct ext4_dir_entry_2 *)buf;
  1640. top = buf + buf_size - reclen;
  1641. while ((char *) de <= top) {
  1642. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1643. buf, buf_size, offset)) {
  1644. res = -EFSCORRUPTED;
  1645. goto return_result;
  1646. }
  1647. /* Provide crypto context and crypto buffer to ext4 match */
  1648. res = ext4_match(fname, de);
  1649. if (res < 0)
  1650. goto return_result;
  1651. if (res > 0) {
  1652. res = -EEXIST;
  1653. goto return_result;
  1654. }
  1655. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1656. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1657. if ((de->inode ? rlen - nlen : rlen) >= reclen)
  1658. break;
  1659. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1660. offset += rlen;
  1661. }
  1662. if ((char *) de > top)
  1663. res = -ENOSPC;
  1664. else {
  1665. *dest_de = de;
  1666. res = 0;
  1667. }
  1668. return_result:
  1669. return res;
  1670. }
  1671. int ext4_insert_dentry(struct inode *dir,
  1672. struct inode *inode,
  1673. struct ext4_dir_entry_2 *de,
  1674. int buf_size,
  1675. struct ext4_filename *fname)
  1676. {
  1677. int nlen, rlen;
  1678. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1679. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1680. if (de->inode) {
  1681. struct ext4_dir_entry_2 *de1 =
  1682. (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1683. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
  1684. de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
  1685. de = de1;
  1686. }
  1687. de->file_type = EXT4_FT_UNKNOWN;
  1688. de->inode = cpu_to_le32(inode->i_ino);
  1689. ext4_set_de_type(inode->i_sb, de, inode->i_mode);
  1690. de->name_len = fname_len(fname);
  1691. memcpy(de->name, fname_name(fname), fname_len(fname));
  1692. return 0;
  1693. }
  1694. /*
  1695. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1696. * it points to a directory entry which is guaranteed to be large
  1697. * enough for new directory entry. If de is NULL, then
  1698. * add_dirent_to_buf will attempt search the directory block for
  1699. * space. It will return -ENOSPC if no space is available, and -EIO
  1700. * and -EEXIST if directory entry already exists.
  1701. */
  1702. static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
  1703. struct inode *dir,
  1704. struct inode *inode, struct ext4_dir_entry_2 *de,
  1705. struct buffer_head *bh)
  1706. {
  1707. unsigned int blocksize = dir->i_sb->s_blocksize;
  1708. int csum_size = 0;
  1709. int err;
  1710. if (ext4_has_metadata_csum(inode->i_sb))
  1711. csum_size = sizeof(struct ext4_dir_entry_tail);
  1712. if (!de) {
  1713. err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
  1714. blocksize - csum_size, fname, &de);
  1715. if (err)
  1716. return err;
  1717. }
  1718. BUFFER_TRACE(bh, "get_write_access");
  1719. err = ext4_journal_get_write_access(handle, bh);
  1720. if (err) {
  1721. ext4_std_error(dir->i_sb, err);
  1722. return err;
  1723. }
  1724. /* By now the buffer is marked for journaling. Due to crypto operations,
  1725. * the following function call may fail */
  1726. err = ext4_insert_dentry(dir, inode, de, blocksize, fname);
  1727. if (err < 0)
  1728. return err;
  1729. /*
  1730. * XXX shouldn't update any times until successful
  1731. * completion of syscall, but too many callers depend
  1732. * on this.
  1733. *
  1734. * XXX similarly, too many callers depend on
  1735. * ext4_new_inode() setting the times, but error
  1736. * recovery deletes the inode, so the worst that can
  1737. * happen is that the times are slightly out of date
  1738. * and/or different from the directory change time.
  1739. */
  1740. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1741. ext4_update_dx_flag(dir);
  1742. dir->i_version++;
  1743. ext4_mark_inode_dirty(handle, dir);
  1744. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1745. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1746. if (err)
  1747. ext4_std_error(dir->i_sb, err);
  1748. return 0;
  1749. }
  1750. /*
  1751. * This converts a one block unindexed directory to a 3 block indexed
  1752. * directory, and adds the dentry to the indexed directory.
  1753. */
  1754. static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
  1755. struct dentry *dentry,
  1756. struct inode *inode, struct buffer_head *bh)
  1757. {
  1758. struct inode *dir = d_inode(dentry->d_parent);
  1759. struct buffer_head *bh2;
  1760. struct dx_root *root;
  1761. struct dx_frame frames[2], *frame;
  1762. struct dx_entry *entries;
  1763. struct ext4_dir_entry_2 *de, *de2;
  1764. struct ext4_dir_entry_tail *t;
  1765. char *data1, *top;
  1766. unsigned len;
  1767. int retval;
  1768. unsigned blocksize;
  1769. ext4_lblk_t block;
  1770. struct fake_dirent *fde;
  1771. int csum_size = 0;
  1772. if (ext4_has_metadata_csum(inode->i_sb))
  1773. csum_size = sizeof(struct ext4_dir_entry_tail);
  1774. blocksize = dir->i_sb->s_blocksize;
  1775. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1776. BUFFER_TRACE(bh, "get_write_access");
  1777. retval = ext4_journal_get_write_access(handle, bh);
  1778. if (retval) {
  1779. ext4_std_error(dir->i_sb, retval);
  1780. brelse(bh);
  1781. return retval;
  1782. }
  1783. root = (struct dx_root *) bh->b_data;
  1784. /* The 0th block becomes the root, move the dirents out */
  1785. fde = &root->dotdot;
  1786. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1787. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1788. if ((char *) de >= (((char *) root) + blocksize)) {
  1789. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1790. brelse(bh);
  1791. return -EFSCORRUPTED;
  1792. }
  1793. len = ((char *) root) + (blocksize - csum_size) - (char *) de;
  1794. /* Allocate new block for the 0th block's dirents */
  1795. bh2 = ext4_append(handle, dir, &block);
  1796. if (IS_ERR(bh2)) {
  1797. brelse(bh);
  1798. return PTR_ERR(bh2);
  1799. }
  1800. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1801. data1 = bh2->b_data;
  1802. memcpy (data1, de, len);
  1803. de = (struct ext4_dir_entry_2 *) data1;
  1804. top = data1 + len;
  1805. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1806. de = de2;
  1807. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1808. (char *) de,
  1809. blocksize);
  1810. if (csum_size) {
  1811. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1812. initialize_dirent_tail(t, blocksize);
  1813. }
  1814. /* Initialize the root; the dot dirents already exist */
  1815. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1816. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1817. blocksize);
  1818. memset (&root->info, 0, sizeof(root->info));
  1819. root->info.info_length = sizeof(root->info);
  1820. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1821. entries = root->entries;
  1822. dx_set_block(entries, 1);
  1823. dx_set_count(entries, 1);
  1824. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1825. /* Initialize as for dx_probe */
  1826. fname->hinfo.hash_version = root->info.hash_version;
  1827. if (fname->hinfo.hash_version <= DX_HASH_TEA)
  1828. fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1829. fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1830. ext4fs_dirhash(fname_name(fname), fname_len(fname), &fname->hinfo);
  1831. memset(frames, 0, sizeof(frames));
  1832. frame = frames;
  1833. frame->entries = entries;
  1834. frame->at = entries;
  1835. frame->bh = bh;
  1836. bh = bh2;
  1837. retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1838. if (retval)
  1839. goto out_frames;
  1840. retval = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1841. if (retval)
  1842. goto out_frames;
  1843. de = do_split(handle,dir, &bh, frame, &fname->hinfo);
  1844. if (IS_ERR(de)) {
  1845. retval = PTR_ERR(de);
  1846. goto out_frames;
  1847. }
  1848. dx_release(frames);
  1849. retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  1850. brelse(bh);
  1851. return retval;
  1852. out_frames:
  1853. /*
  1854. * Even if the block split failed, we have to properly write
  1855. * out all the changes we did so far. Otherwise we can end up
  1856. * with corrupted filesystem.
  1857. */
  1858. ext4_mark_inode_dirty(handle, dir);
  1859. dx_release(frames);
  1860. return retval;
  1861. }
  1862. /*
  1863. * ext4_add_entry()
  1864. *
  1865. * adds a file entry to the specified directory, using the same
  1866. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1867. *
  1868. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1869. * may not sleep between calling this and putting something into
  1870. * the entry, as someone else might have used it while you slept.
  1871. */
  1872. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1873. struct inode *inode)
  1874. {
  1875. struct inode *dir = d_inode(dentry->d_parent);
  1876. struct buffer_head *bh = NULL;
  1877. struct ext4_dir_entry_2 *de;
  1878. struct ext4_dir_entry_tail *t;
  1879. struct super_block *sb;
  1880. struct ext4_filename fname;
  1881. int retval;
  1882. int dx_fallback=0;
  1883. unsigned blocksize;
  1884. ext4_lblk_t block, blocks;
  1885. int csum_size = 0;
  1886. if (ext4_has_metadata_csum(inode->i_sb))
  1887. csum_size = sizeof(struct ext4_dir_entry_tail);
  1888. sb = dir->i_sb;
  1889. blocksize = sb->s_blocksize;
  1890. if (!dentry->d_name.len)
  1891. return -EINVAL;
  1892. retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
  1893. if (retval)
  1894. return retval;
  1895. if (ext4_has_inline_data(dir)) {
  1896. retval = ext4_try_add_inline_entry(handle, &fname,
  1897. dentry, inode);
  1898. if (retval < 0)
  1899. goto out;
  1900. if (retval == 1) {
  1901. retval = 0;
  1902. goto out;
  1903. }
  1904. }
  1905. if (is_dx(dir)) {
  1906. retval = ext4_dx_add_entry(handle, &fname, dentry, inode);
  1907. if (!retval || (retval != ERR_BAD_DX_DIR))
  1908. goto out;
  1909. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1910. dx_fallback++;
  1911. ext4_mark_inode_dirty(handle, dir);
  1912. }
  1913. blocks = dir->i_size >> sb->s_blocksize_bits;
  1914. for (block = 0; block < blocks; block++) {
  1915. bh = ext4_read_dirblock(dir, block, DIRENT);
  1916. if (IS_ERR(bh)) {
  1917. retval = PTR_ERR(bh);
  1918. bh = NULL;
  1919. goto out;
  1920. }
  1921. retval = add_dirent_to_buf(handle, &fname, dir, inode,
  1922. NULL, bh);
  1923. if (retval != -ENOSPC)
  1924. goto out;
  1925. if (blocks == 1 && !dx_fallback &&
  1926. ext4_has_feature_dir_index(sb)) {
  1927. retval = make_indexed_dir(handle, &fname, dentry,
  1928. inode, bh);
  1929. bh = NULL; /* make_indexed_dir releases bh */
  1930. goto out;
  1931. }
  1932. brelse(bh);
  1933. }
  1934. bh = ext4_append(handle, dir, &block);
  1935. if (IS_ERR(bh)) {
  1936. retval = PTR_ERR(bh);
  1937. bh = NULL;
  1938. goto out;
  1939. }
  1940. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1941. de->inode = 0;
  1942. de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
  1943. if (csum_size) {
  1944. t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
  1945. initialize_dirent_tail(t, blocksize);
  1946. }
  1947. retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
  1948. out:
  1949. ext4_fname_free_filename(&fname);
  1950. brelse(bh);
  1951. if (retval == 0)
  1952. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1953. return retval;
  1954. }
  1955. /*
  1956. * Returns 0 for success, or a negative error value
  1957. */
  1958. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  1959. struct dentry *dentry, struct inode *inode)
  1960. {
  1961. struct dx_frame frames[2], *frame;
  1962. struct dx_entry *entries, *at;
  1963. struct buffer_head *bh;
  1964. struct inode *dir = d_inode(dentry->d_parent);
  1965. struct super_block *sb = dir->i_sb;
  1966. struct ext4_dir_entry_2 *de;
  1967. int err;
  1968. frame = dx_probe(fname, dir, NULL, frames);
  1969. if (IS_ERR(frame))
  1970. return PTR_ERR(frame);
  1971. entries = frame->entries;
  1972. at = frame->at;
  1973. bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
  1974. if (IS_ERR(bh)) {
  1975. err = PTR_ERR(bh);
  1976. bh = NULL;
  1977. goto cleanup;
  1978. }
  1979. BUFFER_TRACE(bh, "get_write_access");
  1980. err = ext4_journal_get_write_access(handle, bh);
  1981. if (err)
  1982. goto journal_error;
  1983. err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
  1984. if (err != -ENOSPC)
  1985. goto cleanup;
  1986. /* Block full, should compress but for now just split */
  1987. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1988. dx_get_count(entries), dx_get_limit(entries)));
  1989. /* Need to split index? */
  1990. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1991. ext4_lblk_t newblock;
  1992. unsigned icount = dx_get_count(entries);
  1993. int levels = frame - frames;
  1994. struct dx_entry *entries2;
  1995. struct dx_node *node2;
  1996. struct buffer_head *bh2;
  1997. if (levels && (dx_get_count(frames->entries) ==
  1998. dx_get_limit(frames->entries))) {
  1999. ext4_warning_inode(dir, "Directory index full!");
  2000. err = -ENOSPC;
  2001. goto cleanup;
  2002. }
  2003. bh2 = ext4_append(handle, dir, &newblock);
  2004. if (IS_ERR(bh2)) {
  2005. err = PTR_ERR(bh2);
  2006. goto cleanup;
  2007. }
  2008. node2 = (struct dx_node *)(bh2->b_data);
  2009. entries2 = node2->entries;
  2010. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  2011. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  2012. sb->s_blocksize);
  2013. BUFFER_TRACE(frame->bh, "get_write_access");
  2014. err = ext4_journal_get_write_access(handle, frame->bh);
  2015. if (err)
  2016. goto journal_error;
  2017. if (levels) {
  2018. unsigned icount1 = icount/2, icount2 = icount - icount1;
  2019. unsigned hash2 = dx_get_hash(entries + icount1);
  2020. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  2021. icount1, icount2));
  2022. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  2023. err = ext4_journal_get_write_access(handle,
  2024. frames[0].bh);
  2025. if (err)
  2026. goto journal_error;
  2027. memcpy((char *) entries2, (char *) (entries + icount1),
  2028. icount2 * sizeof(struct dx_entry));
  2029. dx_set_count(entries, icount1);
  2030. dx_set_count(entries2, icount2);
  2031. dx_set_limit(entries2, dx_node_limit(dir));
  2032. /* Which index block gets the new entry? */
  2033. if (at - entries >= icount1) {
  2034. frame->at = at = at - entries - icount1 + entries2;
  2035. frame->entries = entries = entries2;
  2036. swap(frame->bh, bh2);
  2037. }
  2038. dx_insert_block(frames + 0, hash2, newblock);
  2039. dxtrace(dx_show_index("node", frames[1].entries));
  2040. dxtrace(dx_show_index("node",
  2041. ((struct dx_node *) bh2->b_data)->entries));
  2042. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2043. if (err)
  2044. goto journal_error;
  2045. brelse (bh2);
  2046. } else {
  2047. dxtrace(printk(KERN_DEBUG
  2048. "Creating second level index...\n"));
  2049. memcpy((char *) entries2, (char *) entries,
  2050. icount * sizeof(struct dx_entry));
  2051. dx_set_limit(entries2, dx_node_limit(dir));
  2052. /* Set up root */
  2053. dx_set_count(entries, 1);
  2054. dx_set_block(entries + 0, newblock);
  2055. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  2056. /* Add new access path frame */
  2057. frame = frames + 1;
  2058. frame->at = at = at - entries + entries2;
  2059. frame->entries = entries = entries2;
  2060. frame->bh = bh2;
  2061. err = ext4_journal_get_write_access(handle,
  2062. frame->bh);
  2063. if (err)
  2064. goto journal_error;
  2065. }
  2066. err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
  2067. if (err) {
  2068. ext4_std_error(inode->i_sb, err);
  2069. goto cleanup;
  2070. }
  2071. }
  2072. de = do_split(handle, dir, &bh, frame, &fname->hinfo);
  2073. if (IS_ERR(de)) {
  2074. err = PTR_ERR(de);
  2075. goto cleanup;
  2076. }
  2077. err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  2078. goto cleanup;
  2079. journal_error:
  2080. ext4_std_error(dir->i_sb, err);
  2081. cleanup:
  2082. brelse(bh);
  2083. dx_release(frames);
  2084. return err;
  2085. }
  2086. /*
  2087. * ext4_generic_delete_entry deletes a directory entry by merging it
  2088. * with the previous entry
  2089. */
  2090. int ext4_generic_delete_entry(handle_t *handle,
  2091. struct inode *dir,
  2092. struct ext4_dir_entry_2 *de_del,
  2093. struct buffer_head *bh,
  2094. void *entry_buf,
  2095. int buf_size,
  2096. int csum_size)
  2097. {
  2098. struct ext4_dir_entry_2 *de, *pde;
  2099. unsigned int blocksize = dir->i_sb->s_blocksize;
  2100. int i;
  2101. i = 0;
  2102. pde = NULL;
  2103. de = (struct ext4_dir_entry_2 *)entry_buf;
  2104. while (i < buf_size - csum_size) {
  2105. if (ext4_check_dir_entry(dir, NULL, de, bh,
  2106. bh->b_data, bh->b_size, i))
  2107. return -EFSCORRUPTED;
  2108. if (de == de_del) {
  2109. if (pde)
  2110. pde->rec_len = ext4_rec_len_to_disk(
  2111. ext4_rec_len_from_disk(pde->rec_len,
  2112. blocksize) +
  2113. ext4_rec_len_from_disk(de->rec_len,
  2114. blocksize),
  2115. blocksize);
  2116. else
  2117. de->inode = 0;
  2118. dir->i_version++;
  2119. return 0;
  2120. }
  2121. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  2122. pde = de;
  2123. de = ext4_next_entry(de, blocksize);
  2124. }
  2125. return -ENOENT;
  2126. }
  2127. static int ext4_delete_entry(handle_t *handle,
  2128. struct inode *dir,
  2129. struct ext4_dir_entry_2 *de_del,
  2130. struct buffer_head *bh)
  2131. {
  2132. int err, csum_size = 0;
  2133. if (ext4_has_inline_data(dir)) {
  2134. int has_inline_data = 1;
  2135. err = ext4_delete_inline_entry(handle, dir, de_del, bh,
  2136. &has_inline_data);
  2137. if (has_inline_data)
  2138. return err;
  2139. }
  2140. if (ext4_has_metadata_csum(dir->i_sb))
  2141. csum_size = sizeof(struct ext4_dir_entry_tail);
  2142. BUFFER_TRACE(bh, "get_write_access");
  2143. err = ext4_journal_get_write_access(handle, bh);
  2144. if (unlikely(err))
  2145. goto out;
  2146. err = ext4_generic_delete_entry(handle, dir, de_del,
  2147. bh, bh->b_data,
  2148. dir->i_sb->s_blocksize, csum_size);
  2149. if (err)
  2150. goto out;
  2151. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  2152. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  2153. if (unlikely(err))
  2154. goto out;
  2155. return 0;
  2156. out:
  2157. if (err != -ENOENT)
  2158. ext4_std_error(dir->i_sb, err);
  2159. return err;
  2160. }
  2161. /*
  2162. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  2163. * since this indicates that nlinks count was previously 1.
  2164. */
  2165. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  2166. {
  2167. inc_nlink(inode);
  2168. if (is_dx(inode) && inode->i_nlink > 1) {
  2169. /* limit is 16-bit i_links_count */
  2170. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  2171. set_nlink(inode, 1);
  2172. ext4_set_feature_dir_nlink(inode->i_sb);
  2173. }
  2174. }
  2175. }
  2176. /*
  2177. * If a directory had nlink == 1, then we should let it be 1. This indicates
  2178. * directory has >EXT4_LINK_MAX subdirs.
  2179. */
  2180. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  2181. {
  2182. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  2183. drop_nlink(inode);
  2184. }
  2185. static int ext4_add_nondir(handle_t *handle,
  2186. struct dentry *dentry, struct inode *inode)
  2187. {
  2188. int err = ext4_add_entry(handle, dentry, inode);
  2189. if (!err) {
  2190. ext4_mark_inode_dirty(handle, inode);
  2191. unlock_new_inode(inode);
  2192. d_instantiate(dentry, inode);
  2193. return 0;
  2194. }
  2195. drop_nlink(inode);
  2196. unlock_new_inode(inode);
  2197. iput(inode);
  2198. return err;
  2199. }
  2200. /*
  2201. * By the time this is called, we already have created
  2202. * the directory cache entry for the new file, but it
  2203. * is so far negative - it has no inode.
  2204. *
  2205. * If the create succeeds, we fill in the inode information
  2206. * with d_instantiate().
  2207. */
  2208. static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2209. bool excl)
  2210. {
  2211. handle_t *handle;
  2212. struct inode *inode;
  2213. int err, credits, retries = 0;
  2214. err = dquot_initialize(dir);
  2215. if (err)
  2216. return err;
  2217. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2218. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2219. retry:
  2220. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2221. NULL, EXT4_HT_DIR, credits);
  2222. handle = ext4_journal_current_handle();
  2223. err = PTR_ERR(inode);
  2224. if (!IS_ERR(inode)) {
  2225. inode->i_op = &ext4_file_inode_operations;
  2226. inode->i_fop = &ext4_file_operations;
  2227. ext4_set_aops(inode);
  2228. err = ext4_add_nondir(handle, dentry, inode);
  2229. if (!err && IS_DIRSYNC(dir))
  2230. ext4_handle_sync(handle);
  2231. }
  2232. if (handle)
  2233. ext4_journal_stop(handle);
  2234. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2235. goto retry;
  2236. return err;
  2237. }
  2238. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  2239. umode_t mode, dev_t rdev)
  2240. {
  2241. handle_t *handle;
  2242. struct inode *inode;
  2243. int err, credits, retries = 0;
  2244. err = dquot_initialize(dir);
  2245. if (err)
  2246. return err;
  2247. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2248. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2249. retry:
  2250. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2251. NULL, EXT4_HT_DIR, credits);
  2252. handle = ext4_journal_current_handle();
  2253. err = PTR_ERR(inode);
  2254. if (!IS_ERR(inode)) {
  2255. init_special_inode(inode, inode->i_mode, rdev);
  2256. inode->i_op = &ext4_special_inode_operations;
  2257. err = ext4_add_nondir(handle, dentry, inode);
  2258. if (!err && IS_DIRSYNC(dir))
  2259. ext4_handle_sync(handle);
  2260. }
  2261. if (handle)
  2262. ext4_journal_stop(handle);
  2263. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2264. goto retry;
  2265. return err;
  2266. }
  2267. static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  2268. {
  2269. handle_t *handle;
  2270. struct inode *inode;
  2271. int err, retries = 0;
  2272. err = dquot_initialize(dir);
  2273. if (err)
  2274. return err;
  2275. retry:
  2276. inode = ext4_new_inode_start_handle(dir, mode,
  2277. NULL, 0, NULL,
  2278. EXT4_HT_DIR,
  2279. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2280. 4 + EXT4_XATTR_TRANS_BLOCKS);
  2281. handle = ext4_journal_current_handle();
  2282. err = PTR_ERR(inode);
  2283. if (!IS_ERR(inode)) {
  2284. inode->i_op = &ext4_file_inode_operations;
  2285. inode->i_fop = &ext4_file_operations;
  2286. ext4_set_aops(inode);
  2287. d_tmpfile(dentry, inode);
  2288. err = ext4_orphan_add(handle, inode);
  2289. if (err)
  2290. goto err_unlock_inode;
  2291. mark_inode_dirty(inode);
  2292. unlock_new_inode(inode);
  2293. }
  2294. if (handle)
  2295. ext4_journal_stop(handle);
  2296. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2297. goto retry;
  2298. return err;
  2299. err_unlock_inode:
  2300. ext4_journal_stop(handle);
  2301. unlock_new_inode(inode);
  2302. return err;
  2303. }
  2304. struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
  2305. struct ext4_dir_entry_2 *de,
  2306. int blocksize, int csum_size,
  2307. unsigned int parent_ino, int dotdot_real_len)
  2308. {
  2309. de->inode = cpu_to_le32(inode->i_ino);
  2310. de->name_len = 1;
  2311. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  2312. blocksize);
  2313. strcpy(de->name, ".");
  2314. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2315. de = ext4_next_entry(de, blocksize);
  2316. de->inode = cpu_to_le32(parent_ino);
  2317. de->name_len = 2;
  2318. if (!dotdot_real_len)
  2319. de->rec_len = ext4_rec_len_to_disk(blocksize -
  2320. (csum_size + EXT4_DIR_REC_LEN(1)),
  2321. blocksize);
  2322. else
  2323. de->rec_len = ext4_rec_len_to_disk(
  2324. EXT4_DIR_REC_LEN(de->name_len), blocksize);
  2325. strcpy(de->name, "..");
  2326. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2327. return ext4_next_entry(de, blocksize);
  2328. }
  2329. static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2330. struct inode *inode)
  2331. {
  2332. struct buffer_head *dir_block = NULL;
  2333. struct ext4_dir_entry_2 *de;
  2334. struct ext4_dir_entry_tail *t;
  2335. ext4_lblk_t block = 0;
  2336. unsigned int blocksize = dir->i_sb->s_blocksize;
  2337. int csum_size = 0;
  2338. int err;
  2339. if (ext4_has_metadata_csum(dir->i_sb))
  2340. csum_size = sizeof(struct ext4_dir_entry_tail);
  2341. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2342. err = ext4_try_create_inline_dir(handle, dir, inode);
  2343. if (err < 0 && err != -ENOSPC)
  2344. goto out;
  2345. if (!err)
  2346. goto out;
  2347. }
  2348. inode->i_size = 0;
  2349. dir_block = ext4_append(handle, inode, &block);
  2350. if (IS_ERR(dir_block))
  2351. return PTR_ERR(dir_block);
  2352. de = (struct ext4_dir_entry_2 *)dir_block->b_data;
  2353. ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
  2354. set_nlink(inode, 2);
  2355. if (csum_size) {
  2356. t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
  2357. initialize_dirent_tail(t, blocksize);
  2358. }
  2359. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  2360. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  2361. if (err)
  2362. goto out;
  2363. set_buffer_verified(dir_block);
  2364. out:
  2365. brelse(dir_block);
  2366. return err;
  2367. }
  2368. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2369. {
  2370. handle_t *handle;
  2371. struct inode *inode;
  2372. int err, credits, retries = 0;
  2373. if (EXT4_DIR_LINK_MAX(dir))
  2374. return -EMLINK;
  2375. err = dquot_initialize(dir);
  2376. if (err)
  2377. return err;
  2378. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2379. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2380. retry:
  2381. inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
  2382. &dentry->d_name,
  2383. 0, NULL, EXT4_HT_DIR, credits);
  2384. handle = ext4_journal_current_handle();
  2385. err = PTR_ERR(inode);
  2386. if (IS_ERR(inode))
  2387. goto out_stop;
  2388. inode->i_op = &ext4_dir_inode_operations;
  2389. inode->i_fop = &ext4_dir_operations;
  2390. err = ext4_init_new_dir(handle, dir, inode);
  2391. if (err)
  2392. goto out_clear_inode;
  2393. err = ext4_mark_inode_dirty(handle, inode);
  2394. if (!err)
  2395. err = ext4_add_entry(handle, dentry, inode);
  2396. if (err) {
  2397. out_clear_inode:
  2398. clear_nlink(inode);
  2399. unlock_new_inode(inode);
  2400. ext4_mark_inode_dirty(handle, inode);
  2401. iput(inode);
  2402. goto out_stop;
  2403. }
  2404. ext4_inc_count(handle, dir);
  2405. ext4_update_dx_flag(dir);
  2406. err = ext4_mark_inode_dirty(handle, dir);
  2407. if (err)
  2408. goto out_clear_inode;
  2409. unlock_new_inode(inode);
  2410. d_instantiate(dentry, inode);
  2411. if (IS_DIRSYNC(dir))
  2412. ext4_handle_sync(handle);
  2413. out_stop:
  2414. if (handle)
  2415. ext4_journal_stop(handle);
  2416. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2417. goto retry;
  2418. return err;
  2419. }
  2420. /*
  2421. * routine to check that the specified directory is empty (for rmdir)
  2422. */
  2423. int ext4_empty_dir(struct inode *inode)
  2424. {
  2425. unsigned int offset;
  2426. struct buffer_head *bh;
  2427. struct ext4_dir_entry_2 *de, *de1;
  2428. struct super_block *sb;
  2429. int err = 0;
  2430. if (ext4_has_inline_data(inode)) {
  2431. int has_inline_data = 1;
  2432. err = empty_inline_dir(inode, &has_inline_data);
  2433. if (has_inline_data)
  2434. return err;
  2435. }
  2436. sb = inode->i_sb;
  2437. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
  2438. EXT4_ERROR_INODE(inode, "invalid size");
  2439. return 1;
  2440. }
  2441. bh = ext4_read_dirblock(inode, 0, EITHER);
  2442. if (IS_ERR(bh))
  2443. return 1;
  2444. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2445. de1 = ext4_next_entry(de, sb->s_blocksize);
  2446. if (le32_to_cpu(de->inode) != inode->i_ino ||
  2447. le32_to_cpu(de1->inode) == 0 ||
  2448. strcmp(".", de->name) || strcmp("..", de1->name)) {
  2449. ext4_warning_inode(inode, "directory missing '.' and/or '..'");
  2450. brelse(bh);
  2451. return 1;
  2452. }
  2453. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  2454. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  2455. de = ext4_next_entry(de1, sb->s_blocksize);
  2456. while (offset < inode->i_size) {
  2457. if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  2458. unsigned int lblock;
  2459. err = 0;
  2460. brelse(bh);
  2461. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  2462. bh = ext4_read_dirblock(inode, lblock, EITHER);
  2463. if (IS_ERR(bh))
  2464. return 1;
  2465. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2466. }
  2467. if (ext4_check_dir_entry(inode, NULL, de, bh,
  2468. bh->b_data, bh->b_size, offset)) {
  2469. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  2470. sb->s_blocksize);
  2471. offset = (offset | (sb->s_blocksize - 1)) + 1;
  2472. continue;
  2473. }
  2474. if (le32_to_cpu(de->inode)) {
  2475. brelse(bh);
  2476. return 0;
  2477. }
  2478. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2479. de = ext4_next_entry(de, sb->s_blocksize);
  2480. }
  2481. brelse(bh);
  2482. return 1;
  2483. }
  2484. /*
  2485. * ext4_orphan_add() links an unlinked or truncated inode into a list of
  2486. * such inodes, starting at the superblock, in case we crash before the
  2487. * file is closed/deleted, or in case the inode truncate spans multiple
  2488. * transactions and the last transaction is not recovered after a crash.
  2489. *
  2490. * At filesystem recovery time, we walk this list deleting unlinked
  2491. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  2492. *
  2493. * Orphan list manipulation functions must be called under i_mutex unless
  2494. * we are just creating the inode or deleting it.
  2495. */
  2496. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  2497. {
  2498. struct super_block *sb = inode->i_sb;
  2499. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2500. struct ext4_iloc iloc;
  2501. int err = 0, rc;
  2502. bool dirty = false;
  2503. if (!sbi->s_journal || is_bad_inode(inode))
  2504. return 0;
  2505. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2506. !mutex_is_locked(&inode->i_mutex));
  2507. /*
  2508. * Exit early if inode already is on orphan list. This is a big speedup
  2509. * since we don't have to contend on the global s_orphan_lock.
  2510. */
  2511. if (!list_empty(&EXT4_I(inode)->i_orphan))
  2512. return 0;
  2513. /*
  2514. * Orphan handling is only valid for files with data blocks
  2515. * being truncated, or files being unlinked. Note that we either
  2516. * hold i_mutex, or the inode can not be referenced from outside,
  2517. * so i_nlink should not be bumped due to race
  2518. */
  2519. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  2520. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  2521. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2522. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2523. if (err)
  2524. goto out;
  2525. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2526. if (err)
  2527. goto out;
  2528. mutex_lock(&sbi->s_orphan_lock);
  2529. /*
  2530. * Due to previous errors inode may be already a part of on-disk
  2531. * orphan list. If so skip on-disk list modification.
  2532. */
  2533. if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
  2534. (le32_to_cpu(sbi->s_es->s_inodes_count))) {
  2535. /* Insert this inode at the head of the on-disk orphan list */
  2536. NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
  2537. sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  2538. dirty = true;
  2539. }
  2540. list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
  2541. mutex_unlock(&sbi->s_orphan_lock);
  2542. if (dirty) {
  2543. err = ext4_handle_dirty_super(handle, sb);
  2544. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2545. if (!err)
  2546. err = rc;
  2547. if (err) {
  2548. /*
  2549. * We have to remove inode from in-memory list if
  2550. * addition to on disk orphan list failed. Stray orphan
  2551. * list entries can cause panics at unmount time.
  2552. */
  2553. mutex_lock(&sbi->s_orphan_lock);
  2554. list_del(&EXT4_I(inode)->i_orphan);
  2555. mutex_unlock(&sbi->s_orphan_lock);
  2556. }
  2557. }
  2558. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  2559. jbd_debug(4, "orphan inode %lu will point to %d\n",
  2560. inode->i_ino, NEXT_ORPHAN(inode));
  2561. out:
  2562. ext4_std_error(sb, err);
  2563. return err;
  2564. }
  2565. /*
  2566. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  2567. * of such inodes stored on disk, because it is finally being cleaned up.
  2568. */
  2569. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  2570. {
  2571. struct list_head *prev;
  2572. struct ext4_inode_info *ei = EXT4_I(inode);
  2573. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2574. __u32 ino_next;
  2575. struct ext4_iloc iloc;
  2576. int err = 0;
  2577. if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
  2578. return 0;
  2579. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2580. !mutex_is_locked(&inode->i_mutex));
  2581. /* Do this quick check before taking global s_orphan_lock. */
  2582. if (list_empty(&ei->i_orphan))
  2583. return 0;
  2584. if (handle) {
  2585. /* Grab inode buffer early before taking global s_orphan_lock */
  2586. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2587. }
  2588. mutex_lock(&sbi->s_orphan_lock);
  2589. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  2590. prev = ei->i_orphan.prev;
  2591. list_del_init(&ei->i_orphan);
  2592. /* If we're on an error path, we may not have a valid
  2593. * transaction handle with which to update the orphan list on
  2594. * disk, but we still need to remove the inode from the linked
  2595. * list in memory. */
  2596. if (!handle || err) {
  2597. mutex_unlock(&sbi->s_orphan_lock);
  2598. goto out_err;
  2599. }
  2600. ino_next = NEXT_ORPHAN(inode);
  2601. if (prev == &sbi->s_orphan) {
  2602. jbd_debug(4, "superblock will point to %u\n", ino_next);
  2603. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2604. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2605. if (err) {
  2606. mutex_unlock(&sbi->s_orphan_lock);
  2607. goto out_brelse;
  2608. }
  2609. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  2610. mutex_unlock(&sbi->s_orphan_lock);
  2611. err = ext4_handle_dirty_super(handle, inode->i_sb);
  2612. } else {
  2613. struct ext4_iloc iloc2;
  2614. struct inode *i_prev =
  2615. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  2616. jbd_debug(4, "orphan inode %lu will point to %u\n",
  2617. i_prev->i_ino, ino_next);
  2618. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  2619. if (err) {
  2620. mutex_unlock(&sbi->s_orphan_lock);
  2621. goto out_brelse;
  2622. }
  2623. NEXT_ORPHAN(i_prev) = ino_next;
  2624. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  2625. mutex_unlock(&sbi->s_orphan_lock);
  2626. }
  2627. if (err)
  2628. goto out_brelse;
  2629. NEXT_ORPHAN(inode) = 0;
  2630. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2631. out_err:
  2632. ext4_std_error(inode->i_sb, err);
  2633. return err;
  2634. out_brelse:
  2635. brelse(iloc.bh);
  2636. goto out_err;
  2637. }
  2638. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  2639. {
  2640. int retval;
  2641. struct inode *inode;
  2642. struct buffer_head *bh;
  2643. struct ext4_dir_entry_2 *de;
  2644. handle_t *handle = NULL;
  2645. /* Initialize quotas before so that eventual writes go in
  2646. * separate transaction */
  2647. retval = dquot_initialize(dir);
  2648. if (retval)
  2649. return retval;
  2650. retval = dquot_initialize(d_inode(dentry));
  2651. if (retval)
  2652. return retval;
  2653. retval = -ENOENT;
  2654. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2655. if (IS_ERR(bh))
  2656. return PTR_ERR(bh);
  2657. if (!bh)
  2658. goto end_rmdir;
  2659. inode = d_inode(dentry);
  2660. retval = -EFSCORRUPTED;
  2661. if (le32_to_cpu(de->inode) != inode->i_ino)
  2662. goto end_rmdir;
  2663. retval = -ENOTEMPTY;
  2664. if (!ext4_empty_dir(inode))
  2665. goto end_rmdir;
  2666. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2667. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2668. if (IS_ERR(handle)) {
  2669. retval = PTR_ERR(handle);
  2670. handle = NULL;
  2671. goto end_rmdir;
  2672. }
  2673. if (IS_DIRSYNC(dir))
  2674. ext4_handle_sync(handle);
  2675. retval = ext4_delete_entry(handle, dir, de, bh);
  2676. if (retval)
  2677. goto end_rmdir;
  2678. if (!EXT4_DIR_LINK_EMPTY(inode))
  2679. ext4_warning_inode(inode,
  2680. "empty directory '%.*s' has too many links (%u)",
  2681. dentry->d_name.len, dentry->d_name.name,
  2682. inode->i_nlink);
  2683. inode->i_version++;
  2684. clear_nlink(inode);
  2685. /* There's no need to set i_disksize: the fact that i_nlink is
  2686. * zero will ensure that the right thing happens during any
  2687. * recovery. */
  2688. inode->i_size = 0;
  2689. ext4_orphan_add(handle, inode);
  2690. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  2691. ext4_mark_inode_dirty(handle, inode);
  2692. ext4_dec_count(handle, dir);
  2693. ext4_update_dx_flag(dir);
  2694. ext4_mark_inode_dirty(handle, dir);
  2695. end_rmdir:
  2696. brelse(bh);
  2697. if (handle)
  2698. ext4_journal_stop(handle);
  2699. return retval;
  2700. }
  2701. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2702. {
  2703. int retval;
  2704. struct inode *inode;
  2705. struct buffer_head *bh;
  2706. struct ext4_dir_entry_2 *de;
  2707. handle_t *handle = NULL;
  2708. trace_ext4_unlink_enter(dir, dentry);
  2709. /* Initialize quotas before so that eventual writes go
  2710. * in separate transaction */
  2711. retval = dquot_initialize(dir);
  2712. if (retval)
  2713. return retval;
  2714. retval = dquot_initialize(d_inode(dentry));
  2715. if (retval)
  2716. return retval;
  2717. retval = -ENOENT;
  2718. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2719. if (IS_ERR(bh))
  2720. return PTR_ERR(bh);
  2721. if (!bh)
  2722. goto end_unlink;
  2723. inode = d_inode(dentry);
  2724. retval = -EFSCORRUPTED;
  2725. if (le32_to_cpu(de->inode) != inode->i_ino)
  2726. goto end_unlink;
  2727. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2728. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2729. if (IS_ERR(handle)) {
  2730. retval = PTR_ERR(handle);
  2731. handle = NULL;
  2732. goto end_unlink;
  2733. }
  2734. if (IS_DIRSYNC(dir))
  2735. ext4_handle_sync(handle);
  2736. if (inode->i_nlink == 0) {
  2737. ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
  2738. dentry->d_name.len, dentry->d_name.name);
  2739. set_nlink(inode, 1);
  2740. }
  2741. retval = ext4_delete_entry(handle, dir, de, bh);
  2742. if (retval)
  2743. goto end_unlink;
  2744. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  2745. ext4_update_dx_flag(dir);
  2746. ext4_mark_inode_dirty(handle, dir);
  2747. drop_nlink(inode);
  2748. if (!inode->i_nlink)
  2749. ext4_orphan_add(handle, inode);
  2750. inode->i_ctime = ext4_current_time(inode);
  2751. ext4_mark_inode_dirty(handle, inode);
  2752. end_unlink:
  2753. brelse(bh);
  2754. if (handle)
  2755. ext4_journal_stop(handle);
  2756. trace_ext4_unlink_exit(dentry, retval);
  2757. return retval;
  2758. }
  2759. static int ext4_symlink(struct inode *dir,
  2760. struct dentry *dentry, const char *symname)
  2761. {
  2762. handle_t *handle;
  2763. struct inode *inode;
  2764. int err, len = strlen(symname);
  2765. int credits;
  2766. bool encryption_required;
  2767. struct ext4_str disk_link;
  2768. struct ext4_encrypted_symlink_data *sd = NULL;
  2769. disk_link.len = len + 1;
  2770. disk_link.name = (char *) symname;
  2771. encryption_required = (ext4_encrypted_inode(dir) ||
  2772. DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
  2773. if (encryption_required) {
  2774. err = ext4_get_encryption_info(dir);
  2775. if (err)
  2776. return err;
  2777. if (ext4_encryption_info(dir) == NULL)
  2778. return -EPERM;
  2779. disk_link.len = (ext4_fname_encrypted_size(dir, len) +
  2780. sizeof(struct ext4_encrypted_symlink_data));
  2781. sd = kzalloc(disk_link.len, GFP_KERNEL);
  2782. if (!sd)
  2783. return -ENOMEM;
  2784. }
  2785. if (disk_link.len > dir->i_sb->s_blocksize) {
  2786. err = -ENAMETOOLONG;
  2787. goto err_free_sd;
  2788. }
  2789. err = dquot_initialize(dir);
  2790. if (err)
  2791. goto err_free_sd;
  2792. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2793. /*
  2794. * For non-fast symlinks, we just allocate inode and put it on
  2795. * orphan list in the first transaction => we need bitmap,
  2796. * group descriptor, sb, inode block, quota blocks, and
  2797. * possibly selinux xattr blocks.
  2798. */
  2799. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2800. EXT4_XATTR_TRANS_BLOCKS;
  2801. } else {
  2802. /*
  2803. * Fast symlink. We have to add entry to directory
  2804. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2805. * allocate new inode (bitmap, group descriptor, inode block,
  2806. * quota blocks, sb is already counted in previous macros).
  2807. */
  2808. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2809. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
  2810. }
  2811. inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
  2812. &dentry->d_name, 0, NULL,
  2813. EXT4_HT_DIR, credits);
  2814. handle = ext4_journal_current_handle();
  2815. if (IS_ERR(inode)) {
  2816. if (handle)
  2817. ext4_journal_stop(handle);
  2818. err = PTR_ERR(inode);
  2819. goto err_free_sd;
  2820. }
  2821. if (encryption_required) {
  2822. struct qstr istr;
  2823. struct ext4_str ostr;
  2824. istr.name = (const unsigned char *) symname;
  2825. istr.len = len;
  2826. ostr.name = sd->encrypted_path;
  2827. ostr.len = disk_link.len;
  2828. err = ext4_fname_usr_to_disk(inode, &istr, &ostr);
  2829. if (err < 0)
  2830. goto err_drop_inode;
  2831. sd->len = cpu_to_le16(ostr.len);
  2832. disk_link.name = (char *) sd;
  2833. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  2834. }
  2835. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2836. if (!encryption_required)
  2837. inode->i_op = &ext4_symlink_inode_operations;
  2838. ext4_set_aops(inode);
  2839. /*
  2840. * We cannot call page_symlink() with transaction started
  2841. * because it calls into ext4_write_begin() which can wait
  2842. * for transaction commit if we are running out of space
  2843. * and thus we deadlock. So we have to stop transaction now
  2844. * and restart it when symlink contents is written.
  2845. *
  2846. * To keep fs consistent in case of crash, we have to put inode
  2847. * to orphan list in the mean time.
  2848. */
  2849. drop_nlink(inode);
  2850. err = ext4_orphan_add(handle, inode);
  2851. ext4_journal_stop(handle);
  2852. handle = NULL;
  2853. if (err)
  2854. goto err_drop_inode;
  2855. err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
  2856. if (err)
  2857. goto err_drop_inode;
  2858. /*
  2859. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2860. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2861. */
  2862. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2863. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2864. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2865. if (IS_ERR(handle)) {
  2866. err = PTR_ERR(handle);
  2867. handle = NULL;
  2868. goto err_drop_inode;
  2869. }
  2870. set_nlink(inode, 1);
  2871. err = ext4_orphan_del(handle, inode);
  2872. if (err)
  2873. goto err_drop_inode;
  2874. } else {
  2875. /* clear the extent format for fast symlink */
  2876. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2877. if (!encryption_required) {
  2878. inode->i_op = &ext4_fast_symlink_inode_operations;
  2879. inode->i_link = (char *)&EXT4_I(inode)->i_data;
  2880. }
  2881. memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
  2882. disk_link.len);
  2883. inode->i_size = disk_link.len - 1;
  2884. }
  2885. EXT4_I(inode)->i_disksize = inode->i_size;
  2886. err = ext4_add_nondir(handle, dentry, inode);
  2887. if (!err && IS_DIRSYNC(dir))
  2888. ext4_handle_sync(handle);
  2889. if (handle)
  2890. ext4_journal_stop(handle);
  2891. kfree(sd);
  2892. return err;
  2893. err_drop_inode:
  2894. if (handle)
  2895. ext4_journal_stop(handle);
  2896. clear_nlink(inode);
  2897. unlock_new_inode(inode);
  2898. iput(inode);
  2899. err_free_sd:
  2900. kfree(sd);
  2901. return err;
  2902. }
  2903. static int ext4_link(struct dentry *old_dentry,
  2904. struct inode *dir, struct dentry *dentry)
  2905. {
  2906. handle_t *handle;
  2907. struct inode *inode = d_inode(old_dentry);
  2908. int err, retries = 0;
  2909. if (inode->i_nlink >= EXT4_LINK_MAX)
  2910. return -EMLINK;
  2911. if (ext4_encrypted_inode(dir) &&
  2912. !ext4_is_child_context_consistent_with_parent(dir, inode))
  2913. return -EPERM;
  2914. err = dquot_initialize(dir);
  2915. if (err)
  2916. return err;
  2917. retry:
  2918. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2919. (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2920. EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
  2921. if (IS_ERR(handle))
  2922. return PTR_ERR(handle);
  2923. if (IS_DIRSYNC(dir))
  2924. ext4_handle_sync(handle);
  2925. inode->i_ctime = ext4_current_time(inode);
  2926. ext4_inc_count(handle, inode);
  2927. ihold(inode);
  2928. err = ext4_add_entry(handle, dentry, inode);
  2929. if (!err) {
  2930. ext4_mark_inode_dirty(handle, inode);
  2931. /* this can happen only for tmpfile being
  2932. * linked the first time
  2933. */
  2934. if (inode->i_nlink == 1)
  2935. ext4_orphan_del(handle, inode);
  2936. d_instantiate(dentry, inode);
  2937. } else {
  2938. drop_nlink(inode);
  2939. iput(inode);
  2940. }
  2941. ext4_journal_stop(handle);
  2942. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2943. goto retry;
  2944. return err;
  2945. }
  2946. /*
  2947. * Try to find buffer head where contains the parent block.
  2948. * It should be the inode block if it is inlined or the 1st block
  2949. * if it is a normal dir.
  2950. */
  2951. static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
  2952. struct inode *inode,
  2953. int *retval,
  2954. struct ext4_dir_entry_2 **parent_de,
  2955. int *inlined)
  2956. {
  2957. struct buffer_head *bh;
  2958. if (!ext4_has_inline_data(inode)) {
  2959. bh = ext4_read_dirblock(inode, 0, EITHER);
  2960. if (IS_ERR(bh)) {
  2961. *retval = PTR_ERR(bh);
  2962. return NULL;
  2963. }
  2964. *parent_de = ext4_next_entry(
  2965. (struct ext4_dir_entry_2 *)bh->b_data,
  2966. inode->i_sb->s_blocksize);
  2967. return bh;
  2968. }
  2969. *inlined = 1;
  2970. return ext4_get_first_inline_block(inode, parent_de, retval);
  2971. }
  2972. struct ext4_renament {
  2973. struct inode *dir;
  2974. struct dentry *dentry;
  2975. struct inode *inode;
  2976. bool is_dir;
  2977. int dir_nlink_delta;
  2978. /* entry for "dentry" */
  2979. struct buffer_head *bh;
  2980. struct ext4_dir_entry_2 *de;
  2981. int inlined;
  2982. /* entry for ".." in inode if it's a directory */
  2983. struct buffer_head *dir_bh;
  2984. struct ext4_dir_entry_2 *parent_de;
  2985. int dir_inlined;
  2986. };
  2987. static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
  2988. {
  2989. int retval;
  2990. ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
  2991. &retval, &ent->parent_de,
  2992. &ent->dir_inlined);
  2993. if (!ent->dir_bh)
  2994. return retval;
  2995. if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
  2996. return -EFSCORRUPTED;
  2997. BUFFER_TRACE(ent->dir_bh, "get_write_access");
  2998. return ext4_journal_get_write_access(handle, ent->dir_bh);
  2999. }
  3000. static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
  3001. unsigned dir_ino)
  3002. {
  3003. int retval;
  3004. ent->parent_de->inode = cpu_to_le32(dir_ino);
  3005. BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
  3006. if (!ent->dir_inlined) {
  3007. if (is_dx(ent->inode)) {
  3008. retval = ext4_handle_dirty_dx_node(handle,
  3009. ent->inode,
  3010. ent->dir_bh);
  3011. } else {
  3012. retval = ext4_handle_dirty_dirent_node(handle,
  3013. ent->inode,
  3014. ent->dir_bh);
  3015. }
  3016. } else {
  3017. retval = ext4_mark_inode_dirty(handle, ent->inode);
  3018. }
  3019. if (retval) {
  3020. ext4_std_error(ent->dir->i_sb, retval);
  3021. return retval;
  3022. }
  3023. return 0;
  3024. }
  3025. static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
  3026. unsigned ino, unsigned file_type)
  3027. {
  3028. int retval;
  3029. BUFFER_TRACE(ent->bh, "get write access");
  3030. retval = ext4_journal_get_write_access(handle, ent->bh);
  3031. if (retval)
  3032. return retval;
  3033. ent->de->inode = cpu_to_le32(ino);
  3034. if (ext4_has_feature_filetype(ent->dir->i_sb))
  3035. ent->de->file_type = file_type;
  3036. ent->dir->i_version++;
  3037. ent->dir->i_ctime = ent->dir->i_mtime =
  3038. ext4_current_time(ent->dir);
  3039. ext4_mark_inode_dirty(handle, ent->dir);
  3040. BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
  3041. if (!ent->inlined) {
  3042. retval = ext4_handle_dirty_dirent_node(handle,
  3043. ent->dir, ent->bh);
  3044. if (unlikely(retval)) {
  3045. ext4_std_error(ent->dir->i_sb, retval);
  3046. return retval;
  3047. }
  3048. }
  3049. brelse(ent->bh);
  3050. ent->bh = NULL;
  3051. return 0;
  3052. }
  3053. static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
  3054. const struct qstr *d_name)
  3055. {
  3056. int retval = -ENOENT;
  3057. struct buffer_head *bh;
  3058. struct ext4_dir_entry_2 *de;
  3059. bh = ext4_find_entry(dir, d_name, &de, NULL);
  3060. if (IS_ERR(bh))
  3061. return PTR_ERR(bh);
  3062. if (bh) {
  3063. retval = ext4_delete_entry(handle, dir, de, bh);
  3064. brelse(bh);
  3065. }
  3066. return retval;
  3067. }
  3068. static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
  3069. int force_reread)
  3070. {
  3071. int retval;
  3072. /*
  3073. * ent->de could have moved from under us during htree split, so make
  3074. * sure that we are deleting the right entry. We might also be pointing
  3075. * to a stale entry in the unused part of ent->bh so just checking inum
  3076. * and the name isn't enough.
  3077. */
  3078. if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
  3079. ent->de->name_len != ent->dentry->d_name.len ||
  3080. strncmp(ent->de->name, ent->dentry->d_name.name,
  3081. ent->de->name_len) ||
  3082. force_reread) {
  3083. retval = ext4_find_delete_entry(handle, ent->dir,
  3084. &ent->dentry->d_name);
  3085. } else {
  3086. retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
  3087. if (retval == -ENOENT) {
  3088. retval = ext4_find_delete_entry(handle, ent->dir,
  3089. &ent->dentry->d_name);
  3090. }
  3091. }
  3092. if (retval) {
  3093. ext4_warning_inode(ent->dir,
  3094. "Deleting old file: nlink %d, error=%d",
  3095. ent->dir->i_nlink, retval);
  3096. }
  3097. }
  3098. static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
  3099. {
  3100. if (ent->dir_nlink_delta) {
  3101. if (ent->dir_nlink_delta == -1)
  3102. ext4_dec_count(handle, ent->dir);
  3103. else
  3104. ext4_inc_count(handle, ent->dir);
  3105. ext4_mark_inode_dirty(handle, ent->dir);
  3106. }
  3107. }
  3108. static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
  3109. int credits, handle_t **h)
  3110. {
  3111. struct inode *wh;
  3112. handle_t *handle;
  3113. int retries = 0;
  3114. /*
  3115. * for inode block, sb block, group summaries,
  3116. * and inode bitmap
  3117. */
  3118. credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
  3119. EXT4_XATTR_TRANS_BLOCKS + 4);
  3120. retry:
  3121. wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
  3122. &ent->dentry->d_name, 0, NULL,
  3123. EXT4_HT_DIR, credits);
  3124. handle = ext4_journal_current_handle();
  3125. if (IS_ERR(wh)) {
  3126. if (handle)
  3127. ext4_journal_stop(handle);
  3128. if (PTR_ERR(wh) == -ENOSPC &&
  3129. ext4_should_retry_alloc(ent->dir->i_sb, &retries))
  3130. goto retry;
  3131. } else {
  3132. *h = handle;
  3133. init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
  3134. wh->i_op = &ext4_special_inode_operations;
  3135. }
  3136. return wh;
  3137. }
  3138. /*
  3139. * Anybody can rename anything with this: the permission checks are left to the
  3140. * higher-level routines.
  3141. *
  3142. * n.b. old_{dentry,inode) refers to the source dentry/inode
  3143. * while new_{dentry,inode) refers to the destination dentry/inode
  3144. * This comes from rename(const char *oldpath, const char *newpath)
  3145. */
  3146. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  3147. struct inode *new_dir, struct dentry *new_dentry,
  3148. unsigned int flags)
  3149. {
  3150. handle_t *handle = NULL;
  3151. struct ext4_renament old = {
  3152. .dir = old_dir,
  3153. .dentry = old_dentry,
  3154. .inode = d_inode(old_dentry),
  3155. };
  3156. struct ext4_renament new = {
  3157. .dir = new_dir,
  3158. .dentry = new_dentry,
  3159. .inode = d_inode(new_dentry),
  3160. };
  3161. int force_reread;
  3162. int retval;
  3163. struct inode *whiteout = NULL;
  3164. int credits;
  3165. u8 old_file_type;
  3166. retval = dquot_initialize(old.dir);
  3167. if (retval)
  3168. return retval;
  3169. retval = dquot_initialize(new.dir);
  3170. if (retval)
  3171. return retval;
  3172. /* Initialize quotas before so that eventual writes go
  3173. * in separate transaction */
  3174. if (new.inode) {
  3175. retval = dquot_initialize(new.inode);
  3176. if (retval)
  3177. return retval;
  3178. }
  3179. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
  3180. if (IS_ERR(old.bh))
  3181. return PTR_ERR(old.bh);
  3182. /*
  3183. * Check for inode number is _not_ due to possible IO errors.
  3184. * We might rmdir the source, keep it as pwd of some process
  3185. * and merrily kill the link to whatever was created under the
  3186. * same name. Goodbye sticky bit ;-<
  3187. */
  3188. retval = -ENOENT;
  3189. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3190. goto end_rename;
  3191. if ((old.dir != new.dir) &&
  3192. ext4_encrypted_inode(new.dir) &&
  3193. !ext4_is_child_context_consistent_with_parent(new.dir,
  3194. old.inode)) {
  3195. retval = -EPERM;
  3196. goto end_rename;
  3197. }
  3198. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3199. &new.de, &new.inlined);
  3200. if (IS_ERR(new.bh)) {
  3201. retval = PTR_ERR(new.bh);
  3202. new.bh = NULL;
  3203. goto end_rename;
  3204. }
  3205. if (new.bh) {
  3206. if (!new.inode) {
  3207. brelse(new.bh);
  3208. new.bh = NULL;
  3209. }
  3210. }
  3211. if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
  3212. ext4_alloc_da_blocks(old.inode);
  3213. credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3214. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  3215. if (!(flags & RENAME_WHITEOUT)) {
  3216. handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
  3217. if (IS_ERR(handle)) {
  3218. retval = PTR_ERR(handle);
  3219. handle = NULL;
  3220. goto end_rename;
  3221. }
  3222. } else {
  3223. whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
  3224. if (IS_ERR(whiteout)) {
  3225. retval = PTR_ERR(whiteout);
  3226. whiteout = NULL;
  3227. goto end_rename;
  3228. }
  3229. }
  3230. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3231. ext4_handle_sync(handle);
  3232. if (S_ISDIR(old.inode->i_mode)) {
  3233. if (new.inode) {
  3234. retval = -ENOTEMPTY;
  3235. if (!ext4_empty_dir(new.inode))
  3236. goto end_rename;
  3237. } else {
  3238. retval = -EMLINK;
  3239. if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
  3240. goto end_rename;
  3241. }
  3242. retval = ext4_rename_dir_prepare(handle, &old);
  3243. if (retval)
  3244. goto end_rename;
  3245. }
  3246. /*
  3247. * If we're renaming a file within an inline_data dir and adding or
  3248. * setting the new dirent causes a conversion from inline_data to
  3249. * extents/blockmap, we need to force the dirent delete code to
  3250. * re-read the directory, or else we end up trying to delete a dirent
  3251. * from what is now the extent tree root (or a block map).
  3252. */
  3253. force_reread = (new.dir->i_ino == old.dir->i_ino &&
  3254. ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
  3255. old_file_type = old.de->file_type;
  3256. if (whiteout) {
  3257. /*
  3258. * Do this before adding a new entry, so the old entry is sure
  3259. * to be still pointing to the valid old entry.
  3260. */
  3261. retval = ext4_setent(handle, &old, whiteout->i_ino,
  3262. EXT4_FT_CHRDEV);
  3263. if (retval)
  3264. goto end_rename;
  3265. ext4_mark_inode_dirty(handle, whiteout);
  3266. }
  3267. if (!new.bh) {
  3268. retval = ext4_add_entry(handle, new.dentry, old.inode);
  3269. if (retval)
  3270. goto end_rename;
  3271. } else {
  3272. retval = ext4_setent(handle, &new,
  3273. old.inode->i_ino, old_file_type);
  3274. if (retval)
  3275. goto end_rename;
  3276. }
  3277. if (force_reread)
  3278. force_reread = !ext4_test_inode_flag(new.dir,
  3279. EXT4_INODE_INLINE_DATA);
  3280. /*
  3281. * Like most other Unix systems, set the ctime for inodes on a
  3282. * rename.
  3283. */
  3284. old.inode->i_ctime = ext4_current_time(old.inode);
  3285. ext4_mark_inode_dirty(handle, old.inode);
  3286. if (!whiteout) {
  3287. /*
  3288. * ok, that's it
  3289. */
  3290. ext4_rename_delete(handle, &old, force_reread);
  3291. }
  3292. if (new.inode) {
  3293. ext4_dec_count(handle, new.inode);
  3294. new.inode->i_ctime = ext4_current_time(new.inode);
  3295. }
  3296. old.dir->i_ctime = old.dir->i_mtime = ext4_current_time(old.dir);
  3297. ext4_update_dx_flag(old.dir);
  3298. if (old.dir_bh) {
  3299. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3300. if (retval)
  3301. goto end_rename;
  3302. ext4_dec_count(handle, old.dir);
  3303. if (new.inode) {
  3304. /* checked ext4_empty_dir above, can't have another
  3305. * parent, ext4_dec_count() won't work for many-linked
  3306. * dirs */
  3307. clear_nlink(new.inode);
  3308. } else {
  3309. ext4_inc_count(handle, new.dir);
  3310. ext4_update_dx_flag(new.dir);
  3311. ext4_mark_inode_dirty(handle, new.dir);
  3312. }
  3313. }
  3314. ext4_mark_inode_dirty(handle, old.dir);
  3315. if (new.inode) {
  3316. ext4_mark_inode_dirty(handle, new.inode);
  3317. if (!new.inode->i_nlink)
  3318. ext4_orphan_add(handle, new.inode);
  3319. }
  3320. retval = 0;
  3321. end_rename:
  3322. brelse(old.dir_bh);
  3323. brelse(old.bh);
  3324. brelse(new.bh);
  3325. if (whiteout) {
  3326. if (retval)
  3327. drop_nlink(whiteout);
  3328. unlock_new_inode(whiteout);
  3329. iput(whiteout);
  3330. }
  3331. if (handle)
  3332. ext4_journal_stop(handle);
  3333. return retval;
  3334. }
  3335. static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  3336. struct inode *new_dir, struct dentry *new_dentry)
  3337. {
  3338. handle_t *handle = NULL;
  3339. struct ext4_renament old = {
  3340. .dir = old_dir,
  3341. .dentry = old_dentry,
  3342. .inode = d_inode(old_dentry),
  3343. };
  3344. struct ext4_renament new = {
  3345. .dir = new_dir,
  3346. .dentry = new_dentry,
  3347. .inode = d_inode(new_dentry),
  3348. };
  3349. u8 new_file_type;
  3350. int retval;
  3351. if ((ext4_encrypted_inode(old_dir) ||
  3352. ext4_encrypted_inode(new_dir)) &&
  3353. (old_dir != new_dir) &&
  3354. (!ext4_is_child_context_consistent_with_parent(new_dir,
  3355. old.inode) ||
  3356. !ext4_is_child_context_consistent_with_parent(old_dir,
  3357. new.inode)))
  3358. return -EPERM;
  3359. retval = dquot_initialize(old.dir);
  3360. if (retval)
  3361. return retval;
  3362. retval = dquot_initialize(new.dir);
  3363. if (retval)
  3364. return retval;
  3365. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
  3366. &old.de, &old.inlined);
  3367. if (IS_ERR(old.bh))
  3368. return PTR_ERR(old.bh);
  3369. /*
  3370. * Check for inode number is _not_ due to possible IO errors.
  3371. * We might rmdir the source, keep it as pwd of some process
  3372. * and merrily kill the link to whatever was created under the
  3373. * same name. Goodbye sticky bit ;-<
  3374. */
  3375. retval = -ENOENT;
  3376. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3377. goto end_rename;
  3378. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3379. &new.de, &new.inlined);
  3380. if (IS_ERR(new.bh)) {
  3381. retval = PTR_ERR(new.bh);
  3382. new.bh = NULL;
  3383. goto end_rename;
  3384. }
  3385. /* RENAME_EXCHANGE case: old *and* new must both exist */
  3386. if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
  3387. goto end_rename;
  3388. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  3389. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3390. 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  3391. if (IS_ERR(handle)) {
  3392. retval = PTR_ERR(handle);
  3393. handle = NULL;
  3394. goto end_rename;
  3395. }
  3396. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3397. ext4_handle_sync(handle);
  3398. if (S_ISDIR(old.inode->i_mode)) {
  3399. old.is_dir = true;
  3400. retval = ext4_rename_dir_prepare(handle, &old);
  3401. if (retval)
  3402. goto end_rename;
  3403. }
  3404. if (S_ISDIR(new.inode->i_mode)) {
  3405. new.is_dir = true;
  3406. retval = ext4_rename_dir_prepare(handle, &new);
  3407. if (retval)
  3408. goto end_rename;
  3409. }
  3410. /*
  3411. * Other than the special case of overwriting a directory, parents'
  3412. * nlink only needs to be modified if this is a cross directory rename.
  3413. */
  3414. if (old.dir != new.dir && old.is_dir != new.is_dir) {
  3415. old.dir_nlink_delta = old.is_dir ? -1 : 1;
  3416. new.dir_nlink_delta = -old.dir_nlink_delta;
  3417. retval = -EMLINK;
  3418. if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
  3419. (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
  3420. goto end_rename;
  3421. }
  3422. new_file_type = new.de->file_type;
  3423. retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
  3424. if (retval)
  3425. goto end_rename;
  3426. retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
  3427. if (retval)
  3428. goto end_rename;
  3429. /*
  3430. * Like most other Unix systems, set the ctime for inodes on a
  3431. * rename.
  3432. */
  3433. old.inode->i_ctime = ext4_current_time(old.inode);
  3434. new.inode->i_ctime = ext4_current_time(new.inode);
  3435. ext4_mark_inode_dirty(handle, old.inode);
  3436. ext4_mark_inode_dirty(handle, new.inode);
  3437. if (old.dir_bh) {
  3438. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3439. if (retval)
  3440. goto end_rename;
  3441. }
  3442. if (new.dir_bh) {
  3443. retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
  3444. if (retval)
  3445. goto end_rename;
  3446. }
  3447. ext4_update_dir_count(handle, &old);
  3448. ext4_update_dir_count(handle, &new);
  3449. retval = 0;
  3450. end_rename:
  3451. brelse(old.dir_bh);
  3452. brelse(new.dir_bh);
  3453. brelse(old.bh);
  3454. brelse(new.bh);
  3455. if (handle)
  3456. ext4_journal_stop(handle);
  3457. return retval;
  3458. }
  3459. static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
  3460. struct inode *new_dir, struct dentry *new_dentry,
  3461. unsigned int flags)
  3462. {
  3463. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3464. return -EINVAL;
  3465. if (flags & RENAME_EXCHANGE) {
  3466. return ext4_cross_rename(old_dir, old_dentry,
  3467. new_dir, new_dentry);
  3468. }
  3469. return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  3470. }
  3471. /*
  3472. * directories can handle most operations...
  3473. */
  3474. const struct inode_operations ext4_dir_inode_operations = {
  3475. .create = ext4_create,
  3476. .lookup = ext4_lookup,
  3477. .link = ext4_link,
  3478. .unlink = ext4_unlink,
  3479. .symlink = ext4_symlink,
  3480. .mkdir = ext4_mkdir,
  3481. .rmdir = ext4_rmdir,
  3482. .mknod = ext4_mknod,
  3483. .tmpfile = ext4_tmpfile,
  3484. .rename2 = ext4_rename2,
  3485. .setattr = ext4_setattr,
  3486. .setxattr = generic_setxattr,
  3487. .getxattr = generic_getxattr,
  3488. .listxattr = ext4_listxattr,
  3489. .removexattr = generic_removexattr,
  3490. .get_acl = ext4_get_acl,
  3491. .set_acl = ext4_set_acl,
  3492. .fiemap = ext4_fiemap,
  3493. };
  3494. const struct inode_operations ext4_special_inode_operations = {
  3495. .setattr = ext4_setattr,
  3496. .setxattr = generic_setxattr,
  3497. .getxattr = generic_getxattr,
  3498. .listxattr = ext4_listxattr,
  3499. .removexattr = generic_removexattr,
  3500. .get_acl = ext4_get_acl,
  3501. .set_acl = ext4_set_acl,
  3502. };