namei.c 84 KB

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