namei.c 102 KB

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