namei.c 83 KB

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