namei.c 64 KB

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