namei.c 68 KB

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