namei.c 85 KB

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