namei.c 100 KB

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