namei.c 93 KB

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