namei.c 85 KB

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