namei.c 64 KB

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