namei.c 83 KB

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