namei.c 101 KB

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