namei.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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/ext4_fs.h>
  31. #include <linux/ext4_jbd2.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/stat.h>
  34. #include <linux/string.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/bio.h>
  38. #include <linux/smp_lock.h>
  39. #include "namei.h"
  40. #include "xattr.h"
  41. #include "acl.h"
  42. /*
  43. * define how far ahead to read directories while searching them.
  44. */
  45. #define NAMEI_RA_CHUNKS 2
  46. #define NAMEI_RA_BLOCKS 4
  47. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  48. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  49. static struct buffer_head *ext4_append(handle_t *handle,
  50. struct inode *inode,
  51. u32 *block, int *err)
  52. {
  53. struct buffer_head *bh;
  54. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  55. if ((bh = ext4_bread(handle, inode, *block, 1, err))) {
  56. inode->i_size += inode->i_sb->s_blocksize;
  57. EXT4_I(inode)->i_disksize = inode->i_size;
  58. ext4_journal_get_write_access(handle,bh);
  59. }
  60. return bh;
  61. }
  62. #ifndef assert
  63. #define assert(test) J_ASSERT(test)
  64. #endif
  65. #ifndef swap
  66. #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
  67. #endif
  68. #ifdef DX_DEBUG
  69. #define dxtrace(command) command
  70. #else
  71. #define dxtrace(command)
  72. #endif
  73. struct fake_dirent
  74. {
  75. __le32 inode;
  76. __le16 rec_len;
  77. u8 name_len;
  78. u8 file_type;
  79. };
  80. struct dx_countlimit
  81. {
  82. __le16 limit;
  83. __le16 count;
  84. };
  85. struct dx_entry
  86. {
  87. __le32 hash;
  88. __le32 block;
  89. };
  90. /*
  91. * dx_root_info is laid out so that if it should somehow get overlaid by a
  92. * dirent the two low bits of the hash version will be zero. Therefore, the
  93. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  94. */
  95. struct dx_root
  96. {
  97. struct fake_dirent dot;
  98. char dot_name[4];
  99. struct fake_dirent dotdot;
  100. char dotdot_name[4];
  101. struct dx_root_info
  102. {
  103. __le32 reserved_zero;
  104. u8 hash_version;
  105. u8 info_length; /* 8 */
  106. u8 indirect_levels;
  107. u8 unused_flags;
  108. }
  109. info;
  110. struct dx_entry entries[0];
  111. };
  112. struct dx_node
  113. {
  114. struct fake_dirent fake;
  115. struct dx_entry entries[0];
  116. };
  117. struct dx_frame
  118. {
  119. struct buffer_head *bh;
  120. struct dx_entry *entries;
  121. struct dx_entry *at;
  122. };
  123. struct dx_map_entry
  124. {
  125. u32 hash;
  126. u32 offs;
  127. };
  128. #ifdef CONFIG_EXT4_INDEX
  129. static inline unsigned dx_get_block (struct dx_entry *entry);
  130. static void dx_set_block (struct dx_entry *entry, unsigned value);
  131. static inline unsigned dx_get_hash (struct dx_entry *entry);
  132. static void dx_set_hash (struct dx_entry *entry, unsigned value);
  133. static unsigned dx_get_count (struct dx_entry *entries);
  134. static unsigned dx_get_limit (struct dx_entry *entries);
  135. static void dx_set_count (struct dx_entry *entries, unsigned value);
  136. static void dx_set_limit (struct dx_entry *entries, unsigned value);
  137. static unsigned dx_root_limit (struct inode *dir, unsigned infosize);
  138. static unsigned dx_node_limit (struct inode *dir);
  139. static struct dx_frame *dx_probe(struct dentry *dentry,
  140. struct inode *dir,
  141. struct dx_hash_info *hinfo,
  142. struct dx_frame *frame,
  143. int *err);
  144. static void dx_release (struct dx_frame *frames);
  145. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  146. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  147. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  148. static struct ext4_dir_entry_2 *dx_move_dirents (char *from, char *to,
  149. struct dx_map_entry *offsets, int count);
  150. static struct ext4_dir_entry_2* dx_pack_dirents (char *base, int size);
  151. static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block);
  152. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  153. struct dx_frame *frame,
  154. struct dx_frame *frames,
  155. __u32 *start_hash);
  156. static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
  157. struct ext4_dir_entry_2 **res_dir, int *err);
  158. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  159. struct inode *inode);
  160. /*
  161. * Future: use high four bits of block for coalesce-on-delete flags
  162. * Mask them off for now.
  163. */
  164. static inline unsigned dx_get_block (struct dx_entry *entry)
  165. {
  166. return le32_to_cpu(entry->block) & 0x00ffffff;
  167. }
  168. static inline void dx_set_block (struct dx_entry *entry, unsigned value)
  169. {
  170. entry->block = cpu_to_le32(value);
  171. }
  172. static inline unsigned dx_get_hash (struct dx_entry *entry)
  173. {
  174. return le32_to_cpu(entry->hash);
  175. }
  176. static inline void dx_set_hash (struct dx_entry *entry, unsigned value)
  177. {
  178. entry->hash = cpu_to_le32(value);
  179. }
  180. static inline unsigned dx_get_count (struct dx_entry *entries)
  181. {
  182. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  183. }
  184. static inline unsigned dx_get_limit (struct dx_entry *entries)
  185. {
  186. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  187. }
  188. static inline void dx_set_count (struct dx_entry *entries, unsigned value)
  189. {
  190. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  191. }
  192. static inline void dx_set_limit (struct dx_entry *entries, unsigned value)
  193. {
  194. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  195. }
  196. static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize)
  197. {
  198. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  199. EXT4_DIR_REC_LEN(2) - infosize;
  200. return 0? 20: entry_space / sizeof(struct dx_entry);
  201. }
  202. static inline unsigned dx_node_limit (struct inode *dir)
  203. {
  204. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  205. return 0? 22: entry_space / sizeof(struct dx_entry);
  206. }
  207. /*
  208. * Debug
  209. */
  210. #ifdef DX_DEBUG
  211. static void dx_show_index (char * label, struct dx_entry *entries)
  212. {
  213. int i, n = dx_get_count (entries);
  214. printk("%s index ", label);
  215. for (i = 0; i < n; i++) {
  216. printk("%x->%u ", i? dx_get_hash(entries + i) :
  217. 0, dx_get_block(entries + i));
  218. }
  219. printk("\n");
  220. }
  221. struct stats
  222. {
  223. unsigned names;
  224. unsigned space;
  225. unsigned bcount;
  226. };
  227. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  228. int size, int show_names)
  229. {
  230. unsigned names = 0, space = 0;
  231. char *base = (char *) de;
  232. struct dx_hash_info h = *hinfo;
  233. printk("names: ");
  234. while ((char *) de < base + size)
  235. {
  236. if (de->inode)
  237. {
  238. if (show_names)
  239. {
  240. int len = de->name_len;
  241. char *name = de->name;
  242. while (len--) printk("%c", *name++);
  243. ext4fs_dirhash(de->name, de->name_len, &h);
  244. printk(":%x.%u ", h.hash,
  245. ((char *) de - base));
  246. }
  247. space += EXT4_DIR_REC_LEN(de->name_len);
  248. names++;
  249. }
  250. de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
  251. }
  252. printk("(%i)\n", names);
  253. return (struct stats) { names, space, 1 };
  254. }
  255. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  256. struct dx_entry *entries, int levels)
  257. {
  258. unsigned blocksize = dir->i_sb->s_blocksize;
  259. unsigned count = dx_get_count (entries), names = 0, space = 0, i;
  260. unsigned bcount = 0;
  261. struct buffer_head *bh;
  262. int err;
  263. printk("%i indexed blocks...\n", count);
  264. for (i = 0; i < count; i++, entries++)
  265. {
  266. u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0;
  267. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  268. struct stats stats;
  269. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  270. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  271. stats = levels?
  272. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  273. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  274. names += stats.names;
  275. space += stats.space;
  276. bcount += stats.bcount;
  277. brelse (bh);
  278. }
  279. if (bcount)
  280. printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ",
  281. names, space/bcount,(space/bcount)*100/blocksize);
  282. return (struct stats) { names, space, bcount};
  283. }
  284. #endif /* DX_DEBUG */
  285. /*
  286. * Probe for a directory leaf block to search.
  287. *
  288. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  289. * error in the directory index, and the caller should fall back to
  290. * searching the directory normally. The callers of dx_probe **MUST**
  291. * check for this error code, and make sure it never gets reflected
  292. * back to userspace.
  293. */
  294. static struct dx_frame *
  295. dx_probe(struct dentry *dentry, struct inode *dir,
  296. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  297. {
  298. unsigned count, indirect;
  299. struct dx_entry *at, *entries, *p, *q, *m;
  300. struct dx_root *root;
  301. struct buffer_head *bh;
  302. struct dx_frame *frame = frame_in;
  303. u32 hash;
  304. frame->bh = NULL;
  305. if (dentry)
  306. dir = dentry->d_parent->d_inode;
  307. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  308. goto fail;
  309. root = (struct dx_root *) bh->b_data;
  310. if (root->info.hash_version != DX_HASH_TEA &&
  311. root->info.hash_version != DX_HASH_HALF_MD4 &&
  312. root->info.hash_version != DX_HASH_LEGACY) {
  313. ext4_warning(dir->i_sb, __FUNCTION__,
  314. "Unrecognised inode hash code %d",
  315. root->info.hash_version);
  316. brelse(bh);
  317. *err = ERR_BAD_DX_DIR;
  318. goto fail;
  319. }
  320. hinfo->hash_version = root->info.hash_version;
  321. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  322. if (dentry)
  323. ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo);
  324. hash = hinfo->hash;
  325. if (root->info.unused_flags & 1) {
  326. ext4_warning(dir->i_sb, __FUNCTION__,
  327. "Unimplemented inode hash flags: %#06x",
  328. root->info.unused_flags);
  329. brelse(bh);
  330. *err = ERR_BAD_DX_DIR;
  331. goto fail;
  332. }
  333. if ((indirect = root->info.indirect_levels) > 1) {
  334. ext4_warning(dir->i_sb, __FUNCTION__,
  335. "Unimplemented inode hash depth: %#06x",
  336. root->info.indirect_levels);
  337. brelse(bh);
  338. *err = ERR_BAD_DX_DIR;
  339. goto fail;
  340. }
  341. entries = (struct dx_entry *) (((char *)&root->info) +
  342. root->info.info_length);
  343. assert(dx_get_limit(entries) == dx_root_limit(dir,
  344. root->info.info_length));
  345. dxtrace (printk("Look up %x", hash));
  346. while (1)
  347. {
  348. count = dx_get_count(entries);
  349. assert (count && count <= dx_get_limit(entries));
  350. p = entries + 1;
  351. q = entries + count - 1;
  352. while (p <= q)
  353. {
  354. m = p + (q - p)/2;
  355. dxtrace(printk("."));
  356. if (dx_get_hash(m) > hash)
  357. q = m - 1;
  358. else
  359. p = m + 1;
  360. }
  361. if (0) // linear search cross check
  362. {
  363. unsigned n = count - 1;
  364. at = entries;
  365. while (n--)
  366. {
  367. dxtrace(printk(","));
  368. if (dx_get_hash(++at) > hash)
  369. {
  370. at--;
  371. break;
  372. }
  373. }
  374. assert (at == p - 1);
  375. }
  376. at = p - 1;
  377. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  378. frame->bh = bh;
  379. frame->entries = entries;
  380. frame->at = at;
  381. if (!indirect--) return frame;
  382. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  383. goto fail2;
  384. at = entries = ((struct dx_node *) bh->b_data)->entries;
  385. assert (dx_get_limit(entries) == dx_node_limit (dir));
  386. frame++;
  387. }
  388. fail2:
  389. while (frame >= frame_in) {
  390. brelse(frame->bh);
  391. frame--;
  392. }
  393. fail:
  394. return NULL;
  395. }
  396. static void dx_release (struct dx_frame *frames)
  397. {
  398. if (frames[0].bh == NULL)
  399. return;
  400. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  401. brelse(frames[1].bh);
  402. brelse(frames[0].bh);
  403. }
  404. /*
  405. * This function increments the frame pointer to search the next leaf
  406. * block, and reads in the necessary intervening nodes if the search
  407. * should be necessary. Whether or not the search is necessary is
  408. * controlled by the hash parameter. If the hash value is even, then
  409. * the search is only continued if the next block starts with that
  410. * hash value. This is used if we are searching for a specific file.
  411. *
  412. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  413. *
  414. * This function returns 1 if the caller should continue to search,
  415. * or 0 if it should not. If there is an error reading one of the
  416. * index blocks, it will a negative error code.
  417. *
  418. * If start_hash is non-null, it will be filled in with the starting
  419. * hash of the next page.
  420. */
  421. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  422. struct dx_frame *frame,
  423. struct dx_frame *frames,
  424. __u32 *start_hash)
  425. {
  426. struct dx_frame *p;
  427. struct buffer_head *bh;
  428. int err, num_frames = 0;
  429. __u32 bhash;
  430. p = frame;
  431. /*
  432. * Find the next leaf page by incrementing the frame pointer.
  433. * If we run out of entries in the interior node, loop around and
  434. * increment pointer in the parent node. When we break out of
  435. * this loop, num_frames indicates the number of interior
  436. * nodes need to be read.
  437. */
  438. while (1) {
  439. if (++(p->at) < p->entries + dx_get_count(p->entries))
  440. break;
  441. if (p == frames)
  442. return 0;
  443. num_frames++;
  444. p--;
  445. }
  446. /*
  447. * If the hash is 1, then continue only if the next page has a
  448. * continuation hash of any value. This is used for readdir
  449. * handling. Otherwise, check to see if the hash matches the
  450. * desired contiuation hash. If it doesn't, return since
  451. * there's no point to read in the successive index pages.
  452. */
  453. bhash = dx_get_hash(p->at);
  454. if (start_hash)
  455. *start_hash = bhash;
  456. if ((hash & 1) == 0) {
  457. if ((bhash & ~1) != hash)
  458. return 0;
  459. }
  460. /*
  461. * If the hash is HASH_NB_ALWAYS, we always go to the next
  462. * block so no check is necessary
  463. */
  464. while (num_frames--) {
  465. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  466. 0, &err)))
  467. return err; /* Failure */
  468. p++;
  469. brelse (p->bh);
  470. p->bh = bh;
  471. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  472. }
  473. return 1;
  474. }
  475. /*
  476. * p is at least 6 bytes before the end of page
  477. */
  478. static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p)
  479. {
  480. return (struct ext4_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len));
  481. }
  482. /*
  483. * This function fills a red-black tree with information from a
  484. * directory block. It returns the number directory entries loaded
  485. * into the tree. If there is an error it is returned in err.
  486. */
  487. static int htree_dirblock_to_tree(struct file *dir_file,
  488. struct inode *dir, int block,
  489. struct dx_hash_info *hinfo,
  490. __u32 start_hash, __u32 start_minor_hash)
  491. {
  492. struct buffer_head *bh;
  493. struct ext4_dir_entry_2 *de, *top;
  494. int err, count = 0;
  495. dxtrace(printk("In htree dirblock_to_tree: block %d\n", block));
  496. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  497. return err;
  498. de = (struct ext4_dir_entry_2 *) bh->b_data;
  499. top = (struct ext4_dir_entry_2 *) ((char *) de +
  500. dir->i_sb->s_blocksize -
  501. EXT4_DIR_REC_LEN(0));
  502. for (; de < top; de = ext4_next_entry(de)) {
  503. if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  504. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  505. +((char *)de - bh->b_data))) {
  506. /* On error, skip the f_pos to the next block. */
  507. dir_file->f_pos = (dir_file->f_pos |
  508. (dir->i_sb->s_blocksize - 1)) + 1;
  509. brelse (bh);
  510. return count;
  511. }
  512. ext4fs_dirhash(de->name, de->name_len, hinfo);
  513. if ((hinfo->hash < start_hash) ||
  514. ((hinfo->hash == start_hash) &&
  515. (hinfo->minor_hash < start_minor_hash)))
  516. continue;
  517. if (de->inode == 0)
  518. continue;
  519. if ((err = ext4_htree_store_dirent(dir_file,
  520. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  521. brelse(bh);
  522. return err;
  523. }
  524. count++;
  525. }
  526. brelse(bh);
  527. return count;
  528. }
  529. /*
  530. * This function fills a red-black tree with information from a
  531. * directory. We start scanning the directory in hash order, starting
  532. * at start_hash and start_minor_hash.
  533. *
  534. * This function returns the number of entries inserted into the tree,
  535. * or a negative error code.
  536. */
  537. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  538. __u32 start_minor_hash, __u32 *next_hash)
  539. {
  540. struct dx_hash_info hinfo;
  541. struct ext4_dir_entry_2 *de;
  542. struct dx_frame frames[2], *frame;
  543. struct inode *dir;
  544. int block, err;
  545. int count = 0;
  546. int ret;
  547. __u32 hashval;
  548. dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash,
  549. start_minor_hash));
  550. dir = dir_file->f_path.dentry->d_inode;
  551. if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
  552. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  553. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  554. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  555. start_hash, start_minor_hash);
  556. *next_hash = ~0;
  557. return count;
  558. }
  559. hinfo.hash = start_hash;
  560. hinfo.minor_hash = 0;
  561. frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err);
  562. if (!frame)
  563. return err;
  564. /* Add '.' and '..' from the htree header */
  565. if (!start_hash && !start_minor_hash) {
  566. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  567. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  568. goto errout;
  569. count++;
  570. }
  571. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  572. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  573. de = ext4_next_entry(de);
  574. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  575. goto errout;
  576. count++;
  577. }
  578. while (1) {
  579. block = dx_get_block(frame->at);
  580. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  581. start_hash, start_minor_hash);
  582. if (ret < 0) {
  583. err = ret;
  584. goto errout;
  585. }
  586. count += ret;
  587. hashval = ~0;
  588. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  589. frame, frames, &hashval);
  590. *next_hash = hashval;
  591. if (ret < 0) {
  592. err = ret;
  593. goto errout;
  594. }
  595. /*
  596. * Stop if: (a) there are no more entries, or
  597. * (b) we have inserted at least one entry and the
  598. * next hash value is not a continuation
  599. */
  600. if ((ret == 0) ||
  601. (count && ((hashval & 1) == 0)))
  602. break;
  603. }
  604. dx_release(frames);
  605. dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n",
  606. count, *next_hash));
  607. return count;
  608. errout:
  609. dx_release(frames);
  610. return (err);
  611. }
  612. /*
  613. * Directory block splitting, compacting
  614. */
  615. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  616. struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
  617. {
  618. int count = 0;
  619. char *base = (char *) de;
  620. struct dx_hash_info h = *hinfo;
  621. while ((char *) de < base + size)
  622. {
  623. if (de->name_len && de->inode) {
  624. ext4fs_dirhash(de->name, de->name_len, &h);
  625. map_tail--;
  626. map_tail->hash = h.hash;
  627. map_tail->offs = (u32) ((char *) de - base);
  628. count++;
  629. cond_resched();
  630. }
  631. /* XXX: do we need to check rec_len == 0 case? -Chris */
  632. de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len));
  633. }
  634. return count;
  635. }
  636. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  637. {
  638. struct dx_map_entry *p, *q, *top = map + count - 1;
  639. int more;
  640. /* Combsort until bubble sort doesn't suck */
  641. while (count > 2) {
  642. count = count*10/13;
  643. if (count - 9 < 2) /* 9, 10 -> 11 */
  644. count = 11;
  645. for (p = top, q = p - count; q >= map; p--, q--)
  646. if (p->hash < q->hash)
  647. swap(*p, *q);
  648. }
  649. /* Garden variety bubble sort */
  650. do {
  651. more = 0;
  652. q = top;
  653. while (q-- > map) {
  654. if (q[1].hash >= q[0].hash)
  655. continue;
  656. swap(*(q+1), *q);
  657. more = 1;
  658. }
  659. } while(more);
  660. }
  661. static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block)
  662. {
  663. struct dx_entry *entries = frame->entries;
  664. struct dx_entry *old = frame->at, *new = old + 1;
  665. int count = dx_get_count(entries);
  666. assert(count < dx_get_limit(entries));
  667. assert(old < entries + count);
  668. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  669. dx_set_hash(new, hash);
  670. dx_set_block(new, block);
  671. dx_set_count(entries, count + 1);
  672. }
  673. #endif
  674. static void ext4_update_dx_flag(struct inode *inode)
  675. {
  676. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  677. EXT4_FEATURE_COMPAT_DIR_INDEX))
  678. EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
  679. }
  680. /*
  681. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  682. *
  683. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  684. * `de != NULL' is guaranteed by caller.
  685. */
  686. static inline int ext4_match (int len, const char * const name,
  687. struct ext4_dir_entry_2 * de)
  688. {
  689. if (len != de->name_len)
  690. return 0;
  691. if (!de->inode)
  692. return 0;
  693. return !memcmp(name, de->name, len);
  694. }
  695. /*
  696. * Returns 0 if not found, -1 on failure, and 1 on success
  697. */
  698. static inline int search_dirblock(struct buffer_head * bh,
  699. struct inode *dir,
  700. struct dentry *dentry,
  701. unsigned long offset,
  702. struct ext4_dir_entry_2 ** res_dir)
  703. {
  704. struct ext4_dir_entry_2 * de;
  705. char * dlimit;
  706. int de_len;
  707. const char *name = dentry->d_name.name;
  708. int namelen = dentry->d_name.len;
  709. de = (struct ext4_dir_entry_2 *) bh->b_data;
  710. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  711. while ((char *) de < dlimit) {
  712. /* this code is executed quadratically often */
  713. /* do minimal checking `by hand' */
  714. if ((char *) de + namelen <= dlimit &&
  715. ext4_match (namelen, name, de)) {
  716. /* found a match - just to be sure, do a full check */
  717. if (!ext4_check_dir_entry("ext4_find_entry",
  718. dir, de, bh, offset))
  719. return -1;
  720. *res_dir = de;
  721. return 1;
  722. }
  723. /* prevent looping on a bad block */
  724. de_len = le16_to_cpu(de->rec_len);
  725. if (de_len <= 0)
  726. return -1;
  727. offset += de_len;
  728. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  729. }
  730. return 0;
  731. }
  732. /*
  733. * ext4_find_entry()
  734. *
  735. * finds an entry in the specified directory with the wanted name. It
  736. * returns the cache buffer in which the entry was found, and the entry
  737. * itself (as a parameter - res_dir). It does NOT read the inode of the
  738. * entry - you'll have to do that yourself if you want to.
  739. *
  740. * The returned buffer_head has ->b_count elevated. The caller is expected
  741. * to brelse() it when appropriate.
  742. */
  743. static struct buffer_head * ext4_find_entry (struct dentry *dentry,
  744. struct ext4_dir_entry_2 ** res_dir)
  745. {
  746. struct super_block * sb;
  747. struct buffer_head * bh_use[NAMEI_RA_SIZE];
  748. struct buffer_head * bh, *ret = NULL;
  749. unsigned long start, block, b;
  750. int ra_max = 0; /* Number of bh's in the readahead
  751. buffer, bh_use[] */
  752. int ra_ptr = 0; /* Current index into readahead
  753. buffer */
  754. int num = 0;
  755. int nblocks, i, err;
  756. struct inode *dir = dentry->d_parent->d_inode;
  757. int namelen;
  758. const u8 *name;
  759. unsigned blocksize;
  760. *res_dir = NULL;
  761. sb = dir->i_sb;
  762. blocksize = sb->s_blocksize;
  763. namelen = dentry->d_name.len;
  764. name = dentry->d_name.name;
  765. if (namelen > EXT4_NAME_LEN)
  766. return NULL;
  767. #ifdef CONFIG_EXT4_INDEX
  768. if (is_dx(dir)) {
  769. bh = ext4_dx_find_entry(dentry, res_dir, &err);
  770. /*
  771. * On success, or if the error was file not found,
  772. * return. Otherwise, fall back to doing a search the
  773. * old fashioned way.
  774. */
  775. if (bh || (err != ERR_BAD_DX_DIR))
  776. return bh;
  777. dxtrace(printk("ext4_find_entry: dx failed, falling back\n"));
  778. }
  779. #endif
  780. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  781. start = EXT4_I(dir)->i_dir_start_lookup;
  782. if (start >= nblocks)
  783. start = 0;
  784. block = start;
  785. restart:
  786. do {
  787. /*
  788. * We deal with the read-ahead logic here.
  789. */
  790. if (ra_ptr >= ra_max) {
  791. /* Refill the readahead buffer */
  792. ra_ptr = 0;
  793. b = block;
  794. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  795. /*
  796. * Terminate if we reach the end of the
  797. * directory and must wrap, or if our
  798. * search has finished at this block.
  799. */
  800. if (b >= nblocks || (num && block == start)) {
  801. bh_use[ra_max] = NULL;
  802. break;
  803. }
  804. num++;
  805. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  806. bh_use[ra_max] = bh;
  807. if (bh)
  808. ll_rw_block(READ_META, 1, &bh);
  809. }
  810. }
  811. if ((bh = bh_use[ra_ptr++]) == NULL)
  812. goto next;
  813. wait_on_buffer(bh);
  814. if (!buffer_uptodate(bh)) {
  815. /* read error, skip block & hope for the best */
  816. ext4_error(sb, __FUNCTION__, "reading directory #%lu "
  817. "offset %lu", dir->i_ino, block);
  818. brelse(bh);
  819. goto next;
  820. }
  821. i = search_dirblock(bh, dir, dentry,
  822. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  823. if (i == 1) {
  824. EXT4_I(dir)->i_dir_start_lookup = block;
  825. ret = bh;
  826. goto cleanup_and_exit;
  827. } else {
  828. brelse(bh);
  829. if (i < 0)
  830. goto cleanup_and_exit;
  831. }
  832. next:
  833. if (++block >= nblocks)
  834. block = 0;
  835. } while (block != start);
  836. /*
  837. * If the directory has grown while we were searching, then
  838. * search the last part of the directory before giving up.
  839. */
  840. block = nblocks;
  841. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  842. if (block < nblocks) {
  843. start = 0;
  844. goto restart;
  845. }
  846. cleanup_and_exit:
  847. /* Clean up the read-ahead blocks */
  848. for (; ra_ptr < ra_max; ra_ptr++)
  849. brelse (bh_use[ra_ptr]);
  850. return ret;
  851. }
  852. #ifdef CONFIG_EXT4_INDEX
  853. static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry,
  854. struct ext4_dir_entry_2 **res_dir, int *err)
  855. {
  856. struct super_block * sb;
  857. struct dx_hash_info hinfo;
  858. u32 hash;
  859. struct dx_frame frames[2], *frame;
  860. struct ext4_dir_entry_2 *de, *top;
  861. struct buffer_head *bh;
  862. unsigned long block;
  863. int retval;
  864. int namelen = dentry->d_name.len;
  865. const u8 *name = dentry->d_name.name;
  866. struct inode *dir = dentry->d_parent->d_inode;
  867. sb = dir->i_sb;
  868. /* NFS may look up ".." - look at dx_root directory block */
  869. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  870. if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err)))
  871. return NULL;
  872. } else {
  873. frame = frames;
  874. frame->bh = NULL; /* for dx_release() */
  875. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  876. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  877. }
  878. hash = hinfo.hash;
  879. do {
  880. block = dx_get_block(frame->at);
  881. if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
  882. goto errout;
  883. de = (struct ext4_dir_entry_2 *) bh->b_data;
  884. top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  885. EXT4_DIR_REC_LEN(0));
  886. for (; de < top; de = ext4_next_entry(de))
  887. if (ext4_match (namelen, name, de)) {
  888. if (!ext4_check_dir_entry("ext4_find_entry",
  889. dir, de, bh,
  890. (block<<EXT4_BLOCK_SIZE_BITS(sb))
  891. +((char *)de - bh->b_data))) {
  892. brelse (bh);
  893. *err = ERR_BAD_DX_DIR;
  894. goto errout;
  895. }
  896. *res_dir = de;
  897. dx_release (frames);
  898. return bh;
  899. }
  900. brelse (bh);
  901. /* Check to see if we should continue to search */
  902. retval = ext4_htree_next_block(dir, hash, frame,
  903. frames, NULL);
  904. if (retval < 0) {
  905. ext4_warning(sb, __FUNCTION__,
  906. "error reading index page in directory #%lu",
  907. dir->i_ino);
  908. *err = retval;
  909. goto errout;
  910. }
  911. } while (retval == 1);
  912. *err = -ENOENT;
  913. errout:
  914. dxtrace(printk("%s not found\n", name));
  915. dx_release (frames);
  916. return NULL;
  917. }
  918. #endif
  919. static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  920. {
  921. struct inode * inode;
  922. struct ext4_dir_entry_2 * de;
  923. struct buffer_head * bh;
  924. if (dentry->d_name.len > EXT4_NAME_LEN)
  925. return ERR_PTR(-ENAMETOOLONG);
  926. bh = ext4_find_entry(dentry, &de);
  927. inode = NULL;
  928. if (bh) {
  929. unsigned long ino = le32_to_cpu(de->inode);
  930. brelse (bh);
  931. if (!ext4_valid_inum(dir->i_sb, ino)) {
  932. ext4_error(dir->i_sb, "ext4_lookup",
  933. "bad inode number: %lu", ino);
  934. inode = NULL;
  935. } else
  936. inode = iget(dir->i_sb, ino);
  937. if (!inode)
  938. return ERR_PTR(-EACCES);
  939. }
  940. return d_splice_alias(inode, dentry);
  941. }
  942. struct dentry *ext4_get_parent(struct dentry *child)
  943. {
  944. unsigned long ino;
  945. struct dentry *parent;
  946. struct inode *inode;
  947. struct dentry dotdot;
  948. struct ext4_dir_entry_2 * de;
  949. struct buffer_head *bh;
  950. dotdot.d_name.name = "..";
  951. dotdot.d_name.len = 2;
  952. dotdot.d_parent = child; /* confusing, isn't it! */
  953. bh = ext4_find_entry(&dotdot, &de);
  954. inode = NULL;
  955. if (!bh)
  956. return ERR_PTR(-ENOENT);
  957. ino = le32_to_cpu(de->inode);
  958. brelse(bh);
  959. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  960. ext4_error(child->d_inode->i_sb, "ext4_get_parent",
  961. "bad inode number: %lu", ino);
  962. inode = NULL;
  963. } else
  964. inode = iget(child->d_inode->i_sb, ino);
  965. if (!inode)
  966. return ERR_PTR(-EACCES);
  967. parent = d_alloc_anon(inode);
  968. if (!parent) {
  969. iput(inode);
  970. parent = ERR_PTR(-ENOMEM);
  971. }
  972. return parent;
  973. }
  974. #define S_SHIFT 12
  975. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  976. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  977. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  978. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  979. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  980. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  981. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  982. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  983. };
  984. static inline void ext4_set_de_type(struct super_block *sb,
  985. struct ext4_dir_entry_2 *de,
  986. umode_t mode) {
  987. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  988. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  989. }
  990. #ifdef CONFIG_EXT4_INDEX
  991. static struct ext4_dir_entry_2 *
  992. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
  993. {
  994. unsigned rec_len = 0;
  995. while (count--) {
  996. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
  997. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  998. memcpy (to, de, rec_len);
  999. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1000. cpu_to_le16(rec_len);
  1001. de->inode = 0;
  1002. map++;
  1003. to += rec_len;
  1004. }
  1005. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1006. }
  1007. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
  1008. {
  1009. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1010. unsigned rec_len = 0;
  1011. prev = to = de;
  1012. while ((char*)de < base + size) {
  1013. next = (struct ext4_dir_entry_2 *) ((char *) de +
  1014. le16_to_cpu(de->rec_len));
  1015. if (de->inode && de->name_len) {
  1016. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1017. if (de > to)
  1018. memmove(to, de, rec_len);
  1019. to->rec_len = cpu_to_le16(rec_len);
  1020. prev = to;
  1021. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1022. }
  1023. de = next;
  1024. }
  1025. return prev;
  1026. }
  1027. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1028. struct buffer_head **bh,struct dx_frame *frame,
  1029. struct dx_hash_info *hinfo, int *error)
  1030. {
  1031. unsigned blocksize = dir->i_sb->s_blocksize;
  1032. unsigned count, continued;
  1033. struct buffer_head *bh2;
  1034. u32 newblock;
  1035. u32 hash2;
  1036. struct dx_map_entry *map;
  1037. char *data1 = (*bh)->b_data, *data2;
  1038. unsigned split;
  1039. struct ext4_dir_entry_2 *de = NULL, *de2;
  1040. int err = 0;
  1041. bh2 = ext4_append (handle, dir, &newblock, &err);
  1042. if (!(bh2)) {
  1043. brelse(*bh);
  1044. *bh = NULL;
  1045. goto errout;
  1046. }
  1047. BUFFER_TRACE(*bh, "get_write_access");
  1048. err = ext4_journal_get_write_access(handle, *bh);
  1049. if (err)
  1050. goto journal_error;
  1051. BUFFER_TRACE(frame->bh, "get_write_access");
  1052. err = ext4_journal_get_write_access(handle, frame->bh);
  1053. if (err)
  1054. goto journal_error;
  1055. data2 = bh2->b_data;
  1056. /* create map in the end of data2 block */
  1057. map = (struct dx_map_entry *) (data2 + blocksize);
  1058. count = dx_make_map ((struct ext4_dir_entry_2 *) data1,
  1059. blocksize, hinfo, map);
  1060. map -= count;
  1061. split = count/2; // need to adjust to actual middle
  1062. dx_sort_map (map, count);
  1063. hash2 = map[split].hash;
  1064. continued = hash2 == map[split - 1].hash;
  1065. dxtrace(printk("Split block %i at %x, %i/%i\n",
  1066. dx_get_block(frame->at), hash2, split, count-split));
  1067. /* Fancy dance to stay within two buffers */
  1068. de2 = dx_move_dirents(data1, data2, map + split, count - split);
  1069. de = dx_pack_dirents(data1,blocksize);
  1070. de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
  1071. de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2);
  1072. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1073. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1074. /* Which block gets the new entry? */
  1075. if (hinfo->hash >= hash2)
  1076. {
  1077. swap(*bh, bh2);
  1078. de = de2;
  1079. }
  1080. dx_insert_block (frame, hash2 + continued, newblock);
  1081. err = ext4_journal_dirty_metadata (handle, bh2);
  1082. if (err)
  1083. goto journal_error;
  1084. err = ext4_journal_dirty_metadata (handle, frame->bh);
  1085. if (err)
  1086. goto journal_error;
  1087. brelse (bh2);
  1088. dxtrace(dx_show_index ("frame", frame->entries));
  1089. return de;
  1090. journal_error:
  1091. brelse(*bh);
  1092. brelse(bh2);
  1093. *bh = NULL;
  1094. ext4_std_error(dir->i_sb, err);
  1095. errout:
  1096. *error = err;
  1097. return NULL;
  1098. }
  1099. #endif
  1100. /*
  1101. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1102. * it points to a directory entry which is guaranteed to be large
  1103. * enough for new directory entry. If de is NULL, then
  1104. * add_dirent_to_buf will attempt search the directory block for
  1105. * space. It will return -ENOSPC if no space is available, and -EIO
  1106. * and -EEXIST if directory entry already exists.
  1107. *
  1108. * NOTE! bh is NOT released in the case where ENOSPC is returned. In
  1109. * all other cases bh is released.
  1110. */
  1111. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1112. struct inode *inode, struct ext4_dir_entry_2 *de,
  1113. struct buffer_head * bh)
  1114. {
  1115. struct inode *dir = dentry->d_parent->d_inode;
  1116. const char *name = dentry->d_name.name;
  1117. int namelen = dentry->d_name.len;
  1118. unsigned long offset = 0;
  1119. unsigned short reclen;
  1120. int nlen, rlen, err;
  1121. char *top;
  1122. reclen = EXT4_DIR_REC_LEN(namelen);
  1123. if (!de) {
  1124. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1125. top = bh->b_data + dir->i_sb->s_blocksize - reclen;
  1126. while ((char *) de <= top) {
  1127. if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
  1128. bh, offset)) {
  1129. brelse (bh);
  1130. return -EIO;
  1131. }
  1132. if (ext4_match (namelen, name, de)) {
  1133. brelse (bh);
  1134. return -EEXIST;
  1135. }
  1136. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1137. rlen = le16_to_cpu(de->rec_len);
  1138. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1139. break;
  1140. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1141. offset += rlen;
  1142. }
  1143. if ((char *) de > top)
  1144. return -ENOSPC;
  1145. }
  1146. BUFFER_TRACE(bh, "get_write_access");
  1147. err = ext4_journal_get_write_access(handle, bh);
  1148. if (err) {
  1149. ext4_std_error(dir->i_sb, err);
  1150. brelse(bh);
  1151. return err;
  1152. }
  1153. /* By now the buffer is marked for journaling */
  1154. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1155. rlen = le16_to_cpu(de->rec_len);
  1156. if (de->inode) {
  1157. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1158. de1->rec_len = cpu_to_le16(rlen - nlen);
  1159. de->rec_len = cpu_to_le16(nlen);
  1160. de = de1;
  1161. }
  1162. de->file_type = EXT4_FT_UNKNOWN;
  1163. if (inode) {
  1164. de->inode = cpu_to_le32(inode->i_ino);
  1165. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1166. } else
  1167. de->inode = 0;
  1168. de->name_len = namelen;
  1169. memcpy (de->name, name, namelen);
  1170. /*
  1171. * XXX shouldn't update any times until successful
  1172. * completion of syscall, but too many callers depend
  1173. * on this.
  1174. *
  1175. * XXX similarly, too many callers depend on
  1176. * ext4_new_inode() setting the times, but error
  1177. * recovery deletes the inode, so the worst that can
  1178. * happen is that the times are slightly out of date
  1179. * and/or different from the directory change time.
  1180. */
  1181. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  1182. ext4_update_dx_flag(dir);
  1183. dir->i_version++;
  1184. ext4_mark_inode_dirty(handle, dir);
  1185. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1186. err = ext4_journal_dirty_metadata(handle, bh);
  1187. if (err)
  1188. ext4_std_error(dir->i_sb, err);
  1189. brelse(bh);
  1190. return 0;
  1191. }
  1192. #ifdef CONFIG_EXT4_INDEX
  1193. /*
  1194. * This converts a one block unindexed directory to a 3 block indexed
  1195. * directory, and adds the dentry to the indexed directory.
  1196. */
  1197. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1198. struct inode *inode, struct buffer_head *bh)
  1199. {
  1200. struct inode *dir = dentry->d_parent->d_inode;
  1201. const char *name = dentry->d_name.name;
  1202. int namelen = dentry->d_name.len;
  1203. struct buffer_head *bh2;
  1204. struct dx_root *root;
  1205. struct dx_frame frames[2], *frame;
  1206. struct dx_entry *entries;
  1207. struct ext4_dir_entry_2 *de, *de2;
  1208. char *data1, *top;
  1209. unsigned len;
  1210. int retval;
  1211. unsigned blocksize;
  1212. struct dx_hash_info hinfo;
  1213. u32 block;
  1214. struct fake_dirent *fde;
  1215. blocksize = dir->i_sb->s_blocksize;
  1216. dxtrace(printk("Creating index\n"));
  1217. retval = ext4_journal_get_write_access(handle, bh);
  1218. if (retval) {
  1219. ext4_std_error(dir->i_sb, retval);
  1220. brelse(bh);
  1221. return retval;
  1222. }
  1223. root = (struct dx_root *) bh->b_data;
  1224. bh2 = ext4_append (handle, dir, &block, &retval);
  1225. if (!(bh2)) {
  1226. brelse(bh);
  1227. return retval;
  1228. }
  1229. EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
  1230. data1 = bh2->b_data;
  1231. /* The 0th block becomes the root, move the dirents out */
  1232. fde = &root->dotdot;
  1233. de = (struct ext4_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len));
  1234. len = ((char *) root) + blocksize - (char *) de;
  1235. memcpy (data1, de, len);
  1236. de = (struct ext4_dir_entry_2 *) data1;
  1237. top = data1 + len;
  1238. while ((char *)(de2=(void*)de+le16_to_cpu(de->rec_len)) < top)
  1239. de = de2;
  1240. de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de);
  1241. /* Initialize the root; the dot dirents already exist */
  1242. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1243. de->rec_len = cpu_to_le16(blocksize - EXT4_DIR_REC_LEN(2));
  1244. memset (&root->info, 0, sizeof(root->info));
  1245. root->info.info_length = sizeof(root->info);
  1246. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1247. entries = root->entries;
  1248. dx_set_block (entries, 1);
  1249. dx_set_count (entries, 1);
  1250. dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info)));
  1251. /* Initialize as for dx_probe */
  1252. hinfo.hash_version = root->info.hash_version;
  1253. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1254. ext4fs_dirhash(name, namelen, &hinfo);
  1255. frame = frames;
  1256. frame->entries = entries;
  1257. frame->at = entries;
  1258. frame->bh = bh;
  1259. bh = bh2;
  1260. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1261. dx_release (frames);
  1262. if (!(de))
  1263. return retval;
  1264. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1265. }
  1266. #endif
  1267. /*
  1268. * ext4_add_entry()
  1269. *
  1270. * adds a file entry to the specified directory, using the same
  1271. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1272. *
  1273. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1274. * may not sleep between calling this and putting something into
  1275. * the entry, as someone else might have used it while you slept.
  1276. */
  1277. static int ext4_add_entry (handle_t *handle, struct dentry *dentry,
  1278. struct inode *inode)
  1279. {
  1280. struct inode *dir = dentry->d_parent->d_inode;
  1281. unsigned long offset;
  1282. struct buffer_head * bh;
  1283. struct ext4_dir_entry_2 *de;
  1284. struct super_block * sb;
  1285. int retval;
  1286. #ifdef CONFIG_EXT4_INDEX
  1287. int dx_fallback=0;
  1288. #endif
  1289. unsigned blocksize;
  1290. u32 block, blocks;
  1291. sb = dir->i_sb;
  1292. blocksize = sb->s_blocksize;
  1293. if (!dentry->d_name.len)
  1294. return -EINVAL;
  1295. #ifdef CONFIG_EXT4_INDEX
  1296. if (is_dx(dir)) {
  1297. retval = ext4_dx_add_entry(handle, dentry, inode);
  1298. if (!retval || (retval != ERR_BAD_DX_DIR))
  1299. return retval;
  1300. EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
  1301. dx_fallback++;
  1302. ext4_mark_inode_dirty(handle, dir);
  1303. }
  1304. #endif
  1305. blocks = dir->i_size >> sb->s_blocksize_bits;
  1306. for (block = 0, offset = 0; block < blocks; block++) {
  1307. bh = ext4_bread(handle, dir, block, 0, &retval);
  1308. if(!bh)
  1309. return retval;
  1310. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1311. if (retval != -ENOSPC)
  1312. return retval;
  1313. #ifdef CONFIG_EXT4_INDEX
  1314. if (blocks == 1 && !dx_fallback &&
  1315. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1316. return make_indexed_dir(handle, dentry, inode, bh);
  1317. #endif
  1318. brelse(bh);
  1319. }
  1320. bh = ext4_append(handle, dir, &block, &retval);
  1321. if (!bh)
  1322. return retval;
  1323. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1324. de->inode = 0;
  1325. de->rec_len = cpu_to_le16(blocksize);
  1326. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1327. }
  1328. #ifdef CONFIG_EXT4_INDEX
  1329. /*
  1330. * Returns 0 for success, or a negative error value
  1331. */
  1332. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1333. struct inode *inode)
  1334. {
  1335. struct dx_frame frames[2], *frame;
  1336. struct dx_entry *entries, *at;
  1337. struct dx_hash_info hinfo;
  1338. struct buffer_head * bh;
  1339. struct inode *dir = dentry->d_parent->d_inode;
  1340. struct super_block * sb = dir->i_sb;
  1341. struct ext4_dir_entry_2 *de;
  1342. int err;
  1343. frame = dx_probe(dentry, NULL, &hinfo, frames, &err);
  1344. if (!frame)
  1345. return err;
  1346. entries = frame->entries;
  1347. at = frame->at;
  1348. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1349. goto cleanup;
  1350. BUFFER_TRACE(bh, "get_write_access");
  1351. err = ext4_journal_get_write_access(handle, bh);
  1352. if (err)
  1353. goto journal_error;
  1354. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1355. if (err != -ENOSPC) {
  1356. bh = NULL;
  1357. goto cleanup;
  1358. }
  1359. /* Block full, should compress but for now just split */
  1360. dxtrace(printk("using %u of %u node entries\n",
  1361. dx_get_count(entries), dx_get_limit(entries)));
  1362. /* Need to split index? */
  1363. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1364. u32 newblock;
  1365. unsigned icount = dx_get_count(entries);
  1366. int levels = frame - frames;
  1367. struct dx_entry *entries2;
  1368. struct dx_node *node2;
  1369. struct buffer_head *bh2;
  1370. if (levels && (dx_get_count(frames->entries) ==
  1371. dx_get_limit(frames->entries))) {
  1372. ext4_warning(sb, __FUNCTION__,
  1373. "Directory index full!");
  1374. err = -ENOSPC;
  1375. goto cleanup;
  1376. }
  1377. bh2 = ext4_append (handle, dir, &newblock, &err);
  1378. if (!(bh2))
  1379. goto cleanup;
  1380. node2 = (struct dx_node *)(bh2->b_data);
  1381. entries2 = node2->entries;
  1382. node2->fake.rec_len = cpu_to_le16(sb->s_blocksize);
  1383. node2->fake.inode = 0;
  1384. BUFFER_TRACE(frame->bh, "get_write_access");
  1385. err = ext4_journal_get_write_access(handle, frame->bh);
  1386. if (err)
  1387. goto journal_error;
  1388. if (levels) {
  1389. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1390. unsigned hash2 = dx_get_hash(entries + icount1);
  1391. dxtrace(printk("Split index %i/%i\n", icount1, icount2));
  1392. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1393. err = ext4_journal_get_write_access(handle,
  1394. frames[0].bh);
  1395. if (err)
  1396. goto journal_error;
  1397. memcpy ((char *) entries2, (char *) (entries + icount1),
  1398. icount2 * sizeof(struct dx_entry));
  1399. dx_set_count (entries, icount1);
  1400. dx_set_count (entries2, icount2);
  1401. dx_set_limit (entries2, dx_node_limit(dir));
  1402. /* Which index block gets the new entry? */
  1403. if (at - entries >= icount1) {
  1404. frame->at = at = at - entries - icount1 + entries2;
  1405. frame->entries = entries = entries2;
  1406. swap(frame->bh, bh2);
  1407. }
  1408. dx_insert_block (frames + 0, hash2, newblock);
  1409. dxtrace(dx_show_index ("node", frames[1].entries));
  1410. dxtrace(dx_show_index ("node",
  1411. ((struct dx_node *) bh2->b_data)->entries));
  1412. err = ext4_journal_dirty_metadata(handle, bh2);
  1413. if (err)
  1414. goto journal_error;
  1415. brelse (bh2);
  1416. } else {
  1417. dxtrace(printk("Creating second level index...\n"));
  1418. memcpy((char *) entries2, (char *) entries,
  1419. icount * sizeof(struct dx_entry));
  1420. dx_set_limit(entries2, dx_node_limit(dir));
  1421. /* Set up root */
  1422. dx_set_count(entries, 1);
  1423. dx_set_block(entries + 0, newblock);
  1424. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1425. /* Add new access path frame */
  1426. frame = frames + 1;
  1427. frame->at = at = at - entries + entries2;
  1428. frame->entries = entries = entries2;
  1429. frame->bh = bh2;
  1430. err = ext4_journal_get_write_access(handle,
  1431. frame->bh);
  1432. if (err)
  1433. goto journal_error;
  1434. }
  1435. ext4_journal_dirty_metadata(handle, frames[0].bh);
  1436. }
  1437. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1438. if (!de)
  1439. goto cleanup;
  1440. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1441. bh = NULL;
  1442. goto cleanup;
  1443. journal_error:
  1444. ext4_std_error(dir->i_sb, err);
  1445. cleanup:
  1446. if (bh)
  1447. brelse(bh);
  1448. dx_release(frames);
  1449. return err;
  1450. }
  1451. #endif
  1452. /*
  1453. * ext4_delete_entry deletes a directory entry by merging it with the
  1454. * previous entry
  1455. */
  1456. static int ext4_delete_entry (handle_t *handle,
  1457. struct inode * dir,
  1458. struct ext4_dir_entry_2 * de_del,
  1459. struct buffer_head * bh)
  1460. {
  1461. struct ext4_dir_entry_2 * de, * pde;
  1462. int i;
  1463. i = 0;
  1464. pde = NULL;
  1465. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1466. while (i < bh->b_size) {
  1467. if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
  1468. return -EIO;
  1469. if (de == de_del) {
  1470. BUFFER_TRACE(bh, "get_write_access");
  1471. ext4_journal_get_write_access(handle, bh);
  1472. if (pde)
  1473. pde->rec_len =
  1474. cpu_to_le16(le16_to_cpu(pde->rec_len) +
  1475. le16_to_cpu(de->rec_len));
  1476. else
  1477. de->inode = 0;
  1478. dir->i_version++;
  1479. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1480. ext4_journal_dirty_metadata(handle, bh);
  1481. return 0;
  1482. }
  1483. i += le16_to_cpu(de->rec_len);
  1484. pde = de;
  1485. de = (struct ext4_dir_entry_2 *)
  1486. ((char *) de + le16_to_cpu(de->rec_len));
  1487. }
  1488. return -ENOENT;
  1489. }
  1490. static int ext4_add_nondir(handle_t *handle,
  1491. struct dentry *dentry, struct inode *inode)
  1492. {
  1493. int err = ext4_add_entry(handle, dentry, inode);
  1494. if (!err) {
  1495. ext4_mark_inode_dirty(handle, inode);
  1496. d_instantiate(dentry, inode);
  1497. return 0;
  1498. }
  1499. drop_nlink(inode);
  1500. iput(inode);
  1501. return err;
  1502. }
  1503. /*
  1504. * By the time this is called, we already have created
  1505. * the directory cache entry for the new file, but it
  1506. * is so far negative - it has no inode.
  1507. *
  1508. * If the create succeeds, we fill in the inode information
  1509. * with d_instantiate().
  1510. */
  1511. static int ext4_create (struct inode * dir, struct dentry * dentry, int mode,
  1512. struct nameidata *nd)
  1513. {
  1514. handle_t *handle;
  1515. struct inode * inode;
  1516. int err, retries = 0;
  1517. retry:
  1518. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1519. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1520. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1521. if (IS_ERR(handle))
  1522. return PTR_ERR(handle);
  1523. if (IS_DIRSYNC(dir))
  1524. handle->h_sync = 1;
  1525. inode = ext4_new_inode (handle, dir, mode);
  1526. err = PTR_ERR(inode);
  1527. if (!IS_ERR(inode)) {
  1528. inode->i_op = &ext4_file_inode_operations;
  1529. inode->i_fop = &ext4_file_operations;
  1530. ext4_set_aops(inode);
  1531. err = ext4_add_nondir(handle, dentry, inode);
  1532. }
  1533. ext4_journal_stop(handle);
  1534. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1535. goto retry;
  1536. return err;
  1537. }
  1538. static int ext4_mknod (struct inode * dir, struct dentry *dentry,
  1539. int mode, dev_t rdev)
  1540. {
  1541. handle_t *handle;
  1542. struct inode *inode;
  1543. int err, retries = 0;
  1544. if (!new_valid_dev(rdev))
  1545. return -EINVAL;
  1546. retry:
  1547. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1548. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1549. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1550. if (IS_ERR(handle))
  1551. return PTR_ERR(handle);
  1552. if (IS_DIRSYNC(dir))
  1553. handle->h_sync = 1;
  1554. inode = ext4_new_inode (handle, dir, mode);
  1555. err = PTR_ERR(inode);
  1556. if (!IS_ERR(inode)) {
  1557. init_special_inode(inode, inode->i_mode, rdev);
  1558. #ifdef CONFIG_EXT4DEV_FS_XATTR
  1559. inode->i_op = &ext4_special_inode_operations;
  1560. #endif
  1561. err = ext4_add_nondir(handle, dentry, inode);
  1562. }
  1563. ext4_journal_stop(handle);
  1564. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1565. goto retry;
  1566. return err;
  1567. }
  1568. static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode)
  1569. {
  1570. handle_t *handle;
  1571. struct inode * inode;
  1572. struct buffer_head * dir_block;
  1573. struct ext4_dir_entry_2 * de;
  1574. int err, retries = 0;
  1575. if (dir->i_nlink >= EXT4_LINK_MAX)
  1576. return -EMLINK;
  1577. retry:
  1578. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1579. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1580. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1581. if (IS_ERR(handle))
  1582. return PTR_ERR(handle);
  1583. if (IS_DIRSYNC(dir))
  1584. handle->h_sync = 1;
  1585. inode = ext4_new_inode (handle, dir, S_IFDIR | mode);
  1586. err = PTR_ERR(inode);
  1587. if (IS_ERR(inode))
  1588. goto out_stop;
  1589. inode->i_op = &ext4_dir_inode_operations;
  1590. inode->i_fop = &ext4_dir_operations;
  1591. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1592. dir_block = ext4_bread (handle, inode, 0, 1, &err);
  1593. if (!dir_block) {
  1594. drop_nlink(inode); /* is this nlink == 0? */
  1595. ext4_mark_inode_dirty(handle, inode);
  1596. iput (inode);
  1597. goto out_stop;
  1598. }
  1599. BUFFER_TRACE(dir_block, "get_write_access");
  1600. ext4_journal_get_write_access(handle, dir_block);
  1601. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1602. de->inode = cpu_to_le32(inode->i_ino);
  1603. de->name_len = 1;
  1604. de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(de->name_len));
  1605. strcpy (de->name, ".");
  1606. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1607. de = (struct ext4_dir_entry_2 *)
  1608. ((char *) de + le16_to_cpu(de->rec_len));
  1609. de->inode = cpu_to_le32(dir->i_ino);
  1610. de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT4_DIR_REC_LEN(1));
  1611. de->name_len = 2;
  1612. strcpy (de->name, "..");
  1613. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1614. inode->i_nlink = 2;
  1615. BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata");
  1616. ext4_journal_dirty_metadata(handle, dir_block);
  1617. brelse (dir_block);
  1618. ext4_mark_inode_dirty(handle, inode);
  1619. err = ext4_add_entry (handle, dentry, inode);
  1620. if (err) {
  1621. inode->i_nlink = 0;
  1622. ext4_mark_inode_dirty(handle, inode);
  1623. iput (inode);
  1624. goto out_stop;
  1625. }
  1626. inc_nlink(dir);
  1627. ext4_update_dx_flag(dir);
  1628. ext4_mark_inode_dirty(handle, dir);
  1629. d_instantiate(dentry, inode);
  1630. out_stop:
  1631. ext4_journal_stop(handle);
  1632. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1633. goto retry;
  1634. return err;
  1635. }
  1636. /*
  1637. * routine to check that the specified directory is empty (for rmdir)
  1638. */
  1639. static int empty_dir (struct inode * inode)
  1640. {
  1641. unsigned long offset;
  1642. struct buffer_head * bh;
  1643. struct ext4_dir_entry_2 * de, * de1;
  1644. struct super_block * sb;
  1645. int err = 0;
  1646. sb = inode->i_sb;
  1647. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1648. !(bh = ext4_bread (NULL, inode, 0, 0, &err))) {
  1649. if (err)
  1650. ext4_error(inode->i_sb, __FUNCTION__,
  1651. "error %d reading directory #%lu offset 0",
  1652. err, inode->i_ino);
  1653. else
  1654. ext4_warning(inode->i_sb, __FUNCTION__,
  1655. "bad directory (dir #%lu) - no data block",
  1656. inode->i_ino);
  1657. return 1;
  1658. }
  1659. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1660. de1 = (struct ext4_dir_entry_2 *)
  1661. ((char *) de + le16_to_cpu(de->rec_len));
  1662. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1663. !le32_to_cpu(de1->inode) ||
  1664. strcmp (".", de->name) ||
  1665. strcmp ("..", de1->name)) {
  1666. ext4_warning (inode->i_sb, "empty_dir",
  1667. "bad directory (dir #%lu) - no `.' or `..'",
  1668. inode->i_ino);
  1669. brelse (bh);
  1670. return 1;
  1671. }
  1672. offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
  1673. de = (struct ext4_dir_entry_2 *)
  1674. ((char *) de1 + le16_to_cpu(de1->rec_len));
  1675. while (offset < inode->i_size ) {
  1676. if (!bh ||
  1677. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1678. err = 0;
  1679. brelse (bh);
  1680. bh = ext4_bread (NULL, inode,
  1681. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1682. if (!bh) {
  1683. if (err)
  1684. ext4_error(sb, __FUNCTION__,
  1685. "error %d reading directory"
  1686. " #%lu offset %lu",
  1687. err, inode->i_ino, offset);
  1688. offset += sb->s_blocksize;
  1689. continue;
  1690. }
  1691. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1692. }
  1693. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1694. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1695. sb->s_blocksize);
  1696. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1697. continue;
  1698. }
  1699. if (le32_to_cpu(de->inode)) {
  1700. brelse (bh);
  1701. return 0;
  1702. }
  1703. offset += le16_to_cpu(de->rec_len);
  1704. de = (struct ext4_dir_entry_2 *)
  1705. ((char *) de + le16_to_cpu(de->rec_len));
  1706. }
  1707. brelse (bh);
  1708. return 1;
  1709. }
  1710. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1711. * such inodes, starting at the superblock, in case we crash before the
  1712. * file is closed/deleted, or in case the inode truncate spans multiple
  1713. * transactions and the last transaction is not recovered after a crash.
  1714. *
  1715. * At filesystem recovery time, we walk this list deleting unlinked
  1716. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1717. */
  1718. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1719. {
  1720. struct super_block *sb = inode->i_sb;
  1721. struct ext4_iloc iloc;
  1722. int err = 0, rc;
  1723. lock_super(sb);
  1724. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1725. goto out_unlock;
  1726. /* Orphan handling is only valid for files with data blocks
  1727. * being truncated, or files being unlinked. */
  1728. /* @@@ FIXME: Observation from aviro:
  1729. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1730. * here (on lock_super()), so race with ext4_link() which might bump
  1731. * ->i_nlink. For, say it, character device. Not a regular file,
  1732. * not a directory, not a symlink and ->i_nlink > 0.
  1733. */
  1734. J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1735. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1736. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1737. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1738. if (err)
  1739. goto out_unlock;
  1740. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1741. if (err)
  1742. goto out_unlock;
  1743. /* Insert this inode at the head of the on-disk orphan list... */
  1744. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1745. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1746. err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  1747. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1748. if (!err)
  1749. err = rc;
  1750. /* Only add to the head of the in-memory list if all the
  1751. * previous operations succeeded. If the orphan_add is going to
  1752. * fail (possibly taking the journal offline), we can't risk
  1753. * leaving the inode on the orphan list: stray orphan-list
  1754. * entries can cause panics at unmount time.
  1755. *
  1756. * This is safe: on error we're going to ignore the orphan list
  1757. * anyway on the next recovery. */
  1758. if (!err)
  1759. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1760. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1761. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1762. inode->i_ino, NEXT_ORPHAN(inode));
  1763. out_unlock:
  1764. unlock_super(sb);
  1765. ext4_std_error(inode->i_sb, err);
  1766. return err;
  1767. }
  1768. /*
  1769. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1770. * of such inodes stored on disk, because it is finally being cleaned up.
  1771. */
  1772. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1773. {
  1774. struct list_head *prev;
  1775. struct ext4_inode_info *ei = EXT4_I(inode);
  1776. struct ext4_sb_info *sbi;
  1777. unsigned long ino_next;
  1778. struct ext4_iloc iloc;
  1779. int err = 0;
  1780. lock_super(inode->i_sb);
  1781. if (list_empty(&ei->i_orphan)) {
  1782. unlock_super(inode->i_sb);
  1783. return 0;
  1784. }
  1785. ino_next = NEXT_ORPHAN(inode);
  1786. prev = ei->i_orphan.prev;
  1787. sbi = EXT4_SB(inode->i_sb);
  1788. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1789. list_del_init(&ei->i_orphan);
  1790. /* If we're on an error path, we may not have a valid
  1791. * transaction handle with which to update the orphan list on
  1792. * disk, but we still need to remove the inode from the linked
  1793. * list in memory. */
  1794. if (!handle)
  1795. goto out;
  1796. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1797. if (err)
  1798. goto out_err;
  1799. if (prev == &sbi->s_orphan) {
  1800. jbd_debug(4, "superblock will point to %lu\n", ino_next);
  1801. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1802. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1803. if (err)
  1804. goto out_brelse;
  1805. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1806. err = ext4_journal_dirty_metadata(handle, sbi->s_sbh);
  1807. } else {
  1808. struct ext4_iloc iloc2;
  1809. struct inode *i_prev =
  1810. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1811. jbd_debug(4, "orphan inode %lu will point to %lu\n",
  1812. i_prev->i_ino, ino_next);
  1813. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1814. if (err)
  1815. goto out_brelse;
  1816. NEXT_ORPHAN(i_prev) = ino_next;
  1817. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1818. }
  1819. if (err)
  1820. goto out_brelse;
  1821. NEXT_ORPHAN(inode) = 0;
  1822. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1823. out_err:
  1824. ext4_std_error(inode->i_sb, err);
  1825. out:
  1826. unlock_super(inode->i_sb);
  1827. return err;
  1828. out_brelse:
  1829. brelse(iloc.bh);
  1830. goto out_err;
  1831. }
  1832. static int ext4_rmdir (struct inode * dir, struct dentry *dentry)
  1833. {
  1834. int retval;
  1835. struct inode * inode;
  1836. struct buffer_head * bh;
  1837. struct ext4_dir_entry_2 * de;
  1838. handle_t *handle;
  1839. /* Initialize quotas before so that eventual writes go in
  1840. * separate transaction */
  1841. DQUOT_INIT(dentry->d_inode);
  1842. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1843. if (IS_ERR(handle))
  1844. return PTR_ERR(handle);
  1845. retval = -ENOENT;
  1846. bh = ext4_find_entry (dentry, &de);
  1847. if (!bh)
  1848. goto end_rmdir;
  1849. if (IS_DIRSYNC(dir))
  1850. handle->h_sync = 1;
  1851. inode = dentry->d_inode;
  1852. retval = -EIO;
  1853. if (le32_to_cpu(de->inode) != inode->i_ino)
  1854. goto end_rmdir;
  1855. retval = -ENOTEMPTY;
  1856. if (!empty_dir (inode))
  1857. goto end_rmdir;
  1858. retval = ext4_delete_entry(handle, dir, de, bh);
  1859. if (retval)
  1860. goto end_rmdir;
  1861. if (inode->i_nlink != 2)
  1862. ext4_warning (inode->i_sb, "ext4_rmdir",
  1863. "empty directory has nlink!=2 (%d)",
  1864. inode->i_nlink);
  1865. inode->i_version++;
  1866. clear_nlink(inode);
  1867. /* There's no need to set i_disksize: the fact that i_nlink is
  1868. * zero will ensure that the right thing happens during any
  1869. * recovery. */
  1870. inode->i_size = 0;
  1871. ext4_orphan_add(handle, inode);
  1872. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
  1873. ext4_mark_inode_dirty(handle, inode);
  1874. drop_nlink(dir);
  1875. ext4_update_dx_flag(dir);
  1876. ext4_mark_inode_dirty(handle, dir);
  1877. end_rmdir:
  1878. ext4_journal_stop(handle);
  1879. brelse (bh);
  1880. return retval;
  1881. }
  1882. static int ext4_unlink(struct inode * dir, struct dentry *dentry)
  1883. {
  1884. int retval;
  1885. struct inode * inode;
  1886. struct buffer_head * bh;
  1887. struct ext4_dir_entry_2 * de;
  1888. handle_t *handle;
  1889. /* Initialize quotas before so that eventual writes go
  1890. * in separate transaction */
  1891. DQUOT_INIT(dentry->d_inode);
  1892. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1893. if (IS_ERR(handle))
  1894. return PTR_ERR(handle);
  1895. if (IS_DIRSYNC(dir))
  1896. handle->h_sync = 1;
  1897. retval = -ENOENT;
  1898. bh = ext4_find_entry (dentry, &de);
  1899. if (!bh)
  1900. goto end_unlink;
  1901. inode = dentry->d_inode;
  1902. retval = -EIO;
  1903. if (le32_to_cpu(de->inode) != inode->i_ino)
  1904. goto end_unlink;
  1905. if (!inode->i_nlink) {
  1906. ext4_warning (inode->i_sb, "ext4_unlink",
  1907. "Deleting nonexistent file (%lu), %d",
  1908. inode->i_ino, inode->i_nlink);
  1909. inode->i_nlink = 1;
  1910. }
  1911. retval = ext4_delete_entry(handle, dir, de, bh);
  1912. if (retval)
  1913. goto end_unlink;
  1914. dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
  1915. ext4_update_dx_flag(dir);
  1916. ext4_mark_inode_dirty(handle, dir);
  1917. drop_nlink(inode);
  1918. if (!inode->i_nlink)
  1919. ext4_orphan_add(handle, inode);
  1920. inode->i_ctime = dir->i_ctime;
  1921. ext4_mark_inode_dirty(handle, inode);
  1922. retval = 0;
  1923. end_unlink:
  1924. ext4_journal_stop(handle);
  1925. brelse (bh);
  1926. return retval;
  1927. }
  1928. static int ext4_symlink (struct inode * dir,
  1929. struct dentry *dentry, const char * symname)
  1930. {
  1931. handle_t *handle;
  1932. struct inode * inode;
  1933. int l, err, retries = 0;
  1934. l = strlen(symname)+1;
  1935. if (l > dir->i_sb->s_blocksize)
  1936. return -ENAMETOOLONG;
  1937. retry:
  1938. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1939. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  1940. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1941. if (IS_ERR(handle))
  1942. return PTR_ERR(handle);
  1943. if (IS_DIRSYNC(dir))
  1944. handle->h_sync = 1;
  1945. inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO);
  1946. err = PTR_ERR(inode);
  1947. if (IS_ERR(inode))
  1948. goto out_stop;
  1949. if (l > sizeof (EXT4_I(inode)->i_data)) {
  1950. inode->i_op = &ext4_symlink_inode_operations;
  1951. ext4_set_aops(inode);
  1952. /*
  1953. * page_symlink() calls into ext4_prepare/commit_write.
  1954. * We have a transaction open. All is sweetness. It also sets
  1955. * i_size in generic_commit_write().
  1956. */
  1957. err = __page_symlink(inode, symname, l,
  1958. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  1959. if (err) {
  1960. drop_nlink(inode);
  1961. ext4_mark_inode_dirty(handle, inode);
  1962. iput (inode);
  1963. goto out_stop;
  1964. }
  1965. } else {
  1966. inode->i_op = &ext4_fast_symlink_inode_operations;
  1967. memcpy((char*)&EXT4_I(inode)->i_data,symname,l);
  1968. inode->i_size = l-1;
  1969. }
  1970. EXT4_I(inode)->i_disksize = inode->i_size;
  1971. err = ext4_add_nondir(handle, dentry, inode);
  1972. out_stop:
  1973. ext4_journal_stop(handle);
  1974. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1975. goto retry;
  1976. return err;
  1977. }
  1978. static int ext4_link (struct dentry * old_dentry,
  1979. struct inode * dir, struct dentry *dentry)
  1980. {
  1981. handle_t *handle;
  1982. struct inode *inode = old_dentry->d_inode;
  1983. int err, retries = 0;
  1984. if (inode->i_nlink >= EXT4_LINK_MAX)
  1985. return -EMLINK;
  1986. /*
  1987. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  1988. * otherwise has the potential to corrupt the orphan inode list.
  1989. */
  1990. if (inode->i_nlink == 0)
  1991. return -ENOENT;
  1992. retry:
  1993. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1994. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  1995. if (IS_ERR(handle))
  1996. return PTR_ERR(handle);
  1997. if (IS_DIRSYNC(dir))
  1998. handle->h_sync = 1;
  1999. inode->i_ctime = CURRENT_TIME_SEC;
  2000. inc_nlink(inode);
  2001. atomic_inc(&inode->i_count);
  2002. err = ext4_add_nondir(handle, dentry, inode);
  2003. ext4_journal_stop(handle);
  2004. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2005. goto retry;
  2006. return err;
  2007. }
  2008. #define PARENT_INO(buffer) \
  2009. ((struct ext4_dir_entry_2 *) ((char *) buffer + \
  2010. le16_to_cpu(((struct ext4_dir_entry_2 *) buffer)->rec_len)))->inode
  2011. /*
  2012. * Anybody can rename anything with this: the permission checks are left to the
  2013. * higher-level routines.
  2014. */
  2015. static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry,
  2016. struct inode * new_dir,struct dentry *new_dentry)
  2017. {
  2018. handle_t *handle;
  2019. struct inode * old_inode, * new_inode;
  2020. struct buffer_head * old_bh, * new_bh, * dir_bh;
  2021. struct ext4_dir_entry_2 * old_de, * new_de;
  2022. int retval;
  2023. old_bh = new_bh = dir_bh = NULL;
  2024. /* Initialize quotas before so that eventual writes go
  2025. * in separate transaction */
  2026. if (new_dentry->d_inode)
  2027. DQUOT_INIT(new_dentry->d_inode);
  2028. handle = ext4_journal_start(old_dir, 2 *
  2029. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2030. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2031. if (IS_ERR(handle))
  2032. return PTR_ERR(handle);
  2033. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2034. handle->h_sync = 1;
  2035. old_bh = ext4_find_entry (old_dentry, &old_de);
  2036. /*
  2037. * Check for inode number is _not_ due to possible IO errors.
  2038. * We might rmdir the source, keep it as pwd of some process
  2039. * and merrily kill the link to whatever was created under the
  2040. * same name. Goodbye sticky bit ;-<
  2041. */
  2042. old_inode = old_dentry->d_inode;
  2043. retval = -ENOENT;
  2044. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2045. goto end_rename;
  2046. new_inode = new_dentry->d_inode;
  2047. new_bh = ext4_find_entry (new_dentry, &new_de);
  2048. if (new_bh) {
  2049. if (!new_inode) {
  2050. brelse (new_bh);
  2051. new_bh = NULL;
  2052. }
  2053. }
  2054. if (S_ISDIR(old_inode->i_mode)) {
  2055. if (new_inode) {
  2056. retval = -ENOTEMPTY;
  2057. if (!empty_dir (new_inode))
  2058. goto end_rename;
  2059. }
  2060. retval = -EIO;
  2061. dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval);
  2062. if (!dir_bh)
  2063. goto end_rename;
  2064. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2065. goto end_rename;
  2066. retval = -EMLINK;
  2067. if (!new_inode && new_dir!=old_dir &&
  2068. new_dir->i_nlink >= EXT4_LINK_MAX)
  2069. goto end_rename;
  2070. }
  2071. if (!new_bh) {
  2072. retval = ext4_add_entry (handle, new_dentry, old_inode);
  2073. if (retval)
  2074. goto end_rename;
  2075. } else {
  2076. BUFFER_TRACE(new_bh, "get write access");
  2077. ext4_journal_get_write_access(handle, new_bh);
  2078. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2079. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2080. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2081. new_de->file_type = old_de->file_type;
  2082. new_dir->i_version++;
  2083. BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata");
  2084. ext4_journal_dirty_metadata(handle, new_bh);
  2085. brelse(new_bh);
  2086. new_bh = NULL;
  2087. }
  2088. /*
  2089. * Like most other Unix systems, set the ctime for inodes on a
  2090. * rename.
  2091. */
  2092. old_inode->i_ctime = CURRENT_TIME_SEC;
  2093. ext4_mark_inode_dirty(handle, old_inode);
  2094. /*
  2095. * ok, that's it
  2096. */
  2097. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2098. old_de->name_len != old_dentry->d_name.len ||
  2099. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2100. (retval = ext4_delete_entry(handle, old_dir,
  2101. old_de, old_bh)) == -ENOENT) {
  2102. /* old_de could have moved from under us during htree split, so
  2103. * make sure that we are deleting the right entry. We might
  2104. * also be pointing to a stale entry in the unused part of
  2105. * old_bh so just checking inum and the name isn't enough. */
  2106. struct buffer_head *old_bh2;
  2107. struct ext4_dir_entry_2 *old_de2;
  2108. old_bh2 = ext4_find_entry(old_dentry, &old_de2);
  2109. if (old_bh2) {
  2110. retval = ext4_delete_entry(handle, old_dir,
  2111. old_de2, old_bh2);
  2112. brelse(old_bh2);
  2113. }
  2114. }
  2115. if (retval) {
  2116. ext4_warning(old_dir->i_sb, "ext4_rename",
  2117. "Deleting old file (%lu), %d, error=%d",
  2118. old_dir->i_ino, old_dir->i_nlink, retval);
  2119. }
  2120. if (new_inode) {
  2121. drop_nlink(new_inode);
  2122. new_inode->i_ctime = CURRENT_TIME_SEC;
  2123. }
  2124. old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
  2125. ext4_update_dx_flag(old_dir);
  2126. if (dir_bh) {
  2127. BUFFER_TRACE(dir_bh, "get_write_access");
  2128. ext4_journal_get_write_access(handle, dir_bh);
  2129. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2130. BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata");
  2131. ext4_journal_dirty_metadata(handle, dir_bh);
  2132. drop_nlink(old_dir);
  2133. if (new_inode) {
  2134. drop_nlink(new_inode);
  2135. } else {
  2136. inc_nlink(new_dir);
  2137. ext4_update_dx_flag(new_dir);
  2138. ext4_mark_inode_dirty(handle, new_dir);
  2139. }
  2140. }
  2141. ext4_mark_inode_dirty(handle, old_dir);
  2142. if (new_inode) {
  2143. ext4_mark_inode_dirty(handle, new_inode);
  2144. if (!new_inode->i_nlink)
  2145. ext4_orphan_add(handle, new_inode);
  2146. }
  2147. retval = 0;
  2148. end_rename:
  2149. brelse (dir_bh);
  2150. brelse (old_bh);
  2151. brelse (new_bh);
  2152. ext4_journal_stop(handle);
  2153. return retval;
  2154. }
  2155. /*
  2156. * directories can handle most operations...
  2157. */
  2158. const struct inode_operations ext4_dir_inode_operations = {
  2159. .create = ext4_create,
  2160. .lookup = ext4_lookup,
  2161. .link = ext4_link,
  2162. .unlink = ext4_unlink,
  2163. .symlink = ext4_symlink,
  2164. .mkdir = ext4_mkdir,
  2165. .rmdir = ext4_rmdir,
  2166. .mknod = ext4_mknod,
  2167. .rename = ext4_rename,
  2168. .setattr = ext4_setattr,
  2169. #ifdef CONFIG_EXT4DEV_FS_XATTR
  2170. .setxattr = generic_setxattr,
  2171. .getxattr = generic_getxattr,
  2172. .listxattr = ext4_listxattr,
  2173. .removexattr = generic_removexattr,
  2174. #endif
  2175. .permission = ext4_permission,
  2176. };
  2177. const struct inode_operations ext4_special_inode_operations = {
  2178. .setattr = ext4_setattr,
  2179. #ifdef CONFIG_EXT4DEV_FS_XATTR
  2180. .setxattr = generic_setxattr,
  2181. .getxattr = generic_getxattr,
  2182. .listxattr = ext4_listxattr,
  2183. .removexattr = generic_removexattr,
  2184. #endif
  2185. .permission = ext4_permission,
  2186. };