namei.c 67 KB

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